blob: 04fd3856772975b0c9061f24e56777e8232ed615 [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/**
Masanari Iida393b1482013-04-05 01:05:05 +090063 * mei_amthif_host_init - mei initialization amthif client.
Tomas Winkler19838fb2012-11-01 21:17:15 +020064 *
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 Winkler6222f7b2013-01-08 23:07:23 +020074 dev->iamthif_state = MEI_IAMTHIF_IDLE;
75
Tomas Winkler781d0d82013-01-08 23:07:22 +020076 mei_cl_init(cl, dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +020077
Tomas Winkler781d0d82013-01-08 23:07:22 +020078 i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
Tomas Winkler19838fb2012-11-01 21:17:15 +020079 if (i < 0) {
Tomas Winkler781d0d82013-01-08 23:07:22 +020080 dev_info(&dev->pdev->dev, "amthif: failed to find the client\n");
81 return -ENOENT;
Tomas Winkler19838fb2012-11-01 21:17:15 +020082 }
83
Tomas Winkler781d0d82013-01-08 23:07:22 +020084 cl->me_client_id = dev->me_clients[i].client_id;
85
Tomas Winkler19838fb2012-11-01 21:17:15 +020086 /* Assign iamthif_mtu to the value received from ME */
87
88 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
89 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
90 dev->me_clients[i].props.max_msg_length);
91
92 kfree(dev->iamthif_msg_buf);
93 dev->iamthif_msg_buf = NULL;
94
95 /* allocate storage for ME message buffer */
96 msg_buf = kcalloc(dev->iamthif_mtu,
97 sizeof(unsigned char), GFP_KERNEL);
98 if (!msg_buf) {
Tomas Winkler781d0d82013-01-08 23:07:22 +020099 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
100 return -ENOMEM;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200101 }
102
103 dev->iamthif_msg_buf = msg_buf;
104
Tomas Winkler781d0d82013-01-08 23:07:22 +0200105 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
106
107 if (ret < 0) {
108 dev_err(&dev->pdev->dev, "amthif: failed link client\n");
109 return -ENOENT;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200110 }
Tomas Winkler781d0d82013-01-08 23:07:22 +0200111
112 cl->state = MEI_FILE_CONNECTING;
113
114 if (mei_hbm_cl_connect_req(dev, cl)) {
115 dev_dbg(&dev->pdev->dev, "amthif: Failed to connect to ME client\n");
116 cl->state = MEI_FILE_DISCONNECTED;
117 cl->host_client_id = 0;
118 } else {
119 cl->timer_count = MEI_CONNECT_TIMEOUT;
120 }
121 return 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200122}
123
124/**
125 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
126 *
127 * @dev: the device structure
128 * @file: pointer to file object
129 *
130 * returns returned a list entry on success, NULL on failure.
131 */
132struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
133 struct file *file)
134{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200135 struct mei_cl_cb *pos = NULL;
136 struct mei_cl_cb *next = NULL;
137
138 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200139 &dev->amthif_rd_complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200140 if (pos->cl && pos->cl == &dev->iamthif_cl &&
Tomas Winkler19838fb2012-11-01 21:17:15 +0200141 pos->file_object == file)
142 return pos;
143 }
144 return NULL;
145}
146
147
148/**
149 * mei_amthif_read - read data from AMTHIF client
150 *
151 * @dev: the device structure
152 * @if_num: minor number
153 * @file: pointer to file object
154 * @*ubuf: pointer to user data in user space
155 * @length: data length to read
156 * @offset: data read offset
157 *
158 * Locking: called under "dev->device_lock" lock
159 *
160 * returns
161 * returned data length on success,
162 * zero if no data to read,
163 * negative on failure.
164 */
165int mei_amthif_read(struct mei_device *dev, struct file *file,
166 char __user *ubuf, size_t length, loff_t *offset)
167{
168 int rets;
169 int wait_ret;
170 struct mei_cl_cb *cb = NULL;
171 struct mei_cl *cl = file->private_data;
172 unsigned long timeout;
173 int i;
174
175 /* Only Posible if we are in timeout */
176 if (!cl || cl != &dev->iamthif_cl) {
177 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
178 return -ETIMEDOUT;
179 }
180
181 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
182
183 if (i < 0) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200184 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200185 return -ENODEV;
186 }
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200187 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200188 cb = mei_amthif_find_read_list_entry(dev, file);
189
190 /* Check for if we can block or not*/
191 if (cb == NULL && file->f_flags & O_NONBLOCK)
192 return -EAGAIN;
193
194
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200195 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200196 while (cb == NULL) {
197 /* unlock the Mutex */
198 mutex_unlock(&dev->device_lock);
199
200 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
201 (cb = mei_amthif_find_read_list_entry(dev, file)));
202
Alexey Khoroshilove6028db2012-12-22 01:44:16 +0400203 /* Locking again the Mutex */
204 mutex_lock(&dev->device_lock);
205
Tomas Winkler19838fb2012-11-01 21:17:15 +0200206 if (wait_ret)
207 return -ERESTARTSYS;
208
209 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200210 }
211
212
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200213 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200214 dev->iamthif_timer = 0;
215
216 if (cb) {
217 timeout = cb->read_time +
218 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200219 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200220 timeout);
221
222 if (time_after(jiffies, timeout)) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200223 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200224 /* 15 sec for the message has expired */
225 list_del(&cb->list);
226 rets = -ETIMEDOUT;
227 goto free;
228 }
229 }
230 /* if the whole message will fit remove it from the list */
231 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
232 list_del(&cb->list);
233 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
234 /* end of the message has been reached */
235 list_del(&cb->list);
236 rets = 0;
237 goto free;
238 }
239 /* else means that not full buffer will be read and do not
240 * remove message from deletion list
241 */
242
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200243 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200244 cb->response_buffer.size);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200245 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200246
247 /* length is being turncated to PAGE_SIZE, however,
248 * the buf_idx may point beyond */
249 length = min_t(size_t, length, (cb->buf_idx - *offset));
250
251 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
252 rets = -EFAULT;
253 else {
254 rets = length;
255 if ((*offset + length) < cb->buf_idx) {
256 *offset += length;
257 goto out;
258 }
259 }
260free:
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200261 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200262 *offset = 0;
263 mei_io_cb_free(cb);
264out:
265 return rets;
266}
267
268/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200269 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200270 *
271 * @dev: the device structure
272 * @cb: mei call back struct
273 *
274 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200275 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200276 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200277static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200278{
279 struct mei_msg_hdr mei_hdr;
280 int ret;
281
282 if (!dev || !cb)
283 return -ENODEV;
284
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200285 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200286
287 dev->iamthif_state = MEI_IAMTHIF_WRITING;
288 dev->iamthif_current_cb = cb;
289 dev->iamthif_file_object = cb->file_object;
290 dev->iamthif_canceled = false;
291 dev->iamthif_ioctl = true;
292 dev->iamthif_msg_buf_size = cb->request_buffer.size;
293 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
294 cb->request_buffer.size);
295
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200296 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200297 if (ret < 0)
298 return ret;
299
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200300 if (ret && dev->hbuf_is_ready) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200301 ret = 0;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200302 dev->hbuf_is_ready = false;
Tomas Winkler827eef52013-02-06 14:06:41 +0200303 if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
304 mei_hdr.length = mei_hbuf_max_len(dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200305 mei_hdr.msg_complete = 0;
306 } else {
307 mei_hdr.length = cb->request_buffer.size;
308 mei_hdr.msg_complete = 1;
309 }
310
311 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
312 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
313 mei_hdr.reserved = 0;
314 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300315 ret = mei_write_message(dev, &mei_hdr, dev->iamthif_msg_buf);
316 if (ret)
317 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200318
319 if (mei_hdr.msg_complete) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200320 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300321 return -EIO;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200322 dev->iamthif_flow_control_pending = true;
323 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200324 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200325 dev->iamthif_current_cb = cb;
326 dev->iamthif_file_object = cb->file_object;
327 list_add_tail(&cb->list, &dev->write_waiting_list.list);
328 } else {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200329 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200330 list_add_tail(&cb->list, &dev->write_list.list);
331 }
332 } else {
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200333 if (!dev->hbuf_is_ready)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200334 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
335
336 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
337 list_add_tail(&cb->list, &dev->write_list.list);
338 }
339 return 0;
340}
341
342/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200343 * mei_amthif_write - write amthif data to amthif client
344 *
345 * @dev: the device structure
346 * @cb: mei call back struct
347 *
348 * returns 0 on success, <0 on failure.
349 *
350 */
351int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
352{
353 int ret;
354
355 if (!dev || !cb)
356 return -ENODEV;
357
358 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
359 if (ret)
360 return ret;
361
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200362 cb->fop_type = MEI_FOP_IOCTL;
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200363
Tomas Winklere773efc2012-11-11 17:37:58 +0200364 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200365 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
366 dev_dbg(&dev->pdev->dev,
367 "amthif state = %d\n", dev->iamthif_state);
368 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200369 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200370 return 0;
371 }
372 return mei_amthif_send_cmd(dev, cb);
373}
374/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200375 * mei_amthif_run_next_cmd
376 *
377 * @dev: the device structure
378 *
379 * returns 0 on success, <0 on failure.
380 */
381void mei_amthif_run_next_cmd(struct mei_device *dev)
382{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200383 struct mei_cl_cb *pos = NULL;
384 struct mei_cl_cb *next = NULL;
385 int status;
386
387 if (!dev)
388 return;
389
390 dev->iamthif_msg_buf_size = 0;
391 dev->iamthif_msg_buf_index = 0;
392 dev->iamthif_canceled = false;
393 dev->iamthif_ioctl = true;
394 dev->iamthif_state = MEI_IAMTHIF_IDLE;
395 dev->iamthif_timer = 0;
396 dev->iamthif_file_object = NULL;
397
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200398 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200399
Tomas Winklere773efc2012-11-11 17:37:58 +0200400 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200401 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200402
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200403 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200404 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200405 if (status) {
406 dev_dbg(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200407 "amthif write failed status = %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200408 status);
409 return;
410 }
411 break;
412 }
413 }
414}
415
Tomas Winkler744f0f22012-11-11 17:38:02 +0200416
417unsigned int mei_amthif_poll(struct mei_device *dev,
418 struct file *file, poll_table *wait)
419{
420 unsigned int mask = 0;
Tomas Winklerb950ac12013-07-25 20:15:53 +0300421
Tomas Winkler744f0f22012-11-11 17:38:02 +0200422 poll_wait(file, &dev->iamthif_cl.wait, wait);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300423
Tomas Winkler744f0f22012-11-11 17:38:02 +0200424 mutex_lock(&dev->device_lock);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300425 if (!mei_cl_is_connected(&dev->iamthif_cl)) {
426
427 mask = POLLERR;
428
429 } else if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
430 dev->iamthif_file_object == file) {
431
Tomas Winkler744f0f22012-11-11 17:38:02 +0200432 mask |= (POLLIN | POLLRDNORM);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200433 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
Tomas Winkler744f0f22012-11-11 17:38:02 +0200434 mei_amthif_run_next_cmd(dev);
435 }
Tomas Winklerb950ac12013-07-25 20:15:53 +0300436 mutex_unlock(&dev->device_lock);
437
Tomas Winkler744f0f22012-11-11 17:38:02 +0200438 return mask;
439}
440
441
442
Tomas Winkler19838fb2012-11-01 21:17:15 +0200443/**
Masanari Iida393b1482013-04-05 01:05:05 +0900444 * mei_amthif_irq_write_completed - processes completed iamthif operation.
Tomas Winkler19838fb2012-11-01 21:17:15 +0200445 *
446 * @dev: the device structure.
447 * @slots: free slots.
448 * @cb_pos: callback block.
449 * @cl: private data of the file object.
450 * @cmpl_list: complete list.
451 *
452 * returns 0, OK; otherwise, error.
453 */
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300454int mei_amthif_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
455 s32 *slots, struct mei_cl_cb *cmpl_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200456{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300457 struct mei_device *dev = cl->dev;
Tomas Winklere46f1872012-12-25 19:06:10 +0200458 struct mei_msg_hdr mei_hdr;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200459 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200460 u32 msg_slots = mei_data2slots(len);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300461 int rets;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200462
Tomas Winkler136698e2013-09-16 23:44:44 +0300463 rets = mei_cl_flow_ctrl_creds(cl);
464 if (rets < 0)
465 return rets;
466
467 if (rets == 0) {
468 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
469 return 0;
470 }
471
Tomas Winklere46f1872012-12-25 19:06:10 +0200472 mei_hdr.host_addr = cl->host_client_id;
473 mei_hdr.me_addr = cl->me_client_id;
474 mei_hdr.reserved = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200475
476 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200477 mei_hdr.length = len;
478 mei_hdr.msg_complete = 1;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200479 /* Split the message only if we can write the whole host buffer */
480 } else if (*slots == dev->hbuf_depth) {
481 msg_slots = *slots;
482 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200483 mei_hdr.length = len;
484 mei_hdr.msg_complete = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200485 } else {
486 /* wait for next time the host buffer is empty */
487 return 0;
488 }
Tomas Winkler19838fb2012-11-01 21:17:15 +0200489
Tomas Winklere46f1872012-12-25 19:06:10 +0200490 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200491
Tomas Winkler24c656e2012-11-18 15:13:17 +0200492 *slots -= msg_slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300493 rets = mei_write_message(dev, &mei_hdr,
494 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index);
495 if (rets) {
496 dev->iamthif_state = MEI_IAMTHIF_IDLE;
497 cl->status = rets;
498 list_del(&cb->list);
499 return rets;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200500 }
501
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200502 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300503 return -EIO;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200504
Tomas Winklere46f1872012-12-25 19:06:10 +0200505 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200506 cl->status = 0;
507
Tomas Winklere46f1872012-12-25 19:06:10 +0200508 if (mei_hdr.msg_complete) {
Tomas Winkler24c656e2012-11-18 15:13:17 +0200509 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
510 dev->iamthif_flow_control_pending = true;
511
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200512 /* save iamthif cb sent to amthif client */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200513 cb->buf_idx = dev->iamthif_msg_buf_index;
514 dev->iamthif_current_cb = cb;
515
516 list_move_tail(&cb->list, &dev->write_waiting_list.list);
517 }
518
519
Tomas Winkler19838fb2012-11-01 21:17:15 +0200520 return 0;
521}
522
523/**
524 * mei_amthif_irq_read_message - read routine after ISR to
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200525 * handle the read amthif message
Tomas Winkler19838fb2012-11-01 21:17:15 +0200526 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200527 * @dev: the device structure
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200528 * @mei_hdr: header of amthif message
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300529 * @complete_list: An instance of our list structure
Tomas Winkler19838fb2012-11-01 21:17:15 +0200530 *
531 * returns 0 on success, <0 on failure.
532 */
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300533int mei_amthif_irq_read_msg(struct mei_device *dev,
534 struct mei_msg_hdr *mei_hdr,
535 struct mei_cl_cb *complete_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200536{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200537 struct mei_cl_cb *cb;
538 unsigned char *buffer;
539
540 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
541 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
542
543 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
544 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
545
546 mei_read_slots(dev, buffer, mei_hdr->length);
547
548 dev->iamthif_msg_buf_index += mei_hdr->length;
549
550 if (!mei_hdr->msg_complete)
551 return 0;
552
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300553 dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200554 mei_hdr->length);
555
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200556 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200557 if (!dev->iamthif_current_cb)
558 return -ENODEV;
559
560 cb = dev->iamthif_current_cb;
561 dev->iamthif_current_cb = NULL;
562
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200563 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200564 return -ENODEV;
565
566 dev->iamthif_stall_timer = 0;
567 cb->buf_idx = dev->iamthif_msg_buf_index;
568 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200569 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200570 /* found the iamthif cb */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200571 dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
572 dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200573 list_add_tail(&cb->list, &complete_list->list);
574 }
575 return 0;
576}
577
578/**
579 * mei_amthif_irq_read - prepares to read amthif data.
580 *
581 * @dev: the device structure.
582 * @slots: free slots.
583 *
584 * returns 0, OK; otherwise, error.
585 */
586int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
587{
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200588 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200589
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200590 if (*slots < msg_slots)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200591 return -EMSGSIZE;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200592
593 *slots -= msg_slots;
594
Tomas Winkler8120e722012-12-25 19:06:11 +0200595 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200596 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
597 return -EIO;
598 }
599
600 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
601 dev->iamthif_state = MEI_IAMTHIF_READING;
602 dev->iamthif_flow_control_pending = false;
603 dev->iamthif_msg_buf_index = 0;
604 dev->iamthif_msg_buf_size = 0;
605 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200606 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200607 return 0;
608}
609
610/**
611 * mei_amthif_complete - complete amthif callback.
612 *
613 * @dev: the device structure.
614 * @cb_pos: callback block.
615 */
616void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
617{
618 if (dev->iamthif_canceled != 1) {
619 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
620 dev->iamthif_stall_timer = 0;
621 memcpy(cb->response_buffer.data,
622 dev->iamthif_msg_buf,
623 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200624 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200625 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200626 dev->iamthif_timer = jiffies;
627 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
628 dev->iamthif_timer);
629 } else {
630 mei_amthif_run_next_cmd(dev);
631 }
632
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200633 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200634 wake_up_interruptible(&dev->iamthif_cl.wait);
635}
636
Tomas Winklera562d5c2012-11-11 17:38:01 +0200637/**
638 * mei_clear_list - removes all callbacks associated with file
639 * from mei_cb_list
640 *
641 * @dev: device structure.
642 * @file: file structure
643 * @mei_cb_list: callbacks list
644 *
645 * mei_clear_list is called to clear resources associated with file
646 * when application calls close function or Ctrl-C was pressed
647 *
648 * returns true if callback removed from the list, false otherwise
649 */
650static bool mei_clear_list(struct mei_device *dev,
651 const struct file *file, struct list_head *mei_cb_list)
652{
653 struct mei_cl_cb *cb_pos = NULL;
654 struct mei_cl_cb *cb_next = NULL;
655 bool removed = false;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200656
Tomas Winklera562d5c2012-11-11 17:38:01 +0200657 /* list all list member */
658 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
659 /* check if list member associated with a file */
660 if (file == cb_pos->file_object) {
661 /* remove member from the list */
662 list_del(&cb_pos->list);
663 /* check if cb equal to current iamthif cb */
664 if (dev->iamthif_current_cb == cb_pos) {
665 dev->iamthif_current_cb = NULL;
666 /* send flow control to iamthif client */
Tomas Winkler8120e722012-12-25 19:06:11 +0200667 mei_hbm_cl_flow_control_req(dev,
668 &dev->iamthif_cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200669 }
670 /* free all allocated buffers */
671 mei_io_cb_free(cb_pos);
672 cb_pos = NULL;
673 removed = true;
674 }
675 }
676 return removed;
677}
678
679/**
680 * mei_clear_lists - removes all callbacks associated with file
681 *
682 * @dev: device structure
683 * @file: file structure
684 *
685 * mei_clear_lists is called to clear resources associated with file
686 * when application calls close function or Ctrl-C was pressed
687 *
688 * returns true if callback removed from the list, false otherwise
689 */
690static bool mei_clear_lists(struct mei_device *dev, struct file *file)
691{
692 bool removed = false;
693
694 /* remove callbacks associated with a file */
695 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
696 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
697 removed = true;
698
699 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
700
701 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
702 removed = true;
703
704 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
705 removed = true;
706
707 if (mei_clear_list(dev, file, &dev->write_list.list))
708 removed = true;
709
710 /* check if iamthif_current_cb not NULL */
711 if (dev->iamthif_current_cb && !removed) {
712 /* check file and iamthif current cb association */
713 if (dev->iamthif_current_cb->file_object == file) {
714 /* remove cb */
715 mei_io_cb_free(dev->iamthif_current_cb);
716 dev->iamthif_current_cb = NULL;
717 removed = true;
718 }
719 }
720 return removed;
721}
722
723/**
724* mei_amthif_release - the release function
725*
Masanari Iida393b1482013-04-05 01:05:05 +0900726* @dev: device structure
Tomas Winklera562d5c2012-11-11 17:38:01 +0200727* @file: pointer to file structure
728*
729* returns 0 on success, <0 on error
730*/
731int mei_amthif_release(struct mei_device *dev, struct file *file)
732{
733 if (dev->open_handle_count > 0)
734 dev->open_handle_count--;
735
736 if (dev->iamthif_file_object == file &&
737 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
738
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200739 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
Tomas Winklera562d5c2012-11-11 17:38:01 +0200740 dev->iamthif_state);
741 dev->iamthif_canceled = true;
742 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200743 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
Tomas Winklera562d5c2012-11-11 17:38:01 +0200744 mei_amthif_run_next_cmd(dev);
745 }
746 }
747
748 if (mei_clear_lists(dev, file))
749 dev->iamthif_state = MEI_IAMTHIF_IDLE;
750
751 return 0;
752}