2021-10-21 14:55:48 -07:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause-Views
|
2019-07-17 18:13:51 -07:00
|
|
|
/*
|
2023-06-26 11:44:57 -07:00
|
|
|
* Copyright (c) 2019-2023 The Regents of the University of California
|
2021-10-21 14:55:48 -07:00
|
|
|
*/
|
2019-07-17 18:13:51 -07:00
|
|
|
|
|
|
|
#include "mqnic.h"
|
|
|
|
|
2023-05-02 21:23:30 -07:00
|
|
|
struct mqnic_cq *mqnic_create_cq(struct mqnic_if *interface)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
struct mqnic_cq *cq;
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq = kzalloc(sizeof(*cq), GFP_KERNEL);
|
|
|
|
if (!cq)
|
2023-04-30 21:57:32 -07:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2022-01-16 00:04:53 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->dev = interface->dev;
|
|
|
|
cq->interface = interface;
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-05-02 21:23:30 -07:00
|
|
|
cq->cqn = -1;
|
|
|
|
cq->enabled = 0;
|
2021-12-10 21:03:46 -08:00
|
|
|
|
2023-05-02 21:23:30 -07:00
|
|
|
cq->hw_addr = NULL;
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-07-07 01:19:19 -07:00
|
|
|
cq->prod_ptr = 0;
|
|
|
|
cq->cons_ptr = 0;
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-04-30 21:57:32 -07:00
|
|
|
return cq;
|
2021-12-12 01:52:24 -08:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:57:32 -07:00
|
|
|
void mqnic_destroy_cq(struct mqnic_cq *cq)
|
2021-12-12 01:52:24 -08:00
|
|
|
{
|
2023-05-02 21:23:30 -07:00
|
|
|
mqnic_close_cq(cq);
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
kfree(cq);
|
2021-12-12 01:52:24 -08:00
|
|
|
}
|
|
|
|
|
2023-07-10 17:52:34 -07:00
|
|
|
int mqnic_open_cq(struct mqnic_cq *cq, struct mqnic_eq *eq, int size)
|
2021-12-12 01:52:24 -08:00
|
|
|
{
|
2023-05-02 21:23:30 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (cq->enabled || cq->hw_addr || cq->buf || !eq)
|
2022-08-15 23:50:36 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2023-07-10 17:52:34 -07:00
|
|
|
cq->cqn = mqnic_res_alloc(cq->interface->cq_res);
|
2023-05-02 21:23:30 -07:00
|
|
|
if (cq->cqn < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->size = roundup_pow_of_two(size);
|
|
|
|
cq->size_mask = cq->size - 1;
|
2023-05-02 21:23:30 -07:00
|
|
|
cq->stride = roundup_pow_of_two(MQNIC_CPL_SIZE);
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->buf_size = cq->size * cq->stride;
|
|
|
|
cq->buf = dma_alloc_coherent(cq->dev, cq->buf_size, &cq->buf_dma_addr, GFP_KERNEL);
|
2023-05-02 21:23:30 -07:00
|
|
|
if (!cq->buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
2022-08-15 23:50:36 -07:00
|
|
|
}
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->eq = eq;
|
2023-05-02 21:23:30 -07:00
|
|
|
mqnic_eq_attach_cq(eq, cq);
|
2023-07-10 17:52:34 -07:00
|
|
|
cq->hw_addr = mqnic_res_get_addr(cq->interface->cq_res, cq->cqn);
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-07-07 01:19:19 -07:00
|
|
|
cq->prod_ptr = 0;
|
|
|
|
cq->cons_ptr = 0;
|
2023-04-06 20:43:13 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
memset(cq->buf, 1, cq->buf_size);
|
2023-04-06 20:43:13 -07:00
|
|
|
|
2021-10-08 18:31:53 -07:00
|
|
|
// deactivate queue
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_ENABLE | 0, cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set base address
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32((cq->buf_dma_addr & 0xfffff000),
|
|
|
|
cq->hw_addr + MQNIC_CQ_BASE_ADDR_VF_REG + 0);
|
|
|
|
iowrite32(cq->buf_dma_addr >> 32,
|
|
|
|
cq->hw_addr + MQNIC_CQ_BASE_ADDR_VF_REG + 4);
|
|
|
|
// set size
|
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_SIZE | ilog2(cq->size),
|
|
|
|
cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2023-05-02 21:23:30 -07:00
|
|
|
// set EQN
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_EQN | cq->eq->eqn,
|
|
|
|
cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set pointers
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_PROD_PTR | (cq->prod_ptr & MQNIC_CQ_PTR_MASK),
|
|
|
|
cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_CONS_PTR | (cq->cons_ptr & MQNIC_CQ_PTR_MASK),
|
|
|
|
cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
|
|
|
// activate queue
|
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_ENABLE | 1, cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-05-02 21:23:30 -07:00
|
|
|
cq->enabled = 1;
|
2021-12-10 21:04:52 -08:00
|
|
|
|
2021-10-08 18:31:53 -07:00
|
|
|
return 0;
|
2023-05-02 21:23:30 -07:00
|
|
|
|
|
|
|
fail:
|
|
|
|
mqnic_close_eq(eq);
|
|
|
|
return ret;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-05-02 21:23:30 -07:00
|
|
|
void mqnic_close_cq(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-05-02 21:23:30 -07:00
|
|
|
if (cq->hw_addr) {
|
|
|
|
// deactivate queue
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_ENABLE | 0, cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2023-05-02 21:23:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cq->eq) {
|
|
|
|
mqnic_eq_detach_cq(cq->eq, cq);
|
|
|
|
cq->eq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cq->hw_addr = NULL;
|
|
|
|
|
|
|
|
if (cq->buf) {
|
|
|
|
dma_free_coherent(cq->dev, cq->buf_size, cq->buf, cq->buf_dma_addr);
|
|
|
|
cq->buf = NULL;
|
|
|
|
cq->buf_dma_addr = 0;
|
|
|
|
}
|
2021-12-10 21:04:52 -08:00
|
|
|
|
2023-07-10 17:52:34 -07:00
|
|
|
mqnic_res_free(cq->interface->cq_res, cq->cqn);
|
2023-05-02 21:23:30 -07:00
|
|
|
cq->cqn = -1;
|
2021-12-12 13:58:26 -08:00
|
|
|
|
2023-05-02 21:23:30 -07:00
|
|
|
cq->enabled = 0;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-07-07 01:19:19 -07:00
|
|
|
void mqnic_cq_read_prod_ptr(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-07-07 01:19:19 -07:00
|
|
|
cq->prod_ptr += ((ioread32(cq->hw_addr + MQNIC_CQ_PTR_REG) >> 16) - cq->prod_ptr) & MQNIC_CQ_PTR_MASK;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-07-07 01:19:19 -07:00
|
|
|
void mqnic_cq_write_cons_ptr(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_CONS_PTR | (cq->cons_ptr & MQNIC_CQ_PTR_MASK),
|
|
|
|
cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
void mqnic_arm_cq(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-05-02 21:23:30 -07:00
|
|
|
if (!cq->enabled)
|
2021-12-12 13:58:26 -08:00
|
|
|
return;
|
|
|
|
|
2023-07-07 01:19:19 -07:00
|
|
|
iowrite32(MQNIC_CQ_CMD_SET_ARM | 1, cq->hw_addr + MQNIC_CQ_CTRL_STATUS_REG);
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|