2f65f357b7652efed63f0b10d43db79c5e72fbc4
[deb_dpdk.git] / drivers / net / enic / base / vnic_cq.c
1 /*
2  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
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
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include "vnic_dev.h"
36 #include "vnic_cq.h"
37
38 int vnic_cq_mem_size(struct vnic_cq *cq, unsigned int desc_count,
39         unsigned int desc_size)
40 {
41         int mem_size;
42
43         mem_size = vnic_dev_desc_ring_size(&cq->ring, desc_count, desc_size);
44
45         return mem_size;
46 }
47
48 void vnic_cq_free(struct vnic_cq *cq)
49 {
50         vnic_dev_free_desc_ring(cq->vdev, &cq->ring);
51
52         cq->ctrl = NULL;
53 }
54
55 int vnic_cq_alloc(struct vnic_dev *vdev, struct vnic_cq *cq, unsigned int index,
56         unsigned int socket_id,
57         unsigned int desc_count, unsigned int desc_size)
58 {
59         int err;
60         char res_name[NAME_MAX];
61         static int instance;
62
63         cq->index = index;
64         cq->vdev = vdev;
65
66         cq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_CQ, index);
67         if (!cq->ctrl) {
68                 pr_err("Failed to hook CQ[%d] resource\n", index);
69                 return -EINVAL;
70         }
71
72         snprintf(res_name, sizeof(res_name), "%d-cq-%d", instance++, index);
73         err = vnic_dev_alloc_desc_ring(vdev, &cq->ring, desc_count, desc_size,
74                 socket_id, res_name);
75         if (err)
76                 return err;
77
78         return 0;
79 }
80
81 void vnic_cq_init(struct vnic_cq *cq, unsigned int flow_control_enable,
82         unsigned int color_enable, unsigned int cq_head, unsigned int cq_tail,
83         unsigned int cq_tail_color, unsigned int interrupt_enable,
84         unsigned int cq_entry_enable, unsigned int cq_message_enable,
85         unsigned int interrupt_offset, u64 cq_message_addr)
86 {
87         u64 paddr;
88
89         paddr = (u64)cq->ring.base_addr | VNIC_PADDR_TARGET;
90         writeq(paddr, &cq->ctrl->ring_base);
91         iowrite32(cq->ring.desc_count, &cq->ctrl->ring_size);
92         iowrite32(flow_control_enable, &cq->ctrl->flow_control_enable);
93         iowrite32(color_enable, &cq->ctrl->color_enable);
94         iowrite32(cq_head, &cq->ctrl->cq_head);
95         iowrite32(cq_tail, &cq->ctrl->cq_tail);
96         iowrite32(cq_tail_color, &cq->ctrl->cq_tail_color);
97         iowrite32(interrupt_enable, &cq->ctrl->interrupt_enable);
98         iowrite32(cq_entry_enable, &cq->ctrl->cq_entry_enable);
99         iowrite32(cq_message_enable, &cq->ctrl->cq_message_enable);
100         iowrite32(interrupt_offset, &cq->ctrl->interrupt_offset);
101         writeq(cq_message_addr, &cq->ctrl->cq_message_addr);
102
103         cq->interrupt_offset = interrupt_offset;
104 }
105
106 void vnic_cq_clean(struct vnic_cq *cq)
107 {
108         cq->to_clean = 0;
109         cq->last_color = 0;
110
111         iowrite32(0, &cq->ctrl->cq_head);
112         iowrite32(0, &cq->ctrl->cq_tail);
113         iowrite32(1, &cq->ctrl->cq_tail_color);
114
115         vnic_dev_clear_desc_ring(&cq->ring);
116 }