Merge remote-tracking branch 'regulator/fix/core' into regulator-linus
[cascardo/linux.git] / drivers / staging / rdma / hfi1 / pio.h
1 #ifndef _PIO_H
2 #define _PIO_H
3 /*
4  * Copyright(c) 2015, 2016 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
50 /* send context types */
51 #define SC_KERNEL 0
52 #define SC_ACK    1
53 #define SC_USER   2
54 #define SC_MAX    3
55
56 /* invalid send context index */
57 #define INVALID_SCI 0xff
58
59 /* PIO buffer release callback function */
60 typedef void (*pio_release_cb)(void *arg, int code);
61
62 /* PIO release codes - in bits, as there could more than one that apply */
63 #define PRC_OK          0       /* no known error */
64 #define PRC_STATUS_ERR  0x01    /* credit return due to status error */
65 #define PRC_PBC         0x02    /* credit return due to PBC */
66 #define PRC_THRESHOLD   0x04    /* credit return due to threshold */
67 #define PRC_FILL_ERR    0x08    /* credit return due fill error */
68 #define PRC_FORCE       0x10    /* credit return due credit force */
69 #define PRC_SC_DISABLE  0x20    /* clean-up after a context disable */
70
71 /* byte helper */
72 union mix {
73         u64 val64;
74         u32 val32[2];
75         u8  val8[8];
76 };
77
78 /* an allocated PIO buffer */
79 struct pio_buf {
80         struct send_context *sc;/* back pointer to owning send context */
81         pio_release_cb cb;      /* called when the buffer is released */
82         void *arg;              /* argument for cb */
83         void __iomem *start;    /* buffer start address */
84         void __iomem *end;      /* context end address */
85         unsigned long size;     /* context size, in bytes */
86         unsigned long sent_at;  /* buffer is sent when <= free */
87         u32 block_count;        /* size of buffer, in blocks */
88         u32 qw_written;         /* QW written so far */
89         u32 carry_bytes;        /* number of valid bytes in carry */
90         union mix carry;        /* pending unwritten bytes */
91 };
92
93 /* cache line aligned pio buffer array */
94 union pio_shadow_ring {
95         struct pio_buf pbuf;
96         u64 unused[16];         /* cache line spacer */
97 } ____cacheline_aligned;
98
99 /* per-NUMA send context */
100 struct send_context {
101         /* read-only after init */
102         struct hfi1_devdata *dd;                /* device */
103         void __iomem *base_addr;        /* start of PIO memory */
104         union pio_shadow_ring *sr;      /* shadow ring */
105
106         volatile __le64 *hw_free;       /* HW free counter */
107         struct work_struct halt_work;   /* halted context work queue entry */
108         unsigned long flags;            /* flags */
109         int node;                       /* context home node */
110         int type;                       /* context type */
111         u32 sw_index;                   /* software index number */
112         u32 hw_context;                 /* hardware context number */
113         u32 credits;                    /* number of blocks in context */
114         u32 sr_size;                    /* size of the shadow ring */
115         u32 group;                      /* credit return group */
116         /* allocator fields */
117         spinlock_t alloc_lock ____cacheline_aligned_in_smp;
118         unsigned long fill;             /* official alloc count */
119         unsigned long alloc_free;       /* copy of free (less cache thrash) */
120         u32 sr_head;                    /* shadow ring head */
121         /* releaser fields */
122         spinlock_t release_lock ____cacheline_aligned_in_smp;
123         unsigned long free;             /* official free count */
124         u32 sr_tail;                    /* shadow ring tail */
125         /* list for PIO waiters */
126         struct list_head piowait  ____cacheline_aligned_in_smp;
127         spinlock_t credit_ctrl_lock ____cacheline_aligned_in_smp;
128         u64 credit_ctrl;                /* cache for credit control */
129         u32 credit_intr_count;          /* count of credit intr users */
130         u32 __percpu *buffers_allocated;/* count of buffers allocated */
131         wait_queue_head_t halt_wait;    /* wait until kernel sees interrupt */
132 };
133
134 /* send context flags */
135 #define SCF_ENABLED 0x01
136 #define SCF_IN_FREE 0x02
137 #define SCF_HALTED  0x04
138 #define SCF_FROZEN  0x08
139
140 struct send_context_info {
141         struct send_context *sc;        /* allocated working context */
142         u16 allocated;                  /* has this been allocated? */
143         u16 type;                       /* context type */
144         u16 base;                       /* base in PIO array */
145         u16 credits;                    /* size in PIO array */
146 };
147
148 /* DMA credit return, index is always (context & 0x7) */
149 struct credit_return {
150         volatile __le64 cr[8];
151 };
152
153 /* NUMA indexed credit return array */
154 struct credit_return_base {
155         struct credit_return *va;
156         dma_addr_t pa;
157 };
158
159 /* send context configuration sizes (one per type) */
160 struct sc_config_sizes {
161         short int size;
162         short int count;
163 };
164
165 /*
166  * The diagram below details the relationship of the mapping structures
167  *
168  * Since the mapping now allows for non-uniform send contexts per vl, the
169  * number of send contexts for a vl is either the vl_scontexts[vl] or
170  * a computation based on num_kernel_send_contexts/num_vls:
171  *
172  * For example:
173  * nactual = vl_scontexts ? vl_scontexts[vl] : num_kernel_send_contexts/num_vls
174  *
175  * n = roundup to next highest power of 2 using nactual
176  *
177  * In the case where there are num_kernel_send_contexts/num_vls doesn't divide
178  * evenly, the extras are added from the last vl downward.
179  *
180  * For the case where n > nactual, the send contexts are assigned
181  * in a round robin fashion wrapping back to the first send context
182  * for a particular vl.
183  *
184  *               dd->pio_map
185  *                    |                                   pio_map_elem[0]
186  *                    |                                +--------------------+
187  *                    v                                |       mask         |
188  *               pio_vl_map                            |--------------------|
189  *      +--------------------------+                   | ksc[0] -> sc 1     |
190  *      |    list (RCU)            |                   |--------------------|
191  *      |--------------------------|                 ->| ksc[1] -> sc 2     |
192  *      |    mask                  |              --/  |--------------------|
193  *      |--------------------------|            -/     |        *           |
194  *      |    actual_vls (max 8)    |          -/       |--------------------|
195  *      |--------------------------|       --/         | ksc[n] -> sc n     |
196  *      |    vls (max 8)           |     -/            +--------------------+
197  *      |--------------------------|  --/
198  *      |    map[0]                |-/
199  *      |--------------------------|                   +--------------------+
200  *      |    map[1]                |---                |       mask         |
201  *      |--------------------------|   \----           |--------------------|
202  *      |           *              |        \--        | ksc[0] -> sc 1+n   |
203  *      |           *              |           \----   |--------------------|
204  *      |           *              |                \->| ksc[1] -> sc 2+n   |
205  *      |--------------------------|                   |--------------------|
206  *      |   map[vls - 1]           |-                  |         *          |
207  *      +--------------------------+ \-                |--------------------|
208  *                                     \-              | ksc[m] -> sc m+n   |
209  *                                       \             +--------------------+
210  *                                        \-
211  *                                          \
212  *                                           \-        +--------------------+
213  *                                             \-      |       mask         |
214  *                                               \     |--------------------|
215  *                                                \-   | ksc[0] -> sc 1+m+n |
216  *                                                  \- |--------------------|
217  *                                                    >| ksc[1] -> sc 2+m+n |
218  *                                                     |--------------------|
219  *                                                     |         *          |
220  *                                                     |--------------------|
221  *                                                     | ksc[o] -> sc o+m+n |
222  *                                                     +--------------------+
223  *
224  */
225
226 /* Initial number of send contexts per VL */
227 #define INIT_SC_PER_VL 2
228
229 /*
230  * struct pio_map_elem - mapping for a vl
231  * @mask - selector mask
232  * @ksc - array of kernel send contexts for this vl
233  *
234  * The mask is used to "mod" the selector to
235  * produce index into the trailing array of
236  * kscs
237  */
238 struct pio_map_elem {
239         u32 mask;
240         struct send_context *ksc[0];
241 };
242
243 /*
244  * struct pio_vl_map - mapping for a vl
245  * @list - rcu head for free callback
246  * @mask - vl mask to "mod" the vl to produce an index to map array
247  * @actual_vls - number of vls
248  * @vls - numbers of vls rounded to next power of 2
249  * @map - array of pio_map_elem entries
250  *
251  * This is the parent mapping structure. The trailing members of the
252  * struct point to pio_map_elem entries, which in turn point to an
253  * array of kscs for that vl.
254  */
255 struct pio_vl_map {
256         struct rcu_head list;
257         u32 mask;
258         u8 actual_vls;
259         u8 vls;
260         struct pio_map_elem *map[0];
261 };
262
263 int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls,
264                  u8 *vl_scontexts);
265 void free_pio_map(struct hfi1_devdata *dd);
266 struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
267                                                 u32 selector, u8 vl);
268 struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
269                                                 u32 selector, u8 sc5);
270
271 /* send context functions */
272 int init_credit_return(struct hfi1_devdata *dd);
273 void free_credit_return(struct hfi1_devdata *dd);
274 int init_sc_pools_and_sizes(struct hfi1_devdata *dd);
275 int init_send_contexts(struct hfi1_devdata *dd);
276 int init_credit_return(struct hfi1_devdata *dd);
277 int init_pervl_scs(struct hfi1_devdata *dd);
278 struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
279                               uint hdrqentsize, int numa);
280 void sc_free(struct send_context *sc);
281 int sc_enable(struct send_context *sc);
282 void sc_disable(struct send_context *sc);
283 int sc_restart(struct send_context *sc);
284 void sc_return_credits(struct send_context *sc);
285 void sc_flush(struct send_context *sc);
286 void sc_drop(struct send_context *sc);
287 void sc_stop(struct send_context *sc, int bit);
288 struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
289                                 pio_release_cb cb, void *arg);
290 void sc_release_update(struct send_context *sc);
291 void sc_return_credits(struct send_context *sc);
292 void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context);
293 void sc_add_credit_return_intr(struct send_context *sc);
294 void sc_del_credit_return_intr(struct send_context *sc);
295 void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold);
296 u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize);
297 void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint);
298 void sc_wait(struct hfi1_devdata *dd);
299 void set_pio_integrity(struct send_context *sc);
300
301 /* support functions */
302 void pio_reset_all(struct hfi1_devdata *dd);
303 void pio_freeze(struct hfi1_devdata *dd);
304 void pio_kernel_unfreeze(struct hfi1_devdata *dd);
305
306 /* global PIO send control operations */
307 #define PSC_GLOBAL_ENABLE 0
308 #define PSC_GLOBAL_DISABLE 1
309 #define PSC_GLOBAL_VLARB_ENABLE 2
310 #define PSC_GLOBAL_VLARB_DISABLE 3
311 #define PSC_CM_RESET 4
312 #define PSC_DATA_VL_ENABLE 5
313 #define PSC_DATA_VL_DISABLE 6
314
315 void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl);
316 void pio_send_control(struct hfi1_devdata *dd, int op);
317
318 /* PIO copy routines */
319 void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc,
320               const void *from, size_t count);
321 void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc,
322                         const void *from, size_t nbytes);
323 void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes);
324 void seg_pio_copy_end(struct pio_buf *pbuf);
325
326 #endif /* _PIO_H */