1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2013-2016 Freescale Semiconductor Inc.
4 * Copyright 2016-2017 NXP
12 #include <rte_byteorder.h>
13 #include <rte_compat.h>
15 #define MC_CMD_NUM_OF_PARAMS 7
17 #define phys_addr_t uint64_t
24 #define cpu_to_le64 rte_cpu_to_le_64
25 #define cpu_to_le32 rte_cpu_to_le_32
26 #define cpu_to_le16 rte_cpu_to_le_16
28 #define le64_to_cpu rte_le_to_cpu_64
29 #define le32_to_cpu rte_le_to_cpu_32
30 #define le16_to_cpu rte_le_to_cpu_16
32 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
33 #define GENMASK(h, l) \
34 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
36 struct mc_cmd_header {
52 uint64_t params[MC_CMD_NUM_OF_PARAMS];
55 struct mc_rsp_create {
60 MC_CMD_STATUS_OK = 0x0, /* Completed successfully */
61 MC_CMD_STATUS_READY = 0x1, /* Ready to be processed */
62 MC_CMD_STATUS_AUTH_ERR = 0x3, /* Authentication error */
63 MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /* No privilege */
64 MC_CMD_STATUS_DMA_ERR = 0x5, /* DMA or I/O error */
65 MC_CMD_STATUS_CONFIG_ERR = 0x6, /* Configuration error */
66 MC_CMD_STATUS_TIMEOUT = 0x7, /* Operation timed out */
67 MC_CMD_STATUS_NO_RESOURCE = 0x8, /* No resources */
68 MC_CMD_STATUS_NO_MEMORY = 0x9, /* No memory available */
69 MC_CMD_STATUS_BUSY = 0xA, /* Device is busy */
70 MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /* Unsupported operation */
71 MC_CMD_STATUS_INVALID_STATE = 0xC /* Invalid state */
78 /* High priority flag */
79 #define MC_CMD_FLAG_PRI 0x80
80 /* Command completion flag */
81 #define MC_CMD_FLAG_INTR_DIS 0x01
83 #define MC_CMD_HDR_FLAGS_MASK 0xFF00FF00
86 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd);
88 static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
93 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&header;
95 hdr->cmd_id = cpu_to_le16(cmd_id);
96 hdr->token = cpu_to_le16(token);
97 hdr->status = MC_CMD_STATUS_READY;
98 hdr->word[0] |= cpu_to_le32(cmd_flags & MC_CMD_HDR_FLAGS_MASK);
103 static inline uint16_t mc_cmd_hdr_read_token(struct mc_command *cmd)
105 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
106 uint16_t token = le16_to_cpu(hdr->token);
111 static inline uint32_t mc_cmd_read_object_id(struct mc_command *cmd)
113 struct mc_rsp_create *rsp_params;
115 rsp_params = (struct mc_rsp_create *)cmd->params;
116 return le32_to_cpu(rsp_params->object_id);
119 static inline enum mc_cmd_status mc_cmd_read_status(struct mc_command *cmd)
121 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
122 uint8_t status = hdr->status;
124 return (enum mc_cmd_status)status;
128 * mc_write_command - writes a command to a Management Complex (MC) portal
130 * @portal: pointer to an MC portal
131 * @cmd: pointer to a filled command
133 static inline void mc_write_command(struct mc_command __iomem *portal,
134 struct mc_command *cmd)
136 struct mc_cmd_header *cmd_header = (struct mc_cmd_header *)&cmd->header;
137 char *header = (char *)&portal->header;
140 /* copy command parameters into the portal */
141 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
142 iowrite64(cmd->params[i], &portal->params[i]);
144 /* submit the command by writing the header */
145 iowrite32(le32_to_cpu(cmd_header->word[1]), (((uint32_t *)header) + 1));
146 iowrite32(le32_to_cpu(cmd_header->word[0]), (uint32_t *)header);
150 * mc_read_response - reads the response for the last MC command from a
151 * Management Complex (MC) portal
153 * @portal: pointer to an MC portal
154 * @resp: pointer to command response buffer
156 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
158 static inline enum mc_cmd_status mc_read_response(
159 struct mc_command __iomem *portal,
160 struct mc_command *resp)
163 enum mc_cmd_status status;
165 /* Copy command response header from MC portal: */
166 resp->header = ioread64(&portal->header);
167 status = mc_cmd_read_status(resp);
168 if (status != MC_CMD_STATUS_OK)
171 /* Copy command response data from MC portal: */
172 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
173 resp->params[i] = ioread64(&portal->params[i]);
178 #endif /* __FSL_MC_CMD_H */