blob: 74d593fd6cbd694ec9890dac2e07a37cfdf6b9f8 [file] [log] [blame]
Tomas Winkler19838fb2012-11-01 21:17:15 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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#include <linux/kernel.h>
18#include <linux/fs.h>
19#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/fcntl.h>
22#include <linux/aio.h>
23#include <linux/pci.h>
24#include <linux/init.h>
25#include <linux/ioctl.h>
26#include <linux/cdev.h>
27#include <linux/list.h>
28#include <linux/delay.h>
29#include <linux/sched.h>
30#include <linux/uuid.h>
31#include <linux/jiffies.h>
32#include <linux/uaccess.h>
33
34
35#include "mei_dev.h"
36#include "hw.h"
37#include <linux/mei.h>
38#include "interface.h"
39
40const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
41 0xa8, 0x46, 0xe0, 0xff, 0x65,
42 0x81, 0x4c);
43
44/**
45 * mei_amthif_reset_params - initializes mei device iamthif
46 *
47 * @dev: the device structure
48 */
49void mei_amthif_reset_params(struct mei_device *dev)
50{
51 /* reset iamthif parameters. */
52 dev->iamthif_current_cb = NULL;
53 dev->iamthif_msg_buf_size = 0;
54 dev->iamthif_msg_buf_index = 0;
55 dev->iamthif_canceled = false;
56 dev->iamthif_ioctl = false;
57 dev->iamthif_state = MEI_IAMTHIF_IDLE;
58 dev->iamthif_timer = 0;
59}
60
61/**
62 * mei_amthif_host_init_ - mei initialization amthif client.
63 *
64 * @dev: the device structure
65 *
66 */
67void mei_amthif_host_init(struct mei_device *dev)
68{
69 int i;
70 unsigned char *msg_buf;
71
72 mei_cl_init(&dev->iamthif_cl, dev);
73 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
74
75 /* find ME amthi client */
76 i = mei_me_cl_update_filext(dev, &dev->iamthif_cl,
77 &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
78 if (i < 0) {
79 dev_dbg(&dev->pdev->dev, "failed to find iamthif client.\n");
80 return;
81 }
82
83 /* Assign iamthif_mtu to the value received from ME */
84
85 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
86 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
87 dev->me_clients[i].props.max_msg_length);
88
89 kfree(dev->iamthif_msg_buf);
90 dev->iamthif_msg_buf = NULL;
91
92 /* allocate storage for ME message buffer */
93 msg_buf = kcalloc(dev->iamthif_mtu,
94 sizeof(unsigned char), GFP_KERNEL);
95 if (!msg_buf) {
96 dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
97 return;
98 }
99
100 dev->iamthif_msg_buf = msg_buf;
101
102 if (mei_connect(dev, &dev->iamthif_cl)) {
103 dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
104 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
105 dev->iamthif_cl.host_client_id = 0;
106 } else {
107 dev->iamthif_cl.timer_count = MEI_CONNECT_TIMEOUT;
108 }
109}
110
111/**
112 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
113 *
114 * @dev: the device structure
115 * @file: pointer to file object
116 *
117 * returns returned a list entry on success, NULL on failure.
118 */
119struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
120 struct file *file)
121{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200122 struct mei_cl_cb *pos = NULL;
123 struct mei_cl_cb *next = NULL;
124
125 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200126 &dev->amthif_rd_complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200127 if (pos->cl && pos->cl == &dev->iamthif_cl &&
Tomas Winkler19838fb2012-11-01 21:17:15 +0200128 pos->file_object == file)
129 return pos;
130 }
131 return NULL;
132}
133
134
135/**
136 * mei_amthif_read - read data from AMTHIF client
137 *
138 * @dev: the device structure
139 * @if_num: minor number
140 * @file: pointer to file object
141 * @*ubuf: pointer to user data in user space
142 * @length: data length to read
143 * @offset: data read offset
144 *
145 * Locking: called under "dev->device_lock" lock
146 *
147 * returns
148 * returned data length on success,
149 * zero if no data to read,
150 * negative on failure.
151 */
152int mei_amthif_read(struct mei_device *dev, struct file *file,
153 char __user *ubuf, size_t length, loff_t *offset)
154{
155 int rets;
156 int wait_ret;
157 struct mei_cl_cb *cb = NULL;
158 struct mei_cl *cl = file->private_data;
159 unsigned long timeout;
160 int i;
161
162 /* Only Posible if we are in timeout */
163 if (!cl || cl != &dev->iamthif_cl) {
164 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
165 return -ETIMEDOUT;
166 }
167
168 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
169
170 if (i < 0) {
171 dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
172 return -ENODEV;
173 }
174 dev_dbg(&dev->pdev->dev, "checking amthi data\n");
175 cb = mei_amthif_find_read_list_entry(dev, file);
176
177 /* Check for if we can block or not*/
178 if (cb == NULL && file->f_flags & O_NONBLOCK)
179 return -EAGAIN;
180
181
182 dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
183 while (cb == NULL) {
184 /* unlock the Mutex */
185 mutex_unlock(&dev->device_lock);
186
187 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
188 (cb = mei_amthif_find_read_list_entry(dev, file)));
189
190 if (wait_ret)
191 return -ERESTARTSYS;
192
193 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
194
195 /* Locking again the Mutex */
196 mutex_lock(&dev->device_lock);
197 }
198
199
200 dev_dbg(&dev->pdev->dev, "Got amthi data\n");
201 dev->iamthif_timer = 0;
202
203 if (cb) {
204 timeout = cb->read_time +
205 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
206 dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
207 timeout);
208
209 if (time_after(jiffies, timeout)) {
210 dev_dbg(&dev->pdev->dev, "amthi Time out\n");
211 /* 15 sec for the message has expired */
212 list_del(&cb->list);
213 rets = -ETIMEDOUT;
214 goto free;
215 }
216 }
217 /* if the whole message will fit remove it from the list */
218 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
219 list_del(&cb->list);
220 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
221 /* end of the message has been reached */
222 list_del(&cb->list);
223 rets = 0;
224 goto free;
225 }
226 /* else means that not full buffer will be read and do not
227 * remove message from deletion list
228 */
229
230 dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
231 cb->response_buffer.size);
232 dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
233
234 /* length is being turncated to PAGE_SIZE, however,
235 * the buf_idx may point beyond */
236 length = min_t(size_t, length, (cb->buf_idx - *offset));
237
238 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
239 rets = -EFAULT;
240 else {
241 rets = length;
242 if ((*offset + length) < cb->buf_idx) {
243 *offset += length;
244 goto out;
245 }
246 }
247free:
248 dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
249 *offset = 0;
250 mei_io_cb_free(cb);
251out:
252 return rets;
253}
254
255/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200256 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200257 *
258 * @dev: the device structure
259 * @cb: mei call back struct
260 *
261 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200262 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200263 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200264static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200265{
266 struct mei_msg_hdr mei_hdr;
267 int ret;
268
269 if (!dev || !cb)
270 return -ENODEV;
271
272 dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
273
274 dev->iamthif_state = MEI_IAMTHIF_WRITING;
275 dev->iamthif_current_cb = cb;
276 dev->iamthif_file_object = cb->file_object;
277 dev->iamthif_canceled = false;
278 dev->iamthif_ioctl = true;
279 dev->iamthif_msg_buf_size = cb->request_buffer.size;
280 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
281 cb->request_buffer.size);
282
283 ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
284 if (ret < 0)
285 return ret;
286
287 if (ret && dev->mei_host_buffer_is_empty) {
288 ret = 0;
289 dev->mei_host_buffer_is_empty = false;
290 if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
291 mei_hdr.length = mei_hbuf_max_data(dev);
292 mei_hdr.msg_complete = 0;
293 } else {
294 mei_hdr.length = cb->request_buffer.size;
295 mei_hdr.msg_complete = 1;
296 }
297
298 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
299 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
300 mei_hdr.reserved = 0;
301 dev->iamthif_msg_buf_index += mei_hdr.length;
302 if (mei_write_message(dev, &mei_hdr,
303 (unsigned char *)(dev->iamthif_msg_buf),
304 mei_hdr.length))
305 return -ENODEV;
306
307 if (mei_hdr.msg_complete) {
308 if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
309 return -ENODEV;
310 dev->iamthif_flow_control_pending = true;
311 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
312 dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
313 dev->iamthif_current_cb = cb;
314 dev->iamthif_file_object = cb->file_object;
315 list_add_tail(&cb->list, &dev->write_waiting_list.list);
316 } else {
317 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
318 list_add_tail(&cb->list, &dev->write_list.list);
319 }
320 } else {
321 if (!(dev->mei_host_buffer_is_empty))
322 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
323
324 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
325 list_add_tail(&cb->list, &dev->write_list.list);
326 }
327 return 0;
328}
329
330/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200331 * mei_amthif_write - write amthif data to amthif client
332 *
333 * @dev: the device structure
334 * @cb: mei call back struct
335 *
336 * returns 0 on success, <0 on failure.
337 *
338 */
339int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
340{
341 int ret;
342
343 if (!dev || !cb)
344 return -ENODEV;
345
346 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
347 if (ret)
348 return ret;
349
350 cb->major_file_operations = MEI_IOCTL;
351
Tomas Winklere773efc2012-11-11 17:37:58 +0200352 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200353 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
354 dev_dbg(&dev->pdev->dev,
355 "amthif state = %d\n", dev->iamthif_state);
356 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200357 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200358 return 0;
359 }
360 return mei_amthif_send_cmd(dev, cb);
361}
362/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200363 * mei_amthif_run_next_cmd
364 *
365 * @dev: the device structure
366 *
367 * returns 0 on success, <0 on failure.
368 */
369void mei_amthif_run_next_cmd(struct mei_device *dev)
370{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200371 struct mei_cl_cb *pos = NULL;
372 struct mei_cl_cb *next = NULL;
373 int status;
374
375 if (!dev)
376 return;
377
378 dev->iamthif_msg_buf_size = 0;
379 dev->iamthif_msg_buf_index = 0;
380 dev->iamthif_canceled = false;
381 dev->iamthif_ioctl = true;
382 dev->iamthif_state = MEI_IAMTHIF_IDLE;
383 dev->iamthif_timer = 0;
384 dev->iamthif_file_object = NULL;
385
386 dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
387
Tomas Winklere773efc2012-11-11 17:37:58 +0200388 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200389 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200390
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200391 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200392 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200393 if (status) {
394 dev_dbg(&dev->pdev->dev,
395 "amthi write failed status = %d\n",
396 status);
397 return;
398 }
399 break;
400 }
401 }
402}
403
404/**
405 * mei_amthif_irq_process_completed - processes completed iamthif operation.
406 *
407 * @dev: the device structure.
408 * @slots: free slots.
409 * @cb_pos: callback block.
410 * @cl: private data of the file object.
411 * @cmpl_list: complete list.
412 *
413 * returns 0, OK; otherwise, error.
414 */
415int mei_amthif_irq_process_completed(struct mei_device *dev, s32 *slots,
416 struct mei_cl_cb *cb_pos,
417 struct mei_cl *cl,
418 struct mei_cl_cb *cmpl_list)
419{
420 struct mei_msg_hdr *mei_hdr;
421
422 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
423 dev->iamthif_msg_buf_size -
424 dev->iamthif_msg_buf_index)) {
425 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
426 mei_hdr->host_addr = cl->host_client_id;
427 mei_hdr->me_addr = cl->me_client_id;
428 mei_hdr->length = dev->iamthif_msg_buf_size -
429 dev->iamthif_msg_buf_index;
430 mei_hdr->msg_complete = 1;
431 mei_hdr->reserved = 0;
432
433 *slots -= mei_data2slots(mei_hdr->length);
434
435 if (mei_write_message(dev, mei_hdr,
436 (dev->iamthif_msg_buf +
437 dev->iamthif_msg_buf_index),
438 mei_hdr->length)) {
439 dev->iamthif_state = MEI_IAMTHIF_IDLE;
440 cl->status = -ENODEV;
441 list_del(&cb_pos->list);
442 return -ENODEV;
443 } else {
444 if (mei_flow_ctrl_reduce(dev, cl))
445 return -ENODEV;
446 dev->iamthif_msg_buf_index += mei_hdr->length;
447 cb_pos->buf_idx = dev->iamthif_msg_buf_index;
448 cl->status = 0;
449 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
450 dev->iamthif_flow_control_pending = true;
451 /* save iamthif cb sent to amthi client */
452 dev->iamthif_current_cb = cb_pos;
453 list_move_tail(&cb_pos->list,
454 &dev->write_waiting_list.list);
455
456 }
457 } else if (*slots == dev->hbuf_depth) {
458 /* buffer is still empty */
459 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
460 mei_hdr->host_addr = cl->host_client_id;
461 mei_hdr->me_addr = cl->me_client_id;
462 mei_hdr->length =
463 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
464 mei_hdr->msg_complete = 0;
465 mei_hdr->reserved = 0;
466
467 *slots -= mei_data2slots(mei_hdr->length);
468
469 if (mei_write_message(dev, mei_hdr,
470 (dev->iamthif_msg_buf +
471 dev->iamthif_msg_buf_index),
472 mei_hdr->length)) {
473 cl->status = -ENODEV;
474 list_del(&cb_pos->list);
475 } else {
476 dev->iamthif_msg_buf_index += mei_hdr->length;
477 }
478 return -EMSGSIZE;
479 } else {
480 return -EBADMSG;
481 }
482
483 return 0;
484}
485
486/**
487 * mei_amthif_irq_read_message - read routine after ISR to
488 * handle the read amthi message
489 *
490 * @complete_list: An instance of our list structure
491 * @dev: the device structure
492 * @mei_hdr: header of amthi message
493 *
494 * returns 0 on success, <0 on failure.
495 */
496int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
497 struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
498{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200499 struct mei_cl_cb *cb;
500 unsigned char *buffer;
501
502 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
503 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
504
505 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
506 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
507
508 mei_read_slots(dev, buffer, mei_hdr->length);
509
510 dev->iamthif_msg_buf_index += mei_hdr->length;
511
512 if (!mei_hdr->msg_complete)
513 return 0;
514
515 dev_dbg(&dev->pdev->dev,
516 "amthi_message_buffer_index =%d\n",
517 mei_hdr->length);
518
519 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
520 if (!dev->iamthif_current_cb)
521 return -ENODEV;
522
523 cb = dev->iamthif_current_cb;
524 dev->iamthif_current_cb = NULL;
525
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200526 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200527 return -ENODEV;
528
529 dev->iamthif_stall_timer = 0;
530 cb->buf_idx = dev->iamthif_msg_buf_index;
531 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200532 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200533 /* found the iamthif cb */
534 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
535 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
536 list_add_tail(&cb->list, &complete_list->list);
537 }
538 return 0;
539}
540
541/**
542 * mei_amthif_irq_read - prepares to read amthif data.
543 *
544 * @dev: the device structure.
545 * @slots: free slots.
546 *
547 * returns 0, OK; otherwise, error.
548 */
549int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
550{
551
552 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
553 + sizeof(struct hbm_flow_control))) {
554 return -EMSGSIZE;
555 }
556 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
557 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
558 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
559 return -EIO;
560 }
561
562 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
563 dev->iamthif_state = MEI_IAMTHIF_READING;
564 dev->iamthif_flow_control_pending = false;
565 dev->iamthif_msg_buf_index = 0;
566 dev->iamthif_msg_buf_size = 0;
567 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
568 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
569 return 0;
570}
571
572/**
573 * mei_amthif_complete - complete amthif callback.
574 *
575 * @dev: the device structure.
576 * @cb_pos: callback block.
577 */
578void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
579{
580 if (dev->iamthif_canceled != 1) {
581 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
582 dev->iamthif_stall_timer = 0;
583 memcpy(cb->response_buffer.data,
584 dev->iamthif_msg_buf,
585 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200586 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200587 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
588 dev->iamthif_timer = jiffies;
589 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
590 dev->iamthif_timer);
591 } else {
592 mei_amthif_run_next_cmd(dev);
593 }
594
595 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
596 wake_up_interruptible(&dev->iamthif_cl.wait);
597}
598
599