en_rx.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <net/busy_poll.h>
  34. #include <linux/bpf.h>
  35. #include <linux/bpf_trace.h>
  36. #include <linux/mlx4/cq.h>
  37. #include <linux/slab.h>
  38. #include <linux/mlx4/qp.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/rculist.h>
  41. #include <linux/if_ether.h>
  42. #include <linux/if_vlan.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/irq.h>
  45. #if IS_ENABLED(CONFIG_IPV6)
  46. #include <net/ip6_checksum.h>
  47. #endif
  48. #include "mlx4_en.h"
  49. static int mlx4_alloc_page(struct mlx4_en_priv *priv,
  50. struct mlx4_en_rx_alloc *frag,
  51. gfp_t gfp)
  52. {
  53. struct page *page;
  54. dma_addr_t dma;
  55. page = alloc_page(gfp);
  56. if (unlikely(!page))
  57. return -ENOMEM;
  58. dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE, priv->dma_dir);
  59. if (unlikely(dma_mapping_error(priv->ddev, dma))) {
  60. __free_page(page);
  61. return -ENOMEM;
  62. }
  63. frag->page = page;
  64. frag->dma = dma;
  65. frag->page_offset = priv->rx_headroom;
  66. return 0;
  67. }
  68. static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
  69. struct mlx4_en_rx_ring *ring,
  70. struct mlx4_en_rx_desc *rx_desc,
  71. struct mlx4_en_rx_alloc *frags,
  72. gfp_t gfp)
  73. {
  74. int i;
  75. for (i = 0; i < priv->num_frags; i++, frags++) {
  76. if (!frags->page) {
  77. if (mlx4_alloc_page(priv, frags, gfp))
  78. return -ENOMEM;
  79. ring->rx_alloc_pages++;
  80. }
  81. rx_desc->data[i].addr = cpu_to_be64(frags->dma +
  82. frags->page_offset);
  83. }
  84. return 0;
  85. }
  86. static void mlx4_en_free_frag(const struct mlx4_en_priv *priv,
  87. struct mlx4_en_rx_alloc *frag)
  88. {
  89. if (frag->page) {
  90. dma_unmap_page(priv->ddev, frag->dma,
  91. PAGE_SIZE, priv->dma_dir);
  92. __free_page(frag->page);
  93. }
  94. /* We need to clear all fields, otherwise a change of priv->log_rx_info
  95. * could lead to see garbage later in frag->page.
  96. */
  97. memset(frag, 0, sizeof(*frag));
  98. }
  99. static void mlx4_en_init_rx_desc(const struct mlx4_en_priv *priv,
  100. struct mlx4_en_rx_ring *ring, int index)
  101. {
  102. struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
  103. int possible_frags;
  104. int i;
  105. /* Set size and memtype fields */
  106. for (i = 0; i < priv->num_frags; i++) {
  107. rx_desc->data[i].byte_count =
  108. cpu_to_be32(priv->frag_info[i].frag_size);
  109. rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
  110. }
  111. /* If the number of used fragments does not fill up the ring stride,
  112. * remaining (unused) fragments must be padded with null address/size
  113. * and a special memory key */
  114. possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
  115. for (i = priv->num_frags; i < possible_frags; i++) {
  116. rx_desc->data[i].byte_count = 0;
  117. rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
  118. rx_desc->data[i].addr = 0;
  119. }
  120. }
  121. static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
  122. struct mlx4_en_rx_ring *ring, int index,
  123. gfp_t gfp)
  124. {
  125. struct mlx4_en_rx_desc *rx_desc = ring->buf + (index * ring->stride);
  126. struct mlx4_en_rx_alloc *frags = ring->rx_info +
  127. (index << priv->log_rx_info);
  128. if (ring->page_cache.index > 0) {
  129. /* XDP uses a single page per frame */
  130. if (!frags->page) {
  131. ring->page_cache.index--;
  132. frags->page = ring->page_cache.buf[ring->page_cache.index].page;
  133. frags->dma = ring->page_cache.buf[ring->page_cache.index].dma;
  134. }
  135. frags->page_offset = XDP_PACKET_HEADROOM;
  136. rx_desc->data[0].addr = cpu_to_be64(frags->dma +
  137. XDP_PACKET_HEADROOM);
  138. return 0;
  139. }
  140. return mlx4_en_alloc_frags(priv, ring, rx_desc, frags, gfp);
  141. }
  142. static bool mlx4_en_is_ring_empty(const struct mlx4_en_rx_ring *ring)
  143. {
  144. return ring->prod == ring->cons;
  145. }
  146. static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
  147. {
  148. *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
  149. }
  150. /* slow path */
  151. static void mlx4_en_free_rx_desc(const struct mlx4_en_priv *priv,
  152. struct mlx4_en_rx_ring *ring,
  153. int index)
  154. {
  155. struct mlx4_en_rx_alloc *frags;
  156. int nr;
  157. frags = ring->rx_info + (index << priv->log_rx_info);
  158. for (nr = 0; nr < priv->num_frags; nr++) {
  159. en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
  160. mlx4_en_free_frag(priv, frags + nr);
  161. }
  162. }
  163. static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
  164. {
  165. struct mlx4_en_rx_ring *ring;
  166. int ring_ind;
  167. int buf_ind;
  168. int new_size;
  169. for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
  170. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  171. ring = priv->rx_ring[ring_ind];
  172. if (mlx4_en_prepare_rx_desc(priv, ring,
  173. ring->actual_size,
  174. GFP_KERNEL | __GFP_COLD)) {
  175. if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
  176. en_err(priv, "Failed to allocate enough rx buffers\n");
  177. return -ENOMEM;
  178. } else {
  179. new_size = rounddown_pow_of_two(ring->actual_size);
  180. en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
  181. ring->actual_size, new_size);
  182. goto reduce_rings;
  183. }
  184. }
  185. ring->actual_size++;
  186. ring->prod++;
  187. }
  188. }
  189. return 0;
  190. reduce_rings:
  191. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  192. ring = priv->rx_ring[ring_ind];
  193. while (ring->actual_size > new_size) {
  194. ring->actual_size--;
  195. ring->prod--;
  196. mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
  197. }
  198. }
  199. return 0;
  200. }
  201. static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
  202. struct mlx4_en_rx_ring *ring)
  203. {
  204. int index;
  205. en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
  206. ring->cons, ring->prod);
  207. /* Unmap and free Rx buffers */
  208. for (index = 0; index < ring->size; index++) {
  209. en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
  210. mlx4_en_free_rx_desc(priv, ring, index);
  211. }
  212. ring->cons = 0;
  213. ring->prod = 0;
  214. }
  215. void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
  216. {
  217. int i;
  218. int num_of_eqs;
  219. int num_rx_rings;
  220. struct mlx4_dev *dev = mdev->dev;
  221. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
  222. num_of_eqs = max_t(int, MIN_RX_RINGS,
  223. min_t(int,
  224. mlx4_get_eqs_per_port(mdev->dev, i),
  225. DEF_RX_RINGS));
  226. num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
  227. min_t(int, num_of_eqs,
  228. netif_get_num_default_rss_queues());
  229. mdev->profile.prof[i].rx_ring_num =
  230. rounddown_pow_of_two(num_rx_rings);
  231. }
  232. }
  233. int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
  234. struct mlx4_en_rx_ring **pring,
  235. u32 size, u16 stride, int node)
  236. {
  237. struct mlx4_en_dev *mdev = priv->mdev;
  238. struct mlx4_en_rx_ring *ring;
  239. int err = -ENOMEM;
  240. int tmp;
  241. ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
  242. if (!ring) {
  243. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  244. if (!ring) {
  245. en_err(priv, "Failed to allocate RX ring structure\n");
  246. return -ENOMEM;
  247. }
  248. }
  249. ring->prod = 0;
  250. ring->cons = 0;
  251. ring->size = size;
  252. ring->size_mask = size - 1;
  253. ring->stride = stride;
  254. ring->log_stride = ffs(ring->stride) - 1;
  255. ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
  256. tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
  257. sizeof(struct mlx4_en_rx_alloc));
  258. ring->rx_info = vzalloc_node(tmp, node);
  259. if (!ring->rx_info) {
  260. ring->rx_info = vzalloc(tmp);
  261. if (!ring->rx_info) {
  262. err = -ENOMEM;
  263. goto err_ring;
  264. }
  265. }
  266. en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
  267. ring->rx_info, tmp);
  268. /* Allocate HW buffers on provided NUMA node */
  269. set_dev_node(&mdev->dev->persist->pdev->dev, node);
  270. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  271. set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
  272. if (err)
  273. goto err_info;
  274. ring->buf = ring->wqres.buf.direct.buf;
  275. ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
  276. *pring = ring;
  277. return 0;
  278. err_info:
  279. vfree(ring->rx_info);
  280. ring->rx_info = NULL;
  281. err_ring:
  282. kfree(ring);
  283. *pring = NULL;
  284. return err;
  285. }
  286. int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
  287. {
  288. struct mlx4_en_rx_ring *ring;
  289. int i;
  290. int ring_ind;
  291. int err;
  292. int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  293. DS_SIZE * priv->num_frags);
  294. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  295. ring = priv->rx_ring[ring_ind];
  296. ring->prod = 0;
  297. ring->cons = 0;
  298. ring->actual_size = 0;
  299. ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
  300. ring->stride = stride;
  301. if (ring->stride <= TXBB_SIZE) {
  302. /* Stamp first unused send wqe */
  303. __be32 *ptr = (__be32 *)ring->buf;
  304. __be32 stamp = cpu_to_be32(1 << STAMP_SHIFT);
  305. *ptr = stamp;
  306. /* Move pointer to start of rx section */
  307. ring->buf += TXBB_SIZE;
  308. }
  309. ring->log_stride = ffs(ring->stride) - 1;
  310. ring->buf_size = ring->size * ring->stride;
  311. memset(ring->buf, 0, ring->buf_size);
  312. mlx4_en_update_rx_prod_db(ring);
  313. /* Initialize all descriptors */
  314. for (i = 0; i < ring->size; i++)
  315. mlx4_en_init_rx_desc(priv, ring, i);
  316. }
  317. err = mlx4_en_fill_rx_buffers(priv);
  318. if (err)
  319. goto err_buffers;
  320. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  321. ring = priv->rx_ring[ring_ind];
  322. ring->size_mask = ring->actual_size - 1;
  323. mlx4_en_update_rx_prod_db(ring);
  324. }
  325. return 0;
  326. err_buffers:
  327. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
  328. mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
  329. ring_ind = priv->rx_ring_num - 1;
  330. while (ring_ind >= 0) {
  331. if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
  332. priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
  333. ring_ind--;
  334. }
  335. return err;
  336. }
  337. /* We recover from out of memory by scheduling our napi poll
  338. * function (mlx4_en_process_cq), which tries to allocate
  339. * all missing RX buffers (call to mlx4_en_refill_rx_buffers).
  340. */
  341. void mlx4_en_recover_from_oom(struct mlx4_en_priv *priv)
  342. {
  343. int ring;
  344. if (!priv->port_up)
  345. return;
  346. for (ring = 0; ring < priv->rx_ring_num; ring++) {
  347. if (mlx4_en_is_ring_empty(priv->rx_ring[ring])) {
  348. local_bh_disable();
  349. napi_reschedule(&priv->rx_cq[ring]->napi);
  350. local_bh_enable();
  351. }
  352. }
  353. }
  354. /* When the rx ring is running in page-per-packet mode, a released frame can go
  355. * directly into a small cache, to avoid unmapping or touching the page
  356. * allocator. In bpf prog performance scenarios, buffers are either forwarded
  357. * or dropped, never converted to skbs, so every page can come directly from
  358. * this cache when it is sized to be a multiple of the napi budget.
  359. */
  360. bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
  361. struct mlx4_en_rx_alloc *frame)
  362. {
  363. struct mlx4_en_page_cache *cache = &ring->page_cache;
  364. if (cache->index >= MLX4_EN_CACHE_SIZE)
  365. return false;
  366. cache->buf[cache->index].page = frame->page;
  367. cache->buf[cache->index].dma = frame->dma;
  368. cache->index++;
  369. return true;
  370. }
  371. void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
  372. struct mlx4_en_rx_ring **pring,
  373. u32 size, u16 stride)
  374. {
  375. struct mlx4_en_dev *mdev = priv->mdev;
  376. struct mlx4_en_rx_ring *ring = *pring;
  377. struct bpf_prog *old_prog;
  378. old_prog = rcu_dereference_protected(
  379. ring->xdp_prog,
  380. lockdep_is_held(&mdev->state_lock));
  381. if (old_prog)
  382. bpf_prog_put(old_prog);
  383. mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
  384. vfree(ring->rx_info);
  385. ring->rx_info = NULL;
  386. kfree(ring);
  387. *pring = NULL;
  388. }
  389. void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
  390. struct mlx4_en_rx_ring *ring)
  391. {
  392. int i;
  393. for (i = 0; i < ring->page_cache.index; i++) {
  394. dma_unmap_page(priv->ddev, ring->page_cache.buf[i].dma,
  395. PAGE_SIZE, priv->dma_dir);
  396. put_page(ring->page_cache.buf[i].page);
  397. }
  398. ring->page_cache.index = 0;
  399. mlx4_en_free_rx_buf(priv, ring);
  400. if (ring->stride <= TXBB_SIZE)
  401. ring->buf -= TXBB_SIZE;
  402. }
  403. static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
  404. struct mlx4_en_rx_alloc *frags,
  405. struct sk_buff *skb,
  406. int length)
  407. {
  408. const struct mlx4_en_frag_info *frag_info = priv->frag_info;
  409. unsigned int truesize = 0;
  410. int nr, frag_size;
  411. struct page *page;
  412. dma_addr_t dma;
  413. bool release;
  414. /* Collect used fragments while replacing them in the HW descriptors */
  415. for (nr = 0;; frags++) {
  416. frag_size = min_t(int, length, frag_info->frag_size);
  417. page = frags->page;
  418. if (unlikely(!page))
  419. goto fail;
  420. dma = frags->dma;
  421. dma_sync_single_range_for_cpu(priv->ddev, dma, frags->page_offset,
  422. frag_size, priv->dma_dir);
  423. __skb_fill_page_desc(skb, nr, page, frags->page_offset,
  424. frag_size);
  425. truesize += frag_info->frag_stride;
  426. if (frag_info->frag_stride == PAGE_SIZE / 2) {
  427. frags->page_offset ^= PAGE_SIZE / 2;
  428. release = page_count(page) != 1 ||
  429. page_is_pfmemalloc(page) ||
  430. page_to_nid(page) != numa_mem_id();
  431. } else {
  432. u32 sz_align = ALIGN(frag_size, SMP_CACHE_BYTES);
  433. frags->page_offset += sz_align;
  434. release = frags->page_offset + frag_info->frag_size > PAGE_SIZE;
  435. }
  436. if (release) {
  437. dma_unmap_page(priv->ddev, dma, PAGE_SIZE, priv->dma_dir);
  438. frags->page = NULL;
  439. } else {
  440. page_ref_inc(page);
  441. }
  442. nr++;
  443. length -= frag_size;
  444. if (!length)
  445. break;
  446. frag_info++;
  447. }
  448. skb->truesize += truesize;
  449. return nr;
  450. fail:
  451. while (nr > 0) {
  452. nr--;
  453. __skb_frag_unref(skb_shinfo(skb)->frags + nr);
  454. }
  455. return 0;
  456. }
  457. static void validate_loopback(struct mlx4_en_priv *priv, void *va)
  458. {
  459. const unsigned char *data = va + ETH_HLEN;
  460. int i;
  461. for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++) {
  462. if (data[i] != (unsigned char)i)
  463. return;
  464. }
  465. /* Loopback found */
  466. priv->loopback_ok = 1;
  467. }
  468. static bool mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
  469. struct mlx4_en_rx_ring *ring)
  470. {
  471. u32 missing = ring->actual_size - (ring->prod - ring->cons);
  472. /* Try to batch allocations, but not too much. */
  473. if (missing < 8)
  474. return false;
  475. do {
  476. if (mlx4_en_prepare_rx_desc(priv, ring,
  477. ring->prod & ring->size_mask,
  478. GFP_ATOMIC | __GFP_COLD |
  479. __GFP_MEMALLOC))
  480. break;
  481. ring->prod++;
  482. } while (--missing);
  483. return true;
  484. }
  485. /* When hardware doesn't strip the vlan, we need to calculate the checksum
  486. * over it and add it to the hardware's checksum calculation
  487. */
  488. static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
  489. struct vlan_hdr *vlanh)
  490. {
  491. return csum_add(hw_checksum, *(__wsum *)vlanh);
  492. }
  493. /* Although the stack expects checksum which doesn't include the pseudo
  494. * header, the HW adds it. To address that, we are subtracting the pseudo
  495. * header checksum from the checksum value provided by the HW.
  496. */
  497. static void get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
  498. struct iphdr *iph)
  499. {
  500. __u16 length_for_csum = 0;
  501. __wsum csum_pseudo_header = 0;
  502. length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
  503. csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  504. length_for_csum, iph->protocol, 0);
  505. skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
  506. }
  507. #if IS_ENABLED(CONFIG_IPV6)
  508. /* In IPv6 packets, besides subtracting the pseudo header checksum,
  509. * we also compute/add the IP header checksum which
  510. * is not added by the HW.
  511. */
  512. static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
  513. struct ipv6hdr *ipv6h)
  514. {
  515. __wsum csum_pseudo_hdr = 0;
  516. if (unlikely(ipv6h->nexthdr == IPPROTO_FRAGMENT ||
  517. ipv6h->nexthdr == IPPROTO_HOPOPTS))
  518. return -1;
  519. hw_checksum = csum_add(hw_checksum, (__force __wsum)htons(ipv6h->nexthdr));
  520. csum_pseudo_hdr = csum_partial(&ipv6h->saddr,
  521. sizeof(ipv6h->saddr) + sizeof(ipv6h->daddr), 0);
  522. csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ipv6h->payload_len);
  523. csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ntohs(ipv6h->nexthdr));
  524. skb->csum = csum_sub(hw_checksum, csum_pseudo_hdr);
  525. skb->csum = csum_add(skb->csum, csum_partial(ipv6h, sizeof(struct ipv6hdr), 0));
  526. return 0;
  527. }
  528. #endif
  529. static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
  530. netdev_features_t dev_features)
  531. {
  532. __wsum hw_checksum = 0;
  533. void *hdr = (u8 *)va + sizeof(struct ethhdr);
  534. hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
  535. if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
  536. !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
  537. hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
  538. hdr += sizeof(struct vlan_hdr);
  539. }
  540. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4))
  541. get_fixed_ipv4_csum(hw_checksum, skb, hdr);
  542. #if IS_ENABLED(CONFIG_IPV6)
  543. else if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
  544. if (unlikely(get_fixed_ipv6_csum(hw_checksum, skb, hdr)))
  545. return -1;
  546. #endif
  547. return 0;
  548. }
  549. int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  550. {
  551. struct mlx4_en_priv *priv = netdev_priv(dev);
  552. struct mlx4_en_dev *mdev = priv->mdev;
  553. struct mlx4_cqe *cqe;
  554. struct mlx4_en_rx_ring *ring = priv->rx_ring[cq->ring];
  555. struct mlx4_en_rx_alloc *frags;
  556. struct bpf_prog *xdp_prog;
  557. int doorbell_pending;
  558. struct sk_buff *skb;
  559. int index;
  560. int nr;
  561. unsigned int length;
  562. int polled = 0;
  563. int ip_summed;
  564. int factor = priv->cqe_factor;
  565. u64 timestamp;
  566. bool l2_tunnel;
  567. if (unlikely(!priv->port_up))
  568. return 0;
  569. if (unlikely(budget <= 0))
  570. return polled;
  571. /* Protect accesses to: ring->xdp_prog, priv->mac_hash list */
  572. rcu_read_lock();
  573. xdp_prog = rcu_dereference(ring->xdp_prog);
  574. doorbell_pending = 0;
  575. /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
  576. * descriptor offset can be deduced from the CQE index instead of
  577. * reading 'cqe->index' */
  578. index = cq->mcq.cons_index & ring->size_mask;
  579. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  580. /* Process all completed CQEs */
  581. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  582. cq->mcq.cons_index & cq->size)) {
  583. void *va;
  584. frags = ring->rx_info + (index << priv->log_rx_info);
  585. va = page_address(frags[0].page) + frags[0].page_offset;
  586. /*
  587. * make sure we read the CQE after we read the ownership bit
  588. */
  589. dma_rmb();
  590. /* Drop packet on bad receive or bad checksum */
  591. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  592. MLX4_CQE_OPCODE_ERROR)) {
  593. en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
  594. ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
  595. ((struct mlx4_err_cqe *)cqe)->syndrome);
  596. goto next;
  597. }
  598. if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
  599. en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
  600. goto next;
  601. }
  602. /* Check if we need to drop the packet if SRIOV is not enabled
  603. * and not performing the selftest or flb disabled
  604. */
  605. if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
  606. const struct ethhdr *ethh = va;
  607. dma_addr_t dma;
  608. /* Get pointer to first fragment since we haven't
  609. * skb yet and cast it to ethhdr struct
  610. */
  611. dma = frags[0].dma + frags[0].page_offset;
  612. dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
  613. DMA_FROM_DEVICE);
  614. if (is_multicast_ether_addr(ethh->h_dest)) {
  615. struct mlx4_mac_entry *entry;
  616. struct hlist_head *bucket;
  617. unsigned int mac_hash;
  618. /* Drop the packet, since HW loopback-ed it */
  619. mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
  620. bucket = &priv->mac_hash[mac_hash];
  621. hlist_for_each_entry_rcu(entry, bucket, hlist) {
  622. if (ether_addr_equal_64bits(entry->mac,
  623. ethh->h_source))
  624. goto next;
  625. }
  626. }
  627. }
  628. if (unlikely(priv->validate_loopback)) {
  629. validate_loopback(priv, va);
  630. goto next;
  631. }
  632. /*
  633. * Packet is OK - process it.
  634. */
  635. length = be32_to_cpu(cqe->byte_cnt);
  636. length -= ring->fcs_del;
  637. /* A bpf program gets first chance to drop the packet. It may
  638. * read bytes but not past the end of the frag.
  639. */
  640. if (xdp_prog) {
  641. struct xdp_buff xdp;
  642. dma_addr_t dma;
  643. void *orig_data;
  644. u32 act;
  645. dma = frags[0].dma + frags[0].page_offset;
  646. dma_sync_single_for_cpu(priv->ddev, dma,
  647. priv->frag_info[0].frag_size,
  648. DMA_FROM_DEVICE);
  649. xdp.data_hard_start = va - frags[0].page_offset;
  650. xdp.data = va;
  651. xdp.data_end = xdp.data + length;
  652. orig_data = xdp.data;
  653. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  654. if (xdp.data != orig_data) {
  655. length = xdp.data_end - xdp.data;
  656. frags[0].page_offset = xdp.data -
  657. xdp.data_hard_start;
  658. va = xdp.data;
  659. }
  660. switch (act) {
  661. case XDP_PASS:
  662. break;
  663. case XDP_TX:
  664. if (likely(!mlx4_en_xmit_frame(ring, frags, dev,
  665. length, cq->ring,
  666. &doorbell_pending))) {
  667. frags[0].page = NULL;
  668. goto next;
  669. }
  670. trace_xdp_exception(dev, xdp_prog, act);
  671. goto xdp_drop_no_cnt; /* Drop on xmit failure */
  672. default:
  673. bpf_warn_invalid_xdp_action(act);
  674. case XDP_ABORTED:
  675. trace_xdp_exception(dev, xdp_prog, act);
  676. case XDP_DROP:
  677. ring->xdp_drop++;
  678. xdp_drop_no_cnt:
  679. goto next;
  680. }
  681. }
  682. ring->bytes += length;
  683. ring->packets++;
  684. skb = napi_get_frags(&cq->napi);
  685. if (!skb)
  686. goto next;
  687. if (unlikely(ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL)) {
  688. timestamp = mlx4_en_get_cqe_ts(cqe);
  689. mlx4_en_fill_hwtstamps(mdev, skb_hwtstamps(skb),
  690. timestamp);
  691. }
  692. skb_record_rx_queue(skb, cq->ring);
  693. if (likely(dev->features & NETIF_F_RXCSUM)) {
  694. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
  695. MLX4_CQE_STATUS_UDP)) {
  696. if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
  697. cqe->checksum == cpu_to_be16(0xffff)) {
  698. ip_summed = CHECKSUM_UNNECESSARY;
  699. l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
  700. (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
  701. if (l2_tunnel)
  702. skb->csum_level = 1;
  703. ring->csum_ok++;
  704. } else {
  705. goto csum_none;
  706. }
  707. } else {
  708. if (priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
  709. (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  710. MLX4_CQE_STATUS_IPV6))) {
  711. if (check_csum(cqe, skb, va, dev->features)) {
  712. goto csum_none;
  713. } else {
  714. ip_summed = CHECKSUM_COMPLETE;
  715. ring->csum_complete++;
  716. }
  717. } else {
  718. goto csum_none;
  719. }
  720. }
  721. } else {
  722. csum_none:
  723. ip_summed = CHECKSUM_NONE;
  724. ring->csum_none++;
  725. }
  726. skb->ip_summed = ip_summed;
  727. if (dev->features & NETIF_F_RXHASH)
  728. skb_set_hash(skb,
  729. be32_to_cpu(cqe->immed_rss_invalid),
  730. (ip_summed == CHECKSUM_UNNECESSARY) ?
  731. PKT_HASH_TYPE_L4 :
  732. PKT_HASH_TYPE_L3);
  733. if ((cqe->vlan_my_qpn &
  734. cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
  735. (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
  736. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  737. be16_to_cpu(cqe->sl_vid));
  738. else if ((cqe->vlan_my_qpn &
  739. cpu_to_be32(MLX4_CQE_SVLAN_PRESENT_MASK)) &&
  740. (dev->features & NETIF_F_HW_VLAN_STAG_RX))
  741. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
  742. be16_to_cpu(cqe->sl_vid));
  743. nr = mlx4_en_complete_rx_desc(priv, frags, skb, length);
  744. if (likely(nr)) {
  745. skb_shinfo(skb)->nr_frags = nr;
  746. skb->len = length;
  747. skb->data_len = length;
  748. napi_gro_frags(&cq->napi);
  749. } else {
  750. skb->vlan_tci = 0;
  751. skb_clear_hash(skb);
  752. }
  753. next:
  754. ++cq->mcq.cons_index;
  755. index = (cq->mcq.cons_index) & ring->size_mask;
  756. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  757. if (++polled == budget)
  758. break;
  759. }
  760. rcu_read_unlock();
  761. if (polled) {
  762. if (doorbell_pending)
  763. mlx4_en_xmit_doorbell(priv->tx_ring[TX_XDP][cq->ring]);
  764. mlx4_cq_set_ci(&cq->mcq);
  765. wmb(); /* ensure HW sees CQ consumer before we post new buffers */
  766. ring->cons = cq->mcq.cons_index;
  767. }
  768. AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
  769. if (mlx4_en_refill_rx_buffers(priv, ring))
  770. mlx4_en_update_rx_prod_db(ring);
  771. return polled;
  772. }
  773. void mlx4_en_rx_irq(struct mlx4_cq *mcq)
  774. {
  775. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  776. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  777. if (likely(priv->port_up))
  778. napi_schedule_irqoff(&cq->napi);
  779. else
  780. mlx4_en_arm_cq(priv, cq);
  781. }
  782. /* Rx CQ polling - called by NAPI */
  783. int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
  784. {
  785. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  786. struct net_device *dev = cq->dev;
  787. struct mlx4_en_priv *priv = netdev_priv(dev);
  788. int done;
  789. done = mlx4_en_process_rx_cq(dev, cq, budget);
  790. /* If we used up all the quota - we're probably not done yet... */
  791. if (done == budget) {
  792. const struct cpumask *aff;
  793. struct irq_data *idata;
  794. int cpu_curr;
  795. INC_PERF_COUNTER(priv->pstats.napi_quota);
  796. cpu_curr = smp_processor_id();
  797. idata = irq_desc_get_irq_data(cq->irq_desc);
  798. aff = irq_data_get_affinity_mask(idata);
  799. if (likely(cpumask_test_cpu(cpu_curr, aff)))
  800. return budget;
  801. /* Current cpu is not according to smp_irq_affinity -
  802. * probably affinity changed. Need to stop this NAPI
  803. * poll, and restart it on the right CPU.
  804. * Try to avoid returning a too small value (like 0),
  805. * to not fool net_rx_action() and its netdev_budget
  806. */
  807. if (done)
  808. done--;
  809. }
  810. /* Done for now */
  811. if (napi_complete_done(napi, done))
  812. mlx4_en_arm_cq(priv, cq);
  813. return done;
  814. }
  815. void mlx4_en_calc_rx_buf(struct net_device *dev)
  816. {
  817. struct mlx4_en_priv *priv = netdev_priv(dev);
  818. int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
  819. int i = 0;
  820. /* bpf requires buffers to be set up as 1 packet per page.
  821. * This only works when num_frags == 1.
  822. */
  823. if (priv->tx_ring_num[TX_XDP]) {
  824. priv->frag_info[0].frag_size = eff_mtu;
  825. /* This will gain efficient xdp frame recycling at the
  826. * expense of more costly truesize accounting
  827. */
  828. priv->frag_info[0].frag_stride = PAGE_SIZE;
  829. priv->dma_dir = PCI_DMA_BIDIRECTIONAL;
  830. priv->rx_headroom = XDP_PACKET_HEADROOM;
  831. i = 1;
  832. } else {
  833. int frag_size_max = 2048, buf_size = 0;
  834. /* should not happen, right ? */
  835. if (eff_mtu > PAGE_SIZE + (MLX4_EN_MAX_RX_FRAGS - 1) * 2048)
  836. frag_size_max = PAGE_SIZE;
  837. while (buf_size < eff_mtu) {
  838. int frag_stride, frag_size = eff_mtu - buf_size;
  839. int pad, nb;
  840. if (i < MLX4_EN_MAX_RX_FRAGS - 1)
  841. frag_size = min(frag_size, frag_size_max);
  842. priv->frag_info[i].frag_size = frag_size;
  843. frag_stride = ALIGN(frag_size, SMP_CACHE_BYTES);
  844. /* We can only pack 2 1536-bytes frames in on 4K page
  845. * Therefore, each frame would consume more bytes (truesize)
  846. */
  847. nb = PAGE_SIZE / frag_stride;
  848. pad = (PAGE_SIZE - nb * frag_stride) / nb;
  849. pad &= ~(SMP_CACHE_BYTES - 1);
  850. priv->frag_info[i].frag_stride = frag_stride + pad;
  851. buf_size += frag_size;
  852. i++;
  853. }
  854. priv->dma_dir = PCI_DMA_FROMDEVICE;
  855. priv->rx_headroom = 0;
  856. }
  857. priv->num_frags = i;
  858. priv->rx_skb_size = eff_mtu;
  859. priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
  860. en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
  861. eff_mtu, priv->num_frags);
  862. for (i = 0; i < priv->num_frags; i++) {
  863. en_dbg(DRV,
  864. priv,
  865. " frag:%d - size:%d stride:%d\n",
  866. i,
  867. priv->frag_info[i].frag_size,
  868. priv->frag_info[i].frag_stride);
  869. }
  870. }
  871. /* RSS related functions */
  872. static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
  873. struct mlx4_en_rx_ring *ring,
  874. enum mlx4_qp_state *state,
  875. struct mlx4_qp *qp)
  876. {
  877. struct mlx4_en_dev *mdev = priv->mdev;
  878. struct mlx4_qp_context *context;
  879. int err = 0;
  880. context = kmalloc(sizeof(*context), GFP_KERNEL);
  881. if (!context)
  882. return -ENOMEM;
  883. err = mlx4_qp_alloc(mdev->dev, qpn, qp, GFP_KERNEL);
  884. if (err) {
  885. en_err(priv, "Failed to allocate qp #%x\n", qpn);
  886. goto out;
  887. }
  888. qp->event = mlx4_en_sqp_event;
  889. memset(context, 0, sizeof *context);
  890. mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
  891. qpn, ring->cqn, -1, context);
  892. context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
  893. /* Cancel FCS removal if FW allows */
  894. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
  895. context->param3 |= cpu_to_be32(1 << 29);
  896. if (priv->dev->features & NETIF_F_RXFCS)
  897. ring->fcs_del = 0;
  898. else
  899. ring->fcs_del = ETH_FCS_LEN;
  900. } else
  901. ring->fcs_del = 0;
  902. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
  903. if (err) {
  904. mlx4_qp_remove(mdev->dev, qp);
  905. mlx4_qp_free(mdev->dev, qp);
  906. }
  907. mlx4_en_update_rx_prod_db(ring);
  908. out:
  909. kfree(context);
  910. return err;
  911. }
  912. int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
  913. {
  914. int err;
  915. u32 qpn;
  916. err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
  917. MLX4_RESERVE_A0_QP);
  918. if (err) {
  919. en_err(priv, "Failed reserving drop qpn\n");
  920. return err;
  921. }
  922. err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp, GFP_KERNEL);
  923. if (err) {
  924. en_err(priv, "Failed allocating drop qp\n");
  925. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  926. return err;
  927. }
  928. return 0;
  929. }
  930. void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
  931. {
  932. u32 qpn;
  933. qpn = priv->drop_qp.qpn;
  934. mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
  935. mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
  936. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  937. }
  938. /* Allocate rx qp's and configure them according to rss map */
  939. int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
  940. {
  941. struct mlx4_en_dev *mdev = priv->mdev;
  942. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  943. struct mlx4_qp_context context;
  944. struct mlx4_rss_context *rss_context;
  945. int rss_rings;
  946. void *ptr;
  947. u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
  948. MLX4_RSS_TCP_IPV6);
  949. int i, qpn;
  950. int err = 0;
  951. int good_qps = 0;
  952. en_dbg(DRV, priv, "Configuring rss steering\n");
  953. err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
  954. priv->rx_ring_num,
  955. &rss_map->base_qpn, 0);
  956. if (err) {
  957. en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
  958. return err;
  959. }
  960. for (i = 0; i < priv->rx_ring_num; i++) {
  961. qpn = rss_map->base_qpn + i;
  962. err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
  963. &rss_map->state[i],
  964. &rss_map->qps[i]);
  965. if (err)
  966. goto rss_err;
  967. ++good_qps;
  968. }
  969. /* Configure RSS indirection qp */
  970. err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, &rss_map->indir_qp, GFP_KERNEL);
  971. if (err) {
  972. en_err(priv, "Failed to allocate RSS indirection QP\n");
  973. goto rss_err;
  974. }
  975. rss_map->indir_qp.event = mlx4_en_sqp_event;
  976. mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
  977. priv->rx_ring[0]->cqn, -1, &context);
  978. if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
  979. rss_rings = priv->rx_ring_num;
  980. else
  981. rss_rings = priv->prof->rss_rings;
  982. ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
  983. + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
  984. rss_context = ptr;
  985. rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
  986. (rss_map->base_qpn));
  987. rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
  988. if (priv->mdev->profile.udp_rss) {
  989. rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
  990. rss_context->base_qpn_udp = rss_context->default_qpn;
  991. }
  992. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  993. en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
  994. rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
  995. }
  996. rss_context->flags = rss_mask;
  997. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  998. if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
  999. rss_context->hash_fn = MLX4_RSS_HASH_XOR;
  1000. } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
  1001. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1002. memcpy(rss_context->rss_key, priv->rss_key,
  1003. MLX4_EN_RSS_KEY_SIZE);
  1004. } else {
  1005. en_err(priv, "Unknown RSS hash function requested\n");
  1006. err = -EINVAL;
  1007. goto indir_err;
  1008. }
  1009. err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
  1010. &rss_map->indir_qp, &rss_map->indir_state);
  1011. if (err)
  1012. goto indir_err;
  1013. return 0;
  1014. indir_err:
  1015. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1016. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
  1017. mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
  1018. mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
  1019. rss_err:
  1020. for (i = 0; i < good_qps; i++) {
  1021. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1022. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1023. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1024. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1025. }
  1026. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1027. return err;
  1028. }
  1029. void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
  1030. {
  1031. struct mlx4_en_dev *mdev = priv->mdev;
  1032. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  1033. int i;
  1034. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1035. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
  1036. mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
  1037. mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
  1038. for (i = 0; i < priv->rx_ring_num; i++) {
  1039. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1040. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1041. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1042. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1043. }
  1044. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1045. }