summaryrefslogtreecommitdiff
path: root/cleopatre/plcdrv/arm/src/plc_drv.c
blob: 7bd3cbe302871f32c25b9a2ae4df54d57e95b67d (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    plc_drv.c
 * \brief   « brief description »
 * \ingroup Cleopatre - Isis
 *
 * « long description »
 */


#define DRV_NAME	"SPC200AV"
#define DRV_VERSION	"1.0"
#define DRV_RELDATE	"Mar 18, 2008"


#include <linux/kernel.h>
#include <linux/module.h>
//#include <linux/version.h>
#include <linux/init.h>
//#include <linux/errno.h>
#include <linux/netdevice.h>
//#include <linux/etherdevice.h>
//#include <asm/arch/hardware.h>
//#include <asm/io.h>
//#include <linux/proc_fs.h>
//#include <linux/sysctl.h>
//#include <asm/cacheflush.h>
//#include <linux/ethtool.h>
//#include <linux/mii.h>
//#include <linux/dma-mapping.h>
//#include <linux/kthread.h>

#include "common.h"
#include "plc_drv.h"
#include "processing.h"
#include "mailbox.h"

MODULE_AUTHOR("SPiDCOM Technologies");
MODULE_DESCRIPTION("SPC200AV PLC driver");
MODULE_LICENSE("SPiDCOM Technologies 2008");

/** Define declarations */
#define PKT_BUF_SZ 1522 //Size of each ethernet frame allocation
#define SKB_RESERVE (2+32) //align IP Header

#define DATA_TYPE 0
#define MME_TYPE  1

//These identify the driver base version and may not be removed
static char version[] __devinitdata = DRV_NAME " PLC driver v" DRV_VERSION " (" DRV_RELDATE ")\n";

/** Driver Private datas */
typedef struct {
    uint32_t nb_skbs_in_use;
    struct net_device_stats stats;
} Private;

/** Global data declaration */
static struct net_device *plcdrv_dev;

/** Parameters for the module */
//TODO:put the 50 into a define
static int rx_data_skb_ring_size = 50;
static int rx_mme_skb_ring_size = 50;
module_param(rx_data_skb_ring_size, int, 0644);
MODULE_PARM_DESC(rx_data_skb_ring_size, "Number of data Ethernet buffers for PLC-> ARM echanges");
module_param(rx_mme_skb_ring_size, int, 0644);
MODULE_PARM_DESC(rx_mme_skb_ring_size, "Number of MME Ethernet buffers for PLC-> ARM echanges");

/** 
 * find offset of the data field in a sk_buff structure
 *
 * \return  offset
 */
static inline int get_skb_data_offset()
{
    struct sk_buff skb;
    return (int)(&skb - &skb.data);
}

/** 
 * Previous sending message to the HLE layer is ok (IT ALA)
 *
 * \param  int_num  interrupt number.
 * \param  dev_id  device address.
 * \return  error code
 */
static irqreturn_t tx_ack (int int_num, void *dev_id, struct pt_regs *regs)
{
    struct net_device *dev = (struct net_device *)dev_id;
    if(netif_queue_stopped(dev))
        netif_wake_queue(dev);
}//tx_ack

/** 
 * Receive a message from the HLE (after IT LAT)
 *
 * \param  int_num  interrupt number.
 * \param  dev_id  device address.
 * \return  error code
 */
static irqreturn_t rx_handler (int int_num, void *dev_id, struct pt_regs *regs)
{
    mailbox_receive();
    return IRQ_HANDLED;  
}//rx_handler

/** 
 * Receive a packet
 *
 * \param  pointer  buffer pointer.
 * \param  length  length of the message pointed.
 * \return  error code
 */
int plcdrv_rx (void *pointer, int length)
{
    Private *pr = (Private*)plcdrv_dev->priv;
    struct sk_buff skb;
    int offset;

    if(pointer)
    {
        offset = get_skb_data_offset();

        //Find the sk_buff address
        skb = (struct sk_buff *)(pointer - offset);

        //Pass data to the linux internal receive level
        skb->dev = plcdrv_dev;
        skb->protocol = eth_type_trans(skb, plcdrv_dev);
        skb->ip_summed = CHECKSUM_UNNECESSARY;
        priv->stats.rx_packets++;
        priv->stats.rx_bytes += length;
        netif_rx(skb);
        return 0;
    }
    else
    {
        printk(KERN_ERR DRV_NAME": %s: error freeing a NULL buffer\n", dev->name);
        return -1;
    }
}//plcdrv_rx

/** 
 * Transmit a packet
 *
 * \param  skb  packet structure address.
 * \param  dev  device address.
 * \return  error code
 */
static int plcdrv_tx (struct sk_buff *skb, struct net_device *dev)
{
    Private *pr = (Private*)dev->priv;
    int status;

    //Handle transmit
    dev->trans_start = jiffies;

    //Send buffer to lower layers
    status = processing_send((void *)skb->data, skb->len);
    if(status == NEARLY_FULL)
    {
        netif_stop_queue(dev);
    }
    else if(status == FULL)
    {
        netif_stop_queue(dev);
        pr->stats.tx_qfull_err++;
        dev_kfree_skb(skb);
    }
    return 0;
}//plcdrv_tx

/** 
 * Read packet status from the device
 *
 * \param  dev  device address.
 * \return  device stats
 */
static struct net_device_stats *plcdrv_get_stats (struct net_device *dev)
{
    Private *pr = NULL;

    if(dev == NULL)
        return NULL;
    pr = (Private *)dev->priv;
    if(pr == NULL)
        return NULL;

    return &pr->stats;
}//plcdrv_get_stats

/** 
 * Release a sk_buff
 *
 * \param  pointer  buffer pointer.
 * \param  reason  reason to release the buffer
 * \return  error code
 */
int free_buffer(void *pointer, enum free_reason reason)
{
    Private *pr = (Private*)plcdrv_dev->priv;
    struct sk_buff *skb;
    int offset;

    if(pointer)
    {
        //Find the sk_buff address
        offset = get_skb_data_offset();
        skb = (struct sk_buff *)(pointer - offset);

        //Check the free reason for stats
        switch(reason)
        {
        case RX_DROP: pr->stats.rx_dropped++;
                      break;
        case TX_DROP: pr->stats.tx_dropped++;
                      break;
        case TX_COMPLETE: pr->stats.tx_packets++;
                          pr->stats.tx_bytes += skb->len;
                          break;
        }

        //Free sk_buff
        dev_free_skb(skb);
        pr->nb_skbs_in_use--;
        return 0;
    }
    else
    {
        printk(KERN_ERR DRV_NAME": %s: error freeing a NULL buffer\n", dev->name);
        return -1;
    }
}//free_buffer

/** 
 * Alloc sk_buff and send to the communication layer
 *
 * \param  type  type of buffer.
 * \return  error code
 */
int alloc_buffer(enum buffer_type type)
{
    int result = 0;
    Private *pr = (Private*)plcdrv_dev->priv;
    struct sk_buff *skb;

    //Alloc a new sk_buff
    skb = dev_alloc_skb(PKT_BUF_SZ+SKB_RESERVE, GFP_KERNEL);
    if(!skb)
    {
        printk(KERN_ERR DRV_NAME": %s: error allocating sk_buff for\n",plcdrv_dev->name);
        return -ENOMEM;
    }
    skb_reserve(skb, SKB_RESERVE); //To ensure IP header is 4byte aligned after 14byte Eth header

    //Send the buffer to the communication layer
    if(result = mailbox_buffer_add((void*)skb->data), type)
    {
        dev_free_skb(skb);
        return result;
    }
    pr->nb_skbs_in_use++;
    return 0;
}//alloc_buffer

/** 
 * Initialize the device
 *
 * \param  dev  device address.
 * \return  error code
 */
static int plcdrv_open (struct net_device *dev)
{
    Private *pr = (Private*)dev->priv;
    int i;

    //Initialize lower layers
    processing_init();

    //Set number of sk_buff in use
    pr->nb_skbs_in_use = 0;

    //Alloc skbuff pool for data rx packet
    for(i=0 ; i<rx_data_skb_ring_size ; i++)
    {
        if(alloc_buffer(DATA))
        {
            printk(KERN_ERR DRV_NAME ": %s: error creating data buffer pool\n", dev->name);
            return -ENOMEM;
        }
    }
    //Alloc skbuff pool for mme rx packet
    for(i=0 ; i<rx_mme_skb_ring_size ; i++)
    {
        if(alloc_buffer(MME))
        {
            printk(KERN_ERR DRV_NAME ": %s: error creating mme buffer pool\n", dev->name);
            return -ENOMEM;
        }
    }
    //Alloc skbuff pool for interface rx packet
    for(i=0 ; i<rx_mme_skb_ring_size ; i++)
    {
        if(alloc_buffer(INTERFACE))
        {
            printk(KERN_ERR DRV_NAME ": %s: error creating interface buffer pool\n", dev->name);
            return -ENOMEM;
        }
    }

    //Start transmit
    netif_start_queue(dev);
    return 0;
}//plcdrv_open

/** 
 * Stop the device
 *
 * \param  dev  device address.
 * \return  error code
 */
static int plcdrv_close (struct net_device *dev)
{
    Private *pr = (Private*)dev->priv;

    //TODO: send a message to processing layer or communication layer
    //that we want to shutdown le plc driver

    //Wait all allocated sk_buff become free
    //(a sk_buff become free when we receive a send_done procedure)
    while(pr->nb_skbs_in_use);

    //Uninitialize lower layers
    processing_uninit();
    return 0;
}//plcdrv_close

/** 
 * Initialize the module
 *
 * \param  dev  device address.
 * \return  error code
 */
static int plcdrv_init (struct net_device *dev)
{
    int result = 0;
    Private *pr = NULL;

    if(dev == NULL)
        return -ENODEV;

    pr = (Private *)dev->priv;

    //Initialisation functions
//TODO:check if without it's ok    ether_setup (dev);
//TODO    dev->tx_queue_len = 1000;
    dev->open = &plcdrv_open;
    dev->hard_start_xmit = &plcdrv_tx;
    dev->stop = &plcdrv_close;
    dev->get_stats = &plcdrv_get_stats;
    dev->weight = 32;

    //mask mailbox interrupts
    lat_it_disable();
    ala_it_disable();

    //Register these interrupts
    result = request_irq(LAT_IT_NUM, rx_handler, SA_INTERRUPT, "RX Mailbox msg", 0); //LAT interrupt
    result = request_irq(ALA_IT_NUM, tx_ack, SA_INTERRUPT, "TX Ack msg", dev); //ALA interrupt

    return result;
}//plcdrv_init

/** 
 * Register the module
 *
 * \return  error code
 */
static int plcdrv_init_module (void)
{
    int result;
    Private *pr;
    struct net_device *dev;

    printk("%s", version);

    //Alloc memory for netdevice private structure
    dev = alloc_netdev(sizeof(Private), "plc%d", ether_setup);
    if((NULL == dev) || NULL == dev->priv)
    {
        result = -ENOMEM;
    }
    pr = (Private*)dev->priv;

    //Store init function
    dev->init = plcdrv_init;
    SET_MODULE_OWNER(dev); 

    //Register net device
    result = register_netdev(dev);
    if(result < 0)
    {
        printk(KERN_ERR DRV_NAME ": plc0: Could not register device\n");
        kfree(pr);
        free_netdev(dev);
    }
    else
    {
        plcdrv_dev = dev;
    }
    return result;
}//plcdrv_init_module

/** 
 * Release the module
 */
static void plcdrv_exit_module (void)
{
    Private *pr = plcdrv_dev->priv;

    if(plcdrv_dev)
    {
        //Freeing private field of the net device struture
        if(pr)
            kfree(pr);

        //Freeing interrupts
        free_irq(LAT_IT_NUM, plcdrv_dev);
        free_irq(ALA_IT_NUM, plcdrv_dev);
        //Freeing network device
        free_netdev(plcdrv_dev);

        //Unregister net device
        unregister_netdev(plcdrv_dev);
    }

}//plcdrv_exit_module

module_init(plcdrv_init_module);
module_exit(plcdrv_exit_module);