xref: /freebsd/sys/dev/mlx4/mlx4_en/mlx4_en_tx.c (revision 8657387683946d0c03e09fe77029edfe309eeb20)
1 /*
2  * Copyright (c) 2007, 2014 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 
34 #define	LINUXKPI_PARAM_PREFIX mlx4_
35 
36 #include <linux/page.h>
37 #include <dev/mlx4/cq.h>
38 #include <linux/slab.h>
39 #include <dev/mlx4/qp.h>
40 #include <linux/if_vlan.h>
41 #include <linux/vmalloc.h>
42 #include <linux/moduleparam.h>
43 
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip6.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcp_lro.h>
51 #include <netinet/udp.h>
52 
53 #include "en.h"
54 
55 enum {
56 	MAX_INLINE = 104, /* 128 - 16 - 4 - 4 */
57 	MAX_BF = 256,
58 	MIN_PKT_LEN = 17,
59 };
60 
61 static int inline_thold __read_mostly = MAX_INLINE;
62 
63 module_param_named(inline_thold, inline_thold, uint, 0444);
64 MODULE_PARM_DESC(inline_thold, "threshold for using inline data");
65 
66 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
67 			   struct mlx4_en_tx_ring **pring, u32 size,
68 			   u16 stride, int node, int queue_idx)
69 {
70 	struct mlx4_en_dev *mdev = priv->mdev;
71 	struct mlx4_en_tx_ring *ring;
72 	uint32_t x;
73 	int tmp;
74 	int err;
75 
76 	ring = kzalloc_node(sizeof(struct mlx4_en_tx_ring), GFP_KERNEL, node);
77 	if (!ring) {
78 		ring = kzalloc(sizeof(struct mlx4_en_tx_ring), GFP_KERNEL);
79 		if (!ring) {
80 			en_err(priv, "Failed allocating TX ring\n");
81 			return -ENOMEM;
82 		}
83 	}
84 
85 	/* Create DMA descriptor TAG */
86 	if ((err = -bus_dma_tag_create(
87 	    bus_get_dma_tag(mdev->pdev->dev.bsddev),
88 	    1,					/* any alignment */
89 	    0,					/* no boundary */
90 	    BUS_SPACE_MAXADDR,			/* lowaddr */
91 	    BUS_SPACE_MAXADDR,			/* highaddr */
92 	    NULL, NULL,				/* filter, filterarg */
93 	    MLX4_EN_TX_MAX_PAYLOAD_SIZE,	/* maxsize */
94 	    MLX4_EN_TX_MAX_MBUF_FRAGS,		/* nsegments */
95 	    MLX4_EN_TX_MAX_MBUF_SIZE,		/* maxsegsize */
96 	    0,					/* flags */
97 	    NULL, NULL,				/* lockfunc, lockfuncarg */
98 	    &ring->dma_tag)))
99 		goto done;
100 
101 	ring->size = size;
102 	ring->size_mask = size - 1;
103 	ring->stride = stride;
104 	ring->inline_thold = MAX(MIN_PKT_LEN, MIN(inline_thold, MAX_INLINE));
105 	mtx_init(&ring->tx_lock.m, "mlx4 tx", NULL, MTX_DEF);
106 	mtx_init(&ring->comp_lock.m, "mlx4 comp", NULL, MTX_DEF);
107 
108 	/* Allocate the buf ring */
109 	ring->br = buf_ring_alloc(MLX4_EN_DEF_TX_QUEUE_SIZE, M_DEVBUF,
110 		M_WAITOK, &ring->tx_lock.m);
111 	if (ring->br == NULL) {
112 		en_err(priv, "Failed allocating tx_info ring\n");
113 		err = -ENOMEM;
114 		goto err_free_dma_tag;
115 	}
116 
117 	tmp = size * sizeof(struct mlx4_en_tx_info);
118 	ring->tx_info = kzalloc_node(tmp, GFP_KERNEL, node);
119 	if (!ring->tx_info) {
120 		ring->tx_info = kzalloc(tmp, GFP_KERNEL);
121 		if (!ring->tx_info) {
122 			err = -ENOMEM;
123 			goto err_ring;
124 		}
125 	}
126 
127 	/* Create DMA descriptor MAPs */
128 	for (x = 0; x != size; x++) {
129 		err = -bus_dmamap_create(ring->dma_tag, 0,
130 		    &ring->tx_info[x].dma_map);
131 		if (err != 0) {
132 			while (x--) {
133 				bus_dmamap_destroy(ring->dma_tag,
134 				    ring->tx_info[x].dma_map);
135 			}
136 			goto err_info;
137 		}
138 	}
139 
140 	en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
141 		 ring->tx_info, tmp);
142 
143 	ring->buf_size = ALIGN(size * ring->stride, MLX4_EN_PAGE_SIZE);
144 
145 	/* Allocate HW buffers on provided NUMA node */
146 	err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size,
147 				 2 * PAGE_SIZE);
148 	if (err) {
149 		en_err(priv, "Failed allocating hwq resources\n");
150 		goto err_dma_map;
151 	}
152 
153 	err = mlx4_en_map_buffer(&ring->wqres.buf);
154 	if (err) {
155 		en_err(priv, "Failed to map TX buffer\n");
156 		goto err_hwq_res;
157 	}
158 
159 	ring->buf = ring->wqres.buf.direct.buf;
160 
161 	en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d "
162 	       "buf_size:%d dma:%llx\n", ring, ring->buf, ring->size,
163 	       ring->buf_size, (unsigned long long) ring->wqres.buf.direct.map);
164 
165 	err = mlx4_qp_reserve_range(mdev->dev, 1, 1, &ring->qpn,
166 				    MLX4_RESERVE_BF_QP);
167 	if (err) {
168 		en_err(priv, "failed reserving qp for TX ring\n");
169 		goto err_map;
170 	}
171 
172 	err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp);
173 	if (err) {
174 		en_err(priv, "Failed allocating qp %d\n", ring->qpn);
175 		goto err_reserve;
176 	}
177 	ring->qp.event = mlx4_en_sqp_event;
178 
179 	err = mlx4_bf_alloc(mdev->dev, &ring->bf, node);
180 	if (err) {
181 		en_dbg(DRV, priv, "working without blueflame (%d)", err);
182 		ring->bf.uar = &mdev->priv_uar;
183 		ring->bf.uar->map = mdev->uar_map;
184 		ring->bf_enabled = false;
185 	} else
186 		ring->bf_enabled = true;
187 	ring->queue_index = queue_idx;
188 	if (queue_idx < priv->num_tx_rings_p_up )
189 		CPU_SET(queue_idx, &ring->affinity_mask);
190 
191 	*pring = ring;
192 	return 0;
193 
194 err_reserve:
195 	mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
196 err_map:
197 	mlx4_en_unmap_buffer(&ring->wqres.buf);
198 err_hwq_res:
199 	mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
200 err_dma_map:
201 	for (x = 0; x != size; x++)
202 		bus_dmamap_destroy(ring->dma_tag, ring->tx_info[x].dma_map);
203 err_info:
204 	vfree(ring->tx_info);
205 err_ring:
206 	buf_ring_free(ring->br, M_DEVBUF);
207 err_free_dma_tag:
208 	bus_dma_tag_destroy(ring->dma_tag);
209 done:
210 	kfree(ring);
211 	return err;
212 }
213 
214 void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
215 			     struct mlx4_en_tx_ring **pring)
216 {
217 	struct mlx4_en_dev *mdev = priv->mdev;
218 	struct mlx4_en_tx_ring *ring = *pring;
219 	uint32_t x;
220 	en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
221 
222 	buf_ring_free(ring->br, M_DEVBUF);
223 	if (ring->bf_enabled)
224 		mlx4_bf_free(mdev->dev, &ring->bf);
225 	mlx4_qp_remove(mdev->dev, &ring->qp);
226 	mlx4_qp_free(mdev->dev, &ring->qp);
227 	mlx4_qp_release_range(priv->mdev->dev, ring->qpn, 1);
228 	mlx4_en_unmap_buffer(&ring->wqres.buf);
229 	mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
230 	for (x = 0; x != ring->size; x++)
231 		bus_dmamap_destroy(ring->dma_tag, ring->tx_info[x].dma_map);
232 	vfree(ring->tx_info);
233 	mtx_destroy(&ring->tx_lock.m);
234 	mtx_destroy(&ring->comp_lock.m);
235 	bus_dma_tag_destroy(ring->dma_tag);
236 	kfree(ring);
237 	*pring = NULL;
238 }
239 
240 int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
241 			     struct mlx4_en_tx_ring *ring,
242 			     int cq, int user_prio)
243 {
244 	struct mlx4_en_dev *mdev = priv->mdev;
245 	int err;
246 
247 	ring->cqn = cq;
248 	ring->prod = 0;
249 	ring->cons = 0xffffffff;
250 	ring->last_nr_txbb = 1;
251 	ring->poll_cnt = 0;
252 	ring->blocked = 0;
253 	memset(ring->buf, 0, ring->buf_size);
254 
255 	ring->qp_state = MLX4_QP_STATE_RST;
256 	ring->doorbell_qpn = ring->qp.qpn << 8;
257 
258 	mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
259 				ring->cqn, user_prio, &ring->context);
260 	if (ring->bf_enabled)
261 		ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
262 
263 	err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
264 			       &ring->qp, &ring->qp_state);
265 	return err;
266 }
267 
268 void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
269 				struct mlx4_en_tx_ring *ring)
270 {
271 	struct mlx4_en_dev *mdev = priv->mdev;
272 
273 	mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
274 		       MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
275 }
276 
277 static volatile struct mlx4_wqe_data_seg *
278 mlx4_en_store_inline_lso_data(volatile struct mlx4_wqe_data_seg *dseg,
279     struct mbuf *mb, int len, __be32 owner_bit)
280 {
281 	uint8_t *inl = __DEVOLATILE(uint8_t *, dseg);
282 
283 	/* copy data into place */
284 	m_copydata(mb, 0, len, inl + 4);
285 	dseg += DIV_ROUND_UP(4 + len, DS_SIZE_ALIGNMENT);
286 	return (dseg);
287 }
288 
289 static void
290 mlx4_en_store_inline_lso_header(volatile struct mlx4_wqe_data_seg *dseg,
291     int len, __be32 owner_bit)
292 {
293 }
294 
295 static void
296 mlx4_en_stamp_wqe(struct mlx4_en_priv *priv,
297     struct mlx4_en_tx_ring *ring, u32 index, u8 owner)
298 {
299 	struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
300 	struct mlx4_en_tx_desc *tx_desc = (struct mlx4_en_tx_desc *)
301 	    (ring->buf + (index * TXBB_SIZE));
302 	volatile __be32 *ptr = (__be32 *)tx_desc;
303 	const __be32 stamp = cpu_to_be32(STAMP_VAL |
304 	    ((u32)owner << STAMP_SHIFT));
305 	u32 i;
306 
307 	/* Stamp the freed descriptor */
308 	for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE; i += STAMP_STRIDE) {
309 		*ptr = stamp;
310 		ptr += STAMP_DWORDS;
311 	}
312 }
313 
314 static u32
315 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
316     struct mlx4_en_tx_ring *ring, u32 index)
317 {
318 	struct mlx4_en_tx_info *tx_info;
319 	struct mbuf *mb;
320 
321 	tx_info = &ring->tx_info[index];
322 	mb = tx_info->mb;
323 
324 	if (mb == NULL)
325 		goto done;
326 
327 	bus_dmamap_sync(ring->dma_tag, tx_info->dma_map,
328 	    BUS_DMASYNC_POSTWRITE);
329 	bus_dmamap_unload(ring->dma_tag, tx_info->dma_map);
330 
331         m_freem(mb);
332 done:
333 	return (tx_info->nr_txbb);
334 }
335 
336 int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring)
337 {
338 	struct mlx4_en_priv *priv = netdev_priv(dev);
339 	int cnt = 0;
340 
341 	/* Skip last polled descriptor */
342 	ring->cons += ring->last_nr_txbb;
343 	en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
344 		 ring->cons, ring->prod);
345 
346 	if ((u32) (ring->prod - ring->cons) > ring->size) {
347                 en_warn(priv, "Tx consumer passed producer!\n");
348 		return 0;
349 	}
350 
351 	while (ring->cons != ring->prod) {
352 		ring->last_nr_txbb = mlx4_en_free_tx_desc(priv, ring,
353 		    ring->cons & ring->size_mask);
354 		ring->cons += ring->last_nr_txbb;
355 		cnt++;
356 	}
357 
358 	if (cnt)
359 		en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
360 
361 	return cnt;
362 }
363 
364 static bool
365 mlx4_en_tx_ring_is_full(struct mlx4_en_tx_ring *ring)
366 {
367 	int wqs;
368 	wqs = ring->size - (ring->prod - ring->cons);
369 	return (wqs < (HEADROOM + (2 * MLX4_EN_TX_WQE_MAX_WQEBBS)));
370 }
371 
372 static int mlx4_en_process_tx_cq(struct net_device *dev,
373 				 struct mlx4_en_cq *cq)
374 {
375 	struct mlx4_en_priv *priv = netdev_priv(dev);
376 	struct mlx4_cq *mcq = &cq->mcq;
377 	struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring];
378 	struct mlx4_cqe *cqe;
379 	u16 index;
380 	u16 new_index, ring_index, stamp_index;
381 	u32 txbbs_skipped = 0;
382 	u32 txbbs_stamp = 0;
383 	u32 cons_index = mcq->cons_index;
384 	int size = cq->size;
385 	u32 size_mask = ring->size_mask;
386 	struct mlx4_cqe *buf = cq->buf;
387 	int factor = priv->cqe_factor;
388 
389 	if (!priv->port_up)
390 		return 0;
391 
392 	index = cons_index & size_mask;
393 	cqe = &buf[(index << factor) + factor];
394 	ring_index = ring->cons & size_mask;
395 	stamp_index = ring_index;
396 
397 	/* Process all completed CQEs */
398 	while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
399 			cons_index & size)) {
400 		/*
401 		 * make sure we read the CQE after we read the
402 		 * ownership bit
403 		 */
404 		rmb();
405 
406 		if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
407 			     MLX4_CQE_OPCODE_ERROR)) {
408 			en_err(priv, "CQE completed in error - vendor syndrom: 0x%x syndrom: 0x%x\n",
409 			       ((struct mlx4_err_cqe *)cqe)->
410 				       vendor_err_syndrome,
411 			       ((struct mlx4_err_cqe *)cqe)->syndrome);
412 		}
413 
414 		/* Skip over last polled CQE */
415 		new_index = be16_to_cpu(cqe->wqe_index) & size_mask;
416 
417 		do {
418 			txbbs_skipped += ring->last_nr_txbb;
419 			ring_index = (ring_index + ring->last_nr_txbb) & size_mask;
420 			/* free next descriptor */
421 			ring->last_nr_txbb = mlx4_en_free_tx_desc(
422 			    priv, ring, ring_index);
423 			mlx4_en_stamp_wqe(priv, ring, stamp_index,
424 					  !!((ring->cons + txbbs_stamp) &
425 						ring->size));
426 			stamp_index = ring_index;
427 			txbbs_stamp = txbbs_skipped;
428 		} while (ring_index != new_index);
429 
430 		++cons_index;
431 		index = cons_index & size_mask;
432 		cqe = &buf[(index << factor) + factor];
433 	}
434 
435 
436 	/*
437 	 * To prevent CQ overflow we first update CQ consumer and only then
438 	 * the ring consumer.
439 	 */
440 	mcq->cons_index = cons_index;
441 	mlx4_cq_set_ci(mcq);
442 	wmb();
443 	ring->cons += txbbs_skipped;
444 
445 	/* Wakeup Tx queue if it was stopped and ring is not full */
446 	if (unlikely(ring->blocked) && !mlx4_en_tx_ring_is_full(ring)) {
447 		ring->blocked = 0;
448 		if (atomic_fetchadd_int(&priv->blocked, -1) == 1)
449 			atomic_clear_int(&dev->if_drv_flags ,IFF_DRV_OACTIVE);
450 		ring->wake_queue++;
451 		priv->port_stats.wake_queue++;
452 	}
453 	return (0);
454 }
455 
456 void mlx4_en_tx_irq(struct mlx4_cq *mcq)
457 {
458 	struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
459 	struct mlx4_en_priv *priv = netdev_priv(cq->dev);
460 	struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring];
461 
462 	if (priv->port_up == 0 || !spin_trylock(&ring->comp_lock))
463 		return;
464 	mlx4_en_process_tx_cq(cq->dev, cq);
465 	mod_timer(&cq->timer, jiffies + 1);
466 	spin_unlock(&ring->comp_lock);
467 }
468 
469 void mlx4_en_poll_tx_cq(unsigned long data)
470 {
471 	struct mlx4_en_cq *cq = (struct mlx4_en_cq *) data;
472 	struct mlx4_en_priv *priv = netdev_priv(cq->dev);
473 	struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring];
474 	u32 inflight;
475 
476 	INC_PERF_COUNTER(priv->pstats.tx_poll);
477 
478 	if (priv->port_up == 0)
479 		return;
480 	if (!spin_trylock(&ring->comp_lock)) {
481 		mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
482 		return;
483 	}
484 	mlx4_en_process_tx_cq(cq->dev, cq);
485 	inflight = (u32) (ring->prod - ring->cons - ring->last_nr_txbb);
486 
487 	/* If there are still packets in flight and the timer has not already
488 	 * been scheduled by the Tx routine then schedule it here to guarantee
489 	 * completion processing of these packets */
490 	if (inflight && priv->port_up)
491 		mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
492 
493 	spin_unlock(&ring->comp_lock);
494 }
495 
496 static inline void mlx4_en_xmit_poll(struct mlx4_en_priv *priv, int tx_ind)
497 {
498 	struct mlx4_en_cq *cq = priv->tx_cq[tx_ind];
499 	struct mlx4_en_tx_ring *ring = priv->tx_ring[tx_ind];
500 
501 	if (priv->port_up == 0)
502 		return;
503 
504 	/* If we don't have a pending timer, set one up to catch our recent
505 	   post in case the interface becomes idle */
506 	if (!timer_pending(&cq->timer))
507 		mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
508 
509 	/* Poll the CQ every mlx4_en_TX_MODER_POLL packets */
510 	if ((++ring->poll_cnt & (MLX4_EN_TX_POLL_MODER - 1)) == 0)
511 		if (spin_trylock(&ring->comp_lock)) {
512 			mlx4_en_process_tx_cq(priv->dev, cq);
513 			spin_unlock(&ring->comp_lock);
514 		}
515 }
516 
517 static u16
518 mlx4_en_get_inline_hdr_size(struct mlx4_en_tx_ring *ring, struct mbuf *mb)
519 {
520 	u16 retval;
521 
522 	/* only copy from first fragment, if possible */
523 	retval = MIN(ring->inline_thold, mb->m_len);
524 
525 	/* check for too little data */
526 	if (unlikely(retval < MIN_PKT_LEN))
527 		retval = MIN(ring->inline_thold, mb->m_pkthdr.len);
528 	return (retval);
529 }
530 
531 static int
532 mlx4_en_get_header_size(struct mbuf *mb)
533 {
534 	struct ether_vlan_header *eh;
535         struct tcphdr *th;
536         struct ip *ip;
537         int ip_hlen, tcp_hlen;
538 	struct ip6_hdr *ip6;
539 	uint16_t eth_type;
540 	int eth_hdr_len;
541 
542 	eh = mtod(mb, struct ether_vlan_header *);
543 	if (mb->m_len < ETHER_HDR_LEN)
544 		return (0);
545 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
546 		eth_type = ntohs(eh->evl_proto);
547 		eth_hdr_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
548 	} else {
549 		eth_type = ntohs(eh->evl_encap_proto);
550 		eth_hdr_len = ETHER_HDR_LEN;
551 	}
552 	if (mb->m_len < eth_hdr_len)
553 		return (0);
554 	switch (eth_type) {
555 	case ETHERTYPE_IP:
556 		ip = (struct ip *)(mb->m_data + eth_hdr_len);
557 		if (mb->m_len < eth_hdr_len + sizeof(*ip))
558 			return (0);
559 		if (ip->ip_p != IPPROTO_TCP)
560 			return (0);
561 		ip_hlen = ip->ip_hl << 2;
562 		eth_hdr_len += ip_hlen;
563 		break;
564 	case ETHERTYPE_IPV6:
565 		ip6 = (struct ip6_hdr *)(mb->m_data + eth_hdr_len);
566 		if (mb->m_len < eth_hdr_len + sizeof(*ip6))
567 			return (0);
568 		if (ip6->ip6_nxt != IPPROTO_TCP)
569 			return (0);
570 		eth_hdr_len += sizeof(*ip6);
571 		break;
572 	default:
573 		return (0);
574 	}
575 	if (mb->m_len < eth_hdr_len + sizeof(*th))
576 		return (0);
577 	th = (struct tcphdr *)(mb->m_data + eth_hdr_len);
578 	tcp_hlen = th->th_off << 2;
579 	eth_hdr_len += tcp_hlen;
580 	if (mb->m_len < eth_hdr_len)
581 		return (0);
582 	return (eth_hdr_len);
583 }
584 
585 static volatile struct mlx4_wqe_data_seg *
586 mlx4_en_store_inline_data(volatile struct mlx4_wqe_data_seg *dseg,
587     struct mbuf *mb, int len, __be32 owner_bit)
588 {
589 	uint8_t *inl = __DEVOLATILE(uint8_t *, dseg);
590 	const int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - 4;
591 
592 	if (unlikely(len < MIN_PKT_LEN)) {
593 		m_copydata(mb, 0, len, inl + 4);
594 		memset(inl + 4 + len, 0, MIN_PKT_LEN - len);
595 		dseg += DIV_ROUND_UP(4 + MIN_PKT_LEN, DS_SIZE_ALIGNMENT);
596 	} else if (len <= spc) {
597 		m_copydata(mb, 0, len, inl + 4);
598 		dseg += DIV_ROUND_UP(4 + len, DS_SIZE_ALIGNMENT);
599 	} else {
600 		m_copydata(mb, 0, spc, inl + 4);
601 		m_copydata(mb, spc, len - spc, inl + 8 + spc);
602 		dseg += DIV_ROUND_UP(8 + len, DS_SIZE_ALIGNMENT);
603 	}
604 	return (dseg);
605 }
606 
607 static void
608 mlx4_en_store_inline_header(volatile struct mlx4_wqe_data_seg *dseg,
609     int len, __be32 owner_bit)
610 {
611 	uint8_t *inl = __DEVOLATILE(uint8_t *, dseg);
612 	const int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - 4;
613 
614 	if (unlikely(len < MIN_PKT_LEN)) {
615 		*(volatile uint32_t *)inl =
616 		    SET_BYTE_COUNT((1 << 31) | MIN_PKT_LEN);
617 	} else if (len <= spc) {
618 		*(volatile uint32_t *)inl =
619 		    SET_BYTE_COUNT((1 << 31) | len);
620 	} else {
621 		*(volatile uint32_t *)(inl + 4 + spc) =
622 		    SET_BYTE_COUNT((1 << 31) | (len - spc));
623 		wmb();
624 		*(volatile uint32_t *)inl =
625 		    SET_BYTE_COUNT((1 << 31) | spc);
626 	}
627 }
628 
629 static uint32_t hashrandom;
630 static void hashrandom_init(void *arg)
631 {
632 	/*
633 	 * It is assumed that the random subsystem has been
634 	 * initialized when this function is called:
635 	 */
636 	hashrandom = m_ether_tcpip_hash_init();
637 }
638 SYSINIT(hashrandom_init, SI_SUB_RANDOM, SI_ORDER_ANY, &hashrandom_init, NULL);
639 
640 u16 mlx4_en_select_queue(struct net_device *dev, struct mbuf *mb)
641 {
642 	struct mlx4_en_priv *priv = netdev_priv(dev);
643 	u32 rings_p_up = priv->num_tx_rings_p_up;
644 	u32 up = 0;
645 	u32 queue_index;
646 
647 #if (MLX4_EN_NUM_UP > 1)
648 	/* Obtain VLAN information if present */
649 	if (mb->m_flags & M_VLANTAG) {
650 		u32 vlan_tag = mb->m_pkthdr.ether_vtag;
651 	        up = (vlan_tag >> 13) % MLX4_EN_NUM_UP;
652 	}
653 #endif
654 	queue_index = m_ether_tcpip_hash(MBUF_HASHFLAG_L3 | MBUF_HASHFLAG_L4, mb, hashrandom);
655 
656 	return ((queue_index % rings_p_up) + (up * rings_p_up));
657 }
658 
659 static void mlx4_bf_copy(void __iomem *dst, volatile unsigned long *src, unsigned bytecnt)
660 {
661 	__iowrite64_copy(dst, __DEVOLATILE(void *, src), bytecnt / 8);
662 }
663 
664 static int mlx4_en_xmit(struct mlx4_en_priv *priv, int tx_ind, struct mbuf **mbp)
665 {
666 	enum {
667 		DS_FACT = TXBB_SIZE / DS_SIZE_ALIGNMENT,
668 		CTRL_FLAGS = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
669 		    MLX4_WQE_CTRL_SOLICITED),
670 	};
671 	bus_dma_segment_t segs[MLX4_EN_TX_MAX_MBUF_FRAGS];
672 	volatile struct mlx4_wqe_data_seg *dseg;
673 	volatile struct mlx4_wqe_data_seg *dseg_inline;
674 	volatile struct mlx4_en_tx_desc *tx_desc;
675 	struct mlx4_en_tx_ring *ring = priv->tx_ring[tx_ind];
676 	struct ifnet *ifp = priv->dev;
677 	struct mlx4_en_tx_info *tx_info;
678 	struct mbuf *mb = *mbp;
679 	struct mbuf *m;
680 	__be32 owner_bit;
681 	int nr_segs;
682 	int pad;
683 	int err;
684 	u32 bf_size;
685 	u32 bf_prod;
686 	u32 opcode;
687 	u16 index;
688 	u16 ds_cnt;
689 	u16 ihs;
690 
691 	if (unlikely(!priv->port_up)) {
692 		err = EINVAL;
693 		goto tx_drop;
694 	}
695 
696 	/* check if TX ring is full */
697 	if (unlikely(mlx4_en_tx_ring_is_full(ring))) {
698 		/* every full native Tx ring stops queue */
699 		if (ring->blocked == 0)
700 			atomic_add_int(&priv->blocked, 1);
701 		/* Set HW-queue-is-full flag */
702 		atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
703 		priv->port_stats.queue_stopped++;
704 		ring->blocked = 1;
705 		ring->queue_stopped++;
706 
707 		/* Use interrupts to find out when queue opened */
708 		mlx4_en_arm_cq(priv, priv->tx_cq[tx_ind]);
709 		return (ENOBUFS);
710 	}
711 
712 	/* sanity check we are not wrapping around */
713 	KASSERT(((~ring->prod) & ring->size_mask) >=
714 	    (MLX4_EN_TX_WQE_MAX_WQEBBS - 1), ("Wrapping around TX ring"));
715 
716 	/* Track current inflight packets for performance analysis */
717 	AVG_PERF_COUNTER(priv->pstats.inflight_avg,
718 			 (u32) (ring->prod - ring->cons - 1));
719 
720 	/* Track current mbuf packet header length */
721 	AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, mb->m_pkthdr.len);
722 
723 	/* Grab an index and try to transmit packet */
724 	owner_bit = (ring->prod & ring->size) ?
725 		cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0;
726 	index = ring->prod & ring->size_mask;
727 	tx_desc = (volatile struct mlx4_en_tx_desc *)
728 	    (ring->buf + index * TXBB_SIZE);
729 	tx_info = &ring->tx_info[index];
730 	dseg = &tx_desc->data;
731 
732 	/* send a copy of the frame to the BPF listener, if any */
733 	if (ifp != NULL && ifp->if_bpf != NULL)
734 		ETHER_BPF_MTAP(ifp, mb);
735 
736 	/* get default flags */
737 	tx_desc->ctrl.srcrb_flags = CTRL_FLAGS;
738 
739 	if (mb->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO))
740 		tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM);
741 
742 	if (mb->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP |
743 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO))
744 		tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_TCP_UDP_CSUM);
745 
746 	/* do statistics */
747 	if (likely(tx_desc->ctrl.srcrb_flags != CTRL_FLAGS)) {
748 		priv->port_stats.tx_chksum_offload++;
749 		ring->tx_csum++;
750 	}
751 
752 	/* check for VLAN tag */
753 	if (mb->m_flags & M_VLANTAG) {
754 		tx_desc->ctrl.vlan_tag = cpu_to_be16(mb->m_pkthdr.ether_vtag);
755 		tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN;
756 	} else {
757 		tx_desc->ctrl.vlan_tag = 0;
758 		tx_desc->ctrl.ins_vlan = 0;
759 	}
760 
761 	if (unlikely(mlx4_is_mfunc(priv->mdev->dev) || priv->validate_loopback)) {
762 		/*
763 		 * Copy destination MAC address to WQE. This allows
764 		 * loopback in eSwitch, so that VFs and PF can
765 		 * communicate with each other:
766 		 */
767 		m_copydata(mb, 0, 2, __DEVOLATILE(void *, &tx_desc->ctrl.srcrb_flags16[0]));
768 		m_copydata(mb, 2, 4, __DEVOLATILE(void *, &tx_desc->ctrl.imm));
769 	} else {
770 		/* clear immediate field */
771 		tx_desc->ctrl.imm = 0;
772 	}
773 
774 	/* Handle LSO (TSO) packets */
775 	if (mb->m_pkthdr.csum_flags & CSUM_TSO) {
776 		u32 payload_len;
777 		u32 mss = mb->m_pkthdr.tso_segsz;
778 		u32 num_pkts;
779 
780 		opcode = cpu_to_be32(MLX4_OPCODE_LSO | MLX4_WQE_CTRL_RR) |
781 		    owner_bit;
782 		ihs = mlx4_en_get_header_size(mb);
783 		if (unlikely(ihs > MAX_INLINE)) {
784 			ring->oversized_packets++;
785 			err = EINVAL;
786 			goto tx_drop;
787 		}
788 		tx_desc->lso.mss_hdr_size = cpu_to_be32((mss << 16) | ihs);
789 		payload_len = mb->m_pkthdr.len - ihs;
790 		if (unlikely(payload_len == 0))
791 			num_pkts = 1;
792 		else
793 			num_pkts = DIV_ROUND_UP(payload_len, mss);
794 		ring->bytes += payload_len + (num_pkts * ihs);
795 		ring->packets += num_pkts;
796 		ring->tso_packets++;
797 		/* store pointer to inline header */
798 		dseg_inline = dseg;
799 		/* copy data inline */
800 		dseg = mlx4_en_store_inline_lso_data(dseg,
801 		    mb, ihs, owner_bit);
802 	} else {
803 		opcode = cpu_to_be32(MLX4_OPCODE_SEND) |
804 		    owner_bit;
805 		ihs = mlx4_en_get_inline_hdr_size(ring, mb);
806 		ring->bytes += max_t (unsigned int,
807 		    mb->m_pkthdr.len, ETHER_MIN_LEN - ETHER_CRC_LEN);
808 		ring->packets++;
809 		/* store pointer to inline header */
810 		dseg_inline = dseg;
811 		/* copy data inline */
812 		dseg = mlx4_en_store_inline_data(dseg,
813 		    mb, ihs, owner_bit);
814 	}
815 	m_adj(mb, ihs);
816 
817 	err = bus_dmamap_load_mbuf_sg(ring->dma_tag, tx_info->dma_map,
818 	    mb, segs, &nr_segs, BUS_DMA_NOWAIT);
819 	if (unlikely(err == EFBIG)) {
820 		/* Too many mbuf fragments */
821 		ring->defrag_attempts++;
822 		m = m_defrag(mb, M_NOWAIT);
823 		if (m == NULL) {
824 			ring->oversized_packets++;
825 			goto tx_drop;
826 		}
827 		mb = m;
828 		/* Try again */
829 		err = bus_dmamap_load_mbuf_sg(ring->dma_tag, tx_info->dma_map,
830 		    mb, segs, &nr_segs, BUS_DMA_NOWAIT);
831 	}
832 	/* catch errors */
833 	if (unlikely(err != 0)) {
834 		ring->oversized_packets++;
835 		goto tx_drop;
836 	}
837 	/* If there were no errors and we didn't load anything, don't sync. */
838 	if (nr_segs != 0) {
839 		/* make sure all mbuf data is written to RAM */
840 		bus_dmamap_sync(ring->dma_tag, tx_info->dma_map,
841 		    BUS_DMASYNC_PREWRITE);
842 	} else {
843 		/* All data was inlined, free the mbuf. */
844 		bus_dmamap_unload(ring->dma_tag, tx_info->dma_map);
845 		m_freem(mb);
846 		mb = NULL;
847 	}
848 
849 	/* compute number of DS needed */
850 	ds_cnt = (dseg - ((volatile struct mlx4_wqe_data_seg *)tx_desc)) + nr_segs;
851 
852 	/*
853 	 * Check if the next request can wrap around and fill the end
854 	 * of the current request with zero immediate data:
855 	 */
856 	pad = DIV_ROUND_UP(ds_cnt, DS_FACT);
857 	pad = (~(ring->prod + pad)) & ring->size_mask;
858 
859 	if (unlikely(pad < (MLX4_EN_TX_WQE_MAX_WQEBBS - 1))) {
860 		/*
861 		 * Compute the least number of DS blocks we need to
862 		 * pad in order to achieve a TX ring wraparound:
863 		 */
864 		pad = (DS_FACT * (pad + 1));
865 	} else {
866 		/*
867 		 * The hardware will automatically jump to the next
868 		 * TXBB. No need for padding.
869 		 */
870 		pad = 0;
871 	}
872 
873 	/* compute total number of DS blocks */
874 	ds_cnt += pad;
875 	/*
876 	 * When modifying this code, please ensure that the following
877 	 * computation is always less than or equal to 0x3F:
878 	 *
879 	 * ((MLX4_EN_TX_WQE_MAX_WQEBBS - 1) * DS_FACT) +
880 	 * (MLX4_EN_TX_WQE_MAX_WQEBBS * DS_FACT)
881 	 *
882 	 * Else the "ds_cnt" variable can become too big.
883 	 */
884 	tx_desc->ctrl.fence_size = (ds_cnt & 0x3f);
885 
886 	/* store pointer to mbuf */
887 	tx_info->mb = mb;
888 	tx_info->nr_txbb = DIV_ROUND_UP(ds_cnt, DS_FACT);
889 	bf_size = ds_cnt * DS_SIZE_ALIGNMENT;
890 	bf_prod = ring->prod;
891 
892 	/* compute end of "dseg" array */
893 	dseg += nr_segs + pad;
894 
895 	/* pad using zero immediate dseg */
896 	while (pad--) {
897 		dseg--;
898 		dseg->addr = 0;
899 		dseg->lkey = 0;
900 		wmb();
901 		dseg->byte_count = SET_BYTE_COUNT((1 << 31)|0);
902 	}
903 
904 	/* fill segment list */
905 	while (nr_segs--) {
906 		if (unlikely(segs[nr_segs].ds_len == 0)) {
907 			dseg--;
908 			dseg->addr = 0;
909 			dseg->lkey = 0;
910 			wmb();
911 			dseg->byte_count = SET_BYTE_COUNT((1 << 31)|0);
912 		} else {
913 			dseg--;
914 			dseg->addr = cpu_to_be64((uint64_t)segs[nr_segs].ds_addr);
915 			dseg->lkey = cpu_to_be32(priv->mdev->mr.key);
916 			wmb();
917 			dseg->byte_count = SET_BYTE_COUNT((uint32_t)segs[nr_segs].ds_len);
918 		}
919 	}
920 
921 	wmb();
922 
923 	/* write owner bits in reverse order */
924 	if ((opcode & cpu_to_be32(0x1F)) == cpu_to_be32(MLX4_OPCODE_LSO))
925 		mlx4_en_store_inline_lso_header(dseg_inline, ihs, owner_bit);
926 	else
927 		mlx4_en_store_inline_header(dseg_inline, ihs, owner_bit);
928 
929 	/* update producer counter */
930 	ring->prod += tx_info->nr_txbb;
931 
932 	if (ring->bf_enabled && bf_size <= MAX_BF &&
933 	    (tx_desc->ctrl.ins_vlan != MLX4_WQE_CTRL_INS_VLAN)) {
934 
935 		/* store doorbell number */
936 		*(volatile __be32 *) (&tx_desc->ctrl.vlan_tag) |= cpu_to_be32(ring->doorbell_qpn);
937 
938 		/* or in producer number for this WQE */
939 		opcode |= cpu_to_be32((bf_prod & 0xffff) << 8);
940 
941 		/*
942 		 * Ensure the new descriptor hits memory before
943 		 * setting ownership of this descriptor to HW:
944 		 */
945 		wmb();
946 		tx_desc->ctrl.owner_opcode = opcode;
947 		wmb();
948 		mlx4_bf_copy(((u8 *)ring->bf.reg) + ring->bf.offset,
949 		     (volatile unsigned long *) &tx_desc->ctrl, bf_size);
950 		wmb();
951 		ring->bf.offset ^= ring->bf.buf_size;
952 	} else {
953 		/*
954 		 * Ensure the new descriptor hits memory before
955 		 * setting ownership of this descriptor to HW:
956 		 */
957 		wmb();
958 		tx_desc->ctrl.owner_opcode = opcode;
959 		wmb();
960 		writel(cpu_to_be32(ring->doorbell_qpn),
961 		    ((u8 *)ring->bf.uar->map) + MLX4_SEND_DOORBELL);
962 	}
963 
964 	return (0);
965 tx_drop:
966 	*mbp = NULL;
967 	m_freem(mb);
968 	return (err);
969 }
970 
971 static int
972 mlx4_en_transmit_locked(struct ifnet *dev, int tx_ind, struct mbuf *m)
973 {
974 	struct mlx4_en_priv *priv = netdev_priv(dev);
975 	struct mlx4_en_tx_ring *ring;
976 	struct mbuf *next;
977 	int enqueued, err = 0;
978 
979 	ring = priv->tx_ring[tx_ind];
980 	if ((dev->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
981 	    IFF_DRV_RUNNING || priv->port_up == 0) {
982 		if (m != NULL)
983 			err = drbr_enqueue(dev, ring->br, m);
984 		return (err);
985 	}
986 
987 	enqueued = 0;
988 	if (m != NULL)
989 		/*
990 		 * If we can't insert mbuf into drbr, try to xmit anyway.
991 		 * We keep the error we got so we could return that after xmit.
992 		 */
993 		err = drbr_enqueue(dev, ring->br, m);
994 
995 	/* Process the queue */
996 	while ((next = drbr_peek(dev, ring->br)) != NULL) {
997 		if (mlx4_en_xmit(priv, tx_ind, &next) != 0) {
998 			if (next == NULL) {
999 				drbr_advance(dev, ring->br);
1000 			} else {
1001 				drbr_putback(dev, ring->br, next);
1002 			}
1003 			break;
1004 		}
1005 		drbr_advance(dev, ring->br);
1006 		enqueued++;
1007 		if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
1008 			break;
1009 	}
1010 
1011 	if (enqueued > 0)
1012 		ring->watchdog_time = ticks;
1013 
1014 	return (err);
1015 }
1016 
1017 void
1018 mlx4_en_tx_que(void *context, int pending)
1019 {
1020 	struct mlx4_en_tx_ring *ring;
1021 	struct mlx4_en_priv *priv;
1022 	struct net_device *dev;
1023 	struct mlx4_en_cq *cq;
1024 	int tx_ind;
1025 	cq = context;
1026 	dev = cq->dev;
1027 	priv = dev->if_softc;
1028 	tx_ind = cq->ring;
1029 	ring = priv->tx_ring[tx_ind];
1030 
1031 	if (priv->port_up != 0 &&
1032 	    (dev->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1033 		mlx4_en_xmit_poll(priv, tx_ind);
1034 		spin_lock(&ring->tx_lock);
1035                 if (!drbr_empty(dev, ring->br))
1036 			mlx4_en_transmit_locked(dev, tx_ind, NULL);
1037 		spin_unlock(&ring->tx_lock);
1038 	}
1039 }
1040 
1041 int
1042 mlx4_en_transmit(struct ifnet *dev, struct mbuf *m)
1043 {
1044 	struct mlx4_en_priv *priv = netdev_priv(dev);
1045 	struct mlx4_en_tx_ring *ring;
1046 	struct mlx4_en_cq *cq;
1047 	int i, err = 0;
1048 
1049 	if (priv->port_up == 0) {
1050 		m_freem(m);
1051 		return (ENETDOWN);
1052 	}
1053 
1054 	/* Compute which queue to use */
1055 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
1056 		i = (m->m_pkthdr.flowid % 128) % priv->tx_ring_num;
1057 	}
1058 	else {
1059 		i = mlx4_en_select_queue(dev, m);
1060 	}
1061 
1062 	ring = priv->tx_ring[i];
1063 	if (spin_trylock(&ring->tx_lock)) {
1064 		err = mlx4_en_transmit_locked(dev, i, m);
1065 		spin_unlock(&ring->tx_lock);
1066 		/* Poll CQ here */
1067 		mlx4_en_xmit_poll(priv, i);
1068 	} else {
1069 		err = drbr_enqueue(dev, ring->br, m);
1070 		cq = priv->tx_cq[i];
1071 		taskqueue_enqueue(cq->tq, &cq->cq_task);
1072 	}
1073 
1074 #if __FreeBSD_version >= 1100000
1075 	if (unlikely(err != 0))
1076 		if_inc_counter(dev, IFCOUNTER_IQDROPS, 1);
1077 #endif
1078 	return (err);
1079 }
1080 
1081 /*
1082  * Flush ring buffers.
1083  */
1084 void
1085 mlx4_en_qflush(struct ifnet *dev)
1086 {
1087 	struct mlx4_en_priv *priv = netdev_priv(dev);
1088 	struct mlx4_en_tx_ring *ring;
1089 	struct mbuf *m;
1090 
1091 	if (priv->port_up == 0)
1092 		return;
1093 
1094 	for (int i = 0; i < priv->tx_ring_num; i++) {
1095 		ring = priv->tx_ring[i];
1096 		spin_lock(&ring->tx_lock);
1097 		while ((m = buf_ring_dequeue_sc(ring->br)) != NULL)
1098 			m_freem(m);
1099 		spin_unlock(&ring->tx_lock);
1100 	}
1101 	if_qflush(dev);
1102 }
1103