blob: f994fa1fde2ff485a56d561fabd13e8cdab9df38 [file] [log] [blame]
Matt Porterf89efd52005-09-09 12:10:10 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Matt Porterf89efd52005-09-09 12:10:10 -070020#include <linux/rio_ids.h>
21
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/skbuff.h>
25#include <linux/crc32.h>
26#include <linux/ethtool.h>
Alexandre Bouninef41e2472016-03-22 14:26:11 -070027#include <linux/reboot.h>
Matt Porterf89efd52005-09-09 12:10:10 -070028
29#define DRV_NAME "rionet"
Alexandre Bounine2fb717e2012-10-04 17:16:11 -070030#define DRV_VERSION "0.3"
Matt Porterf89efd52005-09-09 12:10:10 -070031#define DRV_AUTHOR "Matt Porter <mporter@kernel.crashing.org>"
32#define DRV_DESC "Ethernet over RapidIO"
33
34MODULE_AUTHOR(DRV_AUTHOR);
35MODULE_DESCRIPTION(DRV_DESC);
36MODULE_LICENSE("GPL");
37
38#define RIONET_DEFAULT_MSGLEVEL \
39 (NETIF_MSG_DRV | \
40 NETIF_MSG_LINK | \
41 NETIF_MSG_RX_ERR | \
42 NETIF_MSG_TX_ERR)
43
44#define RIONET_DOORBELL_JOIN 0x1000
45#define RIONET_DOORBELL_LEAVE 0x1001
46
47#define RIONET_MAILBOX 0
48
49#define RIONET_TX_RING_SIZE CONFIG_RIONET_TX_SIZE
50#define RIONET_RX_RING_SIZE CONFIG_RIONET_RX_SIZE
Alexandre Bounine2fb717e2012-10-04 17:16:11 -070051#define RIONET_MAX_NETS 8
Aurelien Jacquiot92444bb2016-03-22 14:25:45 -070052#define RIONET_MSG_SIZE RIO_MAX_MSG_SIZE
53#define RIONET_MAX_MTU (RIONET_MSG_SIZE - ETH_HLEN)
Matt Porterf89efd52005-09-09 12:10:10 -070054
55struct rionet_private {
56 struct rio_mport *mport;
57 struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
58 struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
Matt Porterf89efd52005-09-09 12:10:10 -070059 int rx_slot;
60 int tx_slot;
61 int tx_cnt;
62 int ack_slot;
63 spinlock_t lock;
64 spinlock_t tx_lock;
65 u32 msg_enable;
66};
67
68struct rionet_peer {
69 struct list_head node;
70 struct rio_dev *rdev;
71 struct resource *res;
72};
73
Alexandre Bounine2fb717e2012-10-04 17:16:11 -070074struct rionet_net {
75 struct net_device *ndev;
76 struct list_head peers;
77 struct rio_dev **active;
78 int nact; /* number of active peers */
79};
Matt Porterf89efd52005-09-09 12:10:10 -070080
Alexandre Bounine2fb717e2012-10-04 17:16:11 -070081static struct rionet_net nets[RIONET_MAX_NETS];
Matt Porterf89efd52005-09-09 12:10:10 -070082
Alexandre Bounine284fb682011-08-25 15:59:13 -070083#define is_rionet_capable(src_ops, dst_ops) \
84 ((src_ops & RIO_SRC_OPS_DATA_MSG) && \
85 (dst_ops & RIO_DST_OPS_DATA_MSG) && \
Matt Porterf89efd52005-09-09 12:10:10 -070086 (src_ops & RIO_SRC_OPS_DOORBELL) && \
87 (dst_ops & RIO_DST_OPS_DOORBELL))
88#define dev_rionet_capable(dev) \
Alexandre Bounine284fb682011-08-25 15:59:13 -070089 is_rionet_capable(dev->src_ops, dev->dst_ops)
Matt Porterf89efd52005-09-09 12:10:10 -070090
Alexandre Bouninee0c87bd2011-11-02 13:39:15 -070091#define RIONET_MAC_MATCH(x) (!memcmp((x), "\00\01\00\01", 4))
92#define RIONET_GET_DESTID(x) ((*((u8 *)x + 4) << 8) | *((u8 *)x + 5))
Matt Porterf89efd52005-09-09 12:10:10 -070093
Matt Porterf89efd52005-09-09 12:10:10 -070094static int rionet_rx_clean(struct net_device *ndev)
95{
96 int i;
97 int error = 0;
Wang Chen4cf16532008-11-12 23:38:14 -080098 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -070099 void *data;
100
101 i = rnet->rx_slot;
102
103 do {
104 if (!rnet->rx_skb[i])
105 continue;
106
107 if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
108 break;
109
110 rnet->rx_skb[i]->data = data;
111 skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
Matt Porterf89efd52005-09-09 12:10:10 -0700112 rnet->rx_skb[i]->protocol =
113 eth_type_trans(rnet->rx_skb[i], ndev);
114 error = netif_rx(rnet->rx_skb[i]);
115
116 if (error == NET_RX_DROP) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700117 ndev->stats.rx_dropped++;
Matt Porterf89efd52005-09-09 12:10:10 -0700118 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700119 ndev->stats.rx_packets++;
120 ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
Matt Porterf89efd52005-09-09 12:10:10 -0700121 }
122
123 } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
124
125 return i;
126}
127
128static void rionet_rx_fill(struct net_device *ndev, int end)
129{
130 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800131 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700132
133 i = rnet->rx_slot;
134 do {
135 rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
136
137 if (!rnet->rx_skb[i])
138 break;
139
140 rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
141 rnet->rx_skb[i]->data);
142 } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
143
144 rnet->rx_slot = i;
145}
146
147static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
148 struct rio_dev *rdev)
149{
Wang Chen4cf16532008-11-12 23:38:14 -0800150 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700151
152 rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
153 rnet->tx_skb[rnet->tx_slot] = skb;
154
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700155 ndev->stats.tx_packets++;
156 ndev->stats.tx_bytes += skb->len;
Matt Porterf89efd52005-09-09 12:10:10 -0700157
158 if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
159 netif_stop_queue(ndev);
160
161 ++rnet->tx_slot;
162 rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
163
164 if (netif_msg_tx_queued(rnet))
David S. Miller8df8a472011-05-22 20:35:54 -0400165 printk(KERN_INFO "%s: queued skb len %8.8x\n", DRV_NAME,
166 skb->len);
Matt Porterf89efd52005-09-09 12:10:10 -0700167
168 return 0;
169}
170
171static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
172{
173 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800174 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700175 struct ethhdr *eth = (struct ethhdr *)skb->data;
176 u16 destid;
177 unsigned long flags;
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700178 int add_num = 1;
Matt Porterf89efd52005-09-09 12:10:10 -0700179
180 local_irq_save(flags);
181 if (!spin_trylock(&rnet->tx_lock)) {
182 local_irq_restore(flags);
183 return NETDEV_TX_LOCKED;
184 }
185
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700186 if (is_multicast_ether_addr(eth->h_dest))
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700187 add_num = nets[rnet->mport->id].nact;
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700188
189 if ((rnet->tx_cnt + add_num) > RIONET_TX_RING_SIZE) {
Matt Porterf89efd52005-09-09 12:10:10 -0700190 netif_stop_queue(ndev);
191 spin_unlock_irqrestore(&rnet->tx_lock, flags);
192 printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
193 ndev->name);
194 return NETDEV_TX_BUSY;
195 }
196
Tobias Klauserabfc89c2011-07-03 23:54:54 +0000197 if (is_multicast_ether_addr(eth->h_dest)) {
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700198 int count = 0;
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700199
Zhang Weie0423232008-04-18 13:33:42 -0700200 for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
201 i++)
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700202 if (nets[rnet->mport->id].active[i]) {
Matt Porterf89efd52005-09-09 12:10:10 -0700203 rionet_queue_tx_msg(skb, ndev,
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700204 nets[rnet->mport->id].active[i]);
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700205 if (count)
206 atomic_inc(&skb->users);
207 count++;
208 }
Matt Porterf89efd52005-09-09 12:10:10 -0700209 } else if (RIONET_MAC_MATCH(eth->h_dest)) {
210 destid = RIONET_GET_DESTID(eth->h_dest);
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700211 if (nets[rnet->mport->id].active[destid])
212 rionet_queue_tx_msg(skb, ndev,
213 nets[rnet->mport->id].active[destid]);
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700214 else {
215 /*
216 * If the target device was removed from the list of
217 * active peers but we still have TX packets targeting
218 * it just report sending a packet to the target
219 * (without actual packet transfer).
220 */
221 dev_kfree_skb_any(skb);
222 ndev->stats.tx_packets++;
223 ndev->stats.tx_bytes += skb->len;
224 }
Matt Porterf89efd52005-09-09 12:10:10 -0700225 }
226
227 spin_unlock_irqrestore(&rnet->tx_lock, flags);
228
Patrick McHardy6ed10652009-06-23 06:03:08 +0000229 return NETDEV_TX_OK;
Matt Porterf89efd52005-09-09 12:10:10 -0700230}
231
232static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
233 u16 info)
234{
235 struct net_device *ndev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800236 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700237 struct rionet_peer *peer;
238
239 if (netif_msg_intr(rnet))
240 printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
241 DRV_NAME, sid, tid, info);
242 if (info == RIONET_DOORBELL_JOIN) {
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700243 if (!nets[rnet->mport->id].active[sid]) {
244 list_for_each_entry(peer,
245 &nets[rnet->mport->id].peers, node) {
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700246 if (peer->rdev->destid == sid) {
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700247 nets[rnet->mport->id].active[sid] =
248 peer->rdev;
249 nets[rnet->mport->id].nact++;
Alexandre Bounine7c4a6102012-10-04 17:15:48 -0700250 }
Matt Porterf89efd52005-09-09 12:10:10 -0700251 }
252 rio_mport_send_doorbell(mport, sid,
253 RIONET_DOORBELL_JOIN);
254 }
255 } else if (info == RIONET_DOORBELL_LEAVE) {
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700256 nets[rnet->mport->id].active[sid] = NULL;
257 nets[rnet->mport->id].nact--;
Matt Porterf89efd52005-09-09 12:10:10 -0700258 } else {
259 if (netif_msg_intr(rnet))
260 printk(KERN_WARNING "%s: unhandled doorbell\n",
261 DRV_NAME);
262 }
263}
264
265static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
266{
267 int n;
268 struct net_device *ndev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800269 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700270
271 if (netif_msg_intr(rnet))
272 printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
273 DRV_NAME, mbox, slot);
274
275 spin_lock(&rnet->lock);
276 if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
277 rionet_rx_fill(ndev, n);
278 spin_unlock(&rnet->lock);
279}
280
281static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
282{
283 struct net_device *ndev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800284 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700285
Aurelien Jacquiot36915972016-03-22 14:25:42 -0700286 spin_lock(&rnet->tx_lock);
Matt Porterf89efd52005-09-09 12:10:10 -0700287
288 if (netif_msg_intr(rnet))
289 printk(KERN_INFO
290 "%s: outbound message event, mbox %d slot %d\n",
291 DRV_NAME, mbox, slot);
292
293 while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
294 /* dma unmap single */
295 dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
296 rnet->tx_skb[rnet->ack_slot] = NULL;
297 ++rnet->ack_slot;
298 rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
299 rnet->tx_cnt--;
300 }
301
302 if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
303 netif_wake_queue(ndev);
304
Aurelien Jacquiot36915972016-03-22 14:25:42 -0700305 spin_unlock(&rnet->tx_lock);
Matt Porterf89efd52005-09-09 12:10:10 -0700306}
307
308static int rionet_open(struct net_device *ndev)
309{
310 int i, rc = 0;
311 struct rionet_peer *peer, *tmp;
Wang Chen4cf16532008-11-12 23:38:14 -0800312 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700313
314 if (netif_msg_ifup(rnet))
315 printk(KERN_INFO "%s: open\n", DRV_NAME);
316
317 if ((rc = rio_request_inb_dbell(rnet->mport,
318 (void *)ndev,
319 RIONET_DOORBELL_JOIN,
320 RIONET_DOORBELL_LEAVE,
321 rionet_dbell_event)) < 0)
322 goto out;
323
324 if ((rc = rio_request_inb_mbox(rnet->mport,
325 (void *)ndev,
326 RIONET_MAILBOX,
327 RIONET_RX_RING_SIZE,
328 rionet_inb_msg_event)) < 0)
329 goto out;
330
331 if ((rc = rio_request_outb_mbox(rnet->mport,
332 (void *)ndev,
333 RIONET_MAILBOX,
334 RIONET_TX_RING_SIZE,
335 rionet_outb_msg_event)) < 0)
336 goto out;
337
338 /* Initialize inbound message ring */
339 for (i = 0; i < RIONET_RX_RING_SIZE; i++)
340 rnet->rx_skb[i] = NULL;
341 rnet->rx_slot = 0;
342 rionet_rx_fill(ndev, 0);
343
344 rnet->tx_slot = 0;
345 rnet->tx_cnt = 0;
346 rnet->ack_slot = 0;
347
348 netif_carrier_on(ndev);
349 netif_start_queue(ndev);
350
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700351 list_for_each_entry_safe(peer, tmp,
352 &nets[rnet->mport->id].peers, node) {
Matt Porterf89efd52005-09-09 12:10:10 -0700353 if (!(peer->res = rio_request_outb_dbell(peer->rdev,
354 RIONET_DOORBELL_JOIN,
355 RIONET_DOORBELL_LEAVE)))
356 {
357 printk(KERN_ERR "%s: error requesting doorbells\n",
358 DRV_NAME);
359 continue;
360 }
361
Alexandre Bounine284fb682011-08-25 15:59:13 -0700362 /* Send a join message */
363 rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
Matt Porterf89efd52005-09-09 12:10:10 -0700364 }
365
366 out:
367 return rc;
368}
369
370static int rionet_close(struct net_device *ndev)
371{
Wang Chen4cf16532008-11-12 23:38:14 -0800372 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700373 struct rionet_peer *peer, *tmp;
374 int i;
375
376 if (netif_msg_ifup(rnet))
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700377 printk(KERN_INFO "%s: close %s\n", DRV_NAME, ndev->name);
Matt Porterf89efd52005-09-09 12:10:10 -0700378
379 netif_stop_queue(ndev);
380 netif_carrier_off(ndev);
381
382 for (i = 0; i < RIONET_RX_RING_SIZE; i++)
Wei Yongjunaaff1e12009-02-25 00:18:12 +0000383 kfree_skb(rnet->rx_skb[i]);
Matt Porterf89efd52005-09-09 12:10:10 -0700384
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700385 list_for_each_entry_safe(peer, tmp,
386 &nets[rnet->mport->id].peers, node) {
387 if (nets[rnet->mport->id].active[peer->rdev->destid]) {
Matt Porterf89efd52005-09-09 12:10:10 -0700388 rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700389 nets[rnet->mport->id].active[peer->rdev->destid] = NULL;
Matt Porterf89efd52005-09-09 12:10:10 -0700390 }
391 rio_release_outb_dbell(peer->rdev, peer->res);
392 }
393
394 rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
395 RIONET_DOORBELL_LEAVE);
396 rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
397 rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
398
399 return 0;
400}
401
Viresh Kumar71db87b2015-07-30 15:04:01 +0530402static void rionet_remove_dev(struct device *dev, struct subsys_interface *sif)
Matt Porterf89efd52005-09-09 12:10:10 -0700403{
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700404 struct rio_dev *rdev = to_rio_dev(dev);
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700405 unsigned char netid = rdev->net->hport->id;
Matt Porterf89efd52005-09-09 12:10:10 -0700406 struct rionet_peer *peer, *tmp;
407
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700408 if (dev_rionet_capable(rdev)) {
409 list_for_each_entry_safe(peer, tmp, &nets[netid].peers, node) {
410 if (peer->rdev == rdev) {
411 if (nets[netid].active[rdev->destid]) {
412 nets[netid].active[rdev->destid] = NULL;
413 nets[netid].nact--;
414 }
Matt Porterf89efd52005-09-09 12:10:10 -0700415
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700416 list_del(&peer->node);
417 kfree(peer);
418 break;
419 }
420 }
Matt Porterf89efd52005-09-09 12:10:10 -0700421 }
422}
423
424static void rionet_get_drvinfo(struct net_device *ndev,
425 struct ethtool_drvinfo *info)
426{
Wang Chen4cf16532008-11-12 23:38:14 -0800427 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700428
Jiri Pirko7826d432013-01-06 00:44:26 +0000429 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
430 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
431 strlcpy(info->fw_version, "n/a", sizeof(info->fw_version));
432 strlcpy(info->bus_info, rnet->mport->name, sizeof(info->bus_info));
Matt Porterf89efd52005-09-09 12:10:10 -0700433}
434
435static u32 rionet_get_msglevel(struct net_device *ndev)
436{
Wang Chen4cf16532008-11-12 23:38:14 -0800437 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700438
439 return rnet->msg_enable;
440}
441
442static void rionet_set_msglevel(struct net_device *ndev, u32 value)
443{
Wang Chen4cf16532008-11-12 23:38:14 -0800444 struct rionet_private *rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700445
446 rnet->msg_enable = value;
447}
448
Aurelien Jacquiot92444bb2016-03-22 14:25:45 -0700449static int rionet_change_mtu(struct net_device *ndev, int new_mtu)
450{
451 if ((new_mtu < 68) || (new_mtu > RIONET_MAX_MTU)) {
452 printk(KERN_ERR "%s: Invalid MTU size %d\n",
453 ndev->name, new_mtu);
454 return -EINVAL;
455 }
456 ndev->mtu = new_mtu;
457 return 0;
458}
459
Jeff Garzik7282d492006-09-13 14:30:00 -0400460static const struct ethtool_ops rionet_ethtool_ops = {
Matt Porterf89efd52005-09-09 12:10:10 -0700461 .get_drvinfo = rionet_get_drvinfo,
462 .get_msglevel = rionet_get_msglevel,
463 .set_msglevel = rionet_set_msglevel,
464 .get_link = ethtool_op_get_link,
465};
466
Alexander Beregalova33a2bb2009-04-15 12:52:56 +0000467static const struct net_device_ops rionet_netdev_ops = {
468 .ndo_open = rionet_open,
469 .ndo_stop = rionet_close,
470 .ndo_start_xmit = rionet_start_xmit,
Aurelien Jacquiot92444bb2016-03-22 14:25:45 -0700471 .ndo_change_mtu = rionet_change_mtu,
Alexander Beregalova33a2bb2009-04-15 12:52:56 +0000472 .ndo_validate_addr = eth_validate_addr,
473 .ndo_set_mac_address = eth_mac_addr,
474};
475
Yinglin Luan55caa922011-06-25 18:12:12 +0000476static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
Matt Porterf89efd52005-09-09 12:10:10 -0700477{
478 int rc = 0;
Matt Porterf89efd52005-09-09 12:10:10 -0700479 struct rionet_private *rnet;
480 u16 device_id;
Akinobu Mitaacc65632012-03-30 01:01:46 +0000481 const size_t rionet_active_bytes = sizeof(void *) *
482 RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
Matt Porterf89efd52005-09-09 12:10:10 -0700483
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700484 nets[mport->id].active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
485 get_order(rionet_active_bytes));
486 if (!nets[mport->id].active) {
Zhang Weie0423232008-04-18 13:33:42 -0700487 rc = -ENOMEM;
488 goto out;
489 }
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700490 memset((void *)nets[mport->id].active, 0, rionet_active_bytes);
Zhang Weie0423232008-04-18 13:33:42 -0700491
Matt Porterf89efd52005-09-09 12:10:10 -0700492 /* Set up private area */
Wang Chen4cf16532008-11-12 23:38:14 -0800493 rnet = netdev_priv(ndev);
Matt Porterf89efd52005-09-09 12:10:10 -0700494 rnet->mport = mport;
495
496 /* Set the default MAC address */
497 device_id = rio_local_get_device_id(mport);
498 ndev->dev_addr[0] = 0x00;
499 ndev->dev_addr[1] = 0x01;
500 ndev->dev_addr[2] = 0x00;
501 ndev->dev_addr[3] = 0x01;
502 ndev->dev_addr[4] = device_id >> 8;
503 ndev->dev_addr[5] = device_id & 0xff;
504
Alexander Beregalova33a2bb2009-04-15 12:52:56 +0000505 ndev->netdev_ops = &rionet_netdev_ops;
Aurelien Jacquiot92444bb2016-03-22 14:25:45 -0700506 ndev->mtu = RIONET_MAX_MTU;
Matt Porterf89efd52005-09-09 12:10:10 -0700507 ndev->features = NETIF_F_LLTX;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -0700508 SET_NETDEV_DEV(ndev, &mport->dev);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000509 ndev->ethtool_ops = &rionet_ethtool_ops;
Matt Porterf89efd52005-09-09 12:10:10 -0700510
Matt Porterf89efd52005-09-09 12:10:10 -0700511 spin_lock_init(&rnet->lock);
512 spin_lock_init(&rnet->tx_lock);
513
514 rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
515
516 rc = register_netdev(ndev);
517 if (rc != 0)
518 goto out;
519
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700520 printk(KERN_INFO "%s: %s %s Version %s, MAC %pM, %s\n",
Matt Porterf89efd52005-09-09 12:10:10 -0700521 ndev->name,
522 DRV_NAME,
523 DRV_DESC,
524 DRV_VERSION,
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700525 ndev->dev_addr,
526 mport->name);
Matt Porterf89efd52005-09-09 12:10:10 -0700527
528 out:
529 return rc;
530}
531
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700532static unsigned long net_table[RIONET_MAX_NETS/sizeof(unsigned long) + 1];
533
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700534static int rionet_add_dev(struct device *dev, struct subsys_interface *sif)
Matt Porterf89efd52005-09-09 12:10:10 -0700535{
536 int rc = -ENODEV;
Alexandre Bounine284fb682011-08-25 15:59:13 -0700537 u32 lsrc_ops, ldst_ops;
Matt Porterf89efd52005-09-09 12:10:10 -0700538 struct rionet_peer *peer;
Yinglin Luan55caa922011-06-25 18:12:12 +0000539 struct net_device *ndev = NULL;
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700540 struct rio_dev *rdev = to_rio_dev(dev);
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700541 unsigned char netid = rdev->net->hport->id;
542 int oldnet;
Matt Porterf89efd52005-09-09 12:10:10 -0700543
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700544 if (netid >= RIONET_MAX_NETS)
545 return rc;
Matt Porterf89efd52005-09-09 12:10:10 -0700546
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700547 oldnet = test_and_set_bit(netid, net_table);
Yinglin Luan55caa922011-06-25 18:12:12 +0000548
Matt Porterf89efd52005-09-09 12:10:10 -0700549 /*
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700550 * If first time through this net, make sure local device is rionet
551 * capable and setup netdev (this step will be skipped in later probes
552 * on the same net).
Matt Porterf89efd52005-09-09 12:10:10 -0700553 */
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700554 if (!oldnet) {
Matt Porterf89efd52005-09-09 12:10:10 -0700555 rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
556 &lsrc_ops);
557 rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
558 &ldst_ops);
Alexandre Bounine284fb682011-08-25 15:59:13 -0700559 if (!is_rionet_capable(lsrc_ops, ldst_ops)) {
Matt Porterf89efd52005-09-09 12:10:10 -0700560 printk(KERN_ERR
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700561 "%s: local device %s is not network capable\n",
562 DRV_NAME, rdev->net->hport->name);
Matt Porterf89efd52005-09-09 12:10:10 -0700563 goto out;
564 }
565
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700566 /* Allocate our net_device structure */
567 ndev = alloc_etherdev(sizeof(struct rionet_private));
568 if (ndev == NULL) {
569 rc = -ENOMEM;
570 goto out;
571 }
572 nets[netid].ndev = ndev;
Yinglin Luan55caa922011-06-25 18:12:12 +0000573 rc = rionet_setup_netdev(rdev->net->hport, ndev);
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700574 if (rc) {
575 printk(KERN_ERR "%s: failed to setup netdev (rc=%d)\n",
576 DRV_NAME, rc);
577 goto out;
578 }
579
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700580 INIT_LIST_HEAD(&nets[netid].peers);
581 nets[netid].nact = 0;
582 } else if (nets[netid].ndev == NULL)
583 goto out;
Matt Porterf89efd52005-09-09 12:10:10 -0700584
585 /*
586 * If the remote device has mailbox/doorbell capabilities,
587 * add it to the peer list.
588 */
589 if (dev_rionet_capable(rdev)) {
590 if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
591 rc = -ENOMEM;
592 goto out;
593 }
594 peer->rdev = rdev;
Alexandre Bounine2fb717e2012-10-04 17:16:11 -0700595 list_add_tail(&peer->node, &nets[netid].peers);
Matt Porterf89efd52005-09-09 12:10:10 -0700596 }
597
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700598 return 0;
599out:
Matt Porterf89efd52005-09-09 12:10:10 -0700600 return rc;
601}
602
Alexandre Bouninef41e2472016-03-22 14:26:11 -0700603static int rionet_shutdown(struct notifier_block *nb, unsigned long code,
604 void *unused)
605{
606 struct rionet_peer *peer, *tmp;
607 int i;
608
609 pr_debug("%s: %s\n", DRV_NAME, __func__);
610
611 for (i = 0; i < RIONET_MAX_NETS; i++) {
612 if (!nets[i].ndev)
613 continue;
614
615 list_for_each_entry_safe(peer, tmp, &nets[i].peers, node) {
616 if (nets[i].active[peer->rdev->destid]) {
617 rio_send_doorbell(peer->rdev,
618 RIONET_DOORBELL_LEAVE);
619 nets[i].active[peer->rdev->destid] = NULL;
620 }
621 }
622 }
623
624 return NOTIFY_DONE;
625}
626
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700627#ifdef MODULE
Matt Porterf89efd52005-09-09 12:10:10 -0700628static struct rio_device_id rionet_id_table[] = {
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700629 {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)},
630 { 0, } /* terminate list */
Matt Porterf89efd52005-09-09 12:10:10 -0700631};
632
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700633MODULE_DEVICE_TABLE(rapidio, rionet_id_table);
634#endif
635
636static struct subsys_interface rionet_interface = {
637 .name = "rionet",
638 .subsys = &rio_bus_type,
639 .add_dev = rionet_add_dev,
640 .remove_dev = rionet_remove_dev,
Matt Porterf89efd52005-09-09 12:10:10 -0700641};
642
Alexandre Bouninef41e2472016-03-22 14:26:11 -0700643static struct notifier_block rionet_notifier = {
644 .notifier_call = rionet_shutdown,
645};
646
Matt Porterf89efd52005-09-09 12:10:10 -0700647static int __init rionet_init(void)
648{
Alexandre Bouninef41e2472016-03-22 14:26:11 -0700649 int ret;
650
651 ret = register_reboot_notifier(&rionet_notifier);
652 if (ret) {
653 pr_err("%s: failed to register reboot notifier (err=%d)\n",
654 DRV_NAME, ret);
655 return ret;
656 }
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700657 return subsys_interface_register(&rionet_interface);
Matt Porterf89efd52005-09-09 12:10:10 -0700658}
659
660static void __exit rionet_exit(void)
661{
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700662 struct rionet_private *rnet;
663 struct net_device *ndev;
664 struct rionet_peer *peer, *tmp;
665 int i;
666
667 for (i = 0; i < RIONET_MAX_NETS; i++) {
668 if (nets[i].ndev != NULL) {
669 ndev = nets[i].ndev;
670 rnet = netdev_priv(ndev);
671 unregister_netdev(ndev);
672
673 list_for_each_entry_safe(peer,
674 tmp, &nets[i].peers, node) {
675 list_del(&peer->node);
676 kfree(peer);
677 }
678
679 free_pages((unsigned long)nets[i].active,
680 get_order(sizeof(void *) *
681 RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size)));
682 nets[i].active = NULL;
683
684 free_netdev(ndev);
685 }
686 }
687
Alexandre Bouninef41e2472016-03-22 14:26:11 -0700688 unregister_reboot_notifier(&rionet_notifier);
Alexandre Bouninee6161d62013-07-03 15:08:52 -0700689 subsys_interface_unregister(&rionet_interface);
Matt Porterf89efd52005-09-09 12:10:10 -0700690}
691
Alexandre Bounine2f809982011-03-23 16:43:04 -0700692late_initcall(rionet_init);
Matt Porterf89efd52005-09-09 12:10:10 -0700693module_exit(rionet_exit);