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
33 * Locking: called under "dev->device_lock" lock
35 * returns me client index or -ENOENT if not found
37 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
41 for (i = 0; i < dev->me_clients_num; ++i)
42 if (uuid_le_cmp(*uuid,
43 dev->me_clients[i].props.protocol_name) == 0)
51 * mei_me_cl_by_id return index to me_clients for client_id
53 * @dev: the device structure
54 * @client_id: me client id
56 * Locking: called under "dev->device_lock" lock
58 * returns index on success, -ENOENT on failure.
61 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
65 for (i = 0; i < dev->me_clients_num; i++)
66 if (dev->me_clients[i].client_id == client_id)
74 * mei_cl_cmp_id - tells if the clients are the same
79 * returns true - if the clients has same host and me ids
82 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
83 const struct mei_cl *cl2)
86 (cl1->host_client_id == cl2->host_client_id) &&
87 (cl1->me_client_id == cl2->me_client_id);
91 * mei_io_list_flush - removes cbs belonging to cl.
93 * @list: an instance of our list structure
94 * @cl: host client, can be NULL for flushing the whole list
95 * @free: whether to free the cbs
97 static void __mei_io_list_flush(struct mei_cl_cb *list,
98 struct mei_cl *cl, bool free)
100 struct mei_cl_cb *cb;
101 struct mei_cl_cb *next;
103 /* enable removing everything if no cl is specified */
104 list_for_each_entry_safe(cb, next, &list->list, list) {
105 if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
114 * mei_io_list_flush - removes list entry belonging to cl.
116 * @list: An instance of our list structure
119 static inline void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
121 __mei_io_list_flush(list, cl, false);
126 * mei_io_list_free - removes cb belonging to cl and free them
128 * @list: An instance of our list structure
131 static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
133 __mei_io_list_flush(list, cl, true);
137 * mei_io_cb_free - free mei_cb_private related memory
139 * @cb: mei callback struct
141 void mei_io_cb_free(struct mei_cl_cb *cb)
146 kfree(cb->request_buffer.data);
147 kfree(cb->response_buffer.data);
152 * mei_io_cb_init - allocate and initialize io callback
155 * @fp: pointer to file structure
157 * returns mei_cl_cb pointer or NULL;
159 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
161 struct mei_cl_cb *cb;
163 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
167 mei_io_list_init(cb);
169 cb->file_object = fp;
176 * mei_io_cb_alloc_req_buf - allocate request buffer
178 * @cb: io callback structure
179 * @length: size of the buffer
181 * returns 0 on success
182 * -EINVAL if cb is NULL
183 * -ENOMEM if allocation failed
185 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
193 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
194 if (!cb->request_buffer.data)
196 cb->request_buffer.size = length;
200 * mei_io_cb_alloc_resp_buf - allocate response buffer
202 * @cb: io callback structure
203 * @length: size of the buffer
205 * returns 0 on success
206 * -EINVAL if cb is NULL
207 * -ENOMEM if allocation failed
209 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
217 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
218 if (!cb->response_buffer.data)
220 cb->response_buffer.size = length;
227 * mei_cl_flush_queues - flushes queue lists belonging to cl.
231 int mei_cl_flush_queues(struct mei_cl *cl)
233 struct mei_device *dev;
235 if (WARN_ON(!cl || !cl->dev))
240 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
241 mei_io_list_flush(&cl->dev->read_list, cl);
242 mei_io_list_free(&cl->dev->write_list, cl);
243 mei_io_list_free(&cl->dev->write_waiting_list, cl);
244 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
245 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
246 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
247 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
253 * mei_cl_init - initializes cl.
255 * @cl: host client to be initialized
258 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
260 memset(cl, 0, sizeof(struct mei_cl));
261 init_waitqueue_head(&cl->wait);
262 init_waitqueue_head(&cl->rx_wait);
263 init_waitqueue_head(&cl->tx_wait);
264 INIT_LIST_HEAD(&cl->link);
265 INIT_LIST_HEAD(&cl->device_link);
266 cl->reading_state = MEI_IDLE;
267 cl->writing_state = MEI_IDLE;
272 * mei_cl_allocate - allocates cl structure and sets it up.
275 * returns The allocated file or NULL on failure
277 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
281 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
285 mei_cl_init(cl, dev);
291 * mei_cl_find_read_cb - find this cl's callback in the read list
295 * returns cb on success, NULL on error
297 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
299 struct mei_device *dev = cl->dev;
300 struct mei_cl_cb *cb;
302 list_for_each_entry(cb, &dev->read_list.list, list)
303 if (mei_cl_cmp_id(cl, cb->cl))
308 /** mei_cl_link: allocate host id in the host map
311 * @id - fixed host id or -1 for generic one
313 * returns 0 on success
314 * -EINVAL on incorrect values
315 * -ENONET if client not found
317 int mei_cl_link(struct mei_cl *cl, int id)
319 struct mei_device *dev;
320 long open_handle_count;
322 if (WARN_ON(!cl || !cl->dev))
327 /* If Id is not assigned get one*/
328 if (id == MEI_HOST_CLIENT_ID_ANY)
329 id = find_first_zero_bit(dev->host_clients_map,
332 if (id >= MEI_CLIENTS_MAX) {
333 dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
337 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
338 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
339 dev_err(&dev->pdev->dev, "open_handle_count exceeded %d",
340 MEI_MAX_OPEN_HANDLE_COUNT);
344 dev->open_handle_count++;
346 cl->host_client_id = id;
347 list_add_tail(&cl->link, &dev->file_list);
349 set_bit(id, dev->host_clients_map);
351 cl->state = MEI_FILE_INITIALIZING;
353 cl_dbg(dev, cl, "link cl\n");
358 * mei_cl_unlink - remove me_cl from the list
362 int mei_cl_unlink(struct mei_cl *cl)
364 struct mei_device *dev;
366 /* don't shout on error exit path */
370 /* wd and amthif might not be initialized */
376 cl_dbg(dev, cl, "unlink client");
378 if (dev->open_handle_count > 0)
379 dev->open_handle_count--;
381 /* never clear the 0 bit */
382 if (cl->host_client_id)
383 clear_bit(cl->host_client_id, dev->host_clients_map);
385 list_del_init(&cl->link);
387 cl->state = MEI_FILE_INITIALIZING;
393 void mei_host_client_init(struct work_struct *work)
395 struct mei_device *dev = container_of(work,
396 struct mei_device, init_work);
397 struct mei_client_properties *client_props;
400 mutex_lock(&dev->device_lock);
402 for (i = 0; i < dev->me_clients_num; i++) {
403 client_props = &dev->me_clients[i].props;
405 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
406 mei_amthif_host_init(dev);
407 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
408 mei_wd_host_init(dev);
409 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
410 mei_nfc_host_init(dev);
414 dev->dev_state = MEI_DEV_ENABLED;
415 dev->reset_count = 0;
417 mutex_unlock(&dev->device_lock);
421 * mei_hbuf_acquire: try to acquire host buffer
423 * @dev: the device structure
424 * returns true if host buffer was acquired
426 bool mei_hbuf_acquire(struct mei_device *dev)
428 if (!dev->hbuf_is_ready) {
429 dev_dbg(&dev->pdev->dev, "hbuf is not ready\n");
433 dev->hbuf_is_ready = false;
439 * mei_cl_disconnect - disconnect host client from the me one
443 * Locking: called under "dev->device_lock" lock
445 * returns 0 on success, <0 on failure.
447 int mei_cl_disconnect(struct mei_cl *cl)
449 struct mei_device *dev;
450 struct mei_cl_cb *cb;
453 if (WARN_ON(!cl || !cl->dev))
458 cl_dbg(dev, cl, "disconnecting");
460 if (cl->state != MEI_FILE_DISCONNECTING)
463 cb = mei_io_cb_init(cl, NULL);
467 cb->fop_type = MEI_FOP_CLOSE;
468 if (mei_hbuf_acquire(dev)) {
469 if (mei_hbm_cl_disconnect_req(dev, cl)) {
471 cl_err(dev, cl, "failed to disconnect.\n");
474 mdelay(10); /* Wait for hardware disconnection ready */
475 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
477 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
478 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
481 mutex_unlock(&dev->device_lock);
483 err = wait_event_timeout(dev->wait_recvd_msg,
484 MEI_FILE_DISCONNECTED == cl->state,
485 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
487 mutex_lock(&dev->device_lock);
488 if (MEI_FILE_DISCONNECTED == cl->state) {
490 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
493 if (MEI_FILE_DISCONNECTED != cl->state)
494 cl_err(dev, cl, "wrong status client disconnect.\n");
497 cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
500 cl_err(dev, cl, "failed to disconnect from FW client.\n");
503 mei_io_list_flush(&dev->ctrl_rd_list, cl);
504 mei_io_list_flush(&dev->ctrl_wr_list, cl);
512 * mei_cl_is_other_connecting - checks if other
513 * client with the same me client id is connecting
515 * @cl: private data of the file object
517 * returns true if other client is connected, false - otherwise.
519 bool mei_cl_is_other_connecting(struct mei_cl *cl)
521 struct mei_device *dev;
522 struct mei_cl *ocl; /* the other client */
524 if (WARN_ON(!cl || !cl->dev))
529 list_for_each_entry(ocl, &dev->file_list, link) {
530 if (ocl->state == MEI_FILE_CONNECTING &&
532 cl->me_client_id == ocl->me_client_id)
541 * mei_cl_connect - connect host client to the me one
545 * Locking: called under "dev->device_lock" lock
547 * returns 0 on success, <0 on failure.
549 int mei_cl_connect(struct mei_cl *cl, struct file *file)
551 struct mei_device *dev;
552 struct mei_cl_cb *cb;
555 if (WARN_ON(!cl || !cl->dev))
560 cb = mei_io_cb_init(cl, file);
566 cb->fop_type = MEI_FOP_CONNECT;
568 /* run hbuf acquire last so we don't have to undo */
569 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
570 if (mei_hbm_cl_connect_req(dev, cl)) {
574 cl->timer_count = MEI_CONNECT_TIMEOUT;
575 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
577 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
580 mutex_unlock(&dev->device_lock);
581 wait_event_timeout(dev->wait_recvd_msg,
582 (cl->state == MEI_FILE_CONNECTED ||
583 cl->state == MEI_FILE_DISCONNECTED),
584 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
585 mutex_lock(&dev->device_lock);
587 if (cl->state != MEI_FILE_CONNECTED) {
588 /* something went really wrong */
590 cl->status = -EFAULT;
592 mei_io_list_flush(&dev->ctrl_rd_list, cl);
593 mei_io_list_flush(&dev->ctrl_wr_list, cl);
604 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
606 * @cl: private data of the file object
608 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
609 * -ENOENT if mei_cl is not present
610 * -EINVAL if single_recv_buf == 0
612 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
614 struct mei_device *dev;
615 struct mei_me_client *me_cl;
618 if (WARN_ON(!cl || !cl->dev))
623 if (!dev->me_clients_num)
626 if (cl->mei_flow_ctrl_creds > 0)
629 id = mei_me_cl_by_id(dev, cl->me_client_id);
631 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
635 me_cl = &dev->me_clients[id];
636 if (me_cl->mei_flow_ctrl_creds) {
637 if (WARN_ON(me_cl->props.single_recv_buf == 0))
645 * mei_cl_flow_ctrl_reduce - reduces flow_control.
647 * @cl: private data of the file object
651 * -ENOENT when me client is not found
652 * -EINVAL when ctrl credits are <= 0
654 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
656 struct mei_device *dev;
657 struct mei_me_client *me_cl;
660 if (WARN_ON(!cl || !cl->dev))
665 id = mei_me_cl_by_id(dev, cl->me_client_id);
667 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
671 me_cl = &dev->me_clients[id];
672 if (me_cl->props.single_recv_buf != 0) {
673 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
675 me_cl->mei_flow_ctrl_creds--;
677 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
679 cl->mei_flow_ctrl_creds--;
685 * mei_cl_read_start - the start read client message function.
689 * returns 0 on success, <0 on failure.
691 int mei_cl_read_start(struct mei_cl *cl, size_t length)
693 struct mei_device *dev;
694 struct mei_cl_cb *cb;
698 if (WARN_ON(!cl || !cl->dev))
703 if (!mei_cl_is_connected(cl))
707 cl_dbg(dev, cl, "read is pending.\n");
710 i = mei_me_cl_by_id(dev, cl->me_client_id);
712 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
716 cb = mei_io_cb_init(cl, NULL);
720 /* always allocate at least client max message */
721 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
722 rets = mei_io_cb_alloc_resp_buf(cb, length);
726 cb->fop_type = MEI_FOP_READ;
727 if (mei_hbuf_acquire(dev)) {
728 if (mei_hbm_cl_flow_control_req(dev, cl)) {
729 cl_err(dev, cl, "flow control send failed\n");
733 list_add_tail(&cb->list, &dev->read_list.list);
735 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
747 * mei_cl_irq_write - write a message to device
748 * from the interrupt thread context
751 * @cb: callback block.
752 * @cmpl_list: complete list.
754 * returns 0, OK; otherwise error.
756 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
757 struct mei_cl_cb *cmpl_list)
759 struct mei_device *dev;
760 struct mei_msg_data *buf;
761 struct mei_msg_hdr mei_hdr;
767 if (WARN_ON(!cl || !cl->dev))
772 buf = &cb->request_buffer;
774 rets = mei_cl_flow_ctrl_creds(cl);
779 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
783 slots = mei_hbuf_empty_slots(dev);
784 len = buf->size - cb->buf_idx;
785 msg_slots = mei_data2slots(len);
787 mei_hdr.host_addr = cl->host_client_id;
788 mei_hdr.me_addr = cl->me_client_id;
789 mei_hdr.reserved = 0;
790 mei_hdr.internal = cb->internal;
792 if (slots >= msg_slots) {
793 mei_hdr.length = len;
794 mei_hdr.msg_complete = 1;
795 /* Split the message only if we can write the whole host buffer */
796 } else if (slots == dev->hbuf_depth) {
798 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
799 mei_hdr.length = len;
800 mei_hdr.msg_complete = 0;
802 /* wait for next time the host buffer is empty */
806 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
807 cb->request_buffer.size, cb->buf_idx);
809 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
812 list_move_tail(&cb->list, &cmpl_list->list);
817 cl->writing_state = MEI_WRITING;
818 cb->buf_idx += mei_hdr.length;
820 if (mei_hdr.msg_complete) {
821 if (mei_cl_flow_ctrl_reduce(cl))
823 list_move_tail(&cb->list, &dev->write_waiting_list.list);
830 * mei_cl_write - submit a write cb to mei device
831 assumes device_lock is locked
834 * @cl: write callback with filled data
836 * returns number of bytes sent on success, <0 on failure.
838 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
840 struct mei_device *dev;
841 struct mei_msg_data *buf;
842 struct mei_msg_hdr mei_hdr;
846 if (WARN_ON(!cl || !cl->dev))
855 buf = &cb->request_buffer;
857 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
860 cb->fop_type = MEI_FOP_WRITE;
862 cl->writing_state = MEI_IDLE;
864 mei_hdr.host_addr = cl->host_client_id;
865 mei_hdr.me_addr = cl->me_client_id;
866 mei_hdr.reserved = 0;
867 mei_hdr.msg_complete = 0;
868 mei_hdr.internal = cb->internal;
870 rets = mei_cl_flow_ctrl_creds(cl);
875 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
879 if (!mei_hbuf_acquire(dev)) {
880 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
885 /* Check for a maximum length */
886 if (buf->size > mei_hbuf_max_len(dev)) {
887 mei_hdr.length = mei_hbuf_max_len(dev);
888 mei_hdr.msg_complete = 0;
890 mei_hdr.length = buf->size;
891 mei_hdr.msg_complete = 1;
894 rets = mei_write_message(dev, &mei_hdr, buf->data);
898 cl->writing_state = MEI_WRITING;
899 cb->buf_idx = mei_hdr.length;
902 if (mei_hdr.msg_complete) {
903 rets = mei_cl_flow_ctrl_reduce(cl);
907 list_add_tail(&cb->list, &dev->write_waiting_list.list);
909 list_add_tail(&cb->list, &dev->write_list.list);
913 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
915 mutex_unlock(&dev->device_lock);
916 rets = wait_event_interruptible(cl->tx_wait,
917 cl->writing_state == MEI_WRITE_COMPLETE);
918 mutex_lock(&dev->device_lock);
919 /* wait_event_interruptible returns -ERESTARTSYS */
921 if (signal_pending(current))
934 * mei_cl_complete - processes completed operation for a client
936 * @cl: private data of the file object.
937 * @cb: callback block.
939 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
941 if (cb->fop_type == MEI_FOP_WRITE) {
944 cl->writing_state = MEI_WRITE_COMPLETE;
945 if (waitqueue_active(&cl->tx_wait))
946 wake_up_interruptible(&cl->tx_wait);
948 } else if (cb->fop_type == MEI_FOP_READ &&
949 MEI_READING == cl->reading_state) {
950 cl->reading_state = MEI_READ_COMPLETE;
951 if (waitqueue_active(&cl->rx_wait))
952 wake_up_interruptible(&cl->rx_wait);
954 mei_cl_bus_rx_event(cl);
961 * mei_cl_all_disconnect - disconnect forcefully all connected clients
966 void mei_cl_all_disconnect(struct mei_device *dev)
970 list_for_each_entry(cl, &dev->file_list, link) {
971 cl->state = MEI_FILE_DISCONNECTED;
972 cl->mei_flow_ctrl_creds = 0;
979 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
983 void mei_cl_all_wakeup(struct mei_device *dev)
986 list_for_each_entry(cl, &dev->file_list, link) {
987 if (waitqueue_active(&cl->rx_wait)) {
988 cl_dbg(dev, cl, "Waking up reading client!\n");
989 wake_up_interruptible(&cl->rx_wait);
991 if (waitqueue_active(&cl->tx_wait)) {
992 cl_dbg(dev, cl, "Waking up writing client!\n");
993 wake_up_interruptible(&cl->tx_wait);
999 * mei_cl_all_write_clear - clear all pending writes
1003 void mei_cl_all_write_clear(struct mei_device *dev)
1005 mei_io_list_free(&dev->write_list, NULL);
1006 mei_io_list_free(&dev->write_waiting_list, NULL);