18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2018 Intel Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef IB_HDRS_H
78c2ecf20Sopenharmony_ci#define IB_HDRS_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/types.h>
108c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
118c2ecf20Sopenharmony_ci#include <rdma/ib_verbs.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define IB_SEQ_NAK	(3 << 29)
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* AETH NAK opcode values */
168c2ecf20Sopenharmony_ci#define IB_RNR_NAK                      0x20
178c2ecf20Sopenharmony_ci#define IB_NAK_PSN_ERROR                0x60
188c2ecf20Sopenharmony_ci#define IB_NAK_INVALID_REQUEST          0x61
198c2ecf20Sopenharmony_ci#define IB_NAK_REMOTE_ACCESS_ERROR      0x62
208c2ecf20Sopenharmony_ci#define IB_NAK_REMOTE_OPERATIONAL_ERROR 0x63
218c2ecf20Sopenharmony_ci#define IB_NAK_INVALID_RD_REQUEST       0x64
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define IB_BTH_REQ_ACK		BIT(31)
248c2ecf20Sopenharmony_ci#define IB_BTH_SOLICITED	BIT(23)
258c2ecf20Sopenharmony_ci#define IB_BTH_MIG_REQ		BIT(22)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define IB_GRH_VERSION		6
288c2ecf20Sopenharmony_ci#define IB_GRH_VERSION_MASK	0xF
298c2ecf20Sopenharmony_ci#define IB_GRH_VERSION_SHIFT	28
308c2ecf20Sopenharmony_ci#define IB_GRH_TCLASS_MASK	0xFF
318c2ecf20Sopenharmony_ci#define IB_GRH_TCLASS_SHIFT	20
328c2ecf20Sopenharmony_ci#define IB_GRH_FLOW_MASK	0xFFFFF
338c2ecf20Sopenharmony_ci#define IB_GRH_FLOW_SHIFT	0
348c2ecf20Sopenharmony_ci#define IB_GRH_NEXT_HDR		0x1B
358c2ecf20Sopenharmony_ci#define IB_FECN_SHIFT 31
368c2ecf20Sopenharmony_ci#define IB_FECN_MASK 1
378c2ecf20Sopenharmony_ci#define IB_FECN_SMASK BIT(IB_FECN_SHIFT)
388c2ecf20Sopenharmony_ci#define IB_BECN_SHIFT 30
398c2ecf20Sopenharmony_ci#define IB_BECN_MASK 1
408c2ecf20Sopenharmony_ci#define IB_BECN_SMASK BIT(IB_BECN_SHIFT)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define IB_AETH_CREDIT_SHIFT	24
438c2ecf20Sopenharmony_ci#define IB_AETH_CREDIT_MASK	0x1F
448c2ecf20Sopenharmony_ci#define IB_AETH_CREDIT_INVAL	0x1F
458c2ecf20Sopenharmony_ci#define IB_AETH_NAK_SHIFT	29
468c2ecf20Sopenharmony_ci#define IB_MSN_MASK		0xFFFFFF
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct ib_reth {
498c2ecf20Sopenharmony_ci	__be64 vaddr;        /* potentially unaligned */
508c2ecf20Sopenharmony_ci	__be32 rkey;
518c2ecf20Sopenharmony_ci	__be32 length;
528c2ecf20Sopenharmony_ci} __packed;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistruct ib_atomic_eth {
558c2ecf20Sopenharmony_ci	__be64 vaddr;        /* potentially unaligned */
568c2ecf20Sopenharmony_ci	__be32 rkey;
578c2ecf20Sopenharmony_ci	__be64 swap_data;    /* potentially unaligned */
588c2ecf20Sopenharmony_ci	__be64 compare_data; /* potentially unaligned */
598c2ecf20Sopenharmony_ci} __packed;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#include <rdma/tid_rdma_defs.h>
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciunion ib_ehdrs {
648c2ecf20Sopenharmony_ci	struct {
658c2ecf20Sopenharmony_ci		__be32 deth[2];
668c2ecf20Sopenharmony_ci		__be32 imm_data;
678c2ecf20Sopenharmony_ci	} ud;
688c2ecf20Sopenharmony_ci	struct {
698c2ecf20Sopenharmony_ci		struct ib_reth reth;
708c2ecf20Sopenharmony_ci		__be32 imm_data;
718c2ecf20Sopenharmony_ci	} rc;
728c2ecf20Sopenharmony_ci	struct {
738c2ecf20Sopenharmony_ci		__be32 aeth;
748c2ecf20Sopenharmony_ci		__be64 atomic_ack_eth; /* potentially unaligned */
758c2ecf20Sopenharmony_ci	} __packed at;
768c2ecf20Sopenharmony_ci	__be32 imm_data;
778c2ecf20Sopenharmony_ci	__be32 aeth;
788c2ecf20Sopenharmony_ci	__be32 ieth;
798c2ecf20Sopenharmony_ci	struct ib_atomic_eth atomic_eth;
808c2ecf20Sopenharmony_ci	/* TID RDMA headers */
818c2ecf20Sopenharmony_ci	union {
828c2ecf20Sopenharmony_ci		struct tid_rdma_read_req r_req;
838c2ecf20Sopenharmony_ci		struct tid_rdma_read_resp r_rsp;
848c2ecf20Sopenharmony_ci		struct tid_rdma_write_req w_req;
858c2ecf20Sopenharmony_ci		struct tid_rdma_write_resp w_rsp;
868c2ecf20Sopenharmony_ci		struct tid_rdma_write_data w_data;
878c2ecf20Sopenharmony_ci		struct tid_rdma_resync resync;
888c2ecf20Sopenharmony_ci		struct tid_rdma_ack ack;
898c2ecf20Sopenharmony_ci	} tid_rdma;
908c2ecf20Sopenharmony_ci}  __packed;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistruct ib_other_headers {
938c2ecf20Sopenharmony_ci	__be32 bth[3];
948c2ecf20Sopenharmony_ci	union ib_ehdrs u;
958c2ecf20Sopenharmony_ci} __packed;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistruct ib_header {
988c2ecf20Sopenharmony_ci	__be16 lrh[4];
998c2ecf20Sopenharmony_ci	union {
1008c2ecf20Sopenharmony_ci		struct {
1018c2ecf20Sopenharmony_ci			struct ib_grh grh;
1028c2ecf20Sopenharmony_ci			struct ib_other_headers oth;
1038c2ecf20Sopenharmony_ci		} l;
1048c2ecf20Sopenharmony_ci		struct ib_other_headers oth;
1058c2ecf20Sopenharmony_ci	} u;
1068c2ecf20Sopenharmony_ci} __packed;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* accessors for unaligned __be64 items */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic inline u64 ib_u64_get(__be64 *p)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	return get_unaligned_be64(p);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic inline void ib_u64_put(u64 val, __be64 *p)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	put_unaligned_be64(val, p);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline u64 get_ib_reth_vaddr(struct ib_reth *reth)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	return ib_u64_get(&reth->vaddr);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic inline void put_ib_reth_vaddr(u64 val, struct ib_reth *reth)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	ib_u64_put(val, &reth->vaddr);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic inline u64 get_ib_ateth_vaddr(struct ib_atomic_eth *ateth)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	return ib_u64_get(&ateth->vaddr);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline void put_ib_ateth_vaddr(u64 val, struct ib_atomic_eth *ateth)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	ib_u64_put(val, &ateth->vaddr);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic inline u64 get_ib_ateth_swap(struct ib_atomic_eth *ateth)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	return ib_u64_get(&ateth->swap_data);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic inline void put_ib_ateth_swap(u64 val, struct ib_atomic_eth *ateth)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	ib_u64_put(val, &ateth->swap_data);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic inline u64 get_ib_ateth_compare(struct ib_atomic_eth *ateth)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	return ib_u64_get(&ateth->compare_data);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic inline void put_ib_ateth_compare(u64 val, struct ib_atomic_eth *ateth)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	ib_u64_put(val, &ateth->compare_data);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/*
1618c2ecf20Sopenharmony_ci * 9B/IB Packet Format
1628c2ecf20Sopenharmony_ci */
1638c2ecf20Sopenharmony_ci#define IB_LNH_MASK		3
1648c2ecf20Sopenharmony_ci#define IB_SC_MASK		0xf
1658c2ecf20Sopenharmony_ci#define IB_SC_SHIFT		12
1668c2ecf20Sopenharmony_ci#define IB_SC5_MASK		0x10
1678c2ecf20Sopenharmony_ci#define IB_SL_MASK		0xf
1688c2ecf20Sopenharmony_ci#define IB_SL_SHIFT		4
1698c2ecf20Sopenharmony_ci#define IB_SL_SHIFT		4
1708c2ecf20Sopenharmony_ci#define IB_LVER_MASK	0xf
1718c2ecf20Sopenharmony_ci#define IB_LVER_SHIFT	8
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic inline u8 ib_get_lnh(struct ib_header *hdr)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	return (be16_to_cpu(hdr->lrh[0]) & IB_LNH_MASK);
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic inline u8 ib_get_sc(struct ib_header *hdr)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	return ((be16_to_cpu(hdr->lrh[0]) >> IB_SC_SHIFT) & IB_SC_MASK);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic inline bool ib_is_sc5(u16 sc5)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	return !!(sc5 & IB_SC5_MASK);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic inline u8 ib_get_sl(struct ib_header *hdr)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	return ((be16_to_cpu(hdr->lrh[0]) >> IB_SL_SHIFT) & IB_SL_MASK);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic inline u16 ib_get_dlid(struct ib_header *hdr)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	return (be16_to_cpu(hdr->lrh[1]));
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic inline u16 ib_get_slid(struct ib_header *hdr)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	return (be16_to_cpu(hdr->lrh[3]));
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic inline u8 ib_get_lver(struct ib_header *hdr)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	return (u8)((be16_to_cpu(hdr->lrh[0]) >> IB_LVER_SHIFT) &
2068c2ecf20Sopenharmony_ci		   IB_LVER_MASK);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic inline u16 ib_get_len(struct ib_header *hdr)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	return (u16)(be16_to_cpu(hdr->lrh[2]));
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline u32 ib_get_qkey(struct ib_other_headers *ohdr)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	return be32_to_cpu(ohdr->u.ud.deth[0]);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic inline u32 ib_get_sqpn(struct ib_other_headers *ohdr)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	return ((be32_to_cpu(ohdr->u.ud.deth[1])) & IB_QPN_MASK);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/*
2258c2ecf20Sopenharmony_ci * BTH
2268c2ecf20Sopenharmony_ci */
2278c2ecf20Sopenharmony_ci#define IB_BTH_OPCODE_MASK	0xff
2288c2ecf20Sopenharmony_ci#define IB_BTH_OPCODE_SHIFT	24
2298c2ecf20Sopenharmony_ci#define IB_BTH_PAD_MASK	3
2308c2ecf20Sopenharmony_ci#define IB_BTH_PKEY_MASK	0xffff
2318c2ecf20Sopenharmony_ci#define IB_BTH_PAD_SHIFT	20
2328c2ecf20Sopenharmony_ci#define IB_BTH_A_MASK		1
2338c2ecf20Sopenharmony_ci#define IB_BTH_A_SHIFT		31
2348c2ecf20Sopenharmony_ci#define IB_BTH_M_MASK		1
2358c2ecf20Sopenharmony_ci#define IB_BTH_M_SHIFT		22
2368c2ecf20Sopenharmony_ci#define IB_BTH_SE_MASK		1
2378c2ecf20Sopenharmony_ci#define IB_BTH_SE_SHIFT	23
2388c2ecf20Sopenharmony_ci#define IB_BTH_TVER_MASK	0xf
2398c2ecf20Sopenharmony_ci#define IB_BTH_TVER_SHIFT	16
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic inline u8 ib_bth_get_pad(struct ib_other_headers *ohdr)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	return ((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_PAD_SHIFT) &
2448c2ecf20Sopenharmony_ci		   IB_BTH_PAD_MASK);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic inline u16 ib_bth_get_pkey(struct ib_other_headers *ohdr)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	return (be32_to_cpu(ohdr->bth[0]) & IB_BTH_PKEY_MASK);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic inline u8 ib_bth_get_opcode(struct ib_other_headers *ohdr)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	return ((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_OPCODE_SHIFT) &
2558c2ecf20Sopenharmony_ci		   IB_BTH_OPCODE_MASK);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic inline u8 ib_bth_get_ackreq(struct ib_other_headers *ohdr)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	return (u8)((be32_to_cpu(ohdr->bth[2]) >> IB_BTH_A_SHIFT) &
2618c2ecf20Sopenharmony_ci		   IB_BTH_A_MASK);
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic inline u8 ib_bth_get_migreq(struct ib_other_headers *ohdr)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_M_SHIFT) &
2678c2ecf20Sopenharmony_ci		    IB_BTH_M_MASK);
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic inline u8 ib_bth_get_se(struct ib_other_headers *ohdr)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_SE_SHIFT) &
2738c2ecf20Sopenharmony_ci		    IB_BTH_SE_MASK);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic inline u32 ib_bth_get_psn(struct ib_other_headers *ohdr)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	return (u32)(be32_to_cpu(ohdr->bth[2]));
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic inline u32 ib_bth_get_qpn(struct ib_other_headers *ohdr)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	return (u32)((be32_to_cpu(ohdr->bth[1])) & IB_QPN_MASK);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic inline bool ib_bth_get_becn(struct ib_other_headers *ohdr)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	return (ohdr->bth[1]) & cpu_to_be32(IB_BECN_SMASK);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic inline bool ib_bth_get_fecn(struct ib_other_headers *ohdr)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	return (ohdr->bth[1]) & cpu_to_be32(IB_FECN_SMASK);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic inline u8 ib_bth_get_tver(struct ib_other_headers *ohdr)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_TVER_SHIFT)  &
2998c2ecf20Sopenharmony_ci		    IB_BTH_TVER_MASK);
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic inline bool ib_bth_is_solicited(struct ib_other_headers *ohdr)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	return ohdr->bth[0] & cpu_to_be32(IB_BTH_SOLICITED);
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic inline bool ib_bth_is_migration(struct ib_other_headers *ohdr)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	return ohdr->bth[0] & cpu_to_be32(IB_BTH_MIG_REQ);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci#endif                          /* IB_HDRS_H */
312