gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / aquantia / atlantic / aq_ring.c
blobbae95a61856081afc83e36c43fd1b1bc59634d7a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * aQuantia Corporation Network Driver
4 * Copyright (C) 2014-2019 aQuantia Corporation. All rights reserved
5 */
7 /* File aq_ring.c: Definition of functions for Rx/Tx rings. */
9 #include "aq_ring.h"
10 #include "aq_nic.h"
11 #include "aq_hw.h"
12 #include "aq_hw_utils.h"
13 #include "aq_ptp.h"
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
18 static inline void aq_free_rxpage(struct aq_rxpage *rxpage, struct device *dev)
20 unsigned int len = PAGE_SIZE << rxpage->order;
22 dma_unmap_page(dev, rxpage->daddr, len, DMA_FROM_DEVICE);
24 /* Drop the ref for being in the ring. */
25 __free_pages(rxpage->page, rxpage->order);
26 rxpage->page = NULL;
29 static int aq_get_rxpage(struct aq_rxpage *rxpage, unsigned int order,
30 struct device *dev)
32 struct page *page;
33 int ret = -ENOMEM;
34 dma_addr_t daddr;
36 page = dev_alloc_pages(order);
37 if (unlikely(!page))
38 goto err_exit;
40 daddr = dma_map_page(dev, page, 0, PAGE_SIZE << order,
41 DMA_FROM_DEVICE);
43 if (unlikely(dma_mapping_error(dev, daddr)))
44 goto free_page;
46 rxpage->page = page;
47 rxpage->daddr = daddr;
48 rxpage->order = order;
49 rxpage->pg_off = 0;
51 return 0;
53 free_page:
54 __free_pages(page, order);
56 err_exit:
57 return ret;
60 static int aq_get_rxpages(struct aq_ring_s *self, struct aq_ring_buff_s *rxbuf,
61 int order)
63 int ret;
65 if (rxbuf->rxdata.page) {
66 /* One means ring is the only user and can reuse */
67 if (page_ref_count(rxbuf->rxdata.page) > 1) {
68 /* Try reuse buffer */
69 rxbuf->rxdata.pg_off += AQ_CFG_RX_FRAME_MAX;
70 if (rxbuf->rxdata.pg_off + AQ_CFG_RX_FRAME_MAX <=
71 (PAGE_SIZE << order)) {
72 self->stats.rx.pg_flips++;
73 } else {
74 /* Buffer exhausted. We have other users and
75 * should release this page and realloc
77 aq_free_rxpage(&rxbuf->rxdata,
78 aq_nic_get_dev(self->aq_nic));
79 self->stats.rx.pg_losts++;
81 } else {
82 rxbuf->rxdata.pg_off = 0;
83 self->stats.rx.pg_reuses++;
87 if (!rxbuf->rxdata.page) {
88 ret = aq_get_rxpage(&rxbuf->rxdata, order,
89 aq_nic_get_dev(self->aq_nic));
90 return ret;
93 return 0;
96 static struct aq_ring_s *aq_ring_alloc(struct aq_ring_s *self,
97 struct aq_nic_s *aq_nic)
99 int err = 0;
101 self->buff_ring =
102 kcalloc(self->size, sizeof(struct aq_ring_buff_s), GFP_KERNEL);
104 if (!self->buff_ring) {
105 err = -ENOMEM;
106 goto err_exit;
108 self->dx_ring = dma_alloc_coherent(aq_nic_get_dev(aq_nic),
109 self->size * self->dx_size,
110 &self->dx_ring_pa, GFP_KERNEL);
111 if (!self->dx_ring) {
112 err = -ENOMEM;
113 goto err_exit;
116 err_exit:
117 if (err < 0) {
118 aq_ring_free(self);
119 self = NULL;
122 return self;
125 struct aq_ring_s *aq_ring_tx_alloc(struct aq_ring_s *self,
126 struct aq_nic_s *aq_nic,
127 unsigned int idx,
128 struct aq_nic_cfg_s *aq_nic_cfg)
130 int err = 0;
132 self->aq_nic = aq_nic;
133 self->idx = idx;
134 self->size = aq_nic_cfg->txds;
135 self->dx_size = aq_nic_cfg->aq_hw_caps->txd_size;
137 self = aq_ring_alloc(self, aq_nic);
138 if (!self) {
139 err = -ENOMEM;
140 goto err_exit;
143 err_exit:
144 if (err < 0) {
145 aq_ring_free(self);
146 self = NULL;
149 return self;
152 struct aq_ring_s *aq_ring_rx_alloc(struct aq_ring_s *self,
153 struct aq_nic_s *aq_nic,
154 unsigned int idx,
155 struct aq_nic_cfg_s *aq_nic_cfg)
157 int err = 0;
159 self->aq_nic = aq_nic;
160 self->idx = idx;
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);
170 if (!self) {
171 err = -ENOMEM;
172 goto err_exit;
175 err_exit:
176 if (err < 0) {
177 aq_ring_free(self);
178 self = NULL;
181 return self;
184 struct aq_ring_s *
185 aq_ring_hwts_rx_alloc(struct aq_ring_s *self, struct aq_nic_s *aq_nic,
186 unsigned int idx, unsigned int size, unsigned int dx_size)
188 struct device *dev = aq_nic_get_dev(aq_nic);
189 size_t sz = size * dx_size + AQ_CFG_RXDS_DEF;
191 memset(self, 0, sizeof(*self));
193 self->aq_nic = aq_nic;
194 self->idx = idx;
195 self->size = size;
196 self->dx_size = dx_size;
198 self->dx_ring = dma_alloc_coherent(dev, sz, &self->dx_ring_pa,
199 GFP_KERNEL);
200 if (!self->dx_ring) {
201 aq_ring_free(self);
202 return NULL;
205 return self;
208 int aq_ring_init(struct aq_ring_s *self)
210 self->hw_head = 0;
211 self->sw_head = 0;
212 self->sw_tail = 0;
214 return 0;
217 static inline bool aq_ring_dx_in_range(unsigned int h, unsigned int i,
218 unsigned int t)
220 return (h < t) ? ((h < i) && (i < t)) : ((h < i) || (i < t));
223 void aq_ring_update_queue_state(struct aq_ring_s *ring)
225 if (aq_ring_avail_dx(ring) <= AQ_CFG_SKB_FRAGS_MAX)
226 aq_ring_queue_stop(ring);
227 else if (aq_ring_avail_dx(ring) > AQ_CFG_RESTART_DESC_THRES)
228 aq_ring_queue_wake(ring);
231 void aq_ring_queue_wake(struct aq_ring_s *ring)
233 struct net_device *ndev = aq_nic_get_ndev(ring->aq_nic);
235 if (__netif_subqueue_stopped(ndev, ring->idx)) {
236 netif_wake_subqueue(ndev, ring->idx);
237 ring->stats.tx.queue_restarts++;
241 void aq_ring_queue_stop(struct aq_ring_s *ring)
243 struct net_device *ndev = aq_nic_get_ndev(ring->aq_nic);
245 if (!__netif_subqueue_stopped(ndev, ring->idx))
246 netif_stop_subqueue(ndev, ring->idx);
249 bool aq_ring_tx_clean(struct aq_ring_s *self)
251 struct device *dev = aq_nic_get_dev(self->aq_nic);
252 unsigned int budget;
254 for (budget = AQ_CFG_TX_CLEAN_BUDGET;
255 budget && self->sw_head != self->hw_head; budget--) {
256 struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
258 if (likely(buff->is_mapped)) {
259 if (unlikely(buff->is_sop)) {
260 if (!buff->is_eop &&
261 buff->eop_index != 0xffffU &&
262 (!aq_ring_dx_in_range(self->sw_head,
263 buff->eop_index,
264 self->hw_head)))
265 break;
267 dma_unmap_single(dev, buff->pa, buff->len,
268 DMA_TO_DEVICE);
269 } else {
270 dma_unmap_page(dev, buff->pa, buff->len,
271 DMA_TO_DEVICE);
275 if (unlikely(buff->is_eop)) {
276 ++self->stats.rx.packets;
277 self->stats.tx.bytes += buff->skb->len;
279 dev_kfree_skb_any(buff->skb);
281 buff->pa = 0U;
282 buff->eop_index = 0xffffU;
283 self->sw_head = aq_ring_next_dx(self, self->sw_head);
286 return !!budget;
289 static void aq_rx_checksum(struct aq_ring_s *self,
290 struct aq_ring_buff_s *buff,
291 struct sk_buff *skb)
293 if (!(self->aq_nic->ndev->features & NETIF_F_RXCSUM))
294 return;
296 if (unlikely(buff->is_cso_err)) {
297 ++self->stats.rx.errors;
298 skb->ip_summed = CHECKSUM_NONE;
299 return;
301 if (buff->is_ip_cso) {
302 __skb_incr_checksum_unnecessary(skb);
303 } else {
304 skb->ip_summed = CHECKSUM_NONE;
307 if (buff->is_udp_cso || buff->is_tcp_cso)
308 __skb_incr_checksum_unnecessary(skb);
311 #define AQ_SKB_ALIGN SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
312 int aq_ring_rx_clean(struct aq_ring_s *self,
313 struct napi_struct *napi,
314 int *work_done,
315 int budget)
317 struct net_device *ndev = aq_nic_get_ndev(self->aq_nic);
318 bool is_rsc_completed = true;
319 int err = 0;
321 for (; (self->sw_head != self->hw_head) && budget;
322 self->sw_head = aq_ring_next_dx(self, self->sw_head),
323 --budget, ++(*work_done)) {
324 struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
325 bool is_ptp_ring = aq_ptp_ring(self->aq_nic, self);
326 struct aq_ring_buff_s *buff_ = NULL;
327 struct sk_buff *skb = NULL;
328 unsigned int next_ = 0U;
329 unsigned int i = 0U;
330 u16 hdr_len;
332 if (buff->is_cleaned)
333 continue;
335 if (!buff->is_eop) {
336 buff_ = buff;
337 do {
338 next_ = buff_->next,
339 buff_ = &self->buff_ring[next_];
340 is_rsc_completed =
341 aq_ring_dx_in_range(self->sw_head,
342 next_,
343 self->hw_head);
345 if (unlikely(!is_rsc_completed))
346 break;
348 buff->is_error |= buff_->is_error;
349 buff->is_cso_err |= buff_->is_cso_err;
351 } while (!buff_->is_eop);
353 if (!is_rsc_completed) {
354 err = 0;
355 goto err_exit;
357 if (buff->is_error ||
358 (buff->is_lro && buff->is_cso_err)) {
359 buff_ = buff;
360 do {
361 next_ = buff_->next,
362 buff_ = &self->buff_ring[next_];
364 buff_->is_cleaned = true;
365 } while (!buff_->is_eop);
367 ++self->stats.rx.errors;
368 continue;
372 if (buff->is_error) {
373 ++self->stats.rx.errors;
374 continue;
377 dma_sync_single_range_for_cpu(aq_nic_get_dev(self->aq_nic),
378 buff->rxdata.daddr,
379 buff->rxdata.pg_off,
380 buff->len, DMA_FROM_DEVICE);
382 /* for single fragment packets use build_skb() */
383 if (buff->is_eop &&
384 buff->len <= AQ_CFG_RX_FRAME_MAX - AQ_SKB_ALIGN) {
385 skb = build_skb(aq_buf_vaddr(&buff->rxdata),
386 AQ_CFG_RX_FRAME_MAX);
387 if (unlikely(!skb)) {
388 err = -ENOMEM;
389 goto err_exit;
391 if (is_ptp_ring)
392 buff->len -=
393 aq_ptp_extract_ts(self->aq_nic, skb,
394 aq_buf_vaddr(&buff->rxdata),
395 buff->len);
396 skb_put(skb, buff->len);
397 page_ref_inc(buff->rxdata.page);
398 } else {
399 skb = napi_alloc_skb(napi, AQ_CFG_RX_HDR_SIZE);
400 if (unlikely(!skb)) {
401 err = -ENOMEM;
402 goto err_exit;
404 if (is_ptp_ring)
405 buff->len -=
406 aq_ptp_extract_ts(self->aq_nic, skb,
407 aq_buf_vaddr(&buff->rxdata),
408 buff->len);
410 hdr_len = buff->len;
411 if (hdr_len > AQ_CFG_RX_HDR_SIZE)
412 hdr_len = eth_get_headlen(skb->dev,
413 aq_buf_vaddr(&buff->rxdata),
414 AQ_CFG_RX_HDR_SIZE);
416 memcpy(__skb_put(skb, hdr_len), aq_buf_vaddr(&buff->rxdata),
417 ALIGN(hdr_len, sizeof(long)));
419 if (buff->len - hdr_len > 0) {
420 skb_add_rx_frag(skb, 0, buff->rxdata.page,
421 buff->rxdata.pg_off + hdr_len,
422 buff->len - hdr_len,
423 AQ_CFG_RX_FRAME_MAX);
424 page_ref_inc(buff->rxdata.page);
427 if (!buff->is_eop) {
428 buff_ = buff;
429 i = 1U;
430 do {
431 next_ = buff_->next,
432 buff_ = &self->buff_ring[next_];
434 dma_sync_single_range_for_cpu(
435 aq_nic_get_dev(self->aq_nic),
436 buff_->rxdata.daddr,
437 buff_->rxdata.pg_off,
438 buff_->len,
439 DMA_FROM_DEVICE);
440 skb_add_rx_frag(skb, i++,
441 buff_->rxdata.page,
442 buff_->rxdata.pg_off,
443 buff_->len,
444 AQ_CFG_RX_FRAME_MAX);
445 page_ref_inc(buff_->rxdata.page);
446 buff_->is_cleaned = 1;
448 buff->is_ip_cso &= buff_->is_ip_cso;
449 buff->is_udp_cso &= buff_->is_udp_cso;
450 buff->is_tcp_cso &= buff_->is_tcp_cso;
451 buff->is_cso_err |= buff_->is_cso_err;
453 } while (!buff_->is_eop);
457 if (buff->is_vlan)
458 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
459 buff->vlan_rx_tag);
461 skb->protocol = eth_type_trans(skb, ndev);
463 aq_rx_checksum(self, buff, skb);
465 skb_set_hash(skb, buff->rss_hash,
466 buff->is_hash_l4 ? PKT_HASH_TYPE_L4 :
467 PKT_HASH_TYPE_NONE);
468 /* Send all PTP traffic to 0 queue */
469 skb_record_rx_queue(skb, is_ptp_ring ? 0 : self->idx);
471 ++self->stats.rx.packets;
472 self->stats.rx.bytes += skb->len;
474 napi_gro_receive(napi, skb);
477 err_exit:
478 return err;
481 void aq_ring_hwts_rx_clean(struct aq_ring_s *self, struct aq_nic_s *aq_nic)
483 while (self->sw_head != self->hw_head) {
484 u64 ns;
486 aq_nic->aq_hw_ops->extract_hwts(aq_nic->aq_hw,
487 self->dx_ring +
488 (self->sw_head * self->dx_size),
489 self->dx_size, &ns);
490 aq_ptp_tx_hwtstamp(aq_nic, ns);
492 self->sw_head = aq_ring_next_dx(self, self->sw_head);
496 int aq_ring_rx_fill(struct aq_ring_s *self)
498 unsigned int page_order = self->page_order;
499 struct aq_ring_buff_s *buff = NULL;
500 int err = 0;
501 int i = 0;
503 if (aq_ring_avail_dx(self) < min_t(unsigned int, AQ_CFG_RX_REFILL_THRES,
504 self->size / 2))
505 return err;
507 for (i = aq_ring_avail_dx(self); i--;
508 self->sw_tail = aq_ring_next_dx(self, self->sw_tail)) {
509 buff = &self->buff_ring[self->sw_tail];
511 buff->flags = 0U;
512 buff->len = AQ_CFG_RX_FRAME_MAX;
514 err = aq_get_rxpages(self, buff, page_order);
515 if (err)
516 goto err_exit;
518 buff->pa = aq_buf_daddr(&buff->rxdata);
519 buff = NULL;
522 err_exit:
523 return err;
526 void aq_ring_rx_deinit(struct aq_ring_s *self)
528 if (!self)
529 goto err_exit;
531 for (; self->sw_head != self->sw_tail;
532 self->sw_head = aq_ring_next_dx(self, self->sw_head)) {
533 struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
535 aq_free_rxpage(&buff->rxdata, aq_nic_get_dev(self->aq_nic));
538 err_exit:;
541 void aq_ring_free(struct aq_ring_s *self)
543 if (!self)
544 goto err_exit;
546 kfree(self->buff_ring);
548 if (self->dx_ring)
549 dma_free_coherent(aq_nic_get_dev(self->aq_nic),
550 self->size * self->dx_size, self->dx_ring,
551 self->dx_ring_pa);
553 err_exit:;