GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / infiniband / hw / hfi1 / user_sdma.h
1 #ifndef _HFI1_USER_SDMA_H
2 #define _HFI1_USER_SDMA_H
3 /*
4  * Copyright(c) 2015 - 2017 Intel Corporation.
5  *
6  * This file is provided under a dual BSD/GPLv2 license.  When using or
7  * redistributing this file, you may do so under either license.
8  *
9  * GPL LICENSE SUMMARY
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * BSD LICENSE
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  *
26  *  - Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  *  - Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in
30  *    the documentation and/or other materials provided with the
31  *    distribution.
32  *  - Neither the name of Intel Corporation nor the names of its
33  *    contributors may be used to endorse or promote products derived
34  *    from this software without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  */
49 #include <linux/device.h>
50 #include <linux/wait.h>
51
52 #include "common.h"
53 #include "iowait.h"
54 #include "user_exp_rcv.h"
55
56 /* The maximum number of Data io vectors per message/request */
57 #define MAX_VECTORS_PER_REQ 8
58 /*
59  * Maximum number of packet to send from each message/request
60  * before moving to the next one.
61  */
62 #define MAX_PKTS_PER_QUEUE 16
63
64 #define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT))
65
66 #define req_opcode(x) \
67         (((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
68 #define req_version(x) \
69         (((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
70 #define req_iovcnt(x) \
71         (((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK)
72
73 /* Number of BTH.PSN bits used for sequence number in expected rcvs */
74 #define BTH_SEQ_MASK 0x7ffull
75
76 #define AHG_KDETH_INTR_SHIFT 12
77 #define AHG_KDETH_SH_SHIFT   13
78 #define AHG_KDETH_ARRAY_SIZE  9
79
80 #define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
81 #define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
82
83 #define AHG_HEADER_SET(arr, idx, dw, bit, width, value)                 \
84         do {                                                            \
85                 if ((idx) < ARRAY_SIZE((arr)))                          \
86                         (arr)[(idx++)] = sdma_build_ahg_descriptor(     \
87                                 (__force u16)(value), (dw), (bit),      \
88                                                         (width));       \
89                 else                                                    \
90                         return -ERANGE;                                 \
91         } while (0)
92
93 /* Tx request flag bits */
94 #define TXREQ_FLAGS_REQ_ACK   BIT(0)      /* Set the ACK bit in the header */
95 #define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1) /* Disable header suppression */
96
97 enum pkt_q_sdma_state {
98         SDMA_PKT_Q_ACTIVE,
99         SDMA_PKT_Q_DEFERRED,
100 };
101
102 /*
103  * Maximum retry attempts to submit a TX request
104  * before putting the process to sleep.
105  */
106 #define MAX_DEFER_RETRY_COUNT 1
107
108 #define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
109
110 #define SDMA_DBG(req, fmt, ...)                              \
111         hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
112                  (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
113                  ##__VA_ARGS__)
114
115 extern uint extended_psn;
116
117 struct hfi1_user_sdma_pkt_q {
118         u16 ctxt;
119         u16 subctxt;
120         u16 n_max_reqs;
121         atomic_t n_reqs;
122         u16 reqidx;
123         struct hfi1_devdata *dd;
124         struct kmem_cache *txreq_cache;
125         struct user_sdma_request *reqs;
126         unsigned long *req_in_use;
127         struct iowait busy;
128         enum pkt_q_sdma_state state;
129         wait_queue_head_t wait;
130         unsigned long unpinned;
131         struct mmu_rb_handler *handler;
132         atomic_t n_locked;
133         struct mm_struct *mm;
134 };
135
136 struct hfi1_user_sdma_comp_q {
137         u16 nentries;
138         struct hfi1_sdma_comp_entry *comps;
139 };
140
141 struct sdma_mmu_node {
142         struct mmu_rb_node rb;
143         struct hfi1_user_sdma_pkt_q *pq;
144         atomic_t refcount;
145         struct page **pages;
146         unsigned int npages;
147 };
148
149 struct user_sdma_iovec {
150         struct list_head list;
151         struct iovec iov;
152         /* number of pages in this vector */
153         unsigned int npages;
154         /* array of pinned pages for this vector */
155         struct page **pages;
156         /*
157          * offset into the virtual address space of the vector at
158          * which we last left off.
159          */
160         u64 offset;
161         struct sdma_mmu_node *node;
162 };
163
164 /* evict operation argument */
165 struct evict_data {
166         u32 cleared;    /* count evicted so far */
167         u32 target;     /* target count to evict */
168 };
169
170 struct user_sdma_request {
171         /* This is the original header from user space */
172         struct hfi1_pkt_header hdr;
173
174         /* Read mostly fields */
175         struct hfi1_user_sdma_pkt_q *pq ____cacheline_aligned_in_smp;
176         struct hfi1_user_sdma_comp_q *cq;
177         /*
178          * Pointer to the SDMA engine for this request.
179          * Since different request could be on different VLs,
180          * each request will need it's own engine pointer.
181          */
182         struct sdma_engine *sde;
183         struct sdma_req_info info;
184         /* TID array values copied from the tid_iov vector */
185         u32 *tids;
186         /* total length of the data in the request */
187         u32 data_len;
188         /* number of elements copied to the tids array */
189         u16 n_tids;
190         /*
191          * We copy the iovs for this request (based on
192          * info.iovcnt). These are only the data vectors
193          */
194         u8 data_iovs;
195         s8 ahg_idx;
196
197         /* Writeable fields shared with interrupt */
198         u64 seqcomp ____cacheline_aligned_in_smp;
199         u64 seqsubmitted;
200
201         /* Send side fields */
202         struct list_head txps ____cacheline_aligned_in_smp;
203         u64 seqnum;
204         /*
205          * KDETH.OFFSET (TID) field
206          * The offset can cover multiple packets, depending on the
207          * size of the TID entry.
208          */
209         u32 tidoffset;
210         /*
211          * KDETH.Offset (Eager) field
212          * We need to remember the initial value so the headers
213          * can be updated properly.
214          */
215         u32 koffset;
216         u32 sent;
217         /* TID index copied from the tid_iov vector */
218         u16 tididx;
219         /* progress index moving along the iovs array */
220         u8 iov_idx;
221         u8 has_error;
222
223         struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
224 } ____cacheline_aligned_in_smp;
225
226 /*
227  * A single txreq could span up to 3 physical pages when the MTU
228  * is sufficiently large (> 4K). Each of the IOV pointers also
229  * needs it's own set of flags so the vector has been handled
230  * independently of each other.
231  */
232 struct user_sdma_txreq {
233         /* Packet header for the txreq */
234         struct hfi1_pkt_header hdr;
235         struct sdma_txreq txreq;
236         struct list_head list;
237         struct user_sdma_request *req;
238         u16 flags;
239         u64 seqnum;
240 };
241
242 int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,
243                                 struct hfi1_filedata *fd);
244 int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd,
245                                struct hfi1_ctxtdata *uctxt);
246 int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
247                                    struct iovec *iovec, unsigned long dim,
248                                    unsigned long *count);
249
250 #endif /* _HFI1_USER_SDMA_H */