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;
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 		desc->des2 = dma_map_single(priv->device, skb->data,
54 					    bmax, DMA_TO_DEVICE);
55 		if (dma_mapping_error(priv->device, desc->des2))
56 			return -1;
57 
58 		priv->tx_skbuff_dma[entry].buf = desc->des2;
59 		priv->tx_skbuff_dma[entry].len = bmax;
60 		priv->tx_skbuff_dma[entry].is_jumbo = true;
61 
62 		desc->des3 = desc->des2 + BUF_SIZE_4KiB;
63 		priv->hw->desc->prepare_tx_desc(desc, 1, bmax, csum,
64 						STMMAC_RING_MODE, 0, false);
65 		priv->tx_skbuff[entry] = NULL;
66 		entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
67 
68 		if (priv->extend_desc)
69 			desc = (struct dma_desc *)(priv->dma_etx + entry);
70 		else
71 			desc = priv->dma_tx + entry;
72 
73 		desc->des2 = dma_map_single(priv->device, skb->data + bmax,
74 					    len, DMA_TO_DEVICE);
75 		if (dma_mapping_error(priv->device, desc->des2))
76 			return -1;
77 		priv->tx_skbuff_dma[entry].buf = desc->des2;
78 		priv->tx_skbuff_dma[entry].len = len;
79 		priv->tx_skbuff_dma[entry].is_jumbo = true;
80 
81 		desc->des3 = desc->des2 + BUF_SIZE_4KiB;
82 		priv->hw->desc->prepare_tx_desc(desc, 0, len, csum,
83 						STMMAC_RING_MODE, 1, true);
84 	} else {
85 		desc->des2 = dma_map_single(priv->device, skb->data,
86 					    nopaged_len, DMA_TO_DEVICE);
87 		if (dma_mapping_error(priv->device, desc->des2))
88 			return -1;
89 		priv->tx_skbuff_dma[entry].buf = desc->des2;
90 		priv->tx_skbuff_dma[entry].len = nopaged_len;
91 		priv->tx_skbuff_dma[entry].is_jumbo = true;
92 		desc->des3 = desc->des2 + BUF_SIZE_4KiB;
93 		priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len, csum,
94 						STMMAC_RING_MODE, 0, true);
95 	}
96 
97 	priv->cur_tx = entry;
98 
99 	return entry;
100 }
101 
102 static unsigned int stmmac_is_jumbo_frm(int len, int enh_desc)
103 {
104 	unsigned int ret = 0;
105 
106 	if (len >= BUF_SIZE_4KiB)
107 		ret = 1;
108 
109 	return ret;
110 }
111 
112 static void stmmac_refill_desc3(void *priv_ptr, struct dma_desc *p)
113 {
114 	struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
115 
116 	/* Fill DES3 in case of RING mode */
117 	if (priv->dma_buf_sz >= BUF_SIZE_8KiB)
118 		p->des3 = p->des2 + BUF_SIZE_8KiB;
119 }
120 
121 /* In ring mode we need to fill the desc3 because it is used as buffer */
122 static void stmmac_init_desc3(struct dma_desc *p)
123 {
124 	p->des3 = p->des2 + BUF_SIZE_8KiB;
125 }
126 
127 static void stmmac_clean_desc3(void *priv_ptr, struct dma_desc *p)
128 {
129 	struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
130 	unsigned int entry = priv->dirty_tx;
131 
132 	/* des3 is only used for jumbo frames tx or time stamping */
133 	if (unlikely(priv->tx_skbuff_dma[entry].is_jumbo ||
134 		     (priv->tx_skbuff_dma[entry].last_segment &&
135 		      !priv->extend_desc && priv->hwts_tx_en)))
136 		p->des3 = 0;
137 }
138 
139 static int stmmac_set_16kib_bfsize(int mtu)
140 {
141 	int ret = 0;
142 	if (unlikely(mtu >= BUF_SIZE_8KiB))
143 		ret = BUF_SIZE_16KiB;
144 	return ret;
145 }
146 
147 const struct stmmac_mode_ops ring_mode_ops = {
148 	.is_jumbo_frm = stmmac_is_jumbo_frm,
149 	.jumbo_frm = stmmac_jumbo_frm,
150 	.refill_desc3 = stmmac_refill_desc3,
151 	.init_desc3 = stmmac_init_desc3,
152 	.clean_desc3 = stmmac_clean_desc3,
153 	.set_16kib_bfsize = stmmac_set_16kib_bfsize,
154 };
155