aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/epl/VirtualEthernetLinux.c
blob: 077724a556cc6d3436208224af389089de65389f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/****************************************************************************

  (c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29
      www.systec-electronic.com

  Project:      openPOWERLINK

  Description:  Virtual Ethernet Driver for Linux

  License:

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. Neither the name of SYSTEC electronic GmbH nor the names of its
       contributors may be used to endorse or promote products derived
       from this software without prior written permission. For written
       permission, please contact info@systec-electronic.com.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    Severability Clause:

        If a provision of this License is or becomes illegal, invalid or
        unenforceable in any jurisdiction, that shall not affect:
        1. the validity or enforceability in that jurisdiction of any other
           provision of this License; or
        2. the validity or enforceability in other jurisdictions of that or
           any other provision of this License.

  -------------------------------------------------------------------------

                $RCSfile: VirtualEthernetLinux.c,v $

                $Author: D.Krueger $

                $Revision: 1.8 $  $Date: 2008/11/20 17:06:51 $

                $State: Exp $

                Build Environment:

  -------------------------------------------------------------------------

  Revision History:

  2006/06/12 -ar:   start of the implementation, version 1.00

  2006/09/18 d.k.:  integration into EPL DLLk module

  ToDo:

  void netif_carrier_off(struct net_device *dev);
  void netif_carrier_on(struct net_device *dev);

****************************************************************************/

#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/if_arp.h>
#include <net/arp.h>

#include <net/protocol.h>
#include <net/pkt_sched.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/mm.h>
#include <linux/types.h>
#include <linux/skbuff.h>	/* for struct sk_buff */

#include "kernel/VirtualEthernet.h"
#include "kernel/EplDllkCal.h"
#include "kernel/EplDllk.h"

#if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_VETH)) != 0)

/***************************************************************************/
/*                                                                         */
/*                                                                         */
/*          G L O B A L   D E F I N I T I O N S                            */
/*                                                                         */
/*                                                                         */
/***************************************************************************/

//---------------------------------------------------------------------------
// const defines
//---------------------------------------------------------------------------

#ifndef EPL_VETH_TX_TIMEOUT
//#define EPL_VETH_TX_TIMEOUT (2*HZ)
#define EPL_VETH_TX_TIMEOUT 0	// d.k.: we use no timeout
#endif

//---------------------------------------------------------------------------
// local types
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// modul globale vars
//---------------------------------------------------------------------------

static struct net_device *pVEthNetDevice_g = NULL;

//---------------------------------------------------------------------------
// local function prototypes
//---------------------------------------------------------------------------

static int VEthOpen(struct net_device *pNetDevice_p);
static int VEthClose(struct net_device *pNetDevice_p);
static int VEthXmit(struct sk_buff *pSkb_p, struct net_device *pNetDevice_p);
static struct net_device_stats *VEthGetStats(struct net_device *pNetDevice_p);
static void VEthTimeout(struct net_device *pNetDevice_p);
static tEplKernel VEthRecvFrame(tEplFrameInfo * pFrameInfo_p);

//=========================================================================//
//                                                                         //
//          P U B L I C   F U N C T I O N S                                //
//                                                                         //
//=========================================================================//

//---------------------------------------------------------------------------
//
// Function:
//
// Description:
//
//
//
// Parameters:
//
//
// Returns:
//
//
// State:
//
//---------------------------------------------------------------------------

static int VEthOpen(struct net_device *pNetDevice_p)
{
	tEplKernel Ret = kEplSuccessful;

	//open the device
//	struct net_device_stats* pStats = netdev_priv(pNetDevice_p);

	//start the interface queue for the network subsystem
	netif_start_queue(pNetDevice_p);

	// register callback function in DLL
	Ret = EplDllkRegAsyncHandler(VEthRecvFrame);

	EPL_DBGLVL_VETH_TRACE1
	    ("VEthOpen: EplDllkRegAsyncHandler returned 0x%02X\n", Ret);

	return 0;
}

static int VEthClose(struct net_device *pNetDevice_p)
{
	tEplKernel Ret = kEplSuccessful;

	EPL_DBGLVL_VETH_TRACE0("VEthClose\n");

	Ret = EplDllkDeregAsyncHandler(VEthRecvFrame);

	//stop the interface queue for the network subsystem
	netif_stop_queue(pNetDevice_p);
	return 0;
}

static int VEthXmit(struct sk_buff *pSkb_p, struct net_device *pNetDevice_p)
{
	tEplKernel Ret = kEplSuccessful;
	tEplFrameInfo FrameInfo;

	//transmit function
	struct net_device_stats *pStats = netdev_priv(pNetDevice_p);

	//save timestemp
	pNetDevice_p->trans_start = jiffies;

	FrameInfo.m_pFrame = (tEplFrame *) pSkb_p->data;
	FrameInfo.m_uiFrameSize = pSkb_p->len;

	//call send fkt on DLL
	Ret = EplDllkCalAsyncSend(&FrameInfo, kEplDllAsyncReqPrioGeneric);
	if (Ret != kEplSuccessful) {
		EPL_DBGLVL_VETH_TRACE1
		    ("VEthXmit: EplDllkCalAsyncSend returned 0x%02X\n", Ret);
		netif_stop_queue(pNetDevice_p);
		goto Exit;
	} else {
		EPL_DBGLVL_VETH_TRACE0("VEthXmit: frame passed to DLL\n");
		dev_kfree_skb(pSkb_p);

		//set stats for the device
		pStats->tx_packets++;
		pStats->tx_bytes += FrameInfo.m_uiFrameSize;
	}

      Exit:
	return 0;

}

