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_io_list_flush - removes list entry belonging to cl.
76 * @list: An instance of our list structure
79 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
82 struct mei_cl_cb *next;
84 list_for_each_entry_safe(cb, next, &list->list, list) {
85 if (cb->cl && mei_cl_cmp_id(cl, cb->cl))
91 * mei_io_cb_free - free mei_cb_private related memory
93 * @cb: mei callback struct
95 void mei_io_cb_free(struct mei_cl_cb *cb)
100 kfree(cb->request_buffer.data);
101 kfree(cb->response_buffer.data);
106 * mei_io_cb_init - allocate and initialize io callback
109 * @fp: pointer to file structure
111 * returns mei_cl_cb pointer or NULL;
113 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
115 struct mei_cl_cb *cb;
117 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
121 mei_io_list_init(cb);
123 cb->file_object = fp;
130 * mei_io_cb_alloc_req_buf - allocate request buffer
132 * @cb: io callback structure
133 * @length: size of the buffer
135 * returns 0 on success
136 * -EINVAL if cb is NULL
137 * -ENOMEM if allocation failed
139 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
147 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
148 if (!cb->request_buffer.data)
150 cb->request_buffer.size = length;
154 * mei_io_cb_alloc_resp_buf - allocate response buffer
156 * @cb: io callback structure
157 * @length: size of the buffer
159 * returns 0 on success
160 * -EINVAL if cb is NULL
161 * -ENOMEM if allocation failed
163 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
171 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
172 if (!cb->response_buffer.data)
174 cb->response_buffer.size = length;
181 * mei_cl_flush_queues - flushes queue lists belonging to cl.
185 int mei_cl_flush_queues(struct mei_cl *cl)
187 struct mei_device *dev;
189 if (WARN_ON(!cl || !cl->dev))
194 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
195 mei_io_list_flush(&cl->dev->read_list, cl);
196 mei_io_list_flush(&cl->dev->write_list, cl);
197 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
198 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
199 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
200 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
201 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
207 * mei_cl_init - initializes cl.
209 * @cl: host client to be initialized
212 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
214 memset(cl, 0, sizeof(struct mei_cl));
215 init_waitqueue_head(&cl->wait);
216 init_waitqueue_head(&cl->rx_wait);
217 init_waitqueue_head(&cl->tx_wait);
218 INIT_LIST_HEAD(&cl->link);
219 INIT_LIST_HEAD(&cl->device_link);
220 cl->reading_state = MEI_IDLE;
221 cl->writing_state = MEI_IDLE;
226 * mei_cl_allocate - allocates cl structure and sets it up.
229 * returns The allocated file or NULL on failure
231 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
235 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
239 mei_cl_init(cl, dev);
245 * mei_cl_find_read_cb - find this cl's callback in the read list
249 * returns cb on success, NULL on error
251 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
253 struct mei_device *dev = cl->dev;
254 struct mei_cl_cb *cb;
256 list_for_each_entry(cb, &dev->read_list.list, list)
257 if (mei_cl_cmp_id(cl, cb->cl))
262 /** mei_cl_link: allocate host id in the host map
265 * @id - fixed host id or -1 for generic one
267 * returns 0 on success
268 * -EINVAL on incorrect values
269 * -ENONET if client not found
271 int mei_cl_link(struct mei_cl *cl, int id)
273 struct mei_device *dev;
274 long open_handle_count;
276 if (WARN_ON(!cl || !cl->dev))
281 /* If Id is not assigned get one*/
282 if (id == MEI_HOST_CLIENT_ID_ANY)
283 id = find_first_zero_bit(dev->host_clients_map,
286 if (id >= MEI_CLIENTS_MAX) {
287 dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
291 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
292 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
293 dev_err(&dev->pdev->dev, "open_handle_count exceeded %d",
294 MEI_MAX_OPEN_HANDLE_COUNT);
298 dev->open_handle_count++;
300 cl->host_client_id = id;
301 list_add_tail(&cl->link, &dev->file_list);
303 set_bit(id, dev->host_clients_map);
305 cl->state = MEI_FILE_INITIALIZING;
307 cl_dbg(dev, cl, "link cl\n");
312 * mei_cl_unlink - remove me_cl from the list
316 int mei_cl_unlink(struct mei_cl *cl)
318 struct mei_device *dev;
320 /* don't shout on error exit path */
324 /* wd and amthif might not be initialized */
330 cl_dbg(dev, cl, "unlink client");
332 if (dev->open_handle_count > 0)
333 dev->open_handle_count--;
335 /* never clear the 0 bit */
336 if (cl->host_client_id)
337 clear_bit(cl->host_client_id, dev->host_clients_map);
339 list_del_init(&cl->link);
341 cl->state = MEI_FILE_INITIALIZING;
347 void mei_host_client_init(struct work_struct *work)
349 struct mei_device *dev = container_of(work,
350 struct mei_device, init_work);
351 struct mei_client_properties *client_props;
354 mutex_lock(&dev->device_lock);
356 for (i = 0; i < dev->me_clients_num; i++) {
357 client_props = &dev->me_clients[i].props;
359 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
360 mei_amthif_host_init(dev);
361 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
362 mei_wd_host_init(dev);
363 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
364 mei_nfc_host_init(dev);
368 dev->dev_state = MEI_DEV_ENABLED;
369 dev->reset_count = 0;
371 mutex_unlock(&dev->device_lock);
375 * mei_hbuf_acquire: try to acquire host buffer
377 * @dev: the device structure
378 * returns true if host buffer was acquired
380 bool mei_hbuf_acquire(struct mei_device *dev)
382 if (!dev->hbuf_is_ready) {
383 dev_dbg(&dev->pdev->dev, "hbuf is not ready\n");
387 dev->hbuf_is_ready = false;
393 * mei_cl_disconnect - disconnect host client from the me one
397 * Locking: called under "dev->device_lock" lock
399 * returns 0 on success, <0 on failure.
401 int mei_cl_disconnect(struct mei_cl *cl)
403 struct mei_device *dev;
404 struct mei_cl_cb *cb;
407 if (WARN_ON(!cl || !cl->dev))
412 cl_dbg(dev, cl, "disconnecting");
414 if (cl->state != MEI_FILE_DISCONNECTING)
417 cb = mei_io_cb_init(cl, NULL);
421 cb->fop_type = MEI_FOP_CLOSE;
422 if (mei_hbuf_acquire(dev)) {
423 if (mei_hbm_cl_disconnect_req(dev, cl)) {
425 cl_err(dev, cl, "failed to disconnect.\n");
428 mdelay(10); /* Wait for hardware disconnection ready */
429 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
431 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
432 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
435 mutex_unlock(&dev->device_lock);
437 err = wait_event_timeout(dev->wait_recvd_msg,
438 MEI_FILE_DISCONNECTED == cl->state,
439 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
441 mutex_lock(&dev->device_lock);
442 if (MEI_FILE_DISCONNECTED == cl->state) {
444 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
447 if (MEI_FILE_DISCONNECTED != cl->state)
448 cl_err(dev, cl, "wrong status client disconnect.\n");
451 cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
454 cl_err(dev, cl, "failed to disconnect from FW client.\n");
457 mei_io_list_flush(&dev->ctrl_rd_list, cl);
458 mei_io_list_flush(&dev->ctrl_wr_list, cl);
466 * mei_cl_is_other_connecting - checks if other
467 * client with the same me client id is connecting
469 * @cl: private data of the file object
471 * returns true if other client is connected, false - otherwise.
473 bool mei_cl_is_other_connecting(struct mei_cl *cl)
475 struct mei_device *dev;
476 struct mei_cl *ocl; /* the other client */
478 if (WARN_ON(!cl || !cl->dev))
483 list_for_each_entry(ocl, &dev->file_list, link) {
484 if (ocl->state == MEI_FILE_CONNECTING &&
486 cl->me_client_id == ocl->me_client_id)
495 * mei_cl_connect - connect host client to the me one
499 * Locking: called under "dev->device_lock" lock
501 * returns 0 on success, <0 on failure.
503 int mei_cl_connect(struct mei_cl *cl, struct file *file)
505 struct mei_device *dev;
506 struct mei_cl_cb *cb;
509 if (WARN_ON(!cl || !cl->dev))
514 cb = mei_io_cb_init(cl, file);
520 cb->fop_type = MEI_FOP_CONNECT;
522 /* run hbuf acquire last so we don't have to undo */
523 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
524 if (mei_hbm_cl_connect_req(dev, cl)) {
528 cl->timer_count = MEI_CONNECT_TIMEOUT;
529 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
531 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
534 mutex_unlock(&dev->device_lock);
535 wait_event_timeout(dev->wait_recvd_msg,
536 (cl->state == MEI_FILE_CONNECTED ||
537 cl->state == MEI_FILE_DISCONNECTED),
538 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
539 mutex_lock(&dev->device_lock);
541 if (cl->state != MEI_FILE_CONNECTED) {
542 /* something went really wrong */
544 cl->status = -EFAULT;
546 mei_io_list_flush(&dev->ctrl_rd_list, cl);
547 mei_io_list_flush(&dev->ctrl_wr_list, cl);
558 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
560 * @cl: private data of the file object
562 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
563 * -ENOENT if mei_cl is not present
564 * -EINVAL if single_recv_buf == 0
566 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
568 struct mei_device *dev;
569 struct mei_me_client *me_cl;
572 if (WARN_ON(!cl || !cl->dev))
577 if (!dev->me_clients_num)
580 if (cl->mei_flow_ctrl_creds > 0)
583 id = mei_me_cl_by_id(dev, cl->me_client_id);
585 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
589 me_cl = &dev->me_clients[id];
590 if (me_cl->mei_flow_ctrl_creds) {
591 if (WARN_ON(me_cl->props.single_recv_buf == 0))
599 * mei_cl_flow_ctrl_reduce - reduces flow_control.
601 * @cl: private data of the file object
605 * -ENOENT when me client is not found
606 * -EINVAL when ctrl credits are <= 0
608 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
610 struct mei_device *dev;
611 struct mei_me_client *me_cl;
614 if (WARN_ON(!cl || !cl->dev))
619 id = mei_me_cl_by_id(dev, cl->me_client_id);
621 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
625 me_cl = &dev->me_clients[id];
626 if (me_cl->props.single_recv_buf != 0) {
627 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
629 me_cl->mei_flow_ctrl_creds--;
631 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
633 cl->mei_flow_ctrl_creds--;
639 * mei_cl_read_start - the start read client message function.
643 * returns 0 on success, <0 on failure.
645 int mei_cl_read_start(struct mei_cl *cl, size_t length)
647 struct mei_device *dev;
648 struct mei_cl_cb *cb;
652 if (WARN_ON(!cl || !cl->dev))
657 if (!mei_cl_is_connected(cl))
661 cl_dbg(dev, cl, "read is pending.\n");
664 i = mei_me_cl_by_id(dev, cl->me_client_id);
666 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
670 cb = mei_io_cb_init(cl, NULL);
674 /* always allocate at least client max message */
675 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
676 rets = mei_io_cb_alloc_resp_buf(cb, length);
680 cb->fop_type = MEI_FOP_READ;
681 if (mei_hbuf_acquire(dev)) {
682 if (mei_hbm_cl_flow_control_req(dev, cl)) {
683 cl_err(dev, cl, "flow control send failed\n");
687 list_add_tail(&cb->list, &dev->read_list.list);
689 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
701 * mei_cl_irq_write_complete - write a message to device
702 * from the interrupt thread context
705 * @cb: callback block.
706 * @slots: free slots.
707 * @cmpl_list: complete list.
709 * returns 0, OK; otherwise error.
711 int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
712 s32 *slots, struct mei_cl_cb *cmpl_list)
714 struct mei_device *dev;
715 struct mei_msg_data *buf;
716 struct mei_msg_hdr mei_hdr;
722 if (WARN_ON(!cl || !cl->dev))
727 buf = &cb->request_buffer;
729 rets = mei_cl_flow_ctrl_creds(cl);
734 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
738 len = buf->size - cb->buf_idx;
739 msg_slots = mei_data2slots(len);
741 mei_hdr.host_addr = cl->host_client_id;
742 mei_hdr.me_addr = cl->me_client_id;
743 mei_hdr.reserved = 0;
744 mei_hdr.internal = cb->internal;
746 if (*slots >= msg_slots) {
747 mei_hdr.length = len;
748 mei_hdr.msg_complete = 1;
749 /* Split the message only if we can write the whole host buffer */
750 } else if (*slots == dev->hbuf_depth) {
752 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
753 mei_hdr.length = len;
754 mei_hdr.msg_complete = 0;
756 /* wait for next time the host buffer is empty */
760 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
761 cb->request_buffer.size, cb->buf_idx);
764 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
767 list_move_tail(&cb->list, &cmpl_list->list);
772 cl->writing_state = MEI_WRITING;
773 cb->buf_idx += mei_hdr.length;
775 if (mei_hdr.msg_complete) {
776 if (mei_cl_flow_ctrl_reduce(cl))
778 list_move_tail(&cb->list, &dev->write_waiting_list.list);
785 * mei_cl_write - submit a write cb to mei device
786 assumes device_lock is locked
789 * @cl: write callback with filled data
791 * returns number of bytes sent on success, <0 on failure.
793 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
795 struct mei_device *dev;
796 struct mei_msg_data *buf;
797 struct mei_msg_hdr mei_hdr;
801 if (WARN_ON(!cl || !cl->dev))
810 buf = &cb->request_buffer;
812 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
815 cb->fop_type = MEI_FOP_WRITE;
817 cl->writing_state = MEI_IDLE;
819 mei_hdr.host_addr = cl->host_client_id;
820 mei_hdr.me_addr = cl->me_client_id;
821 mei_hdr.reserved = 0;
822 mei_hdr.msg_complete = 0;
823 mei_hdr.internal = cb->internal;
825 rets = mei_cl_flow_ctrl_creds(cl);
830 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
834 if (!mei_hbuf_acquire(dev)) {
835 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
840 /* Check for a maximum length */
841 if (buf->size > mei_hbuf_max_len(dev)) {
842 mei_hdr.length = mei_hbuf_max_len(dev);
843 mei_hdr.msg_complete = 0;
845 mei_hdr.length = buf->size;
846 mei_hdr.msg_complete = 1;
849 rets = mei_write_message(dev, &mei_hdr, buf->data);
853 cl->writing_state = MEI_WRITING;
854 cb->buf_idx = mei_hdr.length;
858 if (mei_hdr.msg_complete) {
859 if (mei_cl_flow_ctrl_reduce(cl)) {
863 list_add_tail(&cb->list, &dev->write_waiting_list.list);
865 list_add_tail(&cb->list, &dev->write_list.list);
869 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
871 mutex_unlock(&dev->device_lock);
872 if (wait_event_interruptible(cl->tx_wait,
873 cl->writing_state == MEI_WRITE_COMPLETE)) {
874 if (signal_pending(current))
879 mutex_lock(&dev->device_lock);
887 * mei_cl_complete - processes completed operation for a client
889 * @cl: private data of the file object.
890 * @cb: callback block.
892 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
894 if (cb->fop_type == MEI_FOP_WRITE) {
897 cl->writing_state = MEI_WRITE_COMPLETE;
898 if (waitqueue_active(&cl->tx_wait))
899 wake_up_interruptible(&cl->tx_wait);
901 } else if (cb->fop_type == MEI_FOP_READ &&
902 MEI_READING == cl->reading_state) {
903 cl->reading_state = MEI_READ_COMPLETE;
904 if (waitqueue_active(&cl->rx_wait))
905 wake_up_interruptible(&cl->rx_wait);
907 mei_cl_bus_rx_event(cl);
914 * mei_cl_all_disconnect - disconnect forcefully all connected clients
919 void mei_cl_all_disconnect(struct mei_device *dev)
923 list_for_each_entry(cl, &dev->file_list, link) {
924 cl->state = MEI_FILE_DISCONNECTED;
925 cl->mei_flow_ctrl_creds = 0;
932 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
936 void mei_cl_all_wakeup(struct mei_device *dev)
939 list_for_each_entry(cl, &dev->file_list, link) {
940 if (waitqueue_active(&cl->rx_wait)) {
941 cl_dbg(dev, cl, "Waking up reading client!\n");
942 wake_up_interruptible(&cl->rx_wait);
944 if (waitqueue_active(&cl->tx_wait)) {
945 cl_dbg(dev, cl, "Waking up writing client!\n");
946 wake_up_interruptible(&cl->tx_wait);
952 * mei_cl_all_write_clear - clear all pending writes
956 void mei_cl_all_write_clear(struct mei_device *dev)
958 struct mei_cl_cb *cb, *next;
959 struct list_head *list;
961 list = &dev->write_list.list;
962 list_for_each_entry_safe(cb, next, list, list) {
967 list = &dev->write_waiting_list.list;
968 list_for_each_entry_safe(cb, next, list, list) {