Imported Upstream version 16.07-rc1
[deb_dpdk.git] / drivers / net / thunderx / base / nicvf_mbox.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium networks Ltd. 2016.
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 networks 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 <assert.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "nicvf_plat.h"
39
40 #define NICVF_MBOX_PF_RESPONSE_DELAY_US   (1000)
41
42 static const char *mbox_message[NIC_MBOX_MSG_MAX] =  {
43         [NIC_MBOX_MSG_INVALID]            = "NIC_MBOX_MSG_INVALID",
44         [NIC_MBOX_MSG_READY]              = "NIC_MBOX_MSG_READY",
45         [NIC_MBOX_MSG_ACK]                = "NIC_MBOX_MSG_ACK",
46         [NIC_MBOX_MSG_NACK]               = "NIC_MBOX_MSG_ACK",
47         [NIC_MBOX_MSG_QS_CFG]             = "NIC_MBOX_MSG_QS_CFG",
48         [NIC_MBOX_MSG_RQ_CFG]             = "NIC_MBOX_MSG_RQ_CFG",
49         [NIC_MBOX_MSG_SQ_CFG]             = "NIC_MBOX_MSG_SQ_CFG",
50         [NIC_MBOX_MSG_RQ_DROP_CFG]        = "NIC_MBOX_MSG_RQ_DROP_CFG",
51         [NIC_MBOX_MSG_SET_MAC]            = "NIC_MBOX_MSG_SET_MAC",
52         [NIC_MBOX_MSG_SET_MAX_FRS]        = "NIC_MBOX_MSG_SET_MAX_FRS",
53         [NIC_MBOX_MSG_CPI_CFG]            = "NIC_MBOX_MSG_CPI_CFG",
54         [NIC_MBOX_MSG_RSS_SIZE]           = "NIC_MBOX_MSG_RSS_SIZE",
55         [NIC_MBOX_MSG_RSS_CFG]            = "NIC_MBOX_MSG_RSS_CFG",
56         [NIC_MBOX_MSG_RSS_CFG_CONT]       = "NIC_MBOX_MSG_RSS_CFG_CONT",
57         [NIC_MBOX_MSG_RQ_BP_CFG]          = "NIC_MBOX_MSG_RQ_BP_CFG",
58         [NIC_MBOX_MSG_RQ_SW_SYNC]         = "NIC_MBOX_MSG_RQ_SW_SYNC",
59         [NIC_MBOX_MSG_BGX_LINK_CHANGE]    = "NIC_MBOX_MSG_BGX_LINK_CHANGE",
60         [NIC_MBOX_MSG_ALLOC_SQS]          = "NIC_MBOX_MSG_ALLOC_SQS",
61         [NIC_MBOX_MSG_LOOPBACK]           = "NIC_MBOX_MSG_LOOPBACK",
62         [NIC_MBOX_MSG_RESET_STAT_COUNTER] = "NIC_MBOX_MSG_RESET_STAT_COUNTER",
63         [NIC_MBOX_MSG_CFG_DONE]           = "NIC_MBOX_MSG_CFG_DONE",
64         [NIC_MBOX_MSG_SHUTDOWN]           = "NIC_MBOX_MSG_SHUTDOWN",
65 };
66
67 static inline const char * __attribute__((unused))
68 nicvf_mbox_msg_str(int msg)
69 {
70         assert(msg >= 0 && msg < NIC_MBOX_MSG_MAX);
71         /* undefined messages */
72         if (mbox_message[msg] == NULL)
73                 msg = 0;
74         return mbox_message[msg];
75 }
76
77 static inline void
78 nicvf_mbox_send_msg_to_pf_raw(struct nicvf *nic, struct nic_mbx *mbx)
79 {
80         uint64_t *mbx_data;
81         uint64_t mbx_addr;
82         int i;
83
84         mbx_addr = NIC_VF_PF_MAILBOX_0_1;
85         mbx_data = (uint64_t *)mbx;
86         for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
87                 nicvf_reg_write(nic, mbx_addr, *mbx_data);
88                 mbx_data++;
89                 mbx_addr += sizeof(uint64_t);
90         }
91         nicvf_mbox_log("msg sent %s (VF%d)",
92                         nicvf_mbox_msg_str(mbx->msg.msg), nic->vf_id);
93 }
94
95 static inline void
96 nicvf_mbox_send_async_msg_to_pf(struct nicvf *nic, struct nic_mbx *mbx)
97 {
98         nicvf_mbox_send_msg_to_pf_raw(nic, mbx);
99         /* Messages without ack are racy!*/
100         nicvf_delay_us(NICVF_MBOX_PF_RESPONSE_DELAY_US);
101 }
102
103 static inline int
104 nicvf_mbox_send_msg_to_pf(struct nicvf *nic, struct nic_mbx *mbx)
105 {
106         long timeout;
107         long sleep = 10;
108         int i, retry = 5;
109
110         for (i = 0; i < retry; i++) {
111                 nic->pf_acked = false;
112                 nic->pf_nacked = false;
113                 nicvf_smp_wmb();
114
115                 nicvf_mbox_send_msg_to_pf_raw(nic, mbx);
116                 /* Give some time to get PF response */
117                 nicvf_delay_us(NICVF_MBOX_PF_RESPONSE_DELAY_US);
118                 timeout = NIC_MBOX_MSG_TIMEOUT;
119                 while (timeout > 0) {
120                         /* Periodic poll happens from nicvf_interrupt() */
121                         nicvf_smp_rmb();
122
123                         if (nic->pf_nacked)
124                                 return -EINVAL;
125                         if (nic->pf_acked)
126                                 return 0;
127
128                         nicvf_delay_us(NICVF_MBOX_PF_RESPONSE_DELAY_US);
129                         timeout -= sleep;
130                 }
131                 nicvf_log_error("PF didn't ack to msg 0x%02x %s VF%d (%d/%d)",
132                                 mbx->msg.msg, nicvf_mbox_msg_str(mbx->msg.msg),
133                                 nic->vf_id, i, retry);
134         }
135         return -EBUSY;
136 }
137
138
139 int
140 nicvf_handle_mbx_intr(struct nicvf *nic)
141 {
142         struct nic_mbx mbx;
143         uint64_t *mbx_data = (uint64_t *)&mbx;
144         uint64_t mbx_addr = NIC_VF_PF_MAILBOX_0_1;
145         size_t i;
146
147         for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
148                 *mbx_data = nicvf_reg_read(nic, mbx_addr);
149                 mbx_data++;
150                 mbx_addr += sizeof(uint64_t);
151         }
152
153         /* Overwrite the message so we won't receive it again */
154         nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1, 0x0);
155
156         nicvf_mbox_log("msg received id=0x%hhx %s (VF%d)", mbx.msg.msg,
157                         nicvf_mbox_msg_str(mbx.msg.msg), nic->vf_id);
158
159         switch (mbx.msg.msg) {
160         case NIC_MBOX_MSG_READY:
161                 nic->vf_id = mbx.nic_cfg.vf_id & 0x7F;
162                 nic->tns_mode = mbx.nic_cfg.tns_mode & 0x7F;
163                 nic->node = mbx.nic_cfg.node_id;
164                 nic->sqs_mode = mbx.nic_cfg.sqs_mode;
165                 nic->loopback_supported = mbx.nic_cfg.loopback_supported;
166                 ether_addr_copy((struct ether_addr *)mbx.nic_cfg.mac_addr,
167                                 (struct ether_addr *)nic->mac_addr);
168                 nic->pf_acked = true;
169                 break;
170         case NIC_MBOX_MSG_ACK:
171                 nic->pf_acked = true;
172                 break;
173         case NIC_MBOX_MSG_NACK:
174                 nic->pf_nacked = true;
175                 break;
176         case NIC_MBOX_MSG_RSS_SIZE:
177                 nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
178                 nic->pf_acked = true;
179                 break;
180         case NIC_MBOX_MSG_BGX_LINK_CHANGE:
181                 nic->link_up = mbx.link_status.link_up;
182                 nic->duplex = mbx.link_status.duplex;
183                 nic->speed = mbx.link_status.speed;
184                 nic->pf_acked = true;
185                 break;
186         default:
187                 nicvf_log_error("Invalid message from PF, msg_id=0x%hhx %s",
188                                 mbx.msg.msg, nicvf_mbox_msg_str(mbx.msg.msg));
189                 break;
190         }
191         nicvf_smp_wmb();
192
193         return mbx.msg.msg;
194 }
195
196 /*
197  * Checks if VF is able to communicate with PF
198  * and also gets the VNIC number this VF is associated to.
199  */
200 int
201 nicvf_mbox_check_pf_ready(struct nicvf *nic)
202 {
203         struct nic_mbx mbx = { .msg = {.msg = NIC_MBOX_MSG_READY} };
204
205         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
206 }
207
208 int
209 nicvf_mbox_set_mac_addr(struct nicvf *nic,
210                         const uint8_t mac[NICVF_MAC_ADDR_SIZE])
211 {
212         struct nic_mbx mbx = { .msg = {0} };
213         int i;
214
215         mbx.msg.msg = NIC_MBOX_MSG_SET_MAC;
216         mbx.mac.vf_id = nic->vf_id;
217         for (i = 0; i < 6; i++)
218                 mbx.mac.mac_addr[i] = mac[i];
219
220         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
221 }
222
223 int
224 nicvf_mbox_config_cpi(struct nicvf *nic, uint32_t qcnt)
225 {
226         struct nic_mbx mbx = { .msg = { 0 } };
227
228         mbx.msg.msg = NIC_MBOX_MSG_CPI_CFG;
229         mbx.cpi_cfg.vf_id = nic->vf_id;
230         mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
231         mbx.cpi_cfg.rq_cnt = qcnt;
232
233         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
234 }
235
236 int
237 nicvf_mbox_get_rss_size(struct nicvf *nic)
238 {
239         struct nic_mbx mbx = { .msg = { 0 } };
240
241         mbx.msg.msg = NIC_MBOX_MSG_RSS_SIZE;
242         mbx.rss_size.vf_id = nic->vf_id;
243
244         /* Result will be stored in nic->rss_info.rss_size */
245         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
246 }
247
248 int
249 nicvf_mbox_config_rss(struct nicvf *nic)
250 {
251         struct nic_mbx mbx = { .msg = { 0 } };
252         struct nicvf_rss_reta_info *rss = &nic->rss_info;
253         size_t tot_len = rss->rss_size;
254         size_t cur_len;
255         size_t cur_idx = 0;
256         size_t i;
257
258         mbx.rss_cfg.vf_id = nic->vf_id;
259         mbx.rss_cfg.hash_bits = rss->hash_bits;
260         mbx.rss_cfg.tbl_len = 0;
261         mbx.rss_cfg.tbl_offset = 0;
262
263         while (cur_idx < tot_len) {
264                 cur_len = nicvf_min(tot_len - cur_idx,
265                                 (size_t)RSS_IND_TBL_LEN_PER_MBX_MSG);
266                 mbx.msg.msg = (cur_idx > 0) ?
267                         NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
268                 mbx.rss_cfg.tbl_offset = cur_idx;
269                 mbx.rss_cfg.tbl_len = cur_len;
270                 for (i = 0; i < cur_len; i++)
271                         mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[cur_idx++];
272
273                 if (nicvf_mbox_send_msg_to_pf(nic, &mbx))
274                         return NICVF_ERR_RSS_TBL_UPDATE;
275         }
276
277         return 0;
278 }
279
280 int
281 nicvf_mbox_rq_config(struct nicvf *nic, uint16_t qidx,
282                      struct pf_rq_cfg *pf_rq_cfg)
283 {
284         struct nic_mbx mbx = { .msg = { 0 } };
285
286         mbx.msg.msg = NIC_MBOX_MSG_RQ_CFG;
287         mbx.rq.qs_num = nic->vf_id;
288         mbx.rq.rq_num = qidx;
289         mbx.rq.cfg = pf_rq_cfg->value;
290         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
291 }
292
293 int
294 nicvf_mbox_sq_config(struct nicvf *nic, uint16_t qidx)
295 {
296         struct nic_mbx mbx = { .msg = { 0 } };
297
298         mbx.msg.msg = NIC_MBOX_MSG_SQ_CFG;
299         mbx.sq.qs_num = nic->vf_id;
300         mbx.sq.sq_num = qidx;
301         mbx.sq.sqs_mode = nic->sqs_mode;
302         mbx.sq.cfg = (nic->vf_id << 3) | qidx;
303         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
304 }
305
306 int
307 nicvf_mbox_qset_config(struct nicvf *nic, struct pf_qs_cfg *qs_cfg)
308 {
309         struct nic_mbx mbx = { .msg = { 0 } };
310
311 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
312         qs_cfg->be = 1;
313 #endif
314         /* Send a mailbox msg to PF to config Qset */
315         mbx.msg.msg = NIC_MBOX_MSG_QS_CFG;
316         mbx.qs.num = nic->vf_id;
317         mbx.qs.cfg = qs_cfg->value;
318         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
319 }
320
321 int
322 nicvf_mbox_rq_drop_config(struct nicvf *nic, uint16_t qidx, bool enable)
323 {
324         struct nic_mbx mbx = { .msg = { 0 } };
325         struct pf_rq_drop_cfg *drop_cfg;
326
327         /* Enable CQ drop to reserve sufficient CQEs for all tx packets */
328         mbx.msg.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
329         mbx.rq.qs_num = nic->vf_id;
330         mbx.rq.rq_num = qidx;
331         drop_cfg = (struct pf_rq_drop_cfg *)&mbx.rq.cfg;
332         drop_cfg->value = 0;
333         if (enable) {
334                 drop_cfg->cq_red = 1;
335                 drop_cfg->cq_drop = 2;
336         }
337         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
338 }
339
340 int
341 nicvf_mbox_update_hw_max_frs(struct nicvf *nic, uint16_t mtu)
342 {
343         struct nic_mbx mbx = { .msg = { 0 } };
344
345         mbx.msg.msg = NIC_MBOX_MSG_SET_MAX_FRS;
346         mbx.frs.max_frs = mtu;
347         mbx.frs.vf_id = nic->vf_id;
348         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
349 }
350
351 int
352 nicvf_mbox_rq_sync(struct nicvf *nic)
353 {
354         struct nic_mbx mbx = { .msg = { 0 } };
355
356         /* Make sure all packets in the pipeline are written back into mem */
357         mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
358         mbx.rq.cfg = 0;
359         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
360 }
361
362 int
363 nicvf_mbox_rq_bp_config(struct nicvf *nic, uint16_t qidx, bool enable)
364 {
365         struct nic_mbx mbx = { .msg = { 0 } };
366
367         mbx.msg.msg = NIC_MBOX_MSG_RQ_BP_CFG;
368         mbx.rq.qs_num = nic->vf_id;
369         mbx.rq.rq_num = qidx;
370         mbx.rq.cfg = 0;
371         if (enable)
372                 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (nic->vf_id << 0);
373         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
374 }
375
376 int
377 nicvf_mbox_loopback_config(struct nicvf *nic, bool enable)
378 {
379         struct nic_mbx mbx = { .msg = { 0 } };
380
381         mbx.lbk.msg = NIC_MBOX_MSG_LOOPBACK;
382         mbx.lbk.vf_id = nic->vf_id;
383         mbx.lbk.enable = enable;
384         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
385 }
386
387 int
388 nicvf_mbox_reset_stat_counters(struct nicvf *nic, uint16_t rx_stat_mask,
389                                uint8_t tx_stat_mask, uint16_t rq_stat_mask,
390                                uint16_t sq_stat_mask)
391 {
392         struct nic_mbx mbx = { .msg = { 0 } };
393
394         mbx.reset_stat.msg = NIC_MBOX_MSG_RESET_STAT_COUNTER;
395         mbx.reset_stat.rx_stat_mask = rx_stat_mask;
396         mbx.reset_stat.tx_stat_mask = tx_stat_mask;
397         mbx.reset_stat.rq_stat_mask = rq_stat_mask;
398         mbx.reset_stat.sq_stat_mask = sq_stat_mask;
399         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
400 }
401
402 void
403 nicvf_mbox_shutdown(struct nicvf *nic)
404 {
405         struct nic_mbx mbx = { .msg = { 0 } };
406
407         mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
408         nicvf_mbox_send_msg_to_pf(nic, &mbx);
409 }
410
411 void
412 nicvf_mbox_cfg_done(struct nicvf *nic)
413 {
414         struct nic_mbx mbx = { .msg = { 0 } };
415
416         mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
417         nicvf_mbox_send_async_msg_to_pf(nic, &mbx);
418 }