blob: 3535b2676c974f607cd7d6575f30e98ae055aa2d [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
18#include <linux/pci.h>
19#include <linux/kthread.h>
20#include <linux/interrupt.h>
21#include <linux/fs.h>
22#include <linux/jiffies.h>
23
Tomas Winkler4f3afe12012-05-09 16:38:59 +030024#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020025
26#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020027#include "hbm.h"
Tomas Winkler9dc64d62013-01-08 23:07:17 +020028#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020029#include "client.h"
Oren Weilfb7d8792011-05-15 13:43:42 +030030
31
32/**
Tomas Winkler06ecd642013-02-06 14:06:42 +020033 * mei_complete_handler - processes completed operation.
Oren Weilfb7d8792011-05-15 13:43:42 +030034 *
35 * @cl: private data of the file object.
36 * @cb_pos: callback block.
37 */
Tomas Winkler06ecd642013-02-06 14:06:42 +020038void mei_irq_complete_handler(struct mei_cl *cl, struct mei_cl_cb *cb_pos)
Oren Weilfb7d8792011-05-15 13:43:42 +030039{
Tomas Winkler4b8960b2012-11-11 17:38:00 +020040 if (cb_pos->fop_type == MEI_FOP_WRITE) {
Tomas Winkler601a1ef2012-10-09 16:50:20 +020041 mei_io_cb_free(cb_pos);
Oren Weilfb7d8792011-05-15 13:43:42 +030042 cb_pos = NULL;
43 cl->writing_state = MEI_WRITE_COMPLETE;
44 if (waitqueue_active(&cl->tx_wait))
45 wake_up_interruptible(&cl->tx_wait);
46
Tomas Winkler4b8960b2012-11-11 17:38:00 +020047 } else if (cb_pos->fop_type == MEI_FOP_READ &&
Oren Weilfb7d8792011-05-15 13:43:42 +030048 MEI_READING == cl->reading_state) {
49 cl->reading_state = MEI_READ_COMPLETE;
50 if (waitqueue_active(&cl->rx_wait))
51 wake_up_interruptible(&cl->rx_wait);
52
53 }
54}
55
56/**
Oren Weilfb7d8792011-05-15 13:43:42 +030057 * _mei_irq_thread_state_ok - checks if mei header matches file private data
58 *
59 * @cl: private data of the file object
60 * @mei_hdr: header of mei client message
61 *
62 * returns !=0 if matches, 0 if no match.
63 */
64static int _mei_irq_thread_state_ok(struct mei_cl *cl,
65 struct mei_msg_hdr *mei_hdr)
66{
67 return (cl->host_client_id == mei_hdr->host_addr &&
68 cl->me_client_id == mei_hdr->me_addr &&
69 cl->state == MEI_FILE_CONNECTED &&
70 MEI_READ_COMPLETE != cl->reading_state);
71}
72
73/**
74 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
75 * handle the read mei client message data processing.
76 *
77 * @complete_list: An instance of our list structure
78 * @dev: the device structure
79 * @mei_hdr: header of mei client message
80 *
81 * returns 0 on success, <0 on failure.
82 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +020083static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
Oren Weilfb7d8792011-05-15 13:43:42 +030084 struct mei_device *dev,
85 struct mei_msg_hdr *mei_hdr)
86{
87 struct mei_cl *cl;
88 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
Tomas Winkler479bc592011-06-16 00:46:03 +030089 unsigned char *buffer = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +030090
91 dev_dbg(&dev->pdev->dev, "start client msg\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +020092 if (list_empty(&dev->read_list.list))
Oren Weilfb7d8792011-05-15 13:43:42 +030093 goto quit;
94
Tomas Winklerfb601ad2012-10-15 12:06:48 +020095 list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +020096 cl = cb_pos->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +030097 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
98 cl->reading_state = MEI_READING;
Tomas Winklerebb108ef2012-10-09 16:50:16 +020099 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300100
101 if (cb_pos->response_buffer.size <
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200102 mei_hdr->length + cb_pos->buf_idx) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300103 dev_dbg(&dev->pdev->dev, "message overflow.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200104 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300105 return -ENOMEM;
106 }
107 if (buffer)
108 mei_read_slots(dev, buffer, mei_hdr->length);
109
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200110 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300111 if (mei_hdr->msg_complete) {
112 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200113 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300114 dev_dbg(&dev->pdev->dev,
Tomas Winklera4136b42012-09-11 00:43:22 +0300115 "completed read H cl = %d, ME cl = %d, length = %lu\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300116 cl->host_client_id,
117 cl->me_client_id,
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200118 cb_pos->buf_idx);
119
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200120 list_add_tail(&cb_pos->list,
121 &complete_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300122 }
123
124 break;
125 }
126
127 }
128
129quit:
130 dev_dbg(&dev->pdev->dev, "message read\n");
131 if (!buffer) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200132 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200133 dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
134 MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300135 }
136
137 return 0;
138}
139
140/**
Oren Weilfb7d8792011-05-15 13:43:42 +0300141 * _mei_irq_thread_close - processes close related operation.
142 *
143 * @dev: the device structure.
144 * @slots: free slots.
145 * @cb_pos: callback block.
146 * @cl: private data of the file object.
147 * @cmpl_list: complete list.
148 *
149 * returns 0, OK; otherwise, error.
150 */
151static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
152 struct mei_cl_cb *cb_pos,
153 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200154 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300155{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300156 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200157 sizeof(struct hbm_client_connect_request)))
Oren Weilfb7d8792011-05-15 13:43:42 +0300158 return -EBADMSG;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300159
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200160 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300161
Tomas Winkler8120e722012-12-25 19:06:11 +0200162 if (mei_hbm_cl_disconnect_req(dev, cl)) {
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300163 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200164 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200165 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300166 return -EMSGSIZE;
167 } else {
168 cl->state = MEI_FILE_DISCONNECTING;
169 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200170 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200171 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300172 cl->timer_count = MEI_CONNECT_TIMEOUT;
Oren Weilfb7d8792011-05-15 13:43:42 +0300173 }
174
175 return 0;
176}
177
Oren Weilfb7d8792011-05-15 13:43:42 +0300178
179/**
180 * _mei_hb_read - processes read related operation.
181 *
182 * @dev: the device structure.
183 * @slots: free slots.
184 * @cb_pos: callback block.
185 * @cl: private data of the file object.
186 * @cmpl_list: complete list.
187 *
188 * returns 0, OK; otherwise, error.
189 */
190static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
191 struct mei_cl_cb *cb_pos,
192 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200193 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300194{
Tomas Winkler1e69d642012-05-29 16:39:12 +0300195 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300196 sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300197 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200198 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300199 return -EBADMSG;
200 }
201
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300202 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
203
Tomas Winkler8120e722012-12-25 19:06:11 +0200204 if (mei_hbm_cl_flow_control_req(dev, cl)) {
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200205 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200206 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200207 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200208 return -ENODEV;
209 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200210 list_move_tail(&cb_pos->list, &dev->read_list.list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200211
Oren Weilfb7d8792011-05-15 13:43:42 +0300212 return 0;
213}
214
215
216/**
217 * _mei_irq_thread_ioctl - processes ioctl related operation.
218 *
219 * @dev: the device structure.
220 * @slots: free slots.
221 * @cb_pos: callback block.
222 * @cl: private data of the file object.
223 * @cmpl_list: complete list.
224 *
225 * returns 0, OK; otherwise, error.
226 */
227static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
228 struct mei_cl_cb *cb_pos,
229 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200230 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300231{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300232 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300233 sizeof(struct hbm_client_connect_request))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300234 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200235 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300236 return -EBADMSG;
237 }
238
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300239 cl->state = MEI_FILE_CONNECTING;
Tomas Winkler8120e722012-12-25 19:06:11 +0200240 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
241 if (mei_hbm_cl_connect_req(dev, cl)) {
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300242 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200243 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200244 list_del(&cb_pos->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300245 return -ENODEV;
246 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200247 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300248 cl->timer_count = MEI_CONNECT_TIMEOUT;
249 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300250 return 0;
251}
252
253/**
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200254 * mei_irq_thread_write_complete - write messages to device.
Oren Weilfb7d8792011-05-15 13:43:42 +0300255 *
256 * @dev: the device structure.
257 * @slots: free slots.
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200258 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300259 * @cmpl_list: complete list.
260 *
261 * returns 0, OK; otherwise, error.
262 */
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200263static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
264 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300265{
Tomas Winklere46f1872012-12-25 19:06:10 +0200266 struct mei_msg_hdr mei_hdr;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200267 struct mei_cl *cl = cb->cl;
268 size_t len = cb->request_buffer.size - cb->buf_idx;
269 size_t msg_slots = mei_data2slots(len);
Oren Weilfb7d8792011-05-15 13:43:42 +0300270
Tomas Winklere46f1872012-12-25 19:06:10 +0200271 mei_hdr.host_addr = cl->host_client_id;
272 mei_hdr.me_addr = cl->me_client_id;
273 mei_hdr.reserved = 0;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200274
275 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200276 mei_hdr.length = len;
277 mei_hdr.msg_complete = 1;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200278 /* Split the message only if we can write the whole host buffer */
Tomas Winkler24aadc82012-06-25 23:46:27 +0300279 } else if (*slots == dev->hbuf_depth) {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200280 msg_slots = *slots;
281 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200282 mei_hdr.length = len;
283 mei_hdr.msg_complete = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300284 } else {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200285 /* wait for next time the host buffer is empty */
286 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300287 }
288
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200289 dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
290 cb->request_buffer.size, cb->buf_idx);
Tomas Winklere46f1872012-12-25 19:06:10 +0200291 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200292
293 *slots -= msg_slots;
Tomas Winklere46f1872012-12-25 19:06:10 +0200294 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200295 cb->request_buffer.data + cb->buf_idx)) {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200296 cl->status = -ENODEV;
297 list_move_tail(&cb->list, &cmpl_list->list);
298 return -ENODEV;
299 }
300
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200301 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200302 return -ENODEV;
303
304 cl->status = 0;
Tomas Winklere46f1872012-12-25 19:06:10 +0200305 cb->buf_idx += mei_hdr.length;
306 if (mei_hdr.msg_complete)
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200307 list_move_tail(&cb->list, &dev->write_waiting_list.list);
308
Oren Weilfb7d8792011-05-15 13:43:42 +0300309 return 0;
310}
311
312/**
Oren Weilfb7d8792011-05-15 13:43:42 +0300313 * mei_irq_thread_read_handler - bottom half read routine after ISR to
314 * handle the read processing.
315 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300316 * @dev: the device structure
Tomas Winkler06ecd642013-02-06 14:06:42 +0200317 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300318 * @slots: slots to read.
319 *
320 * returns 0 on success, <0 on failure.
321 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200322int mei_irq_read_handler(struct mei_device *dev,
323 struct mei_cl_cb *cmpl_list, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300324{
325 struct mei_msg_hdr *mei_hdr;
326 struct mei_cl *cl_pos = NULL;
327 struct mei_cl *cl_next = NULL;
328 int ret = 0;
329
330 if (!dev->rd_msg_hdr) {
Tomas Winkler827eef52013-02-06 14:06:41 +0200331 dev->rd_msg_hdr = mei_read_hdr(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300332 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
333 (*slots)--;
334 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
335 }
336 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200337 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300338
339 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
340 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
341 ret = -EBADMSG;
342 goto end;
343 }
344
345 if (mei_hdr->host_addr || mei_hdr->me_addr) {
346 list_for_each_entry_safe(cl_pos, cl_next,
347 &dev->file_list, link) {
348 dev_dbg(&dev->pdev->dev,
349 "list_for_each_entry_safe read host"
350 " client = %d, ME client = %d\n",
351 cl_pos->host_client_id,
352 cl_pos->me_client_id);
353 if (cl_pos->host_client_id == mei_hdr->host_addr &&
354 cl_pos->me_client_id == mei_hdr->me_addr)
355 break;
356 }
357
358 if (&cl_pos->link == &dev->file_list) {
359 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
360 ret = -EBADMSG;
361 goto end;
362 }
363 }
364 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
365 dev_dbg(&dev->pdev->dev,
366 "we can't read the message slots =%08x.\n",
367 *slots);
368 /* we can't read the message */
369 ret = -ERANGE;
370 goto end;
371 }
372
373 /* decide where to read the message too */
374 if (!mei_hdr->host_addr) {
375 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
Tomas Winklerbb1b0132012-12-25 19:06:07 +0200376 mei_hbm_dispatch(dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300377 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
378 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
379 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
380 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
381 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200382
383 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200384
385 ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300386 if (ret)
387 goto end;
Oren Weilfb7d8792011-05-15 13:43:42 +0300388 } else {
389 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
390 ret = mei_irq_thread_read_client_message(cmpl_list,
391 dev, mei_hdr);
392 if (ret)
393 goto end;
394
395 }
396
397 /* reset the number of slots and header */
398 *slots = mei_count_full_read_slots(dev);
399 dev->rd_msg_hdr = 0;
400
401 if (*slots == -EOVERFLOW) {
402 /* overflow - reset */
403 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
404 /* set the event since message has been read */
405 ret = -ERANGE;
406 goto end;
407 }
408end:
409 return ret;
410}
411
412
413/**
Tomas Winkler06ecd642013-02-06 14:06:42 +0200414 * mei_irq_write_handler - dispatch write requests
415 * after irq received
Oren Weilfb7d8792011-05-15 13:43:42 +0300416 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300417 * @dev: the device structure
Tomas Winkler9a84d612012-11-18 15:13:18 +0200418 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300419 *
420 * returns 0 on success, <0 on failure.
421 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200422int mei_irq_write_handler(struct mei_device *dev,
Tomas Winkler9a84d612012-11-18 15:13:18 +0200423 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300424{
425
426 struct mei_cl *cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200427 struct mei_cl_cb *pos = NULL, *next = NULL;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200428 struct mei_cl_cb *list;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200429 s32 slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300430 int ret;
431
Tomas Winkler827eef52013-02-06 14:06:41 +0200432 if (!mei_hbuf_is_ready(dev)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300433 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
434 return 0;
435 }
Tomas Winkler9a84d612012-11-18 15:13:18 +0200436 slots = mei_hbuf_empty_slots(dev);
437 if (slots <= 0)
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300438 return -EMSGSIZE;
439
Oren Weilfb7d8792011-05-15 13:43:42 +0300440 /* complete all waiting for write CB */
441 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
442
443 list = &dev->write_waiting_list;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200444 list_for_each_entry_safe(pos, next, &list->list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200445 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200446 if (cl == NULL)
447 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +0300448
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200449 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200450 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200451 if (MEI_WRITING == cl->writing_state &&
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200452 pos->fop_type == MEI_FOP_WRITE &&
453 cl != &dev->iamthif_cl) {
Tomas Winkler483136e2012-07-04 19:24:54 +0300454 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200455 cl->writing_state = MEI_WRITE_COMPLETE;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200456 list_add_tail(&pos->list, &cmpl_list->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200457 }
458 if (cl == &dev->iamthif_cl) {
459 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
460 if (dev->iamthif_flow_control_pending) {
Tomas Winkler9a84d612012-11-18 15:13:18 +0200461 ret = mei_amthif_irq_read(dev, &slots);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200462 if (ret)
463 return ret;
464 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300465 }
466 }
467
Tomas Winklerc216fde2012-08-16 19:39:43 +0300468 if (dev->wd_state == MEI_WD_STOPPING) {
469 dev->wd_state = MEI_WD_IDLE;
Oren Weilfb7d8792011-05-15 13:43:42 +0300470 wake_up_interruptible(&dev->wait_stop_wd);
Oren Weilfb7d8792011-05-15 13:43:42 +0300471 }
472
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200473 if (dev->wr_ext_msg.hdr.length) {
474 mei_write_message(dev, &dev->wr_ext_msg.hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200475 dev->wr_ext_msg.data);
Tomas Winkler9a84d612012-11-18 15:13:18 +0200476 slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200477 dev->wr_ext_msg.hdr.length = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300478 }
Tomas Winklerb210d752012-08-07 00:03:56 +0300479 if (dev->dev_state == MEI_DEV_ENABLED) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300480 if (dev->wd_pending &&
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200481 mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300482 if (mei_wd_send(dev))
483 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200484 else if (mei_cl_flow_ctrl_reduce(&dev->wd_cl))
Tomas Winkler483136e2012-07-04 19:24:54 +0300485 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +0300486
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300487 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300488
Tomas Winklerc216fde2012-08-16 19:39:43 +0300489 if (dev->wd_state == MEI_WD_RUNNING)
Tomas Winkler9a84d612012-11-18 15:13:18 +0200490 slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300491 else
Tomas Winkler9a84d612012-11-18 15:13:18 +0200492 slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
Oren Weilfb7d8792011-05-15 13:43:42 +0300493 }
494 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300495
496 /* complete control write list CB */
Tomas Winklerc8372092011-11-27 21:43:33 +0200497 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200498 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200499 cl = pos->cl;
Tomas Winklerc8372092011-11-27 21:43:33 +0200500 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200501 list_del(&pos->list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200502 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +0300503 }
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200504 switch (pos->fop_type) {
505 case MEI_FOP_CLOSE:
Tomas Winklerc8372092011-11-27 21:43:33 +0200506 /* send disconnect message */
Tomas Winkler9a84d612012-11-18 15:13:18 +0200507 ret = _mei_irq_thread_close(dev, &slots, pos,
508 cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200509 if (ret)
510 return ret;
511
512 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200513 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +0200514 /* send flow control message */
Tomas Winkler9a84d612012-11-18 15:13:18 +0200515 ret = _mei_irq_thread_read(dev, &slots, pos,
516 cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200517 if (ret)
518 return ret;
519
520 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200521 case MEI_FOP_IOCTL:
Tomas Winklerc8372092011-11-27 21:43:33 +0200522 /* connect message */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200523 if (mei_cl_is_other_connecting(cl))
Tomas Winklerc8372092011-11-27 21:43:33 +0200524 continue;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200525 ret = _mei_irq_thread_ioctl(dev, &slots, pos,
526 cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200527 if (ret)
528 return ret;
529
530 break;
531
532 default:
533 BUG();
534 }
535
Oren Weilfb7d8792011-05-15 13:43:42 +0300536 }
537 /* complete write list CB */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200538 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200539 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200540 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200541 if (cl == NULL)
542 continue;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200543 if (mei_cl_flow_ctrl_creds(cl) <= 0) {
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200544 dev_dbg(&dev->pdev->dev,
545 "No flow control credentials for client %d, not sending.\n",
546 cl->host_client_id);
547 continue;
548 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300549
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200550 if (cl == &dev->iamthif_cl)
Tomas Winkler9a84d612012-11-18 15:13:18 +0200551 ret = mei_amthif_irq_write_complete(dev, &slots,
Tomas Winkler24c656e2012-11-18 15:13:17 +0200552 pos, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200553 else
554 ret = mei_irq_thread_write_complete(dev, &slots, pos,
555 cmpl_list);
556 if (ret)
557 return ret;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200558
Oren Weilfb7d8792011-05-15 13:43:42 +0300559 }
560 return 0;
561}
562
563
564
565/**
566 * mei_timer - timer function.
567 *
568 * @work: pointer to the work_struct structure
569 *
570 * NOTE: This function is called by timer interrupt work
571 */
Oren Weila61c6532011-09-07 09:03:13 +0300572void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +0300573{
574 unsigned long timeout;
575 struct mei_cl *cl_pos = NULL;
576 struct mei_cl *cl_next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300577 struct mei_cl_cb *cb_pos = NULL;
578 struct mei_cl_cb *cb_next = NULL;
579
580 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +0300581 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +0300582
583
584 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300585 if (dev->dev_state != MEI_DEV_ENABLED) {
586 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300587 if (dev->init_clients_timer) {
588 if (--dev->init_clients_timer == 0) {
589 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
590 dev->init_clients_state);
591 mei_reset(dev, 1);
592 }
593 }
594 }
595 goto out;
596 }
597 /*** connect/disconnect timeouts ***/
598 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
599 if (cl_pos->timer_count) {
600 if (--cl_pos->timer_count == 0) {
601 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
602 mei_reset(dev, 1);
603 goto out;
604 }
605 }
606 }
607
Oren Weilfb7d8792011-05-15 13:43:42 +0300608 if (dev->iamthif_stall_timer) {
609 if (--dev->iamthif_stall_timer == 0) {
Masanari Iida32de21f2012-01-25 23:14:56 +0900610 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300611 mei_reset(dev, 1);
612 dev->iamthif_msg_buf_size = 0;
613 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300614 dev->iamthif_canceled = false;
615 dev->iamthif_ioctl = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300616 dev->iamthif_state = MEI_IAMTHIF_IDLE;
617 dev->iamthif_timer = 0;
618
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200619 mei_io_cb_free(dev->iamthif_current_cb);
620 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300621
622 dev->iamthif_file_object = NULL;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200623 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300624 }
625 }
626
627 if (dev->iamthif_timer) {
628
629 timeout = dev->iamthif_timer +
Tomas Winkler3870c322012-11-01 21:17:14 +0200630 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilfb7d8792011-05-15 13:43:42 +0300631
632 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
633 dev->iamthif_timer);
634 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
635 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
636 if (time_after(jiffies, timeout)) {
637 /*
638 * User didn't read the AMTHI data on time (15sec)
639 * freeing AMTHI for other requests
640 */
641
642 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
643
Tomas Winklere773efc2012-11-11 17:37:58 +0200644 list_for_each_entry_safe(cb_pos, cb_next,
645 &dev->amthif_rd_complete_list.list, list) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300646
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200647 cl_pos = cb_pos->file_object->private_data;
Oren Weilfb7d8792011-05-15 13:43:42 +0300648
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200649 /* Finding the AMTHI entry. */
650 if (cl_pos == &dev->iamthif_cl)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200651 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300652 }
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200653 mei_io_cb_free(dev->iamthif_current_cb);
654 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300655
656 dev->iamthif_file_object->private_data = NULL;
657 dev->iamthif_file_object = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300658 dev->iamthif_timer = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200659 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300660
661 }
662 }
663out:
Tomas Winkler441ab502011-12-13 23:39:34 +0200664 schedule_delayed_work(&dev->timer_work, 2 * HZ);
665 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +0300666}
667