124f52149SLeon Romanovsky /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2a977049dSHal Rosenstock /*
3ef700446SSean Hefty  * Copyright (c) 2004, 2011 Intel Corporation.  All rights reserved.
4a977049dSHal Rosenstock  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
5a977049dSHal Rosenstock  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
624f52149SLeon Romanovsky  * Copyright (c) 2019, Mellanox Technologies inc.  All rights reserved.
7a977049dSHal Rosenstock  */
8a9160511SLeon Romanovsky #ifndef CM_MSGS_H
9a977049dSHal Rosenstock #define CM_MSGS_H
10a977049dSHal Rosenstock 
11d05d4ac4SLeon Romanovsky #include <rdma/ibta_vol1_c12.h>
12a4d61e84SRoland Dreier #include <rdma/ib_mad.h>
135d861be8SSean Hefty #include <rdma/ib_cm.h>
14a977049dSHal Rosenstock 
15a977049dSHal Rosenstock /*
16a977049dSHal Rosenstock  * Parameters to routines below should be in network-byte order, and values
17a977049dSHal Rosenstock  * are returned in network-byte order.
18a977049dSHal Rosenstock  */
19a977049dSHal Rosenstock 
20a977049dSHal Rosenstock #define IB_CM_CLASS_VERSION	2 /* IB specification 1.2 */
21a977049dSHal Rosenstock 
cm_req_get_qp_type(struct cm_req_msg * req_msg)22a977049dSHal Rosenstock static inline enum ib_qp_type cm_req_get_qp_type(struct cm_req_msg *req_msg)
23a977049dSHal Rosenstock {
24b6bbee68SJason Gunthorpe 	u8 transport_type = IBA_GET(CM_REQ_TRANSPORT_SERVICE_TYPE, req_msg);
25a977049dSHal Rosenstock 	switch (transport_type) {
26a977049dSHal Rosenstock 	case 0: return IB_QPT_RC;
27a977049dSHal Rosenstock 	case 1: return IB_QPT_UC;
28d26a360bSSean Hefty 	case 3:
29b6bbee68SJason Gunthorpe 		switch (IBA_GET(CM_REQ_EXTENDED_TRANSPORT_TYPE, req_msg)) {
30d26a360bSSean Hefty 		case 1: return IB_QPT_XRC_TGT;
31d26a360bSSean Hefty 		default: return 0;
32d26a360bSSean Hefty 		}
33a977049dSHal Rosenstock 	default: return 0;
34a977049dSHal Rosenstock 	}
35a977049dSHal Rosenstock }
36a977049dSHal Rosenstock 
cm_req_set_qp_type(struct cm_req_msg * req_msg,enum ib_qp_type qp_type)37a977049dSHal Rosenstock static inline void cm_req_set_qp_type(struct cm_req_msg *req_msg,
38a977049dSHal Rosenstock 				      enum ib_qp_type qp_type)
39a977049dSHal Rosenstock {
40a977049dSHal Rosenstock 	switch (qp_type) {
41a977049dSHal Rosenstock 	case IB_QPT_UC:
42b6bbee68SJason Gunthorpe 		IBA_SET(CM_REQ_TRANSPORT_SERVICE_TYPE, req_msg, 1);
433910f44dSRoland Dreier 		break;
44d26a360bSSean Hefty 	case IB_QPT_XRC_INI:
45b6bbee68SJason Gunthorpe 		IBA_SET(CM_REQ_TRANSPORT_SERVICE_TYPE, req_msg, 3);
46b6bbee68SJason Gunthorpe 		IBA_SET(CM_REQ_EXTENDED_TRANSPORT_TYPE, req_msg, 1);
47d26a360bSSean Hefty 		break;
48a977049dSHal Rosenstock 	default:
49b6bbee68SJason Gunthorpe 		IBA_SET(CM_REQ_TRANSPORT_SERVICE_TYPE, req_msg, 0);
50a977049dSHal Rosenstock 	}
51a977049dSHal Rosenstock }
52a977049dSHal Rosenstock 
53a977049dSHal Rosenstock /* Message REJected or MRAed */
54a977049dSHal Rosenstock enum cm_msg_response {
55a977049dSHal Rosenstock 	CM_MSG_RESPONSE_REQ = 0x0,
56a977049dSHal Rosenstock 	CM_MSG_RESPONSE_REP = 0x1,
57a977049dSHal Rosenstock 	CM_MSG_RESPONSE_OTHER = 0x2
58a977049dSHal Rosenstock };
59a977049dSHal Rosenstock 
cm_rep_get_qpn(struct cm_rep_msg * rep_msg,enum ib_qp_type qp_type)60ef700446SSean Hefty static inline __be32 cm_rep_get_qpn(struct cm_rep_msg *rep_msg, enum ib_qp_type qp_type)
61ef700446SSean Hefty {
62ef700446SSean Hefty 	return (qp_type == IB_QPT_XRC_INI) ?
6301adb7f4SJason Gunthorpe 		       cpu_to_be32(IBA_GET(CM_REP_LOCAL_EE_CONTEXT_NUMBER,
6401adb7f4SJason Gunthorpe 					   rep_msg)) :
6501adb7f4SJason Gunthorpe 		       cpu_to_be32(IBA_GET(CM_REP_LOCAL_QPN, rep_msg));
66a977049dSHal Rosenstock }
67a977049dSHal Rosenstock 
68a977049dSHal Rosenstock #endif /* CM_MSGS_H */
69