2021-10-21 14:55:48 -07:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause-Views
|
2019-07-17 18:13:51 -07:00
|
|
|
/*
|
2021-10-21 14:55:48 -07:00
|
|
|
* Copyright 2019-2021, The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The views and conclusions contained in the software and documentation
|
|
|
|
* are those of the authors and should not be interpreted as representing
|
|
|
|
* official policies, either expressed or implied, of The Regents of the
|
|
|
|
* University of California.
|
|
|
|
*/
|
2019-07-17 18:13:51 -07:00
|
|
|
|
|
|
|
#include "mqnic.h"
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
int mqnic_create_cq(struct mqnic_if *interface, struct mqnic_cq **cq_ptr,
|
|
|
|
int cqn, u8 __iomem *hw_addr)
|
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)
|
2021-10-08 18:31:53 -07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
*cq_ptr = cq;
|
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-04-30 21:48:34 -07:00
|
|
|
cq->cqn = cqn;
|
|
|
|
cq->active = 0;
|
2021-12-10 21:03:46 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->hw_addr = hw_addr;
|
|
|
|
cq->hw_ptr_mask = 0xffff;
|
|
|
|
cq->hw_head_ptr = hw_addr + MQNIC_CQ_HEAD_PTR_REG;
|
|
|
|
cq->hw_tail_ptr = hw_addr + MQNIC_CQ_TAIL_PTR_REG;
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->head_ptr = 0;
|
|
|
|
cq->tail_ptr = 0;
|
2021-12-12 01:52:24 -08:00
|
|
|
|
|
|
|
// deactivate queue
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(0, cq->hw_addr + MQNIC_CQ_ACTIVE_LOG_SIZE_REG);
|
2021-12-12 01:52:24 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
void mqnic_destroy_cq(struct mqnic_cq **cq_ptr)
|
2021-12-12 01:52:24 -08:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
struct mqnic_cq *cq = *cq_ptr;
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
mqnic_free_cq(cq);
|
2021-12-12 01:52:24 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
*cq_ptr = NULL;
|
|
|
|
kfree(cq);
|
2021-12-12 01:52:24 -08:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
int mqnic_alloc_cq(struct mqnic_cq *cq, int size, int stride)
|
2021-12-12 01:52:24 -08:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
if (cq->active || cq->buf)
|
2022-08-15 23:50:36 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->size = roundup_pow_of_two(size);
|
|
|
|
cq->size_mask = cq->size - 1;
|
|
|
|
cq->stride = roundup_pow_of_two(stride);
|
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);
|
|
|
|
if (!cq->buf)
|
2021-12-12 01:52:24 -08:00
|
|
|
return -ENOMEM;
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->head_ptr = 0;
|
|
|
|
cq->tail_ptr = 0;
|
2021-10-08 18:31:53 -07:00
|
|
|
|
|
|
|
// deactivate queue
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(0, cq->hw_addr + MQNIC_CQ_ACTIVE_LOG_SIZE_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set base address
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->buf_dma_addr, cq->hw_addr + MQNIC_CQ_BASE_ADDR_REG + 0);
|
|
|
|
iowrite32(cq->buf_dma_addr >> 32, cq->hw_addr + MQNIC_CQ_BASE_ADDR_REG + 4);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set interrupt index
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(0, cq->hw_addr + MQNIC_CQ_INTERRUPT_INDEX_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set pointers
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->head_ptr & cq->hw_ptr_mask, cq->hw_addr + MQNIC_CQ_HEAD_PTR_REG);
|
|
|
|
iowrite32(cq->tail_ptr & cq->hw_ptr_mask, cq->hw_addr + MQNIC_CQ_TAIL_PTR_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set size
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(ilog2(cq->size), cq->hw_addr + MQNIC_CQ_ACTIVE_LOG_SIZE_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
|
|
|
|
return 0;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
void mqnic_free_cq(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
mqnic_deactivate_cq(cq);
|
2019-07-17 18:13:51 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
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;
|
2022-08-15 23:50:36 -07:00
|
|
|
}
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
int mqnic_activate_cq(struct mqnic_cq *cq, struct mqnic_eq *eq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
mqnic_deactivate_cq(cq);
|
2021-12-10 21:04:52 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
if (!cq->buf || !eq)
|
2021-12-12 01:52:24 -08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->eq = eq;
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->head_ptr = 0;
|
|
|
|
cq->tail_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-04-30 21:48:34 -07:00
|
|
|
iowrite32(0, cq->hw_addr + MQNIC_CQ_ACTIVE_LOG_SIZE_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set base address
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->buf_dma_addr, cq->hw_addr + MQNIC_CQ_BASE_ADDR_REG + 0);
|
|
|
|
iowrite32(cq->buf_dma_addr >> 32, cq->hw_addr + MQNIC_CQ_BASE_ADDR_REG + 4);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set interrupt index
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->eq->eqn, cq->hw_addr + MQNIC_CQ_INTERRUPT_INDEX_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set pointers
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->head_ptr & cq->hw_ptr_mask, cq->hw_addr + MQNIC_CQ_HEAD_PTR_REG);
|
|
|
|
iowrite32(cq->tail_ptr & cq->hw_ptr_mask, cq->hw_addr + MQNIC_CQ_TAIL_PTR_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// set size and activate queue
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(ilog2(cq->size) | MQNIC_CQ_ACTIVE_MASK,
|
|
|
|
cq->hw_addr + MQNIC_CQ_ACTIVE_LOG_SIZE_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->active = 1;
|
2021-12-10 21:04:52 -08:00
|
|
|
|
2021-10-08 18:31:53 -07:00
|
|
|
return 0;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
void mqnic_deactivate_cq(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2021-10-08 18:31:53 -07:00
|
|
|
// deactivate queue
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(ilog2(cq->size), cq->hw_addr + MQNIC_CQ_ACTIVE_LOG_SIZE_REG);
|
2021-10-08 18:31:53 -07:00
|
|
|
// disarm queue
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(0, cq->hw_addr + MQNIC_CQ_INTERRUPT_INDEX_REG);
|
2021-12-10 21:04:52 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->eq = NULL;
|
2021-12-12 13:58:26 -08:00
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->active = 0;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
void mqnic_cq_read_head_ptr(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
cq->head_ptr += (ioread32(cq->hw_head_ptr) - cq->head_ptr) & cq->hw_ptr_mask;
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
void mqnic_cq_write_tail_ptr(struct mqnic_cq *cq)
|
2019-07-17 18:13:51 -07:00
|
|
|
{
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->tail_ptr & cq->hw_ptr_mask, cq->hw_tail_ptr);
|
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-04-30 21:48:34 -07:00
|
|
|
if (!cq->active)
|
2021-12-12 13:58:26 -08:00
|
|
|
return;
|
|
|
|
|
2023-04-30 21:48:34 -07:00
|
|
|
iowrite32(cq->eq->eqn | MQNIC_CQ_ARM_MASK,
|
|
|
|
cq->hw_addr + MQNIC_CQ_INTERRUPT_INDEX_REG);
|
2019-07-17 18:13:51 -07:00
|
|
|
}
|