firewire: nosy: fix device shutdown with active client
[firewire-audio.git] / net / atm / mpoa_caches.c
blobe773d8336918b021eeeea82e409fba657f564ab1
1 #include <linux/types.h>
2 #include <linux/atmmpc.h>
3 #include <linux/slab.h>
4 #include <linux/time.h>
6 #include "mpoa_caches.h"
7 #include "mpc.h"
9 /*
10 * mpoa_caches.c: Implementation of ingress and egress cache
11 * handling functions
14 #if 0
15 #define dprintk(format, args...) \
16 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
17 #else
18 #define dprintk(format, args...) \
19 do { if (0) \
20 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
21 } while (0)
22 #endif
24 #if 0
25 #define ddprintk(format, args...) \
26 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
27 #else
28 #define ddprintk(format, args...) \
29 do { if (0) \
30 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
31 } while (0)
32 #endif
34 static in_cache_entry *in_cache_get(__be32 dst_ip,
35 struct mpoa_client *client)
37 in_cache_entry *entry;
39 read_lock_bh(&client->ingress_lock);
40 entry = client->in_cache;
41 while (entry != NULL) {
42 if (entry->ctrl_info.in_dst_ip == dst_ip) {
43 atomic_inc(&entry->use);
44 read_unlock_bh(&client->ingress_lock);
45 return entry;
47 entry = entry->next;
49 read_unlock_bh(&client->ingress_lock);
51 return NULL;
54 static in_cache_entry *in_cache_get_with_mask(__be32 dst_ip,
55 struct mpoa_client *client,
56 __be32 mask)
58 in_cache_entry *entry;
60 read_lock_bh(&client->ingress_lock);
61 entry = client->in_cache;
62 while (entry != NULL) {
63 if ((entry->ctrl_info.in_dst_ip & mask) == (dst_ip & mask)) {
64 atomic_inc(&entry->use);
65 read_unlock_bh(&client->ingress_lock);
66 return entry;
68 entry = entry->next;
70 read_unlock_bh(&client->ingress_lock);
72 return NULL;
76 static in_cache_entry *in_cache_get_by_vcc(struct atm_vcc *vcc,
77 struct mpoa_client *client)
79 in_cache_entry *entry;
81 read_lock_bh(&client->ingress_lock);
82 entry = client->in_cache;
83 while (entry != NULL) {
84 if (entry->shortcut == vcc) {
85 atomic_inc(&entry->use);
86 read_unlock_bh(&client->ingress_lock);
87 return entry;
89 entry = entry->next;
91 read_unlock_bh(&client->ingress_lock);
93 return NULL;
96 static in_cache_entry *in_cache_add_entry(__be32 dst_ip,
97 struct mpoa_client *client)
99 in_cache_entry *entry = kzalloc(sizeof(in_cache_entry), GFP_KERNEL);
101 if (entry == NULL) {
102 pr_info("mpoa: mpoa_caches.c: new_in_cache_entry: out of memory\n");
103 return NULL;
106 dprintk("adding an ingress entry, ip = %pI4\n", &dst_ip);
108 atomic_set(&entry->use, 1);
109 dprintk("new_in_cache_entry: about to lock\n");
110 write_lock_bh(&client->ingress_lock);
111 entry->next = client->in_cache;
112 entry->prev = NULL;
113 if (client->in_cache != NULL)
114 client->in_cache->prev = entry;
115 client->in_cache = entry;
117 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
118 entry->ctrl_info.in_dst_ip = dst_ip;
119 do_gettimeofday(&(entry->tv));
120 entry->retry_time = client->parameters.mpc_p4;
121 entry->count = 1;
122 entry->entry_state = INGRESS_INVALID;
123 entry->ctrl_info.holding_time = HOLDING_TIME_DEFAULT;
124 atomic_inc(&entry->use);
126 write_unlock_bh(&client->ingress_lock);
127 dprintk("new_in_cache_entry: unlocked\n");
129 return entry;
132 static int cache_hit(in_cache_entry *entry, struct mpoa_client *mpc)
134 struct atm_mpoa_qos *qos;
135 struct k_message msg;
137 entry->count++;
138 if (entry->entry_state == INGRESS_RESOLVED && entry->shortcut != NULL)
139 return OPEN;
141 if (entry->entry_state == INGRESS_REFRESHING) {
142 if (entry->count > mpc->parameters.mpc_p1) {
143 msg.type = SND_MPOA_RES_RQST;
144 msg.content.in_info = entry->ctrl_info;
145 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
146 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
147 if (qos != NULL)
148 msg.qos = qos->qos;
149 msg_to_mpoad(&msg, mpc);
150 do_gettimeofday(&(entry->reply_wait));
151 entry->entry_state = INGRESS_RESOLVING;
153 if (entry->shortcut != NULL)
154 return OPEN;
155 return CLOSED;
158 if (entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL)
159 return OPEN;
161 if (entry->count > mpc->parameters.mpc_p1 &&
162 entry->entry_state == INGRESS_INVALID) {
163 dprintk("(%s) threshold exceeded for ip %pI4, sending MPOA res req\n",
164 mpc->dev->name, &entry->ctrl_info.in_dst_ip);
165 entry->entry_state = INGRESS_RESOLVING;
166 msg.type = SND_MPOA_RES_RQST;
167 memcpy(msg.MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN);
168 msg.content.in_info = entry->ctrl_info;
169 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
170 if (qos != NULL)
171 msg.qos = qos->qos;
172 msg_to_mpoad(&msg, mpc);
173 do_gettimeofday(&(entry->reply_wait));
176 return CLOSED;
179 static void in_cache_put(in_cache_entry *entry)
181 if (atomic_dec_and_test(&entry->use)) {
182 memset(entry, 0, sizeof(in_cache_entry));
183 kfree(entry);
186 return;
190 * This should be called with write lock on
192 static void in_cache_remove_entry(in_cache_entry *entry,
193 struct mpoa_client *client)
195 struct atm_vcc *vcc;
196 struct k_message msg;
198 vcc = entry->shortcut;
199 dprintk("removing an ingress entry, ip = %pI4\n",
200 &entry->ctrl_info.in_dst_ip);
202 if (entry->prev != NULL)
203 entry->prev->next = entry->next;
204 else
205 client->in_cache = entry->next;
206 if (entry->next != NULL)
207 entry->next->prev = entry->prev;
208 client->in_ops->put(entry);
209 if (client->in_cache == NULL && client->eg_cache == NULL) {
210 msg.type = STOP_KEEP_ALIVE_SM;
211 msg_to_mpoad(&msg, client);
214 /* Check if the egress side still uses this VCC */
215 if (vcc != NULL) {
216 eg_cache_entry *eg_entry = client->eg_ops->get_by_vcc(vcc,
217 client);
218 if (eg_entry != NULL) {
219 client->eg_ops->put(eg_entry);
220 return;
222 vcc_release_async(vcc, -EPIPE);
225 return;
228 /* Call this every MPC-p2 seconds... Not exactly correct solution,
229 but an easy one... */
230 static void clear_count_and_expired(struct mpoa_client *client)
232 in_cache_entry *entry, *next_entry;
233 struct timeval now;
235 do_gettimeofday(&now);
237 write_lock_bh(&client->ingress_lock);
238 entry = client->in_cache;
239 while (entry != NULL) {
240 entry->count = 0;
241 next_entry = entry->next;
242 if ((now.tv_sec - entry->tv.tv_sec)
243 > entry->ctrl_info.holding_time) {
244 dprintk("holding time expired, ip = %pI4\n",
245 &entry->ctrl_info.in_dst_ip);
246 client->in_ops->remove_entry(entry, client);
248 entry = next_entry;
250 write_unlock_bh(&client->ingress_lock);
252 return;
255 /* Call this every MPC-p4 seconds. */
256 static void check_resolving_entries(struct mpoa_client *client)
259 struct atm_mpoa_qos *qos;
260 in_cache_entry *entry;
261 struct timeval now;
262 struct k_message msg;
264 do_gettimeofday(&now);
266 read_lock_bh(&client->ingress_lock);
267 entry = client->in_cache;
268 while (entry != NULL) {
269 if (entry->entry_state == INGRESS_RESOLVING) {
270 if ((now.tv_sec - entry->hold_down.tv_sec) <
271 client->parameters.mpc_p6) {
272 entry = entry->next; /* Entry in hold down */
273 continue;
275 if ((now.tv_sec - entry->reply_wait.tv_sec) >
276 entry->retry_time) {
277 entry->retry_time = MPC_C1 * (entry->retry_time);
279 * Retry time maximum exceeded,
280 * put entry in hold down.
282 if (entry->retry_time > client->parameters.mpc_p5) {
283 do_gettimeofday(&(entry->hold_down));
284 entry->retry_time = client->parameters.mpc_p4;
285 entry = entry->next;
286 continue;
288 /* Ask daemon to send a resolution request. */
289 memset(&(entry->hold_down), 0, sizeof(struct timeval));
290 msg.type = SND_MPOA_RES_RTRY;
291 memcpy(msg.MPS_ctrl, client->mps_ctrl_addr, ATM_ESA_LEN);
292 msg.content.in_info = entry->ctrl_info;
293 qos = atm_mpoa_search_qos(entry->ctrl_info.in_dst_ip);
294 if (qos != NULL)
295 msg.qos = qos->qos;
296 msg_to_mpoad(&msg, client);
297 do_gettimeofday(&(entry->reply_wait));
300 entry = entry->next;
302 read_unlock_bh(&client->ingress_lock);
305 /* Call this every MPC-p5 seconds. */
306 static void refresh_entries(struct mpoa_client *client)
308 struct timeval now;
309 struct in_cache_entry *entry = client->in_cache;
311 ddprintk("refresh_entries\n");
312 do_gettimeofday(&now);
314 read_lock_bh(&client->ingress_lock);
315 while (entry != NULL) {
316 if (entry->entry_state == INGRESS_RESOLVED) {
317 if (!(entry->refresh_time))
318 entry->refresh_time = (2 * (entry->ctrl_info.holding_time))/3;
319 if ((now.tv_sec - entry->reply_wait.tv_sec) >
320 entry->refresh_time) {
321 dprintk("refreshing an entry.\n");
322 entry->entry_state = INGRESS_REFRESHING;
326 entry = entry->next;
328 read_unlock_bh(&client->ingress_lock);
331 static void in_destroy_cache(struct mpoa_client *mpc)
333 write_lock_irq(&mpc->ingress_lock);
334 while (mpc->in_cache != NULL)
335 mpc->in_ops->remove_entry(mpc->in_cache, mpc);
336 write_unlock_irq(&mpc->ingress_lock);
338 return;
341 static eg_cache_entry *eg_cache_get_by_cache_id(__be32 cache_id,
342 struct mpoa_client *mpc)
344 eg_cache_entry *entry;
346 read_lock_irq(&mpc->egress_lock);
347 entry = mpc->eg_cache;
348 while (entry != NULL) {
349 if (entry->ctrl_info.cache_id == cache_id) {
350 atomic_inc(&entry->use);
351 read_unlock_irq(&mpc->egress_lock);
352 return entry;
354 entry = entry->next;
356 read_unlock_irq(&mpc->egress_lock);
358 return NULL;
361 /* This can be called from any context since it saves CPU flags */
362 static eg_cache_entry *eg_cache_get_by_tag(__be32 tag, struct mpoa_client *mpc)
364 unsigned long flags;
365 eg_cache_entry *entry;
367 read_lock_irqsave(&mpc->egress_lock, flags);
368 entry = mpc->eg_cache;
369 while (entry != NULL) {
370 if (entry->ctrl_info.tag == tag) {
371 atomic_inc(&entry->use);
372 read_unlock_irqrestore(&mpc->egress_lock, flags);
373 return entry;
375 entry = entry->next;
377 read_unlock_irqrestore(&mpc->egress_lock, flags);
379 return NULL;
382 /* This can be called from any context since it saves CPU flags */
383 static eg_cache_entry *eg_cache_get_by_vcc(struct atm_vcc *vcc,
384 struct mpoa_client *mpc)
386 unsigned long flags;
387 eg_cache_entry *entry;
389 read_lock_irqsave(&mpc->egress_lock, flags);
390 entry = mpc->eg_cache;
391 while (entry != NULL) {
392 if (entry->shortcut == vcc) {
393 atomic_inc(&entry->use);
394 read_unlock_irqrestore(&mpc->egress_lock, flags);
395 return entry;
397 entry = entry->next;
399 read_unlock_irqrestore(&mpc->egress_lock, flags);
401 return NULL;
404 static eg_cache_entry *eg_cache_get_by_src_ip(__be32 ipaddr,
405 struct mpoa_client *mpc)
407 eg_cache_entry *entry;
409 read_lock_irq(&mpc->egress_lock);
410 entry = mpc->eg_cache;
411 while (entry != NULL) {
412 if (entry->latest_ip_addr == ipaddr) {
413 atomic_inc(&entry->use);
414 read_unlock_irq(&mpc->egress_lock);
415 return entry;
417 entry = entry->next;
419 read_unlock_irq(&mpc->egress_lock);
421 return NULL;
424 static void eg_cache_put(eg_cache_entry *entry)
426 if (atomic_dec_and_test(&entry->use)) {
427 memset(entry, 0, sizeof(eg_cache_entry));
428 kfree(entry);
431 return;
435 * This should be called with write lock on
437 static void eg_cache_remove_entry(eg_cache_entry *entry,
438 struct mpoa_client *client)
440 struct atm_vcc *vcc;
441 struct k_message msg;
443 vcc = entry->shortcut;
444 dprintk("removing an egress entry.\n");
445 if (entry->prev != NULL)
446 entry->prev->next = entry->next;
447 else
448 client->eg_cache = entry->next;
449 if (entry->next != NULL)
450 entry->next->prev = entry->prev;
451 client->eg_ops->put(entry);
452 if (client->in_cache == NULL && client->eg_cache == NULL) {
453 msg.type = STOP_KEEP_ALIVE_SM;
454 msg_to_mpoad(&msg, client);
457 /* Check if the ingress side still uses this VCC */
458 if (vcc != NULL) {
459 in_cache_entry *in_entry = client->in_ops->get_by_vcc(vcc, client);
460 if (in_entry != NULL) {
461 client->in_ops->put(in_entry);
462 return;
464 vcc_release_async(vcc, -EPIPE);
467 return;
470 static eg_cache_entry *eg_cache_add_entry(struct k_message *msg,
471 struct mpoa_client *client)
473 eg_cache_entry *entry = kzalloc(sizeof(eg_cache_entry), GFP_KERNEL);
475 if (entry == NULL) {
476 pr_info("out of memory\n");
477 return NULL;
480 dprintk("adding an egress entry, ip = %pI4, this should be our IP\n",
481 &msg->content.eg_info.eg_dst_ip);
483 atomic_set(&entry->use, 1);
484 dprintk("new_eg_cache_entry: about to lock\n");
485 write_lock_irq(&client->egress_lock);
486 entry->next = client->eg_cache;
487 entry->prev = NULL;
488 if (client->eg_cache != NULL)
489 client->eg_cache->prev = entry;
490 client->eg_cache = entry;
492 memcpy(entry->MPS_ctrl_ATM_addr, client->mps_ctrl_addr, ATM_ESA_LEN);
493 entry->ctrl_info = msg->content.eg_info;
494 do_gettimeofday(&(entry->tv));
495 entry->entry_state = EGRESS_RESOLVED;
496 dprintk("new_eg_cache_entry cache_id %u\n",
497 ntohl(entry->ctrl_info.cache_id));
498 dprintk("mps_ip = %pI4\n", &entry->ctrl_info.mps_ip);
499 atomic_inc(&entry->use);
501 write_unlock_irq(&client->egress_lock);
502 dprintk("new_eg_cache_entry: unlocked\n");
504 return entry;
507 static void update_eg_cache_entry(eg_cache_entry *entry, uint16_t holding_time)
509 do_gettimeofday(&(entry->tv));
510 entry->entry_state = EGRESS_RESOLVED;
511 entry->ctrl_info.holding_time = holding_time;
513 return;
516 static void clear_expired(struct mpoa_client *client)
518 eg_cache_entry *entry, *next_entry;
519 struct timeval now;
520 struct k_message msg;
522 do_gettimeofday(&now);
524 write_lock_irq(&client->egress_lock);
525 entry = client->eg_cache;
526 while (entry != NULL) {
527 next_entry = entry->next;
528 if ((now.tv_sec - entry->tv.tv_sec)
529 > entry->ctrl_info.holding_time) {
530 msg.type = SND_EGRESS_PURGE;
531 msg.content.eg_info = entry->ctrl_info;
532 dprintk("egress_cache: holding time expired, cache_id = %u.\n",
533 ntohl(entry->ctrl_info.cache_id));
534 msg_to_mpoad(&msg, client);
535 client->eg_ops->remove_entry(entry, client);
537 entry = next_entry;
539 write_unlock_irq(&client->egress_lock);
541 return;
544 static void eg_destroy_cache(struct mpoa_client *mpc)
546 write_lock_irq(&mpc->egress_lock);
547 while (mpc->eg_cache != NULL)
548 mpc->eg_ops->remove_entry(mpc->eg_cache, mpc);
549 write_unlock_irq(&mpc->egress_lock);
551 return;
555 static struct in_cache_ops ingress_ops = {
556 in_cache_add_entry, /* add_entry */
557 in_cache_get, /* get */
558 in_cache_get_with_mask, /* get_with_mask */
559 in_cache_get_by_vcc, /* get_by_vcc */
560 in_cache_put, /* put */
561 in_cache_remove_entry, /* remove_entry */
562 cache_hit, /* cache_hit */
563 clear_count_and_expired, /* clear_count */
564 check_resolving_entries, /* check_resolving */
565 refresh_entries, /* refresh */
566 in_destroy_cache /* destroy_cache */
569 static struct eg_cache_ops egress_ops = {
570 eg_cache_add_entry, /* add_entry */
571 eg_cache_get_by_cache_id, /* get_by_cache_id */
572 eg_cache_get_by_tag, /* get_by_tag */
573 eg_cache_get_by_vcc, /* get_by_vcc */
574 eg_cache_get_by_src_ip, /* get_by_src_ip */
575 eg_cache_put, /* put */
576 eg_cache_remove_entry, /* remove_entry */
577 update_eg_cache_entry, /* update */
578 clear_expired, /* clear_expired */
579 eg_destroy_cache /* destroy_cache */
583 void atm_mpoa_init_cache(struct mpoa_client *mpc)
585 mpc->in_ops = &ingress_ops;
586 mpc->eg_ops = &egress_ops;
588 return;