New upstream version 17.08
[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
66 #define GET_CONFIG(m) \
67         do { \
68                 err = vnic_dev_spec(enic->vdev, \
69                         offsetof(struct vnic_enet_config, m), \
70                         sizeof(c->m), &c->m); \
71                 if (err) { \
72                         dev_err(enic_get_dev(enic), \
73                                 "Error getting %s, %d\n", #m, err); \
74                         return err; \
75                 } \
76         } while (0)
77
78         GET_CONFIG(flags);
79         GET_CONFIG(wq_desc_count);
80         GET_CONFIG(rq_desc_count);
81         GET_CONFIG(mtu);
82         GET_CONFIG(intr_timer_type);
83         GET_CONFIG(intr_mode);
84         GET_CONFIG(intr_timer_usec);
85         GET_CONFIG(loop_tag);
86         GET_CONFIG(num_arfs);
87         GET_CONFIG(max_pkt_size);
88
89         /* max packet size is only defined in newer VIC firmware
90          * and will be 0 for legacy firmware and VICs
91          */
92         if (c->max_pkt_size > ENIC_DEFAULT_RX_MAX_PKT_SIZE)
93                 enic->max_mtu = c->max_pkt_size - (ETHER_HDR_LEN + 4);
94         else
95                 enic->max_mtu = ENIC_DEFAULT_RX_MAX_PKT_SIZE
96                                 - (ETHER_HDR_LEN + 4);
97         if (c->mtu == 0)
98                 c->mtu = 1500;
99
100         enic->rte_dev->data->mtu = min_t(u16, enic->max_mtu,
101                                          max_t(u16, ENIC_MIN_MTU, c->mtu));
102
103         enic->adv_filters = vnic_dev_capable_adv_filters(enic->vdev);
104         dev_info(enic, "Advanced Filters %savailable\n", ((enic->adv_filters)
105                  ? "" : "not "));
106
107         err = vnic_dev_capable_filter_mode(enic->vdev, &enic->flow_filter_mode,
108                                            &enic->filter_tags);
109         if (err) {
110                 dev_err(enic_get_dev(enic),
111                         "Error getting filter modes, %d\n", err);
112                 return err;
113         }
114
115         dev_info(enic, "Flow api filter mode: %s, Filter tagging %savailable\n",
116                 ((enic->flow_filter_mode == FILTER_DPDK_1) ? "DPDK" :
117                 ((enic->flow_filter_mode == FILTER_USNIC_IP) ? "USNIC" :
118                 ((enic->flow_filter_mode == FILTER_IPV4_5TUPLE) ? "5TUPLE" :
119                 "NONE"))),
120                 ((enic->filter_tags) ? "" : "not "));
121
122         c->wq_desc_count =
123                 min_t(u32, ENIC_MAX_WQ_DESCS,
124                 max_t(u32, ENIC_MIN_WQ_DESCS,
125                 c->wq_desc_count));
126         c->wq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
127
128         c->rq_desc_count =
129                 min_t(u32, ENIC_MAX_RQ_DESCS,
130                 max_t(u32, ENIC_MIN_RQ_DESCS,
131                 c->rq_desc_count));
132         c->rq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
133
134         c->intr_timer_usec = min_t(u32, c->intr_timer_usec,
135                 vnic_dev_get_intr_coal_timer_max(enic->vdev));
136
137         dev_info(enic_get_dev(enic),
138                 "vNIC MAC addr %02x:%02x:%02x:%02x:%02x:%02x "
139                 "wq/rq %d/%d mtu %d, max mtu:%d\n",
140                 enic->mac_addr[0], enic->mac_addr[1], enic->mac_addr[2],
141                 enic->mac_addr[3], enic->mac_addr[4], enic->mac_addr[5],
142                 c->wq_desc_count, c->rq_desc_count,
143                 enic->rte_dev->data->mtu, enic->max_mtu);
144         dev_info(enic_get_dev(enic), "vNIC csum tx/rx %s/%s "
145                 "rss %s intr mode %s type %s timer %d usec "
146                 "loopback tag 0x%04x\n",
147                 ENIC_SETTING(enic, TXCSUM) ? "yes" : "no",
148                 ENIC_SETTING(enic, RXCSUM) ? "yes" : "no",
149                 ENIC_SETTING(enic, RSS) ? "yes" : "no",
150                 c->intr_mode == VENET_INTR_MODE_INTX ? "INTx" :
151                 c->intr_mode == VENET_INTR_MODE_MSI ? "MSI" :
152                 c->intr_mode == VENET_INTR_MODE_ANY ? "any" :
153                 "unknown",
154                 c->intr_timer_type == VENET_INTR_TYPE_MIN ? "min" :
155                 c->intr_timer_type == VENET_INTR_TYPE_IDLE ? "idle" :
156                 "unknown",
157                 c->intr_timer_usec,
158                 c->loop_tag);
159
160         return 0;
161 }
162
163 int enic_add_vlan(struct enic *enic, u16 vlanid)
164 {
165         u64 a0 = vlanid, a1 = 0;
166         int wait = 1000;
167         int err;
168
169         err = vnic_dev_cmd(enic->vdev, CMD_VLAN_ADD, &a0, &a1, wait);
170         if (err)
171                 dev_err(enic_get_dev(enic), "Can't add vlan id, %d\n", err);
172
173         return err;
174 }
175
176 int enic_del_vlan(struct enic *enic, u16 vlanid)
177 {
178         u64 a0 = vlanid, a1 = 0;
179         int wait = 1000;
180         int err;
181
182         err = vnic_dev_cmd(enic->vdev, CMD_VLAN_DEL, &a0, &a1, wait);
183         if (err)
184                 dev_err(enic_get_dev(enic), "Can't delete vlan id, %d\n", err);
185
186         return err;
187 }
188
189 int enic_set_nic_cfg(struct enic *enic, u8 rss_default_cpu, u8 rss_hash_type,
190         u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable, u8 tso_ipid_split_en,
191         u8 ig_vlan_strip_en)
192 {
193         u64 a0, a1;
194         u32 nic_cfg;
195         int wait = 1000;
196
197         vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
198                 rss_hash_type, rss_hash_bits, rss_base_cpu,
199                 rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
200
201         a0 = nic_cfg;
202         a1 = 0;
203
204         return vnic_dev_cmd(enic->vdev, CMD_NIC_CFG, &a0, &a1, wait);
205 }
206
207 int enic_set_rss_key(struct enic *enic, dma_addr_t key_pa, u64 len)
208 {
209         u64 a0 = (u64)key_pa, a1 = len;
210         int wait = 1000;
211
212         return vnic_dev_cmd(enic->vdev, CMD_RSS_KEY, &a0, &a1, wait);
213 }
214
215 int enic_set_rss_cpu(struct enic *enic, dma_addr_t cpu_pa, u64 len)
216 {
217         u64 a0 = (u64)cpu_pa, a1 = len;
218         int wait = 1000;
219
220         return vnic_dev_cmd(enic->vdev, CMD_RSS_CPU, &a0, &a1, wait);
221 }
222
223 void enic_free_vnic_resources(struct enic *enic)
224 {
225         unsigned int i;
226
227         for (i = 0; i < enic->wq_count; i++)
228                 vnic_wq_free(&enic->wq[i]);
229         for (i = 0; i < enic_vnic_rq_count(enic); i++)
230                 if (enic->rq[i].in_use)
231                         vnic_rq_free(&enic->rq[i]);
232         for (i = 0; i < enic->cq_count; i++)
233                 vnic_cq_free(&enic->cq[i]);
234         vnic_intr_free(&enic->intr);
235 }
236
237 void enic_get_res_counts(struct enic *enic)
238 {
239         enic->conf_wq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_WQ);
240         enic->conf_rq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_RQ);
241         enic->conf_cq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_CQ);
242         enic->conf_intr_count = vnic_dev_get_res_count(enic->vdev,
243                 RES_TYPE_INTR_CTRL);
244
245         dev_info(enic_get_dev(enic),
246                 "vNIC resources avail: wq %d rq %d cq %d intr %d\n",
247                 enic->conf_wq_count, enic->conf_rq_count,
248                 enic->conf_cq_count, enic->conf_intr_count);
249 }