blob: 6ce45ef2cd6cb8f8c86ed7524722f882a2f55244 [file] [log] [blame]
Oren Weilfb7d8792011-05-15 13:43:42 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weilfb7d8792011-05-15 13:43:42 +03005 *
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
Tomas Winkler40e0b672013-03-27 16:58:30 +020018#include <linux/export.h>
Oren Weilfb7d8792011-05-15 13:43:42 +030019#include <linux/pci.h>
20#include <linux/kthread.h>
21#include <linux/interrupt.h>
22#include <linux/fs.h>
23#include <linux/jiffies.h>
24
Tomas Winkler4f3afe12012-05-09 16:38:59 +030025#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020026
27#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020028#include "hbm.h"
Tomas Winkler9dc64d62013-01-08 23:07:17 +020029#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020030#include "client.h"
Oren Weilfb7d8792011-05-15 13:43:42 +030031
32
33/**
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020034 * mei_cl_complete_handler - processes completed operation for a client
Oren Weilfb7d8792011-05-15 13:43:42 +030035 *
36 * @cl: private data of the file object.
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020037 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +030038 */
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020039static void mei_cl_complete_handler(struct mei_cl *cl, struct mei_cl_cb *cb)
Oren Weilfb7d8792011-05-15 13:43:42 +030040{
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020041 if (cb->fop_type == MEI_FOP_WRITE) {
42 mei_io_cb_free(cb);
43 cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +030044 cl->writing_state = MEI_WRITE_COMPLETE;
45 if (waitqueue_active(&cl->tx_wait))
46 wake_up_interruptible(&cl->tx_wait);
47
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020048 } else if (cb->fop_type == MEI_FOP_READ &&
Oren Weilfb7d8792011-05-15 13:43:42 +030049 MEI_READING == cl->reading_state) {
50 cl->reading_state = MEI_READ_COMPLETE;
51 if (waitqueue_active(&cl->rx_wait))
52 wake_up_interruptible(&cl->rx_wait);
53
54 }
55}
56
57/**
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020058 * mei_irq_compl_handler - dispatch complete handelers
59 * for the completed callbacks
60 *
61 * @dev - mei device
62 * @compl_list - list of completed cbs
63 */
64void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
65{
66 struct mei_cl_cb *cb, *next;
67 struct mei_cl *cl;
68
69 list_for_each_entry_safe(cb, next, &compl_list->list, list) {
70 cl = cb->cl;
71 list_del(&cb->list);
72 if (!cl)
73 continue;
74
75 dev_dbg(&dev->pdev->dev, "completing call back.\n");
76 if (cl == &dev->iamthif_cl)
77 mei_amthif_complete(dev, cb);
78 else
79 mei_cl_complete_handler(cl, cb);
80 }
81}
Tomas Winkler40e0b672013-03-27 16:58:30 +020082EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020083/**
Oren Weilfb7d8792011-05-15 13:43:42 +030084 * _mei_irq_thread_state_ok - checks if mei header matches file private data
85 *
86 * @cl: private data of the file object
87 * @mei_hdr: header of mei client message
88 *
89 * returns !=0 if matches, 0 if no match.
90 */
91static int _mei_irq_thread_state_ok(struct mei_cl *cl,
92 struct mei_msg_hdr *mei_hdr)
93{
94 return (cl->host_client_id == mei_hdr->host_addr &&
95 cl->me_client_id == mei_hdr->me_addr &&
96 cl->state == MEI_FILE_CONNECTED &&
97 MEI_READ_COMPLETE != cl->reading_state);
98}
99
100/**
101 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
102 * handle the read mei client message data processing.
103 *
104 * @complete_list: An instance of our list structure
105 * @dev: the device structure
106 * @mei_hdr: header of mei client message
107 *
108 * returns 0 on success, <0 on failure.
109 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200110static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
Oren Weilfb7d8792011-05-15 13:43:42 +0300111 struct mei_device *dev,
112 struct mei_msg_hdr *mei_hdr)
113{
114 struct mei_cl *cl;
115 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
Tomas Winkler479bc592011-06-16 00:46:03 +0300116 unsigned char *buffer = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300117
118 dev_dbg(&dev->pdev->dev, "start client msg\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200119 if (list_empty(&dev->read_list.list))
Oren Weilfb7d8792011-05-15 13:43:42 +0300120 goto quit;
121
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200122 list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200123 cl = cb_pos->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300124 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
125 cl->reading_state = MEI_READING;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200126 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300127
128 if (cb_pos->response_buffer.size <
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200129 mei_hdr->length + cb_pos->buf_idx) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300130 dev_dbg(&dev->pdev->dev, "message overflow.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200131 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300132 return -ENOMEM;
133 }
134 if (buffer)
135 mei_read_slots(dev, buffer, mei_hdr->length);
136
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200137 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300138 if (mei_hdr->msg_complete) {
139 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200140 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300141 dev_dbg(&dev->pdev->dev,
Tomas Winklera4136b42012-09-11 00:43:22 +0300142 "completed read H cl = %d, ME cl = %d, length = %lu\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300143 cl->host_client_id,
144 cl->me_client_id,
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200145 cb_pos->buf_idx);
146
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200147 list_add_tail(&cb_pos->list,
148 &complete_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300149 }
150
151 break;
152 }
153
154 }
155
156quit:
157 dev_dbg(&dev->pdev->dev, "message read\n");
158 if (!buffer) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200159 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200160 dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
161 MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300162 }
163
164 return 0;
165}
166
167/**
Oren Weilfb7d8792011-05-15 13:43:42 +0300168 * _mei_irq_thread_close - processes close related operation.
169 *
170 * @dev: the device structure.
171 * @slots: free slots.
172 * @cb_pos: callback block.
173 * @cl: private data of the file object.
174 * @cmpl_list: complete list.
175 *
176 * returns 0, OK; otherwise, error.
177 */
178static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
179 struct mei_cl_cb *cb_pos,
180 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200181 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300182{
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200183 u32 msg_slots =
184 mei_data2slots(sizeof(struct hbm_client_connect_request));
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300185
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200186 if (*slots < msg_slots)
187 return -EMSGSIZE;
188
189 *slots -= msg_slots;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300190
Tomas Winkler8120e722012-12-25 19:06:11 +0200191 if (mei_hbm_cl_disconnect_req(dev, cl)) {
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300192 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200193 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200194 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200195 return -EIO;
Oren Weilfb7d8792011-05-15 13:43:42 +0300196 }
197
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200198 cl->state = MEI_FILE_DISCONNECTING;
199 cl->status = 0;
200 cb_pos->buf_idx = 0;
201 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
202 cl->timer_count = MEI_CONNECT_TIMEOUT;
203
Oren Weilfb7d8792011-05-15 13:43:42 +0300204 return 0;
205}
206
Oren Weilfb7d8792011-05-15 13:43:42 +0300207
208/**
209 * _mei_hb_read - processes read related operation.
210 *
211 * @dev: the device structure.
212 * @slots: free slots.
213 * @cb_pos: callback block.
214 * @cl: private data of the file object.
215 * @cmpl_list: complete list.
216 *
217 * returns 0, OK; otherwise, error.
218 */
219static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
220 struct mei_cl_cb *cb_pos,
221 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200222 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300223{
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200224 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
225
226 if (*slots < msg_slots) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300227 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200228 list_del(&cb_pos->list);
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200229 return -EMSGSIZE;
Oren Weilfb7d8792011-05-15 13:43:42 +0300230 }
231
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200232 *slots -= msg_slots;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300233
Tomas Winkler8120e722012-12-25 19:06:11 +0200234 if (mei_hbm_cl_flow_control_req(dev, cl)) {
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200235 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200236 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200237 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200238 return -ENODEV;
239 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200240 list_move_tail(&cb_pos->list, &dev->read_list.list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200241
Oren Weilfb7d8792011-05-15 13:43:42 +0300242 return 0;
243}
244
245
246/**
247 * _mei_irq_thread_ioctl - processes ioctl related operation.
248 *
249 * @dev: the device structure.
250 * @slots: free slots.
251 * @cb_pos: callback block.
252 * @cl: private data of the file object.
253 * @cmpl_list: complete list.
254 *
255 * returns 0, OK; otherwise, error.
256 */
257static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
258 struct mei_cl_cb *cb_pos,
259 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200260 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300261{
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200262 u32 msg_slots =
263 mei_data2slots(sizeof(struct hbm_client_connect_request));
264
265 if (*slots < msg_slots) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300266 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200267 list_del(&cb_pos->list);
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200268 return -EMSGSIZE;
Oren Weilfb7d8792011-05-15 13:43:42 +0300269 }
270
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200271 *slots -= msg_slots;
272
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300273 cl->state = MEI_FILE_CONNECTING;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200274
Tomas Winkler8120e722012-12-25 19:06:11 +0200275 if (mei_hbm_cl_connect_req(dev, cl)) {
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300276 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200277 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200278 list_del(&cb_pos->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300279 return -ENODEV;
280 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200281 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300282 cl->timer_count = MEI_CONNECT_TIMEOUT;
283 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300284 return 0;
285}
286
287/**
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200288 * mei_irq_thread_write_complete - write messages to device.
Oren Weilfb7d8792011-05-15 13:43:42 +0300289 *
290 * @dev: the device structure.
291 * @slots: free slots.
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200292 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300293 * @cmpl_list: complete list.
294 *
295 * returns 0, OK; otherwise, error.
296 */
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200297static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
298 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300299{
Tomas Winklere46f1872012-12-25 19:06:10 +0200300 struct mei_msg_hdr mei_hdr;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200301 struct mei_cl *cl = cb->cl;
302 size_t len = cb->request_buffer.size - cb->buf_idx;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200303 u32 msg_slots = mei_data2slots(len);
Oren Weilfb7d8792011-05-15 13:43:42 +0300304
Tomas Winklere46f1872012-12-25 19:06:10 +0200305 mei_hdr.host_addr = cl->host_client_id;
306 mei_hdr.me_addr = cl->me_client_id;
307 mei_hdr.reserved = 0;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200308
309 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200310 mei_hdr.length = len;
311 mei_hdr.msg_complete = 1;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200312 /* Split the message only if we can write the whole host buffer */
Tomas Winkler24aadc82012-06-25 23:46:27 +0300313 } else if (*slots == dev->hbuf_depth) {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200314 msg_slots = *slots;
315 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200316 mei_hdr.length = len;
317 mei_hdr.msg_complete = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300318 } else {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200319 /* wait for next time the host buffer is empty */
320 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300321 }
322
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200323 dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
324 cb->request_buffer.size, cb->buf_idx);
Tomas Winklere46f1872012-12-25 19:06:10 +0200325 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200326
327 *slots -= msg_slots;
Tomas Winklere46f1872012-12-25 19:06:10 +0200328 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200329 cb->request_buffer.data + cb->buf_idx)) {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200330 cl->status = -ENODEV;
331 list_move_tail(&cb->list, &cmpl_list->list);
332 return -ENODEV;
333 }
334
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200335 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200336 return -ENODEV;
337
338 cl->status = 0;
Tomas Winklere46f1872012-12-25 19:06:10 +0200339 cb->buf_idx += mei_hdr.length;
340 if (mei_hdr.msg_complete)
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200341 list_move_tail(&cb->list, &dev->write_waiting_list.list);
342
Oren Weilfb7d8792011-05-15 13:43:42 +0300343 return 0;
344}
345
346/**
Oren Weilfb7d8792011-05-15 13:43:42 +0300347 * mei_irq_thread_read_handler - bottom half read routine after ISR to
348 * handle the read processing.
349 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300350 * @dev: the device structure
Tomas Winkler06ecd642013-02-06 14:06:42 +0200351 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300352 * @slots: slots to read.
353 *
354 * returns 0 on success, <0 on failure.
355 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200356int mei_irq_read_handler(struct mei_device *dev,
357 struct mei_cl_cb *cmpl_list, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300358{
359 struct mei_msg_hdr *mei_hdr;
360 struct mei_cl *cl_pos = NULL;
361 struct mei_cl *cl_next = NULL;
362 int ret = 0;
363
364 if (!dev->rd_msg_hdr) {
Tomas Winkler827eef52013-02-06 14:06:41 +0200365 dev->rd_msg_hdr = mei_read_hdr(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300366 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
367 (*slots)--;
368 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
369 }
370 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200371 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300372
373 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
374 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
375 ret = -EBADMSG;
376 goto end;
377 }
378
379 if (mei_hdr->host_addr || mei_hdr->me_addr) {
380 list_for_each_entry_safe(cl_pos, cl_next,
381 &dev->file_list, link) {
382 dev_dbg(&dev->pdev->dev,
383 "list_for_each_entry_safe read host"
384 " client = %d, ME client = %d\n",
385 cl_pos->host_client_id,
386 cl_pos->me_client_id);
387 if (cl_pos->host_client_id == mei_hdr->host_addr &&
388 cl_pos->me_client_id == mei_hdr->me_addr)
389 break;
390 }
391
392 if (&cl_pos->link == &dev->file_list) {
393 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
394 ret = -EBADMSG;
395 goto end;
396 }
397 }
398 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
399 dev_dbg(&dev->pdev->dev,
400 "we can't read the message slots =%08x.\n",
401 *slots);
402 /* we can't read the message */
403 ret = -ERANGE;
404 goto end;
405 }
406
407 /* decide where to read the message too */
408 if (!mei_hdr->host_addr) {
409 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
Tomas Winklerbb1b0132012-12-25 19:06:07 +0200410 mei_hbm_dispatch(dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300411 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
412 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
413 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
414 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
415 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200416
417 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200418
419 ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300420 if (ret)
421 goto end;
Oren Weilfb7d8792011-05-15 13:43:42 +0300422 } else {
423 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
424 ret = mei_irq_thread_read_client_message(cmpl_list,
425 dev, mei_hdr);
426 if (ret)
427 goto end;
428
429 }
430
431 /* reset the number of slots and header */
432 *slots = mei_count_full_read_slots(dev);
433 dev->rd_msg_hdr = 0;
434
435 if (*slots == -EOVERFLOW) {
436 /* overflow - reset */
437 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
438 /* set the event since message has been read */
439 ret = -ERANGE;
440 goto end;
441 }
442end:
443 return ret;
444}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200445EXPORT_SYMBOL_GPL(mei_irq_read_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300446
447
448/**
Tomas Winkler06ecd642013-02-06 14:06:42 +0200449 * mei_irq_write_handler - dispatch write requests
450 * after irq received
Oren Weilfb7d8792011-05-15 13:43:42 +0300451 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300452 * @dev: the device structure
Tomas Winkler9a84d612012-11-18 15:13:18 +0200453 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300454 *
455 * returns 0 on success, <0 on failure.
456 */
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200457int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300458{
459
460 struct mei_cl *cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200461 struct mei_cl_cb *pos = NULL, *next = NULL;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200462 struct mei_cl_cb *list;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200463 s32 slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300464 int ret;
465
Tomas Winkler827eef52013-02-06 14:06:41 +0200466 if (!mei_hbuf_is_ready(dev)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300467 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
468 return 0;
469 }
Tomas Winkler9a84d612012-11-18 15:13:18 +0200470 slots = mei_hbuf_empty_slots(dev);
471 if (slots <= 0)
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300472 return -EMSGSIZE;
473
Oren Weilfb7d8792011-05-15 13:43:42 +0300474 /* complete all waiting for write CB */
475 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
476
477 list = &dev->write_waiting_list;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200478 list_for_each_entry_safe(pos, next, &list->list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200479 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200480 if (cl == NULL)
481 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +0300482
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200483 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200484 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200485 if (MEI_WRITING == cl->writing_state &&
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200486 pos->fop_type == MEI_FOP_WRITE &&
487 cl != &dev->iamthif_cl) {
Tomas Winkler483136e2012-07-04 19:24:54 +0300488 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200489 cl->writing_state = MEI_WRITE_COMPLETE;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200490 list_add_tail(&pos->list, &cmpl_list->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200491 }
492 if (cl == &dev->iamthif_cl) {
493 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
494 if (dev->iamthif_flow_control_pending) {
Tomas Winkler9a84d612012-11-18 15:13:18 +0200495 ret = mei_amthif_irq_read(dev, &slots);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200496 if (ret)
497 return ret;
498 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300499 }
500 }
501
Tomas Winklerc216fde2012-08-16 19:39:43 +0300502 if (dev->wd_state == MEI_WD_STOPPING) {
503 dev->wd_state = MEI_WD_IDLE;
Oren Weilfb7d8792011-05-15 13:43:42 +0300504 wake_up_interruptible(&dev->wait_stop_wd);
Oren Weilfb7d8792011-05-15 13:43:42 +0300505 }
506
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200507 if (dev->wr_ext_msg.hdr.length) {
508 mei_write_message(dev, &dev->wr_ext_msg.hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200509 dev->wr_ext_msg.data);
Tomas Winkler9a84d612012-11-18 15:13:18 +0200510 slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200511 dev->wr_ext_msg.hdr.length = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300512 }
Tomas Winklerb210d752012-08-07 00:03:56 +0300513 if (dev->dev_state == MEI_DEV_ENABLED) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300514 if (dev->wd_pending &&
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200515 mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300516 if (mei_wd_send(dev))
517 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200518 else if (mei_cl_flow_ctrl_reduce(&dev->wd_cl))
Tomas Winkler483136e2012-07-04 19:24:54 +0300519 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +0300520
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300521 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300522
Tomas Winklerc216fde2012-08-16 19:39:43 +0300523 if (dev->wd_state == MEI_WD_RUNNING)
Tomas Winkler9a84d612012-11-18 15:13:18 +0200524 slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300525 else
Tomas Winkler9a84d612012-11-18 15:13:18 +0200526 slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
Oren Weilfb7d8792011-05-15 13:43:42 +0300527 }
528 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300529
530 /* complete control write list CB */
Tomas Winklerc8372092011-11-27 21:43:33 +0200531 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200532 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200533 cl = pos->cl;
Tomas Winklerc8372092011-11-27 21:43:33 +0200534 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200535 list_del(&pos->list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200536 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +0300537 }
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200538 switch (pos->fop_type) {
539 case MEI_FOP_CLOSE:
Tomas Winklerc8372092011-11-27 21:43:33 +0200540 /* send disconnect message */
Tomas Winkler9a84d612012-11-18 15:13:18 +0200541 ret = _mei_irq_thread_close(dev, &slots, pos,
542 cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200543 if (ret)
544 return ret;
545
546 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200547 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +0200548 /* send flow control message */
Tomas Winkler9a84d612012-11-18 15:13:18 +0200549 ret = _mei_irq_thread_read(dev, &slots, pos,
550 cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200551 if (ret)
552 return ret;
553
554 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200555 case MEI_FOP_IOCTL:
Tomas Winklerc8372092011-11-27 21:43:33 +0200556 /* connect message */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200557 if (mei_cl_is_other_connecting(cl))
Tomas Winklerc8372092011-11-27 21:43:33 +0200558 continue;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200559 ret = _mei_irq_thread_ioctl(dev, &slots, pos,
560 cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200561 if (ret)
562 return ret;
563
564 break;
565
566 default:
567 BUG();
568 }
569
Oren Weilfb7d8792011-05-15 13:43:42 +0300570 }
571 /* complete write list CB */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200572 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200573 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200574 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200575 if (cl == NULL)
576 continue;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200577 if (mei_cl_flow_ctrl_creds(cl) <= 0) {
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200578 dev_dbg(&dev->pdev->dev,
579 "No flow control credentials for client %d, not sending.\n",
580 cl->host_client_id);
581 continue;
582 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300583
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200584 if (cl == &dev->iamthif_cl)
Tomas Winkler9a84d612012-11-18 15:13:18 +0200585 ret = mei_amthif_irq_write_complete(dev, &slots,
Tomas Winkler24c656e2012-11-18 15:13:17 +0200586 pos, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200587 else
588 ret = mei_irq_thread_write_complete(dev, &slots, pos,
589 cmpl_list);
590 if (ret)
591 return ret;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200592
Oren Weilfb7d8792011-05-15 13:43:42 +0300593 }
594 return 0;
595}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200596EXPORT_SYMBOL_GPL(mei_irq_write_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300597
598
599
600/**
601 * mei_timer - timer function.
602 *
603 * @work: pointer to the work_struct structure
604 *
605 * NOTE: This function is called by timer interrupt work
606 */
Oren Weila61c6532011-09-07 09:03:13 +0300607void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +0300608{
609 unsigned long timeout;
610 struct mei_cl *cl_pos = NULL;
611 struct mei_cl *cl_next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300612 struct mei_cl_cb *cb_pos = NULL;
613 struct mei_cl_cb *cb_next = NULL;
614
615 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +0300616 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +0300617
618
619 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300620 if (dev->dev_state != MEI_DEV_ENABLED) {
621 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300622 if (dev->init_clients_timer) {
623 if (--dev->init_clients_timer == 0) {
624 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
625 dev->init_clients_state);
626 mei_reset(dev, 1);
627 }
628 }
629 }
630 goto out;
631 }
632 /*** connect/disconnect timeouts ***/
633 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
634 if (cl_pos->timer_count) {
635 if (--cl_pos->timer_count == 0) {
636 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
637 mei_reset(dev, 1);
638 goto out;
639 }
640 }
641 }
642
Oren Weilfb7d8792011-05-15 13:43:42 +0300643 if (dev->iamthif_stall_timer) {
644 if (--dev->iamthif_stall_timer == 0) {
Masanari Iida32de21f2012-01-25 23:14:56 +0900645 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300646 mei_reset(dev, 1);
647 dev->iamthif_msg_buf_size = 0;
648 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300649 dev->iamthif_canceled = false;
650 dev->iamthif_ioctl = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300651 dev->iamthif_state = MEI_IAMTHIF_IDLE;
652 dev->iamthif_timer = 0;
653
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200654 mei_io_cb_free(dev->iamthif_current_cb);
655 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300656
657 dev->iamthif_file_object = NULL;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200658 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300659 }
660 }
661
662 if (dev->iamthif_timer) {
663
664 timeout = dev->iamthif_timer +
Tomas Winkler3870c322012-11-01 21:17:14 +0200665 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilfb7d8792011-05-15 13:43:42 +0300666
667 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
668 dev->iamthif_timer);
669 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
670 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
671 if (time_after(jiffies, timeout)) {
672 /*
673 * User didn't read the AMTHI data on time (15sec)
674 * freeing AMTHI for other requests
675 */
676
677 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
678
Tomas Winklere773efc2012-11-11 17:37:58 +0200679 list_for_each_entry_safe(cb_pos, cb_next,
680 &dev->amthif_rd_complete_list.list, list) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300681
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200682 cl_pos = cb_pos->file_object->private_data;
Oren Weilfb7d8792011-05-15 13:43:42 +0300683
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200684 /* Finding the AMTHI entry. */
685 if (cl_pos == &dev->iamthif_cl)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200686 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300687 }
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200688 mei_io_cb_free(dev->iamthif_current_cb);
689 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300690
691 dev->iamthif_file_object->private_data = NULL;
692 dev->iamthif_file_object = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300693 dev->iamthif_timer = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200694 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300695
696 }
697 }
698out:
Tomas Winkler441ab502011-12-13 23:39:34 +0200699 schedule_delayed_work(&dev->timer_work, 2 * HZ);
700 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +0300701}
702