root/drivers/net/eql.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. eql_init
  2. eql_open
  3. eql_close
  4. eql_ioctl
  5. eql_slave_xmit
  6. eql_get_stats
  7. eql_header
  8. eql_rebuild_header
  9. eql_enslave
  10. eql_emancipate
  11. eql_g_slave_cfg
  12. eql_s_slave_cfg
  13. eql_g_master_cfg
  14. eql_s_master_cfg
  15. eql_is_slave
  16. eql_is_master
  17. eql_new_slave
  18. eql_delete_slave
  19. slave_Bps
  20. slave_bps
  21. eql_number_slaves
  22. eql_is_empty
  23. eql_is_full
  24. eql_new_slave_queue
  25. eql_delete_slave_queue
  26. eql_insert_slave
  27. eql_remove_slave
  28. eql_insert_slave_dev
  29. eql_remove_slave_dev
  30. eql_best_slave_dev
  31. eql_best_slave
  32. eql_schedule_slaves
  33. eql_find_slave_dev
  34. eql_first_slave
  35. eql_next_slave
  36. eql_set_best_slave
  37. eql_lock_slave_queue
  38. eql_unlock_slave_queue
  39. eql_is_locked_slave_queue
  40. eql_timer
  41. init_module
  42. cleanup_module

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

/* [previous][next][first][last][top][bottom][index][help] */