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;
187 nicvf_log_error("Invalid message from PF, msg_id=0x%hhx %s",
188 mbx.msg.msg, nicvf_mbox_msg_str(mbx.msg.msg));
197 * Checks if VF is able to communicate with PF
198 * and also gets the VNIC number this VF is associated to.
201 nicvf_mbox_check_pf_ready(struct nicvf *nic)
203 struct nic_mbx mbx = { .msg = {.msg = NIC_MBOX_MSG_READY} };
205 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
209 nicvf_mbox_set_mac_addr(struct nicvf *nic,
210 const uint8_t mac[NICVF_MAC_ADDR_SIZE])
212 struct nic_mbx mbx = { .msg = {0} };
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];
220 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
224 nicvf_mbox_config_cpi(struct nicvf *nic, uint32_t qcnt)
226 struct nic_mbx mbx = { .msg = { 0 } };
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;
233 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
237 nicvf_mbox_get_rss_size(struct nicvf *nic)
239 struct nic_mbx mbx = { .msg = { 0 } };
241 mbx.msg.msg = NIC_MBOX_MSG_RSS_SIZE;
242 mbx.rss_size.vf_id = nic->vf_id;
244 /* Result will be stored in nic->rss_info.rss_size */
245 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
249 nicvf_mbox_config_rss(struct nicvf *nic)
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;
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;
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++];
273 if (nicvf_mbox_send_msg_to_pf(nic, &mbx))
274 return NICVF_ERR_RSS_TBL_UPDATE;
281 nicvf_mbox_rq_config(struct nicvf *nic, uint16_t qidx,
282 struct pf_rq_cfg *pf_rq_cfg)
284 struct nic_mbx mbx = { .msg = { 0 } };
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);
294 nicvf_mbox_sq_config(struct nicvf *nic, uint16_t qidx)
296 struct nic_mbx mbx = { .msg = { 0 } };
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);
307 nicvf_mbox_qset_config(struct nicvf *nic, struct pf_qs_cfg *qs_cfg)
309 struct nic_mbx mbx = { .msg = { 0 } };
311 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
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);
322 nicvf_mbox_rq_drop_config(struct nicvf *nic, uint16_t qidx, bool enable)
324 struct nic_mbx mbx = { .msg = { 0 } };
325 struct pf_rq_drop_cfg *drop_cfg;
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;
334 drop_cfg->cq_red = 1;
335 drop_cfg->cq_drop = 2;
337 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
341 nicvf_mbox_update_hw_max_frs(struct nicvf *nic, uint16_t mtu)
343 struct nic_mbx mbx = { .msg = { 0 } };
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);
352 nicvf_mbox_rq_sync(struct nicvf *nic)
354 struct nic_mbx mbx = { .msg = { 0 } };
356 /* Make sure all packets in the pipeline are written back into mem */
357 mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
359 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
363 nicvf_mbox_rq_bp_config(struct nicvf *nic, uint16_t qidx, bool enable)
365 struct nic_mbx mbx = { .msg = { 0 } };
367 mbx.msg.msg = NIC_MBOX_MSG_RQ_BP_CFG;
368 mbx.rq.qs_num = nic->vf_id;
369 mbx.rq.rq_num = qidx;
372 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (nic->vf_id << 0);
373 return nicvf_mbox_send_msg_to_pf(nic, &mbx);
377 nicvf_mbox_loopback_config(struct nicvf *nic, bool enable)
379 struct nic_mbx mbx = { .msg = { 0 } };
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);
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)
392 struct nic_mbx mbx = { .msg = { 0 } };
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);
403 nicvf_mbox_shutdown(struct nicvf *nic)
405 struct nic_mbx mbx = { .msg = { 0 } };
407 mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
408 nicvf_mbox_send_msg_to_pf(nic, &mbx);
412 nicvf_mbox_cfg_done(struct nicvf *nic)
414 struct nic_mbx mbx = { .msg = { 0 } };
416 mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
417 nicvf_mbox_send_async_msg_to_pf(nic, &mbx);