Imported Upstream version 16.04
[deb_dpdk.git] / drivers / net / enic / enic_res.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 "enic_compat.h"
36 #include "rte_ethdev.h"
37 #include "wq_enet_desc.h"
38 #include "rq_enet_desc.h"
39 #include "cq_enet_desc.h"
40 #include "vnic_resource.h"
41 #include "vnic_enet.h"
42 #include "vnic_dev.h"
43 #include "vnic_wq.h"
44 #include "vnic_rq.h"
45 #include "vnic_cq.h"
46 #include "vnic_intr.h"
47 #include "vnic_stats.h"
48 #include "vnic_nic.h"
49 #include "vnic_rss.h"
50 #include "enic_res.h"
51 #include "enic.h"
52
53 int enic_get_vnic_config(struct enic *enic)
54 {
55         struct vnic_enet_config *c = &enic->config;
56         int err;
57
58         err = vnic_dev_get_mac_addr(enic->vdev, enic->mac_addr);
59         if (err) {
60                 dev_err(enic_get_dev(enic),
61                         "Error getting MAC addr, %d\n", err);
62                 return err;
63         }
64
65 #define GET_CONFIG(m) \
66         do { \
67                 err = vnic_dev_spec(enic->vdev, \
68                         offsetof(struct vnic_enet_config, m), \
69                         sizeof(c->m), &c->m); \
70                 if (err) { \
71                         dev_err(enic_get_dev(enic), \
72                                 "Error getting %s, %d\n", #m, err); \
73                         return err; \
74                 } \
75         } while (0)
76
77         GET_CONFIG(flags);
78         GET_CONFIG(wq_desc_count);
79         GET_CONFIG(rq_desc_count);
80         GET_CONFIG(mtu);
81         GET_CONFIG(intr_timer_type);
82         GET_CONFIG(intr_mode);
83         GET_CONFIG(intr_timer_usec);
84         GET_CONFIG(loop_tag);
85         GET_CONFIG(num_arfs);
86
87         c->wq_desc_count =
88                 min_t(u32, ENIC_MAX_WQ_DESCS,
89                 max_t(u32, ENIC_MIN_WQ_DESCS,
90                 c->wq_desc_count));
91         c->wq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
92
93         c->rq_desc_count =
94                 min_t(u32, ENIC_MAX_RQ_DESCS,
95                 max_t(u32, ENIC_MIN_RQ_DESCS,
96                 c->rq_desc_count));
97         c->rq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
98
99         if (c->mtu == 0)
100                 c->mtu = 1500;
101         c->mtu = min_t(u16, ENIC_MAX_MTU,
102                 max_t(u16, ENIC_MIN_MTU,
103                 c->mtu));
104
105         c->intr_timer_usec = min_t(u32, c->intr_timer_usec,
106                 vnic_dev_get_intr_coal_timer_max(enic->vdev));
107
108         dev_info(enic_get_dev(enic),
109                 "vNIC MAC addr %02x:%02x:%02x:%02x:%02x:%02x "
110                 "wq/rq %d/%d mtu %d\n",
111                 enic->mac_addr[0], enic->mac_addr[1], enic->mac_addr[2],
112                 enic->mac_addr[3], enic->mac_addr[4], enic->mac_addr[5],
113                 c->wq_desc_count, c->rq_desc_count, c->mtu);
114         dev_info(enic_get_dev(enic), "vNIC csum tx/rx %s/%s "
115                 "rss %s intr mode %s type %s timer %d usec "
116                 "loopback tag 0x%04x\n",
117                 ENIC_SETTING(enic, TXCSUM) ? "yes" : "no",
118                 ENIC_SETTING(enic, RXCSUM) ? "yes" : "no",
119                 ENIC_SETTING(enic, RSS) ? "yes" : "no",
120                 c->intr_mode == VENET_INTR_MODE_INTX ? "INTx" :
121                 c->intr_mode == VENET_INTR_MODE_MSI ? "MSI" :
122                 c->intr_mode == VENET_INTR_MODE_ANY ? "any" :
123                 "unknown",
124                 c->intr_timer_type == VENET_INTR_TYPE_MIN ? "min" :
125                 c->intr_timer_type == VENET_INTR_TYPE_IDLE ? "idle" :
126                 "unknown",
127                 c->intr_timer_usec,
128                 c->loop_tag);
129
130         return 0;
131 }
132
133 int enic_add_vlan(struct enic *enic, u16 vlanid)
134 {
135         u64 a0 = vlanid, a1 = 0;
136         int wait = 1000;
137         int err;
138
139         err = vnic_dev_cmd(enic->vdev, CMD_VLAN_ADD, &a0, &a1, wait);
140         if (err)
141                 dev_err(enic_get_dev(enic), "Can't add vlan id, %d\n", err);
142
143         return err;
144 }
145
146 int enic_del_vlan(struct enic *enic, u16 vlanid)
147 {
148         u64 a0 = vlanid, a1 = 0;
149         int wait = 1000;
150         int err;
151
152         err = vnic_dev_cmd(enic->vdev, CMD_VLAN_DEL, &a0, &a1, wait);
153         if (err)
154                 dev_err(enic_get_dev(enic), "Can't delete vlan id, %d\n", err);
155
156         return err;
157 }
158
159 int enic_set_nic_cfg(struct enic *enic, u8 rss_default_cpu, u8 rss_hash_type,
160         u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable, u8 tso_ipid_split_en,
161         u8 ig_vlan_strip_en)
162 {
163         u64 a0, a1;
164         u32 nic_cfg;
165         int wait = 1000;
166
167         vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
168                 rss_hash_type, rss_hash_bits, rss_base_cpu,
169                 rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
170
171         a0 = nic_cfg;
172         a1 = 0;
173
174         return vnic_dev_cmd(enic->vdev, CMD_NIC_CFG, &a0, &a1, wait);
175 }
176
177 int enic_set_rss_key(struct enic *enic, dma_addr_t key_pa, u64 len)
178 {
179         u64 a0 = (u64)key_pa, a1 = len;
180         int wait = 1000;
181
182         return vnic_dev_cmd(enic->vdev, CMD_RSS_KEY, &a0, &a1, wait);
183 }
184
185 int enic_set_rss_cpu(struct enic *enic, dma_addr_t cpu_pa, u64 len)
186 {
187         u64 a0 = (u64)cpu_pa, a1 = len;
188         int wait = 1000;
189
190         return vnic_dev_cmd(enic->vdev, CMD_RSS_CPU, &a0, &a1, wait);
191 }
192
193 void enic_free_vnic_resources(struct enic *enic)
194 {
195         unsigned int i;
196
197         for (i = 0; i < enic->wq_count; i++)
198                 vnic_wq_free(&enic->wq[i]);
199         for (i = 0; i < enic->rq_count; i++)
200                 vnic_rq_free(&enic->rq[i]);
201         for (i = 0; i < enic->cq_count; i++)
202                 vnic_cq_free(&enic->cq[i]);
203         vnic_intr_free(&enic->intr);
204 }
205
206 void enic_get_res_counts(struct enic *enic)
207 {
208         enic->wq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_WQ);
209         enic->rq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_RQ);
210         enic->cq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_CQ);
211         enic->intr_count = vnic_dev_get_res_count(enic->vdev,
212                 RES_TYPE_INTR_CTRL);
213
214         dev_info(enic_get_dev(enic),
215                 "vNIC resources avail: wq %d rq %d cq %d intr %d\n",
216                 enic->wq_count, enic->rq_count,
217                 enic->cq_count, enic->intr_count);
218 }