Skip to content

Commit 970a2e9

Browse files
David VomLehndavem330
authored andcommitted
net: ethernet: aquantia: Vector operations
Add functions to manululate the vector of receive and transmit rings. Signed-off-by: Alexander Loktionov <[email protected]> Signed-off-by: Dmitrii Tarakanov <[email protected]> Signed-off-by: Pavel.Belous <[email protected]> Signed-off-by: Dmitry Bezrukov <[email protected]> Signed-off-by: David M. VomLehn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent bab6de8 commit 970a2e9

File tree

2 files changed

+434
-0
lines changed

2 files changed

+434
-0
lines changed
Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
1+
/*
2+
* aQuantia Corporation Network Driver
3+
* Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
4+
*
5+
* This program is free software; you can redistribute it and/or modify it
6+
* under the terms and conditions of the GNU General Public License,
7+
* version 2, as published by the Free Software Foundation.
8+
*/
9+
10+
/* File aq_vec.c: Definition of common structure for vector of Rx and Tx rings.
11+
* Definition of functions for Rx and Tx rings. Friendly module for aq_nic.
12+
*/
13+
14+
#include "aq_vec.h"
15+
#include "aq_nic.h"
16+
#include "aq_ring.h"
17+
#include "aq_hw.h"
18+
19+
#include <linux/netdevice.h>
20+
21+
struct aq_vec_s {
22+
struct aq_obj_s header;
23+
struct aq_hw_ops *aq_hw_ops;
24+
struct aq_hw_s *aq_hw;
25+
struct aq_nic_s *aq_nic;
26+
unsigned int tx_rings;
27+
unsigned int rx_rings;
28+
struct aq_ring_param_s aq_ring_param;
29+
struct napi_struct napi;
30+
struct aq_ring_s ring[AQ_CFG_TCS_MAX][2];
31+
};
32+
33+
#define AQ_VEC_TX_ID 0
34+
#define AQ_VEC_RX_ID 1
35+
36+
static int aq_vec_poll(struct napi_struct *napi, int budget)
37+
__releases(&self->lock)
38+
__acquires(&self->lock)
39+
{
40+
struct aq_vec_s *self = container_of(napi, struct aq_vec_s, napi);
41+
struct aq_ring_s *ring = NULL;
42+
int work_done = 0;
43+
int err = 0;
44+
unsigned int i = 0U;
45+
unsigned int sw_tail_old = 0U;
46+
bool was_tx_cleaned = false;
47+
48+
if (!self) {
49+
err = -EINVAL;
50+
} else if (spin_trylock(&self->header.lock)) {
51+
for (i = 0U, ring = self->ring[0];
52+
self->tx_rings > i; ++i, ring = self->ring[i]) {
53+
if (self->aq_hw_ops->hw_ring_tx_head_update) {
54+
err = self->aq_hw_ops->hw_ring_tx_head_update(
55+
self->aq_hw,
56+
&ring[AQ_VEC_TX_ID]);
57+
if (err < 0)
58+
goto err_exit;
59+
}
60+
61+
if (ring[AQ_VEC_TX_ID].sw_head !=
62+
ring[AQ_VEC_TX_ID].hw_head) {
63+
err = aq_ring_tx_clean(&ring[AQ_VEC_TX_ID]);
64+
if (err < 0)
65+
goto err_exit;
66+
was_tx_cleaned = true;
67+
}
68+
69+
err = self->aq_hw_ops->hw_ring_rx_receive(self->aq_hw,
70+
&ring[AQ_VEC_RX_ID]);
71+
if (err < 0)
72+
goto err_exit;
73+
74+
if (ring[AQ_VEC_RX_ID].sw_head !=
75+
ring[AQ_VEC_RX_ID].hw_head) {
76+
err = aq_ring_rx_clean(&ring[AQ_VEC_RX_ID],
77+
&work_done,
78+
budget - work_done);
79+
if (err < 0)
80+
goto err_exit;
81+
82+
sw_tail_old = ring[AQ_VEC_RX_ID].sw_tail;
83+
84+
err = aq_ring_rx_fill(&ring[AQ_VEC_RX_ID]);
85+
if (err < 0)
86+
goto err_exit;
87+
88+
err = self->aq_hw_ops->hw_ring_rx_fill(
89+
self->aq_hw,
90+
&ring[AQ_VEC_RX_ID], sw_tail_old);
91+
if (err < 0)
92+
goto err_exit;
93+
}
94+
}
95+
96+
if (was_tx_cleaned)
97+
work_done = budget;
98+
99+
if (work_done < budget) {
100+
napi_complete(napi);
101+
self->aq_hw_ops->hw_irq_enable(self->aq_hw,
102+
1U << self->aq_ring_param.vec_idx);
103+
}
104+
105+
err_exit:
106+
spin_unlock(&self->header.lock);
107+
}
108+
109+
return work_done;
110+
}
111+
112+
struct aq_vec_s *aq_vec_alloc(struct aq_nic_s *aq_nic, unsigned int idx,
113+
struct aq_nic_cfg_s *aq_nic_cfg)
114+
{
115+
struct aq_vec_s *self = NULL;
116+
struct aq_ring_s *ring = NULL;
117+
unsigned int i = 0U;
118+
int err = 0;
119+
120+
self = kzalloc(sizeof(*self), GFP_KERNEL);
121+
if (!self) {
122+
err = -ENOMEM;
123+
goto err_exit;
124+
}
125+
126+
self->aq_nic = aq_nic;
127+
self->aq_ring_param.vec_idx = idx;
128+
self->aq_ring_param.cpu =
129+
idx + aq_nic_cfg->aq_rss.base_cpu_number;
130+
131+
cpumask_set_cpu(self->aq_ring_param.cpu,
132+
&self->aq_ring_param.affinity_mask);
133+
134+
self->tx_rings = 0;
135+
self->rx_rings = 0;
136+
137+
netif_napi_add(aq_nic_get_ndev(aq_nic), &self->napi,
138+
aq_vec_poll, AQ_CFG_NAPI_WEIGHT);
139+
140+
for (i = 0; i < aq_nic_cfg->tcs; ++i) {
141+
unsigned int idx_ring = AQ_NIC_TCVEC2RING(self->nic,
142+
self->tx_rings,
143+
self->aq_ring_param.vec_idx);
144+
145+
ring = aq_ring_tx_alloc(&self->ring[i][AQ_VEC_TX_ID], aq_nic,
146+
idx_ring, aq_nic_cfg);
147+
if (!ring) {
148+
err = -ENOMEM;
149+
goto err_exit;
150+
}
151+
152+
++self->tx_rings;
153+
154+
aq_nic_set_tx_ring(aq_nic, idx_ring, ring);
155+
156+
ring = aq_ring_rx_alloc(&self->ring[i][AQ_VEC_RX_ID], aq_nic,
157+
idx_ring, aq_nic_cfg);
158+
if (!ring) {
159+
err = -ENOMEM;
160+
goto err_exit;
161+
}
162+
163+
++self->rx_rings;
164+
}
165+
166+
err_exit:
167+
if (err < 0) {
168+
aq_vec_free(self);
169+
self = NULL;
170+
}
171+
return self;
172+
}
173+
174+
int aq_vec_init(struct aq_vec_s *self, struct aq_hw_ops *aq_hw_ops,
175+
struct aq_hw_s *aq_hw)
176+
{
177+
struct aq_ring_s *ring = NULL;
178+
unsigned int i = 0U;
179+
int err = 0;
180+
181+
self->aq_hw_ops = aq_hw_ops;
182+
self->aq_hw = aq_hw;
183+
184+
spin_lock_init(&self->header.lock);
185+
186+
for (i = 0U, ring = self->ring[0];
187+
self->tx_rings > i; ++i, ring = self->ring[i]) {
188+
err = aq_ring_init(&ring[AQ_VEC_TX_ID]);
189+
if (err < 0)
190+
goto err_exit;
191+
192+
err = self->aq_hw_ops->hw_ring_tx_init(self->aq_hw,
193+
&ring[AQ_VEC_TX_ID],
194+
&self->aq_ring_param);
195+
if (err < 0)
196+
goto err_exit;
197+
198+
err = aq_ring_init(&ring[AQ_VEC_RX_ID]);
199+
if (err < 0)
200+
goto err_exit;
201+
202+
err = self->aq_hw_ops->hw_ring_rx_init(self->aq_hw,
203+
&ring[AQ_VEC_RX_ID],
204+
&self->aq_ring_param);
205+
if (err < 0)
206+
goto err_exit;
207+
208+
err = aq_ring_rx_fill(&ring[AQ_VEC_RX_ID]);
209+
if (err < 0)
210+
goto err_exit;
211+
212+
err = self->aq_hw_ops->hw_ring_rx_fill(self->aq_hw,
213+
&ring[AQ_VEC_RX_ID], 0U);
214+
if (err < 0)
215+
goto err_exit;
216+
}
217+
218+
err_exit:
219+
return err;
220+
}
221+
222+
int aq_vec_start(struct aq_vec_s *self)
223+
{
224+
struct aq_ring_s *ring = NULL;
225+
unsigned int i = 0U;
226+
int err = 0;
227+
228+
for (i = 0U, ring = self->ring[0];
229+
self->tx_rings > i; ++i, ring = self->ring[i]) {
230+
err = self->aq_hw_ops->hw_ring_tx_start(self->aq_hw,
231+
&ring[AQ_VEC_TX_ID]);
232+
if (err < 0)
233+
goto err_exit;
234+
235+
err = self->aq_hw_ops->hw_ring_rx_start(self->aq_hw,
236+
&ring[AQ_VEC_RX_ID]);
237+
if (err < 0)
238+
goto err_exit;
239+
}
240+
241+
napi_enable(&self->napi);
242+
243+
err_exit:
244+
return err;
245+
}
246+
247+
void aq_vec_stop(struct aq_vec_s *self)
248+
{
249+
struct aq_ring_s *ring = NULL;
250+
unsigned int i = 0U;
251+
252+
for (i = 0U, ring = self->ring[0];
253+
self->tx_rings > i; ++i, ring = self->ring[i]) {
254+
self->aq_hw_ops->hw_ring_tx_stop(self->aq_hw,
255+
&ring[AQ_VEC_TX_ID]);
256+
257+
self->aq_hw_ops->hw_ring_rx_stop(self->aq_hw,
258+
&ring[AQ_VEC_RX_ID]);
259+
}
260+
261+
napi_disable(&self->napi);
262+
}
263+
264+
void aq_vec_deinit(struct aq_vec_s *self)
265+
{
266+
struct aq_ring_s *ring = NULL;
267+
unsigned int i = 0U;
268+
269+
if (!self)
270+
goto err_exit;
271+
272+
for (i = 0U, ring = self->ring[0];
273+
self->tx_rings > i; ++i, ring = self->ring[i]) {
274+
aq_ring_tx_deinit(&ring[AQ_VEC_TX_ID]);
275+
aq_ring_rx_deinit(&ring[AQ_VEC_RX_ID]);
276+
}
277+
err_exit:;
278+
}
279+
280+
void aq_vec_free(struct aq_vec_s *self)
281+
{
282+
struct aq_ring_s *ring = NULL;
283+
unsigned int i = 0U;
284+
285+
if (!self)
286+
goto err_exit;
287+
288+
for (i = 0U, ring = self->ring[0];
289+
self->tx_rings > i; ++i, ring = self->ring[i]) {
290+
aq_ring_free(&ring[AQ_VEC_TX_ID]);
291+
aq_ring_free(&ring[AQ_VEC_RX_ID]);
292+
}
293+
294+
netif_napi_del(&self->napi);
295+
296+
kfree(self);
297+
298+
err_exit:;
299+
}
300+
301+
irqreturn_t aq_vec_isr(int irq, void *private)
302+
{
303+
struct aq_vec_s *self = private;
304+
int err = 0;
305+
306+
if (!self) {
307+
err = -EINVAL;
308+
goto err_exit;
309+
}
310+
napi_schedule(&self->napi);
311+
312+
err_exit:
313+
return err >= 0 ? IRQ_HANDLED : IRQ_NONE;
314+
}
315+
316+
irqreturn_t aq_vec_isr_legacy(int irq, void *private)
317+
{
318+
struct aq_vec_s *self = private;
319+
u64 irq_mask = 0U;
320+
irqreturn_t err = 0;
321+
322+
if (!self) {
323+
err = -EINVAL;
324+
goto err_exit;
325+
}
326+
err = self->aq_hw_ops->hw_irq_read(self->aq_hw, &irq_mask);
327+
if (err < 0)
328+
goto err_exit;
329+
330+
if (irq_mask) {
331+
self->aq_hw_ops->hw_irq_disable(self->aq_hw,
332+
1U << self->aq_ring_param.vec_idx);
333+
napi_schedule(&self->napi);
334+
} else {
335+
self->aq_hw_ops->hw_irq_enable(self->aq_hw, 1U);
336+
err = IRQ_NONE;
337+
}
338+
339+
err_exit:
340+
return err >= 0 ? IRQ_HANDLED : IRQ_NONE;
341+
}
342+
343+
cpumask_t *aq_vec_get_affinity_mask(struct aq_vec_s *self)
344+
{
345+
return &self->aq_ring_param.affinity_mask;
346+
}
347+
348+
void aq_vec_add_stats(struct aq_vec_s *self,
349+
struct aq_ring_stats_rx_s *stats_rx,
350+
struct aq_ring_stats_tx_s *stats_tx)
351+
{
352+
struct aq_ring_s *ring = NULL;
353+
unsigned int r = 0U;
354+
355+
for (r = 0U, ring = self->ring[0];
356+
self->tx_rings > r; ++r, ring = self->ring[r]) {
357+
struct aq_ring_stats_tx_s *tx = &ring[AQ_VEC_TX_ID].stats.tx;
358+
struct aq_ring_stats_rx_s *rx = &ring[AQ_VEC_RX_ID].stats.rx;
359+
360+
stats_rx->packets += rx->packets;
361+
stats_rx->bytes += rx->bytes;
362+
stats_rx->errors += rx->errors;
363+
stats_rx->jumbo_packets += rx->jumbo_packets;
364+
stats_rx->lro_packets += rx->lro_packets;
365+
366+
stats_tx->packets += tx->packets;
367+
stats_tx->bytes += tx->bytes;
368+
stats_tx->errors += tx->errors;
369+
}
370+
}
371+
372+
int aq_vec_get_sw_stats(struct aq_vec_s *self, u64 *data, unsigned int *p_count)
373+
{
374+
unsigned int count = 0U;
375+
struct aq_ring_stats_rx_s stats_rx;
376+
struct aq_ring_stats_tx_s stats_tx;
377+
378+
memset(&stats_rx, 0U, sizeof(struct aq_ring_stats_rx_s));
379+
memset(&stats_tx, 0U, sizeof(struct aq_ring_stats_tx_s));
380+
aq_vec_add_stats(self, &stats_rx, &stats_tx);
381+
382+
data[count] += stats_rx.packets;
383+
data[++count] += stats_tx.packets;
384+
data[++count] += stats_rx.jumbo_packets;
385+
data[++count] += stats_rx.lro_packets;
386+
data[++count] += stats_rx.errors;
387+
388+
if (p_count)
389+
*p_count = ++count;
390+
391+
return 0;
392+
}

0 commit comments

Comments
 (0)