blob: 4c1afcf6f4e905adaa7ab5cc94803143eabb5ced [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;
152 cb->information = dev->iamthif_msg_buf_index;
153 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 Winkleredf1eed2012-02-09 19:25:54 +0200208 buffer = cb_pos->response_buffer.data + cb_pos->information;
Oren Weilfb7d8792011-05-15 13:43:42 +0300209
210 if (cb_pos->response_buffer.size <
211 mei_hdr->length + cb_pos->information) {
212 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
219 cb_pos->information += mei_hdr->length;
220 if (mei_hdr->msg_complete) {
221 cl->status = 0;
222 list_del(&cb_pos->cb_list);
223 dev_dbg(&dev->pdev->dev,
224 "completed read host client = %d,"
225 "ME client = %d, "
226 "data length = %lu\n",
227 cl->host_client_id,
228 cl->me_client_id,
229 cb_pos->information);
230
231 *(cb_pos->response_buffer.data +
232 cb_pos->information) = '\0';
233 dev_dbg(&dev->pdev->dev, "cb_pos->res_buffer - %s\n",
234 cb_pos->response_buffer.data);
235 list_add_tail(&cb_pos->cb_list,
236 &complete_list->mei_cb.cb_list);
237 }
238
239 break;
240 }
241
242 }
243
244quit:
245 dev_dbg(&dev->pdev->dev, "message read\n");
246 if (!buffer) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200247 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +0300248 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
249 *(u32 *) dev->rd_msg_buf);
250 }
251
252 return 0;
253}
254
255/**
256 * _mei_irq_thread_iamthif_read - prepares to read iamthif data.
257 *
258 * @dev: the device structure.
259 * @slots: free slots.
260 *
261 * returns 0, OK; otherwise, error.
262 */
263static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots)
264{
265
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200266 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +0300267 + sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300268 return -EMSGSIZE;
269 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200270 *slots -= (sizeof(struct mei_msg_hdr) +
271 sizeof(struct hbm_flow_control) + 3) / 4;
272 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
273 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
274 return -EIO;
275 }
276
277 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
278 dev->iamthif_state = MEI_IAMTHIF_READING;
279 dev->iamthif_flow_control_pending = false;
280 dev->iamthif_msg_buf_index = 0;
281 dev->iamthif_msg_buf_size = 0;
282 dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
Tomas Winkler726917f2012-06-25 23:46:28 +0300283 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200284 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300285}
286
287/**
288 * _mei_irq_thread_close - processes close related operation.
289 *
290 * @dev: the device structure.
291 * @slots: free slots.
292 * @cb_pos: callback block.
293 * @cl: private data of the file object.
294 * @cmpl_list: complete list.
295 *
296 * returns 0, OK; otherwise, error.
297 */
298static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
299 struct mei_cl_cb *cb_pos,
300 struct mei_cl *cl,
301 struct mei_io_list *cmpl_list)
302{
303 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
304 sizeof(struct hbm_client_disconnect_request))) {
305 *slots -= (sizeof(struct mei_msg_hdr) +
306 sizeof(struct hbm_client_disconnect_request) + 3) / 4;
307
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200308 if (mei_disconnect(dev, cl)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300309 cl->status = 0;
310 cb_pos->information = 0;
311 list_move_tail(&cb_pos->cb_list,
312 &cmpl_list->mei_cb.cb_list);
313 return -EMSGSIZE;
314 } else {
315 cl->state = MEI_FILE_DISCONNECTING;
316 cl->status = 0;
317 cb_pos->information = 0;
318 list_move_tail(&cb_pos->cb_list,
319 &dev->ctrl_rd_list.mei_cb.cb_list);
320 cl->timer_count = MEI_CONNECT_TIMEOUT;
321 }
322 } else {
323 /* return the cancel routine */
324 return -EBADMSG;
325 }
326
327 return 0;
328}
329
330/**
331 * is_treat_specially_client - checks if the message belongs
332 * to the file private data.
333 *
334 * @cl: private data of the file object
335 * @rs: connect response bus message
336 *
337 */
338static bool is_treat_specially_client(struct mei_cl *cl,
339 struct hbm_client_connect_response *rs)
340{
341
342 if (cl->host_client_id == rs->host_addr &&
343 cl->me_client_id == rs->me_addr) {
344 if (!rs->status) {
345 cl->state = MEI_FILE_CONNECTED;
346 cl->status = 0;
347
348 } else {
349 cl->state = MEI_FILE_DISCONNECTED;
350 cl->status = -ENODEV;
351 }
352 cl->timer_count = 0;
353
354 return true;
355 }
356 return false;
357}
358
359/**
360 * mei_client_connect_response - connects to response irq routine
361 *
362 * @dev: the device structure
363 * @rs: connect response bus message
364 */
365static void mei_client_connect_response(struct mei_device *dev,
366 struct hbm_client_connect_response *rs)
367{
368
369 struct mei_cl *cl;
370 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
371
372 dev_dbg(&dev->pdev->dev,
373 "connect_response:\n"
374 "ME Client = %d\n"
375 "Host Client = %d\n"
376 "Status = %d\n",
377 rs->me_addr,
378 rs->host_addr,
379 rs->status);
380
381 /* if WD or iamthif client treat specially */
382
383 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300384 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
Tomas Winkler70cd5332011-12-22 18:50:50 +0200385 mei_watchdog_register(dev);
Oren Weil9ce178e2011-09-07 09:03:09 +0300386
Tomas Winkler70cd5332011-12-22 18:50:50 +0200387 /* next step in the state maching */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300388 mei_host_init_iamthif(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300389 return;
390 }
391
392 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
393 dev->iamthif_state = MEI_IAMTHIF_IDLE;
394 return;
395 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200396 list_for_each_entry_safe(cb_pos, cb_next,
397 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
398
399 cl = (struct mei_cl *)cb_pos->file_private;
400 if (!cl) {
401 list_del(&cb_pos->cb_list);
402 return;
403 }
404 if (MEI_IOCTL == cb_pos->major_file_operations) {
405 if (is_treat_specially_client(cl, rs)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300406 list_del(&cb_pos->cb_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200407 cl->status = 0;
408 cl->timer_count = 0;
409 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300410 }
411 }
412 }
413}
414
415/**
416 * mei_client_disconnect_response - disconnects from response irq routine
417 *
418 * @dev: the device structure
419 * @rs: disconnect response bus message
420 */
421static void mei_client_disconnect_response(struct mei_device *dev,
422 struct hbm_client_connect_response *rs)
423{
424 struct mei_cl *cl;
425 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
426
427 dev_dbg(&dev->pdev->dev,
428 "disconnect_response:\n"
429 "ME Client = %d\n"
430 "Host Client = %d\n"
431 "Status = %d\n",
432 rs->me_addr,
433 rs->host_addr,
434 rs->status);
435
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200436 list_for_each_entry_safe(cb_pos, cb_next,
437 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
438 cl = (struct mei_cl *)cb_pos->file_private;
Oren Weilfb7d8792011-05-15 13:43:42 +0300439
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200440 if (!cl) {
441 list_del(&cb_pos->cb_list);
442 return;
443 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300444
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200445 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
446 if (cl->host_client_id == rs->host_addr &&
447 cl->me_client_id == rs->me_addr) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300448
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200449 list_del(&cb_pos->cb_list);
450 if (!rs->status)
451 cl->state = MEI_FILE_DISCONNECTED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300452
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200453 cl->status = 0;
454 cl->timer_count = 0;
455 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300456 }
457 }
458}
459
460/**
461 * same_flow_addr - tells if they have the same address.
462 *
463 * @file: private data of the file object.
464 * @flow: flow control.
465 *
466 * returns !=0, same; 0,not.
467 */
468static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
469{
470 return (cl->host_client_id == flow->host_addr &&
471 cl->me_client_id == flow->me_addr);
472}
473
474/**
475 * add_single_flow_creds - adds single buffer credentials.
476 *
477 * @file: private data ot the file object.
478 * @flow: flow control.
479 */
480static void add_single_flow_creds(struct mei_device *dev,
481 struct hbm_flow_control *flow)
482{
483 struct mei_me_client *client;
484 int i;
485
Tomas Winklercf9673d2011-06-06 10:44:33 +0300486 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300487 client = &dev->me_clients[i];
488 if (client && flow->me_addr == client->client_id) {
489 if (client->props.single_recv_buf) {
490 client->mei_flow_ctrl_creds++;
491 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
492 flow->me_addr);
493 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
494 client->mei_flow_ctrl_creds);
495 } else {
496 BUG(); /* error in flow control */
497 }
498 }
499 }
500}
501
502/**
503 * mei_client_flow_control_response - flow control response irq routine
504 *
505 * @dev: the device structure
506 * @flow_control: flow control response bus message
507 */
508static void mei_client_flow_control_response(struct mei_device *dev,
509 struct hbm_flow_control *flow_control)
510{
511 struct mei_cl *cl_pos = NULL;
512 struct mei_cl *cl_next = NULL;
513
514 if (!flow_control->host_addr) {
515 /* single receive buffer */
516 add_single_flow_creds(dev, flow_control);
517 } else {
518 /* normal connection */
519 list_for_each_entry_safe(cl_pos, cl_next,
520 &dev->file_list, link) {
521 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
522
523 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
524 cl_pos->host_client_id,
525 cl_pos->me_client_id);
526 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
527 flow_control->host_addr,
528 flow_control->me_addr);
529 if (same_flow_addr(cl_pos, flow_control)) {
530 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
531 flow_control->host_addr,
532 flow_control->me_addr);
533 cl_pos->mei_flow_ctrl_creds++;
534 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
535 cl_pos->mei_flow_ctrl_creds);
536 break;
537 }
538 }
539 }
540}
541
542/**
543 * same_disconn_addr - tells if they have the same address
544 *
545 * @file: private data of the file object.
546 * @disconn: disconnection request.
547 *
548 * returns !=0, same; 0,not.
549 */
550static int same_disconn_addr(struct mei_cl *cl,
551 struct hbm_client_disconnect_request *disconn)
552{
553 return (cl->host_client_id == disconn->host_addr &&
554 cl->me_client_id == disconn->me_addr);
555}
556
557/**
558 * mei_client_disconnect_request - disconnects from request irq routine
559 *
560 * @dev: the device structure.
561 * @disconnect_req: disconnect request bus message.
562 */
563static void mei_client_disconnect_request(struct mei_device *dev,
564 struct hbm_client_disconnect_request *disconnect_req)
565{
566 struct mei_msg_hdr *mei_hdr;
567 struct hbm_client_connect_response *disconnect_res;
568 struct mei_cl *cl_pos = NULL;
569 struct mei_cl *cl_next = NULL;
570
571 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
572 if (same_disconn_addr(cl_pos, disconnect_req)) {
573 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
574 disconnect_req->host_addr,
575 disconnect_req->me_addr);
576 cl_pos->state = MEI_FILE_DISCONNECTED;
577 cl_pos->timer_count = 0;
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300578 if (cl_pos == &dev->wd_cl)
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300579 dev->wd_pending = false;
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300580 else if (cl_pos == &dev->iamthif_cl)
Oren Weilfb7d8792011-05-15 13:43:42 +0300581 dev->iamthif_timer = 0;
582
583 /* prepare disconnect response */
584 mei_hdr =
585 (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
586 mei_hdr->host_addr = 0;
587 mei_hdr->me_addr = 0;
588 mei_hdr->length =
589 sizeof(struct hbm_client_connect_response);
590 mei_hdr->msg_complete = 1;
591 mei_hdr->reserved = 0;
592
593 disconnect_res =
594 (struct hbm_client_connect_response *)
595 &dev->ext_msg_buf[1];
596 disconnect_res->host_addr = cl_pos->host_client_id;
597 disconnect_res->me_addr = cl_pos->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200598 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300599 disconnect_res->status = 0;
600 dev->extra_write_index = 2;
601 break;
602 }
603 }
604}
605
606
607/**
608 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
609 * handle the read bus message cmd processing.
610 *
611 * @dev: the device structure
612 * @mei_hdr: header of bus message
613 */
614static void mei_irq_thread_read_bus_message(struct mei_device *dev,
615 struct mei_msg_hdr *mei_hdr)
616{
617 struct mei_bus_message *mei_msg;
618 struct hbm_host_version_response *version_res;
619 struct hbm_client_connect_response *connect_res;
620 struct hbm_client_connect_response *disconnect_res;
621 struct hbm_flow_control *flow_control;
622 struct hbm_props_response *props_res;
623 struct hbm_host_enum_response *enum_res;
624 struct hbm_client_disconnect_request *disconnect_req;
625 struct hbm_host_stop_request *host_stop_req;
Oren Weilabc51b62011-09-21 16:45:30 +0300626 int res;
Oren Weilfb7d8792011-05-15 13:43:42 +0300627
Oren Weilfb7d8792011-05-15 13:43:42 +0300628
629 /* read the message to our buffer */
Oren Weilfb7d8792011-05-15 13:43:42 +0300630 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200631 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
632 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
Oren Weilfb7d8792011-05-15 13:43:42 +0300633
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200634 switch (mei_msg->hbm_cmd) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300635 case HOST_START_RES_CMD:
636 version_res = (struct hbm_host_version_response *) mei_msg;
637 if (version_res->host_version_supported) {
638 dev->version.major_version = HBM_MAJOR_VERSION;
639 dev->version.minor_version = HBM_MINOR_VERSION;
640 if (dev->mei_state == MEI_INIT_CLIENTS &&
641 dev->init_clients_state == MEI_START_MESSAGE) {
642 dev->init_clients_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300643 mei_host_enum_clients_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300644 } else {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300645 dev->recvd_msg = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300646 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
647 mei_reset(dev, 1);
648 return;
649 }
650 } else {
651 dev->version = version_res->me_max_version;
652 /* send stop message */
Tomas Winkler97d5cb02012-03-06 22:34:32 +0200653 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
Oren Weilfb7d8792011-05-15 13:43:42 +0300654 mei_hdr->host_addr = 0;
655 mei_hdr->me_addr = 0;
656 mei_hdr->length = sizeof(struct hbm_host_stop_request);
657 mei_hdr->msg_complete = 1;
658 mei_hdr->reserved = 0;
659
660 host_stop_req = (struct hbm_host_stop_request *)
661 &dev->wr_msg_buf[1];
662
663 memset(host_stop_req,
664 0,
665 sizeof(struct hbm_host_stop_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200666 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300667 host_stop_req->reason = DRIVER_STOP_REQUEST;
668 mei_write_message(dev, mei_hdr,
669 (unsigned char *) (host_stop_req),
670 mei_hdr->length);
671 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
672 return;
673 }
674
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300675 dev->recvd_msg = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300676 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
677 break;
678
679 case CLIENT_CONNECT_RES_CMD:
680 connect_res =
681 (struct hbm_client_connect_response *) mei_msg;
682 mei_client_connect_response(dev, connect_res);
683 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
684 wake_up(&dev->wait_recvd_msg);
685 break;
686
687 case CLIENT_DISCONNECT_RES_CMD:
688 disconnect_res =
689 (struct hbm_client_connect_response *) mei_msg;
Tomas Winkler441ab502011-12-13 23:39:34 +0200690 mei_client_disconnect_response(dev, disconnect_res);
Oren Weilfb7d8792011-05-15 13:43:42 +0300691 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
692 wake_up(&dev->wait_recvd_msg);
693 break;
694
695 case MEI_FLOW_CONTROL_CMD:
696 flow_control = (struct hbm_flow_control *) mei_msg;
697 mei_client_flow_control_response(dev, flow_control);
698 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
699 break;
700
701 case HOST_CLIENT_PROPERTIES_RES_CMD:
702 props_res = (struct hbm_props_response *)mei_msg;
703 if (props_res->status || !dev->me_clients) {
704 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
705 mei_reset(dev, 1);
706 return;
707 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200708 if (dev->me_clients[dev->me_client_presentation_num]
Oren Weilfb7d8792011-05-15 13:43:42 +0300709 .client_id == props_res->address) {
710
711 dev->me_clients[dev->me_client_presentation_num].props
712 = props_res->client_properties;
713
714 if (dev->mei_state == MEI_INIT_CLIENTS &&
715 dev->init_clients_state ==
716 MEI_CLIENT_PROPERTIES_MESSAGE) {
717 dev->me_client_index++;
718 dev->me_client_presentation_num++;
Oren Weilabc51b62011-09-21 16:45:30 +0300719
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700720 /** Send Client Properties request **/
Oren Weilabc51b62011-09-21 16:45:30 +0300721 res = mei_host_client_properties(dev);
722 if (res < 0) {
723 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
724 return;
725 } else if (!res) {
726 /*
727 * No more clients to send to.
728 * Clear Map for indicating now ME clients
729 * with associated host client
730 */
731 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
732 dev->open_handle_count = 0;
733
734 /*
735 * Reserving the first three client IDs
736 * Client Id 0 - Reserved for MEI Bus Message communications
737 * Client Id 1 - Reserved for Watchdog
738 * Client ID 2 - Reserved for AMTHI
739 */
740 bitmap_set(dev->host_clients_map, 0, 3);
741 dev->mei_state = MEI_ENABLED;
742
743 /* if wd initialization fails, initialization the AMTHI client,
744 * otherwise the AMTHI client will be initialized after the WD client connect response
745 * will be received
746 */
747 if (mei_wd_host_init(dev))
748 mei_host_init_iamthif(dev);
749 }
750
Oren Weilfb7d8792011-05-15 13:43:42 +0300751 } else {
752 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
753 mei_reset(dev, 1);
754 return;
755 }
756 } else {
757 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
758 mei_reset(dev, 1);
759 return;
760 }
761 break;
762
763 case HOST_ENUM_RES_CMD:
764 enum_res = (struct hbm_host_enum_response *) mei_msg;
765 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
766 if (dev->mei_state == MEI_INIT_CLIENTS &&
767 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
768 dev->init_clients_timer = 0;
769 dev->me_client_presentation_num = 0;
770 dev->me_client_index = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300771 mei_allocate_me_clients_storage(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300772 dev->init_clients_state =
773 MEI_CLIENT_PROPERTIES_MESSAGE;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300774 mei_host_client_properties(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300775 } else {
776 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
777 mei_reset(dev, 1);
778 return;
779 }
780 break;
781
782 case HOST_STOP_RES_CMD:
783 dev->mei_state = MEI_DISABLED;
784 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
785 mei_reset(dev, 1);
786 break;
787
788 case CLIENT_DISCONNECT_REQ_CMD:
789 /* search for client */
790 disconnect_req =
791 (struct hbm_client_disconnect_request *) mei_msg;
792 mei_client_disconnect_request(dev, disconnect_req);
793 break;
794
795 case ME_STOP_REQ_CMD:
796 /* prepare stop request */
797 mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
798 mei_hdr->host_addr = 0;
799 mei_hdr->me_addr = 0;
800 mei_hdr->length = sizeof(struct hbm_host_stop_request);
801 mei_hdr->msg_complete = 1;
802 mei_hdr->reserved = 0;
803 host_stop_req =
804 (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
805 memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200806 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
Oren Weilfb7d8792011-05-15 13:43:42 +0300807 host_stop_req->reason = DRIVER_STOP_REQUEST;
808 host_stop_req->reserved[0] = 0;
809 host_stop_req->reserved[1] = 0;
810 dev->extra_write_index = 2;
811 break;
812
813 default:
814 BUG();
815 break;
816
817 }
818}
819
820
821/**
822 * _mei_hb_read - processes read related operation.
823 *
824 * @dev: the device structure.
825 * @slots: free slots.
826 * @cb_pos: callback block.
827 * @cl: private data of the file object.
828 * @cmpl_list: complete list.
829 *
830 * returns 0, OK; otherwise, error.
831 */
832static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
833 struct mei_cl_cb *cb_pos,
834 struct mei_cl *cl,
835 struct mei_io_list *cmpl_list)
836{
837 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
838 sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300839 /* return the cancel routine */
840 list_del(&cb_pos->cb_list);
841 return -EBADMSG;
842 }
843
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200844 *slots -= (sizeof(struct mei_msg_hdr) +
845 sizeof(struct hbm_flow_control) + 3) / 4;
846 if (mei_send_flow_control(dev, cl)) {
847 cl->status = -ENODEV;
848 cb_pos->information = 0;
849 list_move_tail(&cb_pos->cb_list, &cmpl_list->mei_cb.cb_list);
850 return -ENODEV;
851 }
852 list_move_tail(&cb_pos->cb_list, &dev->read_list.mei_cb.cb_list);
853
Oren Weilfb7d8792011-05-15 13:43:42 +0300854 return 0;
855}
856
857
858/**
859 * _mei_irq_thread_ioctl - processes ioctl related operation.
860 *
861 * @dev: the device structure.
862 * @slots: free slots.
863 * @cb_pos: callback block.
864 * @cl: private data of the file object.
865 * @cmpl_list: complete list.
866 *
867 * returns 0, OK; otherwise, error.
868 */
869static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
870 struct mei_cl_cb *cb_pos,
871 struct mei_cl *cl,
872 struct mei_io_list *cmpl_list)
873{
874 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
875 sizeof(struct hbm_client_connect_request))) {
876 cl->state = MEI_FILE_CONNECTING;
877 *slots -= (sizeof(struct mei_msg_hdr) +
878 sizeof(struct hbm_client_connect_request) + 3) / 4;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200879 if (mei_connect(dev, cl)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300880 cl->status = -ENODEV;
881 cb_pos->information = 0;
882 list_del(&cb_pos->cb_list);
883 return -ENODEV;
884 } else {
885 list_move_tail(&cb_pos->cb_list,
886 &dev->ctrl_rd_list.mei_cb.cb_list);
887 cl->timer_count = MEI_CONNECT_TIMEOUT;
888 }
889 } else {
890 /* return the cancel routine */
891 list_del(&cb_pos->cb_list);
892 return -EBADMSG;
893 }
894
895 return 0;
896}
897
898/**
899 * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
900 *
901 * @dev: the device structure.
902 * @slots: free slots.
903 * @cb_pos: callback block.
904 * @cl: private data of the file object.
905 * @cmpl_list: complete list.
906 *
907 * returns 0, OK; otherwise, error.
908 */
909static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
910 struct mei_cl_cb *cb_pos,
911 struct mei_cl *cl,
912 struct mei_io_list *cmpl_list)
913{
914 struct mei_msg_hdr *mei_hdr;
915
916 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
917 (cb_pos->request_buffer.size -
918 cb_pos->information))) {
919 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
920 mei_hdr->host_addr = cl->host_client_id;
921 mei_hdr->me_addr = cl->me_client_id;
922 mei_hdr->length = cb_pos->request_buffer.size -
923 cb_pos->information;
924 mei_hdr->msg_complete = 1;
925 mei_hdr->reserved = 0;
926 dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
927 "mei_hdr->msg_complete = %d\n",
928 cb_pos->request_buffer.size,
929 mei_hdr->msg_complete);
930 dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
931 cb_pos->information);
932 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
933 mei_hdr->length);
934 *slots -= (sizeof(struct mei_msg_hdr) +
935 mei_hdr->length + 3) / 4;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200936 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300937 (unsigned char *)
938 (cb_pos->request_buffer.data +
939 cb_pos->information),
940 mei_hdr->length)) {
941 cl->status = -ENODEV;
942 list_move_tail(&cb_pos->cb_list,
943 &cmpl_list->mei_cb.cb_list);
944 return -ENODEV;
945 } else {
946 if (mei_flow_ctrl_reduce(dev, cl))
947 return -ENODEV;
948 cl->status = 0;
949 cb_pos->information += mei_hdr->length;
950 list_move_tail(&cb_pos->cb_list,
951 &dev->write_waiting_list.mei_cb.cb_list);
952 }
Tomas Winkler24aadc82012-06-25 23:46:27 +0300953 } else if (*slots == dev->hbuf_depth) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300954 /* buffer is still empty */
955 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
956 mei_hdr->host_addr = cl->host_client_id;
957 mei_hdr->me_addr = cl->me_client_id;
958 mei_hdr->length =
959 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
960 mei_hdr->msg_complete = 0;
961 mei_hdr->reserved = 0;
962
963 (*slots) -= (sizeof(struct mei_msg_hdr) +
964 mei_hdr->length + 3) / 4;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200965 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +0300966 (unsigned char *)
967 (cb_pos->request_buffer.data +
968 cb_pos->information),
969 mei_hdr->length)) {
970 cl->status = -ENODEV;
971 list_move_tail(&cb_pos->cb_list,
972 &cmpl_list->mei_cb.cb_list);
973 return -ENODEV;
974 } else {
975 cb_pos->information += mei_hdr->length;
976 dev_dbg(&dev->pdev->dev,
977 "cb_pos->request_buffer.size =%d"
978 " mei_hdr->msg_complete = %d\n",
979 cb_pos->request_buffer.size,
980 mei_hdr->msg_complete);
981 dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
982 cb_pos->information);
983 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
984 mei_hdr->length);
985 }
986 return -EMSGSIZE;
987 } else {
988 return -EBADMSG;
989 }
990
991 return 0;
992}
993
994/**
995 * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
996 *
997 * @dev: the device structure.
998 * @slots: free slots.
999 * @cb_pos: callback block.
1000 * @cl: private data of the file object.
1001 * @cmpl_list: complete list.
1002 *
1003 * returns 0, OK; otherwise, error.
1004 */
1005static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
1006 struct mei_cl_cb *cb_pos,
1007 struct mei_cl *cl,
1008 struct mei_io_list *cmpl_list)
1009{
1010 struct mei_msg_hdr *mei_hdr;
1011
1012 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
1013 dev->iamthif_msg_buf_size -
1014 dev->iamthif_msg_buf_index)) {
1015 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1016 mei_hdr->host_addr = cl->host_client_id;
1017 mei_hdr->me_addr = cl->me_client_id;
1018 mei_hdr->length = dev->iamthif_msg_buf_size -
1019 dev->iamthif_msg_buf_index;
1020 mei_hdr->msg_complete = 1;
1021 mei_hdr->reserved = 0;
1022
1023 *slots -= (sizeof(struct mei_msg_hdr) +
1024 mei_hdr->length + 3) / 4;
1025
Tomas Winkler1ccb7b62012-03-14 14:39:42 +02001026 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +03001027 (dev->iamthif_msg_buf +
1028 dev->iamthif_msg_buf_index),
1029 mei_hdr->length)) {
1030 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1031 cl->status = -ENODEV;
1032 list_del(&cb_pos->cb_list);
1033 return -ENODEV;
1034 } else {
1035 if (mei_flow_ctrl_reduce(dev, cl))
1036 return -ENODEV;
1037 dev->iamthif_msg_buf_index += mei_hdr->length;
1038 cb_pos->information = dev->iamthif_msg_buf_index;
1039 cl->status = 0;
1040 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001041 dev->iamthif_flow_control_pending = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001042 /* save iamthif cb sent to amthi client */
1043 dev->iamthif_current_cb = cb_pos;
1044 list_move_tail(&cb_pos->cb_list,
1045 &dev->write_waiting_list.mei_cb.cb_list);
1046
1047 }
Tomas Winkler24aadc82012-06-25 23:46:27 +03001048 } else if (*slots == dev->hbuf_depth) {
1049 /* buffer is still empty */
Oren Weilfb7d8792011-05-15 13:43:42 +03001050 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1051 mei_hdr->host_addr = cl->host_client_id;
1052 mei_hdr->me_addr = cl->me_client_id;
1053 mei_hdr->length =
1054 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1055 mei_hdr->msg_complete = 0;
1056 mei_hdr->reserved = 0;
1057
1058 *slots -= (sizeof(struct mei_msg_hdr) +
1059 mei_hdr->length + 3) / 4;
1060
Tomas Winkler1ccb7b62012-03-14 14:39:42 +02001061 if (mei_write_message(dev, mei_hdr,
Oren Weilfb7d8792011-05-15 13:43:42 +03001062 (dev->iamthif_msg_buf +
1063 dev->iamthif_msg_buf_index),
1064 mei_hdr->length)) {
1065 cl->status = -ENODEV;
1066 list_del(&cb_pos->cb_list);
1067 } else {
1068 dev->iamthif_msg_buf_index += mei_hdr->length;
1069 }
1070 return -EMSGSIZE;
1071 } else {
1072 return -EBADMSG;
1073 }
1074
1075 return 0;
1076}
1077
1078/**
1079 * mei_irq_thread_read_handler - bottom half read routine after ISR to
1080 * handle the read processing.
1081 *
1082 * @cmpl_list: An instance of our list structure
1083 * @dev: the device structure
1084 * @slots: slots to read.
1085 *
1086 * returns 0 on success, <0 on failure.
1087 */
1088static int mei_irq_thread_read_handler(struct mei_io_list *cmpl_list,
1089 struct mei_device *dev,
1090 s32 *slots)
1091{
1092 struct mei_msg_hdr *mei_hdr;
1093 struct mei_cl *cl_pos = NULL;
1094 struct mei_cl *cl_next = NULL;
1095 int ret = 0;
1096
1097 if (!dev->rd_msg_hdr) {
1098 dev->rd_msg_hdr = mei_mecbrw_read(dev);
1099 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1100 (*slots)--;
1101 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1102 }
1103 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
1104 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
1105
1106 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
1107 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
1108 ret = -EBADMSG;
1109 goto end;
1110 }
1111
1112 if (mei_hdr->host_addr || mei_hdr->me_addr) {
1113 list_for_each_entry_safe(cl_pos, cl_next,
1114 &dev->file_list, link) {
1115 dev_dbg(&dev->pdev->dev,
1116 "list_for_each_entry_safe read host"
1117 " client = %d, ME client = %d\n",
1118 cl_pos->host_client_id,
1119 cl_pos->me_client_id);
1120 if (cl_pos->host_client_id == mei_hdr->host_addr &&
1121 cl_pos->me_client_id == mei_hdr->me_addr)
1122 break;
1123 }
1124
1125 if (&cl_pos->link == &dev->file_list) {
1126 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
1127 ret = -EBADMSG;
1128 goto end;
1129 }
1130 }
1131 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
1132 dev_dbg(&dev->pdev->dev,
1133 "we can't read the message slots =%08x.\n",
1134 *slots);
1135 /* we can't read the message */
1136 ret = -ERANGE;
1137 goto end;
1138 }
1139
1140 /* decide where to read the message too */
1141 if (!mei_hdr->host_addr) {
1142 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
1143 mei_irq_thread_read_bus_message(dev, mei_hdr);
1144 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
1145 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
1146 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
1147 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
1148 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
1149 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
1150 mei_hdr->length);
1151 ret = mei_irq_thread_read_amthi_message(cmpl_list,
1152 dev, mei_hdr);
1153 if (ret)
1154 goto end;
1155
1156 } else {
1157 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
1158 ret = mei_irq_thread_read_client_message(cmpl_list,
1159 dev, mei_hdr);
1160 if (ret)
1161 goto end;
1162
1163 }
1164
1165 /* reset the number of slots and header */
1166 *slots = mei_count_full_read_slots(dev);
1167 dev->rd_msg_hdr = 0;
1168
1169 if (*slots == -EOVERFLOW) {
1170 /* overflow - reset */
1171 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
1172 /* set the event since message has been read */
1173 ret = -ERANGE;
1174 goto end;
1175 }
1176end:
1177 return ret;
1178}
1179
1180
1181/**
1182 * mei_irq_thread_write_handler - bottom half write routine after
1183 * ISR to handle the write processing.
1184 *
1185 * @cmpl_list: An instance of our list structure
1186 * @dev: the device structure
1187 * @slots: slots to write.
1188 *
1189 * returns 0 on success, <0 on failure.
1190 */
1191static int mei_irq_thread_write_handler(struct mei_io_list *cmpl_list,
1192 struct mei_device *dev,
1193 s32 *slots)
1194{
1195
1196 struct mei_cl *cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001197 struct mei_cl_cb *pos = NULL, *next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +03001198 struct mei_io_list *list;
1199 int ret;
1200
Tomas Winkler726917f2012-06-25 23:46:28 +03001201 if (!mei_hbuf_is_empty(dev)) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001202 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
1203 return 0;
1204 }
Tomas Winkler726917f2012-06-25 23:46:28 +03001205 *slots = mei_hbuf_empty_slots(dev);
Tomas Winkler7d5e0e52012-06-19 09:13:36 +03001206 if (*slots <= 0)
1207 return -EMSGSIZE;
1208
Oren Weilfb7d8792011-05-15 13:43:42 +03001209 /* complete all waiting for write CB */
1210 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
1211
1212 list = &dev->write_waiting_list;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001213 list_for_each_entry_safe(pos, next,
1214 &list->mei_cb.cb_list, cb_list) {
1215 cl = (struct mei_cl *)pos->file_private;
1216 if (cl == NULL)
1217 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001218
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001219 cl->status = 0;
1220 list_del(&pos->cb_list);
1221 if (MEI_WRITING == cl->writing_state &&
1222 (pos->major_file_operations == MEI_WRITE) &&
1223 (cl != &dev->iamthif_cl)) {
1224 dev_dbg(&dev->pdev->dev,
1225 "MEI WRITE COMPLETE\n");
1226 cl->writing_state = MEI_WRITE_COMPLETE;
1227 list_add_tail(&pos->cb_list,
1228 &cmpl_list->mei_cb.cb_list);
1229 }
1230 if (cl == &dev->iamthif_cl) {
1231 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
1232 if (dev->iamthif_flow_control_pending) {
1233 ret = _mei_irq_thread_iamthif_read(
1234 dev, slots);
1235 if (ret)
1236 return ret;
1237 }
Oren Weilfb7d8792011-05-15 13:43:42 +03001238 }
1239 }
1240
1241 if (dev->stop && !dev->wd_pending) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001242 dev->wd_stopped = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001243 wake_up_interruptible(&dev->wait_stop_wd);
1244 return 0;
1245 }
1246
1247 if (dev->extra_write_index) {
1248 dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
1249 dev->extra_write_index);
1250 mei_write_message(dev,
1251 (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
1252 (unsigned char *) &dev->ext_msg_buf[1],
1253 (dev->extra_write_index - 1) * sizeof(u32));
1254 *slots -= dev->extra_write_index;
1255 dev->extra_write_index = 0;
1256 }
1257 if (dev->mei_state == MEI_ENABLED) {
1258 if (dev->wd_pending &&
1259 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
1260 if (mei_wd_send(dev))
1261 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
1262 else
1263 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
1264 return -ENODEV;
1265
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001266 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +03001267
Tomas Winklerd242a0a2012-07-04 19:24:50 +03001268 if (dev->wd_timeout)
Oren Weilfb7d8792011-05-15 13:43:42 +03001269 *slots -= (sizeof(struct mei_msg_hdr) +
1270 MEI_START_WD_DATA_SIZE + 3) / 4;
Tomas Winklerd242a0a2012-07-04 19:24:50 +03001271 else
Oren Weilfb7d8792011-05-15 13:43:42 +03001272 *slots -= (sizeof(struct mei_msg_hdr) +
1273 MEI_WD_PARAMS_SIZE + 3) / 4;
Oren Weilfb7d8792011-05-15 13:43:42 +03001274
1275 }
1276 }
1277 if (dev->stop)
Devin J. Pohlydc91e2f2012-02-27 20:32:53 +02001278 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +03001279
1280 /* complete control write list CB */
Tomas Winklerc8372092011-11-27 21:43:33 +02001281 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001282 list_for_each_entry_safe(pos, next,
Oren Weilfb7d8792011-05-15 13:43:42 +03001283 &dev->ctrl_wr_list.mei_cb.cb_list, cb_list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001284 cl = (struct mei_cl *) pos->file_private;
Tomas Winklerc8372092011-11-27 21:43:33 +02001285 if (!cl) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001286 list_del(&pos->cb_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001287 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +03001288 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001289 switch (pos->major_file_operations) {
Tomas Winklerc8372092011-11-27 21:43:33 +02001290 case MEI_CLOSE:
1291 /* send disconnect message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001292 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001293 if (ret)
1294 return ret;
1295
1296 break;
1297 case MEI_READ:
1298 /* send flow control message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001299 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001300 if (ret)
1301 return ret;
1302
1303 break;
1304 case MEI_IOCTL:
1305 /* connect message */
Natalia Ovsyanikove8cd29d2011-12-05 00:16:54 +02001306 if (mei_other_client_is_connecting(dev, cl))
Tomas Winklerc8372092011-11-27 21:43:33 +02001307 continue;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001308 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001309 if (ret)
1310 return ret;
1311
1312 break;
1313
1314 default:
1315 BUG();
1316 }
1317
Oren Weilfb7d8792011-05-15 13:43:42 +03001318 }
1319 /* complete write list CB */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001320 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
1321 list_for_each_entry_safe(pos, next,
1322 &dev->write_list.mei_cb.cb_list, cb_list) {
1323 cl = (struct mei_cl *)pos->file_private;
1324 if (cl == NULL)
1325 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001326
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001327 if (cl != &dev->iamthif_cl) {
Tomas Winklerd2041152012-06-19 09:13:34 +03001328 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001329 dev_dbg(&dev->pdev->dev,
1330 "No flow control"
1331 " credentials for client"
1332 " %d, not sending.\n",
1333 cl->host_client_id);
1334 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001335 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001336 ret = _mei_irq_thread_cmpl(dev, slots,
1337 pos,
1338 cl, cmpl_list);
1339 if (ret)
1340 return ret;
1341
1342 } else if (cl == &dev->iamthif_cl) {
1343 /* IAMTHIF IOCTL */
1344 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
Tomas Winklerd2041152012-06-19 09:13:34 +03001345 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001346 dev_dbg(&dev->pdev->dev,
1347 "No flow control"
1348 " credentials for amthi"
1349 " client %d.\n",
1350 cl->host_client_id);
1351 continue;
1352 }
1353 ret = _mei_irq_thread_cmpl_iamthif(dev,
1354 slots,
1355 pos,
1356 cl,
1357 cmpl_list);
1358 if (ret)
1359 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +03001360
1361 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001362
Oren Weilfb7d8792011-05-15 13:43:42 +03001363 }
1364 return 0;
1365}
1366
1367
1368
1369/**
1370 * mei_timer - timer function.
1371 *
1372 * @work: pointer to the work_struct structure
1373 *
1374 * NOTE: This function is called by timer interrupt work
1375 */
Oren Weila61c6532011-09-07 09:03:13 +03001376void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +03001377{
1378 unsigned long timeout;
1379 struct mei_cl *cl_pos = NULL;
1380 struct mei_cl *cl_next = NULL;
1381 struct list_head *amthi_complete_list = NULL;
1382 struct mei_cl_cb *cb_pos = NULL;
1383 struct mei_cl_cb *cb_next = NULL;
1384
1385 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +03001386 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +03001387
1388
1389 mutex_lock(&dev->device_lock);
1390 if (dev->mei_state != MEI_ENABLED) {
1391 if (dev->mei_state == MEI_INIT_CLIENTS) {
1392 if (dev->init_clients_timer) {
1393 if (--dev->init_clients_timer == 0) {
1394 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1395 dev->init_clients_state);
1396 mei_reset(dev, 1);
1397 }
1398 }
1399 }
1400 goto out;
1401 }
1402 /*** connect/disconnect timeouts ***/
1403 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1404 if (cl_pos->timer_count) {
1405 if (--cl_pos->timer_count == 0) {
1406 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1407 mei_reset(dev, 1);
1408 goto out;
1409 }
1410 }
1411 }
1412
Oren Weilfb7d8792011-05-15 13:43:42 +03001413 if (dev->iamthif_stall_timer) {
1414 if (--dev->iamthif_stall_timer == 0) {
Masanari Iida32de21f2012-01-25 23:14:56 +09001415 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +03001416 mei_reset(dev, 1);
1417 dev->iamthif_msg_buf_size = 0;
1418 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001419 dev->iamthif_canceled = false;
1420 dev->iamthif_ioctl = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001421 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1422 dev->iamthif_timer = 0;
1423
1424 if (dev->iamthif_current_cb)
1425 mei_free_cb_private(dev->iamthif_current_cb);
1426
1427 dev->iamthif_file_object = NULL;
1428 dev->iamthif_current_cb = NULL;
Tomas Winklerc95efb72011-05-25 17:28:21 +03001429 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001430 }
1431 }
1432
1433 if (dev->iamthif_timer) {
1434
1435 timeout = dev->iamthif_timer +
1436 msecs_to_jiffies(IAMTHIF_READ_TIMER);
1437
1438 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1439 dev->iamthif_timer);
1440 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1441 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1442 if (time_after(jiffies, timeout)) {
1443 /*
1444 * User didn't read the AMTHI data on time (15sec)
1445 * freeing AMTHI for other requests
1446 */
1447
1448 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1449
1450 amthi_complete_list = &dev->amthi_read_complete_list.
1451 mei_cb.cb_list;
1452
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001453 list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, cb_list) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001454
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001455 cl_pos = cb_pos->file_object->private_data;
Oren Weilfb7d8792011-05-15 13:43:42 +03001456
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001457 /* Finding the AMTHI entry. */
1458 if (cl_pos == &dev->iamthif_cl)
1459 list_del(&cb_pos->cb_list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001460 }
1461 if (dev->iamthif_current_cb)
1462 mei_free_cb_private(dev->iamthif_current_cb);
1463
1464 dev->iamthif_file_object->private_data = NULL;
1465 dev->iamthif_file_object = NULL;
1466 dev->iamthif_current_cb = NULL;
1467 dev->iamthif_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +03001468 mei_run_next_iamthif_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001469
1470 }
1471 }
1472out:
Tomas Winkler441ab502011-12-13 23:39:34 +02001473 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1474 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +03001475}
1476
1477/**
1478 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1479 * processing.
1480 *
1481 * @irq: The irq number
1482 * @dev_id: pointer to the device structure
1483 *
1484 * returns irqreturn_t
1485 *
1486 */
1487irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1488{
1489 struct mei_device *dev = (struct mei_device *) dev_id;
1490 struct mei_io_list complete_list;
1491 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1492 struct mei_cl *cl;
1493 s32 slots;
1494 int rets;
1495 bool bus_message_received;
1496
1497
1498 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1499 /* initialize our complete list */
1500 mutex_lock(&dev->device_lock);
Tomas Winkler0288c7c2011-06-06 10:44:34 +03001501 mei_io_list_init(&complete_list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001502 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001503
1504 /* Ack the interrupt here
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001505 * In case of MSI we don't go through the quick handler */
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001506 if (pci_dev_msi_enabled(dev->pdev))
1507 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1508
Oren Weilfb7d8792011-05-15 13:43:42 +03001509 dev->me_hw_state = mei_mecsr_read(dev);
1510
1511 /* check if ME wants a reset */
1512 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
1513 dev->mei_state != MEI_RESETING &&
1514 dev->mei_state != MEI_INITIALIZING) {
1515 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1516 mei_reset(dev, 1);
1517 mutex_unlock(&dev->device_lock);
1518 return IRQ_HANDLED;
1519 }
1520
1521 /* check if we need to start the dev */
1522 if ((dev->host_hw_state & H_RDY) == 0) {
1523 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1524 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1525 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1526 mei_hcsr_set(dev);
1527 dev->mei_state = MEI_INIT_CLIENTS;
1528 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1529 /* link is established
1530 * start sending messages.
1531 */
Tomas Winklerc95efb72011-05-25 17:28:21 +03001532 mei_host_start_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001533 mutex_unlock(&dev->device_lock);
1534 return IRQ_HANDLED;
1535 } else {
1536 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1537 mutex_unlock(&dev->device_lock);
1538 return IRQ_HANDLED;
1539 }
1540 }
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001541 /* check slots available for reading */
Oren Weilfb7d8792011-05-15 13:43:42 +03001542 slots = mei_count_full_read_slots(dev);
1543 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1544 slots, dev->extra_write_index);
1545 while (slots > 0 && !dev->extra_write_index) {
1546 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1547 slots, dev->extra_write_index);
1548 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1549 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1550 if (rets)
1551 goto end;
1552 }
1553 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1554end:
1555 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1556 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler726917f2012-06-25 23:46:28 +03001557 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001558
1559 bus_message_received = false;
1560 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1561 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1562 bus_message_received = true;
1563 }
1564 mutex_unlock(&dev->device_lock);
1565 if (bus_message_received) {
1566 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1567 wake_up_interruptible(&dev->wait_recvd_msg);
1568 bus_message_received = false;
1569 }
Tomas Winklerc8372092011-11-27 21:43:33 +02001570 if (list_empty(&complete_list.mei_cb.cb_list))
Oren Weilfb7d8792011-05-15 13:43:42 +03001571 return IRQ_HANDLED;
1572
1573
1574 list_for_each_entry_safe(cb_pos, cb_next,
1575 &complete_list.mei_cb.cb_list, cb_list) {
1576 cl = (struct mei_cl *)cb_pos->file_private;
1577 list_del(&cb_pos->cb_list);
1578 if (cl) {
1579 if (cl != &dev->iamthif_cl) {
1580 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1581 _mei_cmpl(cl, cb_pos);
1582 cb_pos = NULL;
1583 } else if (cl == &dev->iamthif_cl) {
1584 _mei_cmpl_iamthif(dev, cb_pos);
1585 }
1586 }
1587 }
1588 return IRQ_HANDLED;
1589}