l2t.c 18 KB

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