Bluetooth: Convert debug files to actually use debugfs instead of sysfs
[pandora-kernel.git] / net / atm / mpoa_caches.c
1 #include <linux/types.h>
2 #include <linux/atmmpc.h>
3 #include <linux/time.h>
4
5 #include "mpoa_caches.h"
6 #include "mpc.h"
7
8 /*
9  * mpoa_caches.c: Implementation of ingress and egress cache
10  * handling functions
11  */
12
13 #if 0
14 #define dprintk(format, args...)                                        \
15         printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args)  /* debug */
16 #else
17 #define dprintk(format, args...)                                        \
18         do { if (0)                                                     \
19                 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
20         } while (0)
21 #endif
22
23 #if 0
24 #define ddprintk(format, args...)                                       \
25         printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args)  /* debug */
26 #else
27 #define ddprintk(format, args...)                                       \
28         do { if (0)                                                     \
29                 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
30         } while (0)
31 #endif
32
33 static in_cache_entry *in_cache_get(__be32 dst_ip,
34                                     struct mpoa_client *client)
35 {
36         in_cache_entry *entry;
37
38         read_lock_bh(&client->ingress_lock);
39         entry = client->in_cache;
40         while (entry != NULL) {
41                 if (entry->ctrl_info.in_dst_ip == dst_ip) {
42                         atomic_inc(&entry->use);
43                         read_unlock_bh(&client->ingress_lock);
44                         return entry;
45                 }
46                 entry = entry->next;
47         }
48         read_unlock_bh(&client->ingress_lock);
49
50         return NULL;
51 }
52
53 static in_cache_entry *in_cache_get_with_mask(__be32 dst_ip,
54                                               struct mpoa_client *client,
55                                               __be32 mask)
56 {
57         in_cache_entry *entry;
58
59         read_lock_bh(&client->ingress_lock);
60         entry = client->in_cache;
61         while (entry != NULL) {
62                 if ((entry->ctrl_info.in_dst_ip & mask) == (dst_ip & mask)) {
63                         atomic_inc(&entry->use);
64                         read_unlock_bh(&client->ingress_lock);
65                         return entry;
66                 }
67                 entry = entry->next;
68         }
69         read_unlock_bh(&client->ingress_lock);
70
71         return NULL;
72
73 }
74
75 static in_cache_entry *in_cache_get_by_vcc(struct atm_vcc *vcc,
76                                            struct mpoa_client *client)
77 {
78         in_cache_entry *entry;
79
80         read_lock_bh(&client->ingress_lock);
81         entry = client->in_cache;
82         while (entry != NULL) {
83                 if (entry->shortcut == vcc) {
84                         atomic_inc(&entry->use);
85                         read_unlock_bh(&client->ingress_lock);
86                         return entry;
87                 }
88                 entry = entry->next;
89         }
90         read_unlock_bh(&client->ingress_lock);
91
92         return NULL;
93 }
94
95 static in_cache_entry *in_cache_add_entry(__be32 dst_ip,
96                                           struct mpoa_client *client)
97 {
98         in_cache_entry *entry = kzalloc(sizeof(in_cache_entry), GFP_KERNEL);
99
100         if (entry == NULL) {
101                 pr_info("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n");
102                 return NULL;
103         }
104
105         dprintk("adding an ingress entry, ip = %pI4\n", &dst_ip);
106
107         atomic_set(&entry->use, 1);
108         dprintk("new_in_cache_entry: about to lock\n");
109         write_lock_bh(&client->ingress_lock);
110         entry->next = client->in_cache;
111         entry->prev = NULL;
112         if (client->in_cache != NULL)
113                 client->in_cache->prev = entry;
114         client->in_cache = entry;
115
116         memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
117         entry->ctrl_info.in_dst_ip = dst_ip;
118         do_gettimeofday(&(entry->tv));
119         entry->retry_time = client->parameters.mpc_p4;
120         entry->count = 1;
121         entry->entry_state = INGRESS_INVALID;
122         entry->ctrl_info.holding_time = HOLDING_TIME_DEFAULT;
123         atomic_inc(&entry->use);
124
125         write_unlock_bh(&client->ingress_lock);
126         dprintk("new_in_cache_entry: unlocked\n");
127
128         return entry;
129 }
130
131 static int cache_hit(in_cache_entry *entry, struct mpoa_client *mpc)
132 {
133         struct atm_mpoa_qos *qos;
134         struct k_message msg;
135
136         entry->count++;
137         if (entry->entry_state == INGRESS_RESOLVED && entry->shortcut != NULL)
138                 return OPEN;
139
140         if (entry->entry_state == INGRESS_REFRESHING) {
141                 if (entry->count > mpc->parameters.mpc_p1) {
142                         msg.type = SND_MPOA_RES_RQST;
143                         msg.content.in_info = entry->ctrl_info;
144                         memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
145                         qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
146                         if (qos != NULL)
147                                 msg.qos = qos->qos;
148                         msg_to_mpoad(&msg, mpc);
149                         do_gettimeofday(&(entry->reply_wait));
150                         entry->entry_state = INGRESS_RESOLVING;
151                 }
152                 if (entry->shortcut != NULL)
153                         return OPEN;
154                 return CLOSED;
155         }
156
157         if (entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL)
158                 return OPEN;
159
160         if (entry->count > mpc->parameters.mpc_p1 &&
161             entry->entry_state == INGRESS_INVALID) {
162                 dprintk("(%s) threshold exceeded for ip %pI4, sending MPOA res req\n",
163                         mpc->dev->name, &entry->ctrl_info.in_dst_ip);
164                 entry->entry_state = INGRESS_RESOLVING;
165                 msg.type = SND_MPOA_RES_RQST;
166                 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
167                 msg.content.in_info = entry->ctrl_info;
168                 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
169                 if (qos != NULL)
170                         msg.qos = qos->qos;
171                 msg_to_mpoad(&msg, mpc);
172                 do_gettimeofday(&(entry->reply_wait));
173         }
174
175         return CLOSED;
176 }
177
178 static void in_cache_put(in_cache_entry *entry)
179 {
180         if (atomic_dec_and_test(&entry->use)) {
181                 memset(entry, 0, sizeof(in_cache_entry));
182                 kfree(entry);
183         }
184
185         return;
186 }
187
188 /*
189  * This should be called with write lock on
190  */
191 static void in_cache_remove_entry(in_cache_entry *entry,
192                                   struct mpoa_client *client)
193 {
194         struct atm_vcc *vcc;
195         struct k_message msg;
196
197         vcc = entry->shortcut;
198         dprintk("removing an ingress entry, ip = %pI4\n",
199                 &entry->ctrl_info.in_dst_ip);
200
201         if (entry->prev != NULL)
202                 entry->prev->next = entry->next;
203         else
204                 client->in_cache = entry->next;
205         if (entry->next != NULL)
206                 entry->next->prev = entry->prev;
207         client->in_ops->put(entry);
208         if (client->in_cache == NULL && client->eg_cache == NULL) {
209                 msg.type = STOP_KEEP_ALIVE_SM;
210                 msg_to_mpoad(&msg, client);
211         }
212
213         /* Check if the egress side still uses this VCC */
214         if (vcc != NULL) {
215                 eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc,
216                                                                       client);
217                 if (eg_entry != NULL) {
218                         client->eg_ops->put(eg_entry);
219                         return;
220                 }
221                 vcc_release_async(vcc, -EPIPE);
222         }
223
224         return;
225 }
226
227 /* Call this every MPC-p2 seconds... Not exactly correct solution,
228    but an easy one... */
229 static void clear_count_and_expired(struct mpoa_client *client)
230 {
231         in_cache_entry *entry, *next_entry;
232         struct timeval now;
233
234         do_gettimeofday(&now);
235
236         write_lock_bh(&client->ingress_lock);
237         entry = client->in_cache;
238         while (entry != NULL) {
239                 entry->count = 0;
240                 next_entry = entry->next;
241                 if ((now.tv_sec - entry->tv.tv_sec)
242                    > entry->ctrl_info.holding_time) {
243                         dprintk("holding time expired, ip = %pI4\n",
244                                 &entry->ctrl_info.in_dst_ip);
245                         client->in_ops->remove_entry(entry, client);
246                 }
247                 entry = next_entry;
248         }
249         write_unlock_bh(&client->ingress_lock);
250
251         return;
252 }
253
254 /* Call this every MPC-p4 seconds. */
255 static void check_resolving_entries(struct mpoa_client *client)
256 {
257
258         struct atm_mpoa_qos *qos;
259         in_cache_entry *entry;
260         struct timeval now;
261         struct k_message msg;
262
263         do_gettimeofday(&now);
264
265         read_lock_bh(&client->ingress_lock);
266         entry = client->in_cache;
267         while (entry != NULL) {
268                 if (entry->entry_state == INGRESS_RESOLVING) {
269                         if ((now.tv_sec - entry->hold_down.tv_sec) <
270                             client->parameters.mpc_p6) {
271                                 entry = entry->next;    /* Entry in hold down */
272                                 continue;
273                         }
274                         if ((now.tv_sec - entry->reply_wait.tv_sec) >
275                             entry->retry_time) {
276                                 entry->retry_time = MPC_C1 * (entry->retry_time);
277                                 /*
278                                  * Retry time maximum exceeded,
279                                  * put entry in hold down.
280                                  */
281                                 if (entry->retry_time > client->parameters.mpc_p5) {
282                                         do_gettimeofday(&(entry->hold_down));
283                                         entry->retry_time = client->parameters.mpc_p4;
284                                         entry = entry->next;
285                                         continue;
286                                 }
287                                 /* Ask daemon to send a resolution request. */
288                                 memset(&(entry->hold_down), 0, sizeof(struct timeval));
289                                 msg.type = SND_MPOA_RES_RTRY;
290                                 memcpy(msg.MPS_ctrl, client->mps_ctrl_addr, ATM_ESA_LEN);
291                                 msg.content.in_info = entry->ctrl_info;
292                                 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
293                                 if (qos != NULL)
294                                         msg.qos = qos->qos;
295                                 msg_to_mpoad(&msg, client);
296                                 do_gettimeofday(&(entry->reply_wait));
297                         }
298                 }
299                 entry = entry->next;
300         }
301         read_unlock_bh(&client->ingress_lock);
302 }
303
304 /* Call this every MPC-p5 seconds. */
305 static void refresh_entries(struct mpoa_client *client)
306 {
307         struct timeval now;
308         struct in_cache_entry *entry = client->in_cache;
309
310         ddprintk("refresh_entries\n");
311         do_gettimeofday(&now);
312
313         read_lock_bh(&client->ingress_lock);
314         while (entry != NULL) {
315                 if (entry->entry_state == INGRESS_RESOLVED) {
316                         if (!(entry->refresh_time))
317                                 entry->refresh_time = (2 * (entry->ctrl_info.holding_time))/3;
318                         if ((now.tv_sec - entry->reply_wait.tv_sec) >
319                             entry->refresh_time) {
320                                 dprintk("refreshing an entry.\n");
321                                 entry->entry_state = INGRESS_REFRESHING;
322
323                         }
324                 }
325                 entry = entry->next;
326         }
327         read_unlock_bh(&client->ingress_lock);
328 }
329
330 static void in_destroy_cache(struct mpoa_client *mpc)
331 {
332         write_lock_irq(&mpc->ingress_lock);
333         while (mpc->in_cache != NULL)
334                 mpc->in_ops->remove_entry(mpc->in_cache, mpc);
335         write_unlock_irq(&mpc->ingress_lock);
336
337         return;
338 }
339
340 static eg_cache_entry *eg_cache_get_by_cache_id(__be32 cache_id,
341                                                 struct mpoa_client *mpc)
342 {
343         eg_cache_entry *entry;
344
345         read_lock_irq(&mpc->egress_lock);
346         entry = mpc->eg_cache;
347         while (entry != NULL) {
348                 if (entry->ctrl_info.cache_id == cache_id) {
349                         atomic_inc(&entry->use);
350                         read_unlock_irq(&mpc->egress_lock);
351                         return entry;
352                 }
353                 entry = entry->next;
354         }
355         read_unlock_irq(&mpc->egress_lock);
356
357         return NULL;
358 }
359
360 /* This can be called from any context since it saves CPU flags */
361 static eg_cache_entry *eg_cache_get_by_tag(__be32 tag, struct mpoa_client *mpc)
362 {
363         unsigned long flags;
364         eg_cache_entry *entry;
365
366         read_lock_irqsave(&mpc->egress_lock, flags);
367         entry = mpc->eg_cache;
368         while (entry != NULL) {
369                 if (entry->ctrl_info.tag == tag) {
370                         atomic_inc(&entry->use);
371                         read_unlock_irqrestore(&mpc->egress_lock, flags);
372                         return entry;
373                 }
374                 entry = entry->next;
375         }
376         read_unlock_irqrestore(&mpc->egress_lock, flags);
377
378         return NULL;
379 }
380
381 /* This can be called from any context since it saves CPU flags */
382 static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc,
383                                            struct mpoa_client *mpc)
384 {
385         unsigned long flags;
386         eg_cache_entry *entry;
387
388         read_lock_irqsave(&mpc->egress_lock, flags);
389         entry = mpc->eg_cache;
390         while (entry != NULL) {
391                 if (entry->shortcut == vcc) {
392                         atomic_inc(&entry->use);
393                         read_unlock_irqrestore(&mpc->egress_lock, flags);
394                         return entry;
395                 }
396                 entry = entry->next;
397         }
398         read_unlock_irqrestore(&mpc->egress_lock, flags);
399
400         return NULL;
401 }
402
403 static eg_cache_entry *eg_cache_get_by_src_ip(__be32 ipaddr,
404                                               struct mpoa_client *mpc)
405 {
406         eg_cache_entry *entry;
407
408         read_lock_irq(&mpc->egress_lock);
409         entry = mpc->eg_cache;
410         while (entry != NULL) {
411                 if (entry->latest_ip_addr == ipaddr) {
412                         atomic_inc(&entry->use);
413                         read_unlock_irq(&mpc->egress_lock);
414                         return entry;
415                 }
416                 entry = entry->next;
417         }
418         read_unlock_irq(&mpc->egress_lock);
419
420         return NULL;
421 }
422
423 static void eg_cache_put(eg_cache_entry *entry)
424 {
425         if (atomic_dec_and_test(&entry->use)) {
426                 memset(entry, 0, sizeof(eg_cache_entry));
427                 kfree(entry);
428         }
429
430         return;
431 }
432
433 /*
434  * This should be called with write lock on
435  */
436 static void eg_cache_remove_entry(eg_cache_entry *entry,
437                                   struct mpoa_client *client)
438 {
439         struct atm_vcc *vcc;
440         struct k_message msg;
441
442         vcc = entry->shortcut;
443         dprintk("removing an egress entry.\n");
444         if (entry->prev != NULL)
445                 entry->prev->next = entry->next;
446         else
447                 client->eg_cache = entry->next;
448         if (entry->next != NULL)
449                 entry->next->prev = entry->prev;
450         client->eg_ops->put(entry);
451         if (client->in_cache == NULL && client->eg_cache == NULL) {
452                 msg.type = STOP_KEEP_ALIVE_SM;
453                 msg_to_mpoad(&msg, client);
454         }
455
456         /* Check if the ingress side still uses this VCC */
457         if (vcc != NULL) {
458                 in_cache_entry *in_entry = client->in_ops->get_by_vcc(vcc, client);
459                 if (in_entry != NULL) {
460                         client->in_ops->put(in_entry);
461                         return;
462                 }
463                 vcc_release_async(vcc, -EPIPE);
464         }
465
466         return;
467 }
468
469 static eg_cache_entry *eg_cache_add_entry(struct k_message *msg,
470                                           struct mpoa_client *client)
471 {
472         eg_cache_entry *entry = kzalloc(sizeof(eg_cache_entry), GFP_KERNEL);
473
474         if (entry == NULL) {
475                 pr_info("out of memory\n");
476                 return NULL;
477         }
478
479         dprintk("adding an egress entry, ip = %pI4, this should be our IP\n",
480                 &msg->content.eg_info.eg_dst_ip);
481
482         atomic_set(&entry->use, 1);
483         dprintk("new_eg_cache_entry: about to lock\n");
484         write_lock_irq(&client->egress_lock);
485         entry->next = client->eg_cache;
486         entry->prev = NULL;
487         if (client->eg_cache != NULL)
488                 client->eg_cache->prev = entry;
489         client->eg_cache = entry;
490
491         memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
492         entry->ctrl_info = msg->content.eg_info;
493         do_gettimeofday(&(entry->tv));
494         entry->entry_state = EGRESS_RESOLVED;
495         dprintk("new_eg_cache_entry cache_id %u\n",
496                 ntohl(entry->ctrl_info.cache_id));
497         dprintk("mps_ip = %pI4\n", &entry->ctrl_info.mps_ip);
498         atomic_inc(&entry->use);
499
500         write_unlock_irq(&client->egress_lock);
501         dprintk("new_eg_cache_entry: unlocked\n");
502
503         return entry;
504 }
505
506 static void update_eg_cache_entry(eg_cache_entry *entry, uint16_t holding_time)
507 {
508         do_gettimeofday(&(entry->tv));
509         entry->entry_state = EGRESS_RESOLVED;
510         entry->ctrl_info.holding_time = holding_time;
511
512         return;
513 }
514
515 static void clear_expired(struct mpoa_client *client)
516 {
517         eg_cache_entry *entry, *next_entry;
518         struct timeval now;
519         struct k_message msg;
520
521         do_gettimeofday(&now);
522
523         write_lock_irq(&client->egress_lock);
524         entry = client->eg_cache;
525         while (entry != NULL) {
526                 next_entry = entry->next;
527                 if ((now.tv_sec - entry->tv.tv_sec)
528                    > entry->ctrl_info.holding_time) {
529                         msg.type = SND_EGRESS_PURGE;
530                         msg.content.eg_info = entry->ctrl_info;
531                         dprintk("egress_cache: holding time expired, cache_id = %u.\n",
532                                 ntohl(entry->ctrl_info.cache_id));
533                         msg_to_mpoad(&msg, client);
534                         client->eg_ops->remove_entry(entry, client);
535                 }
536                 entry = next_entry;
537         }
538         write_unlock_irq(&client->egress_lock);
539
540         return;
541 }
542
543 static void eg_destroy_cache(struct mpoa_client *mpc)
544 {
545         write_lock_irq(&mpc->egress_lock);
546         while (mpc->eg_cache != NULL)
547                 mpc->eg_ops->remove_entry(mpc->eg_cache, mpc);
548         write_unlock_irq(&mpc->egress_lock);
549
550         return;
551 }
552
553
554 static struct in_cache_ops ingress_ops = {
555         in_cache_add_entry,               /* add_entry       */
556         in_cache_get,                     /* get             */
557         in_cache_get_with_mask,           /* get_with_mask   */
558         in_cache_get_by_vcc,              /* get_by_vcc      */
559         in_cache_put,                     /* put             */
560         in_cache_remove_entry,            /* remove_entry    */
561         cache_hit,                        /* cache_hit       */
562         clear_count_and_expired,          /* clear_count     */
563         check_resolving_entries,          /* check_resolving */
564         refresh_entries,                  /* refresh         */
565         in_destroy_cache                  /* destroy_cache   */
566 };
567
568 static struct eg_cache_ops egress_ops = {
569         eg_cache_add_entry,               /* add_entry        */
570         eg_cache_get_by_cache_id,         /* get_by_cache_id  */
571         eg_cache_get_by_tag,              /* get_by_tag       */
572         eg_cache_get_by_vcc,              /* get_by_vcc       */
573         eg_cache_get_by_src_ip,           /* get_by_src_ip    */
574         eg_cache_put,                     /* put              */
575         eg_cache_remove_entry,            /* remove_entry     */
576         update_eg_cache_entry,            /* update           */
577         clear_expired,                    /* clear_expired    */
578         eg_destroy_cache                  /* destroy_cache    */
579 };
580
581
582 void atm_mpoa_init_cache(struct mpoa_client *mpc)
583 {
584         mpc->in_ops = &ingress_ops;
585         mpc->eg_ops = &egress_ops;
586
587         return;
588 }