New upstream version 17.11-rc3
[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 void vnic_cq_free(struct vnic_cq *cq)
39 {
40         vnic_dev_free_desc_ring(cq->vdev, &cq->ring);
41
42         cq->ctrl = NULL;
43 }
44
45 int vnic_cq_alloc(struct vnic_dev *vdev, struct vnic_cq *cq, unsigned int index,
46         unsigned int socket_id,
47         unsigned int desc_count, unsigned int desc_size)
48 {
49         int err;
50         char res_name[NAME_MAX];
51         static int instance;
52
53         cq->index = index;
54         cq->vdev = vdev;
55
56         cq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_CQ, index);
57         if (!cq->ctrl) {
58                 pr_err("Failed to hook CQ[%u] resource\n", index);
59                 return -EINVAL;
60         }
61
62         snprintf(res_name, sizeof(res_name), "%d-cq-%u", instance++, index);
63         err = vnic_dev_alloc_desc_ring(vdev, &cq->ring, desc_count, desc_size,
64                 socket_id, res_name);
65         if (err)
66                 return err;
67
68         return 0;
69 }
70
71 void vnic_cq_init(struct vnic_cq *cq, unsigned int flow_control_enable,
72         unsigned int color_enable, unsigned int cq_head, unsigned int cq_tail,
73         unsigned int cq_tail_color, unsigned int interrupt_enable,
74         unsigned int cq_entry_enable, unsigned int cq_message_enable,
75         unsigned int interrupt_offset, u64 cq_message_addr)
76 {
77         u64 paddr;
78
79         paddr = (u64)cq->ring.base_addr | VNIC_PADDR_TARGET;
80         writeq(paddr, &cq->ctrl->ring_base);
81         iowrite32(cq->ring.desc_count, &cq->ctrl->ring_size);
82         iowrite32(flow_control_enable, &cq->ctrl->flow_control_enable);
83         iowrite32(color_enable, &cq->ctrl->color_enable);
84         iowrite32(cq_head, &cq->ctrl->cq_head);
85         iowrite32(cq_tail, &cq->ctrl->cq_tail);
86         iowrite32(cq_tail_color, &cq->ctrl->cq_tail_color);
87         iowrite32(interrupt_enable, &cq->ctrl->interrupt_enable);
88         iowrite32(cq_entry_enable, &cq->ctrl->cq_entry_enable);
89         iowrite32(cq_message_enable, &cq->ctrl->cq_message_enable);
90         iowrite32(interrupt_offset, &cq->ctrl->interrupt_offset);
91         writeq(cq_message_addr, &cq->ctrl->cq_message_addr);
92
93         cq->interrupt_offset = interrupt_offset;
94 }
95
96 void vnic_cq_clean(struct vnic_cq *cq)
97 {
98         cq->to_clean = 0;
99         cq->last_color = 0;
100
101         iowrite32(0, &cq->ctrl->cq_head);
102         iowrite32(0, &cq->ctrl->cq_tail);
103         iowrite32(1, &cq->ctrl->cq_tail_color);
104
105         vnic_dev_clear_desc_ring(&cq->ring);
106 }