# HG changeset patch
# Parent 5a53813ed1306484aa98dd95fe1d284ed24111ab
Replace Linux specific code in Socket by DMA calls

diff --git a/rathaxes/samples/e1000/dma.blt b/rathaxes/samples/e1000/dma.blt
--- a/rathaxes/samples/e1000/dma.blt
+++ b/rathaxes/samples/e1000/dma.blt
@@ -1,6 +1,6 @@
 with DMA, Builtin, LKM, Device
 {
-    template type   AbstractDMAHandle()
+    template type   DMA::AbstractDMAHandle()
     {
         chunk   LKM::includes()
         {
@@ -14,11 +14,11 @@
 
         map
         {
-            k_dma_handle: ((dma_addr_t *)${self});
+            k_dma_handle: ((dma_addr_t)${self});
         }
     }
 
-    template type   AbstractDMADirection()
+    template type   DMA::AbstractDMADirection()
     {
         decl    data_types()
         {
@@ -42,7 +42,7 @@
 
         map
         {
-            /* XXX: we should use ${AbstractDMADirection} here: */
+            /* XXX: we should use ${DMA::AbstractDMADirection} here: */
             dma_direction: ((enum dma_data_direction)${self});
         }
     }
@@ -51,11 +51,11 @@
     {
         chunk  ::CALL()
         {
-            dma_map_single(${dev.k_device}, ${buf}, ${size}, ${dir.dma_direction});
+            ((${DMA::DMA::AbstractDMAHandle})dma_map_single(${dev.k_device}, ${buf}, ${size}, ${dir.dma_direction}));
         }
     }
 
-    template sequence   unmap(Device::AbstractDevice dev, AbstractDMAHandle handle, Builtin::number size, DMADirection dir)
+    template sequence   unmap(Device::AbstractDevice dev, DMA::AbstractDMAHandle handle, Builtin::number size, DMADirection dir)
     {
         chunk  ::CALL()
         {
@@ -63,15 +63,23 @@
         }
     }
 
-    template sequence   alloc_coherent(Device::AbstractDevice dev, Builtin::number size, AbstractDMAHandle handle)
+    template sequence   mapping_error(Device::AbstractDevice dev, DMA::AbstractDMAHandle handle)
+    {
+        chunk ::CALL()
+        {
+            dma_mapping_error(${dev.k_device}, ${handle.k_dma_handle});
+        }
+    }
+
+    template sequence   alloc_coherent(Device::AbstractDevice dev, Builtin::number size, DMA::AbstractDMAHandle handle)
     {
         chunk  ::CALL()
         {
-            dma_alloc_coherent(${dev.k_device}, ${size}, ${handle.k_dma_handle}, GFP_KERNEL);
+            dma_alloc_coherent(${dev.k_device}, ${size}, &${handle.k_dma_handle}, GFP_KERNEL);
         }
     }
 
-    template sequence   free_coherent(Device::AbstractDevice dev, Builtin::number size, Builtin::symbol addr, AbstractDMAHandle handle)
+    template sequence   free_coherent(Device::AbstractDevice dev, Builtin::number size, Builtin::symbol addr, DMA::AbstractDMAHandle handle)
     {
         chunk  ::CALL()
         {
diff --git a/rathaxes/samples/e1000/dma.rti b/rathaxes/samples/e1000/dma.rti
--- a/rathaxes/samples/e1000/dma.rti
+++ b/rathaxes/samples/e1000/dma.rti
@@ -29,6 +29,12 @@
         provided chunk  ::CALL();
     }
 
+    /* XXX: Until we have a real error handling mechanism: */
+    provided sequence   mapping_error(Device::AbstractDevice, AbstractDMAHandle)
+    {
+        provided chunk  ::CALL(); /* -> != 0 if the mapping failed */
+    }
+
     provided sequence   alloc_coherent(Device::AbstractDevice, Builtin::number, AbstractDMAHandle)
     {
         /* return the addr and the handle via the AbstractDMAHandle ptr: */
diff --git a/rathaxes/samples/e1000/socket.blt b/rathaxes/samples/e1000/socket.blt
--- a/rathaxes/samples/e1000/socket.blt
+++ b/rathaxes/samples/e1000/socket.blt
@@ -1,4 +1,4 @@
-with Socket, LKM, Device, Ethernet
+with Socket, LKM, Device, DMA, Ethernet
 {
     template type Socket::AbstractSKBuff()
     {
@@ -22,15 +22,15 @@
     {
         decl  data_types()
         {
-            ${Socket::AbstractSKBuff.ref}   skbuff;
-            dma_addr_t                      dma_handle;
+            ${Socket::AbstractSKBuff.ref}       skbuff;
+            ${DMA::AbstractDMAHandle.scalar}    dma_handle;
         }
 
         chunk   LKM::prototypes()
         {
             static void rtx_socket_skbuff_dump_infos(${Socket::SKBuff.ref});
-            static int  rtx_socket_skbuff_map(${Socket::SKBuff.ref}, struct device *, enum dma_data_direction);
-            static void rtx_socket_skbuff_unmap_and_free(${Socket::SKBuff.ref}, struct device *, enum dma_data_direction);
+            static int  rtx_socket_skbuff_map(${Socket::SKBuff.ref}, ${Device::AbstractDevice.ref}, ${DMA::DMADirection.scalar});
+            static void rtx_socket_skbuff_unmap_and_free(${Socket::SKBuff.ref}, struct device *, ${DMA::DMADirection.scalar});
         }
 
         chunk   LKM::code()
@@ -65,23 +65,21 @@
             }
 
             static int rtx_socket_skbuff_map(${Socket::SKBuff.ref} self,
-                                             struct device *dev,
-                                             enum dma_data_direction direction)
+                                             ${Device::AbstractDevice.ref} dev,
+                                             ${DMA::DMADirection.scalar} direction)
             {
-                ${Socket::AbstractSKBuff.ref}   skb = self->skbuff;
-
-                WARN_ON(!${local.skb.k_sk_buff});
-                WARN_ON(self->dma_handle);
+                WARN_ON(!${local.self.k_sk_buff});
+                WARN_ON(${local.self.k_dma_handle});
 
                 self->dma_handle = dma_map_single(
-                        dev,
+                        ${dev.k_device},
                         ${local.skb.k_sk_buff},
                         skb_headlen(${local.skb.k_sk_buff}),
-                        direction);
-                int err = dma_mapping_error(dev, self->dma_handle);
+                        ${local.direction.dma_direction});
+                int err = ${DMA::mapping_error(local.dev, local.self.k_dma_handle)};
                 if (err)
                 {
-                    self->dma_handle = 0;
+                    ${local.self.k_dma_handle} = 0;
                     return err;
                 }
                 return 0;
@@ -89,7 +87,7 @@
 
             static void rtx_socket_skbuff_unmap_and_free(${Socket::SKBuff.ref} self,
                                                          struct device *dev,
-                                                         enum dma_data_direction direction)
+                                                         ${DMA::DMADirection} direction)
             {
                 ${Socket::AbstractSKBuff.ref}   skb = self->skbuff;
 
@@ -100,7 +98,7 @@
                     dma_unmap_single(dev,
                             self->dma_handle,
                             skb_headlen(${local.skb.k_sk_buff}),
-                            direction);
+                            ${local.direction.dma_direction});
                     self->dma_handle = 0;
                 }
                 dev_kfree_skb_any(${local.skb.k_sk_buff});
@@ -126,30 +124,32 @@
 
         method   map_to(Device::AbstractDevice dev)
         {
-            rtx_socket_skbuff_map(${self}, ${dev.k_device}, DMA_TO_DEVICE);
+            rtx_socket_skbuff_map(${self}, ${dev}, RTX_DMA_TO_DEVICE);
         }
 
         method   map_from(Device::AbstractDevice dev)
         {
-            rtx_socket_skbuff_map(${self}, ${dev.k_device}, DMA_FROM_DEVICE);
+            rtx_socket_skbuff_map(${self}, ${dev.k_device}, RTX_DMA_FROM_DEVICE);
         }
 
         method   unmap_to_and_free(Device::AbstractDevice dev)
         {
-            rtx_socket_skbuff_unmap_and_free(${self}, ${dev.k_device}, DMA_TO_DEVICE);
+            rtx_socket_skbuff_unmap_and_free(${self}, ${dev.k_device}, RTX_DMA_TO_DEVICE);
         }
 
         method   unmap_from_and_free(Device::AbstractDevice dev)
         {
-            rtx_socket_skbuff_unmap_and_free(${self}, ${dev.k_device}, DMA_FROM_DEVICE);
+            rtx_socket_skbuff_unmap_and_free(${self}, ${dev.k_device}, RTX_DMA_FROM_DEVICE);
         }
 
         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 management
-            // can be abstracted from the user.
+            // 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
+            // management can be abstracted from the user. But this is at least
+            // useful for internal use:
+            k_sk_buff: ((struct skbuff *)(${self}->skbuff));
+            k_dma_handle: ((dma_addr_t)(${self}->dma_handle));
         }
     }
 }
diff --git a/rathaxes/samples/e1000/socket.rti b/rathaxes/samples/e1000/socket.rti
--- a/rathaxes/samples/e1000/socket.rti
+++ b/rathaxes/samples/e1000/socket.rti
@@ -1,4 +1,4 @@
-interface Socket : LKM, Device
+interface Socket : LKM, Device, DMA
 {
     /* The SKBuff type from the kernel */
     provided type   AbstractSKBuff
@@ -19,5 +19,8 @@
         method      map_from(Device::AbstractDevice);
         method      unmap_to_and_free(Device::AbstractDevice);
         method      unmap_from_and_free(Device::AbstractDevice);
+
+        attribute   Socket::AbstractSKBuff.ref      k_sk_buff;
+        attribute   DMA::AbstractDMAHandle.scalar   k_dma_handle;
     }
 }
