blob: 54d6f1a1dbc9a655acaa356e10a4f0d0810243a0 [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
24#include "mei_dev.h"
Tomas Winkler4f3afe12012-05-09 16:38:59 +030025#include <linux/mei.h>
Oren Weilfb7d8792011-05-15 13:43:42 +030026#include "hw.h"
27#include "interface.h"
28
29
30/**
31 * mei_interrupt_quick_handler - The ISR of the MEI device
32 *
33 * @irq: The irq number
34 * @dev_id: pointer to the device structure
35 *
36 * returns irqreturn_t
37 */
38irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
39{
40 struct mei_device *dev = (struct mei_device *) dev_id;
41 u32 csr_reg = mei_hcsr_read(dev);
42
43 if ((csr_reg & H_IS) != H_IS)
44 return IRQ_NONE;
45
46 /* clear H_IS bit in H_CSR */
47 mei_reg_write(dev, H_CSR, csr_reg);
48
49 return IRQ_WAKE_THREAD;
50}
51
52/**
53 * _mei_cmpl - processes completed operation.
54 *
55 * @cl: private data of the file object.
56 * @cb_pos: callback block.
57 */
58static void _mei_cmpl(struct mei_cl *cl, struct mei_cl_cb *cb_pos)
59{
60 if (cb_pos->major_file_operations == MEI_WRITE) {
61 mei_free_cb_private(cb_pos);
62 cb_pos = NULL;
63 cl->writing_state = MEI_WRITE_COMPLETE;
64 if (waitqueue_active(&cl->tx_wait))
65 wake_up_interruptible(&cl->tx_wait);
66
67 } else if (cb_pos->major_file_operations == MEI_READ &&
68 MEI_READING == cl->reading_state) {
69 cl->reading_state = MEI_READ_COMPLETE;
70 if (waitqueue_active(&cl->rx_wait))
71 wake_up_interruptible(&cl->rx_wait);
72
73 }
74}
75
76/**
77 * _mei_cmpl_iamthif - processes completed iamthif operation.
78 *
79 * @dev: the device structure.
80 * @cb_pos: callback block.
81 */
82static void _mei_cmpl_iamthif(struct mei_device *dev, struct mei_cl_cb *cb_pos)
83{
84 if (dev->iamthif_canceled != 1) {
85 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
86 dev->iamthif_stall_timer = 0;
87 memcpy(cb_pos->response_buffer.data,
88 dev->iamthif_msg_buf,
89 dev->iamthif_msg_buf_index);
Tomas Winklerfb601ad2012-10-15 12:06:48 +020090 list_add_tail(&cb_pos->list, &dev->amthi_read_complete_list.list);
91 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
Oren Weilfb7d8792011-05-15 13:43:42 +030092 dev->iamthif_timer = jiffies;
93 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
94 dev->iamthif_timer);
95 } else {
Tomas Winklerc95efb72011-05-25 17:28:21 +030096 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +030097 }
98
99 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
100 wake_up_interruptible(&dev->iamthif_cl.wait);
101}
102
103
104/**
105 * mei_irq_thread_read_amthi_message - bottom half read routine after ISR to
106 * handle the read amthi message data processing.
107 *
108 * @complete_list: An instance of our list structure
109 * @dev: the device structure
110 * @mei_hdr: header of amthi message
111 *
112 * returns 0 on success, <0 on failure.
113 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200114static int mei_irq_thread_read_amthi_message(struct mei_cl_cb *complete_list,
Oren Weilfb7d8792011-05-15 13:43:42 +0300115 struct mei_device *dev,
116 struct mei_msg_hdr *mei_hdr)
117{
118 struct mei_cl *cl;
119 struct mei_cl_cb *cb;
120 unsigned char *buffer;
121
122 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
123 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
124
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200125 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
Oren Weilfb7d8792011-05-15 13:43:42 +0300126 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
127
128 mei_read_slots(dev, buffer, mei_hdr->length);
129
130 dev->iamthif_msg_buf_index += mei_hdr->length;
131
132 if (!mei_hdr->msg_complete)
133 return 0;
134
135 dev_dbg(&dev->pdev->dev,
136 "amthi_message_buffer_index =%d\n",
137 mei_hdr->length);
138
139 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
140 if (!dev->iamthif_current_cb)
141 return -ENODEV;
142
143 cb = dev->iamthif_current_cb;
144 dev->iamthif_current_cb = NULL;
145
146 cl = (struct mei_cl *)cb->file_private;
147 if (!cl)
148 return -ENODEV;
149
150 dev->iamthif_stall_timer = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200151 cb->buf_idx = dev->iamthif_msg_buf_index;
Oren Weilfb7d8792011-05-15 13:43:42 +0300152 cb->read_time = jiffies;
153 if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
154 /* found the iamthif cb */
155 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
156 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200157 list_add_tail(&cb->list, &complete_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300158 }
159 return 0;
160}
161
162/**
163 * _mei_irq_thread_state_ok - checks if mei header matches file private data
164 *
165 * @cl: private data of the file object
166 * @mei_hdr: header of mei client message
167 *
168 * returns !=0 if matches, 0 if no match.
169 */
170static int _mei_irq_thread_state_ok(struct mei_cl *cl,
171 struct mei_msg_hdr *mei_hdr)
172{
173 return (cl->host_client_id == mei_hdr->host_addr &&
174 cl->me_client_id == mei_hdr->me_addr &&
175 cl->state == MEI_FILE_CONNECTED &&
176 MEI_READ_COMPLETE != cl->reading_state);
177}
178
179/**
180 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
181 * handle the read mei client message data processing.
182 *
183 * @complete_list: An instance of our list structure
184 * @dev: the device structure
185 * @mei_hdr: header of mei client message
186 *
187 * returns 0 on success, <0 on failure.
188 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200189static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
Oren Weilfb7d8792011-05-15 13:43:42 +0300190 struct mei_device *dev,
191 struct mei_msg_hdr *mei_hdr)
192{
193 struct mei_cl *cl;
194 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
Tomas Winkler479bc592011-06-16 00:46:03 +0300195 unsigned char *buffer = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300196
197 dev_dbg(&dev->pdev->dev, "start client msg\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200198 if (list_empty(&dev->read_list.list))
Oren Weilfb7d8792011-05-15 13:43:42 +0300199 goto quit;
200
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200201 list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300202 cl = (struct mei_cl *)cb_pos->file_private;
203 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
204 cl->reading_state = MEI_READING;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200205 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300206
207 if (cb_pos->response_buffer.size <
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200208 mei_hdr->length + cb_pos->buf_idx) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300209 dev_dbg(&dev->pdev->dev, "message overflow.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200210 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300211 return -ENOMEM;
212 }
213 if (buffer)
214 mei_read_slots(dev, buffer, mei_hdr->length);
215
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200216 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300217 if (mei_hdr->msg_complete) {
218 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200219 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300220 dev_dbg(&dev->pdev->dev,
Tomas Winklera4136b42012-09-11 00:43:22 +0300221 "completed read H cl = %d, ME cl = %d, length = %lu\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300222 cl->host_client_id,
223 cl->me_client_id,
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200224 cb_pos->buf_idx);
225
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200226 list_add_tail(&cb_pos->list,
227 &complete_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300228 }
229
230 break;
231 }
232
233 }
234
235quit:
236 dev_dbg(&dev->pdev->dev, "message read\n");
237 if (!buffer) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200238 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +0300239 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
240 *(u32 *) dev->rd_msg_buf);
241 }
242
243 return 0;
244}
245
246/**
247 * _mei_irq_thread_iamthif_read - prepares to read iamthif data.
248 *
249 * @dev: the device structure.
250 * @slots: free slots.
251 *
252 * returns 0, OK; otherwise, error.
253 */
254static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots)
255{
256
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200257 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +0300258 + sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300259 return -EMSGSIZE;
260 }
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300261 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200262 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
263 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
264 return -EIO;
265 }
266
267 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
268 dev->iamthif_state = MEI_IAMTHIF_READING;
269 dev->iamthif_flow_control_pending = false;
270 dev->iamthif_msg_buf_index = 0;
271 dev->iamthif_msg_buf_size = 0;
272 dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
Tomas Winkler726917f2012-06-25 23:46:28 +0300273 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200274 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300275}
276
277/**
278 * _mei_irq_thread_close - processes close related operation.
279 *
280 * @dev: the device structure.
281 * @slots: free slots.
282 * @cb_pos: callback block.
283 * @cl: private data of the file object.
284 * @cmpl_list: complete list.
285 *
286 * returns 0, OK; otherwise, error.
287 */
288static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
289 struct mei_cl_cb *cb_pos,
290 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200291 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300292{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300293 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
294 sizeof(struct hbm_client_disconnect_request)))
Oren Weilfb7d8792011-05-15 13:43:42 +0300295 return -EBADMSG;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300296
297 *slots -= mei_data2slots(sizeof(struct hbm_client_disconnect_request));
298
299 if (mei_disconnect(dev, cl)) {
300 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200301 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200302 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300303 return -EMSGSIZE;
304 } else {
305 cl->state = MEI_FILE_DISCONNECTING;
306 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200307 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200308 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300309 cl->timer_count = MEI_CONNECT_TIMEOUT;
Oren Weilfb7d8792011-05-15 13:43:42 +0300310 }
311
312 return 0;
313}
314
315/**
316 * is_treat_specially_client - checks if the message belongs
317 * to the file private data.
318 *
319 * @cl: private data of the file object
320 * @rs: connect response bus message
321 *
322 */
323static bool is_treat_specially_client(struct mei_cl *cl,
324 struct hbm_client_connect_response *rs)
325{
326
327 if (cl->host_client_id == rs->host_addr &&
328 cl->me_client_id == rs->me_addr) {
329 if (!rs->status) {
330 cl->state = MEI_FILE_CONNECTED;
331 cl->status = 0;
332
333 } else {
334 cl->state = MEI_FILE_DISCONNECTED;
335 cl->status = -ENODEV;
336 }
337 cl->timer_count = 0;
338
339 return true;
340 }
341 return false;
342}
343
344/**
345 * mei_client_connect_response - connects to response irq routine
346 *
347 * @dev: the device structure
348 * @rs: connect response bus message
349 */
350static void mei_client_connect_response(struct mei_device *dev,
351 struct hbm_client_connect_response *rs)
352{
353
354 struct mei_cl *cl;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200355 struct mei_cl_cb *pos = NULL, *next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300356
357 dev_dbg(&dev->pdev->dev,
358 "connect_response:\n"
359 "ME Client = %d\n"
360 "Host Client = %d\n"
361 "Status = %d\n",
362 rs->me_addr,
363 rs->host_addr,
364 rs->status);
365
366 /* if WD or iamthif client treat specially */
367
368 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300369 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
Tomas Winkler70cd5332011-12-22 18:50:50 +0200370 mei_watchdog_register(dev);
Oren Weil9ce178e2011-09-07 09:03:09 +0300371
Tomas Winkler70cd5332011-12-22 18:50:50 +0200372 /* next step in the state maching */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300373 mei_host_init_iamthif(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300374 return;
375 }
376
377 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
378 dev->iamthif_state = MEI_IAMTHIF_IDLE;
379 return;
380 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200381 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200382
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200383 cl = (struct mei_cl *)pos->file_private;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200384 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200385 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200386 return;
387 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200388 if (MEI_IOCTL == pos->major_file_operations) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200389 if (is_treat_specially_client(cl, rs)) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200390 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200391 cl->status = 0;
392 cl->timer_count = 0;
393 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300394 }
395 }
396 }
397}
398
399/**
400 * mei_client_disconnect_response - disconnects from response irq routine
401 *
402 * @dev: the device structure
403 * @rs: disconnect response bus message
404 */
405static void mei_client_disconnect_response(struct mei_device *dev,
406 struct hbm_client_connect_response *rs)
407{
408 struct mei_cl *cl;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200409 struct mei_cl_cb *pos = NULL, *next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300410
411 dev_dbg(&dev->pdev->dev,
412 "disconnect_response:\n"
413 "ME Client = %d\n"
414 "Host Client = %d\n"
415 "Status = %d\n",
416 rs->me_addr,
417 rs->host_addr,
418 rs->status);
419
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200420 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
421 cl = (struct mei_cl *)pos->file_private;
Oren Weilfb7d8792011-05-15 13:43:42 +0300422
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200423 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200424 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200425 return;
426 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300427
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200428 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
429 if (cl->host_client_id == rs->host_addr &&
430 cl->me_client_id == rs->me_addr) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300431
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200432 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200433 if (!rs->status)
434 cl->state = MEI_FILE_DISCONNECTED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300435
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200436 cl->status = 0;
437 cl->timer_count = 0;
438 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300439 }
440 }
441}
442
443/**
444 * same_flow_addr - tells if they have the same address.
445 *
446 * @file: private data of the file object.
447 * @flow: flow control.
448 *
449 * returns !=0, same; 0,not.
450 */
451static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
452{
453 return (cl->host_client_id == flow->host_addr &&
454 cl->me_client_id == flow->me_addr);
455}
456
457/**
458 * add_single_flow_creds - adds single buffer credentials.
459 *
460 * @file: private data ot the file object.
461 * @flow: flow control.
462 */
463static void add_single_flow_creds(struct mei_device *dev,
464 struct hbm_flow_control *flow)
465{
466 struct mei_me_client *client;
467 int i;
468
Tomas Winklercf9673d2011-06-06 10:44:33 +0300469 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300470 client = &dev->me_clients[i];
471 if (client && flow->me_addr == client->client_id) {
472 if (client->props.single_recv_buf) {
473 client->mei_flow_ctrl_creds++;
474 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
475 flow->me_addr);
476 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
477 client->mei_flow_ctrl_creds);
478 } else {
479 BUG(); /* error in flow control */
480 }
481 }
482 }
483}
484
485/**
486 * mei_client_flow_control_response - flow control response irq routine
487 *
488 * @dev: the device structure
489 * @flow_control: flow control response bus message
490 */
491static void mei_client_flow_control_response(struct mei_device *dev,
492 struct hbm_flow_control *flow_control)
493{
494 struct mei_cl *cl_pos = NULL;
495 struct mei_cl *cl_next = NULL;
496
497 if (!flow_control->host_addr) {
498 /* single receive buffer */
499 add_single_flow_creds(dev, flow_control);
500 } else {
501 /* normal connection */
502 list_for_each_entry_safe(cl_pos, cl_next,
503 &dev->file_list, link) {
504 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
505
506 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
507 cl_pos->host_client_id,
508 cl_pos->me_client_id);
509 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
510 flow_control->host_addr,
511 flow_control->me_addr);
512 if (same_flow_addr(cl_pos, flow_control)) {
513 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
514 flow_control->host_addr,
515 flow_control->me_addr);
516 cl_pos->mei_flow_ctrl_creds++;
517 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
518 cl_pos->mei_flow_ctrl_creds);
519 break;
520 }
521 }
522 }
523}
524
525/**
526 * same_disconn_addr - tells if they have the same address
527 *
528 * @file: private data of the file object.
529 * @disconn: disconnection request.
530 *
531 * returns !=0, same; 0,not.
532 */
533static int same_disconn_addr(struct mei_cl *cl,
534 struct hbm_client_disconnect_request *disconn)
535{
536 return (cl->host_client_id == disconn->host_addr &&
537 cl->me_client_id == disconn->me_addr);
538}
539
540/**
541 * mei_client_disconnect_request - disconnects from request irq routine
542 *
543 * @dev: the device structure.
544 * @disconnect_req: disconnect request bus message.
545 */
546static void mei_client_disconnect_request(struct mei_device *dev,
547 struct hbm_client_disconnect_request *disconnect_req)
548{
549 struct mei_msg_hdr *mei_hdr;
550 struct hbm_client_connect_response *disconnect_res;
551 struct mei_cl *cl_pos = NULL;
552 struct mei_cl *cl_next = NULL;
553
554 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
555 if (same_disconn_addr(cl_pos, disconnect_req)) {
556 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
557 disconnect_req->host_addr,
558 disconnect_req->me_addr);
559 cl_pos->state = MEI_FILE_DISCONNECTED;
560 cl_pos->timer_count = 0;
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300561 if (cl_pos == &dev->wd_cl)
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300562 dev->wd_pending = false;
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300563 else if (cl_pos == &dev->iamthif_cl)
Oren Weilfb7d8792011-05-15 13:43:42 +0300564 dev->iamthif_timer = 0;
565
566 /* prepare disconnect response */
567 mei_hdr =
568 (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
569 mei_hdr->host_addr = 0;
570 mei_hdr->me_addr = 0;
571 mei_hdr->length =
572 sizeof(struct hbm_client_connect_response);
573 mei_hdr->msg_complete = 1;
574 mei_hdr->reserved = 0;
575
576 disconnect_res =
577 (struct hbm_client_connect_response *)
578 &dev->ext_msg_buf[1];
579 disconnect_res->host_addr = cl_pos->host_client_id;
580 disconnect_res->me_addr = cl_pos->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200581 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300582 disconnect_res->status = 0;
583 dev->extra_write_index = 2;
584 break;
585 }
586 }
587}
588
589
590/**
591 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
592 * handle the read bus message cmd processing.
593 *
594 * @dev: the device structure
595 * @mei_hdr: header of bus message
596 */
597static void mei_irq_thread_read_bus_message(struct mei_device *dev,
598 struct mei_msg_hdr *mei_hdr)
599{
600 struct mei_bus_message *mei_msg;
601 struct hbm_host_version_response *version_res;
602 struct hbm_client_connect_response *connect_res;
603 struct hbm_client_connect_response *disconnect_res;
604 struct hbm_flow_control *flow_control;
605 struct hbm_props_response *props_res;
606 struct hbm_host_enum_response *enum_res;
607 struct hbm_client_disconnect_request *disconnect_req;
608 struct hbm_host_stop_request *host_stop_req;
Oren Weilabc51b62011-09-21 16:45:30 +0300609 int res;
Oren Weilfb7d8792011-05-15 13:43:42 +0300610
Oren Weilfb7d8792011-05-15 13:43:42 +0300611
612 /* read the message to our buffer */
Oren Weilfb7d8792011-05-15 13:43:42 +0300613 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200614 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
615 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
Oren Weilfb7d8792011-05-15 13:43:42 +0300616
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200617 switch (mei_msg->hbm_cmd) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300618 case HOST_START_RES_CMD:
619 version_res = (struct hbm_host_version_response *) mei_msg;
620 if (version_res->host_version_supported) {
621 dev->version.major_version = HBM_MAJOR_VERSION;
622 dev->version.minor_version = HBM_MINOR_VERSION;
Tomas Winklerb210d752012-08-07 00:03:56 +0300623 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300624 dev->init_clients_state == MEI_START_MESSAGE) {
625 dev->init_clients_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300626 mei_host_enum_clients_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300627 } else {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300628 dev->recvd_msg = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300629 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
630 mei_reset(dev, 1);
631 return;
632 }
633 } else {
634 dev->version = version_res->me_max_version;
635 /* send stop message */
Tomas Winkler97d5cb02012-03-06 22:34:32 +0200636 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
Oren Weilfb7d8792011-05-15 13:43:42 +0300637 mei_hdr->host_addr = 0;
638 mei_hdr->me_addr = 0;
639 mei_hdr->length = sizeof(struct hbm_host_stop_request);
640 mei_hdr->msg_complete = 1;
641 mei_hdr->reserved = 0;
642
643 host_stop_req = (struct hbm_host_stop_request *)
644 &dev->wr_msg_buf[1];
645
646 memset(host_stop_req,
647 0,
648 sizeof(struct hbm_host_stop_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200649 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300650 host_stop_req->reason = DRIVER_STOP_REQUEST;
651 mei_write_message(dev, mei_hdr,
652 (unsigned char *) (host_stop_req),
653 mei_hdr->length);
654 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
655 return;
656 }
657
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300658 dev->recvd_msg = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300659 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
660 break;
661
662 case CLIENT_CONNECT_RES_CMD:
663 connect_res =
664 (struct hbm_client_connect_response *) mei_msg;
665 mei_client_connect_response(dev, connect_res);
666 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
667 wake_up(&dev->wait_recvd_msg);
668 break;
669
670 case CLIENT_DISCONNECT_RES_CMD:
671 disconnect_res =
672 (struct hbm_client_connect_response *) mei_msg;
Tomas Winkler441ab502011-12-13 23:39:34 +0200673 mei_client_disconnect_response(dev, disconnect_res);
Oren Weilfb7d8792011-05-15 13:43:42 +0300674 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
675 wake_up(&dev->wait_recvd_msg);
676 break;
677
678 case MEI_FLOW_CONTROL_CMD:
679 flow_control = (struct hbm_flow_control *) mei_msg;
680 mei_client_flow_control_response(dev, flow_control);
681 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
682 break;
683
684 case HOST_CLIENT_PROPERTIES_RES_CMD:
685 props_res = (struct hbm_props_response *)mei_msg;
686 if (props_res->status || !dev->me_clients) {
687 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
688 mei_reset(dev, 1);
689 return;
690 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200691 if (dev->me_clients[dev->me_client_presentation_num]
Oren Weilfb7d8792011-05-15 13:43:42 +0300692 .client_id == props_res->address) {
693
694 dev->me_clients[dev->me_client_presentation_num].props
695 = props_res->client_properties;
696
Tomas Winklerb210d752012-08-07 00:03:56 +0300697 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300698 dev->init_clients_state ==
699 MEI_CLIENT_PROPERTIES_MESSAGE) {
700 dev->me_client_index++;
701 dev->me_client_presentation_num++;
Oren Weilabc51b62011-09-21 16:45:30 +0300702
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700703 /** Send Client Properties request **/
Oren Weilabc51b62011-09-21 16:45:30 +0300704 res = mei_host_client_properties(dev);
705 if (res < 0) {
706 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
707 return;
708 } else if (!res) {
709 /*
710 * No more clients to send to.
711 * Clear Map for indicating now ME clients
712 * with associated host client
713 */
714 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
715 dev->open_handle_count = 0;
716
717 /*
718 * Reserving the first three client IDs
719 * Client Id 0 - Reserved for MEI Bus Message communications
720 * Client Id 1 - Reserved for Watchdog
721 * Client ID 2 - Reserved for AMTHI
722 */
723 bitmap_set(dev->host_clients_map, 0, 3);
Tomas Winklerb210d752012-08-07 00:03:56 +0300724 dev->dev_state = MEI_DEV_ENABLED;
Oren Weilabc51b62011-09-21 16:45:30 +0300725
726 /* if wd initialization fails, initialization the AMTHI client,
727 * otherwise the AMTHI client will be initialized after the WD client connect response
728 * will be received
729 */
730 if (mei_wd_host_init(dev))
731 mei_host_init_iamthif(dev);
732 }
733
Oren Weilfb7d8792011-05-15 13:43:42 +0300734 } else {
735 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
736 mei_reset(dev, 1);
737 return;
738 }
739 } else {
740 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
741 mei_reset(dev, 1);
742 return;
743 }
744 break;
745
746 case HOST_ENUM_RES_CMD:
747 enum_res = (struct hbm_host_enum_response *) mei_msg;
748 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
Tomas Winklerb210d752012-08-07 00:03:56 +0300749 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300750 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
751 dev->init_clients_timer = 0;
752 dev->me_client_presentation_num = 0;
753 dev->me_client_index = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300754 mei_allocate_me_clients_storage(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300755 dev->init_clients_state =
756 MEI_CLIENT_PROPERTIES_MESSAGE;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300757 mei_host_client_properties(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300758 } else {
759 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
760 mei_reset(dev, 1);
761 return;
762 }
763 break;
764
765 case HOST_STOP_RES_CMD:
Tomas Winklerb210d752012-08-07 00:03:56 +0300766 dev->dev_state = MEI_DEV_DISABLED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300767 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
768 mei_reset(dev, 1);
769 break;
770
771 case CLIENT_DISCONNECT_REQ_CMD:
772 /* search for client */
773 disconnect_req =
774 (struct hbm_client_disconnect_request *) mei_msg;
775 mei_client_disconnect_request(dev, disconnect_req);
776 break;
777
778 case ME_STOP_REQ_CMD:
779 /* prepare stop request */
780 mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
781 mei_hdr->host_addr = 0;
782 mei_hdr->me_addr = 0;
783 mei_hdr->length = sizeof(struct hbm_host_stop_request);
784 mei_hdr->msg_complete = 1;
785 mei_hdr->reserved = 0;
786 host_stop_req =
787 (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
788 memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200789 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300790 host_stop_req->reason = DRIVER_STOP_REQUEST;
791 host_stop_req->reserved[0] = 0;
792 host_stop_req->reserved[1] = 0;
793 dev->extra_write_index = 2;
794 break;
795
796 default:
797 BUG();
798 break;
799
800 }
801}
802
803
804/**
805 * _mei_hb_read - processes read related operation.
806 *
807 * @dev: the device structure.
808 * @slots: free slots.
809 * @cb_pos: callback block.
810 * @cl: private data of the file object.
811 * @cmpl_list: complete list.
812 *
813 * returns 0, OK; otherwise, error.
814 */
815static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
816 struct mei_cl_cb *cb_pos,
817 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200818 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300819{
Tomas Winkler1e69d642012-05-29 16:39:12 +0300820 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300821 sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300822 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200823 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300824 return -EBADMSG;
825 }
826
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300827 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
828
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200829 if (mei_send_flow_control(dev, cl)) {
830 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200831 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200832 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200833 return -ENODEV;
834 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200835 list_move_tail(&cb_pos->list, &dev->read_list.list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200836
Oren Weilfb7d8792011-05-15 13:43:42 +0300837 return 0;
838}
839
840
841/**
842 * _mei_irq_thread_ioctl - processes ioctl related operation.
843 *
844 * @dev: the device structure.
845 * @slots: free slots.
846 * @cb_pos: callback block.
847 * @cl: private data of the file object.
848 * @cmpl_list: complete list.
849 *
850 * returns 0, OK; otherwise, error.
851 */
852static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
853 struct mei_cl_cb *cb_pos,
854 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200855 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300856{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300857 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300858 sizeof(struct hbm_client_connect_request))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300859 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200860 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300861 return -EBADMSG;
862 }
863
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300864 cl->state = MEI_FILE_CONNECTING;
865 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
866 if (mei_connect(dev, cl)) {
867 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200868 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200869 list_del(&cb_pos->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300870 return -ENODEV;
871 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200872 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300873 cl->timer_count = MEI_CONNECT_TIMEOUT;
874 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300875 return 0;
876}
877
878/**
879 * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
880 *
881 * @dev: the device structure.
882 * @slots: free slots.
883 * @cb_pos: callback block.
884 * @cl: private data of the file object.
885 * @cmpl_list: complete list.
886 *
887 * returns 0, OK; otherwise, error.
888 */
889static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
890 struct mei_cl_cb *cb_pos,
891 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200892 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300893{
894 struct mei_msg_hdr *mei_hdr;
895
896 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200897 (cb_pos->request_buffer.size - cb_pos->buf_idx))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300898 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
899 mei_hdr->host_addr = cl->host_client_id;
900 mei_hdr->me_addr = cl->me_client_id;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200901 mei_hdr->length = cb_pos->request_buffer.size - cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300902 mei_hdr->msg_complete = 1;
903 mei_hdr->reserved = 0;
904 dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
905 "mei_hdr->msg_complete = %d\n",
906 cb_pos->request_buffer.size,
907 mei_hdr->msg_complete);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200908 dev_dbg(&dev->pdev->dev, "cb_pos->buf_idx =%lu\n",
909 cb_pos->buf_idx);
Oren Weilfb7d8792011-05-15 13:43:42 +0300910 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
911 mei_hdr->length);
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300912 *slots -= mei_data2slots(mei_hdr->length);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200913 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300914 (unsigned char *)
915 (cb_pos->request_buffer.data +
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200916 cb_pos->buf_idx),
Oren Weilfb7d8792011-05-15 13:43:42 +0300917 mei_hdr->length)) {
918 cl->status = -ENODEV;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200919 list_move_tail(&cb_pos->list, &cmpl_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300920 return -ENODEV;
921 } else {
922 if (mei_flow_ctrl_reduce(dev, cl))
923 return -ENODEV;
924 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200925 cb_pos->buf_idx += mei_hdr->length;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200926 list_move_tail(&cb_pos->list, &dev->write_waiting_list.list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300927 }
Tomas Winkler24aadc82012-06-25 23:46:27 +0300928 } else if (*slots == dev->hbuf_depth) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300929 /* buffer is still empty */
930 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
931 mei_hdr->host_addr = cl->host_client_id;
932 mei_hdr->me_addr = cl->me_client_id;
933 mei_hdr->length =
934 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
935 mei_hdr->msg_complete = 0;
936 mei_hdr->reserved = 0;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300937 *slots -= mei_data2slots(mei_hdr->length);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200938 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300939 (unsigned char *)
940 (cb_pos->request_buffer.data +
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200941 cb_pos->buf_idx),
Oren Weilfb7d8792011-05-15 13:43:42 +0300942 mei_hdr->length)) {
943 cl->status = -ENODEV;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200944 list_move_tail(&cb_pos->list, &cmpl_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300945 return -ENODEV;
946 } else {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200947 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300948 dev_dbg(&dev->pdev->dev,
949 "cb_pos->request_buffer.size =%d"
950 " mei_hdr->msg_complete = %d\n",
951 cb_pos->request_buffer.size,
952 mei_hdr->msg_complete);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200953 dev_dbg(&dev->pdev->dev, "cb_pos->buf_idx =%lu\n",
954 cb_pos->buf_idx);
Oren Weilfb7d8792011-05-15 13:43:42 +0300955 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
956 mei_hdr->length);
957 }
958 return -EMSGSIZE;
959 } else {
960 return -EBADMSG;
961 }
962
963 return 0;
964}
965
966/**
967 * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
968 *
969 * @dev: the device structure.
970 * @slots: free slots.
971 * @cb_pos: callback block.
972 * @cl: private data of the file object.
973 * @cmpl_list: complete list.
974 *
975 * returns 0, OK; otherwise, error.
976 */
977static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
978 struct mei_cl_cb *cb_pos,
979 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200980 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300981{
982 struct mei_msg_hdr *mei_hdr;
983
984 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
985 dev->iamthif_msg_buf_size -
986 dev->iamthif_msg_buf_index)) {
987 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
988 mei_hdr->host_addr = cl->host_client_id;
989 mei_hdr->me_addr = cl->me_client_id;
990 mei_hdr->length = dev->iamthif_msg_buf_size -
991 dev->iamthif_msg_buf_index;
992 mei_hdr->msg_complete = 1;
993 mei_hdr->reserved = 0;
994
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300995 *slots -= mei_data2slots(mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +0300996
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200997 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300998 (dev->iamthif_msg_buf +
999 dev->iamthif_msg_buf_index),
1000 mei_hdr->length)) {
1001 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1002 cl->status = -ENODEV;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001003 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001004 return -ENODEV;
1005 } else {
1006 if (mei_flow_ctrl_reduce(dev, cl))
1007 return -ENODEV;
1008 dev->iamthif_msg_buf_index += mei_hdr->length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +02001009 cb_pos->buf_idx = dev->iamthif_msg_buf_index;
Oren Weilfb7d8792011-05-15 13:43:42 +03001010 cl->status = 0;
1011 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001012 dev->iamthif_flow_control_pending = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001013 /* save iamthif cb sent to amthi client */
1014 dev->iamthif_current_cb = cb_pos;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001015 list_move_tail(&cb_pos->list, &dev->write_waiting_list.list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001016
1017 }
Tomas Winkler24aadc82012-06-25 23:46:27 +03001018 } else if (*slots == dev->hbuf_depth) {
1019 /* buffer is still empty */
Oren Weilfb7d8792011-05-15 13:43:42 +03001020 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1021 mei_hdr->host_addr = cl->host_client_id;
1022 mei_hdr->me_addr = cl->me_client_id;
1023 mei_hdr->length =
1024 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1025 mei_hdr->msg_complete = 0;
1026 mei_hdr->reserved = 0;
1027
Tomas Winkler7bdf72d2012-07-04 19:24:52 +03001028 *slots -= mei_data2slots(mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +03001029
Tomas Winkler1ccb7b62012-03-14 14:39:42 +02001030 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +03001031 (dev->iamthif_msg_buf +
1032 dev->iamthif_msg_buf_index),
1033 mei_hdr->length)) {
1034 cl->status = -ENODEV;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001035 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001036 } else {
1037 dev->iamthif_msg_buf_index += mei_hdr->length;
1038 }
1039 return -EMSGSIZE;
1040 } else {
1041 return -EBADMSG;
1042 }
1043
1044 return 0;
1045}
1046
1047/**
1048 * mei_irq_thread_read_handler - bottom half read routine after ISR to
1049 * handle the read processing.
1050 *
1051 * @cmpl_list: An instance of our list structure
1052 * @dev: the device structure
1053 * @slots: slots to read.
1054 *
1055 * returns 0 on success, <0 on failure.
1056 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001057static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
Oren Weilfb7d8792011-05-15 13:43:42 +03001058 struct mei_device *dev,
1059 s32 *slots)
1060{
1061 struct mei_msg_hdr *mei_hdr;
1062 struct mei_cl *cl_pos = NULL;
1063 struct mei_cl *cl_next = NULL;
1064 int ret = 0;
1065
1066 if (!dev->rd_msg_hdr) {
1067 dev->rd_msg_hdr = mei_mecbrw_read(dev);
1068 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1069 (*slots)--;
1070 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1071 }
1072 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
1073 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
1074
1075 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
1076 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
1077 ret = -EBADMSG;
1078 goto end;
1079 }
1080
1081 if (mei_hdr->host_addr || mei_hdr->me_addr) {
1082 list_for_each_entry_safe(cl_pos, cl_next,
1083 &dev->file_list, link) {
1084 dev_dbg(&dev->pdev->dev,
1085 "list_for_each_entry_safe read host"
1086 " client = %d, ME client = %d\n",
1087 cl_pos->host_client_id,
1088 cl_pos->me_client_id);
1089 if (cl_pos->host_client_id == mei_hdr->host_addr &&
1090 cl_pos->me_client_id == mei_hdr->me_addr)
1091 break;
1092 }
1093
1094 if (&cl_pos->link == &dev->file_list) {
1095 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
1096 ret = -EBADMSG;
1097 goto end;
1098 }
1099 }
1100 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
1101 dev_dbg(&dev->pdev->dev,
1102 "we can't read the message slots =%08x.\n",
1103 *slots);
1104 /* we can't read the message */
1105 ret = -ERANGE;
1106 goto end;
1107 }
1108
1109 /* decide where to read the message too */
1110 if (!mei_hdr->host_addr) {
1111 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
1112 mei_irq_thread_read_bus_message(dev, mei_hdr);
1113 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
1114 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
1115 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
1116 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
1117 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
1118 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
1119 mei_hdr->length);
1120 ret = mei_irq_thread_read_amthi_message(cmpl_list,
1121 dev, mei_hdr);
1122 if (ret)
1123 goto end;
1124
1125 } else {
1126 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
1127 ret = mei_irq_thread_read_client_message(cmpl_list,
1128 dev, mei_hdr);
1129 if (ret)
1130 goto end;
1131
1132 }
1133
1134 /* reset the number of slots and header */
1135 *slots = mei_count_full_read_slots(dev);
1136 dev->rd_msg_hdr = 0;
1137
1138 if (*slots == -EOVERFLOW) {
1139 /* overflow - reset */
1140 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
1141 /* set the event since message has been read */
1142 ret = -ERANGE;
1143 goto end;
1144 }
1145end:
1146 return ret;
1147}
1148
1149
1150/**
1151 * mei_irq_thread_write_handler - bottom half write routine after
1152 * ISR to handle the write processing.
1153 *
1154 * @cmpl_list: An instance of our list structure
1155 * @dev: the device structure
1156 * @slots: slots to write.
1157 *
1158 * returns 0 on success, <0 on failure.
1159 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001160static int mei_irq_thread_write_handler(struct mei_cl_cb *cmpl_list,
1161 struct mei_device *dev, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +03001162{
1163
1164 struct mei_cl *cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001165 struct mei_cl_cb *pos = NULL, *next = NULL;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001166 struct mei_cl_cb *list;
Oren Weilfb7d8792011-05-15 13:43:42 +03001167 int ret;
1168
Tomas Winkler726917f2012-06-25 23:46:28 +03001169 if (!mei_hbuf_is_empty(dev)) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001170 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
1171 return 0;
1172 }
Tomas Winkler726917f2012-06-25 23:46:28 +03001173 *slots = mei_hbuf_empty_slots(dev);
Tomas Winkler7d5e0e52012-06-19 09:13:36 +03001174 if (*slots <= 0)
1175 return -EMSGSIZE;
1176
Oren Weilfb7d8792011-05-15 13:43:42 +03001177 /* complete all waiting for write CB */
1178 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
1179
1180 list = &dev->write_waiting_list;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001181 list_for_each_entry_safe(pos, next, &list->list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001182 cl = (struct mei_cl *)pos->file_private;
1183 if (cl == NULL)
1184 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001185
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001186 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001187 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001188 if (MEI_WRITING == cl->writing_state &&
1189 (pos->major_file_operations == MEI_WRITE) &&
1190 (cl != &dev->iamthif_cl)) {
Tomas Winkler483136e2012-07-04 19:24:54 +03001191 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001192 cl->writing_state = MEI_WRITE_COMPLETE;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001193 list_add_tail(&pos->list, &cmpl_list->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001194 }
1195 if (cl == &dev->iamthif_cl) {
1196 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
1197 if (dev->iamthif_flow_control_pending) {
Tomas Winkler483136e2012-07-04 19:24:54 +03001198 ret = _mei_irq_thread_iamthif_read(dev, slots);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001199 if (ret)
1200 return ret;
1201 }
Oren Weilfb7d8792011-05-15 13:43:42 +03001202 }
1203 }
1204
Tomas Winklerc216fde2012-08-16 19:39:43 +03001205 if (dev->wd_state == MEI_WD_STOPPING) {
1206 dev->wd_state = MEI_WD_IDLE;
Oren Weilfb7d8792011-05-15 13:43:42 +03001207 wake_up_interruptible(&dev->wait_stop_wd);
Oren Weilfb7d8792011-05-15 13:43:42 +03001208 }
1209
1210 if (dev->extra_write_index) {
1211 dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
1212 dev->extra_write_index);
1213 mei_write_message(dev,
1214 (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
1215 (unsigned char *) &dev->ext_msg_buf[1],
1216 (dev->extra_write_index - 1) * sizeof(u32));
1217 *slots -= dev->extra_write_index;
1218 dev->extra_write_index = 0;
1219 }
Tomas Winklerb210d752012-08-07 00:03:56 +03001220 if (dev->dev_state == MEI_DEV_ENABLED) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001221 if (dev->wd_pending &&
Tomas Winkler483136e2012-07-04 19:24:54 +03001222 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001223 if (mei_wd_send(dev))
1224 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
Tomas Winkler483136e2012-07-04 19:24:54 +03001225 else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
1226 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +03001227
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001228 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +03001229
Tomas Winklerc216fde2012-08-16 19:39:43 +03001230 if (dev->wd_state == MEI_WD_RUNNING)
Tomas Winkler248ffdf2012-08-16 19:39:42 +03001231 *slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
Tomas Winklerd242a0a2012-07-04 19:24:50 +03001232 else
Tomas Winkler248ffdf2012-08-16 19:39:42 +03001233 *slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
Oren Weilfb7d8792011-05-15 13:43:42 +03001234 }
1235 }
Oren Weilfb7d8792011-05-15 13:43:42 +03001236
1237 /* complete control write list CB */
Tomas Winklerc8372092011-11-27 21:43:33 +02001238 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001239 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001240 cl = (struct mei_cl *) pos->file_private;
Tomas Winklerc8372092011-11-27 21:43:33 +02001241 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001242 list_del(&pos->list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001243 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +03001244 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001245 switch (pos->major_file_operations) {
Tomas Winklerc8372092011-11-27 21:43:33 +02001246 case MEI_CLOSE:
1247 /* send disconnect message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001248 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001249 if (ret)
1250 return ret;
1251
1252 break;
1253 case MEI_READ:
1254 /* send flow control message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001255 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001256 if (ret)
1257 return ret;
1258
1259 break;
1260 case MEI_IOCTL:
1261 /* connect message */
Natalia Ovsyanikove8cd29d2011-12-05 00:16:54 +02001262 if (mei_other_client_is_connecting(dev, cl))
Tomas Winklerc8372092011-11-27 21:43:33 +02001263 continue;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001264 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001265 if (ret)
1266 return ret;
1267
1268 break;
1269
1270 default:
1271 BUG();
1272 }
1273
Oren Weilfb7d8792011-05-15 13:43:42 +03001274 }
1275 /* complete write list CB */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001276 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001277 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001278 cl = (struct mei_cl *)pos->file_private;
1279 if (cl == NULL)
1280 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001281
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001282 if (cl != &dev->iamthif_cl) {
Tomas Winklerd2041152012-06-19 09:13:34 +03001283 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001284 dev_dbg(&dev->pdev->dev,
Tomas Winkler483136e2012-07-04 19:24:54 +03001285 "No flow control credentials for client %d, not sending.\n",
1286 cl->host_client_id);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001287 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001288 }
Tomas Winkler483136e2012-07-04 19:24:54 +03001289 ret = _mei_irq_thread_cmpl(dev, slots, pos,
1290 cl, cmpl_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001291 if (ret)
1292 return ret;
1293
1294 } else if (cl == &dev->iamthif_cl) {
1295 /* IAMTHIF IOCTL */
1296 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
Tomas Winklerd2041152012-06-19 09:13:34 +03001297 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001298 dev_dbg(&dev->pdev->dev,
Tomas Winkler483136e2012-07-04 19:24:54 +03001299 "No flow control credentials for amthi client %d.\n",
1300 cl->host_client_id);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001301 continue;
1302 }
Tomas Winkler483136e2012-07-04 19:24:54 +03001303 ret = _mei_irq_thread_cmpl_iamthif(dev, slots, pos,
1304 cl, cmpl_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001305 if (ret)
1306 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +03001307
1308 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001309
Oren Weilfb7d8792011-05-15 13:43:42 +03001310 }
1311 return 0;
1312}
1313
1314
1315
1316/**
1317 * mei_timer - timer function.
1318 *
1319 * @work: pointer to the work_struct structure
1320 *
1321 * NOTE: This function is called by timer interrupt work
1322 */
Oren Weila61c6532011-09-07 09:03:13 +03001323void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +03001324{
1325 unsigned long timeout;
1326 struct mei_cl *cl_pos = NULL;
1327 struct mei_cl *cl_next = NULL;
1328 struct list_head *amthi_complete_list = NULL;
1329 struct mei_cl_cb *cb_pos = NULL;
1330 struct mei_cl_cb *cb_next = NULL;
1331
1332 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +03001333 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +03001334
1335
1336 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001337 if (dev->dev_state != MEI_DEV_ENABLED) {
1338 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001339 if (dev->init_clients_timer) {
1340 if (--dev->init_clients_timer == 0) {
1341 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1342 dev->init_clients_state);
1343 mei_reset(dev, 1);
1344 }
1345 }
1346 }
1347 goto out;
1348 }
1349 /*** connect/disconnect timeouts ***/
1350 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1351 if (cl_pos->timer_count) {
1352 if (--cl_pos->timer_count == 0) {
1353 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1354 mei_reset(dev, 1);
1355 goto out;
1356 }
1357 }
1358 }
1359
Oren Weilfb7d8792011-05-15 13:43:42 +03001360 if (dev->iamthif_stall_timer) {
1361 if (--dev->iamthif_stall_timer == 0) {
Masanari Iida32de21f2012-01-25 23:14:56 +09001362 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +03001363 mei_reset(dev, 1);
1364 dev->iamthif_msg_buf_size = 0;
1365 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001366 dev->iamthif_canceled = false;
1367 dev->iamthif_ioctl = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001368 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1369 dev->iamthif_timer = 0;
1370
1371 if (dev->iamthif_current_cb)
1372 mei_free_cb_private(dev->iamthif_current_cb);
1373
1374 dev->iamthif_file_object = NULL;
1375 dev->iamthif_current_cb = NULL;
Tomas Winklerc95efb72011-05-25 17:28:21 +03001376 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001377 }
1378 }
1379
1380 if (dev->iamthif_timer) {
1381
1382 timeout = dev->iamthif_timer +
1383 msecs_to_jiffies(IAMTHIF_READ_TIMER);
1384
1385 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1386 dev->iamthif_timer);
1387 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1388 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1389 if (time_after(jiffies, timeout)) {
1390 /*
1391 * User didn't read the AMTHI data on time (15sec)
1392 * freeing AMTHI for other requests
1393 */
1394
1395 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1396
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001397 amthi_complete_list = &dev->amthi_read_complete_list.list;
Oren Weilfb7d8792011-05-15 13:43:42 +03001398
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001399 list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, list) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001400
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001401 cl_pos = cb_pos->file_object->private_data;
Oren Weilfb7d8792011-05-15 13:43:42 +03001402
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001403 /* Finding the AMTHI entry. */
1404 if (cl_pos == &dev->iamthif_cl)
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001405 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001406 }
1407 if (dev->iamthif_current_cb)
1408 mei_free_cb_private(dev->iamthif_current_cb);
1409
1410 dev->iamthif_file_object->private_data = NULL;
1411 dev->iamthif_file_object = NULL;
1412 dev->iamthif_current_cb = NULL;
1413 dev->iamthif_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +03001414 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001415
1416 }
1417 }
1418out:
Tomas Winkler441ab502011-12-13 23:39:34 +02001419 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1420 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +03001421}
1422
1423/**
1424 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1425 * processing.
1426 *
1427 * @irq: The irq number
1428 * @dev_id: pointer to the device structure
1429 *
1430 * returns irqreturn_t
1431 *
1432 */
1433irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1434{
1435 struct mei_device *dev = (struct mei_device *) dev_id;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001436 struct mei_cl_cb complete_list;
Oren Weilfb7d8792011-05-15 13:43:42 +03001437 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1438 struct mei_cl *cl;
1439 s32 slots;
1440 int rets;
1441 bool bus_message_received;
1442
1443
1444 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1445 /* initialize our complete list */
1446 mutex_lock(&dev->device_lock);
Tomas Winkler0288c7c2011-06-06 10:44:34 +03001447 mei_io_list_init(&complete_list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001448 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001449
1450 /* Ack the interrupt here
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001451 * In case of MSI we don't go through the quick handler */
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001452 if (pci_dev_msi_enabled(dev->pdev))
1453 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1454
Oren Weilfb7d8792011-05-15 13:43:42 +03001455 dev->me_hw_state = mei_mecsr_read(dev);
1456
1457 /* check if ME wants a reset */
1458 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
Tomas Winklerb210d752012-08-07 00:03:56 +03001459 dev->dev_state != MEI_DEV_RESETING &&
1460 dev->dev_state != MEI_DEV_INITIALIZING) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001461 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1462 mei_reset(dev, 1);
1463 mutex_unlock(&dev->device_lock);
1464 return IRQ_HANDLED;
1465 }
1466
1467 /* check if we need to start the dev */
1468 if ((dev->host_hw_state & H_RDY) == 0) {
1469 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1470 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1471 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1472 mei_hcsr_set(dev);
Tomas Winklerb210d752012-08-07 00:03:56 +03001473 dev->dev_state = MEI_DEV_INIT_CLIENTS;
Oren Weilfb7d8792011-05-15 13:43:42 +03001474 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1475 /* link is established
1476 * start sending messages.
1477 */
Tomas Winklerc95efb72011-05-25 17:28:21 +03001478 mei_host_start_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001479 mutex_unlock(&dev->device_lock);
1480 return IRQ_HANDLED;
1481 } else {
1482 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1483 mutex_unlock(&dev->device_lock);
1484 return IRQ_HANDLED;
1485 }
1486 }
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001487 /* check slots available for reading */
Oren Weilfb7d8792011-05-15 13:43:42 +03001488 slots = mei_count_full_read_slots(dev);
1489 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1490 slots, dev->extra_write_index);
1491 while (slots > 0 && !dev->extra_write_index) {
1492 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1493 slots, dev->extra_write_index);
1494 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1495 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1496 if (rets)
1497 goto end;
1498 }
1499 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1500end:
1501 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1502 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler726917f2012-06-25 23:46:28 +03001503 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001504
1505 bus_message_received = false;
1506 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1507 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1508 bus_message_received = true;
1509 }
1510 mutex_unlock(&dev->device_lock);
1511 if (bus_message_received) {
1512 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1513 wake_up_interruptible(&dev->wait_recvd_msg);
1514 bus_message_received = false;
1515 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001516 if (list_empty(&complete_list.list))
Oren Weilfb7d8792011-05-15 13:43:42 +03001517 return IRQ_HANDLED;
1518
1519
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001520 list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001521 cl = (struct mei_cl *)cb_pos->file_private;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001522 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001523 if (cl) {
1524 if (cl != &dev->iamthif_cl) {
1525 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1526 _mei_cmpl(cl, cb_pos);
1527 cb_pos = NULL;
1528 } else if (cl == &dev->iamthif_cl) {
1529 _mei_cmpl_iamthif(dev, cb_pos);
1530 }
1531 }
1532 }
1533 return IRQ_HANDLED;
1534}