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/mei.h>
27 * mei_hbm_me_cl_allocate - allocates storage for me clients
29 * @dev: the device structure
33 static void mei_hbm_me_cl_allocate(struct mei_device
*dev
)
35 struct mei_me_client
*clients
;
38 dev
->me_clients_num
= 0;
39 dev
->me_client_presentation_num
= 0;
40 dev
->me_client_index
= 0;
42 /* count how many ME clients we have */
43 for_each_set_bit(b
, dev
->me_clients_map
, MEI_CLIENTS_MAX
)
44 dev
->me_clients_num
++;
46 if (dev
->me_clients_num
== 0)
49 kfree(dev
->me_clients
);
50 dev
->me_clients
= NULL
;
52 dev_dbg(&dev
->pdev
->dev
, "memory allocation for ME clients size=%zd.\n",
53 dev
->me_clients_num
* sizeof(struct mei_me_client
));
54 /* allocate storage for ME clients representation */
55 clients
= kcalloc(dev
->me_clients_num
,
56 sizeof(struct mei_me_client
), GFP_KERNEL
);
58 dev_err(&dev
->pdev
->dev
, "memory allocation for ME clients failed.\n");
59 dev
->dev_state
= MEI_DEV_RESETTING
;
63 dev
->me_clients
= clients
;
68 * mei_hbm_cl_hdr - construct client hbm header
71 * @hbm_cmd: host bus message command
72 * @buf: buffer for cl header
76 void mei_hbm_cl_hdr(struct mei_cl
*cl
, u8 hbm_cmd
, void *buf
, size_t len
)
78 struct mei_hbm_cl_cmd
*cmd
= buf
;
82 cmd
->hbm_cmd
= hbm_cmd
;
83 cmd
->host_addr
= cl
->host_client_id
;
84 cmd
->me_addr
= cl
->me_client_id
;
88 * same_disconn_addr - tells if they have the same address
90 * @file: private data of the file object.
91 * @disconn: disconnection request.
93 * returns true if addres are same
96 bool mei_hbm_cl_addr_equal(struct mei_cl
*cl
, void *buf
)
98 struct mei_hbm_cl_cmd
*cmd
= buf
;
99 return cl
->host_client_id
== cmd
->host_addr
&&
100 cl
->me_client_id
== cmd
->me_addr
;
105 * is_treat_specially_client - checks if the message belongs
106 * to the file private data.
108 * @cl: private data of the file object
109 * @rs: connect response bus message
112 static bool is_treat_specially_client(struct mei_cl
*cl
,
113 struct hbm_client_connect_response
*rs
)
115 if (mei_hbm_cl_addr_equal(cl
, rs
)) {
117 cl
->state
= MEI_FILE_CONNECTED
;
121 cl
->state
= MEI_FILE_DISCONNECTED
;
122 cl
->status
= -ENODEV
;
132 * mei_hbm_idle - set hbm to idle state
134 * @dev: the device structure
136 void mei_hbm_idle(struct mei_device
*dev
)
138 dev
->init_clients_timer
= 0;
139 dev
->hbm_state
= MEI_HBM_IDLE
;
142 int mei_hbm_start_wait(struct mei_device
*dev
)
145 if (dev
->hbm_state
> MEI_HBM_START
)
148 mutex_unlock(&dev
->device_lock
);
149 ret
= wait_event_interruptible_timeout(dev
->wait_recvd_msg
,
150 dev
->hbm_state
== MEI_HBM_IDLE
||
151 dev
->hbm_state
> MEI_HBM_START
,
152 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT
));
153 mutex_lock(&dev
->device_lock
);
155 if (ret
<= 0 && (dev
->hbm_state
<= MEI_HBM_START
)) {
156 dev
->hbm_state
= MEI_HBM_IDLE
;
157 dev_err(&dev
->pdev
->dev
, "waiting for mei start failed\n");
164 * mei_hbm_start_req - sends start request message.
166 * @dev: the device structure
168 int mei_hbm_start_req(struct mei_device
*dev
)
170 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
171 struct hbm_host_version_request
*start_req
;
172 const size_t len
= sizeof(struct hbm_host_version_request
);
174 mei_hbm_hdr(mei_hdr
, len
);
176 /* host start message */
177 start_req
= (struct hbm_host_version_request
*)dev
->wr_msg
.data
;
178 memset(start_req
, 0, len
);
179 start_req
->hbm_cmd
= HOST_START_REQ_CMD
;
180 start_req
->host_version
.major_version
= HBM_MAJOR_VERSION
;
181 start_req
->host_version
.minor_version
= HBM_MINOR_VERSION
;
183 dev
->hbm_state
= MEI_HBM_IDLE
;
184 if (mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
)) {
185 dev_err(&dev
->pdev
->dev
, "version message write failed\n");
186 dev
->dev_state
= MEI_DEV_RESETTING
;
190 dev
->hbm_state
= MEI_HBM_START
;
191 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
196 * mei_hbm_enum_clients_req - sends enumeration client request message.
198 * @dev: the device structure
202 static void mei_hbm_enum_clients_req(struct mei_device
*dev
)
204 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
205 struct hbm_host_enum_request
*enum_req
;
206 const size_t len
= sizeof(struct hbm_host_enum_request
);
207 /* enumerate clients */
208 mei_hbm_hdr(mei_hdr
, len
);
210 enum_req
= (struct hbm_host_enum_request
*)dev
->wr_msg
.data
;
211 memset(enum_req
, 0, len
);
212 enum_req
->hbm_cmd
= HOST_ENUM_REQ_CMD
;
214 if (mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
)) {
215 dev
->dev_state
= MEI_DEV_RESETTING
;
216 dev_err(&dev
->pdev
->dev
, "enumeration request write failed.\n");
219 dev
->hbm_state
= MEI_HBM_ENUM_CLIENTS
;
220 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
225 * mei_hbm_prop_req - request property for a single client
227 * @dev: the device structure
232 static int mei_hbm_prop_req(struct mei_device
*dev
)
235 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
236 struct hbm_props_request
*prop_req
;
237 const size_t len
= sizeof(struct hbm_props_request
);
238 unsigned long next_client_index
;
239 unsigned long client_num
;
242 client_num
= dev
->me_client_presentation_num
;
244 next_client_index
= find_next_bit(dev
->me_clients_map
, MEI_CLIENTS_MAX
,
245 dev
->me_client_index
);
247 /* We got all client properties */
248 if (next_client_index
== MEI_CLIENTS_MAX
) {
249 dev
->hbm_state
= MEI_HBM_STARTED
;
250 schedule_work(&dev
->init_work
);
255 dev
->me_clients
[client_num
].client_id
= next_client_index
;
256 dev
->me_clients
[client_num
].mei_flow_ctrl_creds
= 0;
258 mei_hbm_hdr(mei_hdr
, len
);
259 prop_req
= (struct hbm_props_request
*)dev
->wr_msg
.data
;
261 memset(prop_req
, 0, sizeof(struct hbm_props_request
));
264 prop_req
->hbm_cmd
= HOST_CLIENT_PROPERTIES_REQ_CMD
;
265 prop_req
->address
= next_client_index
;
267 if (mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
)) {
268 dev
->dev_state
= MEI_DEV_RESETTING
;
269 dev_err(&dev
->pdev
->dev
, "properties request write failed\n");
275 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
276 dev
->me_client_index
= next_client_index
;
282 * mei_hbm_stop_req_prepare - perpare stop request message
285 * @mei_hdr - mei message header
286 * @data - hbm message body buffer
288 static void mei_hbm_stop_req_prepare(struct mei_device
*dev
,
289 struct mei_msg_hdr
*mei_hdr
, unsigned char *data
)
291 struct hbm_host_stop_request
*req
=
292 (struct hbm_host_stop_request
*)data
;
293 const size_t len
= sizeof(struct hbm_host_stop_request
);
295 mei_hbm_hdr(mei_hdr
, len
);
298 req
->hbm_cmd
= HOST_STOP_REQ_CMD
;
299 req
->reason
= DRIVER_STOP_REQUEST
;
303 * mei_hbm_cl_flow_control_req - sends flow control requst.
305 * @dev: the device structure
308 * This function returns -EIO on write failure
310 int mei_hbm_cl_flow_control_req(struct mei_device
*dev
, struct mei_cl
*cl
)
312 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
313 const size_t len
= sizeof(struct hbm_flow_control
);
315 mei_hbm_hdr(mei_hdr
, len
);
316 mei_hbm_cl_hdr(cl
, MEI_FLOW_CONTROL_CMD
, dev
->wr_msg
.data
, len
);
318 dev_dbg(&dev
->pdev
->dev
, "sending flow control host client = %d, ME client = %d\n",
319 cl
->host_client_id
, cl
->me_client_id
);
321 return mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
);
325 * mei_hbm_add_single_flow_creds - adds single buffer credentials.
327 * @dev: the device structure
328 * @flow: flow control.
330 static void mei_hbm_add_single_flow_creds(struct mei_device
*dev
,
331 struct hbm_flow_control
*flow
)
333 struct mei_me_client
*client
;
336 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
337 client
= &dev
->me_clients
[i
];
338 if (client
&& flow
->me_addr
== client
->client_id
) {
339 if (client
->props
.single_recv_buf
) {
340 client
->mei_flow_ctrl_creds
++;
341 dev_dbg(&dev
->pdev
->dev
, "recv flow ctrl msg ME %d (single).\n",
343 dev_dbg(&dev
->pdev
->dev
, "flow control credentials =%d.\n",
344 client
->mei_flow_ctrl_creds
);
346 BUG(); /* error in flow control */
353 * mei_hbm_cl_flow_control_res - flow control response from me
355 * @dev: the device structure
356 * @flow_control: flow control response bus message
358 static void mei_hbm_cl_flow_control_res(struct mei_device
*dev
,
359 struct hbm_flow_control
*flow_control
)
361 struct mei_cl
*cl
= NULL
;
362 struct mei_cl
*next
= NULL
;
364 if (!flow_control
->host_addr
) {
365 /* single receive buffer */
366 mei_hbm_add_single_flow_creds(dev
, flow_control
);
370 /* normal connection */
371 list_for_each_entry_safe(cl
, next
, &dev
->file_list
, link
) {
372 if (mei_hbm_cl_addr_equal(cl
, flow_control
)) {
373 cl
->mei_flow_ctrl_creds
++;
374 dev_dbg(&dev
->pdev
->dev
, "flow ctrl msg for host %d ME %d.\n",
375 flow_control
->host_addr
, flow_control
->me_addr
);
376 dev_dbg(&dev
->pdev
->dev
, "flow control credentials = %d.\n",
377 cl
->mei_flow_ctrl_creds
);
385 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
387 * @dev: the device structure
388 * @cl: a client to disconnect from
390 * This function returns -EIO on write failure
392 int mei_hbm_cl_disconnect_req(struct mei_device
*dev
, struct mei_cl
*cl
)
394 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
395 const size_t len
= sizeof(struct hbm_client_connect_request
);
397 mei_hbm_hdr(mei_hdr
, len
);
398 mei_hbm_cl_hdr(cl
, CLIENT_DISCONNECT_REQ_CMD
, dev
->wr_msg
.data
, len
);
400 return mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
);
404 * mei_hbm_cl_disconnect_res - disconnect response from ME
406 * @dev: the device structure
407 * @rs: disconnect response bus message
409 static void mei_hbm_cl_disconnect_res(struct mei_device
*dev
,
410 struct hbm_client_connect_response
*rs
)
413 struct mei_cl_cb
*pos
= NULL
, *next
= NULL
;
415 dev_dbg(&dev
->pdev
->dev
,
416 "disconnect_response:\n"
424 list_for_each_entry_safe(pos
, next
, &dev
->ctrl_rd_list
.list
, list
) {
428 list_del(&pos
->list
);
432 dev_dbg(&dev
->pdev
->dev
, "list_for_each_entry_safe in ctrl_rd_list.\n");
433 if (mei_hbm_cl_addr_equal(cl
, rs
)) {
434 list_del(&pos
->list
);
436 cl
->state
= MEI_FILE_DISCONNECTED
;
446 * mei_hbm_cl_connect_req - send connection request to specific me client
448 * @dev: the device structure
449 * @cl: a client to connect to
451 * returns -EIO on write failure
453 int mei_hbm_cl_connect_req(struct mei_device
*dev
, struct mei_cl
*cl
)
455 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
456 const size_t len
= sizeof(struct hbm_client_connect_request
);
458 mei_hbm_hdr(mei_hdr
, len
);
459 mei_hbm_cl_hdr(cl
, CLIENT_CONNECT_REQ_CMD
, dev
->wr_msg
.data
, len
);
461 return mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
);
465 * mei_hbm_cl_connect_res - connect resposne from the ME
467 * @dev: the device structure
468 * @rs: connect response bus message
470 static void mei_hbm_cl_connect_res(struct mei_device
*dev
,
471 struct hbm_client_connect_response
*rs
)
475 struct mei_cl_cb
*pos
= NULL
, *next
= NULL
;
477 dev_dbg(&dev
->pdev
->dev
,
478 "connect_response:\n"
486 /* if WD or iamthif client treat specially */
488 if (is_treat_specially_client(&dev
->wd_cl
, rs
)) {
489 dev_dbg(&dev
->pdev
->dev
, "successfully connected to WD client.\n");
490 mei_watchdog_register(dev
);
495 if (is_treat_specially_client(&dev
->iamthif_cl
, rs
)) {
496 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
499 list_for_each_entry_safe(pos
, next
, &dev
->ctrl_rd_list
.list
, list
) {
503 list_del(&pos
->list
);
506 if (pos
->fop_type
== MEI_FOP_IOCTL
) {
507 if (is_treat_specially_client(cl
, rs
)) {
508 list_del(&pos
->list
);
519 * mei_hbm_fw_disconnect_req - disconnect request initiated by me
520 * host sends disoconnect response
522 * @dev: the device structure.
523 * @disconnect_req: disconnect request bus message from the me
525 static void mei_hbm_fw_disconnect_req(struct mei_device
*dev
,
526 struct hbm_client_connect_request
*disconnect_req
)
528 struct mei_cl
*cl
, *next
;
529 const size_t len
= sizeof(struct hbm_client_connect_response
);
531 list_for_each_entry_safe(cl
, next
, &dev
->file_list
, link
) {
532 if (mei_hbm_cl_addr_equal(cl
, disconnect_req
)) {
533 dev_dbg(&dev
->pdev
->dev
, "disconnect request host client %d ME client %d.\n",
534 disconnect_req
->host_addr
,
535 disconnect_req
->me_addr
);
536 cl
->state
= MEI_FILE_DISCONNECTED
;
538 if (cl
== &dev
->wd_cl
)
539 dev
->wd_pending
= false;
540 else if (cl
== &dev
->iamthif_cl
)
541 dev
->iamthif_timer
= 0;
543 /* prepare disconnect response */
544 mei_hbm_hdr(&dev
->wr_ext_msg
.hdr
, len
);
545 mei_hbm_cl_hdr(cl
, CLIENT_DISCONNECT_RES_CMD
,
546 dev
->wr_ext_msg
.data
, len
);
554 * mei_hbm_version_is_supported - checks whether the driver can
555 * support the hbm version of the device
557 * @dev: the device structure
558 * returns true if driver can support hbm version of the device
560 bool mei_hbm_version_is_supported(struct mei_device
*dev
)
562 return (dev
->version
.major_version
< HBM_MAJOR_VERSION
) ||
563 (dev
->version
.major_version
== HBM_MAJOR_VERSION
&&
564 dev
->version
.minor_version
<= HBM_MINOR_VERSION
);
568 * mei_hbm_dispatch - bottom half read routine after ISR to
569 * handle the read bus message cmd processing.
571 * @dev: the device structure
572 * @mei_hdr: header of bus message
574 void mei_hbm_dispatch(struct mei_device
*dev
, struct mei_msg_hdr
*hdr
)
576 struct mei_bus_message
*mei_msg
;
577 struct mei_me_client
*me_client
;
578 struct hbm_host_version_response
*version_res
;
579 struct hbm_client_connect_response
*connect_res
;
580 struct hbm_client_connect_response
*disconnect_res
;
581 struct hbm_client_connect_request
*disconnect_req
;
582 struct hbm_flow_control
*flow_control
;
583 struct hbm_props_response
*props_res
;
584 struct hbm_host_enum_response
*enum_res
;
586 /* read the message to our buffer */
587 BUG_ON(hdr
->length
>= sizeof(dev
->rd_msg_buf
));
588 mei_read_slots(dev
, dev
->rd_msg_buf
, hdr
->length
);
589 mei_msg
= (struct mei_bus_message
*)dev
->rd_msg_buf
;
591 /* ignore spurious message and prevent reset nesting
592 * hbm is put to idle during system reset
594 if (dev
->hbm_state
== MEI_HBM_IDLE
) {
595 dev_dbg(&dev
->pdev
->dev
, "hbm: state is idle ignore spurious messages\n");
599 switch (mei_msg
->hbm_cmd
) {
600 case HOST_START_RES_CMD
:
601 version_res
= (struct hbm_host_version_response
*)mei_msg
;
603 dev_dbg(&dev
->pdev
->dev
, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
604 HBM_MAJOR_VERSION
, HBM_MINOR_VERSION
,
605 version_res
->me_max_version
.major_version
,
606 version_res
->me_max_version
.minor_version
);
608 if (version_res
->host_version_supported
) {
609 dev
->version
.major_version
= HBM_MAJOR_VERSION
;
610 dev
->version
.minor_version
= HBM_MINOR_VERSION
;
612 dev
->version
.major_version
=
613 version_res
->me_max_version
.major_version
;
614 dev
->version
.minor_version
=
615 version_res
->me_max_version
.minor_version
;
618 if (!mei_hbm_version_is_supported(dev
)) {
619 dev_warn(&dev
->pdev
->dev
, "hbm version mismatch: stopping the driver.\n");
621 dev
->hbm_state
= MEI_HBM_STOP
;
622 mei_hbm_stop_req_prepare(dev
, &dev
->wr_msg
.hdr
,
624 mei_write_message(dev
, &dev
->wr_msg
.hdr
,
630 if (dev
->dev_state
== MEI_DEV_INIT_CLIENTS
&&
631 dev
->hbm_state
== MEI_HBM_START
) {
632 dev
->init_clients_timer
= 0;
633 mei_hbm_enum_clients_req(dev
);
635 dev_err(&dev
->pdev
->dev
, "reset: wrong host start response\n");
640 wake_up_interruptible(&dev
->wait_recvd_msg
);
641 dev_dbg(&dev
->pdev
->dev
, "host start response message received.\n");
644 case CLIENT_CONNECT_RES_CMD
:
645 connect_res
= (struct hbm_client_connect_response
*) mei_msg
;
646 mei_hbm_cl_connect_res(dev
, connect_res
);
647 dev_dbg(&dev
->pdev
->dev
, "client connect response message received.\n");
648 wake_up(&dev
->wait_recvd_msg
);
651 case CLIENT_DISCONNECT_RES_CMD
:
652 disconnect_res
= (struct hbm_client_connect_response
*) mei_msg
;
653 mei_hbm_cl_disconnect_res(dev
, disconnect_res
);
654 dev_dbg(&dev
->pdev
->dev
, "client disconnect response message received.\n");
655 wake_up(&dev
->wait_recvd_msg
);
658 case MEI_FLOW_CONTROL_CMD
:
659 flow_control
= (struct hbm_flow_control
*) mei_msg
;
660 mei_hbm_cl_flow_control_res(dev
, flow_control
);
661 dev_dbg(&dev
->pdev
->dev
, "client flow control response message received.\n");
664 case HOST_CLIENT_PROPERTIES_RES_CMD
:
665 props_res
= (struct hbm_props_response
*)mei_msg
;
666 me_client
= &dev
->me_clients
[dev
->me_client_presentation_num
];
668 if (props_res
->status
|| !dev
->me_clients
) {
669 dev_err(&dev
->pdev
->dev
, "reset: properties response hbm wrong status.\n");
674 if (me_client
->client_id
!= props_res
->address
) {
675 dev_err(&dev
->pdev
->dev
, "reset: host properties response address mismatch\n");
680 if (dev
->dev_state
!= MEI_DEV_INIT_CLIENTS
||
681 dev
->hbm_state
!= MEI_HBM_CLIENT_PROPERTIES
) {
682 dev_err(&dev
->pdev
->dev
, "reset: unexpected properties response\n");
688 me_client
->props
= props_res
->client_properties
;
689 dev
->me_client_index
++;
690 dev
->me_client_presentation_num
++;
692 /* request property for the next client */
693 mei_hbm_prop_req(dev
);
697 case HOST_ENUM_RES_CMD
:
698 enum_res
= (struct hbm_host_enum_response
*) mei_msg
;
699 memcpy(dev
->me_clients_map
, enum_res
->valid_addresses
, 32);
700 if (dev
->dev_state
== MEI_DEV_INIT_CLIENTS
&&
701 dev
->hbm_state
== MEI_HBM_ENUM_CLIENTS
) {
702 dev
->init_clients_timer
= 0;
703 mei_hbm_me_cl_allocate(dev
);
704 dev
->hbm_state
= MEI_HBM_CLIENT_PROPERTIES
;
706 /* first property reqeust */
707 mei_hbm_prop_req(dev
);
709 dev_err(&dev
->pdev
->dev
, "reset: unexpected enumeration response hbm.\n");
715 case HOST_STOP_RES_CMD
:
717 if (dev
->hbm_state
!= MEI_HBM_STOP
)
718 dev_err(&dev
->pdev
->dev
, "unexpected stop response hbm.\n");
719 dev
->dev_state
= MEI_DEV_DISABLED
;
720 dev_info(&dev
->pdev
->dev
, "reset: FW stop response.\n");
724 case CLIENT_DISCONNECT_REQ_CMD
:
725 /* search for client */
726 disconnect_req
= (struct hbm_client_connect_request
*)mei_msg
;
727 mei_hbm_fw_disconnect_req(dev
, disconnect_req
);
730 case ME_STOP_REQ_CMD
:
732 dev
->hbm_state
= MEI_HBM_STOP
;
733 mei_hbm_stop_req_prepare(dev
, &dev
->wr_ext_msg
.hdr
,
734 dev
->wr_ext_msg
.data
);