Merge remote-tracking branches 'spi/topic/spidev', 'spi/topic/spidev-test', 'spi...
[sfrench/cifs-2.6.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->pc++;
56         sq->stats.nop++;
57
58         if (notify_hw) {
59                 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
60                 mlx5e_tx_notify_hw(sq, &wqe->ctrl, 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         u32 i = sq->dma_fifo_pc & sq->dma_fifo_mask;
85
86         sq->db.txq.dma_fifo[i].addr = addr;
87         sq->db.txq.dma_fifo[i].size = size;
88         sq->db.txq.dma_fifo[i].type = map_type;
89         sq->dma_fifo_pc++;
90 }
91
92 static inline struct mlx5e_sq_dma *mlx5e_dma_get(struct mlx5e_sq *sq, u32 i)
93 {
94         return &sq->db.txq.dma_fifo[i & sq->dma_fifo_mask];
95 }
96
97 static void mlx5e_dma_unmap_wqe_err(struct mlx5e_sq *sq, u8 num_dma)
98 {
99         int i;
100
101         for (i = 0; i < num_dma; i++) {
102                 struct mlx5e_sq_dma *last_pushed_dma =
103                         mlx5e_dma_get(sq, --sq->dma_fifo_pc);
104
105                 mlx5e_tx_dma_unmap(sq->pdev, last_pushed_dma);
106         }
107 }
108
109 u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
110                        void *accel_priv, select_queue_fallback_t fallback)
111 {
112         struct mlx5e_priv *priv = netdev_priv(dev);
113         int channel_ix = fallback(dev, skb);
114         int up = 0;
115
116         if (!netdev_get_num_tc(dev))
117                 return channel_ix;
118
119         if (skb_vlan_tag_present(skb))
120                 up = skb->vlan_tci >> VLAN_PRIO_SHIFT;
121
122         /* channel_ix can be larger than num_channels since
123          * dev->num_real_tx_queues = num_channels * num_tc
124          */
125         if (channel_ix >= priv->params.num_channels)
126                 channel_ix = reciprocal_scale(channel_ix,
127                                               priv->params.num_channels);
128
129         return priv->channeltc_to_txq_map[channel_ix][up];
130 }
131
132 static inline int mlx5e_skb_l2_header_offset(struct sk_buff *skb)
133 {
134 #define MLX5E_MIN_INLINE (ETH_HLEN + VLAN_HLEN)
135
136         return max(skb_network_offset(skb), MLX5E_MIN_INLINE);
137 }
138
139 static inline int mlx5e_skb_l3_header_offset(struct sk_buff *skb)
140 {
141         struct flow_keys keys;
142
143         if (skb_transport_header_was_set(skb))
144                 return skb_transport_offset(skb);
145         else if (skb_flow_dissect_flow_keys(skb, &keys, 0))
146                 return keys.control.thoff;
147         else
148                 return mlx5e_skb_l2_header_offset(skb);
149 }
150
151 static inline unsigned int mlx5e_calc_min_inline(enum mlx5_inline_modes mode,
152                                                  struct sk_buff *skb)
153 {
154         int hlen;
155
156         switch (mode) {
157         case MLX5_INLINE_MODE_NONE:
158                 return 0;
159         case MLX5_INLINE_MODE_TCP_UDP:
160                 hlen = eth_get_headlen(skb->data, skb_headlen(skb));
161                 if (hlen == ETH_HLEN && !skb_vlan_tag_present(skb))
162                         hlen += VLAN_HLEN;
163                 return hlen;
164         case MLX5_INLINE_MODE_IP:
165                 /* When transport header is set to zero, it means no transport
166                  * header. When transport header is set to 0xff's, it means
167                  * transport header wasn't set.
168                  */
169                 if (skb_transport_offset(skb))
170                         return mlx5e_skb_l3_header_offset(skb);
171                 /* fall through */
172         case MLX5_INLINE_MODE_L2:
173         default:
174                 return mlx5e_skb_l2_header_offset(skb);
175         }
176 }
177
178 static inline u16 mlx5e_get_inline_hdr_size(struct mlx5e_sq *sq,
179                                             struct sk_buff *skb, bool bf)
180 {
181         /* Some NIC TX decisions, e.g loopback, are based on the packet
182          * headers and occur before the data gather.
183          * Therefore these headers must be copied into the WQE
184          */
185         if (bf) {
186                 u16 ihs = skb_headlen(skb);
187
188                 if (skb_vlan_tag_present(skb))
189                         ihs += VLAN_HLEN;
190
191                 if (ihs <= sq->max_inline)
192                         return skb_headlen(skb);
193         }
194         return mlx5e_calc_min_inline(sq->min_inline_mode, skb);
195 }
196
197 static inline void mlx5e_tx_skb_pull_inline(unsigned char **skb_data,
198                                             unsigned int *skb_len,
199                                             unsigned int len)
200 {
201         *skb_len -= len;
202         *skb_data += len;
203 }
204
205 static inline void mlx5e_insert_vlan(void *start, struct sk_buff *skb, u16 ihs,
206                                      unsigned char **skb_data,
207                                      unsigned int *skb_len)
208 {
209         struct vlan_ethhdr *vhdr = (struct vlan_ethhdr *)start;
210         int cpy1_sz = 2 * ETH_ALEN;
211         int cpy2_sz = ihs - cpy1_sz;
212
213         memcpy(vhdr, *skb_data, cpy1_sz);
214         mlx5e_tx_skb_pull_inline(skb_data, skb_len, cpy1_sz);
215         vhdr->h_vlan_proto = skb->vlan_proto;
216         vhdr->h_vlan_TCI = cpu_to_be16(skb_vlan_tag_get(skb));
217         memcpy(&vhdr->h_vlan_encapsulated_proto, *skb_data, cpy2_sz);
218         mlx5e_tx_skb_pull_inline(skb_data, skb_len, cpy2_sz);
219 }
220
221 static netdev_tx_t mlx5e_sq_xmit(struct mlx5e_sq *sq, struct sk_buff *skb)
222 {
223         struct mlx5_wq_cyc       *wq   = &sq->wq;
224
225         u16 pi = sq->pc & wq->sz_m1;
226         struct mlx5e_tx_wqe      *wqe  = mlx5_wq_cyc_get_wqe(wq, pi);
227         struct mlx5e_tx_wqe_info *wi   = &sq->db.txq.wqe_info[pi];
228
229         struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
230         struct mlx5_wqe_eth_seg  *eseg = &wqe->eth;
231         struct mlx5_wqe_data_seg *dseg;
232
233         unsigned char *skb_data = skb->data;
234         unsigned int skb_len = skb->len;
235         u8  opcode = MLX5_OPCODE_SEND;
236         dma_addr_t dma_addr = 0;
237         unsigned int num_bytes;
238         bool bf = false;
239         u16 headlen;
240         u16 ds_cnt;
241         u16 ihs;
242         int i;
243
244         memset(wqe, 0, sizeof(*wqe));
245
246         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
247                 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM;
248                 if (skb->encapsulation) {
249                         eseg->cs_flags |= MLX5_ETH_WQE_L3_INNER_CSUM |
250                                           MLX5_ETH_WQE_L4_INNER_CSUM;
251                         sq->stats.csum_partial_inner++;
252                 } else {
253                         eseg->cs_flags |= MLX5_ETH_WQE_L4_CSUM;
254                 }
255         } else
256                 sq->stats.csum_none++;
257
258         if (sq->cc != sq->prev_cc) {
259                 sq->prev_cc = sq->cc;
260                 sq->bf_budget = (sq->cc == sq->pc) ? MLX5E_SQ_BF_BUDGET : 0;
261         }
262
263         if (skb_is_gso(skb)) {
264                 eseg->mss    = cpu_to_be16(skb_shinfo(skb)->gso_size);
265                 opcode       = MLX5_OPCODE_LSO;
266
267                 if (skb->encapsulation) {
268                         ihs = skb_inner_transport_offset(skb) + inner_tcp_hdrlen(skb);
269                         sq->stats.tso_inner_packets++;
270                         sq->stats.tso_inner_bytes += skb->len - ihs;
271                 } else {
272                         ihs = skb_transport_offset(skb) + tcp_hdrlen(skb);
273                         sq->stats.tso_packets++;
274                         sq->stats.tso_bytes += skb->len - ihs;
275                 }
276
277                 sq->stats.packets += skb_shinfo(skb)->gso_segs;
278                 num_bytes = skb->len + (skb_shinfo(skb)->gso_segs - 1) * ihs;
279         } else {
280                 bf = sq->bf_budget &&
281                      !skb->xmit_more &&
282                      !skb_shinfo(skb)->nr_frags;
283                 ihs = mlx5e_get_inline_hdr_size(sq, skb, bf);
284                 sq->stats.packets++;
285                 num_bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
286         }
287
288         sq->stats.bytes += num_bytes;
289         wi->num_bytes = num_bytes;
290
291         ds_cnt = sizeof(*wqe) / MLX5_SEND_WQE_DS;
292         if (ihs) {
293                 if (skb_vlan_tag_present(skb)) {
294                         mlx5e_insert_vlan(eseg->inline_hdr.start, skb, ihs, &skb_data, &skb_len);
295                         ihs += VLAN_HLEN;
296                 } else {
297                         memcpy(eseg->inline_hdr.start, skb_data, ihs);
298                         mlx5e_tx_skb_pull_inline(&skb_data, &skb_len, ihs);
299                 }
300                 eseg->inline_hdr.sz = cpu_to_be16(ihs);
301                 ds_cnt += DIV_ROUND_UP(ihs - sizeof(eseg->inline_hdr.start), MLX5_SEND_WQE_DS);
302         } else if (skb_vlan_tag_present(skb)) {
303                 eseg->insert.type = cpu_to_be16(MLX5_ETH_WQE_INSERT_VLAN);
304                 eseg->insert.vlan_tci = cpu_to_be16(skb_vlan_tag_get(skb));
305         }
306
307         dseg = (struct mlx5_wqe_data_seg *)cseg + ds_cnt;
308
309         wi->num_dma = 0;
310
311         headlen = skb_len - skb->data_len;
312         if (headlen) {
313                 dma_addr = dma_map_single(sq->pdev, skb_data, headlen,
314                                           DMA_TO_DEVICE);
315                 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
316                         goto dma_unmap_wqe_err;
317
318                 dseg->addr       = cpu_to_be64(dma_addr);
319                 dseg->lkey       = sq->mkey_be;
320                 dseg->byte_count = cpu_to_be32(headlen);
321
322                 mlx5e_dma_push(sq, dma_addr, headlen, MLX5E_DMA_MAP_SINGLE);
323                 wi->num_dma++;
324
325                 dseg++;
326         }
327
328         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
329                 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
330                 int fsz = skb_frag_size(frag);
331
332                 dma_addr = skb_frag_dma_map(sq->pdev, frag, 0, fsz,
333                                             DMA_TO_DEVICE);
334                 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
335                         goto dma_unmap_wqe_err;
336
337                 dseg->addr       = cpu_to_be64(dma_addr);
338                 dseg->lkey       = sq->mkey_be;
339                 dseg->byte_count = cpu_to_be32(fsz);
340
341                 mlx5e_dma_push(sq, dma_addr, fsz, MLX5E_DMA_MAP_PAGE);
342                 wi->num_dma++;
343
344                 dseg++;
345         }
346
347         ds_cnt += wi->num_dma;
348
349         cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | opcode);
350         cseg->qpn_ds           = cpu_to_be32((sq->sqn << 8) | ds_cnt);
351
352         sq->db.txq.skb[pi] = skb;
353
354         wi->num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS);
355         sq->pc += wi->num_wqebbs;
356
357         netdev_tx_sent_queue(sq->txq, wi->num_bytes);
358
359         if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
360                 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
361
362         if (unlikely(!mlx5e_sq_has_room_for(sq, MLX5E_SQ_STOP_ROOM))) {
363                 netif_tx_stop_queue(sq->txq);
364                 sq->stats.stopped++;
365         }
366
367         sq->stats.xmit_more += skb->xmit_more;
368         if (!skb->xmit_more || netif_xmit_stopped(sq->txq)) {
369                 int bf_sz = 0;
370
371                 if (bf && test_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state))
372                         bf_sz = wi->num_wqebbs << 3;
373
374                 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
375                 mlx5e_tx_notify_hw(sq, &wqe->ctrl, bf_sz);
376         }
377
378         /* fill sq edge with nops to avoid wqe wrap around */
379         while ((pi = (sq->pc & wq->sz_m1)) > sq->edge) {
380                 sq->db.txq.skb[pi] = NULL;
381                 mlx5e_send_nop(sq, false);
382         }
383
384         if (bf)
385                 sq->bf_budget--;
386
387         return NETDEV_TX_OK;
388
389 dma_unmap_wqe_err:
390         sq->stats.dropped++;
391         mlx5e_dma_unmap_wqe_err(sq, wi->num_dma);
392
393         dev_kfree_skb_any(skb);
394
395         return NETDEV_TX_OK;
396 }
397
398 netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev)
399 {
400         struct mlx5e_priv *priv = netdev_priv(dev);
401         struct mlx5e_sq *sq = priv->txq_to_sq_map[skb_get_queue_mapping(skb)];
402
403         return mlx5e_sq_xmit(sq, skb);
404 }
405
406 bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq, int napi_budget)
407 {
408         struct mlx5e_sq *sq;
409         u32 dma_fifo_cc;
410         u32 nbytes;
411         u16 npkts;
412         u16 sqcc;
413         int i;
414
415         sq = container_of(cq, struct mlx5e_sq, cq);
416
417         if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
418                 return false;
419
420         npkts = 0;
421         nbytes = 0;
422
423         /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
424          * otherwise a cq overrun may occur
425          */
426         sqcc = sq->cc;
427
428         /* avoid dirtying sq cache line every cqe */
429         dma_fifo_cc = sq->dma_fifo_cc;
430
431         for (i = 0; i < MLX5E_TX_CQ_POLL_BUDGET; i++) {
432                 struct mlx5_cqe64 *cqe;
433                 u16 wqe_counter;
434                 bool last_wqe;
435
436                 cqe = mlx5e_get_cqe(cq);
437                 if (!cqe)
438                         break;
439
440                 mlx5_cqwq_pop(&cq->wq);
441
442                 wqe_counter = be16_to_cpu(cqe->wqe_counter);
443
444                 do {
445                         struct mlx5e_tx_wqe_info *wi;
446                         struct sk_buff *skb;
447                         u16 ci;
448                         int j;
449
450                         last_wqe = (sqcc == wqe_counter);
451
452                         ci = sqcc & sq->wq.sz_m1;
453                         skb = sq->db.txq.skb[ci];
454                         wi = &sq->db.txq.wqe_info[ci];
455
456                         if (unlikely(!skb)) { /* nop */
457                                 sqcc++;
458                                 continue;
459                         }
460
461                         if (unlikely(skb_shinfo(skb)->tx_flags &
462                                      SKBTX_HW_TSTAMP)) {
463                                 struct skb_shared_hwtstamps hwts = {};
464
465                                 mlx5e_fill_hwstamp(sq->tstamp,
466                                                    get_cqe_ts(cqe), &hwts);
467                                 skb_tstamp_tx(skb, &hwts);
468                         }
469
470                         for (j = 0; j < wi->num_dma; j++) {
471                                 struct mlx5e_sq_dma *dma =
472                                         mlx5e_dma_get(sq, dma_fifo_cc++);
473
474                                 mlx5e_tx_dma_unmap(sq->pdev, dma);
475                         }
476
477                         npkts++;
478                         nbytes += wi->num_bytes;
479                         sqcc += wi->num_wqebbs;
480                         napi_consume_skb(skb, napi_budget);
481                 } while (!last_wqe);
482         }
483
484         mlx5_cqwq_update_db_record(&cq->wq);
485
486         /* ensure cq space is freed before enabling more cqes */
487         wmb();
488
489         sq->dma_fifo_cc = dma_fifo_cc;
490         sq->cc = sqcc;
491
492         netdev_tx_completed_queue(sq->txq, npkts, nbytes);
493
494         if (netif_tx_queue_stopped(sq->txq) &&
495             mlx5e_sq_has_room_for(sq, MLX5E_SQ_STOP_ROOM)) {
496                 netif_tx_wake_queue(sq->txq);
497                 sq->stats.wake++;
498         }
499
500         return (i == MLX5E_TX_CQ_POLL_BUDGET);
501 }
502
503 static void mlx5e_free_txq_sq_descs(struct mlx5e_sq *sq)
504 {
505         struct mlx5e_tx_wqe_info *wi;
506         struct sk_buff *skb;
507         u16 ci;
508         int i;
509
510         while (sq->cc != sq->pc) {
511                 ci = sq->cc & sq->wq.sz_m1;
512                 skb = sq->db.txq.skb[ci];
513                 wi = &sq->db.txq.wqe_info[ci];
514
515                 if (!skb) { /* nop */
516                         sq->cc++;
517                         continue;
518                 }
519
520                 for (i = 0; i < wi->num_dma; i++) {
521                         struct mlx5e_sq_dma *dma =
522                                 mlx5e_dma_get(sq, sq->dma_fifo_cc++);
523
524                         mlx5e_tx_dma_unmap(sq->pdev, dma);
525                 }
526
527                 dev_kfree_skb_any(skb);
528                 sq->cc += wi->num_wqebbs;
529         }
530 }
531
532 static void mlx5e_free_xdp_sq_descs(struct mlx5e_sq *sq)
533 {
534         struct mlx5e_sq_wqe_info *wi;
535         struct mlx5e_dma_info *di;
536         u16 ci;
537
538         while (sq->cc != sq->pc) {
539                 ci = sq->cc & sq->wq.sz_m1;
540                 di = &sq->db.xdp.di[ci];
541                 wi = &sq->db.xdp.wqe_info[ci];
542
543                 if (wi->opcode == MLX5_OPCODE_NOP) {
544                         sq->cc++;
545                         continue;
546                 }
547
548                 sq->cc += wi->num_wqebbs;
549
550                 mlx5e_page_release(&sq->channel->rq, di, false);
551         }
552 }
553
554 void mlx5e_free_sq_descs(struct mlx5e_sq *sq)
555 {
556         switch (sq->type) {
557         case MLX5E_SQ_TXQ:
558                 mlx5e_free_txq_sq_descs(sq);
559                 break;
560         case MLX5E_SQ_XDP:
561                 mlx5e_free_xdp_sq_descs(sq);
562                 break;
563         }
564 }