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 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
570 mutex_unlock(&dev->device_lock);
571 rets = wait_event_timeout(dev->wait_recvd_msg,
572 (cl->state == MEI_FILE_CONNECTED ||
573 cl->state == MEI_FILE_DISCONNECTED),
574 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
575 mutex_lock(&dev->device_lock);
577 if (cl->state != MEI_FILE_CONNECTED) {
580 mei_io_list_flush(&dev->ctrl_rd_list, cl);
581 mei_io_list_flush(&dev->ctrl_wr_list, cl);
593 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
595 * @cl: private data of the file object
597 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
598 * -ENOENT if mei_cl is not present
599 * -EINVAL if single_recv_buf == 0
601 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
603 struct mei_device *dev;
606 if (WARN_ON(!cl || !cl->dev))
611 if (!dev->me_clients_num)
614 if (cl->mei_flow_ctrl_creds > 0)
617 for (i = 0; i < dev->me_clients_num; i++) {
618 struct mei_me_client *me_cl = &dev->me_clients[i];
619 if (me_cl->client_id == cl->me_client_id) {
620 if (me_cl->mei_flow_ctrl_creds) {
621 if (WARN_ON(me_cl->props.single_recv_buf == 0))
633 * mei_cl_flow_ctrl_reduce - reduces flow_control.
635 * @cl: private data of the file object
639 * -ENOENT when me client is not found
640 * -EINVAL when ctrl credits are <= 0
642 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
644 struct mei_device *dev;
647 if (WARN_ON(!cl || !cl->dev))
652 if (!dev->me_clients_num)
655 for (i = 0; i < dev->me_clients_num; i++) {
656 struct mei_me_client *me_cl = &dev->me_clients[i];
657 if (me_cl->client_id == cl->me_client_id) {
658 if (me_cl->props.single_recv_buf != 0) {
659 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
661 dev->me_clients[i].mei_flow_ctrl_creds--;
663 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
665 cl->mei_flow_ctrl_creds--;
674 * mei_cl_read_start - the start read client message function.
678 * returns 0 on success, <0 on failure.
680 int mei_cl_read_start(struct mei_cl *cl, size_t length)
682 struct mei_device *dev;
683 struct mei_cl_cb *cb;
687 if (WARN_ON(!cl || !cl->dev))
692 if (!mei_cl_is_connected(cl))
696 cl_dbg(dev, cl, "read is pending.\n");
699 i = mei_me_cl_by_id(dev, cl->me_client_id);
701 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
705 cb = mei_io_cb_init(cl, NULL);
709 /* always allocate at least client max message */
710 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
711 rets = mei_io_cb_alloc_resp_buf(cb, length);
715 cb->fop_type = MEI_FOP_READ;
716 if (dev->hbuf_is_ready) {
717 dev->hbuf_is_ready = false;
718 if (mei_hbm_cl_flow_control_req(dev, cl)) {
719 cl_err(dev, cl, "flow control send failed\n");
723 list_add_tail(&cb->list, &dev->read_list.list);
725 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
737 * mei_cl_irq_write_complete - write a message to device
738 * from the interrupt thread context
741 * @cb: callback block.
742 * @slots: free slots.
743 * @cmpl_list: complete list.
745 * returns 0, OK; otherwise error.
747 int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
748 s32 *slots, struct mei_cl_cb *cmpl_list)
750 struct mei_device *dev;
751 struct mei_msg_data *buf;
752 struct mei_msg_hdr mei_hdr;
758 if (WARN_ON(!cl || !cl->dev))
763 buf = &cb->request_buffer;
765 rets = mei_cl_flow_ctrl_creds(cl);
770 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
774 len = buf->size - cb->buf_idx;
775 msg_slots = mei_data2slots(len);
777 mei_hdr.host_addr = cl->host_client_id;
778 mei_hdr.me_addr = cl->me_client_id;
779 mei_hdr.reserved = 0;
780 mei_hdr.internal = cb->internal;
782 if (*slots >= msg_slots) {
783 mei_hdr.length = len;
784 mei_hdr.msg_complete = 1;
785 /* Split the message only if we can write the whole host buffer */
786 } else if (*slots == dev->hbuf_depth) {
788 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
789 mei_hdr.length = len;
790 mei_hdr.msg_complete = 0;
792 /* wait for next time the host buffer is empty */
796 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
797 cb->request_buffer.size, cb->buf_idx);
800 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
803 list_move_tail(&cb->list, &cmpl_list->list);
808 cl->writing_state = MEI_WRITING;
809 cb->buf_idx += mei_hdr.length;
811 if (mei_hdr.msg_complete) {
812 if (mei_cl_flow_ctrl_reduce(cl))
814 list_move_tail(&cb->list, &dev->write_waiting_list.list);
821 * mei_cl_write - submit a write cb to mei device
822 assumes device_lock is locked
825 * @cl: write callback with filled data
827 * returns number of bytes sent on success, <0 on failure.
829 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
831 struct mei_device *dev;
832 struct mei_msg_data *buf;
833 struct mei_msg_hdr mei_hdr;
837 if (WARN_ON(!cl || !cl->dev))
846 buf = &cb->request_buffer;
848 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
851 cb->fop_type = MEI_FOP_WRITE;
853 rets = mei_cl_flow_ctrl_creds(cl);
857 /* Host buffer is not ready, we queue the request */
858 if (rets == 0 || !dev->hbuf_is_ready) {
860 /* unseting complete will enqueue the cb for write */
861 mei_hdr.msg_complete = 0;
866 dev->hbuf_is_ready = false;
868 /* Check for a maximum length */
869 if (buf->size > mei_hbuf_max_len(dev)) {
870 mei_hdr.length = mei_hbuf_max_len(dev);
871 mei_hdr.msg_complete = 0;
873 mei_hdr.length = buf->size;
874 mei_hdr.msg_complete = 1;
877 mei_hdr.host_addr = cl->host_client_id;
878 mei_hdr.me_addr = cl->me_client_id;
879 mei_hdr.reserved = 0;
880 mei_hdr.internal = cb->internal;
883 rets = mei_write_message(dev, &mei_hdr, buf->data);
887 cl->writing_state = MEI_WRITING;
888 cb->buf_idx = mei_hdr.length;
892 if (mei_hdr.msg_complete) {
893 if (mei_cl_flow_ctrl_reduce(cl)) {
897 list_add_tail(&cb->list, &dev->write_waiting_list.list);
899 list_add_tail(&cb->list, &dev->write_list.list);
903 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
905 mutex_unlock(&dev->device_lock);
906 if (wait_event_interruptible(cl->tx_wait,
907 cl->writing_state == MEI_WRITE_COMPLETE)) {
908 if (signal_pending(current))
913 mutex_lock(&dev->device_lock);
921 * mei_cl_complete - processes completed operation for a client
923 * @cl: private data of the file object.
924 * @cb: callback block.
926 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
928 if (cb->fop_type == MEI_FOP_WRITE) {
931 cl->writing_state = MEI_WRITE_COMPLETE;
932 if (waitqueue_active(&cl->tx_wait))
933 wake_up_interruptible(&cl->tx_wait);
935 } else if (cb->fop_type == MEI_FOP_READ &&
936 MEI_READING == cl->reading_state) {
937 cl->reading_state = MEI_READ_COMPLETE;
938 if (waitqueue_active(&cl->rx_wait))
939 wake_up_interruptible(&cl->rx_wait);
941 mei_cl_bus_rx_event(cl);
948 * mei_cl_all_disconnect - disconnect forcefully all connected clients
953 void mei_cl_all_disconnect(struct mei_device *dev)
955 struct mei_cl *cl, *next;
957 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
958 cl->state = MEI_FILE_DISCONNECTED;
959 cl->mei_flow_ctrl_creds = 0;
966 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
970 void mei_cl_all_wakeup(struct mei_device *dev)
972 struct mei_cl *cl, *next;
973 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
974 if (waitqueue_active(&cl->rx_wait)) {
975 cl_dbg(dev, cl, "Waking up reading client!\n");
976 wake_up_interruptible(&cl->rx_wait);
978 if (waitqueue_active(&cl->tx_wait)) {
979 cl_dbg(dev, cl, "Waking up writing client!\n");
980 wake_up_interruptible(&cl->tx_wait);
986 * mei_cl_all_write_clear - clear all pending writes
990 void mei_cl_all_write_clear(struct mei_device *dev)
992 mei_io_list_free(&dev->write_list, NULL);
993 mei_io_list_free(&dev->write_waiting_list, NULL);