2 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <machine/in_cksum.h>
32 mlx5e_alloc_rx_wqe(struct mlx5e_rq *rq,
33 struct mlx5e_rx_wqe *wqe, u16 ix)
35 bus_dma_segment_t segs[MLX5E_MAX_BUSDMA_RX_SEGS];
42 if (rq->mbuf[ix].mbuf != NULL)
45 mb_head = mb = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
47 if (unlikely(mb == NULL))
50 mb->m_len = MLX5E_MAX_RX_BYTES;
51 mb->m_pkthdr.len = MLX5E_MAX_RX_BYTES;
53 for (i = 1; i < rq->nsegs; i++) {
54 if (mb_head->m_pkthdr.len >= rq->wqe_sz)
56 mb = mb->m_next = m_getjcl(M_NOWAIT, MT_DATA, 0,
58 if (unlikely(mb == NULL)) {
62 mb->m_len = MLX5E_MAX_RX_BYTES;
63 mb_head->m_pkthdr.len += MLX5E_MAX_RX_BYTES;
65 /* rewind to first mbuf in chain */
68 /* get IP header aligned */
69 m_adj(mb, MLX5E_NET_IP_ALIGN);
71 err = -bus_dmamap_load_mbuf_sg(rq->dma_tag, rq->mbuf[ix].dma_map,
72 mb, segs, &nsegs, BUS_DMA_NOWAIT);
75 if (unlikely(nsegs == 0)) {
76 bus_dmamap_unload(rq->dma_tag, rq->mbuf[ix].dma_map);
80 wqe->data[0].addr = cpu_to_be64(segs[0].ds_addr);
81 wqe->data[0].byte_count = cpu_to_be32(segs[0].ds_len |
82 MLX5_HW_START_PADDING);
83 for (i = 1; i != nsegs; i++) {
84 wqe->data[i].addr = cpu_to_be64(segs[i].ds_addr);
85 wqe->data[i].byte_count = cpu_to_be32(segs[i].ds_len);
87 for (; i < rq->nsegs; i++) {
88 wqe->data[i].addr = 0;
89 wqe->data[i].byte_count = 0;
92 rq->mbuf[ix].mbuf = mb;
93 rq->mbuf[ix].data = mb->m_data;
95 bus_dmamap_sync(rq->dma_tag, rq->mbuf[ix].dma_map,
105 mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
107 if (unlikely(rq->enabled == 0))
110 while (!mlx5_wq_ll_is_full(&rq->wq)) {
111 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, rq->wq.head);
113 if (unlikely(mlx5e_alloc_rx_wqe(rq, wqe, rq->wq.head))) {
114 callout_reset_curcpu(&rq->watchdog, 1, (void *)&mlx5e_post_rx_wqes, rq);
117 mlx5_wq_ll_push(&rq->wq, be16_to_cpu(wqe->next.next_wqe_index));
120 /* ensure wqes are visible to device before updating doorbell record */
121 atomic_thread_fence_rel();
123 mlx5_wq_ll_update_db_record(&rq->wq);
127 mlx5e_lro_update_hdr(struct mbuf *mb, struct mlx5_cqe64 *cqe)
129 /* TODO: consider vlans, ip options, ... */
130 struct ether_header *eh;
133 struct ip6_hdr *ip6 = NULL;
134 struct ip *ip4 = NULL;
140 eh = mtod(mb, struct ether_header *);
141 eh_type = ntohs(eh->ether_type);
143 l4_hdr_type = get_cqe_l4_hdr_type(cqe);
144 tcp_ack = ((CQE_L4_HDR_TYPE_TCP_ACK_NO_DATA == l4_hdr_type) ||
145 (CQE_L4_HDR_TYPE_TCP_ACK_AND_DATA == l4_hdr_type));
147 /* TODO: consider vlan */
148 tot_len = be32_to_cpu(cqe->byte_cnt) - ETHER_HDR_LEN;
152 ip4 = (struct ip *)(eh + 1);
153 th = (struct tcphdr *)(ip4 + 1);
156 ip6 = (struct ip6_hdr *)(eh + 1);
157 th = (struct tcphdr *)(ip6 + 1);
163 ts_ptr = (uint32_t *)(th + 1);
165 if (get_cqe_lro_tcppsh(cqe))
166 th->th_flags |= TH_PUSH;
169 th->th_flags |= TH_ACK;
170 th->th_ack = cqe->lro_ack_seq_num;
171 th->th_win = cqe->lro_tcp_win;
174 * FreeBSD handles only 32bit aligned timestamp right after
176 * +--------+--------+--------+--------+
177 * | NOP | NOP | TSopt | 10 |
178 * +--------+--------+--------+--------+
179 * | TSval timestamp |
180 * +--------+--------+--------+--------+
181 * | TSecr timestamp |
182 * +--------+--------+--------+--------+
184 if (get_cqe_lro_timestamp_valid(cqe) &&
185 (__predict_true(*ts_ptr) == ntohl(TCPOPT_NOP << 24 |
186 TCPOPT_NOP << 16 | TCPOPT_TIMESTAMP << 8 |
187 TCPOLEN_TIMESTAMP))) {
189 * cqe->timestamp is 64bit long.
190 * [0-31] - timestamp.
191 * [32-64] - timestamp echo replay.
193 ts_ptr[1] = *(uint32_t *)&cqe->timestamp;
194 ts_ptr[2] = *((uint32_t *)&cqe->timestamp + 1);
198 ip4->ip_ttl = cqe->lro_min_ttl;
199 ip4->ip_len = cpu_to_be16(tot_len);
201 ip4->ip_sum = in_cksum(mb, ip4->ip_hl << 2);
203 ip6->ip6_hlim = cqe->lro_min_ttl;
204 ip6->ip6_plen = cpu_to_be16(tot_len -
205 sizeof(struct ip6_hdr));
207 /* TODO: handle tcp checksum */
211 mlx5e_build_rx_mbuf(struct mlx5_cqe64 *cqe,
212 struct mlx5e_rq *rq, struct mbuf *mb,
215 struct ifnet *ifp = rq->ifp;
216 struct mbuf *mb_head;
217 int lro_num_seg; /* HW LRO session aggregated packets counter */
219 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
220 if (lro_num_seg > 1) {
221 mlx5e_lro_update_hdr(mb, cqe);
222 rq->stats.lro_packets++;
223 rq->stats.lro_bytes += cqe_bcnt;
226 mb->m_pkthdr.len = cqe_bcnt;
227 for (mb_head = mb; mb != NULL; mb = mb->m_next) {
228 if (mb->m_len > cqe_bcnt)
229 mb->m_len = cqe_bcnt;
230 cqe_bcnt -= mb->m_len;
231 if (likely(cqe_bcnt == 0)) {
232 if (likely(mb->m_next != NULL)) {
233 /* trim off empty mbufs */
240 /* rewind to first mbuf in chain */
243 /* check if a Toeplitz hash was computed */
244 if (cqe->rss_hash_type != 0) {
245 mb->m_pkthdr.flowid = be32_to_cpu(cqe->rss_hash_result);
247 /* decode the RSS hash type */
248 switch (cqe->rss_hash_type &
249 (CQE_RSS_DST_HTYPE_L4 | CQE_RSS_DST_HTYPE_IP)) {
251 case (CQE_RSS_DST_HTYPE_TCP | CQE_RSS_DST_HTYPE_IPV4):
252 M_HASHTYPE_SET(mb, M_HASHTYPE_RSS_TCP_IPV4);
254 case (CQE_RSS_DST_HTYPE_UDP | CQE_RSS_DST_HTYPE_IPV4):
255 M_HASHTYPE_SET(mb, M_HASHTYPE_RSS_UDP_IPV4);
257 case CQE_RSS_DST_HTYPE_IPV4:
258 M_HASHTYPE_SET(mb, M_HASHTYPE_RSS_IPV4);
261 case (CQE_RSS_DST_HTYPE_TCP | CQE_RSS_DST_HTYPE_IPV6):
262 M_HASHTYPE_SET(mb, M_HASHTYPE_RSS_TCP_IPV6);
264 case (CQE_RSS_DST_HTYPE_UDP | CQE_RSS_DST_HTYPE_IPV6):
265 M_HASHTYPE_SET(mb, M_HASHTYPE_RSS_UDP_IPV6);
267 case CQE_RSS_DST_HTYPE_IPV6:
268 M_HASHTYPE_SET(mb, M_HASHTYPE_RSS_IPV6);
271 M_HASHTYPE_SET(mb, M_HASHTYPE_OPAQUE_HASH);
275 M_HASHTYPE_SET(mb, M_HASHTYPE_OPAQUE_HASH);
278 mb->m_pkthdr.flowid = rq->ix;
279 M_HASHTYPE_SET(mb, M_HASHTYPE_OPAQUE);
281 mb->m_pkthdr.rcvif = ifp;
283 if (likely(ifp->if_capenable & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) &&
284 ((cqe->hds_ip_ext & (CQE_L2_OK | CQE_L3_OK | CQE_L4_OK)) ==
285 (CQE_L2_OK | CQE_L3_OK | CQE_L4_OK))) {
286 mb->m_pkthdr.csum_flags =
287 CSUM_IP_CHECKED | CSUM_IP_VALID |
288 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
289 mb->m_pkthdr.csum_data = htons(0xffff);
291 rq->stats.csum_none++;
294 if (cqe_has_vlan(cqe)) {
295 mb->m_pkthdr.ether_vtag = be16_to_cpu(cqe->vlan_info);
296 mb->m_flags |= M_VLANTAG;
301 mlx5e_read_cqe_slot(struct mlx5e_cq *cq, u32 cc, void *data)
303 memcpy(data, mlx5_cqwq_get_wqe(&cq->wq, (cc & cq->wq.sz_m1)),
304 sizeof(struct mlx5_cqe64));
308 mlx5e_write_cqe_slot(struct mlx5e_cq *cq, u32 cc, void *data)
310 memcpy(mlx5_cqwq_get_wqe(&cq->wq, cc & cq->wq.sz_m1),
311 data, sizeof(struct mlx5_cqe64));
315 mlx5e_decompress_cqe(struct mlx5e_cq *cq, struct mlx5_cqe64 *title,
316 struct mlx5_mini_cqe8 *mini,
317 u16 wqe_counter, int i)
320 * NOTE: The fields which are not set here are copied from the
321 * initial and common title. See memcpy() in
322 * mlx5e_write_cqe_slot().
324 title->byte_cnt = mini->byte_cnt;
325 title->wqe_counter = cpu_to_be16((wqe_counter + i) & cq->wq.sz_m1);
326 title->rss_hash_result = mini->rx_hash_result;
328 * Since we use MLX5_CQE_FORMAT_HASH when creating the RX CQ,
329 * the value of the checksum should be ignored.
331 title->check_sum = 0;
332 title->op_own = (title->op_own & 0xf0) |
333 (((cq->wq.cc + i) >> cq->wq.log_sz) & 1);
336 #define MLX5E_MINI_ARRAY_SZ 8
337 /* Make sure structs are not packet differently */
338 CTASSERT(sizeof(struct mlx5_cqe64) ==
339 sizeof(struct mlx5_mini_cqe8) * MLX5E_MINI_ARRAY_SZ);
341 mlx5e_decompress_cqes(struct mlx5e_cq *cq)
343 struct mlx5_mini_cqe8 mini_array[MLX5E_MINI_ARRAY_SZ];
344 struct mlx5_cqe64 title;
347 u16 title_wqe_counter;
349 mlx5e_read_cqe_slot(cq, cq->wq.cc, &title);
350 title_wqe_counter = be16_to_cpu(title.wqe_counter);
351 cqe_count = be32_to_cpu(title.byte_cnt);
353 /* Make sure we won't overflow */
354 KASSERT(cqe_count <= cq->wq.sz_m1,
355 ("%s: cqe_count %u > cq->wq.sz_m1 %u", __func__,
356 cqe_count, cq->wq.sz_m1));
358 mlx5e_read_cqe_slot(cq, cq->wq.cc + 1, mini_array);
360 mlx5e_decompress_cqe(cq, &title,
361 &mini_array[i % MLX5E_MINI_ARRAY_SZ],
362 title_wqe_counter, i);
363 mlx5e_write_cqe_slot(cq, cq->wq.cc + i, &title);
368 if (i % MLX5E_MINI_ARRAY_SZ == 0)
369 mlx5e_read_cqe_slot(cq, cq->wq.cc + i, mini_array);
374 mlx5e_poll_rx_cq(struct mlx5e_rq *rq, int budget)
378 for (i = 0; i < budget; i++) {
379 struct mlx5e_rx_wqe *wqe;
380 struct mlx5_cqe64 *cqe;
382 __be16 wqe_counter_be;
386 cqe = mlx5e_get_cqe(&rq->cq);
390 if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED)
391 mlx5e_decompress_cqes(&rq->cq);
393 mlx5_cqwq_pop(&rq->cq.wq);
395 wqe_counter_be = cqe->wqe_counter;
396 wqe_counter = be16_to_cpu(wqe_counter_be);
397 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
398 byte_cnt = be32_to_cpu(cqe->byte_cnt);
400 bus_dmamap_sync(rq->dma_tag,
401 rq->mbuf[wqe_counter].dma_map,
402 BUS_DMASYNC_POSTREAD);
404 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
408 if ((MHLEN - MLX5E_NET_IP_ALIGN) >= byte_cnt &&
409 (mb = m_gethdr(M_NOWAIT, MT_DATA)) != NULL) {
410 /* set maximum mbuf length */
411 mb->m_len = MHLEN - MLX5E_NET_IP_ALIGN;
412 /* get IP header aligned */
413 mb->m_data += MLX5E_NET_IP_ALIGN;
415 bcopy(rq->mbuf[wqe_counter].data, mtod(mb, caddr_t),
418 mb = rq->mbuf[wqe_counter].mbuf;
419 rq->mbuf[wqe_counter].mbuf = NULL; /* safety clear */
421 bus_dmamap_unload(rq->dma_tag,
422 rq->mbuf[wqe_counter].dma_map);
425 mlx5e_build_rx_mbuf(cqe, rq, mb, byte_cnt);
426 rq->stats.bytes += byte_cnt;
429 #if !defined(HAVE_TCP_LRO_RX)
430 tcp_lro_queue_mbuf(&rq->lro, mb);
432 if (mb->m_pkthdr.csum_flags == 0 ||
433 (rq->ifp->if_capenable & IFCAP_LRO) == 0 ||
434 rq->lro.lro_cnt == 0 ||
435 tcp_lro_rx(&rq->lro, mb, 0) != 0) {
436 rq->ifp->if_input(rq->ifp, mb);
440 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
441 &wqe->next.next_wqe_index);
444 mlx5_cqwq_update_db_record(&rq->cq.wq);
446 /* ensure cq space is freed before enabling more cqes */
447 atomic_thread_fence_rel();
452 mlx5e_rx_cq_comp(struct mlx5_core_cq *mcq)
454 struct mlx5e_rq *rq = container_of(mcq, struct mlx5e_rq, cq.mcq);
457 #ifdef HAVE_PER_CQ_EVENT_PACKET
459 #error "MHLEN is too small"
461 struct mbuf *mb = m_gethdr(M_NOWAIT, MT_DATA);
464 /* this code is used for debugging purpose only */
465 mb->m_pkthdr.len = mb->m_len = 15;
466 memset(mb->m_data, 255, 14);
467 mb->m_data[14] = rq->ix;
468 mb->m_pkthdr.rcvif = rq->ifp;
469 rq->ifp->if_input(rq->ifp, mb);
476 * Polling the entire CQ without posting new WQEs results in
477 * lack of receive WQEs during heavy traffic scenarios.
480 if (mlx5e_poll_rx_cq(rq, MLX5E_RX_BUDGET_MAX) !=
483 i += MLX5E_RX_BUDGET_MAX;
484 if (i >= MLX5E_BUDGET_MAX)
486 mlx5e_post_rx_wqes(rq);
488 mlx5e_post_rx_wqes(rq);
489 /* check for dynamic interrupt moderation callback */
490 if (rq->dim.mode != NET_DIM_CQ_PERIOD_MODE_DISABLED)
491 net_dim(&rq->dim, rq->stats.packets, rq->stats.bytes);
492 mlx5e_cq_arm(&rq->cq, MLX5_GET_DOORBELL_LOCK(&rq->channel->priv->doorbell_lock));
493 tcp_lro_flush_all(&rq->lro);
494 mtx_unlock(&rq->mtx);