4 * Copyright (C) Cavium networks Ltd. 2016.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include "nicvf_plat.h"
40 #define NICVF_MBOX_PF_RESPONSE_DELAY_US (1000)
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",
67 static inline const char * __attribute__((unused))
68 nicvf_mbox_msg_str(int msg)
70 assert(msg >= 0 && msg < NIC_MBOX_MSG_MAX);
71 /* undefined messages */
72 if (mbox_message[msg] == NULL)
74 return mbox_message[msg];
78 nicvf_mbox_send_msg_to_pf_raw(struct nicvf *nic, struct nic_mbx *mbx)
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);
89 mbx_addr += sizeof(uint64_t);
91 nicvf_mbox_log("msg sent %s (VF%d)",
92 nicvf_mbox_msg_str(mbx->msg.msg), nic->vf_id);
96 nicvf_mbox_send_async_msg_to_pf(struct nicvf *nic, struct nic_mbx *mbx)
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);
104 nicvf_mbox_send_msg_to_pf(struct nicvf *nic, struct nic_mbx *mbx)
110 for (i = 0; i < retry; i++) {
111 nic->pf_acked = false;
112 nic->pf_nacked = false;
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() */
128 nicvf_delay_us(NICVF_MBOX_PF_RESPONSE_DELAY_US);
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);
140 nicvf_handle_mbx_intr(struct nicvf *nic)
143 uint64_t *mbx_data = (uint64_t *)&mbx;
144 uint64_t mbx_addr = NIC_VF_PF_MAILBOX_0_1;
147 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
148 *mbx_data = nicvf_reg_read(nic, mbx_addr);
150 mbx_addr += sizeof(uint64_t);
153 /* Overwrite the message so we won't receive it again */
154 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1, 0x0);
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);
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;
170 case NIC_MBOX_MSG_ACK:
171 nic->pf_acked = true;
173 case NIC_MBOX_MSG_NACK:
174 nic->pf_nacked = true;
176 case NIC_MBOX_MSG_RSS_SIZE:
177 nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
178 nic->pf_acked = true;
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;
186 case NIC_MBOX_MSG_ALLOC_SQS:
188 if (mbx.sqs_alloc.qs_count != nic->sqs_count) {
189 nicvf_log_error("Received %" PRIu8 "/%" PRIu8
191 mbx.sqs_alloc.qs_count,
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);
204 nic->pf_acked = true;
207 nicvf_log_error("Invalid message from PF, msg_id=0x%hhx %s",
208 mbx.msg.msg, nicvf_mbox_msg_str(mbx.msg.msg));
217 * Checks if VF is able to communicate with PF
218 * and also gets the VNIC number this VF is associated to.
221 nicvf_mbox_check_pf_ready(struct nicvf *nic)
223 struct nic_mbx mbx = { .msg = {.msg = NIC_MBOX_MSG_READY} };
225 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
229 nicvf_mbox_set_mac_addr(struct nicvf *nic,
230 const uint8_t mac[NICVF_MAC_ADDR_SIZE])
232 struct nic_mbx mbx = { .msg = {0} };
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];
240 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
244 nicvf_mbox_config_cpi(struct nicvf *nic, uint32_t qcnt)
246 struct nic_mbx mbx = { .msg = { 0 } };
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;
253 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
257 nicvf_mbox_get_rss_size(struct nicvf *nic)
259 struct nic_mbx mbx = { .msg = { 0 } };
261 mbx.msg.msg = NIC_MBOX_MSG_RSS_SIZE;
262 mbx.rss_size.vf_id = nic->vf_id;
264 /* Result will be stored in nic->rss_info.rss_size */
265 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
269 nicvf_mbox_config_rss(struct nicvf *nic)
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;
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;
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++];
293 if (nicvf_mbox_send_msg_to_pf(nic, &mbx))
294 return NICVF_ERR_RSS_TBL_UPDATE;
301 nicvf_mbox_rq_config(struct nicvf *nic, uint16_t qidx,
302 struct pf_rq_cfg *pf_rq_cfg)
304 struct nic_mbx mbx = { .msg = { 0 } };
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);
314 nicvf_mbox_sq_config(struct nicvf *nic, uint16_t qidx)
316 struct nic_mbx mbx = { .msg = { 0 } };
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);
327 nicvf_mbox_qset_config(struct nicvf *nic, struct pf_qs_cfg *qs_cfg)
329 struct nic_mbx mbx = { .msg = { 0 } };
331 #if NICVF_BYTE_ORDER == NICVF_BIG_ENDIAN
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);
343 nicvf_mbox_request_sqs(struct nicvf *nic)
345 struct nic_mbx mbx = { .msg = { 0 } };
349 assert(nic->sqs_count > 0);
350 assert(nic->sqs_count <= MAX_SQS_PER_VF);
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;
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;
360 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
364 nicvf_mbox_rq_drop_config(struct nicvf *nic, uint16_t qidx, bool enable)
366 struct nic_mbx mbx = { .msg = { 0 } };
367 struct pf_rq_drop_cfg *drop_cfg;
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;
376 drop_cfg->cq_red = 1;
377 drop_cfg->cq_drop = 2;
379 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
383 nicvf_mbox_update_hw_max_frs(struct nicvf *nic, uint16_t mtu)
385 struct nic_mbx mbx = { .msg = { 0 } };
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);
394 nicvf_mbox_rq_sync(struct nicvf *nic)
396 struct nic_mbx mbx = { .msg = { 0 } };
398 /* Make sure all packets in the pipeline are written back into mem */
399 mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
401 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
405 nicvf_mbox_rq_bp_config(struct nicvf *nic, uint16_t qidx, bool enable)
407 struct nic_mbx mbx = { .msg = { 0 } };
409 mbx.msg.msg = NIC_MBOX_MSG_RQ_BP_CFG;
410 mbx.rq.qs_num = nic->vf_id;
411 mbx.rq.rq_num = qidx;
414 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (nic->vf_id << 0);
415 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
419 nicvf_mbox_loopback_config(struct nicvf *nic, bool enable)
421 struct nic_mbx mbx = { .msg = { 0 } };
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);
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)
434 struct nic_mbx mbx = { .msg = { 0 } };
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);
445 nicvf_mbox_shutdown(struct nicvf *nic)
447 struct nic_mbx mbx = { .msg = { 0 } };
449 mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
450 nicvf_mbox_send_msg_to_pf(nic, &mbx);
454 nicvf_mbox_cfg_done(struct nicvf *nic)
456 struct nic_mbx mbx = { .msg = { 0 } };
458 mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
459 nicvf_mbox_send_async_msg_to_pf(nic, &mbx);