15190f052SMike Marciniszyn /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ 25190f052SMike Marciniszyn /* 35190f052SMike Marciniszyn * Copyright(c) 2018 Intel Corporation. 45190f052SMike Marciniszyn * 55190f052SMike Marciniszyn */ 65190f052SMike Marciniszyn #ifndef HFI1_TID_RDMA_H 75190f052SMike Marciniszyn #define HFI1_TID_RDMA_H 85190f052SMike Marciniszyn 9838b6fd2SKaike Wan #include <linux/circ_buf.h> 10838b6fd2SKaike Wan #include "common.h" 11838b6fd2SKaike Wan 12838b6fd2SKaike Wan /* Add a convenience helper */ 13838b6fd2SKaike Wan #define CIRC_ADD(val, add, size) (((val) + (add)) & ((size) - 1)) 14838b6fd2SKaike Wan #define CIRC_NEXT(val, size) CIRC_ADD(val, 1, size) 15838b6fd2SKaike Wan #define CIRC_PREV(val, size) CIRC_ADD(val, -1, size) 16838b6fd2SKaike Wan 17f1ab4efaSKaike Wan #define TID_RDMA_MIN_SEGMENT_SIZE BIT(18) /* 256 KiB (for now) */ 18d22a207dSKaike Wan #define TID_RDMA_MAX_SEGMENT_SIZE BIT(18) /* 256 KiB (for now) */ 19838b6fd2SKaike Wan #define TID_RDMA_MAX_PAGES (BIT(18) >> PAGE_SHIFT) 20d22a207dSKaike Wan 21a0b34f75SKaike Wan /* 22a0b34f75SKaike Wan * Bit definitions for priv->s_flags. 23a0b34f75SKaike Wan * These bit flags overload the bit flags defined for the QP's s_flags. 24a0b34f75SKaike Wan * Due to the fact that these bit fields are used only for the QP priv 25a0b34f75SKaike Wan * s_flags, there are no collisions. 26a0b34f75SKaike Wan * 27a0b34f75SKaike Wan * HFI1_S_TID_WAIT_INTERLCK - QP is waiting for requester interlock 28a0b34f75SKaike Wan */ 293c759e00SKaike Wan #define HFI1_R_TID_RSC_TIMER BIT(2) 3007b92370SKaike Wan /* BIT(4) reserved for RVT_S_ACK_PENDING. */ 31a0b34f75SKaike Wan #define HFI1_S_TID_WAIT_INTERLCK BIT(5) 3207b92370SKaike Wan #define HFI1_R_TID_SW_PSN BIT(19) 33a0b34f75SKaike Wan 34f5a4a95fSKaike Wan /* 35f5a4a95fSKaike Wan * Unlike regular IB RDMA VERBS, which do not require an entry 36f5a4a95fSKaike Wan * in the s_ack_queue, TID RDMA WRITE requests do because they 37f5a4a95fSKaike Wan * generate responses. 38f5a4a95fSKaike Wan * Therefore, the s_ack_queue needs to be extended by a certain 39f5a4a95fSKaike Wan * amount. The key point is that the queue needs to be extended 40f5a4a95fSKaike Wan * without letting the "user" know so they user doesn't end up 41f5a4a95fSKaike Wan * using these extra entries. 42f5a4a95fSKaike Wan */ 43f5a4a95fSKaike Wan #define HFI1_TID_RDMA_WRITE_CNT 8 44f5a4a95fSKaike Wan 45d22a207dSKaike Wan struct tid_rdma_params { 46d22a207dSKaike Wan struct rcu_head rcu_head; 47d22a207dSKaike Wan u32 qp; 48d22a207dSKaike Wan u32 max_len; 49d22a207dSKaike Wan u16 jkey; 50d22a207dSKaike Wan u8 max_read; 51d22a207dSKaike Wan u8 max_write; 52d22a207dSKaike Wan u8 timeout; 53d22a207dSKaike Wan u8 urg; 54d22a207dSKaike Wan u8 version; 55d22a207dSKaike Wan }; 56d22a207dSKaike Wan 57d22a207dSKaike Wan struct tid_rdma_qp_params { 5837356e78SKaike Wan struct work_struct trigger_work; 59d22a207dSKaike Wan struct tid_rdma_params local; 60d22a207dSKaike Wan struct tid_rdma_params __rcu *remote; 61d22a207dSKaike Wan }; 62d22a207dSKaike Wan 6337356e78SKaike Wan /* Track state for each hardware flow */ 6437356e78SKaike Wan struct tid_flow_state { 6537356e78SKaike Wan u32 generation; 6637356e78SKaike Wan u32 psn; 6737356e78SKaike Wan u32 r_next_psn; /* next PSN to be received (in TID space) */ 6837356e78SKaike Wan u8 index; 6937356e78SKaike Wan u8 last_index; 7037356e78SKaike Wan u8 flags; 7137356e78SKaike Wan }; 7237356e78SKaike Wan 73742a3826SKaike Wan enum tid_rdma_req_state { 74742a3826SKaike Wan TID_REQUEST_INACTIVE = 0, 75742a3826SKaike Wan TID_REQUEST_INIT, 76742a3826SKaike Wan TID_REQUEST_INIT_RESEND, 77742a3826SKaike Wan TID_REQUEST_ACTIVE, 78742a3826SKaike Wan TID_REQUEST_RESEND, 79742a3826SKaike Wan TID_REQUEST_RESEND_ACTIVE, 80742a3826SKaike Wan TID_REQUEST_QUEUED, 81742a3826SKaike Wan TID_REQUEST_SYNC, 82742a3826SKaike Wan TID_REQUEST_RNR_NAK, 83742a3826SKaike Wan TID_REQUEST_COMPLETE, 84742a3826SKaike Wan }; 85742a3826SKaike Wan 86838b6fd2SKaike Wan struct tid_rdma_request { 87838b6fd2SKaike Wan struct rvt_qp *qp; 88838b6fd2SKaike Wan struct hfi1_ctxtdata *rcd; 89838b6fd2SKaike Wan union { 90838b6fd2SKaike Wan struct rvt_swqe *swqe; 91838b6fd2SKaike Wan struct rvt_ack_entry *ack; 92838b6fd2SKaike Wan } e; 93838b6fd2SKaike Wan 94838b6fd2SKaike Wan struct tid_rdma_flow *flows; /* array of tid flows */ 9507b92370SKaike Wan struct rvt_sge_state ss; /* SGE state for TID RDMA requests */ 96838b6fd2SKaike Wan u16 n_flows; /* size of the flow buffer window */ 97838b6fd2SKaike Wan u16 setup_head; /* flow index we are setting up */ 98838b6fd2SKaike Wan u16 clear_tail; /* flow index we are clearing */ 99838b6fd2SKaike Wan u16 flow_idx; /* flow index most recently set up */ 10007b92370SKaike Wan u16 acked_tail; 101838b6fd2SKaike Wan 102838b6fd2SKaike Wan u32 seg_len; 103d0d564a1SKaike Wan u32 total_len; 104d0d564a1SKaike Wan u32 r_flow_psn; /* IB PSN of next segment start */ 10572a0ea99SKaike Wan u32 r_last_acked; /* IB PSN of last ACK'ed packet */ 106742a3826SKaike Wan u32 s_next_psn; /* IB PSN of next segment start for read */ 107838b6fd2SKaike Wan 108d0d564a1SKaike Wan u32 total_segs; /* segments required to complete a request */ 109742a3826SKaike Wan u32 cur_seg; /* index of current segment */ 110d0d564a1SKaike Wan u32 comp_seg; /* index of last completed segment */ 111d0d564a1SKaike Wan u32 ack_seg; /* index of last ack'ed segment */ 11207b92370SKaike Wan u32 alloc_seg; /* index of next segment to be allocated */ 113838b6fd2SKaike Wan u32 isge; /* index of "current" sge */ 114742a3826SKaike Wan u32 ack_pending; /* num acks pending for this request */ 115742a3826SKaike Wan 116742a3826SKaike Wan enum tid_rdma_req_state state; 117838b6fd2SKaike Wan }; 118838b6fd2SKaike Wan 119838b6fd2SKaike Wan /* 120838b6fd2SKaike Wan * When header suppression is used, PSNs associated with a "flow" are 121838b6fd2SKaike Wan * relevant (and not the PSNs maintained by verbs). Track per-flow 122838b6fd2SKaike Wan * PSNs here for a TID RDMA segment. 123838b6fd2SKaike Wan * 124838b6fd2SKaike Wan */ 125838b6fd2SKaike Wan struct flow_state { 126838b6fd2SKaike Wan u32 flags; 127838b6fd2SKaike Wan u32 resp_ib_psn; /* The IB PSN of the response for this flow */ 128838b6fd2SKaike Wan u32 generation; /* generation of flow */ 129838b6fd2SKaike Wan u32 spsn; /* starting PSN in TID space */ 130838b6fd2SKaike Wan u32 lpsn; /* last PSN in TID space */ 131838b6fd2SKaike Wan u32 r_next_psn; /* next PSN to be received (in TID space) */ 132742a3826SKaike Wan 133742a3826SKaike Wan /* For tid rdma read */ 134742a3826SKaike Wan u32 ib_spsn; /* starting PSN in Verbs space */ 135742a3826SKaike Wan u32 ib_lpsn; /* last PSn in Verbs space */ 136838b6fd2SKaike Wan }; 137838b6fd2SKaike Wan 138838b6fd2SKaike Wan struct tid_rdma_pageset { 139838b6fd2SKaike Wan dma_addr_t addr : 48; /* Only needed for the first page */ 140838b6fd2SKaike Wan u8 idx: 8; 141838b6fd2SKaike Wan u8 count : 7; 142838b6fd2SKaike Wan u8 mapped: 1; 143838b6fd2SKaike Wan }; 144838b6fd2SKaike Wan 145838b6fd2SKaike Wan /** 146838b6fd2SKaike Wan * kern_tid_node - used for managing TID's in TID groups 147838b6fd2SKaike Wan * 148838b6fd2SKaike Wan * @grp_idx: rcd relative index to tid_group 149838b6fd2SKaike Wan * @map: grp->map captured prior to programming this TID group in HW 150838b6fd2SKaike Wan * @cnt: Only @cnt of available group entries are actually programmed 151838b6fd2SKaike Wan */ 152838b6fd2SKaike Wan struct kern_tid_node { 153838b6fd2SKaike Wan struct tid_group *grp; 154838b6fd2SKaike Wan u8 map; 155838b6fd2SKaike Wan u8 cnt; 156838b6fd2SKaike Wan }; 157838b6fd2SKaike Wan 158838b6fd2SKaike Wan /* Overall info for a TID RDMA segment */ 159838b6fd2SKaike Wan struct tid_rdma_flow { 160838b6fd2SKaike Wan /* 161838b6fd2SKaike Wan * While a TID RDMA segment is being transferred, it uses a QP number 162838b6fd2SKaike Wan * from the "KDETH section of QP numbers" (which is different from the 163838b6fd2SKaike Wan * QP number that originated the request). Bits 11-15 of these QP 164838b6fd2SKaike Wan * numbers identify the "TID flow" for the segment. 165838b6fd2SKaike Wan */ 166838b6fd2SKaike Wan struct flow_state flow_state; 167838b6fd2SKaike Wan struct tid_rdma_request *req; 168d0d564a1SKaike Wan u32 tid_qpn; 169d0d564a1SKaike Wan u32 tid_offset; 170838b6fd2SKaike Wan u32 length; 171742a3826SKaike Wan u32 sent; 172838b6fd2SKaike Wan u8 tnode_cnt; 173838b6fd2SKaike Wan u8 tidcnt; 174742a3826SKaike Wan u8 tid_idx; 175838b6fd2SKaike Wan u8 idx; 176838b6fd2SKaike Wan u8 npagesets; 177838b6fd2SKaike Wan u8 npkts; 178742a3826SKaike Wan u8 pkt; 17972a0ea99SKaike Wan u8 resync_npkts; 180838b6fd2SKaike Wan struct kern_tid_node tnode[TID_RDMA_MAX_PAGES]; 181838b6fd2SKaike Wan struct tid_rdma_pageset pagesets[TID_RDMA_MAX_PAGES]; 182838b6fd2SKaike Wan u32 tid_entry[TID_RDMA_MAX_PAGES]; 183838b6fd2SKaike Wan }; 184838b6fd2SKaike Wan 18507b92370SKaike Wan enum tid_rnr_nak_state { 18607b92370SKaike Wan TID_RNR_NAK_INIT = 0, 18707b92370SKaike Wan TID_RNR_NAK_SEND, 18807b92370SKaike Wan TID_RNR_NAK_SENT, 18907b92370SKaike Wan }; 19007b92370SKaike Wan 191d22a207dSKaike Wan bool tid_rdma_conn_req(struct rvt_qp *qp, u64 *data); 192d22a207dSKaike Wan bool tid_rdma_conn_reply(struct rvt_qp *qp, u64 data); 193d22a207dSKaike Wan bool tid_rdma_conn_resp(struct rvt_qp *qp, u64 *data); 194d22a207dSKaike Wan void tid_rdma_conn_error(struct rvt_qp *qp); 195d22a207dSKaike Wan void tid_rdma_opfn_init(struct rvt_qp *qp, struct tid_rdma_params *p); 196d22a207dSKaike Wan 197d22a207dSKaike Wan int hfi1_kern_exp_rcv_init(struct hfi1_ctxtdata *rcd, int reinit); 198838b6fd2SKaike Wan int hfi1_kern_exp_rcv_setup(struct tid_rdma_request *req, 199838b6fd2SKaike Wan struct rvt_sge_state *ss, bool *last); 200838b6fd2SKaike Wan int hfi1_kern_exp_rcv_clear(struct tid_rdma_request *req); 201838b6fd2SKaike Wan void hfi1_kern_exp_rcv_clear_all(struct tid_rdma_request *req); 202838b6fd2SKaike Wan void __trdma_clean_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe); 203838b6fd2SKaike Wan 204838b6fd2SKaike Wan /** 205838b6fd2SKaike Wan * trdma_clean_swqe - clean flows for swqe if large send queue 206838b6fd2SKaike Wan * @qp: the qp 207838b6fd2SKaike Wan * @wqe: the send wqe 208838b6fd2SKaike Wan */ 209838b6fd2SKaike Wan static inline void trdma_clean_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe) 210838b6fd2SKaike Wan { 211838b6fd2SKaike Wan if (!wqe->priv) 212838b6fd2SKaike Wan return; 213838b6fd2SKaike Wan __trdma_clean_swqe(qp, wqe); 214838b6fd2SKaike Wan } 215d22a207dSKaike Wan 2169905bf06SKaike Wan void hfi1_kern_read_tid_flow_free(struct rvt_qp *qp); 2179905bf06SKaike Wan 2185190f052SMike Marciniszyn int hfi1_qp_priv_init(struct rvt_dev_info *rdi, struct rvt_qp *qp, 2195190f052SMike Marciniszyn struct ib_qp_init_attr *init_attr); 22048a615dcSKaike Wan void hfi1_qp_priv_tid_free(struct rvt_dev_info *rdi, struct rvt_qp *qp); 2215190f052SMike Marciniszyn 22237356e78SKaike Wan void hfi1_tid_rdma_flush_wait(struct rvt_qp *qp); 22337356e78SKaike Wan 22437356e78SKaike Wan int hfi1_kern_setup_hw_flow(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp); 22537356e78SKaike Wan void hfi1_kern_clear_hw_flow(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp); 22637356e78SKaike Wan void hfi1_kern_init_ctxt_generations(struct hfi1_ctxtdata *rcd); 22737356e78SKaike Wan 2282f16a696SKaike Wan struct cntr_entry; 2292f16a696SKaike Wan u64 hfi1_access_sw_tid_wait(const struct cntr_entry *entry, 2302f16a696SKaike Wan void *context, int vl, int mode, u64 data); 2312f16a696SKaike Wan 232742a3826SKaike Wan u32 hfi1_build_tid_rdma_read_packet(struct rvt_swqe *wqe, 233742a3826SKaike Wan struct ib_other_headers *ohdr, 234742a3826SKaike Wan u32 *bth1, u32 *bth2, u32 *len); 235742a3826SKaike Wan u32 hfi1_build_tid_rdma_read_req(struct rvt_qp *qp, struct rvt_swqe *wqe, 236742a3826SKaike Wan struct ib_other_headers *ohdr, u32 *bth1, 237742a3826SKaike Wan u32 *bth2, u32 *len); 238d0d564a1SKaike Wan void hfi1_rc_rcv_tid_rdma_read_req(struct hfi1_packet *packet); 2391db21b50SKaike Wan u32 hfi1_build_tid_rdma_read_resp(struct rvt_qp *qp, struct rvt_ack_entry *e, 2401db21b50SKaike Wan struct ib_other_headers *ohdr, u32 *bth0, 2411db21b50SKaike Wan u32 *bth1, u32 *bth2, u32 *len, bool *last); 2429905bf06SKaike Wan void hfi1_rc_rcv_tid_rdma_read_resp(struct hfi1_packet *packet); 2439905bf06SKaike Wan bool hfi1_handle_kdeth_eflags(struct hfi1_ctxtdata *rcd, 2449905bf06SKaike Wan struct hfi1_pportdata *ppd, 2459905bf06SKaike Wan struct hfi1_packet *packet); 246b126078eSKaike Wan void hfi1_tid_rdma_restart_req(struct rvt_qp *qp, struct rvt_swqe *wqe, 247b126078eSKaike Wan u32 *bth2); 24824b11923SKaike Wan void hfi1_qp_kern_exp_rcv_clear_all(struct rvt_qp *qp); 249a0b34f75SKaike Wan bool hfi1_tid_rdma_wqe_interlock(struct rvt_qp *qp, struct rvt_swqe *wqe); 250742a3826SKaike Wan 251f1ab4efaSKaike Wan void setup_tid_rdma_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe); 252f1ab4efaSKaike Wan static inline void hfi1_setup_tid_rdma_wqe(struct rvt_qp *qp, 253f1ab4efaSKaike Wan struct rvt_swqe *wqe) 254f1ab4efaSKaike Wan { 255f1ab4efaSKaike Wan if (wqe->priv && 256f1ab4efaSKaike Wan wqe->wr.opcode == IB_WR_RDMA_READ && 257f1ab4efaSKaike Wan wqe->length >= TID_RDMA_MIN_SEGMENT_SIZE) 258f1ab4efaSKaike Wan setup_tid_rdma_wqe(qp, wqe); 259f1ab4efaSKaike Wan } 260f1ab4efaSKaike Wan 261c098bbb0SKaike Wan u32 hfi1_build_tid_rdma_write_req(struct rvt_qp *qp, struct rvt_swqe *wqe, 262c098bbb0SKaike Wan struct ib_other_headers *ohdr, 263c098bbb0SKaike Wan u32 *bth1, u32 *bth2, u32 *len); 26407b92370SKaike Wan 26507b92370SKaike Wan void hfi1_compute_tid_rdma_flow_wt(void); 26607b92370SKaike Wan 26707b92370SKaike Wan void hfi1_rc_rcv_tid_rdma_write_req(struct hfi1_packet *packet); 26807b92370SKaike Wan 26938d46d36SKaike Wan u32 hfi1_build_tid_rdma_write_resp(struct rvt_qp *qp, struct rvt_ack_entry *e, 27038d46d36SKaike Wan struct ib_other_headers *ohdr, u32 *bth1, 27138d46d36SKaike Wan u32 bth2, u32 *len, 27238d46d36SKaike Wan struct rvt_sge_state **ss); 27338d46d36SKaike Wan 2743c759e00SKaike Wan void hfi1_del_tid_reap_timer(struct rvt_qp *qp); 2753c759e00SKaike Wan 27672a0ea99SKaike Wan void hfi1_rc_rcv_tid_rdma_write_resp(struct hfi1_packet *packet); 27772a0ea99SKaike Wan 278*539e1908SKaike Wan bool hfi1_build_tid_rdma_packet(struct rvt_swqe *wqe, 279*539e1908SKaike Wan struct ib_other_headers *ohdr, 280*539e1908SKaike Wan u32 *bth1, u32 *bth2, u32 *len); 281*539e1908SKaike Wan 2825190f052SMike Marciniszyn #endif /* HFI1_TID_RDMA_H */ 283