1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
7 #include <rte_atomic.h>
8 #include <rte_common.h>
9 #include <rte_cycles.h>
11 #include <rte_spinlock.h>
13 #include "octeontx_mbox.h"
14 #include "octeontx_pool_logs.h"
16 /* Mbox operation timeout in seconds */
17 #define MBOX_WAIT_TIME_SEC 3
18 #define MAX_RAM_MBOX_LEN ((SSOW_BAR4_LEN >> 1) - 8 /* Mbox header */)
20 /* Mbox channel state */
22 MBOX_CHAN_STATE_REQ = 1,
23 MBOX_CHAN_STATE_RES = 0,
26 /* Response messages */
30 MBOX_RET_INTERNAL_ERR,
35 uint8_t *ram_mbox_base; /* Base address of mbox message stored in ram */
36 uint8_t *reg; /* Store to this register triggers PF mbox interrupt */
37 uint16_t tag_own; /* Last tag which was written to own channel */
41 static struct mbox octeontx_mbox;
44 * Structure used for mbox synchronization
45 * This structure sits at the begin of Mbox RAM and used as main
46 * synchronization point for channel communication
52 uint8_t chan_state : 1;
64 mbox_msgcpy(volatile uint8_t *d, volatile const uint8_t *s, uint16_t size)
68 for (i = 0; i < size; i++)
73 mbox_send_request(struct mbox *m, struct octeontx_mbox_hdr *hdr,
74 const void *txmsg, uint16_t txsize)
76 struct mbox_ram_hdr old_hdr;
77 struct mbox_ram_hdr new_hdr = { {0} };
78 uint64_t *ram_mbox_hdr = (uint64_t *)m->ram_mbox_base;
79 uint8_t *ram_mbox_msg = m->ram_mbox_base + sizeof(struct mbox_ram_hdr);
82 * Initialize the channel with the tag left by last send.
83 * On success full mbox send complete, PF increments the tag by one.
84 * The sender can validate integrity of PF message with this scheme
86 old_hdr.u64 = rte_read64(ram_mbox_hdr);
87 m->tag_own = (old_hdr.tag + 2) & (~0x1ul); /* next even number */
91 mbox_msgcpy(ram_mbox_msg, txmsg, txsize);
94 new_hdr.chan_state = MBOX_CHAN_STATE_REQ;
95 new_hdr.coproc = hdr->coproc;
96 new_hdr.msg = hdr->msg;
97 new_hdr.vfid = hdr->vfid;
98 new_hdr.tag = m->tag_own;
101 /* Write the msg header */
102 rte_write64(new_hdr.u64, ram_mbox_hdr);
104 /* Notify PF about the new msg - write to MBOX reg generates PF IRQ */
105 rte_write64(0, m->reg);
109 mbox_wait_response(struct mbox *m, struct octeontx_mbox_hdr *hdr,
110 void *rxmsg, uint16_t rxsize)
114 struct mbox_ram_hdr rx_hdr;
115 uint64_t *ram_mbox_hdr = (uint64_t *)m->ram_mbox_base;
116 uint8_t *ram_mbox_msg = m->ram_mbox_base + sizeof(struct mbox_ram_hdr);
118 /* Wait for response */
119 wait = MBOX_WAIT_TIME_SEC * 1000 * 10;
122 rx_hdr.u64 = rte_read64(ram_mbox_hdr);
123 if (rx_hdr.chan_state == MBOX_CHAN_STATE_RES)
128 hdr->res_code = rx_hdr.res_code;
138 if (m->tag_own != rx_hdr.tag) {
143 /* PF nacked the msg */
144 if (rx_hdr.res_code != MBOX_RET_SUCCESS) {
149 len = RTE_MIN(rx_hdr.len, rxsize);
151 mbox_msgcpy(rxmsg, ram_mbox_msg, len);
156 mbox_log_err("Failed to send mbox(%d/%d) coproc=%d msg=%d ret=(%d,%d)",
157 m->tag_own, rx_hdr.tag, hdr->coproc, hdr->msg, res,
163 mbox_send(struct mbox *m, struct octeontx_mbox_hdr *hdr, const void *txmsg,
164 uint16_t txsize, void *rxmsg, uint16_t rxsize)
168 if (m->init_once == 0 || hdr == NULL ||
169 txsize > MAX_RAM_MBOX_LEN || rxsize > MAX_RAM_MBOX_LEN) {
170 mbox_log_err("Invalid init_once=%d hdr=%p txsz=%d rxsz=%d",
171 m->init_once, hdr, txsize, rxsize);
175 rte_spinlock_lock(&m->lock);
177 mbox_send_request(m, hdr, txmsg, txsize);
178 res = mbox_wait_response(m, hdr, rxmsg, rxsize);
180 rte_spinlock_unlock(&m->lock);
185 mbox_setup(struct mbox *m)
187 if (unlikely(m->init_once == 0)) {
188 rte_spinlock_init(&m->lock);
189 m->ram_mbox_base = octeontx_ssovf_bar(OCTEONTX_SSO_HWS, 0, 4);
190 m->reg = octeontx_ssovf_bar(OCTEONTX_SSO_GROUP, 0, 0);
191 m->reg += SSO_VHGRP_PF_MBOX(1);
193 if (m->ram_mbox_base == NULL || m->reg == NULL) {
194 mbox_log_err("Invalid ram_mbox_base=%p or reg=%p",
195 m->ram_mbox_base, m->reg);
204 octeontx_ssovf_mbox_send(struct octeontx_mbox_hdr *hdr, void *txdata,
205 uint16_t txlen, void *rxdata, uint16_t rxlen)
207 struct mbox *m = &octeontx_mbox;
209 RTE_BUILD_BUG_ON(sizeof(struct mbox_ram_hdr) != 8);
210 if (rte_eal_process_type() != RTE_PROC_PRIMARY || mbox_setup(m))
213 return mbox_send(m, hdr, txdata, txlen, rxdata, rxlen);