]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/misc/mei/iorw.c
mei: amthif: prefix cb list with amthif
[mirror_ubuntu-bionic-kernel.git] / drivers / misc / mei / iorw.c
CommitLineData
ab841160
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
733ba91c 4 * Copyright (c) 2003-2012, Intel Corporation.
ab841160
OW
5 *
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.
9 *
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
13 * more details.
14 *
15 */
16
17
18#include <linux/kernel.h>
19#include <linux/fs.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>
34
35
36#include "mei_dev.h"
37#include "hw.h"
4f3afe1d 38#include <linux/mei.h>
ab841160 39#include "interface.h"
ab841160 40
601a1efa
TW
41/**
42 * mei_io_cb_free - free mei_cb_private related memory
43 *
44 * @cb: mei callback struct
45 */
46void mei_io_cb_free(struct mei_cl_cb *cb)
47{
48 if (cb == NULL)
49 return;
50
51 kfree(cb->request_buffer.data);
52 kfree(cb->response_buffer.data);
53 kfree(cb);
54}
664df38b
TW
55/**
56 * mei_io_cb_init - allocate and initialize io callback
57 *
58 * @cl - mei client
59 * @file: pointer to file structure
60 *
61 * returns mei_cl_cb pointer or NULL;
62 */
63struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
64{
65 struct mei_cl_cb *cb;
66
67 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
68 if (!cb)
69 return NULL;
70
71 mei_io_list_init(cb);
72
73 cb->file_object = fp;
74 cb->file_private = cl;
75 cb->buf_idx = 0;
76 return cb;
77}
78
79
80/**
81 * mei_io_cb_alloc_req_buf - allocate request buffer
82 *
83 * @cb - io callback structure
84 * @size: size of the buffer
85 *
86 * returns 0 on success
87 * -EINVAL if cb is NULL
88 * -ENOMEM if allocation failed
89 */
90int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
91{
92 if (!cb)
93 return -EINVAL;
94
95 if (length == 0)
96 return 0;
97
98 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
99 if (!cb->request_buffer.data)
100 return -ENOMEM;
101 cb->request_buffer.size = length;
102 return 0;
103}
104/**
105 * mei_io_cb_alloc_req_buf - allocate respose buffer
106 *
107 * @cb - io callback structure
108 * @size: size of the buffer
109 *
110 * returns 0 on success
111 * -EINVAL if cb is NULL
112 * -ENOMEM if allocation failed
113 */
114int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
115{
116 if (!cb)
117 return -EINVAL;
118
119 if (length == 0)
120 return 0;
121
122 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
123 if (!cb->response_buffer.data)
124 return -ENOMEM;
125 cb->response_buffer.size = length;
126 return 0;
127}
128
601a1efa 129
07b509b7
TW
130/**
131 * mei_me_cl_by_id return index to me_clients for client_id
132 *
133 * @dev: the device structure
134 * @client_id: me client id
135 *
136 * Locking: called under "dev->device_lock" lock
137 *
138 * returns index on success, -ENOENT on failure.
139 */
ab841160 140
07b509b7
TW
141int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
142{
143 int i;
144 for (i = 0; i < dev->me_clients_num; i++)
145 if (dev->me_clients[i].client_id == client_id)
146 break;
147 if (WARN_ON(dev->me_clients[i].client_id != client_id))
148 return -ENOENT;
149
150 if (i == dev->me_clients_num)
151 return -ENOENT;
152
153 return i;
154}
ab841160
OW
155
156/**
157 * mei_ioctl_connect_client - the connect to fw client IOCTL function
158 *
159 * @dev: the device structure
160 * @data: IOCTL connect data, input and output parameters
161 * @file: private data of the file object
162 *
163 * Locking: called under "dev->device_lock" lock
164 *
165 * returns 0 on success, <0 on failure.
166 */
167int mei_ioctl_connect_client(struct file *file,
168 struct mei_connect_client_data *data)
169{
170 struct mei_device *dev;
171 struct mei_cl_cb *cb;
172 struct mei_client *client;
173 struct mei_cl *cl;
174 struct mei_cl *cl_pos = NULL;
175 struct mei_cl *cl_next = NULL;
3870c320 176 long timeout = mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT);
ab841160
OW
177 int i;
178 int err;
179 int rets;
180
181 cl = file->private_data;
182 if (WARN_ON(!cl || !cl->dev))
183 return -ENODEV;
184
185 dev = cl->dev;
186
187 dev_dbg(&dev->pdev->dev, "mei_ioctl_connect_client() Entry\n");
188
ab841160 189 /* buffered ioctl cb */
664df38b 190 cb = mei_io_cb_init(cl, file);
ab841160
OW
191 if (!cb) {
192 rets = -ENOMEM;
193 goto end;
194 }
ab841160
OW
195
196 cb->major_file_operations = MEI_IOCTL;
197
b210d750 198 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
199 rets = -ENODEV;
200 goto end;
201 }
202 if (cl->state != MEI_FILE_INITIALIZING &&
203 cl->state != MEI_FILE_DISCONNECTED) {
204 rets = -EBUSY;
205 goto end;
206 }
207
208 /* find ME client we're trying to connect to */
07b509b7 209 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
ab841160
OW
210 if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
211 cl->me_client_id = dev->me_clients[i].client_id;
212 cl->state = MEI_FILE_CONNECTING;
213 }
214
215 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
216 cl->me_client_id);
217 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
218 dev->me_clients[i].props.protocol_version);
219 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
220 dev->me_clients[i].props.max_msg_length);
221
5f9092f3
JM
222 /* if we're connecting to amthi client then we will use the
223 * existing connection
ab841160
OW
224 */
225 if (uuid_le_cmp(data->in_client_uuid, mei_amthi_guid) == 0) {
226 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
227 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
228 rets = -ENODEV;
229 goto end;
230 }
231 clear_bit(cl->host_client_id, dev->host_clients_map);
232 list_for_each_entry_safe(cl_pos, cl_next,
233 &dev->file_list, link) {
0288c7c9 234 if (mei_cl_cmp_id(cl, cl_pos)) {
ab841160
OW
235 dev_dbg(&dev->pdev->dev,
236 "remove file private data node host"
237 " client = %d, ME client = %d.\n",
238 cl_pos->host_client_id,
239 cl_pos->me_client_id);
240 list_del(&cl_pos->link);
241 }
242
243 }
244 dev_dbg(&dev->pdev->dev, "free file private data memory.\n");
245 kfree(cl);
246
247 cl = NULL;
248 file->private_data = &dev->iamthif_cl;
249
250 client = &data->out_client_properties;
251 client->max_msg_length =
252 dev->me_clients[i].props.max_msg_length;
253 client->protocol_version =
254 dev->me_clients[i].props.protocol_version;
255 rets = dev->iamthif_cl.status;
256
257 goto end;
258 }
259
260 if (cl->state != MEI_FILE_CONNECTING) {
261 rets = -ENODEV;
262 goto end;
263 }
264
265
266 /* prepare the output buffer */
267 client = &data->out_client_properties;
268 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
269 client->protocol_version = dev->me_clients[i].props.protocol_version;
270 dev_dbg(&dev->pdev->dev, "Can connect?\n");
271 if (dev->mei_host_buffer_is_empty
272 && !mei_other_client_is_connecting(dev, cl)) {
273 dev_dbg(&dev->pdev->dev, "Sending Connect Message\n");
eb9af0ac 274 dev->mei_host_buffer_is_empty = false;
1ccb7b62 275 if (mei_connect(dev, cl)) {
ab841160
OW
276 dev_dbg(&dev->pdev->dev, "Sending connect message - failed\n");
277 rets = -ENODEV;
278 goto end;
279 } else {
280 dev_dbg(&dev->pdev->dev, "Sending connect message - succeeded\n");
281 cl->timer_count = MEI_CONNECT_TIMEOUT;
fb601adb 282 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
ab841160
OW
283 }
284
285
286 } else {
287 dev_dbg(&dev->pdev->dev, "Queuing the connect request due to device busy\n");
ab841160 288 dev_dbg(&dev->pdev->dev, "add connect cb to control write list.\n");
fb601adb 289 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
ab841160
OW
290 }
291 mutex_unlock(&dev->device_lock);
292 err = wait_event_timeout(dev->wait_recvd_msg,
293 (MEI_FILE_CONNECTED == cl->state ||
3870c320 294 MEI_FILE_DISCONNECTED == cl->state), timeout);
ab841160
OW
295
296 mutex_lock(&dev->device_lock);
297 if (MEI_FILE_CONNECTED == cl->state) {
298 dev_dbg(&dev->pdev->dev, "successfully connected to FW client.\n");
299 rets = cl->status;
300 goto end;
301 } else {
302 dev_dbg(&dev->pdev->dev, "failed to connect to FW client.cl->state = %d.\n",
303 cl->state);
304 if (!err) {
305 dev_dbg(&dev->pdev->dev,
306 "wait_event_interruptible_timeout failed on client"
307 " connect message fw response message.\n");
308 }
309 rets = -EFAULT;
310
0288c7c9
TW
311 mei_io_list_flush(&dev->ctrl_rd_list, cl);
312 mei_io_list_flush(&dev->ctrl_wr_list, cl);
ab841160
OW
313 goto end;
314 }
315 rets = 0;
316end:
317 dev_dbg(&dev->pdev->dev, "free connect cb memory.");
601a1efa 318 mei_io_cb_free(cb);
ab841160
OW
319 return rets;
320}
321
ab841160
OW
322/**
323 * mei_start_read - the start read client message function.
324 *
325 * @dev: the device structure
326 * @if_num: minor number
327 * @cl: private data of the file object
328 *
329 * returns 0 on success, <0 on failure.
330 */
331int mei_start_read(struct mei_device *dev, struct mei_cl *cl)
332{
333 struct mei_cl_cb *cb;
664df38b 334 int rets;
ab841160
OW
335 int i;
336
337 if (cl->state != MEI_FILE_CONNECTED)
338 return -ENODEV;
339
b210d750 340 if (dev->dev_state != MEI_DEV_ENABLED)
ab841160
OW
341 return -ENODEV;
342
ab841160
OW
343 if (cl->read_pending || cl->read_cb) {
344 dev_dbg(&dev->pdev->dev, "read is pending.\n");
345 return -EBUSY;
346 }
664df38b
TW
347 i = mei_me_cl_by_id(dev, cl->me_client_id);
348 if (i < 0) {
349 dev_err(&dev->pdev->dev, "no such me client %d\n",
350 cl->me_client_id);
351 return -ENODEV;
352 }
ab841160 353
664df38b 354 cb = mei_io_cb_init(cl, NULL);
ab841160
OW
355 if (!cb)
356 return -ENOMEM;
357
664df38b
TW
358 rets = mei_io_cb_alloc_resp_buf(cb,
359 dev->me_clients[i].props.max_msg_length);
360 if (rets)
361 goto err;
ab841160 362
ab841160 363 cb->major_file_operations = MEI_READ;
ab841160
OW
364 cl->read_cb = cb;
365 if (dev->mei_host_buffer_is_empty) {
eb9af0ac 366 dev->mei_host_buffer_is_empty = false;
1ccb7b62 367 if (mei_send_flow_control(dev, cl)) {
ab841160 368 rets = -ENODEV;
664df38b 369 goto err;
ab841160 370 }
fb601adb 371 list_add_tail(&cb->list, &dev->read_list.list);
ab841160 372 } else {
fb601adb 373 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
ab841160
OW
374 }
375 return rets;
664df38b 376err:
601a1efa 377 mei_io_cb_free(cb);
ab841160
OW
378 return rets;
379}
380