1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
6 #include <linux/export.h>
7 #include <linux/sched.h>
8 #include <linux/wait.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/slab.h>
12 #include <linux/mei.h>
18 static const char *mei_hbm_status_str(enum mei_hbm_status status
)
20 #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
22 MEI_HBM_STATUS(SUCCESS
);
23 MEI_HBM_STATUS(CLIENT_NOT_FOUND
);
24 MEI_HBM_STATUS(ALREADY_EXISTS
);
25 MEI_HBM_STATUS(REJECTED
);
26 MEI_HBM_STATUS(INVALID_PARAMETER
);
27 MEI_HBM_STATUS(NOT_ALLOWED
);
28 MEI_HBM_STATUS(ALREADY_STARTED
);
29 MEI_HBM_STATUS(NOT_STARTED
);
30 default: return "unknown";
35 static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status
)
37 #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
41 MEI_CL_CS(ALREADY_STARTED
);
42 MEI_CL_CS(OUT_OF_RESOURCES
);
43 MEI_CL_CS(MESSAGE_SMALL
);
44 MEI_CL_CS(NOT_ALLOWED
);
45 default: return "unknown";
50 const char *mei_hbm_state_str(enum mei_hbm_state state
)
52 #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
55 MEI_HBM_STATE(STARTING
);
56 MEI_HBM_STATE(STARTED
);
57 MEI_HBM_STATE(DR_SETUP
);
58 MEI_HBM_STATE(ENUM_CLIENTS
);
59 MEI_HBM_STATE(CLIENT_PROPERTIES
);
60 MEI_HBM_STATE(STOPPED
);
68 * mei_cl_conn_status_to_errno - convert client connect response
69 * status to error code
71 * @status: client connect response status
73 * Return: corresponding error code
75 static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status
)
78 case MEI_CL_CONN_SUCCESS
: return 0;
79 case MEI_CL_CONN_NOT_FOUND
: return -ENOTTY
;
80 case MEI_CL_CONN_ALREADY_STARTED
: return -EBUSY
;
81 case MEI_CL_CONN_OUT_OF_RESOURCES
: return -EBUSY
;
82 case MEI_CL_CONN_MESSAGE_SMALL
: return -EINVAL
;
83 case MEI_CL_CONN_NOT_ALLOWED
: return -EBUSY
;
84 default: return -EINVAL
;
89 * mei_hbm_write_message - wrapper for sending hbm messages.
95 static inline int mei_hbm_write_message(struct mei_device
*dev
,
96 struct mei_msg_hdr
*hdr
,
99 return mei_write_message(dev
, hdr
, sizeof(*hdr
), data
, hdr
->length
);
103 * mei_hbm_idle - set hbm to idle state
105 * @dev: the device structure
107 void mei_hbm_idle(struct mei_device
*dev
)
109 dev
->init_clients_timer
= 0;
110 dev
->hbm_state
= MEI_HBM_IDLE
;
114 * mei_hbm_reset - reset hbm counters and book keeping data structurs
116 * @dev: the device structure
118 void mei_hbm_reset(struct mei_device
*dev
)
120 mei_me_cl_rm_all(dev
);
126 * mei_hbm_hdr - construct hbm header
128 * @mei_hdr: hbm header
129 * @length: payload length
132 static inline void mei_hbm_hdr(struct mei_msg_hdr
*mei_hdr
, size_t length
)
134 memset(mei_hdr
, 0, sizeof(*mei_hdr
));
135 mei_hdr
->length
= length
;
136 mei_hdr
->msg_complete
= 1;
140 * mei_hbm_cl_hdr - construct client hbm header
143 * @hbm_cmd: host bus message command
144 * @buf: buffer for cl header
145 * @len: buffer length
148 void mei_hbm_cl_hdr(struct mei_cl
*cl
, u8 hbm_cmd
, void *buf
, size_t len
)
150 struct mei_hbm_cl_cmd
*cmd
= buf
;
154 cmd
->hbm_cmd
= hbm_cmd
;
155 cmd
->host_addr
= mei_cl_host_addr(cl
);
156 cmd
->me_addr
= mei_cl_me_id(cl
);
160 * mei_hbm_cl_write - write simple hbm client message
162 * @dev: the device structure
164 * @hbm_cmd: host bus message command
165 * @buf: message buffer
166 * @len: buffer length
168 * Return: 0 on success, <0 on failure.
170 static inline int mei_hbm_cl_write(struct mei_device
*dev
, struct mei_cl
*cl
,
171 u8 hbm_cmd
, void *buf
, size_t len
)
173 struct mei_msg_hdr mei_hdr
;
175 mei_hbm_hdr(&mei_hdr
, len
);
176 mei_hbm_cl_hdr(cl
, hbm_cmd
, buf
, len
);
178 return mei_hbm_write_message(dev
, &mei_hdr
, buf
);
182 * mei_hbm_cl_addr_equal - check if the client's and
183 * the message address match
186 * @cmd: hbm client message
188 * Return: true if addresses are the same
191 bool mei_hbm_cl_addr_equal(struct mei_cl
*cl
, struct mei_hbm_cl_cmd
*cmd
)
193 return mei_cl_host_addr(cl
) == cmd
->host_addr
&&
194 mei_cl_me_id(cl
) == cmd
->me_addr
;
198 * mei_hbm_cl_find_by_cmd - find recipient client
200 * @dev: the device structure
201 * @buf: a buffer with hbm cl command
203 * Return: the recipient client or NULL if not found
206 struct mei_cl
*mei_hbm_cl_find_by_cmd(struct mei_device
*dev
, void *buf
)
208 struct mei_hbm_cl_cmd
*cmd
= (struct mei_hbm_cl_cmd
*)buf
;
211 list_for_each_entry(cl
, &dev
->file_list
, link
)
212 if (mei_hbm_cl_addr_equal(cl
, cmd
))
219 * mei_hbm_start_wait - wait for start response message.
221 * @dev: the device structure
223 * Return: 0 on success and < 0 on failure
225 int mei_hbm_start_wait(struct mei_device
*dev
)
229 if (dev
->hbm_state
> MEI_HBM_STARTING
)
232 mutex_unlock(&dev
->device_lock
);
233 ret
= wait_event_timeout(dev
->wait_hbm_start
,
234 dev
->hbm_state
!= MEI_HBM_STARTING
,
235 mei_secs_to_jiffies(MEI_HBM_TIMEOUT
));
236 mutex_lock(&dev
->device_lock
);
238 if (ret
== 0 && (dev
->hbm_state
<= MEI_HBM_STARTING
)) {
239 dev
->hbm_state
= MEI_HBM_IDLE
;
240 dev_err(dev
->dev
, "waiting for mei start failed\n");
247 * mei_hbm_start_req - sends start request message.
249 * @dev: the device structure
251 * Return: 0 on success and < 0 on failure
253 int mei_hbm_start_req(struct mei_device
*dev
)
255 struct mei_msg_hdr mei_hdr
;
256 struct hbm_host_version_request req
;
261 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
263 /* host start message */
264 memset(&req
, 0, sizeof(req
));
265 req
.hbm_cmd
= HOST_START_REQ_CMD
;
266 req
.host_version
.major_version
= HBM_MAJOR_VERSION
;
267 req
.host_version
.minor_version
= HBM_MINOR_VERSION
;
269 dev
->hbm_state
= MEI_HBM_IDLE
;
270 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
272 dev_err(dev
->dev
, "version message write failed: ret = %d\n",
277 dev
->hbm_state
= MEI_HBM_STARTING
;
278 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
279 mei_schedule_stall_timer(dev
);
284 * mei_hbm_dma_setup_req() - setup DMA request
285 * @dev: the device structure
287 * Return: 0 on success and < 0 on failure
289 static int mei_hbm_dma_setup_req(struct mei_device
*dev
)
291 struct mei_msg_hdr mei_hdr
;
292 struct hbm_dma_setup_request req
;
296 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
298 memset(&req
, 0, sizeof(req
));
299 req
.hbm_cmd
= MEI_HBM_DMA_SETUP_REQ_CMD
;
300 for (i
= 0; i
< DMA_DSCR_NUM
; i
++) {
303 paddr
= dev
->dr_dscr
[i
].daddr
;
304 req
.dma_dscr
[i
].addr_hi
= upper_32_bits(paddr
);
305 req
.dma_dscr
[i
].addr_lo
= lower_32_bits(paddr
);
306 req
.dma_dscr
[i
].size
= dev
->dr_dscr
[i
].size
;
309 mei_dma_ring_reset(dev
);
311 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
313 dev_err(dev
->dev
, "dma setup request write failed: ret = %d.\n",
318 dev
->hbm_state
= MEI_HBM_DR_SETUP
;
319 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
320 mei_schedule_stall_timer(dev
);
325 * mei_hbm_capabilities_req - request capabilities
327 * @dev: the device structure
329 * Return: 0 on success and < 0 on failure
331 static int mei_hbm_capabilities_req(struct mei_device
*dev
)
333 struct mei_msg_hdr mei_hdr
;
334 struct hbm_capability_request req
;
337 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
339 memset(&req
, 0, sizeof(req
));
340 req
.hbm_cmd
= MEI_HBM_CAPABILITIES_REQ_CMD
;
341 if (dev
->hbm_f_vt_supported
)
342 req
.capability_requested
[0] = HBM_CAP_VT
;
344 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
347 "capabilities request write failed: ret = %d.\n", ret
);
351 dev
->hbm_state
= MEI_HBM_CAP_SETUP
;
352 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
353 mei_schedule_stall_timer(dev
);
358 * mei_hbm_enum_clients_req - sends enumeration client request message.
360 * @dev: the device structure
362 * Return: 0 on success and < 0 on failure
364 static int mei_hbm_enum_clients_req(struct mei_device
*dev
)
366 struct mei_msg_hdr mei_hdr
;
367 struct hbm_host_enum_request req
;
370 /* enumerate clients */
371 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
373 memset(&req
, 0, sizeof(req
));
374 req
.hbm_cmd
= HOST_ENUM_REQ_CMD
;
375 req
.flags
|= dev
->hbm_f_dc_supported
? MEI_HBM_ENUM_F_ALLOW_ADD
: 0;
376 req
.flags
|= dev
->hbm_f_ie_supported
?
377 MEI_HBM_ENUM_F_IMMEDIATE_ENUM
: 0;
379 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
381 dev_err(dev
->dev
, "enumeration request write failed: ret = %d.\n",
385 dev
->hbm_state
= MEI_HBM_ENUM_CLIENTS
;
386 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
387 mei_schedule_stall_timer(dev
);
392 * mei_hbm_me_cl_add - add new me client to the list
394 * @dev: the device structure
395 * @res: hbm property response
397 * Return: 0 on success and -ENOMEM on allocation failure
400 static int mei_hbm_me_cl_add(struct mei_device
*dev
,
401 struct hbm_props_response
*res
)
403 struct mei_me_client
*me_cl
;
404 const uuid_le
*uuid
= &res
->client_properties
.protocol_name
;
406 mei_me_cl_rm_by_uuid(dev
, uuid
);
408 me_cl
= kzalloc(sizeof(*me_cl
), GFP_KERNEL
);
412 mei_me_cl_init(me_cl
);
414 me_cl
->props
= res
->client_properties
;
415 me_cl
->client_id
= res
->me_addr
;
416 me_cl
->tx_flow_ctrl_creds
= 0;
418 mei_me_cl_add(dev
, me_cl
);
424 * mei_hbm_add_cl_resp - send response to fw on client add request
426 * @dev: the device structure
428 * @status: response status
430 * Return: 0 on success and < 0 on failure
432 static int mei_hbm_add_cl_resp(struct mei_device
*dev
, u8 addr
, u8 status
)
434 struct mei_msg_hdr mei_hdr
;
435 struct hbm_add_client_response resp
;
438 dev_dbg(dev
->dev
, "adding client response\n");
440 mei_hbm_hdr(&mei_hdr
, sizeof(resp
));
442 memset(&resp
, 0, sizeof(resp
));
443 resp
.hbm_cmd
= MEI_HBM_ADD_CLIENT_RES_CMD
;
445 resp
.status
= status
;
447 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &resp
);
449 dev_err(dev
->dev
, "add client response write failed: ret = %d\n",
455 * mei_hbm_fw_add_cl_req - request from the fw to add a client
457 * @dev: the device structure
458 * @req: add client request
460 * Return: 0 on success and < 0 on failure
462 static int mei_hbm_fw_add_cl_req(struct mei_device
*dev
,
463 struct hbm_add_client_request
*req
)
466 u8 status
= MEI_HBMS_SUCCESS
;
468 BUILD_BUG_ON(sizeof(struct hbm_add_client_request
) !=
469 sizeof(struct hbm_props_response
));
471 ret
= mei_hbm_me_cl_add(dev
, (struct hbm_props_response
*)req
);
473 status
= !MEI_HBMS_SUCCESS
;
475 if (dev
->dev_state
== MEI_DEV_ENABLED
)
476 schedule_work(&dev
->bus_rescan_work
);
478 return mei_hbm_add_cl_resp(dev
, req
->me_addr
, status
);
482 * mei_hbm_cl_notify_req - send notification request
484 * @dev: the device structure
485 * @cl: a client to disconnect from
486 * @start: true for start false for stop
488 * Return: 0 on success and -EIO on write failure
490 int mei_hbm_cl_notify_req(struct mei_device
*dev
,
491 struct mei_cl
*cl
, u8 start
)
494 struct mei_msg_hdr mei_hdr
;
495 struct hbm_notification_request req
;
498 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
499 mei_hbm_cl_hdr(cl
, MEI_HBM_NOTIFY_REQ_CMD
, &req
, sizeof(req
));
503 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
505 dev_err(dev
->dev
, "notify request failed: ret = %d\n", ret
);
511 * notify_res_to_fop - convert notification response to the proper
514 * @cmd: client notification start response command
516 * Return: MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP;
518 static inline enum mei_cb_file_ops
notify_res_to_fop(struct mei_hbm_cl_cmd
*cmd
)
520 struct hbm_notification_response
*rs
=
521 (struct hbm_notification_response
*)cmd
;
523 return mei_cl_notify_req2fop(rs
->start
);
527 * mei_hbm_cl_notify_start_res - update the client state according
528 * notify start response
530 * @dev: the device structure
531 * @cl: mei host client
532 * @cmd: client notification start response command
534 static void mei_hbm_cl_notify_start_res(struct mei_device
*dev
,
536 struct mei_hbm_cl_cmd
*cmd
)
538 struct hbm_notification_response
*rs
=
539 (struct hbm_notification_response
*)cmd
;
541 cl_dbg(dev
, cl
, "hbm: notify start response status=%d\n", rs
->status
);
543 if (rs
->status
== MEI_HBMS_SUCCESS
||
544 rs
->status
== MEI_HBMS_ALREADY_STARTED
) {
545 cl
->notify_en
= true;
548 cl
->status
= -EINVAL
;
553 * mei_hbm_cl_notify_stop_res - update the client state according
554 * notify stop response
556 * @dev: the device structure
557 * @cl: mei host client
558 * @cmd: client notification stop response command
560 static void mei_hbm_cl_notify_stop_res(struct mei_device
*dev
,
562 struct mei_hbm_cl_cmd
*cmd
)
564 struct hbm_notification_response
*rs
=
565 (struct hbm_notification_response
*)cmd
;
567 cl_dbg(dev
, cl
, "hbm: notify stop response status=%d\n", rs
->status
);
569 if (rs
->status
== MEI_HBMS_SUCCESS
||
570 rs
->status
== MEI_HBMS_NOT_STARTED
) {
571 cl
->notify_en
= false;
574 /* TODO: spec is not clear yet about other possible issues */
575 cl
->status
= -EINVAL
;
580 * mei_hbm_cl_notify - signal notification event
582 * @dev: the device structure
583 * @cmd: notification client message
585 static void mei_hbm_cl_notify(struct mei_device
*dev
,
586 struct mei_hbm_cl_cmd
*cmd
)
590 cl
= mei_hbm_cl_find_by_cmd(dev
, cmd
);
596 * mei_hbm_prop_req - request property for a single client
598 * @dev: the device structure
599 * @start_idx: client index to start search
601 * Return: 0 on success and < 0 on failure
603 static int mei_hbm_prop_req(struct mei_device
*dev
, unsigned long start_idx
)
605 struct mei_msg_hdr mei_hdr
;
606 struct hbm_props_request req
;
610 addr
= find_next_bit(dev
->me_clients_map
, MEI_CLIENTS_MAX
, start_idx
);
612 /* We got all client properties */
613 if (addr
== MEI_CLIENTS_MAX
) {
614 dev
->hbm_state
= MEI_HBM_STARTED
;
615 mei_host_client_init(dev
);
619 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
621 memset(&req
, 0, sizeof(req
));
623 req
.hbm_cmd
= HOST_CLIENT_PROPERTIES_REQ_CMD
;
626 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
628 dev_err(dev
->dev
, "properties request write failed: ret = %d\n",
633 dev
->init_clients_timer
= MEI_CLIENTS_INIT_TIMEOUT
;
634 mei_schedule_stall_timer(dev
);
640 * mei_hbm_pg - sends pg command
642 * @dev: the device structure
643 * @pg_cmd: the pg command code
645 * Return: -EIO on write failure
646 * -EOPNOTSUPP if the operation is not supported by the protocol
648 int mei_hbm_pg(struct mei_device
*dev
, u8 pg_cmd
)
650 struct mei_msg_hdr mei_hdr
;
651 struct hbm_power_gate req
;
654 if (!dev
->hbm_f_pg_supported
)
657 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
659 memset(&req
, 0, sizeof(req
));
660 req
.hbm_cmd
= pg_cmd
;
662 ret
= mei_hbm_write_message(dev
, &mei_hdr
, &req
);
664 dev_err(dev
->dev
, "power gate command write failed.\n");
667 EXPORT_SYMBOL_GPL(mei_hbm_pg
);
670 * mei_hbm_stop_req - send stop request message
674 * Return: -EIO on write failure
676 static int mei_hbm_stop_req(struct mei_device
*dev
)
678 struct mei_msg_hdr mei_hdr
;
679 struct hbm_host_stop_request req
;
681 mei_hbm_hdr(&mei_hdr
, sizeof(req
));
683 memset(&req
, 0, sizeof(req
));
684 req
.hbm_cmd
= HOST_STOP_REQ_CMD
;
685 req
.reason
= DRIVER_STOP_REQUEST
;
687 return mei_hbm_write_message(dev
, &mei_hdr
, &req
);
691 * mei_hbm_cl_flow_control_req - sends flow control request.
693 * @dev: the device structure
696 * Return: -EIO on write failure
698 int mei_hbm_cl_flow_control_req(struct mei_device
*dev
, struct mei_cl
*cl
)
700 struct hbm_flow_control req
;
702 cl_dbg(dev
, cl
, "sending flow control\n");
703 return mei_hbm_cl_write(dev
, cl
, MEI_FLOW_CONTROL_CMD
,
708 * mei_hbm_add_single_tx_flow_ctrl_creds - adds single buffer credentials.
710 * @dev: the device structure
711 * @fctrl: flow control response bus message
713 * Return: 0 on success, < 0 otherwise
715 static int mei_hbm_add_single_tx_flow_ctrl_creds(struct mei_device
*dev
,
716 struct hbm_flow_control
*fctrl
)
718 struct mei_me_client
*me_cl
;
721 me_cl
= mei_me_cl_by_id(dev
, fctrl
->me_addr
);
723 dev_err(dev
->dev
, "no such me client %d\n", fctrl
->me_addr
);
727 if (WARN_ON(me_cl
->props
.single_recv_buf
== 0)) {
732 me_cl
->tx_flow_ctrl_creds
++;
733 dev_dbg(dev
->dev
, "recv flow ctrl msg ME %d (single) creds = %d.\n",
734 fctrl
->me_addr
, me_cl
->tx_flow_ctrl_creds
);
738 mei_me_cl_put(me_cl
);
743 * mei_hbm_cl_flow_control_res - flow control response from me
745 * @dev: the device structure
746 * @fctrl: flow control response bus message
748 static void mei_hbm_cl_tx_flow_ctrl_creds_res(struct mei_device
*dev
,
749 struct hbm_flow_control
*fctrl
)
753 if (!fctrl
->host_addr
) {
754 /* single receive buffer */
755 mei_hbm_add_single_tx_flow_ctrl_creds(dev
, fctrl
);
759 cl
= mei_hbm_cl_find_by_cmd(dev
, fctrl
);
761 cl
->tx_flow_ctrl_creds
++;
762 cl_dbg(dev
, cl
, "flow control creds = %d.\n",
763 cl
->tx_flow_ctrl_creds
);
769 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
771 * @dev: the device structure
772 * @cl: a client to disconnect from
774 * Return: -EIO on write failure
776 int mei_hbm_cl_disconnect_req(struct mei_device
*dev
, struct mei_cl
*cl
)
778 struct hbm_client_connect_request req
;
780 return mei_hbm_cl_write(dev
, cl
, CLIENT_DISCONNECT_REQ_CMD
,
785 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
787 * @dev: the device structure
788 * @cl: a client to disconnect from
790 * Return: -EIO on write failure
792 int mei_hbm_cl_disconnect_rsp(struct mei_device
*dev
, struct mei_cl
*cl
)
794 struct hbm_client_connect_response resp
;
796 return mei_hbm_cl_write(dev
, cl
, CLIENT_DISCONNECT_RES_CMD
,
797 &resp
, sizeof(resp
));
801 * mei_hbm_cl_disconnect_res - update the client state according
802 * disconnect response
804 * @dev: the device structure
805 * @cl: mei host client
806 * @cmd: disconnect client response host bus message
808 static void mei_hbm_cl_disconnect_res(struct mei_device
*dev
, struct mei_cl
*cl
,
809 struct mei_hbm_cl_cmd
*cmd
)
811 struct hbm_client_connect_response
*rs
=
812 (struct hbm_client_connect_response
*)cmd
;
814 cl_dbg(dev
, cl
, "hbm: disconnect response status=%d\n", rs
->status
);
816 if (rs
->status
== MEI_CL_DISCONN_SUCCESS
)
817 cl
->state
= MEI_FILE_DISCONNECT_REPLY
;
822 * mei_hbm_cl_connect_req - send connection request to specific me client
824 * @dev: the device structure
825 * @cl: a client to connect to
827 * Return: -EIO on write failure
829 int mei_hbm_cl_connect_req(struct mei_device
*dev
, struct mei_cl
*cl
)
831 struct hbm_client_connect_request req
;
833 return mei_hbm_cl_write(dev
, cl
, CLIENT_CONNECT_REQ_CMD
,
838 * mei_hbm_cl_connect_res - update the client state according
839 * connection response
841 * @dev: the device structure
842 * @cl: mei host client
843 * @cmd: connect client response host bus message
845 static void mei_hbm_cl_connect_res(struct mei_device
*dev
, struct mei_cl
*cl
,
846 struct mei_hbm_cl_cmd
*cmd
)
848 struct hbm_client_connect_response
*rs
=
849 (struct hbm_client_connect_response
*)cmd
;
851 cl_dbg(dev
, cl
, "hbm: connect response status=%s\n",
852 mei_cl_conn_status_str(rs
->status
));
854 if (rs
->status
== MEI_CL_CONN_SUCCESS
)
855 cl
->state
= MEI_FILE_CONNECTED
;
857 cl
->state
= MEI_FILE_DISCONNECT_REPLY
;
858 if (rs
->status
== MEI_CL_CONN_NOT_FOUND
) {
859 mei_me_cl_del(dev
, cl
->me_cl
);
860 if (dev
->dev_state
== MEI_DEV_ENABLED
)
861 schedule_work(&dev
->bus_rescan_work
);
864 cl
->status
= mei_cl_conn_status_to_errno(rs
->status
);
868 * mei_hbm_cl_res - process hbm response received on behalf
871 * @dev: the device structure
872 * @rs: hbm client message
873 * @fop_type: file operation type
875 static void mei_hbm_cl_res(struct mei_device
*dev
,
876 struct mei_hbm_cl_cmd
*rs
,
877 enum mei_cb_file_ops fop_type
)
880 struct mei_cl_cb
*cb
, *next
;
883 list_for_each_entry_safe(cb
, next
, &dev
->ctrl_rd_list
, list
) {
887 if (cb
->fop_type
!= fop_type
)
890 if (mei_hbm_cl_addr_equal(cl
, rs
)) {
891 list_del_init(&cb
->list
);
900 case MEI_FOP_CONNECT
:
901 mei_hbm_cl_connect_res(dev
, cl
, rs
);
903 case MEI_FOP_DISCONNECT
:
904 mei_hbm_cl_disconnect_res(dev
, cl
, rs
);
906 case MEI_FOP_NOTIFY_START
:
907 mei_hbm_cl_notify_start_res(dev
, cl
, rs
);
909 case MEI_FOP_NOTIFY_STOP
:
910 mei_hbm_cl_notify_stop_res(dev
, cl
, rs
);
922 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
923 * host sends disconnect response
925 * @dev: the device structure.
926 * @disconnect_req: disconnect request bus message from the me
928 * Return: -ENOMEM on allocation failure
930 static int mei_hbm_fw_disconnect_req(struct mei_device
*dev
,
931 struct hbm_client_connect_request
*disconnect_req
)
934 struct mei_cl_cb
*cb
;
936 cl
= mei_hbm_cl_find_by_cmd(dev
, disconnect_req
);
938 cl_warn(dev
, cl
, "fw disconnect request received\n");
939 cl
->state
= MEI_FILE_DISCONNECTING
;
942 cb
= mei_cl_enqueue_ctrl_wr_cb(cl
, 0, MEI_FOP_DISCONNECT_RSP
,
951 * mei_hbm_pg_enter_res - PG enter response received
953 * @dev: the device structure.
955 * Return: 0 on success, -EPROTO on state mismatch
957 static int mei_hbm_pg_enter_res(struct mei_device
*dev
)
959 if (mei_pg_state(dev
) != MEI_PG_OFF
||
960 dev
->pg_event
!= MEI_PG_EVENT_WAIT
) {
961 dev_err(dev
->dev
, "hbm: pg entry response: state mismatch [%s, %d]\n",
962 mei_pg_state_str(mei_pg_state(dev
)), dev
->pg_event
);
966 dev
->pg_event
= MEI_PG_EVENT_RECEIVED
;
967 wake_up(&dev
->wait_pg
);
973 * mei_hbm_pg_resume - process with PG resume
975 * @dev: the device structure.
977 void mei_hbm_pg_resume(struct mei_device
*dev
)
979 pm_request_resume(dev
->dev
);
981 EXPORT_SYMBOL_GPL(mei_hbm_pg_resume
);
984 * mei_hbm_pg_exit_res - PG exit response received
986 * @dev: the device structure.
988 * Return: 0 on success, -EPROTO on state mismatch
990 static int mei_hbm_pg_exit_res(struct mei_device
*dev
)
992 if (mei_pg_state(dev
) != MEI_PG_ON
||
993 (dev
->pg_event
!= MEI_PG_EVENT_WAIT
&&
994 dev
->pg_event
!= MEI_PG_EVENT_IDLE
)) {
995 dev_err(dev
->dev
, "hbm: pg exit response: state mismatch [%s, %d]\n",
996 mei_pg_state_str(mei_pg_state(dev
)), dev
->pg_event
);
1000 switch (dev
->pg_event
) {
1001 case MEI_PG_EVENT_WAIT
:
1002 dev
->pg_event
= MEI_PG_EVENT_RECEIVED
;
1003 wake_up(&dev
->wait_pg
);
1005 case MEI_PG_EVENT_IDLE
:
1007 * If the driver is not waiting on this then
1008 * this is HW initiated exit from PG.
1009 * Start runtime pm resume sequence to exit from PG.
1011 dev
->pg_event
= MEI_PG_EVENT_RECEIVED
;
1012 mei_hbm_pg_resume(dev
);
1015 WARN(1, "hbm: pg exit response: unexpected pg event = %d\n",
1024 * mei_hbm_config_features - check what hbm features and commands
1025 * are supported by the fw
1027 * @dev: the device structure
1029 static void mei_hbm_config_features(struct mei_device
*dev
)
1031 /* Power Gating Isolation Support */
1032 dev
->hbm_f_pg_supported
= 0;
1033 if (dev
->version
.major_version
> HBM_MAJOR_VERSION_PGI
)
1034 dev
->hbm_f_pg_supported
= 1;
1036 if (dev
->version
.major_version
== HBM_MAJOR_VERSION_PGI
&&
1037 dev
->version
.minor_version
>= HBM_MINOR_VERSION_PGI
)
1038 dev
->hbm_f_pg_supported
= 1;
1040 dev
->hbm_f_dc_supported
= 0;
1041 if (dev
->version
.major_version
>= HBM_MAJOR_VERSION_DC
)
1042 dev
->hbm_f_dc_supported
= 1;
1044 dev
->hbm_f_ie_supported
= 0;
1045 if (dev
->version
.major_version
>= HBM_MAJOR_VERSION_IE
)
1046 dev
->hbm_f_ie_supported
= 1;
1048 /* disconnect on connect timeout instead of link reset */
1049 dev
->hbm_f_dot_supported
= 0;
1050 if (dev
->version
.major_version
>= HBM_MAJOR_VERSION_DOT
)
1051 dev
->hbm_f_dot_supported
= 1;
1053 /* Notification Event Support */
1054 dev
->hbm_f_ev_supported
= 0;
1055 if (dev
->version
.major_version
>= HBM_MAJOR_VERSION_EV
)
1056 dev
->hbm_f_ev_supported
= 1;
1058 /* Fixed Address Client Support */
1059 dev
->hbm_f_fa_supported
= 0;
1060 if (dev
->version
.major_version
>= HBM_MAJOR_VERSION_FA
)
1061 dev
->hbm_f_fa_supported
= 1;
1063 /* OS ver message Support */
1064 dev
->hbm_f_os_supported
= 0;
1065 if (dev
->version
.major_version
>= HBM_MAJOR_VERSION_OS
)
1066 dev
->hbm_f_os_supported
= 1;
1068 /* DMA Ring Support */
1069 dev
->hbm_f_dr_supported
= 0;
1070 if (dev
->version
.major_version
> HBM_MAJOR_VERSION_DR
||
1071 (dev
->version
.major_version
== HBM_MAJOR_VERSION_DR
&&
1072 dev
->version
.minor_version
>= HBM_MINOR_VERSION_DR
))
1073 dev
->hbm_f_dr_supported
= 1;
1076 dev
->hbm_f_vt_supported
= 0;
1077 if (dev
->version
.major_version
> HBM_MAJOR_VERSION_VT
||
1078 (dev
->version
.major_version
== HBM_MAJOR_VERSION_VT
&&
1079 dev
->version
.minor_version
>= HBM_MINOR_VERSION_VT
))
1080 dev
->hbm_f_vt_supported
= 1;
1082 /* Capability message Support */
1083 dev
->hbm_f_cap_supported
= 0;
1084 if (dev
->version
.major_version
> HBM_MAJOR_VERSION_CAP
||
1085 (dev
->version
.major_version
== HBM_MAJOR_VERSION_CAP
&&
1086 dev
->version
.minor_version
>= HBM_MINOR_VERSION_CAP
))
1087 dev
->hbm_f_cap_supported
= 1;
1091 * mei_hbm_version_is_supported - checks whether the driver can
1092 * support the hbm version of the device
1094 * @dev: the device structure
1095 * Return: true if driver can support hbm version of the device
1097 bool mei_hbm_version_is_supported(struct mei_device
*dev
)
1099 return (dev
->version
.major_version
< HBM_MAJOR_VERSION
) ||
1100 (dev
->version
.major_version
== HBM_MAJOR_VERSION
&&
1101 dev
->version
.minor_version
<= HBM_MINOR_VERSION
);
1105 * mei_hbm_dispatch - bottom half read routine after ISR to
1106 * handle the read bus message cmd processing.
1108 * @dev: the device structure
1109 * @hdr: header of bus message
1111 * Return: 0 on success and < 0 on failure
1113 int mei_hbm_dispatch(struct mei_device
*dev
, struct mei_msg_hdr
*hdr
)
1115 struct mei_bus_message
*mei_msg
;
1116 struct hbm_host_version_response
*version_res
;
1117 struct hbm_props_response
*props_res
;
1118 struct hbm_host_enum_response
*enum_res
;
1119 struct hbm_dma_setup_response
*dma_setup_res
;
1120 struct hbm_add_client_request
*add_cl_req
;
1121 struct hbm_capability_response
*capability_res
;
1124 struct mei_hbm_cl_cmd
*cl_cmd
;
1125 struct hbm_client_connect_request
*disconnect_req
;
1126 struct hbm_flow_control
*fctrl
;
1128 /* read the message to our buffer */
1129 BUG_ON(hdr
->length
>= sizeof(dev
->rd_msg_buf
));
1130 mei_read_slots(dev
, dev
->rd_msg_buf
, hdr
->length
);
1131 mei_msg
= (struct mei_bus_message
*)dev
->rd_msg_buf
;
1132 cl_cmd
= (struct mei_hbm_cl_cmd
*)mei_msg
;
1134 /* ignore spurious message and prevent reset nesting
1135 * hbm is put to idle during system reset
1137 if (dev
->hbm_state
== MEI_HBM_IDLE
) {
1138 dev_dbg(dev
->dev
, "hbm: state is idle ignore spurious messages\n");
1142 switch (mei_msg
->hbm_cmd
) {
1143 case HOST_START_RES_CMD
:
1144 dev_dbg(dev
->dev
, "hbm: start: response message received.\n");
1146 dev
->init_clients_timer
= 0;
1148 version_res
= (struct hbm_host_version_response
*)mei_msg
;
1150 dev_dbg(dev
->dev
, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
1151 HBM_MAJOR_VERSION
, HBM_MINOR_VERSION
,
1152 version_res
->me_max_version
.major_version
,
1153 version_res
->me_max_version
.minor_version
);
1155 if (version_res
->host_version_supported
) {
1156 dev
->version
.major_version
= HBM_MAJOR_VERSION
;
1157 dev
->version
.minor_version
= HBM_MINOR_VERSION
;
1159 dev
->version
.major_version
=
1160 version_res
->me_max_version
.major_version
;
1161 dev
->version
.minor_version
=
1162 version_res
->me_max_version
.minor_version
;
1165 if (!mei_hbm_version_is_supported(dev
)) {
1166 dev_warn(dev
->dev
, "hbm: start: version mismatch - stopping the driver.\n");
1168 dev
->hbm_state
= MEI_HBM_STOPPED
;
1169 if (mei_hbm_stop_req(dev
)) {
1170 dev_err(dev
->dev
, "hbm: start: failed to send stop request\n");
1176 mei_hbm_config_features(dev
);
1178 if (dev
->dev_state
!= MEI_DEV_INIT_CLIENTS
||
1179 dev
->hbm_state
!= MEI_HBM_STARTING
) {
1180 dev_err(dev
->dev
, "hbm: start: state mismatch, [%d, %d]\n",
1181 dev
->dev_state
, dev
->hbm_state
);
1185 if (dev
->hbm_f_cap_supported
) {
1186 if (mei_hbm_capabilities_req(dev
))
1188 wake_up(&dev
->wait_hbm_start
);
1192 if (dev
->hbm_f_dr_supported
) {
1193 if (mei_dmam_ring_alloc(dev
))
1194 dev_info(dev
->dev
, "running w/o dma ring\n");
1195 if (mei_dma_ring_is_allocated(dev
)) {
1196 if (mei_hbm_dma_setup_req(dev
))
1199 wake_up(&dev
->wait_hbm_start
);
1204 dev
->hbm_f_dr_supported
= 0;
1205 mei_dmam_ring_free(dev
);
1207 if (mei_hbm_enum_clients_req(dev
))
1210 wake_up(&dev
->wait_hbm_start
);
1213 case MEI_HBM_CAPABILITIES_RES_CMD
:
1214 dev_dbg(dev
->dev
, "hbm: capabilities response: message received.\n");
1216 dev
->init_clients_timer
= 0;
1218 if (dev
->hbm_state
!= MEI_HBM_CAP_SETUP
) {
1219 dev_err(dev
->dev
, "hbm: capabilities response: state mismatch, [%d, %d]\n",
1220 dev
->dev_state
, dev
->hbm_state
);
1224 capability_res
= (struct hbm_capability_response
*)mei_msg
;
1225 if (!(capability_res
->capability_granted
[0] & HBM_CAP_VT
))
1226 dev
->hbm_f_vt_supported
= 0;
1228 if (dev
->hbm_f_dr_supported
) {
1229 if (mei_dmam_ring_alloc(dev
))
1230 dev_info(dev
->dev
, "running w/o dma ring\n");
1231 if (mei_dma_ring_is_allocated(dev
)) {
1232 if (mei_hbm_dma_setup_req(dev
))
1238 dev
->hbm_f_dr_supported
= 0;
1239 mei_dmam_ring_free(dev
);
1241 if (mei_hbm_enum_clients_req(dev
))
1245 case MEI_HBM_DMA_SETUP_RES_CMD
:
1246 dev_dbg(dev
->dev
, "hbm: dma setup response: message received.\n");
1248 dev
->init_clients_timer
= 0;
1250 if (dev
->hbm_state
!= MEI_HBM_DR_SETUP
) {
1251 dev_err(dev
->dev
, "hbm: dma setup response: state mismatch, [%d, %d]\n",
1252 dev
->dev_state
, dev
->hbm_state
);
1256 dma_setup_res
= (struct hbm_dma_setup_response
*)mei_msg
;
1258 if (dma_setup_res
->status
) {
1259 u8 status
= dma_setup_res
->status
;
1261 if (status
== MEI_HBMS_NOT_ALLOWED
) {
1262 dev_dbg(dev
->dev
, "hbm: dma setup not allowed\n");
1264 dev_info(dev
->dev
, "hbm: dma setup response: failure = %d %s\n",
1266 mei_hbm_status_str(status
));
1268 dev
->hbm_f_dr_supported
= 0;
1269 mei_dmam_ring_free(dev
);
1272 if (mei_hbm_enum_clients_req(dev
))
1276 case CLIENT_CONNECT_RES_CMD
:
1277 dev_dbg(dev
->dev
, "hbm: client connect response: message received.\n");
1278 mei_hbm_cl_res(dev
, cl_cmd
, MEI_FOP_CONNECT
);
1281 case CLIENT_DISCONNECT_RES_CMD
:
1282 dev_dbg(dev
->dev
, "hbm: client disconnect response: message received.\n");
1283 mei_hbm_cl_res(dev
, cl_cmd
, MEI_FOP_DISCONNECT
);
1286 case MEI_FLOW_CONTROL_CMD
:
1287 dev_dbg(dev
->dev
, "hbm: client flow control response: message received.\n");
1289 fctrl
= (struct hbm_flow_control
*)mei_msg
;
1290 mei_hbm_cl_tx_flow_ctrl_creds_res(dev
, fctrl
);
1293 case MEI_PG_ISOLATION_ENTRY_RES_CMD
:
1294 dev_dbg(dev
->dev
, "hbm: power gate isolation entry response received\n");
1295 ret
= mei_hbm_pg_enter_res(dev
);
1300 case MEI_PG_ISOLATION_EXIT_REQ_CMD
:
1301 dev_dbg(dev
->dev
, "hbm: power gate isolation exit request received\n");
1302 ret
= mei_hbm_pg_exit_res(dev
);
1307 case HOST_CLIENT_PROPERTIES_RES_CMD
:
1308 dev_dbg(dev
->dev
, "hbm: properties response: message received.\n");
1310 dev
->init_clients_timer
= 0;
1312 if (dev
->dev_state
!= MEI_DEV_INIT_CLIENTS
||
1313 dev
->hbm_state
!= MEI_HBM_CLIENT_PROPERTIES
) {
1314 dev_err(dev
->dev
, "hbm: properties response: state mismatch, [%d, %d]\n",
1315 dev
->dev_state
, dev
->hbm_state
);
1319 props_res
= (struct hbm_props_response
*)mei_msg
;
1321 if (props_res
->status
== MEI_HBMS_CLIENT_NOT_FOUND
) {
1322 dev_dbg(dev
->dev
, "hbm: properties response: %d CLIENT_NOT_FOUND\n",
1323 props_res
->me_addr
);
1324 } else if (props_res
->status
) {
1325 dev_err(dev
->dev
, "hbm: properties response: wrong status = %d %s\n",
1327 mei_hbm_status_str(props_res
->status
));
1330 mei_hbm_me_cl_add(dev
, props_res
);
1333 /* request property for the next client */
1334 if (mei_hbm_prop_req(dev
, props_res
->me_addr
+ 1))
1339 case HOST_ENUM_RES_CMD
:
1340 dev_dbg(dev
->dev
, "hbm: enumeration response: message received\n");
1342 dev
->init_clients_timer
= 0;
1344 enum_res
= (struct hbm_host_enum_response
*) mei_msg
;
1345 BUILD_BUG_ON(sizeof(dev
->me_clients_map
)
1346 < sizeof(enum_res
->valid_addresses
));
1347 memcpy(dev
->me_clients_map
, enum_res
->valid_addresses
,
1348 sizeof(enum_res
->valid_addresses
));
1350 if (dev
->dev_state
!= MEI_DEV_INIT_CLIENTS
||
1351 dev
->hbm_state
!= MEI_HBM_ENUM_CLIENTS
) {
1352 dev_err(dev
->dev
, "hbm: enumeration response: state mismatch, [%d, %d]\n",
1353 dev
->dev_state
, dev
->hbm_state
);
1357 dev
->hbm_state
= MEI_HBM_CLIENT_PROPERTIES
;
1359 /* first property request */
1360 if (mei_hbm_prop_req(dev
, 0))
1365 case HOST_STOP_RES_CMD
:
1366 dev_dbg(dev
->dev
, "hbm: stop response: message received\n");
1368 dev
->init_clients_timer
= 0;
1370 if (dev
->hbm_state
!= MEI_HBM_STOPPED
) {
1371 dev_err(dev
->dev
, "hbm: stop response: state mismatch, [%d, %d]\n",
1372 dev
->dev_state
, dev
->hbm_state
);
1376 dev
->dev_state
= MEI_DEV_POWER_DOWN
;
1377 dev_info(dev
->dev
, "hbm: stop response: resetting.\n");
1378 /* force the reset */
1381 case CLIENT_DISCONNECT_REQ_CMD
:
1382 dev_dbg(dev
->dev
, "hbm: disconnect request: message received\n");
1384 disconnect_req
= (struct hbm_client_connect_request
*)mei_msg
;
1385 mei_hbm_fw_disconnect_req(dev
, disconnect_req
);
1388 case ME_STOP_REQ_CMD
:
1389 dev_dbg(dev
->dev
, "hbm: stop request: message received\n");
1390 dev
->hbm_state
= MEI_HBM_STOPPED
;
1391 if (mei_hbm_stop_req(dev
)) {
1392 dev_err(dev
->dev
, "hbm: stop request: failed to send stop request\n");
1397 case MEI_HBM_ADD_CLIENT_REQ_CMD
:
1398 dev_dbg(dev
->dev
, "hbm: add client request received\n");
1400 * after the host receives the enum_resp
1401 * message clients may be added or removed
1403 if (dev
->hbm_state
<= MEI_HBM_ENUM_CLIENTS
||
1404 dev
->hbm_state
>= MEI_HBM_STOPPED
) {
1405 dev_err(dev
->dev
, "hbm: add client: state mismatch, [%d, %d]\n",
1406 dev
->dev_state
, dev
->hbm_state
);
1409 add_cl_req
= (struct hbm_add_client_request
*)mei_msg
;
1410 ret
= mei_hbm_fw_add_cl_req(dev
, add_cl_req
);
1412 dev_err(dev
->dev
, "hbm: add client: failed to send response %d\n",
1416 dev_dbg(dev
->dev
, "hbm: add client request processed\n");
1419 case MEI_HBM_NOTIFY_RES_CMD
:
1420 dev_dbg(dev
->dev
, "hbm: notify response received\n");
1421 mei_hbm_cl_res(dev
, cl_cmd
, notify_res_to_fop(cl_cmd
));
1424 case MEI_HBM_NOTIFICATION_CMD
:
1425 dev_dbg(dev
->dev
, "hbm: notification\n");
1426 mei_hbm_cl_notify(dev
, cl_cmd
);
1430 WARN(1, "hbm: wrong command %d\n", mei_msg
->hbm_cmd
);