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

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