l2t.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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[0]; /* 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. /* fall through */
  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. struct sk_buff *skb;
  313. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  314. if (e->neigh) {
  315. neigh_release(e->neigh);
  316. e->neigh = NULL;
  317. }
  318. while ((skb = __skb_dequeue(&e->arpq)) != NULL)
  319. kfree_skb(skb);
  320. }
  321. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  322. atomic_inc(&d->nfree);
  323. }
  324. /* Locked version of _t4_l2e_free */
  325. static void t4_l2e_free(struct l2t_entry *e)
  326. {
  327. struct l2t_data *d;
  328. struct sk_buff *skb;
  329. spin_lock_bh(&e->lock);
  330. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  331. if (e->neigh) {
  332. neigh_release(e->neigh);
  333. e->neigh = NULL;
  334. }
  335. while ((skb = __skb_dequeue(&e->arpq)) != NULL)
  336. kfree_skb(skb);
  337. }
  338. spin_unlock_bh(&e->lock);
  339. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  340. atomic_inc(&d->nfree);
  341. }
  342. void cxgb4_l2t_release(struct l2t_entry *e)
  343. {
  344. if (atomic_dec_and_test(&e->refcnt))
  345. t4_l2e_free(e);
  346. }
  347. EXPORT_SYMBOL(cxgb4_l2t_release);
  348. /*
  349. * Update an L2T entry that was previously used for the same next hop as neigh.
  350. * Must be called with softirqs disabled.
  351. */
  352. static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
  353. {
  354. unsigned int nud_state;
  355. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  356. if (neigh != e->neigh)
  357. neigh_replace(e, neigh);
  358. nud_state = neigh->nud_state;
  359. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
  360. !(nud_state & NUD_VALID))
  361. e->state = L2T_STATE_RESOLVING;
  362. else if (nud_state & NUD_CONNECTED)
  363. e->state = L2T_STATE_VALID;
  364. else
  365. e->state = L2T_STATE_STALE;
  366. spin_unlock(&e->lock);
  367. }
  368. struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
  369. const struct net_device *physdev,
  370. unsigned int priority)
  371. {
  372. u8 lport;
  373. u16 vlan;
  374. struct l2t_entry *e;
  375. unsigned int addr_len = neigh->tbl->key_len;
  376. u32 *addr = (u32 *)neigh->primary_key;
  377. int ifidx = neigh->dev->ifindex;
  378. int hash = addr_hash(d, addr, addr_len, ifidx);
  379. if (neigh->dev->flags & IFF_LOOPBACK)
  380. lport = netdev2pinfo(physdev)->tx_chan + 4;
  381. else
  382. lport = netdev2pinfo(physdev)->lport;
  383. if (is_vlan_dev(neigh->dev))
  384. vlan = vlan_dev_vlan_id(neigh->dev);
  385. else
  386. vlan = VLAN_NONE;
  387. write_lock_bh(&d->lock);
  388. for (e = d->l2tab[hash].first; e; e = e->next)
  389. if (!addreq(e, addr) && e->ifindex == ifidx &&
  390. e->vlan == vlan && e->lport == lport) {
  391. l2t_hold(d, e);
  392. if (atomic_read(&e->refcnt) == 1)
  393. reuse_entry(e, neigh);
  394. goto done;
  395. }
  396. /* Need to allocate a new entry */
  397. e = alloc_l2e(d);
  398. if (e) {
  399. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  400. e->state = L2T_STATE_RESOLVING;
  401. if (neigh->dev->flags & IFF_LOOPBACK)
  402. memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
  403. memcpy(e->addr, addr, addr_len);
  404. e->ifindex = ifidx;
  405. e->hash = hash;
  406. e->lport = lport;
  407. e->v6 = addr_len == 16;
  408. atomic_set(&e->refcnt, 1);
  409. neigh_replace(e, neigh);
  410. e->vlan = vlan;
  411. e->next = d->l2tab[hash].first;
  412. d->l2tab[hash].first = e;
  413. spin_unlock(&e->lock);
  414. }
  415. done:
  416. write_unlock_bh(&d->lock);
  417. return e;
  418. }
  419. EXPORT_SYMBOL(cxgb4_l2t_get);
  420. u64 cxgb4_select_ntuple(struct net_device *dev,
  421. const struct l2t_entry *l2t)
  422. {
  423. struct adapter *adap = netdev2adap(dev);
  424. struct tp_params *tp = &adap->params.tp;
  425. u64 ntuple = 0;
  426. /* Initialize each of the fields which we care about which are present
  427. * in the Compressed Filter Tuple.
  428. */
  429. if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
  430. ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
  431. if (tp->port_shift >= 0)
  432. ntuple |= (u64)l2t->lport << tp->port_shift;
  433. if (tp->protocol_shift >= 0)
  434. ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
  435. if (tp->vnic_shift >= 0 && (tp->ingress_config & VNIC_F)) {
  436. u32 viid = cxgb4_port_viid(dev);
  437. u32 vf = FW_VIID_VIN_G(viid);
  438. u32 pf = FW_VIID_PFN_G(viid);
  439. u32 vld = FW_VIID_VIVLD_G(viid);
  440. ntuple |= (u64)(FT_VNID_ID_VF_V(vf) |
  441. FT_VNID_ID_PF_V(pf) |
  442. FT_VNID_ID_VLD_V(vld)) << tp->vnic_shift;
  443. }
  444. return ntuple;
  445. }
  446. EXPORT_SYMBOL(cxgb4_select_ntuple);
  447. /*
  448. * Called when address resolution fails for an L2T entry to handle packets
  449. * on the arpq head. If a packet specifies a failure handler it is invoked,
  450. * otherwise the packet is sent to the device.
  451. */
  452. static void handle_failed_resolution(struct adapter *adap, struct l2t_entry *e)
  453. {
  454. struct sk_buff *skb;
  455. while ((skb = __skb_dequeue(&e->arpq)) != NULL) {
  456. const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
  457. spin_unlock(&e->lock);
  458. if (cb->arp_err_handler)
  459. cb->arp_err_handler(cb->handle, skb);
  460. else
  461. t4_ofld_send(adap, skb);
  462. spin_lock(&e->lock);
  463. }
  464. }
  465. /*
  466. * Called when the host's neighbor layer makes a change to some entry that is
  467. * loaded into the HW L2 table.
  468. */
  469. void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
  470. {
  471. struct l2t_entry *e;
  472. struct sk_buff_head *arpq = NULL;
  473. struct l2t_data *d = adap->l2t;
  474. unsigned int addr_len = neigh->tbl->key_len;
  475. u32 *addr = (u32 *) neigh->primary_key;
  476. int ifidx = neigh->dev->ifindex;
  477. int hash = addr_hash(d, addr, addr_len, ifidx);
  478. read_lock_bh(&d->lock);
  479. for (e = d->l2tab[hash].first; e; e = e->next)
  480. if (!addreq(e, addr) && e->ifindex == ifidx) {
  481. spin_lock(&e->lock);
  482. if (atomic_read(&e->refcnt))
  483. goto found;
  484. spin_unlock(&e->lock);
  485. break;
  486. }
  487. read_unlock_bh(&d->lock);
  488. return;
  489. found:
  490. read_unlock(&d->lock);
  491. if (neigh != e->neigh)
  492. neigh_replace(e, neigh);
  493. if (e->state == L2T_STATE_RESOLVING) {
  494. if (neigh->nud_state & NUD_FAILED) {
  495. arpq = &e->arpq;
  496. } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
  497. !skb_queue_empty(&e->arpq)) {
  498. write_l2e(adap, e, 1);
  499. }
  500. } else {
  501. e->state = neigh->nud_state & NUD_CONNECTED ?
  502. L2T_STATE_VALID : L2T_STATE_STALE;
  503. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
  504. write_l2e(adap, e, 0);
  505. }
  506. if (arpq)
  507. handle_failed_resolution(adap, e);
  508. spin_unlock_bh(&e->lock);
  509. }
  510. /* Allocate an L2T entry for use by a switching rule. Such need to be
  511. * explicitly freed and while busy they are not on any hash chain, so normal
  512. * address resolution updates do not see them.
  513. */
  514. struct l2t_entry *t4_l2t_alloc_switching(struct adapter *adap, u16 vlan,
  515. u8 port, u8 *eth_addr)
  516. {
  517. struct l2t_data *d = adap->l2t;
  518. struct l2t_entry *e;
  519. int ret;
  520. write_lock_bh(&d->lock);
  521. e = find_or_alloc_l2e(d, vlan, port, eth_addr);
  522. if (e) {
  523. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  524. if (!atomic_read(&e->refcnt)) {
  525. e->state = L2T_STATE_SWITCHING;
  526. e->vlan = vlan;
  527. e->lport = port;
  528. ether_addr_copy(e->dmac, eth_addr);
  529. atomic_set(&e->refcnt, 1);
  530. ret = write_l2e(adap, e, 0);
  531. if (ret < 0) {
  532. _t4_l2e_free(e);
  533. spin_unlock(&e->lock);
  534. write_unlock_bh(&d->lock);
  535. return NULL;
  536. }
  537. } else {
  538. atomic_inc(&e->refcnt);
  539. }
  540. spin_unlock(&e->lock);
  541. }
  542. write_unlock_bh(&d->lock);
  543. return e;
  544. }
  545. /**
  546. * @dev: net_device pointer
  547. * @vlan: VLAN Id
  548. * @port: Associated port
  549. * @dmac: Destination MAC address to add to L2T
  550. * Returns pointer to the allocated l2t entry
  551. *
  552. * Allocates an L2T entry for use by switching rule of a filter
  553. */
  554. struct l2t_entry *cxgb4_l2t_alloc_switching(struct net_device *dev, u16 vlan,
  555. u8 port, u8 *dmac)
  556. {
  557. struct adapter *adap = netdev2adap(dev);
  558. return t4_l2t_alloc_switching(adap, vlan, port, dmac);
  559. }
  560. EXPORT_SYMBOL(cxgb4_l2t_alloc_switching);
  561. struct l2t_data *t4_init_l2t(unsigned int l2t_start, unsigned int l2t_end)
  562. {
  563. unsigned int l2t_size;
  564. int i;
  565. struct l2t_data *d;
  566. if (l2t_start >= l2t_end || l2t_end >= L2T_SIZE)
  567. return NULL;
  568. l2t_size = l2t_end - l2t_start + 1;
  569. if (l2t_size < L2T_MIN_HASH_BUCKETS)
  570. return NULL;
  571. d = kvzalloc(sizeof(*d) + l2t_size * sizeof(struct l2t_entry), GFP_KERNEL);
  572. if (!d)
  573. return NULL;
  574. d->l2t_start = l2t_start;
  575. d->l2t_size = l2t_size;
  576. d->rover = d->l2tab;
  577. atomic_set(&d->nfree, l2t_size);
  578. rwlock_init(&d->lock);
  579. for (i = 0; i < d->l2t_size; ++i) {
  580. d->l2tab[i].idx = i;
  581. d->l2tab[i].state = L2T_STATE_UNUSED;
  582. spin_lock_init(&d->l2tab[i].lock);
  583. atomic_set(&d->l2tab[i].refcnt, 0);
  584. skb_queue_head_init(&d->l2tab[i].arpq);
  585. }
  586. return d;
  587. }
  588. static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
  589. {
  590. struct l2t_data *d = seq->private;
  591. return pos >= d->l2t_size ? NULL : &d->l2tab[pos];
  592. }
  593. static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
  594. {
  595. return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  596. }
  597. static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  598. {
  599. v = l2t_get_idx(seq, *pos);
  600. ++(*pos);
  601. return v;
  602. }
  603. static void l2t_seq_stop(struct seq_file *seq, void *v)
  604. {
  605. }
  606. static char l2e_state(const struct l2t_entry *e)
  607. {
  608. switch (e->state) {
  609. case L2T_STATE_VALID: return 'V';
  610. case L2T_STATE_STALE: return 'S';
  611. case L2T_STATE_SYNC_WRITE: return 'W';
  612. case L2T_STATE_RESOLVING:
  613. return skb_queue_empty(&e->arpq) ? 'R' : 'A';
  614. case L2T_STATE_SWITCHING: return 'X';
  615. default:
  616. return 'U';
  617. }
  618. }
  619. static int l2t_seq_show(struct seq_file *seq, void *v)
  620. {
  621. if (v == SEQ_START_TOKEN)
  622. seq_puts(seq, " Idx IP address "
  623. "Ethernet address VLAN/P LP State Users Port\n");
  624. else {
  625. char ip[60];
  626. struct l2t_data *d = seq->private;
  627. struct l2t_entry *e = v;
  628. spin_lock_bh(&e->lock);
  629. if (e->state == L2T_STATE_SWITCHING)
  630. ip[0] = '\0';
  631. else
  632. sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
  633. seq_printf(seq, "%4u %-25s %17pM %4d %u %2u %c %5u %s\n",
  634. e->idx + d->l2t_start, ip, e->dmac,
  635. e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
  636. l2e_state(e), atomic_read(&e->refcnt),
  637. e->neigh ? e->neigh->dev->name : "");
  638. spin_unlock_bh(&e->lock);
  639. }
  640. return 0;
  641. }
  642. static const struct seq_operations l2t_seq_ops = {
  643. .start = l2t_seq_start,
  644. .next = l2t_seq_next,
  645. .stop = l2t_seq_stop,
  646. .show = l2t_seq_show
  647. };
  648. static int l2t_seq_open(struct inode *inode, struct file *file)
  649. {
  650. int rc = seq_open(file, &l2t_seq_ops);
  651. if (!rc) {
  652. struct adapter *adap = inode->i_private;
  653. struct seq_file *seq = file->private_data;
  654. seq->private = adap->l2t;
  655. }
  656. return rc;
  657. }
  658. const struct file_operations t4_l2t_fops = {
  659. .owner = THIS_MODULE,
  660. .open = l2t_seq_open,
  661. .read = seq_read,
  662. .llseek = seq_lseek,
  663. .release = seq_release,
  664. };