blob: 392203d8d254006be16071290895d9e3d048ea15 [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,
127 &dev->amthi_read_complete_list.list, list) {
128 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/**
258 * mei_amthif_write - write amthif data to amthif client
259 *
260 * @dev: the device structure
261 * @cb: mei call back struct
262 *
263 * returns 0 on success, <0 on failure.
264 */
265int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
266{
267 struct mei_msg_hdr mei_hdr;
268 int ret;
269
270 if (!dev || !cb)
271 return -ENODEV;
272
273 dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
274
275 dev->iamthif_state = MEI_IAMTHIF_WRITING;
276 dev->iamthif_current_cb = cb;
277 dev->iamthif_file_object = cb->file_object;
278 dev->iamthif_canceled = false;
279 dev->iamthif_ioctl = true;
280 dev->iamthif_msg_buf_size = cb->request_buffer.size;
281 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
282 cb->request_buffer.size);
283
284 ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
285 if (ret < 0)
286 return ret;
287
288 if (ret && dev->mei_host_buffer_is_empty) {
289 ret = 0;
290 dev->mei_host_buffer_is_empty = false;
291 if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
292 mei_hdr.length = mei_hbuf_max_data(dev);
293 mei_hdr.msg_complete = 0;
294 } else {
295 mei_hdr.length = cb->request_buffer.size;
296 mei_hdr.msg_complete = 1;
297 }
298
299 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
300 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
301 mei_hdr.reserved = 0;
302 dev->iamthif_msg_buf_index += mei_hdr.length;
303 if (mei_write_message(dev, &mei_hdr,
304 (unsigned char *)(dev->iamthif_msg_buf),
305 mei_hdr.length))
306 return -ENODEV;
307
308 if (mei_hdr.msg_complete) {
309 if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
310 return -ENODEV;
311 dev->iamthif_flow_control_pending = true;
312 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
313 dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
314 dev->iamthif_current_cb = cb;
315 dev->iamthif_file_object = cb->file_object;
316 list_add_tail(&cb->list, &dev->write_waiting_list.list);
317 } else {
318 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
319 list_add_tail(&cb->list, &dev->write_list.list);
320 }
321 } else {
322 if (!(dev->mei_host_buffer_is_empty))
323 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
324
325 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
326 list_add_tail(&cb->list, &dev->write_list.list);
327 }
328 return 0;
329}
330
331/**
332 * mei_amthif_run_next_cmd
333 *
334 * @dev: the device structure
335 *
336 * returns 0 on success, <0 on failure.
337 */
338void mei_amthif_run_next_cmd(struct mei_device *dev)
339{
340 struct mei_cl *cl_tmp;
341 struct mei_cl_cb *pos = NULL;
342 struct mei_cl_cb *next = NULL;
343 int status;
344
345 if (!dev)
346 return;
347
348 dev->iamthif_msg_buf_size = 0;
349 dev->iamthif_msg_buf_index = 0;
350 dev->iamthif_canceled = false;
351 dev->iamthif_ioctl = true;
352 dev->iamthif_state = MEI_IAMTHIF_IDLE;
353 dev->iamthif_timer = 0;
354 dev->iamthif_file_object = NULL;
355
356 dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
357
358 list_for_each_entry_safe(pos, next, &dev->amthi_cmd_list.list, list) {
359 list_del(&pos->list);
360 cl_tmp = (struct mei_cl *)pos->file_private;
361
362 if (cl_tmp && cl_tmp == &dev->iamthif_cl) {
363 status = mei_amthif_write(dev, pos);
364 if (status) {
365 dev_dbg(&dev->pdev->dev,
366 "amthi write failed status = %d\n",
367 status);
368 return;
369 }
370 break;
371 }
372 }
373}
374
375/**
376 * mei_amthif_irq_process_completed - processes completed iamthif operation.
377 *
378 * @dev: the device structure.
379 * @slots: free slots.
380 * @cb_pos: callback block.
381 * @cl: private data of the file object.
382 * @cmpl_list: complete list.
383 *
384 * returns 0, OK; otherwise, error.
385 */
386int mei_amthif_irq_process_completed(struct mei_device *dev, s32 *slots,
387 struct mei_cl_cb *cb_pos,
388 struct mei_cl *cl,
389 struct mei_cl_cb *cmpl_list)
390{
391 struct mei_msg_hdr *mei_hdr;
392
393 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
394 dev->iamthif_msg_buf_size -
395 dev->iamthif_msg_buf_index)) {
396 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
397 mei_hdr->host_addr = cl->host_client_id;
398 mei_hdr->me_addr = cl->me_client_id;
399 mei_hdr->length = dev->iamthif_msg_buf_size -
400 dev->iamthif_msg_buf_index;
401 mei_hdr->msg_complete = 1;
402 mei_hdr->reserved = 0;
403
404 *slots -= mei_data2slots(mei_hdr->length);
405
406 if (mei_write_message(dev, mei_hdr,
407 (dev->iamthif_msg_buf +
408 dev->iamthif_msg_buf_index),
409 mei_hdr->length)) {
410 dev->iamthif_state = MEI_IAMTHIF_IDLE;
411 cl->status = -ENODEV;
412 list_del(&cb_pos->list);
413 return -ENODEV;
414 } else {
415 if (mei_flow_ctrl_reduce(dev, cl))
416 return -ENODEV;
417 dev->iamthif_msg_buf_index += mei_hdr->length;
418 cb_pos->buf_idx = dev->iamthif_msg_buf_index;
419 cl->status = 0;
420 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
421 dev->iamthif_flow_control_pending = true;
422 /* save iamthif cb sent to amthi client */
423 dev->iamthif_current_cb = cb_pos;
424 list_move_tail(&cb_pos->list,
425 &dev->write_waiting_list.list);
426
427 }
428 } else if (*slots == dev->hbuf_depth) {
429 /* buffer is still empty */
430 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
431 mei_hdr->host_addr = cl->host_client_id;
432 mei_hdr->me_addr = cl->me_client_id;
433 mei_hdr->length =
434 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
435 mei_hdr->msg_complete = 0;
436 mei_hdr->reserved = 0;
437
438 *slots -= mei_data2slots(mei_hdr->length);
439
440 if (mei_write_message(dev, mei_hdr,
441 (dev->iamthif_msg_buf +
442 dev->iamthif_msg_buf_index),
443 mei_hdr->length)) {
444 cl->status = -ENODEV;
445 list_del(&cb_pos->list);
446 } else {
447 dev->iamthif_msg_buf_index += mei_hdr->length;
448 }
449 return -EMSGSIZE;
450 } else {
451 return -EBADMSG;
452 }
453
454 return 0;
455}
456
457/**
458 * mei_amthif_irq_read_message - read routine after ISR to
459 * handle the read amthi message
460 *
461 * @complete_list: An instance of our list structure
462 * @dev: the device structure
463 * @mei_hdr: header of amthi message
464 *
465 * returns 0 on success, <0 on failure.
466 */
467int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
468 struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
469{
470 struct mei_cl *cl;
471 struct mei_cl_cb *cb;
472 unsigned char *buffer;
473
474 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
475 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
476
477 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
478 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
479
480 mei_read_slots(dev, buffer, mei_hdr->length);
481
482 dev->iamthif_msg_buf_index += mei_hdr->length;
483
484 if (!mei_hdr->msg_complete)
485 return 0;
486
487 dev_dbg(&dev->pdev->dev,
488 "amthi_message_buffer_index =%d\n",
489 mei_hdr->length);
490
491 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
492 if (!dev->iamthif_current_cb)
493 return -ENODEV;
494
495 cb = dev->iamthif_current_cb;
496 dev->iamthif_current_cb = NULL;
497
498 cl = (struct mei_cl *)cb->file_private;
499 if (!cl)
500 return -ENODEV;
501
502 dev->iamthif_stall_timer = 0;
503 cb->buf_idx = dev->iamthif_msg_buf_index;
504 cb->read_time = jiffies;
505 if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
506 /* found the iamthif cb */
507 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
508 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
509 list_add_tail(&cb->list, &complete_list->list);
510 }
511 return 0;
512}
513
514/**
515 * mei_amthif_irq_read - prepares to read amthif data.
516 *
517 * @dev: the device structure.
518 * @slots: free slots.
519 *
520 * returns 0, OK; otherwise, error.
521 */
522int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
523{
524
525 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
526 + sizeof(struct hbm_flow_control))) {
527 return -EMSGSIZE;
528 }
529 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
530 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
531 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
532 return -EIO;
533 }
534
535 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
536 dev->iamthif_state = MEI_IAMTHIF_READING;
537 dev->iamthif_flow_control_pending = false;
538 dev->iamthif_msg_buf_index = 0;
539 dev->iamthif_msg_buf_size = 0;
540 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
541 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
542 return 0;
543}
544
545/**
546 * mei_amthif_complete - complete amthif callback.
547 *
548 * @dev: the device structure.
549 * @cb_pos: callback block.
550 */
551void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
552{
553 if (dev->iamthif_canceled != 1) {
554 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
555 dev->iamthif_stall_timer = 0;
556 memcpy(cb->response_buffer.data,
557 dev->iamthif_msg_buf,
558 dev->iamthif_msg_buf_index);
559 list_add_tail(&cb->list, &dev->amthi_read_complete_list.list);
560 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
561 dev->iamthif_timer = jiffies;
562 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
563 dev->iamthif_timer);
564 } else {
565 mei_amthif_run_next_cmd(dev);
566 }
567
568 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
569 wake_up_interruptible(&dev->iamthif_cl.wait);
570}
571
572