blob: 498b548055e00804f6dc7ea7c72476b36d4428ca [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001#ifndef _PIO_H
2#define _PIO_H
3/*
Jubin John05d6ac12016-02-14 20:22:17 -08004 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04005 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040011 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040022 * 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
Mike Marciniszyn77241052015-07-30 15:17:43 -040050/* send context types */
51#define SC_KERNEL 0
Jianxin Xiong859b5272016-05-12 10:23:47 -070052#define SC_VL15 1
53#define SC_ACK 2
54#define SC_USER 3 /* must be the last one: it may take all left */
55#define SC_MAX 4 /* count of send context types */
Mike Marciniszyn77241052015-07-30 15:17:43 -040056
57/* invalid send context index */
58#define INVALID_SCI 0xff
59
60/* PIO buffer release callback function */
61typedef void (*pio_release_cb)(void *arg, int code);
62
63/* PIO release codes - in bits, as there could more than one that apply */
64#define PRC_OK 0 /* no known error */
65#define PRC_STATUS_ERR 0x01 /* credit return due to status error */
66#define PRC_PBC 0x02 /* credit return due to PBC */
67#define PRC_THRESHOLD 0x04 /* credit return due to threshold */
68#define PRC_FILL_ERR 0x08 /* credit return due fill error */
69#define PRC_FORCE 0x10 /* credit return due credit force */
70#define PRC_SC_DISABLE 0x20 /* clean-up after a context disable */
71
72/* byte helper */
73union mix {
74 u64 val64;
75 u32 val32[2];
76 u8 val8[8];
77};
78
79/* an allocated PIO buffer */
80struct pio_buf {
81 struct send_context *sc;/* back pointer to owning send context */
82 pio_release_cb cb; /* called when the buffer is released */
83 void *arg; /* argument for cb */
84 void __iomem *start; /* buffer start address */
85 void __iomem *end; /* context end address */
86 unsigned long size; /* context size, in bytes */
87 unsigned long sent_at; /* buffer is sent when <= free */
88 u32 block_count; /* size of buffer, in blocks */
89 u32 qw_written; /* QW written so far */
90 u32 carry_bytes; /* number of valid bytes in carry */
91 union mix carry; /* pending unwritten bytes */
92};
93
94/* cache line aligned pio buffer array */
95union pio_shadow_ring {
96 struct pio_buf pbuf;
97 u64 unused[16]; /* cache line spacer */
98} ____cacheline_aligned;
99
100/* per-NUMA send context */
101struct send_context {
102 /* read-only after init */
103 struct hfi1_devdata *dd; /* device */
104 void __iomem *base_addr; /* start of PIO memory */
105 union pio_shadow_ring *sr; /* shadow ring */
Jubin Johnf4d507c2016-02-14 20:20:25 -0800106
Mike Marciniszyn77241052015-07-30 15:17:43 -0400107 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;
Mike Marciniszyn99c7abf2016-10-17 04:19:13 -0700118 u32 sr_head; /* shadow ring head */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400119 unsigned long fill; /* official alloc count */
120 unsigned long alloc_free; /* copy of free (less cache thrash) */
Mike Marciniszyn99c7abf2016-10-17 04:19:13 -0700121 u32 __percpu *buffers_allocated;/* count of buffers allocated */
Sebastian Sanchez2474d772016-10-25 13:12:28 -0700122 u32 fill_wrap; /* tracks fill within ring */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400123 /* releaser fields */
124 spinlock_t release_lock ____cacheline_aligned_in_smp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400125 u32 sr_tail; /* shadow ring tail */
Mike Marciniszyn99c7abf2016-10-17 04:19:13 -0700126 unsigned long free; /* official free count */
127 volatile __le64 *hw_free; /* HW free counter */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400128 /* list for PIO waiters */
129 struct list_head piowait ____cacheline_aligned_in_smp;
130 spinlock_t credit_ctrl_lock ____cacheline_aligned_in_smp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400131 u32 credit_intr_count; /* count of credit intr users */
Mike Marciniszyn99c7abf2016-10-17 04:19:13 -0700132 u64 credit_ctrl; /* cache for credit control */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400133 wait_queue_head_t halt_wait; /* wait until kernel sees interrupt */
134};
135
136/* send context flags */
137#define SCF_ENABLED 0x01
138#define SCF_IN_FREE 0x02
139#define SCF_HALTED 0x04
140#define SCF_FROZEN 0x08
141
142struct send_context_info {
143 struct send_context *sc; /* allocated working context */
144 u16 allocated; /* has this been allocated? */
145 u16 type; /* context type */
146 u16 base; /* base in PIO array */
147 u16 credits; /* size in PIO array */
148};
149
150/* DMA credit return, index is always (context & 0x7) */
151struct credit_return {
152 volatile __le64 cr[8];
153};
154
155/* NUMA indexed credit return array */
156struct credit_return_base {
157 struct credit_return *va;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700158 dma_addr_t dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400159};
160
161/* send context configuration sizes (one per type) */
162struct sc_config_sizes {
163 short int size;
164 short int count;
165};
166
Jubin John35f6bef2016-02-14 12:46:10 -0800167/*
168 * The diagram below details the relationship of the mapping structures
169 *
170 * Since the mapping now allows for non-uniform send contexts per vl, the
171 * number of send contexts for a vl is either the vl_scontexts[vl] or
172 * a computation based on num_kernel_send_contexts/num_vls:
173 *
174 * For example:
175 * nactual = vl_scontexts ? vl_scontexts[vl] : num_kernel_send_contexts/num_vls
176 *
177 * n = roundup to next highest power of 2 using nactual
178 *
179 * In the case where there are num_kernel_send_contexts/num_vls doesn't divide
180 * evenly, the extras are added from the last vl downward.
181 *
182 * For the case where n > nactual, the send contexts are assigned
183 * in a round robin fashion wrapping back to the first send context
184 * for a particular vl.
185 *
186 * dd->pio_map
187 * | pio_map_elem[0]
188 * | +--------------------+
189 * v | mask |
190 * pio_vl_map |--------------------|
191 * +--------------------------+ | ksc[0] -> sc 1 |
192 * | list (RCU) | |--------------------|
193 * |--------------------------| ->| ksc[1] -> sc 2 |
194 * | mask | --/ |--------------------|
195 * |--------------------------| -/ | * |
196 * | actual_vls (max 8) | -/ |--------------------|
197 * |--------------------------| --/ | ksc[n] -> sc n |
198 * | vls (max 8) | -/ +--------------------+
199 * |--------------------------| --/
200 * | map[0] |-/
201 * |--------------------------| +--------------------+
202 * | map[1] |--- | mask |
203 * |--------------------------| \---- |--------------------|
204 * | * | \-- | ksc[0] -> sc 1+n |
205 * | * | \---- |--------------------|
206 * | * | \->| ksc[1] -> sc 2+n |
207 * |--------------------------| |--------------------|
208 * | map[vls - 1] |- | * |
209 * +--------------------------+ \- |--------------------|
210 * \- | ksc[m] -> sc m+n |
211 * \ +--------------------+
212 * \-
213 * \
214 * \- +--------------------+
215 * \- | mask |
216 * \ |--------------------|
217 * \- | ksc[0] -> sc 1+m+n |
218 * \- |--------------------|
219 * >| ksc[1] -> sc 2+m+n |
220 * |--------------------|
221 * | * |
222 * |--------------------|
223 * | ksc[o] -> sc o+m+n |
224 * +--------------------+
225 *
226 */
227
228/* Initial number of send contexts per VL */
229#define INIT_SC_PER_VL 2
230
231/*
232 * struct pio_map_elem - mapping for a vl
233 * @mask - selector mask
234 * @ksc - array of kernel send contexts for this vl
235 *
236 * The mask is used to "mod" the selector to
237 * produce index into the trailing array of
238 * kscs
239 */
240struct pio_map_elem {
241 u32 mask;
242 struct send_context *ksc[0];
243};
244
245/*
246 * struct pio_vl_map - mapping for a vl
247 * @list - rcu head for free callback
248 * @mask - vl mask to "mod" the vl to produce an index to map array
249 * @actual_vls - number of vls
250 * @vls - numbers of vls rounded to next power of 2
251 * @map - array of pio_map_elem entries
252 *
253 * This is the parent mapping structure. The trailing members of the
254 * struct point to pio_map_elem entries, which in turn point to an
255 * array of kscs for that vl.
256 */
257struct pio_vl_map {
258 struct rcu_head list;
259 u32 mask;
260 u8 actual_vls;
261 u8 vls;
262 struct pio_map_elem *map[0];
263};
264
265int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls,
266 u8 *vl_scontexts);
267void free_pio_map(struct hfi1_devdata *dd);
268struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
269 u32 selector, u8 vl);
270struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
271 u32 selector, u8 sc5);
272
Mike Marciniszyn77241052015-07-30 15:17:43 -0400273/* send context functions */
274int init_credit_return(struct hfi1_devdata *dd);
275void free_credit_return(struct hfi1_devdata *dd);
276int init_sc_pools_and_sizes(struct hfi1_devdata *dd);
277int init_send_contexts(struct hfi1_devdata *dd);
278int init_credit_return(struct hfi1_devdata *dd);
279int init_pervl_scs(struct hfi1_devdata *dd);
280struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
281 uint hdrqentsize, int numa);
282void sc_free(struct send_context *sc);
283int sc_enable(struct send_context *sc);
284void sc_disable(struct send_context *sc);
285int sc_restart(struct send_context *sc);
286void sc_return_credits(struct send_context *sc);
287void sc_flush(struct send_context *sc);
288void sc_drop(struct send_context *sc);
289void sc_stop(struct send_context *sc, int bit);
290struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
Jubin John17fb4f22016-02-14 20:21:52 -0800291 pio_release_cb cb, void *arg);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400292void sc_release_update(struct send_context *sc);
293void sc_return_credits(struct send_context *sc);
294void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context);
295void sc_add_credit_return_intr(struct send_context *sc);
296void sc_del_credit_return_intr(struct send_context *sc);
297void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold);
Jianxin Xiong44306f12016-04-12 11:30:28 -0700298u32 sc_percent_to_threshold(struct send_context *sc, u32 percent);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400299u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize);
300void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint);
301void sc_wait(struct hfi1_devdata *dd);
302void set_pio_integrity(struct send_context *sc);
303
304/* support functions */
305void pio_reset_all(struct hfi1_devdata *dd);
306void pio_freeze(struct hfi1_devdata *dd);
307void pio_kernel_unfreeze(struct hfi1_devdata *dd);
308
309/* global PIO send control operations */
310#define PSC_GLOBAL_ENABLE 0
311#define PSC_GLOBAL_DISABLE 1
312#define PSC_GLOBAL_VLARB_ENABLE 2
313#define PSC_GLOBAL_VLARB_DISABLE 3
314#define PSC_CM_RESET 4
315#define PSC_DATA_VL_ENABLE 5
316#define PSC_DATA_VL_DISABLE 6
317
318void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl);
319void pio_send_control(struct hfi1_devdata *dd, int op);
320
Mike Marciniszyn77241052015-07-30 15:17:43 -0400321/* PIO copy routines */
322void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc,
323 const void *from, size_t count);
324void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc,
Jubin John17fb4f22016-02-14 20:21:52 -0800325 const void *from, size_t nbytes);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400326void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes);
327void seg_pio_copy_end(struct pio_buf *pbuf);
328
329#endif /* _PIO_H */