blob: 85e272258ff132f3472884b140b034c310521c2d [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{
Tomas Winkler4b8960b2012-11-11 17:38:00 +020060 if (cb_pos->fop_type == MEI_FOP_WRITE) {
Tomas Winkler601a1ef2012-10-09 16:50:20 +020061 mei_io_cb_free(cb_pos);
Oren Weilfb7d8792011-05-15 13:43:42 +030062 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
Tomas Winkler4b8960b2012-11-11 17:38:00 +020067 } else if (cb_pos->fop_type == MEI_FOP_READ &&
Oren Weilfb7d8792011-05-15 13:43:42 +030068 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/**
Oren Weilfb7d8792011-05-15 13:43:42 +030077 * _mei_irq_thread_state_ok - checks if mei header matches file private data
78 *
79 * @cl: private data of the file object
80 * @mei_hdr: header of mei client message
81 *
82 * returns !=0 if matches, 0 if no match.
83 */
84static int _mei_irq_thread_state_ok(struct mei_cl *cl,
85 struct mei_msg_hdr *mei_hdr)
86{
87 return (cl->host_client_id == mei_hdr->host_addr &&
88 cl->me_client_id == mei_hdr->me_addr &&
89 cl->state == MEI_FILE_CONNECTED &&
90 MEI_READ_COMPLETE != cl->reading_state);
91}
92
93/**
94 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
95 * handle the read mei client message data processing.
96 *
97 * @complete_list: An instance of our list structure
98 * @dev: the device structure
99 * @mei_hdr: header of mei client message
100 *
101 * returns 0 on success, <0 on failure.
102 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200103static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
Oren Weilfb7d8792011-05-15 13:43:42 +0300104 struct mei_device *dev,
105 struct mei_msg_hdr *mei_hdr)
106{
107 struct mei_cl *cl;
108 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
Tomas Winkler479bc592011-06-16 00:46:03 +0300109 unsigned char *buffer = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300110
111 dev_dbg(&dev->pdev->dev, "start client msg\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200112 if (list_empty(&dev->read_list.list))
Oren Weilfb7d8792011-05-15 13:43:42 +0300113 goto quit;
114
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200115 list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200116 cl = cb_pos->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300117 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
118 cl->reading_state = MEI_READING;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200119 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
Oren Weilfb7d8792011-05-15 13:43:42 +0300120
121 if (cb_pos->response_buffer.size <
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200122 mei_hdr->length + cb_pos->buf_idx) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300123 dev_dbg(&dev->pdev->dev, "message overflow.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200124 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300125 return -ENOMEM;
126 }
127 if (buffer)
128 mei_read_slots(dev, buffer, mei_hdr->length);
129
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200130 cb_pos->buf_idx += mei_hdr->length;
Oren Weilfb7d8792011-05-15 13:43:42 +0300131 if (mei_hdr->msg_complete) {
132 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200133 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300134 dev_dbg(&dev->pdev->dev,
Tomas Winklera4136b42012-09-11 00:43:22 +0300135 "completed read H cl = %d, ME cl = %d, length = %lu\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300136 cl->host_client_id,
137 cl->me_client_id,
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200138 cb_pos->buf_idx);
139
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200140 list_add_tail(&cb_pos->list,
141 &complete_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300142 }
143
144 break;
145 }
146
147 }
148
149quit:
150 dev_dbg(&dev->pdev->dev, "message read\n");
151 if (!buffer) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200152 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
Oren Weilfb7d8792011-05-15 13:43:42 +0300153 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
154 *(u32 *) dev->rd_msg_buf);
155 }
156
157 return 0;
158}
159
160/**
Oren Weilfb7d8792011-05-15 13:43:42 +0300161 * _mei_irq_thread_close - processes close related operation.
162 *
163 * @dev: the device structure.
164 * @slots: free slots.
165 * @cb_pos: callback block.
166 * @cl: private data of the file object.
167 * @cmpl_list: complete list.
168 *
169 * returns 0, OK; otherwise, error.
170 */
171static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
172 struct mei_cl_cb *cb_pos,
173 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200174 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300175{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300176 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200177 sizeof(struct hbm_client_connect_request)))
Oren Weilfb7d8792011-05-15 13:43:42 +0300178 return -EBADMSG;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300179
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200180 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300181
182 if (mei_disconnect(dev, cl)) {
183 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200184 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200185 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300186 return -EMSGSIZE;
187 } else {
188 cl->state = MEI_FILE_DISCONNECTING;
189 cl->status = 0;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200190 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200191 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300192 cl->timer_count = MEI_CONNECT_TIMEOUT;
Oren Weilfb7d8792011-05-15 13:43:42 +0300193 }
194
195 return 0;
196}
197
198/**
199 * is_treat_specially_client - checks if the message belongs
200 * to the file private data.
201 *
202 * @cl: private data of the file object
203 * @rs: connect response bus message
204 *
205 */
206static bool is_treat_specially_client(struct mei_cl *cl,
207 struct hbm_client_connect_response *rs)
208{
209
210 if (cl->host_client_id == rs->host_addr &&
211 cl->me_client_id == rs->me_addr) {
212 if (!rs->status) {
213 cl->state = MEI_FILE_CONNECTED;
214 cl->status = 0;
215
216 } else {
217 cl->state = MEI_FILE_DISCONNECTED;
218 cl->status = -ENODEV;
219 }
220 cl->timer_count = 0;
221
222 return true;
223 }
224 return false;
225}
226
227/**
228 * mei_client_connect_response - connects to response irq routine
229 *
230 * @dev: the device structure
231 * @rs: connect response bus message
232 */
233static void mei_client_connect_response(struct mei_device *dev,
234 struct hbm_client_connect_response *rs)
235{
236
237 struct mei_cl *cl;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200238 struct mei_cl_cb *pos = NULL, *next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300239
240 dev_dbg(&dev->pdev->dev,
241 "connect_response:\n"
242 "ME Client = %d\n"
243 "Host Client = %d\n"
244 "Status = %d\n",
245 rs->me_addr,
246 rs->host_addr,
247 rs->status);
248
249 /* if WD or iamthif client treat specially */
250
251 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300252 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
Tomas Winkler70cd5332011-12-22 18:50:50 +0200253 mei_watchdog_register(dev);
Oren Weil9ce178e2011-09-07 09:03:09 +0300254
Tomas Winkler70cd5332011-12-22 18:50:50 +0200255 /* next step in the state maching */
Tomas Winkler19838fb2012-11-01 21:17:15 +0200256 mei_amthif_host_init(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300257 return;
258 }
259
260 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
261 dev->iamthif_state = MEI_IAMTHIF_IDLE;
262 return;
263 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200264 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200265
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200266 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200267 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200268 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200269 return;
270 }
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200271 if (pos->fop_type == MEI_FOP_IOCTL) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200272 if (is_treat_specially_client(cl, rs)) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200273 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200274 cl->status = 0;
275 cl->timer_count = 0;
276 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300277 }
278 }
279 }
280}
281
282/**
283 * mei_client_disconnect_response - disconnects from response irq routine
284 *
285 * @dev: the device structure
286 * @rs: disconnect response bus message
287 */
288static void mei_client_disconnect_response(struct mei_device *dev,
289 struct hbm_client_connect_response *rs)
290{
291 struct mei_cl *cl;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200292 struct mei_cl_cb *pos = NULL, *next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300293
294 dev_dbg(&dev->pdev->dev,
295 "disconnect_response:\n"
296 "ME Client = %d\n"
297 "Host Client = %d\n"
298 "Status = %d\n",
299 rs->me_addr,
300 rs->host_addr,
301 rs->status);
302
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200303 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200304 cl = pos->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300305
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200306 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200307 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200308 return;
309 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300310
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200311 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
312 if (cl->host_client_id == rs->host_addr &&
313 cl->me_client_id == rs->me_addr) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300314
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200315 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200316 if (!rs->status)
317 cl->state = MEI_FILE_DISCONNECTED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300318
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200319 cl->status = 0;
320 cl->timer_count = 0;
321 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300322 }
323 }
324}
325
326/**
327 * same_flow_addr - tells if they have the same address.
328 *
329 * @file: private data of the file object.
330 * @flow: flow control.
331 *
332 * returns !=0, same; 0,not.
333 */
334static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
335{
336 return (cl->host_client_id == flow->host_addr &&
337 cl->me_client_id == flow->me_addr);
338}
339
340/**
341 * add_single_flow_creds - adds single buffer credentials.
342 *
343 * @file: private data ot the file object.
344 * @flow: flow control.
345 */
346static void add_single_flow_creds(struct mei_device *dev,
347 struct hbm_flow_control *flow)
348{
349 struct mei_me_client *client;
350 int i;
351
Tomas Winklercf9673d2011-06-06 10:44:33 +0300352 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300353 client = &dev->me_clients[i];
354 if (client && flow->me_addr == client->client_id) {
355 if (client->props.single_recv_buf) {
356 client->mei_flow_ctrl_creds++;
357 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
358 flow->me_addr);
359 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
360 client->mei_flow_ctrl_creds);
361 } else {
362 BUG(); /* error in flow control */
363 }
364 }
365 }
366}
367
368/**
369 * mei_client_flow_control_response - flow control response irq routine
370 *
371 * @dev: the device structure
372 * @flow_control: flow control response bus message
373 */
374static void mei_client_flow_control_response(struct mei_device *dev,
375 struct hbm_flow_control *flow_control)
376{
377 struct mei_cl *cl_pos = NULL;
378 struct mei_cl *cl_next = NULL;
379
380 if (!flow_control->host_addr) {
381 /* single receive buffer */
382 add_single_flow_creds(dev, flow_control);
383 } else {
384 /* normal connection */
385 list_for_each_entry_safe(cl_pos, cl_next,
386 &dev->file_list, link) {
387 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
388
389 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
390 cl_pos->host_client_id,
391 cl_pos->me_client_id);
392 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
393 flow_control->host_addr,
394 flow_control->me_addr);
395 if (same_flow_addr(cl_pos, flow_control)) {
396 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
397 flow_control->host_addr,
398 flow_control->me_addr);
399 cl_pos->mei_flow_ctrl_creds++;
400 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
401 cl_pos->mei_flow_ctrl_creds);
402 break;
403 }
404 }
405 }
406}
407
408/**
409 * same_disconn_addr - tells if they have the same address
410 *
411 * @file: private data of the file object.
412 * @disconn: disconnection request.
413 *
414 * returns !=0, same; 0,not.
415 */
416static int same_disconn_addr(struct mei_cl *cl,
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200417 struct hbm_client_connect_request *req)
Oren Weilfb7d8792011-05-15 13:43:42 +0300418{
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200419 return (cl->host_client_id == req->host_addr &&
420 cl->me_client_id == req->me_addr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300421}
422
423/**
424 * mei_client_disconnect_request - disconnects from request irq routine
425 *
426 * @dev: the device structure.
427 * @disconnect_req: disconnect request bus message.
428 */
429static void mei_client_disconnect_request(struct mei_device *dev,
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200430 struct hbm_client_connect_request *disconnect_req)
Oren Weilfb7d8792011-05-15 13:43:42 +0300431{
Oren Weilfb7d8792011-05-15 13:43:42 +0300432 struct hbm_client_connect_response *disconnect_res;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200433 struct mei_cl *pos, *next;
434 const size_t len = sizeof(struct hbm_client_connect_response);
Oren Weilfb7d8792011-05-15 13:43:42 +0300435
Tomas Winkler5bd64712012-11-18 15:13:14 +0200436 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
437 if (same_disconn_addr(pos, disconnect_req)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300438 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
439 disconnect_req->host_addr,
440 disconnect_req->me_addr);
Tomas Winkler5bd64712012-11-18 15:13:14 +0200441 pos->state = MEI_FILE_DISCONNECTED;
442 pos->timer_count = 0;
443 if (pos == &dev->wd_cl)
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300444 dev->wd_pending = false;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200445 else if (pos == &dev->iamthif_cl)
Oren Weilfb7d8792011-05-15 13:43:42 +0300446 dev->iamthif_timer = 0;
447
448 /* prepare disconnect response */
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200449 (void)mei_hbm_hdr((u32 *)&dev->wr_ext_msg.hdr, len);
Oren Weilfb7d8792011-05-15 13:43:42 +0300450 disconnect_res =
451 (struct hbm_client_connect_response *)
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200452 &dev->wr_ext_msg.data;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200453 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200454 disconnect_res->host_addr = pos->host_client_id;
455 disconnect_res->me_addr = pos->me_client_id;
Oren Weilfb7d8792011-05-15 13:43:42 +0300456 disconnect_res->status = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300457 break;
458 }
459 }
460}
461
Oren Weilfb7d8792011-05-15 13:43:42 +0300462/**
463 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
464 * handle the read bus message cmd processing.
465 *
466 * @dev: the device structure
467 * @mei_hdr: header of bus message
468 */
469static void mei_irq_thread_read_bus_message(struct mei_device *dev,
470 struct mei_msg_hdr *mei_hdr)
471{
472 struct mei_bus_message *mei_msg;
473 struct hbm_host_version_response *version_res;
474 struct hbm_client_connect_response *connect_res;
475 struct hbm_client_connect_response *disconnect_res;
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200476 struct hbm_client_connect_request *disconnect_req;
Oren Weilfb7d8792011-05-15 13:43:42 +0300477 struct hbm_flow_control *flow_control;
478 struct hbm_props_response *props_res;
479 struct hbm_host_enum_response *enum_res;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200480 struct hbm_host_stop_request *stop_req;
Oren Weilabc51b62011-09-21 16:45:30 +0300481 int res;
Oren Weilfb7d8792011-05-15 13:43:42 +0300482
Oren Weilfb7d8792011-05-15 13:43:42 +0300483
484 /* read the message to our buffer */
Oren Weilfb7d8792011-05-15 13:43:42 +0300485 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200486 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
487 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
Oren Weilfb7d8792011-05-15 13:43:42 +0300488
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200489 switch (mei_msg->hbm_cmd) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300490 case HOST_START_RES_CMD:
491 version_res = (struct hbm_host_version_response *) mei_msg;
492 if (version_res->host_version_supported) {
493 dev->version.major_version = HBM_MAJOR_VERSION;
494 dev->version.minor_version = HBM_MINOR_VERSION;
Tomas Winklerb210d752012-08-07 00:03:56 +0300495 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300496 dev->init_clients_state == MEI_START_MESSAGE) {
497 dev->init_clients_timer = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300498 mei_host_enum_clients_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300499 } else {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300500 dev->recvd_msg = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300501 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
502 mei_reset(dev, 1);
503 return;
504 }
505 } else {
Tomas Winkler5bd64712012-11-18 15:13:14 +0200506 u32 *buf = dev->wr_msg_buf;
507 const size_t len = sizeof(struct hbm_host_stop_request);
508
Oren Weilfb7d8792011-05-15 13:43:42 +0300509 dev->version = version_res->me_max_version;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200510
Oren Weilfb7d8792011-05-15 13:43:42 +0300511 /* send stop message */
Tomas Winkler5bd64712012-11-18 15:13:14 +0200512 mei_hdr = mei_hbm_hdr(&buf[0], len);
513 stop_req = (struct hbm_host_stop_request *)&buf[1];
514 memset(stop_req, 0, len);
515 stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
516 stop_req->reason = DRIVER_STOP_REQUEST;
Oren Weilfb7d8792011-05-15 13:43:42 +0300517
Oren Weilfb7d8792011-05-15 13:43:42 +0300518 mei_write_message(dev, mei_hdr,
Tomas Winkler5bd64712012-11-18 15:13:14 +0200519 (unsigned char *)stop_req, len);
Oren Weilfb7d8792011-05-15 13:43:42 +0300520 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
521 return;
522 }
523
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300524 dev->recvd_msg = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300525 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
526 break;
527
528 case CLIENT_CONNECT_RES_CMD:
Tomas Winkler5bd64712012-11-18 15:13:14 +0200529 connect_res = (struct hbm_client_connect_response *) mei_msg;
Oren Weilfb7d8792011-05-15 13:43:42 +0300530 mei_client_connect_response(dev, connect_res);
531 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
532 wake_up(&dev->wait_recvd_msg);
533 break;
534
535 case CLIENT_DISCONNECT_RES_CMD:
Tomas Winkler5bd64712012-11-18 15:13:14 +0200536 disconnect_res = (struct hbm_client_connect_response *) mei_msg;
Tomas Winkler441ab502011-12-13 23:39:34 +0200537 mei_client_disconnect_response(dev, disconnect_res);
Oren Weilfb7d8792011-05-15 13:43:42 +0300538 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
539 wake_up(&dev->wait_recvd_msg);
540 break;
541
542 case MEI_FLOW_CONTROL_CMD:
543 flow_control = (struct hbm_flow_control *) mei_msg;
544 mei_client_flow_control_response(dev, flow_control);
545 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
546 break;
547
548 case HOST_CLIENT_PROPERTIES_RES_CMD:
549 props_res = (struct hbm_props_response *)mei_msg;
550 if (props_res->status || !dev->me_clients) {
551 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
552 mei_reset(dev, 1);
553 return;
554 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200555 if (dev->me_clients[dev->me_client_presentation_num]
Oren Weilfb7d8792011-05-15 13:43:42 +0300556 .client_id == props_res->address) {
557
558 dev->me_clients[dev->me_client_presentation_num].props
559 = props_res->client_properties;
560
Tomas Winklerb210d752012-08-07 00:03:56 +0300561 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300562 dev->init_clients_state ==
563 MEI_CLIENT_PROPERTIES_MESSAGE) {
564 dev->me_client_index++;
565 dev->me_client_presentation_num++;
Oren Weilabc51b62011-09-21 16:45:30 +0300566
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700567 /** Send Client Properties request **/
Oren Weilabc51b62011-09-21 16:45:30 +0300568 res = mei_host_client_properties(dev);
569 if (res < 0) {
570 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
571 return;
572 } else if (!res) {
573 /*
574 * No more clients to send to.
575 * Clear Map for indicating now ME clients
576 * with associated host client
577 */
578 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
579 dev->open_handle_count = 0;
580
581 /*
582 * Reserving the first three client IDs
583 * Client Id 0 - Reserved for MEI Bus Message communications
584 * Client Id 1 - Reserved for Watchdog
585 * Client ID 2 - Reserved for AMTHI
586 */
587 bitmap_set(dev->host_clients_map, 0, 3);
Tomas Winklerb210d752012-08-07 00:03:56 +0300588 dev->dev_state = MEI_DEV_ENABLED;
Oren Weilabc51b62011-09-21 16:45:30 +0300589
590 /* if wd initialization fails, initialization the AMTHI client,
591 * otherwise the AMTHI client will be initialized after the WD client connect response
592 * will be received
593 */
594 if (mei_wd_host_init(dev))
Tomas Winkler19838fb2012-11-01 21:17:15 +0200595 mei_amthif_host_init(dev);
Oren Weilabc51b62011-09-21 16:45:30 +0300596 }
597
Oren Weilfb7d8792011-05-15 13:43:42 +0300598 } else {
599 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
600 mei_reset(dev, 1);
601 return;
602 }
603 } else {
604 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
605 mei_reset(dev, 1);
606 return;
607 }
608 break;
609
610 case HOST_ENUM_RES_CMD:
611 enum_res = (struct hbm_host_enum_response *) mei_msg;
612 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
Tomas Winklerb210d752012-08-07 00:03:56 +0300613 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
Oren Weilfb7d8792011-05-15 13:43:42 +0300614 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
615 dev->init_clients_timer = 0;
616 dev->me_client_presentation_num = 0;
617 dev->me_client_index = 0;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300618 mei_allocate_me_clients_storage(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300619 dev->init_clients_state =
620 MEI_CLIENT_PROPERTIES_MESSAGE;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300621 mei_host_client_properties(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300622 } else {
623 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
624 mei_reset(dev, 1);
625 return;
626 }
627 break;
628
629 case HOST_STOP_RES_CMD:
Tomas Winklerb210d752012-08-07 00:03:56 +0300630 dev->dev_state = MEI_DEV_DISABLED;
Oren Weilfb7d8792011-05-15 13:43:42 +0300631 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
632 mei_reset(dev, 1);
633 break;
634
635 case CLIENT_DISCONNECT_REQ_CMD:
636 /* search for client */
Tomas Winkleraeba4a02012-11-11 17:38:04 +0200637 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
Oren Weilfb7d8792011-05-15 13:43:42 +0300638 mei_client_disconnect_request(dev, disconnect_req);
639 break;
640
641 case ME_STOP_REQ_CMD:
Tomas Winkler5bd64712012-11-18 15:13:14 +0200642 {
643 /* prepare stop request: sent in next interrupt event */
644
Tomas Winkler5bd64712012-11-18 15:13:14 +0200645 const size_t len = sizeof(struct hbm_host_stop_request);
646
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200647 mei_hdr = mei_hbm_hdr((u32 *)&dev->wr_ext_msg.hdr, len);
648 stop_req = (struct hbm_host_stop_request *)&dev->wr_ext_msg.data;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200649 memset(stop_req, 0, len);
650 stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
651 stop_req->reason = DRIVER_STOP_REQUEST;
Oren Weilfb7d8792011-05-15 13:43:42 +0300652 break;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200653 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300654 default:
655 BUG();
656 break;
657
658 }
659}
660
661
662/**
663 * _mei_hb_read - processes read related operation.
664 *
665 * @dev: the device structure.
666 * @slots: free slots.
667 * @cb_pos: callback block.
668 * @cl: private data of the file object.
669 * @cmpl_list: complete list.
670 *
671 * returns 0, OK; otherwise, error.
672 */
673static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
674 struct mei_cl_cb *cb_pos,
675 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200676 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300677{
Tomas Winkler1e69d642012-05-29 16:39:12 +0300678 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300679 sizeof(struct hbm_flow_control))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300680 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200681 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300682 return -EBADMSG;
683 }
684
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300685 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
686
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200687 if (mei_send_flow_control(dev, cl)) {
688 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200689 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200690 list_move_tail(&cb_pos->list, &cmpl_list->list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200691 return -ENODEV;
692 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200693 list_move_tail(&cb_pos->list, &dev->read_list.list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200694
Oren Weilfb7d8792011-05-15 13:43:42 +0300695 return 0;
696}
697
698
699/**
700 * _mei_irq_thread_ioctl - processes ioctl related operation.
701 *
702 * @dev: the device structure.
703 * @slots: free slots.
704 * @cb_pos: callback block.
705 * @cl: private data of the file object.
706 * @cmpl_list: complete list.
707 *
708 * returns 0, OK; otherwise, error.
709 */
710static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
711 struct mei_cl_cb *cb_pos,
712 struct mei_cl *cl,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200713 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300714{
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300715 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
Oren Weilfb7d8792011-05-15 13:43:42 +0300716 sizeof(struct hbm_client_connect_request))) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300717 /* return the cancel routine */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200718 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300719 return -EBADMSG;
720 }
721
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300722 cl->state = MEI_FILE_CONNECTING;
723 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
724 if (mei_connect(dev, cl)) {
725 cl->status = -ENODEV;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200726 cb_pos->buf_idx = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200727 list_del(&cb_pos->list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300728 return -ENODEV;
729 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200730 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300731 cl->timer_count = MEI_CONNECT_TIMEOUT;
732 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300733 return 0;
734}
735
736/**
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200737 * mei_irq_thread_write_complete - write messages to device.
Oren Weilfb7d8792011-05-15 13:43:42 +0300738 *
739 * @dev: the device structure.
740 * @slots: free slots.
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200741 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300742 * @cmpl_list: complete list.
743 *
744 * returns 0, OK; otherwise, error.
745 */
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200746static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
747 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300748{
749 struct mei_msg_hdr *mei_hdr;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200750 struct mei_cl *cl = cb->cl;
751 size_t len = cb->request_buffer.size - cb->buf_idx;
752 size_t msg_slots = mei_data2slots(len);
Oren Weilfb7d8792011-05-15 13:43:42 +0300753
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200754 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
755 mei_hdr->host_addr = cl->host_client_id;
756 mei_hdr->me_addr = cl->me_client_id;
757 mei_hdr->reserved = 0;
758
759 if (*slots >= msg_slots) {
760 mei_hdr->length = len;
Oren Weilfb7d8792011-05-15 13:43:42 +0300761 mei_hdr->msg_complete = 1;
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200762 /* Split the message only if we can write the whole host buffer */
Tomas Winkler24aadc82012-06-25 23:46:27 +0300763 } else if (*slots == dev->hbuf_depth) {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200764 msg_slots = *slots;
765 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
766 mei_hdr->length = len;
Oren Weilfb7d8792011-05-15 13:43:42 +0300767 mei_hdr->msg_complete = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300768 } else {
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200769 /* wait for next time the host buffer is empty */
770 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300771 }
772
Tomas Winklerea3b5fb2012-11-18 15:13:16 +0200773 dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
774 cb->request_buffer.size, cb->buf_idx);
775 dev_dbg(&dev->pdev->dev, "msg: len = %d complete = %d\n",
776 mei_hdr->length, mei_hdr->msg_complete);
777
778 *slots -= msg_slots;
779 if (mei_write_message(dev, mei_hdr,
780 cb->request_buffer.data + cb->buf_idx, len)) {
781 cl->status = -ENODEV;
782 list_move_tail(&cb->list, &cmpl_list->list);
783 return -ENODEV;
784 }
785
786 if (mei_flow_ctrl_reduce(dev, cl))
787 return -ENODEV;
788
789 cl->status = 0;
790 cb->buf_idx += mei_hdr->length;
791 if (mei_hdr->msg_complete)
792 list_move_tail(&cb->list, &dev->write_waiting_list.list);
793
Oren Weilfb7d8792011-05-15 13:43:42 +0300794 return 0;
795}
796
797/**
Oren Weilfb7d8792011-05-15 13:43:42 +0300798 * mei_irq_thread_read_handler - bottom half read routine after ISR to
799 * handle the read processing.
800 *
801 * @cmpl_list: An instance of our list structure
802 * @dev: the device structure
803 * @slots: slots to read.
804 *
805 * returns 0 on success, <0 on failure.
806 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200807static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
Oren Weilfb7d8792011-05-15 13:43:42 +0300808 struct mei_device *dev,
809 s32 *slots)
810{
811 struct mei_msg_hdr *mei_hdr;
812 struct mei_cl *cl_pos = NULL;
813 struct mei_cl *cl_next = NULL;
814 int ret = 0;
815
816 if (!dev->rd_msg_hdr) {
817 dev->rd_msg_hdr = mei_mecbrw_read(dev);
818 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
819 (*slots)--;
820 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
821 }
822 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
823 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
824
825 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
826 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
827 ret = -EBADMSG;
828 goto end;
829 }
830
831 if (mei_hdr->host_addr || mei_hdr->me_addr) {
832 list_for_each_entry_safe(cl_pos, cl_next,
833 &dev->file_list, link) {
834 dev_dbg(&dev->pdev->dev,
835 "list_for_each_entry_safe read host"
836 " client = %d, ME client = %d\n",
837 cl_pos->host_client_id,
838 cl_pos->me_client_id);
839 if (cl_pos->host_client_id == mei_hdr->host_addr &&
840 cl_pos->me_client_id == mei_hdr->me_addr)
841 break;
842 }
843
844 if (&cl_pos->link == &dev->file_list) {
845 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
846 ret = -EBADMSG;
847 goto end;
848 }
849 }
850 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
851 dev_dbg(&dev->pdev->dev,
852 "we can't read the message slots =%08x.\n",
853 *slots);
854 /* we can't read the message */
855 ret = -ERANGE;
856 goto end;
857 }
858
859 /* decide where to read the message too */
860 if (!mei_hdr->host_addr) {
861 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
862 mei_irq_thread_read_bus_message(dev, mei_hdr);
863 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
864 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
865 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
866 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
867 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
868 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
869 mei_hdr->length);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200870
871 ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300872 if (ret)
873 goto end;
874
875 } else {
876 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
877 ret = mei_irq_thread_read_client_message(cmpl_list,
878 dev, mei_hdr);
879 if (ret)
880 goto end;
881
882 }
883
884 /* reset the number of slots and header */
885 *slots = mei_count_full_read_slots(dev);
886 dev->rd_msg_hdr = 0;
887
888 if (*slots == -EOVERFLOW) {
889 /* overflow - reset */
890 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
891 /* set the event since message has been read */
892 ret = -ERANGE;
893 goto end;
894 }
895end:
896 return ret;
897}
898
899
900/**
901 * mei_irq_thread_write_handler - bottom half write routine after
902 * ISR to handle the write processing.
903 *
904 * @cmpl_list: An instance of our list structure
905 * @dev: the device structure
906 * @slots: slots to write.
907 *
908 * returns 0 on success, <0 on failure.
909 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200910static int mei_irq_thread_write_handler(struct mei_cl_cb *cmpl_list,
911 struct mei_device *dev, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300912{
913
914 struct mei_cl *cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200915 struct mei_cl_cb *pos = NULL, *next = NULL;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200916 struct mei_cl_cb *list;
Oren Weilfb7d8792011-05-15 13:43:42 +0300917 int ret;
918
Tomas Winkler726917f2012-06-25 23:46:28 +0300919 if (!mei_hbuf_is_empty(dev)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300920 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
921 return 0;
922 }
Tomas Winkler726917f2012-06-25 23:46:28 +0300923 *slots = mei_hbuf_empty_slots(dev);
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300924 if (*slots <= 0)
925 return -EMSGSIZE;
926
Oren Weilfb7d8792011-05-15 13:43:42 +0300927 /* complete all waiting for write CB */
928 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
929
930 list = &dev->write_waiting_list;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200931 list_for_each_entry_safe(pos, next, &list->list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200932 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200933 if (cl == NULL)
934 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +0300935
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200936 cl->status = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200937 list_del(&pos->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200938 if (MEI_WRITING == cl->writing_state &&
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200939 pos->fop_type == MEI_FOP_WRITE &&
940 cl != &dev->iamthif_cl) {
Tomas Winkler483136e2012-07-04 19:24:54 +0300941 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200942 cl->writing_state = MEI_WRITE_COMPLETE;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200943 list_add_tail(&pos->list, &cmpl_list->list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200944 }
945 if (cl == &dev->iamthif_cl) {
946 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
947 if (dev->iamthif_flow_control_pending) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200948 ret = mei_amthif_irq_read(dev, slots);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200949 if (ret)
950 return ret;
951 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300952 }
953 }
954
Tomas Winklerc216fde2012-08-16 19:39:43 +0300955 if (dev->wd_state == MEI_WD_STOPPING) {
956 dev->wd_state = MEI_WD_IDLE;
Oren Weilfb7d8792011-05-15 13:43:42 +0300957 wake_up_interruptible(&dev->wait_stop_wd);
Oren Weilfb7d8792011-05-15 13:43:42 +0300958 }
959
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200960 if (dev->wr_ext_msg.hdr.length) {
961 mei_write_message(dev, &dev->wr_ext_msg.hdr,
962 dev->wr_ext_msg.data, dev->wr_ext_msg.hdr.length);
963 *slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
964 dev->wr_ext_msg.hdr.length = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300965 }
Tomas Winklerb210d752012-08-07 00:03:56 +0300966 if (dev->dev_state == MEI_DEV_ENABLED) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300967 if (dev->wd_pending &&
Tomas Winkler483136e2012-07-04 19:24:54 +0300968 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300969 if (mei_wd_send(dev))
970 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
Tomas Winkler483136e2012-07-04 19:24:54 +0300971 else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
972 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +0300973
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300974 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300975
Tomas Winklerc216fde2012-08-16 19:39:43 +0300976 if (dev->wd_state == MEI_WD_RUNNING)
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300977 *slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
Tomas Winklerd242a0a2012-07-04 19:24:50 +0300978 else
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300979 *slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
Oren Weilfb7d8792011-05-15 13:43:42 +0300980 }
981 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300982
983 /* complete control write list CB */
Tomas Winklerc8372092011-11-27 21:43:33 +0200984 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200985 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200986 cl = pos->cl;
Tomas Winklerc8372092011-11-27 21:43:33 +0200987 if (!cl) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200988 list_del(&pos->list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200989 return -ENODEV;
Oren Weilfb7d8792011-05-15 13:43:42 +0300990 }
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200991 switch (pos->fop_type) {
992 case MEI_FOP_CLOSE:
Tomas Winklerc8372092011-11-27 21:43:33 +0200993 /* send disconnect message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200994 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200995 if (ret)
996 return ret;
997
998 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200999 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +02001000 /* send flow control message */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001001 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001002 if (ret)
1003 return ret;
1004
1005 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +02001006 case MEI_FOP_IOCTL:
Tomas Winklerc8372092011-11-27 21:43:33 +02001007 /* connect message */
Natalia Ovsyanikove8cd29d2011-12-05 00:16:54 +02001008 if (mei_other_client_is_connecting(dev, cl))
Tomas Winklerc8372092011-11-27 21:43:33 +02001009 continue;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001010 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +02001011 if (ret)
1012 return ret;
1013
1014 break;
1015
1016 default:
1017 BUG();
1018 }
1019
Oren Weilfb7d8792011-05-15 13:43:42 +03001020 }
1021 /* complete write list CB */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001022 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001023 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +02001024 cl = pos->cl;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001025 if (cl == NULL)
1026 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001027
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001028 if (cl != &dev->iamthif_cl) {
Tomas Winklerd2041152012-06-19 09:13:34 +03001029 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001030 dev_dbg(&dev->pdev->dev,
Tomas Winkler483136e2012-07-04 19:24:54 +03001031 "No flow control credentials for client %d, not sending.\n",
1032 cl->host_client_id);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001033 continue;
Oren Weilfb7d8792011-05-15 13:43:42 +03001034 }
Tomas Winklerea3b5fb2012-11-18 15:13:16 +02001035 ret = mei_irq_thread_write_complete(dev, slots, pos,
1036 cmpl_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001037 if (ret)
1038 return ret;
1039
1040 } else if (cl == &dev->iamthif_cl) {
1041 /* IAMTHIF IOCTL */
1042 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
Tomas Winklerd2041152012-06-19 09:13:34 +03001043 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001044 dev_dbg(&dev->pdev->dev,
Tomas Winkler483136e2012-07-04 19:24:54 +03001045 "No flow control credentials for amthi client %d.\n",
1046 cl->host_client_id);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001047 continue;
1048 }
Tomas Winkler19838fb2012-11-01 21:17:15 +02001049 ret = mei_amthif_irq_process_completed(dev, slots, pos,
1050 cl, cmpl_list);
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001051 if (ret)
1052 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +03001053
1054 }
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001055
Oren Weilfb7d8792011-05-15 13:43:42 +03001056 }
1057 return 0;
1058}
1059
1060
1061
1062/**
1063 * mei_timer - timer function.
1064 *
1065 * @work: pointer to the work_struct structure
1066 *
1067 * NOTE: This function is called by timer interrupt work
1068 */
Oren Weila61c6532011-09-07 09:03:13 +03001069void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +03001070{
1071 unsigned long timeout;
1072 struct mei_cl *cl_pos = NULL;
1073 struct mei_cl *cl_next = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +03001074 struct mei_cl_cb *cb_pos = NULL;
1075 struct mei_cl_cb *cb_next = NULL;
1076
1077 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +03001078 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +03001079
1080
1081 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001082 if (dev->dev_state != MEI_DEV_ENABLED) {
1083 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001084 if (dev->init_clients_timer) {
1085 if (--dev->init_clients_timer == 0) {
1086 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1087 dev->init_clients_state);
1088 mei_reset(dev, 1);
1089 }
1090 }
1091 }
1092 goto out;
1093 }
1094 /*** connect/disconnect timeouts ***/
1095 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1096 if (cl_pos->timer_count) {
1097 if (--cl_pos->timer_count == 0) {
1098 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1099 mei_reset(dev, 1);
1100 goto out;
1101 }
1102 }
1103 }
1104
Oren Weilfb7d8792011-05-15 13:43:42 +03001105 if (dev->iamthif_stall_timer) {
1106 if (--dev->iamthif_stall_timer == 0) {
Masanari Iida32de21f2012-01-25 23:14:56 +09001107 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +03001108 mei_reset(dev, 1);
1109 dev->iamthif_msg_buf_size = 0;
1110 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +03001111 dev->iamthif_canceled = false;
1112 dev->iamthif_ioctl = true;
Oren Weilfb7d8792011-05-15 13:43:42 +03001113 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1114 dev->iamthif_timer = 0;
1115
Tomas Winkler601a1ef2012-10-09 16:50:20 +02001116 mei_io_cb_free(dev->iamthif_current_cb);
1117 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +03001118
1119 dev->iamthif_file_object = NULL;
Tomas Winkler19838fb2012-11-01 21:17:15 +02001120 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001121 }
1122 }
1123
1124 if (dev->iamthif_timer) {
1125
1126 timeout = dev->iamthif_timer +
Tomas Winkler3870c322012-11-01 21:17:14 +02001127 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilfb7d8792011-05-15 13:43:42 +03001128
1129 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1130 dev->iamthif_timer);
1131 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1132 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1133 if (time_after(jiffies, timeout)) {
1134 /*
1135 * User didn't read the AMTHI data on time (15sec)
1136 * freeing AMTHI for other requests
1137 */
1138
1139 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1140
Tomas Winklere773efc2012-11-11 17:37:58 +02001141 list_for_each_entry_safe(cb_pos, cb_next,
1142 &dev->amthif_rd_complete_list.list, list) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001143
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001144 cl_pos = cb_pos->file_object->private_data;
Oren Weilfb7d8792011-05-15 13:43:42 +03001145
Tomas Winklerb7cd2d92011-11-27 21:43:34 +02001146 /* Finding the AMTHI entry. */
1147 if (cl_pos == &dev->iamthif_cl)
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001148 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001149 }
Tomas Winkler601a1ef2012-10-09 16:50:20 +02001150 mei_io_cb_free(dev->iamthif_current_cb);
1151 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +03001152
1153 dev->iamthif_file_object->private_data = NULL;
1154 dev->iamthif_file_object = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +03001155 dev->iamthif_timer = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +02001156 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001157
1158 }
1159 }
1160out:
Tomas Winkler441ab502011-12-13 23:39:34 +02001161 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1162 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +03001163}
1164
1165/**
1166 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1167 * processing.
1168 *
1169 * @irq: The irq number
1170 * @dev_id: pointer to the device structure
1171 *
1172 * returns irqreturn_t
1173 *
1174 */
1175irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1176{
1177 struct mei_device *dev = (struct mei_device *) dev_id;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001178 struct mei_cl_cb complete_list;
Oren Weilfb7d8792011-05-15 13:43:42 +03001179 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1180 struct mei_cl *cl;
1181 s32 slots;
1182 int rets;
1183 bool bus_message_received;
1184
1185
1186 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1187 /* initialize our complete list */
1188 mutex_lock(&dev->device_lock);
Tomas Winkler0288c7c2011-06-06 10:44:34 +03001189 mei_io_list_init(&complete_list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001190 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001191
1192 /* Ack the interrupt here
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001193 * In case of MSI we don't go through the quick handler */
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001194 if (pci_dev_msi_enabled(dev->pdev))
1195 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1196
Oren Weilfb7d8792011-05-15 13:43:42 +03001197 dev->me_hw_state = mei_mecsr_read(dev);
1198
1199 /* check if ME wants a reset */
1200 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
Tomas Winklerb210d752012-08-07 00:03:56 +03001201 dev->dev_state != MEI_DEV_RESETING &&
1202 dev->dev_state != MEI_DEV_INITIALIZING) {
Oren Weilfb7d8792011-05-15 13:43:42 +03001203 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1204 mei_reset(dev, 1);
1205 mutex_unlock(&dev->device_lock);
1206 return IRQ_HANDLED;
1207 }
1208
1209 /* check if we need to start the dev */
1210 if ((dev->host_hw_state & H_RDY) == 0) {
1211 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1212 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1213 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1214 mei_hcsr_set(dev);
Tomas Winklerb210d752012-08-07 00:03:56 +03001215 dev->dev_state = MEI_DEV_INIT_CLIENTS;
Oren Weilfb7d8792011-05-15 13:43:42 +03001216 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1217 /* link is established
1218 * start sending messages.
1219 */
Tomas Winklerc95efb72011-05-25 17:28:21 +03001220 mei_host_start_message(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001221 mutex_unlock(&dev->device_lock);
1222 return IRQ_HANDLED;
1223 } else {
1224 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1225 mutex_unlock(&dev->device_lock);
1226 return IRQ_HANDLED;
1227 }
1228 }
Justin P. Mattock5f9092f32012-03-12 07:18:09 -07001229 /* check slots available for reading */
Oren Weilfb7d8792011-05-15 13:43:42 +03001230 slots = mei_count_full_read_slots(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +02001231 while (slots > 0) {
1232 /* we have urgent data to send so break the read */
1233 if (dev->wr_ext_msg.hdr.length)
1234 break;
1235 dev_dbg(&dev->pdev->dev, "slots =%08x\n", slots);
Oren Weilfb7d8792011-05-15 13:43:42 +03001236 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1237 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1238 if (rets)
1239 goto end;
1240 }
1241 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1242end:
1243 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1244 dev->host_hw_state = mei_hcsr_read(dev);
Tomas Winkler726917f2012-06-25 23:46:28 +03001245 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +03001246
1247 bus_message_received = false;
1248 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1249 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1250 bus_message_received = true;
1251 }
1252 mutex_unlock(&dev->device_lock);
1253 if (bus_message_received) {
1254 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1255 wake_up_interruptible(&dev->wait_recvd_msg);
1256 bus_message_received = false;
1257 }
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001258 if (list_empty(&complete_list.list))
Oren Weilfb7d8792011-05-15 13:43:42 +03001259 return IRQ_HANDLED;
1260
1261
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001262 list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +02001263 cl = cb_pos->cl;
Tomas Winklerfb601ad2012-10-15 12:06:48 +02001264 list_del(&cb_pos->list);
Oren Weilfb7d8792011-05-15 13:43:42 +03001265 if (cl) {
1266 if (cl != &dev->iamthif_cl) {
1267 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1268 _mei_cmpl(cl, cb_pos);
1269 cb_pos = NULL;
1270 } else if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +02001271 mei_amthif_complete(dev, cb_pos);
Oren Weilfb7d8792011-05-15 13:43:42 +03001272 }
1273 }
1274 }
1275 return IRQ_HANDLED;
1276}