bfa_timer.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <bfa_timer.h>
  18. #include <cs/bfa_debug.h>
  19. void
  20. bfa_timer_init(struct bfa_timer_mod_s *mod)
  21. {
  22. INIT_LIST_HEAD(&mod->timer_q);
  23. }
  24. void
  25. bfa_timer_beat(struct bfa_timer_mod_s *mod)
  26. {
  27. struct list_head *qh = &mod->timer_q;
  28. struct list_head *qe, *qe_next;
  29. struct bfa_timer_s *elem;
  30. struct list_head timedout_q;
  31. INIT_LIST_HEAD(&timedout_q);
  32. qe = bfa_q_next(qh);
  33. while (qe != qh) {
  34. qe_next = bfa_q_next(qe);
  35. elem = (struct bfa_timer_s *) qe;
  36. if (elem->timeout <= BFA_TIMER_FREQ) {
  37. elem->timeout = 0;
  38. list_del(&elem->qe);
  39. list_add_tail(&elem->qe, &timedout_q);
  40. } else {
  41. elem->timeout -= BFA_TIMER_FREQ;
  42. }
  43. qe = qe_next; /* go to next elem */
  44. }
  45. /*
  46. * Pop all the timeout entries
  47. */
  48. while (!list_empty(&timedout_q)) {
  49. bfa_q_deq(&timedout_q, &elem);
  50. elem->timercb(elem->arg);
  51. }
  52. }
  53. /**
  54. * Should be called with lock protection
  55. */
  56. void
  57. bfa_timer_begin(struct bfa_timer_mod_s *mod, struct bfa_timer_s *timer,
  58. void (*timercb) (void *), void *arg, unsigned int timeout)
  59. {
  60. bfa_assert(timercb != NULL);
  61. bfa_assert(!bfa_q_is_on_q(&mod->timer_q, timer));
  62. timer->timeout = timeout;
  63. timer->timercb = timercb;
  64. timer->arg = arg;
  65. list_add_tail(&timer->qe, &mod->timer_q);
  66. }
  67. /**
  68. * Should be called with lock protection
  69. */
  70. void
  71. bfa_timer_stop(struct bfa_timer_s *timer)
  72. {
  73. bfa_assert(!list_empty(&timer->qe));
  74. list_del(&timer->qe);
  75. }