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_tx_queue *tx_q = (struct stmmac_tx_queue *)p;
30 	unsigned int nopaged_len = skb_headlen(skb);
31 	struct stmmac_priv *priv = tx_q->priv_data;
32 	unsigned int entry = tx_q->cur_tx;
33 	unsigned int bmax, len, des2;
34 	struct dma_desc *desc;
35 
36 	if (priv->extend_desc)
37 		desc = (struct dma_desc *)(tx_q->dma_etx + entry);
38 	else
39 		desc = tx_q->dma_tx + entry;
40 
41 	if (priv->plat->enh_desc)
42 		bmax = BUF_SIZE_8KiB;
43 	else
44 		bmax = BUF_SIZE_2KiB;
45 
46 	len = nopaged_len - bmax;
47 
48 	if (nopaged_len > BUF_SIZE_8KiB) {
49 
50 		des2 = dma_map_single(priv->device, skb->data, bmax,
51 				      DMA_TO_DEVICE);
52 		desc->des2 = cpu_to_le32(des2);
53 		if (dma_mapping_error(priv->device, des2))
54 			return -1;
55 
56 		tx_q->tx_skbuff_dma[entry].buf = des2;
57 		tx_q->tx_skbuff_dma[entry].len = bmax;
58 		tx_q->tx_skbuff_dma[entry].is_jumbo = true;
59 
60 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
61 		priv->hw->desc->prepare_tx_desc(desc, 1, bmax, csum,
62 						STMMAC_RING_MODE, 0,
63 						false, skb->len);
64 		tx_q->tx_skbuff[entry] = NULL;
65 		entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
66 
67 		if (priv->extend_desc)
68 			desc = (struct dma_desc *)(tx_q->dma_etx + entry);
69 		else
70 			desc = tx_q->dma_tx + entry;
71 
72 		des2 = dma_map_single(priv->device, skb->data + bmax, len,
73 				      DMA_TO_DEVICE);
74 		desc->des2 = cpu_to_le32(des2);
75 		if (dma_mapping_error(priv->device, des2))
76 			return -1;
77 		tx_q->tx_skbuff_dma[entry].buf = des2;
78 		tx_q->tx_skbuff_dma[entry].len = len;
79 		tx_q->tx_skbuff_dma[entry].is_jumbo = true;
80 
81 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
82 		priv->hw->desc->prepare_tx_desc(desc, 0, len, csum,
83 						STMMAC_RING_MODE, 1,
84 						true, skb->len);
85 	} else {
86 		des2 = dma_map_single(priv->device, skb->data,
87 				      nopaged_len, DMA_TO_DEVICE);
88 		desc->des2 = cpu_to_le32(des2);
89 		if (dma_mapping_error(priv->device, des2))
90 			return -1;
91 		tx_q->tx_skbuff_dma[entry].buf = des2;
92 		tx_q->tx_skbuff_dma[entry].len = nopaged_len;
93 		tx_q->tx_skbuff_dma[entry].is_jumbo = true;
94 		desc->des3 = cpu_to_le32(des2 + BUF_SIZE_4KiB);
95 		priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len, csum,
96 						STMMAC_RING_MODE, 0,
97 						true, skb->len);
98 	}
99 
100 	tx_q->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_tx_queue *tx_q = (struct stmmac_tx_queue *)priv_ptr;
133 	struct stmmac_priv *priv = tx_q->priv_data;
134 	unsigned int entry = tx_q->dirty_tx;
135 
136 	/* des3 is only used for jumbo frames tx or time stamping */
137 	if (unlikely(tx_q->tx_skbuff_dma[entry].is_jumbo ||
138 		     (tx_q->tx_skbuff_dma[entry].last_segment &&
139 		      !priv->extend_desc && priv->hwts_tx_en)))
140 		p->des3 = 0;
141 }
142 
143 static int stmmac_set_16kib_bfsize(int mtu)
144 {
145 	int ret = 0;
146 	if (unlikely(mtu >= BUF_SIZE_8KiB))
147 		ret = BUF_SIZE_16KiB;
148 	return ret;
149 }
150 
151 const struct stmmac_mode_ops ring_mode_ops = {
152 	.is_jumbo_frm = stmmac_is_jumbo_frm,
153 	.jumbo_frm = stmmac_jumbo_frm,
154 	.refill_desc3 = stmmac_refill_desc3,
155 	.init_desc3 = stmmac_init_desc3,
156 	.clean_desc3 = stmmac_clean_desc3,
157 	.set_16kib_bfsize = stmmac_set_16kib_bfsize,
158 };
159