Jing Huang | 7725ccf | 2009-09-23 17:46:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005-2009 Brocade Communications Systems, Inc. |
| 3 | * All rights reserved |
| 4 | * www.brocade.com |
| 5 | * |
| 6 | * Linux driver for Brocade Fibre Channel Host Bus Adapter. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License (GPL) Version 2 as |
| 10 | * published by the Free Software Foundation |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <bfa_timer.h> |
| 19 | #include <cs/bfa_debug.h> |
| 20 | |
| 21 | void |
| 22 | bfa_timer_init(struct bfa_timer_mod_s *mod) |
| 23 | { |
| 24 | INIT_LIST_HEAD(&mod->timer_q); |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | bfa_timer_beat(struct bfa_timer_mod_s *mod) |
| 29 | { |
| 30 | struct list_head *qh = &mod->timer_q; |
| 31 | struct list_head *qe, *qe_next; |
| 32 | struct bfa_timer_s *elem; |
| 33 | struct list_head timedout_q; |
| 34 | |
| 35 | INIT_LIST_HEAD(&timedout_q); |
| 36 | |
| 37 | qe = bfa_q_next(qh); |
| 38 | |
| 39 | while (qe != qh) { |
| 40 | qe_next = bfa_q_next(qe); |
| 41 | |
| 42 | elem = (struct bfa_timer_s *) qe; |
| 43 | if (elem->timeout <= BFA_TIMER_FREQ) { |
| 44 | elem->timeout = 0; |
| 45 | list_del(&elem->qe); |
| 46 | list_add_tail(&elem->qe, &timedout_q); |
| 47 | } else { |
| 48 | elem->timeout -= BFA_TIMER_FREQ; |
| 49 | } |
| 50 | |
| 51 | qe = qe_next; /* go to next elem */ |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Pop all the timeout entries |
| 56 | */ |
| 57 | while (!list_empty(&timedout_q)) { |
| 58 | bfa_q_deq(&timedout_q, &elem); |
| 59 | elem->timercb(elem->arg); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Should be called with lock protection |
| 65 | */ |
| 66 | void |
| 67 | bfa_timer_begin(struct bfa_timer_mod_s *mod, struct bfa_timer_s *timer, |
| 68 | void (*timercb) (void *), void *arg, unsigned int timeout) |
| 69 | { |
| 70 | |
| 71 | bfa_assert(timercb != NULL); |
| 72 | bfa_assert(!bfa_q_is_on_q(&mod->timer_q, timer)); |
| 73 | |
| 74 | timer->timeout = timeout; |
| 75 | timer->timercb = timercb; |
| 76 | timer->arg = arg; |
| 77 | |
| 78 | list_add_tail(&timer->qe, &mod->timer_q); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Should be called with lock protection |
| 83 | */ |
| 84 | void |
| 85 | bfa_timer_stop(struct bfa_timer_s *timer) |
| 86 | { |
| 87 | bfa_assert(!list_empty(&timer->qe)); |
| 88 | |
| 89 | list_del(&timer->qe); |
| 90 | } |