xref: /openbmc/linux/drivers/net/rionet.c (revision fd589a8f)
1 /*
2  * rionet - Ethernet driver over RapidIO messaging services
3  *
4  * Copyright 2005 MontaVista Software, Inc.
5  * Matt Porter <mporter@kernel.crashing.org>
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/delay.h>
17 #include <linux/rio.h>
18 #include <linux/rio_drv.h>
19 #include <linux/rio_ids.h>
20 
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/crc32.h>
25 #include <linux/ethtool.h>
26 
27 #define DRV_NAME        "rionet"
28 #define DRV_VERSION     "0.2"
29 #define DRV_AUTHOR      "Matt Porter <mporter@kernel.crashing.org>"
30 #define DRV_DESC        "Ethernet over RapidIO"
31 
32 MODULE_AUTHOR(DRV_AUTHOR);
33 MODULE_DESCRIPTION(DRV_DESC);
34 MODULE_LICENSE("GPL");
35 
36 #define RIONET_DEFAULT_MSGLEVEL \
37 			(NETIF_MSG_DRV          | \
38 			 NETIF_MSG_LINK         | \
39 			 NETIF_MSG_RX_ERR       | \
40 			 NETIF_MSG_TX_ERR)
41 
42 #define RIONET_DOORBELL_JOIN	0x1000
43 #define RIONET_DOORBELL_LEAVE	0x1001
44 
45 #define RIONET_MAILBOX		0
46 
47 #define RIONET_TX_RING_SIZE	CONFIG_RIONET_TX_SIZE
48 #define RIONET_RX_RING_SIZE	CONFIG_RIONET_RX_SIZE
49 
50 static LIST_HEAD(rionet_peers);
51 
52 struct rionet_private {
53 	struct rio_mport *mport;
54 	struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
55 	struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
56 	int rx_slot;
57 	int tx_slot;
58 	int tx_cnt;
59 	int ack_slot;
60 	spinlock_t lock;
61 	spinlock_t tx_lock;
62 	u32 msg_enable;
63 };
64 
65 struct rionet_peer {
66 	struct list_head node;
67 	struct rio_dev *rdev;
68 	struct resource *res;
69 };
70 
71 static int rionet_check = 0;
72 static int rionet_capable = 1;
73 
74 /*
75  * This is a fast lookup table for translating TX
76  * Ethernet packets into a destination RIO device. It
77  * could be made into a hash table to save memory depending
78  * on system trade-offs.
79  */
80 static struct rio_dev **rionet_active;
81 
82 #define is_rionet_capable(pef, src_ops, dst_ops)		\
83 			((pef & RIO_PEF_INB_MBOX) &&		\
84 			 (pef & RIO_PEF_INB_DOORBELL) &&	\
85 			 (src_ops & RIO_SRC_OPS_DOORBELL) &&	\
86 			 (dst_ops & RIO_DST_OPS_DOORBELL))
87 #define dev_rionet_capable(dev) \
88 	is_rionet_capable(dev->pef, dev->src_ops, dev->dst_ops)
89 
90 #define RIONET_MAC_MATCH(x)	(*(u32 *)x == 0x00010001)
91 #define RIONET_GET_DESTID(x)	(*(u16 *)(x + 4))
92 
93 static int rionet_rx_clean(struct net_device *ndev)
94 {
95 	int i;
96 	int error = 0;
97 	struct rionet_private *rnet = netdev_priv(ndev);
98 	void *data;
99 
100 	i = rnet->rx_slot;
101 
102 	do {
103 		if (!rnet->rx_skb[i])
104 			continue;
105 
106 		if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
107 			break;
108 
109 		rnet->rx_skb[i]->data = data;
110 		skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
111 		rnet->rx_skb[i]->protocol =
112 		    eth_type_trans(rnet->rx_skb[i], ndev);
113 		error = netif_rx(rnet->rx_skb[i]);
114 
115 		if (error == NET_RX_DROP) {
116 			ndev->stats.rx_dropped++;
117 		} else {
118 			ndev->stats.rx_packets++;
119 			ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
120 		}
121 
122 	} while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
123 
124 	return i;
125 }
126 
127 static void rionet_rx_fill(struct net_device *ndev, int end)
128 {
129 	int i;
130 	struct rionet_private *rnet = netdev_priv(ndev);
131 
132 	i = rnet->rx_slot;
133 	do {
134 		rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
135 
136 		if (!rnet->rx_skb[i])
137 			break;
138 
139 		rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
140 				   rnet->rx_skb[i]->data);
141 	} while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
142 
143 	rnet->rx_slot = i;
144 }
145 
146 static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
147 			       struct rio_dev *rdev)
148 {
149 	struct rionet_private *rnet = netdev_priv(ndev);
150 
151 	rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
152 	rnet->tx_skb[rnet->tx_slot] = skb;
153 
154 	ndev->stats.tx_packets++;
155 	ndev->stats.tx_bytes += skb->len;
156 
157 	if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
158 		netif_stop_queue(ndev);
159 
160 	++rnet->tx_slot;
161 	rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
162 
163 	if (netif_msg_tx_queued(rnet))
164 		printk(KERN_INFO "%s: queued skb %8.8x len %8.8x\n", DRV_NAME,
165 		       (u32) skb, skb->len);
166 
167 	return 0;
168 }
169 
170 static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
171 {
172 	int i;
173 	struct rionet_private *rnet = netdev_priv(ndev);
174 	struct ethhdr *eth = (struct ethhdr *)skb->data;
175 	u16 destid;
176 	unsigned long flags;
177 
178 	local_irq_save(flags);
179 	if (!spin_trylock(&rnet->tx_lock)) {
180 		local_irq_restore(flags);
181 		return NETDEV_TX_LOCKED;
182 	}
183 
184 	if ((rnet->tx_cnt + 1) > RIONET_TX_RING_SIZE) {
185 		netif_stop_queue(ndev);
186 		spin_unlock_irqrestore(&rnet->tx_lock, flags);
187 		printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
188 		       ndev->name);
189 		return NETDEV_TX_BUSY;
190 	}
191 
192 	if (eth->h_dest[0] & 0x01) {
193 		for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
194 				i++)
195 			if (rionet_active[i])
196 				rionet_queue_tx_msg(skb, ndev,
197 						    rionet_active[i]);
198 	} else if (RIONET_MAC_MATCH(eth->h_dest)) {
199 		destid = RIONET_GET_DESTID(eth->h_dest);
200 		if (rionet_active[destid])
201 			rionet_queue_tx_msg(skb, ndev, rionet_active[destid]);
202 	}
203 
204 	spin_unlock_irqrestore(&rnet->tx_lock, flags);
205 
206 	return NETDEV_TX_OK;
207 }
208 
209 static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
210 			       u16 info)
211 {
212 	struct net_device *ndev = dev_id;
213 	struct rionet_private *rnet = netdev_priv(ndev);
214 	struct rionet_peer *peer;
215 
216 	if (netif_msg_intr(rnet))
217 		printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
218 		       DRV_NAME, sid, tid, info);
219 	if (info == RIONET_DOORBELL_JOIN) {
220 		if (!rionet_active[sid]) {
221 			list_for_each_entry(peer, &rionet_peers, node) {
222 				if (peer->rdev->destid == sid)
223 					rionet_active[sid] = peer->rdev;
224 			}
225 			rio_mport_send_doorbell(mport, sid,
226 						RIONET_DOORBELL_JOIN);
227 		}
228 	} else if (info == RIONET_DOORBELL_LEAVE) {
229 		rionet_active[sid] = NULL;
230 	} else {
231 		if (netif_msg_intr(rnet))
232 			printk(KERN_WARNING "%s: unhandled doorbell\n",
233 			       DRV_NAME);
234 	}
235 }
236 
237 static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
238 {
239 	int n;
240 	struct net_device *ndev = dev_id;
241 	struct rionet_private *rnet = netdev_priv(ndev);
242 
243 	if (netif_msg_intr(rnet))
244 		printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
245 		       DRV_NAME, mbox, slot);
246 
247 	spin_lock(&rnet->lock);
248 	if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
249 		rionet_rx_fill(ndev, n);
250 	spin_unlock(&rnet->lock);
251 }
252 
253 static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
254 {
255 	struct net_device *ndev = dev_id;
256 	struct rionet_private *rnet = netdev_priv(ndev);
257 
258 	spin_lock(&rnet->lock);
259 
260 	if (netif_msg_intr(rnet))
261 		printk(KERN_INFO
262 		       "%s: outbound message event, mbox %d slot %d\n",
263 		       DRV_NAME, mbox, slot);
264 
265 	while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
266 		/* dma unmap single */
267 		dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
268 		rnet->tx_skb[rnet->ack_slot] = NULL;
269 		++rnet->ack_slot;
270 		rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
271 		rnet->tx_cnt--;
272 	}
273 
274 	if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
275 		netif_wake_queue(ndev);
276 
277 	spin_unlock(&rnet->lock);
278 }
279 
280 static int rionet_open(struct net_device *ndev)
281 {
282 	int i, rc = 0;
283 	struct rionet_peer *peer, *tmp;
284 	u32 pwdcsr;
285 	struct rionet_private *rnet = netdev_priv(ndev);
286 
287 	if (netif_msg_ifup(rnet))
288 		printk(KERN_INFO "%s: open\n", DRV_NAME);
289 
290 	if ((rc = rio_request_inb_dbell(rnet->mport,
291 					(void *)ndev,
292 					RIONET_DOORBELL_JOIN,
293 					RIONET_DOORBELL_LEAVE,
294 					rionet_dbell_event)) < 0)
295 		goto out;
296 
297 	if ((rc = rio_request_inb_mbox(rnet->mport,
298 				       (void *)ndev,
299 				       RIONET_MAILBOX,
300 				       RIONET_RX_RING_SIZE,
301 				       rionet_inb_msg_event)) < 0)
302 		goto out;
303 
304 	if ((rc = rio_request_outb_mbox(rnet->mport,
305 					(void *)ndev,
306 					RIONET_MAILBOX,
307 					RIONET_TX_RING_SIZE,
308 					rionet_outb_msg_event)) < 0)
309 		goto out;
310 
311 	/* Initialize inbound message ring */
312 	for (i = 0; i < RIONET_RX_RING_SIZE; i++)
313 		rnet->rx_skb[i] = NULL;
314 	rnet->rx_slot = 0;
315 	rionet_rx_fill(ndev, 0);
316 
317 	rnet->tx_slot = 0;
318 	rnet->tx_cnt = 0;
319 	rnet->ack_slot = 0;
320 
321 	netif_carrier_on(ndev);
322 	netif_start_queue(ndev);
323 
324 	list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
325 		if (!(peer->res = rio_request_outb_dbell(peer->rdev,
326 							 RIONET_DOORBELL_JOIN,
327 							 RIONET_DOORBELL_LEAVE)))
328 		{
329 			printk(KERN_ERR "%s: error requesting doorbells\n",
330 			       DRV_NAME);
331 			continue;
332 		}
333 
334 		/*
335 		 * If device has initialized inbound doorbells,
336 		 * send a join message
337 		 */
338 		rio_read_config_32(peer->rdev, RIO_WRITE_PORT_CSR, &pwdcsr);
339 		if (pwdcsr & RIO_DOORBELL_AVAIL)
340 			rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
341 	}
342 
343       out:
344 	return rc;
345 }
346 
347 static int rionet_close(struct net_device *ndev)
348 {
349 	struct rionet_private *rnet = netdev_priv(ndev);
350 	struct rionet_peer *peer, *tmp;
351 	int i;
352 
353 	if (netif_msg_ifup(rnet))
354 		printk(KERN_INFO "%s: close\n", DRV_NAME);
355 
356 	netif_stop_queue(ndev);
357 	netif_carrier_off(ndev);
358 
359 	for (i = 0; i < RIONET_RX_RING_SIZE; i++)
360 		kfree_skb(rnet->rx_skb[i]);
361 
362 	list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
363 		if (rionet_active[peer->rdev->destid]) {
364 			rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
365 			rionet_active[peer->rdev->destid] = NULL;
366 		}
367 		rio_release_outb_dbell(peer->rdev, peer->res);
368 	}
369 
370 	rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
371 			      RIONET_DOORBELL_LEAVE);
372 	rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
373 	rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
374 
375 	return 0;
376 }
377 
378 static void rionet_remove(struct rio_dev *rdev)
379 {
380 	struct net_device *ndev = NULL;
381 	struct rionet_peer *peer, *tmp;
382 
383 	free_pages((unsigned long)rionet_active, rdev->net->hport->sys_size ?
384 					__ilog2(sizeof(void *)) + 4 : 0);
385 	unregister_netdev(ndev);
386 	kfree(ndev);
387 
388 	list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
389 		list_del(&peer->node);
390 		kfree(peer);
391 	}
392 }
393 
394 static void rionet_get_drvinfo(struct net_device *ndev,
395 			       struct ethtool_drvinfo *info)
396 {
397 	struct rionet_private *rnet = netdev_priv(ndev);
398 
399 	strcpy(info->driver, DRV_NAME);
400 	strcpy(info->version, DRV_VERSION);
401 	strcpy(info->fw_version, "n/a");
402 	strcpy(info->bus_info, rnet->mport->name);
403 }
404 
405 static u32 rionet_get_msglevel(struct net_device *ndev)
406 {
407 	struct rionet_private *rnet = netdev_priv(ndev);
408 
409 	return rnet->msg_enable;
410 }
411 
412 static void rionet_set_msglevel(struct net_device *ndev, u32 value)
413 {
414 	struct rionet_private *rnet = netdev_priv(ndev);
415 
416 	rnet->msg_enable = value;
417 }
418 
419 static const struct ethtool_ops rionet_ethtool_ops = {
420 	.get_drvinfo = rionet_get_drvinfo,
421 	.get_msglevel = rionet_get_msglevel,
422 	.set_msglevel = rionet_set_msglevel,
423 	.get_link = ethtool_op_get_link,
424 };
425 
426 static const struct net_device_ops rionet_netdev_ops = {
427 	.ndo_open		= rionet_open,
428 	.ndo_stop		= rionet_close,
429 	.ndo_start_xmit		= rionet_start_xmit,
430 	.ndo_change_mtu		= eth_change_mtu,
431 	.ndo_validate_addr	= eth_validate_addr,
432 	.ndo_set_mac_address	= eth_mac_addr,
433 };
434 
435 static int rionet_setup_netdev(struct rio_mport *mport)
436 {
437 	int rc = 0;
438 	struct net_device *ndev = NULL;
439 	struct rionet_private *rnet;
440 	u16 device_id;
441 
442 	/* Allocate our net_device structure */
443 	ndev = alloc_etherdev(sizeof(struct rionet_private));
444 	if (ndev == NULL) {
445 		printk(KERN_INFO "%s: could not allocate ethernet device.\n",
446 		       DRV_NAME);
447 		rc = -ENOMEM;
448 		goto out;
449 	}
450 
451 	rionet_active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
452 			mport->sys_size ? __ilog2(sizeof(void *)) + 4 : 0);
453 	if (!rionet_active) {
454 		rc = -ENOMEM;
455 		goto out;
456 	}
457 	memset((void *)rionet_active, 0, sizeof(void *) *
458 				RIO_MAX_ROUTE_ENTRIES(mport->sys_size));
459 
460 	/* Set up private area */
461 	rnet = netdev_priv(ndev);
462 	rnet->mport = mport;
463 
464 	/* Set the default MAC address */
465 	device_id = rio_local_get_device_id(mport);
466 	ndev->dev_addr[0] = 0x00;
467 	ndev->dev_addr[1] = 0x01;
468 	ndev->dev_addr[2] = 0x00;
469 	ndev->dev_addr[3] = 0x01;
470 	ndev->dev_addr[4] = device_id >> 8;
471 	ndev->dev_addr[5] = device_id & 0xff;
472 
473 	ndev->netdev_ops = &rionet_netdev_ops;
474 	ndev->mtu = RIO_MAX_MSG_SIZE - 14;
475 	ndev->features = NETIF_F_LLTX;
476 	SET_ETHTOOL_OPS(ndev, &rionet_ethtool_ops);
477 
478 	spin_lock_init(&rnet->lock);
479 	spin_lock_init(&rnet->tx_lock);
480 
481 	rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
482 
483 	rc = register_netdev(ndev);
484 	if (rc != 0)
485 		goto out;
486 
487 	printk("%s: %s %s Version %s, MAC %pM\n",
488 	       ndev->name,
489 	       DRV_NAME,
490 	       DRV_DESC,
491 	       DRV_VERSION,
492 	       ndev->dev_addr);
493 
494       out:
495 	return rc;
496 }
497 
498 /*
499  * XXX Make multi-net safe
500  */
501 static int rionet_probe(struct rio_dev *rdev, const struct rio_device_id *id)
502 {
503 	int rc = -ENODEV;
504 	u32 lpef, lsrc_ops, ldst_ops;
505 	struct rionet_peer *peer;
506 
507 	/* If local device is not rionet capable, give up quickly */
508 	if (!rionet_capable)
509 		goto out;
510 
511 	/*
512 	 * First time through, make sure local device is rionet
513 	 * capable, setup netdev,  and set flags so this is skipped
514 	 * on later probes
515 	 */
516 	if (!rionet_check) {
517 		rio_local_read_config_32(rdev->net->hport, RIO_PEF_CAR, &lpef);
518 		rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
519 					 &lsrc_ops);
520 		rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
521 					 &ldst_ops);
522 		if (!is_rionet_capable(lpef, lsrc_ops, ldst_ops)) {
523 			printk(KERN_ERR
524 			       "%s: local device is not network capable\n",
525 			       DRV_NAME);
526 			rionet_check = 1;
527 			rionet_capable = 0;
528 			goto out;
529 		}
530 
531 		rc = rionet_setup_netdev(rdev->net->hport);
532 		rionet_check = 1;
533 	}
534 
535 	/*
536 	 * If the remote device has mailbox/doorbell capabilities,
537 	 * add it to the peer list.
538 	 */
539 	if (dev_rionet_capable(rdev)) {
540 		if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
541 			rc = -ENOMEM;
542 			goto out;
543 		}
544 		peer->rdev = rdev;
545 		list_add_tail(&peer->node, &rionet_peers);
546 	}
547 
548       out:
549 	return rc;
550 }
551 
552 static struct rio_device_id rionet_id_table[] = {
553 	{RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}
554 };
555 
556 static struct rio_driver rionet_driver = {
557 	.name = "rionet",
558 	.id_table = rionet_id_table,
559 	.probe = rionet_probe,
560 	.remove = rionet_remove,
561 };
562 
563 static int __init rionet_init(void)
564 {
565 	return rio_register_driver(&rionet_driver);
566 }
567 
568 static void __exit rionet_exit(void)
569 {
570 	rio_unregister_driver(&rionet_driver);
571 }
572 
573 module_init(rionet_init);
574 module_exit(rionet_exit);
575