blob: 320ebd2f06cefbbac7e03bc5ad867d1df86b7887 [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);
90 list_add_tail(&cb_pos->cb_list,
91 &dev->amthi_read_complete_list.mei_cb.cb_list);
92 dev_dbg(&dev->pdev->dev, "amthi read completed.\n");
93 dev->iamthif_timer = jiffies;
94 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
95 dev->iamthif_timer);
96 } else {
Tomas Winklerc95efb72011-05-25 17:28:21 +030097 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +030098 }
99
100 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
101 wake_up_interruptible(&dev->iamthif_cl.wait);
102}
103
104
105/**
106 * mei_irq_thread_read_amthi_message - bottom half read routine after ISR to
107 * handle the read amthi message data processing.
108 *
109 * @complete_list: An instance of our list structure
110 * @dev: the device structure
111 * @mei_hdr: header of amthi message
112 *
113 * returns 0 on success, <0 on failure.
114 */
115static int mei_irq_thread_read_amthi_message(struct mei_io_list *complete_list,
116 struct mei_device *dev,
117 struct mei_msg_hdr *mei_hdr)
118{
119 struct mei_cl *cl;
120 struct mei_cl_cb *cb;
121 unsigned char *buffer;
122
123 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
124 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
125
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200126 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
Oren Weilfb7d8792011-05-15 13:43:42 +0300127 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
128
129 mei_read_slots(dev, buffer, mei_hdr->length);
130
131 dev->iamthif_msg_buf_index += mei_hdr->length;
132
133 if (!mei_hdr->msg_complete)
134 return 0;
135
136 dev_dbg(&dev->pdev->dev,
137 "amthi_message_buffer_index =%d\n",
138 mei_hdr->length);
139
140 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
141 if (!dev->iamthif_current_cb)
142 return -ENODEV;
143
144 cb = dev->iamthif_current_cb;
145 dev->iamthif_current_cb = NULL;
146
147 cl = (struct mei_cl *)cb->file_private;
148 if (!cl)
149 return -ENODEV;
150
151 dev->iamthif_stall_timer = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200152 cb->buf_idx = dev->iamthif_msg_buf_index;
Oren Weilfb7d8792011-05-15 13:43:42 +0300153 cb->read_time = jiffies;
154 if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
155 /* found the iamthif cb */
156 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
157 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
158 list_add_tail(&cb->cb_list,
159 &complete_list->mei_cb.cb_list);
160 }
161 return 0;
162}
163
164/**
165 * _mei_irq_thread_state_ok - checks if mei header matches file private data
166 *
167 * @cl: private data of the file object
168 * @mei_hdr: header of mei client message
169 *
170 * returns !=0 if matches, 0 if no match.
171 */
172static int _mei_irq_thread_state_ok(struct mei_cl *cl,
173 struct mei_msg_hdr *mei_hdr)
174{
175 return (cl->host_client_id == mei_hdr->host_addr &&
176 cl->me_client_id == mei_hdr->me_addr &&
177 cl->state == MEI_FILE_CONNECTED &&
178 MEI_READ_COMPLETE != cl->reading_state);
179}
180
181/**
182 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
183 * handle the read mei client message data processing.
184 *
185 * @complete_list: An instance of our list structure
186 * @dev: the device structure
187 * @mei_hdr: header of mei client message
188 *
189 * returns 0 on success, <0 on failure.
190 */
191static int mei_irq_thread_read_client_message(struct mei_io_list *complete_list,
192 struct mei_device *dev,
193 struct mei_msg_hdr *mei_hdr)
194{
195 struct mei_cl *cl;
196 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
Tomas Winkler479bc592011-06-16 00:46:03 +0300197 unsigned char *buffer = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300198
199 dev_dbg(&dev->pdev->dev, "start client msg\n");
Tomas Winklerc8372092011-11-27 21:43:33 +0200200 if (list_empty(&dev->read_list.mei_cb.cb_list))
Oren Weilfb7d8792011-05-15 13:43:42 +0300201 goto quit;
202
203 list_for_each_entry_safe(cb_pos, cb_next,
204 &dev->read_list.mei_cb.cb_list, cb_list) {
205 cl = (struct mei_cl *)cb_pos->file_private;
206 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
207 cl->reading_state = MEI_READING;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200208 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300209
210 if (cb_pos->response_buffer.size <
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200211 mei_hdr->length + cb_pos->buf_idx) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300212 dev_dbg(&dev->pdev->dev, "message overflow.\n");
213 list_del(&cb_pos->cb_list);
214 return -ENOMEM;
215 }
216 if (buffer)
217 mei_read_slots(dev, buffer, mei_hdr->length);
218
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200219 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300220 if (mei_hdr->msg_complete) {
221 cl->status = 0;
222 list_del(&cb_pos->cb_list);
223 dev_dbg(&dev->pdev->dev,
Tomas Winklera4136b42012-09-11 00:43:22 +0300224 "completed read H cl = %d, ME cl = %d, length = %lu\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300225 cl->host_client_id,
226 cl->me_client_id,
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200227 cb_pos->buf_idx);
228
Oren Weilfb7d8792011-05-15 13:43:42 +0300229 list_add_tail(&cb_pos->cb_list,
230 &complete_list->mei_cb.cb_list);
231 }
232
233 break;
234 }
235
236 }
237
238quit:
239 dev_dbg(&dev->pdev->dev, "message read\n");
240 if (!buffer) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200241 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +0300242 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
243 *(u32 *) dev->rd_msg_buf);
244 }
245
246 return 0;
247}
248
249/**
250 * _mei_irq_thread_iamthif_read - prepares to read iamthif data.
251 *
252 * @dev: the device structure.
253 * @slots: free slots.
254 *
255 * returns 0, OK; otherwise, error.
256 */
257static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots)
258{
259
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200260 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +0300261 + sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300262 return -EMSGSIZE;
263 }
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300264 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200265 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
266 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
267 return -EIO;
268 }
269
270 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
271 dev->iamthif_state = MEI_IAMTHIF_READING;
272 dev->iamthif_flow_control_pending = false;
273 dev->iamthif_msg_buf_index = 0;
274 dev->iamthif_msg_buf_size = 0;
275 dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
Tomas Winkler726917f2012-06-25 23:46:28 +0300276 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200277 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300278}
279
280/**
281 * _mei_irq_thread_close - processes close related operation.
282 *
283 * @dev: the device structure.
284 * @slots: free slots.
285 * @cb_pos: callback block.
286 * @cl: private data of the file object.
287 * @cmpl_list: complete list.
288 *
289 * returns 0, OK; otherwise, error.
290 */
291static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
292 struct mei_cl_cb *cb_pos,
293 struct mei_cl *cl,
294 struct mei_io_list *cmpl_list)
295{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300296 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
297 sizeof(struct hbm_client_disconnect_request)))
Oren Weilfb7d8792011-05-15 13:43:42 +0300298 return -EBADMSG;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300299
300 *slots -= mei_data2slots(sizeof(struct hbm_client_disconnect_request));
301
302 if (mei_disconnect(dev, cl)) {
303 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200304 cb_pos->buf_idx = 0;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300305 list_move_tail(&cb_pos->cb_list,
306 &cmpl_list->mei_cb.cb_list);
307 return -EMSGSIZE;
308 } else {
309 cl->state = MEI_FILE_DISCONNECTING;
310 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200311 cb_pos->buf_idx = 0;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300312 list_move_tail(&cb_pos->cb_list,
313 &dev->ctrl_rd_list.mei_cb.cb_list);
314 cl->timer_count = MEI_CONNECT_TIMEOUT;
Oren Weilfb7d8792011-05-15 13:43:42 +0300315 }
316
317 return 0;
318}
319
320/**
321 * is_treat_specially_client - checks if the message belongs
322 * to the file private data.
323 *
324 * @cl: private data of the file object
325 * @rs: connect response bus message
326 *
327 */
328static bool is_treat_specially_client(struct mei_cl *cl,
329 struct hbm_client_connect_response *rs)
330{
331
332 if (cl->host_client_id == rs->host_addr &&
333 cl->me_client_id == rs->me_addr) {
334 if (!rs->status) {
335 cl->state = MEI_FILE_CONNECTED;
336 cl->status = 0;
337
338 } else {
339 cl->state = MEI_FILE_DISCONNECTED;
340 cl->status = -ENODEV;
341 }
342 cl->timer_count = 0;
343
344 return true;
345 }
346 return false;
347}
348
349/**
350 * mei_client_connect_response - connects to response irq routine
351 *
352 * @dev: the device structure
353 * @rs: connect response bus message
354 */
355static void mei_client_connect_response(struct mei_device *dev,
356 struct hbm_client_connect_response *rs)
357{
358
359 struct mei_cl *cl;
360 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
361
362 dev_dbg(&dev->pdev->dev,
363 "connect_response:\n"
364 "ME Client = %d\n"
365 "Host Client = %d\n"
366 "Status = %d\n",
367 rs->me_addr,
368 rs->host_addr,
369 rs->status);
370
371 /* if WD or iamthif client treat specially */
372
373 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300374 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
Tomas Winkler70cd5332011-12-22 18:50:50 +0200375 mei_watchdog_register(dev);
Oren Weil9ce178e2011-09-07 09:03:09 +0300376
Tomas Winkler70cd5332011-12-22 18:50:50 +0200377 /* next step in the state maching */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300378 mei_host_init_iamthif(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300379 return;
380 }
381
382 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
383 dev->iamthif_state = MEI_IAMTHIF_IDLE;
384 return;
385 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200386 list_for_each_entry_safe(cb_pos, cb_next,
387 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
388
389 cl = (struct mei_cl *)cb_pos->file_private;
390 if (!cl) {
391 list_del(&cb_pos->cb_list);
392 return;
393 }
394 if (MEI_IOCTL == cb_pos->major_file_operations) {
395 if (is_treat_specially_client(cl, rs)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300396 list_del(&cb_pos->cb_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200397 cl->status = 0;
398 cl->timer_count = 0;
399 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300400 }
401 }
402 }
403}
404
405/**
406 * mei_client_disconnect_response - disconnects from response irq routine
407 *
408 * @dev: the device structure
409 * @rs: disconnect response bus message
410 */
411static void mei_client_disconnect_response(struct mei_device *dev,
412 struct hbm_client_connect_response *rs)
413{
414 struct mei_cl *cl;
415 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
416
417 dev_dbg(&dev->pdev->dev,
418 "disconnect_response:\n"
419 "ME Client = %d\n"
420 "Host Client = %d\n"
421 "Status = %d\n",
422 rs->me_addr,
423 rs->host_addr,
424 rs->status);
425
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200426 list_for_each_entry_safe(cb_pos, cb_next,
427 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
428 cl = (struct mei_cl *)cb_pos->file_private;
Oren Weilfb7d8792011-05-15 13:43:42 +0300429
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200430 if (!cl) {
431 list_del(&cb_pos->cb_list);
432 return;
433 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300434
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200435 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
436 if (cl->host_client_id == rs->host_addr &&
437 cl->me_client_id == rs->me_addr) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300438
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200439 list_del(&cb_pos->cb_list);
440 if (!rs->status)
441 cl->state = MEI_FILE_DISCONNECTED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300442
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200443 cl->status = 0;
444 cl->timer_count = 0;
445 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300446 }
447 }
448}
449
450/**
451 * same_flow_addr - tells if they have the same address.
452 *
453 * @file: private data of the file object.
454 * @flow: flow control.
455 *
456 * returns !=0, same; 0,not.
457 */
458static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
459{
460 return (cl->host_client_id == flow->host_addr &&
461 cl->me_client_id == flow->me_addr);
462}
463
464/**
465 * add_single_flow_creds - adds single buffer credentials.
466 *
467 * @file: private data ot the file object.
468 * @flow: flow control.
469 */
470static void add_single_flow_creds(struct mei_device *dev,
471 struct hbm_flow_control *flow)
472{
473 struct mei_me_client *client;
474 int i;
475
Tomas Winklercf9673d2011-06-06 10:44:33 +0300476 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300477 client = &dev->me_clients[i];
478 if (client && flow->me_addr == client->client_id) {
479 if (client->props.single_recv_buf) {
480 client->mei_flow_ctrl_creds++;
481 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
482 flow->me_addr);
483 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
484 client->mei_flow_ctrl_creds);
485 } else {
486 BUG(); /* error in flow control */
487 }
488 }
489 }
490}
491
492/**
493 * mei_client_flow_control_response - flow control response irq routine
494 *
495 * @dev: the device structure
496 * @flow_control: flow control response bus message
497 */
498static void mei_client_flow_control_response(struct mei_device *dev,
499 struct hbm_flow_control *flow_control)
500{
501 struct mei_cl *cl_pos = NULL;
502 struct mei_cl *cl_next = NULL;
503
504 if (!flow_control->host_addr) {
505 /* single receive buffer */
506 add_single_flow_creds(dev, flow_control);
507 } else {
508 /* normal connection */
509 list_for_each_entry_safe(cl_pos, cl_next,
510 &dev->file_list, link) {
511 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
512
513 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
514 cl_pos->host_client_id,
515 cl_pos->me_client_id);
516 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
517 flow_control->host_addr,
518 flow_control->me_addr);
519 if (same_flow_addr(cl_pos, flow_control)) {
520 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
521 flow_control->host_addr,
522 flow_control->me_addr);
523 cl_pos->mei_flow_ctrl_creds++;
524 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
525 cl_pos->mei_flow_ctrl_creds);
526 break;
527 }
528 }
529 }
530}
531
532/**
533 * same_disconn_addr - tells if they have the same address
534 *
535 * @file: private data of the file object.
536 * @disconn: disconnection request.
537 *
538 * returns !=0, same; 0,not.
539 */
540static int same_disconn_addr(struct mei_cl *cl,
541 struct hbm_client_disconnect_request *disconn)
542{
543 return (cl->host_client_id == disconn->host_addr &&
544 cl->me_client_id == disconn->me_addr);
545}
546
547/**
548 * mei_client_disconnect_request - disconnects from request irq routine
549 *
550 * @dev: the device structure.
551 * @disconnect_req: disconnect request bus message.
552 */
553static void mei_client_disconnect_request(struct mei_device *dev,
554 struct hbm_client_disconnect_request *disconnect_req)
555{
556 struct mei_msg_hdr *mei_hdr;
557 struct hbm_client_connect_response *disconnect_res;
558 struct mei_cl *cl_pos = NULL;
559 struct mei_cl *cl_next = NULL;
560
561 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
562 if (same_disconn_addr(cl_pos, disconnect_req)) {
563 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
564 disconnect_req->host_addr,
565 disconnect_req->me_addr);
566 cl_pos->state = MEI_FILE_DISCONNECTED;
567 cl_pos->timer_count = 0;
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300568 if (cl_pos == &dev->wd_cl)
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300569 dev->wd_pending = false;
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300570 else if (cl_pos == &dev->iamthif_cl)
Oren Weilfb7d8792011-05-15 13:43:42 +0300571 dev->iamthif_timer = 0;
572
573 /* prepare disconnect response */
574 mei_hdr =
575 (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
576 mei_hdr->host_addr = 0;
577 mei_hdr->me_addr = 0;
578 mei_hdr->length =
579 sizeof(struct hbm_client_connect_response);
580 mei_hdr->msg_complete = 1;
581 mei_hdr->reserved = 0;
582
583 disconnect_res =
584 (struct hbm_client_connect_response *)
585 &dev->ext_msg_buf[1];
586 disconnect_res->host_addr = cl_pos->host_client_id;
587 disconnect_res->me_addr = cl_pos->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200588 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300589 disconnect_res->status = 0;
590 dev->extra_write_index = 2;
591 break;
592 }
593 }
594}
595
596
597/**
598 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
599 * handle the read bus message cmd processing.
600 *
601 * @dev: the device structure
602 * @mei_hdr: header of bus message
603 */
604static void mei_irq_thread_read_bus_message(struct mei_device *dev,
605 struct mei_msg_hdr *mei_hdr)
606{
607 struct mei_bus_message *mei_msg;
608 struct hbm_host_version_response *version_res;
609 struct hbm_client_connect_response *connect_res;
610 struct hbm_client_connect_response *disconnect_res;
611 struct hbm_flow_control *flow_control;
612 struct hbm_props_response *props_res;
613 struct hbm_host_enum_response *enum_res;
614 struct hbm_client_disconnect_request *disconnect_req;
615 struct hbm_host_stop_request *host_stop_req;
Oren Weilabc51b62011-09-21 16:45:30 +0300616 int res;
Oren Weilfb7d8792011-05-15 13:43:42 +0300617
Oren Weilfb7d8792011-05-15 13:43:42 +0300618
619 /* read the message to our buffer */
Oren Weilfb7d8792011-05-15 13:43:42 +0300620 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200621 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
622 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
Oren Weilfb7d8792011-05-15 13:43:42 +0300623
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200624 switch (mei_msg->hbm_cmd) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300625 case HOST_START_RES_CMD:
626 version_res = (struct hbm_host_version_response *) mei_msg;
627 if (version_res->host_version_supported) {
628 dev->version.major_version = HBM_MAJOR_VERSION;
629 dev->version.minor_version = HBM_MINOR_VERSION;
Tomas Winklerb210d752012-08-07 00:03:56 +0300630 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300631 dev->init_clients_state == MEI_START_MESSAGE) {
632 dev->init_clients_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300633 mei_host_enum_clients_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300634 } else {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300635 dev->recvd_msg = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300636 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
637 mei_reset(dev, 1);
638 return;
639 }
640 } else {
641 dev->version = version_res->me_max_version;
642 /* send stop message */
Tomas Winkler97d5cb02012-03-06 22:34:32 +0200643 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
Oren Weilfb7d8792011-05-15 13:43:42 +0300644 mei_hdr->host_addr = 0;
645 mei_hdr->me_addr = 0;
646 mei_hdr->length = sizeof(struct hbm_host_stop_request);
647 mei_hdr->msg_complete = 1;
648 mei_hdr->reserved = 0;
649
650 host_stop_req = (struct hbm_host_stop_request *)
651 &dev->wr_msg_buf[1];
652
653 memset(host_stop_req,
654 0,
655 sizeof(struct hbm_host_stop_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200656 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300657 host_stop_req->reason = DRIVER_STOP_REQUEST;
658 mei_write_message(dev, mei_hdr,
659 (unsigned char *) (host_stop_req),
660 mei_hdr->length);
661 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
662 return;
663 }
664
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300665 dev->recvd_msg = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300666 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
667 break;
668
669 case CLIENT_CONNECT_RES_CMD:
670 connect_res =
671 (struct hbm_client_connect_response *) mei_msg;
672 mei_client_connect_response(dev, connect_res);
673 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
674 wake_up(&dev->wait_recvd_msg);
675 break;
676
677 case CLIENT_DISCONNECT_RES_CMD:
678 disconnect_res =
679 (struct hbm_client_connect_response *) mei_msg;
Tomas Winkler441ab502011-12-13 23:39:34 +0200680 mei_client_disconnect_response(dev, disconnect_res);
Oren Weilfb7d8792011-05-15 13:43:42 +0300681 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
682 wake_up(&dev->wait_recvd_msg);
683 break;
684
685 case MEI_FLOW_CONTROL_CMD:
686 flow_control = (struct hbm_flow_control *) mei_msg;
687 mei_client_flow_control_response(dev, flow_control);
688 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
689 break;
690
691 case HOST_CLIENT_PROPERTIES_RES_CMD:
692 props_res = (struct hbm_props_response *)mei_msg;
693 if (props_res->status || !dev->me_clients) {
694 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
695 mei_reset(dev, 1);
696 return;
697 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200698 if (dev->me_clients[dev->me_client_presentation_num]
Oren Weilfb7d8792011-05-15 13:43:42 +0300699 .client_id == props_res->address) {
700
701 dev->me_clients[dev->me_client_presentation_num].props
702 = props_res->client_properties;
703
Tomas Winklerb210d752012-08-07 00:03:56 +0300704 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300705 dev->init_clients_state ==
706 MEI_CLIENT_PROPERTIES_MESSAGE) {
707 dev->me_client_index++;
708 dev->me_client_presentation_num++;
Oren Weilabc51b62011-09-21 16:45:30 +0300709
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700710 /** Send Client Properties request **/
Oren Weilabc51b62011-09-21 16:45:30 +0300711 res = mei_host_client_properties(dev);
712 if (res < 0) {
713 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
714 return;
715 } else if (!res) {
716 /*
717 * No more clients to send to.
718 * Clear Map for indicating now ME clients
719 * with associated host client
720 */
721 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
722 dev->open_handle_count = 0;
723
724 /*
725 * Reserving the first three client IDs
726 * Client Id 0 - Reserved for MEI Bus Message communications
727 * Client Id 1 - Reserved for Watchdog
728 * Client ID 2 - Reserved for AMTHI
729 */
730 bitmap_set(dev->host_clients_map, 0, 3);
Tomas Winklerb210d752012-08-07 00:03:56 +0300731 dev->dev_state = MEI_DEV_ENABLED;
Oren Weilabc51b62011-09-21 16:45:30 +0300732
733 /* if wd initialization fails, initialization the AMTHI client,
734 * otherwise the AMTHI client will be initialized after the WD client connect response
735 * will be received
736 */
737 if (mei_wd_host_init(dev))
738 mei_host_init_iamthif(dev);
739 }
740
Oren Weilfb7d8792011-05-15 13:43:42 +0300741 } else {
742 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
743 mei_reset(dev, 1);
744 return;
745 }
746 } else {
747 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
748 mei_reset(dev, 1);
749 return;
750 }
751 break;
752
753 case HOST_ENUM_RES_CMD:
754 enum_res = (struct hbm_host_enum_response *) mei_msg;
755 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
Tomas Winklerb210d752012-08-07 00:03:56 +0300756 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300757 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
758 dev->init_clients_timer = 0;
759 dev->me_client_presentation_num = 0;
760 dev->me_client_index = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300761 mei_allocate_me_clients_storage(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300762 dev->init_clients_state =
763 MEI_CLIENT_PROPERTIES_MESSAGE;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300764 mei_host_client_properties(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300765 } else {
766 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
767 mei_reset(dev, 1);
768 return;
769 }
770 break;
771
772 case HOST_STOP_RES_CMD:
Tomas Winklerb210d752012-08-07 00:03:56 +0300773 dev->dev_state = MEI_DEV_DISABLED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300774 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
775 mei_reset(dev, 1);
776 break;
777
778 case CLIENT_DISCONNECT_REQ_CMD:
779 /* search for client */
780 disconnect_req =
781 (struct hbm_client_disconnect_request *) mei_msg;
782 mei_client_disconnect_request(dev, disconnect_req);
783 break;
784
785 case ME_STOP_REQ_CMD:
786 /* prepare stop request */
787 mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
788 mei_hdr->host_addr = 0;
789 mei_hdr->me_addr = 0;
790 mei_hdr->length = sizeof(struct hbm_host_stop_request);
791 mei_hdr->msg_complete = 1;
792 mei_hdr->reserved = 0;
793 host_stop_req =
794 (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
795 memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200796 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300797 host_stop_req->reason = DRIVER_STOP_REQUEST;
798 host_stop_req->reserved[0] = 0;
799 host_stop_req->reserved[1] = 0;
800 dev->extra_write_index = 2;
801 break;
802
803 default:
804 BUG();
805 break;
806
807 }
808}
809
810
811/**
812 * _mei_hb_read - processes read related operation.
813 *
814 * @dev: the device structure.
815 * @slots: free slots.
816 * @cb_pos: callback block.
817 * @cl: private data of the file object.
818 * @cmpl_list: complete list.
819 *
820 * returns 0, OK; otherwise, error.
821 */
822static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
823 struct mei_cl_cb *cb_pos,
824 struct mei_cl *cl,
825 struct mei_io_list *cmpl_list)
826{
Tomas Winkler1e69d642012-05-29 16:39:12 +0300827 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300828 sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300829 /* return the cancel routine */
830 list_del(&cb_pos->cb_list);
831 return -EBADMSG;
832 }
833
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300834 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
835
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200836 if (mei_send_flow_control(dev, cl)) {
837 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200838 cb_pos->buf_idx = 0;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200839 list_move_tail(&cb_pos->cb_list, &cmpl_list->mei_cb.cb_list);
840 return -ENODEV;
841 }
842 list_move_tail(&cb_pos->cb_list, &dev->read_list.mei_cb.cb_list);
843
Oren Weilfb7d8792011-05-15 13:43:42 +0300844 return 0;
845}
846
847
848/**
849 * _mei_irq_thread_ioctl - processes ioctl related operation.
850 *
851 * @dev: the device structure.
852 * @slots: free slots.
853 * @cb_pos: callback block.
854 * @cl: private data of the file object.
855 * @cmpl_list: complete list.
856 *
857 * returns 0, OK; otherwise, error.
858 */
859static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
860 struct mei_cl_cb *cb_pos,
861 struct mei_cl *cl,
862 struct mei_io_list *cmpl_list)
863{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300864 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300865 sizeof(struct hbm_client_connect_request))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300866 /* return the cancel routine */
867 list_del(&cb_pos->cb_list);
868 return -EBADMSG;
869 }
870
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300871 cl->state = MEI_FILE_CONNECTING;
872 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
873 if (mei_connect(dev, cl)) {
874 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200875 cb_pos->buf_idx = 0;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300876 list_del(&cb_pos->cb_list);
877 return -ENODEV;
878 } else {
879 list_move_tail(&cb_pos->cb_list,
880 &dev->ctrl_rd_list.mei_cb.cb_list);
881 cl->timer_count = MEI_CONNECT_TIMEOUT;
882 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300883 return 0;
884}
885
886/**
887 * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
888 *
889 * @dev: the device structure.
890 * @slots: free slots.
891 * @cb_pos: callback block.
892 * @cl: private data of the file object.
893 * @cmpl_list: complete list.
894 *
895 * returns 0, OK; otherwise, error.
896 */
897static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
898 struct mei_cl_cb *cb_pos,
899 struct mei_cl *cl,
900 struct mei_io_list *cmpl_list)
901{
902 struct mei_msg_hdr *mei_hdr;
903
904 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200905 (cb_pos->request_buffer.size - cb_pos->buf_idx))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300906 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
907 mei_hdr->host_addr = cl->host_client_id;
908 mei_hdr->me_addr = cl->me_client_id;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200909 mei_hdr->length = cb_pos->request_buffer.size - cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300910 mei_hdr->msg_complete = 1;
911 mei_hdr->reserved = 0;
912 dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
913 "mei_hdr->msg_complete = %d\n",
914 cb_pos->request_buffer.size,
915 mei_hdr->msg_complete);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200916 dev_dbg(&dev->pdev->dev, "cb_pos->buf_idx =%lu\n",
917 cb_pos->buf_idx);
Oren Weilfb7d8792011-05-15 13:43:42 +0300918 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
919 mei_hdr->length);
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300920 *slots -= mei_data2slots(mei_hdr->length);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200921 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300922 (unsigned char *)
923 (cb_pos->request_buffer.data +
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200924 cb_pos->buf_idx),
Oren Weilfb7d8792011-05-15 13:43:42 +0300925 mei_hdr->length)) {
926 cl->status = -ENODEV;
927 list_move_tail(&cb_pos->cb_list,
928 &cmpl_list->mei_cb.cb_list);
929 return -ENODEV;
930 } else {
931 if (mei_flow_ctrl_reduce(dev, cl))
932 return -ENODEV;
933 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200934 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300935 list_move_tail(&cb_pos->cb_list,
936 &dev->write_waiting_list.mei_cb.cb_list);
937 }
Tomas Winkler24aadc82012-06-25 23:46:27 +0300938 } else if (*slots == dev->hbuf_depth) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300939 /* buffer is still empty */
940 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
941 mei_hdr->host_addr = cl->host_client_id;
942 mei_hdr->me_addr = cl->me_client_id;
943 mei_hdr->length =
944 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
945 mei_hdr->msg_complete = 0;
946 mei_hdr->reserved = 0;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300947 *slots -= mei_data2slots(mei_hdr->length);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200948 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300949 (unsigned char *)
950 (cb_pos->request_buffer.data +
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200951 cb_pos->buf_idx),
Oren Weilfb7d8792011-05-15 13:43:42 +0300952 mei_hdr->length)) {
953 cl->status = -ENODEV;
954 list_move_tail(&cb_pos->cb_list,
955 &cmpl_list->mei_cb.cb_list);
956 return -ENODEV;
957 } else {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200958 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300959 dev_dbg(&dev->pdev->dev,
960 "cb_pos->request_buffer.size =%d"
961 " mei_hdr->msg_complete = %d\n",
962 cb_pos->request_buffer.size,
963 mei_hdr->msg_complete);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200964 dev_dbg(&dev->pdev->dev, "cb_pos->buf_idx =%lu\n",
965 cb_pos->buf_idx);
Oren Weilfb7d8792011-05-15 13:43:42 +0300966 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
967 mei_hdr->length);
968 }
969 return -EMSGSIZE;
970 } else {
971 return -EBADMSG;
972 }
973
974 return 0;
975}
976
977/**
978 * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
979 *
980 * @dev: the device structure.
981 * @slots: free slots.
982 * @cb_pos: callback block.
983 * @cl: private data of the file object.
984 * @cmpl_list: complete list.
985 *
986 * returns 0, OK; otherwise, error.
987 */
988static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
989 struct mei_cl_cb *cb_pos,
990 struct mei_cl *cl,
991 struct mei_io_list *cmpl_list)
992{
993 struct mei_msg_hdr *mei_hdr;
994
995 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
996 dev->iamthif_msg_buf_size -
997 dev->iamthif_msg_buf_index)) {
998 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
999 mei_hdr->host_addr = cl->host_client_id;
1000 mei_hdr->me_addr = cl->me_client_id;
1001 mei_hdr->length = dev->iamthif_msg_buf_size -
1002 dev->iamthif_msg_buf_index;
1003 mei_hdr->msg_complete = 1;
1004 mei_hdr->reserved = 0;
1005
Tomas Winkler7bdf72d2012-07-04 19:24:52 +03001006 *slots -= mei_data2slots(mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +03001007
Tomas Winkler1ccb7b62012-03-14 14:39:42 +02001008 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +03001009 (dev->iamthif_msg_buf +
1010 dev->iamthif_msg_buf_index),
1011 mei_hdr->length)) {
1012 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1013 cl->status = -ENODEV;
1014 list_del(&cb_pos->cb_list);
1015 return -ENODEV;
1016 } else {
1017 if (mei_flow_ctrl_reduce(dev, cl))
1018 return -ENODEV;
1019 dev->iamthif_msg_buf_index += mei_hdr->length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +02001020 cb_pos->buf_idx = dev->iamthif_msg_buf_index;
Oren Weilfb7d8792011-05-15 13:43:42 +03001021 cl->status = 0;
1022 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001023 dev->iamthif_flow_control_pending = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001024 /* save iamthif cb sent to amthi client */
1025 dev->iamthif_current_cb = cb_pos;
1026 list_move_tail(&cb_pos->cb_list,
1027 &dev->write_waiting_list.mei_cb.cb_list);
1028
1029 }
Tomas Winkler24aadc82012-06-25 23:46:27 +03001030 } else if (*slots == dev->hbuf_depth) {
1031 /* buffer is still empty */
Oren Weilfb7d8792011-05-15 13:43:42 +03001032 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1033 mei_hdr->host_addr = cl->host_client_id;
1034 mei_hdr->me_addr = cl->me_client_id;
1035 mei_hdr->length =
1036 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1037 mei_hdr->msg_complete = 0;
1038 mei_hdr->reserved = 0;
1039
Tomas Winkler7bdf72d2012-07-04 19:24:52 +03001040 *slots -= mei_data2slots(mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +03001041
Tomas Winkler1ccb7b62012-03-14 14:39:42 +02001042 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +03001043 (dev->iamthif_msg_buf +
1044 dev->iamthif_msg_buf_index),
1045 mei_hdr->length)) {
1046 cl->status = -ENODEV;
1047 list_del(&cb_pos->cb_list);
1048 } else {
1049 dev->iamthif_msg_buf_index += mei_hdr->length;
1050 }
1051 return -EMSGSIZE;
1052 } else {
1053 return -EBADMSG;
1054 }
1055
1056 return 0;
1057}
1058
1059/**
1060 * mei_irq_thread_read_handler - bottom half read routine after ISR to
1061 * handle the read processing.
1062 *
1063 * @cmpl_list: An instance of our list structure
1064 * @dev: the device structure
1065 * @slots: slots to read.
1066 *
1067 * returns 0 on success, <0 on failure.
1068 */
1069static int mei_irq_thread_read_handler(struct mei_io_list *cmpl_list,
1070 struct mei_device *dev,
1071 s32 *slots)
1072{
1073 struct mei_msg_hdr *mei_hdr;
1074 struct mei_cl *cl_pos = NULL;
1075 struct mei_cl *cl_next = NULL;
1076 int ret = 0;
1077
1078 if (!dev->rd_msg_hdr) {
1079 dev->rd_msg_hdr = mei_mecbrw_read(dev);
1080 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1081 (*slots)--;
1082 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1083 }
1084 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
1085 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
1086
1087 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
1088 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
1089 ret = -EBADMSG;
1090 goto end;
1091 }
1092
1093 if (mei_hdr->host_addr || mei_hdr->me_addr) {
1094 list_for_each_entry_safe(cl_pos, cl_next,
1095 &dev->file_list, link) {
1096 dev_dbg(&dev->pdev->dev,
1097 "list_for_each_entry_safe read host"
1098 " client = %d, ME client = %d\n",
1099 cl_pos->host_client_id,
1100 cl_pos->me_client_id);
1101 if (cl_pos->host_client_id == mei_hdr->host_addr &&
1102 cl_pos->me_client_id == mei_hdr->me_addr)
1103 break;
1104 }
1105
1106 if (&cl_pos->link == &dev->file_list) {
1107 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
1108 ret = -EBADMSG;
1109 goto end;
1110 }
1111 }
1112 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
1113 dev_dbg(&dev->pdev->dev,
1114 "we can't read the message slots =%08x.\n",
1115 *slots);
1116 /* we can't read the message */
1117 ret = -ERANGE;
1118 goto end;
1119 }
1120
1121 /* decide where to read the message too */
1122 if (!mei_hdr->host_addr) {
1123 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
1124 mei_irq_thread_read_bus_message(dev, mei_hdr);
1125 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
1126 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
1127 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
1128 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
1129 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
1130 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
1131 mei_hdr->length);
1132 ret = mei_irq_thread_read_amthi_message(cmpl_list,
1133 dev, mei_hdr);
1134 if (ret)
1135 goto end;
1136
1137 } else {
1138 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
1139 ret = mei_irq_thread_read_client_message(cmpl_list,
1140 dev, mei_hdr);
1141 if (ret)
1142 goto end;
1143
1144 }
1145
1146 /* reset the number of slots and header */
1147 *slots = mei_count_full_read_slots(dev);
1148 dev->rd_msg_hdr = 0;
1149
1150 if (*slots == -EOVERFLOW) {
1151 /* overflow - reset */
1152 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
1153 /* set the event since message has been read */
1154 ret = -ERANGE;
1155 goto end;
1156 }
1157end:
1158 return ret;
1159}
1160
1161
1162/**
1163 * mei_irq_thread_write_handler - bottom half write routine after
1164 * ISR to handle the write processing.
1165 *
1166 * @cmpl_list: An instance of our list structure
1167 * @dev: the device structure
1168 * @slots: slots to write.
1169 *
1170 * returns 0 on success, <0 on failure.
1171 */
1172static int mei_irq_thread_write_handler(struct mei_io_list *cmpl_list,
1173 struct mei_device *dev,
1174 s32 *slots)
1175{
1176
1177 struct mei_cl *cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001178 struct mei_cl_cb *pos = NULL, *next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +03001179 struct mei_io_list *list;
1180 int ret;
1181
Tomas Winkler726917f2012-06-25 23:46:28 +03001182 if (!mei_hbuf_is_empty(dev)) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001183 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
1184 return 0;
1185 }
Tomas Winkler726917f2012-06-25 23:46:28 +03001186 *slots = mei_hbuf_empty_slots(dev);
Tomas Winkler7d5e0e52012-06-19 09:13:36 +03001187 if (*slots <= 0)
1188 return -EMSGSIZE;
1189
Oren Weilfb7d8792011-05-15 13:43:42 +03001190 /* complete all waiting for write CB */
1191 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
1192
1193 list = &dev->write_waiting_list;
Tomas Winkler483136e2012-07-04 19:24:54 +03001194 list_for_each_entry_safe(pos, next, &list->mei_cb.cb_list, cb_list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001195 cl = (struct mei_cl *)pos->file_private;
1196 if (cl == NULL)
1197 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001198
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001199 cl->status = 0;
1200 list_del(&pos->cb_list);
1201 if (MEI_WRITING == cl->writing_state &&
1202 (pos->major_file_operations == MEI_WRITE) &&
1203 (cl != &dev->iamthif_cl)) {
Tomas Winkler483136e2012-07-04 19:24:54 +03001204 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001205 cl->writing_state = MEI_WRITE_COMPLETE;
1206 list_add_tail(&pos->cb_list,
Tomas Winkler483136e2012-07-04 19:24:54 +03001207 &cmpl_list->mei_cb.cb_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001208 }
1209 if (cl == &dev->iamthif_cl) {
1210 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
1211 if (dev->iamthif_flow_control_pending) {
Tomas Winkler483136e2012-07-04 19:24:54 +03001212 ret = _mei_irq_thread_iamthif_read(dev, slots);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001213 if (ret)
1214 return ret;
1215 }
Oren Weilfb7d8792011-05-15 13:43:42 +03001216 }
1217 }
1218
Tomas Winklerc216fde2012-08-16 19:39:43 +03001219 if (dev->wd_state == MEI_WD_STOPPING) {
1220 dev->wd_state = MEI_WD_IDLE;
Oren Weilfb7d8792011-05-15 13:43:42 +03001221 wake_up_interruptible(&dev->wait_stop_wd);
Oren Weilfb7d8792011-05-15 13:43:42 +03001222 }
1223
1224 if (dev->extra_write_index) {
1225 dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
1226 dev->extra_write_index);
1227 mei_write_message(dev,
1228 (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
1229 (unsigned char *) &dev->ext_msg_buf[1],
1230 (dev->extra_write_index - 1) * sizeof(u32));
1231 *slots -= dev->extra_write_index;
1232 dev->extra_write_index = 0;
1233 }
Tomas Winklerb210d752012-08-07 00:03:56 +03001234 if (dev->dev_state == MEI_DEV_ENABLED) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001235 if (dev->wd_pending &&
Tomas Winkler483136e2012-07-04 19:24:54 +03001236 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001237 if (mei_wd_send(dev))
1238 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
Tomas Winkler483136e2012-07-04 19:24:54 +03001239 else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
1240 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +03001241
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001242 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +03001243
Tomas Winklerc216fde2012-08-16 19:39:43 +03001244 if (dev->wd_state == MEI_WD_RUNNING)
Tomas Winkler248ffdf2012-08-16 19:39:42 +03001245 *slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
Tomas Winklerd242a0a2012-07-04 19:24:50 +03001246 else
Tomas Winkler248ffdf2012-08-16 19:39:42 +03001247 *slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
Oren Weilfb7d8792011-05-15 13:43:42 +03001248 }
1249 }
Oren Weilfb7d8792011-05-15 13:43:42 +03001250
1251 /* complete control write list CB */
Tomas Winklerc8372092011-11-27 21:43:33 +02001252 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001253 list_for_each_entry_safe(pos, next,
Oren Weilfb7d8792011-05-15 13:43:42 +03001254 &dev->ctrl_wr_list.mei_cb.cb_list, cb_list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001255 cl = (struct mei_cl *) pos->file_private;
Tomas Winklerc8372092011-11-27 21:43:33 +02001256 if (!cl) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001257 list_del(&pos->cb_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001258 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +03001259 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001260 switch (pos->major_file_operations) {
Tomas Winklerc8372092011-11-27 21:43:33 +02001261 case MEI_CLOSE:
1262 /* send disconnect message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001263 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001264 if (ret)
1265 return ret;
1266
1267 break;
1268 case MEI_READ:
1269 /* send flow control message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001270 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001271 if (ret)
1272 return ret;
1273
1274 break;
1275 case MEI_IOCTL:
1276 /* connect message */
Natalia Ovsyanikove8cd29d2011-12-05 00:16:54 +02001277 if (mei_other_client_is_connecting(dev, cl))
Tomas Winklerc8372092011-11-27 21:43:33 +02001278 continue;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001279 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001280 if (ret)
1281 return ret;
1282
1283 break;
1284
1285 default:
1286 BUG();
1287 }
1288
Oren Weilfb7d8792011-05-15 13:43:42 +03001289 }
1290 /* complete write list CB */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001291 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
1292 list_for_each_entry_safe(pos, next,
Tomas Winkler483136e2012-07-04 19:24:54 +03001293 &dev->write_list.mei_cb.cb_list, cb_list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001294 cl = (struct mei_cl *)pos->file_private;
1295 if (cl == NULL)
1296 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001297
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001298 if (cl != &dev->iamthif_cl) {
Tomas Winklerd2041152012-06-19 09:13:34 +03001299 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001300 dev_dbg(&dev->pdev->dev,
Tomas Winkler483136e2012-07-04 19:24:54 +03001301 "No flow control credentials for client %d, not sending.\n",
1302 cl->host_client_id);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001303 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001304 }
Tomas Winkler483136e2012-07-04 19:24:54 +03001305 ret = _mei_irq_thread_cmpl(dev, slots, pos,
1306 cl, cmpl_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001307 if (ret)
1308 return ret;
1309
1310 } else if (cl == &dev->iamthif_cl) {
1311 /* IAMTHIF IOCTL */
1312 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
Tomas Winklerd2041152012-06-19 09:13:34 +03001313 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001314 dev_dbg(&dev->pdev->dev,
Tomas Winkler483136e2012-07-04 19:24:54 +03001315 "No flow control credentials for amthi client %d.\n",
1316 cl->host_client_id);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001317 continue;
1318 }
Tomas Winkler483136e2012-07-04 19:24:54 +03001319 ret = _mei_irq_thread_cmpl_iamthif(dev, slots, pos,
1320 cl, cmpl_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001321 if (ret)
1322 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +03001323
1324 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001325
Oren Weilfb7d8792011-05-15 13:43:42 +03001326 }
1327 return 0;
1328}
1329
1330
1331
1332/**
1333 * mei_timer - timer function.
1334 *
1335 * @work: pointer to the work_struct structure
1336 *
1337 * NOTE: This function is called by timer interrupt work
1338 */
Oren Weila61c6532011-09-07 09:03:13 +03001339void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +03001340{
1341 unsigned long timeout;
1342 struct mei_cl *cl_pos = NULL;
1343 struct mei_cl *cl_next = NULL;
1344 struct list_head *amthi_complete_list = NULL;
1345 struct mei_cl_cb *cb_pos = NULL;
1346 struct mei_cl_cb *cb_next = NULL;
1347
1348 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +03001349 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +03001350
1351
1352 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001353 if (dev->dev_state != MEI_DEV_ENABLED) {
1354 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001355 if (dev->init_clients_timer) {
1356 if (--dev->init_clients_timer == 0) {
1357 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1358 dev->init_clients_state);
1359 mei_reset(dev, 1);
1360 }
1361 }
1362 }
1363 goto out;
1364 }
1365 /*** connect/disconnect timeouts ***/
1366 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1367 if (cl_pos->timer_count) {
1368 if (--cl_pos->timer_count == 0) {
1369 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1370 mei_reset(dev, 1);
1371 goto out;
1372 }
1373 }
1374 }
1375
Oren Weilfb7d8792011-05-15 13:43:42 +03001376 if (dev->iamthif_stall_timer) {
1377 if (--dev->iamthif_stall_timer == 0) {
Masanari Iida32de21f2012-01-25 23:14:56 +09001378 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +03001379 mei_reset(dev, 1);
1380 dev->iamthif_msg_buf_size = 0;
1381 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001382 dev->iamthif_canceled = false;
1383 dev->iamthif_ioctl = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001384 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1385 dev->iamthif_timer = 0;
1386
1387 if (dev->iamthif_current_cb)
1388 mei_free_cb_private(dev->iamthif_current_cb);
1389
1390 dev->iamthif_file_object = NULL;
1391 dev->iamthif_current_cb = NULL;
Tomas Winklerc95efb72011-05-25 17:28:21 +03001392 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001393 }
1394 }
1395
1396 if (dev->iamthif_timer) {
1397
1398 timeout = dev->iamthif_timer +
1399 msecs_to_jiffies(IAMTHIF_READ_TIMER);
1400
1401 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1402 dev->iamthif_timer);
1403 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1404 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1405 if (time_after(jiffies, timeout)) {
1406 /*
1407 * User didn't read the AMTHI data on time (15sec)
1408 * freeing AMTHI for other requests
1409 */
1410
1411 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1412
1413 amthi_complete_list = &dev->amthi_read_complete_list.
1414 mei_cb.cb_list;
1415
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001416 list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, cb_list) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001417
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001418 cl_pos = cb_pos->file_object->private_data;
Oren Weilfb7d8792011-05-15 13:43:42 +03001419
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001420 /* Finding the AMTHI entry. */
1421 if (cl_pos == &dev->iamthif_cl)
1422 list_del(&cb_pos->cb_list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001423 }
1424 if (dev->iamthif_current_cb)
1425 mei_free_cb_private(dev->iamthif_current_cb);
1426
1427 dev->iamthif_file_object->private_data = NULL;
1428 dev->iamthif_file_object = NULL;
1429 dev->iamthif_current_cb = NULL;
1430 dev->iamthif_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +03001431 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001432
1433 }
1434 }
1435out:
Tomas Winkler441ab502011-12-13 23:39:34 +02001436 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1437 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +03001438}
1439
1440/**
1441 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1442 * processing.
1443 *
1444 * @irq: The irq number
1445 * @dev_id: pointer to the device structure
1446 *
1447 * returns irqreturn_t
1448 *
1449 */
1450irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1451{
1452 struct mei_device *dev = (struct mei_device *) dev_id;
1453 struct mei_io_list complete_list;
1454 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1455 struct mei_cl *cl;
1456 s32 slots;
1457 int rets;
1458 bool bus_message_received;
1459
1460
1461 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1462 /* initialize our complete list */
1463 mutex_lock(&dev->device_lock);
Tomas Winkler0288c7c2011-06-06 10:44:34 +03001464 mei_io_list_init(&complete_list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001465 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001466
1467 /* Ack the interrupt here
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001468 * In case of MSI we don't go through the quick handler */
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001469 if (pci_dev_msi_enabled(dev->pdev))
1470 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1471
Oren Weilfb7d8792011-05-15 13:43:42 +03001472 dev->me_hw_state = mei_mecsr_read(dev);
1473
1474 /* check if ME wants a reset */
1475 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
Tomas Winklerb210d752012-08-07 00:03:56 +03001476 dev->dev_state != MEI_DEV_RESETING &&
1477 dev->dev_state != MEI_DEV_INITIALIZING) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001478 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1479 mei_reset(dev, 1);
1480 mutex_unlock(&dev->device_lock);
1481 return IRQ_HANDLED;
1482 }
1483
1484 /* check if we need to start the dev */
1485 if ((dev->host_hw_state & H_RDY) == 0) {
1486 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1487 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1488 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1489 mei_hcsr_set(dev);
Tomas Winklerb210d752012-08-07 00:03:56 +03001490 dev->dev_state = MEI_DEV_INIT_CLIENTS;
Oren Weilfb7d8792011-05-15 13:43:42 +03001491 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1492 /* link is established
1493 * start sending messages.
1494 */
Tomas Winklerc95efb72011-05-25 17:28:21 +03001495 mei_host_start_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001496 mutex_unlock(&dev->device_lock);
1497 return IRQ_HANDLED;
1498 } else {
1499 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1500 mutex_unlock(&dev->device_lock);
1501 return IRQ_HANDLED;
1502 }
1503 }
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001504 /* check slots available for reading */
Oren Weilfb7d8792011-05-15 13:43:42 +03001505 slots = mei_count_full_read_slots(dev);
1506 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1507 slots, dev->extra_write_index);
1508 while (slots > 0 && !dev->extra_write_index) {
1509 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1510 slots, dev->extra_write_index);
1511 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1512 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1513 if (rets)
1514 goto end;
1515 }
1516 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1517end:
1518 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1519 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler726917f2012-06-25 23:46:28 +03001520 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001521
1522 bus_message_received = false;
1523 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1524 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1525 bus_message_received = true;
1526 }
1527 mutex_unlock(&dev->device_lock);
1528 if (bus_message_received) {
1529 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1530 wake_up_interruptible(&dev->wait_recvd_msg);
1531 bus_message_received = false;
1532 }
Tomas Winklerc8372092011-11-27 21:43:33 +02001533 if (list_empty(&complete_list.mei_cb.cb_list))
Oren Weilfb7d8792011-05-15 13:43:42 +03001534 return IRQ_HANDLED;
1535
1536
1537 list_for_each_entry_safe(cb_pos, cb_next,
1538 &complete_list.mei_cb.cb_list, cb_list) {
1539 cl = (struct mei_cl *)cb_pos->file_private;
1540 list_del(&cb_pos->cb_list);
1541 if (cl) {
1542 if (cl != &dev->iamthif_cl) {
1543 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1544 _mei_cmpl(cl, cb_pos);
1545 cb_pos = NULL;
1546 } else if (cl == &dev->iamthif_cl) {
1547 _mei_cmpl_iamthif(dev, cb_pos);
1548 }
1549 }
1550 }
1551 return IRQ_HANDLED;
1552}