util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2015-2016 Quantenna Communications, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include "util.h"
  17. void qtnf_sta_list_init(struct qtnf_sta_list *list)
  18. {
  19. if (unlikely(!list))
  20. return;
  21. INIT_LIST_HEAD(&list->head);
  22. atomic_set(&list->size, 0);
  23. }
  24. struct qtnf_sta_node *qtnf_sta_list_lookup(struct qtnf_sta_list *list,
  25. const u8 *mac)
  26. {
  27. struct qtnf_sta_node *node;
  28. if (unlikely(!mac))
  29. return NULL;
  30. list_for_each_entry(node, &list->head, list) {
  31. if (ether_addr_equal(node->mac_addr, mac))
  32. return node;
  33. }
  34. return NULL;
  35. }
  36. struct qtnf_sta_node *qtnf_sta_list_lookup_index(struct qtnf_sta_list *list,
  37. size_t index)
  38. {
  39. struct qtnf_sta_node *node;
  40. if (qtnf_sta_list_size(list) <= index)
  41. return NULL;
  42. list_for_each_entry(node, &list->head, list) {
  43. if (index-- == 0)
  44. return node;
  45. }
  46. return NULL;
  47. }
  48. struct qtnf_sta_node *qtnf_sta_list_add(struct qtnf_vif *vif,
  49. const u8 *mac)
  50. {
  51. struct qtnf_sta_list *list = &vif->sta_list;
  52. struct qtnf_sta_node *node;
  53. if (unlikely(!mac))
  54. return NULL;
  55. node = qtnf_sta_list_lookup(list, mac);
  56. if (node)
  57. goto done;
  58. node = kzalloc(sizeof(*node), GFP_KERNEL);
  59. if (unlikely(!node))
  60. goto done;
  61. ether_addr_copy(node->mac_addr, mac);
  62. list_add_tail(&node->list, &list->head);
  63. atomic_inc(&list->size);
  64. ++vif->generation;
  65. done:
  66. return node;
  67. }
  68. bool qtnf_sta_list_del(struct qtnf_vif *vif, const u8 *mac)
  69. {
  70. struct qtnf_sta_list *list = &vif->sta_list;
  71. struct qtnf_sta_node *node;
  72. bool ret = false;
  73. node = qtnf_sta_list_lookup(list, mac);
  74. if (node) {
  75. list_del(&node->list);
  76. atomic_dec(&list->size);
  77. kfree(node);
  78. ++vif->generation;
  79. ret = true;
  80. }
  81. return ret;
  82. }
  83. void qtnf_sta_list_free(struct qtnf_sta_list *list)
  84. {
  85. struct qtnf_sta_node *node, *tmp;
  86. atomic_set(&list->size, 0);
  87. list_for_each_entry_safe(node, tmp, &list->head, list) {
  88. list_del(&node->list);
  89. kfree(node);
  90. }
  91. INIT_LIST_HEAD(&list->head);
  92. }