l2t.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #define VLAN_NONE 0xfff
  50. /* identifies sync vs async L2T_WRITE_REQs */
  51. #define F_SYNC_WR (1 << 12)
  52. enum {
  53. L2T_STATE_VALID, /* entry is up to date */
  54. L2T_STATE_STALE, /* entry may be used but needs revalidation */
  55. L2T_STATE_RESOLVING, /* entry needs address resolution */
  56. L2T_STATE_SYNC_WRITE, /* synchronous write of entry underway */
  57. /* when state is one of the below the entry is not hashed */
  58. L2T_STATE_SWITCHING, /* entry is being used by a switching filter */
  59. L2T_STATE_UNUSED /* entry not in use */
  60. };
  61. struct l2t_data {
  62. rwlock_t lock;
  63. atomic_t nfree; /* number of free entries */
  64. struct l2t_entry *rover; /* starting point for next allocation */
  65. struct l2t_entry l2tab[L2T_SIZE];
  66. };
  67. static inline unsigned int vlan_prio(const struct l2t_entry *e)
  68. {
  69. return e->vlan >> 13;
  70. }
  71. static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
  72. {
  73. if (atomic_add_return(1, &e->refcnt) == 1) /* 0 -> 1 transition */
  74. atomic_dec(&d->nfree);
  75. }
  76. /*
  77. * To avoid having to check address families we do not allow v4 and v6
  78. * neighbors to be on the same hash chain. We keep v4 entries in the first
  79. * half of available hash buckets and v6 in the second.
  80. */
  81. enum {
  82. L2T_SZ_HALF = L2T_SIZE / 2,
  83. L2T_HASH_MASK = L2T_SZ_HALF - 1
  84. };
  85. static inline unsigned int arp_hash(const u32 *key, int ifindex)
  86. {
  87. return jhash_2words(*key, ifindex, 0) & L2T_HASH_MASK;
  88. }
  89. static inline unsigned int ipv6_hash(const u32 *key, int ifindex)
  90. {
  91. u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
  92. return L2T_SZ_HALF + (jhash_2words(xor, ifindex, 0) & L2T_HASH_MASK);
  93. }
  94. static unsigned int addr_hash(const u32 *addr, int addr_len, int ifindex)
  95. {
  96. return addr_len == 4 ? arp_hash(addr, ifindex) :
  97. ipv6_hash(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 sk_buff *skb;
  127. struct cpl_l2t_write_req *req;
  128. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  129. if (!skb)
  130. return -ENOMEM;
  131. req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
  132. INIT_TP_WR(req, 0);
  133. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ,
  134. e->idx | (sync ? F_SYNC_WR : 0) |
  135. TID_QID_V(adap->sge.fw_evtq.abs_id)));
  136. req->params = htons(L2T_W_PORT_V(e->lport) | L2T_W_NOREPLY_V(!sync));
  137. req->l2t_idx = htons(e->idx);
  138. req->vlan = htons(e->vlan);
  139. if (e->neigh && !(e->neigh->dev->flags & IFF_LOOPBACK))
  140. memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
  141. memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
  142. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  143. t4_ofld_send(adap, skb);
  144. if (sync && e->state != L2T_STATE_SWITCHING)
  145. e->state = L2T_STATE_SYNC_WRITE;
  146. return 0;
  147. }
  148. /*
  149. * Send packets waiting in an L2T entry's ARP queue. Must be called with the
  150. * entry locked.
  151. */
  152. static void send_pending(struct adapter *adap, struct l2t_entry *e)
  153. {
  154. while (e->arpq_head) {
  155. struct sk_buff *skb = e->arpq_head;
  156. e->arpq_head = skb->next;
  157. skb->next = NULL;
  158. t4_ofld_send(adap, skb);
  159. }
  160. e->arpq_tail = NULL;
  161. }
  162. /*
  163. * Process a CPL_L2T_WRITE_RPL. Wake up the ARP queue if it completes a
  164. * synchronous L2T_WRITE. Note that the TID in the reply is really the L2T
  165. * index it refers to.
  166. */
  167. void do_l2t_write_rpl(struct adapter *adap, const struct cpl_l2t_write_rpl *rpl)
  168. {
  169. unsigned int tid = GET_TID(rpl);
  170. unsigned int idx = tid & (L2T_SIZE - 1);
  171. if (unlikely(rpl->status != CPL_ERR_NONE)) {
  172. dev_err(adap->pdev_dev,
  173. "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
  174. rpl->status, idx);
  175. return;
  176. }
  177. if (tid & F_SYNC_WR) {
  178. struct l2t_entry *e = &adap->l2t->l2tab[idx];
  179. spin_lock(&e->lock);
  180. if (e->state != L2T_STATE_SWITCHING) {
  181. send_pending(adap, e);
  182. e->state = (e->neigh->nud_state & NUD_STALE) ?
  183. L2T_STATE_STALE : L2T_STATE_VALID;
  184. }
  185. spin_unlock(&e->lock);
  186. }
  187. }
  188. /*
  189. * Add a packet to an L2T entry's queue of packets awaiting resolution.
  190. * Must be called with the entry's lock held.
  191. */
  192. static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
  193. {
  194. skb->next = NULL;
  195. if (e->arpq_head)
  196. e->arpq_tail->next = skb;
  197. else
  198. e->arpq_head = skb;
  199. e->arpq_tail = skb;
  200. }
  201. int cxgb4_l2t_send(struct net_device *dev, struct sk_buff *skb,
  202. struct l2t_entry *e)
  203. {
  204. struct adapter *adap = netdev2adap(dev);
  205. again:
  206. switch (e->state) {
  207. case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
  208. neigh_event_send(e->neigh, NULL);
  209. spin_lock_bh(&e->lock);
  210. if (e->state == L2T_STATE_STALE)
  211. e->state = L2T_STATE_VALID;
  212. spin_unlock_bh(&e->lock);
  213. case L2T_STATE_VALID: /* fast-path, send the packet on */
  214. return t4_ofld_send(adap, skb);
  215. case L2T_STATE_RESOLVING:
  216. case L2T_STATE_SYNC_WRITE:
  217. spin_lock_bh(&e->lock);
  218. if (e->state != L2T_STATE_SYNC_WRITE &&
  219. e->state != L2T_STATE_RESOLVING) {
  220. spin_unlock_bh(&e->lock);
  221. goto again;
  222. }
  223. arpq_enqueue(e, skb);
  224. spin_unlock_bh(&e->lock);
  225. if (e->state == L2T_STATE_RESOLVING &&
  226. !neigh_event_send(e->neigh, NULL)) {
  227. spin_lock_bh(&e->lock);
  228. if (e->state == L2T_STATE_RESOLVING && e->arpq_head)
  229. write_l2e(adap, e, 1);
  230. spin_unlock_bh(&e->lock);
  231. }
  232. }
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(cxgb4_l2t_send);
  236. /*
  237. * Allocate a free L2T entry. Must be called with l2t_data.lock held.
  238. */
  239. static struct l2t_entry *alloc_l2e(struct l2t_data *d)
  240. {
  241. struct l2t_entry *end, *e, **p;
  242. if (!atomic_read(&d->nfree))
  243. return NULL;
  244. /* there's definitely a free entry */
  245. for (e = d->rover, end = &d->l2tab[L2T_SIZE]; e != end; ++e)
  246. if (atomic_read(&e->refcnt) == 0)
  247. goto found;
  248. for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
  249. ;
  250. found:
  251. d->rover = e + 1;
  252. atomic_dec(&d->nfree);
  253. /*
  254. * The entry we found may be an inactive entry that is
  255. * presently in the hash table. We need to remove it.
  256. */
  257. if (e->state < L2T_STATE_SWITCHING)
  258. for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
  259. if (*p == e) {
  260. *p = e->next;
  261. e->next = NULL;
  262. break;
  263. }
  264. e->state = L2T_STATE_UNUSED;
  265. return e;
  266. }
  267. /*
  268. * Called when an L2T entry has no more users.
  269. */
  270. static void t4_l2e_free(struct l2t_entry *e)
  271. {
  272. struct l2t_data *d;
  273. spin_lock_bh(&e->lock);
  274. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  275. if (e->neigh) {
  276. neigh_release(e->neigh);
  277. e->neigh = NULL;
  278. }
  279. while (e->arpq_head) {
  280. struct sk_buff *skb = e->arpq_head;
  281. e->arpq_head = skb->next;
  282. kfree_skb(skb);
  283. }
  284. e->arpq_tail = NULL;
  285. }
  286. spin_unlock_bh(&e->lock);
  287. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  288. atomic_inc(&d->nfree);
  289. }
  290. void cxgb4_l2t_release(struct l2t_entry *e)
  291. {
  292. if (atomic_dec_and_test(&e->refcnt))
  293. t4_l2e_free(e);
  294. }
  295. EXPORT_SYMBOL(cxgb4_l2t_release);
  296. /*
  297. * Update an L2T entry that was previously used for the same next hop as neigh.
  298. * Must be called with softirqs disabled.
  299. */
  300. static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
  301. {
  302. unsigned int nud_state;
  303. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  304. if (neigh != e->neigh)
  305. neigh_replace(e, neigh);
  306. nud_state = neigh->nud_state;
  307. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
  308. !(nud_state & NUD_VALID))
  309. e->state = L2T_STATE_RESOLVING;
  310. else if (nud_state & NUD_CONNECTED)
  311. e->state = L2T_STATE_VALID;
  312. else
  313. e->state = L2T_STATE_STALE;
  314. spin_unlock(&e->lock);
  315. }
  316. struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
  317. const struct net_device *physdev,
  318. unsigned int priority)
  319. {
  320. u8 lport;
  321. u16 vlan;
  322. struct l2t_entry *e;
  323. int addr_len = neigh->tbl->key_len;
  324. u32 *addr = (u32 *)neigh->primary_key;
  325. int ifidx = neigh->dev->ifindex;
  326. int hash = addr_hash(addr, addr_len, ifidx);
  327. if (neigh->dev->flags & IFF_LOOPBACK)
  328. lport = netdev2pinfo(physdev)->tx_chan + 4;
  329. else
  330. lport = netdev2pinfo(physdev)->lport;
  331. if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
  332. vlan = vlan_dev_vlan_id(neigh->dev);
  333. else
  334. vlan = VLAN_NONE;
  335. write_lock_bh(&d->lock);
  336. for (e = d->l2tab[hash].first; e; e = e->next)
  337. if (!addreq(e, addr) && e->ifindex == ifidx &&
  338. e->vlan == vlan && e->lport == lport) {
  339. l2t_hold(d, e);
  340. if (atomic_read(&e->refcnt) == 1)
  341. reuse_entry(e, neigh);
  342. goto done;
  343. }
  344. /* Need to allocate a new entry */
  345. e = alloc_l2e(d);
  346. if (e) {
  347. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  348. e->state = L2T_STATE_RESOLVING;
  349. if (neigh->dev->flags & IFF_LOOPBACK)
  350. memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
  351. memcpy(e->addr, addr, addr_len);
  352. e->ifindex = ifidx;
  353. e->hash = hash;
  354. e->lport = lport;
  355. e->v6 = addr_len == 16;
  356. atomic_set(&e->refcnt, 1);
  357. neigh_replace(e, neigh);
  358. e->vlan = vlan;
  359. e->next = d->l2tab[hash].first;
  360. d->l2tab[hash].first = e;
  361. spin_unlock(&e->lock);
  362. }
  363. done:
  364. write_unlock_bh(&d->lock);
  365. return e;
  366. }
  367. EXPORT_SYMBOL(cxgb4_l2t_get);
  368. u64 cxgb4_select_ntuple(struct net_device *dev,
  369. const struct l2t_entry *l2t)
  370. {
  371. struct adapter *adap = netdev2adap(dev);
  372. struct tp_params *tp = &adap->params.tp;
  373. u64 ntuple = 0;
  374. /* Initialize each of the fields which we care about which are present
  375. * in the Compressed Filter Tuple.
  376. */
  377. if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
  378. ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
  379. if (tp->port_shift >= 0)
  380. ntuple |= (u64)l2t->lport << tp->port_shift;
  381. if (tp->protocol_shift >= 0)
  382. ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
  383. if (tp->vnic_shift >= 0) {
  384. u32 viid = cxgb4_port_viid(dev);
  385. u32 vf = FW_VIID_VIN_G(viid);
  386. u32 pf = FW_VIID_PFN_G(viid);
  387. u32 vld = FW_VIID_VIVLD_G(viid);
  388. ntuple |= (u64)(FT_VNID_ID_VF_V(vf) |
  389. FT_VNID_ID_PF_V(pf) |
  390. FT_VNID_ID_VLD_V(vld)) << tp->vnic_shift;
  391. }
  392. return ntuple;
  393. }
  394. EXPORT_SYMBOL(cxgb4_select_ntuple);
  395. /*
  396. * Called when address resolution fails for an L2T entry to handle packets
  397. * on the arpq head. If a packet specifies a failure handler it is invoked,
  398. * otherwise the packet is sent to the device.
  399. */
  400. static void handle_failed_resolution(struct adapter *adap, struct sk_buff *arpq)
  401. {
  402. while (arpq) {
  403. struct sk_buff *skb = arpq;
  404. const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
  405. arpq = skb->next;
  406. skb->next = NULL;
  407. if (cb->arp_err_handler)
  408. cb->arp_err_handler(cb->handle, skb);
  409. else
  410. t4_ofld_send(adap, skb);
  411. }
  412. }
  413. /*
  414. * Called when the host's neighbor layer makes a change to some entry that is
  415. * loaded into the HW L2 table.
  416. */
  417. void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
  418. {
  419. struct l2t_entry *e;
  420. struct sk_buff *arpq = NULL;
  421. struct l2t_data *d = adap->l2t;
  422. int addr_len = neigh->tbl->key_len;
  423. u32 *addr = (u32 *) neigh->primary_key;
  424. int ifidx = neigh->dev->ifindex;
  425. int hash = addr_hash(addr, addr_len, ifidx);
  426. read_lock_bh(&d->lock);
  427. for (e = d->l2tab[hash].first; e; e = e->next)
  428. if (!addreq(e, addr) && e->ifindex == ifidx) {
  429. spin_lock(&e->lock);
  430. if (atomic_read(&e->refcnt))
  431. goto found;
  432. spin_unlock(&e->lock);
  433. break;
  434. }
  435. read_unlock_bh(&d->lock);
  436. return;
  437. found:
  438. read_unlock(&d->lock);
  439. if (neigh != e->neigh)
  440. neigh_replace(e, neigh);
  441. if (e->state == L2T_STATE_RESOLVING) {
  442. if (neigh->nud_state & NUD_FAILED) {
  443. arpq = e->arpq_head;
  444. e->arpq_head = e->arpq_tail = NULL;
  445. } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
  446. e->arpq_head) {
  447. write_l2e(adap, e, 1);
  448. }
  449. } else {
  450. e->state = neigh->nud_state & NUD_CONNECTED ?
  451. L2T_STATE_VALID : L2T_STATE_STALE;
  452. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
  453. write_l2e(adap, e, 0);
  454. }
  455. spin_unlock_bh(&e->lock);
  456. if (arpq)
  457. handle_failed_resolution(adap, arpq);
  458. }
  459. /* Allocate an L2T entry for use by a switching rule. Such need to be
  460. * explicitly freed and while busy they are not on any hash chain, so normal
  461. * address resolution updates do not see them.
  462. */
  463. struct l2t_entry *t4_l2t_alloc_switching(struct l2t_data *d)
  464. {
  465. struct l2t_entry *e;
  466. write_lock_bh(&d->lock);
  467. e = alloc_l2e(d);
  468. if (e) {
  469. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  470. e->state = L2T_STATE_SWITCHING;
  471. atomic_set(&e->refcnt, 1);
  472. spin_unlock(&e->lock);
  473. }
  474. write_unlock_bh(&d->lock);
  475. return e;
  476. }
  477. /* Sets/updates the contents of a switching L2T entry that has been allocated
  478. * with an earlier call to @t4_l2t_alloc_switching.
  479. */
  480. int t4_l2t_set_switching(struct adapter *adap, struct l2t_entry *e, u16 vlan,
  481. u8 port, u8 *eth_addr)
  482. {
  483. e->vlan = vlan;
  484. e->lport = port;
  485. memcpy(e->dmac, eth_addr, ETH_ALEN);
  486. return write_l2e(adap, e, 0);
  487. }
  488. struct l2t_data *t4_init_l2t(void)
  489. {
  490. int i;
  491. struct l2t_data *d;
  492. d = t4_alloc_mem(sizeof(*d));
  493. if (!d)
  494. return NULL;
  495. d->rover = d->l2tab;
  496. atomic_set(&d->nfree, L2T_SIZE);
  497. rwlock_init(&d->lock);
  498. for (i = 0; i < L2T_SIZE; ++i) {
  499. d->l2tab[i].idx = i;
  500. d->l2tab[i].state = L2T_STATE_UNUSED;
  501. spin_lock_init(&d->l2tab[i].lock);
  502. atomic_set(&d->l2tab[i].refcnt, 0);
  503. }
  504. return d;
  505. }
  506. static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
  507. {
  508. struct l2t_entry *l2tab = seq->private;
  509. return pos >= L2T_SIZE ? NULL : &l2tab[pos];
  510. }
  511. static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
  512. {
  513. return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  514. }
  515. static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  516. {
  517. v = l2t_get_idx(seq, *pos);
  518. if (v)
  519. ++*pos;
  520. return v;
  521. }
  522. static void l2t_seq_stop(struct seq_file *seq, void *v)
  523. {
  524. }
  525. static char l2e_state(const struct l2t_entry *e)
  526. {
  527. switch (e->state) {
  528. case L2T_STATE_VALID: return 'V';
  529. case L2T_STATE_STALE: return 'S';
  530. case L2T_STATE_SYNC_WRITE: return 'W';
  531. case L2T_STATE_RESOLVING: return e->arpq_head ? 'A' : 'R';
  532. case L2T_STATE_SWITCHING: return 'X';
  533. default:
  534. return 'U';
  535. }
  536. }
  537. static int l2t_seq_show(struct seq_file *seq, void *v)
  538. {
  539. if (v == SEQ_START_TOKEN)
  540. seq_puts(seq, " Idx IP address "
  541. "Ethernet address VLAN/P LP State Users Port\n");
  542. else {
  543. char ip[60];
  544. struct l2t_entry *e = v;
  545. spin_lock_bh(&e->lock);
  546. if (e->state == L2T_STATE_SWITCHING)
  547. ip[0] = '\0';
  548. else
  549. sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
  550. seq_printf(seq, "%4u %-25s %17pM %4d %u %2u %c %5u %s\n",
  551. e->idx, ip, e->dmac,
  552. e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
  553. l2e_state(e), atomic_read(&e->refcnt),
  554. e->neigh ? e->neigh->dev->name : "");
  555. spin_unlock_bh(&e->lock);
  556. }
  557. return 0;
  558. }
  559. static const struct seq_operations l2t_seq_ops = {
  560. .start = l2t_seq_start,
  561. .next = l2t_seq_next,
  562. .stop = l2t_seq_stop,
  563. .show = l2t_seq_show
  564. };
  565. static int l2t_seq_open(struct inode *inode, struct file *file)
  566. {
  567. int rc = seq_open(file, &l2t_seq_ops);
  568. if (!rc) {
  569. struct adapter *adap = inode->i_private;
  570. struct seq_file *seq = file->private_data;
  571. seq->private = adap->l2t->l2tab;
  572. }
  573. return rc;
  574. }
  575. const struct file_operations t4_l2t_fops = {
  576. .owner = THIS_MODULE,
  577. .open = l2t_seq_open,
  578. .read = seq_read,
  579. .llseek = seq_lseek,
  580. .release = seq_release,
  581. };