xref: /openbmc/linux/drivers/net/rionet.c (revision 36915976eca58f2eefa040ba8f9939672564df61)
1f89efd52SMatt Porter /*
2f89efd52SMatt Porter  * rionet - Ethernet driver over RapidIO messaging services
3f89efd52SMatt Porter  *
4f89efd52SMatt Porter  * Copyright 2005 MontaVista Software, Inc.
5f89efd52SMatt Porter  * Matt Porter <mporter@kernel.crashing.org>
6f89efd52SMatt Porter  *
7f89efd52SMatt Porter  * This program is free software; you can redistribute  it and/or modify it
8f89efd52SMatt Porter  * under  the terms of  the GNU General  Public License as published by the
9f89efd52SMatt Porter  * Free Software Foundation;  either version 2 of the  License, or (at your
10f89efd52SMatt Porter  * option) any later version.
11f89efd52SMatt Porter  */
12f89efd52SMatt Porter 
13f89efd52SMatt Porter #include <linux/module.h>
14f89efd52SMatt Porter #include <linux/kernel.h>
15f89efd52SMatt Porter #include <linux/dma-mapping.h>
16f89efd52SMatt Porter #include <linux/delay.h>
17f89efd52SMatt Porter #include <linux/rio.h>
18f89efd52SMatt Porter #include <linux/rio_drv.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20f89efd52SMatt Porter #include <linux/rio_ids.h>
21f89efd52SMatt Porter 
22f89efd52SMatt Porter #include <linux/netdevice.h>
23f89efd52SMatt Porter #include <linux/etherdevice.h>
24f89efd52SMatt Porter #include <linux/skbuff.h>
25f89efd52SMatt Porter #include <linux/crc32.h>
26f89efd52SMatt Porter #include <linux/ethtool.h>
27f89efd52SMatt Porter 
28f89efd52SMatt Porter #define DRV_NAME        "rionet"
292fb717ecSAlexandre Bounine #define DRV_VERSION     "0.3"
30f89efd52SMatt Porter #define DRV_AUTHOR      "Matt Porter <mporter@kernel.crashing.org>"
31f89efd52SMatt Porter #define DRV_DESC        "Ethernet over RapidIO"
32f89efd52SMatt Porter 
33f89efd52SMatt Porter MODULE_AUTHOR(DRV_AUTHOR);
34f89efd52SMatt Porter MODULE_DESCRIPTION(DRV_DESC);
35f89efd52SMatt Porter MODULE_LICENSE("GPL");
36f89efd52SMatt Porter 
37f89efd52SMatt Porter #define RIONET_DEFAULT_MSGLEVEL \
38f89efd52SMatt Porter 			(NETIF_MSG_DRV          | \
39f89efd52SMatt Porter 			 NETIF_MSG_LINK         | \
40f89efd52SMatt Porter 			 NETIF_MSG_RX_ERR       | \
41f89efd52SMatt Porter 			 NETIF_MSG_TX_ERR)
42f89efd52SMatt Porter 
43f89efd52SMatt Porter #define RIONET_DOORBELL_JOIN	0x1000
44f89efd52SMatt Porter #define RIONET_DOORBELL_LEAVE	0x1001
45f89efd52SMatt Porter 
46f89efd52SMatt Porter #define RIONET_MAILBOX		0
47f89efd52SMatt Porter 
48f89efd52SMatt Porter #define RIONET_TX_RING_SIZE	CONFIG_RIONET_TX_SIZE
49f89efd52SMatt Porter #define RIONET_RX_RING_SIZE	CONFIG_RIONET_RX_SIZE
502fb717ecSAlexandre Bounine #define RIONET_MAX_NETS		8
51f89efd52SMatt Porter 
52f89efd52SMatt Porter struct rionet_private {
53f89efd52SMatt Porter 	struct rio_mport *mport;
54f89efd52SMatt Porter 	struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
55f89efd52SMatt Porter 	struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
56f89efd52SMatt Porter 	int rx_slot;
57f89efd52SMatt Porter 	int tx_slot;
58f89efd52SMatt Porter 	int tx_cnt;
59f89efd52SMatt Porter 	int ack_slot;
60f89efd52SMatt Porter 	spinlock_t lock;
61f89efd52SMatt Porter 	spinlock_t tx_lock;
62f89efd52SMatt Porter 	u32 msg_enable;
63f89efd52SMatt Porter };
64f89efd52SMatt Porter 
65f89efd52SMatt Porter struct rionet_peer {
66f89efd52SMatt Porter 	struct list_head node;
67f89efd52SMatt Porter 	struct rio_dev *rdev;
68f89efd52SMatt Porter 	struct resource *res;
69f89efd52SMatt Porter };
70f89efd52SMatt Porter 
712fb717ecSAlexandre Bounine struct rionet_net {
722fb717ecSAlexandre Bounine 	struct net_device *ndev;
732fb717ecSAlexandre Bounine 	struct list_head peers;
742fb717ecSAlexandre Bounine 	struct rio_dev **active;
752fb717ecSAlexandre Bounine 	int nact;	/* number of active peers */
762fb717ecSAlexandre Bounine };
77f89efd52SMatt Porter 
782fb717ecSAlexandre Bounine static struct rionet_net nets[RIONET_MAX_NETS];
79f89efd52SMatt Porter 
80284fb68dSAlexandre Bounine #define is_rionet_capable(src_ops, dst_ops)			\
81284fb68dSAlexandre Bounine 			((src_ops & RIO_SRC_OPS_DATA_MSG) &&	\
82284fb68dSAlexandre Bounine 			 (dst_ops & RIO_DST_OPS_DATA_MSG) &&	\
83f89efd52SMatt Porter 			 (src_ops & RIO_SRC_OPS_DOORBELL) &&	\
84f89efd52SMatt Porter 			 (dst_ops & RIO_DST_OPS_DOORBELL))
85f89efd52SMatt Porter #define dev_rionet_capable(dev) \
86284fb68dSAlexandre Bounine 	is_rionet_capable(dev->src_ops, dev->dst_ops)
87f89efd52SMatt Porter 
88e0c87bd9SAlexandre Bounine #define RIONET_MAC_MATCH(x)	(!memcmp((x), "\00\01\00\01", 4))
89e0c87bd9SAlexandre Bounine #define RIONET_GET_DESTID(x)	((*((u8 *)x + 4) << 8) | *((u8 *)x + 5))
90f89efd52SMatt Porter 
91f89efd52SMatt Porter static int rionet_rx_clean(struct net_device *ndev)
92f89efd52SMatt Porter {
93f89efd52SMatt Porter 	int i;
94f89efd52SMatt Porter 	int error = 0;
954cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
96f89efd52SMatt Porter 	void *data;
97f89efd52SMatt Porter 
98f89efd52SMatt Porter 	i = rnet->rx_slot;
99f89efd52SMatt Porter 
100f89efd52SMatt Porter 	do {
101f89efd52SMatt Porter 		if (!rnet->rx_skb[i])
102f89efd52SMatt Porter 			continue;
103f89efd52SMatt Porter 
104f89efd52SMatt Porter 		if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
105f89efd52SMatt Porter 			break;
106f89efd52SMatt Porter 
107f89efd52SMatt Porter 		rnet->rx_skb[i]->data = data;
108f89efd52SMatt Porter 		skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
109f89efd52SMatt Porter 		rnet->rx_skb[i]->protocol =
110f89efd52SMatt Porter 		    eth_type_trans(rnet->rx_skb[i], ndev);
111f89efd52SMatt Porter 		error = netif_rx(rnet->rx_skb[i]);
112f89efd52SMatt Porter 
113f89efd52SMatt Porter 		if (error == NET_RX_DROP) {
11409f75cd7SJeff Garzik 			ndev->stats.rx_dropped++;
115f89efd52SMatt Porter 		} else {
11609f75cd7SJeff Garzik 			ndev->stats.rx_packets++;
11709f75cd7SJeff Garzik 			ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
118f89efd52SMatt Porter 		}
119f89efd52SMatt Porter 
120f89efd52SMatt Porter 	} while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
121f89efd52SMatt Porter 
122f89efd52SMatt Porter 	return i;
123f89efd52SMatt Porter }
124f89efd52SMatt Porter 
125f89efd52SMatt Porter static void rionet_rx_fill(struct net_device *ndev, int end)
126f89efd52SMatt Porter {
127f89efd52SMatt Porter 	int i;
1284cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
129f89efd52SMatt Porter 
130f89efd52SMatt Porter 	i = rnet->rx_slot;
131f89efd52SMatt Porter 	do {
132f89efd52SMatt Porter 		rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
133f89efd52SMatt Porter 
134f89efd52SMatt Porter 		if (!rnet->rx_skb[i])
135f89efd52SMatt Porter 			break;
136f89efd52SMatt Porter 
137f89efd52SMatt Porter 		rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
138f89efd52SMatt Porter 				   rnet->rx_skb[i]->data);
139f89efd52SMatt Porter 	} while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
140f89efd52SMatt Porter 
141f89efd52SMatt Porter 	rnet->rx_slot = i;
142f89efd52SMatt Porter }
143f89efd52SMatt Porter 
144f89efd52SMatt Porter static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
145f89efd52SMatt Porter 			       struct rio_dev *rdev)
146f89efd52SMatt Porter {
1474cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
148f89efd52SMatt Porter 
149f89efd52SMatt Porter 	rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
150f89efd52SMatt Porter 	rnet->tx_skb[rnet->tx_slot] = skb;
151f89efd52SMatt Porter 
15209f75cd7SJeff Garzik 	ndev->stats.tx_packets++;
15309f75cd7SJeff Garzik 	ndev->stats.tx_bytes += skb->len;
154f89efd52SMatt Porter 
155f89efd52SMatt Porter 	if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
156f89efd52SMatt Porter 		netif_stop_queue(ndev);
157f89efd52SMatt Porter 
158f89efd52SMatt Porter 	++rnet->tx_slot;
159f89efd52SMatt Porter 	rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
160f89efd52SMatt Porter 
161f89efd52SMatt Porter 	if (netif_msg_tx_queued(rnet))
1628df8a475SDavid S. Miller 		printk(KERN_INFO "%s: queued skb len %8.8x\n", DRV_NAME,
1638df8a475SDavid S. Miller 		       skb->len);
164f89efd52SMatt Porter 
165f89efd52SMatt Porter 	return 0;
166f89efd52SMatt Porter }
167f89efd52SMatt Porter 
168f89efd52SMatt Porter static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
169f89efd52SMatt Porter {
170f89efd52SMatt Porter 	int i;
1714cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
172f89efd52SMatt Porter 	struct ethhdr *eth = (struct ethhdr *)skb->data;
173f89efd52SMatt Porter 	u16 destid;
174f89efd52SMatt Porter 	unsigned long flags;
1757c4a6106SAlexandre Bounine 	int add_num = 1;
176f89efd52SMatt Porter 
177f89efd52SMatt Porter 	local_irq_save(flags);
178f89efd52SMatt Porter 	if (!spin_trylock(&rnet->tx_lock)) {
179f89efd52SMatt Porter 		local_irq_restore(flags);
180f89efd52SMatt Porter 		return NETDEV_TX_LOCKED;
181f89efd52SMatt Porter 	}
182f89efd52SMatt Porter 
1837c4a6106SAlexandre Bounine 	if (is_multicast_ether_addr(eth->h_dest))
1842fb717ecSAlexandre Bounine 		add_num = nets[rnet->mport->id].nact;
1857c4a6106SAlexandre Bounine 
1867c4a6106SAlexandre Bounine 	if ((rnet->tx_cnt + add_num) > RIONET_TX_RING_SIZE) {
187f89efd52SMatt Porter 		netif_stop_queue(ndev);
188f89efd52SMatt Porter 		spin_unlock_irqrestore(&rnet->tx_lock, flags);
189f89efd52SMatt Porter 		printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
190f89efd52SMatt Porter 		       ndev->name);
191f89efd52SMatt Porter 		return NETDEV_TX_BUSY;
192f89efd52SMatt Porter 	}
193f89efd52SMatt Porter 
194abfc89c7STobias Klauser 	if (is_multicast_ether_addr(eth->h_dest)) {
1957c4a6106SAlexandre Bounine 		int count = 0;
1962fb717ecSAlexandre Bounine 
197e0423236SZhang Wei 		for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
198e0423236SZhang Wei 				i++)
1992fb717ecSAlexandre Bounine 			if (nets[rnet->mport->id].active[i]) {
200f89efd52SMatt Porter 				rionet_queue_tx_msg(skb, ndev,
2012fb717ecSAlexandre Bounine 					nets[rnet->mport->id].active[i]);
2027c4a6106SAlexandre Bounine 				if (count)
2037c4a6106SAlexandre Bounine 					atomic_inc(&skb->users);
2047c4a6106SAlexandre Bounine 				count++;
2057c4a6106SAlexandre Bounine 			}
206f89efd52SMatt Porter 	} else if (RIONET_MAC_MATCH(eth->h_dest)) {
207f89efd52SMatt Porter 		destid = RIONET_GET_DESTID(eth->h_dest);
2082fb717ecSAlexandre Bounine 		if (nets[rnet->mport->id].active[destid])
2092fb717ecSAlexandre Bounine 			rionet_queue_tx_msg(skb, ndev,
2102fb717ecSAlexandre Bounine 					nets[rnet->mport->id].active[destid]);
211e6161d64SAlexandre Bounine 		else {
212e6161d64SAlexandre Bounine 			/*
213e6161d64SAlexandre Bounine 			 * If the target device was removed from the list of
214e6161d64SAlexandre Bounine 			 * active peers but we still have TX packets targeting
215e6161d64SAlexandre Bounine 			 * it just report sending a packet to the target
216e6161d64SAlexandre Bounine 			 * (without actual packet transfer).
217e6161d64SAlexandre Bounine 			 */
218e6161d64SAlexandre Bounine 			dev_kfree_skb_any(skb);
219e6161d64SAlexandre Bounine 			ndev->stats.tx_packets++;
220e6161d64SAlexandre Bounine 			ndev->stats.tx_bytes += skb->len;
221e6161d64SAlexandre Bounine 		}
222f89efd52SMatt Porter 	}
223f89efd52SMatt Porter 
224f89efd52SMatt Porter 	spin_unlock_irqrestore(&rnet->tx_lock, flags);
225f89efd52SMatt Porter 
2266ed10654SPatrick McHardy 	return NETDEV_TX_OK;
227f89efd52SMatt Porter }
228f89efd52SMatt Porter 
229f89efd52SMatt Porter static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
230f89efd52SMatt Porter 			       u16 info)
231f89efd52SMatt Porter {
232f89efd52SMatt Porter 	struct net_device *ndev = dev_id;
2334cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
234f89efd52SMatt Porter 	struct rionet_peer *peer;
235f89efd52SMatt Porter 
236f89efd52SMatt Porter 	if (netif_msg_intr(rnet))
237f89efd52SMatt Porter 		printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
238f89efd52SMatt Porter 		       DRV_NAME, sid, tid, info);
239f89efd52SMatt Porter 	if (info == RIONET_DOORBELL_JOIN) {
2402fb717ecSAlexandre Bounine 		if (!nets[rnet->mport->id].active[sid]) {
2412fb717ecSAlexandre Bounine 			list_for_each_entry(peer,
2422fb717ecSAlexandre Bounine 					   &nets[rnet->mport->id].peers, node) {
2437c4a6106SAlexandre Bounine 				if (peer->rdev->destid == sid) {
2442fb717ecSAlexandre Bounine 					nets[rnet->mport->id].active[sid] =
2452fb717ecSAlexandre Bounine 								peer->rdev;
2462fb717ecSAlexandre Bounine 					nets[rnet->mport->id].nact++;
2477c4a6106SAlexandre Bounine 				}
248f89efd52SMatt Porter 			}
249f89efd52SMatt Porter 			rio_mport_send_doorbell(mport, sid,
250f89efd52SMatt Porter 						RIONET_DOORBELL_JOIN);
251f89efd52SMatt Porter 		}
252f89efd52SMatt Porter 	} else if (info == RIONET_DOORBELL_LEAVE) {
2532fb717ecSAlexandre Bounine 		nets[rnet->mport->id].active[sid] = NULL;
2542fb717ecSAlexandre Bounine 		nets[rnet->mport->id].nact--;
255f89efd52SMatt Porter 	} else {
256f89efd52SMatt Porter 		if (netif_msg_intr(rnet))
257f89efd52SMatt Porter 			printk(KERN_WARNING "%s: unhandled doorbell\n",
258f89efd52SMatt Porter 			       DRV_NAME);
259f89efd52SMatt Porter 	}
260f89efd52SMatt Porter }
261f89efd52SMatt Porter 
262f89efd52SMatt Porter static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
263f89efd52SMatt Porter {
264f89efd52SMatt Porter 	int n;
265f89efd52SMatt Porter 	struct net_device *ndev = dev_id;
2664cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
267f89efd52SMatt Porter 
268f89efd52SMatt Porter 	if (netif_msg_intr(rnet))
269f89efd52SMatt Porter 		printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
270f89efd52SMatt Porter 		       DRV_NAME, mbox, slot);
271f89efd52SMatt Porter 
272f89efd52SMatt Porter 	spin_lock(&rnet->lock);
273f89efd52SMatt Porter 	if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
274f89efd52SMatt Porter 		rionet_rx_fill(ndev, n);
275f89efd52SMatt Porter 	spin_unlock(&rnet->lock);
276f89efd52SMatt Porter }
277f89efd52SMatt Porter 
278f89efd52SMatt Porter static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
279f89efd52SMatt Porter {
280f89efd52SMatt Porter 	struct net_device *ndev = dev_id;
2814cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
282f89efd52SMatt Porter 
283*36915976SAurelien Jacquiot 	spin_lock(&rnet->tx_lock);
284f89efd52SMatt Porter 
285f89efd52SMatt Porter 	if (netif_msg_intr(rnet))
286f89efd52SMatt Porter 		printk(KERN_INFO
287f89efd52SMatt Porter 		       "%s: outbound message event, mbox %d slot %d\n",
288f89efd52SMatt Porter 		       DRV_NAME, mbox, slot);
289f89efd52SMatt Porter 
290f89efd52SMatt Porter 	while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
291f89efd52SMatt Porter 		/* dma unmap single */
292f89efd52SMatt Porter 		dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
293f89efd52SMatt Porter 		rnet->tx_skb[rnet->ack_slot] = NULL;
294f89efd52SMatt Porter 		++rnet->ack_slot;
295f89efd52SMatt Porter 		rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
296f89efd52SMatt Porter 		rnet->tx_cnt--;
297f89efd52SMatt Porter 	}
298f89efd52SMatt Porter 
299f89efd52SMatt Porter 	if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
300f89efd52SMatt Porter 		netif_wake_queue(ndev);
301f89efd52SMatt Porter 
302*36915976SAurelien Jacquiot 	spin_unlock(&rnet->tx_lock);
303f89efd52SMatt Porter }
304f89efd52SMatt Porter 
305f89efd52SMatt Porter static int rionet_open(struct net_device *ndev)
306f89efd52SMatt Porter {
307f89efd52SMatt Porter 	int i, rc = 0;
308f89efd52SMatt Porter 	struct rionet_peer *peer, *tmp;
3094cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
310f89efd52SMatt Porter 
311f89efd52SMatt Porter 	if (netif_msg_ifup(rnet))
312f89efd52SMatt Porter 		printk(KERN_INFO "%s: open\n", DRV_NAME);
313f89efd52SMatt Porter 
314f89efd52SMatt Porter 	if ((rc = rio_request_inb_dbell(rnet->mport,
315f89efd52SMatt Porter 					(void *)ndev,
316f89efd52SMatt Porter 					RIONET_DOORBELL_JOIN,
317f89efd52SMatt Porter 					RIONET_DOORBELL_LEAVE,
318f89efd52SMatt Porter 					rionet_dbell_event)) < 0)
319f89efd52SMatt Porter 		goto out;
320f89efd52SMatt Porter 
321f89efd52SMatt Porter 	if ((rc = rio_request_inb_mbox(rnet->mport,
322f89efd52SMatt Porter 				       (void *)ndev,
323f89efd52SMatt Porter 				       RIONET_MAILBOX,
324f89efd52SMatt Porter 				       RIONET_RX_RING_SIZE,
325f89efd52SMatt Porter 				       rionet_inb_msg_event)) < 0)
326f89efd52SMatt Porter 		goto out;
327f89efd52SMatt Porter 
328f89efd52SMatt Porter 	if ((rc = rio_request_outb_mbox(rnet->mport,
329f89efd52SMatt Porter 					(void *)ndev,
330f89efd52SMatt Porter 					RIONET_MAILBOX,
331f89efd52SMatt Porter 					RIONET_TX_RING_SIZE,
332f89efd52SMatt Porter 					rionet_outb_msg_event)) < 0)
333f89efd52SMatt Porter 		goto out;
334f89efd52SMatt Porter 
335f89efd52SMatt Porter 	/* Initialize inbound message ring */
336f89efd52SMatt Porter 	for (i = 0; i < RIONET_RX_RING_SIZE; i++)
337f89efd52SMatt Porter 		rnet->rx_skb[i] = NULL;
338f89efd52SMatt Porter 	rnet->rx_slot = 0;
339f89efd52SMatt Porter 	rionet_rx_fill(ndev, 0);
340f89efd52SMatt Porter 
341f89efd52SMatt Porter 	rnet->tx_slot = 0;
342f89efd52SMatt Porter 	rnet->tx_cnt = 0;
343f89efd52SMatt Porter 	rnet->ack_slot = 0;
344f89efd52SMatt Porter 
345f89efd52SMatt Porter 	netif_carrier_on(ndev);
346f89efd52SMatt Porter 	netif_start_queue(ndev);
347f89efd52SMatt Porter 
3482fb717ecSAlexandre Bounine 	list_for_each_entry_safe(peer, tmp,
3492fb717ecSAlexandre Bounine 				 &nets[rnet->mport->id].peers, node) {
350f89efd52SMatt Porter 		if (!(peer->res = rio_request_outb_dbell(peer->rdev,
351f89efd52SMatt Porter 							 RIONET_DOORBELL_JOIN,
352f89efd52SMatt Porter 							 RIONET_DOORBELL_LEAVE)))
353f89efd52SMatt Porter 		{
354f89efd52SMatt Porter 			printk(KERN_ERR "%s: error requesting doorbells\n",
355f89efd52SMatt Porter 			       DRV_NAME);
356f89efd52SMatt Porter 			continue;
357f89efd52SMatt Porter 		}
358f89efd52SMatt Porter 
359284fb68dSAlexandre Bounine 		/* Send a join message */
360f89efd52SMatt Porter 		rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
361f89efd52SMatt Porter 	}
362f89efd52SMatt Porter 
363f89efd52SMatt Porter       out:
364f89efd52SMatt Porter 	return rc;
365f89efd52SMatt Porter }
366f89efd52SMatt Porter 
367f89efd52SMatt Porter static int rionet_close(struct net_device *ndev)
368f89efd52SMatt Porter {
3694cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
370f89efd52SMatt Porter 	struct rionet_peer *peer, *tmp;
371f89efd52SMatt Porter 	int i;
372f89efd52SMatt Porter 
373f89efd52SMatt Porter 	if (netif_msg_ifup(rnet))
3742fb717ecSAlexandre Bounine 		printk(KERN_INFO "%s: close %s\n", DRV_NAME, ndev->name);
375f89efd52SMatt Porter 
376f89efd52SMatt Porter 	netif_stop_queue(ndev);
377f89efd52SMatt Porter 	netif_carrier_off(ndev);
378f89efd52SMatt Porter 
379f89efd52SMatt Porter 	for (i = 0; i < RIONET_RX_RING_SIZE; i++)
380f89efd52SMatt Porter 		kfree_skb(rnet->rx_skb[i]);
381f89efd52SMatt Porter 
3822fb717ecSAlexandre Bounine 	list_for_each_entry_safe(peer, tmp,
3832fb717ecSAlexandre Bounine 				 &nets[rnet->mport->id].peers, node) {
3842fb717ecSAlexandre Bounine 		if (nets[rnet->mport->id].active[peer->rdev->destid]) {
385f89efd52SMatt Porter 			rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
3862fb717ecSAlexandre Bounine 			nets[rnet->mport->id].active[peer->rdev->destid] = NULL;
387f89efd52SMatt Porter 		}
388f89efd52SMatt Porter 		rio_release_outb_dbell(peer->rdev, peer->res);
389f89efd52SMatt Porter 	}
390f89efd52SMatt Porter 
391f89efd52SMatt Porter 	rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
392f89efd52SMatt Porter 			      RIONET_DOORBELL_LEAVE);
393f89efd52SMatt Porter 	rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
394f89efd52SMatt Porter 	rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
395f89efd52SMatt Porter 
396f89efd52SMatt Porter 	return 0;
397f89efd52SMatt Porter }
398f89efd52SMatt Porter 
39971db87baSViresh Kumar static void rionet_remove_dev(struct device *dev, struct subsys_interface *sif)
400f89efd52SMatt Porter {
401e6161d64SAlexandre Bounine 	struct rio_dev *rdev = to_rio_dev(dev);
4022fb717ecSAlexandre Bounine 	unsigned char netid = rdev->net->hport->id;
403f89efd52SMatt Porter 	struct rionet_peer *peer, *tmp;
404f89efd52SMatt Porter 
405e6161d64SAlexandre Bounine 	if (dev_rionet_capable(rdev)) {
4062fb717ecSAlexandre Bounine 		list_for_each_entry_safe(peer, tmp, &nets[netid].peers, node) {
407e6161d64SAlexandre Bounine 			if (peer->rdev == rdev) {
408e6161d64SAlexandre Bounine 				if (nets[netid].active[rdev->destid]) {
409e6161d64SAlexandre Bounine 					nets[netid].active[rdev->destid] = NULL;
410e6161d64SAlexandre Bounine 					nets[netid].nact--;
411f89efd52SMatt Porter 				}
4122fb717ecSAlexandre Bounine 
413e6161d64SAlexandre Bounine 				list_del(&peer->node);
414e6161d64SAlexandre Bounine 				kfree(peer);
415e6161d64SAlexandre Bounine 				break;
416e6161d64SAlexandre Bounine 			}
417e6161d64SAlexandre Bounine 		}
418e6161d64SAlexandre Bounine 	}
419f89efd52SMatt Porter }
420f89efd52SMatt Porter 
421f89efd52SMatt Porter static void rionet_get_drvinfo(struct net_device *ndev,
422f89efd52SMatt Porter 			       struct ethtool_drvinfo *info)
423f89efd52SMatt Porter {
4244cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
425f89efd52SMatt Porter 
4267826d43fSJiri Pirko 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
4277826d43fSJiri Pirko 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
4287826d43fSJiri Pirko 	strlcpy(info->fw_version, "n/a", sizeof(info->fw_version));
4297826d43fSJiri Pirko 	strlcpy(info->bus_info, rnet->mport->name, sizeof(info->bus_info));
430f89efd52SMatt Porter }
431f89efd52SMatt Porter 
432f89efd52SMatt Porter static u32 rionet_get_msglevel(struct net_device *ndev)
433f89efd52SMatt Porter {
4344cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
435f89efd52SMatt Porter 
436f89efd52SMatt Porter 	return rnet->msg_enable;
437f89efd52SMatt Porter }
438f89efd52SMatt Porter 
439f89efd52SMatt Porter static void rionet_set_msglevel(struct net_device *ndev, u32 value)
440f89efd52SMatt Porter {
4414cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
442f89efd52SMatt Porter 
443f89efd52SMatt Porter 	rnet->msg_enable = value;
444f89efd52SMatt Porter }
445f89efd52SMatt Porter 
4467282d491SJeff Garzik static const struct ethtool_ops rionet_ethtool_ops = {
447f89efd52SMatt Porter 	.get_drvinfo = rionet_get_drvinfo,
448f89efd52SMatt Porter 	.get_msglevel = rionet_get_msglevel,
449f89efd52SMatt Porter 	.set_msglevel = rionet_set_msglevel,
450f89efd52SMatt Porter 	.get_link = ethtool_op_get_link,
451f89efd52SMatt Porter };
452f89efd52SMatt Porter 
453a33a2bb3SAlexander Beregalov static const struct net_device_ops rionet_netdev_ops = {
454a33a2bb3SAlexander Beregalov 	.ndo_open		= rionet_open,
455a33a2bb3SAlexander Beregalov 	.ndo_stop		= rionet_close,
456a33a2bb3SAlexander Beregalov 	.ndo_start_xmit		= rionet_start_xmit,
457a33a2bb3SAlexander Beregalov 	.ndo_change_mtu		= eth_change_mtu,
458a33a2bb3SAlexander Beregalov 	.ndo_validate_addr	= eth_validate_addr,
459a33a2bb3SAlexander Beregalov 	.ndo_set_mac_address	= eth_mac_addr,
460a33a2bb3SAlexander Beregalov };
461a33a2bb3SAlexander Beregalov 
46255caa924SYinglin Luan static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
463f89efd52SMatt Porter {
464f89efd52SMatt Porter 	int rc = 0;
465f89efd52SMatt Porter 	struct rionet_private *rnet;
466f89efd52SMatt Porter 	u16 device_id;
467acc65632SAkinobu Mita 	const size_t rionet_active_bytes = sizeof(void *) *
468acc65632SAkinobu Mita 				RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
469f89efd52SMatt Porter 
4702fb717ecSAlexandre Bounine 	nets[mport->id].active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
471acc65632SAkinobu Mita 						get_order(rionet_active_bytes));
4722fb717ecSAlexandre Bounine 	if (!nets[mport->id].active) {
473e0423236SZhang Wei 		rc = -ENOMEM;
474e0423236SZhang Wei 		goto out;
475e0423236SZhang Wei 	}
4762fb717ecSAlexandre Bounine 	memset((void *)nets[mport->id].active, 0, rionet_active_bytes);
477e0423236SZhang Wei 
478f89efd52SMatt Porter 	/* Set up private area */
4794cf1653aSWang Chen 	rnet = netdev_priv(ndev);
480f89efd52SMatt Porter 	rnet->mport = mport;
481f89efd52SMatt Porter 
482f89efd52SMatt Porter 	/* Set the default MAC address */
483f89efd52SMatt Porter 	device_id = rio_local_get_device_id(mport);
484f89efd52SMatt Porter 	ndev->dev_addr[0] = 0x00;
485f89efd52SMatt Porter 	ndev->dev_addr[1] = 0x01;
486f89efd52SMatt Porter 	ndev->dev_addr[2] = 0x00;
487f89efd52SMatt Porter 	ndev->dev_addr[3] = 0x01;
488f89efd52SMatt Porter 	ndev->dev_addr[4] = device_id >> 8;
489f89efd52SMatt Porter 	ndev->dev_addr[5] = device_id & 0xff;
490f89efd52SMatt Porter 
491a33a2bb3SAlexander Beregalov 	ndev->netdev_ops = &rionet_netdev_ops;
492f89efd52SMatt Porter 	ndev->mtu = RIO_MAX_MSG_SIZE - 14;
493f89efd52SMatt Porter 	ndev->features = NETIF_F_LLTX;
4942aaf308bSAlexandre Bounine 	SET_NETDEV_DEV(ndev, &mport->dev);
4957ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &rionet_ethtool_ops;
496f89efd52SMatt Porter 
497f89efd52SMatt Porter 	spin_lock_init(&rnet->lock);
498f89efd52SMatt Porter 	spin_lock_init(&rnet->tx_lock);
499f89efd52SMatt Porter 
500f89efd52SMatt Porter 	rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
501f89efd52SMatt Porter 
502f89efd52SMatt Porter 	rc = register_netdev(ndev);
503f89efd52SMatt Porter 	if (rc != 0)
504f89efd52SMatt Porter 		goto out;
505f89efd52SMatt Porter 
5062fb717ecSAlexandre Bounine 	printk(KERN_INFO "%s: %s %s Version %s, MAC %pM, %s\n",
507f89efd52SMatt Porter 	       ndev->name,
508f89efd52SMatt Porter 	       DRV_NAME,
509f89efd52SMatt Porter 	       DRV_DESC,
510f89efd52SMatt Porter 	       DRV_VERSION,
5112fb717ecSAlexandre Bounine 	       ndev->dev_addr,
5122fb717ecSAlexandre Bounine 	       mport->name);
513f89efd52SMatt Porter 
514f89efd52SMatt Porter       out:
515f89efd52SMatt Porter 	return rc;
516f89efd52SMatt Porter }
517f89efd52SMatt Porter 
5182fb717ecSAlexandre Bounine static unsigned long net_table[RIONET_MAX_NETS/sizeof(unsigned long) + 1];
5192fb717ecSAlexandre Bounine 
520e6161d64SAlexandre Bounine static int rionet_add_dev(struct device *dev, struct subsys_interface *sif)
521f89efd52SMatt Porter {
522f89efd52SMatt Porter 	int rc = -ENODEV;
523284fb68dSAlexandre Bounine 	u32 lsrc_ops, ldst_ops;
524f89efd52SMatt Porter 	struct rionet_peer *peer;
52555caa924SYinglin Luan 	struct net_device *ndev = NULL;
526e6161d64SAlexandre Bounine 	struct rio_dev *rdev = to_rio_dev(dev);
5272fb717ecSAlexandre Bounine 	unsigned char netid = rdev->net->hport->id;
5282fb717ecSAlexandre Bounine 	int oldnet;
529f89efd52SMatt Porter 
5302fb717ecSAlexandre Bounine 	if (netid >= RIONET_MAX_NETS)
5312fb717ecSAlexandre Bounine 		return rc;
5322fb717ecSAlexandre Bounine 
5332fb717ecSAlexandre Bounine 	oldnet = test_and_set_bit(netid, net_table);
5342fb717ecSAlexandre Bounine 
5352fb717ecSAlexandre Bounine 	/*
536e6161d64SAlexandre Bounine 	 * If first time through this net, make sure local device is rionet
537e6161d64SAlexandre Bounine 	 * capable and setup netdev (this step will be skipped in later probes
538e6161d64SAlexandre Bounine 	 * on the same net).
5392fb717ecSAlexandre Bounine 	 */
5402fb717ecSAlexandre Bounine 	if (!oldnet) {
5412fb717ecSAlexandre Bounine 		rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
5422fb717ecSAlexandre Bounine 					 &lsrc_ops);
5432fb717ecSAlexandre Bounine 		rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
5442fb717ecSAlexandre Bounine 					 &ldst_ops);
5452fb717ecSAlexandre Bounine 		if (!is_rionet_capable(lsrc_ops, ldst_ops)) {
5462fb717ecSAlexandre Bounine 			printk(KERN_ERR
5472fb717ecSAlexandre Bounine 			       "%s: local device %s is not network capable\n",
5482fb717ecSAlexandre Bounine 			       DRV_NAME, rdev->net->hport->name);
549f89efd52SMatt Porter 			goto out;
5502fb717ecSAlexandre Bounine 		}
551f89efd52SMatt Porter 
55255caa924SYinglin Luan 		/* Allocate our net_device structure */
55355caa924SYinglin Luan 		ndev = alloc_etherdev(sizeof(struct rionet_private));
55455caa924SYinglin Luan 		if (ndev == NULL) {
55555caa924SYinglin Luan 			rc = -ENOMEM;
55655caa924SYinglin Luan 			goto out;
55755caa924SYinglin Luan 		}
5582fb717ecSAlexandre Bounine 		nets[netid].ndev = ndev;
55955caa924SYinglin Luan 		rc = rionet_setup_netdev(rdev->net->hport, ndev);
560e6161d64SAlexandre Bounine 		if (rc) {
561e6161d64SAlexandre Bounine 			printk(KERN_ERR "%s: failed to setup netdev (rc=%d)\n",
562e6161d64SAlexandre Bounine 			       DRV_NAME, rc);
563e6161d64SAlexandre Bounine 			goto out;
564e6161d64SAlexandre Bounine 		}
565e6161d64SAlexandre Bounine 
5662fb717ecSAlexandre Bounine 		INIT_LIST_HEAD(&nets[netid].peers);
5672fb717ecSAlexandre Bounine 		nets[netid].nact = 0;
5682fb717ecSAlexandre Bounine 	} else if (nets[netid].ndev == NULL)
5692fb717ecSAlexandre Bounine 		goto out;
570f89efd52SMatt Porter 
571f89efd52SMatt Porter 	/*
572f89efd52SMatt Porter 	 * If the remote device has mailbox/doorbell capabilities,
573f89efd52SMatt Porter 	 * add it to the peer list.
574f89efd52SMatt Porter 	 */
575f89efd52SMatt Porter 	if (dev_rionet_capable(rdev)) {
576f89efd52SMatt Porter 		if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
577f89efd52SMatt Porter 			rc = -ENOMEM;
578f89efd52SMatt Porter 			goto out;
579f89efd52SMatt Porter 		}
580f89efd52SMatt Porter 		peer->rdev = rdev;
5812fb717ecSAlexandre Bounine 		list_add_tail(&peer->node, &nets[netid].peers);
582f89efd52SMatt Porter 	}
583f89efd52SMatt Porter 
584e6161d64SAlexandre Bounine 	return 0;
585f89efd52SMatt Porter out:
586f89efd52SMatt Porter 	return rc;
587f89efd52SMatt Porter }
588f89efd52SMatt Porter 
589e6161d64SAlexandre Bounine #ifdef MODULE
590f89efd52SMatt Porter static struct rio_device_id rionet_id_table[] = {
591e6161d64SAlexandre Bounine 	{RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)},
592e6161d64SAlexandre Bounine 	{ 0, }	/* terminate list */
593f89efd52SMatt Porter };
594f89efd52SMatt Porter 
595e6161d64SAlexandre Bounine MODULE_DEVICE_TABLE(rapidio, rionet_id_table);
596e6161d64SAlexandre Bounine #endif
597e6161d64SAlexandre Bounine 
598e6161d64SAlexandre Bounine static struct subsys_interface rionet_interface = {
599f89efd52SMatt Porter 	.name		= "rionet",
600e6161d64SAlexandre Bounine 	.subsys		= &rio_bus_type,
601e6161d64SAlexandre Bounine 	.add_dev	= rionet_add_dev,
602e6161d64SAlexandre Bounine 	.remove_dev	= rionet_remove_dev,
603f89efd52SMatt Porter };
604f89efd52SMatt Porter 
605f89efd52SMatt Porter static int __init rionet_init(void)
606f89efd52SMatt Porter {
607e6161d64SAlexandre Bounine 	return subsys_interface_register(&rionet_interface);
608f89efd52SMatt Porter }
609f89efd52SMatt Porter 
610f89efd52SMatt Porter static void __exit rionet_exit(void)
611f89efd52SMatt Porter {
612e6161d64SAlexandre Bounine 	struct rionet_private *rnet;
613e6161d64SAlexandre Bounine 	struct net_device *ndev;
614e6161d64SAlexandre Bounine 	struct rionet_peer *peer, *tmp;
615e6161d64SAlexandre Bounine 	int i;
616e6161d64SAlexandre Bounine 
617e6161d64SAlexandre Bounine 	for (i = 0; i < RIONET_MAX_NETS; i++) {
618e6161d64SAlexandre Bounine 		if (nets[i].ndev != NULL) {
619e6161d64SAlexandre Bounine 			ndev = nets[i].ndev;
620e6161d64SAlexandre Bounine 			rnet = netdev_priv(ndev);
621e6161d64SAlexandre Bounine 			unregister_netdev(ndev);
622e6161d64SAlexandre Bounine 
623e6161d64SAlexandre Bounine 			list_for_each_entry_safe(peer,
624e6161d64SAlexandre Bounine 						 tmp, &nets[i].peers, node) {
625e6161d64SAlexandre Bounine 				list_del(&peer->node);
626e6161d64SAlexandre Bounine 				kfree(peer);
627e6161d64SAlexandre Bounine 			}
628e6161d64SAlexandre Bounine 
629e6161d64SAlexandre Bounine 			free_pages((unsigned long)nets[i].active,
630e6161d64SAlexandre Bounine 				 get_order(sizeof(void *) *
631e6161d64SAlexandre Bounine 				 RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size)));
632e6161d64SAlexandre Bounine 			nets[i].active = NULL;
633e6161d64SAlexandre Bounine 
634e6161d64SAlexandre Bounine 			free_netdev(ndev);
635e6161d64SAlexandre Bounine 		}
636e6161d64SAlexandre Bounine 	}
637e6161d64SAlexandre Bounine 
638e6161d64SAlexandre Bounine 	subsys_interface_unregister(&rionet_interface);
639f89efd52SMatt Porter }
640f89efd52SMatt Porter 
6412f809985SAlexandre Bounine late_initcall(rionet_init);
642f89efd52SMatt Porter module_exit(rionet_exit);
643