# HG changeset patch
# Parent 606e807b8bc7a3a7ea6f1b895bfe909cd7875c2e
rathaxes: add sk_buff abstraction and add the implementation of the xmit function for the ethernet system. We have a fully (empty) functionnal ethernet driver

diff -r 606e807b8bc7 rathaxes/samples/lkm/CMakeLists.txt
--- a/rathaxes/samples/lkm/CMakeLists.txt	Sat Jan 07 20:02:55 2012 +0100
+++ b/rathaxes/samples/lkm/CMakeLists.txt	Sat Jan 07 20:48:23 2012 +0100
@@ -1,6 +1,6 @@
 ADD_RATHAXES_SOURCES(lkm lkm.rtx
-                     RTI log.rti lkm.rti pci.rti ethernet.rti
-                     BLT log.blt lkm.blt pci.blt ethernet.blt)
+                     RTI log.rti lkm.rti pci.rti socket.rti ethernet.rti e1000.rti
+                     BLT log.blt lkm.blt pci.blt socket.blt ethernet.blt e1000.blt)
 
 # We can't name lkm since it's already used as the target name to generate the
 # source (with ADD_RATHAXES_SOURCES).
diff -r 606e807b8bc7 rathaxes/samples/lkm/e1000.blt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rathaxes/samples/lkm/e1000.blt	Sat Jan 07 20:48:23 2012 +0100
@@ -0,0 +1,72 @@
+with e1000, Ethernet, Socket, PCI, LKM, Log
+{
+    template type   e1000::Context()
+    {
+        chunk   LKM::includes()
+        {
+            /*
+             * Force the generation of the structure in the "headers part, we
+             * have to do this since we do not use the structure in this blt
+             * (we hacked a bit and used it in ethernet.blt directly).
+             */
+            typedef int ${e1000::Context}; /* CNorm __std__ workaround */
+            ${e1000::Context} force_declaration_in_includes;
+        }
+
+        chunk   ::decl()
+        {
+            struct rtx_e1000_ctx
+            {
+                int                             bars;
+                unsigned char /* __iomem */     *ioaddr;
+            };
+        }
+
+        map
+        {
+        }
+    }
+
+    template sequence   e1000::create()
+    {
+        chunk ::CALL
+        {
+            rtx_ether_ctx->hw_ctx.bars = pci_select_bars(pdev, IORESOURCE_MEM);
+            if (pci_enable_device_mem(pdev))
+            {
+                ${Log::info("e1000::create: pci_enable_device_mem failed")};
+            }
+
+            if (pci_request_selected_regions(pdev, rtx_ether_ctx->hw_ctx.bars, ${config.name}))
+            {
+                ${Log::info("e1000::create: pci_request_selected_regions failed")};
+            }
+
+            if (${config.set_master})
+            {
+                pci_set_master(pdev);
+            }
+
+            /* 0 here is for BAR_0: */
+            rtx_ether_ctx->hw_ctx.ioaddr = pci_ioremap_bar(pdev, 0);
+            if (!rtx_ether_ctx->hw_ctx.ioaddr)
+            {
+                ${Log::info("e1000::create: pci_ioremap_bar failed")};
+            }
+        }
+    }
+
+    template sequence   e1000::destroy()
+    {
+        chunk ::CALL
+        {
+            // XXX: add a check in order to avoid freeing none allocated
+            // resources.
+            struct net_device *net_dev = pci_get_drvdata(pdev);
+            struct rtx_ethernet_dev* rtx_ether_ctx = netdev_priv(net_dev);
+            iounmap(rtx_ether_ctx->hw_ctx.ioaddr);
+            pci_release_selected_regions(pdev, rtx_ether_ctx->hw_ctx.bars);
+            pci_release_region(pdev, 0);
+        }
+    }
+}
diff -r 606e807b8bc7 rathaxes/samples/lkm/e1000.rti
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rathaxes/samples/lkm/e1000.rti	Sat Jan 07 20:48:23 2012 +0100
@@ -0,0 +1,15 @@
+interface e1000 : Socket, Ethernet, PCI, LKM
+{
+    provided type   e1000::Context;
+
+    /* Not sure if we need the argument */
+    provided sequence   e1000::create()
+    {
+        provided chunk  ::CALL;
+    }
+
+    provided sequence   e1000::destroy()
+    {
+        provided chunk  ::CALL;
+    }
+}
diff -r 606e807b8bc7 rathaxes/samples/lkm/ethernet.blt
--- a/rathaxes/samples/lkm/ethernet.blt	Sat Jan 07 20:02:55 2012 +0100
+++ b/rathaxes/samples/lkm/ethernet.blt	Sat Jan 07 20:48:23 2012 +0100
@@ -18,8 +18,11 @@
                  * I think it's useless to use the ${PCI::Device} "abstraction"
                  * here, since we are already in a Linux specific context here.
                  */
