blob: f88cb26364f59b681e756c0481c3be1ebad89ad1 [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 Winkler9dc64d62013-01-08 23:07:17 +020037#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020038#include "client.h"
Tomas Winkler19838fb2012-11-01 21:17:15 +020039
Tomas Winkler1a1aca42013-01-08 23:07:21 +020040const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
41 0xac, 0xa8, 0x46, 0xe0,
42 0xff, 0x65, 0x81, 0x4c);
Tomas Winkler19838fb2012-11-01 21:17:15 +020043
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;
Alexander Usyskin4a704572013-09-02 13:29:47 +030059 dev->iamthif_stall_timer = 0;
Tomas Winkler22f96a02013-09-16 23:44:47 +030060 dev->iamthif_open_count = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +020061}
62
63/**
Masanari Iida393b1482013-04-05 01:05:05 +090064 * mei_amthif_host_init - mei initialization amthif client.
Tomas Winkler19838fb2012-11-01 21:17:15 +020065 *
66 * @dev: the device structure
67 *
68 */
Tomas Winkler781d0d82013-01-08 23:07:22 +020069int mei_amthif_host_init(struct mei_device *dev)
Tomas Winkler19838fb2012-11-01 21:17:15 +020070{
Tomas Winkler781d0d82013-01-08 23:07:22 +020071 struct mei_cl *cl = &dev->iamthif_cl;
Tomas Winkler19838fb2012-11-01 21:17:15 +020072 unsigned char *msg_buf;
Tomas Winkler781d0d82013-01-08 23:07:22 +020073 int ret, i;
Tomas Winkler19838fb2012-11-01 21:17:15 +020074
Tomas Winkler6222f7b2013-01-08 23:07:23 +020075 dev->iamthif_state = MEI_IAMTHIF_IDLE;
76
Tomas Winkler781d0d82013-01-08 23:07:22 +020077 mei_cl_init(cl, dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +020078
Tomas Winkler781d0d82013-01-08 23:07:22 +020079 i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
Tomas Winkler19838fb2012-11-01 21:17:15 +020080 if (i < 0) {
Tomas Winkleraf68fb62013-09-16 23:44:48 +030081 ret = i;
82 dev_info(&dev->pdev->dev,
83 "amthif: failed to find the client %d\n", ret);
84 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +020085 }
86
Tomas Winkler781d0d82013-01-08 23:07:22 +020087 cl->me_client_id = dev->me_clients[i].client_id;
88
Tomas Winkler19838fb2012-11-01 21:17:15 +020089 /* Assign iamthif_mtu to the value received from ME */
90
91 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
92 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
93 dev->me_clients[i].props.max_msg_length);
94
95 kfree(dev->iamthif_msg_buf);
96 dev->iamthif_msg_buf = NULL;
97
98 /* allocate storage for ME message buffer */
99 msg_buf = kcalloc(dev->iamthif_mtu,
100 sizeof(unsigned char), GFP_KERNEL);
101 if (!msg_buf) {
Tomas Winkler781d0d82013-01-08 23:07:22 +0200102 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
103 return -ENOMEM;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200104 }
105
106 dev->iamthif_msg_buf = msg_buf;
107
Tomas Winkler781d0d82013-01-08 23:07:22 +0200108 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
109
110 if (ret < 0) {
Tomas Winkleraf68fb62013-09-16 23:44:48 +0300111 dev_err(&dev->pdev->dev,
112 "amthif: failed link client %d\n", ret);
113 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200114 }
Tomas Winkler781d0d82013-01-08 23:07:22 +0200115
116 cl->state = MEI_FILE_CONNECTING;
117
118 if (mei_hbm_cl_connect_req(dev, cl)) {
119 dev_dbg(&dev->pdev->dev, "amthif: Failed to connect to ME client\n");
120 cl->state = MEI_FILE_DISCONNECTED;
121 cl->host_client_id = 0;
122 } else {
123 cl->timer_count = MEI_CONNECT_TIMEOUT;
124 }
125 return 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200126}
127
128/**
129 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
130 *
131 * @dev: the device structure
132 * @file: pointer to file object
133 *
134 * returns returned a list entry on success, NULL on failure.
135 */
136struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
137 struct file *file)
138{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200139 struct mei_cl_cb *pos = NULL;
140 struct mei_cl_cb *next = NULL;
141
142 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200143 &dev->amthif_rd_complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200144 if (pos->cl && pos->cl == &dev->iamthif_cl &&
Tomas Winkler19838fb2012-11-01 21:17:15 +0200145 pos->file_object == file)
146 return pos;
147 }
148 return NULL;
149}
150
151
152/**
153 * mei_amthif_read - read data from AMTHIF client
154 *
155 * @dev: the device structure
156 * @if_num: minor number
157 * @file: pointer to file object
158 * @*ubuf: pointer to user data in user space
159 * @length: data length to read
160 * @offset: data read offset
161 *
162 * Locking: called under "dev->device_lock" lock
163 *
164 * returns
165 * returned data length on success,
166 * zero if no data to read,
167 * negative on failure.
168 */
169int mei_amthif_read(struct mei_device *dev, struct file *file,
170 char __user *ubuf, size_t length, loff_t *offset)
171{
172 int rets;
173 int wait_ret;
174 struct mei_cl_cb *cb = NULL;
175 struct mei_cl *cl = file->private_data;
176 unsigned long timeout;
177 int i;
178
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200179 /* Only possible if we are in timeout */
Tomas Winkler19838fb2012-11-01 21:17:15 +0200180 if (!cl || cl != &dev->iamthif_cl) {
181 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
182 return -ETIMEDOUT;
183 }
184
185 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
186
187 if (i < 0) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200188 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200189 return -ENODEV;
190 }
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200191 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200192 cb = mei_amthif_find_read_list_entry(dev, file);
193
194 /* Check for if we can block or not*/
195 if (cb == NULL && file->f_flags & O_NONBLOCK)
196 return -EAGAIN;
197
198
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200199 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200200 while (cb == NULL) {
201 /* unlock the Mutex */
202 mutex_unlock(&dev->device_lock);
203
204 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
205 (cb = mei_amthif_find_read_list_entry(dev, file)));
206
Alexey Khoroshilove6028db2012-12-22 01:44:16 +0400207 /* Locking again the Mutex */
208 mutex_lock(&dev->device_lock);
209
Tomas Winkler19838fb2012-11-01 21:17:15 +0200210 if (wait_ret)
211 return -ERESTARTSYS;
212
213 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200214 }
215
216
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200217 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200218 dev->iamthif_timer = 0;
219
220 if (cb) {
221 timeout = cb->read_time +
222 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200223 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200224 timeout);
225
226 if (time_after(jiffies, timeout)) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200227 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200228 /* 15 sec for the message has expired */
229 list_del(&cb->list);
230 rets = -ETIMEDOUT;
231 goto free;
232 }
233 }
234 /* if the whole message will fit remove it from the list */
235 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
236 list_del(&cb->list);
237 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
238 /* end of the message has been reached */
239 list_del(&cb->list);
240 rets = 0;
241 goto free;
242 }
243 /* else means that not full buffer will be read and do not
244 * remove message from deletion list
245 */
246
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200247 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200248 cb->response_buffer.size);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200249 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200250
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200251 /* length is being truncated to PAGE_SIZE, however,
Tomas Winkler19838fb2012-11-01 21:17:15 +0200252 * the buf_idx may point beyond */
253 length = min_t(size_t, length, (cb->buf_idx - *offset));
254
255 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
256 rets = -EFAULT;
257 else {
258 rets = length;
259 if ((*offset + length) < cb->buf_idx) {
260 *offset += length;
261 goto out;
262 }
263 }
264free:
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200265 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200266 *offset = 0;
267 mei_io_cb_free(cb);
268out:
269 return rets;
270}
271
272/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200273 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200274 *
275 * @dev: the device structure
276 * @cb: mei call back struct
277 *
278 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200279 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200280 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200281static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200282{
283 struct mei_msg_hdr mei_hdr;
284 int ret;
285
286 if (!dev || !cb)
287 return -ENODEV;
288
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200289 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200290
291 dev->iamthif_state = MEI_IAMTHIF_WRITING;
292 dev->iamthif_current_cb = cb;
293 dev->iamthif_file_object = cb->file_object;
294 dev->iamthif_canceled = false;
295 dev->iamthif_ioctl = true;
296 dev->iamthif_msg_buf_size = cb->request_buffer.size;
297 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
298 cb->request_buffer.size);
299
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200300 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200301 if (ret < 0)
302 return ret;
303
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200304 if (ret && dev->hbuf_is_ready) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200305 ret = 0;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200306 dev->hbuf_is_ready = false;
Tomas Winkler827eef52013-02-06 14:06:41 +0200307 if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
308 mei_hdr.length = mei_hbuf_max_len(dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200309 mei_hdr.msg_complete = 0;
310 } else {
311 mei_hdr.length = cb->request_buffer.size;
312 mei_hdr.msg_complete = 1;
313 }
314
315 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
316 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
317 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200318 mei_hdr.internal = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200319 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300320 ret = mei_write_message(dev, &mei_hdr, dev->iamthif_msg_buf);
321 if (ret)
322 return ret;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200323
324 if (mei_hdr.msg_complete) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200325 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300326 return -EIO;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200327 dev->iamthif_flow_control_pending = true;
328 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200329 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200330 dev->iamthif_current_cb = cb;
331 dev->iamthif_file_object = cb->file_object;
332 list_add_tail(&cb->list, &dev->write_waiting_list.list);
333 } else {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200334 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200335 list_add_tail(&cb->list, &dev->write_list.list);
336 }
337 } else {
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200338 if (!dev->hbuf_is_ready)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200339 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
340
341 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
342 list_add_tail(&cb->list, &dev->write_list.list);
343 }
344 return 0;
345}
346
347/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200348 * mei_amthif_write - write amthif data to amthif client
349 *
350 * @dev: the device structure
351 * @cb: mei call back struct
352 *
353 * returns 0 on success, <0 on failure.
354 *
355 */
356int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
357{
358 int ret;
359
360 if (!dev || !cb)
361 return -ENODEV;
362
363 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
364 if (ret)
365 return ret;
366
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200367 cb->fop_type = MEI_FOP_WRITE;
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200368
Tomas Winklere773efc2012-11-11 17:37:58 +0200369 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200370 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
371 dev_dbg(&dev->pdev->dev,
372 "amthif state = %d\n", dev->iamthif_state);
373 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200374 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200375 return 0;
376 }
377 return mei_amthif_send_cmd(dev, cb);
378}
379/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200380 * mei_amthif_run_next_cmd
381 *
382 * @dev: the device structure
383 *
384 * returns 0 on success, <0 on failure.
385 */
386void mei_amthif_run_next_cmd(struct mei_device *dev)
387{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200388 struct mei_cl_cb *pos = NULL;
389 struct mei_cl_cb *next = NULL;
390 int status;
391
392 if (!dev)
393 return;
394
395 dev->iamthif_msg_buf_size = 0;
396 dev->iamthif_msg_buf_index = 0;
397 dev->iamthif_canceled = false;
398 dev->iamthif_ioctl = true;
399 dev->iamthif_state = MEI_IAMTHIF_IDLE;
400 dev->iamthif_timer = 0;
401 dev->iamthif_file_object = NULL;
402
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200403 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200404
Tomas Winklere773efc2012-11-11 17:37:58 +0200405 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200406 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200407
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200408 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200409 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200410 if (status) {
411 dev_dbg(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200412 "amthif write failed status = %d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200413 status);
414 return;
415 }
416 break;
417 }
418 }
419}
420
Tomas Winkler744f0f22012-11-11 17:38:02 +0200421
422unsigned int mei_amthif_poll(struct mei_device *dev,
423 struct file *file, poll_table *wait)
424{
425 unsigned int mask = 0;
Tomas Winklerb950ac12013-07-25 20:15:53 +0300426
Tomas Winkler744f0f22012-11-11 17:38:02 +0200427 poll_wait(file, &dev->iamthif_cl.wait, wait);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300428
Tomas Winkler744f0f22012-11-11 17:38:02 +0200429 mutex_lock(&dev->device_lock);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300430 if (!mei_cl_is_connected(&dev->iamthif_cl)) {
431
432 mask = POLLERR;
433
434 } else if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
435 dev->iamthif_file_object == file) {
436
Tomas Winkler744f0f22012-11-11 17:38:02 +0200437 mask |= (POLLIN | POLLRDNORM);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200438 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
Tomas Winkler744f0f22012-11-11 17:38:02 +0200439 mei_amthif_run_next_cmd(dev);
440 }
Tomas Winklerb950ac12013-07-25 20:15:53 +0300441 mutex_unlock(&dev->device_lock);
442
Tomas Winkler744f0f22012-11-11 17:38:02 +0200443 return mask;
444}
445
446
447
Tomas Winkler19838fb2012-11-01 21:17:15 +0200448/**
Masanari Iida393b1482013-04-05 01:05:05 +0900449 * mei_amthif_irq_write_completed - processes completed iamthif operation.
Tomas Winkler19838fb2012-11-01 21:17:15 +0200450 *
451 * @dev: the device structure.
452 * @slots: free slots.
453 * @cb_pos: callback block.
454 * @cl: private data of the file object.
455 * @cmpl_list: complete list.
456 *
457 * returns 0, OK; otherwise, error.
458 */
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300459int mei_amthif_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
460 s32 *slots, struct mei_cl_cb *cmpl_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200461{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300462 struct mei_device *dev = cl->dev;
Tomas Winklere46f1872012-12-25 19:06:10 +0200463 struct mei_msg_hdr mei_hdr;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200464 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200465 u32 msg_slots = mei_data2slots(len);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300466 int rets;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200467
Tomas Winkler136698e2013-09-16 23:44:44 +0300468 rets = mei_cl_flow_ctrl_creds(cl);
469 if (rets < 0)
470 return rets;
471
472 if (rets == 0) {
473 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
474 return 0;
475 }
476
Tomas Winklere46f1872012-12-25 19:06:10 +0200477 mei_hdr.host_addr = cl->host_client_id;
478 mei_hdr.me_addr = cl->me_client_id;
479 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200480 mei_hdr.internal = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200481
482 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200483 mei_hdr.length = len;
484 mei_hdr.msg_complete = 1;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200485 /* Split the message only if we can write the whole host buffer */
486 } else if (*slots == dev->hbuf_depth) {
487 msg_slots = *slots;
488 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200489 mei_hdr.length = len;
490 mei_hdr.msg_complete = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200491 } else {
492 /* wait for next time the host buffer is empty */
493 return 0;
494 }
Tomas Winkler19838fb2012-11-01 21:17:15 +0200495
Tomas Winklere46f1872012-12-25 19:06:10 +0200496 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200497
Tomas Winkler24c656e2012-11-18 15:13:17 +0200498 *slots -= msg_slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300499 rets = mei_write_message(dev, &mei_hdr,
500 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index);
501 if (rets) {
502 dev->iamthif_state = MEI_IAMTHIF_IDLE;
503 cl->status = rets;
504 list_del(&cb->list);
505 return rets;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200506 }
507
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200508 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300509 return -EIO;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200510
Tomas Winklere46f1872012-12-25 19:06:10 +0200511 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200512 cl->status = 0;
513
Tomas Winklere46f1872012-12-25 19:06:10 +0200514 if (mei_hdr.msg_complete) {
Tomas Winkler24c656e2012-11-18 15:13:17 +0200515 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
516 dev->iamthif_flow_control_pending = true;
517
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200518 /* save iamthif cb sent to amthif client */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200519 cb->buf_idx = dev->iamthif_msg_buf_index;
520 dev->iamthif_current_cb = cb;
521
522 list_move_tail(&cb->list, &dev->write_waiting_list.list);
523 }
524
525
Tomas Winkler19838fb2012-11-01 21:17:15 +0200526 return 0;
527}
528
529/**
530 * mei_amthif_irq_read_message - read routine after ISR to
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200531 * handle the read amthif message
Tomas Winkler19838fb2012-11-01 21:17:15 +0200532 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200533 * @dev: the device structure
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200534 * @mei_hdr: header of amthif message
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300535 * @complete_list: An instance of our list structure
Tomas Winkler19838fb2012-11-01 21:17:15 +0200536 *
537 * returns 0 on success, <0 on failure.
538 */
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300539int mei_amthif_irq_read_msg(struct mei_device *dev,
540 struct mei_msg_hdr *mei_hdr,
541 struct mei_cl_cb *complete_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200542{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200543 struct mei_cl_cb *cb;
544 unsigned char *buffer;
545
546 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
547 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
548
549 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
550 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
551
552 mei_read_slots(dev, buffer, mei_hdr->length);
553
554 dev->iamthif_msg_buf_index += mei_hdr->length;
555
556 if (!mei_hdr->msg_complete)
557 return 0;
558
Tomas Winkler5ceb46e2013-04-19 21:16:53 +0300559 dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
Tomas Winkler19838fb2012-11-01 21:17:15 +0200560 mei_hdr->length);
561
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200562 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200563 if (!dev->iamthif_current_cb)
564 return -ENODEV;
565
566 cb = dev->iamthif_current_cb;
567 dev->iamthif_current_cb = NULL;
568
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200569 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200570 return -ENODEV;
571
572 dev->iamthif_stall_timer = 0;
573 cb->buf_idx = dev->iamthif_msg_buf_index;
574 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200575 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200576 /* found the iamthif cb */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200577 dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
578 dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200579 list_add_tail(&cb->list, &complete_list->list);
580 }
581 return 0;
582}
583
584/**
585 * mei_amthif_irq_read - prepares to read amthif data.
586 *
587 * @dev: the device structure.
588 * @slots: free slots.
589 *
590 * returns 0, OK; otherwise, error.
591 */
592int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
593{
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200594 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200595
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200596 if (*slots < msg_slots)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200597 return -EMSGSIZE;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200598
599 *slots -= msg_slots;
600
Tomas Winkler8120e722012-12-25 19:06:11 +0200601 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200602 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
603 return -EIO;
604 }
605
606 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
607 dev->iamthif_state = MEI_IAMTHIF_READING;
608 dev->iamthif_flow_control_pending = false;
609 dev->iamthif_msg_buf_index = 0;
610 dev->iamthif_msg_buf_size = 0;
611 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200612 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200613 return 0;
614}
615
616/**
617 * mei_amthif_complete - complete amthif callback.
618 *
619 * @dev: the device structure.
620 * @cb_pos: callback block.
621 */
622void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
623{
624 if (dev->iamthif_canceled != 1) {
625 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
626 dev->iamthif_stall_timer = 0;
627 memcpy(cb->response_buffer.data,
628 dev->iamthif_msg_buf,
629 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200630 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200631 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200632 dev->iamthif_timer = jiffies;
633 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
634 dev->iamthif_timer);
635 } else {
636 mei_amthif_run_next_cmd(dev);
637 }
638
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200639 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200640 wake_up_interruptible(&dev->iamthif_cl.wait);
641}
642
Tomas Winklera562d5c2012-11-11 17:38:01 +0200643/**
644 * mei_clear_list - removes all callbacks associated with file
645 * from mei_cb_list
646 *
647 * @dev: device structure.
648 * @file: file structure
649 * @mei_cb_list: callbacks list
650 *
651 * mei_clear_list is called to clear resources associated with file
652 * when application calls close function or Ctrl-C was pressed
653 *
654 * returns true if callback removed from the list, false otherwise
655 */
656static bool mei_clear_list(struct mei_device *dev,
657 const struct file *file, struct list_head *mei_cb_list)
658{
659 struct mei_cl_cb *cb_pos = NULL;
660 struct mei_cl_cb *cb_next = NULL;
661 bool removed = false;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200662
Tomas Winklera562d5c2012-11-11 17:38:01 +0200663 /* list all list member */
664 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
665 /* check if list member associated with a file */
666 if (file == cb_pos->file_object) {
667 /* remove member from the list */
668 list_del(&cb_pos->list);
669 /* check if cb equal to current iamthif cb */
670 if (dev->iamthif_current_cb == cb_pos) {
671 dev->iamthif_current_cb = NULL;
672 /* send flow control to iamthif client */
Tomas Winkler8120e722012-12-25 19:06:11 +0200673 mei_hbm_cl_flow_control_req(dev,
674 &dev->iamthif_cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200675 }
676 /* free all allocated buffers */
677 mei_io_cb_free(cb_pos);
678 cb_pos = NULL;
679 removed = true;
680 }
681 }
682 return removed;
683}
684
685/**
686 * mei_clear_lists - removes all callbacks associated with file
687 *
688 * @dev: device structure
689 * @file: file structure
690 *
691 * mei_clear_lists is called to clear resources associated with file
692 * when application calls close function or Ctrl-C was pressed
693 *
694 * returns true if callback removed from the list, false otherwise
695 */
696static bool mei_clear_lists(struct mei_device *dev, struct file *file)
697{
698 bool removed = false;
699
700 /* remove callbacks associated with a file */
701 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
702 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
703 removed = true;
704
705 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
706
707 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
708 removed = true;
709
710 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
711 removed = true;
712
713 if (mei_clear_list(dev, file, &dev->write_list.list))
714 removed = true;
715
716 /* check if iamthif_current_cb not NULL */
717 if (dev->iamthif_current_cb && !removed) {
718 /* check file and iamthif current cb association */
719 if (dev->iamthif_current_cb->file_object == file) {
720 /* remove cb */
721 mei_io_cb_free(dev->iamthif_current_cb);
722 dev->iamthif_current_cb = NULL;
723 removed = true;
724 }
725 }
726 return removed;
727}
728
729/**
730* mei_amthif_release - the release function
731*
Masanari Iida393b1482013-04-05 01:05:05 +0900732* @dev: device structure
Tomas Winklera562d5c2012-11-11 17:38:01 +0200733* @file: pointer to file structure
734*
735* returns 0 on success, <0 on error
736*/
737int mei_amthif_release(struct mei_device *dev, struct file *file)
738{
Tomas Winkler22f96a02013-09-16 23:44:47 +0300739 if (dev->iamthif_open_count > 0)
740 dev->iamthif_open_count--;
Tomas Winklera562d5c2012-11-11 17:38:01 +0200741
742 if (dev->iamthif_file_object == file &&
743 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
744
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200745 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
Tomas Winklera562d5c2012-11-11 17:38:01 +0200746 dev->iamthif_state);
747 dev->iamthif_canceled = true;
748 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200749 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
Tomas Winklera562d5c2012-11-11 17:38:01 +0200750 mei_amthif_run_next_cmd(dev);
751 }
752 }
753
754 if (mei_clear_lists(dev, file))
755 dev->iamthif_state = MEI_IAMTHIF_IDLE;
756
757 return 0;
758}