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/pci.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/delay.h>
22 #include <linux/mei.h>
29 * mei_me_cl_by_uuid - locate index of me client
32 * returns me client index or -ENOENT if not found
34 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
38 for (i = 0; i < dev->me_clients_num; ++i)
39 if (uuid_le_cmp(*uuid,
40 dev->me_clients[i].props.protocol_name) == 0) {
50 * mei_me_cl_by_id return index to me_clients for client_id
52 * @dev: the device structure
53 * @client_id: me client id
55 * Locking: called under "dev->device_lock" lock
57 * returns index on success, -ENOENT on failure.
60 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
63 for (i = 0; i < dev->me_clients_num; i++)
64 if (dev->me_clients[i].client_id == client_id)
66 if (WARN_ON(dev->me_clients[i].client_id != client_id))
69 if (i == dev->me_clients_num)
77 * mei_io_list_flush - removes list entry belonging to cl.
79 * @list: An instance of our list structure
82 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
85 struct mei_cl_cb *next;
87 list_for_each_entry_safe(cb, next, &list->list, list) {
88 if (cb->cl && mei_cl_cmp_id(cl, cb->cl))
94 * mei_io_cb_free - free mei_cb_private related memory
96 * @cb: mei callback struct
98 void mei_io_cb_free(struct mei_cl_cb *cb)
103 kfree(cb->request_buffer.data);
104 kfree(cb->response_buffer.data);
109 * mei_io_cb_init - allocate and initialize io callback
112 * @fp: pointer to file structure
114 * returns mei_cl_cb pointer or NULL;
116 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
118 struct mei_cl_cb *cb;
120 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
124 mei_io_list_init(cb);
126 cb->file_object = fp;
133 * mei_io_cb_alloc_req_buf - allocate request buffer
135 * @cb: io callback structure
136 * @length: size of the buffer
138 * returns 0 on success
139 * -EINVAL if cb is NULL
140 * -ENOMEM if allocation failed
142 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
150 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
151 if (!cb->request_buffer.data)
153 cb->request_buffer.size = length;
157 * mei_io_cb_alloc_resp_buf - allocate respose buffer
159 * @cb: io callback structure
160 * @length: size of the buffer
162 * returns 0 on success
163 * -EINVAL if cb is NULL
164 * -ENOMEM if allocation failed
166 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
174 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
175 if (!cb->response_buffer.data)
177 cb->response_buffer.size = length;
184 * mei_cl_flush_queues - flushes queue lists belonging to cl.
188 int mei_cl_flush_queues(struct mei_cl *cl)
190 struct mei_device *dev;
192 if (WARN_ON(!cl || !cl->dev))
197 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
198 mei_io_list_flush(&cl->dev->read_list, cl);
199 mei_io_list_flush(&cl->dev->write_list, cl);
200 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
201 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
202 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
203 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
204 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
210 * mei_cl_init - initializes intialize cl.
212 * @cl: host client to be initialized
215 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
217 memset(cl, 0, sizeof(struct mei_cl));
218 init_waitqueue_head(&cl->wait);
219 init_waitqueue_head(&cl->rx_wait);
220 init_waitqueue_head(&cl->tx_wait);
221 INIT_LIST_HEAD(&cl->link);
222 INIT_LIST_HEAD(&cl->device_link);
223 cl->reading_state = MEI_IDLE;
224 cl->writing_state = MEI_IDLE;
229 * mei_cl_allocate - allocates cl structure and sets it up.
232 * returns The allocated file or NULL on failure
234 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
238 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
242 mei_cl_init(cl, dev);
248 * mei_cl_find_read_cb - find this cl's callback in the read list
252 * returns cb on success, NULL on error
254 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
256 struct mei_device *dev = cl->dev;
257 struct mei_cl_cb *cb = NULL;
258 struct mei_cl_cb *next = NULL;
260 list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
261 if (mei_cl_cmp_id(cl, cb->cl))
266 /** mei_cl_link: allocte host id in the host map
269 * @id - fixed host id or -1 for genereting one
271 * returns 0 on success
272 * -EINVAL on incorrect values
273 * -ENONET if client not found
275 int mei_cl_link(struct mei_cl *cl, int id)
277 struct mei_device *dev;
279 if (WARN_ON(!cl || !cl->dev))
284 /* If Id is not asigned get one*/
285 if (id == MEI_HOST_CLIENT_ID_ANY)
286 id = find_first_zero_bit(dev->host_clients_map,
289 if (id >= MEI_CLIENTS_MAX) {
290 dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
294 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
295 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
296 MEI_MAX_OPEN_HANDLE_COUNT);
300 dev->open_handle_count++;
302 cl->host_client_id = id;
303 list_add_tail(&cl->link, &dev->file_list);
305 set_bit(id, dev->host_clients_map);
307 cl->state = MEI_FILE_INITIALIZING;
309 cl_dbg(dev, cl, "link cl\n");
314 * mei_cl_unlink - remove me_cl from the list
318 int mei_cl_unlink(struct mei_cl *cl)
320 struct mei_device *dev;
321 struct mei_cl *pos, *next;
323 /* don't shout on error exit path */
327 /* wd and amthif might not be initialized */
333 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
334 if (cl->host_client_id == pos->host_client_id) {
335 cl_dbg(dev, cl, "remove host client = %d, ME client = %d\n",
336 pos->host_client_id, pos->me_client_id);
337 list_del_init(&pos->link);
345 void mei_host_client_init(struct work_struct *work)
347 struct mei_device *dev = container_of(work,
348 struct mei_device, init_work);
349 struct mei_client_properties *client_props;
352 mutex_lock(&dev->device_lock);
354 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
355 dev->open_handle_count = 0;
358 * Reserving the first three client IDs
359 * 0: Reserved for MEI Bus Message communications
360 * 1: Reserved for Watchdog
361 * 2: Reserved for AMTHI
363 bitmap_set(dev->host_clients_map, 0, 3);
365 for (i = 0; i < dev->me_clients_num; i++) {
366 client_props = &dev->me_clients[i].props;
368 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
369 mei_amthif_host_init(dev);
370 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
371 mei_wd_host_init(dev);
372 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
373 mei_nfc_host_init(dev);
377 dev->dev_state = MEI_DEV_ENABLED;
379 mutex_unlock(&dev->device_lock);
384 * mei_cl_disconnect - disconnect host clinet form the me one
388 * Locking: called under "dev->device_lock" lock
390 * returns 0 on success, <0 on failure.
392 int mei_cl_disconnect(struct mei_cl *cl)
394 struct mei_device *dev;
395 struct mei_cl_cb *cb;
398 if (WARN_ON(!cl || !cl->dev))
403 cl_dbg(dev, cl, "disconnecting");
405 if (cl->state != MEI_FILE_DISCONNECTING)
408 cb = mei_io_cb_init(cl, NULL);
412 cb->fop_type = MEI_FOP_CLOSE;
413 if (dev->hbuf_is_ready) {
414 dev->hbuf_is_ready = false;
415 if (mei_hbm_cl_disconnect_req(dev, cl)) {
417 cl_err(dev, cl, "failed to disconnect.\n");
420 mdelay(10); /* Wait for hardware disconnection ready */
421 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
423 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
424 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
427 mutex_unlock(&dev->device_lock);
429 err = wait_event_timeout(dev->wait_recvd_msg,
430 MEI_FILE_DISCONNECTED == cl->state,
431 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
433 mutex_lock(&dev->device_lock);
434 if (MEI_FILE_DISCONNECTED == cl->state) {
436 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
439 if (MEI_FILE_DISCONNECTED != cl->state)
440 cl_err(dev, cl, "wrong status client disconnect.\n");
443 cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
446 cl_err(dev, cl, "failed to disconnect from FW client.\n");
449 mei_io_list_flush(&dev->ctrl_rd_list, cl);
450 mei_io_list_flush(&dev->ctrl_wr_list, cl);
458 * mei_cl_is_other_connecting - checks if other
459 * client with the same me client id is connecting
461 * @cl: private data of the file object
463 * returns ture if other client is connected, 0 - otherwise.
465 bool mei_cl_is_other_connecting(struct mei_cl *cl)
467 struct mei_device *dev;
471 if (WARN_ON(!cl || !cl->dev))
476 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
477 if ((pos->state == MEI_FILE_CONNECTING) &&
478 (pos != cl) && cl->me_client_id == pos->me_client_id)
487 * mei_cl_connect - connect host clinet to the me one
491 * Locking: called under "dev->device_lock" lock
493 * returns 0 on success, <0 on failure.
495 int mei_cl_connect(struct mei_cl *cl, struct file *file)
497 struct mei_device *dev;
498 struct mei_cl_cb *cb;
501 if (WARN_ON(!cl || !cl->dev))
506 cb = mei_io_cb_init(cl, file);
512 cb->fop_type = MEI_FOP_IOCTL;
514 if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
515 dev->hbuf_is_ready = false;
517 if (mei_hbm_cl_connect_req(dev, cl)) {
521 cl->timer_count = MEI_CONNECT_TIMEOUT;
522 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
524 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
527 mutex_unlock(&dev->device_lock);
528 rets = wait_event_timeout(dev->wait_recvd_msg,
529 (cl->state == MEI_FILE_CONNECTED ||
530 cl->state == MEI_FILE_DISCONNECTED),
531 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
532 mutex_lock(&dev->device_lock);
534 if (cl->state != MEI_FILE_CONNECTED) {
537 mei_io_list_flush(&dev->ctrl_rd_list, cl);
538 mei_io_list_flush(&dev->ctrl_wr_list, cl);
550 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
552 * @cl: private data of the file object
554 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
555 * -ENOENT if mei_cl is not present
556 * -EINVAL if single_recv_buf == 0
558 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
560 struct mei_device *dev;
563 if (WARN_ON(!cl || !cl->dev))
568 if (!dev->me_clients_num)
571 if (cl->mei_flow_ctrl_creds > 0)
574 for (i = 0; i < dev->me_clients_num; i++) {
575 struct mei_me_client *me_cl = &dev->me_clients[i];
576 if (me_cl->client_id == cl->me_client_id) {
577 if (me_cl->mei_flow_ctrl_creds) {
578 if (WARN_ON(me_cl->props.single_recv_buf == 0))
590 * mei_cl_flow_ctrl_reduce - reduces flow_control.
592 * @cl: private data of the file object
596 * -ENOENT when me client is not found
597 * -EINVAL when ctrl credits are <= 0
599 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
601 struct mei_device *dev;
604 if (WARN_ON(!cl || !cl->dev))
609 if (!dev->me_clients_num)
612 for (i = 0; i < dev->me_clients_num; i++) {
613 struct mei_me_client *me_cl = &dev->me_clients[i];
614 if (me_cl->client_id == cl->me_client_id) {
615 if (me_cl->props.single_recv_buf != 0) {
616 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
618 dev->me_clients[i].mei_flow_ctrl_creds--;
620 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
622 cl->mei_flow_ctrl_creds--;
631 * mei_cl_read_start - the start read client message function.
635 * returns 0 on success, <0 on failure.
637 int mei_cl_read_start(struct mei_cl *cl, size_t length)
639 struct mei_device *dev;
640 struct mei_cl_cb *cb;
644 if (WARN_ON(!cl || !cl->dev))
649 if (!mei_cl_is_connected(cl))
653 cl_dbg(dev, cl, "read is pending.\n");
656 i = mei_me_cl_by_id(dev, cl->me_client_id);
658 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
662 cb = mei_io_cb_init(cl, NULL);
666 /* always allocate at least client max message */
667 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
668 rets = mei_io_cb_alloc_resp_buf(cb, length);
672 cb->fop_type = MEI_FOP_READ;
674 if (dev->hbuf_is_ready) {
675 dev->hbuf_is_ready = false;
676 if (mei_hbm_cl_flow_control_req(dev, cl)) {
677 cl_err(dev, cl, "flow control send failed\n");
681 list_add_tail(&cb->list, &dev->read_list.list);
683 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
692 * mei_cl_irq_write_complete - write a message to device
693 * from the interrupt thread context
696 * @cb: callback block.
697 * @slots: free slots.
698 * @cmpl_list: complete list.
700 * returns 0, OK; otherwise error.
702 int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
703 s32 *slots, struct mei_cl_cb *cmpl_list)
705 struct mei_device *dev;
706 struct mei_msg_data *buf;
707 struct mei_msg_hdr mei_hdr;
713 if (WARN_ON(!cl || !cl->dev))
718 buf = &cb->request_buffer;
720 rets = mei_cl_flow_ctrl_creds(cl);
725 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
729 len = buf->size - cb->buf_idx;
730 msg_slots = mei_data2slots(len);
732 mei_hdr.host_addr = cl->host_client_id;
733 mei_hdr.me_addr = cl->me_client_id;
734 mei_hdr.reserved = 0;
736 if (*slots >= msg_slots) {
737 mei_hdr.length = len;
738 mei_hdr.msg_complete = 1;
739 /* Split the message only if we can write the whole host buffer */
740 } else if (*slots == dev->hbuf_depth) {
742 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
743 mei_hdr.length = len;
744 mei_hdr.msg_complete = 0;
746 /* wait for next time the host buffer is empty */
750 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
751 cb->request_buffer.size, cb->buf_idx);
754 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
757 list_move_tail(&cb->list, &cmpl_list->list);
762 cl->writing_state = MEI_WRITING;
763 cb->buf_idx += mei_hdr.length;
765 if (mei_hdr.msg_complete) {
766 if (mei_cl_flow_ctrl_reduce(cl))
768 list_move_tail(&cb->list, &dev->write_waiting_list.list);
775 * mei_cl_write - submit a write cb to mei device
776 assumes device_lock is locked
779 * @cl: write callback with filled data
781 * returns numbe of bytes sent on success, <0 on failure.
783 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
785 struct mei_device *dev;
786 struct mei_msg_data *buf;
787 struct mei_msg_hdr mei_hdr;
791 if (WARN_ON(!cl || !cl->dev))
800 buf = &cb->request_buffer;
802 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
805 cb->fop_type = MEI_FOP_WRITE;
807 rets = mei_cl_flow_ctrl_creds(cl);
811 /* Host buffer is not ready, we queue the request */
812 if (rets == 0 || !dev->hbuf_is_ready) {
814 /* unseting complete will enqueue the cb for write */
815 mei_hdr.msg_complete = 0;
820 dev->hbuf_is_ready = false;
822 /* Check for a maximum length */
823 if (buf->size > mei_hbuf_max_len(dev)) {
824 mei_hdr.length = mei_hbuf_max_len(dev);
825 mei_hdr.msg_complete = 0;
827 mei_hdr.length = buf->size;
828 mei_hdr.msg_complete = 1;
831 mei_hdr.host_addr = cl->host_client_id;
832 mei_hdr.me_addr = cl->me_client_id;
833 mei_hdr.reserved = 0;
836 rets = mei_write_message(dev, &mei_hdr, buf->data);
840 cl->writing_state = MEI_WRITING;
841 cb->buf_idx = mei_hdr.length;
845 if (mei_hdr.msg_complete) {
846 if (mei_cl_flow_ctrl_reduce(cl)) {
850 list_add_tail(&cb->list, &dev->write_waiting_list.list);
852 list_add_tail(&cb->list, &dev->write_list.list);
856 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
858 mutex_unlock(&dev->device_lock);
859 if (wait_event_interruptible(cl->tx_wait,
860 cl->writing_state == MEI_WRITE_COMPLETE)) {
861 if (signal_pending(current))
866 mutex_lock(&dev->device_lock);
874 * mei_cl_complete - processes completed operation for a client
876 * @cl: private data of the file object.
877 * @cb: callback block.
879 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
881 if (cb->fop_type == MEI_FOP_WRITE) {
884 cl->writing_state = MEI_WRITE_COMPLETE;
885 if (waitqueue_active(&cl->tx_wait))
886 wake_up_interruptible(&cl->tx_wait);
888 } else if (cb->fop_type == MEI_FOP_READ &&
889 MEI_READING == cl->reading_state) {
890 cl->reading_state = MEI_READ_COMPLETE;
891 if (waitqueue_active(&cl->rx_wait))
892 wake_up_interruptible(&cl->rx_wait);
894 mei_cl_bus_rx_event(cl);
901 * mei_cl_all_disconnect - disconnect forcefully all connected clients
906 void mei_cl_all_disconnect(struct mei_device *dev)
908 struct mei_cl *cl, *next;
910 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
911 cl->state = MEI_FILE_DISCONNECTED;
912 cl->mei_flow_ctrl_creds = 0;
920 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
924 void mei_cl_all_wakeup(struct mei_device *dev)
926 struct mei_cl *cl, *next;
927 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
928 if (waitqueue_active(&cl->rx_wait)) {
929 cl_dbg(dev, cl, "Waking up reading client!\n");
930 wake_up_interruptible(&cl->rx_wait);
932 if (waitqueue_active(&cl->tx_wait)) {
933 cl_dbg(dev, cl, "Waking up writing client!\n");
934 wake_up_interruptible(&cl->tx_wait);
940 * mei_cl_all_write_clear - clear all pending writes
944 void mei_cl_all_write_clear(struct mei_device *dev)
946 struct mei_cl_cb *cb, *next;
948 list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {