blob: 88e6aa0f6b4eb5e9cf01f4ea7153ee794d2d3d84 [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 Winkler9dc64d62013-01-08 23:07:17 +020038#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020039#include "client.h"
Tomas Winkler19838fb2012-11-01 21:17:15 +020040
Tomas Winkler1a1aca42013-01-08 23:07:21 +020041const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
42 0xac, 0xa8, 0x46, 0xe0,
43 0xff, 0x65, 0x81, 0x4c);
Tomas Winkler19838fb2012-11-01 21:17:15 +020044
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;
60}
61
62/**
63 * mei_amthif_host_init_ - mei initialization amthif client.
64 *
65 * @dev: the device structure
66 *
67 */
Tomas Winkler781d0d82013-01-08 23:07:22 +020068int mei_amthif_host_init(struct mei_device *dev)
Tomas Winkler19838fb2012-11-01 21:17:15 +020069{
Tomas Winkler781d0d82013-01-08 23:07:22 +020070 struct mei_cl *cl = &dev->iamthif_cl;
Tomas Winkler19838fb2012-11-01 21:17:15 +020071 unsigned char *msg_buf;
Tomas Winkler781d0d82013-01-08 23:07:22 +020072 int ret, i;
Tomas Winkler19838fb2012-11-01 21:17:15 +020073
Tomas Winkler781d0d82013-01-08 23:07:22 +020074 mei_cl_init(cl, dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +020075
Tomas Winkler781d0d82013-01-08 23:07:22 +020076 i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
Tomas Winkler19838fb2012-11-01 21:17:15 +020077 if (i < 0) {
Tomas Winkler781d0d82013-01-08 23:07:22 +020078 dev_info(&dev->pdev->dev, "amthif: failed to find the client\n");
79 return -ENOENT;
Tomas Winkler19838fb2012-11-01 21:17:15 +020080 }
81
Tomas Winkler781d0d82013-01-08 23:07:22 +020082 cl->me_client_id = dev->me_clients[i].client_id;
83
Tomas Winkler19838fb2012-11-01 21:17:15 +020084 /* Assign iamthif_mtu to the value received from ME */
85
86 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
87 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
88 dev->me_clients[i].props.max_msg_length);
89
90 kfree(dev->iamthif_msg_buf);
91 dev->iamthif_msg_buf = NULL;
92
93 /* allocate storage for ME message buffer */
94 msg_buf = kcalloc(dev->iamthif_mtu,
95 sizeof(unsigned char), GFP_KERNEL);
96 if (!msg_buf) {
Tomas Winkler781d0d82013-01-08 23:07:22 +020097 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
98 return -ENOMEM;
Tomas Winkler19838fb2012-11-01 21:17:15 +020099 }
100
101 dev->iamthif_msg_buf = msg_buf;
102
Tomas Winkler781d0d82013-01-08 23:07:22 +0200103 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
104
105 if (ret < 0) {
106 dev_err(&dev->pdev->dev, "amthif: failed link client\n");
107 return -ENOENT;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200108 }
Tomas Winkler781d0d82013-01-08 23:07:22 +0200109
110 cl->state = MEI_FILE_CONNECTING;
111
112 if (mei_hbm_cl_connect_req(dev, cl)) {
113 dev_dbg(&dev->pdev->dev, "amthif: Failed to connect to ME client\n");
114 cl->state = MEI_FILE_DISCONNECTED;
115 cl->host_client_id = 0;
116 } else {
117 cl->timer_count = MEI_CONNECT_TIMEOUT;
118 }
119 return 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200120}
121
122/**
123 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
124 *
125 * @dev: the device structure
126 * @file: pointer to file object
127 *
128 * returns returned a list entry on success, NULL on failure.
129 */
130struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
131 struct file *file)
132{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200133 struct mei_cl_cb *pos = NULL;
134 struct mei_cl_cb *next = NULL;
135
136 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200137 &dev->amthif_rd_complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200138 if (pos->cl && pos->cl == &dev->iamthif_cl &&
Tomas Winkler19838fb2012-11-01 21:17:15 +0200139 pos->file_object == file)
140 return pos;
141 }
142 return NULL;
143}
144
145
146/**
147 * mei_amthif_read - read data from AMTHIF client
148 *
149 * @dev: the device structure
150 * @if_num: minor number
151 * @file: pointer to file object
152 * @*ubuf: pointer to user data in user space
153 * @length: data length to read
154 * @offset: data read offset
155 *
156 * Locking: called under "dev->device_lock" lock
157 *
158 * returns
159 * returned data length on success,
160 * zero if no data to read,
161 * negative on failure.
162 */
163int mei_amthif_read(struct mei_device *dev, struct file *file,
164 char __user *ubuf, size_t length, loff_t *offset)
165{
166 int rets;
167 int wait_ret;
168 struct mei_cl_cb *cb = NULL;
169 struct mei_cl *cl = file->private_data;
170 unsigned long timeout;
171 int i;
172
173 /* Only Posible if we are in timeout */
174 if (!cl || cl != &dev->iamthif_cl) {
175 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
176 return -ETIMEDOUT;
177 }
178
179 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
180
181 if (i < 0) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200182 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200183 return -ENODEV;
184 }
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200185 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200186 cb = mei_amthif_find_read_list_entry(dev, file);
187
188 /* Check for if we can block or not*/
189 if (cb == NULL && file->f_flags & O_NONBLOCK)
190 return -EAGAIN;
191
192
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200193 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200194 while (cb == NULL) {
195 /* unlock the Mutex */
196 mutex_unlock(&dev->device_lock);
197
198 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
199 (cb = mei_amthif_find_read_list_entry(dev, file)));
200
201 if (wait_ret)
202 return -ERESTARTSYS;
203
204 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
205
206 /* Locking again the Mutex */
207 mutex_lock(&dev->device_lock);
208 }
209
210
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200211 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200212 dev->iamthif_timer = 0;
213
214 if (cb) {
215 timeout = cb->read_time +
216 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200217 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200218 timeout);
219
220 if (time_after(jiffies, timeout)) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200221 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200222 /* 15 sec for the message has expired */
223 list_del(&cb->list);
224 rets = -ETIMEDOUT;
225 goto free;
226 }
227 }
228 /* if the whole message will fit remove it from the list */
229 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
230 list_del(&cb->list);
231 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
232 /* end of the message has been reached */
233 list_del(&cb->list);
234 rets = 0;
235 goto free;
236 }
237 /* else means that not full buffer will be read and do not
238 * remove message from deletion list
239 */
240
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200241 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200242 cb->response_buffer.size);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200243 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200244
245 /* length is being turncated to PAGE_SIZE, however,
246 * the buf_idx may point beyond */
247 length = min_t(size_t, length, (cb->buf_idx - *offset));
248
249 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
250 rets = -EFAULT;
251 else {
252 rets = length;
253 if ((*offset + length) < cb->buf_idx) {
254 *offset += length;
255 goto out;
256 }
257 }
258free:
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200259 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200260 *offset = 0;
261 mei_io_cb_free(cb);
262out:
263 return rets;
264}
265
266/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200267 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200268 *
269 * @dev: the device structure
270 * @cb: mei call back struct
271 *
272 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200273 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200274 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200275static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200276{
277 struct mei_msg_hdr mei_hdr;
278 int ret;
279
280 if (!dev || !cb)
281 return -ENODEV;
282
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200283 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200284
285 dev->iamthif_state = MEI_IAMTHIF_WRITING;
286 dev->iamthif_current_cb = cb;
287 dev->iamthif_file_object = cb->file_object;
288 dev->iamthif_canceled = false;
289 dev->iamthif_ioctl = true;
290 dev->iamthif_msg_buf_size = cb->request_buffer.size;
291 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
292 cb->request_buffer.size);
293
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200294 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200295 if (ret < 0)
296 return ret;
297
298 if (ret && dev->mei_host_buffer_is_empty) {
299 ret = 0;
300 dev->mei_host_buffer_is_empty = false;
301 if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
302 mei_hdr.length = mei_hbuf_max_data(dev);
303 mei_hdr.msg_complete = 0;
304 } else {
305 mei_hdr.length = cb->request_buffer.size;
306 mei_hdr.msg_complete = 1;
307 }
308
309 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
310 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
311 mei_hdr.reserved = 0;
312 dev->iamthif_msg_buf_index += mei_hdr.length;
313 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200314 (unsigned char *)dev->iamthif_msg_buf))
Tomas Winkler19838fb2012-11-01 21:17:15 +0200315 return -ENODEV;
316
317 if (mei_hdr.msg_complete) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200318 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
Tomas Winkler19838fb2012-11-01 21:17:15 +0200319 return -ENODEV;
320 dev->iamthif_flow_control_pending = true;
321 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200322 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200323 dev->iamthif_current_cb = cb;
324 dev->iamthif_file_object = cb->file_object;
325 list_add_tail(&cb->list, &dev->write_waiting_list.list);
326 } else {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200327 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200328 list_add_tail(&cb->list, &dev->write_list.list);
329 }
330 } else {
331 if (!(dev->mei_host_buffer_is_empty))
332 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
333
334 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
335 list_add_tail(&cb->list, &dev->write_list.list);
336 }
337 return 0;
338}
339
340/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200341 * mei_amthif_write - write amthif data to amthif client
342 *
343 * @dev: the device structure
344 * @cb: mei call back struct
345 *
346 * returns 0 on success, <0 on failure.
347 *
348 */
349int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
350{
351 int ret;
352
353 if (!dev || !cb)
354 return -ENODEV;
355
356 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
357 if (ret)
358 return ret;
359
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200360 cb->fop_type = MEI_FOP_IOCTL;
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200361
Tomas Winklere773efc2012-11-11 17:37:58 +0200362 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200363 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
364 dev_dbg(&dev->pdev->dev,
365 "amthif state = %d\n", dev->iamthif_state);
366 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200367 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200368 return 0;
369 }
370 return mei_amthif_send_cmd(dev, cb);
371}
372/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200373 * mei_amthif_run_next_cmd
374 *
375 * @dev: the device structure
376 *
377 * returns 0 on success, <0 on failure.
378 */
379void mei_amthif_run_next_cmd(struct mei_device *dev)
380{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200381 struct mei_cl_cb *pos = NULL;
382 struct mei_cl_cb *next = NULL;
383 int status;
384
385 if (!dev)
386 return;
387
388 dev->iamthif_msg_buf_size = 0;
389 dev->iamthif_msg_buf_index = 0;
390 dev->iamthif_canceled = false;
391 dev->iamthif_ioctl = true;
392 dev->iamthif_state = MEI_IAMTHIF_IDLE;
393 dev->iamthif_timer = 0;
394 dev->iamthif_file_object = NULL;
395
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200396 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200397
Tomas Winklere773efc2012-11-11 17:37:58 +0200398 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200399 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200400
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200401 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200402 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200403 if (status) {
404 dev_dbg(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200405 "amthif write failed status = %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200406 status);
407 return;
408 }
409 break;
410 }
411 }
412}
413
Tomas Winkler744f0f22012-11-11 17:38:02 +0200414
415unsigned int mei_amthif_poll(struct mei_device *dev,
416 struct file *file, poll_table *wait)
417{
418 unsigned int mask = 0;
419 mutex_unlock(&dev->device_lock);
420 poll_wait(file, &dev->iamthif_cl.wait, wait);
421 mutex_lock(&dev->device_lock);
422 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
423 dev->iamthif_file_object == file) {
424 mask |= (POLLIN | POLLRDNORM);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200425 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
Tomas Winkler744f0f22012-11-11 17:38:02 +0200426 mei_amthif_run_next_cmd(dev);
427 }
428 return mask;
429}
430
431
432
Tomas Winkler19838fb2012-11-01 21:17:15 +0200433/**
434 * mei_amthif_irq_process_completed - processes completed iamthif operation.
435 *
436 * @dev: the device structure.
437 * @slots: free slots.
438 * @cb_pos: callback block.
439 * @cl: private data of the file object.
440 * @cmpl_list: complete list.
441 *
442 * returns 0, OK; otherwise, error.
443 */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200444int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
445 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200446{
Tomas Winklere46f1872012-12-25 19:06:10 +0200447 struct mei_msg_hdr mei_hdr;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200448 struct mei_cl *cl = cb->cl;
449 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
450 size_t msg_slots = mei_data2slots(len);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200451
Tomas Winklere46f1872012-12-25 19:06:10 +0200452 mei_hdr.host_addr = cl->host_client_id;
453 mei_hdr.me_addr = cl->me_client_id;
454 mei_hdr.reserved = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200455
456 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200457 mei_hdr.length = len;
458 mei_hdr.msg_complete = 1;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200459 /* Split the message only if we can write the whole host buffer */
460 } else if (*slots == dev->hbuf_depth) {
461 msg_slots = *slots;
462 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200463 mei_hdr.length = len;
464 mei_hdr.msg_complete = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200465 } else {
466 /* wait for next time the host buffer is empty */
467 return 0;
468 }
Tomas Winkler19838fb2012-11-01 21:17:15 +0200469
Tomas Winklere46f1872012-12-25 19:06:10 +0200470 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200471
Tomas Winkler24c656e2012-11-18 15:13:17 +0200472 *slots -= msg_slots;
Tomas Winklere46f1872012-12-25 19:06:10 +0200473 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200474 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200475 dev->iamthif_state = MEI_IAMTHIF_IDLE;
476 cl->status = -ENODEV;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200477 list_del(&cb->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200478 return -ENODEV;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200479 }
480
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200481 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler24c656e2012-11-18 15:13:17 +0200482 return -ENODEV;
483
Tomas Winklere46f1872012-12-25 19:06:10 +0200484 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200485 cl->status = 0;
486
Tomas Winklere46f1872012-12-25 19:06:10 +0200487 if (mei_hdr.msg_complete) {
Tomas Winkler24c656e2012-11-18 15:13:17 +0200488 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
489 dev->iamthif_flow_control_pending = true;
490
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200491 /* save iamthif cb sent to amthif client */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200492 cb->buf_idx = dev->iamthif_msg_buf_index;
493 dev->iamthif_current_cb = cb;
494
495 list_move_tail(&cb->list, &dev->write_waiting_list.list);
496 }
497
498
Tomas Winkler19838fb2012-11-01 21:17:15 +0200499 return 0;
500}
501
502/**
503 * mei_amthif_irq_read_message - read routine after ISR to
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200504 * handle the read amthif message
Tomas Winkler19838fb2012-11-01 21:17:15 +0200505 *
506 * @complete_list: An instance of our list structure
507 * @dev: the device structure
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200508 * @mei_hdr: header of amthif message
Tomas Winkler19838fb2012-11-01 21:17:15 +0200509 *
510 * returns 0 on success, <0 on failure.
511 */
512int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
513 struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
514{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200515 struct mei_cl_cb *cb;
516 unsigned char *buffer;
517
518 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
519 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
520
521 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
522 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
523
524 mei_read_slots(dev, buffer, mei_hdr->length);
525
526 dev->iamthif_msg_buf_index += mei_hdr->length;
527
528 if (!mei_hdr->msg_complete)
529 return 0;
530
531 dev_dbg(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200532 "amthif_message_buffer_index =%d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200533 mei_hdr->length);
534
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200535 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200536 if (!dev->iamthif_current_cb)
537 return -ENODEV;
538
539 cb = dev->iamthif_current_cb;
540 dev->iamthif_current_cb = NULL;
541
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200542 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200543 return -ENODEV;
544
545 dev->iamthif_stall_timer = 0;
546 cb->buf_idx = dev->iamthif_msg_buf_index;
547 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200548 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200549 /* found the iamthif cb */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200550 dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
551 dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200552 list_add_tail(&cb->list, &complete_list->list);
553 }
554 return 0;
555}
556
557/**
558 * mei_amthif_irq_read - prepares to read amthif data.
559 *
560 * @dev: the device structure.
561 * @slots: free slots.
562 *
563 * returns 0, OK; otherwise, error.
564 */
565int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
566{
567
568 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
569 + sizeof(struct hbm_flow_control))) {
570 return -EMSGSIZE;
571 }
572 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
Tomas Winkler8120e722012-12-25 19:06:11 +0200573 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200574 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
575 return -EIO;
576 }
577
578 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
579 dev->iamthif_state = MEI_IAMTHIF_READING;
580 dev->iamthif_flow_control_pending = false;
581 dev->iamthif_msg_buf_index = 0;
582 dev->iamthif_msg_buf_size = 0;
583 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
584 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
585 return 0;
586}
587
588/**
589 * mei_amthif_complete - complete amthif callback.
590 *
591 * @dev: the device structure.
592 * @cb_pos: callback block.
593 */
594void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
595{
596 if (dev->iamthif_canceled != 1) {
597 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
598 dev->iamthif_stall_timer = 0;
599 memcpy(cb->response_buffer.data,
600 dev->iamthif_msg_buf,
601 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200602 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200603 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200604 dev->iamthif_timer = jiffies;
605 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
606 dev->iamthif_timer);
607 } else {
608 mei_amthif_run_next_cmd(dev);
609 }
610
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200611 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200612 wake_up_interruptible(&dev->iamthif_cl.wait);
613}
614
Tomas Winklera562d5c2012-11-11 17:38:01 +0200615/**
616 * mei_clear_list - removes all callbacks associated with file
617 * from mei_cb_list
618 *
619 * @dev: device structure.
620 * @file: file structure
621 * @mei_cb_list: callbacks list
622 *
623 * mei_clear_list is called to clear resources associated with file
624 * when application calls close function or Ctrl-C was pressed
625 *
626 * returns true if callback removed from the list, false otherwise
627 */
628static bool mei_clear_list(struct mei_device *dev,
629 const struct file *file, struct list_head *mei_cb_list)
630{
631 struct mei_cl_cb *cb_pos = NULL;
632 struct mei_cl_cb *cb_next = NULL;
633 bool removed = false;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200634
Tomas Winklera562d5c2012-11-11 17:38:01 +0200635 /* list all list member */
636 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
637 /* check if list member associated with a file */
638 if (file == cb_pos->file_object) {
639 /* remove member from the list */
640 list_del(&cb_pos->list);
641 /* check if cb equal to current iamthif cb */
642 if (dev->iamthif_current_cb == cb_pos) {
643 dev->iamthif_current_cb = NULL;
644 /* send flow control to iamthif client */
Tomas Winkler8120e722012-12-25 19:06:11 +0200645 mei_hbm_cl_flow_control_req(dev,
646 &dev->iamthif_cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200647 }
648 /* free all allocated buffers */
649 mei_io_cb_free(cb_pos);
650 cb_pos = NULL;
651 removed = true;
652 }
653 }
654 return removed;
655}
656
657/**
658 * mei_clear_lists - removes all callbacks associated with file
659 *
660 * @dev: device structure
661 * @file: file structure
662 *
663 * mei_clear_lists is called to clear resources associated with file
664 * when application calls close function or Ctrl-C was pressed
665 *
666 * returns true if callback removed from the list, false otherwise
667 */
668static bool mei_clear_lists(struct mei_device *dev, struct file *file)
669{
670 bool removed = false;
671
672 /* remove callbacks associated with a file */
673 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
674 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
675 removed = true;
676
677 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
678
679 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
680 removed = true;
681
682 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
683 removed = true;
684
685 if (mei_clear_list(dev, file, &dev->write_list.list))
686 removed = true;
687
688 /* check if iamthif_current_cb not NULL */
689 if (dev->iamthif_current_cb && !removed) {
690 /* check file and iamthif current cb association */
691 if (dev->iamthif_current_cb->file_object == file) {
692 /* remove cb */
693 mei_io_cb_free(dev->iamthif_current_cb);
694 dev->iamthif_current_cb = NULL;
695 removed = true;
696 }
697 }
698 return removed;
699}
700
701/**
702* mei_amthif_release - the release function
703*
704* @inode: pointer to inode structure
705* @file: pointer to file structure
706*
707* returns 0 on success, <0 on error
708*/
709int mei_amthif_release(struct mei_device *dev, struct file *file)
710{
711 if (dev->open_handle_count > 0)
712 dev->open_handle_count--;
713
714 if (dev->iamthif_file_object == file &&
715 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
716
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200717 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
Tomas Winklera562d5c2012-11-11 17:38:01 +0200718 dev->iamthif_state);
719 dev->iamthif_canceled = true;
720 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200721 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
Tomas Winklera562d5c2012-11-11 17:38:01 +0200722 mei_amthif_run_next_cmd(dev);
723 }
724 }
725
726 if (mei_clear_lists(dev, file))
727 dev->iamthif_state = MEI_IAMTHIF_IDLE;
728
729 return 0;
730}