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_cl_cmp_id - tells if the clients are the same
82 * returns true - if the clients has same host and me ids
85 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
86 const struct mei_cl *cl2)
89 (cl1->host_client_id == cl2->host_client_id) &&
90 (cl1->me_client_id == cl2->me_client_id);
94 * mei_io_list_flush - removes cbs belonging to cl.
96 * @list: an instance of our list structure
97 * @cl: host client, can be NULL for flushing the whole list
98 * @free: whether to free the cbs
100 static void __mei_io_list_flush(struct mei_cl_cb *list,
101 struct mei_cl *cl, bool free)
103 struct mei_cl_cb *cb;
104 struct mei_cl_cb *next;
106 /* enable removing everything if no cl is specified */
107 list_for_each_entry_safe(cb, next, &list->list, list) {
108 if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
117 * mei_io_list_flush - removes list entry belonging to cl.
119 * @list: An instance of our list structure
122 static inline void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
124 __mei_io_list_flush(list, cl, false);
129 * mei_io_list_free - removes cb belonging to cl and free them
131 * @list: An instance of our list structure
134 static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
136 __mei_io_list_flush(list, cl, true);
140 * mei_io_cb_free - free mei_cb_private related memory
142 * @cb: mei callback struct
144 void mei_io_cb_free(struct mei_cl_cb *cb)
149 kfree(cb->request_buffer.data);
150 kfree(cb->response_buffer.data);
155 * mei_io_cb_init - allocate and initialize io callback
158 * @fp: pointer to file structure
160 * returns mei_cl_cb pointer or NULL;
162 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
164 struct mei_cl_cb *cb;
166 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
170 mei_io_list_init(cb);
172 cb->file_object = fp;
179 * mei_io_cb_alloc_req_buf - allocate request buffer
181 * @cb: io callback structure
182 * @length: size of the buffer
184 * returns 0 on success
185 * -EINVAL if cb is NULL
186 * -ENOMEM if allocation failed
188 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
196 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
197 if (!cb->request_buffer.data)
199 cb->request_buffer.size = length;
203 * mei_io_cb_alloc_resp_buf - allocate response buffer
205 * @cb: io callback structure
206 * @length: size of the buffer
208 * returns 0 on success
209 * -EINVAL if cb is NULL
210 * -ENOMEM if allocation failed
212 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
220 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
221 if (!cb->response_buffer.data)
223 cb->response_buffer.size = length;
230 * mei_cl_flush_queues - flushes queue lists belonging to cl.
234 int mei_cl_flush_queues(struct mei_cl *cl)
236 struct mei_device *dev;
238 if (WARN_ON(!cl || !cl->dev))
243 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
244 mei_io_list_flush(&cl->dev->read_list, cl);
245 mei_io_list_free(&cl->dev->write_list, cl);
246 mei_io_list_free(&cl->dev->write_waiting_list, cl);
247 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
248 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
249 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
250 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
256 * mei_cl_init - initializes cl.
258 * @cl: host client to be initialized
261 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
263 memset(cl, 0, sizeof(struct mei_cl));
264 init_waitqueue_head(&cl->wait);
265 init_waitqueue_head(&cl->rx_wait);
266 init_waitqueue_head(&cl->tx_wait);
267 INIT_LIST_HEAD(&cl->link);
268 INIT_LIST_HEAD(&cl->device_link);
269 cl->reading_state = MEI_IDLE;
270 cl->writing_state = MEI_IDLE;
275 * mei_cl_allocate - allocates cl structure and sets it up.
278 * returns The allocated file or NULL on failure
280 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
284 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
288 mei_cl_init(cl, dev);
294 * mei_cl_find_read_cb - find this cl's callback in the read list
298 * returns cb on success, NULL on error
300 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
302 struct mei_device *dev = cl->dev;
303 struct mei_cl_cb *cb = NULL;
304 struct mei_cl_cb *next = NULL;
306 list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
307 if (mei_cl_cmp_id(cl, cb->cl))
312 /** mei_cl_link: allocate host id in the host map
315 * @id - fixed host id or -1 for generic one
317 * returns 0 on success
318 * -EINVAL on incorrect values
319 * -ENONET if client not found
321 int mei_cl_link(struct mei_cl *cl, int id)
323 struct mei_device *dev;
324 long open_handle_count;
326 if (WARN_ON(!cl || !cl->dev))
331 /* If Id is not assigned get one*/
332 if (id == MEI_HOST_CLIENT_ID_ANY)
333 id = find_first_zero_bit(dev->host_clients_map,
336 if (id >= MEI_CLIENTS_MAX) {
337 dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
341 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
342 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
343 dev_err(&dev->pdev->dev, "open_handle_count exceeded %d",
344 MEI_MAX_OPEN_HANDLE_COUNT);
348 dev->open_handle_count++;
350 cl->host_client_id = id;
351 list_add_tail(&cl->link, &dev->file_list);
353 set_bit(id, dev->host_clients_map);
355 cl->state = MEI_FILE_INITIALIZING;
357 cl_dbg(dev, cl, "link cl\n");
362 * mei_cl_unlink - remove me_cl from the list
366 int mei_cl_unlink(struct mei_cl *cl)
368 struct mei_device *dev;
370 /* don't shout on error exit path */
374 /* wd and amthif might not be initialized */
380 cl_dbg(dev, cl, "unlink client");
382 if (dev->open_handle_count > 0)
383 dev->open_handle_count--;
385 /* never clear the 0 bit */
386 if (cl->host_client_id)
387 clear_bit(cl->host_client_id, dev->host_clients_map);
389 list_del_init(&cl->link);
391 cl->state = MEI_FILE_INITIALIZING;
397 void mei_host_client_init(struct work_struct *work)
399 struct mei_device *dev = container_of(work,
400 struct mei_device, init_work);
401 struct mei_client_properties *client_props;
404 mutex_lock(&dev->device_lock);
406 for (i = 0; i < dev->me_clients_num; i++) {
407 client_props = &dev->me_clients[i].props;
409 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
410 mei_amthif_host_init(dev);
411 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
412 mei_wd_host_init(dev);
413 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
414 mei_nfc_host_init(dev);
418 dev->dev_state = MEI_DEV_ENABLED;
419 dev->reset_count = 0;
421 mutex_unlock(&dev->device_lock);
426 * mei_cl_disconnect - disconnect host client from the me one
430 * Locking: called under "dev->device_lock" lock
432 * returns 0 on success, <0 on failure.
434 int mei_cl_disconnect(struct mei_cl *cl)
436 struct mei_device *dev;
437 struct mei_cl_cb *cb;
440 if (WARN_ON(!cl || !cl->dev))
445 cl_dbg(dev, cl, "disconnecting");
447 if (cl->state != MEI_FILE_DISCONNECTING)
450 cb = mei_io_cb_init(cl, NULL);
454 cb->fop_type = MEI_FOP_CLOSE;
455 if (dev->hbuf_is_ready) {
456 dev->hbuf_is_ready = false;
457 if (mei_hbm_cl_disconnect_req(dev, cl)) {
459 cl_err(dev, cl, "failed to disconnect.\n");
462 cl->timer_count = MEI_CONNECT_TIMEOUT;
463 mdelay(10); /* Wait for hardware disconnection ready */
464 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
466 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
467 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
470 mutex_unlock(&dev->device_lock);
472 err = wait_event_timeout(dev->wait_recvd_msg,
473 MEI_FILE_DISCONNECTED == cl->state,
474 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
476 mutex_lock(&dev->device_lock);
477 if (MEI_FILE_DISCONNECTED == cl->state) {
479 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
482 if (MEI_FILE_DISCONNECTED != cl->state)
483 cl_err(dev, cl, "wrong status client disconnect.\n");
486 cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
489 cl_err(dev, cl, "failed to disconnect from FW client.\n");
492 mei_io_list_flush(&dev->ctrl_rd_list, cl);
493 mei_io_list_flush(&dev->ctrl_wr_list, cl);
501 * mei_cl_is_other_connecting - checks if other
502 * client with the same me client id is connecting
504 * @cl: private data of the file object
506 * returns true if other client is connected, false - otherwise.
508 bool mei_cl_is_other_connecting(struct mei_cl *cl)
510 struct mei_device *dev;
514 if (WARN_ON(!cl || !cl->dev))
519 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
520 if ((pos->state == MEI_FILE_CONNECTING) &&
521 (pos != cl) && cl->me_client_id == pos->me_client_id)
530 * mei_cl_connect - connect host client to the me one
534 * Locking: called under "dev->device_lock" lock
536 * returns 0 on success, <0 on failure.
538 int mei_cl_connect(struct mei_cl *cl, struct file *file)
540 struct mei_device *dev;
541 struct mei_cl_cb *cb;
544 if (WARN_ON(!cl || !cl->dev))
549 cb = mei_io_cb_init(cl, file);
555 cb->fop_type = MEI_FOP_IOCTL;
557 if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
558 dev->hbuf_is_ready = false;
560 if (mei_hbm_cl_connect_req(dev, cl)) {
564 cl->timer_count = MEI_CONNECT_TIMEOUT;
565 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
567 cl->state = MEI_FILE_INITIALIZING;
568 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
571 mutex_unlock(&dev->device_lock);
572 rets = wait_event_timeout(dev->wait_recvd_msg,
573 (cl->state == MEI_FILE_CONNECTED ||
574 cl->state == MEI_FILE_DISCONNECTED),
575 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
576 mutex_lock(&dev->device_lock);
578 if (cl->state != MEI_FILE_CONNECTED) {
581 mei_io_list_flush(&dev->ctrl_rd_list, cl);
582 mei_io_list_flush(&dev->ctrl_wr_list, cl);
594 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
596 * @cl: private data of the file object
598 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
599 * -ENOENT if mei_cl is not present
600 * -EINVAL if single_recv_buf == 0
602 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
604 struct mei_device *dev;
607 if (WARN_ON(!cl || !cl->dev))
612 if (!dev->me_clients_num)
615 if (cl->mei_flow_ctrl_creds > 0)
618 for (i = 0; i < dev->me_clients_num; i++) {
619 struct mei_me_client *me_cl = &dev->me_clients[i];
620 if (me_cl->client_id == cl->me_client_id) {
621 if (me_cl->mei_flow_ctrl_creds) {
622 if (WARN_ON(me_cl->props.single_recv_buf == 0))
634 * mei_cl_flow_ctrl_reduce - reduces flow_control.
636 * @cl: private data of the file object
640 * -ENOENT when me client is not found
641 * -EINVAL when ctrl credits are <= 0
643 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
645 struct mei_device *dev;
648 if (WARN_ON(!cl || !cl->dev))
653 if (!dev->me_clients_num)
656 for (i = 0; i < dev->me_clients_num; i++) {
657 struct mei_me_client *me_cl = &dev->me_clients[i];
658 if (me_cl->client_id == cl->me_client_id) {
659 if (me_cl->props.single_recv_buf != 0) {
660 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
662 dev->me_clients[i].mei_flow_ctrl_creds--;
664 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
666 cl->mei_flow_ctrl_creds--;
675 * mei_cl_read_start - the start read client message function.
679 * returns 0 on success, <0 on failure.
681 int mei_cl_read_start(struct mei_cl *cl, size_t length)
683 struct mei_device *dev;
684 struct mei_cl_cb *cb;
688 if (WARN_ON(!cl || !cl->dev))
693 if (!mei_cl_is_connected(cl))
697 cl_dbg(dev, cl, "read is pending.\n");
700 i = mei_me_cl_by_id(dev, cl->me_client_id);
702 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
706 cb = mei_io_cb_init(cl, NULL);
710 /* always allocate at least client max message */
711 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
712 rets = mei_io_cb_alloc_resp_buf(cb, length);
716 cb->fop_type = MEI_FOP_READ;
717 if (dev->hbuf_is_ready) {
718 dev->hbuf_is_ready = false;
719 if (mei_hbm_cl_flow_control_req(dev, cl)) {
720 cl_err(dev, cl, "flow control send failed\n");
724 list_add_tail(&cb->list, &dev->read_list.list);
726 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
738 * mei_cl_irq_write_complete - write a message to device
739 * from the interrupt thread context
742 * @cb: callback block.
743 * @slots: free slots.
744 * @cmpl_list: complete list.
746 * returns 0, OK; otherwise error.
748 int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
749 s32 *slots, struct mei_cl_cb *cmpl_list)
751 struct mei_device *dev;
752 struct mei_msg_data *buf;
753 struct mei_msg_hdr mei_hdr;
759 if (WARN_ON(!cl || !cl->dev))
764 buf = &cb->request_buffer;
766 rets = mei_cl_flow_ctrl_creds(cl);
771 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
775 len = buf->size - cb->buf_idx;
776 msg_slots = mei_data2slots(len);
778 mei_hdr.host_addr = cl->host_client_id;
779 mei_hdr.me_addr = cl->me_client_id;
780 mei_hdr.reserved = 0;
781 mei_hdr.internal = cb->internal;
783 if (*slots >= msg_slots) {
784 mei_hdr.length = len;
785 mei_hdr.msg_complete = 1;
786 /* Split the message only if we can write the whole host buffer */
787 } else if (*slots == dev->hbuf_depth) {
789 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
790 mei_hdr.length = len;
791 mei_hdr.msg_complete = 0;
793 /* wait for next time the host buffer is empty */
797 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
798 cb->request_buffer.size, cb->buf_idx);
801 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
804 list_move_tail(&cb->list, &cmpl_list->list);
809 cl->writing_state = MEI_WRITING;
810 cb->buf_idx += mei_hdr.length;
812 if (mei_hdr.msg_complete) {
813 if (mei_cl_flow_ctrl_reduce(cl))
815 list_move_tail(&cb->list, &dev->write_waiting_list.list);
822 * mei_cl_write - submit a write cb to mei device
823 assumes device_lock is locked
826 * @cl: write callback with filled data
828 * returns number of bytes sent on success, <0 on failure.
830 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
832 struct mei_device *dev;
833 struct mei_msg_data *buf;
834 struct mei_msg_hdr mei_hdr;
838 if (WARN_ON(!cl || !cl->dev))
847 buf = &cb->request_buffer;
849 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
852 cb->fop_type = MEI_FOP_WRITE;
854 rets = mei_cl_flow_ctrl_creds(cl);
858 /* Host buffer is not ready, we queue the request */
859 if (rets == 0 || !dev->hbuf_is_ready) {
861 /* unseting complete will enqueue the cb for write */
862 mei_hdr.msg_complete = 0;
867 dev->hbuf_is_ready = false;
869 /* Check for a maximum length */
870 if (buf->size > mei_hbuf_max_len(dev)) {
871 mei_hdr.length = mei_hbuf_max_len(dev);
872 mei_hdr.msg_complete = 0;
874 mei_hdr.length = buf->size;
875 mei_hdr.msg_complete = 1;
878 mei_hdr.host_addr = cl->host_client_id;
879 mei_hdr.me_addr = cl->me_client_id;
880 mei_hdr.reserved = 0;
881 mei_hdr.internal = cb->internal;
884 rets = mei_write_message(dev, &mei_hdr, buf->data);
888 cl->writing_state = MEI_WRITING;
889 cb->buf_idx = mei_hdr.length;
893 if (mei_hdr.msg_complete) {
894 if (mei_cl_flow_ctrl_reduce(cl)) {
898 list_add_tail(&cb->list, &dev->write_waiting_list.list);
900 list_add_tail(&cb->list, &dev->write_list.list);
904 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
906 mutex_unlock(&dev->device_lock);
907 if (wait_event_interruptible(cl->tx_wait,
908 cl->writing_state == MEI_WRITE_COMPLETE)) {
909 if (signal_pending(current))
914 mutex_lock(&dev->device_lock);
922 * mei_cl_complete - processes completed operation for a client
924 * @cl: private data of the file object.
925 * @cb: callback block.
927 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
929 if (cb->fop_type == MEI_FOP_WRITE) {
932 cl->writing_state = MEI_WRITE_COMPLETE;
933 if (waitqueue_active(&cl->tx_wait))
934 wake_up_interruptible(&cl->tx_wait);
936 } else if (cb->fop_type == MEI_FOP_READ &&
937 MEI_READING == cl->reading_state) {
938 cl->reading_state = MEI_READ_COMPLETE;
939 if (waitqueue_active(&cl->rx_wait))
940 wake_up_interruptible(&cl->rx_wait);
942 mei_cl_bus_rx_event(cl);
949 * mei_cl_all_disconnect - disconnect forcefully all connected clients
954 void mei_cl_all_disconnect(struct mei_device *dev)
956 struct mei_cl *cl, *next;
958 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
959 cl->state = MEI_FILE_DISCONNECTED;
960 cl->mei_flow_ctrl_creds = 0;
967 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
971 void mei_cl_all_wakeup(struct mei_device *dev)
973 struct mei_cl *cl, *next;
974 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
975 if (waitqueue_active(&cl->rx_wait)) {
976 cl_dbg(dev, cl, "Waking up reading client!\n");
977 wake_up_interruptible(&cl->rx_wait);
979 if (waitqueue_active(&cl->tx_wait)) {
980 cl_dbg(dev, cl, "Waking up writing client!\n");
981 wake_up_interruptible(&cl->tx_wait);
987 * mei_cl_all_write_clear - clear all pending writes
991 void mei_cl_all_write_clear(struct mei_device *dev)
993 mei_io_list_free(&dev->write_list, NULL);
994 mei_io_list_free(&dev->write_waiting_list, NULL);