New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / fslmc / mc / fsl_mc_cmd.h
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2013-2016 Freescale Semiconductor Inc.
8  * Copyright 2016-2017 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 #ifndef __FSL_MC_CMD_H
41 #define __FSL_MC_CMD_H
42
43 #include <rte_byteorder.h>
44 #include <stdint.h>
45
46 #define MC_CMD_NUM_OF_PARAMS    7
47
48 #define phys_addr_t     uint64_t
49
50 #define u64     uint64_t
51 #define u32     uint32_t
52 #define u16     uint16_t
53 #define u8      uint8_t
54
55 #define cpu_to_le64     rte_cpu_to_le_64
56 #define cpu_to_le32     rte_cpu_to_le_32
57 #define cpu_to_le16     rte_cpu_to_le_16
58
59 #define le64_to_cpu     rte_le_to_cpu_64
60 #define le32_to_cpu     rte_le_to_cpu_32
61 #define le16_to_cpu     rte_le_to_cpu_16
62
63 #define BITS_PER_LONG                   64
64 #define GENMASK(h, l) \
65                 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
66
67 struct mc_cmd_header {
68         union {
69                 struct {
70                         uint8_t src_id;
71                         uint8_t flags_hw;
72                         uint8_t status;
73                         uint8_t flags_sw;
74                         uint16_t token;
75                         uint16_t cmd_id;
76                 };
77                 uint32_t word[2];
78         };
79 };
80
81 struct mc_command {
82         uint64_t header;
83         uint64_t params[MC_CMD_NUM_OF_PARAMS];
84 };
85
86 struct mc_rsp_create {
87         uint32_t object_id;
88 };
89
90 enum mc_cmd_status {
91         MC_CMD_STATUS_OK = 0x0, /* Completed successfully */
92         MC_CMD_STATUS_READY = 0x1, /* Ready to be processed */
93         MC_CMD_STATUS_AUTH_ERR = 0x3, /* Authentication error */
94         MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /* No privilege */
95         MC_CMD_STATUS_DMA_ERR = 0x5, /* DMA or I/O error */
96         MC_CMD_STATUS_CONFIG_ERR = 0x6, /* Configuration error */
97         MC_CMD_STATUS_TIMEOUT = 0x7, /* Operation timed out */
98         MC_CMD_STATUS_NO_RESOURCE = 0x8, /* No resources */
99         MC_CMD_STATUS_NO_MEMORY = 0x9, /* No memory available */
100         MC_CMD_STATUS_BUSY = 0xA, /* Device is busy */
101         MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /* Unsupported operation */
102         MC_CMD_STATUS_INVALID_STATE = 0xC /* Invalid state */
103 };
104
105 /*
106  * MC command flags
107  */
108
109 /* High priority flag */
110 #define MC_CMD_FLAG_PRI         0x80
111 /* Command completion flag */
112 #define MC_CMD_FLAG_INTR_DIS    0x01
113
114 #define MC_CMD_HDR_FLAGS_MASK   0xFF00FF00
115
116 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd);
117
118 static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
119                                             uint32_t cmd_flags,
120                                             uint16_t token)
121 {
122         uint64_t header = 0;
123         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&header;
124
125         hdr->cmd_id = cpu_to_le16(cmd_id);
126         hdr->token = cpu_to_le16(token);
127         hdr->status = MC_CMD_STATUS_READY;
128         hdr->word[0] |= cpu_to_le32(cmd_flags & MC_CMD_HDR_FLAGS_MASK);
129
130         return header;
131 }
132
133 static inline uint16_t mc_cmd_hdr_read_token(struct mc_command *cmd)
134 {
135         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
136         uint16_t token = le16_to_cpu(hdr->token);
137
138         return token;
139 }
140
141 static inline uint32_t mc_cmd_read_object_id(struct mc_command *cmd)
142 {
143         struct mc_rsp_create *rsp_params;
144
145         rsp_params = (struct mc_rsp_create *)cmd->params;
146         return le32_to_cpu(rsp_params->object_id);
147 }
148
149 static inline enum mc_cmd_status mc_cmd_read_status(struct mc_command *cmd)
150 {
151         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
152         uint8_t status = hdr->status;
153
154         return (enum mc_cmd_status)status;
155 }
156
157 /**
158  * mc_write_command - writes a command to a Management Complex (MC) portal
159  *
160  * @portal: pointer to an MC portal
161  * @cmd: pointer to a filled command
162  */
163 static inline void mc_write_command(struct mc_command __iomem *portal,
164                                     struct mc_command *cmd)
165 {
166         struct mc_cmd_header *cmd_header = (struct mc_cmd_header *)&cmd->header;
167         char *header = (char *)&portal->header;
168         int i;
169
170         /* copy command parameters into the portal */
171         for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
172                 iowrite64(cmd->params[i], &portal->params[i]);
173
174         /* submit the command by writing the header */
175         iowrite32(le32_to_cpu(cmd_header->word[1]), (((uint32_t *)header) + 1));
176         iowrite32(le32_to_cpu(cmd_header->word[0]), (uint32_t *)header);
177 }
178
179 /**
180  * mc_read_response - reads the response for the last MC command from a
181  * Management Complex (MC) portal
182  *
183  * @portal: pointer to an MC portal
184  * @resp: pointer to command response buffer
185  *
186  * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
187  */
188 static inline enum mc_cmd_status mc_read_response(
189                                         struct mc_command __iomem *portal,
190                                         struct mc_command *resp)
191 {
192         int i;
193         enum mc_cmd_status status;
194
195         /* Copy command response header from MC portal: */
196         resp->header = ioread64(&portal->header);
197         status = mc_cmd_read_status(resp);
198         if (status != MC_CMD_STATUS_OK)
199                 return status;
200
201         /* Copy command response data from MC portal: */
202         for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
203                 resp->params[i] = ioread64(&portal->params[i]);
204
205         return status;
206 }
207
208 #endif /* __FSL_MC_CMD_H */