util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */
  3. #include "util.h"
  4. #include "qtn_hw_ids.h"
  5. void qtnf_sta_list_init(struct qtnf_sta_list *list)
  6. {
  7. if (unlikely(!list))
  8. return;
  9. INIT_LIST_HEAD(&list->head);
  10. atomic_set(&list->size, 0);
  11. }
  12. struct qtnf_sta_node *qtnf_sta_list_lookup(struct qtnf_sta_list *list,
  13. const u8 *mac)
  14. {
  15. struct qtnf_sta_node *node;
  16. if (unlikely(!mac))
  17. return NULL;
  18. list_for_each_entry(node, &list->head, list) {
  19. if (ether_addr_equal(node->mac_addr, mac))
  20. return node;
  21. }
  22. return NULL;
  23. }
  24. struct qtnf_sta_node *qtnf_sta_list_lookup_index(struct qtnf_sta_list *list,
  25. size_t index)
  26. {
  27. struct qtnf_sta_node *node;
  28. if (qtnf_sta_list_size(list) <= index)
  29. return NULL;
  30. list_for_each_entry(node, &list->head, list) {
  31. if (index-- == 0)
  32. return node;
  33. }
  34. return NULL;
  35. }
  36. struct qtnf_sta_node *qtnf_sta_list_add(struct qtnf_vif *vif,
  37. const u8 *mac)
  38. {
  39. struct qtnf_sta_list *list = &vif->sta_list;
  40. struct qtnf_sta_node *node;
  41. if (unlikely(!mac))
  42. return NULL;
  43. node = qtnf_sta_list_lookup(list, mac);
  44. if (node)
  45. goto done;
  46. node = kzalloc(sizeof(*node), GFP_KERNEL);
  47. if (unlikely(!node))
  48. goto done;
  49. ether_addr_copy(node->mac_addr, mac);
  50. list_add_tail(&node->list, &list->head);
  51. atomic_inc(&list->size);
  52. ++vif->generation;
  53. done:
  54. return node;
  55. }
  56. bool qtnf_sta_list_del(struct qtnf_vif *vif, const u8 *mac)
  57. {
  58. struct qtnf_sta_list *list = &vif->sta_list;
  59. struct qtnf_sta_node *node;
  60. bool ret = false;
  61. node = qtnf_sta_list_lookup(list, mac);
  62. if (node) {
  63. list_del(&node->list);
  64. atomic_dec(&list->size);
  65. kfree(node);
  66. ++vif->generation;
  67. ret = true;
  68. }
  69. return ret;
  70. }
  71. void qtnf_sta_list_free(struct qtnf_sta_list *list)
  72. {
  73. struct qtnf_sta_node *node, *tmp;
  74. atomic_set(&list->size, 0);
  75. list_for_each_entry_safe(node, tmp, &list->head, list) {
  76. list_del(&node->list);
  77. kfree(node);
  78. }
  79. INIT_LIST_HEAD(&list->head);
  80. }
  81. const char *qtnf_chipid_to_string(unsigned long chip_id)
  82. {
  83. switch (chip_id) {
  84. case QTN_CHIP_ID_TOPAZ:
  85. return "Topaz";
  86. case QTN_CHIP_ID_PEARL:
  87. return "Pearl revA";
  88. case QTN_CHIP_ID_PEARL_B:
  89. return "Pearl revB";
  90. case QTN_CHIP_ID_PEARL_C:
  91. return "Pearl revC";
  92. default:
  93. return "unknown";
  94. }
  95. }
  96. EXPORT_SYMBOL_GPL(qtnf_chipid_to_string);