e26319bbeb2ca8ec20fa77d92228a165d5b5a046
[deb_dpdk.git] / drivers / net / thunderx / base / nicvf_mbox.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 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, 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 <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         case NIC_MBOX_MSG_ALLOC_SQS:
187                 assert_primary(nic);
188                 if (mbx.sqs_alloc.qs_count != nic->sqs_count) {
189                         nicvf_log_error("Received %" PRIu8 "/%" PRIu8
190                                         " secondary qsets",
191                                         mbx.sqs_alloc.qs_count,
192                                         nic->sqs_count);
193                         abort();
194                 }
195                 for (i = 0; i < mbx.sqs_alloc.qs_count; i++) {
196                         if (mbx.sqs_alloc.svf[i] != nic->snicvf[i]->vf_id) {
197                                 nicvf_log_error("Received secondary qset[%zu] "
198                                                 "ID %" PRIu8 " expected %"
199                                                 PRIu8, i, mbx.sqs_alloc.svf[i],
200                                                 nic->snicvf[i]->vf_id);
201                                 abort();
202                         }
203                 }
204                 nic->pf_acked = true;
205                 break;
206         default:
207                 nicvf_log_error("Invalid message from PF, msg_id=0x%hhx %s",
208                                 mbx.msg.msg, nicvf_mbox_msg_str(mbx.msg.msg));
209                 break;
210         }
211         nicvf_smp_wmb();
212
213         return mbx.msg.msg;
214 }
215
216 /*
217  * Checks if VF is able to communicate with PF
218  * and also gets the VNIC number this VF is associated to.
219  */
220 int
221 nicvf_mbox_check_pf_ready(struct nicvf *nic)
222 {
223         struct nic_mbx mbx = { .msg = {.msg = NIC_MBOX_MSG_READY} };
224
225         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
226 }
227
228 int
229 nicvf_mbox_set_mac_addr(struct nicvf *nic,
230                         const uint8_t mac[NICVF_MAC_ADDR_SIZE])
231 {
232         struct nic_mbx mbx = { .msg = {0} };
233         int i;
234
235         mbx.msg.msg = NIC_MBOX_MSG_SET_MAC;
236         mbx.mac.vf_id = nic->vf_id;
237         for (i = 0; i < 6; i++)
238                 mbx.mac.mac_addr[i] = mac[i];
239
240         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
241 }
242
243 int
244 nicvf_mbox_config_cpi(struct nicvf *nic, uint32_t qcnt)
245 {
246         struct nic_mbx mbx = { .msg = { 0 } };
247
248         mbx.msg.msg = NIC_MBOX_MSG_CPI_CFG;
249         mbx.cpi_cfg.vf_id = nic->vf_id;
250         mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
251         mbx.cpi_cfg.rq_cnt = qcnt;
252
253         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
254 }
255
256 int
257 nicvf_mbox_get_rss_size(struct nicvf *nic)
258 {
259         struct nic_mbx mbx = { .msg = { 0 } };
260
261         mbx.msg.msg = NIC_MBOX_MSG_RSS_SIZE;
262         mbx.rss_size.vf_id = nic->vf_id;
263
264         /* Result will be stored in nic->rss_info.rss_size */
265         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
266 }
267
268 int
269 nicvf_mbox_config_rss(struct nicvf *nic)
270 {
271         struct nic_mbx mbx = { .msg = { 0 } };
272         struct nicvf_rss_reta_info *rss = &nic->rss_info;
273         size_t tot_len = rss->rss_size;
274         size_t cur_len;
275         size_t cur_idx = 0;
276         size_t i;
277
278         mbx.rss_cfg.vf_id = nic->vf_id;
279         mbx.rss_cfg.hash_bits = rss->hash_bits;
280         mbx.rss_cfg.tbl_len = 0;
281         mbx.rss_cfg.tbl_offset = 0;
282
283         while (cur_idx < tot_len) {
284                 cur_len = nicvf_min(tot_len - cur_idx,
285                                 (size_t)RSS_IND_TBL_LEN_PER_MBX_MSG);
286                 mbx.msg.msg = (cur_idx > 0) ?
287                         NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
288                 mbx.rss_cfg.tbl_offset = cur_idx;
289                 mbx.rss_cfg.tbl_len = cur_len;
290                 for (i = 0; i < cur_len; i++)
291                         mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[cur_idx++];
292
293                 if (nicvf_mbox_send_msg_to_pf(nic, &mbx))
294                         return NICVF_ERR_RSS_TBL_UPDATE;
295         }
296
297         return 0;
298 }
299
300 int
301 nicvf_mbox_rq_config(struct nicvf *nic, uint16_t qidx,
302                      struct pf_rq_cfg *pf_rq_cfg)
303 {
304         struct nic_mbx mbx = { .msg = { 0 } };
305
306         mbx.msg.msg = NIC_MBOX_MSG_RQ_CFG;
307         mbx.rq.qs_num = nic->vf_id;
308         mbx.rq.rq_num = qidx;
309         mbx.rq.cfg = pf_rq_cfg->value;
310         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
311 }
312
313 int
314 nicvf_mbox_sq_config(struct nicvf *nic, uint16_t qidx)
315 {
316         struct nic_mbx mbx = { .msg = { 0 } };
317
318         mbx.msg.msg = NIC_MBOX_MSG_SQ_CFG;
319         mbx.sq.qs_num = nic->vf_id;
320         mbx.sq.sq_num = qidx;
321         mbx.sq.sqs_mode = nic->sqs_mode;
322         mbx.sq.cfg = (nic->vf_id << 3) | qidx;
323         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
324 }
325
326 int
327 nicvf_mbox_qset_config(struct nicvf *nic, struct pf_qs_cfg *qs_cfg)
328 {
329         struct nic_mbx mbx = { .msg = { 0 } };
330
331 #if NICVF_BYTE_ORDER == NICVF_BIG_ENDIAN
332         qs_cfg->be = 1;
333 #endif
334         /* Send a mailbox msg to PF to config Qset */
335         mbx.msg.msg = NIC_MBOX_MSG_QS_CFG;
336         mbx.qs.num = nic->vf_id;
337         mbx.qs.sqs_count = nic->sqs_count;
338         mbx.qs.cfg = qs_cfg->value;
339         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
340 }
341
342 int
343 nicvf_mbox_request_sqs(struct nicvf *nic)
344 {
345         struct nic_mbx mbx = { .msg = { 0 } };
346         size_t i;
347
348         assert_primary(nic);
349         assert(nic->sqs_count > 0);
350         assert(nic->sqs_count <= MAX_SQS_PER_VF);
351
352         mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
353         mbx.sqs_alloc.spec = 1;
354         mbx.sqs_alloc.qs_count = nic->sqs_count;
355
356         /* Set no of Rx/Tx queues in each of the SQsets */
357         for (i = 0; i < nic->sqs_count; i++)
358                 mbx.sqs_alloc.svf[i] = nic->snicvf[i]->vf_id;
359
360         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
361 }
362
363 int
364 nicvf_mbox_rq_drop_config(struct nicvf *nic, uint16_t qidx, bool enable)
365 {
366         struct nic_mbx mbx = { .msg = { 0 } };
367         struct pf_rq_drop_cfg *drop_cfg;
368
369         /* Enable CQ drop to reserve sufficient CQEs for all tx packets */
370         mbx.msg.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
371         mbx.rq.qs_num = nic->vf_id;
372         mbx.rq.rq_num = qidx;
373         drop_cfg = (struct pf_rq_drop_cfg *)&mbx.rq.cfg;
374         drop_cfg->value = 0;
375         if (enable) {
376                 drop_cfg->cq_red = 1;
377                 drop_cfg->cq_drop = 2;
378         }
379         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
380 }
381
382 int
383 nicvf_mbox_update_hw_max_frs(struct nicvf *nic, uint16_t mtu)
384 {
385         struct nic_mbx mbx = { .msg = { 0 } };
386
387         mbx.msg.msg = NIC_MBOX_MSG_SET_MAX_FRS;
388         mbx.frs.max_frs = mtu;
389         mbx.frs.vf_id = nic->vf_id;
390         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
391 }
392
393 int
394 nicvf_mbox_rq_sync(struct nicvf *nic)
395 {
396         struct nic_mbx mbx = { .msg = { 0 } };
397
398         /* Make sure all packets in the pipeline are written back into mem */
399         mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
400         mbx.rq.cfg = 0;
401         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
402 }
403
404 int
405 nicvf_mbox_rq_bp_config(struct nicvf *nic, uint16_t qidx, bool enable)
406 {
407         struct nic_mbx mbx = { .msg = { 0 } };
408
409         mbx.msg.msg = NIC_MBOX_MSG_RQ_BP_CFG;
410         mbx.rq.qs_num = nic->vf_id;
411         mbx.rq.rq_num = qidx;
412         mbx.rq.cfg = 0;
413         if (enable)
414                 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (nic->vf_id << 0);
415         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
416 }
417
418 int
419 nicvf_mbox_loopback_config(struct nicvf *nic, bool enable)
420 {
421         struct nic_mbx mbx = { .msg = { 0 } };
422
423         mbx.lbk.msg = NIC_MBOX_MSG_LOOPBACK;
424         mbx.lbk.vf_id = nic->vf_id;
425         mbx.lbk.enable = enable;
426         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
427 }
428
429 int
430 nicvf_mbox_reset_stat_counters(struct nicvf *nic, uint16_t rx_stat_mask,
431                                uint8_t tx_stat_mask, uint16_t rq_stat_mask,
432                                uint16_t sq_stat_mask)
433 {
434         struct nic_mbx mbx = { .msg = { 0 } };
435
436         mbx.reset_stat.msg = NIC_MBOX_MSG_RESET_STAT_COUNTER;
437         mbx.reset_stat.rx_stat_mask = rx_stat_mask;
438         mbx.reset_stat.tx_stat_mask = tx_stat_mask;
439         mbx.reset_stat.rq_stat_mask = rq_stat_mask;
440         mbx.reset_stat.sq_stat_mask = sq_stat_mask;
441         return nicvf_mbox_send_msg_to_pf(nic, &mbx);
442 }
443
444 void
445 nicvf_mbox_shutdown(struct nicvf *nic)
446 {
447         struct nic_mbx mbx = { .msg = { 0 } };
448
449         mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
450         nicvf_mbox_send_msg_to_pf(nic, &mbx);
451 }
452
453 void
454 nicvf_mbox_cfg_done(struct nicvf *nic)
455 {
456         struct nic_mbx mbx = { .msg = { 0 } };
457
458         mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
459         nicvf_mbox_send_async_msg_to_pf(nic, &mbx);
460 }