blob: 71f3a5d244ff054755c25da30298dc8c021e30f1 [file] [log] [blame]
Amir Vadaie586b3b2015-05-28 22:28:46 +03001/*
2 * Copyright (c) 2015, 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 <linux/ip.h>
34#include <linux/ipv6.h>
35#include <linux/tcp.h>
Eric Dumazet7ae92ae2015-11-18 06:30:55 -080036#include <net/busy_poll.h>
Amir Vadaie586b3b2015-05-28 22:28:46 +030037#include "en.h"
Amir Vadai12185a92016-03-08 12:42:38 +020038#include "en_tc.h"
Amir Vadaie586b3b2015-05-28 22:28:46 +030039
Eran Ben Elishaef9814d2015-12-29 14:58:31 +020040static inline bool mlx5e_rx_hw_stamp(struct mlx5e_tstamp *tstamp)
41{
42 return tstamp->hwtstamp_config.rx_filter == HWTSTAMP_FILTER_ALL;
43}
44
Tariq Toukan2f48af12016-04-20 22:02:12 +030045int mlx5e_alloc_rx_wqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe *wqe, u16 ix)
Amir Vadaie586b3b2015-05-28 22:28:46 +030046{
47 struct sk_buff *skb;
48 dma_addr_t dma_addr;
49
50 skb = netdev_alloc_skb(rq->netdev, rq->wqe_sz);
51 if (unlikely(!skb))
52 return -ENOMEM;
53
Amir Vadaie586b3b2015-05-28 22:28:46 +030054 dma_addr = dma_map_single(rq->pdev,
55 /* hw start padding */
Saeed Mahameedfc11fbf2015-06-11 14:47:28 +030056 skb->data,
57 /* hw end padding */
Amir Vadaie586b3b2015-05-28 22:28:46 +030058 rq->wqe_sz,
59 DMA_FROM_DEVICE);
60
61 if (unlikely(dma_mapping_error(rq->pdev, dma_addr)))
62 goto err_free_skb;
63
Saeed Mahameedfc11fbf2015-06-11 14:47:28 +030064 skb_reserve(skb, MLX5E_NET_IP_ALIGN);
65
Amir Vadaie586b3b2015-05-28 22:28:46 +030066 *((dma_addr_t *)skb->cb) = dma_addr;
67 wqe->data.addr = cpu_to_be64(dma_addr + MLX5E_NET_IP_ALIGN);
68
69 rq->skb[ix] = skb;
70
71 return 0;
72
73err_free_skb:
74 dev_kfree_skb(skb);
75
76 return -ENOMEM;
77}
78
Tariq Toukan461017c2016-04-20 22:02:13 +030079int mlx5e_alloc_rx_mpwqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe *wqe, u16 ix)
80{
81 struct mlx5e_mpw_info *wi = &rq->wqe_info[ix];
82 gfp_t gfp_mask;
83 int i;
84
85 gfp_mask = GFP_ATOMIC | __GFP_COLD | __GFP_MEMALLOC;
86 wi->dma_info.page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
87 MLX5_MPWRQ_WQE_PAGE_ORDER);
88 if (unlikely(!wi->dma_info.page))
89 return -ENOMEM;
90
91 wi->dma_info.addr = dma_map_page(rq->pdev, wi->dma_info.page, 0,
92 rq->wqe_sz, PCI_DMA_FROMDEVICE);
93 if (unlikely(dma_mapping_error(rq->pdev, wi->dma_info.addr))) {
94 put_page(wi->dma_info.page);
95 return -ENOMEM;
96 }
97
98 /* We split the high-order page into order-0 ones and manage their
99 * reference counter to minimize the memory held by small skb fragments
100 */
101 split_page(wi->dma_info.page, MLX5_MPWRQ_WQE_PAGE_ORDER);
102 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++) {
103 atomic_add(MLX5_MPWRQ_STRIDES_PER_PAGE,
104 &wi->dma_info.page[i]._count);
105 wi->skbs_frags[i] = 0;
106 }
107
108 wi->consumed_strides = 0;
109 wqe->data.addr = cpu_to_be64(wi->dma_info.addr);
110
111 return 0;
112}
113
Amir Vadaie586b3b2015-05-28 22:28:46 +0300114bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
115{
116 struct mlx5_wq_ll *wq = &rq->wq;
117
118 if (unlikely(!test_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state)))
119 return false;
120
121 while (!mlx5_wq_ll_is_full(wq)) {
122 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(wq, wq->head);
123
Tariq Toukan2f48af12016-04-20 22:02:12 +0300124 if (unlikely(rq->alloc_wqe(rq, wqe, wq->head)))
Amir Vadaie586b3b2015-05-28 22:28:46 +0300125 break;
126
127 mlx5_wq_ll_push(wq, be16_to_cpu(wqe->next.next_wqe_index));
128 }
129
130 /* ensure wqes are visible to device before updating doorbell record */
131 dma_wmb();
132
133 mlx5_wq_ll_update_db_record(wq);
134
135 return !mlx5_wq_ll_is_full(wq);
136}
137
Tariq Toukan461017c2016-04-20 22:02:13 +0300138static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
139 u32 cqe_bcnt)
Amir Vadaie586b3b2015-05-28 22:28:46 +0300140{
141 struct ethhdr *eth = (struct ethhdr *)(skb->data);
142 struct iphdr *ipv4 = (struct iphdr *)(skb->data + ETH_HLEN);
143 struct ipv6hdr *ipv6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
144 struct tcphdr *tcp;
145
146 u8 l4_hdr_type = get_cqe_l4_hdr_type(cqe);
147 int tcp_ack = ((CQE_L4_HDR_TYPE_TCP_ACK_NO_DATA == l4_hdr_type) ||
148 (CQE_L4_HDR_TYPE_TCP_ACK_AND_DATA == l4_hdr_type));
149
Tariq Toukan461017c2016-04-20 22:02:13 +0300150 u16 tot_len = cqe_bcnt - ETH_HLEN;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300151
152 if (eth->h_proto == htons(ETH_P_IP)) {
153 tcp = (struct tcphdr *)(skb->data + ETH_HLEN +
154 sizeof(struct iphdr));
155 ipv6 = NULL;
Achiad Shochatd9a40272015-08-16 16:04:49 +0300156 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300157 } else {
158 tcp = (struct tcphdr *)(skb->data + ETH_HLEN +
159 sizeof(struct ipv6hdr));
160 ipv4 = NULL;
Achiad Shochatd9a40272015-08-16 16:04:49 +0300161 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300162 }
163
164 if (get_cqe_lro_tcppsh(cqe))
165 tcp->psh = 1;
166
167 if (tcp_ack) {
168 tcp->ack = 1;
169 tcp->ack_seq = cqe->lro_ack_seq_num;
170 tcp->window = cqe->lro_tcp_win;
171 }
172
173 if (ipv4) {
174 ipv4->ttl = cqe->lro_min_ttl;
175 ipv4->tot_len = cpu_to_be16(tot_len);
176 ipv4->check = 0;
177 ipv4->check = ip_fast_csum((unsigned char *)ipv4,
178 ipv4->ihl);
179 } else {
180 ipv6->hop_limit = cqe->lro_min_ttl;
181 ipv6->payload_len = cpu_to_be16(tot_len -
182 sizeof(struct ipv6hdr));
183 }
184}
185
186static inline void mlx5e_skb_set_hash(struct mlx5_cqe64 *cqe,
187 struct sk_buff *skb)
188{
189 u8 cht = cqe->rss_hash_type;
190 int ht = (cht & CQE_RSS_HTYPE_L4) ? PKT_HASH_TYPE_L4 :
191 (cht & CQE_RSS_HTYPE_IP) ? PKT_HASH_TYPE_L3 :
192 PKT_HASH_TYPE_NONE;
193 skb_set_hash(skb, be32_to_cpu(cqe->rss_hash_result), ht);
194}
195
Achiad Shochatbbceefc2015-08-16 16:04:52 +0300196static inline bool is_first_ethertype_ip(struct sk_buff *skb)
197{
198 __be16 ethertype = ((struct ethhdr *)skb->data)->h_proto;
199
200 return (ethertype == htons(ETH_P_IP) || ethertype == htons(ETH_P_IPV6));
201}
202
203static inline void mlx5e_handle_csum(struct net_device *netdev,
204 struct mlx5_cqe64 *cqe,
205 struct mlx5e_rq *rq,
Matthew Finlay5f6d12d2016-02-22 18:17:30 +0200206 struct sk_buff *skb,
207 bool lro)
Achiad Shochatbbceefc2015-08-16 16:04:52 +0300208{
209 if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
210 goto csum_none;
211
Matthew Finlay5f6d12d2016-02-22 18:17:30 +0200212 if (lro) {
Achiad Shochatbbceefc2015-08-16 16:04:52 +0300213 skb->ip_summed = CHECKSUM_UNNECESSARY;
Matthew Finlay5f6d12d2016-02-22 18:17:30 +0200214 } else if (likely(is_first_ethertype_ip(skb))) {
Achiad Shochatbbceefc2015-08-16 16:04:52 +0300215 skb->ip_summed = CHECKSUM_COMPLETE;
David S. Millerecf842f2015-08-17 21:22:26 -0700216 skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
Achiad Shochatbbceefc2015-08-16 16:04:52 +0300217 rq->stats.csum_sw++;
218 } else {
219 goto csum_none;
220 }
221
222 return;
223
224csum_none:
225 skb->ip_summed = CHECKSUM_NONE;
226 rq->stats.csum_none++;
227}
228
Amir Vadaie586b3b2015-05-28 22:28:46 +0300229static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
Tariq Toukan461017c2016-04-20 22:02:13 +0300230 u32 cqe_bcnt,
Amir Vadaie586b3b2015-05-28 22:28:46 +0300231 struct mlx5e_rq *rq,
232 struct sk_buff *skb)
233{
234 struct net_device *netdev = rq->netdev;
Eran Ben Elishaef9814d2015-12-29 14:58:31 +0200235 struct mlx5e_tstamp *tstamp = rq->tstamp;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300236 int lro_num_seg;
237
Amir Vadaie586b3b2015-05-28 22:28:46 +0300238 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
239 if (lro_num_seg > 1) {
Tariq Toukan461017c2016-04-20 22:02:13 +0300240 mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
Achiad Shochatd9a40272015-08-16 16:04:49 +0300241 skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
Amir Vadaie586b3b2015-05-28 22:28:46 +0300242 rq->stats.lro_packets++;
243 rq->stats.lro_bytes += cqe_bcnt;
244 }
245
Eran Ben Elishaef9814d2015-12-29 14:58:31 +0200246 if (unlikely(mlx5e_rx_hw_stamp(tstamp)))
247 mlx5e_fill_hwstamp(tstamp, get_cqe_ts(cqe), skb_hwtstamps(skb));
248
Matthew Finlay5f6d12d2016-02-22 18:17:30 +0200249 mlx5e_handle_csum(netdev, cqe, rq, skb, !!lro_num_seg);
Amir Vadaie586b3b2015-05-28 22:28:46 +0300250
251 skb->protocol = eth_type_trans(skb, netdev);
252
253 skb_record_rx_queue(skb, rq->ix);
254
255 if (likely(netdev->features & NETIF_F_RXHASH))
256 mlx5e_skb_set_hash(cqe, skb);
257
258 if (cqe_has_vlan(cqe))
259 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
260 be16_to_cpu(cqe->vlan_info));
Amir Vadai12185a92016-03-08 12:42:38 +0200261
262 skb->mark = be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300263}
264
Tariq Toukan461017c2016-04-20 22:02:13 +0300265static inline void mlx5e_complete_rx_cqe(struct mlx5e_rq *rq,
266 struct mlx5_cqe64 *cqe,
267 u32 cqe_bcnt,
268 struct sk_buff *skb)
269{
270 rq->stats.packets++;
271 rq->stats.bytes += cqe_bcnt;
272 mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
273 napi_gro_receive(rq->cq.napi, skb);
274}
275
Tariq Toukan2f48af12016-04-20 22:02:12 +0300276void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
277{
278 struct mlx5e_rx_wqe *wqe;
279 struct sk_buff *skb;
280 __be16 wqe_counter_be;
281 u16 wqe_counter;
Tariq Toukan461017c2016-04-20 22:02:13 +0300282 u32 cqe_bcnt;
Tariq Toukan2f48af12016-04-20 22:02:12 +0300283
284 wqe_counter_be = cqe->wqe_counter;
285 wqe_counter = be16_to_cpu(wqe_counter_be);
286 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
287 skb = rq->skb[wqe_counter];
288 prefetch(skb->data);
289 rq->skb[wqe_counter] = NULL;
290
291 dma_unmap_single(rq->pdev,
292 *((dma_addr_t *)skb->cb),
293 rq->wqe_sz,
294 DMA_FROM_DEVICE);
295
296 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
297 rq->stats.wqe_err++;
298 dev_kfree_skb(skb);
299 goto wq_ll_pop;
300 }
301
Tariq Toukan461017c2016-04-20 22:02:13 +0300302 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
303 skb_put(skb, cqe_bcnt);
304
305 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
Tariq Toukan2f48af12016-04-20 22:02:12 +0300306
307wq_ll_pop:
308 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
309 &wqe->next.next_wqe_index);
310}
311
Tariq Toukan461017c2016-04-20 22:02:13 +0300312void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
313{
314 u16 cstrides = mpwrq_get_cqe_consumed_strides(cqe);
315 u16 stride_ix = mpwrq_get_cqe_stride_index(cqe);
316 u16 wqe_id = be16_to_cpu(cqe->wqe_id);
317 struct mlx5e_mpw_info *wi = &rq->wqe_info[wqe_id];
318 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_id);
319 struct sk_buff *skb;
320 u32 consumed_bytes;
321 u32 head_offset;
322 u32 frag_offset;
323 u32 wqe_offset;
324 u32 page_idx;
325 u16 byte_cnt;
326 u16 cqe_bcnt;
327 u16 headlen;
328 int i;
329
330 wi->consumed_strides += cstrides;
331
332 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
333 rq->stats.wqe_err++;
334 goto mpwrq_cqe_out;
335 }
336
337 if (unlikely(mpwrq_is_filler_cqe(cqe))) {
338 rq->stats.mpwqe_filler++;
339 goto mpwrq_cqe_out;
340 }
341
342 skb = netdev_alloc_skb(rq->netdev,
343 ALIGN(MLX5_MPWRQ_SMALL_PACKET_THRESHOLD,
344 sizeof(long)));
345 if (unlikely(!skb))
346 goto mpwrq_cqe_out;
347
348 prefetch(skb->data);
349 wqe_offset = stride_ix * MLX5_MPWRQ_STRIDE_SIZE;
350 consumed_bytes = cstrides * MLX5_MPWRQ_STRIDE_SIZE;
351 dma_sync_single_for_cpu(rq->pdev, wi->dma_info.addr + wqe_offset,
352 consumed_bytes, DMA_FROM_DEVICE);
353
354 head_offset = wqe_offset & (PAGE_SIZE - 1);
355 page_idx = wqe_offset >> PAGE_SHIFT;
356 cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
357 headlen = min_t(u16, MLX5_MPWRQ_SMALL_PACKET_THRESHOLD, cqe_bcnt);
358 frag_offset = head_offset + headlen;
359
360 byte_cnt = cqe_bcnt - headlen;
361 while (byte_cnt) {
362 u32 pg_consumed_bytes =
363 min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
364 unsigned int truesize =
365 ALIGN(pg_consumed_bytes, MLX5_MPWRQ_STRIDE_SIZE);
366
367 wi->skbs_frags[page_idx]++;
368 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
369 &wi->dma_info.page[page_idx], frag_offset,
370 pg_consumed_bytes, truesize);
371 byte_cnt -= pg_consumed_bytes;
372 frag_offset = 0;
373 page_idx++;
374 }
375
376 skb_copy_to_linear_data(skb,
377 page_address(wi->dma_info.page) + wqe_offset,
378 ALIGN(headlen, sizeof(long)));
379 /* skb linear part was allocated with headlen and aligned to long */
380 skb->tail += headlen;
381 skb->len += headlen;
382
383 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
384
385mpwrq_cqe_out:
386 if (likely(wi->consumed_strides < MLX5_MPWRQ_NUM_STRIDES))
387 return;
388
389 dma_unmap_page(rq->pdev, wi->dma_info.addr, rq->wqe_sz,
390 PCI_DMA_FROMDEVICE);
391 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++) {
392 atomic_sub(MLX5_MPWRQ_STRIDES_PER_PAGE - wi->skbs_frags[i],
393 &wi->dma_info.page[i]._count);
394 put_page(&wi->dma_info.page[i]);
395 }
396 mlx5_wq_ll_pop(&rq->wq, cqe->wqe_id, &wqe->next.next_wqe_index);
397}
398
Eric Dumazet44fb6fb2015-11-18 06:30:56 -0800399int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget)
Amir Vadaie586b3b2015-05-28 22:28:46 +0300400{
Achiad Shochate3391052015-06-23 17:14:20 +0300401 struct mlx5e_rq *rq = container_of(cq, struct mlx5e_rq, cq);
Eric Dumazet44fb6fb2015-11-18 06:30:56 -0800402 int work_done;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300403
Eric Dumazet44fb6fb2015-11-18 06:30:56 -0800404 for (work_done = 0; work_done < budget; work_done++) {
Tariq Toukan2f48af12016-04-20 22:02:12 +0300405 struct mlx5_cqe64 *cqe = mlx5e_get_cqe(cq);
Amir Vadaie586b3b2015-05-28 22:28:46 +0300406
Amir Vadaie586b3b2015-05-28 22:28:46 +0300407 if (!cqe)
408 break;
409
Achiad Shochata1f5a1a2015-06-23 17:14:21 +0300410 mlx5_cqwq_pop(&cq->wq);
411
Tariq Toukan2f48af12016-04-20 22:02:12 +0300412 rq->handle_rx_cqe(rq, cqe);
Amir Vadaie586b3b2015-05-28 22:28:46 +0300413 }
414
415 mlx5_cqwq_update_db_record(&cq->wq);
416
417 /* ensure cq space is freed before enabling more cqes */
418 wmb();
419
Eric Dumazet44fb6fb2015-11-18 06:30:56 -0800420 return work_done;
Amir Vadaie586b3b2015-05-28 22:28:46 +0300421}