blob: 9b18840cba969aa0a3162a79298c4b1713926c11 [file] [log] [blame]
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 *
18 */
19
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/pci.h>
24#include <linux/netdevice.h>
25
26#include "wq_enet_desc.h"
27#include "rq_enet_desc.h"
28#include "cq_enet_desc.h"
29#include "vnic_resource.h"
30#include "vnic_enet.h"
31#include "vnic_dev.h"
32#include "vnic_wq.h"
33#include "vnic_rq.h"
34#include "vnic_cq.h"
35#include "vnic_intr.h"
36#include "vnic_stats.h"
37#include "vnic_nic.h"
38#include "vnic_rss.h"
39#include "enic_res.h"
40#include "enic.h"
41
42int enic_get_vnic_config(struct enic *enic)
43{
44 struct vnic_enet_config *c = &enic->config;
45 int err;
46
47 err = vnic_dev_mac_addr(enic->vdev, enic->mac_addr);
48 if (err) {
49 printk(KERN_ERR PFX "Error getting MAC addr, %d\n", err);
50 return err;
51 }
52
53#define GET_CONFIG(m) \
54 do { \
55 err = vnic_dev_spec(enic->vdev, \
56 offsetof(struct vnic_enet_config, m), \
57 sizeof(c->m), &c->m); \
58 if (err) { \
59 printk(KERN_ERR PFX \
60 "Error getting %s, %d\n", #m, err); \
61 return err; \
62 } \
63 } while (0)
64
65 GET_CONFIG(flags);
66 GET_CONFIG(wq_desc_count);
67 GET_CONFIG(rq_desc_count);
68 GET_CONFIG(mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -070069 GET_CONFIG(intr_timer_type);
70 GET_CONFIG(intr_mode);
Scott Feldman7c844592009-12-23 13:27:54 +000071 GET_CONFIG(intr_timer_usec);
Scott Feldman01f2e4e2008-09-15 09:17:11 -070072
73 c->wq_desc_count =
74 min_t(u32, ENIC_MAX_WQ_DESCS,
75 max_t(u32, ENIC_MIN_WQ_DESCS,
76 c->wq_desc_count));
Scott Feldmanbd249622009-12-23 13:27:48 +000077 c->wq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070078
79 c->rq_desc_count =
80 min_t(u32, ENIC_MAX_RQ_DESCS,
81 max_t(u32, ENIC_MIN_RQ_DESCS,
82 c->rq_desc_count));
Scott Feldmanbd249622009-12-23 13:27:48 +000083 c->rq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070084
85 if (c->mtu == 0)
86 c->mtu = 1500;
87 c->mtu = min_t(u16, ENIC_MAX_MTU,
88 max_t(u16, ENIC_MIN_MTU,
89 c->mtu));
90
Scott Feldman7c844592009-12-23 13:27:54 +000091 c->intr_timer_usec = min_t(u32,
92 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
93 c->intr_timer_usec);
Scott Feldman01f2e4e2008-09-15 09:17:11 -070094
Johannes Berg7c510e42008-10-27 17:47:26 -070095 printk(KERN_INFO PFX "vNIC MAC addr %pM wq/rq %d/%d\n",
96 enic->mac_addr, c->wq_desc_count, c->rq_desc_count);
Scott Feldman01f2e4e2008-09-15 09:17:11 -070097 printk(KERN_INFO PFX "vNIC mtu %d csum tx/rx %d/%d tso/lro %d/%d "
Scott Feldman7c844592009-12-23 13:27:54 +000098 "intr timer %d usec\n",
Scott Feldman01f2e4e2008-09-15 09:17:11 -070099 c->mtu, ENIC_SETTING(enic, TXCSUM),
100 ENIC_SETTING(enic, RXCSUM), ENIC_SETTING(enic, TSO),
Scott Feldman7c844592009-12-23 13:27:54 +0000101 ENIC_SETTING(enic, LRO), c->intr_timer_usec);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700102
103 return 0;
104}
105
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700106void enic_add_multicast_addr(struct enic *enic, u8 *addr)
107{
108 vnic_dev_add_addr(enic->vdev, addr);
109}
110
111void enic_del_multicast_addr(struct enic *enic, u8 *addr)
112{
113 vnic_dev_del_addr(enic->vdev, addr);
114}
115
116void enic_add_vlan(struct enic *enic, u16 vlanid)
117{
118 u64 a0 = vlanid, a1 = 0;
119 int wait = 1000;
120 int err;
121
122 err = vnic_dev_cmd(enic->vdev, CMD_VLAN_ADD, &a0, &a1, wait);
123 if (err)
124 printk(KERN_ERR PFX "Can't add vlan id, %d\n", err);
125}
126
127void enic_del_vlan(struct enic *enic, u16 vlanid)
128{
129 u64 a0 = vlanid, a1 = 0;
130 int wait = 1000;
131 int err;
132
133 err = vnic_dev_cmd(enic->vdev, CMD_VLAN_DEL, &a0, &a1, wait);
134 if (err)
135 printk(KERN_ERR PFX "Can't delete vlan id, %d\n", err);
136}
137
138int enic_set_nic_cfg(struct enic *enic, u8 rss_default_cpu, u8 rss_hash_type,
139 u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable, u8 tso_ipid_split_en,
140 u8 ig_vlan_strip_en)
141{
142 u64 a0, a1;
143 u32 nic_cfg;
144 int wait = 1000;
145
146 vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
147 rss_hash_type, rss_hash_bits, rss_base_cpu,
148 rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
149
150 a0 = nic_cfg;
151 a1 = 0;
152
153 return vnic_dev_cmd(enic->vdev, CMD_NIC_CFG, &a0, &a1, wait);
154}
155
Scott Feldman6ba9cdc2009-09-03 17:02:24 +0000156int enic_set_rss_key(struct enic *enic, dma_addr_t key_pa, u64 len)
157{
158 u64 a0 = (u64)key_pa, a1 = len;
159 int wait = 1000;
160
161 return vnic_dev_cmd(enic->vdev, CMD_RSS_KEY, &a0, &a1, wait);
162}
163
164int enic_set_rss_cpu(struct enic *enic, dma_addr_t cpu_pa, u64 len)
165{
166 u64 a0 = (u64)cpu_pa, a1 = len;
167 int wait = 1000;
168
169 return vnic_dev_cmd(enic->vdev, CMD_RSS_CPU, &a0, &a1, wait);
170}
171
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700172void enic_free_vnic_resources(struct enic *enic)
173{
174 unsigned int i;
175
176 for (i = 0; i < enic->wq_count; i++)
177 vnic_wq_free(&enic->wq[i]);
178 for (i = 0; i < enic->rq_count; i++)
179 vnic_rq_free(&enic->rq[i]);
180 for (i = 0; i < enic->cq_count; i++)
181 vnic_cq_free(&enic->cq[i]);
182 for (i = 0; i < enic->intr_count; i++)
183 vnic_intr_free(&enic->intr[i]);
184}
185
186void enic_get_res_counts(struct enic *enic)
187{
Scott Feldman6ba9cdc2009-09-03 17:02:24 +0000188 enic->wq_count = min_t(int,
189 vnic_dev_get_res_count(enic->vdev, RES_TYPE_WQ),
190 ENIC_WQ_MAX);
191 enic->rq_count = min_t(int,
192 vnic_dev_get_res_count(enic->vdev, RES_TYPE_RQ),
193 ENIC_RQ_MAX);
194 enic->cq_count = min_t(int,
195 vnic_dev_get_res_count(enic->vdev, RES_TYPE_CQ),
196 ENIC_CQ_MAX);
197 enic->intr_count = min_t(int,
198 vnic_dev_get_res_count(enic->vdev, RES_TYPE_INTR_CTRL),
199 ENIC_INTR_MAX);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700200
201 printk(KERN_INFO PFX "vNIC resources avail: "
202 "wq %d rq %d cq %d intr %d\n",
203 enic->wq_count, enic->rq_count,
204 enic->cq_count, enic->intr_count);
205}
206
207void enic_init_vnic_resources(struct enic *enic)
208{
209 enum vnic_dev_intr_mode intr_mode;
210 unsigned int mask_on_assertion;
211 unsigned int interrupt_offset;
212 unsigned int error_interrupt_enable;
213 unsigned int error_interrupt_offset;
214 unsigned int cq_index;
215 unsigned int i;
216
217 intr_mode = vnic_dev_get_intr_mode(enic->vdev);
218
219 /* Init RQ/WQ resources.
220 *
221 * RQ[0 - n-1] point to CQ[0 - n-1]
222 * WQ[0 - m-1] point to CQ[n - n+m-1]
223 *
224 * Error interrupt is not enabled for MSI.
225 */
226
227 switch (intr_mode) {
228 case VNIC_DEV_INTR_MODE_INTX:
229 case VNIC_DEV_INTR_MODE_MSIX:
230 error_interrupt_enable = 1;
231 error_interrupt_offset = enic->intr_count - 2;
232 break;
233 default:
234 error_interrupt_enable = 0;
235 error_interrupt_offset = 0;
236 break;
237 }
238
239 for (i = 0; i < enic->rq_count; i++) {
240 cq_index = i;
241 vnic_rq_init(&enic->rq[i],
242 cq_index,
243 error_interrupt_enable,
244 error_interrupt_offset);
245 }
246
247 for (i = 0; i < enic->wq_count; i++) {
248 cq_index = enic->rq_count + i;
249 vnic_wq_init(&enic->wq[i],
250 cq_index,
251 error_interrupt_enable,
252 error_interrupt_offset);
253 }
254
255 /* Init CQ resources
256 *
257 * CQ[0 - n+m-1] point to INTR[0] for INTx, MSI
258 * CQ[0 - n+m-1] point to INTR[0 - n+m-1] for MSI-X
259 */
260
261 for (i = 0; i < enic->cq_count; i++) {
262
263 switch (intr_mode) {
264 case VNIC_DEV_INTR_MODE_MSIX:
265 interrupt_offset = i;
266 break;
267 default:
268 interrupt_offset = 0;
269 break;
270 }
271
272 vnic_cq_init(&enic->cq[i],
273 0 /* flow_control_enable */,
274 1 /* color_enable */,
275 0 /* cq_head */,
276 0 /* cq_tail */,
277 1 /* cq_tail_color */,
278 1 /* interrupt_enable */,
279 1 /* cq_entry_enable */,
280 0 /* cq_message_enable */,
281 interrupt_offset,
282 0 /* cq_message_addr */);
283 }
284
285 /* Init INTR resources
286 *
287 * mask_on_assertion is not used for INTx due to the level-
288 * triggered nature of INTx
289 */
290
291 switch (intr_mode) {
292 case VNIC_DEV_INTR_MODE_MSI:
293 case VNIC_DEV_INTR_MODE_MSIX:
294 mask_on_assertion = 1;
295 break;
296 default:
297 mask_on_assertion = 0;
298 break;
299 }
300
301 for (i = 0; i < enic->intr_count; i++) {
302 vnic_intr_init(&enic->intr[i],
Scott Feldman7c844592009-12-23 13:27:54 +0000303 INTR_COALESCE_USEC_TO_HW(enic->config.intr_timer_usec),
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700304 enic->config.intr_timer_type,
305 mask_on_assertion);
306 }
307
308 /* Clear LIF stats
309 */
310
311 vnic_dev_stats_clear(enic->vdev);
312}
313
314int enic_alloc_vnic_resources(struct enic *enic)
315{
316 enum vnic_dev_intr_mode intr_mode;
317 unsigned int i;
318 int err;
319
320 intr_mode = vnic_dev_get_intr_mode(enic->vdev);
321
322 printk(KERN_INFO PFX "vNIC resources used: "
323 "wq %d rq %d cq %d intr %d intr mode %s\n",
324 enic->wq_count, enic->rq_count,
325 enic->cq_count, enic->intr_count,
326 intr_mode == VNIC_DEV_INTR_MODE_INTX ? "legacy PCI INTx" :
327 intr_mode == VNIC_DEV_INTR_MODE_MSI ? "MSI" :
328 intr_mode == VNIC_DEV_INTR_MODE_MSIX ? "MSI-X" :
329 "unknown"
330 );
331
332 /* Allocate queue resources
333 */
334
335 for (i = 0; i < enic->wq_count; i++) {
336 err = vnic_wq_alloc(enic->vdev, &enic->wq[i], i,
337 enic->config.wq_desc_count,
338 sizeof(struct wq_enet_desc));
339 if (err)
340 goto err_out_cleanup;
341 }
342
343 for (i = 0; i < enic->rq_count; i++) {
344 err = vnic_rq_alloc(enic->vdev, &enic->rq[i], i,
345 enic->config.rq_desc_count,
346 sizeof(struct rq_enet_desc));
347 if (err)
348 goto err_out_cleanup;
349 }
350
351 for (i = 0; i < enic->cq_count; i++) {
352 if (i < enic->rq_count)
353 err = vnic_cq_alloc(enic->vdev, &enic->cq[i], i,
354 enic->config.rq_desc_count,
355 sizeof(struct cq_enet_rq_desc));
356 else
357 err = vnic_cq_alloc(enic->vdev, &enic->cq[i], i,
358 enic->config.wq_desc_count,
359 sizeof(struct cq_enet_wq_desc));
360 if (err)
361 goto err_out_cleanup;
362 }
363
364 for (i = 0; i < enic->intr_count; i++) {
365 err = vnic_intr_alloc(enic->vdev, &enic->intr[i], i);
366 if (err)
367 goto err_out_cleanup;
368 }
369
370 /* Hook remaining resource
371 */
372
373 enic->legacy_pba = vnic_dev_get_res(enic->vdev,
374 RES_TYPE_INTR_PBA_LEGACY, 0);
375 if (!enic->legacy_pba && intr_mode == VNIC_DEV_INTR_MODE_INTX) {
376 printk(KERN_ERR PFX "Failed to hook legacy pba resource\n");
377 err = -ENODEV;
378 goto err_out_cleanup;
379 }
380
381 return 0;
382
383err_out_cleanup:
384 enic_free_vnic_resources(enic);
385
386 return err;
387}