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 = (struct cpl_l2t_write_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. case L2T_STATE_VALID: /* fast-path, send the packet on */
  207. return t4_ofld_send(adap, skb);
  208. case L2T_STATE_RESOLVING:
  209. case L2T_STATE_SYNC_WRITE:
  210. spin_lock_bh(&e->lock);
  211. if (e->state != L2T_STATE_SYNC_WRITE &&
  212. e->state != L2T_STATE_RESOLVING) {
  213. spin_unlock_bh(&e->lock);
  214. goto again;
  215. }
  216. arpq_enqueue(e, skb);
  217. spin_unlock_bh(&e->lock);
  218. if (e->state == L2T_STATE_RESOLVING &&
  219. !neigh_event_send(e->neigh, NULL)) {
  220. spin_lock_bh(&e->lock);
  221. if (e->state == L2T_STATE_RESOLVING &&
  222. !skb_queue_empty(&e->arpq))
  223. write_l2e(adap, e, 1);
  224. spin_unlock_bh(&e->lock);
  225. }
  226. }
  227. return 0;
  228. }
  229. EXPORT_SYMBOL(cxgb4_l2t_send);
  230. /*
  231. * Allocate a free L2T entry. Must be called with l2t_data.lock held.
  232. */
  233. static struct l2t_entry *alloc_l2e(struct l2t_data *d)
  234. {
  235. struct l2t_entry *end, *e, **p;
  236. if (!atomic_read(&d->nfree))
  237. return NULL;
  238. /* there's definitely a free entry */
  239. for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
  240. if (atomic_read(&e->refcnt) == 0)
  241. goto found;
  242. for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
  243. ;
  244. found:
  245. d->rover = e + 1;
  246. atomic_dec(&d->nfree);
  247. /*
  248. * The entry we found may be an inactive entry that is
  249. * presently in the hash table. We need to remove it.
  250. */
  251. if (e->state < L2T_STATE_SWITCHING)
  252. for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
  253. if (*p == e) {
  254. *p = e->next;
  255. e->next = NULL;
  256. break;
  257. }
  258. e->state = L2T_STATE_UNUSED;
  259. return e;
  260. }
  261. static struct l2t_entry *find_or_alloc_l2e(struct l2t_data *d, u16 vlan,
  262. u8 port, u8 *dmac)
  263. {
  264. struct l2t_entry *end, *e, **p;
  265. struct l2t_entry *first_free = NULL;
  266. for (e = &d->l2tab[0], end = &d->l2tab[d->l2t_size]; e != end; ++e) {
  267. if (atomic_read(&e->refcnt) == 0) {
  268. if (!first_free)
  269. first_free = e;
  270. } else {
  271. if (e->state == L2T_STATE_SWITCHING) {
  272. if (ether_addr_equal(e->dmac, dmac) &&
  273. (e->vlan == vlan) && (e->lport == port))
  274. goto exists;
  275. }
  276. }
  277. }
  278. if (first_free) {
  279. e = first_free;
  280. goto found;
  281. }
  282. return NULL;
  283. found:
  284. /* The entry we found may be an inactive entry that is
  285. * presently in the hash table. We need to remove it.
  286. */
  287. if (e->state < L2T_STATE_SWITCHING)
  288. for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
  289. if (*p == e) {
  290. *p = e->next;
  291. e->next = NULL;
  292. break;
  293. }
  294. e->state = L2T_STATE_UNUSED;
  295. exists:
  296. return e;
  297. }
  298. /* Called when an L2T entry has no more users. The entry is left in the hash
  299. * table since it is likely to be reused but we also bump nfree to indicate
  300. * that the entry can be reallocated for a different neighbor. We also drop
  301. * the existing neighbor reference in case the neighbor is going away and is
  302. * waiting on our reference.
  303. *
  304. * Because entries can be reallocated to other neighbors once their ref count
  305. * drops to 0 we need to take the entry's lock to avoid races with a new
  306. * incarnation.
  307. */
  308. static void _t4_l2e_free(struct l2t_entry *e)
  309. {
  310. struct l2t_data *d;
  311. struct sk_buff *skb;
  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. while ((skb = __skb_dequeue(&e->arpq)) != NULL)
  318. kfree_skb(skb);
  319. }
  320. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  321. atomic_inc(&d->nfree);
  322. }
  323. /* Locked version of _t4_l2e_free */
  324. static void t4_l2e_free(struct l2t_entry *e)
  325. {
  326. struct l2t_data *d;
  327. struct sk_buff *skb;
  328. spin_lock_bh(&e->lock);
  329. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  330. if (e->neigh) {
  331. neigh_release(e->neigh);
  332. e->neigh = NULL;
  333. }
  334. while ((skb = __skb_dequeue(&e->arpq)) != NULL)
  335. kfree_skb(skb);
  336. }
  337. spin_unlock_bh(&e->lock);
  338. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  339. atomic_inc(&d->nfree);
  340. }
  341. void cxgb4_l2t_release(struct l2t_entry *e)
  342. {
  343. if (atomic_dec_and_test(&e->refcnt))
  344. t4_l2e_free(e);
  345. }
  346. EXPORT_SYMBOL(cxgb4_l2t_release);
  347. /*
  348. * Update an L2T entry that was previously used for the same next hop as neigh.
  349. * Must be called with softirqs disabled.
  350. */
  351. static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
  352. {
  353. unsigned int nud_state;
  354. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  355. if (neigh != e->neigh)
  356. neigh_replace(e, neigh);
  357. nud_state = neigh->nud_state;
  358. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
  359. !(nud_state & NUD_VALID))
  360. e->state = L2T_STATE_RESOLVING;
  361. else if (nud_state & NUD_CONNECTED)
  362. e->state = L2T_STATE_VALID;
  363. else
  364. e->state = L2T_STATE_STALE;
  365. spin_unlock(&e->lock);
  366. }
  367. struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
  368. const struct net_device *physdev,
  369. unsigned int priority)
  370. {
  371. u8 lport;
  372. u16 vlan;
  373. struct l2t_entry *e;
  374. int addr_len = neigh->tbl->key_len;
  375. u32 *addr = (u32 *)neigh->primary_key;
  376. int ifidx = neigh->dev->ifindex;
  377. int hash = addr_hash(d, addr, addr_len, ifidx);
  378. if (neigh->dev->flags & IFF_LOOPBACK)
  379. lport = netdev2pinfo(physdev)->tx_chan + 4;
  380. else
  381. lport = netdev2pinfo(physdev)->lport;
  382. if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
  383. vlan = vlan_dev_vlan_id(neigh->dev);
  384. else
  385. vlan = VLAN_NONE;
  386. write_lock_bh(&d->lock);
  387. for (e = d->l2tab[hash].first; e; e = e->next)
  388. if (!addreq(e, addr) && e->ifindex == ifidx &&
  389. e->vlan == vlan && e->lport == lport) {
  390. l2t_hold(d, e);
  391. if (atomic_read(&e->refcnt) == 1)
  392. reuse_entry(e, neigh);
  393. goto done;
  394. }
  395. /* Need to allocate a new entry */
  396. e = alloc_l2e(d);
  397. if (e) {
  398. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  399. e->state = L2T_STATE_RESOLVING;
  400. if (neigh->dev->flags & IFF_LOOPBACK)
  401. memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
  402. memcpy(e->addr, addr, addr_len);
  403. e->ifindex = ifidx;
  404. e->hash = hash;
  405. e->lport = lport;
  406. e->v6 = addr_len == 16;
  407. atomic_set(&e->refcnt, 1);
  408. neigh_replace(e, neigh);
  409. e->vlan = vlan;
  410. e->next = d->l2tab[hash].first;
  411. d->l2tab[hash].first = e;
  412. spin_unlock(&e->lock);
  413. }
  414. done:
  415. write_unlock_bh(&d->lock);
  416. return e;
  417. }
  418. EXPORT_SYMBOL(cxgb4_l2t_get);
  419. u64 cxgb4_select_ntuple(struct net_device *dev,
  420. const struct l2t_entry *l2t)
  421. {
  422. struct adapter *adap = netdev2adap(dev);
  423. struct tp_params *tp = &adap->params.tp;
  424. u64 ntuple = 0;
  425. /* Initialize each of the fields which we care about which are present
  426. * in the Compressed Filter Tuple.
  427. */
  428. if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
  429. ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
  430. if (tp->port_shift >= 0)
  431. ntuple |= (u64)l2t->lport << tp->port_shift;
  432. if (tp->protocol_shift >= 0)
  433. ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
  434. if (tp->vnic_shift >= 0) {
  435. u32 viid = cxgb4_port_viid(dev);
  436. u32 vf = FW_VIID_VIN_G(viid);
  437. u32 pf = FW_VIID_PFN_G(viid);
  438. u32 vld = FW_VIID_VIVLD_G(viid);
  439. ntuple |= (u64)(FT_VNID_ID_VF_V(vf) |
  440. FT_VNID_ID_PF_V(pf) |
  441. FT_VNID_ID_VLD_V(vld)) << tp->vnic_shift;
  442. }
  443. return ntuple;
  444. }
  445. EXPORT_SYMBOL(cxgb4_select_ntuple);
  446. /*
  447. * Called when address resolution fails for an L2T entry to handle packets
  448. * on the arpq head. If a packet specifies a failure handler it is invoked,
  449. * otherwise the packet is sent to the device.
  450. */
  451. static void handle_failed_resolution(struct adapter *adap, struct l2t_entry *e)
  452. {
  453. struct sk_buff *skb;
  454. while ((skb = __skb_dequeue(&e->arpq)) != NULL) {
  455. const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
  456. spin_unlock(&e->lock);
  457. if (cb->arp_err_handler)
  458. cb->arp_err_handler(cb->handle, skb);
  459. else
  460. t4_ofld_send(adap, skb);
  461. spin_lock(&e->lock);
  462. }
  463. }
  464. /*
  465. * Called when the host's neighbor layer makes a change to some entry that is
  466. * loaded into the HW L2 table.
  467. */
  468. void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
  469. {
  470. struct l2t_entry *e;
  471. struct sk_buff_head *arpq = NULL;
  472. struct l2t_data *d = adap->l2t;
  473. int addr_len = neigh->tbl->key_len;
  474. u32 *addr = (u32 *) neigh->primary_key;
  475. int ifidx = neigh->dev->ifindex;
  476. int hash = addr_hash(d, addr, addr_len, ifidx);
  477. read_lock_bh(&d->lock);
  478. for (e = d->l2tab[hash].first; e; e = e->next)
  479. if (!addreq(e, addr) && e->ifindex == ifidx) {
  480. spin_lock(&e->lock);
  481. if (atomic_read(&e->refcnt))
  482. goto found;
  483. spin_unlock(&e->lock);
  484. break;
  485. }
  486. read_unlock_bh(&d->lock);
  487. return;
  488. found:
  489. read_unlock(&d->lock);
  490. if (neigh != e->neigh)
  491. neigh_replace(e, neigh);
  492. if (e->state == L2T_STATE_RESOLVING) {
  493. if (neigh->nud_state & NUD_FAILED) {
  494. arpq = &e->arpq;
  495. } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
  496. !skb_queue_empty(&e->arpq)) {
  497. write_l2e(adap, e, 1);
  498. }
  499. } else {
  500. e->state = neigh->nud_state & NUD_CONNECTED ?
  501. L2T_STATE_VALID : L2T_STATE_STALE;
  502. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
  503. write_l2e(adap, e, 0);
  504. }
  505. if (arpq)
  506. handle_failed_resolution(adap, e);
  507. spin_unlock_bh(&e->lock);
  508. }
  509. /* Allocate an L2T entry for use by a switching rule. Such need to be
  510. * explicitly freed and while busy they are not on any hash chain, so normal
  511. * address resolution updates do not see them.
  512. */
  513. struct l2t_entry *t4_l2t_alloc_switching(struct adapter *adap, u16 vlan,
  514. u8 port, u8 *eth_addr)
  515. {
  516. struct l2t_data *d = adap->l2t;
  517. struct l2t_entry *e;
  518. int ret;
  519. write_lock_bh(&d->lock);
  520. e = find_or_alloc_l2e(d, vlan, port, eth_addr);
  521. if (e) {
  522. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  523. if (!atomic_read(&e->refcnt)) {
  524. e->state = L2T_STATE_SWITCHING;
  525. e->vlan = vlan;
  526. e->lport = port;
  527. ether_addr_copy(e->dmac, eth_addr);
  528. atomic_set(&e->refcnt, 1);
  529. ret = write_l2e(adap, e, 0);
  530. if (ret < 0) {
  531. _t4_l2e_free(e);
  532. spin_unlock(&e->lock);
  533. write_unlock_bh(&d->lock);
  534. return NULL;
  535. }
  536. } else {
  537. atomic_inc(&e->refcnt);
  538. }
  539. spin_unlock(&e->lock);
  540. }
  541. write_unlock_bh(&d->lock);
  542. return e;
  543. }
  544. /**
  545. * @dev: net_device pointer
  546. * @vlan: VLAN Id
  547. * @port: Associated port
  548. * @dmac: Destination MAC address to add to L2T
  549. * Returns pointer to the allocated l2t entry
  550. *
  551. * Allocates an L2T entry for use by switching rule of a filter
  552. */
  553. struct l2t_entry *cxgb4_l2t_alloc_switching(struct net_device *dev, u16 vlan,
  554. u8 port, u8 *dmac)
  555. {
  556. struct adapter *adap = netdev2adap(dev);
  557. return t4_l2t_alloc_switching(adap, vlan, port, dmac);
  558. }
  559. EXPORT_SYMBOL(cxgb4_l2t_alloc_switching);
  560. struct l2t_data *t4_init_l2t(unsigned int l2t_start, unsigned int l2t_end)
  561. {
  562. unsigned int l2t_size;
  563. int i;
  564. struct l2t_data *d;
  565. if (l2t_start >= l2t_end || l2t_end >= L2T_SIZE)
  566. return NULL;
  567. l2t_size = l2t_end - l2t_start + 1;
  568. if (l2t_size < L2T_MIN_HASH_BUCKETS)
  569. return NULL;
  570. d = t4_alloc_mem(sizeof(*d) + l2t_size * sizeof(struct l2t_entry));
  571. if (!d)
  572. return NULL;
  573. d->l2t_start = l2t_start;
  574. d->l2t_size = l2t_size;
  575. d->rover = d->l2tab;
  576. atomic_set(&d->nfree, l2t_size);
  577. rwlock_init(&d->lock);
  578. for (i = 0; i < d->l2t_size; ++i) {
  579. d->l2tab[i].idx = i;
  580. d->l2tab[i].state = L2T_STATE_UNUSED;
  581. spin_lock_init(&d->l2tab[i].lock);
  582. atomic_set(&d->l2tab[i].refcnt, 0);
  583. skb_queue_head_init(&d->l2tab[i].arpq);
  584. }
  585. return d;
  586. }
  587. static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
  588. {
  589. struct l2t_data *d = seq->private;
  590. return pos >= d->l2t_size ? NULL : &d->l2tab[pos];
  591. }
  592. static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
  593. {
  594. return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  595. }
  596. static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  597. {
  598. v = l2t_get_idx(seq, *pos);
  599. if (v)
  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. };