xref: /linux/drivers/net/ethernet/stmicro/stmmac/ring_mode.c (revision ab520be8cd5d56867fc95cfbc34b90880faf1f9d)
1 /*******************************************************************************
2   Specialised functions for managing Ring mode
3 
4   Copyright(C) 2011  STMicroelectronics Ltd
5 
6   It defines all the functions used to handle the normal/enhanced
7   descriptors in case of the DMA is configured to work in chained or
8   in ring mode.
9 
10   This program is free software; you can redistribute it and/or modify it
11   under the terms and conditions of the GNU General Public License,
12   version 2, as published by the Free Software Foundation.
13 
14   This program is distributed in the hope it will be useful, but WITHOUT
15   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17   more details.
18 
19   You should have received a copy of the GNU General Public License along with
20   this program; if not, write to the Free Software Foundation, Inc.,
21   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 
23   The full GNU General Public License is included in this distribution in
24   the file called "COPYING".
25 
26   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
27 *******************************************************************************/
28 
29 #include "stmmac.h"
30 
31 static int stmmac_jumbo_frm(void *p, struct sk_buff *skb, int csum)
32 {
33 	struct stmmac_priv *priv = (struct stmmac_priv *)p;
34 	unsigned int entry = priv->cur_tx;
35 	struct dma_desc *desc;
36 	unsigned int nopaged_len = skb_headlen(skb);
37 	unsigned int bmax, len, des2;
38 
39 	if (priv->extend_desc)
40 		desc = (struct dma_desc *)(priv->dma_etx + entry);
41 	else
42 		desc = priv->dma_tx + entry;
43 
44 	if (priv->plat->enh_desc)
45 		bmax = BUF_SIZE_8KiB;
46 	else
47 		bmax = BUF_SIZE_2KiB;
48 
49 	len = nopaged_len - bmax;
50 
51 	if (nopaged_len > BUF_SIZE_8KiB) {
52 
53 		des2 = dma_map_single(priv->device, skb->data, bmax,
54 				      DMA_TO_DEVICE);
55 		desc->des2 = cpu_to_le32(des2);
56 		if (dma_mapping_error(priv->device, des2))
57 			return -1;
58 
59 		priv->tx_skbuff_dma[entry].buf = des2;
60 		priv->tx_skbuff_dma[entry].len = bmax;
61 		priv->tx_skbuff_dma[entry].is_jumbo = true;
62 
63 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
64 		priv->hw->desc->prepare_tx_desc(desc, 1, bmax, csum,
65 						STMMAC_RING_MODE, 0, false);
66 		priv->tx_skbuff[entry] = NULL;
67 		entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
68 
69 		if (priv->extend_desc)
70 			desc = (struct dma_desc *)(priv->dma_etx + entry);
71 		else
72 			desc = priv->dma_tx + entry;
73 
74 		des2 = dma_map_single(priv->device, skb->data + bmax, len,
75 				      DMA_TO_DEVICE);
76 		desc->des2 = cpu_to_le32(des2);
77 		if (dma_mapping_error(priv->device, des2))
78 			return -1;
79 		priv->tx_skbuff_dma[entry].buf = des2;
80 		priv->tx_skbuff_dma[entry].len = len;
81 		priv->tx_skbuff_dma[entry].is_jumbo = true;
82 
83 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
84 		priv->hw->desc->prepare_tx_desc(desc, 0, len, csum,
85 						STMMAC_RING_MODE, 1, true);
86 	} else {
87 		des2 = dma_map_single(priv->device, skb->data,
88 				      nopaged_len, DMA_TO_DEVICE);
89 		desc->des2 = cpu_to_le32(des2);
90 		if (dma_mapping_error(priv->device, des2))
91 			return -1;
92 		priv->tx_skbuff_dma[entry].buf = des2;
93 		priv->tx_skbuff_dma[entry].len = nopaged_len;
94 		priv->tx_skbuff_dma[entry].is_jumbo = true;
95 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
96 		priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len, csum,
97 						STMMAC_RING_MODE, 0, true);
98 	}
99 
100 	priv->cur_tx = entry;
101 
102 	return entry;
103 }
104 
105 static unsigned int stmmac_is_jumbo_frm(int len, int enh_desc)
106 {
107 	unsigned int ret = 0;
108 
109 	if (len >= BUF_SIZE_4KiB)
110 		ret = 1;
111 
112 	return ret;
113 }
114 
115 static void stmmac_refill_desc3(void *priv_ptr, struct dma_desc *p)
116 {
117 	struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
118 
119 	/* Fill DES3 in case of RING mode */
120 	if (priv->dma_buf_sz >= BUF_SIZE_8KiB)
121 		p->des3 = cpu_to_le32(le32_to_cpu(p->des2) + BUF_SIZE_8KiB);
122 }
123 
124 /* In ring mode we need to fill the desc3 because it is used as buffer */
125 static void stmmac_init_desc3(struct dma_desc *p)
126 {
127 	p->des3 = cpu_to_le32(le32_to_cpu(p->des2) + BUF_SIZE_8KiB);
128 }
129 
130 static void stmmac_clean_desc3(void *priv_ptr, struct dma_desc *p)
131 {
132 	struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
133 	unsigned int entry = priv->dirty_tx;
134 
135 	/* des3 is only used for jumbo frames tx or time stamping */
136 	if (unlikely(priv->tx_skbuff_dma[entry].is_jumbo ||
137 		     (priv->tx_skbuff_dma[entry].last_segment &&
138 		      !priv->extend_desc && priv->hwts_tx_en)))
139 		p->des3 = 0;
140 }
141 
142 static int stmmac_set_16kib_bfsize(int mtu)
143 {
144 	int ret = 0;
145 	if (unlikely(mtu >= BUF_SIZE_8KiB))
146 		ret = BUF_SIZE_16KiB;
147 	return ret;
148 }
149 
150 const struct stmmac_mode_ops ring_mode_ops = {
151 	.is_jumbo_frm = stmmac_is_jumbo_frm,
152 	.jumbo_frm = stmmac_jumbo_frm,
153 	.refill_desc3 = stmmac_refill_desc3,
154 	.init_desc3 = stmmac_init_desc3,
155 	.clean_desc3 = stmmac_clean_desc3,
156 	.set_16kib_bfsize = stmmac_set_16kib_bfsize,
157 };
158