3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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
17 #include <linux/kernel.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>
37 #include <linux/mei.h>
38 #include "interface.h"
40 const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
41 0xa8, 0x46, 0xe0, 0xff, 0x65,
45 * mei_amthif_reset_params - initializes mei device iamthif
47 * @dev: the device structure
49 void mei_amthif_reset_params(struct mei_device *dev)
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;
62 * mei_amthif_host_init_ - mei initialization amthif client.
64 * @dev: the device structure
67 void mei_amthif_host_init(struct mei_device *dev)
70 unsigned char *msg_buf;
72 mei_cl_init(&dev->iamthif_cl, dev);
73 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
75 /* find ME amthi client */
76 i = mei_me_cl_link(dev, &dev->iamthif_cl,
77 &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
79 dev_info(&dev->pdev->dev, "failed to find iamthif client.\n");
83 /* Assign iamthif_mtu to the value received from ME */
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);
89 kfree(dev->iamthif_msg_buf);
90 dev->iamthif_msg_buf = NULL;
92 /* allocate storage for ME message buffer */
93 msg_buf = kcalloc(dev->iamthif_mtu,
94 sizeof(unsigned char), GFP_KERNEL);
96 dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
100 dev->iamthif_msg_buf = msg_buf;
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;
107 dev->iamthif_cl.timer_count = MEI_CONNECT_TIMEOUT;
112 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
114 * @dev: the device structure
115 * @file: pointer to file object
117 * returns returned a list entry on success, NULL on failure.
119 struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
122 struct mei_cl_cb *pos = NULL;
123 struct mei_cl_cb *next = NULL;
125 list_for_each_entry_safe(pos, next,
126 &dev->amthif_rd_complete_list.list, list) {
127 if (pos->cl && pos->cl == &dev->iamthif_cl &&
128 pos->file_object == file)
136 * mei_amthif_read - read data from AMTHIF client
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
145 * Locking: called under "dev->device_lock" lock
148 * returned data length on success,
149 * zero if no data to read,
150 * negative on failure.
152 int mei_amthif_read(struct mei_device *dev, struct file *file,
153 char __user *ubuf, size_t length, loff_t *offset)
157 struct mei_cl_cb *cb = NULL;
158 struct mei_cl *cl = file->private_data;
159 unsigned long timeout;
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");
168 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
171 dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
174 dev_dbg(&dev->pdev->dev, "checking amthi data\n");
175 cb = mei_amthif_find_read_list_entry(dev, file);
177 /* Check for if we can block or not*/
178 if (cb == NULL && file->f_flags & O_NONBLOCK)
182 dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
184 /* unlock the Mutex */
185 mutex_unlock(&dev->device_lock);
187 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
188 (cb = mei_amthif_find_read_list_entry(dev, file)));
193 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
195 /* Locking again the Mutex */
196 mutex_lock(&dev->device_lock);
200 dev_dbg(&dev->pdev->dev, "Got amthi data\n");
201 dev->iamthif_timer = 0;
204 timeout = cb->read_time +
205 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
206 dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
209 if (time_after(jiffies, timeout)) {
210 dev_dbg(&dev->pdev->dev, "amthi Time out\n");
211 /* 15 sec for the message has expired */
217 /* if the whole message will fit remove it from the list */
218 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
220 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
221 /* end of the message has been reached */
226 /* else means that not full buffer will be read and do not
227 * remove message from deletion list
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);
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));
238 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
242 if ((*offset + length) < cb->buf_idx) {
248 dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
256 * mei_amthif_send_cmd - send amthif command to the ME
258 * @dev: the device structure
259 * @cb: mei call back struct
261 * returns 0 on success, <0 on failure.
264 static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
266 struct mei_msg_hdr mei_hdr;
272 dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
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);
283 ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
287 if (ret && dev->mei_host_buffer_is_empty) {
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;
294 mei_hdr.length = cb->request_buffer.size;
295 mei_hdr.msg_complete = 1;
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),
307 if (mei_hdr.msg_complete) {
308 if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
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);
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);
321 if (!(dev->mei_host_buffer_is_empty))
322 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
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);
331 * mei_amthif_write - write amthif data to amthif client
333 * @dev: the device structure
334 * @cb: mei call back struct
336 * returns 0 on success, <0 on failure.
339 int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
346 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
350 cb->fop_type = MEI_FOP_IOCTL;
352 if (!list_empty(&dev->amthif_cmd_list.list) ||
353 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");
357 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
360 return mei_amthif_send_cmd(dev, cb);
363 * mei_amthif_run_next_cmd
365 * @dev: the device structure
367 * returns 0 on success, <0 on failure.
369 void mei_amthif_run_next_cmd(struct mei_device *dev)
371 struct mei_cl_cb *pos = NULL;
372 struct mei_cl_cb *next = NULL;
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;
386 dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
388 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
389 list_del(&pos->list);
391 if (pos->cl && pos->cl == &dev->iamthif_cl) {
392 status = mei_amthif_send_cmd(dev, pos);
394 dev_dbg(&dev->pdev->dev,
395 "amthi write failed status = %d\n",
405 unsigned int mei_amthif_poll(struct mei_device *dev,
406 struct file *file, poll_table *wait)
408 unsigned int mask = 0;
409 mutex_unlock(&dev->device_lock);
410 poll_wait(file, &dev->iamthif_cl.wait, wait);
411 mutex_lock(&dev->device_lock);
412 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
413 dev->iamthif_file_object == file) {
414 mask |= (POLLIN | POLLRDNORM);
415 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
416 mei_amthif_run_next_cmd(dev);
424 * mei_amthif_irq_process_completed - processes completed iamthif operation.
426 * @dev: the device structure.
427 * @slots: free slots.
428 * @cb_pos: callback block.
429 * @cl: private data of the file object.
430 * @cmpl_list: complete list.
432 * returns 0, OK; otherwise, error.
434 int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
435 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
437 struct mei_msg_hdr *mei_hdr;
438 struct mei_cl *cl = cb->cl;
439 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
440 size_t msg_slots = mei_data2slots(len);
442 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
443 mei_hdr->host_addr = cl->host_client_id;
444 mei_hdr->me_addr = cl->me_client_id;
445 mei_hdr->reserved = 0;
447 if (*slots >= msg_slots) {
448 mei_hdr->length = len;
449 mei_hdr->msg_complete = 1;
450 /* Split the message only if we can write the whole host buffer */
451 } else if (*slots == dev->hbuf_depth) {
453 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
454 mei_hdr->length = len;
455 mei_hdr->msg_complete = 0;
457 /* wait for next time the host buffer is empty */
461 dev_dbg(&dev->pdev->dev, "msg: len = %d complete = %d\n",
462 mei_hdr->length, mei_hdr->msg_complete);
465 if (mei_write_message(dev, mei_hdr,
466 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index,
468 dev->iamthif_state = MEI_IAMTHIF_IDLE;
469 cl->status = -ENODEV;
474 if (mei_flow_ctrl_reduce(dev, cl))
477 dev->iamthif_msg_buf_index += mei_hdr->length;
480 if (mei_hdr->msg_complete) {
481 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
482 dev->iamthif_flow_control_pending = true;
484 /* save iamthif cb sent to amthi client */
485 cb->buf_idx = dev->iamthif_msg_buf_index;
486 dev->iamthif_current_cb = cb;
488 list_move_tail(&cb->list, &dev->write_waiting_list.list);
496 * mei_amthif_irq_read_message - read routine after ISR to
497 * handle the read amthi message
499 * @complete_list: An instance of our list structure
500 * @dev: the device structure
501 * @mei_hdr: header of amthi message
503 * returns 0 on success, <0 on failure.
505 int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
506 struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
508 struct mei_cl_cb *cb;
509 unsigned char *buffer;
511 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
512 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
514 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
515 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
517 mei_read_slots(dev, buffer, mei_hdr->length);
519 dev->iamthif_msg_buf_index += mei_hdr->length;
521 if (!mei_hdr->msg_complete)
524 dev_dbg(&dev->pdev->dev,
525 "amthi_message_buffer_index =%d\n",
528 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
529 if (!dev->iamthif_current_cb)
532 cb = dev->iamthif_current_cb;
533 dev->iamthif_current_cb = NULL;
538 dev->iamthif_stall_timer = 0;
539 cb->buf_idx = dev->iamthif_msg_buf_index;
540 cb->read_time = jiffies;
541 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
542 /* found the iamthif cb */
543 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
544 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
545 list_add_tail(&cb->list, &complete_list->list);
551 * mei_amthif_irq_read - prepares to read amthif data.
553 * @dev: the device structure.
554 * @slots: free slots.
556 * returns 0, OK; otherwise, error.
558 int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
561 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
562 + sizeof(struct hbm_flow_control))) {
565 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
566 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
567 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
571 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
572 dev->iamthif_state = MEI_IAMTHIF_READING;
573 dev->iamthif_flow_control_pending = false;
574 dev->iamthif_msg_buf_index = 0;
575 dev->iamthif_msg_buf_size = 0;
576 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
577 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
582 * mei_amthif_complete - complete amthif callback.
584 * @dev: the device structure.
585 * @cb_pos: callback block.
587 void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
589 if (dev->iamthif_canceled != 1) {
590 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
591 dev->iamthif_stall_timer = 0;
592 memcpy(cb->response_buffer.data,
593 dev->iamthif_msg_buf,
594 dev->iamthif_msg_buf_index);
595 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
596 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
597 dev->iamthif_timer = jiffies;
598 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
601 mei_amthif_run_next_cmd(dev);
604 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
605 wake_up_interruptible(&dev->iamthif_cl.wait);
609 * mei_clear_list - removes all callbacks associated with file
612 * @dev: device structure.
613 * @file: file structure
614 * @mei_cb_list: callbacks list
616 * mei_clear_list is called to clear resources associated with file
617 * when application calls close function or Ctrl-C was pressed
619 * returns true if callback removed from the list, false otherwise
621 static bool mei_clear_list(struct mei_device *dev,
622 const struct file *file, struct list_head *mei_cb_list)
624 struct mei_cl_cb *cb_pos = NULL;
625 struct mei_cl_cb *cb_next = NULL;
626 bool removed = false;
628 /* list all list member */
629 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
630 /* check if list member associated with a file */
631 if (file == cb_pos->file_object) {
632 /* remove member from the list */
633 list_del(&cb_pos->list);
634 /* check if cb equal to current iamthif cb */
635 if (dev->iamthif_current_cb == cb_pos) {
636 dev->iamthif_current_cb = NULL;
637 /* send flow control to iamthif client */
638 mei_send_flow_control(dev, &dev->iamthif_cl);
640 /* free all allocated buffers */
641 mei_io_cb_free(cb_pos);
650 * mei_clear_lists - removes all callbacks associated with file
652 * @dev: device structure
653 * @file: file structure
655 * mei_clear_lists is called to clear resources associated with file
656 * when application calls close function or Ctrl-C was pressed
658 * returns true if callback removed from the list, false otherwise
660 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
662 bool removed = false;
664 /* remove callbacks associated with a file */
665 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
666 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
669 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
671 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
674 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
677 if (mei_clear_list(dev, file, &dev->write_list.list))
680 /* check if iamthif_current_cb not NULL */
681 if (dev->iamthif_current_cb && !removed) {
682 /* check file and iamthif current cb association */
683 if (dev->iamthif_current_cb->file_object == file) {
685 mei_io_cb_free(dev->iamthif_current_cb);
686 dev->iamthif_current_cb = NULL;
694 * mei_amthif_release - the release function
696 * @inode: pointer to inode structure
697 * @file: pointer to file structure
699 * returns 0 on success, <0 on error
701 int mei_amthif_release(struct mei_device *dev, struct file *file)
703 if (dev->open_handle_count > 0)
704 dev->open_handle_count--;
706 if (dev->iamthif_file_object == file &&
707 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
709 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
711 dev->iamthif_canceled = true;
712 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
713 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
714 mei_amthif_run_next_cmd(dev);
718 if (mei_clear_lists(dev, file))
719 dev->iamthif_state = MEI_IAMTHIF_IDLE;