Import changeset
[linux-flexiantxendom0-3.2.10.git] / net / atm / mpc.c
1 #include <linux/kernel.h>
2 #include <linux/string.h>
3 #include <linux/timer.h>
4 #include <linux/init.h>
5 #include <linux/bitops.h>
6
7 /* We are an ethernet device */
8 #include <linux/if_ether.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <net/sock.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <asm/byteorder.h>
15 #include <asm/uaccess.h>
16 #include <asm/checksum.h>   /* for ip_fast_csum() */
17 #include <net/arp.h>
18 #include <net/dst.h>
19 #include <linux/proc_fs.h>
20
21 /* And atm device */
22 #include <linux/atmdev.h>
23 #include <linux/atmlec.h>
24 #include <linux/atmmpc.h>
25 /* Modular too */
26 #include <linux/config.h>
27 #include <linux/module.h>
28
29 #include "lec.h"
30 #include "mpc.h"
31 #include "resources.h"  /* for bind_vcc() */
32
33 /*
34  * mpc.c: Implementation of MPOA client kernel part 
35  */
36
37 #if 0
38 #define dprintk printk   /* debug */
39 #else
40 #define dprintk(format,args...)
41 #endif
42
43 #if 0
44 #define ddprintk printk  /* more debug */
45 #else
46 #define ddprintk(format,args...)
47 #endif
48
49
50
51 #define MPOA_TAG_LEN 4
52
53 /* mpc_daemon -> kernel */
54 static void MPOA_trigger_rcvd (struct k_message *msg, struct mpoa_client *mpc);
55 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
56 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
57 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
58 static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
59 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action);
60 static void MPOA_cache_impos_rcvd(struct k_message *msg, struct mpoa_client *mpc);
61 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
62 static void set_mps_mac_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
63
64 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
65                           uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type);
66 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
67
68 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc);
69 static void mpoad_close(struct atm_vcc *vcc);
70 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
71
72 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
73 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev);
74 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev);
75 static void mpc_timer_refresh(void);
76 static void mpc_cache_check( unsigned long checking_time  );
77
78 static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
79         0xaa, 0xaa, 0x03,
80         {0x00, 0x00, 0x5e},
81         {0x00, 0x03}         /* For MPOA control PDUs */
82 };        
83 static struct llc_snap_hdr llc_snap_mpoa_data = {
84         0xaa, 0xaa, 0x03,
85         {0x00, 0x00, 0x00},
86         {0x08, 0x00}         /* This is for IP PDUs only */
87 };        
88 static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
89         0xaa, 0xaa, 0x03,
90         {0x00, 0x00, 0x00},
91         {0x88, 0x4c}         /* This is for tagged data PDUs */
92 };        
93
94 static struct notifier_block mpoa_notifier = {
95         mpoa_event_listener,
96         NULL,
97         0
98 };
99
100 #ifdef CONFIG_PROC_FS
101 extern int mpc_proc_init(void);
102 extern void mpc_proc_clean(void);
103 #endif
104
105 struct mpoa_client *mpcs = NULL; /* FIXME */
106 static struct atm_mpoa_qos *qos_head = NULL;
107 static struct timer_list mpc_timer;
108
109
110 static struct mpoa_client *find_mpc_by_itfnum(int itf)
111 {
112         struct mpoa_client *mpc;
113         
114         mpc = mpcs;  /* our global linked list */
115         while (mpc != NULL) {
116                 if (mpc->dev_num == itf)
117                         return mpc;
118                 mpc = mpc->next;    
119         }
120
121         return NULL;   /* not found */
122 }
123
124 static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
125 {
126         struct mpoa_client *mpc;
127         
128         mpc = mpcs;  /* our global linked list */
129         while (mpc != NULL) {
130                 if (mpc->mpoad_vcc == vcc)
131                         return mpc;
132                 mpc = mpc->next;
133         }
134
135         return NULL;   /* not found */
136 }
137
138 static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
139 {
140         struct mpoa_client *mpc;
141         
142         mpc = mpcs;  /* our global linked list */
143         while (mpc != NULL) {
144                 if (mpc->dev == dev)
145                         return mpc;
146                 mpc = mpc->next;
147         }
148
149         return NULL;   /* not found */
150 }
151
152 /*
153  * Functions for managing QoS list
154  */
155
156 /*
157  * Overwrites the old entry or makes a new one.
158  */
159 struct atm_mpoa_qos *atm_mpoa_add_qos(uint32_t dst_ip, struct atm_qos *qos)
160 {
161         struct atm_mpoa_qos *entry;
162
163         entry = atm_mpoa_search_qos(dst_ip);
164         if (entry != NULL) {
165                 entry->qos = *qos;
166                 return entry;
167         }
168
169         entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
170         if (entry == NULL) {
171                 printk("mpoa: atm_mpoa_add_qos: out of memory\n");
172                 return entry;
173         }
174
175         entry->ipaddr = dst_ip;
176         entry->qos = *qos;
177
178         entry->next = qos_head;
179         qos_head = entry;
180
181         return entry;
182 }
183
184 struct atm_mpoa_qos *atm_mpoa_search_qos(uint32_t dst_ip)
185 {
186         struct atm_mpoa_qos *qos;
187
188         qos = qos_head;
189         while( qos != NULL ){
190                 if(qos->ipaddr == dst_ip) {
191                         break;
192                 }
193                 qos = qos->next;
194         }
195
196         return qos;
197 }        
198
199 /*
200  * Returns 0 for failure
201  */
202 int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
203 {
204
205         struct atm_mpoa_qos *curr;
206
207         if (entry == NULL) return 0;
208         if (entry == qos_head) {
209                 qos_head = qos_head->next;
210                 kfree(entry);
211                 return 1;
212         }
213
214         curr = qos_head;
215         while (curr != NULL) {
216                 if (curr->next == entry) {
217                         curr->next = entry->next;
218                         kfree(entry);
219                         return 1;
220                 }
221                 curr = curr->next;
222         }
223
224         return 0;
225 }
226
227 void atm_mpoa_disp_qos(char *page, int *len)
228 {
229
230         unsigned char *ip;
231         char ipaddr[16];
232         struct atm_mpoa_qos *qos;
233
234         qos = qos_head;
235         *len += sprintf(page + *len, "QoS entries for shortcuts:\n");
236         *len += sprintf(page + *len, "IP address\n  TX:max_pcr pcr     min_pcr max_cdv max_sdu\n  RX:max_pcr pcr     min_pcr max_cdv max_sdu\n");
237
238         ipaddr[sizeof(ipaddr)-1] = '\0';
239         while (qos != NULL) {
240                 ip = (unsigned char *)&qos->ipaddr;
241                 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(ip));
242                 *len += sprintf(page + *len, "%u.%u.%u.%u\n     %-7d %-7d %-7d %-7d %-7d\n     %-7d %-7d %-7d %-7d %-7d\n",
243                                 NIPQUAD(ipaddr),
244                                 qos->qos.txtp.max_pcr, qos->qos.txtp.pcr, qos->qos.txtp.min_pcr, qos->qos.txtp.max_cdv, qos->qos.txtp.max_sdu,
245                                 qos->qos.rxtp.max_pcr, qos->qos.rxtp.pcr, qos->qos.rxtp.min_pcr, qos->qos.rxtp.max_cdv, qos->qos.rxtp.max_sdu);
246                 qos = qos->next;
247         }
248         
249         return;
250 }
251
252 static struct net_device *find_lec_by_itfnum(int itf)
253 {
254         extern struct atm_lane_ops atm_lane_ops; /* in common.c */
255         
256         if (atm_lane_ops.get_lecs == NULL)
257                 return NULL;
258
259         return atm_lane_ops.get_lecs()[itf]; /* FIXME: something better */
260 }
261
262 static struct mpoa_client *alloc_mpc(void)
263 {
264         struct mpoa_client *mpc;
265
266         mpc = kmalloc(sizeof (struct mpoa_client), GFP_KERNEL);
267         if (mpc == NULL)
268                 return NULL;
269         memset(mpc, 0, sizeof(struct mpoa_client));
270         mpc->ingress_lock = RW_LOCK_UNLOCKED;
271         mpc->egress_lock  = RW_LOCK_UNLOCKED;
272         mpc->next = mpcs;
273         atm_mpoa_init_cache(mpc);
274
275         mpc->parameters.mpc_p1 = MPC_P1;
276         mpc->parameters.mpc_p2 = MPC_P2;
277         memset(mpc->parameters.mpc_p3,0,sizeof(mpc->parameters.mpc_p3));
278         mpc->parameters.mpc_p4 = MPC_P4;
279         mpc->parameters.mpc_p5 = MPC_P5; 
280         mpc->parameters.mpc_p6 = MPC_P6;
281         
282         mpcs = mpc;
283         
284         return mpc;
285 }
286
287 /*
288  *
289  * start_mpc() puts the MPC on line. All the packets destined
290  * to the lec underneath us are now being monitored and 
291  * shortcuts will be established.
292  *
293  */
294 static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
295 {
296         
297         dprintk("mpoa: (%s) start_mpc:\n", mpc->dev->name); 
298         if (dev->hard_start_xmit == NULL) {
299                 printk("mpoa: (%s) start_mpc: dev->hard_start_xmit == NULL, not starting\n",
300                        dev->name);
301                 return;
302         }
303         mpc->old_hard_start_xmit = dev->hard_start_xmit;
304         dev->hard_start_xmit = mpc_send_packet;
305
306         return;
307 }
308
309 static void stop_mpc(struct mpoa_client *mpc)
310 {
311         
312         dprintk("mpoa: (%s) stop_mpc:", mpc->dev->name); 
313
314         /* Lets not nullify lec device's dev->hard_start_xmit */
315         if (mpc->dev->hard_start_xmit != mpc_send_packet) {
316                 dprintk(" mpc already stopped, not fatal\n");
317                 return;
318         }
319         dprintk("\n");
320         mpc->dev->hard_start_xmit = mpc->old_hard_start_xmit;
321         mpc->old_hard_start_xmit = NULL;
322         /* close_shortcuts(mpc);    ??? FIXME */
323         
324         return;
325 }
326
327 static const char * __attribute__ ((unused)) mpoa_device_type_string(char type)
328 {
329         switch(type) {
330         case NON_MPOA:
331                 return "non-MPOA device";
332                 break;
333         case MPS:
334                 return "MPS";
335                 break;
336         case MPC:
337                 return "MPC";
338                 break;
339         case MPS_AND_MPC:
340                 return "both MPS and MPC";
341                 break;
342         default:
343                 return "unspecified (non-MPOA) device";
344                 break;
345         }
346
347         return ""; /* not reached */
348 }
349
350 /*
351  * lec device calls this via its dev->priv->lane2_ops->associate_indicator()
352  * when it sees a TLV in LE_ARP packet.
353  * We fill in the pointer above when we see a LANE2 lec initializing
354  * See LANE2 spec 3.1.5
355  *
356  * Quite a big and ugly function but when you look at it
357  * all it does is to try to locate and parse MPOA Device
358  * Type TLV.
359  * We give our lec a pointer to this function and when the
360  * lec sees a TLV it uses the pointer to call this function.
361  *
362  */
363 static void lane2_assoc_ind(struct net_device *dev, uint8_t *mac_addr,
364                             uint8_t *tlvs, uint32_t sizeoftlvs)
365 {
366         uint32_t type;
367         uint8_t length, mpoa_device_type, number_of_mps_macs;
368         uint8_t *end_of_tlvs;
369         struct mpoa_client *mpc;
370         
371         mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
372         dprintk("mpoa: (%s) lane2_assoc_ind: received TLV(s), ", dev->name);
373         dprintk("total length of all TLVs %d\n", sizeoftlvs);
374         mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
375         if (mpc == NULL) {
376                 printk("mpoa: (%s) lane2_assoc_ind: no mpc\n", dev->name);
377                 return;
378         }
379         end_of_tlvs = tlvs + sizeoftlvs;
380         while (end_of_tlvs - tlvs >= 5) {
381                 type = (tlvs[0] << 24) | (tlvs[1] << 16) | (tlvs[2] << 8) | tlvs[3];
382                 length = tlvs[4];
383                 tlvs += 5;
384                 dprintk("    type 0x%x length %02x\n", type, length);
385                 if (tlvs + length > end_of_tlvs) {
386                         printk("TLV value extends past its buffer, aborting parse\n");
387                         return;
388                 }
389                 
390                 if (type == 0) {
391                         printk("mpoa: (%s) lane2_assoc_ind: TLV type was 0, returning\n", dev->name);
392                         return;
393                 }
394
395                 if (type != TLV_MPOA_DEVICE_TYPE) {
396                         tlvs += length;
397                         continue;  /* skip other TLVs */
398                 }
399                 mpoa_device_type = *tlvs++;
400                 number_of_mps_macs = *tlvs++;
401                 dprintk("mpoa: (%s) MPOA device type '%s', ", dev->name, mpoa_device_type_string(mpoa_device_type));
402                 if (mpoa_device_type == MPS_AND_MPC &&
403                     length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
404                         printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
405                                dev->name);
406                         continue;
407                 }
408                 if ((mpoa_device_type == MPS || mpoa_device_type == MPC)
409                     && length < 22 + number_of_mps_macs*ETH_ALEN) {
410                         printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
411                                 dev->name);
412                         continue;
413                 }
414                 if (mpoa_device_type != MPS && mpoa_device_type != MPS_AND_MPC) {
415                         dprintk("ignoring non-MPS device\n");
416                         if (mpoa_device_type == MPC) tlvs += 20;
417                         continue;  /* we are only interested in MPSs */
418                 }
419                 if (number_of_mps_macs == 0 && mpoa_device_type == MPS_AND_MPC) {
420                         printk("\nmpoa: (%s) lane2_assoc_ind: MPS_AND_MPC has zero MACs\n", dev->name);
421                         continue;  /* someone should read the spec */
422                 }
423                 dprintk("this MPS has %d MAC addresses\n", number_of_mps_macs);
424                 
425                 /* ok, now we can go and tell our daemon the control address of MPS */
426                 send_set_mps_ctrl_addr(tlvs, mpc);
427                 
428                 tlvs = copy_macs(mpc, mac_addr, tlvs, number_of_mps_macs, mpoa_device_type);
429                 if (tlvs == NULL) return;
430         }
431         if (end_of_tlvs - tlvs != 0)
432                 printk("mpoa: (%s) lane2_assoc_ind: ignoring %d bytes of trailing TLV carbage\n",
433                        dev->name, end_of_tlvs - tlvs);
434         return;
435 }
436
437 /*
438  * Store at least advertizing router's MAC address
439  * plus the possible MAC address(es) to mpc->mps_macs.
440  * For a freshly allocated MPOA client mpc->mps_macs == 0.
441  */
442 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
443                           uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type)
444 {
445         int num_macs;
446         num_macs = (mps_macs > 1) ? mps_macs : 1;
447
448         if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
449                 if (mpc->number_of_mps_macs != 0) kfree(mpc->mps_macs);
450                 mpc->number_of_mps_macs = 0;
451                 mpc->mps_macs = kmalloc(num_macs*ETH_ALEN, GFP_KERNEL);
452                 if (mpc->mps_macs == NULL) {
453                         printk("mpoa: (%s) copy_macs: out of mem\n", mpc->dev->name);
454                         return NULL;
455                 }
456         }
457         memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
458         tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
459         if (mps_macs > 0)
460                 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
461         tlvs += mps_macs*ETH_ALEN;
462         mpc->number_of_mps_macs = num_macs;
463
464         return tlvs;
465 }
466
467 static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
468 {
469         in_cache_entry *entry;
470         struct iphdr *iph;
471         char *buff;
472         uint32_t ipaddr = 0;
473
474         static struct {
475                 struct llc_snap_hdr hdr;
476                 uint32_t tag;
477         } tagged_llc_snap_hdr = {
478                 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
479                 0
480         };
481
482         buff = skb->data + mpc->dev->hard_header_len;
483         iph = (struct iphdr *)buff;
484         ipaddr = iph->daddr;
485
486         ddprintk("mpoa: (%s) send_via_shortcut: ipaddr 0x%x\n", mpc->dev->name, ipaddr);        
487
488         entry = mpc->in_ops->get(ipaddr, mpc);
489         if (entry == NULL) {
490                 entry = mpc->in_ops->add_entry(ipaddr, mpc);
491                 if (entry != NULL) mpc->in_ops->put(entry);
492                 return 1;
493         }
494         if (mpc->in_ops->cache_hit(entry, mpc) != OPEN){   /* threshold not exceeded or VCC not ready */
495                 ddprintk("mpoa: (%s) send_via_shortcut: cache_hit: returns != OPEN\n", mpc->dev->name);        
496                 mpc->in_ops->put(entry);
497                 return 1;
498         }
499
500         ddprintk("mpoa: (%s) send_via_shortcut: using shortcut\n", mpc->dev->name);        
501         /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
502         if (iph->ttl <= 1) {
503                 ddprintk("mpoa: (%s) send_via_shortcut: IP ttl = %u, using LANE\n", mpc->dev->name, iph->ttl);        
504                 mpc->in_ops->put(entry);
505                 return 1;
506         }
507         iph->ttl--;
508         iph->check = 0;
509         iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
510
511         if (entry->ctrl_info.tag != 0) {
512                 ddprintk("mpoa: (%s) send_via_shortcut: adding tag 0x%x\n", mpc->dev->name, entry->ctrl_info.tag);
513                 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
514                 skb_pull(skb, ETH_HLEN);                       /* get rid of Eth header */
515                 skb_push(skb, sizeof(tagged_llc_snap_hdr));    /* add LLC/SNAP header   */
516                 memcpy(skb->data, &tagged_llc_snap_hdr, sizeof(tagged_llc_snap_hdr));
517         } else {
518                 skb_pull(skb, ETH_HLEN);                        /* get rid of Eth header */
519                 skb_push(skb, sizeof(struct llc_snap_hdr));     /* add LLC/SNAP header + tag  */
520                 memcpy(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr));
521         }
522
523         atomic_add(skb->truesize, &entry->shortcut->tx_inuse);
524         ATM_SKB(skb)->iovcnt = 0; /* just to be safe ... */
525         ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
526         entry->shortcut->send(entry->shortcut, skb);
527         entry->packets_fwded++;
528         mpc->in_ops->put(entry);
529
530         return 0;
531 }
532
533 /*
534  * Probably needs some error checks and locking, not sure...
535  */
536 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev)
537 {
538         int retval;
539         struct mpoa_client *mpc;
540         struct ethhdr *eth;
541         int i = 0;
542         
543         mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
544         if(mpc == NULL) {
545                 printk("mpoa: (%s) mpc_send_packet: no MPC found\n", dev->name);
546                 goto non_ip;
547         }
548
549         eth = (struct ethhdr *)skb->data;
550         if (eth->h_proto != htons(ETH_P_IP))
551                 goto non_ip; /* Multi-Protocol Over ATM :-) */
552
553         while (i < mpc->number_of_mps_macs) {
554                 if (memcmp(eth->h_dest, (mpc->mps_macs + i*ETH_ALEN), ETH_ALEN) == 0)
555                         if ( send_via_shortcut(skb, mpc) == 0 )           /* try shortcut */
556                                 return 0;                                 /* success!     */
557                 i++;
558         }
559
560  non_ip:
561         retval = mpc->old_hard_start_xmit(skb,dev);
562         
563         return retval;
564 }
565
566 int atm_mpoa_vcc_attach(struct atm_vcc *vcc, long arg)
567 {
568         int bytes_left;
569         struct mpoa_client *mpc;
570         struct atmmpc_ioc ioc_data;
571         in_cache_entry *in_entry;
572         uint32_t  ipaddr;
573         unsigned char *ip;
574
575         bytes_left = copy_from_user(&ioc_data, (void *)arg, sizeof(struct atmmpc_ioc));
576         if (bytes_left != 0) {
577                 printk("mpoa: mpc_vcc_attach: Short read (missed %d bytes) from userland\n", bytes_left);
578                 return -EFAULT;
579         }
580         ipaddr = ioc_data.ipaddr;
581         if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
582                 return -EINVAL;
583         
584         mpc = find_mpc_by_itfnum(ioc_data.dev_num);
585         if (mpc == NULL)
586                 return -EINVAL;
587         
588         if (ioc_data.type == MPC_SOCKET_INGRESS) {
589                 in_entry = mpc->in_ops->get(ipaddr, mpc);
590                 if (in_entry == NULL || in_entry->entry_state < INGRESS_RESOLVED) {
591                         printk("mpoa: (%s) mpc_vcc_attach: did not find RESOLVED entry from ingress cache\n",
592                                 mpc->dev->name);
593                         if (in_entry != NULL) mpc->in_ops->put(in_entry);
594                         return -EINVAL;
595                 }
596                 ip = (unsigned char*)&in_entry->ctrl_info.in_dst_ip;
597                 printk("mpoa: (%s) mpc_vcc_attach: attaching ingress SVC, entry = %u.%u.%u.%u\n",
598                        mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
599                 in_entry->shortcut = vcc;
600                 mpc->in_ops->put(in_entry);
601         } else {
602                 printk("mpoa: (%s) mpc_vcc_attach: attaching egress SVC\n", mpc->dev->name);
603         }
604
605         vcc->proto_data = mpc->dev;
606         vcc->push = mpc_push;
607
608         return 0;
609 }
610
611 /*
612  *
613  */
614 static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
615 {
616         struct mpoa_client *mpc;
617         in_cache_entry *in_entry;
618         eg_cache_entry *eg_entry;
619         
620         mpc = find_mpc_by_lec(dev);
621         if (mpc == NULL) {
622                 printk("mpoa: (%s) mpc_vcc_close: close for unknown MPC\n", dev->name);
623                 return;
624         }
625
626         dprintk("mpoa: (%s) mpc_vcc_close:\n", dev->name);
627         in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
628         if (in_entry) {
629                 unsigned char *ip __attribute__ ((unused)) =
630                     (unsigned char *)&in_entry->ctrl_info.in_dst_ip;
631                 dprintk("mpoa: (%s) mpc_vcc_close: ingress SVC closed ip = %u.%u.%u.%u\n",
632                        mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
633                 in_entry->shortcut = NULL;
634                 mpc->in_ops->put(in_entry);
635         }
636         eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
637         if (eg_entry) {
638                 dprintk("mpoa: (%s) mpc_vcc_close: egress SVC closed\n", mpc->dev->name);
639                 eg_entry->shortcut = NULL;
640                 mpc->eg_ops->put(eg_entry);
641         }
642
643         if (in_entry == NULL && eg_entry == NULL)
644                 dprintk("mpoa: (%s) mpc_vcc_close:  unused vcc closed\n", dev->name);
645
646         return;
647 }
648
649 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
650 {
651         struct net_device *dev = (struct net_device *)vcc->proto_data;
652         struct sk_buff *new_skb;
653         eg_cache_entry *eg;
654         struct mpoa_client *mpc;
655         uint32_t tag;
656         char *tmp;
657         
658         ddprintk("mpoa: (%s) mpc_push:\n", dev->name);
659         if (skb == NULL) {
660                 dprintk("mpoa: (%s) mpc_push: null skb, closing VCC\n", dev->name);
661                 mpc_vcc_close(vcc, dev);
662                 return;
663         }
664         
665         skb->dev = dev;
666         if (memcmp(skb->data, &llc_snap_mpoa_ctrl, sizeof(struct llc_snap_hdr)) == 0) {
667                 dprintk("mpoa: (%s) mpc_push: control packet arrived\n", dev->name);
668                 skb_queue_tail(&vcc->recvq, skb);           /* Pass control packets to daemon */
669                 wake_up(&vcc->sleep);
670                 return;
671         }
672
673         /* data coming over the shortcut */
674         atm_return(vcc, skb->truesize);
675
676         mpc = find_mpc_by_lec(dev);
677         if (mpc == NULL) {
678                 printk("mpoa: (%s) mpc_push: unknown MPC\n", dev->name);
679                 return;
680         }
681
682         if (memcmp(skb->data, &llc_snap_mpoa_data_tagged, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
683                 ddprintk("mpoa: (%s) mpc_push: tagged data packet arrived\n", dev->name);
684
685         } else if (memcmp(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
686                 printk("mpoa: (%s) mpc_push: non-tagged data packet arrived\n", dev->name);
687                 printk("           mpc_push: non-tagged data unsupported, purging\n");
688                 dev_kfree_skb_any(skb);
689                 return;
690         } else {
691                 printk("mpoa: (%s) mpc_push: garbage arrived, purging\n", dev->name);
692                 dev_kfree_skb_any(skb);
693                 return;
694         }
695
696         tmp = skb->data + sizeof(struct llc_snap_hdr);
697         tag = *(uint32_t *)tmp;
698
699         eg = mpc->eg_ops->get_by_tag(tag, mpc);
700         if (eg == NULL) {
701                 printk("mpoa: (%s) mpc_push: Didn't find egress cache entry, tag = %u\n",
702                        dev->name,tag);
703                 purge_egress_shortcut(vcc, NULL);
704                 dev_kfree_skb_any(skb);
705                 return;
706         }
707         
708         /*
709          * See if ingress MPC is using shortcut we opened as a return channel.
710          * This means we have a bi-directional vcc opened by us.
711          */ 
712         if (eg->shortcut == NULL) {
713                 eg->shortcut = vcc;
714                 printk("mpoa: (%s) mpc_push: egress SVC in use\n", dev->name);
715         }
716
717         skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag)); /* get rid of LLC/SNAP header */
718         new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length); /* LLC/SNAP is shorter than MAC header :( */
719         dev_kfree_skb_any(skb);
720         if (new_skb == NULL){
721                 mpc->eg_ops->put(eg);
722                 return;
723         }
724         skb_push(new_skb, eg->ctrl_info.DH_length);     /* add MAC header */
725         memcpy(new_skb->data, eg->ctrl_info.DLL_header, eg->ctrl_info.DH_length);
726         new_skb->protocol = eth_type_trans(new_skb, dev);
727         new_skb->nh.raw = new_skb->data;
728
729         eg->latest_ip_addr = new_skb->nh.iph->saddr;
730         eg->packets_rcvd++;
731         mpc->eg_ops->put(eg);
732
733         netif_rx(new_skb);
734
735         return;
736 }
737
738 static struct atmdev_ops mpc_ops = { /* only send is required */
739         close:  mpoad_close,
740         send:   msg_from_mpoad
741 };
742
743 static struct atm_dev mpc_dev = {
744         &mpc_ops,       /* device operations    */
745         NULL,           /* PHY operations       */
746         "mpc",          /* device type name     */
747         42,             /* device index (dummy) */
748         NULL,           /* VCC table            */
749         NULL,           /* last VCC             */
750         NULL,           /* per-device data      */
751         NULL,           /* private PHY data     */
752         { 0 },          /* device flags         */
753         NULL,           /* local ATM address    */
754         { 0 }           /* no ESI               */
755         /* rest of the members will be 0 */
756 };
757
758 int atm_mpoa_mpoad_attach (struct atm_vcc *vcc, int arg)
759 {
760         struct mpoa_client *mpc;
761         struct lec_priv *priv;
762         
763         if (mpcs == NULL) {
764                 init_timer(&mpc_timer);
765                 mpc_timer_refresh();
766
767                 /* This lets us now how our LECs are doing */
768                 register_netdevice_notifier(&mpoa_notifier);
769         }
770         
771         mpc = find_mpc_by_itfnum(arg);
772         if (mpc == NULL) {
773                 dprintk("mpoa: mpoad_attach: allocating new mpc for itf %d\n", arg);
774                 mpc = alloc_mpc();
775                 mpc->dev_num = arg;
776                 mpc->dev = find_lec_by_itfnum(arg); /* NULL if there was no lec */
777         }
778         if (mpc->mpoad_vcc) {
779                 printk("mpoa: mpoad_attach: mpoad is already present for itf %d\n", arg);
780                 return -EADDRINUSE;
781         }
782
783         if (mpc->dev) { /* check if the lec is LANE2 capable */
784                 priv = (struct lec_priv *)mpc->dev->priv;
785                 if (priv->lane_version < 2)
786                         mpc->dev = NULL;
787                 else
788                         priv->lane2_ops->associate_indicator = lane2_assoc_ind;  
789         }
790
791         mpc->mpoad_vcc = vcc;
792         bind_vcc(vcc, &mpc_dev);
793         set_bit(ATM_VF_META,&vcc->flags);
794         set_bit(ATM_VF_READY,&vcc->flags);
795
796         if (mpc->dev) {
797                 char empty[ATM_ESA_LEN];
798                 memset(empty, 0, ATM_ESA_LEN);
799                 
800                 start_mpc(mpc, mpc->dev);
801                 /* set address if mpcd e.g. gets killed and restarted.
802                  * If we do not do it now we have to wait for the next LE_ARP
803                  */
804                 if ( memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0 )
805                         send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
806         }
807
808         MOD_INC_USE_COUNT;
809         return arg;
810 }
811
812 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc)
813 {
814         struct k_message mesg;
815
816         memcpy (mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
817         
818         mesg.type = SET_MPS_CTRL_ADDR;
819         memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
820         msg_to_mpoad(&mesg, mpc);
821
822         return;
823 }
824
825 static void mpoad_close(struct atm_vcc *vcc)
826 {
827         struct mpoa_client *mpc;
828         struct sk_buff *skb;
829
830         mpc = find_mpc_by_vcc(vcc);
831         if (mpc == NULL) {
832                 printk("mpoa: mpoad_close: did not find MPC\n");
833                 return;
834         }
835         if (!mpc->mpoad_vcc) {
836                 printk("mpoa: mpoad_close: close for non-present mpoad\n");
837                 return;
838         }
839         
840         mpc->mpoad_vcc = NULL;
841         if (mpc->dev) {
842                 struct lec_priv *priv = (struct lec_priv *)mpc->dev->priv;
843                 priv->lane2_ops->associate_indicator = NULL;
844                 stop_mpc(mpc);
845         }
846
847         mpc->in_ops->destroy_cache(mpc);
848         mpc->eg_ops->destroy_cache(mpc);
849
850         while ( (skb = skb_dequeue(&vcc->recvq)) ){
851                 atm_return(vcc, skb->truesize);
852                 kfree_skb(skb);
853         }
854         
855         printk("mpoa: (%s) going down\n",
856                 (mpc->dev) ? mpc->dev->name : "<unknown>");
857         MOD_DEC_USE_COUNT;
858
859         return;
860 }
861
862 /*
863  *
864  */
865 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
866 {
867         
868         struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
869         struct k_message *mesg = (struct k_message*)skb->data;
870         atomic_sub(skb->truesize+ATM_PDU_OVHD, &vcc->tx_inuse);
871         
872         if (mpc == NULL) {
873                 printk("mpoa: msg_from_mpoad: no mpc found\n");
874                 return 0;
875         }
876         dprintk("mpoa: (%s) msg_from_mpoad:", (mpc->dev) ? mpc->dev->name : "<unknown>");
877         switch(mesg->type) {
878         case MPOA_RES_REPLY_RCVD:
879                 dprintk(" mpoa_res_reply_rcvd\n");
880                 MPOA_res_reply_rcvd(mesg, mpc);
881                 break;
882         case MPOA_TRIGGER_RCVD:
883                 dprintk(" mpoa_trigger_rcvd\n");
884                 MPOA_trigger_rcvd(mesg, mpc);
885                 break;
886         case INGRESS_PURGE_RCVD:
887                 dprintk(" nhrp_purge_rcvd\n");
888                 ingress_purge_rcvd(mesg, mpc);
889                 break;
890         case EGRESS_PURGE_RCVD:
891                 dprintk(" egress_purge_reply_rcvd\n");
892                 egress_purge_rcvd(mesg, mpc);
893                 break;
894         case MPS_DEATH:
895                 dprintk(" mps_death\n");
896                 mps_death(mesg, mpc);
897                 break;
898         case CACHE_IMPOS_RCVD:
899                 dprintk(" cache_impos_rcvd\n");
900                 MPOA_cache_impos_rcvd(mesg, mpc);
901                 break;
902         case SET_MPC_CTRL_ADDR:
903                 dprintk(" set_mpc_ctrl_addr\n");
904                 set_mpc_ctrl_addr_rcvd(mesg, mpc);
905                 break;
906         case SET_MPS_MAC_ADDR:
907                 dprintk(" set_mps_mac_addr\n");
908                 set_mps_mac_addr_rcvd(mesg, mpc);
909                 break;
910         case CLEAN_UP_AND_EXIT:
911                 dprintk(" clean_up_and_exit\n");
912                 clean_up(mesg, mpc, DIE);
913                 break;
914         case RELOAD:
915                 dprintk(" reload\n");
916                 clean_up(mesg, mpc, RELOAD);
917                 break;
918         case SET_MPC_PARAMS:
919                 dprintk(" set_mpc_params\n");
920                 mpc->parameters = mesg->content.params;
921                 break;
922         default:
923                 dprintk(" unknown message %d\n", mesg->type);
924                 break;
925         }
926         kfree_skb(skb);
927
928         return 0;
929 }
930
931 /* Remember that this function may not do things that sleep */
932 int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
933 {
934         struct sk_buff *skb;
935
936         if (mpc == NULL || !mpc->mpoad_vcc) {
937                 printk("mpoa: msg_to_mpoad: mesg %d to a non-existent mpoad\n", mesg->type);
938                 return -ENXIO;
939         }
940
941         skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
942         if (skb == NULL)
943                 return -ENOMEM;
944         skb_put(skb, sizeof(struct k_message));
945         memcpy(skb->data, mesg, sizeof(struct k_message));
946         atm_force_charge(mpc->mpoad_vcc, skb->truesize);
947         skb_queue_tail(&mpc->mpoad_vcc->recvq, skb);
948         wake_up(&mpc->mpoad_vcc->sleep);
949
950         return 0;
951 }
952
953 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev_ptr)
954 {
955         struct net_device *dev;
956         struct mpoa_client *mpc;
957         struct lec_priv *priv;
958
959         dev = (struct net_device *)dev_ptr;
960         if (dev->name == NULL || strncmp(dev->name, "lec", 3))
961                 return NOTIFY_DONE; /* we are only interested in lec:s */
962         
963         switch (event) {
964         case NETDEV_REGISTER:       /* a new lec device was allocated */
965                 priv = (struct lec_priv *)dev->priv;
966                 if (priv->lane_version < 2)
967                         break;
968                 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
969                 mpc = find_mpc_by_itfnum(priv->itfnum);
970                 if (mpc == NULL) {
971                         dprintk("mpoa: mpoa_event_listener: allocating new mpc for %s\n",
972                                dev->name);
973                         mpc = alloc_mpc();
974                         if (mpc == NULL) {
975                                 printk("mpoa: mpoa_event_listener: no new mpc");
976                                 break;
977                         }
978                 }
979                 mpc->dev_num = priv->itfnum;
980                 mpc->dev = dev;
981                 dprintk("mpoa: (%s) was initialized\n", dev->name);
982                 break;
983         case NETDEV_UNREGISTER:
984                 /* the lec device was deallocated */
985                 mpc = find_mpc_by_lec(dev);
986                 if (mpc == NULL)
987                         break;
988                 dprintk("mpoa: device (%s) was deallocated\n", dev->name);
989                 stop_mpc(mpc);
990                 mpc->dev = NULL;
991                 break;
992         case NETDEV_UP:
993                 /* the dev was ifconfig'ed up */
994                 mpc = find_mpc_by_lec(dev);
995                 if (mpc == NULL)
996                         break;
997                 if (mpc->mpoad_vcc != NULL) {
998                         start_mpc(mpc, dev);
999                 }
1000                 break;
1001         case NETDEV_DOWN:
1002                 /* the dev was ifconfig'ed down */
1003                 /* this means that the flow of packets from the
1004                  * upper layer stops
1005                  */
1006                 mpc = find_mpc_by_lec(dev);
1007                 if (mpc == NULL)
1008                         break;
1009                 if (mpc->mpoad_vcc != NULL) {
1010                         stop_mpc(mpc);
1011                 }
1012                 break;
1013         case NETDEV_REBOOT:
1014         case NETDEV_CHANGE:
1015         case NETDEV_CHANGEMTU:
1016         case NETDEV_CHANGEADDR:
1017         case NETDEV_GOING_DOWN:
1018                 break;
1019         default:
1020                 break;
1021         }
1022
1023         return NOTIFY_DONE;
1024 }
1025
1026 /*
1027  * Functions which are called after a message is received from mpcd.
1028  * Msg is reused on purpose.
1029  */
1030
1031
1032 static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1033 {
1034         uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1035         in_cache_entry *entry;
1036
1037         entry = mpc->in_ops->get(dst_ip, mpc);
1038         if(entry == NULL){
1039                 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1040                 entry->entry_state = INGRESS_RESOLVING;
1041                 msg->type = SND_MPOA_RES_RQST;
1042                 msg->content.in_info = entry->ctrl_info;
1043                 msg_to_mpoad(msg, mpc);
1044                 do_gettimeofday(&(entry->reply_wait));
1045                 mpc->in_ops->put(entry);
1046                 return;
1047         }
1048         
1049         if(entry->entry_state == INGRESS_INVALID){
1050                 entry->entry_state = INGRESS_RESOLVING;
1051                 msg->type = SND_MPOA_RES_RQST;
1052                 msg->content.in_info = entry->ctrl_info;
1053                 msg_to_mpoad(msg, mpc);
1054                 do_gettimeofday(&(entry->reply_wait));
1055                 mpc->in_ops->put(entry);
1056                 return;
1057         }
1058         
1059         printk("mpoa: (%s) MPOA_trigger_rcvd: entry already in resolving state\n",
1060                 (mpc->dev) ? mpc->dev->name : "<unknown>");
1061         mpc->in_ops->put(entry);
1062         return;
1063 }
1064
1065 /*
1066  * Things get complicated because we have to check if there's an egress
1067  * shortcut with suitable traffic parameters we could use. 
1068  */
1069 static void check_qos_and_open_shortcut(struct k_message *msg, struct mpoa_client *client, in_cache_entry *entry)
1070 {
1071         uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1072         unsigned char *ip __attribute__ ((unused)) = (unsigned char *)&dst_ip;
1073         struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1074         eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1075
1076         if(eg_entry && eg_entry->shortcut){
1077                 if(eg_entry->shortcut->qos.txtp.traffic_class &
1078                    msg->qos.txtp.traffic_class &
1079                    (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)){
1080                             if(eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1081                                     entry->shortcut = eg_entry->shortcut;
1082                             else if(eg_entry->shortcut->qos.txtp.max_pcr > 0)
1083                                     entry->shortcut = eg_entry->shortcut;
1084                 }
1085                 if(entry->shortcut){
1086                         dprintk("mpoa: (%s) using egress SVC to reach %u.%u.%u.%u\n",client->dev->name, NIPQUAD(ip));
1087                         client->eg_ops->put(eg_entry);
1088                         return;
1089                 }
1090         }
1091         if (eg_entry != NULL)
1092                 client->eg_ops->put(eg_entry);
1093
1094         /* No luck in the egress cache we must open an ingress SVC */
1095         msg->type = OPEN_INGRESS_SVC;
1096         if (qos && (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class))
1097         {
1098                 msg->qos = qos->qos;
1099                 printk("mpoa: (%s) trying to get a CBR shortcut\n",client->dev->name);
1100         }
1101         else memset(&msg->qos,0,sizeof(struct atm_qos));
1102         msg_to_mpoad(msg, client);
1103         return;
1104 }
1105
1106 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1107 {
1108         unsigned char *ip;
1109
1110         uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1111         in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
1112         ip = (unsigned char *)&dst_ip;
1113         dprintk("mpoa: (%s) MPOA_res_reply_rcvd: ip %u.%u.%u.%u\n", mpc->dev->name, NIPQUAD(ip));
1114         ddprintk("mpoa: (%s) MPOA_res_reply_rcvd() entry = %p", mpc->dev->name, entry);
1115         if(entry == NULL){
1116                 printk("\nmpoa: (%s) ARGH, received res. reply for an entry that doesn't exist.\n", mpc->dev->name);
1117                 return;
1118         }
1119         ddprintk(" entry_state = %d ", entry->entry_state);     
1120
1121         if (entry->entry_state == INGRESS_RESOLVED) {
1122                 printk("\nmpoa: (%s) MPOA_res_reply_rcvd for RESOLVED entry!\n", mpc->dev->name);
1123                 mpc->in_ops->put(entry);
1124                 return;
1125         }
1126
1127         entry->ctrl_info = msg->content.in_info;
1128         do_gettimeofday(&(entry->tv));
1129         do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
1130         entry->refresh_time = 0;
1131         ddprintk("entry->shortcut = %p\n", entry->shortcut);
1132
1133         if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL){
1134                 entry->entry_state = INGRESS_RESOLVED; 
1135                 mpc->in_ops->put(entry);
1136                 return; /* Shortcut already open... */
1137         }
1138
1139         if (entry->shortcut != NULL) {
1140                 printk("mpoa: (%s) MPOA_res_reply_rcvd: entry->shortcut != NULL, impossible!\n",
1141                        mpc->dev->name);
1142                 mpc->in_ops->put(entry);
1143                 return;
1144         }
1145         
1146         check_qos_and_open_shortcut(msg, mpc, entry);
1147         entry->entry_state = INGRESS_RESOLVED;
1148         mpc->in_ops->put(entry);
1149
1150         return;
1151
1152 }
1153
1154 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1155 {
1156         uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1157         uint32_t mask = msg->ip_mask;
1158         unsigned char *ip = (unsigned char *)&dst_ip;
1159         in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1160
1161         if(entry == NULL){
1162                 printk("mpoa: (%s) ingress_purge_rcvd: purge for a non-existing entry, ", mpc->dev->name);
1163                 printk("ip = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
1164                 return;
1165         }
1166
1167         do {
1168                 dprintk("mpoa: (%s) ingress_purge_rcvd: removing an ingress entry, ip = %u.%u.%u.%u\n" ,
1169                         mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
1170                 write_lock_bh(&mpc->ingress_lock);
1171                 mpc->in_ops->remove_entry(entry, mpc);
1172                 write_unlock_bh(&mpc->ingress_lock);
1173                 mpc->in_ops->put(entry);
1174                 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1175         } while (entry != NULL);
1176
1177         return;
1178
1179
1180 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1181 {
1182         uint32_t cache_id = msg->content.eg_info.cache_id;
1183         eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
1184         
1185         if (entry == NULL) {
1186                 dprintk("mpoa: (%s) egress_purge_rcvd: purge for a non-existing entry\n", mpc->dev->name);
1187                 return;
1188         }
1189
1190         write_lock_irq(&mpc->egress_lock);
1191         mpc->eg_ops->remove_entry(entry, mpc);
1192         write_unlock_irq(&mpc->egress_lock);
1193
1194         mpc->eg_ops->put(entry);
1195
1196         return;
1197
1198
1199 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1200 {
1201         struct k_message *purge_msg;
1202         struct sk_buff *skb;
1203
1204         dprintk("mpoa: purge_egress_shortcut: entering\n");
1205         if (vcc == NULL) {
1206                 printk("mpoa: purge_egress_shortcut: vcc == NULL\n");
1207                 return;
1208         }
1209
1210         skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1211         if (skb == NULL) {
1212                  printk("mpoa: purge_egress_shortcut: out of memory\n");
1213                 return;
1214         }
1215
1216         skb_put(skb, sizeof(struct k_message));
1217         memset(skb->data, 0, sizeof(struct k_message));
1218         purge_msg = (struct k_message *)skb->data;
1219         purge_msg->type = DATA_PLANE_PURGE;
1220         if (entry != NULL)
1221                 purge_msg->content.eg_info = entry->ctrl_info;
1222
1223         atm_force_charge(vcc, skb->truesize);
1224         skb_queue_tail(&vcc->recvq, skb);
1225         wake_up(&vcc->sleep);
1226         dprintk("mpoa: purge_egress_shortcut: exiting:\n");
1227
1228         return;
1229 }
1230
1231 /*
1232  * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1233  * of the egress shortcuts we have.
1234  */
1235 static void mps_death( struct k_message * msg, struct mpoa_client * mpc )
1236 {
1237         eg_cache_entry *entry;
1238
1239         dprintk("mpoa: (%s) mps_death:\n", mpc->dev->name);
1240
1241         if(memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)){
1242                 printk("mpoa: (%s) mps_death: wrong MPS\n", mpc->dev->name);
1243                 return;
1244         }
1245
1246         /* FIXME: This knows too much of the cache structure */
1247         read_lock_irq(&mpc->egress_lock);
1248         entry = mpc->eg_cache;
1249         while (entry != NULL) {
1250                 purge_egress_shortcut(entry->shortcut, entry);
1251                 entry = entry->next;
1252         }
1253         read_unlock_irq(&mpc->egress_lock);
1254
1255         mpc->in_ops->destroy_cache(mpc);
1256         mpc->eg_ops->destroy_cache(mpc);
1257
1258         return;
1259 }
1260
1261 static void MPOA_cache_impos_rcvd( struct k_message * msg, struct mpoa_client * mpc)
1262 {
1263         uint16_t holding_time;
1264         eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
1265         
1266         holding_time = msg->content.eg_info.holding_time;
1267         dprintk("mpoa: (%s) MPOA_cache_impos_rcvd: entry = %p, holding_time = %u\n",
1268                mpc->dev->name, entry, holding_time);
1269         if(entry == NULL && holding_time) {
1270                 entry = mpc->eg_ops->add_entry(msg, mpc);
1271                 mpc->eg_ops->put(entry);
1272                 return;
1273         }
1274         if(holding_time){
1275                 mpc->eg_ops->update(entry, holding_time);
1276                 return;
1277         }
1278         
1279         write_lock_irq(&mpc->egress_lock);
1280         mpc->eg_ops->remove_entry(entry, mpc);
1281         write_unlock_irq(&mpc->egress_lock);
1282
1283         mpc->eg_ops->put(entry);
1284         
1285         return;
1286 }
1287
1288 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc)
1289 {
1290         struct lec_priv *priv;
1291         int i, retval ;
1292
1293         uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1294
1295         tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type  */
1296         tlv[4] = 1 + 1 + ATM_ESA_LEN;  /* length                           */
1297         tlv[5] = 0x02;                 /* MPOA client                      */
1298         tlv[6] = 0x00;                 /* number of MPS MAC addresses      */
1299
1300         memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1301         memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1302
1303         dprintk("mpoa: (%s) setting MPC ctrl ATM address to ",
1304                (mpc->dev) ? mpc->dev->name : "<unknown>");
1305         for (i = 7; i < sizeof(tlv); i++)
1306                 dprintk("%02x ", tlv[i]);
1307         dprintk("\n");
1308
1309         if (mpc->dev) {
1310                 priv = (struct lec_priv *)mpc->dev->priv;
1311                 retval = priv->lane2_ops->associate_req(mpc->dev, mpc->dev->dev_addr, tlv, sizeof(tlv));
1312                 if (retval == 0)
1313                         printk("mpoa: (%s) MPOA device type TLV association failed\n", mpc->dev->name);
1314                 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1315                 if (retval < 0)
1316                         printk("mpoa: (%s) targetless LE_ARP request failed\n", mpc->dev->name);
1317         }
1318
1319         return;
1320 }
1321
1322 static void set_mps_mac_addr_rcvd(struct k_message *msg, struct mpoa_client *client)
1323 {
1324
1325         if(client->number_of_mps_macs)
1326                 kfree(client->mps_macs);
1327         client->number_of_mps_macs = 0;
1328         client->mps_macs = kmalloc(ETH_ALEN,GFP_KERNEL);
1329         if (client->mps_macs == NULL) {
1330                 printk("mpoa: set_mps_mac_addr_rcvd: out of memory\n");
1331                 return;
1332         }
1333         client->number_of_mps_macs = 1;
1334         memcpy(client->mps_macs, msg->MPS_ctrl, ETH_ALEN);
1335         
1336         return;
1337 }
1338
1339 /*
1340  * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1341  */
1342 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1343 {
1344
1345         eg_cache_entry *entry;
1346         msg->type = SND_EGRESS_PURGE;
1347
1348
1349         /* FIXME: This knows too much of the cache structure */
1350         read_lock_irq(&mpc->egress_lock);
1351         entry = mpc->eg_cache;
1352         while (entry != NULL){
1353                     msg->content.eg_info = entry->ctrl_info;
1354                     dprintk("mpoa: cache_id %u\n", entry->ctrl_info.cache_id);
1355                     msg_to_mpoad(msg, mpc);
1356                     entry = entry->next;
1357         }
1358         read_unlock_irq(&mpc->egress_lock);
1359
1360         msg->type = action;
1361         msg_to_mpoad(msg, mpc);
1362         return;
1363 }
1364
1365 static void mpc_timer_refresh()
1366 {
1367         mpc_timer.expires = jiffies + (MPC_P2 * HZ);
1368         mpc_timer.data = mpc_timer.expires;
1369         mpc_timer.function = mpc_cache_check;
1370         add_timer(&mpc_timer);
1371         
1372         return;
1373 }
1374
1375 static void mpc_cache_check( unsigned long checking_time  )
1376 {
1377         struct mpoa_client *mpc = mpcs;
1378         static unsigned long previous_resolving_check_time = 0;
1379         static unsigned long previous_refresh_time = 0;
1380         
1381         while( mpc != NULL ){
1382                 mpc->in_ops->clear_count(mpc);
1383                 mpc->eg_ops->clear_expired(mpc);
1384                 if(checking_time - previous_resolving_check_time > mpc->parameters.mpc_p4 * HZ ){
1385                         mpc->in_ops->check_resolving(mpc);
1386                         previous_resolving_check_time = checking_time;
1387                 }
1388                 if(checking_time - previous_refresh_time > mpc->parameters.mpc_p5 * HZ ){
1389                         mpc->in_ops->refresh(mpc);
1390                         previous_refresh_time = checking_time;
1391                 }
1392                 mpc = mpc->next;
1393         }
1394         mpc_timer_refresh();
1395         
1396         return;
1397 }
1398
1399 void atm_mpoa_init_ops(struct atm_mpoa_ops *ops)
1400 {
1401         ops->mpoad_attach = atm_mpoa_mpoad_attach;
1402         ops->vcc_attach = atm_mpoa_vcc_attach;
1403
1404 #ifdef CONFIG_PROC_FS
1405         if(mpc_proc_init() != 0)
1406                 printk(KERN_INFO "mpoa: failed to initialize /proc/mpoa\n");
1407         else
1408                 printk(KERN_INFO "mpoa: /proc/mpoa initialized\n");
1409 #endif
1410
1411         printk("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
1412
1413         return;
1414 }
1415
1416 #ifdef MODULE
1417 int init_module(void)
1418 {
1419         extern struct atm_mpoa_ops atm_mpoa_ops;
1420
1421         atm_mpoa_init_ops(&atm_mpoa_ops);
1422
1423         return 0;
1424 }
1425
1426 void cleanup_module(void)
1427 {
1428         extern struct atm_mpoa_ops atm_mpoa_ops;
1429         struct mpoa_client *mpc, *tmp;
1430         struct atm_mpoa_qos *qos, *nextqos;
1431         struct lec_priv *priv;
1432
1433         if (MOD_IN_USE) {
1434                 printk("mpc.c: module in use\n");
1435                 return;
1436         }
1437 #ifdef CONFIG_PROC_FS
1438         mpc_proc_clean();
1439 #endif
1440
1441         del_timer(&mpc_timer);
1442         unregister_netdevice_notifier(&mpoa_notifier);
1443         atm_mpoa_ops.mpoad_attach = NULL;
1444         atm_mpoa_ops.vcc_attach = NULL;
1445
1446         mpc = mpcs;
1447         mpcs = NULL;
1448         while (mpc != NULL) {
1449                 tmp = mpc->next;
1450                 if (mpc->dev != NULL) {
1451                         stop_mpc(mpc);
1452                         priv = (struct lec_priv *)mpc->dev->priv;
1453                         if (priv->lane2_ops != NULL)
1454                                 priv->lane2_ops->associate_indicator = NULL;
1455                 }
1456                 ddprintk("mpoa: cleanup_module: about to clear caches\n");
1457                 mpc->in_ops->destroy_cache(mpc);
1458                 mpc->eg_ops->destroy_cache(mpc);
1459                 ddprintk("mpoa: cleanup_module: caches cleared\n");
1460                 kfree(mpc->mps_macs);
1461                 memset(mpc, 0, sizeof(struct mpoa_client));
1462                 ddprintk("mpoa: cleanup_module: about to kfree %p\n", mpc);
1463                 kfree(mpc);
1464                 ddprintk("mpoa: cleanup_module: next mpc is at %p\n", tmp);
1465                 mpc = tmp;
1466         }
1467
1468         qos = qos_head;
1469         qos_head = NULL;
1470         while (qos != NULL) {
1471                 nextqos = qos->next;
1472                 dprintk("mpoa: cleanup_module: freeing qos entry %p\n", qos);
1473                 kfree(qos);
1474                 qos = nextqos;
1475         }
1476
1477         return;
1478 }
1479 #endif /* MODULE */