1#ifndef _HFI1_EXP_RCV_H
2#define _HFI1_EXP_RCV_H
3/*
4 * Copyright(c) 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
50#include "hfi.h"
51
52#define EXP_TID_SET_EMPTY(set) (set.count == 0 && list_empty(&set.list))
53
54#define EXP_TID_TIDLEN_MASK   0x7FFULL
55#define EXP_TID_TIDLEN_SHIFT  0
56#define EXP_TID_TIDCTRL_MASK  0x3ULL
57#define EXP_TID_TIDCTRL_SHIFT 20
58#define EXP_TID_TIDIDX_MASK   0x3FFULL
59#define EXP_TID_TIDIDX_SHIFT  22
60#define EXP_TID_GET(tid, field)	\
61	(((tid) >> EXP_TID_TID##field##_SHIFT) & EXP_TID_TID##field##_MASK)
62
63#define EXP_TID_SET(field, value)			\
64	(((value) & EXP_TID_TID##field##_MASK) <<	\
65	 EXP_TID_TID##field##_SHIFT)
66#define EXP_TID_CLEAR(tid, field) ({					\
67		(tid) &= ~(EXP_TID_TID##field##_MASK <<			\
68			   EXP_TID_TID##field##_SHIFT);			\
69		})
70#define EXP_TID_RESET(tid, field, value) do {				\
71		EXP_TID_CLEAR(tid, field);				\
72		(tid) |= EXP_TID_SET(field, (value));			\
73	} while (0)
74
75/*
76 * Define fields in the KDETH header so we can update the header
77 * template.
78 */
79#define KDETH_OFFSET_SHIFT        0
80#define KDETH_OFFSET_MASK         0x7fff
81#define KDETH_OM_SHIFT            15
82#define KDETH_OM_MASK             0x1
83#define KDETH_TID_SHIFT           16
84#define KDETH_TID_MASK            0x3ff
85#define KDETH_TIDCTRL_SHIFT       26
86#define KDETH_TIDCTRL_MASK        0x3
87#define KDETH_INTR_SHIFT          28
88#define KDETH_INTR_MASK           0x1
89#define KDETH_SH_SHIFT            29
90#define KDETH_SH_MASK             0x1
91#define KDETH_KVER_SHIFT          30
92#define KDETH_KVER_MASK           0x3
93#define KDETH_JKEY_SHIFT          0x0
94#define KDETH_JKEY_MASK           0xff
95#define KDETH_HCRC_UPPER_SHIFT    16
96#define KDETH_HCRC_UPPER_MASK     0xff
97#define KDETH_HCRC_LOWER_SHIFT    24
98#define KDETH_HCRC_LOWER_MASK     0xff
99
100#define KDETH_GET(val, field)						\
101	(((le32_to_cpu((val))) >> KDETH_##field##_SHIFT) & KDETH_##field##_MASK)
102#define KDETH_SET(dw, field, val) do {					\
103		u32 dwval = le32_to_cpu(dw);				\
104		dwval &= ~(KDETH_##field##_MASK << KDETH_##field##_SHIFT); \
105		dwval |= (((val) & KDETH_##field##_MASK) << \
106			  KDETH_##field##_SHIFT);			\
107		dw = cpu_to_le32(dwval);				\
108	} while (0)
109
110#define KDETH_RESET(dw, field, val) ({ dw = 0; KDETH_SET(dw, field, val); })
111
112/* KDETH OM multipliers and switch over point */
113#define KDETH_OM_SMALL     4
114#define KDETH_OM_SMALL_SHIFT     2
115#define KDETH_OM_LARGE     64
116#define KDETH_OM_LARGE_SHIFT     6
117#define KDETH_OM_MAX_SIZE  (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1))
118
119struct tid_group {
120	struct list_head list;
121	u32 base;
122	u8 size;
123	u8 used;
124	u8 map;
125};
126
127/*
128 * Write an "empty" RcvArray entry.
129 * This function exists so the TID registaration code can use it
130 * to write to unused/unneeded entries and still take advantage
131 * of the WC performance improvements. The HFI will ignore this
132 * write to the RcvArray entry.
133 */
134static inline void rcv_array_wc_fill(struct hfi1_devdata *dd, u32 index)
135{
136	/*
137	 * Doing the WC fill writes only makes sense if the device is
138	 * present and the RcvArray has been mapped as WC memory.
139	 */
140	if ((dd->flags & HFI1_PRESENT) && dd->rcvarray_wc) {
141		writeq(0, dd->rcvarray_wc + (index * 8));
142		if ((index & 3) == 3)
143			flush_wc();
144	}
145}
146
147static inline void tid_group_add_tail(struct tid_group *grp,
148				      struct exp_tid_set *set)
149{
150	list_add_tail(&grp->list, &set->list);
151	set->count++;
152}
153
154static inline void tid_group_remove(struct tid_group *grp,
155				    struct exp_tid_set *set)
156{
157	list_del_init(&grp->list);
158	set->count--;
159}
160
161static inline void tid_group_move(struct tid_group *group,
162				  struct exp_tid_set *s1,
163				  struct exp_tid_set *s2)
164{
165	tid_group_remove(group, s1);
166	tid_group_add_tail(group, s2);
167}
168
169static inline struct tid_group *tid_group_pop(struct exp_tid_set *set)
170{
171	struct tid_group *grp =
172		list_first_entry(&set->list, struct tid_group, list);
173	list_del_init(&grp->list);
174	set->count--;
175	return grp;
176}
177
178static inline u32 rcventry2tidinfo(u32 rcventry)
179{
180	u32 pair = rcventry & ~0x1;
181
182	return EXP_TID_SET(IDX, pair >> 1) |
183		EXP_TID_SET(CTRL, 1 << (rcventry - pair));
184}
185
186/**
187 * hfi1_tid_group_to_idx - convert an index to a group
188 * @rcd - the receive context
189 * @grp - the group pointer
190 */
191static inline u16
192hfi1_tid_group_to_idx(struct hfi1_ctxtdata *rcd, struct tid_group *grp)
193{
194	return grp - &rcd->groups[0];
195}
196
197/**
198 * hfi1_idx_to_tid_group - convert a group to an index
199 * @rcd - the receive context
200 * @idx - the index
201 */
202static inline struct tid_group *
203hfi1_idx_to_tid_group(struct hfi1_ctxtdata *rcd, u16 idx)
204{
205	return &rcd->groups[idx];
206}
207
208int hfi1_alloc_ctxt_rcv_groups(struct hfi1_ctxtdata *rcd);
209void hfi1_free_ctxt_rcv_groups(struct hfi1_ctxtdata *rcd);
210void hfi1_exp_tid_group_init(struct hfi1_ctxtdata *rcd);
211
212#endif /* _HFI1_EXP_RCV_H */
213