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
18 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/fcntl.h>
23 #include <linux/aio.h>
24 #include <linux/pci.h>
25 #include <linux/init.h>
26 #include <linux/ioctl.h>
27 #include <linux/cdev.h>
28 #include <linux/list.h>
29 #include <linux/delay.h>
30 #include <linux/sched.h>
31 #include <linux/uuid.h>
32 #include <linux/jiffies.h>
33 #include <linux/uaccess.h>
38 #include <linux/mei.h>
39 #include "interface.h"
42 * mei_io_cb_free - free mei_cb_private related memory
44 * @cb: mei callback struct
46 void mei_io_cb_free(struct mei_cl_cb *cb)
51 kfree(cb->request_buffer.data);
52 kfree(cb->response_buffer.data);
56 * mei_io_cb_init - allocate and initialize io callback
59 * @file: pointer to file structure
61 * returns mei_cl_cb pointer or NULL;
63 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
67 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
81 * mei_io_cb_alloc_req_buf - allocate request buffer
83 * @cb - io callback structure
84 * @size: size of the buffer
86 * returns 0 on success
87 * -EINVAL if cb is NULL
88 * -ENOMEM if allocation failed
90 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
98 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
99 if (!cb->request_buffer.data)
101 cb->request_buffer.size = length;
105 * mei_io_cb_alloc_req_buf - allocate respose buffer
107 * @cb - io callback structure
108 * @size: size of the buffer
110 * returns 0 on success
111 * -EINVAL if cb is NULL
112 * -ENOMEM if allocation failed
114 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
122 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
123 if (!cb->response_buffer.data)
125 cb->response_buffer.size = length;
131 * mei_me_cl_by_id return index to me_clients for client_id
133 * @dev: the device structure
134 * @client_id: me client id
136 * Locking: called under "dev->device_lock" lock
138 * returns index on success, -ENOENT on failure.
141 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
144 for (i = 0; i < dev->me_clients_num; i++)
145 if (dev->me_clients[i].client_id == client_id)
147 if (WARN_ON(dev->me_clients[i].client_id != client_id))
150 if (i == dev->me_clients_num)
157 * mei_ioctl_connect_client - the connect to fw client IOCTL function
159 * @dev: the device structure
160 * @data: IOCTL connect data, input and output parameters
161 * @file: private data of the file object
163 * Locking: called under "dev->device_lock" lock
165 * returns 0 on success, <0 on failure.
167 int mei_ioctl_connect_client(struct file *file,
168 struct mei_connect_client_data *data)
170 struct mei_device *dev;
171 struct mei_cl_cb *cb;
172 struct mei_client *client;
174 long timeout = mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT);
179 cl = file->private_data;
180 if (WARN_ON(!cl || !cl->dev))
185 dev_dbg(&dev->pdev->dev, "mei_ioctl_connect_client() Entry\n");
187 /* buffered ioctl cb */
188 cb = mei_io_cb_init(cl, file);
194 cb->fop_type = MEI_FOP_IOCTL;
196 if (dev->dev_state != MEI_DEV_ENABLED) {
200 if (cl->state != MEI_FILE_INITIALIZING &&
201 cl->state != MEI_FILE_DISCONNECTED) {
206 /* find ME client we're trying to connect to */
207 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
208 if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
209 cl->me_client_id = dev->me_clients[i].client_id;
210 cl->state = MEI_FILE_CONNECTING;
213 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
215 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
216 dev->me_clients[i].props.protocol_version);
217 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
218 dev->me_clients[i].props.max_msg_length);
220 /* if we're connecting to amthi client then we will use the
221 * existing connection
223 if (uuid_le_cmp(data->in_client_uuid, mei_amthi_guid) == 0) {
224 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
225 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
229 clear_bit(cl->host_client_id, dev->host_clients_map);
230 mei_me_cl_unlink(dev, cl);
234 file->private_data = &dev->iamthif_cl;
236 client = &data->out_client_properties;
237 client->max_msg_length =
238 dev->me_clients[i].props.max_msg_length;
239 client->protocol_version =
240 dev->me_clients[i].props.protocol_version;
241 rets = dev->iamthif_cl.status;
246 if (cl->state != MEI_FILE_CONNECTING) {
252 /* prepare the output buffer */
253 client = &data->out_client_properties;
254 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
255 client->protocol_version = dev->me_clients[i].props.protocol_version;
256 dev_dbg(&dev->pdev->dev, "Can connect?\n");
257 if (dev->mei_host_buffer_is_empty
258 && !mei_other_client_is_connecting(dev, cl)) {
259 dev_dbg(&dev->pdev->dev, "Sending Connect Message\n");
260 dev->mei_host_buffer_is_empty = false;
261 if (mei_connect(dev, cl)) {
262 dev_dbg(&dev->pdev->dev, "Sending connect message - failed\n");
266 dev_dbg(&dev->pdev->dev, "Sending connect message - succeeded\n");
267 cl->timer_count = MEI_CONNECT_TIMEOUT;
268 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
273 dev_dbg(&dev->pdev->dev, "Queuing the connect request due to device busy\n");
274 dev_dbg(&dev->pdev->dev, "add connect cb to control write list.\n");
275 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
277 mutex_unlock(&dev->device_lock);
278 err = wait_event_timeout(dev->wait_recvd_msg,
279 (MEI_FILE_CONNECTED == cl->state ||
280 MEI_FILE_DISCONNECTED == cl->state), timeout);
282 mutex_lock(&dev->device_lock);
283 if (MEI_FILE_CONNECTED == cl->state) {
284 dev_dbg(&dev->pdev->dev, "successfully connected to FW client.\n");
288 dev_dbg(&dev->pdev->dev, "failed to connect to FW client.cl->state = %d.\n",
291 dev_dbg(&dev->pdev->dev,
292 "wait_event_interruptible_timeout failed on client"
293 " connect message fw response message.\n");
297 mei_io_list_flush(&dev->ctrl_rd_list, cl);
298 mei_io_list_flush(&dev->ctrl_wr_list, cl);
303 dev_dbg(&dev->pdev->dev, "free connect cb memory.");
309 * mei_start_read - the start read client message function.
311 * @dev: the device structure
312 * @if_num: minor number
313 * @cl: private data of the file object
315 * returns 0 on success, <0 on failure.
317 int mei_start_read(struct mei_device *dev, struct mei_cl *cl)
319 struct mei_cl_cb *cb;
323 if (cl->state != MEI_FILE_CONNECTED)
326 if (dev->dev_state != MEI_DEV_ENABLED)
329 if (cl->read_pending || cl->read_cb) {
330 dev_dbg(&dev->pdev->dev, "read is pending.\n");
333 i = mei_me_cl_by_id(dev, cl->me_client_id);
335 dev_err(&dev->pdev->dev, "no such me client %d\n",
340 cb = mei_io_cb_init(cl, NULL);
344 rets = mei_io_cb_alloc_resp_buf(cb,
345 dev->me_clients[i].props.max_msg_length);
349 cb->fop_type = MEI_FOP_READ;
351 if (dev->mei_host_buffer_is_empty) {
352 dev->mei_host_buffer_is_empty = false;
353 if (mei_send_flow_control(dev, cl)) {
357 list_add_tail(&cb->list, &dev->read_list.list);
359 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);