static struct net_device_stats *VEthGetStats(struct net_device *pNetDevice_p)
{
	EPL_DBGLVL_VETH_TRACE0("VEthGetStats\n");

	return netdev_priv(pNetDevice_p);
}

static void VEthTimeout(struct net_device *pNetDevice_p)
{
	EPL_DBGLVL_VETH_TRACE0("VEthTimeout(\n");

	// $$$ d.k.: move to extra function, which is called by DLL when new space is available in TxFifo
	if (netif_queue_stopped(pNetDevice_p)) {
		netif_wake_queue(pNetDevice_p);
	}
}

static tEplKernel VEthRecvFrame(tEplFrameInfo * pFrameInfo_p)
{
	tEplKernel Ret = kEplSuccessful;
	struct net_device *pNetDevice = pVEthNetDevice_g;
	struct net_device_stats *pStats = netdev_priv(pNetDevice);
	struct sk_buff *pSkb;

	EPL_DBGLVL_VETH_TRACE1("VEthRecvFrame: FrameSize=%u\n",
			       pFrameInfo_p->m_uiFrameSize);

	pSkb = dev_alloc_skb(pFrameInfo_p->m_uiFrameSize + 2);
	if (pSkb == NULL) {
		pStats->rx_dropped++;
		goto Exit;
	}
	pSkb->dev = pNetDevice;

	skb_reserve(pSkb, 2);

	memcpy((void *)skb_put(pSkb, pFrameInfo_p->m_uiFrameSize),
	       pFrameInfo_p->m_pFrame, pFrameInfo_p->m_uiFrameSize);

	pSkb->protocol = eth_type_trans(pSkb, pNetDevice);
	pSkb->ip_summed = CHECKSUM_UNNECESSARY;

	// call netif_rx with skb
	netif_rx(pSkb);

	EPL_DBGLVL_VETH_TRACE1("VEthRecvFrame: SrcMAC=0x%llx\n",
			       AmiGetQword48FromBe(pFrameInfo_p->m_pFrame->
						   m_be_abSrcMac));

	// update receive statistics
	pStats->rx_packets++;
	pStats->rx_bytes += pFrameInfo_p->m_uiFrameSize;

      Exit:
	return Ret;
}

static const struct net_device_ops epl_netdev_ops = {
	.ndo_open		= VEthOpen,
	.ndo_stop		= VEthClose,
	.ndo_get_stats		= VEthGetStats,
	.ndo_start_xmit		= VEthXmit,
	.ndo_tx_timeout		= VEthTimeout,
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_set_mac_address	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
};

tEplKernel VEthAddInstance(tEplDllkInitParam *pInitParam_p)
{
	tEplKernel Ret = kEplSuccessful;

	// allocate net device structure with priv pointing to stats structure
	pVEthNetDevice_g =
	    alloc_netdev(sizeof(struct net_device_stats), EPL_VETH_NAME,
			 ether_setup);
//    pVEthNetDevice_g = alloc_etherdev(sizeof (struct net_device_stats));

	if (pVEthNetDevice_g == NULL) {
		Ret = kEplNoResource;
		goto Exit;
	}

	pVEthNetDevice_g->netdev_ops = &epl_netdev_ops;
	pVEthNetDevice_g->watchdog_timeo = EPL_VETH_TX_TIMEOUT;
	pVEthNetDevice_g->destructor = free_netdev;

	// copy own MAC address to net device structure
	memcpy(pVEthNetDevice_g->dev_addr, pInitParam_p->m_be_abSrcMac, 6);

	//register VEth to the network subsystem
	if (register_netdev(pVEthNetDevice_g)) {
		EPL_DBGLVL_VETH_TRACE0
		    ("VEthAddInstance: Could not register VEth...\n");
	} else {
		EPL_DBGLVL_VETH_TRACE0
		    ("VEthAddInstance: Register VEth successfull...\n");
	}

      Exit:
	return Ret;
}

tEplKernel VEthDelInstance(void)
{
	tEplKernel Ret = kEplSuccessful;

	if (pVEthNetDevice_g != NULL) {
		//unregister VEth from the network subsystem
		unregister_netdev(pVEthNetDevice_g);
		// destructor was set to free_netdev,
		// so we do not need to call free_netdev here
		pVEthNetDevice_g = NULL;
	}

	return Ret;
}

#endif // (((EPL_MODULE_INTEGRATION) & (EPL_MODULE_VETH)) != 0)