l2t.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2014 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 <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/if.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/jhash.h>
  39. #include <linux/module.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/seq_file.h>
  42. #include <net/neighbour.h>
  43. #include "cxgb4.h"
  44. #include "l2t.h"
  45. #include "t4_msg.h"
  46. #include "t4fw_api.h"
  47. #include "t4_regs.h"
  48. #include "t4_values.h"
  49. /* identifies sync vs async L2T_WRITE_REQs */
  50. #define SYNC_WR_S 12
  51. #define SYNC_WR_V(x) ((x) << SYNC_WR_S)
  52. #define SYNC_WR_F SYNC_WR_V(1)
  53. struct l2t_data {
  54. unsigned int l2t_start; /* start index of our piece of the L2T */
  55. unsigned int l2t_size; /* number of entries in l2tab */
  56. rwlock_t lock;
  57. atomic_t nfree; /* number of free entries */
  58. struct l2t_entry *rover; /* starting point for next allocation */
  59. struct l2t_entry l2tab[]; /* MUST BE LAST */
  60. };
  61. static inline unsigned int vlan_prio(const struct l2t_entry *e)
  62. {
  63. return e->vlan >> VLAN_PRIO_SHIFT;
  64. }
  65. static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
  66. {
  67. if (atomic_add_return(1, &e->refcnt) == 1) /* 0 -> 1 transition */
  68. atomic_dec(&d->nfree);
  69. }
  70. /*
  71. * To avoid having to check address families we do not allow v4 and v6
  72. * neighbors to be on the same hash chain. We keep v4 entries in the first
  73. * half of available hash buckets and v6 in the second. We need at least two
  74. * entries in our L2T for this scheme to work.
  75. */
  76. enum {
  77. L2T_MIN_HASH_BUCKETS = 2,
  78. };
  79. static inline unsigned int arp_hash(struct l2t_data *d, const u32 *key,
  80. int ifindex)
  81. {
  82. unsigned int l2t_size_half = d->l2t_size / 2;
  83. return jhash_2words(*key, ifindex, 0) % l2t_size_half;
  84. }
  85. static inline unsigned int ipv6_hash(struct l2t_data *d, const u32 *key,
  86. int ifindex)
  87. {
  88. unsigned int l2t_size_half = d->l2t_size / 2;
  89. u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
  90. return (l2t_size_half +
  91. (jhash_2words(xor, ifindex, 0) % l2t_size_half));
  92. }
  93. static unsigned int addr_hash(struct l2t_data *d, const u32 *addr,
  94. int addr_len, int ifindex)
  95. {
  96. return addr_len == 4 ? arp_hash(d, addr, ifindex) :
  97. ipv6_hash(d, addr, ifindex);
  98. }
  99. /*
  100. * Checks if an L2T entry is for the given IP/IPv6 address. It does not check
  101. * whether the L2T entry and the address are of the same address family.
  102. * Callers ensure an address is only checked against L2T entries of the same
  103. * family, something made trivial by the separation of IP and IPv6 hash chains
  104. * mentioned above. Returns 0 if there's a match,
  105. */
  106. static int addreq(const struct l2t_entry *e, const u32 *addr)
  107. {
  108. if (e->v6)
  109. return (e->addr[0] ^ addr[0]) | (e->addr[1] ^ addr[1]) |
  110. (e->addr[2] ^ addr[2]) | (e->addr[3] ^ addr[3]);
  111. return e->addr[0] ^ addr[0];
  112. }
  113. static void neigh_replace(struct l2t_entry *e, struct neighbour *n)
  114. {
  115. neigh_hold(n);
  116. if (e->neigh)
  117. neigh_release(e->neigh);
  118. e->neigh = n;
  119. }
  120. /*
  121. * Write an L2T entry. Must be called with the entry locked.
  122. * The write may be synchronous or asynchronous.
  123. */
  124. static int write_l2e(struct adapter *adap, struct l2t_entry *e, int sync)
  125. {
  126. struct l2t_data *d = adap->l2t;
  127. unsigned int l2t_idx = e->idx + d->l2t_start;
  128. struct sk_buff *skb;
  129. struct cpl_l2t_write_req *req;
  130. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  131. if (!skb)
  132. return -ENOMEM;
  133. req = __skb_put(skb, sizeof(*req));
  134. INIT_TP_WR(req, 0);
  135. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ,
  136. l2t_idx | (sync ? SYNC_WR_F : 0) |
  137. TID_QID_V(adap->sge.fw_evtq.abs_id)));
  138. req->params = htons(L2T_W_PORT_V(e->lport) | L2T_W_NOREPLY_V(!sync));
  139. req->l2t_idx = htons(l2t_idx);
  140. req->vlan = htons(e->vlan);
  141. if (e->neigh && !(e->neigh->dev->flags & IFF_LOOPBACK))
  142. memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
  143. memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
  144. t4_mgmt_tx(adap, skb);
  145. if (sync && e->state != L2T_STATE_SWITCHING)
  146. e->state = L2T_STATE_SYNC_WRITE;
  147. return 0;
  148. }
  149. /*
  150. * Send packets waiting in an L2T entry's ARP queue. Must be called with the
  151. * entry locked.
  152. */
  153. static void send_pending(struct adapter *adap, struct l2t_entry *e)
  154. {
  155. struct sk_buff *skb;
  156. while ((skb = __skb_dequeue(&e->arpq)) != NULL)
  157. t4_ofld_send(adap, skb);
  158. }
  159. /*
  160. * Process a CPL_L2T_WRITE_RPL. Wake up the ARP queue if it completes a
  161. * synchronous L2T_WRITE. Note that the TID in the reply is really the L2T
  162. * index it refers to.
  163. */
  164. void do_l2t_write_rpl(struct adapter *adap, const struct cpl_l2t_write_rpl *rpl)
  165. {
  166. struct l2t_data *d = adap->l2t;
  167. unsigned int tid = GET_TID(rpl);
  168. unsigned int l2t_idx = tid % L2T_SIZE;
  169. if (unlikely(rpl->status != CPL_ERR_NONE)) {
  170. dev_err(adap->pdev_dev,
  171. "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
  172. rpl->status, l2t_idx);
  173. return;
  174. }
  175. if (tid & SYNC_WR_F) {
  176. struct l2t_entry *e = &d->l2tab[l2t_idx - d->l2t_start];
  177. spin_lock(&e->lock);
  178. if (e->state != L2T_STATE_SWITCHING) {
  179. send_pending(adap, e);
  180. e->state = (e->neigh->nud_state & NUD_STALE) ?
  181. L2T_STATE_STALE : L2T_STATE_VALID;
  182. }
  183. spin_unlock(&e->lock);
  184. }
  185. }
  186. /*
  187. * Add a packet to an L2T entry's queue of packets awaiting resolution.
  188. * Must be called with the entry's lock held.
  189. */
  190. static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
  191. {
  192. __skb_queue_tail(&e->arpq, skb);
  193. }
  194. int cxgb4_l2t_send(struct net_device *dev, struct sk_buff *skb,
  195. struct l2t_entry *e)
  196. {
  197. struct adapter *adap = netdev2adap(dev);
  198. again:
  199. switch (e->state) {
  200. case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
  201. neigh_event_send(e->neigh, NULL);
  202. spin_lock_bh(&e->lock);
  203. if (e->state == L2T_STATE_STALE)
  204. e->state = L2T_STATE_VALID;
  205. spin_unlock_bh(&e->lock);
  206. fallthrough;
  207. case L2T_STATE_VALID: /* fast-path, send the packet on */
  208. return t4_ofld_send(adap, skb);
  209. case L2T_STATE_RESOLVING:
  210. case L2T_STATE_SYNC_WRITE:
  211. spin_lock_bh(&e->lock);
  212. if (e->state != L2T_STATE_SYNC_WRITE &&
  213. e->state != L2T_STATE_RESOLVING) {
  214. spin_unlock_bh(&e->lock);
  215. goto again;
  216. }
  217. arpq_enqueue(e, skb);
  218. spin_unlock_bh(&e->lock);
  219. if (e->state == L2T_STATE_RESOLVING &&
  220. !neigh_event_send(e->neigh, NULL)) {
  221. spin_lock_bh(&e->lock);
  222. if (e->state == L2T_STATE_RESOLVING &&
  223. !skb_queue_empty(&e->arpq))
  224. write_l2e(adap, e, 1);
  225. spin_unlock_bh(&e->lock);
  226. }
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(cxgb4_l2t_send);
  231. /*
  232. * Allocate a free L2T entry. Must be called with l2t_data.lock held.
  233. */
  234. static struct l2t_entry *alloc_l2e(struct l2t_data *d)
  235. {
  236. struct l2t_entry *end, *e, **p;
  237. if (!atomic_read(&d->nfree))
  238. return NULL;
  239. /* there's definitely a free entry */
  240. for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
  241. if (atomic_read(&e->refcnt) == 0)
  242. goto found;
  243. for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
  244. ;
  245. found:
  246. d->rover = e + 1;
  247. atomic_dec(&d->nfree);
  248. /*
  249. * The entry we found may be an inactive entry that is
  250. * presently in the hash table. We need to remove it.
  251. */
  252. if (e->state < L2T_STATE_SWITCHING)
  253. for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
  254. if (*p == e) {
  255. *p = e->next;
  256. e->next = NULL;
  257. break;
  258. }
  259. e->state = L2T_STATE_UNUSED;
  260. return e;
  261. }
  262. static struct l2t_entry *find_or_alloc_l2e(struct l2t_data *d, u16 vlan,
  263. u8 port, u8 *dmac)
  264. {
  265. struct l2t_entry *end, *e, **p;
  266. struct l2t_entry *first_free = NULL;
  267. for (e = &d->l2tab[0], end = &d->l2tab[d->l2t_size]; e != end; ++e) {
  268. if (atomic_read(&e->refcnt) == 0) {
  269. if (!first_free)
  270. first_free = e;
  271. } else {
  272. if (e->state == L2T_STATE_SWITCHING) {
  273. if (ether_addr_equal(e->dmac, dmac) &&
  274. (e->vlan == vlan) && (e->lport == port))
  275. goto exists;
  276. }
  277. }
  278. }
  279. if (first_free) {
  280. e = first_free;
  281. goto found;
  282. }
  283. return NULL;
  284. found:
  285. /* The entry we found may be an inactive entry that is
  286. * presently in the hash table. We need to remove it.
  287. */
  288. if (e->state < L2T_STATE_SWITCHING)
  289. for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
  290. if (*p == e) {
  291. *p = e->next;
  292. e->next = NULL;
  293. break;
  294. }
  295. e->state = L2T_STATE_UNUSED;
  296. exists:
  297. return e;
  298. }
  299. /* Called when an L2T entry has no more users. The entry is left in the hash
  300. * table since it is likely to be reused but we also bump nfree to indicate
  301. * that the entry can be reallocated for a different neighbor. We also drop
  302. * the existing neighbor reference in case the neighbor is going away and is
  303. * waiting on our reference.
  304. *
  305. * Because entries can be reallocated to other neighbors once their ref count
  306. * drops to 0 we need to take the entry's lock to avoid races with a new
  307. * incarnation.
  308. */
  309. static void _t4_l2e_free(struct l2t_entry *e)
  310. {
  311. struct l2t_data *d;
  312. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  313. if (e->neigh) {
  314. neigh_release(e->neigh);
  315. e->neigh = NULL;
  316. }
  317. __skb_queue_purge(&e->arpq);
  318. }
  319. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  320. atomic_inc(&d->nfree);
  321. }
  322. /* Locked version of _t4_l2e_free */
  323. static void t4_l2e_free(struct l2t_entry *e)
  324. {
  325. struct l2t_data *d;
  326. spin_lock_bh(&e->lock);
  327. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  328. if (e->neigh) {
  329. neigh_release(e->neigh);
  330. e->neigh = NULL;
  331. }
  332. __skb_queue_purge(&e->arpq);
  333. }
  334. spin_unlock_bh(&e->lock);
  335. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  336. atomic_inc(&d->nfree);
  337. }
  338. void cxgb4_l2t_release(struct l2t_entry *e)
  339. {
  340. if (atomic_dec_and_test(&e->refcnt))
  341. t4_l2e_free(e);
  342. }
  343. EXPORT_SYMBOL(cxgb4_l2t_release);
  344. /*
  345. * Update an L2T entry that was previously used for the same next hop as neigh.
  346. * Must be called with softirqs disabled.
  347. */
  348. static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
  349. {
  350. unsigned int nud_state;
  351. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  352. if (neigh != e->neigh)
  353. neigh_replace(e, neigh);
  354. nud_state = neigh->nud_state;
  355. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
  356. !(nud_state & NUD_VALID))
  357. e->state = L2T_STATE_RESOLVING;
  358. else if (nud_state & NUD_CONNECTED)
  359. e->state = L2T_STATE_VALID;
  360. else
  361. e->state = L2T_STATE_STALE;
  362. spin_unlock(&e->lock);
  363. }
  364. struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
  365. const struct net_device *physdev,
  366. unsigned int priority)
  367. {
  368. u8 lport;
  369. u16 vlan;
  370. struct l2t_entry *e;
  371. unsigned int addr_len = neigh->tbl->key_len;
  372. u32 *addr = (u32 *)neigh->primary_key;
  373. int ifidx = neigh->dev->ifindex;
  374. int hash = addr_hash(d, addr, addr_len, ifidx);
  375. if (neigh->dev->flags & IFF_LOOPBACK)
  376. lport = netdev2pinfo(physdev)->tx_chan + 4;
  377. else
  378. lport = netdev2pinfo(physdev)->lport;
  379. if (is_vlan_dev(neigh->dev)) {
  380. vlan = vlan_dev_vlan_id(neigh->dev);
  381. vlan |= vlan_dev_get_egress_qos_mask(neigh->dev, priority);
  382. } else {
  383. vlan = VLAN_NONE;
  384. }
  385. write_lock_bh(&d->lock);
  386. for (e = d->l2tab[hash].first; e; e = e->next)
  387. if (!addreq(e, addr) && e->ifindex == ifidx &&
  388. e->vlan == vlan && e->lport == lport) {
  389. l2t_hold(d, e);
  390. if (atomic_read(&e->refcnt) == 1)
  391. reuse_entry(e, neigh);
  392. goto done;
  393. }
  394. /* Need to allocate a new entry */
  395. e = alloc_l2e(d);
  396. if (e) {
  397. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  398. e->state = L2T_STATE_RESOLVING;
  399. if (neigh->dev->flags & IFF_LOOPBACK)
  400. memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
  401. memcpy(e->addr, addr, addr_len);
  402. e->ifindex = ifidx;
  403. e->hash = hash;
  404. e->lport = lport;
  405. e->v6 = addr_len == 16;
  406. atomic_set(&e->refcnt, 1);
  407. neigh_replace(e, neigh);
  408. e->vlan = vlan;
  409. e->next = d->l2tab[hash].first;
  410. d->l2tab[hash].first = e;
  411. spin_unlock(&e->lock);
  412. }
  413. done:
  414. write_unlock_bh(&d->lock);
  415. return e;
  416. }
  417. EXPORT_SYMBOL(cxgb4_l2t_get);
  418. u64 cxgb4_select_ntuple(struct net_device *dev,
  419. const struct l2t_entry *l2t)
  420. {
  421. struct adapter *adap = netdev2adap(dev);
  422. struct tp_params *tp = &adap->params.tp;
  423. u64 ntuple = 0;
  424. /* Initialize each of the fields which we care about which are present
  425. * in the Compressed Filter Tuple.
  426. */
  427. if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
  428. ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
  429. if (tp->port_shift >= 0)
  430. ntuple |= (u64)l2t->lport << tp->port_shift;
  431. if (tp->protocol_shift >= 0)
  432. ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
  433. if (tp->vnic_shift >= 0 && (tp->ingress_config & VNIC_F)) {
  434. struct port_info *pi = (struct port_info *)netdev_priv(dev);
  435. ntuple |= (u64)(FT_VNID_ID_VF_V(pi->vin) |
  436. FT_VNID_ID_PF_V(adap->pf) |
  437. FT_VNID_ID_VLD_V(pi->vivld)) << tp->vnic_shift;
  438. }
  439. return ntuple;
  440. }
  441. EXPORT_SYMBOL(cxgb4_select_ntuple);
  442. /*
  443. * Called when the host's neighbor layer makes a change to some entry that is
  444. * loaded into the HW L2 table.
  445. */
  446. void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
  447. {
  448. unsigned int addr_len = neigh->tbl->key_len;
  449. u32 *addr = (u32 *) neigh->primary_key;
  450. int hash, ifidx = neigh->dev->ifindex;
  451. struct sk_buff_head *arpq = NULL;
  452. struct l2t_data *d = adap->l2t;
  453. struct l2t_entry *e;
  454. hash = addr_hash(d, addr, addr_len, ifidx);
  455. read_lock_bh(&d->lock);
  456. for (e = d->l2tab[hash].first; e; e = e->next)
  457. if (!addreq(e, addr) && e->ifindex == ifidx) {
  458. spin_lock(&e->lock);
  459. if (atomic_read(&e->refcnt))
  460. goto found;
  461. spin_unlock(&e->lock);
  462. break;
  463. }
  464. read_unlock_bh(&d->lock);
  465. return;
  466. found:
  467. read_unlock(&d->lock);
  468. if (neigh != e->neigh)
  469. neigh_replace(e, neigh);
  470. if (e->state == L2T_STATE_RESOLVING) {
  471. if (neigh->nud_state & NUD_FAILED) {
  472. arpq = &e->arpq;
  473. } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
  474. !skb_queue_empty(&e->arpq)) {
  475. write_l2e(adap, e, 1);
  476. }
  477. } else {
  478. e->state = neigh->nud_state & NUD_CONNECTED ?
  479. L2T_STATE_VALID : L2T_STATE_STALE;
  480. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
  481. write_l2e(adap, e, 0);
  482. }
  483. if (arpq) {
  484. struct sk_buff *skb;
  485. /* Called when address resolution fails for an L2T
  486. * entry to handle packets on the arpq head. If a
  487. * packet specifies a failure handler it is invoked,
  488. * otherwise the packet is sent to the device.
  489. */
  490. while ((skb = __skb_dequeue(&e->arpq)) != NULL) {
  491. const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
  492. spin_unlock(&e->lock);
  493. if (cb->arp_err_handler)
  494. cb->arp_err_handler(cb->handle, skb);
  495. else
  496. t4_ofld_send(adap, skb);
  497. spin_lock(&e->lock);
  498. }
  499. }
  500. spin_unlock_bh(&e->lock);
  501. }
  502. /* Allocate an L2T entry for use by a switching rule. Such need to be
  503. * explicitly freed and while busy they are not on any hash chain, so normal
  504. * address resolution updates do not see them.
  505. */
  506. struct l2t_entry *t4_l2t_alloc_switching(struct adapter *adap, u16 vlan,
  507. u8 port, u8 *eth_addr)
  508. {
  509. struct l2t_data *d = adap->l2t;
  510. struct l2t_entry *e;
  511. int ret;
  512. write_lock_bh(&d->lock);
  513. e = find_or_alloc_l2e(d, vlan, port, eth_addr);
  514. if (e) {
  515. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  516. if (!atomic_read(&e->refcnt)) {
  517. e->state = L2T_STATE_SWITCHING;
  518. e->vlan = vlan;
  519. e->lport = port;
  520. ether_addr_copy(e->dmac, eth_addr);
  521. atomic_set(&e->refcnt, 1);
  522. ret = write_l2e(adap, e, 0);
  523. if (ret < 0) {
  524. _t4_l2e_free(e);
  525. spin_unlock(&e->lock);
  526. write_unlock_bh(&d->lock);
  527. return NULL;
  528. }
  529. } else {
  530. atomic_inc(&e->refcnt);
  531. }
  532. spin_unlock(&e->lock);
  533. }
  534. write_unlock_bh(&d->lock);
  535. return e;
  536. }
  537. /**
  538. * cxgb4_l2t_alloc_switching - Allocates an L2T entry for switch filters
  539. * @dev: net_device pointer
  540. * @vlan: VLAN Id
  541. * @port: Associated port
  542. * @dmac: Destination MAC address to add to L2T
  543. * Returns pointer to the allocated l2t entry
  544. *
  545. * Allocates an L2T entry for use by switching rule of a filter
  546. */
  547. struct l2t_entry *cxgb4_l2t_alloc_switching(struct net_device *dev, u16 vlan,
  548. u8 port, u8 *dmac)
  549. {
  550. struct adapter *adap = netdev2adap(dev);
  551. return t4_l2t_alloc_switching(adap, vlan, port, dmac);
  552. }
  553. EXPORT_SYMBOL(cxgb4_l2t_alloc_switching);
  554. struct l2t_data *t4_init_l2t(unsigned int l2t_start, unsigned int l2t_end)
  555. {
  556. unsigned int l2t_size;
  557. int i;
  558. struct l2t_data *d;
  559. if (l2t_start >= l2t_end || l2t_end >= L2T_SIZE)
  560. return NULL;
  561. l2t_size = l2t_end - l2t_start + 1;
  562. if (l2t_size < L2T_MIN_HASH_BUCKETS)
  563. return NULL;
  564. d = kvzalloc(struct_size(d, l2tab, l2t_size), GFP_KERNEL);
  565. if (!d)
  566. return NULL;
  567. d->l2t_start = l2t_start;
  568. d->l2t_size = l2t_size;
  569. d->rover = d->l2tab;
  570. atomic_set(&d->nfree, l2t_size);
  571. rwlock_init(&d->lock);
  572. for (i = 0; i < d->l2t_size; ++i) {
  573. d->l2tab[i].idx = i;
  574. d->l2tab[i].state = L2T_STATE_UNUSED;
  575. spin_lock_init(&d->l2tab[i].lock);
  576. atomic_set(&d->l2tab[i].refcnt, 0);
  577. skb_queue_head_init(&d->l2tab[i].arpq);
  578. }
  579. return d;
  580. }
  581. static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
  582. {
  583. struct l2t_data *d = seq->private;
  584. return pos >= d->l2t_size ? NULL : &d->l2tab[pos];
  585. }
  586. static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
  587. {
  588. return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  589. }
  590. static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  591. {
  592. v = l2t_get_idx(seq, *pos);
  593. ++(*pos);
  594. return v;
  595. }
  596. static void l2t_seq_stop(struct seq_file *seq, void *v)
  597. {
  598. }
  599. static char l2e_state(const struct l2t_entry *e)
  600. {
  601. switch (e->state) {
  602. case L2T_STATE_VALID: return 'V';
  603. case L2T_STATE_STALE: return 'S';
  604. case L2T_STATE_SYNC_WRITE: return 'W';
  605. case L2T_STATE_RESOLVING:
  606. return skb_queue_empty(&e->arpq) ? 'R' : 'A';
  607. case L2T_STATE_SWITCHING: return 'X';
  608. default:
  609. return 'U';
  610. }
  611. }
  612. bool cxgb4_check_l2t_valid(struct l2t_entry *e)
  613. {
  614. bool valid;
  615. spin_lock(&e->lock);
  616. valid = (e->state == L2T_STATE_VALID);
  617. spin_unlock(&e->lock);
  618. return valid;
  619. }
  620. EXPORT_SYMBOL(cxgb4_check_l2t_valid);
  621. static int l2t_seq_show(struct seq_file *seq, void *v)
  622. {
  623. if (v == SEQ_START_TOKEN)
  624. seq_puts(seq, " Idx IP address "
  625. "Ethernet address VLAN/P LP State Users Port\n");
  626. else {
  627. char ip[60];
  628. struct l2t_data *d = seq->private;
  629. struct l2t_entry *e = v;
  630. spin_lock_bh(&e->lock);
  631. if (e->state == L2T_STATE_SWITCHING)
  632. ip[0] = '\0';
  633. else
  634. sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
  635. seq_printf(seq, "%4u %-25s %17pM %4d %u %2u %c %5u %s\n",
  636. e->idx + d->l2t_start, ip, e->dmac,
  637. e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
  638. l2e_state(e), atomic_read(&e->refcnt),
  639. e->neigh ? e->neigh->dev->name : "");
  640. spin_unlock_bh(&e->lock);
  641. }
  642. return 0;
  643. }
  644. static const struct seq_operations l2t_seq_ops = {
  645. .start = l2t_seq_start,
  646. .next = l2t_seq_next,
  647. .stop = l2t_seq_stop,
  648. .show = l2t_seq_show
  649. };
  650. static int l2t_seq_open(struct inode *inode, struct file *file)
  651. {
  652. int rc = seq_open(file, &l2t_seq_ops);
  653. if (!rc) {
  654. struct adapter *adap = inode->i_private;
  655. struct seq_file *seq = file->private_data;
  656. seq->private = adap->l2t;
  657. }
  658. return rc;
  659. }
  660. const struct file_operations t4_l2t_fops = {
  661. .owner = THIS_MODULE,
  662. .open = l2t_seq_open,
  663. .read = seq_read,
  664. .llseek = seq_lseek,
  665. .release = seq_release,
  666. };