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/export.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
23 #include <linux/mei.h>
29 static const char *mei_hbm_status_str(enum mei_hbm_status status)
31 #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
33 MEI_HBM_STATUS(SUCCESS);
34 MEI_HBM_STATUS(CLIENT_NOT_FOUND);
35 MEI_HBM_STATUS(ALREADY_EXISTS);
36 MEI_HBM_STATUS(REJECTED);
37 MEI_HBM_STATUS(INVALID_PARAMETER);
38 MEI_HBM_STATUS(NOT_ALLOWED);
39 MEI_HBM_STATUS(ALREADY_STARTED);
40 MEI_HBM_STATUS(NOT_STARTED);
41 default: return "unknown";
46 static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
48 #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
52 MEI_CL_CS(ALREADY_STARTED);
53 MEI_CL_CS(OUT_OF_RESOURCES);
54 MEI_CL_CS(MESSAGE_SMALL);
55 MEI_CL_CS(NOT_ALLOWED);
56 default: return "unknown";
61 const char *mei_hbm_state_str(enum mei_hbm_state state)
63 #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
66 MEI_HBM_STATE(STARTING);
67 MEI_HBM_STATE(STARTED);
68 MEI_HBM_STATE(DR_SETUP);
69 MEI_HBM_STATE(ENUM_CLIENTS);
70 MEI_HBM_STATE(CLIENT_PROPERTIES);
71 MEI_HBM_STATE(STOPPED);
79 * mei_cl_conn_status_to_errno - convert client connect response
80 * status to error code
82 * @status: client connect response status
84 * Return: corresponding error code
86 static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
89 case MEI_CL_CONN_SUCCESS: return 0;
90 case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
91 case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
92 case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
93 case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
94 case MEI_CL_CONN_NOT_ALLOWED: return -EBUSY;
95 default: return -EINVAL;
100 * mei_hbm_write_message - wrapper for sending hbm messages.
106 static inline int mei_hbm_write_message(struct mei_device *dev,
107 struct mei_msg_hdr *hdr,
110 return mei_write_message(dev, hdr, sizeof(*hdr), data, hdr->length);
114 * mei_hbm_idle - set hbm to idle state
116 * @dev: the device structure
118 void mei_hbm_idle(struct mei_device *dev)
120 dev->init_clients_timer = 0;
121 dev->hbm_state = MEI_HBM_IDLE;
125 * mei_hbm_reset - reset hbm counters and book keeping data structurs
127 * @dev: the device structure
129 void mei_hbm_reset(struct mei_device *dev)
131 mei_me_cl_rm_all(dev);
137 * mei_hbm_hdr - construct hbm header
140 * @length: payload length
143 static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length)
147 hdr->length = length;
148 hdr->msg_complete = 1;
155 * mei_hbm_cl_hdr - construct client hbm header
158 * @hbm_cmd: host bus message command
159 * @buf: buffer for cl header
160 * @len: buffer length
163 void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
165 struct mei_hbm_cl_cmd *cmd = buf;
169 cmd->hbm_cmd = hbm_cmd;
170 cmd->host_addr = mei_cl_host_addr(cl);
171 cmd->me_addr = mei_cl_me_id(cl);
175 * mei_hbm_cl_write - write simple hbm client message
177 * @dev: the device structure
179 * @hbm_cmd: host bus message command
180 * @buf: message buffer
181 * @len: buffer length
183 * Return: 0 on success, <0 on failure.
185 static inline int mei_hbm_cl_write(struct mei_device *dev, struct mei_cl *cl,
186 u8 hbm_cmd, void *buf, size_t len)
188 struct mei_msg_hdr mei_hdr;
190 mei_hbm_hdr(&mei_hdr, len);
191 mei_hbm_cl_hdr(cl, hbm_cmd, buf, len);
193 return mei_hbm_write_message(dev, &mei_hdr, buf);
197 * mei_hbm_cl_addr_equal - check if the client's and
198 * the message address match
201 * @cmd: hbm client message
203 * Return: true if addresses are the same
206 bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
208 return mei_cl_host_addr(cl) == cmd->host_addr &&
209 mei_cl_me_id(cl) == cmd->me_addr;
213 * mei_hbm_cl_find_by_cmd - find recipient client
215 * @dev: the device structure
216 * @buf: a buffer with hbm cl command
218 * Return: the recipient client or NULL if not found
221 struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
223 struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
226 list_for_each_entry(cl, &dev->file_list, link)
227 if (mei_hbm_cl_addr_equal(cl, cmd))
234 * mei_hbm_start_wait - wait for start response message.
236 * @dev: the device structure
238 * Return: 0 on success and < 0 on failure
240 int mei_hbm_start_wait(struct mei_device *dev)
244 if (dev->hbm_state > MEI_HBM_STARTING)
247 mutex_unlock(&dev->device_lock);
248 ret = wait_event_timeout(dev->wait_hbm_start,
249 dev->hbm_state != MEI_HBM_STARTING,
250 mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
251 mutex_lock(&dev->device_lock);
253 if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
254 dev->hbm_state = MEI_HBM_IDLE;
255 dev_err(dev->dev, "waiting for mei start failed\n");
262 * mei_hbm_start_req - sends start request message.
264 * @dev: the device structure
266 * Return: 0 on success and < 0 on failure
268 int mei_hbm_start_req(struct mei_device *dev)
270 struct mei_msg_hdr mei_hdr;
271 struct hbm_host_version_request start_req;
272 const size_t len = sizeof(struct hbm_host_version_request);
277 mei_hbm_hdr(&mei_hdr, len);
279 /* host start message */
280 memset(&start_req, 0, len);
281 start_req.hbm_cmd = HOST_START_REQ_CMD;
282 start_req.host_version.major_version = HBM_MAJOR_VERSION;
283 start_req.host_version.minor_version = HBM_MINOR_VERSION;
285 dev->hbm_state = MEI_HBM_IDLE;
286 ret = mei_hbm_write_message(dev, &mei_hdr, &start_req);
288 dev_err(dev->dev, "version message write failed: ret = %d\n",
293 dev->hbm_state = MEI_HBM_STARTING;
294 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
295 mei_schedule_stall_timer(dev);
300 * mei_hbm_dma_setup_req() - setup DMA request
301 * @dev: the device structure
303 * Return: 0 on success and < 0 on failure
305 static int mei_hbm_dma_setup_req(struct mei_device *dev)
307 struct mei_msg_hdr mei_hdr;
308 struct hbm_dma_setup_request req;
309 const size_t len = sizeof(struct hbm_dma_setup_request);
313 mei_hbm_hdr(&mei_hdr, len);
315 memset(&req, 0, len);
316 req.hbm_cmd = MEI_HBM_DMA_SETUP_REQ_CMD;
317 for (i = 0; i < DMA_DSCR_NUM; i++) {
320 paddr = dev->dr_dscr[i].daddr;
321 req.dma_dscr[i].addr_hi = upper_32_bits(paddr);
322 req.dma_dscr[i].addr_lo = lower_32_bits(paddr);
323 req.dma_dscr[i].size = dev->dr_dscr[i].size;
326 mei_dma_ring_reset(dev);
328 ret = mei_hbm_write_message(dev, &mei_hdr, &req);
330 dev_err(dev->dev, "dma setup request write failed: ret = %d.\n",
335 dev->hbm_state = MEI_HBM_DR_SETUP;
336 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
337 mei_schedule_stall_timer(dev);
342 * mei_hbm_enum_clients_req - sends enumeration client request message.
344 * @dev: the device structure
346 * Return: 0 on success and < 0 on failure
348 static int mei_hbm_enum_clients_req(struct mei_device *dev)
350 struct mei_msg_hdr mei_hdr;
351 struct hbm_host_enum_request enum_req;
352 const size_t len = sizeof(struct hbm_host_enum_request);
355 /* enumerate clients */
356 mei_hbm_hdr(&mei_hdr, len);
358 memset(&enum_req, 0, len);
359 enum_req.hbm_cmd = HOST_ENUM_REQ_CMD;
360 enum_req.flags |= dev->hbm_f_dc_supported ?
361 MEI_HBM_ENUM_F_ALLOW_ADD : 0;
362 enum_req.flags |= dev->hbm_f_ie_supported ?
363 MEI_HBM_ENUM_F_IMMEDIATE_ENUM : 0;
365 ret = mei_hbm_write_message(dev, &mei_hdr, &enum_req);
367 dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
371 dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
372 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
373 mei_schedule_stall_timer(dev);
378 * mei_hbm_me_cl_add - add new me client to the list
380 * @dev: the device structure
381 * @res: hbm property response
383 * Return: 0 on success and -ENOMEM on allocation failure
386 static int mei_hbm_me_cl_add(struct mei_device *dev,
387 struct hbm_props_response *res)
389 struct mei_me_client *me_cl;
390 const uuid_le *uuid = &res->client_properties.protocol_name;
392 mei_me_cl_rm_by_uuid(dev, uuid);
394 me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
398 mei_me_cl_init(me_cl);
400 me_cl->props = res->client_properties;
401 me_cl->client_id = res->me_addr;
402 me_cl->tx_flow_ctrl_creds = 0;
404 mei_me_cl_add(dev, me_cl);
410 * mei_hbm_add_cl_resp - send response to fw on client add request
412 * @dev: the device structure
414 * @status: response status
416 * Return: 0 on success and < 0 on failure
418 static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status)
420 struct mei_msg_hdr mei_hdr;
421 struct hbm_add_client_response resp;
422 const size_t len = sizeof(struct hbm_add_client_response);
425 dev_dbg(dev->dev, "adding client response\n");
427 mei_hbm_hdr(&mei_hdr, len);
429 memset(&resp, 0, sizeof(struct hbm_add_client_response));
430 resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
432 resp.status = status;
434 ret = mei_hbm_write_message(dev, &mei_hdr, &resp);
436 dev_err(dev->dev, "add client response write failed: ret = %d\n",
442 * mei_hbm_fw_add_cl_req - request from the fw to add a client
444 * @dev: the device structure
445 * @req: add client request
447 * Return: 0 on success and < 0 on failure
449 static int mei_hbm_fw_add_cl_req(struct mei_device *dev,
450 struct hbm_add_client_request *req)
453 u8 status = MEI_HBMS_SUCCESS;
455 BUILD_BUG_ON(sizeof(struct hbm_add_client_request) !=
456 sizeof(struct hbm_props_response));
458 ret = mei_hbm_me_cl_add(dev, (struct hbm_props_response *)req);
460 status = !MEI_HBMS_SUCCESS;
462 if (dev->dev_state == MEI_DEV_ENABLED)
463 schedule_work(&dev->bus_rescan_work);
465 return mei_hbm_add_cl_resp(dev, req->me_addr, status);
469 * mei_hbm_cl_notify_req - send notification request
471 * @dev: the device structure
472 * @cl: a client to disconnect from
473 * @start: true for start false for stop
475 * Return: 0 on success and -EIO on write failure
477 int mei_hbm_cl_notify_req(struct mei_device *dev,
478 struct mei_cl *cl, u8 start)
481 struct mei_msg_hdr mei_hdr;
482 struct hbm_notification_request req;
483 const size_t len = sizeof(struct hbm_notification_request);
486 mei_hbm_hdr(&mei_hdr, len);
487 mei_hbm_cl_hdr(cl, MEI_HBM_NOTIFY_REQ_CMD, &req, len);
491 ret = mei_hbm_write_message(dev, &mei_hdr, &req);
493 dev_err(dev->dev, "notify request failed: ret = %d\n", ret);
499 * notify_res_to_fop - convert notification response to the proper
502 * @cmd: client notification start response command
504 * Return: MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP;
506 static inline enum mei_cb_file_ops notify_res_to_fop(struct mei_hbm_cl_cmd *cmd)
508 struct hbm_notification_response *rs =
509 (struct hbm_notification_response *)cmd;
511 return mei_cl_notify_req2fop(rs->start);
515 * mei_hbm_cl_notify_start_res - update the client state according
516 * notify start response
518 * @dev: the device structure
519 * @cl: mei host client
520 * @cmd: client notification start response command
522 static void mei_hbm_cl_notify_start_res(struct mei_device *dev,
524 struct mei_hbm_cl_cmd *cmd)
526 struct hbm_notification_response *rs =
527 (struct hbm_notification_response *)cmd;
529 cl_dbg(dev, cl, "hbm: notify start response status=%d\n", rs->status);
531 if (rs->status == MEI_HBMS_SUCCESS ||
532 rs->status == MEI_HBMS_ALREADY_STARTED) {
533 cl->notify_en = true;
536 cl->status = -EINVAL;
541 * mei_hbm_cl_notify_stop_res - update the client state according
542 * notify stop response
544 * @dev: the device structure
545 * @cl: mei host client
546 * @cmd: client notification stop response command
548 static void mei_hbm_cl_notify_stop_res(struct mei_device *dev,
550 struct mei_hbm_cl_cmd *cmd)
552 struct hbm_notification_response *rs =
553 (struct hbm_notification_response *)cmd;
555 cl_dbg(dev, cl, "hbm: notify stop response status=%d\n", rs->status);
557 if (rs->status == MEI_HBMS_SUCCESS ||
558 rs->status == MEI_HBMS_NOT_STARTED) {
559 cl->notify_en = false;
562 /* TODO: spec is not clear yet about other possible issues */
563 cl->status = -EINVAL;
568 * mei_hbm_cl_notify - signal notification event
570 * @dev: the device structure
571 * @cmd: notification client message
573 static void mei_hbm_cl_notify(struct mei_device *dev,
574 struct mei_hbm_cl_cmd *cmd)
578 cl = mei_hbm_cl_find_by_cmd(dev, cmd);
584 * mei_hbm_prop_req - request property for a single client
586 * @dev: the device structure
587 * @start_idx: client index to start search
589 * Return: 0 on success and < 0 on failure
591 static int mei_hbm_prop_req(struct mei_device *dev, unsigned long start_idx)
593 struct mei_msg_hdr mei_hdr;
594 struct hbm_props_request prop_req;
595 const size_t len = sizeof(struct hbm_props_request);
599 addr = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, start_idx);
601 /* We got all client properties */
602 if (addr == MEI_CLIENTS_MAX) {
603 dev->hbm_state = MEI_HBM_STARTED;
604 mei_host_client_init(dev);
609 mei_hbm_hdr(&mei_hdr, len);
611 memset(&prop_req, 0, sizeof(struct hbm_props_request));
613 prop_req.hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
614 prop_req.me_addr = addr;
616 ret = mei_hbm_write_message(dev, &mei_hdr, &prop_req);
618 dev_err(dev->dev, "properties request write failed: ret = %d\n",
623 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
624 mei_schedule_stall_timer(dev);
630 * mei_hbm_pg - sends pg command
632 * @dev: the device structure
633 * @pg_cmd: the pg command code
635 * Return: -EIO on write failure
636 * -EOPNOTSUPP if the operation is not supported by the protocol
638 int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
640 struct mei_msg_hdr mei_hdr;
641 struct hbm_power_gate req;
642 const size_t len = sizeof(struct hbm_power_gate);
645 if (!dev->hbm_f_pg_supported)
648 mei_hbm_hdr(&mei_hdr, len);
650 memset(&req, 0, len);
651 req.hbm_cmd = pg_cmd;
653 ret = mei_hbm_write_message(dev, &mei_hdr, &req);
655 dev_err(dev->dev, "power gate command write failed.\n");
658 EXPORT_SYMBOL_GPL(mei_hbm_pg);
661 * mei_hbm_stop_req - send stop request message
665 * Return: -EIO on write failure
667 static int mei_hbm_stop_req(struct mei_device *dev)
669 struct mei_msg_hdr mei_hdr;
670 struct hbm_host_stop_request req;
671 const size_t len = sizeof(struct hbm_host_stop_request);
673 mei_hbm_hdr(&mei_hdr, len);
675 memset(&req, 0, len);
676 req.hbm_cmd = HOST_STOP_REQ_CMD;
677 req.reason = DRIVER_STOP_REQUEST;
679 return mei_hbm_write_message(dev, &mei_hdr, &req);
683 * mei_hbm_cl_flow_control_req - sends flow control request.
685 * @dev: the device structure
688 * Return: -EIO on write failure
690 int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
692 struct hbm_flow_control req;
694 cl_dbg(dev, cl, "sending flow control\n");
695 return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD,
700 * mei_hbm_add_single_tx_flow_ctrl_creds - adds single buffer credentials.
702 * @dev: the device structure
703 * @fctrl: flow control response bus message
705 * Return: 0 on success, < 0 otherwise
707 static int mei_hbm_add_single_tx_flow_ctrl_creds(struct mei_device *dev,
708 struct hbm_flow_control *fctrl)
710 struct mei_me_client *me_cl;
713 me_cl = mei_me_cl_by_id(dev, fctrl->me_addr);
715 dev_err(dev->dev, "no such me client %d\n", fctrl->me_addr);
719 if (WARN_ON(me_cl->props.single_recv_buf == 0)) {
724 me_cl->tx_flow_ctrl_creds++;
725 dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
726 fctrl->me_addr, me_cl->tx_flow_ctrl_creds);
730 mei_me_cl_put(me_cl);
735 * mei_hbm_cl_flow_control_res - flow control response from me
737 * @dev: the device structure
738 * @fctrl: flow control response bus message
740 static void mei_hbm_cl_tx_flow_ctrl_creds_res(struct mei_device *dev,
741 struct hbm_flow_control *fctrl)
745 if (!fctrl->host_addr) {
746 /* single receive buffer */
747 mei_hbm_add_single_tx_flow_ctrl_creds(dev, fctrl);
751 cl = mei_hbm_cl_find_by_cmd(dev, fctrl);
753 cl->tx_flow_ctrl_creds++;
754 cl_dbg(dev, cl, "flow control creds = %d.\n",
755 cl->tx_flow_ctrl_creds);
761 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
763 * @dev: the device structure
764 * @cl: a client to disconnect from
766 * Return: -EIO on write failure
768 int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
770 struct hbm_client_connect_request req;
772 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD,
777 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
779 * @dev: the device structure
780 * @cl: a client to disconnect from
782 * Return: -EIO on write failure
784 int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
786 struct hbm_client_connect_response resp;
788 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD,
789 &resp, sizeof(resp));
793 * mei_hbm_cl_disconnect_res - update the client state according
794 * disconnect response
796 * @dev: the device structure
797 * @cl: mei host client
798 * @cmd: disconnect client response host bus message
800 static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
801 struct mei_hbm_cl_cmd *cmd)
803 struct hbm_client_connect_response *rs =
804 (struct hbm_client_connect_response *)cmd;
806 cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
808 if (rs->status == MEI_CL_DISCONN_SUCCESS)
809 cl->state = MEI_FILE_DISCONNECT_REPLY;
814 * mei_hbm_cl_connect_req - send connection request to specific me client
816 * @dev: the device structure
817 * @cl: a client to connect to
819 * Return: -EIO on write failure
821 int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
823 struct hbm_client_connect_request req;
825 return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD,
830 * mei_hbm_cl_connect_res - update the client state according
831 * connection response
833 * @dev: the device structure
834 * @cl: mei host client
835 * @cmd: connect client response host bus message
837 static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
838 struct mei_hbm_cl_cmd *cmd)
840 struct hbm_client_connect_response *rs =
841 (struct hbm_client_connect_response *)cmd;
843 cl_dbg(dev, cl, "hbm: connect response status=%s\n",
844 mei_cl_conn_status_str(rs->status));
846 if (rs->status == MEI_CL_CONN_SUCCESS)
847 cl->state = MEI_FILE_CONNECTED;
849 cl->state = MEI_FILE_DISCONNECT_REPLY;
850 if (rs->status == MEI_CL_CONN_NOT_FOUND) {
851 mei_me_cl_del(dev, cl->me_cl);
852 if (dev->dev_state == MEI_DEV_ENABLED)
853 schedule_work(&dev->bus_rescan_work);
856 cl->status = mei_cl_conn_status_to_errno(rs->status);
860 * mei_hbm_cl_res - process hbm response received on behalf
863 * @dev: the device structure
864 * @rs: hbm client message
865 * @fop_type: file operation type
867 static void mei_hbm_cl_res(struct mei_device *dev,
868 struct mei_hbm_cl_cmd *rs,
869 enum mei_cb_file_ops fop_type)
872 struct mei_cl_cb *cb, *next;
875 list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) {
879 if (cb->fop_type != fop_type)
882 if (mei_hbm_cl_addr_equal(cl, rs)) {
883 list_del_init(&cb->list);
892 case MEI_FOP_CONNECT:
893 mei_hbm_cl_connect_res(dev, cl, rs);
895 case MEI_FOP_DISCONNECT:
896 mei_hbm_cl_disconnect_res(dev, cl, rs);
898 case MEI_FOP_NOTIFY_START:
899 mei_hbm_cl_notify_start_res(dev, cl, rs);
901 case MEI_FOP_NOTIFY_STOP:
902 mei_hbm_cl_notify_stop_res(dev, cl, rs);
914 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
915 * host sends disconnect response
917 * @dev: the device structure.
918 * @disconnect_req: disconnect request bus message from the me
920 * Return: -ENOMEM on allocation failure
922 static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
923 struct hbm_client_connect_request *disconnect_req)
926 struct mei_cl_cb *cb;
928 cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
930 cl_warn(dev, cl, "fw disconnect request received\n");
931 cl->state = MEI_FILE_DISCONNECTING;
934 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT_RSP,
943 * mei_hbm_pg_enter_res - PG enter response received
945 * @dev: the device structure.
947 * Return: 0 on success, -EPROTO on state mismatch
949 static int mei_hbm_pg_enter_res(struct mei_device *dev)
951 if (mei_pg_state(dev) != MEI_PG_OFF ||
952 dev->pg_event != MEI_PG_EVENT_WAIT) {
953 dev_err(dev->dev, "hbm: pg entry response: state mismatch [%s, %d]\n",
954 mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
958 dev->pg_event = MEI_PG_EVENT_RECEIVED;
959 wake_up(&dev->wait_pg);
965 * mei_hbm_pg_resume - process with PG resume
967 * @dev: the device structure.
969 void mei_hbm_pg_resume(struct mei_device *dev)
971 pm_request_resume(dev->dev);
973 EXPORT_SYMBOL_GPL(mei_hbm_pg_resume);
976 * mei_hbm_pg_exit_res - PG exit response received
978 * @dev: the device structure.
980 * Return: 0 on success, -EPROTO on state mismatch
982 static int mei_hbm_pg_exit_res(struct mei_device *dev)
984 if (mei_pg_state(dev) != MEI_PG_ON ||
985 (dev->pg_event != MEI_PG_EVENT_WAIT &&
986 dev->pg_event != MEI_PG_EVENT_IDLE)) {
987 dev_err(dev->dev, "hbm: pg exit response: state mismatch [%s, %d]\n",
988 mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
992 switch (dev->pg_event) {
993 case MEI_PG_EVENT_WAIT:
994 dev->pg_event = MEI_PG_EVENT_RECEIVED;
995 wake_up(&dev->wait_pg);
997 case MEI_PG_EVENT_IDLE:
999 * If the driver is not waiting on this then
1000 * this is HW initiated exit from PG.
1001 * Start runtime pm resume sequence to exit from PG.
1003 dev->pg_event = MEI_PG_EVENT_RECEIVED;
1004 mei_hbm_pg_resume(dev);
1007 WARN(1, "hbm: pg exit response: unexpected pg event = %d\n",
1016 * mei_hbm_config_features - check what hbm features and commands
1017 * are supported by the fw
1019 * @dev: the device structure
1021 static void mei_hbm_config_features(struct mei_device *dev)
1023 /* Power Gating Isolation Support */
1024 dev->hbm_f_pg_supported = 0;
1025 if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
1026 dev->hbm_f_pg_supported = 1;
1028 if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
1029 dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
1030 dev->hbm_f_pg_supported = 1;
1032 if (dev->version.major_version >= HBM_MAJOR_VERSION_DC)
1033 dev->hbm_f_dc_supported = 1;
1035 if (dev->version.major_version >= HBM_MAJOR_VERSION_IE)
1036 dev->hbm_f_ie_supported = 1;
1038 /* disconnect on connect timeout instead of link reset */
1039 if (dev->version.major_version >= HBM_MAJOR_VERSION_DOT)
1040 dev->hbm_f_dot_supported = 1;
1042 /* Notification Event Support */
1043 if (dev->version.major_version >= HBM_MAJOR_VERSION_EV)
1044 dev->hbm_f_ev_supported = 1;
1046 /* Fixed Address Client Support */
1047 if (dev->version.major_version >= HBM_MAJOR_VERSION_FA)
1048 dev->hbm_f_fa_supported = 1;
1050 /* OS ver message Support */
1051 if (dev->version.major_version >= HBM_MAJOR_VERSION_OS)
1052 dev->hbm_f_os_supported = 1;
1054 /* DMA Ring Support */
1055 if (dev->version.major_version > HBM_MAJOR_VERSION_DR ||
1056 (dev->version.major_version == HBM_MAJOR_VERSION_DR &&
1057 dev->version.minor_version >= HBM_MINOR_VERSION_DR))
1058 dev->hbm_f_dr_supported = 1;
1062 * mei_hbm_version_is_supported - checks whether the driver can
1063 * support the hbm version of the device
1065 * @dev: the device structure
1066 * Return: true if driver can support hbm version of the device
1068 bool mei_hbm_version_is_supported(struct mei_device *dev)
1070 return (dev->version.major_version < HBM_MAJOR_VERSION) ||
1071 (dev->version.major_version == HBM_MAJOR_VERSION &&
1072 dev->version.minor_version <= HBM_MINOR_VERSION);
1076 * mei_hbm_dispatch - bottom half read routine after ISR to
1077 * handle the read bus message cmd processing.
1079 * @dev: the device structure
1080 * @hdr: header of bus message
1082 * Return: 0 on success and < 0 on failure
1084 int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
1086 struct mei_bus_message *mei_msg;
1087 struct hbm_host_version_response *version_res;
1088 struct hbm_props_response *props_res;
1089 struct hbm_host_enum_response *enum_res;
1090 struct hbm_dma_setup_response *dma_setup_res;
1091 struct hbm_add_client_request *add_cl_req;
1094 struct mei_hbm_cl_cmd *cl_cmd;
1095 struct hbm_client_connect_request *disconnect_req;
1096 struct hbm_flow_control *fctrl;
1098 /* read the message to our buffer */
1099 BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
1100 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
1101 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
1102 cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg;
1104 /* ignore spurious message and prevent reset nesting
1105 * hbm is put to idle during system reset
1107 if (dev->hbm_state == MEI_HBM_IDLE) {
1108 dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
1112 switch (mei_msg->hbm_cmd) {
1113 case HOST_START_RES_CMD:
1114 dev_dbg(dev->dev, "hbm: start: response message received.\n");
1116 dev->init_clients_timer = 0;
1118 version_res = (struct hbm_host_version_response *)mei_msg;
1120 dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
1121 HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
1122 version_res->me_max_version.major_version,
1123 version_res->me_max_version.minor_version);
1125 if (version_res->host_version_supported) {
1126 dev->version.major_version = HBM_MAJOR_VERSION;
1127 dev->version.minor_version = HBM_MINOR_VERSION;
1129 dev->version.major_version =
1130 version_res->me_max_version.major_version;
1131 dev->version.minor_version =
1132 version_res->me_max_version.minor_version;
1135 if (!mei_hbm_version_is_supported(dev)) {
1136 dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
1138 dev->hbm_state = MEI_HBM_STOPPED;
1139 if (mei_hbm_stop_req(dev)) {
1140 dev_err(dev->dev, "hbm: start: failed to send stop request\n");
1146 mei_hbm_config_features(dev);
1148 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1149 dev->hbm_state != MEI_HBM_STARTING) {
1150 dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
1151 dev->dev_state, dev->hbm_state);
1155 if (dev->hbm_f_dr_supported) {
1156 if (mei_dmam_ring_alloc(dev))
1157 dev_info(dev->dev, "running w/o dma ring\n");
1158 if (mei_dma_ring_is_allocated(dev)) {
1159 if (mei_hbm_dma_setup_req(dev))
1162 wake_up(&dev->wait_hbm_start);
1167 dev->hbm_f_dr_supported = 0;
1168 mei_dmam_ring_free(dev);
1170 if (mei_hbm_enum_clients_req(dev))
1173 wake_up(&dev->wait_hbm_start);
1176 case MEI_HBM_DMA_SETUP_RES_CMD:
1177 dev_dbg(dev->dev, "hbm: dma setup response: message received.\n");
1179 dev->init_clients_timer = 0;
1181 if (dev->hbm_state != MEI_HBM_DR_SETUP) {
1182 dev_err(dev->dev, "hbm: dma setup response: state mismatch, [%d, %d]\n",
1183 dev->dev_state, dev->hbm_state);
1187 dma_setup_res = (struct hbm_dma_setup_response *)mei_msg;
1189 if (dma_setup_res->status) {
1190 u8 status = dma_setup_res->status;
1192 if (status == MEI_HBMS_NOT_ALLOWED) {
1193 dev_dbg(dev->dev, "hbm: dma setup not allowed\n");
1195 dev_info(dev->dev, "hbm: dma setup response: failure = %d %s\n",
1197 mei_hbm_status_str(status));
1199 dev->hbm_f_dr_supported = 0;
1200 mei_dmam_ring_free(dev);
1203 if (mei_hbm_enum_clients_req(dev))
1207 case CLIENT_CONNECT_RES_CMD:
1208 dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
1209 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
1212 case CLIENT_DISCONNECT_RES_CMD:
1213 dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
1214 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
1217 case MEI_FLOW_CONTROL_CMD:
1218 dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
1220 fctrl = (struct hbm_flow_control *)mei_msg;
1221 mei_hbm_cl_tx_flow_ctrl_creds_res(dev, fctrl);
1224 case MEI_PG_ISOLATION_ENTRY_RES_CMD:
1225 dev_dbg(dev->dev, "hbm: power gate isolation entry response received\n");
1226 ret = mei_hbm_pg_enter_res(dev);
1231 case MEI_PG_ISOLATION_EXIT_REQ_CMD:
1232 dev_dbg(dev->dev, "hbm: power gate isolation exit request received\n");
1233 ret = mei_hbm_pg_exit_res(dev);
1238 case HOST_CLIENT_PROPERTIES_RES_CMD:
1239 dev_dbg(dev->dev, "hbm: properties response: message received.\n");
1241 dev->init_clients_timer = 0;
1243 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1244 dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
1245 dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
1246 dev->dev_state, dev->hbm_state);
1250 props_res = (struct hbm_props_response *)mei_msg;
1252 if (props_res->status == MEI_HBMS_CLIENT_NOT_FOUND) {
1253 dev_dbg(dev->dev, "hbm: properties response: %d CLIENT_NOT_FOUND\n",
1254 props_res->me_addr);
1255 } else if (props_res->status) {
1256 dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
1258 mei_hbm_status_str(props_res->status));
1261 mei_hbm_me_cl_add(dev, props_res);
1264 /* request property for the next client */
1265 if (mei_hbm_prop_req(dev, props_res->me_addr + 1))
1270 case HOST_ENUM_RES_CMD:
1271 dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
1273 dev->init_clients_timer = 0;
1275 enum_res = (struct hbm_host_enum_response *) mei_msg;
1276 BUILD_BUG_ON(sizeof(dev->me_clients_map)
1277 < sizeof(enum_res->valid_addresses));
1278 memcpy(dev->me_clients_map, enum_res->valid_addresses,
1279 sizeof(enum_res->valid_addresses));
1281 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1282 dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
1283 dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
1284 dev->dev_state, dev->hbm_state);
1288 dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
1290 /* first property request */
1291 if (mei_hbm_prop_req(dev, 0))
1296 case HOST_STOP_RES_CMD:
1297 dev_dbg(dev->dev, "hbm: stop response: message received\n");
1299 dev->init_clients_timer = 0;
1301 if (dev->hbm_state != MEI_HBM_STOPPED) {
1302 dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
1303 dev->dev_state, dev->hbm_state);
1307 dev->dev_state = MEI_DEV_POWER_DOWN;
1308 dev_info(dev->dev, "hbm: stop response: resetting.\n");
1309 /* force the reset */
1313 case CLIENT_DISCONNECT_REQ_CMD:
1314 dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
1316 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
1317 mei_hbm_fw_disconnect_req(dev, disconnect_req);
1320 case ME_STOP_REQ_CMD:
1321 dev_dbg(dev->dev, "hbm: stop request: message received\n");
1322 dev->hbm_state = MEI_HBM_STOPPED;
1323 if (mei_hbm_stop_req(dev)) {
1324 dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
1329 case MEI_HBM_ADD_CLIENT_REQ_CMD:
1330 dev_dbg(dev->dev, "hbm: add client request received\n");
1332 * after the host receives the enum_resp
1333 * message clients may be added or removed
1335 if (dev->hbm_state <= MEI_HBM_ENUM_CLIENTS ||
1336 dev->hbm_state >= MEI_HBM_STOPPED) {
1337 dev_err(dev->dev, "hbm: add client: state mismatch, [%d, %d]\n",
1338 dev->dev_state, dev->hbm_state);
1341 add_cl_req = (struct hbm_add_client_request *)mei_msg;
1342 ret = mei_hbm_fw_add_cl_req(dev, add_cl_req);
1344 dev_err(dev->dev, "hbm: add client: failed to send response %d\n",
1348 dev_dbg(dev->dev, "hbm: add client request processed\n");
1351 case MEI_HBM_NOTIFY_RES_CMD:
1352 dev_dbg(dev->dev, "hbm: notify response received\n");
1353 mei_hbm_cl_res(dev, cl_cmd, notify_res_to_fop(cl_cmd));
1356 case MEI_HBM_NOTIFICATION_CMD:
1357 dev_dbg(dev->dev, "hbm: notification\n");
1358 mei_hbm_cl_notify(dev, cl_cmd);
1362 WARN(1, "hbm: wrong command %d\n", mei_msg->hbm_cmd);