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>
34 #include <linux/mei.h>
41 const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
42 0xac, 0xa8, 0x46, 0xe0,
43 0xff, 0x65, 0x81, 0x4c);
46 * mei_amthif_reset_params - initializes mei device iamthif
48 * @dev: the device structure
50 void mei_amthif_reset_params(struct mei_device *dev)
52 /* reset iamthif parameters. */
53 dev->iamthif_current_cb = NULL;
54 dev->iamthif_msg_buf_size = 0;
55 dev->iamthif_msg_buf_index = 0;
56 dev->iamthif_canceled = false;
57 dev->iamthif_ioctl = false;
58 dev->iamthif_state = MEI_IAMTHIF_IDLE;
59 dev->iamthif_timer = 0;
60 dev->iamthif_stall_timer = 0;
61 dev->iamthif_open_count = 0;
65 * mei_amthif_host_init - mei initialization amthif client.
67 * @dev: the device structure
70 int mei_amthif_host_init(struct mei_device *dev)
72 struct mei_cl *cl = &dev->iamthif_cl;
73 unsigned char *msg_buf;
76 dev->iamthif_state = MEI_IAMTHIF_IDLE;
80 i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
82 dev_info(&dev->pdev->dev, "amthif: failed to find the client\n");
86 cl->me_client_id = dev->me_clients[i].client_id;
88 /* Assign iamthif_mtu to the value received from ME */
90 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
91 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
92 dev->me_clients[i].props.max_msg_length);
94 kfree(dev->iamthif_msg_buf);
95 dev->iamthif_msg_buf = NULL;
97 /* allocate storage for ME message buffer */
98 msg_buf = kcalloc(dev->iamthif_mtu,
99 sizeof(unsigned char), GFP_KERNEL);
101 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
105 dev->iamthif_msg_buf = msg_buf;
107 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
110 dev_err(&dev->pdev->dev, "amthif: failed link client\n");
114 cl->state = MEI_FILE_CONNECTING;
116 if (mei_hbm_cl_connect_req(dev, cl)) {
117 dev_dbg(&dev->pdev->dev, "amthif: Failed to connect to ME client\n");
118 cl->state = MEI_FILE_DISCONNECTED;
119 cl->host_client_id = 0;
121 cl->timer_count = MEI_CONNECT_TIMEOUT;
127 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
129 * @dev: the device structure
130 * @file: pointer to file object
132 * returns returned a list entry on success, NULL on failure.
134 struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
137 struct mei_cl_cb *pos = NULL;
138 struct mei_cl_cb *next = NULL;
140 list_for_each_entry_safe(pos, next,
141 &dev->amthif_rd_complete_list.list, list) {
142 if (pos->cl && pos->cl == &dev->iamthif_cl &&
143 pos->file_object == file)
151 * mei_amthif_read - read data from AMTHIF client
153 * @dev: the device structure
154 * @if_num: minor number
155 * @file: pointer to file object
156 * @*ubuf: pointer to user data in user space
157 * @length: data length to read
158 * @offset: data read offset
160 * Locking: called under "dev->device_lock" lock
163 * returned data length on success,
164 * zero if no data to read,
165 * negative on failure.
167 int mei_amthif_read(struct mei_device *dev, struct file *file,
168 char __user *ubuf, size_t length, loff_t *offset)
172 struct mei_cl_cb *cb = NULL;
173 struct mei_cl *cl = file->private_data;
174 unsigned long timeout;
177 /* Only Posible if we are in timeout */
178 if (!cl || cl != &dev->iamthif_cl) {
179 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
183 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
186 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
189 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
190 cb = mei_amthif_find_read_list_entry(dev, file);
192 /* Check for if we can block or not*/
193 if (cb == NULL && file->f_flags & O_NONBLOCK)
197 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
199 /* unlock the Mutex */
200 mutex_unlock(&dev->device_lock);
202 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
203 (cb = mei_amthif_find_read_list_entry(dev, file)));
205 /* Locking again the Mutex */
206 mutex_lock(&dev->device_lock);
211 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
215 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
216 dev->iamthif_timer = 0;
219 timeout = cb->read_time +
220 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
221 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
224 if (time_after(jiffies, timeout)) {
225 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
226 /* 15 sec for the message has expired */
232 /* if the whole message will fit remove it from the list */
233 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
235 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
236 /* end of the message has been reached */
241 /* else means that not full buffer will be read and do not
242 * remove message from deletion list
245 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
246 cb->response_buffer.size);
247 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
249 /* length is being turncated to PAGE_SIZE, however,
250 * the buf_idx may point beyond */
251 length = min_t(size_t, length, (cb->buf_idx - *offset));
253 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
257 if ((*offset + length) < cb->buf_idx) {
263 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
271 * mei_amthif_send_cmd - send amthif command to the ME
273 * @dev: the device structure
274 * @cb: mei call back struct
276 * returns 0 on success, <0 on failure.
279 static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
281 struct mei_msg_hdr mei_hdr;
287 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
289 dev->iamthif_state = MEI_IAMTHIF_WRITING;
290 dev->iamthif_current_cb = cb;
291 dev->iamthif_file_object = cb->file_object;
292 dev->iamthif_canceled = false;
293 dev->iamthif_ioctl = true;
294 dev->iamthif_msg_buf_size = cb->request_buffer.size;
295 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
296 cb->request_buffer.size);
298 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
302 if (ret && dev->hbuf_is_ready) {
304 dev->hbuf_is_ready = false;
305 if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
306 mei_hdr.length = mei_hbuf_max_len(dev);
307 mei_hdr.msg_complete = 0;
309 mei_hdr.length = cb->request_buffer.size;
310 mei_hdr.msg_complete = 1;
313 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
314 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
315 mei_hdr.reserved = 0;
316 dev->iamthif_msg_buf_index += mei_hdr.length;
317 ret = mei_write_message(dev, &mei_hdr, dev->iamthif_msg_buf);
321 if (mei_hdr.msg_complete) {
322 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
324 dev->iamthif_flow_control_pending = true;
325 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
326 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
327 dev->iamthif_current_cb = cb;
328 dev->iamthif_file_object = cb->file_object;
329 list_add_tail(&cb->list, &dev->write_waiting_list.list);
331 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
332 list_add_tail(&cb->list, &dev->write_list.list);
335 if (!dev->hbuf_is_ready)
336 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
338 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
339 list_add_tail(&cb->list, &dev->write_list.list);
345 * mei_amthif_write - write amthif data to amthif client
347 * @dev: the device structure
348 * @cb: mei call back struct
350 * returns 0 on success, <0 on failure.
353 int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
360 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
364 cb->fop_type = MEI_FOP_IOCTL;
366 if (!list_empty(&dev->amthif_cmd_list.list) ||
367 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
368 dev_dbg(&dev->pdev->dev,
369 "amthif state = %d\n", dev->iamthif_state);
370 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
371 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
374 return mei_amthif_send_cmd(dev, cb);
377 * mei_amthif_run_next_cmd
379 * @dev: the device structure
381 * returns 0 on success, <0 on failure.
383 void mei_amthif_run_next_cmd(struct mei_device *dev)
385 struct mei_cl_cb *pos = NULL;
386 struct mei_cl_cb *next = NULL;
392 dev->iamthif_msg_buf_size = 0;
393 dev->iamthif_msg_buf_index = 0;
394 dev->iamthif_canceled = false;
395 dev->iamthif_ioctl = true;
396 dev->iamthif_state = MEI_IAMTHIF_IDLE;
397 dev->iamthif_timer = 0;
398 dev->iamthif_file_object = NULL;
400 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
402 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
403 list_del(&pos->list);
405 if (pos->cl && pos->cl == &dev->iamthif_cl) {
406 status = mei_amthif_send_cmd(dev, pos);
408 dev_dbg(&dev->pdev->dev,
409 "amthif write failed status = %d\n",
419 unsigned int mei_amthif_poll(struct mei_device *dev,
420 struct file *file, poll_table *wait)
422 unsigned int mask = 0;
424 poll_wait(file, &dev->iamthif_cl.wait, wait);
426 mutex_lock(&dev->device_lock);
427 if (!mei_cl_is_connected(&dev->iamthif_cl)) {
431 } else if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
432 dev->iamthif_file_object == file) {
434 mask |= (POLLIN | POLLRDNORM);
435 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
436 mei_amthif_run_next_cmd(dev);
438 mutex_unlock(&dev->device_lock);
446 * mei_amthif_irq_write_completed - processes completed iamthif operation.
448 * @dev: the device structure.
449 * @slots: free slots.
450 * @cb_pos: callback block.
451 * @cl: private data of the file object.
452 * @cmpl_list: complete list.
454 * returns 0, OK; otherwise, error.
456 int mei_amthif_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
457 s32 *slots, struct mei_cl_cb *cmpl_list)
459 struct mei_device *dev = cl->dev;
460 struct mei_msg_hdr mei_hdr;
461 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
462 u32 msg_slots = mei_data2slots(len);
465 rets = mei_cl_flow_ctrl_creds(cl);
470 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
474 mei_hdr.host_addr = cl->host_client_id;
475 mei_hdr.me_addr = cl->me_client_id;
476 mei_hdr.reserved = 0;
478 if (*slots >= msg_slots) {
479 mei_hdr.length = len;
480 mei_hdr.msg_complete = 1;
481 /* Split the message only if we can write the whole host buffer */
482 } else if (*slots == dev->hbuf_depth) {
484 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
485 mei_hdr.length = len;
486 mei_hdr.msg_complete = 0;
488 /* wait for next time the host buffer is empty */
492 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
495 rets = mei_write_message(dev, &mei_hdr,
496 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index);
498 dev->iamthif_state = MEI_IAMTHIF_IDLE;
504 if (mei_cl_flow_ctrl_reduce(cl))
507 dev->iamthif_msg_buf_index += mei_hdr.length;
510 if (mei_hdr.msg_complete) {
511 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
512 dev->iamthif_flow_control_pending = true;
514 /* save iamthif cb sent to amthif client */
515 cb->buf_idx = dev->iamthif_msg_buf_index;
516 dev->iamthif_current_cb = cb;
518 list_move_tail(&cb->list, &dev->write_waiting_list.list);
526 * mei_amthif_irq_read_message - read routine after ISR to
527 * handle the read amthif message
529 * @dev: the device structure
530 * @mei_hdr: header of amthif message
531 * @complete_list: An instance of our list structure
533 * returns 0 on success, <0 on failure.
535 int mei_amthif_irq_read_msg(struct mei_device *dev,
536 struct mei_msg_hdr *mei_hdr,
537 struct mei_cl_cb *complete_list)
539 struct mei_cl_cb *cb;
540 unsigned char *buffer;
542 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
543 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
545 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
546 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
548 mei_read_slots(dev, buffer, mei_hdr->length);
550 dev->iamthif_msg_buf_index += mei_hdr->length;
552 if (!mei_hdr->msg_complete)
555 dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
558 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
559 if (!dev->iamthif_current_cb)
562 cb = dev->iamthif_current_cb;
563 dev->iamthif_current_cb = NULL;
568 dev->iamthif_stall_timer = 0;
569 cb->buf_idx = dev->iamthif_msg_buf_index;
570 cb->read_time = jiffies;
571 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
572 /* found the iamthif cb */
573 dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
574 dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
575 list_add_tail(&cb->list, &complete_list->list);
581 * mei_amthif_irq_read - prepares to read amthif data.
583 * @dev: the device structure.
584 * @slots: free slots.
586 * returns 0, OK; otherwise, error.
588 int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
590 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
592 if (*slots < msg_slots)
597 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
598 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
602 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
603 dev->iamthif_state = MEI_IAMTHIF_READING;
604 dev->iamthif_flow_control_pending = false;
605 dev->iamthif_msg_buf_index = 0;
606 dev->iamthif_msg_buf_size = 0;
607 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
608 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
613 * mei_amthif_complete - complete amthif callback.
615 * @dev: the device structure.
616 * @cb_pos: callback block.
618 void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
620 if (dev->iamthif_canceled != 1) {
621 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
622 dev->iamthif_stall_timer = 0;
623 memcpy(cb->response_buffer.data,
624 dev->iamthif_msg_buf,
625 dev->iamthif_msg_buf_index);
626 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
627 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
628 dev->iamthif_timer = jiffies;
629 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
632 mei_amthif_run_next_cmd(dev);
635 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
636 wake_up_interruptible(&dev->iamthif_cl.wait);
640 * mei_clear_list - removes all callbacks associated with file
643 * @dev: device structure.
644 * @file: file structure
645 * @mei_cb_list: callbacks list
647 * mei_clear_list is called to clear resources associated with file
648 * when application calls close function or Ctrl-C was pressed
650 * returns true if callback removed from the list, false otherwise
652 static bool mei_clear_list(struct mei_device *dev,
653 const struct file *file, struct list_head *mei_cb_list)
655 struct mei_cl_cb *cb_pos = NULL;
656 struct mei_cl_cb *cb_next = NULL;
657 bool removed = false;
659 /* list all list member */
660 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
661 /* check if list member associated with a file */
662 if (file == cb_pos->file_object) {
663 /* remove member from the list */
664 list_del(&cb_pos->list);
665 /* check if cb equal to current iamthif cb */
666 if (dev->iamthif_current_cb == cb_pos) {
667 dev->iamthif_current_cb = NULL;
668 /* send flow control to iamthif client */
669 mei_hbm_cl_flow_control_req(dev,
672 /* free all allocated buffers */
673 mei_io_cb_free(cb_pos);
682 * mei_clear_lists - removes all callbacks associated with file
684 * @dev: device structure
685 * @file: file structure
687 * mei_clear_lists is called to clear resources associated with file
688 * when application calls close function or Ctrl-C was pressed
690 * returns true if callback removed from the list, false otherwise
692 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
694 bool removed = false;
696 /* remove callbacks associated with a file */
697 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
698 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
701 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
703 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
706 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
709 if (mei_clear_list(dev, file, &dev->write_list.list))
712 /* check if iamthif_current_cb not NULL */
713 if (dev->iamthif_current_cb && !removed) {
714 /* check file and iamthif current cb association */
715 if (dev->iamthif_current_cb->file_object == file) {
717 mei_io_cb_free(dev->iamthif_current_cb);
718 dev->iamthif_current_cb = NULL;
726 * mei_amthif_release - the release function
728 * @dev: device structure
729 * @file: pointer to file structure
731 * returns 0 on success, <0 on error
733 int mei_amthif_release(struct mei_device *dev, struct file *file)
735 if (dev->iamthif_open_count > 0)
736 dev->iamthif_open_count--;
738 if (dev->iamthif_file_object == file &&
739 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
741 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
743 dev->iamthif_canceled = true;
744 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
745 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
746 mei_amthif_run_next_cmd(dev);
750 if (mei_clear_lists(dev, file))
751 dev->iamthif_state = MEI_IAMTHIF_IDLE;