1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3 Specialised functions for managing Chained mode
4
5 Copyright(C) 2011 STMicroelectronics Ltd
6
7 It defines all the functions used to handle the normal/enhanced
8 descriptors in case of the DMA is configured to work in chained or
9 in ring mode.
10
11
12 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
13 *******************************************************************************/
14
15 #include "stmmac.h"
16
jumbo_frm(struct stmmac_tx_queue * tx_q,struct sk_buff * skb,int csum)17 static int jumbo_frm(struct stmmac_tx_queue *tx_q, struct sk_buff *skb,
18 int csum)
19 {
20 unsigned int nopaged_len = skb_headlen(skb);
21 struct stmmac_priv *priv = tx_q->priv_data;
22 unsigned int entry = tx_q->cur_tx;
23 unsigned int bmax, buf_len;
24 unsigned int i = 1, len;
25 struct dma_desc *desc;
26 dma_addr_t des2;
27
28 desc = tx_q->dma_tx + entry;
29
30 if (priv->plat->enh_desc)
31 bmax = BUF_SIZE_8KiB;
32 else
33 bmax = BUF_SIZE_2KiB;
34
35 buf_len = min_t(unsigned int, nopaged_len, bmax);
36 len = nopaged_len - buf_len;
37
38 des2 = dma_map_single(priv->device, skb->data,
39 buf_len, DMA_TO_DEVICE);
40 desc->des2 = cpu_to_le32(lower_32_bits(des2));
41 if (dma_mapping_error(priv->device, des2))
42 return -1;
43 tx_q->tx_skbuff_dma[entry].buf = des2;
44 tx_q->tx_skbuff_dma[entry].len = buf_len;
45 /* do not close the descriptor and do not set own bit */
46 stmmac_prepare_tx_desc(priv, desc, 1, buf_len, csum, STMMAC_CHAIN_MODE,
47 0, false, skb->len);
48
49 while (len != 0) {
50 tx_q->tx_skbuff[entry] = NULL;
51 entry = STMMAC_NEXT_ENTRY(entry, priv->dma_conf.dma_tx_size);
52 desc = tx_q->dma_tx + entry;
53
54 if (len > bmax) {
55 des2 = dma_map_single(priv->device,
56 (skb->data + bmax * i),
57 bmax, DMA_TO_DEVICE);
58 desc->des2 = cpu_to_le32(lower_32_bits(des2));
59 if (dma_mapping_error(priv->device, des2))
60 return -1;
61 tx_q->tx_skbuff_dma[entry].buf = des2;
62 tx_q->tx_skbuff_dma[entry].len = bmax;
63 stmmac_prepare_tx_desc(priv, desc, 0, bmax, csum,
64 STMMAC_CHAIN_MODE, 1, false, skb->len);
65 len -= bmax;
66 i++;
67 } else {
68 des2 = dma_map_single(priv->device,
69 (skb->data + bmax * i), len,
70 DMA_TO_DEVICE);
71 desc->des2 = cpu_to_le32(lower_32_bits(des2));
72 if (dma_mapping_error(priv->device, des2))
73 return -1;
74 tx_q->tx_skbuff_dma[entry].buf = des2;
75 tx_q->tx_skbuff_dma[entry].len = len;
76 /* last descriptor can be set now */
77 stmmac_prepare_tx_desc(priv, desc, 0, len, csum,
78 STMMAC_CHAIN_MODE, 1, true, skb->len);
79 len = 0;
80 }
81 }
82
83 tx_q->cur_tx = entry;
84
85 return entry;
86 }
87
is_jumbo_frm(unsigned int len,bool enh_desc)88 static bool is_jumbo_frm(unsigned int len, bool enh_desc)
89 {
90 bool ret = false;
91
92 if ((enh_desc && (len > BUF_SIZE_8KiB)) ||
93 (!enh_desc && (len > BUF_SIZE_2KiB)))
94 ret = true;
95
96 return ret;
97 }
98
init_dma_chain(void * des,dma_addr_t phy_addr,unsigned int size,unsigned int extend_desc)99 static void init_dma_chain(void *des, dma_addr_t phy_addr,
100 unsigned int size, unsigned int extend_desc)
101 {
102 /*
103 * In chained mode the des3 points to the next element in the ring.
104 * The latest element has to point to the head.
105 */
106 int i;
107 dma_addr_t dma_phy = phy_addr;
108
109 if (extend_desc) {
110 struct dma_extended_desc *p = (struct dma_extended_desc *)des;
111 for (i = 0; i < (size - 1); i++) {
112 dma_phy += sizeof(struct dma_extended_desc);
113 p->basic.des3 = cpu_to_le32((unsigned int)dma_phy);
114 p++;
115 }
116 p->basic.des3 = cpu_to_le32((unsigned int)phy_addr);
117
118 } else {
119 struct dma_desc *p = (struct dma_desc *)des;
120 for (i = 0; i < (size - 1); i++) {
121 dma_phy += sizeof(struct dma_desc);
122 p->des3 = cpu_to_le32((unsigned int)dma_phy);
123 p++;
124 }
125 p->des3 = cpu_to_le32((unsigned int)phy_addr);
126 }
127 }
128
refill_desc3(struct stmmac_rx_queue * rx_q,struct dma_desc * p)129 static void refill_desc3(struct stmmac_rx_queue *rx_q, struct dma_desc *p)
130 {
131 struct stmmac_priv *priv = rx_q->priv_data;
132
133 if (priv->hwts_rx_en && !priv->extend_desc)
134 /* NOTE: Device will overwrite des3 with timestamp value if
135 * 1588-2002 time stamping is enabled, hence reinitialize it
136 * to keep explicit chaining in the descriptor.
137 */
138 p->des3 = cpu_to_le32((unsigned int)(rx_q->dma_rx_phy +
139 (((rx_q->dirty_rx) + 1) %
140 priv->dma_conf.dma_rx_size) *
141 sizeof(struct dma_desc)));
142 }
143
clean_desc3(struct stmmac_tx_queue * tx_q,struct dma_desc * p)144 static void clean_desc3(struct stmmac_tx_queue *tx_q, struct dma_desc *p)
145 {
146 struct stmmac_priv *priv = tx_q->priv_data;
147 unsigned int entry = tx_q->dirty_tx;
148
149 if (tx_q->tx_skbuff_dma[entry].last_segment && !priv->extend_desc &&
150 priv->hwts_tx_en)
151 /* NOTE: Device will overwrite des3 with timestamp value if
152 * 1588-2002 time stamping is enabled, hence reinitialize it
153 * to keep explicit chaining in the descriptor.
154 */
155 p->des3 = cpu_to_le32((unsigned int)((tx_q->dma_tx_phy +
156 ((tx_q->dirty_tx + 1) %
157 priv->dma_conf.dma_tx_size))
158 * sizeof(struct dma_desc)));
159 }
160
161 const struct stmmac_mode_ops chain_mode_ops = {
162 .init = init_dma_chain,
163 .is_jumbo_frm = is_jumbo_frm,
164 .jumbo_frm = jumbo_frm,
165 .refill_desc3 = refill_desc3,
166 .clean_desc3 = clean_desc3,
167 };
168