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;
64 * mei_amthif_host_init - mei initialization amthif client.
66 * @dev: the device structure
69 int mei_amthif_host_init(struct mei_device *dev)
71 struct mei_cl *cl = &dev->iamthif_cl;
72 unsigned char *msg_buf;
75 dev->iamthif_state = MEI_IAMTHIF_IDLE;
79 i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
81 dev_info(&dev->pdev->dev, "amthif: failed to find the client\n");
85 cl->me_client_id = dev->me_clients[i].client_id;
87 /* Assign iamthif_mtu to the value received from ME */
89 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
90 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
91 dev->me_clients[i].props.max_msg_length);
93 kfree(dev->iamthif_msg_buf);
94 dev->iamthif_msg_buf = NULL;
96 /* allocate storage for ME message buffer */
97 msg_buf = kcalloc(dev->iamthif_mtu,
98 sizeof(unsigned char), GFP_KERNEL);
100 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
104 dev->iamthif_msg_buf = msg_buf;
106 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
109 dev_err(&dev->pdev->dev, "amthif: failed link client\n");
113 cl->state = MEI_FILE_CONNECTING;
115 if (mei_hbm_cl_connect_req(dev, cl)) {
116 dev_dbg(&dev->pdev->dev, "amthif: Failed to connect to ME client\n");
117 cl->state = MEI_FILE_DISCONNECTED;
118 cl->host_client_id = 0;
120 cl->timer_count = MEI_CONNECT_TIMEOUT;
126 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
128 * @dev: the device structure
129 * @file: pointer to file object
131 * returns returned a list entry on success, NULL on failure.
133 struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
136 struct mei_cl_cb *pos = NULL;
137 struct mei_cl_cb *next = NULL;
139 list_for_each_entry_safe(pos, next,
140 &dev->amthif_rd_complete_list.list, list) {
141 if (pos->cl && pos->cl == &dev->iamthif_cl &&
142 pos->file_object == file)
150 * mei_amthif_read - read data from AMTHIF client
152 * @dev: the device structure
153 * @if_num: minor number
154 * @file: pointer to file object
155 * @*ubuf: pointer to user data in user space
156 * @length: data length to read
157 * @offset: data read offset
159 * Locking: called under "dev->device_lock" lock
162 * returned data length on success,
163 * zero if no data to read,
164 * negative on failure.
166 int mei_amthif_read(struct mei_device *dev, struct file *file,
167 char __user *ubuf, size_t length, loff_t *offset)
171 struct mei_cl_cb *cb = NULL;
172 struct mei_cl *cl = file->private_data;
173 unsigned long timeout;
176 /* Only Posible if we are in timeout */
177 if (!cl || cl != &dev->iamthif_cl) {
178 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
182 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
185 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
188 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
189 cb = mei_amthif_find_read_list_entry(dev, file);
191 /* Check for if we can block or not*/
192 if (cb == NULL && file->f_flags & O_NONBLOCK)
196 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
198 /* unlock the Mutex */
199 mutex_unlock(&dev->device_lock);
201 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
202 (cb = mei_amthif_find_read_list_entry(dev, file)));
204 /* Locking again the Mutex */
205 mutex_lock(&dev->device_lock);
210 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
214 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
215 dev->iamthif_timer = 0;
218 timeout = cb->read_time +
219 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
220 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
223 if (time_after(jiffies, timeout)) {
224 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
225 /* 15 sec for the message has expired */
231 /* if the whole message will fit remove it from the list */
232 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
234 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
235 /* end of the message has been reached */
240 /* else means that not full buffer will be read and do not
241 * remove message from deletion list
244 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
245 cb->response_buffer.size);
246 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
248 /* length is being turncated to PAGE_SIZE, however,
249 * the buf_idx may point beyond */
250 length = min_t(size_t, length, (cb->buf_idx - *offset));
252 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
256 if ((*offset + length) < cb->buf_idx) {
262 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
270 * mei_amthif_send_cmd - send amthif command to the ME
272 * @dev: the device structure
273 * @cb: mei call back struct
275 * returns 0 on success, <0 on failure.
278 static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
280 struct mei_msg_hdr mei_hdr;
286 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
288 dev->iamthif_state = MEI_IAMTHIF_WRITING;
289 dev->iamthif_current_cb = cb;
290 dev->iamthif_file_object = cb->file_object;
291 dev->iamthif_canceled = false;
292 dev->iamthif_ioctl = true;
293 dev->iamthif_msg_buf_size = cb->request_buffer.size;
294 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
295 cb->request_buffer.size);
297 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
301 if (ret && dev->hbuf_is_ready) {
303 dev->hbuf_is_ready = false;
304 if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
305 mei_hdr.length = mei_hbuf_max_len(dev);
306 mei_hdr.msg_complete = 0;
308 mei_hdr.length = cb->request_buffer.size;
309 mei_hdr.msg_complete = 1;
312 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
313 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
314 mei_hdr.reserved = 0;
315 dev->iamthif_msg_buf_index += mei_hdr.length;
316 if (mei_write_message(dev, &mei_hdr,
317 (unsigned char *)dev->iamthif_msg_buf))
320 if (mei_hdr.msg_complete) {
321 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
323 dev->iamthif_flow_control_pending = true;
324 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
325 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
326 dev->iamthif_current_cb = cb;
327 dev->iamthif_file_object = cb->file_object;
328 list_add_tail(&cb->list, &dev->write_waiting_list.list);
330 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
331 list_add_tail(&cb->list, &dev->write_list.list);
334 if (!dev->hbuf_is_ready)
335 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
337 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
338 list_add_tail(&cb->list, &dev->write_list.list);
344 * mei_amthif_write - write amthif data to amthif client
346 * @dev: the device structure
347 * @cb: mei call back struct
349 * returns 0 on success, <0 on failure.
352 int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
359 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
363 cb->fop_type = MEI_FOP_IOCTL;
365 if (!list_empty(&dev->amthif_cmd_list.list) ||
366 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
367 dev_dbg(&dev->pdev->dev,
368 "amthif state = %d\n", dev->iamthif_state);
369 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
370 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
373 return mei_amthif_send_cmd(dev, cb);
376 * mei_amthif_run_next_cmd
378 * @dev: the device structure
380 * returns 0 on success, <0 on failure.
382 void mei_amthif_run_next_cmd(struct mei_device *dev)
384 struct mei_cl_cb *pos = NULL;
385 struct mei_cl_cb *next = NULL;
391 dev->iamthif_msg_buf_size = 0;
392 dev->iamthif_msg_buf_index = 0;
393 dev->iamthif_canceled = false;
394 dev->iamthif_ioctl = true;
395 dev->iamthif_state = MEI_IAMTHIF_IDLE;
396 dev->iamthif_timer = 0;
397 dev->iamthif_file_object = NULL;
399 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
401 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
402 list_del(&pos->list);
404 if (pos->cl && pos->cl == &dev->iamthif_cl) {
405 status = mei_amthif_send_cmd(dev, pos);
407 dev_dbg(&dev->pdev->dev,
408 "amthif write failed status = %d\n",
418 unsigned int mei_amthif_poll(struct mei_device *dev,
419 struct file *file, poll_table *wait)
421 unsigned int mask = 0;
422 mutex_unlock(&dev->device_lock);
423 poll_wait(file, &dev->iamthif_cl.wait, wait);
424 mutex_lock(&dev->device_lock);
425 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
426 dev->iamthif_file_object == file) {
427 mask |= (POLLIN | POLLRDNORM);
428 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
429 mei_amthif_run_next_cmd(dev);
437 * mei_amthif_irq_write_completed - processes completed iamthif operation.
439 * @dev: the device structure.
440 * @slots: free slots.
441 * @cb_pos: callback block.
442 * @cl: private data of the file object.
443 * @cmpl_list: complete list.
445 * returns 0, OK; otherwise, error.
447 int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
448 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
450 struct mei_msg_hdr mei_hdr;
451 struct mei_cl *cl = cb->cl;
452 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
453 u32 msg_slots = mei_data2slots(len);
455 mei_hdr.host_addr = cl->host_client_id;
456 mei_hdr.me_addr = cl->me_client_id;
457 mei_hdr.reserved = 0;
459 if (*slots >= msg_slots) {
460 mei_hdr.length = len;
461 mei_hdr.msg_complete = 1;
462 /* Split the message only if we can write the whole host buffer */
463 } else if (*slots == dev->hbuf_depth) {
465 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
466 mei_hdr.length = len;
467 mei_hdr.msg_complete = 0;
469 /* wait for next time the host buffer is empty */
473 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
476 if (mei_write_message(dev, &mei_hdr,
477 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index)) {
478 dev->iamthif_state = MEI_IAMTHIF_IDLE;
479 cl->status = -ENODEV;
484 if (mei_cl_flow_ctrl_reduce(cl))
487 dev->iamthif_msg_buf_index += mei_hdr.length;
490 if (mei_hdr.msg_complete) {
491 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
492 dev->iamthif_flow_control_pending = true;
494 /* save iamthif cb sent to amthif client */
495 cb->buf_idx = dev->iamthif_msg_buf_index;
496 dev->iamthif_current_cb = cb;
498 list_move_tail(&cb->list, &dev->write_waiting_list.list);
506 * mei_amthif_irq_read_message - read routine after ISR to
507 * handle the read amthif message
509 * @dev: the device structure
510 * @mei_hdr: header of amthif message
511 * @complete_list: An instance of our list structure
513 * returns 0 on success, <0 on failure.
515 int mei_amthif_irq_read_msg(struct mei_device *dev,
516 struct mei_msg_hdr *mei_hdr,
517 struct mei_cl_cb *complete_list)
519 struct mei_cl_cb *cb;
520 unsigned char *buffer;
522 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
523 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
525 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
526 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
528 mei_read_slots(dev, buffer, mei_hdr->length);
530 dev->iamthif_msg_buf_index += mei_hdr->length;
532 if (!mei_hdr->msg_complete)
535 dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
538 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
539 if (!dev->iamthif_current_cb)
542 cb = dev->iamthif_current_cb;
543 dev->iamthif_current_cb = NULL;
548 dev->iamthif_stall_timer = 0;
549 cb->buf_idx = dev->iamthif_msg_buf_index;
550 cb->read_time = jiffies;
551 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
552 /* found the iamthif cb */
553 dev_dbg(&dev->pdev->dev, "complete the amthif read cb.\n ");
554 dev_dbg(&dev->pdev->dev, "add the amthif read cb to complete.\n ");
555 list_add_tail(&cb->list, &complete_list->list);
561 * mei_amthif_irq_read - prepares to read amthif data.
563 * @dev: the device structure.
564 * @slots: free slots.
566 * returns 0, OK; otherwise, error.
568 int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
570 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
572 if (*slots < msg_slots)
577 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
578 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
582 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
583 dev->iamthif_state = MEI_IAMTHIF_READING;
584 dev->iamthif_flow_control_pending = false;
585 dev->iamthif_msg_buf_index = 0;
586 dev->iamthif_msg_buf_size = 0;
587 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
588 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
593 * mei_amthif_complete - complete amthif callback.
595 * @dev: the device structure.
596 * @cb_pos: callback block.
598 void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
600 if (dev->iamthif_canceled != 1) {
601 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
602 dev->iamthif_stall_timer = 0;
603 memcpy(cb->response_buffer.data,
604 dev->iamthif_msg_buf,
605 dev->iamthif_msg_buf_index);
606 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
607 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
608 dev->iamthif_timer = jiffies;
609 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
612 mei_amthif_run_next_cmd(dev);
615 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
616 wake_up_interruptible(&dev->iamthif_cl.wait);
620 * mei_clear_list - removes all callbacks associated with file
623 * @dev: device structure.
624 * @file: file structure
625 * @mei_cb_list: callbacks list
627 * mei_clear_list is called to clear resources associated with file
628 * when application calls close function or Ctrl-C was pressed
630 * returns true if callback removed from the list, false otherwise
632 static bool mei_clear_list(struct mei_device *dev,
633 const struct file *file, struct list_head *mei_cb_list)
635 struct mei_cl_cb *cb_pos = NULL;
636 struct mei_cl_cb *cb_next = NULL;
637 bool removed = false;
639 /* list all list member */
640 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
641 /* check if list member associated with a file */
642 if (file == cb_pos->file_object) {
643 /* remove member from the list */
644 list_del(&cb_pos->list);
645 /* check if cb equal to current iamthif cb */
646 if (dev->iamthif_current_cb == cb_pos) {
647 dev->iamthif_current_cb = NULL;
648 /* send flow control to iamthif client */
649 mei_hbm_cl_flow_control_req(dev,
652 /* free all allocated buffers */
653 mei_io_cb_free(cb_pos);
662 * mei_clear_lists - removes all callbacks associated with file
664 * @dev: device structure
665 * @file: file structure
667 * mei_clear_lists is called to clear resources associated with file
668 * when application calls close function or Ctrl-C was pressed
670 * returns true if callback removed from the list, false otherwise
672 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
674 bool removed = false;
676 /* remove callbacks associated with a file */
677 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
678 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
681 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
683 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
686 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
689 if (mei_clear_list(dev, file, &dev->write_list.list))
692 /* check if iamthif_current_cb not NULL */
693 if (dev->iamthif_current_cb && !removed) {
694 /* check file and iamthif current cb association */
695 if (dev->iamthif_current_cb->file_object == file) {
697 mei_io_cb_free(dev->iamthif_current_cb);
698 dev->iamthif_current_cb = NULL;
706 * mei_amthif_release - the release function
708 * @dev: device structure
709 * @file: pointer to file structure
711 * returns 0 on success, <0 on error
713 int mei_amthif_release(struct mei_device *dev, struct file *file)
715 if (dev->open_handle_count > 0)
716 dev->open_handle_count--;
718 if (dev->iamthif_file_object == file &&
719 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
721 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
723 dev->iamthif_canceled = true;
724 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
725 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
726 mei_amthif_run_next_cmd(dev);
730 if (mei_clear_lists(dev, file))
731 dev->iamthif_state = MEI_IAMTHIF_IDLE;