2 * aQuantia Corporation Network Driver
3 * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
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.
10 /* File aq_ring.c: Definition of functions for Rx/Tx rings. */
15 #include "aq_hw_utils.h"
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
20 static inline void aq_free_rxpage(struct aq_rxpage
*rxpage
, struct device
*dev
)
22 unsigned int len
= PAGE_SIZE
<< rxpage
->order
;
24 dma_unmap_page(dev
, rxpage
->daddr
, len
, DMA_FROM_DEVICE
);
26 /* Drop the ref for being in the ring. */
27 __free_pages(rxpage
->page
, rxpage
->order
);
31 static int aq_get_rxpage(struct aq_rxpage
*rxpage
, unsigned int order
,
38 page
= dev_alloc_pages(order
);
42 daddr
= dma_map_page(dev
, page
, 0, PAGE_SIZE
<< order
,
45 if (unlikely(dma_mapping_error(dev
, daddr
)))
49 rxpage
->daddr
= daddr
;
50 rxpage
->order
= order
;
56 __free_pages(page
, order
);
62 static int aq_get_rxpages(struct aq_ring_s
*self
, struct aq_ring_buff_s
*rxbuf
,
67 if (rxbuf
->rxdata
.page
) {
68 /* One means ring is the only user and can reuse */
69 if (page_ref_count(rxbuf
->rxdata
.page
) > 1) {
70 /* Try reuse buffer */
71 rxbuf
->rxdata
.pg_off
+= AQ_CFG_RX_FRAME_MAX
;
72 if (rxbuf
->rxdata
.pg_off
+ AQ_CFG_RX_FRAME_MAX
<=
73 (PAGE_SIZE
<< order
)) {
74 self
->stats
.rx
.pg_flips
++;
76 /* Buffer exhausted. We have other users and
77 * should release this page and realloc
79 aq_free_rxpage(&rxbuf
->rxdata
,
80 aq_nic_get_dev(self
->aq_nic
));
81 self
->stats
.rx
.pg_losts
++;
84 rxbuf
->rxdata
.pg_off
= 0;
85 self
->stats
.rx
.pg_reuses
++;
89 if (!rxbuf
->rxdata
.page
) {
90 ret
= aq_get_rxpage(&rxbuf
->rxdata
, order
,
91 aq_nic_get_dev(self
->aq_nic
));
98 static struct aq_ring_s
*aq_ring_alloc(struct aq_ring_s
*self
,
99 struct aq_nic_s
*aq_nic
)
104 kcalloc(self
->size
, sizeof(struct aq_ring_buff_s
), GFP_KERNEL
);
106 if (!self
->buff_ring
) {
110 self
->dx_ring
= dma_alloc_coherent(aq_nic_get_dev(aq_nic
),
111 self
->size
* self
->dx_size
,
112 &self
->dx_ring_pa
, GFP_KERNEL
);
113 if (!self
->dx_ring
) {
126 struct aq_ring_s
*aq_ring_tx_alloc(struct aq_ring_s
*self
,
127 struct aq_nic_s
*aq_nic
,
129 struct aq_nic_cfg_s
*aq_nic_cfg
)
133 self
->aq_nic
= aq_nic
;
135 self
->size
= aq_nic_cfg
->txds
;
136 self
->dx_size
= aq_nic_cfg
->aq_hw_caps
->txd_size
;
138 self
= aq_ring_alloc(self
, aq_nic
);
152 struct aq_ring_s
*aq_ring_rx_alloc(struct aq_ring_s
*self
,
153 struct aq_nic_s
*aq_nic
,
155 struct aq_nic_cfg_s
*aq_nic_cfg
)
159 self
->aq_nic
= aq_nic
;
161 self
->size
= aq_nic_cfg
->rxds
;
162 self
->dx_size
= aq_nic_cfg
->aq_hw_caps
->rxd_size
;
163 self
->page_order
= fls(AQ_CFG_RX_FRAME_MAX
/ PAGE_SIZE
+
164 (AQ_CFG_RX_FRAME_MAX
% PAGE_SIZE
? 1 : 0)) - 1;
166 if (aq_nic_cfg
->rxpageorder
> self
->page_order
)
167 self
->page_order
= aq_nic_cfg
->rxpageorder
;
169 self
= aq_ring_alloc(self
, aq_nic
);
183 int aq_ring_init(struct aq_ring_s
*self
)
191 static inline bool aq_ring_dx_in_range(unsigned int h
, unsigned int i
,
194 return (h
< t
) ? ((h
< i
) && (i
< t
)) : ((h
< i
) || (i
< t
));
197 void aq_ring_update_queue_state(struct aq_ring_s
*ring
)
199 if (aq_ring_avail_dx(ring
) <= AQ_CFG_SKB_FRAGS_MAX
)
200 aq_ring_queue_stop(ring
);
201 else if (aq_ring_avail_dx(ring
) > AQ_CFG_RESTART_DESC_THRES
)
202 aq_ring_queue_wake(ring
);
205 void aq_ring_queue_wake(struct aq_ring_s
*ring
)
207 struct net_device
*ndev
= aq_nic_get_ndev(ring
->aq_nic
);
209 if (__netif_subqueue_stopped(ndev
, ring
->idx
)) {
210 netif_wake_subqueue(ndev
, ring
->idx
);
211 ring
->stats
.tx
.queue_restarts
++;
215 void aq_ring_queue_stop(struct aq_ring_s
*ring
)
217 struct net_device
*ndev
= aq_nic_get_ndev(ring
->aq_nic
);
219 if (!__netif_subqueue_stopped(ndev
, ring
->idx
))
220 netif_stop_subqueue(ndev
, ring
->idx
);
223 bool aq_ring_tx_clean(struct aq_ring_s
*self
)
225 struct device
*dev
= aq_nic_get_dev(self
->aq_nic
);
228 for (budget
= AQ_CFG_TX_CLEAN_BUDGET
;
229 budget
&& self
->sw_head
!= self
->hw_head
; budget
--) {
230 struct aq_ring_buff_s
*buff
= &self
->buff_ring
[self
->sw_head
];
232 if (likely(buff
->is_mapped
)) {
233 if (unlikely(buff
->is_sop
)) {
235 buff
->eop_index
!= 0xffffU
&&
236 (!aq_ring_dx_in_range(self
->sw_head
,
241 dma_unmap_single(dev
, buff
->pa
, buff
->len
,
244 dma_unmap_page(dev
, buff
->pa
, buff
->len
,
249 if (unlikely(buff
->is_eop
))
250 dev_kfree_skb_any(buff
->skb
);
253 buff
->eop_index
= 0xffffU
;
254 self
->sw_head
= aq_ring_next_dx(self
, self
->sw_head
);
260 static void aq_rx_checksum(struct aq_ring_s
*self
,
261 struct aq_ring_buff_s
*buff
,
264 if (!(self
->aq_nic
->ndev
->features
& NETIF_F_RXCSUM
))
267 if (unlikely(buff
->is_cso_err
)) {
268 ++self
->stats
.rx
.errors
;
269 skb
->ip_summed
= CHECKSUM_NONE
;
272 if (buff
->is_ip_cso
) {
273 __skb_incr_checksum_unnecessary(skb
);
275 skb
->ip_summed
= CHECKSUM_NONE
;
278 if (buff
->is_udp_cso
|| buff
->is_tcp_cso
)
279 __skb_incr_checksum_unnecessary(skb
);
282 #define AQ_SKB_ALIGN SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
283 int aq_ring_rx_clean(struct aq_ring_s
*self
,
284 struct napi_struct
*napi
,
288 struct net_device
*ndev
= aq_nic_get_ndev(self
->aq_nic
);
289 bool is_rsc_completed
= true;
292 for (; (self
->sw_head
!= self
->hw_head
) && budget
;
293 self
->sw_head
= aq_ring_next_dx(self
, self
->sw_head
),
294 --budget
, ++(*work_done
)) {
295 struct aq_ring_buff_s
*buff
= &self
->buff_ring
[self
->sw_head
];
296 struct aq_ring_buff_s
*buff_
= NULL
;
297 struct sk_buff
*skb
= NULL
;
298 unsigned int next_
= 0U;
302 if (buff
->is_cleaned
)
309 buff_
= &self
->buff_ring
[next_
];
311 aq_ring_dx_in_range(self
->sw_head
,
315 if (unlikely(!is_rsc_completed
))
318 buff
->is_error
|= buff_
->is_error
;
320 } while (!buff_
->is_eop
);
322 if (!is_rsc_completed
) {
326 if (buff
->is_error
) {
330 buff_
= &self
->buff_ring
[next_
];
332 buff_
->is_cleaned
= true;
333 } while (!buff_
->is_eop
);
335 ++self
->stats
.rx
.errors
;
340 if (buff
->is_error
) {
341 ++self
->stats
.rx
.errors
;
345 dma_sync_single_range_for_cpu(aq_nic_get_dev(self
->aq_nic
),
348 buff
->len
, DMA_FROM_DEVICE
);
350 /* for single fragment packets use build_skb() */
352 buff
->len
<= AQ_CFG_RX_FRAME_MAX
- AQ_SKB_ALIGN
) {
353 skb
= build_skb(aq_buf_vaddr(&buff
->rxdata
),
354 AQ_CFG_RX_FRAME_MAX
);
355 if (unlikely(!skb
)) {
359 skb_put(skb
, buff
->len
);
360 page_ref_inc(buff
->rxdata
.page
);
362 skb
= napi_alloc_skb(napi
, AQ_CFG_RX_HDR_SIZE
);
363 if (unlikely(!skb
)) {
369 if (hdr_len
> AQ_CFG_RX_HDR_SIZE
)
370 hdr_len
= eth_get_headlen(skb
->dev
,
371 aq_buf_vaddr(&buff
->rxdata
),
374 memcpy(__skb_put(skb
, hdr_len
), aq_buf_vaddr(&buff
->rxdata
),
375 ALIGN(hdr_len
, sizeof(long)));
377 if (buff
->len
- hdr_len
> 0) {
378 skb_add_rx_frag(skb
, 0, buff
->rxdata
.page
,
379 buff
->rxdata
.pg_off
+ hdr_len
,
381 AQ_CFG_RX_FRAME_MAX
);
382 page_ref_inc(buff
->rxdata
.page
);
390 buff_
= &self
->buff_ring
[next_
];
392 dma_sync_single_range_for_cpu(
393 aq_nic_get_dev(self
->aq_nic
),
395 buff_
->rxdata
.pg_off
,
398 skb_add_rx_frag(skb
, i
++,
400 buff_
->rxdata
.pg_off
,
402 AQ_CFG_RX_FRAME_MAX
);
403 page_ref_inc(buff_
->rxdata
.page
);
404 buff_
->is_cleaned
= 1;
406 buff
->is_ip_cso
&= buff_
->is_ip_cso
;
407 buff
->is_udp_cso
&= buff_
->is_udp_cso
;
408 buff
->is_tcp_cso
&= buff_
->is_tcp_cso
;
409 buff
->is_cso_err
|= buff_
->is_cso_err
;
411 } while (!buff_
->is_eop
);
415 skb
->protocol
= eth_type_trans(skb
, ndev
);
417 aq_rx_checksum(self
, buff
, skb
);
419 skb_set_hash(skb
, buff
->rss_hash
,
420 buff
->is_hash_l4
? PKT_HASH_TYPE_L4
:
423 skb_record_rx_queue(skb
, self
->idx
);
425 ++self
->stats
.rx
.packets
;
426 self
->stats
.rx
.bytes
+= skb
->len
;
428 napi_gro_receive(napi
, skb
);
435 int aq_ring_rx_fill(struct aq_ring_s
*self
)
437 unsigned int page_order
= self
->page_order
;
438 struct aq_ring_buff_s
*buff
= NULL
;
442 if (aq_ring_avail_dx(self
) < min_t(unsigned int, AQ_CFG_RX_REFILL_THRES
,
446 for (i
= aq_ring_avail_dx(self
); i
--;
447 self
->sw_tail
= aq_ring_next_dx(self
, self
->sw_tail
)) {
448 buff
= &self
->buff_ring
[self
->sw_tail
];
451 buff
->len
= AQ_CFG_RX_FRAME_MAX
;
453 err
= aq_get_rxpages(self
, buff
, page_order
);
457 buff
->pa
= aq_buf_daddr(&buff
->rxdata
);
465 void aq_ring_rx_deinit(struct aq_ring_s
*self
)
470 for (; self
->sw_head
!= self
->sw_tail
;
471 self
->sw_head
= aq_ring_next_dx(self
, self
->sw_head
)) {
472 struct aq_ring_buff_s
*buff
= &self
->buff_ring
[self
->sw_head
];
474 aq_free_rxpage(&buff
->rxdata
, aq_nic_get_dev(self
->aq_nic
));
480 void aq_ring_free(struct aq_ring_s
*self
)
485 kfree(self
->buff_ring
);
488 dma_free_coherent(aq_nic_get_dev(self
->aq_nic
),
489 self
->size
* self
->dx_size
, self
->dx_ring
,