blob: add4254eb850ea64d71a8867d84a6b3d11bc2abf [file] [log] [blame]
Tomas Winkler19838fb2012-11-01 21:17:15 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/fs.h>
19#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/fcntl.h>
22#include <linux/aio.h>
23#include <linux/pci.h>
24#include <linux/init.h>
25#include <linux/ioctl.h>
26#include <linux/cdev.h>
27#include <linux/list.h>
28#include <linux/delay.h>
29#include <linux/sched.h>
30#include <linux/uuid.h>
31#include <linux/jiffies.h>
32#include <linux/uaccess.h>
33
Tomas Winkler47a73802012-12-25 19:06:03 +020034#include <linux/mei.h>
Tomas Winkler19838fb2012-11-01 21:17:15 +020035
36#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020037#include "hbm.h"
Tomas Winkler19838fb2012-11-01 21:17:15 +020038#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 */
Tomas Winklerff8b2f42012-11-11 17:38:03 +020076 i = mei_me_cl_link(dev, &dev->iamthif_cl,
Tomas Winkler19838fb2012-11-01 21:17:15 +020077 &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
78 if (i < 0) {
Tomas Winklerff8b2f42012-11-11 17:38:03 +020079 dev_info(&dev->pdev->dev, "failed to find iamthif client.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +020080 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
Tomas Winkler8120e722012-12-25 19:06:11 +0200102 if (mei_hbm_cl_connect_req(dev, &dev->iamthif_cl)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200103 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{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200122 struct mei_cl_cb *pos = NULL;
123 struct mei_cl_cb *next = NULL;
124
125 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200126 &dev->amthif_rd_complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200127 if (pos->cl && pos->cl == &dev->iamthif_cl &&
Tomas Winkler19838fb2012-11-01 21:17:15 +0200128 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/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200256 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200257 *
258 * @dev: the device structure
259 * @cb: mei call back struct
260 *
261 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200262 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200263 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200264static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200265{
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,
Tomas Winkler438763f2012-12-25 19:05:59 +0200303 (unsigned char *)dev->iamthif_msg_buf))
Tomas Winkler19838fb2012-11-01 21:17:15 +0200304 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
329/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200330 * 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
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200349 cb->fop_type = MEI_FOP_IOCTL;
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200350
Tomas Winklere773efc2012-11-11 17:37:58 +0200351 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200352 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");
Tomas Winklere773efc2012-11-11 17:37:58 +0200356 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200357 return 0;
358 }
359 return mei_amthif_send_cmd(dev, cb);
360}
361/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200362 * 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{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200370 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
Tomas Winklere773efc2012-11-11 17:37:58 +0200387 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200388 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200389
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200390 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200391 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200392 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
Tomas Winkler744f0f22012-11-11 17:38:02 +0200403
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
Tomas Winkler19838fb2012-11-01 21:17:15 +0200422/**
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 */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200433int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
434 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200435{
Tomas Winklere46f1872012-12-25 19:06:10 +0200436 struct mei_msg_hdr mei_hdr;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200437 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);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200440
Tomas Winklere46f1872012-12-25 19:06:10 +0200441 mei_hdr.host_addr = cl->host_client_id;
442 mei_hdr.me_addr = cl->me_client_id;
443 mei_hdr.reserved = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200444
445 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200446 mei_hdr.length = len;
447 mei_hdr.msg_complete = 1;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200448 /* 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);
Tomas Winklere46f1872012-12-25 19:06:10 +0200452 mei_hdr.length = len;
453 mei_hdr.msg_complete = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200454 } else {
455 /* wait for next time the host buffer is empty */
456 return 0;
457 }
Tomas Winkler19838fb2012-11-01 21:17:15 +0200458
Tomas Winklere46f1872012-12-25 19:06:10 +0200459 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200460
Tomas Winkler24c656e2012-11-18 15:13:17 +0200461 *slots -= msg_slots;
Tomas Winklere46f1872012-12-25 19:06:10 +0200462 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200463 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200464 dev->iamthif_state = MEI_IAMTHIF_IDLE;
465 cl->status = -ENODEV;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200466 list_del(&cb->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200467 return -ENODEV;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200468 }
469
Tomas Winkler24c656e2012-11-18 15:13:17 +0200470 if (mei_flow_ctrl_reduce(dev, cl))
471 return -ENODEV;
472
Tomas Winklere46f1872012-12-25 19:06:10 +0200473 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200474 cl->status = 0;
475
Tomas Winklere46f1872012-12-25 19:06:10 +0200476 if (mei_hdr.msg_complete) {
Tomas Winkler24c656e2012-11-18 15:13:17 +0200477 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);
485 }
486
487
Tomas Winkler19838fb2012-11-01 21:17:15 +0200488 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{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200504 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
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200531 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200532 return -ENODEV;
533
534 dev->iamthif_stall_timer = 0;
535 cb->buf_idx = dev->iamthif_msg_buf_index;
536 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200537 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200538 /* 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));
Tomas Winkler8120e722012-12-25 19:06:11 +0200562 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200563 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);
Tomas Winklere773efc2012-11-11 17:37:58 +0200591 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200592 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
Tomas Winklera562d5c2012-11-11 17:38:01 +0200604/**
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;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200623
Tomas Winklera562d5c2012-11-11 17:38:01 +0200624 /* 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 */
Tomas Winkler8120e722012-12-25 19:06:11 +0200634 mei_hbm_cl_flow_control_req(dev,
635 &dev->iamthif_cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200636 }
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;
665
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}