RDMA/rxe: Move crc32 init code to rxe_icrc.c
[linux-2.6-microblaze.git] / drivers / infiniband / sw / rxe / rxe_icrc.c
index 66b2aad..62bcdfc 100644 (file)
@@ -4,11 +4,50 @@
  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  */
 
+#include <linux/crc32.h>
+
 #include "rxe.h"
 #include "rxe_loc.h"
 
+int rxe_icrc_init(struct rxe_dev *rxe)
+{
+       struct crypto_shash *tfm;
+
+       tfm = crypto_alloc_shash("crc32", 0, 0);
+       if (IS_ERR(tfm)) {
+               pr_warn("failed to init crc32 algorithm err:%ld\n",
+                              PTR_ERR(tfm));
+               return PTR_ERR(tfm);
+       }
+
+       rxe->tfm = tfm;
+
+       return 0;
+}
+
+static u32 rxe_crc32(struct rxe_dev *rxe, u32 crc, void *next, size_t len)
+{
+       u32 icrc;
+       int err;
+
+       SHASH_DESC_ON_STACK(shash, rxe->tfm);
+
+       shash->tfm = rxe->tfm;
+       *(u32 *)shash_desc_ctx(shash) = crc;
+       err = crypto_shash_update(shash, next, len);
+       if (unlikely(err)) {
+               pr_warn_ratelimited("failed crc calculation, err: %d\n", err);
+               return crc32_le(crc, next, len);
+       }
+
+       icrc = *(u32 *)shash_desc_ctx(shash);
+       barrier_data(shash_desc_ctx(shash));
+
+       return icrc;
+}
+
 /* Compute a partial ICRC for all the IB transport headers. */
-u32 rxe_icrc_hdr(struct rxe_pkt_info *pkt, struct sk_buff *skb)
+static u32 rxe_icrc_hdr(struct sk_buff *skb, struct rxe_pkt_info *pkt)
 {
        unsigned int bth_offset = 0;
        struct iphdr *ip4h = NULL;
@@ -67,3 +106,54 @@ u32 rxe_icrc_hdr(struct rxe_pkt_info *pkt, struct sk_buff *skb)
                        rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
        return crc;
 }
+
+/**
+ * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
+ *                   delivered in the packet.
+ * @skb: packet buffer
+ * @pkt: packet info
+ *
+ * Return: 0 if the values match else an error
+ */
+int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
+{
+       __be32 *icrcp;
+       u32 pkt_icrc;
+       u32 icrc;
+
+       icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
+       pkt_icrc = be32_to_cpu(*icrcp);
+
+       icrc = rxe_icrc_hdr(skb, pkt);
+       icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
+                               payload_size(pkt) + bth_pad(pkt));
+       icrc = (__force u32)cpu_to_be32(~icrc);
+
+       if (unlikely(icrc != pkt_icrc)) {
+               if (skb->protocol == htons(ETH_P_IPV6))
+                       pr_warn_ratelimited("bad ICRC from %pI6c\n",
+                                           &ipv6_hdr(skb)->saddr);
+               else if (skb->protocol == htons(ETH_P_IP))
+                       pr_warn_ratelimited("bad ICRC from %pI4\n",
+                                           &ip_hdr(skb)->saddr);
+               else
+                       pr_warn_ratelimited("bad ICRC from unknown\n");
+
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/* rxe_icrc_generate- compute ICRC for a packet. */
+void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
+{
+       __be32 *icrcp;
+       u32 icrc;
+
+       icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
+       icrc = rxe_icrc_hdr(skb, pkt);
+       icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
+                               payload_size(pkt) + bth_pad(pkt));
+       *icrcp = (__force __be32)~icrc;
+}