smt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include "cxgb4.h"
  35. #include "smt.h"
  36. #include "t4_msg.h"
  37. #include "t4fw_api.h"
  38. #include "t4_regs.h"
  39. #include "t4_values.h"
  40. struct smt_data *t4_init_smt(void)
  41. {
  42. unsigned int smt_size;
  43. struct smt_data *s;
  44. int i;
  45. smt_size = SMT_SIZE;
  46. s = kvzalloc(sizeof(*s) + smt_size * sizeof(struct smt_entry),
  47. GFP_KERNEL);
  48. if (!s)
  49. return NULL;
  50. s->smt_size = smt_size;
  51. rwlock_init(&s->lock);
  52. for (i = 0; i < s->smt_size; ++i) {
  53. s->smtab[i].idx = i;
  54. s->smtab[i].state = SMT_STATE_UNUSED;
  55. memset(&s->smtab[i].src_mac, 0, ETH_ALEN);
  56. spin_lock_init(&s->smtab[i].lock);
  57. atomic_set(&s->smtab[i].refcnt, 0);
  58. }
  59. return s;
  60. }
  61. static struct smt_entry *find_or_alloc_smte(struct smt_data *s, u8 *smac)
  62. {
  63. struct smt_entry *first_free = NULL;
  64. struct smt_entry *e, *end;
  65. for (e = &s->smtab[0], end = &s->smtab[s->smt_size]; e != end; ++e) {
  66. if (atomic_read(&e->refcnt) == 0) {
  67. if (!first_free)
  68. first_free = e;
  69. } else {
  70. if (e->state == SMT_STATE_SWITCHING) {
  71. /* This entry is actually in use. See if we can
  72. * re-use it ?
  73. */
  74. if (memcmp(e->src_mac, smac, ETH_ALEN) == 0)
  75. goto found_reuse;
  76. }
  77. }
  78. }
  79. if (first_free) {
  80. e = first_free;
  81. goto found;
  82. }
  83. return NULL;
  84. found:
  85. e->state = SMT_STATE_UNUSED;
  86. found_reuse:
  87. return e;
  88. }
  89. static void t4_smte_free(struct smt_entry *e)
  90. {
  91. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  92. e->state = SMT_STATE_UNUSED;
  93. }
  94. }
  95. /**
  96. * @e: smt entry to release
  97. *
  98. * Releases ref count and frees up an smt entry from SMT table
  99. */
  100. void cxgb4_smt_release(struct smt_entry *e)
  101. {
  102. spin_lock_bh(&e->lock);
  103. if (atomic_dec_and_test(&e->refcnt))
  104. t4_smte_free(e);
  105. spin_unlock_bh(&e->lock);
  106. }
  107. EXPORT_SYMBOL(cxgb4_smt_release);
  108. void do_smt_write_rpl(struct adapter *adap, const struct cpl_smt_write_rpl *rpl)
  109. {
  110. unsigned int smtidx = TID_TID_G(GET_TID(rpl));
  111. struct smt_data *s = adap->smt;
  112. if (unlikely(rpl->status != CPL_ERR_NONE)) {
  113. struct smt_entry *e = &s->smtab[smtidx];
  114. dev_err(adap->pdev_dev,
  115. "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
  116. rpl->status, smtidx);
  117. spin_lock(&e->lock);
  118. e->state = SMT_STATE_ERROR;
  119. spin_unlock(&e->lock);
  120. return;
  121. }
  122. }
  123. static int write_smt_entry(struct adapter *adapter, struct smt_entry *e)
  124. {
  125. struct cpl_t6_smt_write_req *t6req;
  126. struct smt_data *s = adapter->smt;
  127. struct cpl_smt_write_req *req;
  128. struct sk_buff *skb;
  129. int size;
  130. u8 row;
  131. if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) {
  132. size = sizeof(*req);
  133. skb = alloc_skb(size, GFP_ATOMIC);
  134. if (!skb)
  135. return -ENOMEM;
  136. /* Source MAC Table (SMT) contains 256 SMAC entries
  137. * organized in 128 rows of 2 entries each.
  138. */
  139. req = (struct cpl_smt_write_req *)__skb_put(skb, size);
  140. INIT_TP_WR(req, 0);
  141. /* Each row contains an SMAC pair.
  142. * LSB selects the SMAC entry within a row
  143. */
  144. row = (e->idx >> 1);
  145. if (e->idx & 1) {
  146. req->pfvf1 = 0x0;
  147. memcpy(req->src_mac1, e->src_mac, ETH_ALEN);
  148. /* fill pfvf0/src_mac0 with entry
  149. * at prev index from smt-tab.
  150. */
  151. req->pfvf0 = 0x0;
  152. memcpy(req->src_mac0, s->smtab[e->idx - 1].src_mac,
  153. ETH_ALEN);
  154. } else {
  155. req->pfvf0 = 0x0;
  156. memcpy(req->src_mac0, e->src_mac, ETH_ALEN);
  157. /* fill pfvf1/src_mac1 with entry
  158. * at next index from smt-tab
  159. */
  160. req->pfvf1 = 0x0;
  161. memcpy(req->src_mac1, s->smtab[e->idx + 1].src_mac,
  162. ETH_ALEN);
  163. }
  164. } else {
  165. size = sizeof(*t6req);
  166. skb = alloc_skb(size, GFP_ATOMIC);
  167. if (!skb)
  168. return -ENOMEM;
  169. /* Source MAC Table (SMT) contains 256 SMAC entries */
  170. t6req = (struct cpl_t6_smt_write_req *)__skb_put(skb, size);
  171. INIT_TP_WR(t6req, 0);
  172. req = (struct cpl_smt_write_req *)t6req;
  173. /* fill pfvf0/src_mac0 from smt-tab */
  174. req->pfvf0 = 0x0;
  175. memcpy(req->src_mac0, s->smtab[e->idx].src_mac, ETH_ALEN);
  176. row = e->idx;
  177. }
  178. OPCODE_TID(req) =
  179. htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, e->idx |
  180. TID_QID_V(adapter->sge.fw_evtq.abs_id)));
  181. req->params = htonl(SMTW_NORPL_V(0) |
  182. SMTW_IDX_V(row) |
  183. SMTW_OVLAN_IDX_V(0));
  184. t4_mgmt_tx(adapter, skb);
  185. return 0;
  186. }
  187. static struct smt_entry *t4_smt_alloc_switching(struct adapter *adap, u16 pfvf,
  188. u8 *smac)
  189. {
  190. struct smt_data *s = adap->smt;
  191. struct smt_entry *e;
  192. write_lock_bh(&s->lock);
  193. e = find_or_alloc_smte(s, smac);
  194. if (e) {
  195. spin_lock(&e->lock);
  196. if (!atomic_read(&e->refcnt)) {
  197. atomic_set(&e->refcnt, 1);
  198. e->state = SMT_STATE_SWITCHING;
  199. e->pfvf = pfvf;
  200. memcpy(e->src_mac, smac, ETH_ALEN);
  201. write_smt_entry(adap, e);
  202. } else {
  203. atomic_inc(&e->refcnt);
  204. }
  205. spin_unlock(&e->lock);
  206. }
  207. write_unlock_bh(&s->lock);
  208. return e;
  209. }
  210. /**
  211. * @dev: net_device pointer
  212. * @smac: MAC address to add to SMT
  213. * Returns pointer to the SMT entry created
  214. *
  215. * Allocates an SMT entry to be used by switching rule of a filter.
  216. */
  217. struct smt_entry *cxgb4_smt_alloc_switching(struct net_device *dev, u8 *smac)
  218. {
  219. struct adapter *adap = netdev2adap(dev);
  220. return t4_smt_alloc_switching(adap, 0x0, smac);
  221. }
  222. EXPORT_SYMBOL(cxgb4_smt_alloc_switching);