RDMA/rxe: Move crc32 init code to rxe_icrc.c
[linux-2.6-microblaze.git] / drivers / infiniband / sw / rxe / rxe_icrc.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5  */
6
7 #include <linux/crc32.h>
8
9 #include "rxe.h"
10 #include "rxe_loc.h"
11
12 int rxe_icrc_init(struct rxe_dev *rxe)
13 {
14         struct crypto_shash *tfm;
15
16         tfm = crypto_alloc_shash("crc32", 0, 0);
17         if (IS_ERR(tfm)) {
18                 pr_warn("failed to init crc32 algorithm err:%ld\n",
19                                PTR_ERR(tfm));
20                 return PTR_ERR(tfm);
21         }
22
23         rxe->tfm = tfm;
24
25         return 0;
26 }
27
28 static u32 rxe_crc32(struct rxe_dev *rxe, u32 crc, void *next, size_t len)
29 {
30         u32 icrc;
31         int err;
32
33         SHASH_DESC_ON_STACK(shash, rxe->tfm);
34
35         shash->tfm = rxe->tfm;
36         *(u32 *)shash_desc_ctx(shash) = crc;
37         err = crypto_shash_update(shash, next, len);
38         if (unlikely(err)) {
39                 pr_warn_ratelimited("failed crc calculation, err: %d\n", err);
40                 return crc32_le(crc, next, len);
41         }
42
43         icrc = *(u32 *)shash_desc_ctx(shash);
44         barrier_data(shash_desc_ctx(shash));
45
46         return icrc;
47 }
48
49 /* Compute a partial ICRC for all the IB transport headers. */
50 static u32 rxe_icrc_hdr(struct sk_buff *skb, struct rxe_pkt_info *pkt)
51 {
52         unsigned int bth_offset = 0;
53         struct iphdr *ip4h = NULL;
54         struct ipv6hdr *ip6h = NULL;
55         struct udphdr *udph;
56         struct rxe_bth *bth;
57         int crc;
58         int length;
59         int hdr_size = sizeof(struct udphdr) +
60                 (skb->protocol == htons(ETH_P_IP) ?
61                 sizeof(struct iphdr) : sizeof(struct ipv6hdr));
62         /* pseudo header buffer size is calculate using ipv6 header size since
63          * it is bigger than ipv4
64          */
65         u8 pshdr[sizeof(struct udphdr) +
66                 sizeof(struct ipv6hdr) +
67                 RXE_BTH_BYTES];
68
69         /* This seed is the result of computing a CRC with a seed of
70          * 0xfffffff and 8 bytes of 0xff representing a masked LRH.
71          */
72         crc = 0xdebb20e3;
73
74         if (skb->protocol == htons(ETH_P_IP)) { /* IPv4 */
75                 memcpy(pshdr, ip_hdr(skb), hdr_size);
76                 ip4h = (struct iphdr *)pshdr;
77                 udph = (struct udphdr *)(ip4h + 1);
78
79                 ip4h->ttl = 0xff;
80                 ip4h->check = CSUM_MANGLED_0;
81                 ip4h->tos = 0xff;
82         } else {                                /* IPv6 */
83                 memcpy(pshdr, ipv6_hdr(skb), hdr_size);
84                 ip6h = (struct ipv6hdr *)pshdr;
85                 udph = (struct udphdr *)(ip6h + 1);
86
87                 memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
88                 ip6h->priority = 0xf;
89                 ip6h->hop_limit = 0xff;
90         }
91         udph->check = CSUM_MANGLED_0;
92
93         bth_offset += hdr_size;
94
95         memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
96         bth = (struct rxe_bth *)&pshdr[bth_offset];
97
98         /* exclude bth.resv8a */
99         bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
100
101         length = hdr_size + RXE_BTH_BYTES;
102         crc = rxe_crc32(pkt->rxe, crc, pshdr, length);
103
104         /* And finish to compute the CRC on the remainder of the headers. */
105         crc = rxe_crc32(pkt->rxe, crc, pkt->hdr + RXE_BTH_BYTES,
106                         rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
107         return crc;
108 }
109
110 /**
111  * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
112  *                    delivered in the packet.
113  * @skb: packet buffer
114  * @pkt: packet info
115  *
116  * Return: 0 if the values match else an error
117  */
118 int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
119 {
120         __be32 *icrcp;
121         u32 pkt_icrc;
122         u32 icrc;
123
124         icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
125         pkt_icrc = be32_to_cpu(*icrcp);
126
127         icrc = rxe_icrc_hdr(skb, pkt);
128         icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
129                                 payload_size(pkt) + bth_pad(pkt));
130         icrc = (__force u32)cpu_to_be32(~icrc);
131
132         if (unlikely(icrc != pkt_icrc)) {
133                 if (skb->protocol == htons(ETH_P_IPV6))
134                         pr_warn_ratelimited("bad ICRC from %pI6c\n",
135                                             &ipv6_hdr(skb)->saddr);
136                 else if (skb->protocol == htons(ETH_P_IP))
137                         pr_warn_ratelimited("bad ICRC from %pI4\n",
138                                             &ip_hdr(skb)->saddr);
139                 else
140                         pr_warn_ratelimited("bad ICRC from unknown\n");
141
142                 return -EINVAL;
143         }
144
145         return 0;
146 }
147
148 /* rxe_icrc_generate- compute ICRC for a packet. */
149 void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
150 {
151         __be32 *icrcp;
152         u32 icrc;
153
154         icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
155         icrc = rxe_icrc_hdr(skb, pkt);
156         icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
157                                 payload_size(pkt) + bth_pad(pkt));
158         *icrcp = (__force __be32)~icrc;
159 }