OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [eql.c] - Blame information for rev 1626

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
2
 * Equalizer Load-balancer for serial network interfaces.
3
 *
4
 * (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
5
 * NCM: Network and Communications Management, Inc.
6
 *
7
 *
8
 *      This software may be used and distributed according to the terms
9
 *      of the GNU Public License, incorporated herein by reference.
10
 *
11
 * The author may be reached as simon@ncm.com, or C/O
12
 *    NCM
13
 *    Attn: Simon Janes
14
 *    6803 Whittier Ave
15
 *    McLean VA 22101
16
 *    Phone: 1-703-847-0040 ext 103
17
 */
18
 
19
static const char *version =
20
        "Equalizer1996: $Revision: 1.1 $ $Date: 2005-12-20 10:17:14 $ Simon Janes (simon@ncm.com)\n";
21
 
22
/*
23
 * Sources:
24
 *   skeleton.c by Donald Becker.
25
 * Inspirations:
26
 *   The Harried and Overworked Alan Cox
27
 * Conspiracies:
28
 *   The Alan Cox and Mike McLagan plot to get someone else to do the code,
29
 *   which turned out to be me.
30
 */
31
 
32
/*
33
 * $Log: not supported by cvs2svn $
34
 * Revision 1.1.1.1  2001/09/10 07:44:23  simons
35
 * Initial import
36
 *
37
 * Revision 1.1.1.1  2001/07/02 17:58:23  simons
38
 * Initial revision
39
 *
40
 * Revision 1.9  1996/10/12 11:14:37  davem
41
 * Quick merge to 2.0.20
42
 *
43
 * Revision 1.2  1996/04/11 17:51:52  guru
44
 * Added one-line eql_remove_slave patch.
45
 *
46
 * Revision 1.1  1996/04/11 17:44:17  guru
47
 * Initial revision
48
 *
49
 * Revision 3.13  1996/01/21  15:17:18  alan
50
 * tx_queue_len changes.
51
 * reformatted.
52
 *
53
 * Revision 3.12  1995/03/22  21:07:51  anarchy
54
 * Added suser() checks on configuration.
55
 * Moved header file.
56
 *
57
 * Revision 3.11  1995/01/19  23:14:31  guru
58
 *                    slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
59
 *                      (priority_Bps) + bytes_queued * 8;
60
 *
61
 * Revision 3.10  1995/01/19  23:07:53  guru
62
 * back to
63
 *                    slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
64
 *                      (priority_Bps) + bytes_queued;
65
 *
66
 * Revision 3.9  1995/01/19  22:38:20  guru
67
 *                    slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
68
 *                      (priority_Bps) + bytes_queued * 4;
69
 *
70
 * Revision 3.8  1995/01/19  22:30:55  guru
71
 *       slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
72
 *                      (priority_Bps) + bytes_queued * 2;
73
 *
74
 * Revision 3.7  1995/01/19  21:52:35  guru
75
 * printk's trimmed out.
76
 *
77
 * Revision 3.6  1995/01/19  21:49:56  guru
78
 * This is working pretty well. I gained 1 K/s in speed.. now it's just
79
 * robustness and printk's to be diked out.
80
 *
81
 * Revision 3.5  1995/01/18  22:29:59  guru
82
 * still crashes the kernel when the lock_wait thing is woken up.
83
 *
84
 * Revision 3.4  1995/01/18  21:59:47  guru
85
 * Broken set-bit locking snapshot
86
 *
87
 * Revision 3.3  1995/01/17  22:09:18  guru
88
 * infinite sleep in a lock somewhere..
89
 *
90
 * Revision 3.2  1995/01/15  16:46:06  guru
91
 * Log trimmed of non-pertinent 1.x branch messages
92
 *
93
 * Revision 3.1  1995/01/15  14:41:45  guru
94
 * New Scheduler and timer stuff...
95
 *
96
 * Revision 1.15  1995/01/15  14:29:02  guru
97
 * Will make 1.14 (now 1.15) the 3.0 branch, and the 1.12 the 2.0 branch, the one
98
 * with the dumber scheduler
99
 *
100
 * Revision 1.14  1995/01/15  02:37:08  guru
101
 * shock.. the kept-new-versions could have zonked working
102
 * stuff.. shudder
103
 *
104
 * Revision 1.13  1995/01/15  02:36:31  guru
105
 * big changes
106
 *
107
 *      scheduler was torn out and replaced with something smarter
108
 *
109
 *      global names not prefixed with eql_ were renamed to protect
110
 *      against namespace collisions
111
 *
112
 *      a few more abstract interfaces were added to facilitate any
113
 *      potential change of datastructure.  the driver is still using
114
 *      a linked list of slaves.  going to a heap would be a bit of
115
 *      an overkill.
116
 *
117
 *      this compiles fine with no warnings.
118
 *
119
 *      the locking mechanism and timer stuff must be written however,
120
 *      this version will not work otherwise
121
 *
122
 */
123
 
124
#include <linux/module.h>
125
 
126
#include <linux/kernel.h>
127
#include <linux/sched.h>
128
#include <linux/types.h>
129
#include <linux/fcntl.h>
130
#include <linux/interrupt.h>
131
#include <linux/ptrace.h>
132
#include <linux/ioport.h>
133
#include <linux/in.h>
134
#include <linux/malloc.h>
135
#include <linux/string.h>
136
#include <asm/system.h>
137
#include <asm/bitops.h>
138
#include <asm/io.h>
139
#include <asm/dma.h>
140
#include <linux/errno.h>              
141
 
142
#include <linux/netdevice.h>
143
#include <linux/if.h>
144
#include <linux/if_arp.h>
145
#include <linux/timer.h>
146
 
147
#include <linux/if_eql.h>
148
 
149
#ifndef EQL_DEBUG
150
/* #undef EQL_DEBUG      -* print nothing at all, not even a boot-banner */
151
/* #define EQL_DEBUG 1   -* print only the boot-banner */
152
/* #define EQL_DEBUG 5   -* print major function entries */
153
/* #define EQL_DEBUG 20  -* print subfunction entries */
154
/* #define EQL_DEBUG 50  -* print utility entries */
155
/* #define EQL_DEBUG 100 -* print voluminous function entries */
156
#define EQL_DEBUG 1
157
#endif
158
static unsigned int eql_debug = EQL_DEBUG;
159
 
160
int        eql_init(struct device *dev); /*  */
161
static int eql_open(struct device *dev); /*  */
162
static int eql_close(struct device *dev); /*  */
163
static int eql_ioctl(struct device *dev, struct ifreq *ifr, int cmd); /*  */
164
static int eql_slave_xmit(struct sk_buff *skb, struct device *dev); /*  */
165
 
166
static struct enet_statistics *eql_get_stats(struct device *dev); /*  */
167
static int eql_header(struct sk_buff *skb, struct device *dev,
168
                      unsigned short type, void *daddr, void *saddr,
169
                      unsigned len); /*  */
170
static int eql_rebuild_header(void *buff, struct device *dev,
171
                              unsigned long raddr, struct sk_buff *skb); /*  */
172
 
173
/* ioctl() handlers
174
   ---------------- */
175
static int eql_enslave(struct device *dev,  slaving_request_t *srq); /*  */
176
static int eql_emancipate(struct device *dev, slaving_request_t *srq); /*  */
177
 
178
static int eql_g_slave_cfg(struct device *dev, slave_config_t *sc); /*  */
179
static int eql_s_slave_cfg(struct device *dev, slave_config_t *sc); /*  */
180
 
181
static int eql_g_master_cfg(struct device *dev, master_config_t *mc); /*  */
182
static int eql_s_master_cfg(struct device *dev, master_config_t *mc); /*  */
183
 
184
static inline int eql_is_slave(struct device *dev); /*  */
185
static inline int eql_is_master(struct device *dev); /*  */
186
 
187
static slave_t *eql_new_slave(void); /*  */
188
static void eql_delete_slave(slave_t *slave); /*  */
189
 
190
/* static long eql_slave_priority(slave_t *slave); -*  */
191
static inline int eql_number_slaves(slave_queue_t *queue); /*  */
192
 
193
static inline int eql_is_empty(slave_queue_t *queue); /*  */
194
static inline int eql_is_full(slave_queue_t *queue); /*  */
195
 
196
static slave_queue_t *eql_new_slave_queue(struct device *dev); /*  */
197
static void eql_delete_slave_queue(slave_queue_t *queue); /*  */
198
 
199
static int eql_insert_slave(slave_queue_t *queue, slave_t *slave); /*  */
200
static slave_t *eql_remove_slave(slave_queue_t *queue, slave_t *slave); /*  */
201
 
202
/* static int eql_insert_slave_dev(slave_queue_t *queue, struct device *dev); -*  */
203
static int eql_remove_slave_dev(slave_queue_t *queue, struct device *dev); /*  */
204
 
205
static inline struct device *eql_best_slave_dev(slave_queue_t *queue); /*  */
206
static inline slave_t *eql_best_slave(slave_queue_t *queue); /*  */
207
static inline slave_t *eql_first_slave(slave_queue_t *queue); /*  */
208
static inline slave_t *eql_next_slave(slave_queue_t *queue, slave_t *slave); /*  */
209
 
210
static inline void eql_set_best_slave(slave_queue_t *queue, slave_t *slave); /*  */
211
static inline void eql_schedule_slaves(slave_queue_t *queue); /*  */
212
 
213
static slave_t *eql_find_slave_dev(slave_queue_t *queue, struct device *dev); /*  */
214
 
215
/* static inline eql_lock_slave_queue(slave_queue_t *queue); -*  */
216
/* static inline eql_unlock_slave_queue(slave_queue_t *queue); -*  */
217
 
218
static void eql_timer(unsigned long param);     /*  */
219
 
220
/* struct device * interface functions
221
   ---------------------------------------------------------
222
   */
223
 
224
int eql_init(struct device *dev)
225
{
226
        static unsigned version_printed = 0;
227
        /* static unsigned num_masters     = 0; */
228
        equalizer_t *eql = 0;
229
        int i;
230
 
231
        if ( version_printed++ == 0 && eql_debug > 0)
232
                printk(version);
233
        /*
234
         *      Initialize the device structure.
235
         */
236
        dev->priv = kmalloc (sizeof (equalizer_t), GFP_KERNEL);
237
        if (dev->priv == NULL)
238
                return -ENOMEM;
239
        memset (dev->priv, 0, sizeof (equalizer_t));
240
        eql = (equalizer_t *) dev->priv;
241
 
242
        eql->stats = kmalloc (sizeof (struct enet_statistics), GFP_KERNEL);
243
        if (eql->stats == NULL)
244
        {
245
                kfree(dev->priv);
246
                dev->priv = NULL;
247
                return -ENOMEM;
248
        }
249
        memset (eql->stats, 0, sizeof (struct enet_statistics));
250
 
251
        init_timer (&eql->timer);
252
        eql->timer.data         = (unsigned long) dev->priv;
253
        eql->timer.expires      = jiffies+EQL_DEFAULT_RESCHED_IVAL;
254
        eql->timer.function     = &eql_timer;
255
        eql->timer_on           = 0;
256
 
257
        dev->open               = eql_open;
258
        dev->stop               = eql_close;
259
        dev->do_ioctl           = eql_ioctl;
260
        dev->hard_start_xmit    = eql_slave_xmit;
261
        dev->get_stats          = eql_get_stats;
262
 
263
        /*
264
         *      Fill in the fields of the device structure with
265
         *      eql-generic values. This should be in a common
266
         *      file instead of per-driver.
267
         */
268
 
269
        for (i = 0; i < DEV_NUMBUFFS; i++)
270
                skb_queue_head_init(&dev->buffs[i]);
271
 
272
        dev->hard_header        = eql_header;
273
        dev->rebuild_header     = eql_rebuild_header;
274
        dev->hard_header_len    = MAX_HEADER; /* enough space for any slave */
275
 
276
        /*
277
         *      Now we undo some of the things that eth_setup does
278
         *      that we don't like
279
         */
280
 
281
        dev->mtu                = EQL_DEFAULT_MTU;      /* set to 576 in eql.h */
282
        dev->flags              = IFF_MASTER;
283
 
284
        dev->family             = AF_INET;
285
        dev->pa_addr            = 0;
286
        dev->pa_brdaddr         = 0;
287
        dev->pa_mask            = 0;
288
        dev->pa_alen            = 4;
289
 
290
        dev->type               = ARPHRD_SLIP;
291
        dev->tx_queue_len       = 5;            /* Hands them off fast */
292
 
293
        return 0;
294
}
295
 
296
static int eql_open(struct device *dev)
297
{
298
        equalizer_t *eql = (equalizer_t *) dev->priv;
299
        slave_queue_t *new_queue;
300
 
301
#ifdef EQL_DEBUG
302
        if (eql_debug >= 5)
303
                printk ("%s: open\n", dev->name);
304
#endif
305
 
306
        printk ("%s: remember to turn off Van-Jacobson compression on your slave devices.\n", dev->name);
307
 
308
        new_queue = eql_new_slave_queue (dev);
309
 
310
        if (new_queue != 0)
311
        {
312
                new_queue->master_dev = dev;
313
                eql->queue = new_queue;
314
                eql->queue->lock = 0;
315
                eql->min_slaves = 1;
316
                eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
317
 
318
                printk ("%s: adding timer\n", dev->name);
319
                eql->timer_on = 1;
320
                add_timer (&eql->timer);
321
 
322
                MOD_INC_USE_COUNT;
323
                return 0;
324
        }
325
        return 1;
326
}
327
 
328
 
329
static int eql_close(struct device *dev)
330
{
331
        equalizer_t *eql = (equalizer_t *) dev->priv;
332
 
333
#ifdef EQL_DEBUG
334
        if ( eql_debug >= 5)
335
                printk ("%s: close\n", dev->name);
336
#endif
337
        /*
338
         *      The timer has to be stopped first before we start hacking away
339
         *      at the data structure it scans every so often...
340
         */
341
 
342
#ifdef EQL_DEBUG
343
        printk ("%s: stopping timer\n", dev->name);
344
#endif  
345
        eql->timer_on = 0;
346
        del_timer (&eql->timer);
347
 
348
        eql_delete_slave_queue (eql->queue);
349
 
350
        MOD_DEC_USE_COUNT;
351
        return 0;
352
}
353
 
354
 
355
static int eql_ioctl(struct device *dev, struct ifreq *ifr, int cmd)
356
{
357
        if(!suser() && cmd!=EQL_GETMASTRCFG && cmd!=EQL_GETSLAVECFG)
358
                return -EPERM;
359
        switch (cmd)
360
        {
361
                case EQL_ENSLAVE:
362
                        return eql_enslave (dev, (slaving_request_t *) ifr->ifr_data);
363
                case EQL_EMANCIPATE:
364
                        return eql_emancipate (dev, (slaving_request_t *) ifr->ifr_data);
365
                case EQL_GETSLAVECFG:
366
                        return eql_g_slave_cfg (dev, (slave_config_t *) ifr->ifr_data);
367
                case EQL_SETSLAVECFG:
368
                        return eql_s_slave_cfg (dev, (slave_config_t *) ifr->ifr_data);
369
                case EQL_GETMASTRCFG:
370
                        return eql_g_master_cfg (dev, (master_config_t *) ifr->ifr_data);
371
                case EQL_SETMASTRCFG:
372
                        return eql_s_master_cfg (dev, (master_config_t *) ifr->ifr_data);
373
                default:
374
                        return -EOPNOTSUPP;
375
        }
376
}
377
 
378
 
379
static int eql_slave_xmit(struct sk_buff *skb, struct device *dev)
380
{
381
        equalizer_t *eql = (equalizer_t *) dev->priv;
382
        struct device *slave_dev = 0;
383
        slave_t *slave;
384
 
385
        if (skb == NULL)
386
                return 0;
387
 
388
#if 0
389
        /* Make a copy we can free so we don't mess up the skb->dev pointer */
390
        skb2 = skb_clone(skb, GFP_ATOMIC);
391
 
392
        if (skb2 == NULL)
393
                return 1;
394
#endif
395
 
396
        eql_schedule_slaves (eql->queue);
397
 
398
        slave = eql_best_slave (eql->queue);
399
        slave_dev = slave ? slave->dev : 0;
400
 
401
        if ( slave_dev != 0 )
402
        {
403
#ifdef EQL_DEBUG
404
                if (eql_debug >= 100)
405
                        printk ("%s: %d slaves xmitng %ld B %s\n",
406
                                dev->name, eql_number_slaves (eql->queue), skb->len,
407
                                slave_dev->name);
408
#endif
409
 
410
                /* Rip off the fake header */
411
                skb_pull(skb,MAX_HEADER);
412
 
413
                /* The original code had no hard header constructed.
414
                 * If a frame is fragmented on EQL and then passed to PPP,
415
                 * or ISDN, the result will be a panic.
416
                 * The solution is to call the hard_header constructor
417
                 * for the device we point to just before we send the packet.
418
                 * If this fails we drop the packet.
419
                 * We don't know any special parameters for the hard_header
420
                 * constructor at this point, so we pass in made up values
421
                 * that will cause the constructor to fail on every device
422
                 * except those that we are allowed to use EQL on:
423
                 * PPP, SLIP and ISDN (in some cases!).
424
                 * The worst thing that happens is if some fool
425
                 * configures EQL to enslave something that needs
426
                 * these parameters it throws out packets.
427
                 * This is not an issue for the things EQL is intended for
428
                 * anyway, and probably would have crashed the kernel
429
                 * or sent garbage down the wire previously.
430
                 */
431
 
432
                if (slave_dev->hard_header == NULL
433
                || slave_dev->hard_header(skb,slave_dev,
434
                        ETH_P_IP,NULL,NULL,skb->len) >= 0) {
435
                        slave->bytes_queued += skb->len;
436
                        dev_queue_xmit (skb, slave_dev, 1);
437
                        eql->stats->tx_packets++;
438
                        /* dev_kfree_skb(skb, FREE_WRITE); */
439
                        return 0;
440
                }
441
        }
442
 
443
        /*
444
         *      The alternative for this is the return 1 and have
445
         *      dev_queue_xmit just queue it up on the eql's queue.
446
         */
447
 
448
        eql->stats->tx_dropped++;
449
        /* dev_kfree_skb(skb2, FREE_WRITE); */
450
        dev_kfree_skb(skb, FREE_WRITE);
451
        return 0;
452
}
453
 
454
 
455
static struct enet_statistics * eql_get_stats(struct device *dev)
456
{
457
        equalizer_t *eql = (equalizer_t *) dev->priv;
458
        return eql->stats;
459
}
460
 
461
 
462
static int  eql_header(struct sk_buff *skb, struct device *dev,
463
           unsigned short type, void *daddr, void *saddr,
464
           unsigned len)
465
{
466
        /* Fake header to keep space during buggy IP fragmentation.  */
467
        skb_push(skb,MAX_HEADER);
468
        return MAX_HEADER;
469
}
470
 
471
 
472
static int eql_rebuild_header(void *buff, struct device *dev,
473
                   unsigned long raddr, struct sk_buff *skb)
474
{
475
        return 0;
476
}
477
 
478
/*
479
 *      Private ioctl functions
480
 */
481
 
482
static int eql_enslave(struct device *dev, slaving_request_t *srqp)
483
{
484
        struct device *master_dev;
485
        struct device *slave_dev;
486
        slaving_request_t srq;
487
        int err;
488
 
489
        err = verify_area(VERIFY_READ, (void *)srqp, sizeof (slaving_request_t));
490
        if (err)
491
          {
492
#ifdef EQL_DEBUG
493
        if (eql_debug >= 20)
494
                printk ("EQL enslave: error detected by verify_area\n");
495
#endif  
496
                return err;
497
          }
498
        memcpy_fromfs (&srq, srqp, sizeof (slaving_request_t));
499
 
500
#ifdef EQL_DEBUG
501
        if (eql_debug >= 20)
502
                printk ("%s: enslave '%s' %ld bps\n", dev->name,
503
                        srq.slave_name, srq.priority);
504
#endif  
505
        master_dev = dev;               /* for "clarity" */
506
        slave_dev  = dev_get (srq.slave_name);
507
 
508
        if (master_dev != 0 && slave_dev != 0)
509
        {
510
                if ((master_dev->flags & IFF_UP) == IFF_UP)
511
                {
512
                        /*slave is not a master & not already a slave:*/
513
                        if (! eql_is_master (slave_dev)  &&
514
                            ! eql_is_slave (slave_dev) )
515
                        {
516
                                slave_t *s = eql_new_slave ();
517
                                equalizer_t *eql =
518
                                        (equalizer_t *) master_dev->priv;
519
                                s->dev = slave_dev;
520
                                s->priority = srq.priority;
521
                                s->priority_bps = srq.priority;
522
                                s->priority_Bps = srq.priority / 8;
523
                                slave_dev->flags |= IFF_SLAVE;
524
                                eql_insert_slave (eql->queue, s);
525
                                return 0;
526
                        }
527
#ifdef EQL_DEBUG
528
                        else if (eql_debug >= 20)
529
                                printk ("EQL enslave: slave is master or slave is already slave\n");
530
#endif  
531
                }
532
#ifdef EQL_DEBUG
533
                else if (eql_debug >= 20)
534
                        printk ("EQL enslave: master device not up!\n");
535
#endif  
536
        }
537
#ifdef EQL_DEBUG
538
        else if (eql_debug >= 20)
539
                printk ("EQL enslave: master or slave are NULL");
540
#endif  
541
        return -EINVAL;
542
}
543
 
544
static int eql_emancipate(struct device *dev, slaving_request_t *srqp)
545
{
546
        struct device *master_dev;
547
        struct device *slave_dev;
548
        slaving_request_t srq;
549
        int err;
550
 
551
        err = verify_area(VERIFY_READ, (void *)srqp, sizeof (slaving_request_t));
552
        if (err)
553
                return err;
554
 
555
        memcpy_fromfs (&srq, srqp, sizeof (slaving_request_t));
556
#ifdef EQL_DEBUG
557
        if (eql_debug >= 20)
558
                printk ("%s: emancipate `%s`\n", dev->name, srq.slave_name);
559
#endif
560
        master_dev = dev;               /* for "clarity" */
561
        slave_dev  = dev_get (srq.slave_name);
562
 
563
        if ( eql_is_slave (slave_dev) ) /* really is a slave */
564
        {
565
                equalizer_t *eql = (equalizer_t *) master_dev->priv;
566
                slave_dev->flags = slave_dev->flags & ~IFF_SLAVE;
567
                eql_remove_slave_dev (eql->queue, slave_dev);
568
                return 0;
569
        }
570
        return -EINVAL;
571
}
572
 
573
 
574
static int eql_g_slave_cfg(struct device *dev, slave_config_t *scp)
575
{
576
        slave_t *slave;
577
        equalizer_t *eql;
578
        struct device *slave_dev;
579
        slave_config_t sc;
580
        int err;
581
 
582
        err = verify_area(VERIFY_READ, (void *)scp, sizeof (slave_config_t));
583
        if (err)
584
                return err;
585
 
586
        memcpy_fromfs (&sc, scp, sizeof (slave_config_t));
587
#ifdef EQL_DEBUG
588
        if (eql_debug >= 20)
589
                printk ("%s: get config for slave `%s'\n", dev->name, sc.slave_name);
590
#endif
591
        eql = (equalizer_t *) dev->priv;
592
        slave_dev = dev_get (sc.slave_name);
593
 
594
        if ( eql_is_slave (slave_dev) )
595
        {
596
                slave = eql_find_slave_dev (eql->queue,  slave_dev);
597
                if (slave != 0)
598
                {
599
                        sc.priority = slave->priority;
600
                        err = verify_area(VERIFY_WRITE, (void *)scp, sizeof (slave_config_t));
601
                        if (err)
602
                                return err;
603
                        memcpy_tofs (scp, &sc, sizeof (slave_config_t));
604
                        return 0;
605
                }
606
        }
607
        return -EINVAL;
608
}
609
 
610
 
611
static int eql_s_slave_cfg(struct device *dev, slave_config_t *scp)
612
{
613
        slave_t *slave;
614
        equalizer_t *eql;
615
        struct device *slave_dev;
616
        slave_config_t sc;
617
        int err;
618
 
619
        err = verify_area(VERIFY_READ, (void *)scp, sizeof (slave_config_t));
620
        if (err)
621
                return err;
622
 
623
#ifdef EQL_DEBUG
624
        if (eql_debug >= 20)
625
                printk ("%s: set config for slave `%s'\n", dev->name, sc.slave_name);
626
#endif
627
 
628
        memcpy_fromfs (&sc, scp, sizeof (slave_config_t));
629
 
630
        eql = (equalizer_t *) dev->priv;
631
        slave_dev = dev_get (sc.slave_name);
632
 
633
        if ( eql_is_slave (slave_dev) )
634
        {
635
                slave = eql_find_slave_dev (eql->queue, slave_dev);
636
                if (slave != 0)
637
                {
638
                        slave->priority = sc.priority;
639
                        slave->priority_bps = sc.priority;
640
                        slave->priority_Bps = sc.priority / 8;
641
                        return 0;
642
                }
643
        }
644
        return -EINVAL;
645
}
646
 
647
 
648
static int eql_g_master_cfg(struct device *dev, master_config_t *mcp)
649
{
650
        equalizer_t *eql;
651
        master_config_t mc;
652
 
653
#if EQL_DEBUG
654
        if (eql_debug >= 20)
655
                printk ("%s: get master config\n", dev->name);
656
#endif
657
 
658
        if ( eql_is_master (dev) )
659
        {
660
                int err;
661
                err = verify_area(VERIFY_WRITE, (void *)mcp, sizeof (master_config_t));
662
                if (err)
663
                        return err;
664
                eql = (equalizer_t *) dev->priv;
665
                mc.max_slaves = eql->max_slaves;
666
                mc.min_slaves = eql->min_slaves;
667
                memcpy_tofs (mcp, &mc, sizeof (master_config_t));
668
                return 0;
669
        }
670
        return -EINVAL;
671
}
672
 
673
 
674
static int eql_s_master_cfg(struct device *dev, master_config_t *mcp)
675
{
676
        equalizer_t *eql;
677
        master_config_t mc;
678
        int err;
679
 
680
        err = verify_area(VERIFY_READ, (void *)mcp, sizeof (master_config_t));
681
        if (err)
682
                return err;
683
#if EQL_DEBUG
684
        if (eql_debug >= 20)
685
                printk ("%s: set master config\n", dev->name);
686
#endif
687
        memcpy_fromfs (&mc, mcp, sizeof (master_config_t));
688
        if ( eql_is_master (dev) )
689
        {
690
                eql = (equalizer_t *) dev->priv;
691
                eql->max_slaves = mc.max_slaves;
692
                eql->min_slaves = mc.min_slaves;
693
                return 0;
694
        }
695
        return -EINVAL;
696
}
697
 
698
/*
699
 *      Private device support functions
700
 */
701
 
702
static inline int eql_is_slave(struct device *dev)
703
{
704
        if (dev)
705
        {
706
                if ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
707
                        return 1;
708
        }
709
        return 0;
710
}
711
 
712
 
713
static inline int eql_is_master(struct device *dev)
714
{
715
        if (dev)
716
        {
717
                if ((dev->flags & IFF_MASTER) == IFF_MASTER)
718
                return 1;
719
        }
720
        return 0;
721
}
722
 
723
 
724
static slave_t *eql_new_slave(void)
725
{
726
        slave_t *slave;
727
 
728
        slave = (slave_t *) kmalloc (sizeof (slave_t), GFP_KERNEL);
729
        if (slave)
730
        {
731
                memset(slave, 0, sizeof (slave_t));
732
                return slave;
733
        }
734
        return 0;
735
}
736
 
737
 
738
static void eql_delete_slave(slave_t *slave)
739
{
740
        kfree (slave);
741
}
742
 
743
 
744
#if 0                           /* not currently used, will be used
745
                                   when we really use a priority queue */
746
static long slave_Bps(slave_t *slave)
747
{
748
        return (slave->priority_Bps);
749
}
750
 
751
static long slave_bps(slave_t *slave)
752
{
753
        return (slave->priority_bps);
754
}
755
 
756
#endif
757
 
758
static inline int eql_number_slaves(slave_queue_t *queue)
759
{
760
        return queue->num_slaves;
761
}
762
 
763
static inline int eql_is_empty(slave_queue_t *queue)
764
{
765
        if (eql_number_slaves (queue) == 0)
766
                return 1;
767
        return 0;
768
}
769
 
770
static inline int eql_is_full(slave_queue_t *queue)
771
{
772
        equalizer_t *eql = (equalizer_t *) queue->master_dev->priv;
773
 
774
        if (eql_number_slaves (queue) == eql->max_slaves)
775
                return 1;
776
        return 0;
777
}
778
 
779
static slave_queue_t *eql_new_slave_queue(struct device *dev)
780
{
781
        slave_queue_t *queue;
782
        slave_t *head_slave;
783
        slave_t *tail_slave;
784
 
785
        queue = (slave_queue_t *) kmalloc (sizeof (slave_queue_t), GFP_KERNEL);
786
        if (queue == NULL)
787
                return 0;
788
        memset (queue, 0, sizeof (slave_queue_t));
789
        head_slave = eql_new_slave ();
790
        tail_slave = eql_new_slave ();
791
 
792
        if ( head_slave != 0 &&
793
                tail_slave != 0 )
794
        {
795
                head_slave->next = tail_slave;
796
                tail_slave->next = 0;
797
                queue->head = head_slave;
798
                queue->num_slaves = 0;
799
                queue->master_dev = dev;
800
        }
801
        else
802
        {
803
                if (head_slave)
804
                        kfree(head_slave);
805
                if (tail_slave)
806
                        kfree(tail_slave);
807
                kfree (queue);
808
                return 0;
809
        }
810
        return queue;
811
}
812
 
813
 
814
static void eql_delete_slave_queue(slave_queue_t *queue)
815
{
816
        slave_t *zapped;
817
        /*
818
         *      This should only be called when there isn't a
819
         *      timer running that scans the data periodically..
820
         *      dev_close stops the timer...
821
         */
822
 
823
        while ( ! eql_is_empty (queue) )
824
        {
825
                zapped = eql_remove_slave (queue, queue->head->next);
826
                eql_delete_slave (zapped);
827
        }
828
        kfree (queue->head->next);
829
        kfree (queue->head);
830
        kfree (queue);
831
}
832
 
833
static int eql_insert_slave(slave_queue_t *queue, slave_t *slave)
834
{
835
        cli ();
836
 
837
        if ( ! eql_is_full (queue) )
838
        {
839
                slave_t *duplicate_slave = 0;
840
                duplicate_slave = eql_find_slave_dev (queue, slave->dev);
841
                if (duplicate_slave != 0)
842
                {
843
/*                        printk ("%s: found a duplicate, killing it and replacing\n",
844
                                  queue->master_dev->name); */
845
                        eql_delete_slave (eql_remove_slave (queue, duplicate_slave));
846
                }
847
                slave->next = queue->head->next;
848
                queue->head->next = slave;
849
                queue->num_slaves++;
850
                sti ();
851
                return 0;
852
        }
853
        sti ();
854
        return 1;
855
}
856
 
857
 
858
static slave_t *eql_remove_slave(slave_queue_t *queue, slave_t *slave)
859
{
860
        slave_t *prev;
861
        slave_t *curr;
862
 
863
        cli ();
864
 
865
        prev = queue->head;
866
        curr = queue->head->next;
867
        while (curr != slave &&
868
                curr->dev != 0 )
869
        {
870
/*              printk ("%s: remove_slave; searching...\n", queue->master_dev->name); */
871
                prev = curr;
872
                curr = curr->next;
873
        }
874
 
875
        if (curr == slave)
876
        {
877
                prev->next = curr->next;
878
                queue->num_slaves--;
879
                curr->dev->flags = curr->dev->flags & ~IFF_SLAVE;
880
                sti();
881
                return curr;
882
        }
883
        sti ();
884
        return 0;                        /* not found */
885
}
886
 
887
 
888
static int eql_remove_slave_dev(slave_queue_t *queue, struct device *dev)
889
{
890
        slave_t *prev;
891
        slave_t *curr;
892
        slave_t *target;
893
 
894
        target = eql_find_slave_dev (queue, dev);
895
 
896
        if (target != 0)
897
        {
898
                cli ();
899
                prev = queue->head;
900
                curr = prev->next;
901
                while (curr != target)
902
                {
903
                        prev = curr;
904
                        curr = curr->next;
905
                }
906
                prev->next = curr->next;
907
                queue->num_slaves--;
908
                sti ();
909
                eql_delete_slave (curr);
910
                return 0;
911
        }
912
        return 1;
913
}
914
 
915
 
916
static inline struct device *eql_best_slave_dev(slave_queue_t *queue)
917
{
918
        if (queue->best_slave != 0)
919
        {
920
                if (queue->best_slave->dev != 0)
921
                        return queue->best_slave->dev;
922
                else
923
                        return 0;
924
        }
925
        else
926
                return 0;
927
}
928
 
929
 
930
static inline slave_t *eql_best_slave(slave_queue_t *queue)
931
{
932
        return queue->best_slave;
933
}
934
 
935
static inline void eql_schedule_slaves(slave_queue_t *queue)
936
{
937
        struct device *master_dev = queue->master_dev;
938
        slave_t *best_slave = 0;
939
        slave_t *slave_corpse = 0;
940
 
941
#ifdef EQL_DEBUG
942
        if (eql_debug >= 100)
943
                printk ("%s: schedule %d slaves\n",
944
                        master_dev->name, eql_number_slaves (queue));
945
#endif
946
        if ( eql_is_empty (queue) )
947
        {
948
                /*
949
                 *      No slaves to play with
950
                 */
951
                eql_set_best_slave (queue, (slave_t *) 0);
952
                return;
953
        }
954
        else
955
        {
956
                /*
957
                 *      Make a pass to set the best slave
958
                 */
959
                unsigned long best_load = (unsigned long) ULONG_MAX;
960
                slave_t *slave = 0;
961
                int i;
962
 
963
                cli ();
964
                for (i = 1, slave = eql_first_slave (queue);
965
                        i <= eql_number_slaves (queue);
966
                        i++, slave = eql_next_slave (queue, slave))
967
                {
968
                        /*
969
                         *      Go through the slave list once, updating best_slave
970
                         *      whenever a new best_load is found, whenever a dead
971
                         *      slave is found, it is marked to be pulled out of the
972
                         *      queue
973
                         */
974
 
975
                        unsigned long slave_load;
976
                        unsigned long bytes_queued;
977
                        unsigned long priority_Bps;
978
 
979
                        if (slave != 0)
980
                        {
981
                                bytes_queued = slave->bytes_queued;
982
                                priority_Bps = slave->priority_Bps;
983
                                if ( slave->dev != 0)
984
                                {
985
                                        if ((slave->dev->flags & IFF_UP) == IFF_UP )
986
                                        {
987
                                                slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
988
                                                        (priority_Bps) + bytes_queued * 8;
989
 
990
                                                if (slave_load < best_load)
991
                                                {
992
                                                        best_load = slave_load;
993
                                                        best_slave = slave;
994
                                                }
995
                                        }
996
                                        else            /* we found a dead slave */
997
                                        {
998
                                                /*
999
                                                 *      We only bury one slave at a time, if more than
1000
                                                 *      one slave dies, we will bury him on the next
1001
                                                 *      reschedule. slaves don't die all at once that
1002
                                                 *      much anyway
1003
                                                 */
1004
                                                slave_corpse = slave;
1005
                                        }
1006
                                }
1007
                        }
1008
                } /* for */
1009
                sti ();
1010
                eql_set_best_slave (queue, best_slave);
1011
        } /* else */
1012
        if (slave_corpse != 0)
1013
        {
1014
                printk ("eql: scheduler found dead slave, burying...\n");
1015
                eql_delete_slave (eql_remove_slave (queue, slave_corpse));
1016
        }
1017
        return;
1018
}
1019
 
1020
 
1021
static slave_t * eql_find_slave_dev(slave_queue_t *queue, struct device *dev)
1022
{
1023
        slave_t *slave = 0;
1024
        slave = eql_first_slave(queue);
1025
 
1026
        while (slave != 0 && slave->dev != dev && slave != 0)
1027
        {
1028
#if 0
1029
                if (slave->dev != 0)
1030
                        printk ("eql: find_slave_dev; looked at '%s'...\n", slave->dev->name);
1031
                else
1032
                        printk ("eql: find_slave_dev; looked at nothing...\n");
1033
#endif
1034
                slave = slave->next;
1035
        }
1036
        return slave;
1037
}
1038
 
1039
 
1040
static inline slave_t *eql_first_slave(slave_queue_t *queue)
1041
{
1042
        return queue->head->next;
1043
}
1044
 
1045
 
1046
static inline slave_t *eql_next_slave(slave_queue_t *queue, slave_t *slave)
1047
{
1048
        return slave->next;
1049
}
1050
 
1051
static inline void eql_set_best_slave(slave_queue_t *queue, slave_t *slave)
1052
{
1053
        queue->best_slave = slave;
1054
}
1055
 
1056
static void eql_timer(unsigned long param)
1057
{
1058
        equalizer_t *eql = (equalizer_t *) param;
1059
        slave_t *slave;
1060
        slave_t *slave_corpse = 0;
1061
        int i;
1062
 
1063
        if ( ! eql_is_empty (eql->queue) )
1064
        {
1065
                cli ();
1066
                for (i = 1, slave = eql_first_slave (eql->queue);
1067
                        i <= eql_number_slaves (eql->queue);
1068
                        i++, slave = eql_next_slave (eql->queue, slave))
1069
                {
1070
                        if (slave != 0)
1071
                        {
1072
                                if ((slave->dev->flags & IFF_UP) == IFF_UP )
1073
                                {
1074
                                        slave->bytes_queued -= slave->priority_Bps;
1075
                                        if (slave->bytes_queued < 0)
1076
                                                slave->bytes_queued = 0;
1077
                                }
1078
                                else
1079
                                        slave_corpse = slave;
1080
                        }
1081
                }
1082
                sti ();
1083
                if (slave_corpse != 0)
1084
                {
1085
                        printk ("eql: timer found dead slave, burying...\n");
1086
                        eql_delete_slave (eql_remove_slave (eql->queue, slave_corpse));
1087
                }
1088
        }
1089
 
1090
        if (eql->timer_on != 0)
1091
        {
1092
                eql->timer.expires = jiffies+EQL_DEFAULT_RESCHED_IVAL;
1093
                add_timer (&eql->timer);
1094
        }
1095
}
1096
 
1097
#ifdef MODULE
1098
static struct device dev_eql =
1099
{
1100
        "eql", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, eql_init
1101
};
1102
 
1103
int init_module(void)
1104
{
1105
        if (register_netdev(&dev_eql) != 0) {
1106
                printk("eql: register_netdev() returned non-zero.\n");
1107
                return -EIO;
1108
        }
1109
        return 0;
1110
}
1111
 
1112
void cleanup_module(void)
1113
{
1114
        unregister_netdev(&dev_eql);
1115
}
1116
#endif /* MODULE */
1117
 
1118
/*
1119
 * Local Variables:
1120
 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c eql.c"
1121
 * version-control: t
1122
 * kept-new-versions: 20
1123
 * End:
1124
 */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.