1145eba1aSCai Huoqing /* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */
2f48ad614SDennis Dalessandro /*
3*d2c02346SBrendan Cunningham  * Copyright(c) 2023 - Cornelis Networks, Inc.
45465f110SKaike Wan  * Copyright(c) 2015 - 2018 Intel Corporation.
5f48ad614SDennis Dalessandro  */
6145eba1aSCai Huoqing #ifndef _HFI1_USER_SDMA_H
7145eba1aSCai Huoqing #define _HFI1_USER_SDMA_H
8145eba1aSCai Huoqing 
9f48ad614SDennis Dalessandro #include <linux/device.h>
10f48ad614SDennis Dalessandro #include <linux/wait.h>
11f48ad614SDennis Dalessandro 
12f48ad614SDennis Dalessandro #include "common.h"
13f48ad614SDennis Dalessandro #include "iowait.h"
14f48ad614SDennis Dalessandro #include "user_exp_rcv.h"
156b13215dSMike Marciniszyn #include "mmu_rb.h"
16*d2c02346SBrendan Cunningham #include "pinning.h"
17*d2c02346SBrendan Cunningham #include "sdma.h"
18f48ad614SDennis Dalessandro 
1932500f27SHarish Chegondi /* The maximum number of Data io vectors per message/request */
2032500f27SHarish Chegondi #define MAX_VECTORS_PER_REQ 8
2132500f27SHarish Chegondi /*
2232500f27SHarish Chegondi  * Maximum number of packet to send from each message/request
2332500f27SHarish Chegondi  * before moving to the next one.
2432500f27SHarish Chegondi  */
2532500f27SHarish Chegondi #define MAX_PKTS_PER_QUEUE 16
2632500f27SHarish Chegondi 
2732500f27SHarish Chegondi #define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT))
2832500f27SHarish Chegondi 
2932500f27SHarish Chegondi #define req_opcode(x) \
3032500f27SHarish Chegondi 	(((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
3132500f27SHarish Chegondi #define req_version(x) \
3232500f27SHarish Chegondi 	(((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
3332500f27SHarish Chegondi #define req_iovcnt(x) \
3432500f27SHarish Chegondi 	(((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK)
3532500f27SHarish Chegondi 
3632500f27SHarish Chegondi /* Number of BTH.PSN bits used for sequence number in expected rcvs */
3732500f27SHarish Chegondi #define BTH_SEQ_MASK 0x7ffull
3832500f27SHarish Chegondi 
3932500f27SHarish Chegondi #define AHG_KDETH_INTR_SHIFT 12
4032500f27SHarish Chegondi #define AHG_KDETH_SH_SHIFT   13
4132500f27SHarish Chegondi #define AHG_KDETH_ARRAY_SIZE  9
4232500f27SHarish Chegondi 
4332500f27SHarish Chegondi #define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
4432500f27SHarish Chegondi #define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
4532500f27SHarish Chegondi 
46d34ed562SHarish Chegondi /**
47d34ed562SHarish Chegondi  * Build an SDMA AHG header update descriptor and save it to an array.
48d34ed562SHarish Chegondi  * @arr        - Array to save the descriptor to.
49d34ed562SHarish Chegondi  * @idx        - Index of the array at which the descriptor will be saved.
50d34ed562SHarish Chegondi  * @array_size - Size of the array arr.
51d34ed562SHarish Chegondi  * @dw         - Update index into the header in DWs.
52d34ed562SHarish Chegondi  * @bit        - Start bit.
53d34ed562SHarish Chegondi  * @width      - Field width.
54d34ed562SHarish Chegondi  * @value      - 16 bits of immediate data to write into the field.
55d34ed562SHarish Chegondi  * Returns -ERANGE if idx is invalid. If successful, returns the next index
56d34ed562SHarish Chegondi  * (idx + 1) of the array to be used for the next descriptor.
57d34ed562SHarish Chegondi  */
ahg_header_set(u32 * arr,int idx,size_t array_size,u8 dw,u8 bit,u8 width,u16 value)58d34ed562SHarish Chegondi static inline int ahg_header_set(u32 *arr, int idx, size_t array_size,
59d34ed562SHarish Chegondi 				 u8 dw, u8 bit, u8 width, u16 value)
60d34ed562SHarish Chegondi {
61d34ed562SHarish Chegondi 	if ((size_t)idx >= array_size)
62d34ed562SHarish Chegondi 		return -ERANGE;
63d34ed562SHarish Chegondi 	arr[idx++] = sdma_build_ahg_descriptor(value, dw, bit, width);
64d34ed562SHarish Chegondi 	return idx;
65d34ed562SHarish Chegondi }
6632500f27SHarish Chegondi 
6732500f27SHarish Chegondi /* Tx request flag bits */
6832500f27SHarish Chegondi #define TXREQ_FLAGS_REQ_ACK   BIT(0)      /* Set the ACK bit in the header */
6932500f27SHarish Chegondi #define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1) /* Disable header suppression */
7032500f27SHarish Chegondi 
7128a9a9e8SMichael J. Ruhl enum pkt_q_sdma_state {
7228a9a9e8SMichael J. Ruhl 	SDMA_PKT_Q_ACTIVE,
7328a9a9e8SMichael J. Ruhl 	SDMA_PKT_Q_DEFERRED,
7428a9a9e8SMichael J. Ruhl };
7532500f27SHarish Chegondi 
7632500f27SHarish Chegondi #define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
7732500f27SHarish Chegondi 
7832500f27SHarish Chegondi #define SDMA_DBG(req, fmt, ...)				     \
7932500f27SHarish Chegondi 	hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
8032500f27SHarish Chegondi 		 (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
8132500f27SHarish Chegondi 		 ##__VA_ARGS__)
8232500f27SHarish Chegondi 
83f48ad614SDennis Dalessandro struct hfi1_user_sdma_pkt_q {
84e6f7622dSMichael J. Ruhl 	u16 ctxt;
858737ce95SMichael J. Ruhl 	u16 subctxt;
86f48ad614SDennis Dalessandro 	u16 n_max_reqs;
87f48ad614SDennis Dalessandro 	atomic_t n_reqs;
88f48ad614SDennis Dalessandro 	u16 reqidx;
89f48ad614SDennis Dalessandro 	struct hfi1_devdata *dd;
90f48ad614SDennis Dalessandro 	struct kmem_cache *txreq_cache;
91f48ad614SDennis Dalessandro 	struct user_sdma_request *reqs;
927b3256e3SDean Luick 	unsigned long *req_in_use;
93f48ad614SDennis Dalessandro 	struct iowait busy;
9428a9a9e8SMichael J. Ruhl 	enum pkt_q_sdma_state state;
95f48ad614SDennis Dalessandro 	wait_queue_head_t wait;
96f48ad614SDennis Dalessandro 	unsigned long unpinned;
97e0b09ac5SDean Luick 	struct mmu_rb_handler *handler;
98b7df192fSDean Luick 	atomic_t n_locked;
99f48ad614SDennis Dalessandro };
100f48ad614SDennis Dalessandro 
101f48ad614SDennis Dalessandro struct hfi1_user_sdma_comp_q {
102f48ad614SDennis Dalessandro 	u16 nentries;
103f48ad614SDennis Dalessandro 	struct hfi1_sdma_comp_entry *comps;
104f48ad614SDennis Dalessandro };
105f48ad614SDennis Dalessandro 
10632500f27SHarish Chegondi struct user_sdma_iovec {
10732500f27SHarish Chegondi 	struct list_head list;
10832500f27SHarish Chegondi 	struct iovec iov;
10932500f27SHarish Chegondi 	/*
11032500f27SHarish Chegondi 	 * offset into the virtual address space of the vector at
11132500f27SHarish Chegondi 	 * which we last left off.
11232500f27SHarish Chegondi 	 */
11332500f27SHarish Chegondi 	u64 offset;
11432500f27SHarish Chegondi };
11532500f27SHarish Chegondi 
11632500f27SHarish Chegondi /* evict operation argument */
11732500f27SHarish Chegondi struct evict_data {
11832500f27SHarish Chegondi 	u32 cleared;	/* count evicted so far */
11932500f27SHarish Chegondi 	u32 target;	/* target count to evict */
12032500f27SHarish Chegondi };
12132500f27SHarish Chegondi 
12232500f27SHarish Chegondi struct user_sdma_request {
12332500f27SHarish Chegondi 	/* This is the original header from user space */
12432500f27SHarish Chegondi 	struct hfi1_pkt_header hdr;
12532500f27SHarish Chegondi 
12632500f27SHarish Chegondi 	/* Read mostly fields */
12732500f27SHarish Chegondi 	struct hfi1_user_sdma_pkt_q *pq ____cacheline_aligned_in_smp;
12832500f27SHarish Chegondi 	struct hfi1_user_sdma_comp_q *cq;
12932500f27SHarish Chegondi 	/*
13032500f27SHarish Chegondi 	 * Pointer to the SDMA engine for this request.
13132500f27SHarish Chegondi 	 * Since different request could be on different VLs,
13232500f27SHarish Chegondi 	 * each request will need it's own engine pointer.
13332500f27SHarish Chegondi 	 */
13432500f27SHarish Chegondi 	struct sdma_engine *sde;
13532500f27SHarish Chegondi 	struct sdma_req_info info;
13632500f27SHarish Chegondi 	/* TID array values copied from the tid_iov vector */
13732500f27SHarish Chegondi 	u32 *tids;
13832500f27SHarish Chegondi 	/* total length of the data in the request */
13932500f27SHarish Chegondi 	u32 data_len;
14032500f27SHarish Chegondi 	/* number of elements copied to the tids array */
14132500f27SHarish Chegondi 	u16 n_tids;
14232500f27SHarish Chegondi 	/*
14332500f27SHarish Chegondi 	 * We copy the iovs for this request (based on
14432500f27SHarish Chegondi 	 * info.iovcnt). These are only the data vectors
14532500f27SHarish Chegondi 	 */
14632500f27SHarish Chegondi 	u8 data_iovs;
14732500f27SHarish Chegondi 	s8 ahg_idx;
14832500f27SHarish Chegondi 
14932500f27SHarish Chegondi 	/* Writeable fields shared with interrupt */
1503ca633f1SMichael J. Ruhl 	u16 seqcomp ____cacheline_aligned_in_smp;
1513ca633f1SMichael J. Ruhl 	u16 seqsubmitted;
15232500f27SHarish Chegondi 
15332500f27SHarish Chegondi 	/* Send side fields */
15432500f27SHarish Chegondi 	struct list_head txps ____cacheline_aligned_in_smp;
1553ca633f1SMichael J. Ruhl 	u16 seqnum;
15632500f27SHarish Chegondi 	/*
15732500f27SHarish Chegondi 	 * KDETH.OFFSET (TID) field
15832500f27SHarish Chegondi 	 * The offset can cover multiple packets, depending on the
15932500f27SHarish Chegondi 	 * size of the TID entry.
16032500f27SHarish Chegondi 	 */
16132500f27SHarish Chegondi 	u32 tidoffset;
16232500f27SHarish Chegondi 	/*
16332500f27SHarish Chegondi 	 * KDETH.Offset (Eager) field
16432500f27SHarish Chegondi 	 * We need to remember the initial value so the headers
16532500f27SHarish Chegondi 	 * can be updated properly.
16632500f27SHarish Chegondi 	 */
16732500f27SHarish Chegondi 	u32 koffset;
16832500f27SHarish Chegondi 	u32 sent;
16932500f27SHarish Chegondi 	/* TID index copied from the tid_iov vector */
17032500f27SHarish Chegondi 	u16 tididx;
17132500f27SHarish Chegondi 	/* progress index moving along the iovs array */
17232500f27SHarish Chegondi 	u8 iov_idx;
17332500f27SHarish Chegondi 	u8 has_error;
17432500f27SHarish Chegondi 
17532500f27SHarish Chegondi 	struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
17632500f27SHarish Chegondi } ____cacheline_aligned_in_smp;
17732500f27SHarish Chegondi 
17832500f27SHarish Chegondi /*
17932500f27SHarish Chegondi  * A single txreq could span up to 3 physical pages when the MTU
18032500f27SHarish Chegondi  * is sufficiently large (> 4K). Each of the IOV pointers also
18132500f27SHarish Chegondi  * needs it's own set of flags so the vector has been handled
18232500f27SHarish Chegondi  * independently of each other.
18332500f27SHarish Chegondi  */
18432500f27SHarish Chegondi struct user_sdma_txreq {
18532500f27SHarish Chegondi 	/* Packet header for the txreq */
18632500f27SHarish Chegondi 	struct hfi1_pkt_header hdr;
18732500f27SHarish Chegondi 	struct sdma_txreq txreq;
18832500f27SHarish Chegondi 	struct list_head list;
18932500f27SHarish Chegondi 	struct user_sdma_request *req;
19032500f27SHarish Chegondi 	u16 flags;
1913ca633f1SMichael J. Ruhl 	u16 seqnum;
19232500f27SHarish Chegondi };
19332500f27SHarish Chegondi 
1945042cddfSMichael J. Ruhl int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,
1955042cddfSMichael J. Ruhl 				struct hfi1_filedata *fd);
196e87473bcSMichael J. Ruhl int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd,
197e87473bcSMichael J. Ruhl 			       struct hfi1_ctxtdata *uctxt);
1985042cddfSMichael J. Ruhl int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
1995042cddfSMichael J. Ruhl 				   struct iovec *iovec, unsigned long dim,
2005042cddfSMichael J. Ruhl 				   unsigned long *count);
201f4cd8765SMichael J. Ruhl #endif /* _HFI1_USER_SDMA_H */
202