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_me_cl_by_id return index to me_clients for client_id
44 * @dev: the device structure
45 * @client_id: me client id
47 * Locking: called under "dev->device_lock" lock
49 * returns index on success, -ENOENT on failure.
52 int mei_me_cl_by_id(struct mei_device
*dev
, u8 client_id
)
55 for (i
= 0; i
< dev
->me_clients_num
; i
++)
56 if (dev
->me_clients
[i
].client_id
== client_id
)
58 if (WARN_ON(dev
->me_clients
[i
].client_id
!= client_id
))
61 if (i
== dev
->me_clients_num
)
68 * mei_ioctl_connect_client - the connect to fw client IOCTL function
70 * @dev: the device structure
71 * @data: IOCTL connect data, input and output parameters
72 * @file: private data of the file object
74 * Locking: called under "dev->device_lock" lock
76 * returns 0 on success, <0 on failure.
78 int mei_ioctl_connect_client(struct file
*file
,
79 struct mei_connect_client_data
*data
)
81 struct mei_device
*dev
;
83 struct mei_client
*client
;
85 struct mei_cl
*cl_pos
= NULL
;
86 struct mei_cl
*cl_next
= NULL
;
87 long timeout
= CONNECT_TIMEOUT
;
92 cl
= file
->private_data
;
93 if (WARN_ON(!cl
|| !cl
->dev
))
98 dev_dbg(&dev
->pdev
->dev
, "mei_ioctl_connect_client() Entry\n");
101 /* buffered ioctl cb */
102 cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
107 INIT_LIST_HEAD(&cb
->cb_list
);
109 cb
->major_file_operations
= MEI_IOCTL
;
111 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
115 if (cl
->state
!= MEI_FILE_INITIALIZING
&&
116 cl
->state
!= MEI_FILE_DISCONNECTED
) {
121 /* find ME client we're trying to connect to */
122 i
= mei_me_cl_by_uuid(dev
, &data
->in_client_uuid
);
123 if (i
>= 0 && !dev
->me_clients
[i
].props
.fixed_address
) {
124 cl
->me_client_id
= dev
->me_clients
[i
].client_id
;
125 cl
->state
= MEI_FILE_CONNECTING
;
128 dev_dbg(&dev
->pdev
->dev
, "Connect to FW Client ID = %d\n",
130 dev_dbg(&dev
->pdev
->dev
, "FW Client - Protocol Version = %d\n",
131 dev
->me_clients
[i
].props
.protocol_version
);
132 dev_dbg(&dev
->pdev
->dev
, "FW Client - Max Msg Len = %d\n",
133 dev
->me_clients
[i
].props
.max_msg_length
);
135 /* if we're connecting to amthi client then we will use the
136 * existing connection
138 if (uuid_le_cmp(data
->in_client_uuid
, mei_amthi_guid
) == 0) {
139 dev_dbg(&dev
->pdev
->dev
, "FW Client is amthi\n");
140 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
144 clear_bit(cl
->host_client_id
, dev
->host_clients_map
);
145 list_for_each_entry_safe(cl_pos
, cl_next
,
146 &dev
->file_list
, link
) {
147 if (mei_cl_cmp_id(cl
, cl_pos
)) {
148 dev_dbg(&dev
->pdev
->dev
,
149 "remove file private data node host"
150 " client = %d, ME client = %d.\n",
151 cl_pos
->host_client_id
,
152 cl_pos
->me_client_id
);
153 list_del(&cl_pos
->link
);
157 dev_dbg(&dev
->pdev
->dev
, "free file private data memory.\n");
161 file
->private_data
= &dev
->iamthif_cl
;
163 client
= &data
->out_client_properties
;
164 client
->max_msg_length
=
165 dev
->me_clients
[i
].props
.max_msg_length
;
166 client
->protocol_version
=
167 dev
->me_clients
[i
].props
.protocol_version
;
168 rets
= dev
->iamthif_cl
.status
;
173 if (cl
->state
!= MEI_FILE_CONNECTING
) {
179 /* prepare the output buffer */
180 client
= &data
->out_client_properties
;
181 client
->max_msg_length
= dev
->me_clients
[i
].props
.max_msg_length
;
182 client
->protocol_version
= dev
->me_clients
[i
].props
.protocol_version
;
183 dev_dbg(&dev
->pdev
->dev
, "Can connect?\n");
184 if (dev
->mei_host_buffer_is_empty
185 && !mei_other_client_is_connecting(dev
, cl
)) {
186 dev_dbg(&dev
->pdev
->dev
, "Sending Connect Message\n");
187 dev
->mei_host_buffer_is_empty
= false;
188 if (mei_connect(dev
, cl
)) {
189 dev_dbg(&dev
->pdev
->dev
, "Sending connect message - failed\n");
193 dev_dbg(&dev
->pdev
->dev
, "Sending connect message - succeeded\n");
194 cl
->timer_count
= MEI_CONNECT_TIMEOUT
;
195 cb
->file_private
= cl
;
196 list_add_tail(&cb
->cb_list
,
197 &dev
->ctrl_rd_list
.mei_cb
.
203 dev_dbg(&dev
->pdev
->dev
, "Queuing the connect request due to device busy\n");
204 cb
->file_private
= cl
;
205 dev_dbg(&dev
->pdev
->dev
, "add connect cb to control write list.\n");
206 list_add_tail(&cb
->cb_list
,
207 &dev
->ctrl_wr_list
.mei_cb
.cb_list
);
209 mutex_unlock(&dev
->device_lock
);
210 err
= wait_event_timeout(dev
->wait_recvd_msg
,
211 (MEI_FILE_CONNECTED
== cl
->state
||
212 MEI_FILE_DISCONNECTED
== cl
->state
),
215 mutex_lock(&dev
->device_lock
);
216 if (MEI_FILE_CONNECTED
== cl
->state
) {
217 dev_dbg(&dev
->pdev
->dev
, "successfully connected to FW client.\n");
221 dev_dbg(&dev
->pdev
->dev
, "failed to connect to FW client.cl->state = %d.\n",
224 dev_dbg(&dev
->pdev
->dev
,
225 "wait_event_interruptible_timeout failed on client"
226 " connect message fw response message.\n");
230 mei_io_list_flush(&dev
->ctrl_rd_list
, cl
);
231 mei_io_list_flush(&dev
->ctrl_wr_list
, cl
);
236 dev_dbg(&dev
->pdev
->dev
, "free connect cb memory.");
242 * find_amthi_read_list_entry - finds a amthilist entry for current file
244 * @dev: the device structure
245 * @file: pointer to file object
247 * returns returned a list entry on success, NULL on failure.
249 struct mei_cl_cb
*find_amthi_read_list_entry(
250 struct mei_device
*dev
,
253 struct mei_cl
*cl_temp
;
254 struct mei_cl_cb
*pos
= NULL
;
255 struct mei_cl_cb
*next
= NULL
;
257 list_for_each_entry_safe(pos
, next
,
258 &dev
->amthi_read_complete_list
.mei_cb
.cb_list
, cb_list
) {
259 cl_temp
= (struct mei_cl
*)pos
->file_private
;
260 if (cl_temp
&& cl_temp
== &dev
->iamthif_cl
&&
261 pos
->file_object
== file
)
268 * amthi_read - read data from AMTHI client
270 * @dev: the device structure
271 * @if_num: minor number
272 * @file: pointer to file object
273 * @*ubuf: pointer to user data in user space
274 * @length: data length to read
275 * @offset: data read offset
277 * Locking: called under "dev->device_lock" lock
280 * returned data length on success,
281 * zero if no data to read,
282 * negative on failure.
284 int amthi_read(struct mei_device
*dev
, struct file
*file
,
285 char __user
*ubuf
, size_t length
, loff_t
*offset
)
289 struct mei_cl_cb
*cb
= NULL
;
290 struct mei_cl
*cl
= file
->private_data
;
291 unsigned long timeout
;
294 /* Only Posible if we are in timeout */
295 if (!cl
|| cl
!= &dev
->iamthif_cl
) {
296 dev_dbg(&dev
->pdev
->dev
, "bad file ext.\n");
300 i
= mei_me_cl_by_id(dev
, dev
->iamthif_cl
.me_client_id
);
303 dev_dbg(&dev
->pdev
->dev
, "amthi client not found.\n");
306 dev_dbg(&dev
->pdev
->dev
, "checking amthi data\n");
307 cb
= find_amthi_read_list_entry(dev
, file
);
309 /* Check for if we can block or not*/
310 if (cb
== NULL
&& file
->f_flags
& O_NONBLOCK
)
314 dev_dbg(&dev
->pdev
->dev
, "waiting for amthi data\n");
316 /* unlock the Mutex */
317 mutex_unlock(&dev
->device_lock
);
319 wait_ret
= wait_event_interruptible(dev
->iamthif_cl
.wait
,
320 (cb
= find_amthi_read_list_entry(dev
, file
)));
325 dev_dbg(&dev
->pdev
->dev
, "woke up from sleep\n");
327 /* Locking again the Mutex */
328 mutex_lock(&dev
->device_lock
);
332 dev_dbg(&dev
->pdev
->dev
, "Got amthi data\n");
333 dev
->iamthif_timer
= 0;
336 timeout
= cb
->read_time
+ msecs_to_jiffies(IAMTHIF_READ_TIMER
);
337 dev_dbg(&dev
->pdev
->dev
, "amthi timeout = %lud\n",
340 if (time_after(jiffies
, timeout
)) {
341 dev_dbg(&dev
->pdev
->dev
, "amthi Time out\n");
342 /* 15 sec for the message has expired */
343 list_del(&cb
->cb_list
);
348 /* if the whole message will fit remove it from the list */
349 if (cb
->information
>= *offset
&& length
>= (cb
->information
- *offset
))
350 list_del(&cb
->cb_list
);
351 else if (cb
->information
> 0 && cb
->information
<= *offset
) {
352 /* end of the message has been reached */
353 list_del(&cb
->cb_list
);
357 /* else means that not full buffer will be read and do not
358 * remove message from deletion list
361 dev_dbg(&dev
->pdev
->dev
, "amthi cb->response_buffer size - %d\n",
362 cb
->response_buffer
.size
);
363 dev_dbg(&dev
->pdev
->dev
, "amthi cb->information - %lu\n",
366 /* length is being turncated to PAGE_SIZE, however,
367 * the information may be longer */
368 length
= min_t(size_t, length
, (cb
->information
- *offset
));
370 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
))
374 if ((*offset
+ length
) < cb
->information
) {
380 dev_dbg(&dev
->pdev
->dev
, "free amthi cb memory.\n");
382 mei_free_cb_private(cb
);
388 * mei_start_read - the start read client message function.
390 * @dev: the device structure
391 * @if_num: minor number
392 * @cl: private data of the file object
394 * returns 0 on success, <0 on failure.
396 int mei_start_read(struct mei_device
*dev
, struct mei_cl
*cl
)
398 struct mei_cl_cb
*cb
;
402 if (cl
->state
!= MEI_FILE_CONNECTED
)
405 if (dev
->dev_state
!= MEI_DEV_ENABLED
)
408 dev_dbg(&dev
->pdev
->dev
, "check if read is pending.\n");
409 if (cl
->read_pending
|| cl
->read_cb
) {
410 dev_dbg(&dev
->pdev
->dev
, "read is pending.\n");
414 cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
418 dev_dbg(&dev
->pdev
->dev
, "allocation call back successful. host client = %d, ME client = %d\n",
419 cl
->host_client_id
, cl
->me_client_id
);
420 i
= mei_me_cl_by_id(dev
, cl
->me_client_id
);
426 cb
->response_buffer
.size
= dev
->me_clients
[i
].props
.max_msg_length
;
427 cb
->response_buffer
.data
=
428 kmalloc(cb
->response_buffer
.size
, GFP_KERNEL
);
429 if (!cb
->response_buffer
.data
) {
433 dev_dbg(&dev
->pdev
->dev
, "allocation call back data success.\n");
434 cb
->major_file_operations
= MEI_READ
;
435 /* make sure information is zero before we start */
437 cb
->file_private
= (void *) cl
;
439 if (dev
->mei_host_buffer_is_empty
) {
440 dev
->mei_host_buffer_is_empty
= false;
441 if (mei_send_flow_control(dev
, cl
)) {
445 list_add_tail(&cb
->cb_list
, &dev
->read_list
.mei_cb
.cb_list
);
447 list_add_tail(&cb
->cb_list
, &dev
->ctrl_wr_list
.mei_cb
.cb_list
);
451 mei_free_cb_private(cb
);
456 * amthi_write - write iamthif data to amthi client
458 * @dev: the device structure
459 * @cb: mei call back struct
461 * returns 0 on success, <0 on failure.
463 int amthi_write(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
465 struct mei_msg_hdr mei_hdr
;
471 dev_dbg(&dev
->pdev
->dev
, "write data to amthi client.\n");
473 dev
->iamthif_state
= MEI_IAMTHIF_WRITING
;
474 dev
->iamthif_current_cb
= cb
;
475 dev
->iamthif_file_object
= cb
->file_object
;
476 dev
->iamthif_canceled
= false;
477 dev
->iamthif_ioctl
= true;
478 dev
->iamthif_msg_buf_size
= cb
->request_buffer
.size
;
479 memcpy(dev
->iamthif_msg_buf
, cb
->request_buffer
.data
,
480 cb
->request_buffer
.size
);
482 ret
= mei_flow_ctrl_creds(dev
, &dev
->iamthif_cl
);
486 if (ret
&& dev
->mei_host_buffer_is_empty
) {
488 dev
->mei_host_buffer_is_empty
= false;
489 if (cb
->request_buffer
.size
> mei_hbuf_max_data(dev
)) {
490 mei_hdr
.length
= mei_hbuf_max_data(dev
);
491 mei_hdr
.msg_complete
= 0;
493 mei_hdr
.length
= cb
->request_buffer
.size
;
494 mei_hdr
.msg_complete
= 1;
497 mei_hdr
.host_addr
= dev
->iamthif_cl
.host_client_id
;
498 mei_hdr
.me_addr
= dev
->iamthif_cl
.me_client_id
;
499 mei_hdr
.reserved
= 0;
500 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
501 if (mei_write_message(dev
, &mei_hdr
,
502 (unsigned char *)(dev
->iamthif_msg_buf
),
506 if (mei_hdr
.msg_complete
) {
507 if (mei_flow_ctrl_reduce(dev
, &dev
->iamthif_cl
))
509 dev
->iamthif_flow_control_pending
= true;
510 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
511 dev_dbg(&dev
->pdev
->dev
, "add amthi cb to write waiting list\n");
512 dev
->iamthif_current_cb
= cb
;
513 dev
->iamthif_file_object
= cb
->file_object
;
514 list_add_tail(&cb
->cb_list
,
515 &dev
->write_waiting_list
.mei_cb
.cb_list
);
517 dev_dbg(&dev
->pdev
->dev
, "message does not complete, "
518 "so add amthi cb to write list.\n");
519 list_add_tail(&cb
->cb_list
,
520 &dev
->write_list
.mei_cb
.cb_list
);
523 if (!(dev
->mei_host_buffer_is_empty
))
524 dev_dbg(&dev
->pdev
->dev
, "host buffer is not empty");
526 dev_dbg(&dev
->pdev
->dev
, "No flow control credentials, "
527 "so add iamthif cb to write list.\n");
528 list_add_tail(&cb
->cb_list
, &dev
->write_list
.mei_cb
.cb_list
);
534 * iamthif_ioctl_send_msg - send cmd data to amthi client
536 * @dev: the device structure
538 * returns 0 on success, <0 on failure.
540 void mei_run_next_iamthif_cmd(struct mei_device
*dev
)
542 struct mei_cl
*cl_tmp
;
543 struct mei_cl_cb
*pos
= NULL
;
544 struct mei_cl_cb
*next
= NULL
;
550 dev
->iamthif_msg_buf_size
= 0;
551 dev
->iamthif_msg_buf_index
= 0;
552 dev
->iamthif_canceled
= false;
553 dev
->iamthif_ioctl
= true;
554 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
555 dev
->iamthif_timer
= 0;
556 dev
->iamthif_file_object
= NULL
;
558 dev_dbg(&dev
->pdev
->dev
, "complete amthi cmd_list cb.\n");
560 list_for_each_entry_safe(pos
, next
,
561 &dev
->amthi_cmd_list
.mei_cb
.cb_list
, cb_list
) {
562 list_del(&pos
->cb_list
);
563 cl_tmp
= (struct mei_cl
*)pos
->file_private
;
565 if (cl_tmp
&& cl_tmp
== &dev
->iamthif_cl
) {
566 status
= amthi_write(dev
, pos
);
568 dev_dbg(&dev
->pdev
->dev
,
569 "amthi write failed status = %d\n",
579 * mei_free_cb_private - free mei_cb_private related memory
581 * @cb: mei callback struct
583 void mei_free_cb_private(struct mei_cl_cb
*cb
)
588 kfree(cb
->request_buffer
.data
);
589 kfree(cb
->response_buffer
.data
);