• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip Sparx5 Switch driver
3  *
4  * Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
5  */
6 
7 #include "sparx5_main_regs.h"
8 #include "sparx5_main.h"
9 
10 #define XTR_EOF_0     ntohl((__force __be32)0x80000000u)
11 #define XTR_EOF_1     ntohl((__force __be32)0x80000001u)
12 #define XTR_EOF_2     ntohl((__force __be32)0x80000002u)
13 #define XTR_EOF_3     ntohl((__force __be32)0x80000003u)
14 #define XTR_PRUNED    ntohl((__force __be32)0x80000004u)
15 #define XTR_ABORT     ntohl((__force __be32)0x80000005u)
16 #define XTR_ESCAPE    ntohl((__force __be32)0x80000006u)
17 #define XTR_NOT_READY ntohl((__force __be32)0x80000007u)
18 
19 #define XTR_VALID_BYTES(x)      (4 - ((x) & 3))
20 
21 #define INJ_TIMEOUT_NS 50000
22 
sparx5_xtr_flush(struct sparx5 * sparx5,u8 grp)23 void sparx5_xtr_flush(struct sparx5 *sparx5, u8 grp)
24 {
25 	/* Start flush */
26 	spx5_wr(QS_XTR_FLUSH_FLUSH_SET(BIT(grp)), sparx5, QS_XTR_FLUSH);
27 
28 	/* Allow to drain */
29 	mdelay(1);
30 
31 	/* All Queues normal */
32 	spx5_wr(0, sparx5, QS_XTR_FLUSH);
33 }
34 
sparx5_ifh_parse(u32 * ifh,struct frame_info * info)35 void sparx5_ifh_parse(u32 *ifh, struct frame_info *info)
36 {
37 	u8 *xtr_hdr = (u8 *)ifh;
38 
39 	/* FWD is bit 45-72 (28 bits), but we only read the 27 LSB for now */
40 	u32 fwd =
41 		((u32)xtr_hdr[27] << 24) |
42 		((u32)xtr_hdr[28] << 16) |
43 		((u32)xtr_hdr[29] <<  8) |
44 		((u32)xtr_hdr[30] <<  0);
45 	fwd = (fwd >> 5);
46 	info->src_port = FIELD_GET(GENMASK(7, 1), fwd);
47 }
48 
sparx5_xtr_grp(struct sparx5 * sparx5,u8 grp,bool byte_swap)49 static void sparx5_xtr_grp(struct sparx5 *sparx5, u8 grp, bool byte_swap)
50 {
51 	bool eof_flag = false, pruned_flag = false, abort_flag = false;
52 	struct net_device *netdev;
53 	struct sparx5_port *port;
54 	struct frame_info fi;
55 	int i, byte_cnt = 0;
56 	struct sk_buff *skb;
57 	u32 ifh[IFH_LEN];
58 	u32 *rxbuf;
59 
60 	/* Get IFH */
61 	for (i = 0; i < IFH_LEN; i++)
62 		ifh[i] = spx5_rd(sparx5, QS_XTR_RD(grp));
63 
64 	/* Decode IFH (whats needed) */
65 	sparx5_ifh_parse(ifh, &fi);
66 
67 	/* Map to port netdev */
68 	port = fi.src_port < SPX5_PORTS ?
69 		sparx5->ports[fi.src_port] : NULL;
70 	if (!port || !port->ndev) {
71 		dev_err(sparx5->dev, "Data on inactive port %d\n", fi.src_port);
72 		sparx5_xtr_flush(sparx5, grp);
73 		return;
74 	}
75 
76 	/* Have netdev, get skb */
77 	netdev = port->ndev;
78 	skb = netdev_alloc_skb(netdev, netdev->mtu + ETH_HLEN);
79 	if (!skb) {
80 		sparx5_xtr_flush(sparx5, grp);
81 		dev_err(sparx5->dev, "No skb allocated\n");
82 		netdev->stats.rx_dropped++;
83 		return;
84 	}
85 	rxbuf = (u32 *)skb->data;
86 
87 	/* Now, pull frame data */
88 	while (!eof_flag) {
89 		u32 val = spx5_rd(sparx5, QS_XTR_RD(grp));
90 		u32 cmp = val;
91 
92 		if (byte_swap)
93 			cmp = ntohl((__force __be32)val);
94 
95 		switch (cmp) {
96 		case XTR_NOT_READY:
97 			break;
98 		case XTR_ABORT:
99 			/* No accompanying data */
100 			abort_flag = true;
101 			eof_flag = true;
102 			break;
103 		case XTR_EOF_0:
104 		case XTR_EOF_1:
105 		case XTR_EOF_2:
106 		case XTR_EOF_3:
107 			/* This assumes STATUS_WORD_POS == 1, Status
108 			 * just after last data
109 			 */
110 			if (!byte_swap)
111 				val = ntohl((__force __be32)val);
112 			byte_cnt -= (4 - XTR_VALID_BYTES(val));
113 			eof_flag = true;
114 			break;
115 		case XTR_PRUNED:
116 			/* But get the last 4 bytes as well */
117 			eof_flag = true;
118 			pruned_flag = true;
119 			fallthrough;
120 		case XTR_ESCAPE:
121 			*rxbuf = spx5_rd(sparx5, QS_XTR_RD(grp));
122 			byte_cnt += 4;
123 			rxbuf++;
124 			break;
125 		default:
126 			*rxbuf = val;
127 			byte_cnt += 4;
128 			rxbuf++;
129 		}
130 	}
131 
132 	if (abort_flag || pruned_flag || !eof_flag) {
133 		netdev_err(netdev, "Discarded frame: abort:%d pruned:%d eof:%d\n",
134 			   abort_flag, pruned_flag, eof_flag);
135 		kfree_skb(skb);
136 		netdev->stats.rx_dropped++;
137 		return;
138 	}
139 
140 	/* Everything we see on an interface that is in the HW bridge
141 	 * has already been forwarded
142 	 */
143 	if (test_bit(port->portno, sparx5->bridge_mask))
144 		skb->offload_fwd_mark = 1;
145 
146 	/* Finish up skb */
147 	skb_put(skb, byte_cnt - ETH_FCS_LEN);
148 	eth_skb_pad(skb);
149 	skb->protocol = eth_type_trans(skb, netdev);
150 	netdev->stats.rx_bytes += skb->len;
151 	netdev->stats.rx_packets++;
152 	netif_rx(skb);
153 }
154 
sparx5_inject(struct sparx5 * sparx5,u32 * ifh,struct sk_buff * skb,struct net_device * ndev)155 static int sparx5_inject(struct sparx5 *sparx5,
156 			 u32 *ifh,
157 			 struct sk_buff *skb,
158 			 struct net_device *ndev)
159 {
160 	int grp = INJ_QUEUE;
161 	u32 val, w, count;
162 	u8 *buf;
163 
164 	val = spx5_rd(sparx5, QS_INJ_STATUS);
165 	if (!(QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp))) {
166 		pr_err_ratelimited("Injection: Queue not ready: 0x%lx\n",
167 				   QS_INJ_STATUS_FIFO_RDY_GET(val));
168 		return -EBUSY;
169 	}
170 
171 	/* Indicate SOF */
172 	spx5_wr(QS_INJ_CTRL_SOF_SET(1) |
173 		QS_INJ_CTRL_GAP_SIZE_SET(1),
174 		sparx5, QS_INJ_CTRL(grp));
175 
176 	/* Write the IFH to the chip. */
177 	for (w = 0; w < IFH_LEN; w++)
178 		spx5_wr(ifh[w], sparx5, QS_INJ_WR(grp));
179 
180 	/* Write words, round up */
181 	count = DIV_ROUND_UP(skb->len, 4);
182 	buf = skb->data;
183 	for (w = 0; w < count; w++, buf += 4) {
184 		val = get_unaligned((const u32 *)buf);
185 		spx5_wr(val, sparx5, QS_INJ_WR(grp));
186 	}
187 
188 	/* Add padding */
189 	while (w < (60 / 4)) {
190 		spx5_wr(0, sparx5, QS_INJ_WR(grp));
191 		w++;
192 	}
193 
194 	/* Indicate EOF and valid bytes in last word */
195 	spx5_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
196 		QS_INJ_CTRL_VLD_BYTES_SET(skb->len < 60 ? 0 : skb->len % 4) |
197 		QS_INJ_CTRL_EOF_SET(1),
198 		sparx5, QS_INJ_CTRL(grp));
199 
200 	/* Add dummy CRC */
201 	spx5_wr(0, sparx5, QS_INJ_WR(grp));
202 	w++;
203 
204 	val = spx5_rd(sparx5, QS_INJ_STATUS);
205 	if (QS_INJ_STATUS_WMARK_REACHED_GET(val) & BIT(grp)) {
206 		struct sparx5_port *port = netdev_priv(ndev);
207 
208 		pr_err_ratelimited("Injection: Watermark reached: 0x%lx\n",
209 				   QS_INJ_STATUS_WMARK_REACHED_GET(val));
210 		netif_stop_queue(ndev);
211 		hrtimer_start(&port->inj_timer, INJ_TIMEOUT_NS,
212 			      HRTIMER_MODE_REL);
213 	}
214 
215 	return NETDEV_TX_OK;
216 }
217 
sparx5_port_xmit_impl(struct sk_buff * skb,struct net_device * dev)218 int sparx5_port_xmit_impl(struct sk_buff *skb, struct net_device *dev)
219 {
220 	struct net_device_stats *stats = &dev->stats;
221 	struct sparx5_port *port = netdev_priv(dev);
222 	struct sparx5 *sparx5 = port->sparx5;
223 	int ret;
224 
225 	if (sparx5->fdma_irq > 0)
226 		ret = sparx5_fdma_xmit(sparx5, port->ifh, skb);
227 	else
228 		ret = sparx5_inject(sparx5, port->ifh, skb, dev);
229 
230 	if (ret == NETDEV_TX_OK) {
231 		stats->tx_bytes += skb->len;
232 		stats->tx_packets++;
233 		skb_tx_timestamp(skb);
234 		dev_kfree_skb_any(skb);
235 	} else {
236 		stats->tx_dropped++;
237 	}
238 	return ret;
239 }
240 
sparx5_injection_timeout(struct hrtimer * tmr)241 static enum hrtimer_restart sparx5_injection_timeout(struct hrtimer *tmr)
242 {
243 	struct sparx5_port *port = container_of(tmr, struct sparx5_port,
244 						inj_timer);
245 	int grp = INJ_QUEUE;
246 	u32 val;
247 
248 	val = spx5_rd(port->sparx5, QS_INJ_STATUS);
249 	if (QS_INJ_STATUS_WMARK_REACHED_GET(val) & BIT(grp)) {
250 		pr_err_ratelimited("Injection: Reset watermark count\n");
251 		/* Reset Watermark count to restart */
252 		spx5_rmw(DSM_DEV_TX_STOP_WM_CFG_DEV_TX_CNT_CLR_SET(1),
253 			 DSM_DEV_TX_STOP_WM_CFG_DEV_TX_CNT_CLR,
254 			 port->sparx5,
255 			 DSM_DEV_TX_STOP_WM_CFG(port->portno));
256 	}
257 	netif_wake_queue(port->ndev);
258 	return HRTIMER_NORESTART;
259 }
260 
sparx5_manual_injection_mode(struct sparx5 * sparx5)261 int sparx5_manual_injection_mode(struct sparx5 *sparx5)
262 {
263 	const int byte_swap = 1;
264 	int portno;
265 
266 	/* Change mode to manual extraction and injection */
267 	spx5_wr(QS_XTR_GRP_CFG_MODE_SET(1) |
268 		QS_XTR_GRP_CFG_STATUS_WORD_POS_SET(1) |
269 		QS_XTR_GRP_CFG_BYTE_SWAP_SET(byte_swap),
270 		sparx5, QS_XTR_GRP_CFG(XTR_QUEUE));
271 	spx5_wr(QS_INJ_GRP_CFG_MODE_SET(1) |
272 		QS_INJ_GRP_CFG_BYTE_SWAP_SET(byte_swap),
273 		sparx5, QS_INJ_GRP_CFG(INJ_QUEUE));
274 
275 	/* CPU ports capture setup */
276 	for (portno = SPX5_PORT_CPU_0; portno <= SPX5_PORT_CPU_1; portno++) {
277 		/* ASM CPU port: No preamble, IFH, enable padding */
278 		spx5_wr(ASM_PORT_CFG_PAD_ENA_SET(1) |
279 			ASM_PORT_CFG_NO_PREAMBLE_ENA_SET(1) |
280 			ASM_PORT_CFG_INJ_FORMAT_CFG_SET(1), /* 1 = IFH */
281 			sparx5, ASM_PORT_CFG(portno));
282 
283 		/* Reset WM cnt to unclog queued frames */
284 		spx5_rmw(DSM_DEV_TX_STOP_WM_CFG_DEV_TX_CNT_CLR_SET(1),
285 			 DSM_DEV_TX_STOP_WM_CFG_DEV_TX_CNT_CLR,
286 			 sparx5,
287 			 DSM_DEV_TX_STOP_WM_CFG(portno));
288 
289 		/* Set Disassembler Stop Watermark level */
290 		spx5_rmw(DSM_DEV_TX_STOP_WM_CFG_DEV_TX_STOP_WM_SET(0),
291 			 DSM_DEV_TX_STOP_WM_CFG_DEV_TX_STOP_WM,
292 			 sparx5,
293 			 DSM_DEV_TX_STOP_WM_CFG(portno));
294 
295 		/* Enable Disassembler buffer underrun watchdog
296 		 */
297 		spx5_rmw(DSM_BUF_CFG_UNDERFLOW_WATCHDOG_DIS_SET(0),
298 			 DSM_BUF_CFG_UNDERFLOW_WATCHDOG_DIS,
299 			 sparx5,
300 			 DSM_BUF_CFG(portno));
301 	}
302 	return 0;
303 }
304 
sparx5_xtr_handler(int irq,void * _sparx5)305 irqreturn_t sparx5_xtr_handler(int irq, void *_sparx5)
306 {
307 	struct sparx5 *s5 = _sparx5;
308 	int poll = 64;
309 
310 	/* Check data in queue */
311 	while (spx5_rd(s5, QS_XTR_DATA_PRESENT) & BIT(XTR_QUEUE) && poll-- > 0)
312 		sparx5_xtr_grp(s5, XTR_QUEUE, false);
313 
314 	return IRQ_HANDLED;
315 }
316 
sparx5_port_inj_timer_setup(struct sparx5_port * port)317 void sparx5_port_inj_timer_setup(struct sparx5_port *port)
318 {
319 	hrtimer_init(&port->inj_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
320 	port->inj_timer.function = sparx5_injection_timeout;
321 }
322