2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/tcp.h>
34 #include <linux/if_vlan.h>
37 #define MLX5E_SQ_NOPS_ROOM MLX5_SEND_WQE_MAX_WQEBBS
38 #define MLX5E_SQ_STOP_ROOM (MLX5_SEND_WQE_MAX_WQEBBS +\
41 void mlx5e_send_nop(struct mlx5e_sq
*sq
, bool notify_hw
)
43 struct mlx5_wq_cyc
*wq
= &sq
->wq
;
45 u16 pi
= sq
->pc
& wq
->sz_m1
;
46 struct mlx5e_tx_wqe
*wqe
= mlx5_wq_cyc_get_wqe(wq
, pi
);
48 struct mlx5_wqe_ctrl_seg
*cseg
= &wqe
->ctrl
;
50 memset(cseg
, 0, sizeof(*cseg
));
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);
59 cseg
->fm_ce_se
= MLX5_WQE_CTRL_CQ_UPDATE
;
60 mlx5e_tx_notify_hw(sq
, wqe
, 0);
64 static inline void mlx5e_tx_dma_unmap(struct device
*pdev
,
65 struct mlx5e_sq_dma
*dma
)
68 case MLX5E_DMA_MAP_SINGLE
:
69 dma_unmap_single(pdev
, dma
->addr
, dma
->size
, DMA_TO_DEVICE
);
71 case MLX5E_DMA_MAP_PAGE
:
72 dma_unmap_page(pdev
, dma
->addr
, dma
->size
, DMA_TO_DEVICE
);
75 WARN_ONCE(true, "mlx5e_tx_dma_unmap unknown DMA type!\n");
79 static inline void mlx5e_dma_push(struct mlx5e_sq
*sq
,
82 enum mlx5e_dma_map_type map_type
)
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
;
90 static inline struct mlx5e_sq_dma
*mlx5e_dma_get(struct mlx5e_sq
*sq
, u32 i
)
92 return &sq
->dma_fifo
[i
& sq
->dma_fifo_mask
];
95 static void mlx5e_dma_unmap_wqe_err(struct mlx5e_sq
*sq
, u8 num_dma
)
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
);
103 mlx5e_tx_dma_unmap(sq
->pdev
, last_pushed_dma
);
107 u16
mlx5e_select_queue(struct net_device
*dev
, struct sk_buff
*skb
,
108 void *accel_priv
, select_queue_fallback_t fallback
)
110 struct mlx5e_priv
*priv
= netdev_priv(dev
);
111 int channel_ix
= fallback(dev
, skb
);
112 int up
= skb_vlan_tag_present(skb
) ?
113 skb
->vlan_tci
>> VLAN_PRIO_SHIFT
:
114 priv
->default_vlan_prio
;
115 int tc
= netdev_get_prio_tc_map(dev
, up
);
117 return priv
->channeltc_to_txq_map
[channel_ix
][tc
];
120 static inline u16
mlx5e_get_inline_hdr_size(struct mlx5e_sq
*sq
,
121 struct sk_buff
*skb
, bool bf
)
123 /* Some NIC TX decisions, e.g loopback, are based on the packet
124 * headers and occur before the data gather.
125 * Therefore these headers must be copied into the WQE
127 #define MLX5E_MIN_INLINE ETH_HLEN
130 u16 ihs
= skb_headlen(skb
);
132 if (skb_vlan_tag_present(skb
))
135 if (ihs
<= sq
->max_inline
)
136 return skb_headlen(skb
);
139 return MLX5E_MIN_INLINE
;
142 static inline void mlx5e_tx_skb_pull_inline(unsigned char **skb_data
,
143 unsigned int *skb_len
,
150 static inline void mlx5e_insert_vlan(void *start
, struct sk_buff
*skb
, u16 ihs
,
151 unsigned char **skb_data
,
152 unsigned int *skb_len
)
154 struct vlan_ethhdr
*vhdr
= (struct vlan_ethhdr
*)start
;
155 int cpy1_sz
= 2 * ETH_ALEN
;
156 int cpy2_sz
= ihs
- cpy1_sz
;
158 memcpy(vhdr
, *skb_data
, cpy1_sz
);
159 mlx5e_tx_skb_pull_inline(skb_data
, skb_len
, cpy1_sz
);
160 vhdr
->h_vlan_proto
= skb
->vlan_proto
;
161 vhdr
->h_vlan_TCI
= cpu_to_be16(skb_vlan_tag_get(skb
));
162 memcpy(&vhdr
->h_vlan_encapsulated_proto
, *skb_data
, cpy2_sz
);
163 mlx5e_tx_skb_pull_inline(skb_data
, skb_len
, cpy2_sz
);
166 static netdev_tx_t
mlx5e_sq_xmit(struct mlx5e_sq
*sq
, struct sk_buff
*skb
)
168 struct mlx5_wq_cyc
*wq
= &sq
->wq
;
170 u16 pi
= sq
->pc
& wq
->sz_m1
;
171 struct mlx5e_tx_wqe
*wqe
= mlx5_wq_cyc_get_wqe(wq
, pi
);
172 struct mlx5e_tx_wqe_info
*wi
= &sq
->wqe_info
[pi
];
174 struct mlx5_wqe_ctrl_seg
*cseg
= &wqe
->ctrl
;
175 struct mlx5_wqe_eth_seg
*eseg
= &wqe
->eth
;
176 struct mlx5_wqe_data_seg
*dseg
;
178 unsigned char *skb_data
= skb
->data
;
179 unsigned int skb_len
= skb
->len
;
180 u8 opcode
= MLX5_OPCODE_SEND
;
181 dma_addr_t dma_addr
= 0;
188 memset(wqe
, 0, sizeof(*wqe
));
190 if (likely(skb
->ip_summed
== CHECKSUM_PARTIAL
))
191 eseg
->cs_flags
= MLX5_ETH_WQE_L3_CSUM
| MLX5_ETH_WQE_L4_CSUM
;
193 sq
->stats
.csum_offload_none
++;
195 if (sq
->cc
!= sq
->prev_cc
) {
196 sq
->prev_cc
= sq
->cc
;
197 sq
->bf_budget
= (sq
->cc
== sq
->pc
) ? MLX5E_SQ_BF_BUDGET
: 0;
200 if (skb_is_gso(skb
)) {
203 eseg
->mss
= cpu_to_be16(skb_shinfo(skb
)->gso_size
);
204 opcode
= MLX5_OPCODE_LSO
;
205 ihs
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
206 payload_len
= skb
->len
- ihs
;
207 wi
->num_bytes
= skb
->len
+
208 (skb_shinfo(skb
)->gso_segs
- 1) * ihs
;
209 sq
->stats
.tso_packets
++;
210 sq
->stats
.tso_bytes
+= payload_len
;
212 bf
= sq
->bf_budget
&&
214 !skb_shinfo(skb
)->nr_frags
;
215 ihs
= mlx5e_get_inline_hdr_size(sq
, skb
, bf
);
216 wi
->num_bytes
= max_t(unsigned int, skb
->len
, ETH_ZLEN
);
219 if (skb_vlan_tag_present(skb
)) {
220 mlx5e_insert_vlan(eseg
->inline_hdr_start
, skb
, ihs
, &skb_data
,
224 memcpy(eseg
->inline_hdr_start
, skb_data
, ihs
);
225 mlx5e_tx_skb_pull_inline(&skb_data
, &skb_len
, ihs
);
228 eseg
->inline_hdr_sz
= cpu_to_be16(ihs
);
230 ds_cnt
= sizeof(*wqe
) / MLX5_SEND_WQE_DS
;
231 ds_cnt
+= DIV_ROUND_UP(ihs
- sizeof(eseg
->inline_hdr_start
),
233 dseg
= (struct mlx5_wqe_data_seg
*)cseg
+ ds_cnt
;
237 headlen
= skb_len
- skb
->data_len
;
239 dma_addr
= dma_map_single(sq
->pdev
, skb_data
, headlen
,
241 if (unlikely(dma_mapping_error(sq
->pdev
, dma_addr
)))
242 goto dma_unmap_wqe_err
;
244 dseg
->addr
= cpu_to_be64(dma_addr
);
245 dseg
->lkey
= sq
->mkey_be
;
246 dseg
->byte_count
= cpu_to_be32(headlen
);
248 mlx5e_dma_push(sq
, dma_addr
, headlen
, MLX5E_DMA_MAP_SINGLE
);
254 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
255 struct skb_frag_struct
*frag
= &skb_shinfo(skb
)->frags
[i
];
256 int fsz
= skb_frag_size(frag
);
258 dma_addr
= skb_frag_dma_map(sq
->pdev
, frag
, 0, fsz
,
260 if (unlikely(dma_mapping_error(sq
->pdev
, dma_addr
)))
261 goto dma_unmap_wqe_err
;
263 dseg
->addr
= cpu_to_be64(dma_addr
);
264 dseg
->lkey
= sq
->mkey_be
;
265 dseg
->byte_count
= cpu_to_be32(fsz
);
267 mlx5e_dma_push(sq
, dma_addr
, fsz
, MLX5E_DMA_MAP_PAGE
);
273 ds_cnt
+= wi
->num_dma
;
275 cseg
->opmod_idx_opcode
= cpu_to_be32((sq
->pc
<< 8) | opcode
);
276 cseg
->qpn_ds
= cpu_to_be32((sq
->sqn
<< 8) | ds_cnt
);
280 wi
->num_wqebbs
= DIV_ROUND_UP(ds_cnt
, MLX5_SEND_WQEBB_NUM_DS
);
281 sq
->pc
+= wi
->num_wqebbs
;
283 netdev_tx_sent_queue(sq
->txq
, wi
->num_bytes
);
285 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
))
286 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
288 if (unlikely(!mlx5e_sq_has_room_for(sq
, MLX5E_SQ_STOP_ROOM
))) {
289 netif_tx_stop_queue(sq
->txq
);
293 if (!skb
->xmit_more
|| netif_xmit_stopped(sq
->txq
)) {
296 if (bf
&& sq
->uar_bf_map
)
297 bf_sz
= wi
->num_wqebbs
<< 3;
299 cseg
->fm_ce_se
= MLX5_WQE_CTRL_CQ_UPDATE
;
300 mlx5e_tx_notify_hw(sq
, wqe
, bf_sz
);
303 /* fill sq edge with nops to avoid wqe wrap around */
304 while ((sq
->pc
& wq
->sz_m1
) > sq
->edge
)
305 mlx5e_send_nop(sq
, false);
307 sq
->bf_budget
= bf
? sq
->bf_budget
- 1 : 0;
314 mlx5e_dma_unmap_wqe_err(sq
, wi
->num_dma
);
316 dev_kfree_skb_any(skb
);
321 netdev_tx_t
mlx5e_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
323 struct mlx5e_priv
*priv
= netdev_priv(dev
);
324 struct mlx5e_sq
*sq
= priv
->txq_to_sq_map
[skb_get_queue_mapping(skb
)];
326 return mlx5e_sq_xmit(sq
, skb
);
329 bool mlx5e_poll_tx_cq(struct mlx5e_cq
*cq
)
338 sq
= container_of(cq
, struct mlx5e_sq
, cq
);
343 /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
344 * otherwise a cq overrun may occur
348 /* avoid dirtying sq cache line every cqe */
349 dma_fifo_cc
= sq
->dma_fifo_cc
;
351 for (i
= 0; i
< MLX5E_TX_CQ_POLL_BUDGET
; i
++) {
352 struct mlx5_cqe64
*cqe
;
356 cqe
= mlx5e_get_cqe(cq
);
360 mlx5_cqwq_pop(&cq
->wq
);
362 wqe_counter
= be16_to_cpu(cqe
->wqe_counter
);
365 struct mlx5e_tx_wqe_info
*wi
;
370 last_wqe
= (sqcc
== wqe_counter
);
372 ci
= sqcc
& sq
->wq
.sz_m1
;
374 wi
= &sq
->wqe_info
[ci
];
376 if (unlikely(!skb
)) { /* nop */
382 if (unlikely(skb_shinfo(skb
)->tx_flags
&
384 struct skb_shared_hwtstamps hwts
= {};
386 mlx5e_fill_hwstamp(sq
->tstamp
,
387 get_cqe_ts(cqe
), &hwts
);
388 skb_tstamp_tx(skb
, &hwts
);
391 for (j
= 0; j
< wi
->num_dma
; j
++) {
392 struct mlx5e_sq_dma
*dma
=
393 mlx5e_dma_get(sq
, dma_fifo_cc
++);
395 mlx5e_tx_dma_unmap(sq
->pdev
, dma
);
399 nbytes
+= wi
->num_bytes
;
400 sqcc
+= wi
->num_wqebbs
;
405 mlx5_cqwq_update_db_record(&cq
->wq
);
407 /* ensure cq space is freed before enabling more cqes */
410 sq
->dma_fifo_cc
= dma_fifo_cc
;
413 netdev_tx_completed_queue(sq
->txq
, npkts
, nbytes
);
415 if (netif_tx_queue_stopped(sq
->txq
) &&
416 mlx5e_sq_has_room_for(sq
, MLX5E_SQ_STOP_ROOM
) &&
417 likely(test_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE
, &sq
->state
))) {
418 netif_tx_wake_queue(sq
->txq
);
422 return (i
== MLX5E_TX_CQ_POLL_BUDGET
);