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   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21 
22   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
24 
25 #include "stmmac.h"
26 
27 static int stmmac_jumbo_frm(void *p, struct sk_buff *skb, int csum)
28 {
29 	struct stmmac_priv *priv = (struct stmmac_priv *)p;
30 	unsigned int entry = priv->cur_tx;
31 	struct dma_desc *desc;
32 	unsigned int nopaged_len = skb_headlen(skb);
33 	unsigned int bmax, len, des2;
34 
35 	if (priv->extend_desc)
36 		desc = (struct dma_desc *)(priv->dma_etx + entry);
37 	else
38 		desc = priv->dma_tx + entry;
39 
40 	if (priv->plat->enh_desc)
41 		bmax = BUF_SIZE_8KiB;
42 	else
43 		bmax = BUF_SIZE_2KiB;
44 
45 	len = nopaged_len - bmax;
46 
47 	if (nopaged_len > BUF_SIZE_8KiB) {
48 
49 		des2 = dma_map_single(priv->device, skb->data, bmax,
50 				      DMA_TO_DEVICE);
51 		desc->des2 = cpu_to_le32(des2);
52 		if (dma_mapping_error(priv->device, des2))
53 			return -1;
54 
55 		priv->tx_skbuff_dma[entry].buf = des2;
56 		priv->tx_skbuff_dma[entry].len = bmax;
57 		priv->tx_skbuff_dma[entry].is_jumbo = true;
58 
59 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
60 		priv->hw->desc->prepare_tx_desc(desc, 1, bmax, csum,
61 						STMMAC_RING_MODE, 0, false);
62 		priv->tx_skbuff[entry] = NULL;
63 		entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
64 
65 		if (priv->extend_desc)
66 			desc = (struct dma_desc *)(priv->dma_etx + entry);
67 		else
68 			desc = priv->dma_tx + entry;
69 
70 		des2 = dma_map_single(priv->device, skb->data + bmax, len,
71 				      DMA_TO_DEVICE);
72 		desc->des2 = cpu_to_le32(des2);
73 		if (dma_mapping_error(priv->device, des2))
74 			return -1;
75 		priv->tx_skbuff_dma[entry].buf = des2;
76 		priv->tx_skbuff_dma[entry].len = len;
77 		priv->tx_skbuff_dma[entry].is_jumbo = true;
78 
79 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
80 		priv->hw->desc->prepare_tx_desc(desc, 0, len, csum,
81 						STMMAC_RING_MODE, 1, true);
82 	} else {
83 		des2 = dma_map_single(priv->device, skb->data,
84 				      nopaged_len, DMA_TO_DEVICE);
85 		desc->des2 = cpu_to_le32(des2);
86 		if (dma_mapping_error(priv->device, des2))
87 			return -1;
88 		priv->tx_skbuff_dma[entry].buf = des2;
89 		priv->tx_skbuff_dma[entry].len = nopaged_len;
90 		priv->tx_skbuff_dma[entry].is_jumbo = true;
91 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
92 		priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len, csum,
93 						STMMAC_RING_MODE, 0, true);
94 	}
95 
96 	priv->cur_tx = entry;
97 
98 	return entry;
99 }
100 
101 static unsigned int stmmac_is_jumbo_frm(int len, int enh_desc)
102 {
103 	unsigned int ret = 0;
104 
105 	if (len >= BUF_SIZE_4KiB)
106 		ret = 1;
107 
108 	return ret;
109 }
110 
111 static void stmmac_refill_desc3(void *priv_ptr, struct dma_desc *p)
112 {
113 	struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
114 
115 	/* Fill DES3 in case of RING mode */
116 	if (priv->dma_buf_sz >= BUF_SIZE_8KiB)
117 		p->des3 = cpu_to_le32(le32_to_cpu(p->des2) + BUF_SIZE_8KiB);
118 }
119 
120 /* In ring mode we need to fill the desc3 because it is used as buffer */
121 static void stmmac_init_desc3(struct dma_desc *p)
122 {
123 	p->des3 = cpu_to_le32(le32_to_cpu(p->des2) + BUF_SIZE_8KiB);
124 }
125 
126 static void stmmac_clean_desc3(void *priv_ptr, struct dma_desc *p)
127 {
128 	struct stmmac_priv *priv = (struct stmmac_priv *)priv_ptr;
129 	unsigned int entry = priv->dirty_tx;
130 
131 	/* des3 is only used for jumbo frames tx or time stamping */
132 	if (unlikely(priv->tx_skbuff_dma[entry].is_jumbo ||
133 		     (priv->tx_skbuff_dma[entry].last_segment &&
134 		      !priv->extend_desc && priv->hwts_tx_en)))
135 		p->des3 = 0;
136 }
137 
138 static int stmmac_set_16kib_bfsize(int mtu)
139 {
140 	int ret = 0;
141 	if (unlikely(mtu >= BUF_SIZE_8KiB))
142 		ret = BUF_SIZE_16KiB;
143 	return ret;
144 }
145 
146 const struct stmmac_mode_ops ring_mode_ops = {
147 	.is_jumbo_frm = stmmac_is_jumbo_frm,
148 	.jumbo_frm = stmmac_jumbo_frm,
149 	.refill_desc3 = stmmac_refill_desc3,
150 	.init_desc3 = stmmac_init_desc3,
151 	.clean_desc3 = stmmac_clean_desc3,
152 	.set_16kib_bfsize = stmmac_set_16kib_bfsize,
153 };
154