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 /* count how many ME clients we have */
39 for_each_set_bit(b
, dev
->me_clients_map
, MEI_CLIENTS_MAX
)
40 dev
->me_clients_num
++;
42 if (dev
->me_clients_num
<= 0)
45 kfree(dev
->me_clients
);
46 dev
->me_clients
= NULL
;
48 dev_dbg(&dev
->pdev
->dev
, "memory allocation for ME clients size=%zd.\n",
49 dev
->me_clients_num
* sizeof(struct mei_me_client
));
50 /* allocate storage for ME clients representation */
51 clients
= kcalloc(dev
->me_clients_num
,
52 sizeof(struct mei_me_client
), GFP_KERNEL
);
54 dev_err(&dev
->pdev
->dev
, "memory allocation for ME clients failed.\n");
55 dev
->dev_state
= MEI_DEV_RESETTING
;
59 dev
->me_clients
= clients
;
64 * mei_hbm_cl_hdr - construct client hbm header
67 * @hbm_cmd: host bus message command
68 * @buf: buffer for cl header
72 void mei_hbm_cl_hdr(struct mei_cl
*cl
, u8 hbm_cmd
, void *buf
, size_t len
)
74 struct mei_hbm_cl_cmd
*cmd
= buf
;
78 cmd
->hbm_cmd
= hbm_cmd
;
79 cmd
->host_addr
= cl
->host_client_id
;
80 cmd
->me_addr
= cl
->me_client_id
;
84 * same_disconn_addr - tells if they have the same address
86 * @file: private data of the file object.
87 * @disconn: disconnection request.
89 * returns true if addres are same
92 bool mei_hbm_cl_addr_equal(struct mei_cl
*cl
, void *buf
)
94 struct mei_hbm_cl_cmd
*cmd
= buf
;
95 return cl
->host_client_id
== cmd
->host_addr
&&
96 cl
->me_client_id
== cmd
->me_addr
;
101 * is_treat_specially_client - checks if the message belongs
102 * to the file private data.
104 * @cl: private data of the file object
105 * @rs: connect response bus message
108 static bool is_treat_specially_client(struct mei_cl
*cl
,
109 struct hbm_client_connect_response
*rs
)
111 if (mei_hbm_cl_addr_equal(cl
, rs
)) {
113 cl
->state
= MEI_FILE_CONNECTED
;
117 cl
->state
= MEI_FILE_DISCONNECTED
;
118 cl
->status
= -ENODEV
;
127 int mei_hbm_start_wait(struct mei_device
*dev
)
130 if (dev
->hbm_state
> MEI_HBM_START
)
133 mutex_unlock(&dev
->device_lock
);
134 ret
= wait_event_interruptible_timeout(dev
->wait_recvd_msg
,
135 dev
->hbm_state
== MEI_HBM_IDLE
||
136 dev
->hbm_state
> MEI_HBM_START
,
137 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT
));
138 mutex_lock(&dev
->device_lock
);
140 if (ret
<= 0 && (dev
->hbm_state
<= MEI_HBM_START
)) {
141 dev
->hbm_state
= MEI_HBM_IDLE
;
142 dev_err(&dev
->pdev
->dev
, "waiting for mei start failed\n");
149 * mei_hbm_start_req - sends start request message.
151 * @dev: the device structure
153 int mei_hbm_start_req(struct mei_device
*dev
)
155 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
156 struct hbm_host_version_request
*start_req
;
157 const size_t len
= sizeof(struct hbm_host_version_request
);
159 mei_hbm_hdr(mei_hdr
, len
);
161 /* host start message */
162 start_req
= (struct hbm_host_version_request
*)dev
->wr_msg
.data
;
163 memset(start_req
, 0, len
);
164 start_req
->hbm_cmd
= HOST_START_REQ_CMD
;
165 start_req
->host_version
.major_version
= HBM_MAJOR_VERSION
;
166 start_req
->host_version
.minor_version
= HBM_MINOR_VERSION
;
168 dev
->hbm_state
= MEI_HBM_IDLE
;
169 if (mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
)) {
170 dev_err(&dev
->pdev
->dev
, "version message write failed\n");
171 dev
->dev_state
= MEI_DEV_RESETTING
;
175 dev
->hbm_state
= MEI_HBM_START
;
176 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
181 * mei_hbm_enum_clients_req - sends enumeration client request message.
183 * @dev: the device structure
187 static void mei_hbm_enum_clients_req(struct mei_device
*dev
)
189 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
190 struct hbm_host_enum_request
*enum_req
;
191 const size_t len
= sizeof(struct hbm_host_enum_request
);
192 /* enumerate clients */
193 mei_hbm_hdr(mei_hdr
, len
);
195 enum_req
= (struct hbm_host_enum_request
*)dev
->wr_msg
.data
;
196 memset(enum_req
, 0, len
);
197 enum_req
->hbm_cmd
= HOST_ENUM_REQ_CMD
;
199 if (mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
)) {
200 dev
->dev_state
= MEI_DEV_RESETTING
;
201 dev_err(&dev
->pdev
->dev
, "enumeration request write failed.\n");
204 dev
->hbm_state
= MEI_HBM_ENUM_CLIENTS
;
205 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
210 * mei_hbm_prop_req - request property for a single client
212 * @dev: the device structure
217 static int mei_hbm_prop_req(struct mei_device
*dev
)
220 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
221 struct hbm_props_request
*prop_req
;
222 const size_t len
= sizeof(struct hbm_props_request
);
223 unsigned long next_client_index
;
227 client_num
= dev
->me_client_presentation_num
;
229 next_client_index
= find_next_bit(dev
->me_clients_map
, MEI_CLIENTS_MAX
,
230 dev
->me_client_index
);
232 /* We got all client properties */
233 if (next_client_index
== MEI_CLIENTS_MAX
) {
234 dev
->hbm_state
= MEI_HBM_STARTED
;
235 schedule_work(&dev
->init_work
);
240 dev
->me_clients
[client_num
].client_id
= next_client_index
;
241 dev
->me_clients
[client_num
].mei_flow_ctrl_creds
= 0;
243 mei_hbm_hdr(mei_hdr
, len
);
244 prop_req
= (struct hbm_props_request
*)dev
->wr_msg
.data
;
246 memset(prop_req
, 0, sizeof(struct hbm_props_request
));
249 prop_req
->hbm_cmd
= HOST_CLIENT_PROPERTIES_REQ_CMD
;
250 prop_req
->address
= next_client_index
;
252 if (mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
)) {
253 dev
->dev_state
= MEI_DEV_RESETTING
;
254 dev_err(&dev
->pdev
->dev
, "properties request write failed\n");
260 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
261 dev
->me_client_index
= next_client_index
;
267 * mei_hbm_stop_req_prepare - perpare stop request message
270 * @mei_hdr - mei message header
271 * @data - hbm message body buffer
273 static void mei_hbm_stop_req_prepare(struct mei_device
*dev
,
274 struct mei_msg_hdr
*mei_hdr
, unsigned char *data
)
276 struct hbm_host_stop_request
*req
=
277 (struct hbm_host_stop_request
*)data
;
278 const size_t len
= sizeof(struct hbm_host_stop_request
);
280 mei_hbm_hdr(mei_hdr
, len
);
283 req
->hbm_cmd
= HOST_STOP_REQ_CMD
;
284 req
->reason
= DRIVER_STOP_REQUEST
;
288 * mei_hbm_cl_flow_control_req - sends flow control requst.
290 * @dev: the device structure
293 * This function returns -EIO on write failure
295 int mei_hbm_cl_flow_control_req(struct mei_device
*dev
, struct mei_cl
*cl
)
297 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
298 const size_t len
= sizeof(struct hbm_flow_control
);
300 mei_hbm_hdr(mei_hdr
, len
);
301 mei_hbm_cl_hdr(cl
, MEI_FLOW_CONTROL_CMD
, dev
->wr_msg
.data
, len
);
303 dev_dbg(&dev
->pdev
->dev
, "sending flow control host client = %d, ME client = %d\n",
304 cl
->host_client_id
, cl
->me_client_id
);
306 return mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
);
310 * mei_hbm_add_single_flow_creds - adds single buffer credentials.
312 * @dev: the device structure
313 * @flow: flow control.
315 static void mei_hbm_add_single_flow_creds(struct mei_device
*dev
,
316 struct hbm_flow_control
*flow
)
318 struct mei_me_client
*client
;
321 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
322 client
= &dev
->me_clients
[i
];
323 if (client
&& flow
->me_addr
== client
->client_id
) {
324 if (client
->props
.single_recv_buf
) {
325 client
->mei_flow_ctrl_creds
++;
326 dev_dbg(&dev
->pdev
->dev
, "recv flow ctrl msg ME %d (single).\n",
328 dev_dbg(&dev
->pdev
->dev
, "flow control credentials =%d.\n",
329 client
->mei_flow_ctrl_creds
);
331 BUG(); /* error in flow control */
338 * mei_hbm_cl_flow_control_res - flow control response from me
340 * @dev: the device structure
341 * @flow_control: flow control response bus message
343 static void mei_hbm_cl_flow_control_res(struct mei_device
*dev
,
344 struct hbm_flow_control
*flow_control
)
346 struct mei_cl
*cl
= NULL
;
347 struct mei_cl
*next
= NULL
;
349 if (!flow_control
->host_addr
) {
350 /* single receive buffer */
351 mei_hbm_add_single_flow_creds(dev
, flow_control
);
355 /* normal connection */
356 list_for_each_entry_safe(cl
, next
, &dev
->file_list
, link
) {
357 if (mei_hbm_cl_addr_equal(cl
, flow_control
)) {
358 cl
->mei_flow_ctrl_creds
++;
359 dev_dbg(&dev
->pdev
->dev
, "flow ctrl msg for host %d ME %d.\n",
360 flow_control
->host_addr
, flow_control
->me_addr
);
361 dev_dbg(&dev
->pdev
->dev
, "flow control credentials = %d.\n",
362 cl
->mei_flow_ctrl_creds
);
370 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
372 * @dev: the device structure
373 * @cl: a client to disconnect from
375 * This function returns -EIO on write failure
377 int mei_hbm_cl_disconnect_req(struct mei_device
*dev
, struct mei_cl
*cl
)
379 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
380 const size_t len
= sizeof(struct hbm_client_connect_request
);
382 mei_hbm_hdr(mei_hdr
, len
);
383 mei_hbm_cl_hdr(cl
, CLIENT_DISCONNECT_REQ_CMD
, dev
->wr_msg
.data
, len
);
385 return mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
);
389 * mei_hbm_cl_disconnect_res - disconnect response from ME
391 * @dev: the device structure
392 * @rs: disconnect response bus message
394 static void mei_hbm_cl_disconnect_res(struct mei_device
*dev
,
395 struct hbm_client_connect_response
*rs
)
398 struct mei_cl_cb
*pos
= NULL
, *next
= NULL
;
400 dev_dbg(&dev
->pdev
->dev
,
401 "disconnect_response:\n"
409 list_for_each_entry_safe(pos
, next
, &dev
->ctrl_rd_list
.list
, list
) {
413 list_del(&pos
->list
);
417 dev_dbg(&dev
->pdev
->dev
, "list_for_each_entry_safe in ctrl_rd_list.\n");
418 if (mei_hbm_cl_addr_equal(cl
, rs
)) {
419 list_del(&pos
->list
);
421 cl
->state
= MEI_FILE_DISCONNECTED
;
431 * mei_hbm_cl_connect_req - send connection request to specific me client
433 * @dev: the device structure
434 * @cl: a client to connect to
436 * returns -EIO on write failure
438 int mei_hbm_cl_connect_req(struct mei_device
*dev
, struct mei_cl
*cl
)
440 struct mei_msg_hdr
*mei_hdr
= &dev
->wr_msg
.hdr
;
441 const size_t len
= sizeof(struct hbm_client_connect_request
);
443 mei_hbm_hdr(mei_hdr
, len
);
444 mei_hbm_cl_hdr(cl
, CLIENT_CONNECT_REQ_CMD
, dev
->wr_msg
.data
, len
);
446 return mei_write_message(dev
, mei_hdr
, dev
->wr_msg
.data
);
450 * mei_hbm_cl_connect_res - connect resposne from the ME
452 * @dev: the device structure
453 * @rs: connect response bus message
455 static void mei_hbm_cl_connect_res(struct mei_device
*dev
,
456 struct hbm_client_connect_response
*rs
)
460 struct mei_cl_cb
*pos
= NULL
, *next
= NULL
;
462 dev_dbg(&dev
->pdev
->dev
,
463 "connect_response:\n"
471 /* if WD or iamthif client treat specially */
473 if (is_treat_specially_client(&dev
->wd_cl
, rs
)) {
474 dev_dbg(&dev
->pdev
->dev
, "successfully connected to WD client.\n");
475 mei_watchdog_register(dev
);
480 if (is_treat_specially_client(&dev
->iamthif_cl
, rs
)) {
481 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
484 list_for_each_entry_safe(pos
, next
, &dev
->ctrl_rd_list
.list
, list
) {
488 list_del(&pos
->list
);
491 if (pos
->fop_type
== MEI_FOP_IOCTL
) {
492 if (is_treat_specially_client(cl
, rs
)) {
493 list_del(&pos
->list
);
504 * mei_hbm_fw_disconnect_req - disconnect request initiated by me
505 * host sends disoconnect response
507 * @dev: the device structure.
508 * @disconnect_req: disconnect request bus message from the me
510 static void mei_hbm_fw_disconnect_req(struct mei_device
*dev
,
511 struct hbm_client_connect_request
*disconnect_req
)
513 struct mei_cl
*cl
, *next
;
514 const size_t len
= sizeof(struct hbm_client_connect_response
);
516 list_for_each_entry_safe(cl
, next
, &dev
->file_list
, link
) {
517 if (mei_hbm_cl_addr_equal(cl
, disconnect_req
)) {
518 dev_dbg(&dev
->pdev
->dev
, "disconnect request host client %d ME client %d.\n",
519 disconnect_req
->host_addr
,
520 disconnect_req
->me_addr
);
521 cl
->state
= MEI_FILE_DISCONNECTED
;
523 if (cl
== &dev
->wd_cl
)
524 dev
->wd_pending
= false;
525 else if (cl
== &dev
->iamthif_cl
)
526 dev
->iamthif_timer
= 0;
528 /* prepare disconnect response */
529 mei_hbm_hdr(&dev
->wr_ext_msg
.hdr
, len
);
530 mei_hbm_cl_hdr(cl
, CLIENT_DISCONNECT_RES_CMD
,
531 dev
->wr_ext_msg
.data
, len
);
539 * mei_hbm_version_is_supported - checks whether the driver can
540 * support the hbm version of the device
542 * @dev: the device structure
543 * returns true if driver can support hbm version of the device
545 bool mei_hbm_version_is_supported(struct mei_device
*dev
)
547 return (dev
->version
.major_version
< HBM_MAJOR_VERSION
) ||
548 (dev
->version
.major_version
== HBM_MAJOR_VERSION
&&
549 dev
->version
.minor_version
<= HBM_MINOR_VERSION
);
553 * mei_hbm_dispatch - bottom half read routine after ISR to
554 * handle the read bus message cmd processing.
556 * @dev: the device structure
557 * @mei_hdr: header of bus message
559 void mei_hbm_dispatch(struct mei_device
*dev
, struct mei_msg_hdr
*hdr
)
561 struct mei_bus_message
*mei_msg
;
562 struct mei_me_client
*me_client
;
563 struct hbm_host_version_response
*version_res
;
564 struct hbm_client_connect_response
*connect_res
;
565 struct hbm_client_connect_response
*disconnect_res
;
566 struct hbm_client_connect_request
*disconnect_req
;
567 struct hbm_flow_control
*flow_control
;
568 struct hbm_props_response
*props_res
;
569 struct hbm_host_enum_response
*enum_res
;
571 /* read the message to our buffer */
572 BUG_ON(hdr
->length
>= sizeof(dev
->rd_msg_buf
));
573 mei_read_slots(dev
, dev
->rd_msg_buf
, hdr
->length
);
574 mei_msg
= (struct mei_bus_message
*)dev
->rd_msg_buf
;
576 switch (mei_msg
->hbm_cmd
) {
577 case HOST_START_RES_CMD
:
578 version_res
= (struct hbm_host_version_response
*)mei_msg
;
580 dev_dbg(&dev
->pdev
->dev
, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
581 HBM_MAJOR_VERSION
, HBM_MINOR_VERSION
,
582 version_res
->me_max_version
.major_version
,
583 version_res
->me_max_version
.minor_version
);
585 if (version_res
->host_version_supported
) {
586 dev
->version
.major_version
= HBM_MAJOR_VERSION
;
587 dev
->version
.minor_version
= HBM_MINOR_VERSION
;
589 dev
->version
.major_version
=
590 version_res
->me_max_version
.major_version
;
591 dev
->version
.minor_version
=
592 version_res
->me_max_version
.minor_version
;
595 if (!mei_hbm_version_is_supported(dev
)) {
596 dev_warn(&dev
->pdev
->dev
, "hbm version mismatch: stopping the driver.\n");
598 dev
->hbm_state
= MEI_HBM_STOP
;
599 mei_hbm_stop_req_prepare(dev
, &dev
->wr_msg
.hdr
,
601 mei_write_message(dev
, &dev
->wr_msg
.hdr
,
607 if (dev
->dev_state
== MEI_DEV_INIT_CLIENTS
&&
608 dev
->hbm_state
== MEI_HBM_START
) {
609 dev
->init_clients_timer
= 0;
610 mei_hbm_enum_clients_req(dev
);
612 dev_err(&dev
->pdev
->dev
, "reset: wrong host start response\n");
617 wake_up_interruptible(&dev
->wait_recvd_msg
);
618 dev_dbg(&dev
->pdev
->dev
, "host start response message received.\n");
621 case CLIENT_CONNECT_RES_CMD
:
622 connect_res
= (struct hbm_client_connect_response
*) mei_msg
;
623 mei_hbm_cl_connect_res(dev
, connect_res
);
624 dev_dbg(&dev
->pdev
->dev
, "client connect response message received.\n");
625 wake_up(&dev
->wait_recvd_msg
);
628 case CLIENT_DISCONNECT_RES_CMD
:
629 disconnect_res
= (struct hbm_client_connect_response
*) mei_msg
;
630 mei_hbm_cl_disconnect_res(dev
, disconnect_res
);
631 dev_dbg(&dev
->pdev
->dev
, "client disconnect response message received.\n");
632 wake_up(&dev
->wait_recvd_msg
);
635 case MEI_FLOW_CONTROL_CMD
:
636 flow_control
= (struct hbm_flow_control
*) mei_msg
;
637 mei_hbm_cl_flow_control_res(dev
, flow_control
);
638 dev_dbg(&dev
->pdev
->dev
, "client flow control response message received.\n");
641 case HOST_CLIENT_PROPERTIES_RES_CMD
:
642 props_res
= (struct hbm_props_response
*)mei_msg
;
643 me_client
= &dev
->me_clients
[dev
->me_client_presentation_num
];
645 if (props_res
->status
|| !dev
->me_clients
) {
646 dev_err(&dev
->pdev
->dev
, "reset: properties response hbm wrong status.\n");
651 if (me_client
->client_id
!= props_res
->address
) {
652 dev_err(&dev
->pdev
->dev
, "reset: host properties response address mismatch\n");
657 if (dev
->dev_state
!= MEI_DEV_INIT_CLIENTS
||
658 dev
->hbm_state
!= MEI_HBM_CLIENT_PROPERTIES
) {
659 dev_err(&dev
->pdev
->dev
, "reset: unexpected properties response\n");
665 me_client
->props
= props_res
->client_properties
;
666 dev
->me_client_index
++;
667 dev
->me_client_presentation_num
++;
669 /* request property for the next client */
670 mei_hbm_prop_req(dev
);
674 case HOST_ENUM_RES_CMD
:
675 enum_res
= (struct hbm_host_enum_response
*) mei_msg
;
676 memcpy(dev
->me_clients_map
, enum_res
->valid_addresses
, 32);
677 if (dev
->dev_state
== MEI_DEV_INIT_CLIENTS
&&
678 dev
->hbm_state
== MEI_HBM_ENUM_CLIENTS
) {
679 dev
->init_clients_timer
= 0;
680 dev
->me_client_presentation_num
= 0;
681 dev
->me_client_index
= 0;
682 mei_hbm_me_cl_allocate(dev
);
683 dev
->hbm_state
= MEI_HBM_CLIENT_PROPERTIES
;
685 /* first property reqeust */
686 mei_hbm_prop_req(dev
);
688 dev_err(&dev
->pdev
->dev
, "reset: unexpected enumeration response hbm.\n");
694 case HOST_STOP_RES_CMD
:
696 if (dev
->hbm_state
!= MEI_HBM_STOP
)
697 dev_err(&dev
->pdev
->dev
, "unexpected stop response hbm.\n");
698 dev
->dev_state
= MEI_DEV_DISABLED
;
699 dev_info(&dev
->pdev
->dev
, "reset: FW stop response.\n");
703 case CLIENT_DISCONNECT_REQ_CMD
:
704 /* search for client */
705 disconnect_req
= (struct hbm_client_connect_request
*)mei_msg
;
706 mei_hbm_fw_disconnect_req(dev
, disconnect_req
);
709 case ME_STOP_REQ_CMD
:
711 dev
->hbm_state
= MEI_HBM_STOP
;
712 mei_hbm_stop_req_prepare(dev
, &dev
->wr_ext_msg
.hdr
,
713 dev
->wr_ext_msg
.data
);