blob: 0d6234db00fa126330a664007e4093aac14a6190 [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>
Tomas Winkler19838fb2012-11-01 21:17:15 +020024#include <linux/ioctl.h>
25#include <linux/cdev.h>
26#include <linux/list.h>
27#include <linux/delay.h>
28#include <linux/sched.h>
29#include <linux/uuid.h>
30#include <linux/jiffies.h>
31#include <linux/uaccess.h>
32
Tomas Winkler47a73802012-12-25 19:06:03 +020033#include <linux/mei.h>
Tomas Winkler19838fb2012-11-01 21:17:15 +020034
35#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020036#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020037#include "client.h"
Tomas Winkler19838fb2012-11-01 21:17:15 +020038
Tomas Winkler1a1aca42013-01-08 23:07:21 +020039const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
40 0xac, 0xa8, 0x46, 0xe0,
41 0xff, 0x65, 0x81, 0x4c);
Tomas Winkler19838fb2012-11-01 21:17:15 +020042
43/**
44 * mei_amthif_reset_params - initializes mei device iamthif
45 *
46 * @dev: the device structure
47 */
48void mei_amthif_reset_params(struct mei_device *dev)
49{
50 /* reset iamthif parameters. */
51 dev->iamthif_current_cb = NULL;
52 dev->iamthif_msg_buf_size = 0;
53 dev->iamthif_msg_buf_index = 0;
54 dev->iamthif_canceled = false;
55 dev->iamthif_ioctl = false;
56 dev->iamthif_state = MEI_IAMTHIF_IDLE;
57 dev->iamthif_timer = 0;
Alexander Usyskin4a704572013-09-02 13:29:47 +030058 dev->iamthif_stall_timer = 0;
Tomas Winkler22f96a02013-09-16 23:44:47 +030059 dev->iamthif_open_count = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +020060}
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 Winkleraf68fb62013-09-16 23:44:48 +030080 dev_info(&dev->pdev->dev,
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +020081 "amthif: failed to find the client %d\n", i);
82 return -ENOTTY;
Tomas Winkler19838fb2012-11-01 21:17:15 +020083 }
84
Tomas Winkler781d0d82013-01-08 23:07:22 +020085 cl->me_client_id = dev->me_clients[i].client_id;
86
Tomas Winkler19838fb2012-11-01 21:17:15 +020087 /* Assign iamthif_mtu to the value received from ME */
88
89 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
90 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
91 dev->me_clients[i].props.max_msg_length);
92
93 kfree(dev->iamthif_msg_buf);
94 dev->iamthif_msg_buf = NULL;
95
96 /* allocate storage for ME message buffer */
97 msg_buf = kcalloc(dev->iamthif_mtu,
98 sizeof(unsigned char), GFP_KERNEL);
99 if (!msg_buf) {
Tomas Winkler781d0d82013-01-08 23:07:22 +0200100 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
101 return -ENOMEM;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200102 }
103
104 dev->iamthif_msg_buf = msg_buf;
105
Tomas Winkler781d0d82013-01-08 23:07:22 +0200106 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
107
108 if (ret < 0) {
Tomas Winkleraf68fb62013-09-16 23:44:48 +0300109 dev_err(&dev->pdev->dev,
110 "amthif: failed link client %d\n", ret);
111 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200112 }
Tomas Winkler781d0d82013-01-08 23:07:22 +0200113
Tomas Winkler64092852014-02-17 15:13:21 +0200114 ret = mei_cl_connect(cl, NULL);
115
116 dev->iamthif_state = MEI_IAMTHIF_IDLE;
117
118 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200119}
120
121/**
122 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
123 *
124 * @dev: the device structure
125 * @file: pointer to file object
126 *
127 * returns returned a list entry on success, NULL on failure.
128 */
129struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
130 struct file *file)
131{
Tomas Winkler31f88f52014-02-17 15:13:25 +0200132 struct mei_cl_cb *cb;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200133
Tomas Winkler31f88f52014-02-17 15:13:25 +0200134 list_for_each_entry(cb, &dev->amthif_rd_complete_list.list, list) {
135 if (cb->cl && cb->cl == &dev->iamthif_cl &&
136 cb->file_object == file)
137 return cb;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200138 }
139 return NULL;
140}
141
142
143/**
144 * mei_amthif_read - read data from AMTHIF client
145 *
146 * @dev: the device structure
147 * @if_num: minor number
148 * @file: pointer to file object
149 * @*ubuf: pointer to user data in user space
150 * @length: data length to read
151 * @offset: data read offset
152 *
153 * Locking: called under "dev->device_lock" lock
154 *
155 * returns
156 * returned data length on success,
157 * zero if no data to read,
158 * negative on failure.
159 */
160int mei_amthif_read(struct mei_device *dev, struct file *file,
161 char __user *ubuf, size_t length, loff_t *offset)
162{
163 int rets;
164 int wait_ret;
165 struct mei_cl_cb *cb = NULL;
166 struct mei_cl *cl = file->private_data;
167 unsigned long timeout;
168 int i;
169
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200170 /* Only possible if we are in timeout */
Tomas Winkler19838fb2012-11-01 21:17:15 +0200171 if (!cl || cl != &dev->iamthif_cl) {
172 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200173 return -ETIME;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200174 }
175
176 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200177 if (i < 0) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200178 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200179 return -ENOTTY;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200180 }
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200181 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200182 cb = mei_amthif_find_read_list_entry(dev, file);
183
184 /* Check for if we can block or not*/
185 if (cb == NULL && file->f_flags & O_NONBLOCK)
186 return -EAGAIN;
187
188
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200189 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200190 while (cb == NULL) {
191 /* unlock the Mutex */
192 mutex_unlock(&dev->device_lock);
193
194 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
195 (cb = mei_amthif_find_read_list_entry(dev, file)));
196
Alexey Khoroshilove6028db2012-12-22 01:44:16 +0400197 /* Locking again the Mutex */
198 mutex_lock(&dev->device_lock);
199
Tomas Winkler19838fb2012-11-01 21:17:15 +0200200 if (wait_ret)
201 return -ERESTARTSYS;
202
203 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200204 }
205
206
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200207 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200208 dev->iamthif_timer = 0;
209
210 if (cb) {
211 timeout = cb->read_time +
212 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200213 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200214 timeout);
215
216 if (time_after(jiffies, timeout)) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200217 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200218 /* 15 sec for the message has expired */
219 list_del(&cb->list);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200220 rets = -ETIME;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200221 goto free;
222 }
223 }
224 /* if the whole message will fit remove it from the list */
225 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
226 list_del(&cb->list);
227 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
228 /* end of the message has been reached */
229 list_del(&cb->list);
230 rets = 0;
231 goto free;
232 }
233 /* else means that not full buffer will be read and do not
234 * remove message from deletion list
235 */
236
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200237 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200238 cb->response_buffer.size);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200239 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200240
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200241 /* length is being truncated to PAGE_SIZE, however,
Tomas Winkler19838fb2012-11-01 21:17:15 +0200242 * the buf_idx may point beyond */
243 length = min_t(size_t, length, (cb->buf_idx - *offset));
244
Alexander Usyskindbac4742014-03-12 13:19:13 +0200245 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
246 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200247 rets = -EFAULT;
Tomas Winkler34e267d2014-03-16 14:35:57 +0200248 } else {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200249 rets = length;
250 if ((*offset + length) < cb->buf_idx) {
251 *offset += length;
252 goto out;
253 }
254 }
255free:
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200256 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200257 *offset = 0;
258 mei_io_cb_free(cb);
259out:
260 return rets;
261}
262
263/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200264 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200265 *
266 * @dev: the device structure
267 * @cb: mei call back struct
268 *
269 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200270 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200271 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200272static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200273{
274 struct mei_msg_hdr mei_hdr;
275 int ret;
276
277 if (!dev || !cb)
278 return -ENODEV;
279
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200280 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200281
282 dev->iamthif_state = MEI_IAMTHIF_WRITING;
283 dev->iamthif_current_cb = cb;
284 dev->iamthif_file_object = cb->file_object;
285 dev->iamthif_canceled = false;
286 dev->iamthif_ioctl = true;
287 dev->iamthif_msg_buf_size = cb->request_buffer.size;
288 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
289 cb->request_buffer.size);
290
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200291 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200292 if (ret < 0)
293 return ret;
294
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200295 if (ret && mei_hbuf_acquire(dev)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200296 ret = 0;
Tomas Winkler827eef52013-02-06 14:06:41 +0200297 if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
298 mei_hdr.length = mei_hbuf_max_len(dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200299 mei_hdr.msg_complete = 0;
300 } else {
301 mei_hdr.length = cb->request_buffer.size;
302 mei_hdr.msg_complete = 1;
303 }
304
305 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
306 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
307 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200308 mei_hdr.internal = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200309 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300310 ret = mei_write_message(dev, &mei_hdr, dev->iamthif_msg_buf);
311 if (ret)
312 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200313
314 if (mei_hdr.msg_complete) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200315 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300316 return -EIO;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200317 dev->iamthif_flow_control_pending = true;
318 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200319 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200320 dev->iamthif_current_cb = cb;
321 dev->iamthif_file_object = cb->file_object;
322 list_add_tail(&cb->list, &dev->write_waiting_list.list);
323 } else {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200324 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200325 list_add_tail(&cb->list, &dev->write_list.list);
326 }
327 } else {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200328 list_add_tail(&cb->list, &dev->write_list.list);
329 }
330 return 0;
331}
332
333/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200334 * mei_amthif_write - write amthif data to amthif client
335 *
336 * @dev: the device structure
337 * @cb: mei call back struct
338 *
339 * returns 0 on success, <0 on failure.
340 *
341 */
342int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
343{
344 int ret;
345
346 if (!dev || !cb)
347 return -ENODEV;
348
349 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
350 if (ret)
351 return ret;
352
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200353 cb->fop_type = MEI_FOP_WRITE;
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200354
Tomas Winklere773efc2012-11-11 17:37:58 +0200355 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200356 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
357 dev_dbg(&dev->pdev->dev,
358 "amthif state = %d\n", dev->iamthif_state);
359 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200360 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200361 return 0;
362 }
363 return mei_amthif_send_cmd(dev, cb);
364}
365/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200366 * mei_amthif_run_next_cmd
367 *
368 * @dev: the device structure
369 *
370 * returns 0 on success, <0 on failure.
371 */
372void mei_amthif_run_next_cmd(struct mei_device *dev)
373{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200374 struct mei_cl_cb *pos = NULL;
375 struct mei_cl_cb *next = NULL;
376 int status;
377
378 if (!dev)
379 return;
380
381 dev->iamthif_msg_buf_size = 0;
382 dev->iamthif_msg_buf_index = 0;
383 dev->iamthif_canceled = false;
384 dev->iamthif_ioctl = true;
385 dev->iamthif_state = MEI_IAMTHIF_IDLE;
386 dev->iamthif_timer = 0;
387 dev->iamthif_file_object = NULL;
388
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200389 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200390
Tomas Winklere773efc2012-11-11 17:37:58 +0200391 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200392 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200393
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200394 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200395 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200396 if (status) {
397 dev_dbg(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200398 "amthif write failed status = %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200399 status);
400 return;
401 }
402 break;
403 }
404 }
405}
406
Tomas Winkler744f0f22012-11-11 17:38:02 +0200407
408unsigned int mei_amthif_poll(struct mei_device *dev,
409 struct file *file, poll_table *wait)
410{
411 unsigned int mask = 0;
Tomas Winklerb950ac12013-07-25 20:15:53 +0300412
Tomas Winkler744f0f22012-11-11 17:38:02 +0200413 poll_wait(file, &dev->iamthif_cl.wait, wait);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300414
Tomas Winkler744f0f22012-11-11 17:38:02 +0200415 mutex_lock(&dev->device_lock);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300416 if (!mei_cl_is_connected(&dev->iamthif_cl)) {
417
418 mask = POLLERR;
419
420 } else if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
421 dev->iamthif_file_object == file) {
422
Tomas Winkler744f0f22012-11-11 17:38:02 +0200423 mask |= (POLLIN | POLLRDNORM);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200424 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
Tomas Winkler744f0f22012-11-11 17:38:02 +0200425 mei_amthif_run_next_cmd(dev);
426 }
Tomas Winklerb950ac12013-07-25 20:15:53 +0300427 mutex_unlock(&dev->device_lock);
428
Tomas Winkler744f0f22012-11-11 17:38:02 +0200429 return mask;
430}
431
432
433
Tomas Winkler19838fb2012-11-01 21:17:15 +0200434/**
Tomas Winkler9d098192014-02-19 17:35:48 +0200435 * mei_amthif_irq_write - write iamthif command in irq thread context.
Tomas Winkler19838fb2012-11-01 21:17:15 +0200436 *
437 * @dev: the device structure.
Tomas Winkler19838fb2012-11-01 21:17:15 +0200438 * @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 Winkler9d098192014-02-19 17:35:48 +0200444int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
445 struct mei_cl_cb *cmpl_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200446{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300447 struct mei_device *dev = cl->dev;
Tomas Winklere46f1872012-12-25 19:06:10 +0200448 struct mei_msg_hdr mei_hdr;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200449 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200450 u32 msg_slots = mei_data2slots(len);
Tomas Winkler9d098192014-02-19 17:35:48 +0200451 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300452 int rets;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200453
Tomas Winkler136698e2013-09-16 23:44:44 +0300454 rets = mei_cl_flow_ctrl_creds(cl);
455 if (rets < 0)
456 return rets;
457
458 if (rets == 0) {
459 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
460 return 0;
461 }
462
Tomas Winklere46f1872012-12-25 19:06:10 +0200463 mei_hdr.host_addr = cl->host_client_id;
464 mei_hdr.me_addr = cl->me_client_id;
465 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200466 mei_hdr.internal = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200467
Tomas Winkler9d098192014-02-19 17:35:48 +0200468 slots = mei_hbuf_empty_slots(dev);
469
470 if (slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200471 mei_hdr.length = len;
472 mei_hdr.msg_complete = 1;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200473 /* Split the message only if we can write the whole host buffer */
Tomas Winkler9d098192014-02-19 17:35:48 +0200474 } else if (slots == dev->hbuf_depth) {
475 msg_slots = slots;
476 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200477 mei_hdr.length = len;
478 mei_hdr.msg_complete = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200479 } else {
480 /* wait for next time the host buffer is empty */
481 return 0;
482 }
Tomas Winkler19838fb2012-11-01 21:17:15 +0200483
Tomas Winklere46f1872012-12-25 19:06:10 +0200484 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200485
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300486 rets = mei_write_message(dev, &mei_hdr,
487 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index);
488 if (rets) {
489 dev->iamthif_state = MEI_IAMTHIF_IDLE;
490 cl->status = rets;
491 list_del(&cb->list);
492 return rets;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200493 }
494
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200495 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300496 return -EIO;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200497
Tomas Winklere46f1872012-12-25 19:06:10 +0200498 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200499 cl->status = 0;
500
Tomas Winklere46f1872012-12-25 19:06:10 +0200501 if (mei_hdr.msg_complete) {
Tomas Winkler24c656e2012-11-18 15:13:17 +0200502 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
503 dev->iamthif_flow_control_pending = true;
504
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200505 /* save iamthif cb sent to amthif client */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200506 cb->buf_idx = dev->iamthif_msg_buf_index;
507 dev->iamthif_current_cb = cb;
508
509 list_move_tail(&cb->list, &dev->write_waiting_list.list);
510 }
511
512
Tomas Winkler19838fb2012-11-01 21:17:15 +0200513 return 0;
514}
515
516/**
517 * mei_amthif_irq_read_message - read routine after ISR to
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200518 * handle the read amthif message
Tomas Winkler19838fb2012-11-01 21:17:15 +0200519 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200520 * @dev: the device structure
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200521 * @mei_hdr: header of amthif message
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300522 * @complete_list: An instance of our list structure
Tomas Winkler19838fb2012-11-01 21:17:15 +0200523 *
524 * returns 0 on success, <0 on failure.
525 */
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300526int mei_amthif_irq_read_msg(struct mei_device *dev,
527 struct mei_msg_hdr *mei_hdr,
528 struct mei_cl_cb *complete_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200529{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200530 struct mei_cl_cb *cb;
531 unsigned char *buffer;
532
533 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
534 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
535
536 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
537 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
538
539 mei_read_slots(dev, buffer, mei_hdr->length);
540
541 dev->iamthif_msg_buf_index += mei_hdr->length;
542
543 if (!mei_hdr->msg_complete)
544 return 0;
545
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300546 dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200547 mei_hdr->length);
548
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200549 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200550 if (!dev->iamthif_current_cb)
551 return -ENODEV;
552
553 cb = dev->iamthif_current_cb;
554 dev->iamthif_current_cb = NULL;
555
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200556 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200557 return -ENODEV;
558
559 dev->iamthif_stall_timer = 0;
560 cb->buf_idx = dev->iamthif_msg_buf_index;
561 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200562 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200563 /* found the iamthif cb */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200564 dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
565 dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200566 list_add_tail(&cb->list, &complete_list->list);
567 }
568 return 0;
569}
570
571/**
572 * mei_amthif_irq_read - prepares to read amthif data.
573 *
574 * @dev: the device structure.
575 * @slots: free slots.
576 *
577 * returns 0, OK; otherwise, error.
578 */
579int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
580{
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200581 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200582
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200583 if (*slots < msg_slots)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200584 return -EMSGSIZE;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200585
586 *slots -= msg_slots;
587
Tomas Winkler8120e722012-12-25 19:06:11 +0200588 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200589 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
590 return -EIO;
591 }
592
593 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
594 dev->iamthif_state = MEI_IAMTHIF_READING;
595 dev->iamthif_flow_control_pending = false;
596 dev->iamthif_msg_buf_index = 0;
597 dev->iamthif_msg_buf_size = 0;
598 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200599 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200600 return 0;
601}
602
603/**
604 * mei_amthif_complete - complete amthif callback.
605 *
606 * @dev: the device structure.
607 * @cb_pos: callback block.
608 */
609void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
610{
611 if (dev->iamthif_canceled != 1) {
612 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
613 dev->iamthif_stall_timer = 0;
614 memcpy(cb->response_buffer.data,
615 dev->iamthif_msg_buf,
616 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200617 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200618 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200619 dev->iamthif_timer = jiffies;
620 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
621 dev->iamthif_timer);
622 } else {
623 mei_amthif_run_next_cmd(dev);
624 }
625
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200626 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200627 wake_up_interruptible(&dev->iamthif_cl.wait);
628}
629
Tomas Winklera562d5c2012-11-11 17:38:01 +0200630/**
631 * mei_clear_list - removes all callbacks associated with file
632 * from mei_cb_list
633 *
634 * @dev: device structure.
635 * @file: file structure
636 * @mei_cb_list: callbacks list
637 *
638 * mei_clear_list is called to clear resources associated with file
639 * when application calls close function or Ctrl-C was pressed
640 *
641 * returns true if callback removed from the list, false otherwise
642 */
643static bool mei_clear_list(struct mei_device *dev,
644 const struct file *file, struct list_head *mei_cb_list)
645{
646 struct mei_cl_cb *cb_pos = NULL;
647 struct mei_cl_cb *cb_next = NULL;
648 bool removed = false;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200649
Tomas Winklera562d5c2012-11-11 17:38:01 +0200650 /* list all list member */
651 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
652 /* check if list member associated with a file */
653 if (file == cb_pos->file_object) {
654 /* remove member from the list */
655 list_del(&cb_pos->list);
656 /* check if cb equal to current iamthif cb */
657 if (dev->iamthif_current_cb == cb_pos) {
658 dev->iamthif_current_cb = NULL;
659 /* send flow control to iamthif client */
Tomas Winkler8120e722012-12-25 19:06:11 +0200660 mei_hbm_cl_flow_control_req(dev,
661 &dev->iamthif_cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200662 }
663 /* free all allocated buffers */
664 mei_io_cb_free(cb_pos);
665 cb_pos = NULL;
666 removed = true;
667 }
668 }
669 return removed;
670}
671
672/**
673 * mei_clear_lists - removes all callbacks associated with file
674 *
675 * @dev: device structure
676 * @file: file structure
677 *
678 * mei_clear_lists is called to clear resources associated with file
679 * when application calls close function or Ctrl-C was pressed
680 *
681 * returns true if callback removed from the list, false otherwise
682 */
683static bool mei_clear_lists(struct mei_device *dev, struct file *file)
684{
685 bool removed = false;
686
687 /* remove callbacks associated with a file */
688 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
689 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
690 removed = true;
691
692 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
693
694 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
695 removed = true;
696
697 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
698 removed = true;
699
700 if (mei_clear_list(dev, file, &dev->write_list.list))
701 removed = true;
702
703 /* check if iamthif_current_cb not NULL */
704 if (dev->iamthif_current_cb && !removed) {
705 /* check file and iamthif current cb association */
706 if (dev->iamthif_current_cb->file_object == file) {
707 /* remove cb */
708 mei_io_cb_free(dev->iamthif_current_cb);
709 dev->iamthif_current_cb = NULL;
710 removed = true;
711 }
712 }
713 return removed;
714}
715
716/**
717* mei_amthif_release - the release function
718*
Masanari Iida393b1482013-04-05 01:05:05 +0900719* @dev: device structure
Tomas Winklera562d5c2012-11-11 17:38:01 +0200720* @file: pointer to file structure
721*
722* returns 0 on success, <0 on error
723*/
724int mei_amthif_release(struct mei_device *dev, struct file *file)
725{
Tomas Winkler22f96a02013-09-16 23:44:47 +0300726 if (dev->iamthif_open_count > 0)
727 dev->iamthif_open_count--;
Tomas Winklera562d5c2012-11-11 17:38:01 +0200728
729 if (dev->iamthif_file_object == file &&
730 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
731
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200732 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
Tomas Winklera562d5c2012-11-11 17:38:01 +0200733 dev->iamthif_state);
734 dev->iamthif_canceled = true;
735 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200736 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
Tomas Winklera562d5c2012-11-11 17:38:01 +0200737 mei_amthif_run_next_cmd(dev);
738 }
739 }
740
741 if (mei_clear_lists(dev, file))
742 dev->iamthif_state = MEI_IAMTHIF_IDLE;
743
744 return 0;
745}