drivers:staging:mei Fix some typos in staging/mei
[deliverable/linux.git] / drivers / staging / mei / interrupt.c
CommitLineData
fb7d879f
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
733ba91c 4 * Copyright (c) 2003-2012, Intel Corporation.
fb7d879f
OW
5 *
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"
25#include "mei.h"
26#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 {
c95efb74 97 mei_run_next_iamthif_cmd(dev);
fb7d879f
OW
98 }
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
edf1eed4 126 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
fb7d879f
OW
127 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;
479bc59d 197 unsigned char *buffer = NULL;
fb7d879f
OW
198
199 dev_dbg(&dev->pdev->dev, "start client msg\n");
c8372094 200 if (list_empty(&dev->read_list.mei_cb.cb_list))
fb7d879f
OW
201 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;
edf1eed4 208 buffer = cb_pos->response_buffer.data + cb_pos->information;
fb7d879f
OW
209
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) {
edf1eed4 247 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
fb7d879f
OW
248 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
266 if (((*slots) * sizeof(u32)) >= (sizeof(struct mei_msg_hdr)
267 + sizeof(struct hbm_flow_control))) {
268 *slots -= (sizeof(struct mei_msg_hdr) +
269 sizeof(struct hbm_flow_control) + 3) / 4;
270 if (!mei_send_flow_control(dev, &dev->iamthif_cl)) {
271 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
272 } else {
273 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
274 dev->iamthif_state = MEI_IAMTHIF_READING;
eb9af0ac 275 dev->iamthif_flow_control_pending = false;
fb7d879f
OW
276 dev->iamthif_msg_buf_index = 0;
277 dev->iamthif_msg_buf_size = 0;
278 dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
279 dev->mei_host_buffer_is_empty =
280 mei_host_buffer_is_empty(dev);
281 }
282 return 0;
283 } else {
284 return -EMSGSIZE;
285 }
286}
287
288/**
289 * _mei_irq_thread_close - processes close related operation.
290 *
291 * @dev: the device structure.
292 * @slots: free slots.
293 * @cb_pos: callback block.
294 * @cl: private data of the file object.
295 * @cmpl_list: complete list.
296 *
297 * returns 0, OK; otherwise, error.
298 */
299static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
300 struct mei_cl_cb *cb_pos,
301 struct mei_cl *cl,
302 struct mei_io_list *cmpl_list)
303{
304 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
305 sizeof(struct hbm_client_disconnect_request))) {
306 *slots -= (sizeof(struct mei_msg_hdr) +
307 sizeof(struct hbm_client_disconnect_request) + 3) / 4;
308
309 if (!mei_disconnect(dev, cl)) {
310 cl->status = 0;
311 cb_pos->information = 0;
312 list_move_tail(&cb_pos->cb_list,
313 &cmpl_list->mei_cb.cb_list);
314 return -EMSGSIZE;
315 } else {
316 cl->state = MEI_FILE_DISCONNECTING;
317 cl->status = 0;
318 cb_pos->information = 0;
319 list_move_tail(&cb_pos->cb_list,
320 &dev->ctrl_rd_list.mei_cb.cb_list);
321 cl->timer_count = MEI_CONNECT_TIMEOUT;
322 }
323 } else {
324 /* return the cancel routine */
325 return -EBADMSG;
326 }
327
328 return 0;
329}
330
331/**
332 * is_treat_specially_client - checks if the message belongs
333 * to the file private data.
334 *
335 * @cl: private data of the file object
336 * @rs: connect response bus message
337 *
338 */
339static bool is_treat_specially_client(struct mei_cl *cl,
340 struct hbm_client_connect_response *rs)
341{
342
343 if (cl->host_client_id == rs->host_addr &&
344 cl->me_client_id == rs->me_addr) {
345 if (!rs->status) {
346 cl->state = MEI_FILE_CONNECTED;
347 cl->status = 0;
348
349 } else {
350 cl->state = MEI_FILE_DISCONNECTED;
351 cl->status = -ENODEV;
352 }
353 cl->timer_count = 0;
354
355 return true;
356 }
357 return false;
358}
359
360/**
361 * mei_client_connect_response - connects to response irq routine
362 *
363 * @dev: the device structure
364 * @rs: connect response bus message
365 */
366static void mei_client_connect_response(struct mei_device *dev,
367 struct hbm_client_connect_response *rs)
368{
369
370 struct mei_cl *cl;
371 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
372
373 dev_dbg(&dev->pdev->dev,
374 "connect_response:\n"
375 "ME Client = %d\n"
376 "Host Client = %d\n"
377 "Status = %d\n",
378 rs->me_addr,
379 rs->host_addr,
380 rs->status);
381
382 /* if WD or iamthif client treat specially */
383
384 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
fb7d879f 385 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
70cd5337 386 mei_watchdog_register(dev);
9ce178e5 387
70cd5337 388 /* next step in the state maching */
c95efb74 389 mei_host_init_iamthif(dev);
fb7d879f
OW
390 return;
391 }
392
393 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
394 dev->iamthif_state = MEI_IAMTHIF_IDLE;
395 return;
396 }
b7cd2d9f
TW
397 list_for_each_entry_safe(cb_pos, cb_next,
398 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
399
400 cl = (struct mei_cl *)cb_pos->file_private;
401 if (!cl) {
402 list_del(&cb_pos->cb_list);
403 return;
404 }
405 if (MEI_IOCTL == cb_pos->major_file_operations) {
406 if (is_treat_specially_client(cl, rs)) {
fb7d879f 407 list_del(&cb_pos->cb_list);
b7cd2d9f
TW
408 cl->status = 0;
409 cl->timer_count = 0;
410 break;
fb7d879f
OW
411 }
412 }
413 }
414}
415
416/**
417 * mei_client_disconnect_response - disconnects from response irq routine
418 *
419 * @dev: the device structure
420 * @rs: disconnect response bus message
421 */
422static void mei_client_disconnect_response(struct mei_device *dev,
423 struct hbm_client_connect_response *rs)
424{
425 struct mei_cl *cl;
426 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
427
428 dev_dbg(&dev->pdev->dev,
429 "disconnect_response:\n"
430 "ME Client = %d\n"
431 "Host Client = %d\n"
432 "Status = %d\n",
433 rs->me_addr,
434 rs->host_addr,
435 rs->status);
436
b7cd2d9f
TW
437 list_for_each_entry_safe(cb_pos, cb_next,
438 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
439 cl = (struct mei_cl *)cb_pos->file_private;
fb7d879f 440
b7cd2d9f
TW
441 if (!cl) {
442 list_del(&cb_pos->cb_list);
443 return;
444 }
fb7d879f 445
b7cd2d9f
TW
446 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
447 if (cl->host_client_id == rs->host_addr &&
448 cl->me_client_id == rs->me_addr) {
fb7d879f 449
b7cd2d9f
TW
450 list_del(&cb_pos->cb_list);
451 if (!rs->status)
452 cl->state = MEI_FILE_DISCONNECTED;
fb7d879f 453
b7cd2d9f
TW
454 cl->status = 0;
455 cl->timer_count = 0;
456 break;
fb7d879f
OW
457 }
458 }
459}
460
461/**
462 * same_flow_addr - tells if they have the same address.
463 *
464 * @file: private data of the file object.
465 * @flow: flow control.
466 *
467 * returns !=0, same; 0,not.
468 */
469static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
470{
471 return (cl->host_client_id == flow->host_addr &&
472 cl->me_client_id == flow->me_addr);
473}
474
475/**
476 * add_single_flow_creds - adds single buffer credentials.
477 *
478 * @file: private data ot the file object.
479 * @flow: flow control.
480 */
481static void add_single_flow_creds(struct mei_device *dev,
482 struct hbm_flow_control *flow)
483{
484 struct mei_me_client *client;
485 int i;
486
cf9673da 487 for (i = 0; i < dev->me_clients_num; i++) {
fb7d879f
OW
488 client = &dev->me_clients[i];
489 if (client && flow->me_addr == client->client_id) {
490 if (client->props.single_recv_buf) {
491 client->mei_flow_ctrl_creds++;
492 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
493 flow->me_addr);
494 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
495 client->mei_flow_ctrl_creds);
496 } else {
497 BUG(); /* error in flow control */
498 }
499 }
500 }
501}
502
503/**
504 * mei_client_flow_control_response - flow control response irq routine
505 *
506 * @dev: the device structure
507 * @flow_control: flow control response bus message
508 */
509static void mei_client_flow_control_response(struct mei_device *dev,
510 struct hbm_flow_control *flow_control)
511{
512 struct mei_cl *cl_pos = NULL;
513 struct mei_cl *cl_next = NULL;
514
515 if (!flow_control->host_addr) {
516 /* single receive buffer */
517 add_single_flow_creds(dev, flow_control);
518 } else {
519 /* normal connection */
520 list_for_each_entry_safe(cl_pos, cl_next,
521 &dev->file_list, link) {
522 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
523
524 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
525 cl_pos->host_client_id,
526 cl_pos->me_client_id);
527 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
528 flow_control->host_addr,
529 flow_control->me_addr);
530 if (same_flow_addr(cl_pos, flow_control)) {
531 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
532 flow_control->host_addr,
533 flow_control->me_addr);
534 cl_pos->mei_flow_ctrl_creds++;
535 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
536 cl_pos->mei_flow_ctrl_creds);
537 break;
538 }
539 }
540 }
541}
542
543/**
544 * same_disconn_addr - tells if they have the same address
545 *
546 * @file: private data of the file object.
547 * @disconn: disconnection request.
548 *
549 * returns !=0, same; 0,not.
550 */
551static int same_disconn_addr(struct mei_cl *cl,
552 struct hbm_client_disconnect_request *disconn)
553{
554 return (cl->host_client_id == disconn->host_addr &&
555 cl->me_client_id == disconn->me_addr);
556}
557
558/**
559 * mei_client_disconnect_request - disconnects from request irq routine
560 *
561 * @dev: the device structure.
562 * @disconnect_req: disconnect request bus message.
563 */
564static void mei_client_disconnect_request(struct mei_device *dev,
565 struct hbm_client_disconnect_request *disconnect_req)
566{
567 struct mei_msg_hdr *mei_hdr;
568 struct hbm_client_connect_response *disconnect_res;
569 struct mei_cl *cl_pos = NULL;
570 struct mei_cl *cl_next = NULL;
571
572 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
573 if (same_disconn_addr(cl_pos, disconnect_req)) {
574 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
575 disconnect_req->host_addr,
576 disconnect_req->me_addr);
577 cl_pos->state = MEI_FILE_DISCONNECTED;
578 cl_pos->timer_count = 0;
579 if (cl_pos == &dev->wd_cl) {
580 dev->wd_due_counter = 0;
eb9af0ac 581 dev->wd_pending = false;
fb7d879f
OW
582 } else if (cl_pos == &dev->iamthif_cl)
583 dev->iamthif_timer = 0;
584
585 /* prepare disconnect response */
586 mei_hdr =
587 (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
588 mei_hdr->host_addr = 0;
589 mei_hdr->me_addr = 0;
590 mei_hdr->length =
591 sizeof(struct hbm_client_connect_response);
592 mei_hdr->msg_complete = 1;
593 mei_hdr->reserved = 0;
594
595 disconnect_res =
596 (struct hbm_client_connect_response *)
597 &dev->ext_msg_buf[1];
598 disconnect_res->host_addr = cl_pos->host_client_id;
599 disconnect_res->me_addr = cl_pos->me_client_id;
1ca7e782 600 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
fb7d879f
OW
601 disconnect_res->status = 0;
602 dev->extra_write_index = 2;
603 break;
604 }
605 }
606}
607
608
609/**
610 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
611 * handle the read bus message cmd processing.
612 *
613 * @dev: the device structure
614 * @mei_hdr: header of bus message
615 */
616static void mei_irq_thread_read_bus_message(struct mei_device *dev,
617 struct mei_msg_hdr *mei_hdr)
618{
619 struct mei_bus_message *mei_msg;
620 struct hbm_host_version_response *version_res;
621 struct hbm_client_connect_response *connect_res;
622 struct hbm_client_connect_response *disconnect_res;
623 struct hbm_flow_control *flow_control;
624 struct hbm_props_response *props_res;
625 struct hbm_host_enum_response *enum_res;
626 struct hbm_client_disconnect_request *disconnect_req;
627 struct hbm_host_stop_request *host_stop_req;
abc51b6d 628 int res;
fb7d879f 629
fb7d879f
OW
630
631 /* read the message to our buffer */
fb7d879f 632 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
edf1eed4
TW
633 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
634 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
fb7d879f 635
1ca7e782 636 switch (mei_msg->hbm_cmd) {
fb7d879f
OW
637 case HOST_START_RES_CMD:
638 version_res = (struct hbm_host_version_response *) mei_msg;
639 if (version_res->host_version_supported) {
640 dev->version.major_version = HBM_MAJOR_VERSION;
641 dev->version.minor_version = HBM_MINOR_VERSION;
642 if (dev->mei_state == MEI_INIT_CLIENTS &&
643 dev->init_clients_state == MEI_START_MESSAGE) {
644 dev->init_clients_timer = 0;
c95efb74 645 mei_host_enum_clients_message(dev);
fb7d879f 646 } else {
eb9af0ac 647 dev->recvd_msg = false;
fb7d879f
OW
648 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
649 mei_reset(dev, 1);
650 return;
651 }
652 } else {
653 dev->version = version_res->me_max_version;
654 /* send stop message */
97d5cb09 655 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
fb7d879f
OW
656 mei_hdr->host_addr = 0;
657 mei_hdr->me_addr = 0;
658 mei_hdr->length = sizeof(struct hbm_host_stop_request);
659 mei_hdr->msg_complete = 1;
660 mei_hdr->reserved = 0;
661
662 host_stop_req = (struct hbm_host_stop_request *)
663 &dev->wr_msg_buf[1];
664
665 memset(host_stop_req,
666 0,
667 sizeof(struct hbm_host_stop_request));
1ca7e782 668 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
fb7d879f
OW
669 host_stop_req->reason = DRIVER_STOP_REQUEST;
670 mei_write_message(dev, mei_hdr,
671 (unsigned char *) (host_stop_req),
672 mei_hdr->length);
673 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
674 return;
675 }
676
eb9af0ac 677 dev->recvd_msg = true;
fb7d879f
OW
678 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
679 break;
680
681 case CLIENT_CONNECT_RES_CMD:
682 connect_res =
683 (struct hbm_client_connect_response *) mei_msg;
684 mei_client_connect_response(dev, connect_res);
685 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
686 wake_up(&dev->wait_recvd_msg);
687 break;
688
689 case CLIENT_DISCONNECT_RES_CMD:
690 disconnect_res =
691 (struct hbm_client_connect_response *) mei_msg;
441ab50f 692 mei_client_disconnect_response(dev, disconnect_res);
fb7d879f
OW
693 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
694 wake_up(&dev->wait_recvd_msg);
695 break;
696
697 case MEI_FLOW_CONTROL_CMD:
698 flow_control = (struct hbm_flow_control *) mei_msg;
699 mei_client_flow_control_response(dev, flow_control);
700 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
701 break;
702
703 case HOST_CLIENT_PROPERTIES_RES_CMD:
704 props_res = (struct hbm_props_response *)mei_msg;
705 if (props_res->status || !dev->me_clients) {
706 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
707 mei_reset(dev, 1);
708 return;
709 }
441ab50f 710 if (dev->me_clients[dev->me_client_presentation_num]
fb7d879f
OW
711 .client_id == props_res->address) {
712
713 dev->me_clients[dev->me_client_presentation_num].props
714 = props_res->client_properties;
715
716 if (dev->mei_state == MEI_INIT_CLIENTS &&
717 dev->init_clients_state ==
718 MEI_CLIENT_PROPERTIES_MESSAGE) {
719 dev->me_client_index++;
720 dev->me_client_presentation_num++;
abc51b6d 721
5f9092f3 722 /** Send Client Properties request **/
abc51b6d
OW
723 res = mei_host_client_properties(dev);
724 if (res < 0) {
725 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
726 return;
727 } else if (!res) {
728 /*
729 * No more clients to send to.
730 * Clear Map for indicating now ME clients
731 * with associated host client
732 */
733 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
734 dev->open_handle_count = 0;
735
736 /*
737 * Reserving the first three client IDs
738 * Client Id 0 - Reserved for MEI Bus Message communications
739 * Client Id 1 - Reserved for Watchdog
740 * Client ID 2 - Reserved for AMTHI
741 */
742 bitmap_set(dev->host_clients_map, 0, 3);
743 dev->mei_state = MEI_ENABLED;
744
745 /* if wd initialization fails, initialization the AMTHI client,
746 * otherwise the AMTHI client will be initialized after the WD client connect response
747 * will be received
748 */
749 if (mei_wd_host_init(dev))
750 mei_host_init_iamthif(dev);
751 }
752
fb7d879f
OW
753 } else {
754 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
755 mei_reset(dev, 1);
756 return;
757 }
758 } else {
759 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
760 mei_reset(dev, 1);
761 return;
762 }
763 break;
764
765 case HOST_ENUM_RES_CMD:
766 enum_res = (struct hbm_host_enum_response *) mei_msg;
767 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
768 if (dev->mei_state == MEI_INIT_CLIENTS &&
769 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
770 dev->init_clients_timer = 0;
771 dev->me_client_presentation_num = 0;
772 dev->me_client_index = 0;
c95efb74 773 mei_allocate_me_clients_storage(dev);
fb7d879f
OW
774 dev->init_clients_state =
775 MEI_CLIENT_PROPERTIES_MESSAGE;
c95efb74 776 mei_host_client_properties(dev);
fb7d879f
OW
777 } else {
778 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
779 mei_reset(dev, 1);
780 return;
781 }
782 break;
783
784 case HOST_STOP_RES_CMD:
785 dev->mei_state = MEI_DISABLED;
786 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
787 mei_reset(dev, 1);
788 break;
789
790 case CLIENT_DISCONNECT_REQ_CMD:
791 /* search for client */
792 disconnect_req =
793 (struct hbm_client_disconnect_request *) mei_msg;
794 mei_client_disconnect_request(dev, disconnect_req);
795 break;
796
797 case ME_STOP_REQ_CMD:
798 /* prepare stop request */
799 mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
800 mei_hdr->host_addr = 0;
801 mei_hdr->me_addr = 0;
802 mei_hdr->length = sizeof(struct hbm_host_stop_request);
803 mei_hdr->msg_complete = 1;
804 mei_hdr->reserved = 0;
805 host_stop_req =
806 (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
807 memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
1ca7e782 808 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
fb7d879f
OW
809 host_stop_req->reason = DRIVER_STOP_REQUEST;
810 host_stop_req->reserved[0] = 0;
811 host_stop_req->reserved[1] = 0;
812 dev->extra_write_index = 2;
813 break;
814
815 default:
816 BUG();
817 break;
818
819 }
820}
821
822
823/**
824 * _mei_hb_read - processes read related operation.
825 *
826 * @dev: the device structure.
827 * @slots: free slots.
828 * @cb_pos: callback block.
829 * @cl: private data of the file object.
830 * @cmpl_list: complete list.
831 *
832 * returns 0, OK; otherwise, error.
833 */
834static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
835 struct mei_cl_cb *cb_pos,
836 struct mei_cl *cl,
837 struct mei_io_list *cmpl_list)
838{
839 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
840 sizeof(struct hbm_flow_control))) {
841 *slots -= (sizeof(struct mei_msg_hdr) +
842 sizeof(struct hbm_flow_control) + 3) / 4;
843 if (!mei_send_flow_control(dev, cl)) {
844 cl->status = -ENODEV;
845 cb_pos->information = 0;
846 list_move_tail(&cb_pos->cb_list,
847 &cmpl_list->mei_cb.cb_list);
848 return -ENODEV;
849 } else {
850 list_move_tail(&cb_pos->cb_list,
851 &dev->read_list.mei_cb.cb_list);
852 }
853 } else {
854 /* return the cancel routine */
855 list_del(&cb_pos->cb_list);
856 return -EBADMSG;
857 }
858
859 return 0;
860}
861
862
863/**
864 * _mei_irq_thread_ioctl - processes ioctl related operation.
865 *
866 * @dev: the device structure.
867 * @slots: free slots.
868 * @cb_pos: callback block.
869 * @cl: private data of the file object.
870 * @cmpl_list: complete list.
871 *
872 * returns 0, OK; otherwise, error.
873 */
874static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
875 struct mei_cl_cb *cb_pos,
876 struct mei_cl *cl,
877 struct mei_io_list *cmpl_list)
878{
879 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
880 sizeof(struct hbm_client_connect_request))) {
881 cl->state = MEI_FILE_CONNECTING;
882 *slots -= (sizeof(struct mei_msg_hdr) +
883 sizeof(struct hbm_client_connect_request) + 3) / 4;
884 if (!mei_connect(dev, cl)) {
885 cl->status = -ENODEV;
886 cb_pos->information = 0;
887 list_del(&cb_pos->cb_list);
888 return -ENODEV;
889 } else {
890 list_move_tail(&cb_pos->cb_list,
891 &dev->ctrl_rd_list.mei_cb.cb_list);
892 cl->timer_count = MEI_CONNECT_TIMEOUT;
893 }
894 } else {
895 /* return the cancel routine */
896 list_del(&cb_pos->cb_list);
897 return -EBADMSG;
898 }
899
900 return 0;
901}
902
903/**
904 * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
905 *
906 * @dev: the device structure.
907 * @slots: free slots.
908 * @cb_pos: callback block.
909 * @cl: private data of the file object.
910 * @cmpl_list: complete list.
911 *
912 * returns 0, OK; otherwise, error.
913 */
914static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
915 struct mei_cl_cb *cb_pos,
916 struct mei_cl *cl,
917 struct mei_io_list *cmpl_list)
918{
919 struct mei_msg_hdr *mei_hdr;
920
921 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
922 (cb_pos->request_buffer.size -
923 cb_pos->information))) {
924 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
925 mei_hdr->host_addr = cl->host_client_id;
926 mei_hdr->me_addr = cl->me_client_id;
927 mei_hdr->length = cb_pos->request_buffer.size -
928 cb_pos->information;
929 mei_hdr->msg_complete = 1;
930 mei_hdr->reserved = 0;
931 dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
932 "mei_hdr->msg_complete = %d\n",
933 cb_pos->request_buffer.size,
934 mei_hdr->msg_complete);
935 dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
936 cb_pos->information);
937 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
938 mei_hdr->length);
939 *slots -= (sizeof(struct mei_msg_hdr) +
940 mei_hdr->length + 3) / 4;
941 if (!mei_write_message(dev, mei_hdr,
942 (unsigned char *)
943 (cb_pos->request_buffer.data +
944 cb_pos->information),
945 mei_hdr->length)) {
946 cl->status = -ENODEV;
947 list_move_tail(&cb_pos->cb_list,
948 &cmpl_list->mei_cb.cb_list);
949 return -ENODEV;
950 } else {
951 if (mei_flow_ctrl_reduce(dev, cl))
952 return -ENODEV;
953 cl->status = 0;
954 cb_pos->information += mei_hdr->length;
955 list_move_tail(&cb_pos->cb_list,
956 &dev->write_waiting_list.mei_cb.cb_list);
957 }
958 } else if (*slots == ((dev->host_hw_state & H_CBD) >> 24)) {
959 /* buffer is still empty */
960 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
961 mei_hdr->host_addr = cl->host_client_id;
962 mei_hdr->me_addr = cl->me_client_id;
963 mei_hdr->length =
964 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
965 mei_hdr->msg_complete = 0;
966 mei_hdr->reserved = 0;
967
968 (*slots) -= (sizeof(struct mei_msg_hdr) +
969 mei_hdr->length + 3) / 4;
970 if (!mei_write_message(dev, mei_hdr,
971 (unsigned char *)
972 (cb_pos->request_buffer.data +
973 cb_pos->information),
974 mei_hdr->length)) {
975 cl->status = -ENODEV;
976 list_move_tail(&cb_pos->cb_list,
977 &cmpl_list->mei_cb.cb_list);
978 return -ENODEV;
979 } else {
980 cb_pos->information += mei_hdr->length;
981 dev_dbg(&dev->pdev->dev,
982 "cb_pos->request_buffer.size =%d"
983 " mei_hdr->msg_complete = %d\n",
984 cb_pos->request_buffer.size,
985 mei_hdr->msg_complete);
986 dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
987 cb_pos->information);
988 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
989 mei_hdr->length);
990 }
991 return -EMSGSIZE;
992 } else {
993 return -EBADMSG;
994 }
995
996 return 0;
997}
998
999/**
1000 * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
1001 *
1002 * @dev: the device structure.
1003 * @slots: free slots.
1004 * @cb_pos: callback block.
1005 * @cl: private data of the file object.
1006 * @cmpl_list: complete list.
1007 *
1008 * returns 0, OK; otherwise, error.
1009 */
1010static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
1011 struct mei_cl_cb *cb_pos,
1012 struct mei_cl *cl,
1013 struct mei_io_list *cmpl_list)
1014{
1015 struct mei_msg_hdr *mei_hdr;
1016
1017 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
1018 dev->iamthif_msg_buf_size -
1019 dev->iamthif_msg_buf_index)) {
1020 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1021 mei_hdr->host_addr = cl->host_client_id;
1022 mei_hdr->me_addr = cl->me_client_id;
1023 mei_hdr->length = dev->iamthif_msg_buf_size -
1024 dev->iamthif_msg_buf_index;
1025 mei_hdr->msg_complete = 1;
1026 mei_hdr->reserved = 0;
1027
1028 *slots -= (sizeof(struct mei_msg_hdr) +
1029 mei_hdr->length + 3) / 4;
1030
1031 if (!mei_write_message(dev, mei_hdr,
1032 (dev->iamthif_msg_buf +
1033 dev->iamthif_msg_buf_index),
1034 mei_hdr->length)) {
1035 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1036 cl->status = -ENODEV;
1037 list_del(&cb_pos->cb_list);
1038 return -ENODEV;
1039 } else {
1040 if (mei_flow_ctrl_reduce(dev, cl))
1041 return -ENODEV;
1042 dev->iamthif_msg_buf_index += mei_hdr->length;
1043 cb_pos->information = dev->iamthif_msg_buf_index;
1044 cl->status = 0;
1045 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
eb9af0ac 1046 dev->iamthif_flow_control_pending = true;
fb7d879f
OW
1047 /* save iamthif cb sent to amthi client */
1048 dev->iamthif_current_cb = cb_pos;
1049 list_move_tail(&cb_pos->cb_list,
1050 &dev->write_waiting_list.mei_cb.cb_list);
1051
1052 }
1053 } else if (*slots == ((dev->host_hw_state & H_CBD) >> 24)) {
1054 /* buffer is still empty */
1055 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1056 mei_hdr->host_addr = cl->host_client_id;
1057 mei_hdr->me_addr = cl->me_client_id;
1058 mei_hdr->length =
1059 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1060 mei_hdr->msg_complete = 0;
1061 mei_hdr->reserved = 0;
1062
1063 *slots -= (sizeof(struct mei_msg_hdr) +
1064 mei_hdr->length + 3) / 4;
1065
1066 if (!mei_write_message(dev, mei_hdr,
1067 (dev->iamthif_msg_buf +
1068 dev->iamthif_msg_buf_index),
1069 mei_hdr->length)) {
1070 cl->status = -ENODEV;
1071 list_del(&cb_pos->cb_list);
1072 } else {
1073 dev->iamthif_msg_buf_index += mei_hdr->length;
1074 }
1075 return -EMSGSIZE;
1076 } else {
1077 return -EBADMSG;
1078 }
1079
1080 return 0;
1081}
1082
1083/**
1084 * mei_irq_thread_read_handler - bottom half read routine after ISR to
1085 * handle the read processing.
1086 *
1087 * @cmpl_list: An instance of our list structure
1088 * @dev: the device structure
1089 * @slots: slots to read.
1090 *
1091 * returns 0 on success, <0 on failure.
1092 */
1093static int mei_irq_thread_read_handler(struct mei_io_list *cmpl_list,
1094 struct mei_device *dev,
1095 s32 *slots)
1096{
1097 struct mei_msg_hdr *mei_hdr;
1098 struct mei_cl *cl_pos = NULL;
1099 struct mei_cl *cl_next = NULL;
1100 int ret = 0;
1101
1102 if (!dev->rd_msg_hdr) {
1103 dev->rd_msg_hdr = mei_mecbrw_read(dev);
1104 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1105 (*slots)--;
1106 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1107 }
1108 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
1109 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
1110
1111 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
1112 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
1113 ret = -EBADMSG;
1114 goto end;
1115 }
1116
1117 if (mei_hdr->host_addr || mei_hdr->me_addr) {
1118 list_for_each_entry_safe(cl_pos, cl_next,
1119 &dev->file_list, link) {
1120 dev_dbg(&dev->pdev->dev,
1121 "list_for_each_entry_safe read host"
1122 " client = %d, ME client = %d\n",
1123 cl_pos->host_client_id,
1124 cl_pos->me_client_id);
1125 if (cl_pos->host_client_id == mei_hdr->host_addr &&
1126 cl_pos->me_client_id == mei_hdr->me_addr)
1127 break;
1128 }
1129
1130 if (&cl_pos->link == &dev->file_list) {
1131 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
1132 ret = -EBADMSG;
1133 goto end;
1134 }
1135 }
1136 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
1137 dev_dbg(&dev->pdev->dev,
1138 "we can't read the message slots =%08x.\n",
1139 *slots);
1140 /* we can't read the message */
1141 ret = -ERANGE;
1142 goto end;
1143 }
1144
1145 /* decide where to read the message too */
1146 if (!mei_hdr->host_addr) {
1147 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
1148 mei_irq_thread_read_bus_message(dev, mei_hdr);
1149 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
1150 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
1151 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
1152 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
1153 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
1154 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
1155 mei_hdr->length);
1156 ret = mei_irq_thread_read_amthi_message(cmpl_list,
1157 dev, mei_hdr);
1158 if (ret)
1159 goto end;
1160
1161 } else {
1162 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
1163 ret = mei_irq_thread_read_client_message(cmpl_list,
1164 dev, mei_hdr);
1165 if (ret)
1166 goto end;
1167
1168 }
1169
1170 /* reset the number of slots and header */
1171 *slots = mei_count_full_read_slots(dev);
1172 dev->rd_msg_hdr = 0;
1173
1174 if (*slots == -EOVERFLOW) {
1175 /* overflow - reset */
1176 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
1177 /* set the event since message has been read */
1178 ret = -ERANGE;
1179 goto end;
1180 }
1181end:
1182 return ret;
1183}
1184
1185
1186/**
1187 * mei_irq_thread_write_handler - bottom half write routine after
1188 * ISR to handle the write processing.
1189 *
1190 * @cmpl_list: An instance of our list structure
1191 * @dev: the device structure
1192 * @slots: slots to write.
1193 *
1194 * returns 0 on success, <0 on failure.
1195 */
1196static int mei_irq_thread_write_handler(struct mei_io_list *cmpl_list,
1197 struct mei_device *dev,
1198 s32 *slots)
1199{
1200
1201 struct mei_cl *cl;
b7cd2d9f 1202 struct mei_cl_cb *pos = NULL, *next = NULL;
fb7d879f
OW
1203 struct mei_io_list *list;
1204 int ret;
1205
1206 if (!mei_host_buffer_is_empty(dev)) {
1207 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
1208 return 0;
1209 }
fb7d879f
OW
1210 *slots = mei_count_empty_write_slots(dev);
1211 /* complete all waiting for write CB */
1212 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
1213
1214 list = &dev->write_waiting_list;
b7cd2d9f
TW
1215 list_for_each_entry_safe(pos, next,
1216 &list->mei_cb.cb_list, cb_list) {
1217 cl = (struct mei_cl *)pos->file_private;
1218 if (cl == NULL)
1219 continue;
1220
1221 cl->status = 0;
1222 list_del(&pos->cb_list);
1223 if (MEI_WRITING == cl->writing_state &&
1224 (pos->major_file_operations == MEI_WRITE) &&
1225 (cl != &dev->iamthif_cl)) {
1226 dev_dbg(&dev->pdev->dev,
1227 "MEI WRITE COMPLETE\n");
1228 cl->writing_state = MEI_WRITE_COMPLETE;
1229 list_add_tail(&pos->cb_list,
1230 &cmpl_list->mei_cb.cb_list);
1231 }
1232 if (cl == &dev->iamthif_cl) {
1233 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
1234 if (dev->iamthif_flow_control_pending) {
1235 ret = _mei_irq_thread_iamthif_read(
1236 dev, slots);
1237 if (ret)
1238 return ret;
fb7d879f 1239 }
fb7d879f
OW
1240 }
1241 }
1242
1243 if (dev->stop && !dev->wd_pending) {
eb9af0ac 1244 dev->wd_stopped = true;
fb7d879f
OW
1245 wake_up_interruptible(&dev->wait_stop_wd);
1246 return 0;
1247 }
1248
1249 if (dev->extra_write_index) {
1250 dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
1251 dev->extra_write_index);
1252 mei_write_message(dev,
1253 (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
1254 (unsigned char *) &dev->ext_msg_buf[1],
1255 (dev->extra_write_index - 1) * sizeof(u32));
1256 *slots -= dev->extra_write_index;
1257 dev->extra_write_index = 0;
1258 }
1259 if (dev->mei_state == MEI_ENABLED) {
1260 if (dev->wd_pending &&
1261 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
1262 if (mei_wd_send(dev))
1263 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
1264 else
1265 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
1266 return -ENODEV;
1267
eb9af0ac 1268 dev->wd_pending = false;
fb7d879f
OW
1269
1270 if (dev->wd_timeout) {
1271 *slots -= (sizeof(struct mei_msg_hdr) +
1272 MEI_START_WD_DATA_SIZE + 3) / 4;
1273 dev->wd_due_counter = 2;
1274 } else {
1275 *slots -= (sizeof(struct mei_msg_hdr) +
1276 MEI_WD_PARAMS_SIZE + 3) / 4;
1277 dev->wd_due_counter = 0;
1278 }
1279
1280 }
1281 }
1282 if (dev->stop)
dc91e2f1 1283 return -ENODEV;
fb7d879f
OW
1284
1285 /* complete control write list CB */
c8372094 1286 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
b7cd2d9f 1287 list_for_each_entry_safe(pos, next,
fb7d879f 1288 &dev->ctrl_wr_list.mei_cb.cb_list, cb_list) {
b7cd2d9f 1289 cl = (struct mei_cl *) pos->file_private;
c8372094 1290 if (!cl) {
b7cd2d9f 1291 list_del(&pos->cb_list);
c8372094
TW
1292 return -ENODEV;
1293 }
b7cd2d9f 1294 switch (pos->major_file_operations) {
c8372094
TW
1295 case MEI_CLOSE:
1296 /* send disconnect message */
b7cd2d9f 1297 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
c8372094
TW
1298 if (ret)
1299 return ret;
fb7d879f 1300
c8372094
TW
1301 break;
1302 case MEI_READ:
1303 /* send flow control message */
b7cd2d9f 1304 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
c8372094
TW
1305 if (ret)
1306 return ret;
fb7d879f 1307
c8372094
TW
1308 break;
1309 case MEI_IOCTL:
1310 /* connect message */
e8cd29d8 1311 if (mei_other_client_is_connecting(dev, cl))
c8372094 1312 continue;
b7cd2d9f 1313 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
c8372094
TW
1314 if (ret)
1315 return ret;
fb7d879f 1316
c8372094 1317 break;
fb7d879f 1318
c8372094
TW
1319 default:
1320 BUG();
fb7d879f 1321 }
c8372094 1322
fb7d879f
OW
1323 }
1324 /* complete write list CB */
b7cd2d9f
TW
1325 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
1326 list_for_each_entry_safe(pos, next,
1327 &dev->write_list.mei_cb.cb_list, cb_list) {
1328 cl = (struct mei_cl *)pos->file_private;
1329 if (cl == NULL)
1330 continue;
1331
1332 if (cl != &dev->iamthif_cl) {
1333 if (!mei_flow_ctrl_creds(dev, cl)) {
1334 dev_dbg(&dev->pdev->dev,
1335 "No flow control"
1336 " credentials for client"
1337 " %d, not sending.\n",
1338 cl->host_client_id);
1339 continue;
1340 }
1341 ret = _mei_irq_thread_cmpl(dev, slots,
1342 pos,
1343 cl, cmpl_list);
1344 if (ret)
1345 return ret;
fb7d879f 1346
b7cd2d9f
TW
1347 } else if (cl == &dev->iamthif_cl) {
1348 /* IAMTHIF IOCTL */
1349 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
1350 if (!mei_flow_ctrl_creds(dev, cl)) {
1351 dev_dbg(&dev->pdev->dev,
1352 "No flow control"
1353 " credentials for amthi"
1354 " client %d.\n",
1355 cl->host_client_id);
1356 continue;
fb7d879f 1357 }
b7cd2d9f
TW
1358 ret = _mei_irq_thread_cmpl_iamthif(dev,
1359 slots,
1360 pos,
1361 cl,
1362 cmpl_list);
1363 if (ret)
1364 return ret;
fb7d879f
OW
1365
1366 }
b7cd2d9f 1367
fb7d879f
OW
1368 }
1369 return 0;
1370}
1371
1372
1373
1374/**
1375 * mei_timer - timer function.
1376 *
1377 * @work: pointer to the work_struct structure
1378 *
1379 * NOTE: This function is called by timer interrupt work
1380 */
a61c6530 1381void mei_timer(struct work_struct *work)
fb7d879f
OW
1382{
1383 unsigned long timeout;
1384 struct mei_cl *cl_pos = NULL;
1385 struct mei_cl *cl_next = NULL;
1386 struct list_head *amthi_complete_list = NULL;
1387 struct mei_cl_cb *cb_pos = NULL;
1388 struct mei_cl_cb *cb_next = NULL;
1389
1390 struct mei_device *dev = container_of(work,
a61c6530 1391 struct mei_device, timer_work.work);
fb7d879f
OW
1392
1393
1394 mutex_lock(&dev->device_lock);
1395 if (dev->mei_state != MEI_ENABLED) {
1396 if (dev->mei_state == MEI_INIT_CLIENTS) {
1397 if (dev->init_clients_timer) {
1398 if (--dev->init_clients_timer == 0) {
1399 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1400 dev->init_clients_state);
1401 mei_reset(dev, 1);
1402 }
1403 }
1404 }
1405 goto out;
1406 }
1407 /*** connect/disconnect timeouts ***/
1408 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1409 if (cl_pos->timer_count) {
1410 if (--cl_pos->timer_count == 0) {
1411 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1412 mei_reset(dev, 1);
1413 goto out;
1414 }
1415 }
1416 }
1417
fb7d879f
OW
1418 if (dev->iamthif_stall_timer) {
1419 if (--dev->iamthif_stall_timer == 0) {
32de21f7 1420 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
fb7d879f
OW
1421 mei_reset(dev, 1);
1422 dev->iamthif_msg_buf_size = 0;
1423 dev->iamthif_msg_buf_index = 0;
eb9af0ac
TW
1424 dev->iamthif_canceled = false;
1425 dev->iamthif_ioctl = true;
fb7d879f
OW
1426 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1427 dev->iamthif_timer = 0;
1428
1429 if (dev->iamthif_current_cb)
1430 mei_free_cb_private(dev->iamthif_current_cb);
1431
1432 dev->iamthif_file_object = NULL;
1433 dev->iamthif_current_cb = NULL;
c95efb74 1434 mei_run_next_iamthif_cmd(dev);
fb7d879f
OW
1435 }
1436 }
1437
1438 if (dev->iamthif_timer) {
1439
1440 timeout = dev->iamthif_timer +
1441 msecs_to_jiffies(IAMTHIF_READ_TIMER);
1442
1443 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1444 dev->iamthif_timer);
1445 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1446 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1447 if (time_after(jiffies, timeout)) {
1448 /*
1449 * User didn't read the AMTHI data on time (15sec)
1450 * freeing AMTHI for other requests
1451 */
1452
1453 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1454
1455 amthi_complete_list = &dev->amthi_read_complete_list.
1456 mei_cb.cb_list;
1457
b7cd2d9f 1458 list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, cb_list) {
fb7d879f 1459
b7cd2d9f 1460 cl_pos = cb_pos->file_object->private_data;
fb7d879f 1461
b7cd2d9f
TW
1462 /* Finding the AMTHI entry. */
1463 if (cl_pos == &dev->iamthif_cl)
1464 list_del(&cb_pos->cb_list);
fb7d879f
OW
1465 }
1466 if (dev->iamthif_current_cb)
1467 mei_free_cb_private(dev->iamthif_current_cb);
1468
1469 dev->iamthif_file_object->private_data = NULL;
1470 dev->iamthif_file_object = NULL;
1471 dev->iamthif_current_cb = NULL;
1472 dev->iamthif_timer = 0;
c95efb74 1473 mei_run_next_iamthif_cmd(dev);
fb7d879f
OW
1474
1475 }
1476 }
1477out:
441ab50f
TW
1478 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1479 mutex_unlock(&dev->device_lock);
fb7d879f
OW
1480}
1481
1482/**
1483 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1484 * processing.
1485 *
1486 * @irq: The irq number
1487 * @dev_id: pointer to the device structure
1488 *
1489 * returns irqreturn_t
1490 *
1491 */
1492irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1493{
1494 struct mei_device *dev = (struct mei_device *) dev_id;
1495 struct mei_io_list complete_list;
1496 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1497 struct mei_cl *cl;
1498 s32 slots;
1499 int rets;
1500 bool bus_message_received;
1501
1502
1503 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1504 /* initialize our complete list */
1505 mutex_lock(&dev->device_lock);
0288c7c9 1506 mei_io_list_init(&complete_list);
fb7d879f 1507 dev->host_hw_state = mei_hcsr_read(dev);
4f61a7ad
TW
1508
1509 /* Ack the interrupt here
5f9092f3 1510 * In case of MSI we don't go through the quick handler */
4f61a7ad
TW
1511 if (pci_dev_msi_enabled(dev->pdev))
1512 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1513
fb7d879f
OW
1514 dev->me_hw_state = mei_mecsr_read(dev);
1515
1516 /* check if ME wants a reset */
1517 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
1518 dev->mei_state != MEI_RESETING &&
1519 dev->mei_state != MEI_INITIALIZING) {
1520 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1521 mei_reset(dev, 1);
1522 mutex_unlock(&dev->device_lock);
1523 return IRQ_HANDLED;
1524 }
1525
1526 /* check if we need to start the dev */
1527 if ((dev->host_hw_state & H_RDY) == 0) {
1528 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1529 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1530 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1531 mei_hcsr_set(dev);
1532 dev->mei_state = MEI_INIT_CLIENTS;
1533 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1534 /* link is established
1535 * start sending messages.
1536 */
c95efb74 1537 mei_host_start_message(dev);
fb7d879f
OW
1538 mutex_unlock(&dev->device_lock);
1539 return IRQ_HANDLED;
1540 } else {
1541 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1542 mutex_unlock(&dev->device_lock);
1543 return IRQ_HANDLED;
1544 }
1545 }
5f9092f3 1546 /* check slots available for reading */
fb7d879f
OW
1547 slots = mei_count_full_read_slots(dev);
1548 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1549 slots, dev->extra_write_index);
1550 while (slots > 0 && !dev->extra_write_index) {
1551 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1552 slots, dev->extra_write_index);
1553 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1554 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1555 if (rets)
1556 goto end;
1557 }
1558 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1559end:
1560 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1561 dev->host_hw_state = mei_hcsr_read(dev);
1562 dev->mei_host_buffer_is_empty = mei_host_buffer_is_empty(dev);
1563
1564 bus_message_received = false;
1565 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1566 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1567 bus_message_received = true;
1568 }
1569 mutex_unlock(&dev->device_lock);
1570 if (bus_message_received) {
1571 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1572 wake_up_interruptible(&dev->wait_recvd_msg);
1573 bus_message_received = false;
1574 }
c8372094 1575 if (list_empty(&complete_list.mei_cb.cb_list))
fb7d879f
OW
1576 return IRQ_HANDLED;
1577
1578
1579 list_for_each_entry_safe(cb_pos, cb_next,
1580 &complete_list.mei_cb.cb_list, cb_list) {
1581 cl = (struct mei_cl *)cb_pos->file_private;
1582 list_del(&cb_pos->cb_list);
1583 if (cl) {
1584 if (cl != &dev->iamthif_cl) {
1585 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1586 _mei_cmpl(cl, cb_pos);
1587 cb_pos = NULL;
1588 } else if (cl == &dev->iamthif_cl) {
1589 _mei_cmpl_iamthif(dev, cb_pos);
1590 }
1591 }
1592 }
1593 return IRQ_HANDLED;
1594}
This page took 0.165495 seconds and 5 git commands to generate.