• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2009-2012 Cavium, Inc
7  */
8 
9 #include <linux/platform_device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/etherdevice.h>
12 #include <linux/capability.h>
13 #include <linux/net_tstamp.h>
14 #include <linux/interrupt.h>
15 #include <linux/netdevice.h>
16 #include <linux/spinlock.h>
17 #include <linux/if_vlan.h>
18 #include <linux/of_mdio.h>
19 #include <linux/module.h>
20 #include <linux/of_net.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/phy.h>
24 #include <linux/io.h>
25 
26 #include <asm/octeon/octeon.h>
27 #include <asm/octeon/cvmx-mixx-defs.h>
28 #include <asm/octeon/cvmx-agl-defs.h>
29 
30 #define DRV_NAME "octeon_mgmt"
31 #define DRV_VERSION "2.0"
32 #define DRV_DESCRIPTION \
33 	"Cavium Networks Octeon MII (management) port Network Driver"
34 
35 #define OCTEON_MGMT_NAPI_WEIGHT 16
36 
37 /* Ring sizes that are powers of two allow for more efficient modulo
38  * opertions.
39  */
40 #define OCTEON_MGMT_RX_RING_SIZE 512
41 #define OCTEON_MGMT_TX_RING_SIZE 128
42 
43 /* Allow 8 bytes for vlan and FCS. */
44 #define OCTEON_MGMT_RX_HEADROOM (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN)
45 
46 union mgmt_port_ring_entry {
47 	u64 d64;
48 	struct {
49 #define RING_ENTRY_CODE_DONE 0xf
50 #define RING_ENTRY_CODE_MORE 0x10
51 #ifdef __BIG_ENDIAN_BITFIELD
52 		u64 reserved_62_63:2;
53 		/* Length of the buffer/packet in bytes */
54 		u64 len:14;
55 		/* For TX, signals that the packet should be timestamped */
56 		u64 tstamp:1;
57 		/* The RX error code */
58 		u64 code:7;
59 		/* Physical address of the buffer */
60 		u64 addr:40;
61 #else
62 		u64 addr:40;
63 		u64 code:7;
64 		u64 tstamp:1;
65 		u64 len:14;
66 		u64 reserved_62_63:2;
67 #endif
68 	} s;
69 };
70 
71 #define MIX_ORING1	0x0
72 #define MIX_ORING2	0x8
73 #define MIX_IRING1	0x10
74 #define MIX_IRING2	0x18
75 #define MIX_CTL		0x20
76 #define MIX_IRHWM	0x28
77 #define MIX_IRCNT	0x30
78 #define MIX_ORHWM	0x38
79 #define MIX_ORCNT	0x40
80 #define MIX_ISR		0x48
81 #define MIX_INTENA	0x50
82 #define MIX_REMCNT	0x58
83 #define MIX_BIST	0x78
84 
85 #define AGL_GMX_PRT_CFG			0x10
86 #define AGL_GMX_RX_FRM_CTL		0x18
87 #define AGL_GMX_RX_FRM_MAX		0x30
88 #define AGL_GMX_RX_JABBER		0x38
89 #define AGL_GMX_RX_STATS_CTL		0x50
90 
91 #define AGL_GMX_RX_STATS_PKTS_DRP	0xb0
92 #define AGL_GMX_RX_STATS_OCTS_DRP	0xb8
93 #define AGL_GMX_RX_STATS_PKTS_BAD	0xc0
94 
95 #define AGL_GMX_RX_ADR_CTL		0x100
96 #define AGL_GMX_RX_ADR_CAM_EN		0x108
97 #define AGL_GMX_RX_ADR_CAM0		0x180
98 #define AGL_GMX_RX_ADR_CAM1		0x188
99 #define AGL_GMX_RX_ADR_CAM2		0x190
100 #define AGL_GMX_RX_ADR_CAM3		0x198
101 #define AGL_GMX_RX_ADR_CAM4		0x1a0
102 #define AGL_GMX_RX_ADR_CAM5		0x1a8
103 
104 #define AGL_GMX_TX_CLK			0x208
105 #define AGL_GMX_TX_STATS_CTL		0x268
106 #define AGL_GMX_TX_CTL			0x270
107 #define AGL_GMX_TX_STAT0		0x280
108 #define AGL_GMX_TX_STAT1		0x288
109 #define AGL_GMX_TX_STAT2		0x290
110 #define AGL_GMX_TX_STAT3		0x298
111 #define AGL_GMX_TX_STAT4		0x2a0
112 #define AGL_GMX_TX_STAT5		0x2a8
113 #define AGL_GMX_TX_STAT6		0x2b0
114 #define AGL_GMX_TX_STAT7		0x2b8
115 #define AGL_GMX_TX_STAT8		0x2c0
116 #define AGL_GMX_TX_STAT9		0x2c8
117 
118 struct octeon_mgmt {
119 	struct net_device *netdev;
120 	u64 mix;
121 	u64 agl;
122 	u64 agl_prt_ctl;
123 	int port;
124 	int irq;
125 	bool has_rx_tstamp;
126 	u64 *tx_ring;
127 	dma_addr_t tx_ring_handle;
128 	unsigned int tx_next;
129 	unsigned int tx_next_clean;
130 	unsigned int tx_current_fill;
131 	/* The tx_list lock also protects the ring related variables */
132 	struct sk_buff_head tx_list;
133 
134 	/* RX variables only touched in napi_poll.  No locking necessary. */
135 	u64 *rx_ring;
136 	dma_addr_t rx_ring_handle;
137 	unsigned int rx_next;
138 	unsigned int rx_next_fill;
139 	unsigned int rx_current_fill;
140 	struct sk_buff_head rx_list;
141 
142 	spinlock_t lock;
143 	unsigned int last_duplex;
144 	unsigned int last_link;
145 	unsigned int last_speed;
146 	struct device *dev;
147 	struct napi_struct napi;
148 	struct tasklet_struct tx_clean_tasklet;
149 	struct device_node *phy_np;
150 	resource_size_t mix_phys;
151 	resource_size_t mix_size;
152 	resource_size_t agl_phys;
153 	resource_size_t agl_size;
154 	resource_size_t agl_prt_ctl_phys;
155 	resource_size_t agl_prt_ctl_size;
156 };
157 
octeon_mgmt_set_rx_irq(struct octeon_mgmt * p,int enable)158 static void octeon_mgmt_set_rx_irq(struct octeon_mgmt *p, int enable)
159 {
160 	union cvmx_mixx_intena mix_intena;
161 	unsigned long flags;
162 
163 	spin_lock_irqsave(&p->lock, flags);
164 	mix_intena.u64 = cvmx_read_csr(p->mix + MIX_INTENA);
165 	mix_intena.s.ithena = enable ? 1 : 0;
166 	cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
167 	spin_unlock_irqrestore(&p->lock, flags);
168 }
169 
octeon_mgmt_set_tx_irq(struct octeon_mgmt * p,int enable)170 static void octeon_mgmt_set_tx_irq(struct octeon_mgmt *p, int enable)
171 {
172 	union cvmx_mixx_intena mix_intena;
173 	unsigned long flags;
174 
175 	spin_lock_irqsave(&p->lock, flags);
176 	mix_intena.u64 = cvmx_read_csr(p->mix + MIX_INTENA);
177 	mix_intena.s.othena = enable ? 1 : 0;
178 	cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
179 	spin_unlock_irqrestore(&p->lock, flags);
180 }
181 
octeon_mgmt_enable_rx_irq(struct octeon_mgmt * p)182 static void octeon_mgmt_enable_rx_irq(struct octeon_mgmt *p)
183 {
184 	octeon_mgmt_set_rx_irq(p, 1);
185 }
186 
octeon_mgmt_disable_rx_irq(struct octeon_mgmt * p)187 static void octeon_mgmt_disable_rx_irq(struct octeon_mgmt *p)
188 {
189 	octeon_mgmt_set_rx_irq(p, 0);
190 }
191 
octeon_mgmt_enable_tx_irq(struct octeon_mgmt * p)192 static void octeon_mgmt_enable_tx_irq(struct octeon_mgmt *p)
193 {
194 	octeon_mgmt_set_tx_irq(p, 1);
195 }
196 
octeon_mgmt_disable_tx_irq(struct octeon_mgmt * p)197 static void octeon_mgmt_disable_tx_irq(struct octeon_mgmt *p)
198 {
199 	octeon_mgmt_set_tx_irq(p, 0);
200 }
201 
ring_max_fill(unsigned int ring_size)202 static unsigned int ring_max_fill(unsigned int ring_size)
203 {
204 	return ring_size - 8;
205 }
206 
ring_size_to_bytes(unsigned int ring_size)207 static unsigned int ring_size_to_bytes(unsigned int ring_size)
208 {
209 	return ring_size * sizeof(union mgmt_port_ring_entry);
210 }
211 
octeon_mgmt_rx_fill_ring(struct net_device * netdev)212 static void octeon_mgmt_rx_fill_ring(struct net_device *netdev)
213 {
214 	struct octeon_mgmt *p = netdev_priv(netdev);
215 
216 	while (p->rx_current_fill < ring_max_fill(OCTEON_MGMT_RX_RING_SIZE)) {
217 		unsigned int size;
218 		union mgmt_port_ring_entry re;
219 		struct sk_buff *skb;
220 
221 		/* CN56XX pass 1 needs 8 bytes of padding.  */
222 		size = netdev->mtu + OCTEON_MGMT_RX_HEADROOM + 8 + NET_IP_ALIGN;
223 
224 		skb = netdev_alloc_skb(netdev, size);
225 		if (!skb)
226 			break;
227 		skb_reserve(skb, NET_IP_ALIGN);
228 		__skb_queue_tail(&p->rx_list, skb);
229 
230 		re.d64 = 0;
231 		re.s.len = size;
232 		re.s.addr = dma_map_single(p->dev, skb->data,
233 					   size,
234 					   DMA_FROM_DEVICE);
235 
236 		/* Put it in the ring.  */
237 		p->rx_ring[p->rx_next_fill] = re.d64;
238 		/* Make sure there is no reorder of filling the ring and ringing
239 		 * the bell
240 		 */
241 		wmb();
242 
243 		dma_sync_single_for_device(p->dev, p->rx_ring_handle,
244 					   ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
245 					   DMA_BIDIRECTIONAL);
246 		p->rx_next_fill =
247 			(p->rx_next_fill + 1) % OCTEON_MGMT_RX_RING_SIZE;
248 		p->rx_current_fill++;
249 		/* Ring the bell.  */
250 		cvmx_write_csr(p->mix + MIX_IRING2, 1);
251 	}
252 }
253 
octeon_mgmt_clean_tx_buffers(struct octeon_mgmt * p)254 static void octeon_mgmt_clean_tx_buffers(struct octeon_mgmt *p)
255 {
256 	union cvmx_mixx_orcnt mix_orcnt;
257 	union mgmt_port_ring_entry re;
258 	struct sk_buff *skb;
259 	int cleaned = 0;
260 	unsigned long flags;
261 
262 	mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT);
263 	while (mix_orcnt.s.orcnt) {
264 		spin_lock_irqsave(&p->tx_list.lock, flags);
265 
266 		mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT);
267 
268 		if (mix_orcnt.s.orcnt == 0) {
269 			spin_unlock_irqrestore(&p->tx_list.lock, flags);
270 			break;
271 		}
272 
273 		dma_sync_single_for_cpu(p->dev, p->tx_ring_handle,
274 					ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
275 					DMA_BIDIRECTIONAL);
276 
277 		re.d64 = p->tx_ring[p->tx_next_clean];
278 		p->tx_next_clean =
279 			(p->tx_next_clean + 1) % OCTEON_MGMT_TX_RING_SIZE;
280 		skb = __skb_dequeue(&p->tx_list);
281 
282 		mix_orcnt.u64 = 0;
283 		mix_orcnt.s.orcnt = 1;
284 
285 		/* Acknowledge to hardware that we have the buffer.  */
286 		cvmx_write_csr(p->mix + MIX_ORCNT, mix_orcnt.u64);
287 		p->tx_current_fill--;
288 
289 		spin_unlock_irqrestore(&p->tx_list.lock, flags);
290 
291 		dma_unmap_single(p->dev, re.s.addr, re.s.len,
292 				 DMA_TO_DEVICE);
293 
294 		/* Read the hardware TX timestamp if one was recorded */
295 		if (unlikely(re.s.tstamp)) {
296 			struct skb_shared_hwtstamps ts;
297 			u64 ns;
298 
299 			memset(&ts, 0, sizeof(ts));
300 			/* Read the timestamp */
301 			ns = cvmx_read_csr(CVMX_MIXX_TSTAMP(p->port));
302 			/* Remove the timestamp from the FIFO */
303 			cvmx_write_csr(CVMX_MIXX_TSCTL(p->port), 0);
304 			/* Tell the kernel about the timestamp */
305 			ts.hwtstamp = ns_to_ktime(ns);
306 			skb_tstamp_tx(skb, &ts);
307 		}
308 
309 		dev_kfree_skb_any(skb);
310 		cleaned++;
311 
312 		mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT);
313 	}
314 
315 	if (cleaned && netif_queue_stopped(p->netdev))
316 		netif_wake_queue(p->netdev);
317 }
318 
octeon_mgmt_clean_tx_tasklet(unsigned long arg)319 static void octeon_mgmt_clean_tx_tasklet(unsigned long arg)
320 {
321 	struct octeon_mgmt *p = (struct octeon_mgmt *)arg;
322 	octeon_mgmt_clean_tx_buffers(p);
323 	octeon_mgmt_enable_tx_irq(p);
324 }
325 
octeon_mgmt_update_rx_stats(struct net_device * netdev)326 static void octeon_mgmt_update_rx_stats(struct net_device *netdev)
327 {
328 	struct octeon_mgmt *p = netdev_priv(netdev);
329 	unsigned long flags;
330 	u64 drop, bad;
331 
332 	/* These reads also clear the count registers.  */
333 	drop = cvmx_read_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP);
334 	bad = cvmx_read_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD);
335 
336 	if (drop || bad) {
337 		/* Do an atomic update. */
338 		spin_lock_irqsave(&p->lock, flags);
339 		netdev->stats.rx_errors += bad;
340 		netdev->stats.rx_dropped += drop;
341 		spin_unlock_irqrestore(&p->lock, flags);
342 	}
343 }
344 
octeon_mgmt_update_tx_stats(struct net_device * netdev)345 static void octeon_mgmt_update_tx_stats(struct net_device *netdev)
346 {
347 	struct octeon_mgmt *p = netdev_priv(netdev);
348 	unsigned long flags;
349 
350 	union cvmx_agl_gmx_txx_stat0 s0;
351 	union cvmx_agl_gmx_txx_stat1 s1;
352 
353 	/* These reads also clear the count registers.  */
354 	s0.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_STAT0);
355 	s1.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_STAT1);
356 
357 	if (s0.s.xsdef || s0.s.xscol || s1.s.scol || s1.s.mcol) {
358 		/* Do an atomic update. */
359 		spin_lock_irqsave(&p->lock, flags);
360 		netdev->stats.tx_errors += s0.s.xsdef + s0.s.xscol;
361 		netdev->stats.collisions += s1.s.scol + s1.s.mcol;
362 		spin_unlock_irqrestore(&p->lock, flags);
363 	}
364 }
365 
366 /*
367  * Dequeue a receive skb and its corresponding ring entry.  The ring
368  * entry is returned, *pskb is updated to point to the skb.
369  */
octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt * p,struct sk_buff ** pskb)370 static u64 octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt *p,
371 					 struct sk_buff **pskb)
372 {
373 	union mgmt_port_ring_entry re;
374 
375 	dma_sync_single_for_cpu(p->dev, p->rx_ring_handle,
376 				ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
377 				DMA_BIDIRECTIONAL);
378 
379 	re.d64 = p->rx_ring[p->rx_next];
380 	p->rx_next = (p->rx_next + 1) % OCTEON_MGMT_RX_RING_SIZE;
381 	p->rx_current_fill--;
382 	*pskb = __skb_dequeue(&p->rx_list);
383 
384 	dma_unmap_single(p->dev, re.s.addr,
385 			 ETH_FRAME_LEN + OCTEON_MGMT_RX_HEADROOM,
386 			 DMA_FROM_DEVICE);
387 
388 	return re.d64;
389 }
390 
391 
octeon_mgmt_receive_one(struct octeon_mgmt * p)392 static int octeon_mgmt_receive_one(struct octeon_mgmt *p)
393 {
394 	struct net_device *netdev = p->netdev;
395 	union cvmx_mixx_ircnt mix_ircnt;
396 	union mgmt_port_ring_entry re;
397 	struct sk_buff *skb;
398 	struct sk_buff *skb2;
399 	struct sk_buff *skb_new;
400 	union mgmt_port_ring_entry re2;
401 	int rc = 1;
402 
403 
404 	re.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb);
405 	if (likely(re.s.code == RING_ENTRY_CODE_DONE)) {
406 		/* A good packet, send it up. */
407 		skb_put(skb, re.s.len);
408 good:
409 		/* Process the RX timestamp if it was recorded */
410 		if (p->has_rx_tstamp) {
411 			/* The first 8 bytes are the timestamp */
412 			u64 ns = *(u64 *)skb->data;
413 			struct skb_shared_hwtstamps *ts;
414 			ts = skb_hwtstamps(skb);
415 			ts->hwtstamp = ns_to_ktime(ns);
416 			__skb_pull(skb, 8);
417 		}
418 		skb->protocol = eth_type_trans(skb, netdev);
419 		netdev->stats.rx_packets++;
420 		netdev->stats.rx_bytes += skb->len;
421 		netif_receive_skb(skb);
422 		rc = 0;
423 	} else if (re.s.code == RING_ENTRY_CODE_MORE) {
424 		/* Packet split across skbs.  This can happen if we
425 		 * increase the MTU.  Buffers that are already in the
426 		 * rx ring can then end up being too small.  As the rx
427 		 * ring is refilled, buffers sized for the new MTU
428 		 * will be used and we should go back to the normal
429 		 * non-split case.
430 		 */
431 		skb_put(skb, re.s.len);
432 		do {
433 			re2.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb2);
434 			if (re2.s.code != RING_ENTRY_CODE_MORE
435 				&& re2.s.code != RING_ENTRY_CODE_DONE)
436 				goto split_error;
437 			skb_put(skb2,  re2.s.len);
438 			skb_new = skb_copy_expand(skb, 0, skb2->len,
439 						  GFP_ATOMIC);
440 			if (!skb_new)
441 				goto split_error;
442 			if (skb_copy_bits(skb2, 0, skb_tail_pointer(skb_new),
443 					  skb2->len))
444 				goto split_error;
445 			skb_put(skb_new, skb2->len);
446 			dev_kfree_skb_any(skb);
447 			dev_kfree_skb_any(skb2);
448 			skb = skb_new;
449 		} while (re2.s.code == RING_ENTRY_CODE_MORE);
450 		goto good;
451 	} else {
452 		/* Some other error, discard it. */
453 		dev_kfree_skb_any(skb);
454 		/* Error statistics are accumulated in
455 		 * octeon_mgmt_update_rx_stats.
456 		 */
457 	}
458 	goto done;
459 split_error:
460 	/* Discard the whole mess. */
461 	dev_kfree_skb_any(skb);
462 	dev_kfree_skb_any(skb2);
463 	while (re2.s.code == RING_ENTRY_CODE_MORE) {
464 		re2.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb2);
465 		dev_kfree_skb_any(skb2);
466 	}
467 	netdev->stats.rx_errors++;
468 
469 done:
470 	/* Tell the hardware we processed a packet.  */
471 	mix_ircnt.u64 = 0;
472 	mix_ircnt.s.ircnt = 1;
473 	cvmx_write_csr(p->mix + MIX_IRCNT, mix_ircnt.u64);
474 	return rc;
475 }
476 
octeon_mgmt_receive_packets(struct octeon_mgmt * p,int budget)477 static int octeon_mgmt_receive_packets(struct octeon_mgmt *p, int budget)
478 {
479 	unsigned int work_done = 0;
480 	union cvmx_mixx_ircnt mix_ircnt;
481 	int rc;
482 
483 	mix_ircnt.u64 = cvmx_read_csr(p->mix + MIX_IRCNT);
484 	while (work_done < budget && mix_ircnt.s.ircnt) {
485 
486 		rc = octeon_mgmt_receive_one(p);
487 		if (!rc)
488 			work_done++;
489 
490 		/* Check for more packets. */
491 		mix_ircnt.u64 = cvmx_read_csr(p->mix + MIX_IRCNT);
492 	}
493 
494 	octeon_mgmt_rx_fill_ring(p->netdev);
495 
496 	return work_done;
497 }
498 
octeon_mgmt_napi_poll(struct napi_struct * napi,int budget)499 static int octeon_mgmt_napi_poll(struct napi_struct *napi, int budget)
500 {
501 	struct octeon_mgmt *p = container_of(napi, struct octeon_mgmt, napi);
502 	struct net_device *netdev = p->netdev;
503 	unsigned int work_done = 0;
504 
505 	work_done = octeon_mgmt_receive_packets(p, budget);
506 
507 	if (work_done < budget) {
508 		/* We stopped because no more packets were available. */
509 		napi_complete_done(napi, work_done);
510 		octeon_mgmt_enable_rx_irq(p);
511 	}
512 	octeon_mgmt_update_rx_stats(netdev);
513 
514 	return work_done;
515 }
516 
517 /* Reset the hardware to clean state.  */
octeon_mgmt_reset_hw(struct octeon_mgmt * p)518 static void octeon_mgmt_reset_hw(struct octeon_mgmt *p)
519 {
520 	union cvmx_mixx_ctl mix_ctl;
521 	union cvmx_mixx_bist mix_bist;
522 	union cvmx_agl_gmx_bist agl_gmx_bist;
523 
524 	mix_ctl.u64 = 0;
525 	cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
526 	do {
527 		mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
528 	} while (mix_ctl.s.busy);
529 	mix_ctl.s.reset = 1;
530 	cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
531 	cvmx_read_csr(p->mix + MIX_CTL);
532 	octeon_io_clk_delay(64);
533 
534 	mix_bist.u64 = cvmx_read_csr(p->mix + MIX_BIST);
535 	if (mix_bist.u64)
536 		dev_warn(p->dev, "MIX failed BIST (0x%016llx)\n",
537 			(unsigned long long)mix_bist.u64);
538 
539 	agl_gmx_bist.u64 = cvmx_read_csr(CVMX_AGL_GMX_BIST);
540 	if (agl_gmx_bist.u64)
541 		dev_warn(p->dev, "AGL failed BIST (0x%016llx)\n",
542 			 (unsigned long long)agl_gmx_bist.u64);
543 }
544 
545 struct octeon_mgmt_cam_state {
546 	u64 cam[6];
547 	u64 cam_mask;
548 	int cam_index;
549 };
550 
octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state * cs,unsigned char * addr)551 static void octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state *cs,
552 				      unsigned char *addr)
553 {
554 	int i;
555 
556 	for (i = 0; i < 6; i++)
557 		cs->cam[i] |= (u64)addr[i] << (8 * (cs->cam_index));
558 	cs->cam_mask |= (1ULL << cs->cam_index);
559 	cs->cam_index++;
560 }
561 
octeon_mgmt_set_rx_filtering(struct net_device * netdev)562 static void octeon_mgmt_set_rx_filtering(struct net_device *netdev)
563 {
564 	struct octeon_mgmt *p = netdev_priv(netdev);
565 	union cvmx_agl_gmx_rxx_adr_ctl adr_ctl;
566 	union cvmx_agl_gmx_prtx_cfg agl_gmx_prtx;
567 	unsigned long flags;
568 	unsigned int prev_packet_enable;
569 	unsigned int cam_mode = 1; /* 1 - Accept on CAM match */
570 	unsigned int multicast_mode = 1; /* 1 - Reject all multicast.  */
571 	struct octeon_mgmt_cam_state cam_state;
572 	struct netdev_hw_addr *ha;
573 	int available_cam_entries;
574 
575 	memset(&cam_state, 0, sizeof(cam_state));
576 
577 	if ((netdev->flags & IFF_PROMISC) || netdev->uc.count > 7) {
578 		cam_mode = 0;
579 		available_cam_entries = 8;
580 	} else {
581 		/* One CAM entry for the primary address, leaves seven
582 		 * for the secondary addresses.
583 		 */
584 		available_cam_entries = 7 - netdev->uc.count;
585 	}
586 
587 	if (netdev->flags & IFF_MULTICAST) {
588 		if (cam_mode == 0 || (netdev->flags & IFF_ALLMULTI) ||
589 		    netdev_mc_count(netdev) > available_cam_entries)
590 			multicast_mode = 2; /* 2 - Accept all multicast.  */
591 		else
592 			multicast_mode = 0; /* 0 - Use CAM.  */
593 	}
594 
595 	if (cam_mode == 1) {
596 		/* Add primary address. */
597 		octeon_mgmt_cam_state_add(&cam_state, netdev->dev_addr);
598 		netdev_for_each_uc_addr(ha, netdev)
599 			octeon_mgmt_cam_state_add(&cam_state, ha->addr);
600 	}
601 	if (multicast_mode == 0) {
602 		netdev_for_each_mc_addr(ha, netdev)
603 			octeon_mgmt_cam_state_add(&cam_state, ha->addr);
604 	}
605 
606 	spin_lock_irqsave(&p->lock, flags);
607 
608 	/* Disable packet I/O. */
609 	agl_gmx_prtx.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
610 	prev_packet_enable = agl_gmx_prtx.s.en;
611 	agl_gmx_prtx.s.en = 0;
612 	cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, agl_gmx_prtx.u64);
613 
614 	adr_ctl.u64 = 0;
615 	adr_ctl.s.cam_mode = cam_mode;
616 	adr_ctl.s.mcst = multicast_mode;
617 	adr_ctl.s.bcst = 1;     /* Allow broadcast */
618 
619 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CTL, adr_ctl.u64);
620 
621 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM0, cam_state.cam[0]);
622 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM1, cam_state.cam[1]);
623 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM2, cam_state.cam[2]);
624 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM3, cam_state.cam[3]);
625 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM4, cam_state.cam[4]);
626 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM5, cam_state.cam[5]);
627 	cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM_EN, cam_state.cam_mask);
628 
629 	/* Restore packet I/O. */
630 	agl_gmx_prtx.s.en = prev_packet_enable;
631 	cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, agl_gmx_prtx.u64);
632 
633 	spin_unlock_irqrestore(&p->lock, flags);
634 }
635 
octeon_mgmt_set_mac_address(struct net_device * netdev,void * addr)636 static int octeon_mgmt_set_mac_address(struct net_device *netdev, void *addr)
637 {
638 	int r = eth_mac_addr(netdev, addr);
639 
640 	if (r)
641 		return r;
642 
643 	octeon_mgmt_set_rx_filtering(netdev);
644 
645 	return 0;
646 }
647 
octeon_mgmt_change_mtu(struct net_device * netdev,int new_mtu)648 static int octeon_mgmt_change_mtu(struct net_device *netdev, int new_mtu)
649 {
650 	struct octeon_mgmt *p = netdev_priv(netdev);
651 	int max_packet = new_mtu + ETH_HLEN + ETH_FCS_LEN;
652 
653 	netdev->mtu = new_mtu;
654 
655 	/* HW lifts the limit if the frame is VLAN tagged
656 	 * (+4 bytes per each tag, up to two tags)
657 	 */
658 	cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_MAX, max_packet);
659 	/* Set the hardware to truncate packets larger than the MTU. The jabber
660 	 * register must be set to a multiple of 8 bytes, so round up. JABBER is
661 	 * an unconditional limit, so we need to account for two possible VLAN
662 	 * tags.
663 	 */
664 	cvmx_write_csr(p->agl + AGL_GMX_RX_JABBER,
665 		       (max_packet + 7 + VLAN_HLEN * 2) & 0xfff8);
666 
667 	return 0;
668 }
669 
octeon_mgmt_interrupt(int cpl,void * dev_id)670 static irqreturn_t octeon_mgmt_interrupt(int cpl, void *dev_id)
671 {
672 	struct net_device *netdev = dev_id;
673 	struct octeon_mgmt *p = netdev_priv(netdev);
674 	union cvmx_mixx_isr mixx_isr;
675 
676 	mixx_isr.u64 = cvmx_read_csr(p->mix + MIX_ISR);
677 
678 	/* Clear any pending interrupts */
679 	cvmx_write_csr(p->mix + MIX_ISR, mixx_isr.u64);
680 	cvmx_read_csr(p->mix + MIX_ISR);
681 
682 	if (mixx_isr.s.irthresh) {
683 		octeon_mgmt_disable_rx_irq(p);
684 		napi_schedule(&p->napi);
685 	}
686 	if (mixx_isr.s.orthresh) {
687 		octeon_mgmt_disable_tx_irq(p);
688 		tasklet_schedule(&p->tx_clean_tasklet);
689 	}
690 
691 	return IRQ_HANDLED;
692 }
693 
octeon_mgmt_ioctl_hwtstamp(struct net_device * netdev,struct ifreq * rq,int cmd)694 static int octeon_mgmt_ioctl_hwtstamp(struct net_device *netdev,
695 				      struct ifreq *rq, int cmd)
696 {
697 	struct octeon_mgmt *p = netdev_priv(netdev);
698 	struct hwtstamp_config config;
699 	union cvmx_mio_ptp_clock_cfg ptp;
700 	union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl;
701 	bool have_hw_timestamps = false;
702 
703 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
704 		return -EFAULT;
705 
706 	if (config.flags) /* reserved for future extensions */
707 		return -EINVAL;
708 
709 	/* Check the status of hardware for tiemstamps */
710 	if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
711 		/* Get the current state of the PTP clock */
712 		ptp.u64 = cvmx_read_csr(CVMX_MIO_PTP_CLOCK_CFG);
713 		if (!ptp.s.ext_clk_en) {
714 			/* The clock has not been configured to use an
715 			 * external source.  Program it to use the main clock
716 			 * reference.
717 			 */
718 			u64 clock_comp = (NSEC_PER_SEC << 32) /	octeon_get_io_clock_rate();
719 			if (!ptp.s.ptp_en)
720 				cvmx_write_csr(CVMX_MIO_PTP_CLOCK_COMP, clock_comp);
721 			netdev_info(netdev,
722 				    "PTP Clock using sclk reference @ %lldHz\n",
723 				    (NSEC_PER_SEC << 32) / clock_comp);
724 		} else {
725 			/* The clock is already programmed to use a GPIO */
726 			u64 clock_comp = cvmx_read_csr(CVMX_MIO_PTP_CLOCK_COMP);
727 			netdev_info(netdev,
728 				    "PTP Clock using GPIO%d @ %lld Hz\n",
729 				    ptp.s.ext_clk_in, (NSEC_PER_SEC << 32) / clock_comp);
730 		}
731 
732 		/* Enable the clock if it wasn't done already */
733 		if (!ptp.s.ptp_en) {
734 			ptp.s.ptp_en = 1;
735 			cvmx_write_csr(CVMX_MIO_PTP_CLOCK_CFG, ptp.u64);
736 		}
737 		have_hw_timestamps = true;
738 	}
739 
740 	if (!have_hw_timestamps)
741 		return -EINVAL;
742 
743 	switch (config.tx_type) {
744 	case HWTSTAMP_TX_OFF:
745 	case HWTSTAMP_TX_ON:
746 		break;
747 	default:
748 		return -ERANGE;
749 	}
750 
751 	switch (config.rx_filter) {
752 	case HWTSTAMP_FILTER_NONE:
753 		p->has_rx_tstamp = false;
754 		rxx_frm_ctl.u64 = cvmx_read_csr(p->agl + AGL_GMX_RX_FRM_CTL);
755 		rxx_frm_ctl.s.ptp_mode = 0;
756 		cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
757 		break;
758 	case HWTSTAMP_FILTER_ALL:
759 	case HWTSTAMP_FILTER_SOME:
760 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
761 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
762 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
763 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
764 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
765 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
766 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
767 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
768 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
769 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
770 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
771 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
772 	case HWTSTAMP_FILTER_NTP_ALL:
773 		p->has_rx_tstamp = have_hw_timestamps;
774 		config.rx_filter = HWTSTAMP_FILTER_ALL;
775 		if (p->has_rx_tstamp) {
776 			rxx_frm_ctl.u64 = cvmx_read_csr(p->agl + AGL_GMX_RX_FRM_CTL);
777 			rxx_frm_ctl.s.ptp_mode = 1;
778 			cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
779 		}
780 		break;
781 	default:
782 		return -ERANGE;
783 	}
784 
785 	if (copy_to_user(rq->ifr_data, &config, sizeof(config)))
786 		return -EFAULT;
787 
788 	return 0;
789 }
790 
octeon_mgmt_ioctl(struct net_device * netdev,struct ifreq * rq,int cmd)791 static int octeon_mgmt_ioctl(struct net_device *netdev,
792 			     struct ifreq *rq, int cmd)
793 {
794 	switch (cmd) {
795 	case SIOCSHWTSTAMP:
796 		return octeon_mgmt_ioctl_hwtstamp(netdev, rq, cmd);
797 	default:
798 		if (netdev->phydev)
799 			return phy_mii_ioctl(netdev->phydev, rq, cmd);
800 		return -EINVAL;
801 	}
802 }
803 
octeon_mgmt_disable_link(struct octeon_mgmt * p)804 static void octeon_mgmt_disable_link(struct octeon_mgmt *p)
805 {
806 	union cvmx_agl_gmx_prtx_cfg prtx_cfg;
807 
808 	/* Disable GMX before we make any changes. */
809 	prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
810 	prtx_cfg.s.en = 0;
811 	prtx_cfg.s.tx_en = 0;
812 	prtx_cfg.s.rx_en = 0;
813 	cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
814 
815 	if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
816 		int i;
817 		for (i = 0; i < 10; i++) {
818 			prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
819 			if (prtx_cfg.s.tx_idle == 1 || prtx_cfg.s.rx_idle == 1)
820 				break;
821 			mdelay(1);
822 			i++;
823 		}
824 	}
825 }
826 
octeon_mgmt_enable_link(struct octeon_mgmt * p)827 static void octeon_mgmt_enable_link(struct octeon_mgmt *p)
828 {
829 	union cvmx_agl_gmx_prtx_cfg prtx_cfg;
830 
831 	/* Restore the GMX enable state only if link is set */
832 	prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
833 	prtx_cfg.s.tx_en = 1;
834 	prtx_cfg.s.rx_en = 1;
835 	prtx_cfg.s.en = 1;
836 	cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
837 }
838 
octeon_mgmt_update_link(struct octeon_mgmt * p)839 static void octeon_mgmt_update_link(struct octeon_mgmt *p)
840 {
841 	struct net_device *ndev = p->netdev;
842 	struct phy_device *phydev = ndev->phydev;
843 	union cvmx_agl_gmx_prtx_cfg prtx_cfg;
844 
845 	prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
846 
847 	if (!phydev->link)
848 		prtx_cfg.s.duplex = 1;
849 	else
850 		prtx_cfg.s.duplex = phydev->duplex;
851 
852 	switch (phydev->speed) {
853 	case 10:
854 		prtx_cfg.s.speed = 0;
855 		prtx_cfg.s.slottime = 0;
856 
857 		if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
858 			prtx_cfg.s.burst = 1;
859 			prtx_cfg.s.speed_msb = 1;
860 		}
861 		break;
862 	case 100:
863 		prtx_cfg.s.speed = 0;
864 		prtx_cfg.s.slottime = 0;
865 
866 		if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
867 			prtx_cfg.s.burst = 1;
868 			prtx_cfg.s.speed_msb = 0;
869 		}
870 		break;
871 	case 1000:
872 		/* 1000 MBits is only supported on 6XXX chips */
873 		if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
874 			prtx_cfg.s.speed = 1;
875 			prtx_cfg.s.speed_msb = 0;
876 			/* Only matters for half-duplex */
877 			prtx_cfg.s.slottime = 1;
878 			prtx_cfg.s.burst = phydev->duplex;
879 		}
880 		break;
881 	case 0:  /* No link */
882 	default:
883 		break;
884 	}
885 
886 	/* Write the new GMX setting with the port still disabled. */
887 	cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
888 
889 	/* Read GMX CFG again to make sure the config is completed. */
890 	prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
891 
892 	if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
893 		union cvmx_agl_gmx_txx_clk agl_clk;
894 		union cvmx_agl_prtx_ctl prtx_ctl;
895 
896 		prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
897 		agl_clk.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_CLK);
898 		/* MII (both speeds) and RGMII 1000 speed. */
899 		agl_clk.s.clk_cnt = 1;
900 		if (prtx_ctl.s.mode == 0) { /* RGMII mode */
901 			if (phydev->speed == 10)
902 				agl_clk.s.clk_cnt = 50;
903 			else if (phydev->speed == 100)
904 				agl_clk.s.clk_cnt = 5;
905 		}
906 		cvmx_write_csr(p->agl + AGL_GMX_TX_CLK, agl_clk.u64);
907 	}
908 }
909 
octeon_mgmt_adjust_link(struct net_device * netdev)910 static void octeon_mgmt_adjust_link(struct net_device *netdev)
911 {
912 	struct octeon_mgmt *p = netdev_priv(netdev);
913 	struct phy_device *phydev = netdev->phydev;
914 	unsigned long flags;
915 	int link_changed = 0;
916 
917 	if (!phydev)
918 		return;
919 
920 	spin_lock_irqsave(&p->lock, flags);
921 
922 
923 	if (!phydev->link && p->last_link)
924 		link_changed = -1;
925 
926 	if (phydev->link &&
927 	    (p->last_duplex != phydev->duplex ||
928 	     p->last_link != phydev->link ||
929 	     p->last_speed != phydev->speed)) {
930 		octeon_mgmt_disable_link(p);
931 		link_changed = 1;
932 		octeon_mgmt_update_link(p);
933 		octeon_mgmt_enable_link(p);
934 	}
935 
936 	p->last_link = phydev->link;
937 	p->last_speed = phydev->speed;
938 	p->last_duplex = phydev->duplex;
939 
940 	spin_unlock_irqrestore(&p->lock, flags);
941 
942 	if (link_changed != 0) {
943 		if (link_changed > 0)
944 			netdev_info(netdev, "Link is up - %d/%s\n",
945 				    phydev->speed, phydev->duplex == DUPLEX_FULL ? "Full" : "Half");
946 		else
947 			netdev_info(netdev, "Link is down\n");
948 	}
949 }
950 
octeon_mgmt_init_phy(struct net_device * netdev)951 static int octeon_mgmt_init_phy(struct net_device *netdev)
952 {
953 	struct octeon_mgmt *p = netdev_priv(netdev);
954 	struct phy_device *phydev = NULL;
955 
956 	if (octeon_is_simulation() || p->phy_np == NULL) {
957 		/* No PHYs in the simulator. */
958 		netif_carrier_on(netdev);
959 		return 0;
960 	}
961 
962 	phydev = of_phy_connect(netdev, p->phy_np,
963 				octeon_mgmt_adjust_link, 0,
964 				PHY_INTERFACE_MODE_MII);
965 
966 	if (!phydev)
967 		return -ENODEV;
968 
969 	return 0;
970 }
971 
octeon_mgmt_open(struct net_device * netdev)972 static int octeon_mgmt_open(struct net_device *netdev)
973 {
974 	struct octeon_mgmt *p = netdev_priv(netdev);
975 	union cvmx_mixx_ctl mix_ctl;
976 	union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode;
977 	union cvmx_mixx_oring1 oring1;
978 	union cvmx_mixx_iring1 iring1;
979 	union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl;
980 	union cvmx_mixx_irhwm mix_irhwm;
981 	union cvmx_mixx_orhwm mix_orhwm;
982 	union cvmx_mixx_intena mix_intena;
983 	struct sockaddr sa;
984 
985 	/* Allocate ring buffers.  */
986 	p->tx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
987 			     GFP_KERNEL);
988 	if (!p->tx_ring)
989 		return -ENOMEM;
990 	p->tx_ring_handle =
991 		dma_map_single(p->dev, p->tx_ring,
992 			       ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
993 			       DMA_BIDIRECTIONAL);
994 	p->tx_next = 0;
995 	p->tx_next_clean = 0;
996 	p->tx_current_fill = 0;
997 
998 
999 	p->rx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
1000 			     GFP_KERNEL);
1001 	if (!p->rx_ring)
1002 		goto err_nomem;
1003 	p->rx_ring_handle =
1004 		dma_map_single(p->dev, p->rx_ring,
1005 			       ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
1006 			       DMA_BIDIRECTIONAL);
1007 
1008 	p->rx_next = 0;
1009 	p->rx_next_fill = 0;
1010 	p->rx_current_fill = 0;
1011 
1012 	octeon_mgmt_reset_hw(p);
1013 
1014 	mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
1015 
1016 	/* Bring it out of reset if needed. */
1017 	if (mix_ctl.s.reset) {
1018 		mix_ctl.s.reset = 0;
1019 		cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
1020 		do {
1021 			mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
1022 		} while (mix_ctl.s.reset);
1023 	}
1024 
1025 	if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
1026 		agl_gmx_inf_mode.u64 = 0;
1027 		agl_gmx_inf_mode.s.en = 1;
1028 		cvmx_write_csr(CVMX_AGL_GMX_INF_MODE, agl_gmx_inf_mode.u64);
1029 	}
1030 	if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X)
1031 		|| OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
1032 		/* Force compensation values, as they are not
1033 		 * determined properly by HW
1034 		 */
1035 		union cvmx_agl_gmx_drv_ctl drv_ctl;
1036 
1037 		drv_ctl.u64 = cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL);
1038 		if (p->port) {
1039 			drv_ctl.s.byp_en1 = 1;
1040 			drv_ctl.s.nctl1 = 6;
1041 			drv_ctl.s.pctl1 = 6;
1042 		} else {
1043 			drv_ctl.s.byp_en = 1;
1044 			drv_ctl.s.nctl = 6;
1045 			drv_ctl.s.pctl = 6;
1046 		}
1047 		cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL, drv_ctl.u64);
1048 	}
1049 
1050 	oring1.u64 = 0;
1051 	oring1.s.obase = p->tx_ring_handle >> 3;
1052 	oring1.s.osize = OCTEON_MGMT_TX_RING_SIZE;
1053 	cvmx_write_csr(p->mix + MIX_ORING1, oring1.u64);
1054 
1055 	iring1.u64 = 0;
1056 	iring1.s.ibase = p->rx_ring_handle >> 3;
1057 	iring1.s.isize = OCTEON_MGMT_RX_RING_SIZE;
1058 	cvmx_write_csr(p->mix + MIX_IRING1, iring1.u64);
1059 
1060 	memcpy(sa.sa_data, netdev->dev_addr, ETH_ALEN);
1061 	octeon_mgmt_set_mac_address(netdev, &sa);
1062 
1063 	octeon_mgmt_change_mtu(netdev, netdev->mtu);
1064 
1065 	/* Enable the port HW. Packets are not allowed until
1066 	 * cvmx_mgmt_port_enable() is called.
1067 	 */
1068 	mix_ctl.u64 = 0;
1069 	mix_ctl.s.crc_strip = 1;    /* Strip the ending CRC */
1070 	mix_ctl.s.en = 1;           /* Enable the port */
1071 	mix_ctl.s.nbtarb = 0;       /* Arbitration mode */
1072 	/* MII CB-request FIFO programmable high watermark */
1073 	mix_ctl.s.mrq_hwm = 1;
1074 #ifdef __LITTLE_ENDIAN
1075 	mix_ctl.s.lendian = 1;
1076 #endif
1077 	cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
1078 
1079 	/* Read the PHY to find the mode of the interface. */
1080 	if (octeon_mgmt_init_phy(netdev)) {
1081 		dev_err(p->dev, "Cannot initialize PHY on MIX%d.\n", p->port);
1082 		goto err_noirq;
1083 	}
1084 
1085 	/* Set the mode of the interface, RGMII/MII. */
1086 	if (OCTEON_IS_MODEL(OCTEON_CN6XXX) && netdev->phydev) {
1087 		union cvmx_agl_prtx_ctl agl_prtx_ctl;
1088 		int rgmii_mode = (netdev->phydev->supported &
1089 				  (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)) != 0;
1090 
1091 		agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
1092 		agl_prtx_ctl.s.mode = rgmii_mode ? 0 : 1;
1093 		cvmx_write_csr(p->agl_prt_ctl,	agl_prtx_ctl.u64);
1094 
1095 		/* MII clocks counts are based on the 125Mhz
1096 		 * reference, which has an 8nS period. So our delays
1097 		 * need to be multiplied by this factor.
1098 		 */
1099 #define NS_PER_PHY_CLK 8
1100 
1101 		/* Take the DLL and clock tree out of reset */
1102 		agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
1103 		agl_prtx_ctl.s.clkrst = 0;
1104 		if (rgmii_mode) {
1105 			agl_prtx_ctl.s.dllrst = 0;
1106 			agl_prtx_ctl.s.clktx_byp = 0;
1107 		}
1108 		cvmx_write_csr(p->agl_prt_ctl,	agl_prtx_ctl.u64);
1109 		cvmx_read_csr(p->agl_prt_ctl); /* Force write out before wait */
1110 
1111 		/* Wait for the DLL to lock. External 125 MHz
1112 		 * reference clock must be stable at this point.
1113 		 */
1114 		ndelay(256 * NS_PER_PHY_CLK);
1115 
1116 		/* Enable the interface */
1117 		agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
1118 		agl_prtx_ctl.s.enable = 1;
1119 		cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64);
1120 
1121 		/* Read the value back to force the previous write */
1122 		agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
1123 
1124 		/* Enable the compensation controller */
1125 		agl_prtx_ctl.s.comp = 1;
1126 		agl_prtx_ctl.s.drv_byp = 0;
1127 		cvmx_write_csr(p->agl_prt_ctl,	agl_prtx_ctl.u64);
1128 		/* Force write out before wait. */
1129 		cvmx_read_csr(p->agl_prt_ctl);
1130 
1131 		/* For compensation state to lock. */
1132 		ndelay(1040 * NS_PER_PHY_CLK);
1133 
1134 		/* Default Interframe Gaps are too small.  Recommended
1135 		 * workaround is.
1136 		 *
1137 		 * AGL_GMX_TX_IFG[IFG1]=14
1138 		 * AGL_GMX_TX_IFG[IFG2]=10
1139 		 */
1140 		cvmx_write_csr(CVMX_AGL_GMX_TX_IFG, 0xae);
1141 	}
1142 
1143 	octeon_mgmt_rx_fill_ring(netdev);
1144 
1145 	/* Clear statistics. */
1146 	/* Clear on read. */
1147 	cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_CTL, 1);
1148 	cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP, 0);
1149 	cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD, 0);
1150 
1151 	cvmx_write_csr(p->agl + AGL_GMX_TX_STATS_CTL, 1);
1152 	cvmx_write_csr(p->agl + AGL_GMX_TX_STAT0, 0);
1153 	cvmx_write_csr(p->agl + AGL_GMX_TX_STAT1, 0);
1154 
1155 	/* Clear any pending interrupts */
1156 	cvmx_write_csr(p->mix + MIX_ISR, cvmx_read_csr(p->mix + MIX_ISR));
1157 
1158 	if (request_irq(p->irq, octeon_mgmt_interrupt, 0, netdev->name,
1159 			netdev)) {
1160 		dev_err(p->dev, "request_irq(%d) failed.\n", p->irq);
1161 		goto err_noirq;
1162 	}
1163 
1164 	/* Interrupt every single RX packet */
1165 	mix_irhwm.u64 = 0;
1166 	mix_irhwm.s.irhwm = 0;
1167 	cvmx_write_csr(p->mix + MIX_IRHWM, mix_irhwm.u64);
1168 
1169 	/* Interrupt when we have 1 or more packets to clean.  */
1170 	mix_orhwm.u64 = 0;
1171 	mix_orhwm.s.orhwm = 0;
1172 	cvmx_write_csr(p->mix + MIX_ORHWM, mix_orhwm.u64);
1173 
1174 	/* Enable receive and transmit interrupts */
1175 	mix_intena.u64 = 0;
1176 	mix_intena.s.ithena = 1;
1177 	mix_intena.s.othena = 1;
1178 	cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
1179 
1180 	/* Enable packet I/O. */
1181 
1182 	rxx_frm_ctl.u64 = 0;
1183 	rxx_frm_ctl.s.ptp_mode = p->has_rx_tstamp ? 1 : 0;
1184 	rxx_frm_ctl.s.pre_align = 1;
1185 	/* When set, disables the length check for non-min sized pkts
1186 	 * with padding in the client data.
1187 	 */
1188 	rxx_frm_ctl.s.pad_len = 1;
1189 	/* When set, disables the length check for VLAN pkts */
1190 	rxx_frm_ctl.s.vlan_len = 1;
1191 	/* When set, PREAMBLE checking is  less strict */
1192 	rxx_frm_ctl.s.pre_free = 1;
1193 	/* Control Pause Frames can match station SMAC */
1194 	rxx_frm_ctl.s.ctl_smac = 0;
1195 	/* Control Pause Frames can match globally assign Multicast address */
1196 	rxx_frm_ctl.s.ctl_mcst = 1;
1197 	/* Forward pause information to TX block */
1198 	rxx_frm_ctl.s.ctl_bck = 1;
1199 	/* Drop Control Pause Frames */
1200 	rxx_frm_ctl.s.ctl_drp = 1;
1201 	/* Strip off the preamble */
1202 	rxx_frm_ctl.s.pre_strp = 1;
1203 	/* This port is configured to send PREAMBLE+SFD to begin every
1204 	 * frame.  GMX checks that the PREAMBLE is sent correctly.
1205 	 */
1206 	rxx_frm_ctl.s.pre_chk = 1;
1207 	cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
1208 
1209 	/* Configure the port duplex, speed and enables */
1210 	octeon_mgmt_disable_link(p);
1211 	if (netdev->phydev)
1212 		octeon_mgmt_update_link(p);
1213 	octeon_mgmt_enable_link(p);
1214 
1215 	p->last_link = 0;
1216 	p->last_speed = 0;
1217 	/* PHY is not present in simulator. The carrier is enabled
1218 	 * while initializing the phy for simulator, leave it enabled.
1219 	 */
1220 	if (netdev->phydev) {
1221 		netif_carrier_off(netdev);
1222 		phy_start_aneg(netdev->phydev);
1223 	}
1224 
1225 	netif_wake_queue(netdev);
1226 	napi_enable(&p->napi);
1227 
1228 	return 0;
1229 err_noirq:
1230 	octeon_mgmt_reset_hw(p);
1231 	dma_unmap_single(p->dev, p->rx_ring_handle,
1232 			 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
1233 			 DMA_BIDIRECTIONAL);
1234 	kfree(p->rx_ring);
1235 err_nomem:
1236 	dma_unmap_single(p->dev, p->tx_ring_handle,
1237 			 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
1238 			 DMA_BIDIRECTIONAL);
1239 	kfree(p->tx_ring);
1240 	return -ENOMEM;
1241 }
1242 
octeon_mgmt_stop(struct net_device * netdev)1243 static int octeon_mgmt_stop(struct net_device *netdev)
1244 {
1245 	struct octeon_mgmt *p = netdev_priv(netdev);
1246 
1247 	napi_disable(&p->napi);
1248 	netif_stop_queue(netdev);
1249 
1250 	if (netdev->phydev)
1251 		phy_disconnect(netdev->phydev);
1252 
1253 	netif_carrier_off(netdev);
1254 
1255 	octeon_mgmt_reset_hw(p);
1256 
1257 	free_irq(p->irq, netdev);
1258 
1259 	/* dma_unmap is a nop on Octeon, so just free everything.  */
1260 	skb_queue_purge(&p->tx_list);
1261 	skb_queue_purge(&p->rx_list);
1262 
1263 	dma_unmap_single(p->dev, p->rx_ring_handle,
1264 			 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
1265 			 DMA_BIDIRECTIONAL);
1266 	kfree(p->rx_ring);
1267 
1268 	dma_unmap_single(p->dev, p->tx_ring_handle,
1269 			 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
1270 			 DMA_BIDIRECTIONAL);
1271 	kfree(p->tx_ring);
1272 
1273 	return 0;
1274 }
1275 
1276 static netdev_tx_t
octeon_mgmt_xmit(struct sk_buff * skb,struct net_device * netdev)1277 octeon_mgmt_xmit(struct sk_buff *skb, struct net_device *netdev)
1278 {
1279 	struct octeon_mgmt *p = netdev_priv(netdev);
1280 	union mgmt_port_ring_entry re;
1281 	unsigned long flags;
1282 	netdev_tx_t rv = NETDEV_TX_BUSY;
1283 
1284 	re.d64 = 0;
1285 	re.s.tstamp = ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) != 0);
1286 	re.s.len = skb->len;
1287 	re.s.addr = dma_map_single(p->dev, skb->data,
1288 				   skb->len,
1289 				   DMA_TO_DEVICE);
1290 
1291 	spin_lock_irqsave(&p->tx_list.lock, flags);
1292 
1293 	if (unlikely(p->tx_current_fill >= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE) - 1)) {
1294 		spin_unlock_irqrestore(&p->tx_list.lock, flags);
1295 		netif_stop_queue(netdev);
1296 		spin_lock_irqsave(&p->tx_list.lock, flags);
1297 	}
1298 
1299 	if (unlikely(p->tx_current_fill >=
1300 		     ring_max_fill(OCTEON_MGMT_TX_RING_SIZE))) {
1301 		spin_unlock_irqrestore(&p->tx_list.lock, flags);
1302 		dma_unmap_single(p->dev, re.s.addr, re.s.len,
1303 				 DMA_TO_DEVICE);
1304 		goto out;
1305 	}
1306 
1307 	__skb_queue_tail(&p->tx_list, skb);
1308 
1309 	/* Put it in the ring.  */
1310 	p->tx_ring[p->tx_next] = re.d64;
1311 	p->tx_next = (p->tx_next + 1) % OCTEON_MGMT_TX_RING_SIZE;
1312 	p->tx_current_fill++;
1313 
1314 	spin_unlock_irqrestore(&p->tx_list.lock, flags);
1315 
1316 	dma_sync_single_for_device(p->dev, p->tx_ring_handle,
1317 				   ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
1318 				   DMA_BIDIRECTIONAL);
1319 
1320 	netdev->stats.tx_packets++;
1321 	netdev->stats.tx_bytes += skb->len;
1322 
1323 	/* Ring the bell.  */
1324 	cvmx_write_csr(p->mix + MIX_ORING2, 1);
1325 
1326 	netif_trans_update(netdev);
1327 	rv = NETDEV_TX_OK;
1328 out:
1329 	octeon_mgmt_update_tx_stats(netdev);
1330 	return rv;
1331 }
1332 
1333 #ifdef CONFIG_NET_POLL_CONTROLLER
octeon_mgmt_poll_controller(struct net_device * netdev)1334 static void octeon_mgmt_poll_controller(struct net_device *netdev)
1335 {
1336 	struct octeon_mgmt *p = netdev_priv(netdev);
1337 
1338 	octeon_mgmt_receive_packets(p, 16);
1339 	octeon_mgmt_update_rx_stats(netdev);
1340 }
1341 #endif
1342 
octeon_mgmt_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * info)1343 static void octeon_mgmt_get_drvinfo(struct net_device *netdev,
1344 				    struct ethtool_drvinfo *info)
1345 {
1346 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1347 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1348 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1349 	strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
1350 }
1351 
octeon_mgmt_nway_reset(struct net_device * dev)1352 static int octeon_mgmt_nway_reset(struct net_device *dev)
1353 {
1354 	if (!capable(CAP_NET_ADMIN))
1355 		return -EPERM;
1356 
1357 	if (dev->phydev)
1358 		return phy_start_aneg(dev->phydev);
1359 
1360 	return -EOPNOTSUPP;
1361 }
1362 
1363 static const struct ethtool_ops octeon_mgmt_ethtool_ops = {
1364 	.get_drvinfo = octeon_mgmt_get_drvinfo,
1365 	.nway_reset = octeon_mgmt_nway_reset,
1366 	.get_link = ethtool_op_get_link,
1367 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
1368 	.set_link_ksettings = phy_ethtool_set_link_ksettings,
1369 };
1370 
1371 static const struct net_device_ops octeon_mgmt_ops = {
1372 	.ndo_open =			octeon_mgmt_open,
1373 	.ndo_stop =			octeon_mgmt_stop,
1374 	.ndo_start_xmit =		octeon_mgmt_xmit,
1375 	.ndo_set_rx_mode =		octeon_mgmt_set_rx_filtering,
1376 	.ndo_set_mac_address =		octeon_mgmt_set_mac_address,
1377 	.ndo_do_ioctl =			octeon_mgmt_ioctl,
1378 	.ndo_change_mtu =		octeon_mgmt_change_mtu,
1379 #ifdef CONFIG_NET_POLL_CONTROLLER
1380 	.ndo_poll_controller =		octeon_mgmt_poll_controller,
1381 #endif
1382 };
1383 
octeon_mgmt_probe(struct platform_device * pdev)1384 static int octeon_mgmt_probe(struct platform_device *pdev)
1385 {
1386 	struct net_device *netdev;
1387 	struct octeon_mgmt *p;
1388 	const __be32 *data;
1389 	const u8 *mac;
1390 	struct resource *res_mix;
1391 	struct resource *res_agl;
1392 	struct resource *res_agl_prt_ctl;
1393 	int len;
1394 	int result;
1395 
1396 	netdev = alloc_etherdev(sizeof(struct octeon_mgmt));
1397 	if (netdev == NULL)
1398 		return -ENOMEM;
1399 
1400 	SET_NETDEV_DEV(netdev, &pdev->dev);
1401 
1402 	platform_set_drvdata(pdev, netdev);
1403 	p = netdev_priv(netdev);
1404 	netif_napi_add(netdev, &p->napi, octeon_mgmt_napi_poll,
1405 		       OCTEON_MGMT_NAPI_WEIGHT);
1406 
1407 	p->netdev = netdev;
1408 	p->dev = &pdev->dev;
1409 	p->has_rx_tstamp = false;
1410 
1411 	data = of_get_property(pdev->dev.of_node, "cell-index", &len);
1412 	if (data && len == sizeof(*data)) {
1413 		p->port = be32_to_cpup(data);
1414 	} else {
1415 		dev_err(&pdev->dev, "no 'cell-index' property\n");
1416 		result = -ENXIO;
1417 		goto err;
1418 	}
1419 
1420 	snprintf(netdev->name, IFNAMSIZ, "mgmt%d", p->port);
1421 
1422 	result = platform_get_irq(pdev, 0);
1423 	if (result < 0)
1424 		goto err;
1425 
1426 	p->irq = result;
1427 
1428 	res_mix = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1429 	if (res_mix == NULL) {
1430 		dev_err(&pdev->dev, "no 'reg' resource\n");
1431 		result = -ENXIO;
1432 		goto err;
1433 	}
1434 
1435 	res_agl = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1436 	if (res_agl == NULL) {
1437 		dev_err(&pdev->dev, "no 'reg' resource\n");
1438 		result = -ENXIO;
1439 		goto err;
1440 	}
1441 
1442 	res_agl_prt_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1443 	if (res_agl_prt_ctl == NULL) {
1444 		dev_err(&pdev->dev, "no 'reg' resource\n");
1445 		result = -ENXIO;
1446 		goto err;
1447 	}
1448 
1449 	p->mix_phys = res_mix->start;
1450 	p->mix_size = resource_size(res_mix);
1451 	p->agl_phys = res_agl->start;
1452 	p->agl_size = resource_size(res_agl);
1453 	p->agl_prt_ctl_phys = res_agl_prt_ctl->start;
1454 	p->agl_prt_ctl_size = resource_size(res_agl_prt_ctl);
1455 
1456 
1457 	if (!devm_request_mem_region(&pdev->dev, p->mix_phys, p->mix_size,
1458 				     res_mix->name)) {
1459 		dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
1460 			res_mix->name);
1461 		result = -ENXIO;
1462 		goto err;
1463 	}
1464 
1465 	if (!devm_request_mem_region(&pdev->dev, p->agl_phys, p->agl_size,
1466 				     res_agl->name)) {
1467 		result = -ENXIO;
1468 		dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
1469 			res_agl->name);
1470 		goto err;
1471 	}
1472 
1473 	if (!devm_request_mem_region(&pdev->dev, p->agl_prt_ctl_phys,
1474 				     p->agl_prt_ctl_size, res_agl_prt_ctl->name)) {
1475 		result = -ENXIO;
1476 		dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
1477 			res_agl_prt_ctl->name);
1478 		goto err;
1479 	}
1480 
1481 	p->mix = (u64)devm_ioremap(&pdev->dev, p->mix_phys, p->mix_size);
1482 	p->agl = (u64)devm_ioremap(&pdev->dev, p->agl_phys, p->agl_size);
1483 	p->agl_prt_ctl = (u64)devm_ioremap(&pdev->dev, p->agl_prt_ctl_phys,
1484 					   p->agl_prt_ctl_size);
1485 	if (!p->mix || !p->agl || !p->agl_prt_ctl) {
1486 		dev_err(&pdev->dev, "failed to map I/O memory\n");
1487 		result = -ENOMEM;
1488 		goto err;
1489 	}
1490 
1491 	spin_lock_init(&p->lock);
1492 
1493 	skb_queue_head_init(&p->tx_list);
1494 	skb_queue_head_init(&p->rx_list);
1495 	tasklet_init(&p->tx_clean_tasklet,
1496 		     octeon_mgmt_clean_tx_tasklet, (unsigned long)p);
1497 
1498 	netdev->priv_flags |= IFF_UNICAST_FLT;
1499 
1500 	netdev->netdev_ops = &octeon_mgmt_ops;
1501 	netdev->ethtool_ops = &octeon_mgmt_ethtool_ops;
1502 
1503 	netdev->min_mtu = 64 - OCTEON_MGMT_RX_HEADROOM;
1504 	netdev->max_mtu = 16383 - OCTEON_MGMT_RX_HEADROOM - VLAN_HLEN;
1505 
1506 	mac = of_get_mac_address(pdev->dev.of_node);
1507 
1508 	if (mac)
1509 		memcpy(netdev->dev_addr, mac, ETH_ALEN);
1510 	else
1511 		eth_hw_addr_random(netdev);
1512 
1513 	p->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
1514 
1515 	result = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1516 	if (result)
1517 		goto err;
1518 
1519 	netif_carrier_off(netdev);
1520 	result = register_netdev(netdev);
1521 	if (result)
1522 		goto err;
1523 
1524 	dev_info(&pdev->dev, "Version " DRV_VERSION "\n");
1525 	return 0;
1526 
1527 err:
1528 	of_node_put(p->phy_np);
1529 	free_netdev(netdev);
1530 	return result;
1531 }
1532 
octeon_mgmt_remove(struct platform_device * pdev)1533 static int octeon_mgmt_remove(struct platform_device *pdev)
1534 {
1535 	struct net_device *netdev = platform_get_drvdata(pdev);
1536 	struct octeon_mgmt *p = netdev_priv(netdev);
1537 
1538 	unregister_netdev(netdev);
1539 	of_node_put(p->phy_np);
1540 	free_netdev(netdev);
1541 	return 0;
1542 }
1543 
1544 static const struct of_device_id octeon_mgmt_match[] = {
1545 	{
1546 		.compatible = "cavium,octeon-5750-mix",
1547 	},
1548 	{},
1549 };
1550 MODULE_DEVICE_TABLE(of, octeon_mgmt_match);
1551 
1552 static struct platform_driver octeon_mgmt_driver = {
1553 	.driver = {
1554 		.name		= "octeon_mgmt",
1555 		.of_match_table = octeon_mgmt_match,
1556 	},
1557 	.probe		= octeon_mgmt_probe,
1558 	.remove		= octeon_mgmt_remove,
1559 };
1560 
1561 extern void octeon_mdiobus_force_mod_depencency(void);
1562 
octeon_mgmt_mod_init(void)1563 static int __init octeon_mgmt_mod_init(void)
1564 {
1565 	/* Force our mdiobus driver module to be loaded first. */
1566 	octeon_mdiobus_force_mod_depencency();
1567 	return platform_driver_register(&octeon_mgmt_driver);
1568 }
1569 
octeon_mgmt_mod_exit(void)1570 static void __exit octeon_mgmt_mod_exit(void)
1571 {
1572 	platform_driver_unregister(&octeon_mgmt_driver);
1573 }
1574 
1575 module_init(octeon_mgmt_mod_init);
1576 module_exit(octeon_mgmt_mod_exit);
1577 
1578 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1579 MODULE_AUTHOR("David Daney");
1580 MODULE_LICENSE("GPL");
1581 MODULE_VERSION(DRV_VERSION);
1582