-                struct pci_dev      *pci_dev;
-                struct net_device   *net_dev;
+                struct pci_dev          *pci_dev;
+                struct net_device       *net_dev;
+
+                /* while waiting on issue #8 */
+                struct rtx_e1000_ctx    hw_ctx;
             };
         }
 
@@ -51,6 +54,24 @@
         }
     }
 
+    template sequence   Ethernet::send(Ethernet::Device dev, Socket::SKBuff skb)
+    {
+        chunk LKM::prototypes()
+        {
+            static int  rtx_ethernet_xmit(struct sk_buff* skb, struct net_device *dev);
+        }
+
+        chunk LKM::code()
+        {
+            static int  rtx_ethernet_xmit(struct sk_buff* skb, struct net_device *dev)
+            {
+                ${pointcut ::IMPLEMENTATION};
+
+                return 0;
+            }
+        }
+    }
+
     template sequence   Ethernet::close(Ethernet::Device dev)
     {
         chunk LKM::prototypes()
@@ -100,7 +121,7 @@
             {
                 .ndo_open = rtx_ethernet_open,
                 .ndo_stop = rtx_ethernet_close,
-                .ndo_start_xmit = NULL,
+                .ndo_start_xmit = rtx_ethernet_xmit,
             };
         }
 
@@ -159,9 +180,6 @@
     {
         chunk ::CALL
         {
-            struct net_device   *net_dev;
-
-            net_dev = pci_get_drvdata(pdev); // should be ${pdev}, see above
             unregister_netdev(net_dev);
             /*
              * If we had some cleanup todo with struct rtx_ether_ctx we would
diff -r 606e807b8bc7 rathaxes/samples/lkm/ethernet.rti
--- a/rathaxes/samples/lkm/ethernet.rti	Sat Jan 07 20:02:55 2012 +0100
+++ b/rathaxes/samples/lkm/ethernet.rti	Sat Jan 07 20:48:23 2012 +0100
@@ -1,4 +1,4 @@
-interface Ethernet : PCI, LKM
+interface Ethernet : Socket, PCI, LKM
 {
     provided type       Ethernet::Device;
 
@@ -10,6 +10,12 @@
         provided chunk  LKM::code;
     }
 
+    required sequence   Ethernet::send(Ethernet::Device dev, Socket::SKBuff skb)
+    {
+        provided chunk  LKM::prototypes;
+        provided chunk  LKM::code;
+    }
+
     required sequence   Ethernet::close(Ethernet::Device)
     {
         provided chunk  LKM::prototypes;
diff -r 606e807b8bc7 rathaxes/samples/lkm/lkm.rtx
--- a/rathaxes/samples/lkm/lkm.rtx	Sat Jan 07 20:02:55 2012 +0100
+++ b/rathaxes/samples/lkm/lkm.rtx	Sat Jan 07 20:48:23 2012 +0100
@@ -15,15 +15,22 @@
         Log::info("Got an interruption");
     }
 
+    Ethernet::send(Ethernet::Device dev, Socket::SKBuff skb)
+    {
+        Log::info("We have one packet to transmit!");
+    }
+
     PCI::probe(PCI::Device dev)
     {
         Log::info("Probe the device");
         Ethernet::init(dev);
+        e1000::create();
     }
 
     PCI::remove(PCI::Device dev)
     {
         Log::info("Remove the pci device");
+        e1000::destroy();
         Ethernet::exit(dev);
     }
 
@@ -45,10 +52,11 @@
     LKM::name = "hello";
     LKM::author = "Rathaxes";
     LKM::description = "Hello World Loadable Kernel Module (LKM)";
-    LKM::license = "BSD";
+    LKM::license = "GPL";
 
     PCI::vendor_id = 0x8086;
     PCI::product_id = 0x100f;
+    PCI::set_master = true;
 
     Ethernet::ifname = "rtx%d";
 }
diff -r 606e807b8bc7 rathaxes/samples/lkm/socket.blt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rathaxes/samples/lkm/socket.blt	Sat Jan 07 20:48:23 2012 +0100
@@ -0,0 +1,27 @@
+with Socket, LKM
+{
+    template type Socket::SKBuff()
+    {
+        chunk LKM::includes()
+        {
+            #include <linux/sk_buff.h>
+        }
+
+        chunk ::decl()
+        {
+            struct sk_buff;
+        }
+
+        chunk ::init()
+        {
+        }
+
+        map
+        {
+            // some work may have to be done here in order
+            // to access to some field of the sk_buff.
+            // We should determine if all the sk_buff managment
+            // can be abstracted from the user.
+        }
+    }
+}
diff -r 606e807b8bc7 rathaxes/samples/lkm/socket.rti
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rathaxes/samples/lkm/socket.rti	Sat Jan 07 20:48:23 2012 +0100
@@ -0,0 +1,4 @@
+interface Socket : LKM
+{
+    provided type Socket::SKBuff;
+}
