New upstream version 17.08
[deb_dpdk.git] / drivers / event / octeontx / ssovf_mbox.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium, Inc nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <string.h>
34
35 #include <rte_atomic.h>
36 #include <rte_common.h>
37 #include <rte_cycles.h>
38 #include <rte_io.h>
39 #include <rte_spinlock.h>
40
41 #include "ssovf_evdev.h"
42
43 /* Mbox operation timeout in seconds */
44 #define MBOX_WAIT_TIME_SEC      3
45 #define MAX_RAM_MBOX_LEN        ((SSOW_BAR4_LEN >> 1) - 8 /* Mbox header */)
46
47 /* Mbox channel state */
48 enum {
49         MBOX_CHAN_STATE_REQ = 1,
50         MBOX_CHAN_STATE_RES = 0,
51 };
52
53 /* Response messages */
54 enum {
55         MBOX_RET_SUCCESS,
56         MBOX_RET_INVALID,
57         MBOX_RET_INTERNAL_ERR,
58 };
59
60 struct mbox {
61         int init_once;
62         uint8_t *ram_mbox_base; /* Base address of mbox message stored in ram */
63         uint8_t *reg; /* Store to this register triggers PF mbox interrupt */
64         uint16_t tag_own; /* Last tag which was written to own channel */
65         rte_spinlock_t lock;
66 };
67
68 static struct mbox octeontx_mbox;
69
70 /*
71  * Structure used for mbox synchronization
72  * This structure sits at the begin of Mbox RAM and used as main
73  * synchronization point for channel communication
74  */
75 struct mbox_ram_hdr {
76         union {
77                 uint64_t u64;
78                 struct {
79                         uint8_t chan_state : 1;
80                         uint8_t coproc : 7;
81                         uint8_t msg;
82                         uint8_t vfid;
83                         uint8_t res_code;
84                         uint16_t tag;
85                         uint16_t len;
86                 };
87         };
88 };
89
90 static inline void
91 mbox_send_request(struct mbox *m, struct octeontx_mbox_hdr *hdr,
92                         const void *txmsg, uint16_t txsize)
93 {
94         struct mbox_ram_hdr old_hdr;
95         struct mbox_ram_hdr new_hdr = { {0} };
96         uint64_t *ram_mbox_hdr = (uint64_t *)m->ram_mbox_base;
97         uint8_t *ram_mbox_msg = m->ram_mbox_base + sizeof(struct mbox_ram_hdr);
98
99         /*
100          * Initialize the channel with the tag left by last send.
101          * On success full mbox send complete, PF increments the tag by one.
102          * The sender can validate integrity of PF message with this scheme
103          */
104         old_hdr.u64 = rte_read64(ram_mbox_hdr);
105         m->tag_own = (old_hdr.tag + 2) & (~0x1ul); /* next even number */
106
107         /* Copy msg body */
108         if (txmsg)
109                 memcpy(ram_mbox_msg, txmsg, txsize);
110
111         /* Prepare new hdr */
112         new_hdr.chan_state = MBOX_CHAN_STATE_REQ;
113         new_hdr.coproc = hdr->coproc;
114         new_hdr.msg = hdr->msg;
115         new_hdr.vfid = hdr->vfid;
116         new_hdr.tag = m->tag_own;
117         new_hdr.len = txsize;
118
119         /* Write the msg header */
120         rte_write64(new_hdr.u64, ram_mbox_hdr);
121         rte_io_wmb();
122         /* Notify PF about the new msg - write to MBOX reg generates PF IRQ */
123         rte_write64(0, m->reg);
124 }
125
126 static inline int
127 mbox_wait_response(struct mbox *m, struct octeontx_mbox_hdr *hdr,
128                         void *rxmsg, uint16_t rxsize)
129 {
130         int res = 0, wait;
131         uint16_t len;
132         struct mbox_ram_hdr rx_hdr;
133         uint64_t *ram_mbox_hdr = (uint64_t *)m->ram_mbox_base;
134         uint8_t *ram_mbox_msg = m->ram_mbox_base + sizeof(struct mbox_ram_hdr);
135
136         /* Wait for response */
137         wait = MBOX_WAIT_TIME_SEC * 1000 * 10;
138         while (wait > 0) {
139                 rte_delay_us(100);
140                 rx_hdr.u64 = rte_read64(ram_mbox_hdr);
141                 if (rx_hdr.chan_state == MBOX_CHAN_STATE_RES)
142                         break;
143                 --wait;
144         }
145
146         hdr->res_code = rx_hdr.res_code;
147         m->tag_own++;
148
149         /* Timeout */
150         if (wait <= 0) {
151                 res = -ETIMEDOUT;
152                 goto error;
153         }
154
155         /* Tag mismatch */
156         if (m->tag_own != rx_hdr.tag) {
157                 res = -EINVAL;
158                 goto error;
159         }
160
161         /* PF nacked the msg */
162         if (rx_hdr.res_code != MBOX_RET_SUCCESS) {
163                 res = -EBADMSG;
164                 goto error;
165         }
166
167         len = RTE_MIN(rx_hdr.len, rxsize);
168         if (rxmsg)
169                 memcpy(rxmsg, ram_mbox_msg, len);
170
171         return len;
172
173 error:
174         ssovf_log_err("Failed to send mbox(%d/%d) coproc=%d msg=%d ret=(%d,%d)",
175                         m->tag_own, rx_hdr.tag, hdr->coproc, hdr->msg, res,
176                         hdr->res_code);
177         return res;
178 }
179
180 static inline int
181 mbox_send(struct mbox *m, struct octeontx_mbox_hdr *hdr, const void *txmsg,
182                 uint16_t txsize, void *rxmsg, uint16_t rxsize)
183 {
184         int res = -EINVAL;
185
186         if (m->init_once == 0 || hdr == NULL ||
187                 txsize > MAX_RAM_MBOX_LEN || rxsize > MAX_RAM_MBOX_LEN) {
188                 ssovf_log_err("Invalid init_once=%d hdr=%p txsz=%d rxsz=%d",
189                                 m->init_once, hdr, txsize, rxsize);
190                 return res;
191         }
192
193         rte_spinlock_lock(&m->lock);
194
195         mbox_send_request(m, hdr, txmsg, txsize);
196         res = mbox_wait_response(m, hdr, rxmsg, rxsize);
197
198         rte_spinlock_unlock(&m->lock);
199         return res;
200 }
201
202 static inline int
203 mbox_setup(struct mbox *m)
204 {
205         if (unlikely(m->init_once == 0)) {
206                 rte_spinlock_init(&m->lock);
207                 m->ram_mbox_base = octeontx_ssovf_bar(OCTEONTX_SSO_HWS, 0, 4);
208                 m->reg = octeontx_ssovf_bar(OCTEONTX_SSO_GROUP, 0, 0);
209                 m->reg += SSO_VHGRP_PF_MBOX(1);
210
211                 if (m->ram_mbox_base == NULL || m->reg == NULL) {
212                         ssovf_log_err("Invalid ram_mbox_base=%p or reg=%p",
213                                 m->ram_mbox_base, m->reg);
214                         return -EINVAL;
215                 }
216                 m->init_once = 1;
217         }
218         return 0;
219 }
220
221 int
222 octeontx_ssovf_mbox_send(struct octeontx_mbox_hdr *hdr, void *txdata,
223                                  uint16_t txlen, void *rxdata, uint16_t rxlen)
224 {
225         struct mbox *m = &octeontx_mbox;
226
227         RTE_BUILD_BUG_ON(sizeof(struct mbox_ram_hdr) != 8);
228         if (rte_eal_process_type() != RTE_PROC_PRIMARY || mbox_setup(m))
229                 return -EINVAL;
230
231         return mbox_send(m, hdr, txdata, txlen, rxdata, rxlen);
232 }