xref: /openbmc/linux/drivers/net/rionet.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f89efd52SMatt Porter /*
3f89efd52SMatt Porter  * rionet - Ethernet driver over RapidIO messaging services
4f89efd52SMatt Porter  *
5f89efd52SMatt Porter  * Copyright 2005 MontaVista Software, Inc.
6f89efd52SMatt Porter  * Matt Porter <mporter@kernel.crashing.org>
7f89efd52SMatt Porter  */
8f89efd52SMatt Porter 
9f89efd52SMatt Porter #include <linux/module.h>
10f89efd52SMatt Porter #include <linux/kernel.h>
11f89efd52SMatt Porter #include <linux/dma-mapping.h>
12f89efd52SMatt Porter #include <linux/delay.h>
13f89efd52SMatt Porter #include <linux/rio.h>
14f89efd52SMatt Porter #include <linux/rio_drv.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
16f89efd52SMatt Porter #include <linux/rio_ids.h>
17f89efd52SMatt Porter 
18f89efd52SMatt Porter #include <linux/netdevice.h>
19f89efd52SMatt Porter #include <linux/etherdevice.h>
20f89efd52SMatt Porter #include <linux/skbuff.h>
21f89efd52SMatt Porter #include <linux/crc32.h>
22f89efd52SMatt Porter #include <linux/ethtool.h>
23f41e2472SAlexandre Bounine #include <linux/reboot.h>
24f89efd52SMatt Porter 
25f89efd52SMatt Porter #define DRV_NAME        "rionet"
262fb717ecSAlexandre Bounine #define DRV_VERSION     "0.3"
27f89efd52SMatt Porter #define DRV_AUTHOR      "Matt Porter <mporter@kernel.crashing.org>"
28f89efd52SMatt Porter #define DRV_DESC        "Ethernet over RapidIO"
29f89efd52SMatt Porter 
30f89efd52SMatt Porter MODULE_AUTHOR(DRV_AUTHOR);
31f89efd52SMatt Porter MODULE_DESCRIPTION(DRV_DESC);
32f89efd52SMatt Porter MODULE_LICENSE("GPL");
33f89efd52SMatt Porter 
34f89efd52SMatt Porter #define RIONET_DEFAULT_MSGLEVEL \
35f89efd52SMatt Porter 			(NETIF_MSG_DRV          | \
36f89efd52SMatt Porter 			 NETIF_MSG_LINK         | \
37f89efd52SMatt Porter 			 NETIF_MSG_RX_ERR       | \
38f89efd52SMatt Porter 			 NETIF_MSG_TX_ERR)
39f89efd52SMatt Porter 
40f89efd52SMatt Porter #define RIONET_DOORBELL_JOIN	0x1000
41f89efd52SMatt Porter #define RIONET_DOORBELL_LEAVE	0x1001
42f89efd52SMatt Porter 
43f89efd52SMatt Porter #define RIONET_MAILBOX		0
44f89efd52SMatt Porter 
45f89efd52SMatt Porter #define RIONET_TX_RING_SIZE	CONFIG_RIONET_TX_SIZE
46f89efd52SMatt Porter #define RIONET_RX_RING_SIZE	CONFIG_RIONET_RX_SIZE
472fb717ecSAlexandre Bounine #define RIONET_MAX_NETS		8
4892444bb3SAurelien Jacquiot #define RIONET_MSG_SIZE         RIO_MAX_MSG_SIZE
4992444bb3SAurelien Jacquiot #define RIONET_MAX_MTU          (RIONET_MSG_SIZE - ETH_HLEN)
50f89efd52SMatt Porter 
51f89efd52SMatt Porter struct rionet_private {
52f89efd52SMatt Porter 	struct rio_mport *mport;
53f89efd52SMatt Porter 	struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
54f89efd52SMatt Porter 	struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
55f89efd52SMatt Porter 	int rx_slot;
56f89efd52SMatt Porter 	int tx_slot;
57f89efd52SMatt Porter 	int tx_cnt;
58f89efd52SMatt Porter 	int ack_slot;
59f89efd52SMatt Porter 	spinlock_t lock;
60f89efd52SMatt Porter 	spinlock_t tx_lock;
61f89efd52SMatt Porter 	u32 msg_enable;
6234ed2ebbSAlexandre Bounine 	bool open;
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;
7434ed2ebbSAlexandre Bounine 	spinlock_t lock;	/* net info access lock */
752fb717ecSAlexandre Bounine 	struct rio_dev **active;
762fb717ecSAlexandre Bounine 	int nact;	/* number of active peers */
772fb717ecSAlexandre Bounine };
78f89efd52SMatt Porter 
792fb717ecSAlexandre Bounine static struct rionet_net nets[RIONET_MAX_NETS];
80f89efd52SMatt Porter 
81284fb68dSAlexandre Bounine #define is_rionet_capable(src_ops, dst_ops)			\
82284fb68dSAlexandre Bounine 			((src_ops & RIO_SRC_OPS_DATA_MSG) &&	\
83284fb68dSAlexandre Bounine 			 (dst_ops & RIO_DST_OPS_DATA_MSG) &&	\
84f89efd52SMatt Porter 			 (src_ops & RIO_SRC_OPS_DOORBELL) &&	\
85f89efd52SMatt Porter 			 (dst_ops & RIO_DST_OPS_DOORBELL))
86f89efd52SMatt Porter #define dev_rionet_capable(dev) \
87284fb68dSAlexandre Bounine 	is_rionet_capable(dev->src_ops, dev->dst_ops)
88f89efd52SMatt Porter 
89e0c87bd9SAlexandre Bounine #define RIONET_MAC_MATCH(x)	(!memcmp((x), "\00\01\00\01", 4))
90e0c87bd9SAlexandre Bounine #define RIONET_GET_DESTID(x)	((*((u8 *)x + 4) << 8) | *((u8 *)x + 5))
91f89efd52SMatt Porter 
rionet_rx_clean(struct net_device * ndev)92f89efd52SMatt Porter static int rionet_rx_clean(struct net_device *ndev)
93f89efd52SMatt Porter {
94f89efd52SMatt Porter 	int i;
95f89efd52SMatt Porter 	int error = 0;
964cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
97f89efd52SMatt Porter 	void *data;
98f89efd52SMatt Porter 
99f89efd52SMatt Porter 	i = rnet->rx_slot;
100f89efd52SMatt Porter 
101f89efd52SMatt Porter 	do {
102f89efd52SMatt Porter 		if (!rnet->rx_skb[i])
103f89efd52SMatt Porter 			continue;
104f89efd52SMatt Porter 
105f89efd52SMatt Porter 		if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
106f89efd52SMatt Porter 			break;
107f89efd52SMatt Porter 
108f89efd52SMatt Porter 		rnet->rx_skb[i]->data = data;
109f89efd52SMatt Porter 		skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
110f89efd52SMatt Porter 		rnet->rx_skb[i]->protocol =
111f89efd52SMatt Porter 		    eth_type_trans(rnet->rx_skb[i], ndev);
112baebdf48SSebastian Andrzej Siewior 		error = __netif_rx(rnet->rx_skb[i]);
113f89efd52SMatt Porter 
114f89efd52SMatt Porter 		if (error == NET_RX_DROP) {
11509f75cd7SJeff Garzik 			ndev->stats.rx_dropped++;
116f89efd52SMatt Porter 		} else {
11709f75cd7SJeff Garzik 			ndev->stats.rx_packets++;
11809f75cd7SJeff Garzik 			ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
119f89efd52SMatt Porter 		}
120f89efd52SMatt Porter 
121f89efd52SMatt Porter 	} while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
122f89efd52SMatt Porter 
123f89efd52SMatt Porter 	return i;
124f89efd52SMatt Porter }
125f89efd52SMatt Porter 
rionet_rx_fill(struct net_device * ndev,int end)126f89efd52SMatt Porter static void rionet_rx_fill(struct net_device *ndev, int end)
127f89efd52SMatt Porter {
128f89efd52SMatt Porter 	int i;
1294cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
130f89efd52SMatt Porter 
131f89efd52SMatt Porter 	i = rnet->rx_slot;
132f89efd52SMatt Porter 	do {
133f89efd52SMatt Porter 		rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
134f89efd52SMatt Porter 
135f89efd52SMatt Porter 		if (!rnet->rx_skb[i])
136f89efd52SMatt Porter 			break;
137f89efd52SMatt Porter 
138f89efd52SMatt Porter 		rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
139f89efd52SMatt Porter 				   rnet->rx_skb[i]->data);
140f89efd52SMatt Porter 	} while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
141f89efd52SMatt Porter 
142f89efd52SMatt Porter 	rnet->rx_slot = i;
143f89efd52SMatt Porter }
144f89efd52SMatt Porter 
rionet_queue_tx_msg(struct sk_buff * skb,struct net_device * ndev,struct rio_dev * rdev)145f89efd52SMatt Porter static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
146f89efd52SMatt Porter 			       struct rio_dev *rdev)
147f89efd52SMatt Porter {
1484cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
149f89efd52SMatt Porter 
150f89efd52SMatt Porter 	rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
151f89efd52SMatt Porter 	rnet->tx_skb[rnet->tx_slot] = skb;
152f89efd52SMatt Porter 
15309f75cd7SJeff Garzik 	ndev->stats.tx_packets++;
15409f75cd7SJeff Garzik 	ndev->stats.tx_bytes += skb->len;
155f89efd52SMatt Porter 
156f89efd52SMatt Porter 	if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
157f89efd52SMatt Porter 		netif_stop_queue(ndev);
158f89efd52SMatt Porter 
159f89efd52SMatt Porter 	++rnet->tx_slot;
160f89efd52SMatt Porter 	rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
161f89efd52SMatt Porter 
162f89efd52SMatt Porter 	if (netif_msg_tx_queued(rnet))
1638df8a475SDavid S. Miller 		printk(KERN_INFO "%s: queued skb len %8.8x\n", DRV_NAME,
1648df8a475SDavid S. Miller 		       skb->len);
165f89efd52SMatt Porter 
166f89efd52SMatt Porter 	return 0;
167f89efd52SMatt Porter }
168f89efd52SMatt Porter 
rionet_start_xmit(struct sk_buff * skb,struct net_device * ndev)16951070a36SYunjian Wang static netdev_tx_t rionet_start_xmit(struct sk_buff *skb,
17051070a36SYunjian Wang 				     struct net_device *ndev)
171f89efd52SMatt Porter {
172f89efd52SMatt Porter 	int i;
1734cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
174f89efd52SMatt Porter 	struct ethhdr *eth = (struct ethhdr *)skb->data;
175f89efd52SMatt Porter 	u16 destid;
176f89efd52SMatt Porter 	unsigned long flags;
1777c4a6106SAlexandre Bounine 	int add_num = 1;
178f89efd52SMatt Porter 
179a6086a89SFlorian Westphal 	spin_lock_irqsave(&rnet->tx_lock, flags);
180f89efd52SMatt Porter 
1817c4a6106SAlexandre Bounine 	if (is_multicast_ether_addr(eth->h_dest))
1822fb717ecSAlexandre Bounine 		add_num = nets[rnet->mport->id].nact;
1837c4a6106SAlexandre Bounine 
1847c4a6106SAlexandre Bounine 	if ((rnet->tx_cnt + add_num) > RIONET_TX_RING_SIZE) {
185f89efd52SMatt Porter 		netif_stop_queue(ndev);
186f89efd52SMatt Porter 		spin_unlock_irqrestore(&rnet->tx_lock, flags);
187f89efd52SMatt Porter 		printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
188f89efd52SMatt Porter 		       ndev->name);
189f89efd52SMatt Porter 		return NETDEV_TX_BUSY;
190f89efd52SMatt Porter 	}
191f89efd52SMatt Porter 
192abfc89c7STobias Klauser 	if (is_multicast_ether_addr(eth->h_dest)) {
1937c4a6106SAlexandre Bounine 		int count = 0;
1942fb717ecSAlexandre Bounine 
195e0423236SZhang Wei 		for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
196e0423236SZhang Wei 				i++)
1972fb717ecSAlexandre Bounine 			if (nets[rnet->mport->id].active[i]) {
198f89efd52SMatt Porter 				rionet_queue_tx_msg(skb, ndev,
1992fb717ecSAlexandre Bounine 					nets[rnet->mport->id].active[i]);
2007c4a6106SAlexandre Bounine 				if (count)
20163354797SReshetova, Elena 					refcount_inc(&skb->users);
2027c4a6106SAlexandre Bounine 				count++;
2037c4a6106SAlexandre Bounine 			}
204f89efd52SMatt Porter 	} else if (RIONET_MAC_MATCH(eth->h_dest)) {
205f89efd52SMatt Porter 		destid = RIONET_GET_DESTID(eth->h_dest);
2062fb717ecSAlexandre Bounine 		if (nets[rnet->mport->id].active[destid])
2072fb717ecSAlexandre Bounine 			rionet_queue_tx_msg(skb, ndev,
2082fb717ecSAlexandre Bounine 					nets[rnet->mport->id].active[destid]);
209e6161d64SAlexandre Bounine 		else {
210e6161d64SAlexandre Bounine 			/*
211e6161d64SAlexandre Bounine 			 * If the target device was removed from the list of
212e6161d64SAlexandre Bounine 			 * active peers but we still have TX packets targeting
213e6161d64SAlexandre Bounine 			 * it just report sending a packet to the target
214e6161d64SAlexandre Bounine 			 * (without actual packet transfer).
215e6161d64SAlexandre Bounine 			 */
216e6161d64SAlexandre Bounine 			ndev->stats.tx_packets++;
217e6161d64SAlexandre Bounine 			ndev->stats.tx_bytes += skb->len;
218cfc43519SPan Bian 			dev_kfree_skb_any(skb);
219e6161d64SAlexandre Bounine 		}
220f89efd52SMatt Porter 	}
221f89efd52SMatt Porter 
222f89efd52SMatt Porter 	spin_unlock_irqrestore(&rnet->tx_lock, flags);
223f89efd52SMatt Porter 
2246ed10654SPatrick McHardy 	return NETDEV_TX_OK;
225f89efd52SMatt Porter }
226f89efd52SMatt Porter 
rionet_dbell_event(struct rio_mport * mport,void * dev_id,u16 sid,u16 tid,u16 info)227f89efd52SMatt Porter static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
228f89efd52SMatt Porter 			       u16 info)
229f89efd52SMatt Porter {
230f89efd52SMatt Porter 	struct net_device *ndev = dev_id;
2314cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
232f89efd52SMatt Porter 	struct rionet_peer *peer;
23334ed2ebbSAlexandre Bounine 	unsigned char netid = rnet->mport->id;
234f89efd52SMatt Porter 
235f89efd52SMatt Porter 	if (netif_msg_intr(rnet))
236f89efd52SMatt Porter 		printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
237f89efd52SMatt Porter 		       DRV_NAME, sid, tid, info);
238f89efd52SMatt Porter 	if (info == RIONET_DOORBELL_JOIN) {
23934ed2ebbSAlexandre Bounine 		if (!nets[netid].active[sid]) {
24034ed2ebbSAlexandre Bounine 			spin_lock(&nets[netid].lock);
24134ed2ebbSAlexandre Bounine 			list_for_each_entry(peer, &nets[netid].peers, node) {
2427c4a6106SAlexandre Bounine 				if (peer->rdev->destid == sid) {
24334ed2ebbSAlexandre Bounine 					nets[netid].active[sid] = peer->rdev;
24434ed2ebbSAlexandre Bounine 					nets[netid].nact++;
2457c4a6106SAlexandre Bounine 				}
246f89efd52SMatt Porter 			}
24734ed2ebbSAlexandre Bounine 			spin_unlock(&nets[netid].lock);
24834ed2ebbSAlexandre Bounine 
249f89efd52SMatt Porter 			rio_mport_send_doorbell(mport, sid,
250f89efd52SMatt Porter 						RIONET_DOORBELL_JOIN);
251f89efd52SMatt Porter 		}
252f89efd52SMatt Porter 	} else if (info == RIONET_DOORBELL_LEAVE) {
25334ed2ebbSAlexandre Bounine 		spin_lock(&nets[netid].lock);
25434ed2ebbSAlexandre Bounine 		if (nets[netid].active[sid]) {
25534ed2ebbSAlexandre Bounine 			nets[netid].active[sid] = NULL;
25634ed2ebbSAlexandre Bounine 			nets[netid].nact--;
25734ed2ebbSAlexandre Bounine 		}
25834ed2ebbSAlexandre Bounine 		spin_unlock(&nets[netid].lock);
259f89efd52SMatt Porter 	} else {
260f89efd52SMatt Porter 		if (netif_msg_intr(rnet))
261f89efd52SMatt Porter 			printk(KERN_WARNING "%s: unhandled doorbell\n",
262f89efd52SMatt Porter 			       DRV_NAME);
263f89efd52SMatt Porter 	}
264f89efd52SMatt Porter }
265f89efd52SMatt Porter 
rionet_inb_msg_event(struct rio_mport * mport,void * dev_id,int mbox,int slot)266f89efd52SMatt Porter static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
267f89efd52SMatt Porter {
268f89efd52SMatt Porter 	int n;
269f89efd52SMatt Porter 	struct net_device *ndev = dev_id;
2704cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
271f89efd52SMatt Porter 
272f89efd52SMatt Porter 	if (netif_msg_intr(rnet))
273f89efd52SMatt Porter 		printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
274f89efd52SMatt Porter 		       DRV_NAME, mbox, slot);
275f89efd52SMatt Porter 
276f89efd52SMatt Porter 	spin_lock(&rnet->lock);
277f89efd52SMatt Porter 	if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
278f89efd52SMatt Porter 		rionet_rx_fill(ndev, n);
279f89efd52SMatt Porter 	spin_unlock(&rnet->lock);
280f89efd52SMatt Porter }
281f89efd52SMatt Porter 
rionet_outb_msg_event(struct rio_mport * mport,void * dev_id,int mbox,int slot)282f89efd52SMatt Porter static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
283f89efd52SMatt Porter {
284f89efd52SMatt Porter 	struct net_device *ndev = dev_id;
2854cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
286f89efd52SMatt Porter 
28736915976SAurelien Jacquiot 	spin_lock(&rnet->tx_lock);
288f89efd52SMatt Porter 
289f89efd52SMatt Porter 	if (netif_msg_intr(rnet))
290f89efd52SMatt Porter 		printk(KERN_INFO
291f89efd52SMatt Porter 		       "%s: outbound message event, mbox %d slot %d\n",
292f89efd52SMatt Porter 		       DRV_NAME, mbox, slot);
293f89efd52SMatt Porter 
294f89efd52SMatt Porter 	while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
295f89efd52SMatt Porter 		/* dma unmap single */
296f89efd52SMatt Porter 		dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
297f89efd52SMatt Porter 		rnet->tx_skb[rnet->ack_slot] = NULL;
298f89efd52SMatt Porter 		++rnet->ack_slot;
299f89efd52SMatt Porter 		rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
300f89efd52SMatt Porter 		rnet->tx_cnt--;
301f89efd52SMatt Porter 	}
302f89efd52SMatt Porter 
303f89efd52SMatt Porter 	if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
304f89efd52SMatt Porter 		netif_wake_queue(ndev);
305f89efd52SMatt Porter 
30636915976SAurelien Jacquiot 	spin_unlock(&rnet->tx_lock);
307f89efd52SMatt Porter }
308f89efd52SMatt Porter 
rionet_open(struct net_device * ndev)309f89efd52SMatt Porter static int rionet_open(struct net_device *ndev)
310f89efd52SMatt Porter {
311f89efd52SMatt Porter 	int i, rc = 0;
31234ed2ebbSAlexandre Bounine 	struct rionet_peer *peer;
3134cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
31434ed2ebbSAlexandre Bounine 	unsigned char netid = rnet->mport->id;
31534ed2ebbSAlexandre Bounine 	unsigned long flags;
316f89efd52SMatt Porter 
317f89efd52SMatt Porter 	if (netif_msg_ifup(rnet))
318f89efd52SMatt Porter 		printk(KERN_INFO "%s: open\n", DRV_NAME);
319f89efd52SMatt Porter 
320f89efd52SMatt Porter 	if ((rc = rio_request_inb_dbell(rnet->mport,
321f89efd52SMatt Porter 					(void *)ndev,
322f89efd52SMatt Porter 					RIONET_DOORBELL_JOIN,
323f89efd52SMatt Porter 					RIONET_DOORBELL_LEAVE,
324f89efd52SMatt Porter 					rionet_dbell_event)) < 0)
325f89efd52SMatt Porter 		goto out;
326f89efd52SMatt Porter 
327f89efd52SMatt Porter 	if ((rc = rio_request_inb_mbox(rnet->mport,
328f89efd52SMatt Porter 				       (void *)ndev,
329f89efd52SMatt Porter 				       RIONET_MAILBOX,
330f89efd52SMatt Porter 				       RIONET_RX_RING_SIZE,
331f89efd52SMatt Porter 				       rionet_inb_msg_event)) < 0)
332f89efd52SMatt Porter 		goto out;
333f89efd52SMatt Porter 
334f89efd52SMatt Porter 	if ((rc = rio_request_outb_mbox(rnet->mport,
335f89efd52SMatt Porter 					(void *)ndev,
336f89efd52SMatt Porter 					RIONET_MAILBOX,
337f89efd52SMatt Porter 					RIONET_TX_RING_SIZE,
338f89efd52SMatt Porter 					rionet_outb_msg_event)) < 0)
339f89efd52SMatt Porter 		goto out;
340f89efd52SMatt Porter 
341f89efd52SMatt Porter 	/* Initialize inbound message ring */
342f89efd52SMatt Porter 	for (i = 0; i < RIONET_RX_RING_SIZE; i++)
343f89efd52SMatt Porter 		rnet->rx_skb[i] = NULL;
344f89efd52SMatt Porter 	rnet->rx_slot = 0;
345f89efd52SMatt Porter 	rionet_rx_fill(ndev, 0);
346f89efd52SMatt Porter 
347f89efd52SMatt Porter 	rnet->tx_slot = 0;
348f89efd52SMatt Porter 	rnet->tx_cnt = 0;
349f89efd52SMatt Porter 	rnet->ack_slot = 0;
350f89efd52SMatt Porter 
351f89efd52SMatt Porter 	netif_carrier_on(ndev);
352f89efd52SMatt Porter 	netif_start_queue(ndev);
353f89efd52SMatt Porter 
35434ed2ebbSAlexandre Bounine 	spin_lock_irqsave(&nets[netid].lock, flags);
35534ed2ebbSAlexandre Bounine 	list_for_each_entry(peer, &nets[netid].peers, node) {
356284fb68dSAlexandre Bounine 		/* Send a join message */
357f89efd52SMatt Porter 		rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
358f89efd52SMatt Porter 	}
35934ed2ebbSAlexandre Bounine 	spin_unlock_irqrestore(&nets[netid].lock, flags);
36034ed2ebbSAlexandre Bounine 	rnet->open = true;
361f89efd52SMatt Porter 
362f89efd52SMatt Porter       out:
363f89efd52SMatt Porter 	return rc;
364f89efd52SMatt Porter }
365f89efd52SMatt Porter 
rionet_close(struct net_device * ndev)366f89efd52SMatt Porter static int rionet_close(struct net_device *ndev)
367f89efd52SMatt Porter {
3684cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
36934ed2ebbSAlexandre Bounine 	struct rionet_peer *peer;
37034ed2ebbSAlexandre Bounine 	unsigned char netid = rnet->mport->id;
37134ed2ebbSAlexandre Bounine 	unsigned long flags;
372f89efd52SMatt Porter 	int i;
373f89efd52SMatt Porter 
374f89efd52SMatt Porter 	if (netif_msg_ifup(rnet))
3752fb717ecSAlexandre Bounine 		printk(KERN_INFO "%s: close %s\n", DRV_NAME, ndev->name);
376f89efd52SMatt Porter 
377f89efd52SMatt Porter 	netif_stop_queue(ndev);
378f89efd52SMatt Porter 	netif_carrier_off(ndev);
37934ed2ebbSAlexandre Bounine 	rnet->open = false;
380f89efd52SMatt Porter 
381f89efd52SMatt Porter 	for (i = 0; i < RIONET_RX_RING_SIZE; i++)
382f89efd52SMatt Porter 		kfree_skb(rnet->rx_skb[i]);
383f89efd52SMatt Porter 
38434ed2ebbSAlexandre Bounine 	spin_lock_irqsave(&nets[netid].lock, flags);
38534ed2ebbSAlexandre Bounine 	list_for_each_entry(peer, &nets[netid].peers, node) {
38634ed2ebbSAlexandre Bounine 		if (nets[netid].active[peer->rdev->destid]) {
387f89efd52SMatt Porter 			rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
38834ed2ebbSAlexandre Bounine 			nets[netid].active[peer->rdev->destid] = NULL;
389f89efd52SMatt Porter 		}
39034ed2ebbSAlexandre Bounine 		if (peer->res)
391f89efd52SMatt Porter 			rio_release_outb_dbell(peer->rdev, peer->res);
392f89efd52SMatt Porter 	}
39334ed2ebbSAlexandre Bounine 	spin_unlock_irqrestore(&nets[netid].lock, flags);
394f89efd52SMatt Porter 
395f89efd52SMatt Porter 	rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
396f89efd52SMatt Porter 			      RIONET_DOORBELL_LEAVE);
397f89efd52SMatt Porter 	rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
398f89efd52SMatt Porter 	rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
399f89efd52SMatt Porter 
400f89efd52SMatt Porter 	return 0;
401f89efd52SMatt Porter }
402f89efd52SMatt Porter 
rionet_remove_dev(struct device * dev,struct subsys_interface * sif)40371db87baSViresh Kumar static void rionet_remove_dev(struct device *dev, struct subsys_interface *sif)
404f89efd52SMatt Porter {
405e6161d64SAlexandre Bounine 	struct rio_dev *rdev = to_rio_dev(dev);
4062fb717ecSAlexandre Bounine 	unsigned char netid = rdev->net->hport->id;
40734ed2ebbSAlexandre Bounine 	struct rionet_peer *peer;
40834ed2ebbSAlexandre Bounine 	int state, found = 0;
40934ed2ebbSAlexandre Bounine 	unsigned long flags;
410f89efd52SMatt Porter 
41134ed2ebbSAlexandre Bounine 	if (!dev_rionet_capable(rdev))
41234ed2ebbSAlexandre Bounine 		return;
41334ed2ebbSAlexandre Bounine 
41434ed2ebbSAlexandre Bounine 	spin_lock_irqsave(&nets[netid].lock, flags);
41534ed2ebbSAlexandre Bounine 	list_for_each_entry(peer, &nets[netid].peers, node) {
416e6161d64SAlexandre Bounine 		if (peer->rdev == rdev) {
41734ed2ebbSAlexandre Bounine 			list_del(&peer->node);
418e6161d64SAlexandre Bounine 			if (nets[netid].active[rdev->destid]) {
41934ed2ebbSAlexandre Bounine 				state = atomic_read(&rdev->state);
42034ed2ebbSAlexandre Bounine 				if (state != RIO_DEVICE_GONE &&
42134ed2ebbSAlexandre Bounine 				    state != RIO_DEVICE_INITIALIZING) {
42234ed2ebbSAlexandre Bounine 					rio_send_doorbell(rdev,
42334ed2ebbSAlexandre Bounine 							RIONET_DOORBELL_LEAVE);
42434ed2ebbSAlexandre Bounine 				}
425e6161d64SAlexandre Bounine 				nets[netid].active[rdev->destid] = NULL;
426e6161d64SAlexandre Bounine 				nets[netid].nact--;
427f89efd52SMatt Porter 			}
42834ed2ebbSAlexandre Bounine 			found = 1;
429e6161d64SAlexandre Bounine 			break;
430e6161d64SAlexandre Bounine 		}
431e6161d64SAlexandre Bounine 	}
43234ed2ebbSAlexandre Bounine 	spin_unlock_irqrestore(&nets[netid].lock, flags);
43334ed2ebbSAlexandre Bounine 
43434ed2ebbSAlexandre Bounine 	if (found) {
43534ed2ebbSAlexandre Bounine 		if (peer->res)
43634ed2ebbSAlexandre Bounine 			rio_release_outb_dbell(rdev, peer->res);
43734ed2ebbSAlexandre Bounine 		kfree(peer);
438e6161d64SAlexandre Bounine 	}
439f89efd52SMatt Porter }
440f89efd52SMatt Porter 
rionet_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * info)441f89efd52SMatt Porter static void rionet_get_drvinfo(struct net_device *ndev,
442f89efd52SMatt Porter 			       struct ethtool_drvinfo *info)
443f89efd52SMatt Porter {
4444cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
445f89efd52SMatt Porter 
446fb3ceec1SWolfram Sang 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
447fb3ceec1SWolfram Sang 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
448fb3ceec1SWolfram Sang 	strscpy(info->fw_version, "n/a", sizeof(info->fw_version));
449fb3ceec1SWolfram Sang 	strscpy(info->bus_info, rnet->mport->name, sizeof(info->bus_info));
450f89efd52SMatt Porter }
451f89efd52SMatt Porter 
rionet_get_msglevel(struct net_device * ndev)452f89efd52SMatt Porter static u32 rionet_get_msglevel(struct net_device *ndev)
453f89efd52SMatt Porter {
4544cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
455f89efd52SMatt Porter 
456f89efd52SMatt Porter 	return rnet->msg_enable;
457f89efd52SMatt Porter }
458f89efd52SMatt Porter 
rionet_set_msglevel(struct net_device * ndev,u32 value)459f89efd52SMatt Porter static void rionet_set_msglevel(struct net_device *ndev, u32 value)
460f89efd52SMatt Porter {
4614cf1653aSWang Chen 	struct rionet_private *rnet = netdev_priv(ndev);
462f89efd52SMatt Porter 
463f89efd52SMatt Porter 	rnet->msg_enable = value;
464f89efd52SMatt Porter }
465f89efd52SMatt Porter 
4667282d491SJeff Garzik static const struct ethtool_ops rionet_ethtool_ops = {
467f89efd52SMatt Porter 	.get_drvinfo = rionet_get_drvinfo,
468f89efd52SMatt Porter 	.get_msglevel = rionet_get_msglevel,
469f89efd52SMatt Porter 	.set_msglevel = rionet_set_msglevel,
470f89efd52SMatt Porter 	.get_link = ethtool_op_get_link,
471f89efd52SMatt Porter };
472f89efd52SMatt Porter 
473a33a2bb3SAlexander Beregalov static const struct net_device_ops rionet_netdev_ops = {
474a33a2bb3SAlexander Beregalov 	.ndo_open		= rionet_open,
475a33a2bb3SAlexander Beregalov 	.ndo_stop		= rionet_close,
476a33a2bb3SAlexander Beregalov 	.ndo_start_xmit		= rionet_start_xmit,
477a33a2bb3SAlexander Beregalov 	.ndo_validate_addr	= eth_validate_addr,
478a33a2bb3SAlexander Beregalov 	.ndo_set_mac_address	= eth_mac_addr,
479a33a2bb3SAlexander Beregalov };
480a33a2bb3SAlexander Beregalov 
rionet_setup_netdev(struct rio_mport * mport,struct net_device * ndev)48155caa924SYinglin Luan static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
482f89efd52SMatt Porter {
483f89efd52SMatt Porter 	int rc = 0;
484f89efd52SMatt Porter 	struct rionet_private *rnet;
4855f07da89SJakub Kicinski 	u8 addr[ETH_ALEN];
486f89efd52SMatt Porter 	u16 device_id;
487acc65632SAkinobu Mita 	const size_t rionet_active_bytes = sizeof(void *) *
488acc65632SAkinobu Mita 				RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
489f89efd52SMatt Porter 
4902fb717ecSAlexandre Bounine 	nets[mport->id].active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
491acc65632SAkinobu Mita 						get_order(rionet_active_bytes));
4922fb717ecSAlexandre Bounine 	if (!nets[mport->id].active) {
493e0423236SZhang Wei 		rc = -ENOMEM;
494e0423236SZhang Wei 		goto out;
495e0423236SZhang Wei 	}
4962fb717ecSAlexandre Bounine 	memset((void *)nets[mport->id].active, 0, rionet_active_bytes);
497e0423236SZhang Wei 
498f89efd52SMatt Porter 	/* Set up private area */
4994cf1653aSWang Chen 	rnet = netdev_priv(ndev);
500f89efd52SMatt Porter 	rnet->mport = mport;
50134ed2ebbSAlexandre Bounine 	rnet->open = false;
502f89efd52SMatt Porter 
503f89efd52SMatt Porter 	/* Set the default MAC address */
504f89efd52SMatt Porter 	device_id = rio_local_get_device_id(mport);
5055f07da89SJakub Kicinski 	addr[0] = 0x00;
5065f07da89SJakub Kicinski 	addr[1] = 0x01;
5075f07da89SJakub Kicinski 	addr[2] = 0x00;
5085f07da89SJakub Kicinski 	addr[3] = 0x01;
5095f07da89SJakub Kicinski 	addr[4] = device_id >> 8;
5105f07da89SJakub Kicinski 	addr[5] = device_id & 0xff;
5115f07da89SJakub Kicinski 	eth_hw_addr_set(ndev, addr);
512f89efd52SMatt Porter 
513a33a2bb3SAlexander Beregalov 	ndev->netdev_ops = &rionet_netdev_ops;
51492444bb3SAurelien Jacquiot 	ndev->mtu = RIONET_MAX_MTU;
515b3e3893eSJarod Wilson 	/* MTU range: 68 - 4082 */
516b3e3893eSJarod Wilson 	ndev->min_mtu = ETH_MIN_MTU;
517b3e3893eSJarod Wilson 	ndev->max_mtu = RIONET_MAX_MTU;
518f89efd52SMatt Porter 	ndev->features = NETIF_F_LLTX;
5192aaf308bSAlexandre Bounine 	SET_NETDEV_DEV(ndev, &mport->dev);
5207ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &rionet_ethtool_ops;
521f89efd52SMatt Porter 
522f89efd52SMatt Porter 	spin_lock_init(&rnet->lock);
523f89efd52SMatt Porter 	spin_lock_init(&rnet->tx_lock);
524f89efd52SMatt Porter 
525f89efd52SMatt Porter 	rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
526f89efd52SMatt Porter 
527f89efd52SMatt Porter 	rc = register_netdev(ndev);
52834ed2ebbSAlexandre Bounine 	if (rc != 0) {
52934ed2ebbSAlexandre Bounine 		free_pages((unsigned long)nets[mport->id].active,
53034ed2ebbSAlexandre Bounine 			   get_order(rionet_active_bytes));
531f89efd52SMatt Porter 		goto out;
53234ed2ebbSAlexandre Bounine 	}
533f89efd52SMatt Porter 
5342fb717ecSAlexandre Bounine 	printk(KERN_INFO "%s: %s %s Version %s, MAC %pM, %s\n",
535f89efd52SMatt Porter 	       ndev->name,
536f89efd52SMatt Porter 	       DRV_NAME,
537f89efd52SMatt Porter 	       DRV_DESC,
538f89efd52SMatt Porter 	       DRV_VERSION,
5392fb717ecSAlexandre Bounine 	       ndev->dev_addr,
5402fb717ecSAlexandre Bounine 	       mport->name);
541f89efd52SMatt Porter 
542f89efd52SMatt Porter       out:
543f89efd52SMatt Porter 	return rc;
544f89efd52SMatt Porter }
545f89efd52SMatt Porter 
rionet_add_dev(struct device * dev,struct subsys_interface * sif)546e6161d64SAlexandre Bounine static int rionet_add_dev(struct device *dev, struct subsys_interface *sif)
547f89efd52SMatt Porter {
548f89efd52SMatt Porter 	int rc = -ENODEV;
549284fb68dSAlexandre Bounine 	u32 lsrc_ops, ldst_ops;
550f89efd52SMatt Porter 	struct rionet_peer *peer;
55155caa924SYinglin Luan 	struct net_device *ndev = NULL;
552e6161d64SAlexandre Bounine 	struct rio_dev *rdev = to_rio_dev(dev);
5532fb717ecSAlexandre Bounine 	unsigned char netid = rdev->net->hport->id;
554f89efd52SMatt Porter 
5552fb717ecSAlexandre Bounine 	if (netid >= RIONET_MAX_NETS)
5562fb717ecSAlexandre Bounine 		return rc;
5572fb717ecSAlexandre Bounine 
5582fb717ecSAlexandre Bounine 	/*
559e6161d64SAlexandre Bounine 	 * If first time through this net, make sure local device is rionet
560e6161d64SAlexandre Bounine 	 * capable and setup netdev (this step will be skipped in later probes
561e6161d64SAlexandre Bounine 	 * on the same net).
5622fb717ecSAlexandre Bounine 	 */
56334ed2ebbSAlexandre Bounine 	if (!nets[netid].ndev) {
5642fb717ecSAlexandre Bounine 		rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
5652fb717ecSAlexandre Bounine 					 &lsrc_ops);
5662fb717ecSAlexandre Bounine 		rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
5672fb717ecSAlexandre Bounine 					 &ldst_ops);
5682fb717ecSAlexandre Bounine 		if (!is_rionet_capable(lsrc_ops, ldst_ops)) {
5692fb717ecSAlexandre Bounine 			printk(KERN_ERR
5702fb717ecSAlexandre Bounine 			       "%s: local device %s is not network capable\n",
5712fb717ecSAlexandre Bounine 			       DRV_NAME, rdev->net->hport->name);
572f89efd52SMatt Porter 			goto out;
5732fb717ecSAlexandre Bounine 		}
574f89efd52SMatt Porter 
57555caa924SYinglin Luan 		/* Allocate our net_device structure */
57655caa924SYinglin Luan 		ndev = alloc_etherdev(sizeof(struct rionet_private));
57755caa924SYinglin Luan 		if (ndev == NULL) {
57855caa924SYinglin Luan 			rc = -ENOMEM;
57955caa924SYinglin Luan 			goto out;
58055caa924SYinglin Luan 		}
58134ed2ebbSAlexandre Bounine 
58255caa924SYinglin Luan 		rc = rionet_setup_netdev(rdev->net->hport, ndev);
583e6161d64SAlexandre Bounine 		if (rc) {
584e6161d64SAlexandre Bounine 			printk(KERN_ERR "%s: failed to setup netdev (rc=%d)\n",
585e6161d64SAlexandre Bounine 			       DRV_NAME, rc);
58634ed2ebbSAlexandre Bounine 			free_netdev(ndev);
587e6161d64SAlexandre Bounine 			goto out;
588e6161d64SAlexandre Bounine 		}
589e6161d64SAlexandre Bounine 
5902fb717ecSAlexandre Bounine 		INIT_LIST_HEAD(&nets[netid].peers);
59134ed2ebbSAlexandre Bounine 		spin_lock_init(&nets[netid].lock);
5922fb717ecSAlexandre Bounine 		nets[netid].nact = 0;
59334ed2ebbSAlexandre Bounine 		nets[netid].ndev = ndev;
59434ed2ebbSAlexandre Bounine 	}
595f89efd52SMatt Porter 
596f89efd52SMatt Porter 	/*
597f89efd52SMatt Porter 	 * If the remote device has mailbox/doorbell capabilities,
598f89efd52SMatt Porter 	 * add it to the peer list.
599f89efd52SMatt Porter 	 */
600f89efd52SMatt Porter 	if (dev_rionet_capable(rdev)) {
60134ed2ebbSAlexandre Bounine 		struct rionet_private *rnet;
60234ed2ebbSAlexandre Bounine 		unsigned long flags;
60334ed2ebbSAlexandre Bounine 
60434ed2ebbSAlexandre Bounine 		rnet = netdev_priv(nets[netid].ndev);
60534ed2ebbSAlexandre Bounine 
60634ed2ebbSAlexandre Bounine 		peer = kzalloc(sizeof(*peer), GFP_KERNEL);
60734ed2ebbSAlexandre Bounine 		if (!peer) {
608f89efd52SMatt Porter 			rc = -ENOMEM;
609f89efd52SMatt Porter 			goto out;
610f89efd52SMatt Porter 		}
611f89efd52SMatt Porter 		peer->rdev = rdev;
61234ed2ebbSAlexandre Bounine 		peer->res = rio_request_outb_dbell(peer->rdev,
61334ed2ebbSAlexandre Bounine 						RIONET_DOORBELL_JOIN,
61434ed2ebbSAlexandre Bounine 						RIONET_DOORBELL_LEAVE);
61534ed2ebbSAlexandre Bounine 		if (!peer->res) {
61634ed2ebbSAlexandre Bounine 			pr_err("%s: error requesting doorbells\n", DRV_NAME);
61734ed2ebbSAlexandre Bounine 			kfree(peer);
61834ed2ebbSAlexandre Bounine 			rc = -ENOMEM;
61934ed2ebbSAlexandre Bounine 			goto out;
62034ed2ebbSAlexandre Bounine 		}
62134ed2ebbSAlexandre Bounine 
62234ed2ebbSAlexandre Bounine 		spin_lock_irqsave(&nets[netid].lock, flags);
6232fb717ecSAlexandre Bounine 		list_add_tail(&peer->node, &nets[netid].peers);
62434ed2ebbSAlexandre Bounine 		spin_unlock_irqrestore(&nets[netid].lock, flags);
62534ed2ebbSAlexandre Bounine 		pr_debug("%s: %s add peer %s\n",
62634ed2ebbSAlexandre Bounine 			 DRV_NAME, __func__, rio_name(rdev));
62734ed2ebbSAlexandre Bounine 
62834ed2ebbSAlexandre Bounine 		/* If netdev is already opened, send join request to new peer */
62934ed2ebbSAlexandre Bounine 		if (rnet->open)
63034ed2ebbSAlexandre Bounine 			rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
631f89efd52SMatt Porter 	}
632f89efd52SMatt Porter 
633e6161d64SAlexandre Bounine 	return 0;
634f89efd52SMatt Porter out:
635f89efd52SMatt Porter 	return rc;
636f89efd52SMatt Porter }
637f89efd52SMatt Porter 
rionet_shutdown(struct notifier_block * nb,unsigned long code,void * unused)638f41e2472SAlexandre Bounine static int rionet_shutdown(struct notifier_block *nb, unsigned long code,
639f41e2472SAlexandre Bounine 			   void *unused)
640f41e2472SAlexandre Bounine {
64134ed2ebbSAlexandre Bounine 	struct rionet_peer *peer;
64234ed2ebbSAlexandre Bounine 	unsigned long flags;
643f41e2472SAlexandre Bounine 	int i;
644f41e2472SAlexandre Bounine 
645f41e2472SAlexandre Bounine 	pr_debug("%s: %s\n", DRV_NAME, __func__);
646f41e2472SAlexandre Bounine 
647f41e2472SAlexandre Bounine 	for (i = 0; i < RIONET_MAX_NETS; i++) {
648f41e2472SAlexandre Bounine 		if (!nets[i].ndev)
649f41e2472SAlexandre Bounine 			continue;
650f41e2472SAlexandre Bounine 
65134ed2ebbSAlexandre Bounine 		spin_lock_irqsave(&nets[i].lock, flags);
65234ed2ebbSAlexandre Bounine 		list_for_each_entry(peer, &nets[i].peers, node) {
653f41e2472SAlexandre Bounine 			if (nets[i].active[peer->rdev->destid]) {
654f41e2472SAlexandre Bounine 				rio_send_doorbell(peer->rdev,
655f41e2472SAlexandre Bounine 						  RIONET_DOORBELL_LEAVE);
656f41e2472SAlexandre Bounine 				nets[i].active[peer->rdev->destid] = NULL;
657f41e2472SAlexandre Bounine 			}
658f41e2472SAlexandre Bounine 		}
65934ed2ebbSAlexandre Bounine 		spin_unlock_irqrestore(&nets[i].lock, flags);
660f41e2472SAlexandre Bounine 	}
661f41e2472SAlexandre Bounine 
662f41e2472SAlexandre Bounine 	return NOTIFY_DONE;
663f41e2472SAlexandre Bounine }
664f41e2472SAlexandre Bounine 
rionet_remove_mport(struct device * dev)665*2243acd5SGreg Kroah-Hartman static void rionet_remove_mport(struct device *dev)
666b7dfca8bSAlexandre Bounine {
667b7dfca8bSAlexandre Bounine 	struct rio_mport *mport = to_rio_mport(dev);
668b7dfca8bSAlexandre Bounine 	struct net_device *ndev;
669b7dfca8bSAlexandre Bounine 	int id = mport->id;
670b7dfca8bSAlexandre Bounine 
671b7dfca8bSAlexandre Bounine 	pr_debug("%s %s\n", __func__, mport->name);
672b7dfca8bSAlexandre Bounine 
673b7dfca8bSAlexandre Bounine 	WARN(nets[id].nact, "%s called when connected to %d peers\n",
674b7dfca8bSAlexandre Bounine 	     __func__, nets[id].nact);
675b7dfca8bSAlexandre Bounine 	WARN(!nets[id].ndev, "%s called for mport without NDEV\n",
676b7dfca8bSAlexandre Bounine 	     __func__);
677b7dfca8bSAlexandre Bounine 
678b7dfca8bSAlexandre Bounine 	if (nets[id].ndev) {
679b7dfca8bSAlexandre Bounine 		ndev = nets[id].ndev;
680b7dfca8bSAlexandre Bounine 		netif_stop_queue(ndev);
681b7dfca8bSAlexandre Bounine 		unregister_netdev(ndev);
682b7dfca8bSAlexandre Bounine 
683b7dfca8bSAlexandre Bounine 		free_pages((unsigned long)nets[id].active,
684b7dfca8bSAlexandre Bounine 			   get_order(sizeof(void *) *
685b7dfca8bSAlexandre Bounine 			   RIO_MAX_ROUTE_ENTRIES(mport->sys_size)));
686b7dfca8bSAlexandre Bounine 		nets[id].active = NULL;
687b7dfca8bSAlexandre Bounine 		free_netdev(ndev);
688b7dfca8bSAlexandre Bounine 		nets[id].ndev = NULL;
689b7dfca8bSAlexandre Bounine 	}
690b7dfca8bSAlexandre Bounine }
691b7dfca8bSAlexandre Bounine 
692e6161d64SAlexandre Bounine #ifdef MODULE
693f89efd52SMatt Porter static struct rio_device_id rionet_id_table[] = {
694e6161d64SAlexandre Bounine 	{RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)},
695e6161d64SAlexandre Bounine 	{ 0, }	/* terminate list */
696f89efd52SMatt Porter };
697f89efd52SMatt Porter 
698e6161d64SAlexandre Bounine MODULE_DEVICE_TABLE(rapidio, rionet_id_table);
699e6161d64SAlexandre Bounine #endif
700e6161d64SAlexandre Bounine 
701e6161d64SAlexandre Bounine static struct subsys_interface rionet_interface = {
702f89efd52SMatt Porter 	.name		= "rionet",
703e6161d64SAlexandre Bounine 	.subsys		= &rio_bus_type,
704e6161d64SAlexandre Bounine 	.add_dev	= rionet_add_dev,
705e6161d64SAlexandre Bounine 	.remove_dev	= rionet_remove_dev,
706f89efd52SMatt Porter };
707f89efd52SMatt Porter 
708f41e2472SAlexandre Bounine static struct notifier_block rionet_notifier = {
709f41e2472SAlexandre Bounine 	.notifier_call = rionet_shutdown,
710f41e2472SAlexandre Bounine };
711f41e2472SAlexandre Bounine 
712b7dfca8bSAlexandre Bounine /* the rio_mport_interface is used to handle local mport devices */
713b7dfca8bSAlexandre Bounine static struct class_interface rio_mport_interface __refdata = {
714b7dfca8bSAlexandre Bounine 	.class = &rio_mport_class,
715b7dfca8bSAlexandre Bounine 	.add_dev = NULL,
716b7dfca8bSAlexandre Bounine 	.remove_dev = rionet_remove_mport,
717b7dfca8bSAlexandre Bounine };
718b7dfca8bSAlexandre Bounine 
rionet_init(void)719f89efd52SMatt Porter static int __init rionet_init(void)
720f89efd52SMatt Porter {
721f41e2472SAlexandre Bounine 	int ret;
722f41e2472SAlexandre Bounine 
723f41e2472SAlexandre Bounine 	ret = register_reboot_notifier(&rionet_notifier);
724f41e2472SAlexandre Bounine 	if (ret) {
725f41e2472SAlexandre Bounine 		pr_err("%s: failed to register reboot notifier (err=%d)\n",
726f41e2472SAlexandre Bounine 		       DRV_NAME, ret);
727f41e2472SAlexandre Bounine 		return ret;
728f41e2472SAlexandre Bounine 	}
729b7dfca8bSAlexandre Bounine 
730b7dfca8bSAlexandre Bounine 	ret = class_interface_register(&rio_mport_interface);
731b7dfca8bSAlexandre Bounine 	if (ret) {
732b7dfca8bSAlexandre Bounine 		pr_err("%s: class_interface_register error: %d\n",
733b7dfca8bSAlexandre Bounine 		       DRV_NAME, ret);
734b7dfca8bSAlexandre Bounine 		return ret;
735b7dfca8bSAlexandre Bounine 	}
736b7dfca8bSAlexandre Bounine 
737e6161d64SAlexandre Bounine 	return subsys_interface_register(&rionet_interface);
738f89efd52SMatt Porter }
739f89efd52SMatt Porter 
rionet_exit(void)740f89efd52SMatt Porter static void __exit rionet_exit(void)
741f89efd52SMatt Porter {
742f41e2472SAlexandre Bounine 	unregister_reboot_notifier(&rionet_notifier);
743e6161d64SAlexandre Bounine 	subsys_interface_unregister(&rionet_interface);
744b7dfca8bSAlexandre Bounine 	class_interface_unregister(&rio_mport_interface);
745f89efd52SMatt Porter }
746f89efd52SMatt Porter 
7472f809985SAlexandre Bounine late_initcall(rionet_init);
748f89efd52SMatt Porter module_exit(rionet_exit);
749