blob: 1de28df94da45757278690d851e2c16f44619d2d [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{
122 struct mei_cl *cl_temp;
123 struct mei_cl_cb *pos = NULL;
124 struct mei_cl_cb *next = NULL;
125
126 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200127 &dev->amthif_rd_complete_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200128 cl_temp = (struct mei_cl *)pos->file_private;
129 if (cl_temp && cl_temp == &dev->iamthif_cl &&
130 pos->file_object == file)
131 return pos;
132 }
133 return NULL;
134}
135
136
137/**
138 * mei_amthif_read - read data from AMTHIF client
139 *
140 * @dev: the device structure
141 * @if_num: minor number
142 * @file: pointer to file object
143 * @*ubuf: pointer to user data in user space
144 * @length: data length to read
145 * @offset: data read offset
146 *
147 * Locking: called under "dev->device_lock" lock
148 *
149 * returns
150 * returned data length on success,
151 * zero if no data to read,
152 * negative on failure.
153 */
154int mei_amthif_read(struct mei_device *dev, struct file *file,
155 char __user *ubuf, size_t length, loff_t *offset)
156{
157 int rets;
158 int wait_ret;
159 struct mei_cl_cb *cb = NULL;
160 struct mei_cl *cl = file->private_data;
161 unsigned long timeout;
162 int i;
163
164 /* Only Posible if we are in timeout */
165 if (!cl || cl != &dev->iamthif_cl) {
166 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
167 return -ETIMEDOUT;
168 }
169
170 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
171
172 if (i < 0) {
173 dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
174 return -ENODEV;
175 }
176 dev_dbg(&dev->pdev->dev, "checking amthi data\n");
177 cb = mei_amthif_find_read_list_entry(dev, file);
178
179 /* Check for if we can block or not*/
180 if (cb == NULL && file->f_flags & O_NONBLOCK)
181 return -EAGAIN;
182
183
184 dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
185 while (cb == NULL) {
186 /* unlock the Mutex */
187 mutex_unlock(&dev->device_lock);
188
189 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
190 (cb = mei_amthif_find_read_list_entry(dev, file)));
191
192 if (wait_ret)
193 return -ERESTARTSYS;
194
195 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
196
197 /* Locking again the Mutex */
198 mutex_lock(&dev->device_lock);
199 }
200
201
202 dev_dbg(&dev->pdev->dev, "Got amthi data\n");
203 dev->iamthif_timer = 0;
204
205 if (cb) {
206 timeout = cb->read_time +
207 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
208 dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
209 timeout);
210
211 if (time_after(jiffies, timeout)) {
212 dev_dbg(&dev->pdev->dev, "amthi Time out\n");
213 /* 15 sec for the message has expired */
214 list_del(&cb->list);
215 rets = -ETIMEDOUT;
216 goto free;
217 }
218 }
219 /* if the whole message will fit remove it from the list */
220 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
221 list_del(&cb->list);
222 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
223 /* end of the message has been reached */
224 list_del(&cb->list);
225 rets = 0;
226 goto free;
227 }
228 /* else means that not full buffer will be read and do not
229 * remove message from deletion list
230 */
231
232 dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
233 cb->response_buffer.size);
234 dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
235
236 /* length is being turncated to PAGE_SIZE, however,
237 * the buf_idx may point beyond */
238 length = min_t(size_t, length, (cb->buf_idx - *offset));
239
240 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
241 rets = -EFAULT;
242 else {
243 rets = length;
244 if ((*offset + length) < cb->buf_idx) {
245 *offset += length;
246 goto out;
247 }
248 }
249free:
250 dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
251 *offset = 0;
252 mei_io_cb_free(cb);
253out:
254 return rets;
255}
256
257/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200258 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200259 *
260 * @dev: the device structure
261 * @cb: mei call back struct
262 *
263 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200264 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200265 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200266static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200267{
268 struct mei_msg_hdr mei_hdr;
269 int ret;
270
271 if (!dev || !cb)
272 return -ENODEV;
273
274 dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
275
276 dev->iamthif_state = MEI_IAMTHIF_WRITING;
277 dev->iamthif_current_cb = cb;
278 dev->iamthif_file_object = cb->file_object;
279 dev->iamthif_canceled = false;
280 dev->iamthif_ioctl = true;
281 dev->iamthif_msg_buf_size = cb->request_buffer.size;
282 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
283 cb->request_buffer.size);
284
285 ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
286 if (ret < 0)
287 return ret;
288
289 if (ret && dev->mei_host_buffer_is_empty) {
290 ret = 0;
291 dev->mei_host_buffer_is_empty = false;
292 if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
293 mei_hdr.length = mei_hbuf_max_data(dev);
294 mei_hdr.msg_complete = 0;
295 } else {
296 mei_hdr.length = cb->request_buffer.size;
297 mei_hdr.msg_complete = 1;
298 }
299
300 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
301 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
302 mei_hdr.reserved = 0;
303 dev->iamthif_msg_buf_index += mei_hdr.length;
304 if (mei_write_message(dev, &mei_hdr,
305 (unsigned char *)(dev->iamthif_msg_buf),
306 mei_hdr.length))
307 return -ENODEV;
308
309 if (mei_hdr.msg_complete) {
310 if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
311 return -ENODEV;
312 dev->iamthif_flow_control_pending = true;
313 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
314 dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
315 dev->iamthif_current_cb = cb;
316 dev->iamthif_file_object = cb->file_object;
317 list_add_tail(&cb->list, &dev->write_waiting_list.list);
318 } else {
319 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
320 list_add_tail(&cb->list, &dev->write_list.list);
321 }
322 } else {
323 if (!(dev->mei_host_buffer_is_empty))
324 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
325
326 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
327 list_add_tail(&cb->list, &dev->write_list.list);
328 }
329 return 0;
330}
331
332/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200333 * mei_amthif_write - write amthif data to amthif client
334 *
335 * @dev: the device structure
336 * @cb: mei call back struct
337 *
338 * returns 0 on success, <0 on failure.
339 *
340 */
341int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
342{
343 int ret;
344
345 if (!dev || !cb)
346 return -ENODEV;
347
348 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
349 if (ret)
350 return ret;
351
352 cb->major_file_operations = MEI_IOCTL;
353
Tomas Winklere773efc2012-11-11 17:37:58 +0200354 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200355 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
356 dev_dbg(&dev->pdev->dev,
357 "amthif state = %d\n", dev->iamthif_state);
358 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200359 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200360 return 0;
361 }
362 return mei_amthif_send_cmd(dev, cb);
363}
364/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200365 * mei_amthif_run_next_cmd
366 *
367 * @dev: the device structure
368 *
369 * returns 0 on success, <0 on failure.
370 */
371void mei_amthif_run_next_cmd(struct mei_device *dev)
372{
373 struct mei_cl *cl_tmp;
374 struct mei_cl_cb *pos = NULL;
375 struct mei_cl_cb *next = NULL;
376 int status;
377
378 if (!dev)
379 return;
380
381 dev->iamthif_msg_buf_size = 0;
382 dev->iamthif_msg_buf_index = 0;
383 dev->iamthif_canceled = false;
384 dev->iamthif_ioctl = true;
385 dev->iamthif_state = MEI_IAMTHIF_IDLE;
386 dev->iamthif_timer = 0;
387 dev->iamthif_file_object = NULL;
388
389 dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
390
Tomas Winklere773efc2012-11-11 17:37:58 +0200391 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200392 list_del(&pos->list);
393 cl_tmp = (struct mei_cl *)pos->file_private;
394
395 if (cl_tmp && cl_tmp == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200396 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200397 if (status) {
398 dev_dbg(&dev->pdev->dev,
399 "amthi write failed status = %d\n",
400 status);
401 return;
402 }
403 break;
404 }
405 }
406}
407
408/**
409 * mei_amthif_irq_process_completed - processes completed iamthif operation.
410 *
411 * @dev: the device structure.
412 * @slots: free slots.
413 * @cb_pos: callback block.
414 * @cl: private data of the file object.
415 * @cmpl_list: complete list.
416 *
417 * returns 0, OK; otherwise, error.
418 */
419int mei_amthif_irq_process_completed(struct mei_device *dev, s32 *slots,
420 struct mei_cl_cb *cb_pos,
421 struct mei_cl *cl,
422 struct mei_cl_cb *cmpl_list)
423{
424 struct mei_msg_hdr *mei_hdr;
425
426 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
427 dev->iamthif_msg_buf_size -
428 dev->iamthif_msg_buf_index)) {
429 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
430 mei_hdr->host_addr = cl->host_client_id;
431 mei_hdr->me_addr = cl->me_client_id;
432 mei_hdr->length = dev->iamthif_msg_buf_size -
433 dev->iamthif_msg_buf_index;
434 mei_hdr->msg_complete = 1;
435 mei_hdr->reserved = 0;
436
437 *slots -= mei_data2slots(mei_hdr->length);
438
439 if (mei_write_message(dev, mei_hdr,
440 (dev->iamthif_msg_buf +
441 dev->iamthif_msg_buf_index),
442 mei_hdr->length)) {
443 dev->iamthif_state = MEI_IAMTHIF_IDLE;
444 cl->status = -ENODEV;
445 list_del(&cb_pos->list);
446 return -ENODEV;
447 } else {
448 if (mei_flow_ctrl_reduce(dev, cl))
449 return -ENODEV;
450 dev->iamthif_msg_buf_index += mei_hdr->length;
451 cb_pos->buf_idx = dev->iamthif_msg_buf_index;
452 cl->status = 0;
453 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
454 dev->iamthif_flow_control_pending = true;
455 /* save iamthif cb sent to amthi client */
456 dev->iamthif_current_cb = cb_pos;
457 list_move_tail(&cb_pos->list,
458 &dev->write_waiting_list.list);
459
460 }
461 } else if (*slots == dev->hbuf_depth) {
462 /* buffer is still empty */
463 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
464 mei_hdr->host_addr = cl->host_client_id;
465 mei_hdr->me_addr = cl->me_client_id;
466 mei_hdr->length =
467 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
468 mei_hdr->msg_complete = 0;
469 mei_hdr->reserved = 0;
470
471 *slots -= mei_data2slots(mei_hdr->length);
472
473 if (mei_write_message(dev, mei_hdr,
474 (dev->iamthif_msg_buf +
475 dev->iamthif_msg_buf_index),
476 mei_hdr->length)) {
477 cl->status = -ENODEV;
478 list_del(&cb_pos->list);
479 } else {
480 dev->iamthif_msg_buf_index += mei_hdr->length;
481 }
482 return -EMSGSIZE;
483 } else {
484 return -EBADMSG;
485 }
486
487 return 0;
488}
489
490/**
491 * mei_amthif_irq_read_message - read routine after ISR to
492 * handle the read amthi message
493 *
494 * @complete_list: An instance of our list structure
495 * @dev: the device structure
496 * @mei_hdr: header of amthi message
497 *
498 * returns 0 on success, <0 on failure.
499 */
500int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
501 struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
502{
503 struct mei_cl *cl;
504 struct mei_cl_cb *cb;
505 unsigned char *buffer;
506
507 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
508 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
509
510 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
511 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
512
513 mei_read_slots(dev, buffer, mei_hdr->length);
514
515 dev->iamthif_msg_buf_index += mei_hdr->length;
516
517 if (!mei_hdr->msg_complete)
518 return 0;
519
520 dev_dbg(&dev->pdev->dev,
521 "amthi_message_buffer_index =%d\n",
522 mei_hdr->length);
523
524 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
525 if (!dev->iamthif_current_cb)
526 return -ENODEV;
527
528 cb = dev->iamthif_current_cb;
529 dev->iamthif_current_cb = NULL;
530
531 cl = (struct mei_cl *)cb->file_private;
532 if (!cl)
533 return -ENODEV;
534
535 dev->iamthif_stall_timer = 0;
536 cb->buf_idx = dev->iamthif_msg_buf_index;
537 cb->read_time = jiffies;
538 if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
539 /* found the iamthif cb */
540 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
541 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
542 list_add_tail(&cb->list, &complete_list->list);
543 }
544 return 0;
545}
546
547/**
548 * mei_amthif_irq_read - prepares to read amthif data.
549 *
550 * @dev: the device structure.
551 * @slots: free slots.
552 *
553 * returns 0, OK; otherwise, error.
554 */
555int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
556{
557
558 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
559 + sizeof(struct hbm_flow_control))) {
560 return -EMSGSIZE;
561 }
562 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
563 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
564 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
565 return -EIO;
566 }
567
568 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
569 dev->iamthif_state = MEI_IAMTHIF_READING;
570 dev->iamthif_flow_control_pending = false;
571 dev->iamthif_msg_buf_index = 0;
572 dev->iamthif_msg_buf_size = 0;
573 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
574 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
575 return 0;
576}
577
578/**
579 * mei_amthif_complete - complete amthif callback.
580 *
581 * @dev: the device structure.
582 * @cb_pos: callback block.
583 */
584void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
585{
586 if (dev->iamthif_canceled != 1) {
587 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
588 dev->iamthif_stall_timer = 0;
589 memcpy(cb->response_buffer.data,
590 dev->iamthif_msg_buf,
591 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200592 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200593 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
594 dev->iamthif_timer = jiffies;
595 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
596 dev->iamthif_timer);
597 } else {
598 mei_amthif_run_next_cmd(dev);
599 }
600
601 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
602 wake_up_interruptible(&dev->iamthif_cl.wait);
603}
604
605