/* CCM messages via the mailbox. CMSGs get wrapped into simple TLVs * and copied into the mailbox. Multiple messages can be copied to * form a batch. Threads come in with CMSG formed in an skb, then * enqueue that skb onto the request queue. If threads skb is first * in queue this thread will handle the mailbox operation. It copies * up to 64 messages into the mailbox (making sure that both requests * and replies will fit. After FW is done processing the batch it * copies the data out and wakes waiting threads. * If a thread is waiting it either gets its the message completed * (response is copied into the same skb as the request, overwriting * it), or becomes the first in queue. * Completions and next-to-run are signaled via the control buffer * to limit potential cache line bounces.
*/
/** * struct nfp_ccm_mbox_cmsg_cb - CCM mailbox specific info * @state: processing state (/stage) of the message * @err: error encountered during processing if any * @max_len: max(request_len, reply_len) * @exp_reply: expected reply length (0 means don't validate) * @posted: the message was posted and nobody waits for the reply
*/ struct nfp_ccm_mbox_cmsg_cb { enum nfp_net_mbox_cmsg_state state; int err; unsignedint max_len; unsignedint exp_reply; bool posted;
};
static u32 nfp_ccm_mbox_max_msg(struct nfp_net *nn)
{ return round_down(nn->tlv_caps.mbox_len, 4) -
NFP_NET_CFG_MBOX_SIMPLE_VAL - /* common mbox command header */
4 * 2; /* Msg TLV plus End TLV headers */
}
staticvoid nfp_ccm_mbox_copy_in(struct nfp_net *nn, struct sk_buff *last)
{ struct sk_buff *skb; int reserve, i, cnt;
__be32 *data;
u32 off, len;
off = nn->tlv_caps.mbox_off + NFP_NET_CFG_MBOX_SIMPLE_VAL;
skb = __skb_peek(&nn->mbox_cmsg.queue); while (true) {
nfp_ccm_mbox_write_tlv(nn, off, NFP_NET_MBOX_TLV_TYPE_MSG,
skb->len);
off += 4;
/* Write data word by word, skb->data should be aligned */
data = (__be32 *)skb->data;
cnt = skb->len / 4; for (i = 0 ; i < cnt; i++) {
nn_writel(nn, off, be32_to_cpu(data[i]));
off += 4;
} if (skb->len & 3) {
__be32 tmp = 0;
/* We overcopy here slightly, but that's okay, * the skb is large enough, and the garbage will * be ignored (beyond skb->len).
*/
skb_data = (__be32 *)skb->data;
memcpy(skb_data, &hdr, 4);
cnt = DIV_ROUND_UP(length, 4); for (i = 1 ; i < cnt; i++)
skb_data[i] = cpu_to_be32(readl(data + i * 4));
}
cb->state = NFP_NET_MBOX_CMSG_STATE_REPLY_FOUND;
next_tlv:
data += round_up(length, 4); if (data + 4 > end) {
nn_dp_warn(&nn->dp, "reached end of MBOX without END TLV\n"); break;
}
}
smp_wmb(); /* order the skb->data vs. cb->state */
spin_lock_bh(&nn->mbox_cmsg.queue.lock); do {
skb = __skb_dequeue(&nn->mbox_cmsg.queue);
cb = (void *)skb->cb;
if (cb->state != NFP_NET_MBOX_CMSG_STATE_REPLY_FOUND) {
cb->err = -ENOENT;
smp_wmb(); /* order the cb->err vs. cb->state */
}
cb->state = NFP_NET_MBOX_CMSG_STATE_DONE;
if (cb->posted) { if (cb->err)
nn_dp_warn(&nn->dp, "mailbox posted msg failed type:%u err:%d\n",
nfp_ccm_get_type(skb), cb->err);
dev_consume_skb_any(skb);
}
} while (skb != last);
staticvoid nfp_ccm_mbox_run_queue_unlock(struct nfp_net *nn)
__releases(&nn->mbox_cmsg.queue.lock)
{ int space = nn->tlv_caps.mbox_len - NFP_NET_CFG_MBOX_SIMPLE_VAL; struct sk_buff *skb, *last; int cnt, err;
space -= 4; /* for End TLV */
/* First skb must fit, because it's ours and we checked it fits */
cnt = 1;
last = skb = __skb_peek(&nn->mbox_cmsg.queue);
space -= 4 + nfp_ccm_mbox_maxlen(skb);
while (!skb_queue_is_last(&nn->mbox_cmsg.queue, last)) {
skb = skb_queue_next(&nn->mbox_cmsg.queue, last);
space -= 4 + nfp_ccm_mbox_maxlen(skb); if (space < 0) break;
last = skb;
nfp_ccm_mbox_set_busy(skb);
cnt++; if (cnt == NFP_CCM_MBOX_BATCH_LIMIT) break;
}
spin_unlock_bh(&nn->mbox_cmsg.queue.lock);
/* Now we own all skb's marked in progress, new requests may arrive * at the end of the queue.
*/
nn_ctrl_bar_lock(nn);
nfp_ccm_mbox_copy_in(nn, last);
err = nfp_net_mbox_reconfig(nn, NFP_NET_CFG_MBOX_CMD_TLV_CMSG); if (!err)
nfp_ccm_mbox_copy_out(nn, last); else
nfp_ccm_mbox_mark_all_err(nn, last, -EIO);
if (cb->err)
dev_kfree_skb_any(skb); return cb->err;
}
/* If wait timed out but the command is already in progress we have * to wait until it finishes. Runners has ownership of the skbs marked * as busy.
*/ staticint
nfp_ccm_mbox_unlink_unlock(struct nfp_net *nn, struct sk_buff *skb, enum nfp_ccm_type type)
__releases(&nn->mbox_cmsg.queue.lock)
{ bool was_first;
if (nfp_ccm_mbox_in_progress(skb)) {
spin_unlock_bh(&nn->mbox_cmsg.queue.lock);
wait_event(nn->mbox_cmsg.wq, nfp_ccm_mbox_done(skb));
smp_rmb(); /* pairs with smp_wmb() after data is written */ return nfp_ccm_mbox_skb_return(skb);
}
was_first = nfp_ccm_mbox_should_run(nn, skb);
__skb_unlink(skb, &nn->mbox_cmsg.queue); if (was_first)
nfp_ccm_mbox_mark_next_runner(nn);
spin_unlock_bh(&nn->mbox_cmsg.queue.lock);
if (was_first)
wake_up_all(&nn->mbox_cmsg.wq);
nn_dp_warn(&nn->dp, "time out waiting for mbox response to 0x%02x\n",
type); return -ETIMEDOUT;
}
if (unlikely(!(nn->tlv_caps.mbox_cmsg_types & BIT(type)))) {
nn_dp_warn(&nn->dp, "message type %d not supported by mailbox\n", type); return -EINVAL;
}
/* If the reply size is unknown assume it will take the entire * mailbox, the callers should do their best for this to never * happen.
*/ if (!max_reply_size)
max_reply_size = mbox_max;
max_reply_size = round_up(max_reply_size, 4);
/* Make sure we can fit the entire reply into the skb, * and that we don't have to slow down the mbox handler * with allocations.
*/
undersize = max_reply_size - (skb_end_pointer(skb) - skb->data); if (undersize > 0) {
err = pskb_expand_head(skb, 0, undersize, flags); if (err) {
nn_dp_warn(&nn->dp, "can't allocate reply buffer for mailbox\n"); return err;
}
}
/* Make sure that request and response both fit into the mailbox */
max_len = max(max_reply_size, round_up(skb->len, 4)); if (max_len > mbox_max) {
nn_dp_warn(&nn->dp, "message too big for the mailbox: %u/%u vs %u\n",
skb->len, max_reply_size, mbox_max); return -EMSGSIZE;
}
err = nfp_ccm_mbox_msg_enqueue(nn, skb, type, critical); if (err) goto err_unlock;
/* First in queue takes the mailbox lock and processes the batch */ if (!nfp_ccm_mbox_is_first(nn, skb)) { bool to;
spin_unlock_bh(&nn->mbox_cmsg.queue.lock);
to = !wait_event_timeout(nn->mbox_cmsg.wq,
nfp_ccm_mbox_done(skb) ||
nfp_ccm_mbox_should_run(nn, skb),
msecs_to_jiffies(NFP_CCM_TIMEOUT));
/* fast path for those completed by another thread */ if (nfp_ccm_mbox_done(skb)) {
smp_rmb(); /* pairs with wmb after data is written */ return nfp_ccm_mbox_skb_return(skb);
}
spin_lock_bh(&nn->mbox_cmsg.queue.lock);
if (!nfp_ccm_mbox_is_first(nn, skb)) {
WARN_ON(!to);
nn = container_of(work, struct nfp_net, mbox_cmsg.wait_work);
skb = skb_peek(&nn->mbox_cmsg.queue); if (WARN_ON(!skb || !nfp_ccm_mbox_is_posted(skb))) /* Should never happen so it's unclear what to do here.. */ goto exit_unlock_wake;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.