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