a05c070cbc2f756d2b271ee73de9d21b1a9f4b4a
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tx.c
1 /*
2  * Copyright (c) 2015-2016, 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/tcp.h>
34 #include <linux/if_vlan.h>
35 #include "en.h"
36
37 #define MLX5E_SQ_NOPS_ROOM  MLX5_SEND_WQE_MAX_WQEBBS
38 #define MLX5E_SQ_STOP_ROOM (MLX5_SEND_WQE_MAX_WQEBBS +\
39                             MLX5E_SQ_NOPS_ROOM)
40
41 void mlx5e_send_nop(struct mlx5e_sq *sq, bool notify_hw)
42 {
43         struct mlx5_wq_cyc                *wq  = &sq->wq;
44
45         u16 pi = sq->pc & wq->sz_m1;
46         struct mlx5e_tx_wqe              *wqe  = mlx5_wq_cyc_get_wqe(wq, pi);
47
48         struct mlx5_wqe_ctrl_seg         *cseg = &wqe->ctrl;
49
50         memset(cseg, 0, sizeof(*cseg));
51
52         cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_NOP);
53         cseg->qpn_ds           = cpu_to_be32((sq->sqn << 8) | 0x01);
54
55         sq->skb[pi] = NULL;
56         sq->pc++;
57
58         if (notify_hw) {
59                 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
60                 mlx5e_tx_notify_hw(sq, wqe, 0);
61         }
62 }
63
64 static inline void mlx5e_tx_dma_unmap(struct device *pdev,
65                                       struct mlx5e_sq_dma *dma)
66 {
67         switch (dma->type) {
68         case MLX5E_DMA_MAP_SINGLE:
69                 dma_unmap_single(pdev, dma->addr, dma->size, DMA_TO_DEVICE);
70                 break;
71         case MLX5E_DMA_MAP_PAGE:
72                 dma_unmap_page(pdev, dma->addr, dma->size, DMA_TO_DEVICE);
73                 break;
74         default:
75                 WARN_ONCE(true, "mlx5e_tx_dma_unmap unknown DMA type!\n");
76         }
77 }
78
79 static inline void mlx5e_dma_push(struct mlx5e_sq *sq,
80                                   dma_addr_t addr,
81                                   u32 size,
82                                   enum mlx5e_dma_map_type map_type)
83 {
84         sq->dma_fifo[sq->dma_fifo_pc & sq->dma_fifo_mask].addr = addr;
85         sq->dma_fifo[sq->dma_fifo_pc & sq->dma_fifo_mask].size = size;
86         sq->dma_fifo[sq->dma_fifo_pc & sq->dma_fifo_mask].type = map_type;
87         sq->dma_fifo_pc++;
88 }
89
90 static inline struct mlx5e_sq_dma *mlx5e_dma_get(struct mlx5e_sq *sq, u32 i)
91 {
92         return &sq->dma_fifo[i & sq->dma_fifo_mask];
93 }
94
95 static void mlx5e_dma_unmap_wqe_err(struct mlx5e_sq *sq, u8 num_dma)
96 {
97         int i;
98
99         for (i = 0; i < num_dma; i++) {
100                 struct mlx5e_sq_dma *last_pushed_dma =
101                         mlx5e_dma_get(sq, --sq->dma_fifo_pc);
102
103                 mlx5e_tx_dma_unmap(sq->pdev, last_pushed_dma);
104         }
105 }
106
107 u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
108                        void *accel_priv, select_queue_fallback_t fallback)
109 {
110         struct mlx5e_priv *priv = netdev_priv(dev);
111         int channel_ix = fallback(dev, skb);
112         int up = (netdev_get_num_tc(dev) && skb_vlan_tag_present(skb)) ?
113                  skb->vlan_tci >> VLAN_PRIO_SHIFT : 0;
114
115         return priv->channeltc_to_txq_map[channel_ix][up];
116 }
117
118 static inline u16 mlx5e_get_inline_hdr_size(struct mlx5e_sq *sq,
119                                             struct sk_buff *skb, bool bf)
120 {
121         /* Some NIC TX decisions, e.g loopback, are based on the packet
122          * headers and occur before the data gather.
123          * Therefore these headers must be copied into the WQE
124          */
125 #define MLX5E_MIN_INLINE ETH_HLEN
126
127         if (bf) {
128                 u16 ihs = skb_headlen(skb);
129
130                 if (skb_vlan_tag_present(skb))
131                         ihs += VLAN_HLEN;
132
133                 if (ihs <= sq->max_inline)
134                         return skb_headlen(skb);
135         }
136
137         return MLX5E_MIN_INLINE;
138 }
139
140 static inline void mlx5e_tx_skb_pull_inline(unsigned char **skb_data,
141                                             unsigned int *skb_len,
142                                             unsigned int len)
143 {
144         *skb_len -= len;
145         *skb_data += len;
146 }
147
148 static inline void mlx5e_insert_vlan(void *start, struct sk_buff *skb, u16 ihs,
149                                      unsigned char **skb_data,
150                                      unsigned int *skb_len)
151 {
152         struct vlan_ethhdr *vhdr = (struct vlan_ethhdr *)start;
153         int cpy1_sz = 2 * ETH_ALEN;
154         int cpy2_sz = ihs - cpy1_sz;
155
156         memcpy(vhdr, *skb_data, cpy1_sz);
157         mlx5e_tx_skb_pull_inline(skb_data, skb_len, cpy1_sz);
158         vhdr->h_vlan_proto = skb->vlan_proto;
159         vhdr->h_vlan_TCI = cpu_to_be16(skb_vlan_tag_get(skb));
160         memcpy(&vhdr->h_vlan_encapsulated_proto, *skb_data, cpy2_sz);
161         mlx5e_tx_skb_pull_inline(skb_data, skb_len, cpy2_sz);
162 }
163
164 static netdev_tx_t mlx5e_sq_xmit(struct mlx5e_sq *sq, struct sk_buff *skb)
165 {
166         struct mlx5_wq_cyc       *wq   = &sq->wq;
167
168         u16 pi = sq->pc & wq->sz_m1;
169         struct mlx5e_tx_wqe      *wqe  = mlx5_wq_cyc_get_wqe(wq, pi);
170         struct mlx5e_tx_wqe_info *wi   = &sq->wqe_info[pi];
171
172         struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
173         struct mlx5_wqe_eth_seg  *eseg = &wqe->eth;
174         struct mlx5_wqe_data_seg *dseg;
175
176         unsigned char *skb_data = skb->data;
177         unsigned int skb_len = skb->len;
178         u8  opcode = MLX5_OPCODE_SEND;
179         dma_addr_t dma_addr = 0;
180         bool bf = false;
181         u16 headlen;
182         u16 ds_cnt;
183         u16 ihs;
184         int i;
185
186         memset(wqe, 0, sizeof(*wqe));
187
188         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
189                 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM;
190                 if (skb->encapsulation) {
191                         eseg->cs_flags |= MLX5_ETH_WQE_L3_INNER_CSUM |
192                                           MLX5_ETH_WQE_L4_INNER_CSUM;
193                         sq->stats.csum_offload_inner++;
194                 } else {
195                         eseg->cs_flags |= MLX5_ETH_WQE_L4_CSUM;
196                 }
197         } else
198                 sq->stats.csum_offload_none++;
199
200         if (sq->cc != sq->prev_cc) {
201                 sq->prev_cc = sq->cc;
202                 sq->bf_budget = (sq->cc == sq->pc) ? MLX5E_SQ_BF_BUDGET : 0;
203         }
204
205         if (skb_is_gso(skb)) {
206                 eseg->mss    = cpu_to_be16(skb_shinfo(skb)->gso_size);
207                 opcode       = MLX5_OPCODE_LSO;
208
209                 if (skb->encapsulation) {
210                         ihs = skb_inner_transport_offset(skb) + inner_tcp_hdrlen(skb);
211                         sq->stats.tso_inner_packets++;
212                         sq->stats.tso_inner_bytes += skb->len - ihs;
213                 } else {
214                         ihs = skb_transport_offset(skb) + tcp_hdrlen(skb);
215                         sq->stats.tso_packets++;
216                         sq->stats.tso_bytes += skb->len - ihs;
217                 }
218
219                 wi->num_bytes = skb->len +
220                                 (skb_shinfo(skb)->gso_segs - 1) * ihs;
221         } else {
222                 bf = sq->bf_budget &&
223                      !skb->xmit_more &&
224                      !skb_shinfo(skb)->nr_frags;
225                 ihs = mlx5e_get_inline_hdr_size(sq, skb, bf);
226                 wi->num_bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
227         }
228
229         if (skb_vlan_tag_present(skb)) {
230                 mlx5e_insert_vlan(eseg->inline_hdr_start, skb, ihs, &skb_data,
231                                   &skb_len);
232                 ihs += VLAN_HLEN;
233         } else {
234                 memcpy(eseg->inline_hdr_start, skb_data, ihs);
235                 mlx5e_tx_skb_pull_inline(&skb_data, &skb_len, ihs);
236         }
237
238         eseg->inline_hdr_sz = cpu_to_be16(ihs);
239
240         ds_cnt  = sizeof(*wqe) / MLX5_SEND_WQE_DS;
241         ds_cnt += DIV_ROUND_UP(ihs - sizeof(eseg->inline_hdr_start),
242                                MLX5_SEND_WQE_DS);
243         dseg    = (struct mlx5_wqe_data_seg *)cseg + ds_cnt;
244
245         wi->num_dma = 0;
246
247         headlen = skb_len - skb->data_len;
248         if (headlen) {
249                 dma_addr = dma_map_single(sq->pdev, skb_data, headlen,
250                                           DMA_TO_DEVICE);
251                 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
252                         goto dma_unmap_wqe_err;
253
254                 dseg->addr       = cpu_to_be64(dma_addr);
255                 dseg->lkey       = sq->mkey_be;
256                 dseg->byte_count = cpu_to_be32(headlen);
257
258                 mlx5e_dma_push(sq, dma_addr, headlen, MLX5E_DMA_MAP_SINGLE);
259                 wi->num_dma++;
260
261                 dseg++;
262         }
263
264         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
265                 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
266                 int fsz = skb_frag_size(frag);
267
268                 dma_addr = skb_frag_dma_map(sq->pdev, frag, 0, fsz,
269                                             DMA_TO_DEVICE);
270                 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
271                         goto dma_unmap_wqe_err;
272
273                 dseg->addr       = cpu_to_be64(dma_addr);
274                 dseg->lkey       = sq->mkey_be;
275                 dseg->byte_count = cpu_to_be32(fsz);
276
277                 mlx5e_dma_push(sq, dma_addr, fsz, MLX5E_DMA_MAP_PAGE);
278                 wi->num_dma++;
279
280                 dseg++;
281         }
282
283         ds_cnt += wi->num_dma;
284
285         cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | opcode);
286         cseg->qpn_ds           = cpu_to_be32((sq->sqn << 8) | ds_cnt);
287
288         sq->skb[pi] = skb;
289
290         wi->num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
291         sq->pc += wi->num_wqebbs;
292
293         netdev_tx_sent_queue(sq->txq, wi->num_bytes);
294
295         if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
296                 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
297
298         if (unlikely(!mlx5e_sq_has_room_for(sq, MLX5E_SQ_STOP_ROOM))) {
299                 netif_tx_stop_queue(sq->txq);
300                 sq->stats.stopped++;
301         }
302
303         if (!skb->xmit_more || netif_xmit_stopped(sq->txq)) {
304                 int bf_sz = 0;
305
306                 if (bf && sq->uar_bf_map)
307                         bf_sz = wi->num_wqebbs << 3;
308
309                 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
310                 mlx5e_tx_notify_hw(sq, wqe, bf_sz);
311         }
312
313         /* fill sq edge with nops to avoid wqe wrap around */
314         while ((sq->pc & wq->sz_m1) > sq->edge)
315                 mlx5e_send_nop(sq, false);
316
317         sq->bf_budget = bf ? sq->bf_budget - 1 : 0;
318
319         sq->stats.packets++;
320         return NETDEV_TX_OK;
321
322 dma_unmap_wqe_err:
323         sq->stats.dropped++;
324         mlx5e_dma_unmap_wqe_err(sq, wi->num_dma);
325
326         dev_kfree_skb_any(skb);
327
328         return NETDEV_TX_OK;
329 }
330
331 netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev)
332 {
333         struct mlx5e_priv *priv = netdev_priv(dev);
334         struct mlx5e_sq *sq = priv->txq_to_sq_map[skb_get_queue_mapping(skb)];
335
336         return mlx5e_sq_xmit(sq, skb);
337 }
338
339 bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq)
340 {
341         struct mlx5e_sq *sq;
342         u32 dma_fifo_cc;
343         u32 nbytes;
344         u16 npkts;
345         u16 sqcc;
346         int i;
347
348         /* avoid accessing cq (dma coherent memory) if not needed */
349         if (!test_and_clear_bit(MLX5E_CQ_HAS_CQES, &cq->flags))
350                 return false;
351
352         sq = container_of(cq, struct mlx5e_sq, cq);
353
354         npkts = 0;
355         nbytes = 0;
356
357         /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
358          * otherwise a cq overrun may occur
359          */
360         sqcc = sq->cc;
361
362         /* avoid dirtying sq cache line every cqe */
363         dma_fifo_cc = sq->dma_fifo_cc;
364
365         for (i = 0; i < MLX5E_TX_CQ_POLL_BUDGET; i++) {
366                 struct mlx5_cqe64 *cqe;
367                 u16 wqe_counter;
368                 bool last_wqe;
369
370                 cqe = mlx5e_get_cqe(cq);
371                 if (!cqe)
372                         break;
373
374                 mlx5_cqwq_pop(&cq->wq);
375
376                 wqe_counter = be16_to_cpu(cqe->wqe_counter);
377
378                 do {
379                         struct mlx5e_tx_wqe_info *wi;
380                         struct sk_buff *skb;
381                         u16 ci;
382                         int j;
383
384                         last_wqe = (sqcc == wqe_counter);
385
386                         ci = sqcc & sq->wq.sz_m1;
387                         skb = sq->skb[ci];
388                         wi = &sq->wqe_info[ci];
389
390                         if (unlikely(!skb)) { /* nop */
391                                 sq->stats.nop++;
392                                 sqcc++;
393                                 continue;
394                         }
395
396                         if (unlikely(skb_shinfo(skb)->tx_flags &
397                                      SKBTX_HW_TSTAMP)) {
398                                 struct skb_shared_hwtstamps hwts = {};
399
400                                 mlx5e_fill_hwstamp(sq->tstamp,
401                                                    get_cqe_ts(cqe), &hwts);
402                                 skb_tstamp_tx(skb, &hwts);
403                         }
404
405                         for (j = 0; j < wi->num_dma; j++) {
406                                 struct mlx5e_sq_dma *dma =
407                                         mlx5e_dma_get(sq, dma_fifo_cc++);
408
409                                 mlx5e_tx_dma_unmap(sq->pdev, dma);
410                         }
411
412                         npkts++;
413                         nbytes += wi->num_bytes;
414                         sqcc += wi->num_wqebbs;
415                         dev_kfree_skb(skb);
416                 } while (!last_wqe);
417         }
418
419         mlx5_cqwq_update_db_record(&cq->wq);
420
421         /* ensure cq space is freed before enabling more cqes */
422         wmb();
423
424         sq->dma_fifo_cc = dma_fifo_cc;
425         sq->cc = sqcc;
426
427         netdev_tx_completed_queue(sq->txq, npkts, nbytes);
428
429         if (netif_tx_queue_stopped(sq->txq) &&
430             mlx5e_sq_has_room_for(sq, MLX5E_SQ_STOP_ROOM) &&
431             likely(test_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state))) {
432                                 netif_tx_wake_queue(sq->txq);
433                                 sq->stats.wake++;
434         }
435         if (i == MLX5E_TX_CQ_POLL_BUDGET) {
436                 set_bit(MLX5E_CQ_HAS_CQES, &cq->flags);
437                 return true;
438         }
439
440         return false;
441 }