xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/hfi1/hfi.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #ifndef _HFI1_KERNEL_H
2*4882a593Smuzhiyun #define _HFI1_KERNEL_H
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun  * Copyright(c) 2020 Cornelis Networks, Inc.
5*4882a593Smuzhiyun  * Copyright(c) 2015-2020 Intel Corporation.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
8*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
13*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
14*4882a593Smuzhiyun  * published by the Free Software Foundation.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
17*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
18*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19*4882a593Smuzhiyun  * General Public License for more details.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * BSD LICENSE
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
24*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
25*4882a593Smuzhiyun  * are met:
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  *  - Redistributions of source code must retain the above copyright
28*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
29*4882a593Smuzhiyun  *  - Redistributions in binary form must reproduce the above copyright
30*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
31*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
32*4882a593Smuzhiyun  *    distribution.
33*4882a593Smuzhiyun  *  - Neither the name of Intel Corporation nor the names of its
34*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
35*4882a593Smuzhiyun  *    from this software without specific prior written permission.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include <linux/interrupt.h>
52*4882a593Smuzhiyun #include <linux/pci.h>
53*4882a593Smuzhiyun #include <linux/dma-mapping.h>
54*4882a593Smuzhiyun #include <linux/mutex.h>
55*4882a593Smuzhiyun #include <linux/list.h>
56*4882a593Smuzhiyun #include <linux/scatterlist.h>
57*4882a593Smuzhiyun #include <linux/slab.h>
58*4882a593Smuzhiyun #include <linux/io.h>
59*4882a593Smuzhiyun #include <linux/fs.h>
60*4882a593Smuzhiyun #include <linux/completion.h>
61*4882a593Smuzhiyun #include <linux/kref.h>
62*4882a593Smuzhiyun #include <linux/sched.h>
63*4882a593Smuzhiyun #include <linux/cdev.h>
64*4882a593Smuzhiyun #include <linux/delay.h>
65*4882a593Smuzhiyun #include <linux/kthread.h>
66*4882a593Smuzhiyun #include <linux/i2c.h>
67*4882a593Smuzhiyun #include <linux/i2c-algo-bit.h>
68*4882a593Smuzhiyun #include <linux/xarray.h>
69*4882a593Smuzhiyun #include <rdma/ib_hdrs.h>
70*4882a593Smuzhiyun #include <rdma/opa_addr.h>
71*4882a593Smuzhiyun #include <linux/rhashtable.h>
72*4882a593Smuzhiyun #include <linux/netdevice.h>
73*4882a593Smuzhiyun #include <rdma/rdma_vt.h>
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #include "chip_registers.h"
76*4882a593Smuzhiyun #include "common.h"
77*4882a593Smuzhiyun #include "opfn.h"
78*4882a593Smuzhiyun #include "verbs.h"
79*4882a593Smuzhiyun #include "pio.h"
80*4882a593Smuzhiyun #include "chip.h"
81*4882a593Smuzhiyun #include "mad.h"
82*4882a593Smuzhiyun #include "qsfp.h"
83*4882a593Smuzhiyun #include "platform.h"
84*4882a593Smuzhiyun #include "affinity.h"
85*4882a593Smuzhiyun #include "msix.h"
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* bumped 1 from s/w major version of TrueScale */
88*4882a593Smuzhiyun #define HFI1_CHIP_VERS_MAJ 3U
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* don't care about this except printing */
91*4882a593Smuzhiyun #define HFI1_CHIP_VERS_MIN 0U
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /* The Organization Unique Identifier (Mfg code), and its position in GUID */
94*4882a593Smuzhiyun #define HFI1_OUI 0x001175
95*4882a593Smuzhiyun #define HFI1_OUI_LSB 40
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define DROP_PACKET_OFF		0
98*4882a593Smuzhiyun #define DROP_PACKET_ON		1
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define NEIGHBOR_TYPE_HFI		0
101*4882a593Smuzhiyun #define NEIGHBOR_TYPE_SWITCH	1
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #define HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES 5
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun extern unsigned long hfi1_cap_mask;
106*4882a593Smuzhiyun #define HFI1_CAP_KGET_MASK(mask, cap) ((mask) & HFI1_CAP_##cap)
107*4882a593Smuzhiyun #define HFI1_CAP_UGET_MASK(mask, cap) \
108*4882a593Smuzhiyun 	(((mask) >> HFI1_CAP_USER_SHIFT) & HFI1_CAP_##cap)
109*4882a593Smuzhiyun #define HFI1_CAP_KGET(cap) (HFI1_CAP_KGET_MASK(hfi1_cap_mask, cap))
110*4882a593Smuzhiyun #define HFI1_CAP_UGET(cap) (HFI1_CAP_UGET_MASK(hfi1_cap_mask, cap))
111*4882a593Smuzhiyun #define HFI1_CAP_IS_KSET(cap) (!!HFI1_CAP_KGET(cap))
112*4882a593Smuzhiyun #define HFI1_CAP_IS_USET(cap) (!!HFI1_CAP_UGET(cap))
113*4882a593Smuzhiyun #define HFI1_MISC_GET() ((hfi1_cap_mask >> HFI1_CAP_MISC_SHIFT) & \
114*4882a593Smuzhiyun 			HFI1_CAP_MISC_MASK)
115*4882a593Smuzhiyun /* Offline Disabled Reason is 4-bits */
116*4882a593Smuzhiyun #define HFI1_ODR_MASK(rsn) ((rsn) & OPA_PI_MASK_OFFLINE_REASON)
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun  * Control context is always 0 and handles the error packets.
120*4882a593Smuzhiyun  * It also handles the VL15 and multicast packets.
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun #define HFI1_CTRL_CTXT    0
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun  * Driver context will store software counters for each of the events
126*4882a593Smuzhiyun  * associated with these status registers
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun #define NUM_CCE_ERR_STATUS_COUNTERS 41
129*4882a593Smuzhiyun #define NUM_RCV_ERR_STATUS_COUNTERS 64
130*4882a593Smuzhiyun #define NUM_MISC_ERR_STATUS_COUNTERS 13
131*4882a593Smuzhiyun #define NUM_SEND_PIO_ERR_STATUS_COUNTERS 36
132*4882a593Smuzhiyun #define NUM_SEND_DMA_ERR_STATUS_COUNTERS 4
133*4882a593Smuzhiyun #define NUM_SEND_EGRESS_ERR_STATUS_COUNTERS 64
134*4882a593Smuzhiyun #define NUM_SEND_ERR_STATUS_COUNTERS 3
135*4882a593Smuzhiyun #define NUM_SEND_CTXT_ERR_STATUS_COUNTERS 5
136*4882a593Smuzhiyun #define NUM_SEND_DMA_ENG_ERR_STATUS_COUNTERS 24
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * per driver stats, either not device nor port-specific, or
140*4882a593Smuzhiyun  * summed over all of the devices and ports.
141*4882a593Smuzhiyun  * They are described by name via ipathfs filesystem, so layout
142*4882a593Smuzhiyun  * and number of elements can change without breaking compatibility.
143*4882a593Smuzhiyun  * If members are added or deleted hfi1_statnames[] in debugfs.c must
144*4882a593Smuzhiyun  * change to match.
145*4882a593Smuzhiyun  */
146*4882a593Smuzhiyun struct hfi1_ib_stats {
147*4882a593Smuzhiyun 	__u64 sps_ints; /* number of interrupts handled */
148*4882a593Smuzhiyun 	__u64 sps_errints; /* number of error interrupts */
149*4882a593Smuzhiyun 	__u64 sps_txerrs; /* tx-related packet errors */
150*4882a593Smuzhiyun 	__u64 sps_rcverrs; /* non-crc rcv packet errors */
151*4882a593Smuzhiyun 	__u64 sps_hwerrs; /* hardware errors reported (parity, etc.) */
152*4882a593Smuzhiyun 	__u64 sps_nopiobufs; /* no pio bufs avail from kernel */
153*4882a593Smuzhiyun 	__u64 sps_ctxts; /* number of contexts currently open */
154*4882a593Smuzhiyun 	__u64 sps_lenerrs; /* number of kernel packets where RHF != LRH len */
155*4882a593Smuzhiyun 	__u64 sps_buffull;
156*4882a593Smuzhiyun 	__u64 sps_hdrfull;
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun extern struct hfi1_ib_stats hfi1_stats;
160*4882a593Smuzhiyun extern const struct pci_error_handlers hfi1_pci_err_handler;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun extern int num_driver_cntrs;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun  * First-cut criterion for "device is active" is
166*4882a593Smuzhiyun  * two thousand dwords combined Tx, Rx traffic per
167*4882a593Smuzhiyun  * 5-second interval. SMA packets are 64 dwords,
168*4882a593Smuzhiyun  * and occur "a few per second", presumably each way.
169*4882a593Smuzhiyun  */
170*4882a593Smuzhiyun #define HFI1_TRAFFIC_ACTIVE_THRESHOLD (2000)
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /*
173*4882a593Smuzhiyun  * Below contains all data related to a single context (formerly called port).
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun struct hfi1_opcode_stats_perctx;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun struct ctxt_eager_bufs {
179*4882a593Smuzhiyun 	struct eager_buffer {
180*4882a593Smuzhiyun 		void *addr;
181*4882a593Smuzhiyun 		dma_addr_t dma;
182*4882a593Smuzhiyun 		ssize_t len;
183*4882a593Smuzhiyun 	} *buffers;
184*4882a593Smuzhiyun 	struct {
185*4882a593Smuzhiyun 		void *addr;
186*4882a593Smuzhiyun 		dma_addr_t dma;
187*4882a593Smuzhiyun 	} *rcvtids;
188*4882a593Smuzhiyun 	u32 size;                /* total size of eager buffers */
189*4882a593Smuzhiyun 	u32 rcvtid_size;         /* size of each eager rcv tid */
190*4882a593Smuzhiyun 	u16 count;               /* size of buffers array */
191*4882a593Smuzhiyun 	u16 numbufs;             /* number of buffers allocated */
192*4882a593Smuzhiyun 	u16 alloced;             /* number of rcvarray entries used */
193*4882a593Smuzhiyun 	u16 threshold;           /* head update threshold */
194*4882a593Smuzhiyun };
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun struct exp_tid_set {
197*4882a593Smuzhiyun 	struct list_head list;
198*4882a593Smuzhiyun 	u32 count;
199*4882a593Smuzhiyun };
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun struct hfi1_ctxtdata;
202*4882a593Smuzhiyun typedef int (*intr_handler)(struct hfi1_ctxtdata *rcd, int data);
203*4882a593Smuzhiyun typedef void (*rhf_rcv_function_ptr)(struct hfi1_packet *packet);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun struct tid_queue {
206*4882a593Smuzhiyun 	struct list_head queue_head;
207*4882a593Smuzhiyun 			/* queue head for QP TID resource waiters */
208*4882a593Smuzhiyun 	u32 enqueue;	/* count of tid enqueues */
209*4882a593Smuzhiyun 	u32 dequeue;	/* count of tid dequeues */
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun struct hfi1_ctxtdata {
213*4882a593Smuzhiyun 	/* rcvhdrq base, needs mmap before useful */
214*4882a593Smuzhiyun 	void *rcvhdrq;
215*4882a593Smuzhiyun 	/* kernel virtual address where hdrqtail is updated */
216*4882a593Smuzhiyun 	volatile __le64 *rcvhdrtail_kvaddr;
217*4882a593Smuzhiyun 	/* so functions that need physical port can get it easily */
218*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
219*4882a593Smuzhiyun 	/* so file ops can get at unit */
220*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
221*4882a593Smuzhiyun 	/* this receive context's assigned PIO ACK send context */
222*4882a593Smuzhiyun 	struct send_context *sc;
223*4882a593Smuzhiyun 	/* per context recv functions */
224*4882a593Smuzhiyun 	const rhf_rcv_function_ptr *rhf_rcv_function_map;
225*4882a593Smuzhiyun 	/*
226*4882a593Smuzhiyun 	 * The interrupt handler for a particular receive context can vary
227*4882a593Smuzhiyun 	 * throughout it's lifetime. This is not a lock protected data member so
228*4882a593Smuzhiyun 	 * it must be updated atomically and the prev and new value must always
229*4882a593Smuzhiyun 	 * be valid. Worst case is we process an extra interrupt and up to 64
230*4882a593Smuzhiyun 	 * packets with the wrong interrupt handler.
231*4882a593Smuzhiyun 	 */
232*4882a593Smuzhiyun 	intr_handler do_interrupt;
233*4882a593Smuzhiyun 	/** fast handler after autoactive */
234*4882a593Smuzhiyun 	intr_handler fast_handler;
235*4882a593Smuzhiyun 	/** slow handler */
236*4882a593Smuzhiyun 	intr_handler slow_handler;
237*4882a593Smuzhiyun 	/* napi pointer assiociated with netdev */
238*4882a593Smuzhiyun 	struct napi_struct *napi;
239*4882a593Smuzhiyun 	/* verbs rx_stats per rcd */
240*4882a593Smuzhiyun 	struct hfi1_opcode_stats_perctx *opstats;
241*4882a593Smuzhiyun 	/* clear interrupt mask */
242*4882a593Smuzhiyun 	u64 imask;
243*4882a593Smuzhiyun 	/* ctxt rcvhdrq head offset */
244*4882a593Smuzhiyun 	u32 head;
245*4882a593Smuzhiyun 	/* number of rcvhdrq entries */
246*4882a593Smuzhiyun 	u16 rcvhdrq_cnt;
247*4882a593Smuzhiyun 	u8 ireg;	/* clear interrupt register */
248*4882a593Smuzhiyun 	/* receive packet sequence counter */
249*4882a593Smuzhiyun 	u8 seq_cnt;
250*4882a593Smuzhiyun 	/* size of each of the rcvhdrq entries */
251*4882a593Smuzhiyun 	u8 rcvhdrqentsize;
252*4882a593Smuzhiyun 	/* offset of RHF within receive header entry */
253*4882a593Smuzhiyun 	u8 rhf_offset;
254*4882a593Smuzhiyun 	/* dynamic receive available interrupt timeout */
255*4882a593Smuzhiyun 	u8 rcvavail_timeout;
256*4882a593Smuzhiyun 	/* Indicates that this is vnic context */
257*4882a593Smuzhiyun 	bool is_vnic;
258*4882a593Smuzhiyun 	/* vnic queue index this context is mapped to */
259*4882a593Smuzhiyun 	u8 vnic_q_idx;
260*4882a593Smuzhiyun 	/* Is ASPM interrupt supported for this context */
261*4882a593Smuzhiyun 	bool aspm_intr_supported;
262*4882a593Smuzhiyun 	/* ASPM state (enabled/disabled) for this context */
263*4882a593Smuzhiyun 	bool aspm_enabled;
264*4882a593Smuzhiyun 	/* Is ASPM processing enabled for this context (in intr context) */
265*4882a593Smuzhiyun 	bool aspm_intr_enable;
266*4882a593Smuzhiyun 	struct ctxt_eager_bufs egrbufs;
267*4882a593Smuzhiyun 	/* QPs waiting for context processing */
268*4882a593Smuzhiyun 	struct list_head qp_wait_list;
269*4882a593Smuzhiyun 	/* tid allocation lists */
270*4882a593Smuzhiyun 	struct exp_tid_set tid_group_list;
271*4882a593Smuzhiyun 	struct exp_tid_set tid_used_list;
272*4882a593Smuzhiyun 	struct exp_tid_set tid_full_list;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Timer for re-enabling ASPM if interrupt activity quiets down */
275*4882a593Smuzhiyun 	struct timer_list aspm_timer;
276*4882a593Smuzhiyun 	/* per-context configuration flags */
277*4882a593Smuzhiyun 	unsigned long flags;
278*4882a593Smuzhiyun 	/* array of tid_groups */
279*4882a593Smuzhiyun 	struct tid_group  *groups;
280*4882a593Smuzhiyun 	/* mmap of hdrq, must fit in 44 bits */
281*4882a593Smuzhiyun 	dma_addr_t rcvhdrq_dma;
282*4882a593Smuzhiyun 	dma_addr_t rcvhdrqtailaddr_dma;
283*4882a593Smuzhiyun 	/* Last interrupt timestamp */
284*4882a593Smuzhiyun 	ktime_t aspm_ts_last_intr;
285*4882a593Smuzhiyun 	/* Last timestamp at which we scheduled a timer for this context */
286*4882a593Smuzhiyun 	ktime_t aspm_ts_timer_sched;
287*4882a593Smuzhiyun 	/* Lock to serialize between intr, timer intr and user threads */
288*4882a593Smuzhiyun 	spinlock_t aspm_lock;
289*4882a593Smuzhiyun 	/* Reference count the base context usage */
290*4882a593Smuzhiyun 	struct kref kref;
291*4882a593Smuzhiyun 	/* numa node of this context */
292*4882a593Smuzhiyun 	int numa_id;
293*4882a593Smuzhiyun 	/* associated msix interrupt. */
294*4882a593Smuzhiyun 	s16 msix_intr;
295*4882a593Smuzhiyun 	/* job key */
296*4882a593Smuzhiyun 	u16 jkey;
297*4882a593Smuzhiyun 	/* number of RcvArray groups for this context. */
298*4882a593Smuzhiyun 	u16 rcv_array_groups;
299*4882a593Smuzhiyun 	/* index of first eager TID entry. */
300*4882a593Smuzhiyun 	u16 eager_base;
301*4882a593Smuzhiyun 	/* number of expected TID entries */
302*4882a593Smuzhiyun 	u16 expected_count;
303*4882a593Smuzhiyun 	/* index of first expected TID entry. */
304*4882a593Smuzhiyun 	u16 expected_base;
305*4882a593Smuzhiyun 	/* Device context index */
306*4882a593Smuzhiyun 	u8 ctxt;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/* PSM Specific fields */
309*4882a593Smuzhiyun 	/* lock protecting all Expected TID data */
310*4882a593Smuzhiyun 	struct mutex exp_mutex;
311*4882a593Smuzhiyun 	/* lock protecting all Expected TID data of kernel contexts */
312*4882a593Smuzhiyun 	spinlock_t exp_lock;
313*4882a593Smuzhiyun 	/* Queue for QP's waiting for HW TID flows */
314*4882a593Smuzhiyun 	struct tid_queue flow_queue;
315*4882a593Smuzhiyun 	/* Queue for QP's waiting for HW receive array entries */
316*4882a593Smuzhiyun 	struct tid_queue rarr_queue;
317*4882a593Smuzhiyun 	/* when waiting for rcv or pioavail */
318*4882a593Smuzhiyun 	wait_queue_head_t wait;
319*4882a593Smuzhiyun 	/* uuid from PSM */
320*4882a593Smuzhiyun 	u8 uuid[16];
321*4882a593Smuzhiyun 	/* same size as task_struct .comm[], command that opened context */
322*4882a593Smuzhiyun 	char comm[TASK_COMM_LEN];
323*4882a593Smuzhiyun 	/* Bitmask of in use context(s) */
324*4882a593Smuzhiyun 	DECLARE_BITMAP(in_use_ctxts, HFI1_MAX_SHARED_CTXTS);
325*4882a593Smuzhiyun 	/* per-context event flags for fileops/intr communication */
326*4882a593Smuzhiyun 	unsigned long event_flags;
327*4882a593Smuzhiyun 	/* A page of memory for rcvhdrhead, rcvegrhead, rcvegrtail * N */
328*4882a593Smuzhiyun 	void *subctxt_uregbase;
329*4882a593Smuzhiyun 	/* An array of pages for the eager receive buffers * N */
330*4882a593Smuzhiyun 	void *subctxt_rcvegrbuf;
331*4882a593Smuzhiyun 	/* An array of pages for the eager header queue entries * N */
332*4882a593Smuzhiyun 	void *subctxt_rcvhdr_base;
333*4882a593Smuzhiyun 	/* total number of polled urgent packets */
334*4882a593Smuzhiyun 	u32 urgent;
335*4882a593Smuzhiyun 	/* saved total number of polled urgent packets for poll edge trigger */
336*4882a593Smuzhiyun 	u32 urgent_poll;
337*4882a593Smuzhiyun 	/* Type of packets or conditions we want to poll for */
338*4882a593Smuzhiyun 	u16 poll_type;
339*4882a593Smuzhiyun 	/* non-zero if ctxt is being shared. */
340*4882a593Smuzhiyun 	u16 subctxt_id;
341*4882a593Smuzhiyun 	/* The version of the library which opened this ctxt */
342*4882a593Smuzhiyun 	u32 userversion;
343*4882a593Smuzhiyun 	/*
344*4882a593Smuzhiyun 	 * non-zero if ctxt can be shared, and defines the maximum number of
345*4882a593Smuzhiyun 	 * sub-contexts for this device context.
346*4882a593Smuzhiyun 	 */
347*4882a593Smuzhiyun 	u8 subctxt_cnt;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	/* Bit mask to track free TID RDMA HW flows */
350*4882a593Smuzhiyun 	unsigned long flow_mask;
351*4882a593Smuzhiyun 	struct tid_flow_state flows[RXE_NUM_TID_FLOWS];
352*4882a593Smuzhiyun };
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun /**
355*4882a593Smuzhiyun  * rcvhdrq_size - return total size in bytes for header queue
356*4882a593Smuzhiyun  * @rcd: the receive context
357*4882a593Smuzhiyun  *
358*4882a593Smuzhiyun  * rcvhdrqentsize is in DWs, so we have to convert to bytes
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  */
rcvhdrq_size(struct hfi1_ctxtdata * rcd)361*4882a593Smuzhiyun static inline u32 rcvhdrq_size(struct hfi1_ctxtdata *rcd)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	return PAGE_ALIGN(rcd->rcvhdrq_cnt *
364*4882a593Smuzhiyun 			  rcd->rcvhdrqentsize * sizeof(u32));
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun  * Represents a single packet at a high level. Put commonly computed things in
369*4882a593Smuzhiyun  * here so we do not have to keep doing them over and over. The rule of thumb is
370*4882a593Smuzhiyun  * if something is used one time to derive some value, store that something in
371*4882a593Smuzhiyun  * here. If it is used multiple times, then store the result of that derivation
372*4882a593Smuzhiyun  * in here.
373*4882a593Smuzhiyun  */
374*4882a593Smuzhiyun struct hfi1_packet {
375*4882a593Smuzhiyun 	void *ebuf;
376*4882a593Smuzhiyun 	void *hdr;
377*4882a593Smuzhiyun 	void *payload;
378*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd;
379*4882a593Smuzhiyun 	__le32 *rhf_addr;
380*4882a593Smuzhiyun 	struct rvt_qp *qp;
381*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
382*4882a593Smuzhiyun 	struct ib_grh *grh;
383*4882a593Smuzhiyun 	struct opa_16b_mgmt *mgmt;
384*4882a593Smuzhiyun 	u64 rhf;
385*4882a593Smuzhiyun 	u32 maxcnt;
386*4882a593Smuzhiyun 	u32 rhqoff;
387*4882a593Smuzhiyun 	u32 dlid;
388*4882a593Smuzhiyun 	u32 slid;
389*4882a593Smuzhiyun 	int numpkt;
390*4882a593Smuzhiyun 	u16 tlen;
391*4882a593Smuzhiyun 	s16 etail;
392*4882a593Smuzhiyun 	u16 pkey;
393*4882a593Smuzhiyun 	u8 hlen;
394*4882a593Smuzhiyun 	u8 rsize;
395*4882a593Smuzhiyun 	u8 updegr;
396*4882a593Smuzhiyun 	u8 etype;
397*4882a593Smuzhiyun 	u8 extra_byte;
398*4882a593Smuzhiyun 	u8 pad;
399*4882a593Smuzhiyun 	u8 sc;
400*4882a593Smuzhiyun 	u8 sl;
401*4882a593Smuzhiyun 	u8 opcode;
402*4882a593Smuzhiyun 	bool migrated;
403*4882a593Smuzhiyun };
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /* Packet types */
406*4882a593Smuzhiyun #define HFI1_PKT_TYPE_9B  0
407*4882a593Smuzhiyun #define HFI1_PKT_TYPE_16B 1
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun  * OPA 16B Header
411*4882a593Smuzhiyun  */
412*4882a593Smuzhiyun #define OPA_16B_L4_MASK		0xFFull
413*4882a593Smuzhiyun #define OPA_16B_SC_MASK		0x1F00000ull
414*4882a593Smuzhiyun #define OPA_16B_SC_SHIFT	20
415*4882a593Smuzhiyun #define OPA_16B_LID_MASK	0xFFFFFull
416*4882a593Smuzhiyun #define OPA_16B_DLID_MASK	0xF000ull
417*4882a593Smuzhiyun #define OPA_16B_DLID_SHIFT	20
418*4882a593Smuzhiyun #define OPA_16B_DLID_HIGH_SHIFT	12
419*4882a593Smuzhiyun #define OPA_16B_SLID_MASK	0xF00ull
420*4882a593Smuzhiyun #define OPA_16B_SLID_SHIFT	20
421*4882a593Smuzhiyun #define OPA_16B_SLID_HIGH_SHIFT	8
422*4882a593Smuzhiyun #define OPA_16B_BECN_MASK       0x80000000ull
423*4882a593Smuzhiyun #define OPA_16B_BECN_SHIFT      31
424*4882a593Smuzhiyun #define OPA_16B_FECN_MASK       0x10000000ull
425*4882a593Smuzhiyun #define OPA_16B_FECN_SHIFT      28
426*4882a593Smuzhiyun #define OPA_16B_L2_MASK		0x60000000ull
427*4882a593Smuzhiyun #define OPA_16B_L2_SHIFT	29
428*4882a593Smuzhiyun #define OPA_16B_PKEY_MASK	0xFFFF0000ull
429*4882a593Smuzhiyun #define OPA_16B_PKEY_SHIFT	16
430*4882a593Smuzhiyun #define OPA_16B_LEN_MASK	0x7FF00000ull
431*4882a593Smuzhiyun #define OPA_16B_LEN_SHIFT	20
432*4882a593Smuzhiyun #define OPA_16B_RC_MASK		0xE000000ull
433*4882a593Smuzhiyun #define OPA_16B_RC_SHIFT	25
434*4882a593Smuzhiyun #define OPA_16B_AGE_MASK	0xFF0000ull
435*4882a593Smuzhiyun #define OPA_16B_AGE_SHIFT	16
436*4882a593Smuzhiyun #define OPA_16B_ENTROPY_MASK	0xFFFFull
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun /*
439*4882a593Smuzhiyun  * OPA 16B L2/L4 Encodings
440*4882a593Smuzhiyun  */
441*4882a593Smuzhiyun #define OPA_16B_L4_9B		0x00
442*4882a593Smuzhiyun #define OPA_16B_L2_TYPE		0x02
443*4882a593Smuzhiyun #define OPA_16B_L4_FM		0x08
444*4882a593Smuzhiyun #define OPA_16B_L4_IB_LOCAL	0x09
445*4882a593Smuzhiyun #define OPA_16B_L4_IB_GLOBAL	0x0A
446*4882a593Smuzhiyun #define OPA_16B_L4_ETHR		OPA_VNIC_L4_ETHR
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun /*
449*4882a593Smuzhiyun  * OPA 16B Management
450*4882a593Smuzhiyun  */
451*4882a593Smuzhiyun #define OPA_16B_L4_FM_PAD	3  /* fixed 3B pad */
452*4882a593Smuzhiyun #define OPA_16B_L4_FM_HLEN	24 /* 16B(16) + L4_FM(8) */
453*4882a593Smuzhiyun 
hfi1_16B_get_l4(struct hfi1_16b_header * hdr)454*4882a593Smuzhiyun static inline u8 hfi1_16B_get_l4(struct hfi1_16b_header *hdr)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun 	return (u8)(hdr->lrh[2] & OPA_16B_L4_MASK);
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
hfi1_16B_get_sc(struct hfi1_16b_header * hdr)459*4882a593Smuzhiyun static inline u8 hfi1_16B_get_sc(struct hfi1_16b_header *hdr)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun 	return (u8)((hdr->lrh[1] & OPA_16B_SC_MASK) >> OPA_16B_SC_SHIFT);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
hfi1_16B_get_dlid(struct hfi1_16b_header * hdr)464*4882a593Smuzhiyun static inline u32 hfi1_16B_get_dlid(struct hfi1_16b_header *hdr)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	return (u32)((hdr->lrh[1] & OPA_16B_LID_MASK) |
467*4882a593Smuzhiyun 		     (((hdr->lrh[2] & OPA_16B_DLID_MASK) >>
468*4882a593Smuzhiyun 		     OPA_16B_DLID_HIGH_SHIFT) << OPA_16B_DLID_SHIFT));
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
hfi1_16B_get_slid(struct hfi1_16b_header * hdr)471*4882a593Smuzhiyun static inline u32 hfi1_16B_get_slid(struct hfi1_16b_header *hdr)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	return (u32)((hdr->lrh[0] & OPA_16B_LID_MASK) |
474*4882a593Smuzhiyun 		     (((hdr->lrh[2] & OPA_16B_SLID_MASK) >>
475*4882a593Smuzhiyun 		     OPA_16B_SLID_HIGH_SHIFT) << OPA_16B_SLID_SHIFT));
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
hfi1_16B_get_becn(struct hfi1_16b_header * hdr)478*4882a593Smuzhiyun static inline u8 hfi1_16B_get_becn(struct hfi1_16b_header *hdr)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun 	return (u8)((hdr->lrh[0] & OPA_16B_BECN_MASK) >> OPA_16B_BECN_SHIFT);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun 
hfi1_16B_get_fecn(struct hfi1_16b_header * hdr)483*4882a593Smuzhiyun static inline u8 hfi1_16B_get_fecn(struct hfi1_16b_header *hdr)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	return (u8)((hdr->lrh[1] & OPA_16B_FECN_MASK) >> OPA_16B_FECN_SHIFT);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun 
hfi1_16B_get_l2(struct hfi1_16b_header * hdr)488*4882a593Smuzhiyun static inline u8 hfi1_16B_get_l2(struct hfi1_16b_header *hdr)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun 	return (u8)((hdr->lrh[1] & OPA_16B_L2_MASK) >> OPA_16B_L2_SHIFT);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
hfi1_16B_get_pkey(struct hfi1_16b_header * hdr)493*4882a593Smuzhiyun static inline u16 hfi1_16B_get_pkey(struct hfi1_16b_header *hdr)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	return (u16)((hdr->lrh[2] & OPA_16B_PKEY_MASK) >> OPA_16B_PKEY_SHIFT);
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
hfi1_16B_get_rc(struct hfi1_16b_header * hdr)498*4882a593Smuzhiyun static inline u8 hfi1_16B_get_rc(struct hfi1_16b_header *hdr)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun 	return (u8)((hdr->lrh[1] & OPA_16B_RC_MASK) >> OPA_16B_RC_SHIFT);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
hfi1_16B_get_age(struct hfi1_16b_header * hdr)503*4882a593Smuzhiyun static inline u8 hfi1_16B_get_age(struct hfi1_16b_header *hdr)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	return (u8)((hdr->lrh[3] & OPA_16B_AGE_MASK) >> OPA_16B_AGE_SHIFT);
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
hfi1_16B_get_len(struct hfi1_16b_header * hdr)508*4882a593Smuzhiyun static inline u16 hfi1_16B_get_len(struct hfi1_16b_header *hdr)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	return (u16)((hdr->lrh[0] & OPA_16B_LEN_MASK) >> OPA_16B_LEN_SHIFT);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
hfi1_16B_get_entropy(struct hfi1_16b_header * hdr)513*4882a593Smuzhiyun static inline u16 hfi1_16B_get_entropy(struct hfi1_16b_header *hdr)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	return (u16)(hdr->lrh[3] & OPA_16B_ENTROPY_MASK);
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun #define OPA_16B_MAKE_QW(low_dw, high_dw) (((u64)(high_dw) << 32) | (low_dw))
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun /*
521*4882a593Smuzhiyun  * BTH
522*4882a593Smuzhiyun  */
523*4882a593Smuzhiyun #define OPA_16B_BTH_PAD_MASK	7
hfi1_16B_bth_get_pad(struct ib_other_headers * ohdr)524*4882a593Smuzhiyun static inline u8 hfi1_16B_bth_get_pad(struct ib_other_headers *ohdr)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun 	return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_PAD_SHIFT) &
527*4882a593Smuzhiyun 		   OPA_16B_BTH_PAD_MASK);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /*
531*4882a593Smuzhiyun  * 16B Management
532*4882a593Smuzhiyun  */
533*4882a593Smuzhiyun #define OPA_16B_MGMT_QPN_MASK	0xFFFFFF
hfi1_16B_get_dest_qpn(struct opa_16b_mgmt * mgmt)534*4882a593Smuzhiyun static inline u32 hfi1_16B_get_dest_qpn(struct opa_16b_mgmt *mgmt)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	return be32_to_cpu(mgmt->dest_qpn) & OPA_16B_MGMT_QPN_MASK;
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun 
hfi1_16B_get_src_qpn(struct opa_16b_mgmt * mgmt)539*4882a593Smuzhiyun static inline u32 hfi1_16B_get_src_qpn(struct opa_16b_mgmt *mgmt)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun 	return be32_to_cpu(mgmt->src_qpn) & OPA_16B_MGMT_QPN_MASK;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun 
hfi1_16B_set_qpn(struct opa_16b_mgmt * mgmt,u32 dest_qp,u32 src_qp)544*4882a593Smuzhiyun static inline void hfi1_16B_set_qpn(struct opa_16b_mgmt *mgmt,
545*4882a593Smuzhiyun 				    u32 dest_qp, u32 src_qp)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	mgmt->dest_qpn = cpu_to_be32(dest_qp & OPA_16B_MGMT_QPN_MASK);
548*4882a593Smuzhiyun 	mgmt->src_qpn = cpu_to_be32(src_qp & OPA_16B_MGMT_QPN_MASK);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun /**
552*4882a593Smuzhiyun  * hfi1_get_rc_ohdr - get extended header
553*4882a593Smuzhiyun  * @opah - the opaheader
554*4882a593Smuzhiyun  */
555*4882a593Smuzhiyun static inline struct ib_other_headers *
hfi1_get_rc_ohdr(struct hfi1_opa_header * opah)556*4882a593Smuzhiyun hfi1_get_rc_ohdr(struct hfi1_opa_header *opah)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
559*4882a593Smuzhiyun 	struct ib_header *hdr = NULL;
560*4882a593Smuzhiyun 	struct hfi1_16b_header *hdr_16b = NULL;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	/* Find out where the BTH is */
563*4882a593Smuzhiyun 	if (opah->hdr_type == HFI1_PKT_TYPE_9B) {
564*4882a593Smuzhiyun 		hdr = &opah->ibh;
565*4882a593Smuzhiyun 		if (ib_get_lnh(hdr) == HFI1_LRH_BTH)
566*4882a593Smuzhiyun 			ohdr = &hdr->u.oth;
567*4882a593Smuzhiyun 		else
568*4882a593Smuzhiyun 			ohdr = &hdr->u.l.oth;
569*4882a593Smuzhiyun 	} else {
570*4882a593Smuzhiyun 		u8 l4;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 		hdr_16b = &opah->opah;
573*4882a593Smuzhiyun 		l4  = hfi1_16B_get_l4(hdr_16b);
574*4882a593Smuzhiyun 		if (l4 == OPA_16B_L4_IB_LOCAL)
575*4882a593Smuzhiyun 			ohdr = &hdr_16b->u.oth;
576*4882a593Smuzhiyun 		else
577*4882a593Smuzhiyun 			ohdr = &hdr_16b->u.l.oth;
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 	return ohdr;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun struct rvt_sge_state;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun /*
585*4882a593Smuzhiyun  * Get/Set IB link-level config parameters for f_get/set_ib_cfg()
586*4882a593Smuzhiyun  * Mostly for MADs that set or query link parameters, also ipath
587*4882a593Smuzhiyun  * config interfaces
588*4882a593Smuzhiyun  */
589*4882a593Smuzhiyun #define HFI1_IB_CFG_LIDLMC 0 /* LID (LS16b) and Mask (MS16b) */
590*4882a593Smuzhiyun #define HFI1_IB_CFG_LWID_DG_ENB 1 /* allowed Link-width downgrade */
591*4882a593Smuzhiyun #define HFI1_IB_CFG_LWID_ENB 2 /* allowed Link-width */
592*4882a593Smuzhiyun #define HFI1_IB_CFG_LWID 3 /* currently active Link-width */
593*4882a593Smuzhiyun #define HFI1_IB_CFG_SPD_ENB 4 /* allowed Link speeds */
594*4882a593Smuzhiyun #define HFI1_IB_CFG_SPD 5 /* current Link spd */
595*4882a593Smuzhiyun #define HFI1_IB_CFG_RXPOL_ENB 6 /* Auto-RX-polarity enable */
596*4882a593Smuzhiyun #define HFI1_IB_CFG_LREV_ENB 7 /* Auto-Lane-reversal enable */
597*4882a593Smuzhiyun #define HFI1_IB_CFG_LINKLATENCY 8 /* Link Latency (IB1.2 only) */
598*4882a593Smuzhiyun #define HFI1_IB_CFG_HRTBT 9 /* IB heartbeat off/enable/auto; DDR/QDR only */
599*4882a593Smuzhiyun #define HFI1_IB_CFG_OP_VLS 10 /* operational VLs */
600*4882a593Smuzhiyun #define HFI1_IB_CFG_VL_HIGH_CAP 11 /* num of VL high priority weights */
601*4882a593Smuzhiyun #define HFI1_IB_CFG_VL_LOW_CAP 12 /* num of VL low priority weights */
602*4882a593Smuzhiyun #define HFI1_IB_CFG_OVERRUN_THRESH 13 /* IB overrun threshold */
603*4882a593Smuzhiyun #define HFI1_IB_CFG_PHYERR_THRESH 14 /* IB PHY error threshold */
604*4882a593Smuzhiyun #define HFI1_IB_CFG_LINKDEFAULT 15 /* IB link default (sleep/poll) */
605*4882a593Smuzhiyun #define HFI1_IB_CFG_PKEYS 16 /* update partition keys */
606*4882a593Smuzhiyun #define HFI1_IB_CFG_MTU 17 /* update MTU in IBC */
607*4882a593Smuzhiyun #define HFI1_IB_CFG_VL_HIGH_LIMIT 19
608*4882a593Smuzhiyun #define HFI1_IB_CFG_PMA_TICKS 20 /* PMA sample tick resolution */
609*4882a593Smuzhiyun #define HFI1_IB_CFG_PORT 21 /* switch port we are connected to */
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun /*
612*4882a593Smuzhiyun  * HFI or Host Link States
613*4882a593Smuzhiyun  *
614*4882a593Smuzhiyun  * These describe the states the driver thinks the logical and physical
615*4882a593Smuzhiyun  * states are in.  Used as an argument to set_link_state().  Implemented
616*4882a593Smuzhiyun  * as bits for easy multi-state checking.  The actual state can only be
617*4882a593Smuzhiyun  * one.
618*4882a593Smuzhiyun  */
619*4882a593Smuzhiyun #define __HLS_UP_INIT_BP	0
620*4882a593Smuzhiyun #define __HLS_UP_ARMED_BP	1
621*4882a593Smuzhiyun #define __HLS_UP_ACTIVE_BP	2
622*4882a593Smuzhiyun #define __HLS_DN_DOWNDEF_BP	3	/* link down default */
623*4882a593Smuzhiyun #define __HLS_DN_POLL_BP	4
624*4882a593Smuzhiyun #define __HLS_DN_DISABLE_BP	5
625*4882a593Smuzhiyun #define __HLS_DN_OFFLINE_BP	6
626*4882a593Smuzhiyun #define __HLS_VERIFY_CAP_BP	7
627*4882a593Smuzhiyun #define __HLS_GOING_UP_BP	8
628*4882a593Smuzhiyun #define __HLS_GOING_OFFLINE_BP  9
629*4882a593Smuzhiyun #define __HLS_LINK_COOLDOWN_BP 10
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun #define HLS_UP_INIT	  BIT(__HLS_UP_INIT_BP)
632*4882a593Smuzhiyun #define HLS_UP_ARMED	  BIT(__HLS_UP_ARMED_BP)
633*4882a593Smuzhiyun #define HLS_UP_ACTIVE	  BIT(__HLS_UP_ACTIVE_BP)
634*4882a593Smuzhiyun #define HLS_DN_DOWNDEF	  BIT(__HLS_DN_DOWNDEF_BP) /* link down default */
635*4882a593Smuzhiyun #define HLS_DN_POLL	  BIT(__HLS_DN_POLL_BP)
636*4882a593Smuzhiyun #define HLS_DN_DISABLE	  BIT(__HLS_DN_DISABLE_BP)
637*4882a593Smuzhiyun #define HLS_DN_OFFLINE	  BIT(__HLS_DN_OFFLINE_BP)
638*4882a593Smuzhiyun #define HLS_VERIFY_CAP	  BIT(__HLS_VERIFY_CAP_BP)
639*4882a593Smuzhiyun #define HLS_GOING_UP	  BIT(__HLS_GOING_UP_BP)
640*4882a593Smuzhiyun #define HLS_GOING_OFFLINE BIT(__HLS_GOING_OFFLINE_BP)
641*4882a593Smuzhiyun #define HLS_LINK_COOLDOWN BIT(__HLS_LINK_COOLDOWN_BP)
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun #define HLS_UP (HLS_UP_INIT | HLS_UP_ARMED | HLS_UP_ACTIVE)
644*4882a593Smuzhiyun #define HLS_DOWN ~(HLS_UP)
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun #define HLS_DEFAULT HLS_DN_POLL
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun /* use this MTU size if none other is given */
649*4882a593Smuzhiyun #define HFI1_DEFAULT_ACTIVE_MTU 10240
650*4882a593Smuzhiyun /* use this MTU size as the default maximum */
651*4882a593Smuzhiyun #define HFI1_DEFAULT_MAX_MTU 10240
652*4882a593Smuzhiyun /* default partition key */
653*4882a593Smuzhiyun #define DEFAULT_PKEY 0xffff
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun /*
656*4882a593Smuzhiyun  * Possible fabric manager config parameters for fm_{get,set}_table()
657*4882a593Smuzhiyun  */
658*4882a593Smuzhiyun #define FM_TBL_VL_HIGH_ARB		1 /* Get/set VL high prio weights */
659*4882a593Smuzhiyun #define FM_TBL_VL_LOW_ARB		2 /* Get/set VL low prio weights */
660*4882a593Smuzhiyun #define FM_TBL_BUFFER_CONTROL		3 /* Get/set Buffer Control */
661*4882a593Smuzhiyun #define FM_TBL_SC2VLNT			4 /* Get/set SC->VLnt */
662*4882a593Smuzhiyun #define FM_TBL_VL_PREEMPT_ELEMS		5 /* Get (no set) VL preempt elems */
663*4882a593Smuzhiyun #define FM_TBL_VL_PREEMPT_MATRIX	6 /* Get (no set) VL preempt matrix */
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun /*
666*4882a593Smuzhiyun  * Possible "operations" for f_rcvctrl(ppd, op, ctxt)
667*4882a593Smuzhiyun  * these are bits so they can be combined, e.g.
668*4882a593Smuzhiyun  * HFI1_RCVCTRL_INTRAVAIL_ENB | HFI1_RCVCTRL_CTXT_ENB
669*4882a593Smuzhiyun  */
670*4882a593Smuzhiyun #define HFI1_RCVCTRL_TAILUPD_ENB 0x01
671*4882a593Smuzhiyun #define HFI1_RCVCTRL_TAILUPD_DIS 0x02
672*4882a593Smuzhiyun #define HFI1_RCVCTRL_CTXT_ENB 0x04
673*4882a593Smuzhiyun #define HFI1_RCVCTRL_CTXT_DIS 0x08
674*4882a593Smuzhiyun #define HFI1_RCVCTRL_INTRAVAIL_ENB 0x10
675*4882a593Smuzhiyun #define HFI1_RCVCTRL_INTRAVAIL_DIS 0x20
676*4882a593Smuzhiyun #define HFI1_RCVCTRL_PKEY_ENB 0x40  /* Note, default is enabled */
677*4882a593Smuzhiyun #define HFI1_RCVCTRL_PKEY_DIS 0x80
678*4882a593Smuzhiyun #define HFI1_RCVCTRL_TIDFLOW_ENB 0x0400
679*4882a593Smuzhiyun #define HFI1_RCVCTRL_TIDFLOW_DIS 0x0800
680*4882a593Smuzhiyun #define HFI1_RCVCTRL_ONE_PKT_EGR_ENB 0x1000
681*4882a593Smuzhiyun #define HFI1_RCVCTRL_ONE_PKT_EGR_DIS 0x2000
682*4882a593Smuzhiyun #define HFI1_RCVCTRL_NO_RHQ_DROP_ENB 0x4000
683*4882a593Smuzhiyun #define HFI1_RCVCTRL_NO_RHQ_DROP_DIS 0x8000
684*4882a593Smuzhiyun #define HFI1_RCVCTRL_NO_EGR_DROP_ENB 0x10000
685*4882a593Smuzhiyun #define HFI1_RCVCTRL_NO_EGR_DROP_DIS 0x20000
686*4882a593Smuzhiyun #define HFI1_RCVCTRL_URGENT_ENB 0x40000
687*4882a593Smuzhiyun #define HFI1_RCVCTRL_URGENT_DIS 0x80000
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun /* partition enforcement flags */
690*4882a593Smuzhiyun #define HFI1_PART_ENFORCE_IN	0x1
691*4882a593Smuzhiyun #define HFI1_PART_ENFORCE_OUT	0x2
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun /* how often we check for synthetic counter wrap around */
694*4882a593Smuzhiyun #define SYNTH_CNT_TIME 3
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun /* Counter flags */
697*4882a593Smuzhiyun #define CNTR_NORMAL		0x0 /* Normal counters, just read register */
698*4882a593Smuzhiyun #define CNTR_SYNTH		0x1 /* Synthetic counters, saturate at all 1s */
699*4882a593Smuzhiyun #define CNTR_DISABLED		0x2 /* Disable this counter */
700*4882a593Smuzhiyun #define CNTR_32BIT		0x4 /* Simulate 64 bits for this counter */
701*4882a593Smuzhiyun #define CNTR_VL			0x8 /* Per VL counter */
702*4882a593Smuzhiyun #define CNTR_SDMA              0x10
703*4882a593Smuzhiyun #define CNTR_INVALID_VL		-1  /* Specifies invalid VL */
704*4882a593Smuzhiyun #define CNTR_MODE_W		0x0
705*4882a593Smuzhiyun #define CNTR_MODE_R		0x1
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun /* VLs Supported/Operational */
708*4882a593Smuzhiyun #define HFI1_MIN_VLS_SUPPORTED 1
709*4882a593Smuzhiyun #define HFI1_MAX_VLS_SUPPORTED 8
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun #define HFI1_GUIDS_PER_PORT  5
712*4882a593Smuzhiyun #define HFI1_PORT_GUID_INDEX 0
713*4882a593Smuzhiyun 
incr_cntr64(u64 * cntr)714*4882a593Smuzhiyun static inline void incr_cntr64(u64 *cntr)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	if (*cntr < (u64)-1LL)
717*4882a593Smuzhiyun 		(*cntr)++;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
incr_cntr32(u32 * cntr)720*4882a593Smuzhiyun static inline void incr_cntr32(u32 *cntr)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	if (*cntr < (u32)-1LL)
723*4882a593Smuzhiyun 		(*cntr)++;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun #define MAX_NAME_SIZE 64
727*4882a593Smuzhiyun struct hfi1_msix_entry {
728*4882a593Smuzhiyun 	enum irq_type type;
729*4882a593Smuzhiyun 	int irq;
730*4882a593Smuzhiyun 	void *arg;
731*4882a593Smuzhiyun 	cpumask_t mask;
732*4882a593Smuzhiyun 	struct irq_affinity_notify notify;
733*4882a593Smuzhiyun };
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun struct hfi1_msix_info {
736*4882a593Smuzhiyun 	/* lock to synchronize in_use_msix access */
737*4882a593Smuzhiyun 	spinlock_t msix_lock;
738*4882a593Smuzhiyun 	DECLARE_BITMAP(in_use_msix, CCE_NUM_MSIX_VECTORS);
739*4882a593Smuzhiyun 	struct hfi1_msix_entry *msix_entries;
740*4882a593Smuzhiyun 	u16 max_requested;
741*4882a593Smuzhiyun };
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /* per-SL CCA information */
744*4882a593Smuzhiyun struct cca_timer {
745*4882a593Smuzhiyun 	struct hrtimer hrtimer;
746*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd; /* read-only */
747*4882a593Smuzhiyun 	int sl; /* read-only */
748*4882a593Smuzhiyun 	u16 ccti; /* read/write - current value of CCTI */
749*4882a593Smuzhiyun };
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun struct link_down_reason {
752*4882a593Smuzhiyun 	/*
753*4882a593Smuzhiyun 	 * SMA-facing value.  Should be set from .latest when
754*4882a593Smuzhiyun 	 * HLS_UP_* -> HLS_DN_* transition actually occurs.
755*4882a593Smuzhiyun 	 */
756*4882a593Smuzhiyun 	u8 sma;
757*4882a593Smuzhiyun 	u8 latest;
758*4882a593Smuzhiyun };
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun enum {
761*4882a593Smuzhiyun 	LO_PRIO_TABLE,
762*4882a593Smuzhiyun 	HI_PRIO_TABLE,
763*4882a593Smuzhiyun 	MAX_PRIO_TABLE
764*4882a593Smuzhiyun };
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun struct vl_arb_cache {
767*4882a593Smuzhiyun 	/* protect vl arb cache */
768*4882a593Smuzhiyun 	spinlock_t lock;
769*4882a593Smuzhiyun 	struct ib_vl_weight_elem table[VL_ARB_TABLE_SIZE];
770*4882a593Smuzhiyun };
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun /*
773*4882a593Smuzhiyun  * The structure below encapsulates data relevant to a physical IB Port.
774*4882a593Smuzhiyun  * Current chips support only one such port, but the separation
775*4882a593Smuzhiyun  * clarifies things a bit. Note that to conform to IB conventions,
776*4882a593Smuzhiyun  * port-numbers are one-based. The first or only port is port1.
777*4882a593Smuzhiyun  */
778*4882a593Smuzhiyun struct hfi1_pportdata {
779*4882a593Smuzhiyun 	struct hfi1_ibport ibport_data;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
782*4882a593Smuzhiyun 	struct kobject pport_cc_kobj;
783*4882a593Smuzhiyun 	struct kobject sc2vl_kobj;
784*4882a593Smuzhiyun 	struct kobject sl2sc_kobj;
785*4882a593Smuzhiyun 	struct kobject vl2mtu_kobj;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	/* PHY support */
788*4882a593Smuzhiyun 	struct qsfp_data qsfp_info;
789*4882a593Smuzhiyun 	/* Values for SI tuning of SerDes */
790*4882a593Smuzhiyun 	u32 port_type;
791*4882a593Smuzhiyun 	u32 tx_preset_eq;
792*4882a593Smuzhiyun 	u32 tx_preset_noeq;
793*4882a593Smuzhiyun 	u32 rx_preset;
794*4882a593Smuzhiyun 	u8  local_atten;
795*4882a593Smuzhiyun 	u8  remote_atten;
796*4882a593Smuzhiyun 	u8  default_atten;
797*4882a593Smuzhiyun 	u8  max_power_class;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	/* did we read platform config from scratch registers? */
800*4882a593Smuzhiyun 	bool config_from_scratch;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	/* GUIDs for this interface, in host order, guids[0] is a port guid */
803*4882a593Smuzhiyun 	u64 guids[HFI1_GUIDS_PER_PORT];
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	/* GUID for peer interface, in host order */
806*4882a593Smuzhiyun 	u64 neighbor_guid;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	/* up or down physical link state */
809*4882a593Smuzhiyun 	u32 linkup;
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	/*
812*4882a593Smuzhiyun 	 * this address is mapped read-only into user processes so they can
813*4882a593Smuzhiyun 	 * get status cheaply, whenever they want.  One qword of status per port
814*4882a593Smuzhiyun 	 */
815*4882a593Smuzhiyun 	u64 *statusp;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	/* SendDMA related entries */
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	struct workqueue_struct *hfi1_wq;
820*4882a593Smuzhiyun 	struct workqueue_struct *link_wq;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	/* move out of interrupt context */
823*4882a593Smuzhiyun 	struct work_struct link_vc_work;
824*4882a593Smuzhiyun 	struct work_struct link_up_work;
825*4882a593Smuzhiyun 	struct work_struct link_down_work;
826*4882a593Smuzhiyun 	struct work_struct sma_message_work;
827*4882a593Smuzhiyun 	struct work_struct freeze_work;
828*4882a593Smuzhiyun 	struct work_struct link_downgrade_work;
829*4882a593Smuzhiyun 	struct work_struct link_bounce_work;
830*4882a593Smuzhiyun 	struct delayed_work start_link_work;
831*4882a593Smuzhiyun 	/* host link state variables */
832*4882a593Smuzhiyun 	struct mutex hls_lock;
833*4882a593Smuzhiyun 	u32 host_link_state;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* these are the "32 bit" regs */
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	u32 ibmtu; /* The MTU programmed for this unit */
838*4882a593Smuzhiyun 	/*
839*4882a593Smuzhiyun 	 * Current max size IB packet (in bytes) including IB headers, that
840*4882a593Smuzhiyun 	 * we can send. Changes when ibmtu changes.
841*4882a593Smuzhiyun 	 */
842*4882a593Smuzhiyun 	u32 ibmaxlen;
843*4882a593Smuzhiyun 	u32 current_egress_rate; /* units [10^6 bits/sec] */
844*4882a593Smuzhiyun 	/* LID programmed for this instance */
845*4882a593Smuzhiyun 	u32 lid;
846*4882a593Smuzhiyun 	/* list of pkeys programmed; 0 if not set */
847*4882a593Smuzhiyun 	u16 pkeys[MAX_PKEY_VALUES];
848*4882a593Smuzhiyun 	u16 link_width_supported;
849*4882a593Smuzhiyun 	u16 link_width_downgrade_supported;
850*4882a593Smuzhiyun 	u16 link_speed_supported;
851*4882a593Smuzhiyun 	u16 link_width_enabled;
852*4882a593Smuzhiyun 	u16 link_width_downgrade_enabled;
853*4882a593Smuzhiyun 	u16 link_speed_enabled;
854*4882a593Smuzhiyun 	u16 link_width_active;
855*4882a593Smuzhiyun 	u16 link_width_downgrade_tx_active;
856*4882a593Smuzhiyun 	u16 link_width_downgrade_rx_active;
857*4882a593Smuzhiyun 	u16 link_speed_active;
858*4882a593Smuzhiyun 	u8 vls_supported;
859*4882a593Smuzhiyun 	u8 vls_operational;
860*4882a593Smuzhiyun 	u8 actual_vls_operational;
861*4882a593Smuzhiyun 	/* LID mask control */
862*4882a593Smuzhiyun 	u8 lmc;
863*4882a593Smuzhiyun 	/* Rx Polarity inversion (compensate for ~tx on partner) */
864*4882a593Smuzhiyun 	u8 rx_pol_inv;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	u8 hw_pidx;     /* physical port index */
867*4882a593Smuzhiyun 	u8 port;        /* IB port number and index into dd->pports - 1 */
868*4882a593Smuzhiyun 	/* type of neighbor node */
869*4882a593Smuzhiyun 	u8 neighbor_type;
870*4882a593Smuzhiyun 	u8 neighbor_normal;
871*4882a593Smuzhiyun 	u8 neighbor_fm_security; /* 1 if firmware checking is disabled */
872*4882a593Smuzhiyun 	u8 neighbor_port_number;
873*4882a593Smuzhiyun 	u8 is_sm_config_started;
874*4882a593Smuzhiyun 	u8 offline_disabled_reason;
875*4882a593Smuzhiyun 	u8 is_active_optimize_enabled;
876*4882a593Smuzhiyun 	u8 driver_link_ready;	/* driver ready for active link */
877*4882a593Smuzhiyun 	u8 link_enabled;	/* link enabled? */
878*4882a593Smuzhiyun 	u8 linkinit_reason;
879*4882a593Smuzhiyun 	u8 local_tx_rate;	/* rate given to 8051 firmware */
880*4882a593Smuzhiyun 	u8 qsfp_retry_count;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	/* placeholders for IB MAD packet settings */
883*4882a593Smuzhiyun 	u8 overrun_threshold;
884*4882a593Smuzhiyun 	u8 phy_error_threshold;
885*4882a593Smuzhiyun 	unsigned int is_link_down_queued;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	/* Used to override LED behavior for things like maintenance beaconing*/
888*4882a593Smuzhiyun 	/*
889*4882a593Smuzhiyun 	 * Alternates per phase of blink
890*4882a593Smuzhiyun 	 * [0] holds LED off duration, [1] holds LED on duration
891*4882a593Smuzhiyun 	 */
892*4882a593Smuzhiyun 	unsigned long led_override_vals[2];
893*4882a593Smuzhiyun 	u8 led_override_phase; /* LSB picks from vals[] */
894*4882a593Smuzhiyun 	atomic_t led_override_timer_active;
895*4882a593Smuzhiyun 	/* Used to flash LEDs in override mode */
896*4882a593Smuzhiyun 	struct timer_list led_override_timer;
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	u32 sm_trap_qp;
899*4882a593Smuzhiyun 	u32 sa_qp;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/*
902*4882a593Smuzhiyun 	 * cca_timer_lock protects access to the per-SL cca_timer
903*4882a593Smuzhiyun 	 * structures (specifically the ccti member).
904*4882a593Smuzhiyun 	 */
905*4882a593Smuzhiyun 	spinlock_t cca_timer_lock ____cacheline_aligned_in_smp;
906*4882a593Smuzhiyun 	struct cca_timer cca_timer[OPA_MAX_SLS];
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	/* List of congestion control table entries */
909*4882a593Smuzhiyun 	struct ib_cc_table_entry_shadow ccti_entries[CC_TABLE_SHADOW_MAX];
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	/* congestion entries, each entry corresponding to a SL */
912*4882a593Smuzhiyun 	struct opa_congestion_setting_entry_shadow
913*4882a593Smuzhiyun 		congestion_entries[OPA_MAX_SLS];
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	/*
916*4882a593Smuzhiyun 	 * cc_state_lock protects (write) access to the per-port
917*4882a593Smuzhiyun 	 * struct cc_state.
918*4882a593Smuzhiyun 	 */
919*4882a593Smuzhiyun 	spinlock_t cc_state_lock ____cacheline_aligned_in_smp;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	struct cc_state __rcu *cc_state;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/* Total number of congestion control table entries */
924*4882a593Smuzhiyun 	u16 total_cct_entry;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* Bit map identifying service level */
927*4882a593Smuzhiyun 	u32 cc_sl_control_map;
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	/* CA's max number of 64 entry units in the congestion control table */
930*4882a593Smuzhiyun 	u8 cc_max_table_entries;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	/*
933*4882a593Smuzhiyun 	 * begin congestion log related entries
934*4882a593Smuzhiyun 	 * cc_log_lock protects all congestion log related data
935*4882a593Smuzhiyun 	 */
936*4882a593Smuzhiyun 	spinlock_t cc_log_lock ____cacheline_aligned_in_smp;
937*4882a593Smuzhiyun 	u8 threshold_cong_event_map[OPA_MAX_SLS / 8];
938*4882a593Smuzhiyun 	u16 threshold_event_counter;
939*4882a593Smuzhiyun 	struct opa_hfi1_cong_log_event_internal cc_events[OPA_CONG_LOG_ELEMS];
940*4882a593Smuzhiyun 	int cc_log_idx; /* index for logging events */
941*4882a593Smuzhiyun 	int cc_mad_idx; /* index for reporting events */
942*4882a593Smuzhiyun 	/* end congestion log related entries */
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	struct vl_arb_cache vl_arb_cache[MAX_PRIO_TABLE];
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	/* port relative counter buffer */
947*4882a593Smuzhiyun 	u64 *cntrs;
948*4882a593Smuzhiyun 	/* port relative synthetic counter buffer */
949*4882a593Smuzhiyun 	u64 *scntrs;
950*4882a593Smuzhiyun 	/* port_xmit_discards are synthesized from different egress errors */
951*4882a593Smuzhiyun 	u64 port_xmit_discards;
952*4882a593Smuzhiyun 	u64 port_xmit_discards_vl[C_VL_COUNT];
953*4882a593Smuzhiyun 	u64 port_xmit_constraint_errors;
954*4882a593Smuzhiyun 	u64 port_rcv_constraint_errors;
955*4882a593Smuzhiyun 	/* count of 'link_err' interrupts from DC */
956*4882a593Smuzhiyun 	u64 link_downed;
957*4882a593Smuzhiyun 	/* number of times link retrained successfully */
958*4882a593Smuzhiyun 	u64 link_up;
959*4882a593Smuzhiyun 	/* number of times a link unknown frame was reported */
960*4882a593Smuzhiyun 	u64 unknown_frame_count;
961*4882a593Smuzhiyun 	/* port_ltp_crc_mode is returned in 'portinfo' MADs */
962*4882a593Smuzhiyun 	u16 port_ltp_crc_mode;
963*4882a593Smuzhiyun 	/* port_crc_mode_enabled is the crc we support */
964*4882a593Smuzhiyun 	u8 port_crc_mode_enabled;
965*4882a593Smuzhiyun 	/* mgmt_allowed is also returned in 'portinfo' MADs */
966*4882a593Smuzhiyun 	u8 mgmt_allowed;
967*4882a593Smuzhiyun 	u8 part_enforce; /* partition enforcement flags */
968*4882a593Smuzhiyun 	struct link_down_reason local_link_down_reason;
969*4882a593Smuzhiyun 	struct link_down_reason neigh_link_down_reason;
970*4882a593Smuzhiyun 	/* Value to be sent to link peer on LinkDown .*/
971*4882a593Smuzhiyun 	u8 remote_link_down_reason;
972*4882a593Smuzhiyun 	/* Error events that will cause a port bounce. */
973*4882a593Smuzhiyun 	u32 port_error_action;
974*4882a593Smuzhiyun 	struct work_struct linkstate_active_work;
975*4882a593Smuzhiyun 	/* Does this port need to prescan for FECNs */
976*4882a593Smuzhiyun 	bool cc_prescan;
977*4882a593Smuzhiyun 	/*
978*4882a593Smuzhiyun 	 * Sample sendWaitCnt & sendWaitVlCnt during link transition
979*4882a593Smuzhiyun 	 * and counter request.
980*4882a593Smuzhiyun 	 */
981*4882a593Smuzhiyun 	u64 port_vl_xmit_wait_last[C_VL_COUNT + 1];
982*4882a593Smuzhiyun 	u16 prev_link_width;
983*4882a593Smuzhiyun 	u64 vl_xmit_flit_cnt[C_VL_COUNT + 1];
984*4882a593Smuzhiyun };
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun typedef void (*opcode_handler)(struct hfi1_packet *packet);
987*4882a593Smuzhiyun typedef void (*hfi1_make_req)(struct rvt_qp *qp,
988*4882a593Smuzhiyun 			      struct hfi1_pkt_state *ps,
989*4882a593Smuzhiyun 			      struct rvt_swqe *wqe);
990*4882a593Smuzhiyun extern const rhf_rcv_function_ptr normal_rhf_rcv_functions[];
991*4882a593Smuzhiyun extern const rhf_rcv_function_ptr netdev_rhf_rcv_functions[];
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun /* return values for the RHF receive functions */
994*4882a593Smuzhiyun #define RHF_RCV_CONTINUE  0	/* keep going */
995*4882a593Smuzhiyun #define RHF_RCV_DONE	  1	/* stop, this packet processed */
996*4882a593Smuzhiyun #define RHF_RCV_REPROCESS 2	/* stop. retain this packet */
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun struct rcv_array_data {
999*4882a593Smuzhiyun 	u16 ngroups;
1000*4882a593Smuzhiyun 	u16 nctxt_extra;
1001*4882a593Smuzhiyun 	u8 group_size;
1002*4882a593Smuzhiyun };
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun struct per_vl_data {
1005*4882a593Smuzhiyun 	u16 mtu;
1006*4882a593Smuzhiyun 	struct send_context *sc;
1007*4882a593Smuzhiyun };
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun /* 16 to directly index */
1010*4882a593Smuzhiyun #define PER_VL_SEND_CONTEXTS 16
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun struct err_info_rcvport {
1013*4882a593Smuzhiyun 	u8 status_and_code;
1014*4882a593Smuzhiyun 	u64 packet_flit1;
1015*4882a593Smuzhiyun 	u64 packet_flit2;
1016*4882a593Smuzhiyun };
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun struct err_info_constraint {
1019*4882a593Smuzhiyun 	u8 status;
1020*4882a593Smuzhiyun 	u16 pkey;
1021*4882a593Smuzhiyun 	u32 slid;
1022*4882a593Smuzhiyun };
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun struct hfi1_temp {
1025*4882a593Smuzhiyun 	unsigned int curr;       /* current temperature */
1026*4882a593Smuzhiyun 	unsigned int lo_lim;     /* low temperature limit */
1027*4882a593Smuzhiyun 	unsigned int hi_lim;     /* high temperature limit */
1028*4882a593Smuzhiyun 	unsigned int crit_lim;   /* critical temperature limit */
1029*4882a593Smuzhiyun 	u8 triggers;      /* temperature triggers */
1030*4882a593Smuzhiyun };
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun struct hfi1_i2c_bus {
1033*4882a593Smuzhiyun 	struct hfi1_devdata *controlling_dd; /* current controlling device */
1034*4882a593Smuzhiyun 	struct i2c_adapter adapter;	/* bus details */
1035*4882a593Smuzhiyun 	struct i2c_algo_bit_data algo;	/* bus algorithm details */
1036*4882a593Smuzhiyun 	int num;			/* bus number, 0 or 1 */
1037*4882a593Smuzhiyun };
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun /* common data between shared ASIC HFIs */
1040*4882a593Smuzhiyun struct hfi1_asic_data {
1041*4882a593Smuzhiyun 	struct hfi1_devdata *dds[2];	/* back pointers */
1042*4882a593Smuzhiyun 	struct mutex asic_resource_mutex;
1043*4882a593Smuzhiyun 	struct hfi1_i2c_bus *i2c_bus0;
1044*4882a593Smuzhiyun 	struct hfi1_i2c_bus *i2c_bus1;
1045*4882a593Smuzhiyun };
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun /* sizes for both the QP and RSM map tables */
1048*4882a593Smuzhiyun #define NUM_MAP_ENTRIES	 256
1049*4882a593Smuzhiyun #define NUM_MAP_REGS      32
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun /* Virtual NIC information */
1052*4882a593Smuzhiyun struct hfi1_vnic_data {
1053*4882a593Smuzhiyun 	struct kmem_cache *txreq_cache;
1054*4882a593Smuzhiyun 	u8 num_vports;
1055*4882a593Smuzhiyun };
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun struct hfi1_vnic_vport_info;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun /* device data struct now contains only "general per-device" info.
1060*4882a593Smuzhiyun  * fields related to a physical IB port are in a hfi1_pportdata struct.
1061*4882a593Smuzhiyun  */
1062*4882a593Smuzhiyun struct sdma_engine;
1063*4882a593Smuzhiyun struct sdma_vl_map;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun #define BOARD_VERS_MAX 96 /* how long the version string can be */
1066*4882a593Smuzhiyun #define SERIAL_MAX 16 /* length of the serial number */
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun typedef int (*send_routine)(struct rvt_qp *, struct hfi1_pkt_state *, u64);
1069*4882a593Smuzhiyun struct hfi1_devdata {
1070*4882a593Smuzhiyun 	struct hfi1_ibdev verbs_dev;     /* must be first */
1071*4882a593Smuzhiyun 	/* pointers to related structs for this device */
1072*4882a593Smuzhiyun 	/* pci access data structure */
1073*4882a593Smuzhiyun 	struct pci_dev *pcidev;
1074*4882a593Smuzhiyun 	struct cdev user_cdev;
1075*4882a593Smuzhiyun 	struct cdev diag_cdev;
1076*4882a593Smuzhiyun 	struct cdev ui_cdev;
1077*4882a593Smuzhiyun 	struct device *user_device;
1078*4882a593Smuzhiyun 	struct device *diag_device;
1079*4882a593Smuzhiyun 	struct device *ui_device;
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	/* first mapping up to RcvArray */
1082*4882a593Smuzhiyun 	u8 __iomem *kregbase1;
1083*4882a593Smuzhiyun 	resource_size_t physaddr;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	/* second uncached mapping from RcvArray to pio send buffers */
1086*4882a593Smuzhiyun 	u8 __iomem *kregbase2;
1087*4882a593Smuzhiyun 	/* for detecting offset above kregbase2 address */
1088*4882a593Smuzhiyun 	u32 base2_start;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	/* Per VL data. Enough for all VLs but not all elements are set/used. */
1091*4882a593Smuzhiyun 	struct per_vl_data vld[PER_VL_SEND_CONTEXTS];
1092*4882a593Smuzhiyun 	/* send context data */
1093*4882a593Smuzhiyun 	struct send_context_info *send_contexts;
1094*4882a593Smuzhiyun 	/* map hardware send contexts to software index */
1095*4882a593Smuzhiyun 	u8 *hw_to_sw;
1096*4882a593Smuzhiyun 	/* spinlock for allocating and releasing send context resources */
1097*4882a593Smuzhiyun 	spinlock_t sc_lock;
1098*4882a593Smuzhiyun 	/* lock for pio_map */
1099*4882a593Smuzhiyun 	spinlock_t pio_map_lock;
1100*4882a593Smuzhiyun 	/* Send Context initialization lock. */
1101*4882a593Smuzhiyun 	spinlock_t sc_init_lock;
1102*4882a593Smuzhiyun 	/* lock for sdma_map */
1103*4882a593Smuzhiyun 	spinlock_t                          sde_map_lock;
1104*4882a593Smuzhiyun 	/* array of kernel send contexts */
1105*4882a593Smuzhiyun 	struct send_context **kernel_send_context;
1106*4882a593Smuzhiyun 	/* array of vl maps */
1107*4882a593Smuzhiyun 	struct pio_vl_map __rcu *pio_map;
1108*4882a593Smuzhiyun 	/* default flags to last descriptor */
1109*4882a593Smuzhiyun 	u64 default_desc1;
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	/* fields common to all SDMA engines */
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	volatile __le64                    *sdma_heads_dma; /* DMA'ed by chip */
1114*4882a593Smuzhiyun 	dma_addr_t                          sdma_heads_phys;
1115*4882a593Smuzhiyun 	void                               *sdma_pad_dma; /* DMA'ed by chip */
1116*4882a593Smuzhiyun 	dma_addr_t                          sdma_pad_phys;
1117*4882a593Smuzhiyun 	/* for deallocation */
1118*4882a593Smuzhiyun 	size_t                              sdma_heads_size;
1119*4882a593Smuzhiyun 	/* num used */
1120*4882a593Smuzhiyun 	u32                                 num_sdma;
1121*4882a593Smuzhiyun 	/* array of engines sized by num_sdma */
1122*4882a593Smuzhiyun 	struct sdma_engine                 *per_sdma;
1123*4882a593Smuzhiyun 	/* array of vl maps */
1124*4882a593Smuzhiyun 	struct sdma_vl_map __rcu           *sdma_map;
1125*4882a593Smuzhiyun 	/* SPC freeze waitqueue and variable */
1126*4882a593Smuzhiyun 	wait_queue_head_t		  sdma_unfreeze_wq;
1127*4882a593Smuzhiyun 	atomic_t			  sdma_unfreeze_count;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	u32 lcb_access_count;		/* count of LCB users */
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	/* common data between shared ASIC HFIs in this OS */
1132*4882a593Smuzhiyun 	struct hfi1_asic_data *asic_data;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	/* mem-mapped pointer to base of PIO buffers */
1135*4882a593Smuzhiyun 	void __iomem *piobase;
1136*4882a593Smuzhiyun 	/*
1137*4882a593Smuzhiyun 	 * write-combining mem-mapped pointer to base of RcvArray
1138*4882a593Smuzhiyun 	 * memory.
1139*4882a593Smuzhiyun 	 */
1140*4882a593Smuzhiyun 	void __iomem *rcvarray_wc;
1141*4882a593Smuzhiyun 	/*
1142*4882a593Smuzhiyun 	 * credit return base - a per-NUMA range of DMA address that
1143*4882a593Smuzhiyun 	 * the chip will use to update the per-context free counter
1144*4882a593Smuzhiyun 	 */
1145*4882a593Smuzhiyun 	struct credit_return_base *cr_base;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 	/* send context numbers and sizes for each type */
1148*4882a593Smuzhiyun 	struct sc_config_sizes sc_sizes[SC_MAX];
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	char *boardname; /* human readable board info */
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	u64 ctx0_seq_drop;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	/* reset value */
1155*4882a593Smuzhiyun 	u64 z_int_counter;
1156*4882a593Smuzhiyun 	u64 z_rcv_limit;
1157*4882a593Smuzhiyun 	u64 z_send_schedule;
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	u64 __percpu *send_schedule;
1160*4882a593Smuzhiyun 	/* number of reserved contexts for netdev usage */
1161*4882a593Smuzhiyun 	u16 num_netdev_contexts;
1162*4882a593Smuzhiyun 	/* number of receive contexts in use by the driver */
1163*4882a593Smuzhiyun 	u32 num_rcv_contexts;
1164*4882a593Smuzhiyun 	/* number of pio send contexts in use by the driver */
1165*4882a593Smuzhiyun 	u32 num_send_contexts;
1166*4882a593Smuzhiyun 	/*
1167*4882a593Smuzhiyun 	 * number of ctxts available for PSM open
1168*4882a593Smuzhiyun 	 */
1169*4882a593Smuzhiyun 	u32 freectxts;
1170*4882a593Smuzhiyun 	/* total number of available user/PSM contexts */
1171*4882a593Smuzhiyun 	u32 num_user_contexts;
1172*4882a593Smuzhiyun 	/* base receive interrupt timeout, in CSR units */
1173*4882a593Smuzhiyun 	u32 rcv_intr_timeout_csr;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	spinlock_t sendctrl_lock; /* protect changes to SendCtrl */
1176*4882a593Smuzhiyun 	spinlock_t rcvctrl_lock; /* protect changes to RcvCtrl */
1177*4882a593Smuzhiyun 	spinlock_t uctxt_lock; /* protect rcd changes */
1178*4882a593Smuzhiyun 	struct mutex dc8051_lock; /* exclusive access to 8051 */
1179*4882a593Smuzhiyun 	struct workqueue_struct *update_cntr_wq;
1180*4882a593Smuzhiyun 	struct work_struct update_cntr_work;
1181*4882a593Smuzhiyun 	/* exclusive access to 8051 memory */
1182*4882a593Smuzhiyun 	spinlock_t dc8051_memlock;
1183*4882a593Smuzhiyun 	int dc8051_timed_out;	/* remember if the 8051 timed out */
1184*4882a593Smuzhiyun 	/*
1185*4882a593Smuzhiyun 	 * A page that will hold event notification bitmaps for all
1186*4882a593Smuzhiyun 	 * contexts. This page will be mapped into all processes.
1187*4882a593Smuzhiyun 	 */
1188*4882a593Smuzhiyun 	unsigned long *events;
1189*4882a593Smuzhiyun 	/*
1190*4882a593Smuzhiyun 	 * per unit status, see also portdata statusp
1191*4882a593Smuzhiyun 	 * mapped read-only into user processes so they can get unit and
1192*4882a593Smuzhiyun 	 * IB link status cheaply
1193*4882a593Smuzhiyun 	 */
1194*4882a593Smuzhiyun 	struct hfi1_status *status;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	/* revision register shadow */
1197*4882a593Smuzhiyun 	u64 revision;
1198*4882a593Smuzhiyun 	/* Base GUID for device (network order) */
1199*4882a593Smuzhiyun 	u64 base_guid;
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	/* both sides of the PCIe link are gen3 capable */
1202*4882a593Smuzhiyun 	u8 link_gen3_capable;
1203*4882a593Smuzhiyun 	u8 dc_shutdown;
1204*4882a593Smuzhiyun 	/* localbus width (1, 2,4,8,16,32) from config space  */
1205*4882a593Smuzhiyun 	u32 lbus_width;
1206*4882a593Smuzhiyun 	/* localbus speed in MHz */
1207*4882a593Smuzhiyun 	u32 lbus_speed;
1208*4882a593Smuzhiyun 	int unit; /* unit # of this chip */
1209*4882a593Smuzhiyun 	int node; /* home node of this chip */
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	/* save these PCI fields to restore after a reset */
1212*4882a593Smuzhiyun 	u32 pcibar0;
1213*4882a593Smuzhiyun 	u32 pcibar1;
1214*4882a593Smuzhiyun 	u32 pci_rom;
1215*4882a593Smuzhiyun 	u16 pci_command;
1216*4882a593Smuzhiyun 	u16 pcie_devctl;
1217*4882a593Smuzhiyun 	u16 pcie_lnkctl;
1218*4882a593Smuzhiyun 	u16 pcie_devctl2;
1219*4882a593Smuzhiyun 	u32 pci_msix0;
1220*4882a593Smuzhiyun 	u32 pci_tph2;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	/*
1223*4882a593Smuzhiyun 	 * ASCII serial number, from flash, large enough for original
1224*4882a593Smuzhiyun 	 * all digit strings, and longer serial number format
1225*4882a593Smuzhiyun 	 */
1226*4882a593Smuzhiyun 	u8 serial[SERIAL_MAX];
1227*4882a593Smuzhiyun 	/* human readable board version */
1228*4882a593Smuzhiyun 	u8 boardversion[BOARD_VERS_MAX];
1229*4882a593Smuzhiyun 	u8 lbus_info[32]; /* human readable localbus info */
1230*4882a593Smuzhiyun 	/* chip major rev, from CceRevision */
1231*4882a593Smuzhiyun 	u8 majrev;
1232*4882a593Smuzhiyun 	/* chip minor rev, from CceRevision */
1233*4882a593Smuzhiyun 	u8 minrev;
1234*4882a593Smuzhiyun 	/* hardware ID */
1235*4882a593Smuzhiyun 	u8 hfi1_id;
1236*4882a593Smuzhiyun 	/* implementation code */
1237*4882a593Smuzhiyun 	u8 icode;
1238*4882a593Smuzhiyun 	/* vAU of this device */
1239*4882a593Smuzhiyun 	u8 vau;
1240*4882a593Smuzhiyun 	/* vCU of this device */
1241*4882a593Smuzhiyun 	u8 vcu;
1242*4882a593Smuzhiyun 	/* link credits of this device */
1243*4882a593Smuzhiyun 	u16 link_credits;
1244*4882a593Smuzhiyun 	/* initial vl15 credits to use */
1245*4882a593Smuzhiyun 	u16 vl15_init;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	/*
1248*4882a593Smuzhiyun 	 * Cached value for vl15buf, read during verify cap interrupt. VL15
1249*4882a593Smuzhiyun 	 * credits are to be kept at 0 and set when handling the link-up
1250*4882a593Smuzhiyun 	 * interrupt. This removes the possibility of receiving VL15 MAD
1251*4882a593Smuzhiyun 	 * packets before this HFI is ready.
1252*4882a593Smuzhiyun 	 */
1253*4882a593Smuzhiyun 	u16 vl15buf_cached;
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	/* Misc small ints */
1256*4882a593Smuzhiyun 	u8 n_krcv_queues;
1257*4882a593Smuzhiyun 	u8 qos_shift;
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	u16 irev;	/* implementation revision */
1260*4882a593Smuzhiyun 	u32 dc8051_ver; /* 8051 firmware version */
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	spinlock_t hfi1_diag_trans_lock; /* protect diag observer ops */
1263*4882a593Smuzhiyun 	struct platform_config platform_config;
1264*4882a593Smuzhiyun 	struct platform_config_cache pcfg_cache;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	struct diag_client *diag_client;
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	/* general interrupt: mask of handled interrupts */
1269*4882a593Smuzhiyun 	u64 gi_mask[CCE_NUM_INT_CSRS];
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	struct rcv_array_data rcv_entries;
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	/* cycle length of PS* counters in HW (in picoseconds) */
1274*4882a593Smuzhiyun 	u16 psxmitwait_check_rate;
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	/*
1277*4882a593Smuzhiyun 	 * 64 bit synthetic counters
1278*4882a593Smuzhiyun 	 */
1279*4882a593Smuzhiyun 	struct timer_list synth_stats_timer;
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	/* MSI-X information */
1282*4882a593Smuzhiyun 	struct hfi1_msix_info msix_info;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	/*
1285*4882a593Smuzhiyun 	 * device counters
1286*4882a593Smuzhiyun 	 */
1287*4882a593Smuzhiyun 	char *cntrnames;
1288*4882a593Smuzhiyun 	size_t cntrnameslen;
1289*4882a593Smuzhiyun 	size_t ndevcntrs;
1290*4882a593Smuzhiyun 	u64 *cntrs;
1291*4882a593Smuzhiyun 	u64 *scntrs;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	/*
1294*4882a593Smuzhiyun 	 * remembered values for synthetic counters
1295*4882a593Smuzhiyun 	 */
1296*4882a593Smuzhiyun 	u64 last_tx;
1297*4882a593Smuzhiyun 	u64 last_rx;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	/*
1300*4882a593Smuzhiyun 	 * per-port counters
1301*4882a593Smuzhiyun 	 */
1302*4882a593Smuzhiyun 	size_t nportcntrs;
1303*4882a593Smuzhiyun 	char *portcntrnames;
1304*4882a593Smuzhiyun 	size_t portcntrnameslen;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	struct err_info_rcvport err_info_rcvport;
1307*4882a593Smuzhiyun 	struct err_info_constraint err_info_rcv_constraint;
1308*4882a593Smuzhiyun 	struct err_info_constraint err_info_xmit_constraint;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	atomic_t drop_packet;
1311*4882a593Smuzhiyun 	bool do_drop;
1312*4882a593Smuzhiyun 	u8 err_info_uncorrectable;
1313*4882a593Smuzhiyun 	u8 err_info_fmconfig;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	/*
1316*4882a593Smuzhiyun 	 * Software counters for the status bits defined by the
1317*4882a593Smuzhiyun 	 * associated error status registers
1318*4882a593Smuzhiyun 	 */
1319*4882a593Smuzhiyun 	u64 cce_err_status_cnt[NUM_CCE_ERR_STATUS_COUNTERS];
1320*4882a593Smuzhiyun 	u64 rcv_err_status_cnt[NUM_RCV_ERR_STATUS_COUNTERS];
1321*4882a593Smuzhiyun 	u64 misc_err_status_cnt[NUM_MISC_ERR_STATUS_COUNTERS];
1322*4882a593Smuzhiyun 	u64 send_pio_err_status_cnt[NUM_SEND_PIO_ERR_STATUS_COUNTERS];
1323*4882a593Smuzhiyun 	u64 send_dma_err_status_cnt[NUM_SEND_DMA_ERR_STATUS_COUNTERS];
1324*4882a593Smuzhiyun 	u64 send_egress_err_status_cnt[NUM_SEND_EGRESS_ERR_STATUS_COUNTERS];
1325*4882a593Smuzhiyun 	u64 send_err_status_cnt[NUM_SEND_ERR_STATUS_COUNTERS];
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	/* Software counter that spans all contexts */
1328*4882a593Smuzhiyun 	u64 sw_ctxt_err_status_cnt[NUM_SEND_CTXT_ERR_STATUS_COUNTERS];
1329*4882a593Smuzhiyun 	/* Software counter that spans all DMA engines */
1330*4882a593Smuzhiyun 	u64 sw_send_dma_eng_err_status_cnt[
1331*4882a593Smuzhiyun 		NUM_SEND_DMA_ENG_ERR_STATUS_COUNTERS];
1332*4882a593Smuzhiyun 	/* Software counter that aggregates all cce_err_status errors */
1333*4882a593Smuzhiyun 	u64 sw_cce_err_status_aggregate;
1334*4882a593Smuzhiyun 	/* Software counter that aggregates all bypass packet rcv errors */
1335*4882a593Smuzhiyun 	u64 sw_rcv_bypass_packet_errors;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* Save the enabled LCB error bits */
1338*4882a593Smuzhiyun 	u64 lcb_err_en;
1339*4882a593Smuzhiyun 	struct cpu_mask_set *comp_vect;
1340*4882a593Smuzhiyun 	int *comp_vect_mappings;
1341*4882a593Smuzhiyun 	u32 comp_vect_possible_cpus;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	/*
1344*4882a593Smuzhiyun 	 * Capability to have different send engines simply by changing a
1345*4882a593Smuzhiyun 	 * pointer value.
1346*4882a593Smuzhiyun 	 */
1347*4882a593Smuzhiyun 	send_routine process_pio_send ____cacheline_aligned_in_smp;
1348*4882a593Smuzhiyun 	send_routine process_dma_send;
1349*4882a593Smuzhiyun 	void (*pio_inline_send)(struct hfi1_devdata *dd, struct pio_buf *pbuf,
1350*4882a593Smuzhiyun 				u64 pbc, const void *from, size_t count);
1351*4882a593Smuzhiyun 	int (*process_vnic_dma_send)(struct hfi1_devdata *dd, u8 q_idx,
1352*4882a593Smuzhiyun 				     struct hfi1_vnic_vport_info *vinfo,
1353*4882a593Smuzhiyun 				     struct sk_buff *skb, u64 pbc, u8 plen);
1354*4882a593Smuzhiyun 	/* hfi1_pportdata, points to array of (physical) port-specific
1355*4882a593Smuzhiyun 	 * data structs, indexed by pidx (0..n-1)
1356*4882a593Smuzhiyun 	 */
1357*4882a593Smuzhiyun 	struct hfi1_pportdata *pport;
1358*4882a593Smuzhiyun 	/* receive context data */
1359*4882a593Smuzhiyun 	struct hfi1_ctxtdata **rcd;
1360*4882a593Smuzhiyun 	u64 __percpu *int_counter;
1361*4882a593Smuzhiyun 	/* verbs tx opcode stats */
1362*4882a593Smuzhiyun 	struct hfi1_opcode_stats_perctx __percpu *tx_opstats;
1363*4882a593Smuzhiyun 	/* device (not port) flags, basically device capabilities */
1364*4882a593Smuzhiyun 	u16 flags;
1365*4882a593Smuzhiyun 	/* Number of physical ports available */
1366*4882a593Smuzhiyun 	u8 num_pports;
1367*4882a593Smuzhiyun 	/* Lowest context number which can be used by user processes or VNIC */
1368*4882a593Smuzhiyun 	u8 first_dyn_alloc_ctxt;
1369*4882a593Smuzhiyun 	/* adding a new field here would make it part of this cacheline */
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 	/* seqlock for sc2vl */
1372*4882a593Smuzhiyun 	seqlock_t sc2vl_lock ____cacheline_aligned_in_smp;
1373*4882a593Smuzhiyun 	u64 sc2vl[4];
1374*4882a593Smuzhiyun 	u64 __percpu *rcv_limit;
1375*4882a593Smuzhiyun 	/* adding a new field here would make it part of this cacheline */
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	/* OUI comes from the HW. Used everywhere as 3 separate bytes. */
1378*4882a593Smuzhiyun 	u8 oui1;
1379*4882a593Smuzhiyun 	u8 oui2;
1380*4882a593Smuzhiyun 	u8 oui3;
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	/* Timer and counter used to detect RcvBufOvflCnt changes */
1383*4882a593Smuzhiyun 	struct timer_list rcverr_timer;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	wait_queue_head_t event_queue;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	/* receive context tail dummy address */
1388*4882a593Smuzhiyun 	__le64 *rcvhdrtail_dummy_kvaddr;
1389*4882a593Smuzhiyun 	dma_addr_t rcvhdrtail_dummy_dma;
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	u32 rcv_ovfl_cnt;
1392*4882a593Smuzhiyun 	/* Serialize ASPM enable/disable between multiple verbs contexts */
1393*4882a593Smuzhiyun 	spinlock_t aspm_lock;
1394*4882a593Smuzhiyun 	/* Number of verbs contexts which have disabled ASPM */
1395*4882a593Smuzhiyun 	atomic_t aspm_disabled_cnt;
1396*4882a593Smuzhiyun 	/* Keeps track of user space clients */
1397*4882a593Smuzhiyun 	atomic_t user_refcount;
1398*4882a593Smuzhiyun 	/* Used to wait for outstanding user space clients before dev removal */
1399*4882a593Smuzhiyun 	struct completion user_comp;
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 	bool eprom_available;	/* true if EPROM is available for this device */
1402*4882a593Smuzhiyun 	bool aspm_supported;	/* Does HW support ASPM */
1403*4882a593Smuzhiyun 	bool aspm_enabled;	/* ASPM state: enabled/disabled */
1404*4882a593Smuzhiyun 	struct rhashtable *sdma_rht;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	/* vnic data */
1407*4882a593Smuzhiyun 	struct hfi1_vnic_data vnic;
1408*4882a593Smuzhiyun 	/* Lock to protect IRQ SRC register access */
1409*4882a593Smuzhiyun 	spinlock_t irq_src_lock;
1410*4882a593Smuzhiyun 	int vnic_num_vports;
1411*4882a593Smuzhiyun 	struct net_device *dummy_netdev;
1412*4882a593Smuzhiyun 	struct hfi1_affinity_node *affinity_entry;
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	/* Keeps track of IPoIB RSM rule users */
1415*4882a593Smuzhiyun 	atomic_t ipoib_rsm_usr_num;
1416*4882a593Smuzhiyun };
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun /* 8051 firmware version helper */
1419*4882a593Smuzhiyun #define dc8051_ver(a, b, c) ((a) << 16 | (b) << 8 | (c))
1420*4882a593Smuzhiyun #define dc8051_ver_maj(a) (((a) & 0xff0000) >> 16)
1421*4882a593Smuzhiyun #define dc8051_ver_min(a) (((a) & 0x00ff00) >> 8)
1422*4882a593Smuzhiyun #define dc8051_ver_patch(a) ((a) & 0x0000ff)
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun /* f_put_tid types */
1425*4882a593Smuzhiyun #define PT_EXPECTED       0
1426*4882a593Smuzhiyun #define PT_EAGER          1
1427*4882a593Smuzhiyun #define PT_INVALID_FLUSH  2
1428*4882a593Smuzhiyun #define PT_INVALID        3
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun struct tid_rb_node;
1431*4882a593Smuzhiyun struct mmu_rb_node;
1432*4882a593Smuzhiyun struct mmu_rb_handler;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun /* Private data for file operations */
1435*4882a593Smuzhiyun struct hfi1_filedata {
1436*4882a593Smuzhiyun 	struct srcu_struct pq_srcu;
1437*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
1438*4882a593Smuzhiyun 	struct hfi1_ctxtdata *uctxt;
1439*4882a593Smuzhiyun 	struct hfi1_user_sdma_comp_q *cq;
1440*4882a593Smuzhiyun 	/* update side lock for SRCU */
1441*4882a593Smuzhiyun 	spinlock_t pq_rcu_lock;
1442*4882a593Smuzhiyun 	struct hfi1_user_sdma_pkt_q __rcu *pq;
1443*4882a593Smuzhiyun 	u16 subctxt;
1444*4882a593Smuzhiyun 	/* for cpu affinity; -1 if none */
1445*4882a593Smuzhiyun 	int rec_cpu_num;
1446*4882a593Smuzhiyun 	u32 tid_n_pinned;
1447*4882a593Smuzhiyun 	bool use_mn;
1448*4882a593Smuzhiyun 	struct tid_rb_node **entry_to_rb;
1449*4882a593Smuzhiyun 	spinlock_t tid_lock; /* protect tid_[limit,used] counters */
1450*4882a593Smuzhiyun 	u32 tid_limit;
1451*4882a593Smuzhiyun 	u32 tid_used;
1452*4882a593Smuzhiyun 	u32 *invalid_tids;
1453*4882a593Smuzhiyun 	u32 invalid_tid_idx;
1454*4882a593Smuzhiyun 	/* protect invalid_tids array and invalid_tid_idx */
1455*4882a593Smuzhiyun 	spinlock_t invalid_lock;
1456*4882a593Smuzhiyun };
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun extern struct xarray hfi1_dev_table;
1459*4882a593Smuzhiyun struct hfi1_devdata *hfi1_lookup(int unit);
1460*4882a593Smuzhiyun 
uctxt_offset(struct hfi1_ctxtdata * uctxt)1461*4882a593Smuzhiyun static inline unsigned long uctxt_offset(struct hfi1_ctxtdata *uctxt)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun 	return (uctxt->ctxt - uctxt->dd->first_dyn_alloc_ctxt) *
1464*4882a593Smuzhiyun 		HFI1_MAX_SHARED_CTXTS;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun int hfi1_init(struct hfi1_devdata *dd, int reinit);
1468*4882a593Smuzhiyun int hfi1_count_active_units(void);
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun int hfi1_diag_add(struct hfi1_devdata *dd);
1471*4882a593Smuzhiyun void hfi1_diag_remove(struct hfi1_devdata *dd);
1472*4882a593Smuzhiyun void handle_linkup_change(struct hfi1_devdata *dd, u32 linkup);
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun void handle_user_interrupt(struct hfi1_ctxtdata *rcd);
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd);
1477*4882a593Smuzhiyun int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd);
1478*4882a593Smuzhiyun int hfi1_create_kctxts(struct hfi1_devdata *dd);
1479*4882a593Smuzhiyun int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa,
1480*4882a593Smuzhiyun 			 struct hfi1_ctxtdata **rcd);
1481*4882a593Smuzhiyun void hfi1_free_ctxt(struct hfi1_ctxtdata *rcd);
1482*4882a593Smuzhiyun void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
1483*4882a593Smuzhiyun 			 struct hfi1_devdata *dd, u8 hw_pidx, u8 port);
1484*4882a593Smuzhiyun void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd);
1485*4882a593Smuzhiyun int hfi1_rcd_put(struct hfi1_ctxtdata *rcd);
1486*4882a593Smuzhiyun int hfi1_rcd_get(struct hfi1_ctxtdata *rcd);
1487*4882a593Smuzhiyun struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd,
1488*4882a593Smuzhiyun 						 u16 ctxt);
1489*4882a593Smuzhiyun struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt);
1490*4882a593Smuzhiyun int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread);
1491*4882a593Smuzhiyun int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread);
1492*4882a593Smuzhiyun int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread);
1493*4882a593Smuzhiyun int handle_receive_interrupt_napi_fp(struct hfi1_ctxtdata *rcd, int budget);
1494*4882a593Smuzhiyun int handle_receive_interrupt_napi_sp(struct hfi1_ctxtdata *rcd, int budget);
1495*4882a593Smuzhiyun void set_all_slowpath(struct hfi1_devdata *dd);
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun extern const struct pci_device_id hfi1_pci_tbl[];
1498*4882a593Smuzhiyun void hfi1_make_ud_req_9B(struct rvt_qp *qp,
1499*4882a593Smuzhiyun 			 struct hfi1_pkt_state *ps,
1500*4882a593Smuzhiyun 			 struct rvt_swqe *wqe);
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun void hfi1_make_ud_req_16B(struct rvt_qp *qp,
1503*4882a593Smuzhiyun 			  struct hfi1_pkt_state *ps,
1504*4882a593Smuzhiyun 			  struct rvt_swqe *wqe);
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun /* receive packet handler dispositions */
1507*4882a593Smuzhiyun #define RCV_PKT_OK      0x0 /* keep going */
1508*4882a593Smuzhiyun #define RCV_PKT_LIMIT   0x1 /* stop, hit limit, start thread */
1509*4882a593Smuzhiyun #define RCV_PKT_DONE    0x2 /* stop, no more packets detected */
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun /**
1512*4882a593Smuzhiyun  * hfi1_rcd_head - add accessor for rcd head
1513*4882a593Smuzhiyun  * @rcd: the context
1514*4882a593Smuzhiyun  */
hfi1_rcd_head(struct hfi1_ctxtdata * rcd)1515*4882a593Smuzhiyun static inline u32 hfi1_rcd_head(struct hfi1_ctxtdata *rcd)
1516*4882a593Smuzhiyun {
1517*4882a593Smuzhiyun 	return rcd->head;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun /**
1521*4882a593Smuzhiyun  * hfi1_set_rcd_head - add accessor for rcd head
1522*4882a593Smuzhiyun  * @rcd: the context
1523*4882a593Smuzhiyun  * @head: the new head
1524*4882a593Smuzhiyun  */
hfi1_set_rcd_head(struct hfi1_ctxtdata * rcd,u32 head)1525*4882a593Smuzhiyun static inline void hfi1_set_rcd_head(struct hfi1_ctxtdata *rcd, u32 head)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun 	rcd->head = head;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun /* calculate the current RHF address */
get_rhf_addr(struct hfi1_ctxtdata * rcd)1531*4882a593Smuzhiyun static inline __le32 *get_rhf_addr(struct hfi1_ctxtdata *rcd)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun 	return (__le32 *)rcd->rcvhdrq + rcd->head + rcd->rhf_offset;
1534*4882a593Smuzhiyun }
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun /* return DMA_RTAIL configuration */
get_dma_rtail_setting(struct hfi1_ctxtdata * rcd)1537*4882a593Smuzhiyun static inline bool get_dma_rtail_setting(struct hfi1_ctxtdata *rcd)
1538*4882a593Smuzhiyun {
1539*4882a593Smuzhiyun 	return !!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL);
1540*4882a593Smuzhiyun }
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun /**
1543*4882a593Smuzhiyun  * hfi1_seq_incr_wrap - wrapping increment for sequence
1544*4882a593Smuzhiyun  * @seq: the current sequence number
1545*4882a593Smuzhiyun  *
1546*4882a593Smuzhiyun  * Returns: the incremented seq
1547*4882a593Smuzhiyun  */
hfi1_seq_incr_wrap(u8 seq)1548*4882a593Smuzhiyun static inline u8 hfi1_seq_incr_wrap(u8 seq)
1549*4882a593Smuzhiyun {
1550*4882a593Smuzhiyun 	if (++seq > RHF_MAX_SEQ)
1551*4882a593Smuzhiyun 		seq = 1;
1552*4882a593Smuzhiyun 	return seq;
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun /**
1556*4882a593Smuzhiyun  * hfi1_seq_cnt - return seq_cnt member
1557*4882a593Smuzhiyun  * @rcd: the receive context
1558*4882a593Smuzhiyun  *
1559*4882a593Smuzhiyun  * Return seq_cnt member
1560*4882a593Smuzhiyun  */
hfi1_seq_cnt(struct hfi1_ctxtdata * rcd)1561*4882a593Smuzhiyun static inline u8 hfi1_seq_cnt(struct hfi1_ctxtdata *rcd)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun 	return rcd->seq_cnt;
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun /**
1567*4882a593Smuzhiyun  * hfi1_set_seq_cnt - return seq_cnt member
1568*4882a593Smuzhiyun  * @rcd: the receive context
1569*4882a593Smuzhiyun  *
1570*4882a593Smuzhiyun  * Return seq_cnt member
1571*4882a593Smuzhiyun  */
hfi1_set_seq_cnt(struct hfi1_ctxtdata * rcd,u8 cnt)1572*4882a593Smuzhiyun static inline void hfi1_set_seq_cnt(struct hfi1_ctxtdata *rcd, u8 cnt)
1573*4882a593Smuzhiyun {
1574*4882a593Smuzhiyun 	rcd->seq_cnt = cnt;
1575*4882a593Smuzhiyun }
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun /**
1578*4882a593Smuzhiyun  * last_rcv_seq - is last
1579*4882a593Smuzhiyun  * @rcd: the receive context
1580*4882a593Smuzhiyun  * @seq: sequence
1581*4882a593Smuzhiyun  *
1582*4882a593Smuzhiyun  * return true if last packet
1583*4882a593Smuzhiyun  */
last_rcv_seq(struct hfi1_ctxtdata * rcd,u32 seq)1584*4882a593Smuzhiyun static inline bool last_rcv_seq(struct hfi1_ctxtdata *rcd, u32 seq)
1585*4882a593Smuzhiyun {
1586*4882a593Smuzhiyun 	return seq != rcd->seq_cnt;
1587*4882a593Smuzhiyun }
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun /**
1590*4882a593Smuzhiyun  * rcd_seq_incr - increment context sequence number
1591*4882a593Smuzhiyun  * @rcd: the receive context
1592*4882a593Smuzhiyun  * @seq: the current sequence number
1593*4882a593Smuzhiyun  *
1594*4882a593Smuzhiyun  * Returns: true if the this was the last packet
1595*4882a593Smuzhiyun  */
hfi1_seq_incr(struct hfi1_ctxtdata * rcd,u32 seq)1596*4882a593Smuzhiyun static inline bool hfi1_seq_incr(struct hfi1_ctxtdata *rcd, u32 seq)
1597*4882a593Smuzhiyun {
1598*4882a593Smuzhiyun 	rcd->seq_cnt = hfi1_seq_incr_wrap(rcd->seq_cnt);
1599*4882a593Smuzhiyun 	return last_rcv_seq(rcd, seq);
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun /**
1603*4882a593Smuzhiyun  * get_hdrqentsize - return hdrq entry size
1604*4882a593Smuzhiyun  * @rcd: the receive context
1605*4882a593Smuzhiyun  */
get_hdrqentsize(struct hfi1_ctxtdata * rcd)1606*4882a593Smuzhiyun static inline u8 get_hdrqentsize(struct hfi1_ctxtdata *rcd)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun 	return rcd->rcvhdrqentsize;
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun /**
1612*4882a593Smuzhiyun  * get_hdrq_cnt - return hdrq count
1613*4882a593Smuzhiyun  * @rcd: the receive context
1614*4882a593Smuzhiyun  */
get_hdrq_cnt(struct hfi1_ctxtdata * rcd)1615*4882a593Smuzhiyun static inline u16 get_hdrq_cnt(struct hfi1_ctxtdata *rcd)
1616*4882a593Smuzhiyun {
1617*4882a593Smuzhiyun 	return rcd->rcvhdrq_cnt;
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun /**
1621*4882a593Smuzhiyun  * hfi1_is_slowpath - check if this context is slow path
1622*4882a593Smuzhiyun  * @rcd: the receive context
1623*4882a593Smuzhiyun  */
hfi1_is_slowpath(struct hfi1_ctxtdata * rcd)1624*4882a593Smuzhiyun static inline bool hfi1_is_slowpath(struct hfi1_ctxtdata *rcd)
1625*4882a593Smuzhiyun {
1626*4882a593Smuzhiyun 	return rcd->do_interrupt == rcd->slow_handler;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun /**
1630*4882a593Smuzhiyun  * hfi1_is_fastpath - check if this context is fast path
1631*4882a593Smuzhiyun  * @rcd: the receive context
1632*4882a593Smuzhiyun  */
hfi1_is_fastpath(struct hfi1_ctxtdata * rcd)1633*4882a593Smuzhiyun static inline bool hfi1_is_fastpath(struct hfi1_ctxtdata *rcd)
1634*4882a593Smuzhiyun {
1635*4882a593Smuzhiyun 	if (rcd->ctxt == HFI1_CTRL_CTXT)
1636*4882a593Smuzhiyun 		return false;
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 	return rcd->do_interrupt == rcd->fast_handler;
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun /**
1642*4882a593Smuzhiyun  * hfi1_set_fast - change to the fast handler
1643*4882a593Smuzhiyun  * @rcd: the receive context
1644*4882a593Smuzhiyun  */
hfi1_set_fast(struct hfi1_ctxtdata * rcd)1645*4882a593Smuzhiyun static inline void hfi1_set_fast(struct hfi1_ctxtdata *rcd)
1646*4882a593Smuzhiyun {
1647*4882a593Smuzhiyun 	if (unlikely(!rcd))
1648*4882a593Smuzhiyun 		return;
1649*4882a593Smuzhiyun 	if (unlikely(!hfi1_is_fastpath(rcd)))
1650*4882a593Smuzhiyun 		rcd->do_interrupt = rcd->fast_handler;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun int hfi1_reset_device(int);
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun void receive_interrupt_work(struct work_struct *work);
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun /* extract service channel from header and rhf */
hfi1_9B_get_sc5(struct ib_header * hdr,u64 rhf)1658*4882a593Smuzhiyun static inline int hfi1_9B_get_sc5(struct ib_header *hdr, u64 rhf)
1659*4882a593Smuzhiyun {
1660*4882a593Smuzhiyun 	return ib_get_sc(hdr) | ((!!(rhf_dc_info(rhf))) << 4);
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun #define HFI1_JKEY_WIDTH       16
1664*4882a593Smuzhiyun #define HFI1_JKEY_MASK        (BIT(16) - 1)
1665*4882a593Smuzhiyun #define HFI1_ADMIN_JKEY_RANGE 32
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun /*
1668*4882a593Smuzhiyun  * J_KEYs are split and allocated in the following groups:
1669*4882a593Smuzhiyun  *   0 - 31    - users with administrator privileges
1670*4882a593Smuzhiyun  *  32 - 63    - kernel protocols using KDETH packets
1671*4882a593Smuzhiyun  *  64 - 65535 - all other users using KDETH packets
1672*4882a593Smuzhiyun  */
generate_jkey(kuid_t uid)1673*4882a593Smuzhiyun static inline u16 generate_jkey(kuid_t uid)
1674*4882a593Smuzhiyun {
1675*4882a593Smuzhiyun 	u16 jkey = from_kuid(current_user_ns(), uid) & HFI1_JKEY_MASK;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	if (capable(CAP_SYS_ADMIN))
1678*4882a593Smuzhiyun 		jkey &= HFI1_ADMIN_JKEY_RANGE - 1;
1679*4882a593Smuzhiyun 	else if (jkey < 64)
1680*4882a593Smuzhiyun 		jkey |= BIT(HFI1_JKEY_WIDTH - 1);
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	return jkey;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun /*
1686*4882a593Smuzhiyun  * active_egress_rate
1687*4882a593Smuzhiyun  *
1688*4882a593Smuzhiyun  * returns the active egress rate in units of [10^6 bits/sec]
1689*4882a593Smuzhiyun  */
active_egress_rate(struct hfi1_pportdata * ppd)1690*4882a593Smuzhiyun static inline u32 active_egress_rate(struct hfi1_pportdata *ppd)
1691*4882a593Smuzhiyun {
1692*4882a593Smuzhiyun 	u16 link_speed = ppd->link_speed_active;
1693*4882a593Smuzhiyun 	u16 link_width = ppd->link_width_active;
1694*4882a593Smuzhiyun 	u32 egress_rate;
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun 	if (link_speed == OPA_LINK_SPEED_25G)
1697*4882a593Smuzhiyun 		egress_rate = 25000;
1698*4882a593Smuzhiyun 	else /* assume OPA_LINK_SPEED_12_5G */
1699*4882a593Smuzhiyun 		egress_rate = 12500;
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun 	switch (link_width) {
1702*4882a593Smuzhiyun 	case OPA_LINK_WIDTH_4X:
1703*4882a593Smuzhiyun 		egress_rate *= 4;
1704*4882a593Smuzhiyun 		break;
1705*4882a593Smuzhiyun 	case OPA_LINK_WIDTH_3X:
1706*4882a593Smuzhiyun 		egress_rate *= 3;
1707*4882a593Smuzhiyun 		break;
1708*4882a593Smuzhiyun 	case OPA_LINK_WIDTH_2X:
1709*4882a593Smuzhiyun 		egress_rate *= 2;
1710*4882a593Smuzhiyun 		break;
1711*4882a593Smuzhiyun 	default:
1712*4882a593Smuzhiyun 		/* assume IB_WIDTH_1X */
1713*4882a593Smuzhiyun 		break;
1714*4882a593Smuzhiyun 	}
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	return egress_rate;
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun /*
1720*4882a593Smuzhiyun  * egress_cycles
1721*4882a593Smuzhiyun  *
1722*4882a593Smuzhiyun  * Returns the number of 'fabric clock cycles' to egress a packet
1723*4882a593Smuzhiyun  * of length 'len' bytes, at 'rate' Mbit/s. Since the fabric clock
1724*4882a593Smuzhiyun  * rate is (approximately) 805 MHz, the units of the returned value
1725*4882a593Smuzhiyun  * are (1/805 MHz).
1726*4882a593Smuzhiyun  */
egress_cycles(u32 len,u32 rate)1727*4882a593Smuzhiyun static inline u32 egress_cycles(u32 len, u32 rate)
1728*4882a593Smuzhiyun {
1729*4882a593Smuzhiyun 	u32 cycles;
1730*4882a593Smuzhiyun 
1731*4882a593Smuzhiyun 	/*
1732*4882a593Smuzhiyun 	 * cycles is:
1733*4882a593Smuzhiyun 	 *
1734*4882a593Smuzhiyun 	 *          (length) [bits] / (rate) [bits/sec]
1735*4882a593Smuzhiyun 	 *  ---------------------------------------------------
1736*4882a593Smuzhiyun 	 *  fabric_clock_period == 1 /(805 * 10^6) [cycles/sec]
1737*4882a593Smuzhiyun 	 */
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	cycles = len * 8; /* bits */
1740*4882a593Smuzhiyun 	cycles *= 805;
1741*4882a593Smuzhiyun 	cycles /= rate;
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	return cycles;
1744*4882a593Smuzhiyun }
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun void set_link_ipg(struct hfi1_pportdata *ppd);
1747*4882a593Smuzhiyun void process_becn(struct hfi1_pportdata *ppd, u8 sl, u32 rlid, u32 lqpn,
1748*4882a593Smuzhiyun 		  u32 rqpn, u8 svc_type);
1749*4882a593Smuzhiyun void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
1750*4882a593Smuzhiyun 		u16 pkey, u32 slid, u32 dlid, u8 sc5,
1751*4882a593Smuzhiyun 		const struct ib_grh *old_grh);
1752*4882a593Smuzhiyun void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
1753*4882a593Smuzhiyun 		    u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
1754*4882a593Smuzhiyun 		    u8 sc5, const struct ib_grh *old_grh);
1755*4882a593Smuzhiyun typedef void (*hfi1_handle_cnp)(struct hfi1_ibport *ibp, struct rvt_qp *qp,
1756*4882a593Smuzhiyun 				u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
1757*4882a593Smuzhiyun 				u8 sc5, const struct ib_grh *old_grh);
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun #define PKEY_CHECK_INVALID -1
1760*4882a593Smuzhiyun int egress_pkey_check(struct hfi1_pportdata *ppd, u32 slid, u16 pkey,
1761*4882a593Smuzhiyun 		      u8 sc5, int8_t s_pkey_index);
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun #define PACKET_EGRESS_TIMEOUT 350
pause_for_credit_return(struct hfi1_devdata * dd)1764*4882a593Smuzhiyun static inline void pause_for_credit_return(struct hfi1_devdata *dd)
1765*4882a593Smuzhiyun {
1766*4882a593Smuzhiyun 	/* Pause at least 1us, to ensure chip returns all credits */
1767*4882a593Smuzhiyun 	u32 usec = cclock_to_ns(dd, PACKET_EGRESS_TIMEOUT) / 1000;
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 	udelay(usec ? usec : 1);
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun /**
1773*4882a593Smuzhiyun  * sc_to_vlt() reverse lookup sc to vl
1774*4882a593Smuzhiyun  * @dd - devdata
1775*4882a593Smuzhiyun  * @sc5 - 5 bit sc
1776*4882a593Smuzhiyun  */
sc_to_vlt(struct hfi1_devdata * dd,u8 sc5)1777*4882a593Smuzhiyun static inline u8 sc_to_vlt(struct hfi1_devdata *dd, u8 sc5)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun 	unsigned seq;
1780*4882a593Smuzhiyun 	u8 rval;
1781*4882a593Smuzhiyun 
1782*4882a593Smuzhiyun 	if (sc5 >= OPA_MAX_SCS)
1783*4882a593Smuzhiyun 		return (u8)(0xff);
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 	do {
1786*4882a593Smuzhiyun 		seq = read_seqbegin(&dd->sc2vl_lock);
1787*4882a593Smuzhiyun 		rval = *(((u8 *)dd->sc2vl) + sc5);
1788*4882a593Smuzhiyun 	} while (read_seqretry(&dd->sc2vl_lock, seq));
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	return rval;
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun #define PKEY_MEMBER_MASK 0x8000
1794*4882a593Smuzhiyun #define PKEY_LOW_15_MASK 0x7fff
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun /*
1797*4882a593Smuzhiyun  * ingress_pkey_matches_entry - return 1 if the pkey matches ent (ent
1798*4882a593Smuzhiyun  * being an entry from the ingress partition key table), return 0
1799*4882a593Smuzhiyun  * otherwise. Use the matching criteria for ingress partition keys
1800*4882a593Smuzhiyun  * specified in the OPAv1 spec., section 9.10.14.
1801*4882a593Smuzhiyun  */
ingress_pkey_matches_entry(u16 pkey,u16 ent)1802*4882a593Smuzhiyun static inline int ingress_pkey_matches_entry(u16 pkey, u16 ent)
1803*4882a593Smuzhiyun {
1804*4882a593Smuzhiyun 	u16 mkey = pkey & PKEY_LOW_15_MASK;
1805*4882a593Smuzhiyun 	u16 ment = ent & PKEY_LOW_15_MASK;
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun 	if (mkey == ment) {
1808*4882a593Smuzhiyun 		/*
1809*4882a593Smuzhiyun 		 * If pkey[15] is clear (limited partition member),
1810*4882a593Smuzhiyun 		 * is bit 15 in the corresponding table element
1811*4882a593Smuzhiyun 		 * clear (limited member)?
1812*4882a593Smuzhiyun 		 */
1813*4882a593Smuzhiyun 		if (!(pkey & PKEY_MEMBER_MASK))
1814*4882a593Smuzhiyun 			return !!(ent & PKEY_MEMBER_MASK);
1815*4882a593Smuzhiyun 		return 1;
1816*4882a593Smuzhiyun 	}
1817*4882a593Smuzhiyun 	return 0;
1818*4882a593Smuzhiyun }
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun /*
1821*4882a593Smuzhiyun  * ingress_pkey_table_search - search the entire pkey table for
1822*4882a593Smuzhiyun  * an entry which matches 'pkey'. return 0 if a match is found,
1823*4882a593Smuzhiyun  * and 1 otherwise.
1824*4882a593Smuzhiyun  */
ingress_pkey_table_search(struct hfi1_pportdata * ppd,u16 pkey)1825*4882a593Smuzhiyun static int ingress_pkey_table_search(struct hfi1_pportdata *ppd, u16 pkey)
1826*4882a593Smuzhiyun {
1827*4882a593Smuzhiyun 	int i;
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 	for (i = 0; i < MAX_PKEY_VALUES; i++) {
1830*4882a593Smuzhiyun 		if (ingress_pkey_matches_entry(pkey, ppd->pkeys[i]))
1831*4882a593Smuzhiyun 			return 0;
1832*4882a593Smuzhiyun 	}
1833*4882a593Smuzhiyun 	return 1;
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun /*
1837*4882a593Smuzhiyun  * ingress_pkey_table_fail - record a failure of ingress pkey validation,
1838*4882a593Smuzhiyun  * i.e., increment port_rcv_constraint_errors for the port, and record
1839*4882a593Smuzhiyun  * the 'error info' for this failure.
1840*4882a593Smuzhiyun  */
ingress_pkey_table_fail(struct hfi1_pportdata * ppd,u16 pkey,u32 slid)1841*4882a593Smuzhiyun static void ingress_pkey_table_fail(struct hfi1_pportdata *ppd, u16 pkey,
1842*4882a593Smuzhiyun 				    u32 slid)
1843*4882a593Smuzhiyun {
1844*4882a593Smuzhiyun 	struct hfi1_devdata *dd = ppd->dd;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	incr_cntr64(&ppd->port_rcv_constraint_errors);
1847*4882a593Smuzhiyun 	if (!(dd->err_info_rcv_constraint.status & OPA_EI_STATUS_SMASK)) {
1848*4882a593Smuzhiyun 		dd->err_info_rcv_constraint.status |= OPA_EI_STATUS_SMASK;
1849*4882a593Smuzhiyun 		dd->err_info_rcv_constraint.slid = slid;
1850*4882a593Smuzhiyun 		dd->err_info_rcv_constraint.pkey = pkey;
1851*4882a593Smuzhiyun 	}
1852*4882a593Smuzhiyun }
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun /*
1855*4882a593Smuzhiyun  * ingress_pkey_check - Return 0 if the ingress pkey is valid, return 1
1856*4882a593Smuzhiyun  * otherwise. Use the criteria in the OPAv1 spec, section 9.10.14. idx
1857*4882a593Smuzhiyun  * is a hint as to the best place in the partition key table to begin
1858*4882a593Smuzhiyun  * searching. This function should not be called on the data path because
1859*4882a593Smuzhiyun  * of performance reasons. On datapath pkey check is expected to be done
1860*4882a593Smuzhiyun  * by HW and rcv_pkey_check function should be called instead.
1861*4882a593Smuzhiyun  */
ingress_pkey_check(struct hfi1_pportdata * ppd,u16 pkey,u8 sc5,u8 idx,u32 slid,bool force)1862*4882a593Smuzhiyun static inline int ingress_pkey_check(struct hfi1_pportdata *ppd, u16 pkey,
1863*4882a593Smuzhiyun 				     u8 sc5, u8 idx, u32 slid, bool force)
1864*4882a593Smuzhiyun {
1865*4882a593Smuzhiyun 	if (!(force) && !(ppd->part_enforce & HFI1_PART_ENFORCE_IN))
1866*4882a593Smuzhiyun 		return 0;
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	/* If SC15, pkey[0:14] must be 0x7fff */
1869*4882a593Smuzhiyun 	if ((sc5 == 0xf) && ((pkey & PKEY_LOW_15_MASK) != PKEY_LOW_15_MASK))
1870*4882a593Smuzhiyun 		goto bad;
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 	/* Is the pkey = 0x0, or 0x8000? */
1873*4882a593Smuzhiyun 	if ((pkey & PKEY_LOW_15_MASK) == 0)
1874*4882a593Smuzhiyun 		goto bad;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	/* The most likely matching pkey has index 'idx' */
1877*4882a593Smuzhiyun 	if (ingress_pkey_matches_entry(pkey, ppd->pkeys[idx]))
1878*4882a593Smuzhiyun 		return 0;
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	/* no match - try the whole table */
1881*4882a593Smuzhiyun 	if (!ingress_pkey_table_search(ppd, pkey))
1882*4882a593Smuzhiyun 		return 0;
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun bad:
1885*4882a593Smuzhiyun 	ingress_pkey_table_fail(ppd, pkey, slid);
1886*4882a593Smuzhiyun 	return 1;
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun /*
1890*4882a593Smuzhiyun  * rcv_pkey_check - Return 0 if the ingress pkey is valid, return 1
1891*4882a593Smuzhiyun  * otherwise. It only ensures pkey is vlid for QP0. This function
1892*4882a593Smuzhiyun  * should be called on the data path instead of ingress_pkey_check
1893*4882a593Smuzhiyun  * as on data path, pkey check is done by HW (except for QP0).
1894*4882a593Smuzhiyun  */
rcv_pkey_check(struct hfi1_pportdata * ppd,u16 pkey,u8 sc5,u16 slid)1895*4882a593Smuzhiyun static inline int rcv_pkey_check(struct hfi1_pportdata *ppd, u16 pkey,
1896*4882a593Smuzhiyun 				 u8 sc5, u16 slid)
1897*4882a593Smuzhiyun {
1898*4882a593Smuzhiyun 	if (!(ppd->part_enforce & HFI1_PART_ENFORCE_IN))
1899*4882a593Smuzhiyun 		return 0;
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun 	/* If SC15, pkey[0:14] must be 0x7fff */
1902*4882a593Smuzhiyun 	if ((sc5 == 0xf) && ((pkey & PKEY_LOW_15_MASK) != PKEY_LOW_15_MASK))
1903*4882a593Smuzhiyun 		goto bad;
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	return 0;
1906*4882a593Smuzhiyun bad:
1907*4882a593Smuzhiyun 	ingress_pkey_table_fail(ppd, pkey, slid);
1908*4882a593Smuzhiyun 	return 1;
1909*4882a593Smuzhiyun }
1910*4882a593Smuzhiyun 
1911*4882a593Smuzhiyun /* MTU handling */
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun /* MTU enumeration, 256-4k match IB */
1914*4882a593Smuzhiyun #define OPA_MTU_0     0
1915*4882a593Smuzhiyun #define OPA_MTU_256   1
1916*4882a593Smuzhiyun #define OPA_MTU_512   2
1917*4882a593Smuzhiyun #define OPA_MTU_1024  3
1918*4882a593Smuzhiyun #define OPA_MTU_2048  4
1919*4882a593Smuzhiyun #define OPA_MTU_4096  5
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun u32 lrh_max_header_bytes(struct hfi1_devdata *dd);
1922*4882a593Smuzhiyun int mtu_to_enum(u32 mtu, int default_if_bad);
1923*4882a593Smuzhiyun u16 enum_to_mtu(int mtu);
valid_ib_mtu(unsigned int mtu)1924*4882a593Smuzhiyun static inline int valid_ib_mtu(unsigned int mtu)
1925*4882a593Smuzhiyun {
1926*4882a593Smuzhiyun 	return mtu == 256 || mtu == 512 ||
1927*4882a593Smuzhiyun 		mtu == 1024 || mtu == 2048 ||
1928*4882a593Smuzhiyun 		mtu == 4096;
1929*4882a593Smuzhiyun }
1930*4882a593Smuzhiyun 
valid_opa_max_mtu(unsigned int mtu)1931*4882a593Smuzhiyun static inline int valid_opa_max_mtu(unsigned int mtu)
1932*4882a593Smuzhiyun {
1933*4882a593Smuzhiyun 	return mtu >= 2048 &&
1934*4882a593Smuzhiyun 		(valid_ib_mtu(mtu) || mtu == 8192 || mtu == 10240);
1935*4882a593Smuzhiyun }
1936*4882a593Smuzhiyun 
1937*4882a593Smuzhiyun int set_mtu(struct hfi1_pportdata *ppd);
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc);
1940*4882a593Smuzhiyun void hfi1_disable_after_error(struct hfi1_devdata *dd);
1941*4882a593Smuzhiyun int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit);
1942*4882a593Smuzhiyun int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encode);
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun int fm_get_table(struct hfi1_pportdata *ppd, int which, void *t);
1945*4882a593Smuzhiyun int fm_set_table(struct hfi1_pportdata *ppd, int which, void *t);
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun void set_up_vau(struct hfi1_devdata *dd, u8 vau);
1948*4882a593Smuzhiyun void set_up_vl15(struct hfi1_devdata *dd, u16 vl15buf);
1949*4882a593Smuzhiyun void reset_link_credits(struct hfi1_devdata *dd);
1950*4882a593Smuzhiyun void assign_remote_cm_au_table(struct hfi1_devdata *dd, u8 vcu);
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun int set_buffer_control(struct hfi1_pportdata *ppd, struct buffer_control *bc);
1953*4882a593Smuzhiyun 
dd_from_ppd(struct hfi1_pportdata * ppd)1954*4882a593Smuzhiyun static inline struct hfi1_devdata *dd_from_ppd(struct hfi1_pportdata *ppd)
1955*4882a593Smuzhiyun {
1956*4882a593Smuzhiyun 	return ppd->dd;
1957*4882a593Smuzhiyun }
1958*4882a593Smuzhiyun 
dd_from_dev(struct hfi1_ibdev * dev)1959*4882a593Smuzhiyun static inline struct hfi1_devdata *dd_from_dev(struct hfi1_ibdev *dev)
1960*4882a593Smuzhiyun {
1961*4882a593Smuzhiyun 	return container_of(dev, struct hfi1_devdata, verbs_dev);
1962*4882a593Smuzhiyun }
1963*4882a593Smuzhiyun 
dd_from_ibdev(struct ib_device * ibdev)1964*4882a593Smuzhiyun static inline struct hfi1_devdata *dd_from_ibdev(struct ib_device *ibdev)
1965*4882a593Smuzhiyun {
1966*4882a593Smuzhiyun 	return dd_from_dev(to_idev(ibdev));
1967*4882a593Smuzhiyun }
1968*4882a593Smuzhiyun 
ppd_from_ibp(struct hfi1_ibport * ibp)1969*4882a593Smuzhiyun static inline struct hfi1_pportdata *ppd_from_ibp(struct hfi1_ibport *ibp)
1970*4882a593Smuzhiyun {
1971*4882a593Smuzhiyun 	return container_of(ibp, struct hfi1_pportdata, ibport_data);
1972*4882a593Smuzhiyun }
1973*4882a593Smuzhiyun 
dev_from_rdi(struct rvt_dev_info * rdi)1974*4882a593Smuzhiyun static inline struct hfi1_ibdev *dev_from_rdi(struct rvt_dev_info *rdi)
1975*4882a593Smuzhiyun {
1976*4882a593Smuzhiyun 	return container_of(rdi, struct hfi1_ibdev, rdi);
1977*4882a593Smuzhiyun }
1978*4882a593Smuzhiyun 
to_iport(struct ib_device * ibdev,u8 port)1979*4882a593Smuzhiyun static inline struct hfi1_ibport *to_iport(struct ib_device *ibdev, u8 port)
1980*4882a593Smuzhiyun {
1981*4882a593Smuzhiyun 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1982*4882a593Smuzhiyun 	unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun 	WARN_ON(pidx >= dd->num_pports);
1985*4882a593Smuzhiyun 	return &dd->pport[pidx].ibport_data;
1986*4882a593Smuzhiyun }
1987*4882a593Smuzhiyun 
rcd_to_iport(struct hfi1_ctxtdata * rcd)1988*4882a593Smuzhiyun static inline struct hfi1_ibport *rcd_to_iport(struct hfi1_ctxtdata *rcd)
1989*4882a593Smuzhiyun {
1990*4882a593Smuzhiyun 	return &rcd->ppd->ibport_data;
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun /**
1994*4882a593Smuzhiyun  * hfi1_may_ecn - Check whether FECN or BECN processing should be done
1995*4882a593Smuzhiyun  * @pkt: the packet to be evaluated
1996*4882a593Smuzhiyun  *
1997*4882a593Smuzhiyun  * Check whether the FECN or BECN bits in the packet's header are
1998*4882a593Smuzhiyun  * enabled, depending on packet type.
1999*4882a593Smuzhiyun  *
2000*4882a593Smuzhiyun  * This function only checks for FECN and BECN bits. Additional checks
2001*4882a593Smuzhiyun  * are done in the slowpath (hfi1_process_ecn_slowpath()) in order to
2002*4882a593Smuzhiyun  * ensure correct handling.
2003*4882a593Smuzhiyun  */
hfi1_may_ecn(struct hfi1_packet * pkt)2004*4882a593Smuzhiyun static inline bool hfi1_may_ecn(struct hfi1_packet *pkt)
2005*4882a593Smuzhiyun {
2006*4882a593Smuzhiyun 	bool fecn, becn;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	if (pkt->etype == RHF_RCV_TYPE_BYPASS) {
2009*4882a593Smuzhiyun 		fecn = hfi1_16B_get_fecn(pkt->hdr);
2010*4882a593Smuzhiyun 		becn = hfi1_16B_get_becn(pkt->hdr);
2011*4882a593Smuzhiyun 	} else {
2012*4882a593Smuzhiyun 		fecn = ib_bth_get_fecn(pkt->ohdr);
2013*4882a593Smuzhiyun 		becn = ib_bth_get_becn(pkt->ohdr);
2014*4882a593Smuzhiyun 	}
2015*4882a593Smuzhiyun 	return fecn || becn;
2016*4882a593Smuzhiyun }
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun bool hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
2019*4882a593Smuzhiyun 			       bool prescan);
process_ecn(struct rvt_qp * qp,struct hfi1_packet * pkt)2020*4882a593Smuzhiyun static inline bool process_ecn(struct rvt_qp *qp, struct hfi1_packet *pkt)
2021*4882a593Smuzhiyun {
2022*4882a593Smuzhiyun 	bool do_work;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 	do_work = hfi1_may_ecn(pkt);
2025*4882a593Smuzhiyun 	if (unlikely(do_work))
2026*4882a593Smuzhiyun 		return hfi1_process_ecn_slowpath(qp, pkt, false);
2027*4882a593Smuzhiyun 	return false;
2028*4882a593Smuzhiyun }
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun /*
2031*4882a593Smuzhiyun  * Return the indexed PKEY from the port PKEY table.
2032*4882a593Smuzhiyun  */
hfi1_get_pkey(struct hfi1_ibport * ibp,unsigned index)2033*4882a593Smuzhiyun static inline u16 hfi1_get_pkey(struct hfi1_ibport *ibp, unsigned index)
2034*4882a593Smuzhiyun {
2035*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2036*4882a593Smuzhiyun 	u16 ret;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	if (index >= ARRAY_SIZE(ppd->pkeys))
2039*4882a593Smuzhiyun 		ret = 0;
2040*4882a593Smuzhiyun 	else
2041*4882a593Smuzhiyun 		ret = ppd->pkeys[index];
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun 	return ret;
2044*4882a593Smuzhiyun }
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun /*
2047*4882a593Smuzhiyun  * Return the indexed GUID from the port GUIDs table.
2048*4882a593Smuzhiyun  */
get_sguid(struct hfi1_ibport * ibp,unsigned int index)2049*4882a593Smuzhiyun static inline __be64 get_sguid(struct hfi1_ibport *ibp, unsigned int index)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun 	WARN_ON(index >= HFI1_GUIDS_PER_PORT);
2054*4882a593Smuzhiyun 	return cpu_to_be64(ppd->guids[index]);
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun /*
2058*4882a593Smuzhiyun  * Called by readers of cc_state only, must call under rcu_read_lock().
2059*4882a593Smuzhiyun  */
get_cc_state(struct hfi1_pportdata * ppd)2060*4882a593Smuzhiyun static inline struct cc_state *get_cc_state(struct hfi1_pportdata *ppd)
2061*4882a593Smuzhiyun {
2062*4882a593Smuzhiyun 	return rcu_dereference(ppd->cc_state);
2063*4882a593Smuzhiyun }
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun /*
2066*4882a593Smuzhiyun  * Called by writers of cc_state only,  must call under cc_state_lock.
2067*4882a593Smuzhiyun  */
2068*4882a593Smuzhiyun static inline
get_cc_state_protected(struct hfi1_pportdata * ppd)2069*4882a593Smuzhiyun struct cc_state *get_cc_state_protected(struct hfi1_pportdata *ppd)
2070*4882a593Smuzhiyun {
2071*4882a593Smuzhiyun 	return rcu_dereference_protected(ppd->cc_state,
2072*4882a593Smuzhiyun 					 lockdep_is_held(&ppd->cc_state_lock));
2073*4882a593Smuzhiyun }
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun /*
2076*4882a593Smuzhiyun  * values for dd->flags (_device_ related flags)
2077*4882a593Smuzhiyun  */
2078*4882a593Smuzhiyun #define HFI1_INITTED           0x1    /* chip and driver up and initted */
2079*4882a593Smuzhiyun #define HFI1_PRESENT           0x2    /* chip accesses can be done */
2080*4882a593Smuzhiyun #define HFI1_FROZEN            0x4    /* chip in SPC freeze */
2081*4882a593Smuzhiyun #define HFI1_HAS_SDMA_TIMEOUT  0x8
2082*4882a593Smuzhiyun #define HFI1_HAS_SEND_DMA      0x10   /* Supports Send DMA */
2083*4882a593Smuzhiyun #define HFI1_FORCED_FREEZE     0x80   /* driver forced freeze mode */
2084*4882a593Smuzhiyun #define HFI1_SHUTDOWN          0x100  /* device is shutting down */
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun /* IB dword length mask in PBC (lower 11 bits); same for all chips */
2087*4882a593Smuzhiyun #define HFI1_PBC_LENGTH_MASK                     ((1 << 11) - 1)
2088*4882a593Smuzhiyun 
2089*4882a593Smuzhiyun /* ctxt_flag bit offsets */
2090*4882a593Smuzhiyun 		/* base context has not finished initializing */
2091*4882a593Smuzhiyun #define HFI1_CTXT_BASE_UNINIT 1
2092*4882a593Smuzhiyun 		/* base context initaliation failed */
2093*4882a593Smuzhiyun #define HFI1_CTXT_BASE_FAILED 2
2094*4882a593Smuzhiyun 		/* waiting for a packet to arrive */
2095*4882a593Smuzhiyun #define HFI1_CTXT_WAITING_RCV 3
2096*4882a593Smuzhiyun 		/* waiting for an urgent packet to arrive */
2097*4882a593Smuzhiyun #define HFI1_CTXT_WAITING_URG 4
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun /* free up any allocated data at closes */
2100*4882a593Smuzhiyun int hfi1_init_dd(struct hfi1_devdata *dd);
2101*4882a593Smuzhiyun void hfi1_free_devdata(struct hfi1_devdata *dd);
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun /* LED beaconing functions */
2104*4882a593Smuzhiyun void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
2105*4882a593Smuzhiyun 			     unsigned int timeoff);
2106*4882a593Smuzhiyun void shutdown_led_override(struct hfi1_pportdata *ppd);
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun #define HFI1_CREDIT_RETURN_RATE (100)
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun /*
2111*4882a593Smuzhiyun  * The number of words for the KDETH protocol field.  If this is
2112*4882a593Smuzhiyun  * larger then the actual field used, then part of the payload
2113*4882a593Smuzhiyun  * will be in the header.
2114*4882a593Smuzhiyun  *
2115*4882a593Smuzhiyun  * Optimally, we want this sized so that a typical case will
2116*4882a593Smuzhiyun  * use full cache lines.  The typical local KDETH header would
2117*4882a593Smuzhiyun  * be:
2118*4882a593Smuzhiyun  *
2119*4882a593Smuzhiyun  *	Bytes	Field
2120*4882a593Smuzhiyun  *	  8	LRH
2121*4882a593Smuzhiyun  *	 12	BHT
2122*4882a593Smuzhiyun  *	 ??	KDETH
2123*4882a593Smuzhiyun  *	  8	RHF
2124*4882a593Smuzhiyun  *	---
2125*4882a593Smuzhiyun  *	 28 + KDETH
2126*4882a593Smuzhiyun  *
2127*4882a593Smuzhiyun  * For a 64-byte cache line, KDETH would need to be 36 bytes or 9 DWORDS
2128*4882a593Smuzhiyun  */
2129*4882a593Smuzhiyun #define DEFAULT_RCVHDRSIZE 9
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun /*
2132*4882a593Smuzhiyun  * Maximal header byte count:
2133*4882a593Smuzhiyun  *
2134*4882a593Smuzhiyun  *	Bytes	Field
2135*4882a593Smuzhiyun  *	  8	LRH
2136*4882a593Smuzhiyun  *	 40	GRH (optional)
2137*4882a593Smuzhiyun  *	 12	BTH
2138*4882a593Smuzhiyun  *	 ??	KDETH
2139*4882a593Smuzhiyun  *	  8	RHF
2140*4882a593Smuzhiyun  *	---
2141*4882a593Smuzhiyun  *	 68 + KDETH
2142*4882a593Smuzhiyun  *
2143*4882a593Smuzhiyun  * We also want to maintain a cache line alignment to assist DMA'ing
2144*4882a593Smuzhiyun  * of the header bytes.  Round up to a good size.
2145*4882a593Smuzhiyun  */
2146*4882a593Smuzhiyun #define DEFAULT_RCVHDR_ENTSIZE 32
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
2149*4882a593Smuzhiyun 			u32 nlocked, u32 npages);
2150*4882a593Smuzhiyun int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr,
2151*4882a593Smuzhiyun 			    size_t npages, bool writable, struct page **pages);
2152*4882a593Smuzhiyun void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
2153*4882a593Smuzhiyun 			     size_t npages, bool dirty);
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun /**
2156*4882a593Smuzhiyun  * hfi1_rcvhdrtail_kvaddr - return tail kvaddr
2157*4882a593Smuzhiyun  * @rcd - the receive context
2158*4882a593Smuzhiyun  */
hfi1_rcvhdrtail_kvaddr(const struct hfi1_ctxtdata * rcd)2159*4882a593Smuzhiyun static inline __le64 *hfi1_rcvhdrtail_kvaddr(const struct hfi1_ctxtdata *rcd)
2160*4882a593Smuzhiyun {
2161*4882a593Smuzhiyun 	return (__le64 *)rcd->rcvhdrtail_kvaddr;
2162*4882a593Smuzhiyun }
2163*4882a593Smuzhiyun 
clear_rcvhdrtail(const struct hfi1_ctxtdata * rcd)2164*4882a593Smuzhiyun static inline void clear_rcvhdrtail(const struct hfi1_ctxtdata *rcd)
2165*4882a593Smuzhiyun {
2166*4882a593Smuzhiyun 	u64 *kv = (u64 *)hfi1_rcvhdrtail_kvaddr(rcd);
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun 	if (kv)
2169*4882a593Smuzhiyun 		*kv = 0ULL;
2170*4882a593Smuzhiyun }
2171*4882a593Smuzhiyun 
get_rcvhdrtail(const struct hfi1_ctxtdata * rcd)2172*4882a593Smuzhiyun static inline u32 get_rcvhdrtail(const struct hfi1_ctxtdata *rcd)
2173*4882a593Smuzhiyun {
2174*4882a593Smuzhiyun 	/*
2175*4882a593Smuzhiyun 	 * volatile because it's a DMA target from the chip, routine is
2176*4882a593Smuzhiyun 	 * inlined, and don't want register caching or reordering.
2177*4882a593Smuzhiyun 	 */
2178*4882a593Smuzhiyun 	return (u32)le64_to_cpu(*hfi1_rcvhdrtail_kvaddr(rcd));
2179*4882a593Smuzhiyun }
2180*4882a593Smuzhiyun 
hfi1_packet_present(struct hfi1_ctxtdata * rcd)2181*4882a593Smuzhiyun static inline bool hfi1_packet_present(struct hfi1_ctxtdata *rcd)
2182*4882a593Smuzhiyun {
2183*4882a593Smuzhiyun 	if (likely(!rcd->rcvhdrtail_kvaddr)) {
2184*4882a593Smuzhiyun 		u32 seq = rhf_rcv_seq(rhf_to_cpu(get_rhf_addr(rcd)));
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 		return !last_rcv_seq(rcd, seq);
2187*4882a593Smuzhiyun 	}
2188*4882a593Smuzhiyun 	return hfi1_rcd_head(rcd) != get_rcvhdrtail(rcd);
2189*4882a593Smuzhiyun }
2190*4882a593Smuzhiyun 
2191*4882a593Smuzhiyun /*
2192*4882a593Smuzhiyun  * sysfs interface.
2193*4882a593Smuzhiyun  */
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun extern const char ib_hfi1_version[];
2196*4882a593Smuzhiyun extern const struct attribute_group ib_hfi1_attr_group;
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun int hfi1_device_create(struct hfi1_devdata *dd);
2199*4882a593Smuzhiyun void hfi1_device_remove(struct hfi1_devdata *dd);
2200*4882a593Smuzhiyun 
2201*4882a593Smuzhiyun int hfi1_create_port_files(struct ib_device *ibdev, u8 port_num,
2202*4882a593Smuzhiyun 			   struct kobject *kobj);
2203*4882a593Smuzhiyun int hfi1_verbs_register_sysfs(struct hfi1_devdata *dd);
2204*4882a593Smuzhiyun void hfi1_verbs_unregister_sysfs(struct hfi1_devdata *dd);
2205*4882a593Smuzhiyun /* Hook for sysfs read of QSFP */
2206*4882a593Smuzhiyun int qsfp_dump(struct hfi1_pportdata *ppd, char *buf, int len);
2207*4882a593Smuzhiyun 
2208*4882a593Smuzhiyun int hfi1_pcie_init(struct hfi1_devdata *dd);
2209*4882a593Smuzhiyun void hfi1_pcie_cleanup(struct pci_dev *pdev);
2210*4882a593Smuzhiyun int hfi1_pcie_ddinit(struct hfi1_devdata *dd, struct pci_dev *pdev);
2211*4882a593Smuzhiyun void hfi1_pcie_ddcleanup(struct hfi1_devdata *);
2212*4882a593Smuzhiyun int pcie_speeds(struct hfi1_devdata *dd);
2213*4882a593Smuzhiyun int restore_pci_variables(struct hfi1_devdata *dd);
2214*4882a593Smuzhiyun int save_pci_variables(struct hfi1_devdata *dd);
2215*4882a593Smuzhiyun int do_pcie_gen3_transition(struct hfi1_devdata *dd);
2216*4882a593Smuzhiyun void tune_pcie_caps(struct hfi1_devdata *dd);
2217*4882a593Smuzhiyun int parse_platform_config(struct hfi1_devdata *dd);
2218*4882a593Smuzhiyun int get_platform_config_field(struct hfi1_devdata *dd,
2219*4882a593Smuzhiyun 			      enum platform_config_table_type_encoding
2220*4882a593Smuzhiyun 			      table_type, int table_index, int field_index,
2221*4882a593Smuzhiyun 			      u32 *data, u32 len);
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun struct pci_dev *get_pci_dev(struct rvt_dev_info *rdi);
2224*4882a593Smuzhiyun 
2225*4882a593Smuzhiyun /*
2226*4882a593Smuzhiyun  * Flush write combining store buffers (if present) and perform a write
2227*4882a593Smuzhiyun  * barrier.
2228*4882a593Smuzhiyun  */
flush_wc(void)2229*4882a593Smuzhiyun static inline void flush_wc(void)
2230*4882a593Smuzhiyun {
2231*4882a593Smuzhiyun 	asm volatile("sfence" : : : "memory");
2232*4882a593Smuzhiyun }
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun void handle_eflags(struct hfi1_packet *packet);
2235*4882a593Smuzhiyun void seqfile_dump_rcd(struct seq_file *s, struct hfi1_ctxtdata *rcd);
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun /* global module parameter variables */
2238*4882a593Smuzhiyun extern unsigned int hfi1_max_mtu;
2239*4882a593Smuzhiyun extern unsigned int hfi1_cu;
2240*4882a593Smuzhiyun extern unsigned int user_credit_return_threshold;
2241*4882a593Smuzhiyun extern int num_user_contexts;
2242*4882a593Smuzhiyun extern unsigned long n_krcvqs;
2243*4882a593Smuzhiyun extern uint krcvqs[];
2244*4882a593Smuzhiyun extern int krcvqsset;
2245*4882a593Smuzhiyun extern uint loopback;
2246*4882a593Smuzhiyun extern uint quick_linkup;
2247*4882a593Smuzhiyun extern uint rcv_intr_timeout;
2248*4882a593Smuzhiyun extern uint rcv_intr_count;
2249*4882a593Smuzhiyun extern uint rcv_intr_dynamic;
2250*4882a593Smuzhiyun extern ushort link_crc_mask;
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun extern struct mutex hfi1_mutex;
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun /* Number of seconds before our card status check...  */
2255*4882a593Smuzhiyun #define STATUS_TIMEOUT 60
2256*4882a593Smuzhiyun 
2257*4882a593Smuzhiyun #define DRIVER_NAME		"hfi1"
2258*4882a593Smuzhiyun #define HFI1_USER_MINOR_BASE     0
2259*4882a593Smuzhiyun #define HFI1_TRACE_MINOR         127
2260*4882a593Smuzhiyun #define HFI1_NMINORS             255
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun #define PCI_VENDOR_ID_INTEL 0x8086
2263*4882a593Smuzhiyun #define PCI_DEVICE_ID_INTEL0 0x24f0
2264*4882a593Smuzhiyun #define PCI_DEVICE_ID_INTEL1 0x24f1
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun #define HFI1_PKT_USER_SC_INTEGRITY					    \
2267*4882a593Smuzhiyun 	(SEND_CTXT_CHECK_ENABLE_DISALLOW_NON_KDETH_PACKETS_SMASK	    \
2268*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_KDETH_PACKETS_SMASK		\
2269*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_BYPASS_SMASK		    \
2270*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_GRH_SMASK)
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun #define HFI1_PKT_KERNEL_SC_INTEGRITY					    \
2273*4882a593Smuzhiyun 	(SEND_CTXT_CHECK_ENABLE_DISALLOW_KDETH_PACKETS_SMASK)
2274*4882a593Smuzhiyun 
hfi1_pkt_default_send_ctxt_mask(struct hfi1_devdata * dd,u16 ctxt_type)2275*4882a593Smuzhiyun static inline u64 hfi1_pkt_default_send_ctxt_mask(struct hfi1_devdata *dd,
2276*4882a593Smuzhiyun 						  u16 ctxt_type)
2277*4882a593Smuzhiyun {
2278*4882a593Smuzhiyun 	u64 base_sc_integrity;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	/* No integrity checks if HFI1_CAP_NO_INTEGRITY is set */
2281*4882a593Smuzhiyun 	if (HFI1_CAP_IS_KSET(NO_INTEGRITY))
2282*4882a593Smuzhiyun 		return 0;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 	base_sc_integrity =
2285*4882a593Smuzhiyun 	SEND_CTXT_CHECK_ENABLE_DISALLOW_BYPASS_BAD_PKT_LEN_SMASK
2286*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_PBC_STATIC_RATE_CONTROL_SMASK
2287*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_TOO_LONG_BYPASS_PACKETS_SMASK
2288*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_TOO_LONG_IB_PACKETS_SMASK
2289*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_BAD_PKT_LEN_SMASK
2290*4882a593Smuzhiyun #ifndef CONFIG_FAULT_INJECTION
2291*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_PBC_TEST_SMASK
2292*4882a593Smuzhiyun #endif
2293*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_TOO_SMALL_BYPASS_PACKETS_SMASK
2294*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_TOO_SMALL_IB_PACKETS_SMASK
2295*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_RAW_IPV6_SMASK
2296*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_DISALLOW_RAW_SMASK
2297*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_CHECK_BYPASS_VL_MAPPING_SMASK
2298*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_CHECK_VL_MAPPING_SMASK
2299*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_CHECK_OPCODE_SMASK
2300*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_CHECK_SLID_SMASK
2301*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_CHECK_VL_SMASK
2302*4882a593Smuzhiyun 	| SEND_CTXT_CHECK_ENABLE_CHECK_ENABLE_SMASK;
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 	if (ctxt_type == SC_USER)
2305*4882a593Smuzhiyun 		base_sc_integrity |=
2306*4882a593Smuzhiyun #ifndef CONFIG_FAULT_INJECTION
2307*4882a593Smuzhiyun 			SEND_CTXT_CHECK_ENABLE_DISALLOW_PBC_TEST_SMASK |
2308*4882a593Smuzhiyun #endif
2309*4882a593Smuzhiyun 			HFI1_PKT_USER_SC_INTEGRITY;
2310*4882a593Smuzhiyun 	else if (ctxt_type != SC_KERNEL)
2311*4882a593Smuzhiyun 		base_sc_integrity |= HFI1_PKT_KERNEL_SC_INTEGRITY;
2312*4882a593Smuzhiyun 
2313*4882a593Smuzhiyun 	/* turn on send-side job key checks if !A0 */
2314*4882a593Smuzhiyun 	if (!is_ax(dd))
2315*4882a593Smuzhiyun 		base_sc_integrity |= SEND_CTXT_CHECK_ENABLE_CHECK_JOB_KEY_SMASK;
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun 	return base_sc_integrity;
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun 
hfi1_pkt_base_sdma_integrity(struct hfi1_devdata * dd)2320*4882a593Smuzhiyun static inline u64 hfi1_pkt_base_sdma_integrity(struct hfi1_devdata *dd)
2321*4882a593Smuzhiyun {
2322*4882a593Smuzhiyun 	u64 base_sdma_integrity;
2323*4882a593Smuzhiyun 
2324*4882a593Smuzhiyun 	/* No integrity checks if HFI1_CAP_NO_INTEGRITY is set */
2325*4882a593Smuzhiyun 	if (HFI1_CAP_IS_KSET(NO_INTEGRITY))
2326*4882a593Smuzhiyun 		return 0;
2327*4882a593Smuzhiyun 
2328*4882a593Smuzhiyun 	base_sdma_integrity =
2329*4882a593Smuzhiyun 	SEND_DMA_CHECK_ENABLE_DISALLOW_BYPASS_BAD_PKT_LEN_SMASK
2330*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_TOO_LONG_BYPASS_PACKETS_SMASK
2331*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_TOO_LONG_IB_PACKETS_SMASK
2332*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_BAD_PKT_LEN_SMASK
2333*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_TOO_SMALL_BYPASS_PACKETS_SMASK
2334*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_TOO_SMALL_IB_PACKETS_SMASK
2335*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_RAW_IPV6_SMASK
2336*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_DISALLOW_RAW_SMASK
2337*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_CHECK_BYPASS_VL_MAPPING_SMASK
2338*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_CHECK_VL_MAPPING_SMASK
2339*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_CHECK_OPCODE_SMASK
2340*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_CHECK_SLID_SMASK
2341*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_CHECK_VL_SMASK
2342*4882a593Smuzhiyun 	| SEND_DMA_CHECK_ENABLE_CHECK_ENABLE_SMASK;
2343*4882a593Smuzhiyun 
2344*4882a593Smuzhiyun 	if (!HFI1_CAP_IS_KSET(STATIC_RATE_CTRL))
2345*4882a593Smuzhiyun 		base_sdma_integrity |=
2346*4882a593Smuzhiyun 		SEND_DMA_CHECK_ENABLE_DISALLOW_PBC_STATIC_RATE_CONTROL_SMASK;
2347*4882a593Smuzhiyun 
2348*4882a593Smuzhiyun 	/* turn on send-side job key checks if !A0 */
2349*4882a593Smuzhiyun 	if (!is_ax(dd))
2350*4882a593Smuzhiyun 		base_sdma_integrity |=
2351*4882a593Smuzhiyun 			SEND_DMA_CHECK_ENABLE_CHECK_JOB_KEY_SMASK;
2352*4882a593Smuzhiyun 
2353*4882a593Smuzhiyun 	return base_sdma_integrity;
2354*4882a593Smuzhiyun }
2355*4882a593Smuzhiyun 
2356*4882a593Smuzhiyun #define dd_dev_emerg(dd, fmt, ...) \
2357*4882a593Smuzhiyun 	dev_emerg(&(dd)->pcidev->dev, "%s: " fmt, \
2358*4882a593Smuzhiyun 		  rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), ##__VA_ARGS__)
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun #define dd_dev_err(dd, fmt, ...) \
2361*4882a593Smuzhiyun 	dev_err(&(dd)->pcidev->dev, "%s: " fmt, \
2362*4882a593Smuzhiyun 		rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), ##__VA_ARGS__)
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun #define dd_dev_err_ratelimited(dd, fmt, ...) \
2365*4882a593Smuzhiyun 	dev_err_ratelimited(&(dd)->pcidev->dev, "%s: " fmt, \
2366*4882a593Smuzhiyun 			    rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), \
2367*4882a593Smuzhiyun 			    ##__VA_ARGS__)
2368*4882a593Smuzhiyun 
2369*4882a593Smuzhiyun #define dd_dev_warn(dd, fmt, ...) \
2370*4882a593Smuzhiyun 	dev_warn(&(dd)->pcidev->dev, "%s: " fmt, \
2371*4882a593Smuzhiyun 		 rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), ##__VA_ARGS__)
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun #define dd_dev_warn_ratelimited(dd, fmt, ...) \
2374*4882a593Smuzhiyun 	dev_warn_ratelimited(&(dd)->pcidev->dev, "%s: " fmt, \
2375*4882a593Smuzhiyun 			     rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), \
2376*4882a593Smuzhiyun 			     ##__VA_ARGS__)
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun #define dd_dev_info(dd, fmt, ...) \
2379*4882a593Smuzhiyun 	dev_info(&(dd)->pcidev->dev, "%s: " fmt, \
2380*4882a593Smuzhiyun 		 rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), ##__VA_ARGS__)
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun #define dd_dev_info_ratelimited(dd, fmt, ...) \
2383*4882a593Smuzhiyun 	dev_info_ratelimited(&(dd)->pcidev->dev, "%s: " fmt, \
2384*4882a593Smuzhiyun 			     rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), \
2385*4882a593Smuzhiyun 			     ##__VA_ARGS__)
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun #define dd_dev_dbg(dd, fmt, ...) \
2388*4882a593Smuzhiyun 	dev_dbg(&(dd)->pcidev->dev, "%s: " fmt, \
2389*4882a593Smuzhiyun 		rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), ##__VA_ARGS__)
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun #define hfi1_dev_porterr(dd, port, fmt, ...) \
2392*4882a593Smuzhiyun 	dev_err(&(dd)->pcidev->dev, "%s: port %u: " fmt, \
2393*4882a593Smuzhiyun 		rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), (port), ##__VA_ARGS__)
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun /*
2396*4882a593Smuzhiyun  * this is used for formatting hw error messages...
2397*4882a593Smuzhiyun  */
2398*4882a593Smuzhiyun struct hfi1_hwerror_msgs {
2399*4882a593Smuzhiyun 	u64 mask;
2400*4882a593Smuzhiyun 	const char *msg;
2401*4882a593Smuzhiyun 	size_t sz;
2402*4882a593Smuzhiyun };
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun /* in intr.c... */
2405*4882a593Smuzhiyun void hfi1_format_hwerrors(u64 hwerrs,
2406*4882a593Smuzhiyun 			  const struct hfi1_hwerror_msgs *hwerrmsgs,
2407*4882a593Smuzhiyun 			  size_t nhwerrmsgs, char *msg, size_t lmsg);
2408*4882a593Smuzhiyun 
2409*4882a593Smuzhiyun #define USER_OPCODE_CHECK_VAL 0xC0
2410*4882a593Smuzhiyun #define USER_OPCODE_CHECK_MASK 0xC0
2411*4882a593Smuzhiyun #define OPCODE_CHECK_VAL_DISABLED 0x0
2412*4882a593Smuzhiyun #define OPCODE_CHECK_MASK_DISABLED 0x0
2413*4882a593Smuzhiyun 
hfi1_reset_cpu_counters(struct hfi1_devdata * dd)2414*4882a593Smuzhiyun static inline void hfi1_reset_cpu_counters(struct hfi1_devdata *dd)
2415*4882a593Smuzhiyun {
2416*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
2417*4882a593Smuzhiyun 	int i;
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 	dd->z_int_counter = get_all_cpu_total(dd->int_counter);
2420*4882a593Smuzhiyun 	dd->z_rcv_limit = get_all_cpu_total(dd->rcv_limit);
2421*4882a593Smuzhiyun 	dd->z_send_schedule = get_all_cpu_total(dd->send_schedule);
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 	ppd = (struct hfi1_pportdata *)(dd + 1);
2424*4882a593Smuzhiyun 	for (i = 0; i < dd->num_pports; i++, ppd++) {
2425*4882a593Smuzhiyun 		ppd->ibport_data.rvp.z_rc_acks =
2426*4882a593Smuzhiyun 			get_all_cpu_total(ppd->ibport_data.rvp.rc_acks);
2427*4882a593Smuzhiyun 		ppd->ibport_data.rvp.z_rc_qacks =
2428*4882a593Smuzhiyun 			get_all_cpu_total(ppd->ibport_data.rvp.rc_qacks);
2429*4882a593Smuzhiyun 	}
2430*4882a593Smuzhiyun }
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun /* Control LED state */
setextled(struct hfi1_devdata * dd,u32 on)2433*4882a593Smuzhiyun static inline void setextled(struct hfi1_devdata *dd, u32 on)
2434*4882a593Smuzhiyun {
2435*4882a593Smuzhiyun 	if (on)
2436*4882a593Smuzhiyun 		write_csr(dd, DCC_CFG_LED_CNTRL, 0x1F);
2437*4882a593Smuzhiyun 	else
2438*4882a593Smuzhiyun 		write_csr(dd, DCC_CFG_LED_CNTRL, 0x10);
2439*4882a593Smuzhiyun }
2440*4882a593Smuzhiyun 
2441*4882a593Smuzhiyun /* return the i2c resource given the target */
i2c_target(u32 target)2442*4882a593Smuzhiyun static inline u32 i2c_target(u32 target)
2443*4882a593Smuzhiyun {
2444*4882a593Smuzhiyun 	return target ? CR_I2C2 : CR_I2C1;
2445*4882a593Smuzhiyun }
2446*4882a593Smuzhiyun 
2447*4882a593Smuzhiyun /* return the i2c chain chip resource that this HFI uses for QSFP */
qsfp_resource(struct hfi1_devdata * dd)2448*4882a593Smuzhiyun static inline u32 qsfp_resource(struct hfi1_devdata *dd)
2449*4882a593Smuzhiyun {
2450*4882a593Smuzhiyun 	return i2c_target(dd->hfi1_id);
2451*4882a593Smuzhiyun }
2452*4882a593Smuzhiyun 
2453*4882a593Smuzhiyun /* Is this device integrated or discrete? */
is_integrated(struct hfi1_devdata * dd)2454*4882a593Smuzhiyun static inline bool is_integrated(struct hfi1_devdata *dd)
2455*4882a593Smuzhiyun {
2456*4882a593Smuzhiyun 	return dd->pcidev->device == PCI_DEVICE_ID_INTEL1;
2457*4882a593Smuzhiyun }
2458*4882a593Smuzhiyun 
2459*4882a593Smuzhiyun /**
2460*4882a593Smuzhiyun  * hfi1_need_drop - detect need for drop
2461*4882a593Smuzhiyun  * @dd: - the device
2462*4882a593Smuzhiyun  *
2463*4882a593Smuzhiyun  * In some cases, the first packet needs to be dropped.
2464*4882a593Smuzhiyun  *
2465*4882a593Smuzhiyun  * Return true is the current packet needs to be dropped and false otherwise.
2466*4882a593Smuzhiyun  */
hfi1_need_drop(struct hfi1_devdata * dd)2467*4882a593Smuzhiyun static inline bool hfi1_need_drop(struct hfi1_devdata *dd)
2468*4882a593Smuzhiyun {
2469*4882a593Smuzhiyun 	if (unlikely(dd->do_drop &&
2470*4882a593Smuzhiyun 		     atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
2471*4882a593Smuzhiyun 		     DROP_PACKET_ON)) {
2472*4882a593Smuzhiyun 		dd->do_drop = false;
2473*4882a593Smuzhiyun 		return true;
2474*4882a593Smuzhiyun 	}
2475*4882a593Smuzhiyun 	return false;
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun int hfi1_tempsense_rd(struct hfi1_devdata *dd, struct hfi1_temp *temp);
2479*4882a593Smuzhiyun 
2480*4882a593Smuzhiyun #define DD_DEV_ENTRY(dd)       __string(dev, dev_name(&(dd)->pcidev->dev))
2481*4882a593Smuzhiyun #define DD_DEV_ASSIGN(dd)      __assign_str(dev, dev_name(&(dd)->pcidev->dev))
2482*4882a593Smuzhiyun 
hfi1_update_ah_attr(struct ib_device * ibdev,struct rdma_ah_attr * attr)2483*4882a593Smuzhiyun static inline void hfi1_update_ah_attr(struct ib_device *ibdev,
2484*4882a593Smuzhiyun 				       struct rdma_ah_attr *attr)
2485*4882a593Smuzhiyun {
2486*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
2487*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
2488*4882a593Smuzhiyun 	u32 dlid = rdma_ah_get_dlid(attr);
2489*4882a593Smuzhiyun 
2490*4882a593Smuzhiyun 	/*
2491*4882a593Smuzhiyun 	 * Kernel clients may not have setup GRH information
2492*4882a593Smuzhiyun 	 * Set that here.
2493*4882a593Smuzhiyun 	 */
2494*4882a593Smuzhiyun 	ibp = to_iport(ibdev, rdma_ah_get_port_num(attr));
2495*4882a593Smuzhiyun 	ppd = ppd_from_ibp(ibp);
2496*4882a593Smuzhiyun 	if ((((dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) ||
2497*4882a593Smuzhiyun 	      (ppd->lid >= be16_to_cpu(IB_MULTICAST_LID_BASE))) &&
2498*4882a593Smuzhiyun 	    (dlid != be32_to_cpu(OPA_LID_PERMISSIVE)) &&
2499*4882a593Smuzhiyun 	    (dlid != be16_to_cpu(IB_LID_PERMISSIVE)) &&
2500*4882a593Smuzhiyun 	    (!(rdma_ah_get_ah_flags(attr) & IB_AH_GRH))) ||
2501*4882a593Smuzhiyun 	    (rdma_ah_get_make_grd(attr))) {
2502*4882a593Smuzhiyun 		rdma_ah_set_ah_flags(attr, IB_AH_GRH);
2503*4882a593Smuzhiyun 		rdma_ah_set_interface_id(attr, OPA_MAKE_ID(dlid));
2504*4882a593Smuzhiyun 		rdma_ah_set_subnet_prefix(attr, ibp->rvp.gid_prefix);
2505*4882a593Smuzhiyun 	}
2506*4882a593Smuzhiyun }
2507*4882a593Smuzhiyun 
2508*4882a593Smuzhiyun /*
2509*4882a593Smuzhiyun  * hfi1_check_mcast- Check if the given lid is
2510*4882a593Smuzhiyun  * in the OPA multicast range.
2511*4882a593Smuzhiyun  *
2512*4882a593Smuzhiyun  * The LID might either reside in ah.dlid or might be
2513*4882a593Smuzhiyun  * in the GRH of the address handle as DGID if extended
2514*4882a593Smuzhiyun  * addresses are in use.
2515*4882a593Smuzhiyun  */
hfi1_check_mcast(u32 lid)2516*4882a593Smuzhiyun static inline bool hfi1_check_mcast(u32 lid)
2517*4882a593Smuzhiyun {
2518*4882a593Smuzhiyun 	return ((lid >= opa_get_mcast_base(OPA_MCAST_NR)) &&
2519*4882a593Smuzhiyun 		(lid != be32_to_cpu(OPA_LID_PERMISSIVE)));
2520*4882a593Smuzhiyun }
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun #define opa_get_lid(lid, format)	\
2523*4882a593Smuzhiyun 	__opa_get_lid(lid, OPA_PORT_PACKET_FORMAT_##format)
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun /* Convert a lid to a specific lid space */
__opa_get_lid(u32 lid,u8 format)2526*4882a593Smuzhiyun static inline u32 __opa_get_lid(u32 lid, u8 format)
2527*4882a593Smuzhiyun {
2528*4882a593Smuzhiyun 	bool is_mcast = hfi1_check_mcast(lid);
2529*4882a593Smuzhiyun 
2530*4882a593Smuzhiyun 	switch (format) {
2531*4882a593Smuzhiyun 	case OPA_PORT_PACKET_FORMAT_8B:
2532*4882a593Smuzhiyun 	case OPA_PORT_PACKET_FORMAT_10B:
2533*4882a593Smuzhiyun 		if (is_mcast)
2534*4882a593Smuzhiyun 			return (lid - opa_get_mcast_base(OPA_MCAST_NR) +
2535*4882a593Smuzhiyun 				0xF0000);
2536*4882a593Smuzhiyun 		return lid & 0xFFFFF;
2537*4882a593Smuzhiyun 	case OPA_PORT_PACKET_FORMAT_16B:
2538*4882a593Smuzhiyun 		if (is_mcast)
2539*4882a593Smuzhiyun 			return (lid - opa_get_mcast_base(OPA_MCAST_NR) +
2540*4882a593Smuzhiyun 				0xF00000);
2541*4882a593Smuzhiyun 		return lid & 0xFFFFFF;
2542*4882a593Smuzhiyun 	case OPA_PORT_PACKET_FORMAT_9B:
2543*4882a593Smuzhiyun 		if (is_mcast)
2544*4882a593Smuzhiyun 			return (lid -
2545*4882a593Smuzhiyun 				opa_get_mcast_base(OPA_MCAST_NR) +
2546*4882a593Smuzhiyun 				be16_to_cpu(IB_MULTICAST_LID_BASE));
2547*4882a593Smuzhiyun 		else
2548*4882a593Smuzhiyun 			return lid & 0xFFFF;
2549*4882a593Smuzhiyun 	default:
2550*4882a593Smuzhiyun 		return lid;
2551*4882a593Smuzhiyun 	}
2552*4882a593Smuzhiyun }
2553*4882a593Smuzhiyun 
2554*4882a593Smuzhiyun /* Return true if the given lid is the OPA 16B multicast range */
hfi1_is_16B_mcast(u32 lid)2555*4882a593Smuzhiyun static inline bool hfi1_is_16B_mcast(u32 lid)
2556*4882a593Smuzhiyun {
2557*4882a593Smuzhiyun 	return ((lid >=
2558*4882a593Smuzhiyun 		opa_get_lid(opa_get_mcast_base(OPA_MCAST_NR), 16B)) &&
2559*4882a593Smuzhiyun 		(lid != opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B)));
2560*4882a593Smuzhiyun }
2561*4882a593Smuzhiyun 
hfi1_make_opa_lid(struct rdma_ah_attr * attr)2562*4882a593Smuzhiyun static inline void hfi1_make_opa_lid(struct rdma_ah_attr *attr)
2563*4882a593Smuzhiyun {
2564*4882a593Smuzhiyun 	const struct ib_global_route *grh = rdma_ah_read_grh(attr);
2565*4882a593Smuzhiyun 	u32 dlid = rdma_ah_get_dlid(attr);
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	/* Modify ah_attr.dlid to be in the 32 bit LID space.
2568*4882a593Smuzhiyun 	 * This is how the address will be laid out:
2569*4882a593Smuzhiyun 	 * Assuming MCAST_NR to be 4,
2570*4882a593Smuzhiyun 	 * 32 bit permissive LID = 0xFFFFFFFF
2571*4882a593Smuzhiyun 	 * Multicast LID range = 0xFFFFFFFE to 0xF0000000
2572*4882a593Smuzhiyun 	 * Unicast LID range = 0xEFFFFFFF to 1
2573*4882a593Smuzhiyun 	 * Invalid LID = 0
2574*4882a593Smuzhiyun 	 */
2575*4882a593Smuzhiyun 	if (ib_is_opa_gid(&grh->dgid))
2576*4882a593Smuzhiyun 		dlid = opa_get_lid_from_gid(&grh->dgid);
2577*4882a593Smuzhiyun 	else if ((dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
2578*4882a593Smuzhiyun 		 (dlid != be16_to_cpu(IB_LID_PERMISSIVE)) &&
2579*4882a593Smuzhiyun 		 (dlid != be32_to_cpu(OPA_LID_PERMISSIVE)))
2580*4882a593Smuzhiyun 		dlid = dlid - be16_to_cpu(IB_MULTICAST_LID_BASE) +
2581*4882a593Smuzhiyun 			opa_get_mcast_base(OPA_MCAST_NR);
2582*4882a593Smuzhiyun 	else if (dlid == be16_to_cpu(IB_LID_PERMISSIVE))
2583*4882a593Smuzhiyun 		dlid = be32_to_cpu(OPA_LID_PERMISSIVE);
2584*4882a593Smuzhiyun 
2585*4882a593Smuzhiyun 	rdma_ah_set_dlid(attr, dlid);
2586*4882a593Smuzhiyun }
2587*4882a593Smuzhiyun 
hfi1_get_packet_type(u32 lid)2588*4882a593Smuzhiyun static inline u8 hfi1_get_packet_type(u32 lid)
2589*4882a593Smuzhiyun {
2590*4882a593Smuzhiyun 	/* 9B if lid > 0xF0000000 */
2591*4882a593Smuzhiyun 	if (lid >= opa_get_mcast_base(OPA_MCAST_NR))
2592*4882a593Smuzhiyun 		return HFI1_PKT_TYPE_9B;
2593*4882a593Smuzhiyun 
2594*4882a593Smuzhiyun 	/* 16B if lid > 0xC000 */
2595*4882a593Smuzhiyun 	if (lid >= opa_get_lid(opa_get_mcast_base(OPA_MCAST_NR), 9B))
2596*4882a593Smuzhiyun 		return HFI1_PKT_TYPE_16B;
2597*4882a593Smuzhiyun 
2598*4882a593Smuzhiyun 	return HFI1_PKT_TYPE_9B;
2599*4882a593Smuzhiyun }
2600*4882a593Smuzhiyun 
hfi1_get_hdr_type(u32 lid,struct rdma_ah_attr * attr)2601*4882a593Smuzhiyun static inline bool hfi1_get_hdr_type(u32 lid, struct rdma_ah_attr *attr)
2602*4882a593Smuzhiyun {
2603*4882a593Smuzhiyun 	/*
2604*4882a593Smuzhiyun 	 * If there was an incoming 16B packet with permissive
2605*4882a593Smuzhiyun 	 * LIDs, OPA GIDs would have been programmed when those
2606*4882a593Smuzhiyun 	 * packets were received. A 16B packet will have to
2607*4882a593Smuzhiyun 	 * be sent in response to that packet. Return a 16B
2608*4882a593Smuzhiyun 	 * header type if that's the case.
2609*4882a593Smuzhiyun 	 */
2610*4882a593Smuzhiyun 	if (rdma_ah_get_dlid(attr) == be32_to_cpu(OPA_LID_PERMISSIVE))
2611*4882a593Smuzhiyun 		return (ib_is_opa_gid(&rdma_ah_read_grh(attr)->dgid)) ?
2612*4882a593Smuzhiyun 			HFI1_PKT_TYPE_16B : HFI1_PKT_TYPE_9B;
2613*4882a593Smuzhiyun 
2614*4882a593Smuzhiyun 	/*
2615*4882a593Smuzhiyun 	 * Return a 16B header type if either the the destination
2616*4882a593Smuzhiyun 	 * or source lid is extended.
2617*4882a593Smuzhiyun 	 */
2618*4882a593Smuzhiyun 	if (hfi1_get_packet_type(rdma_ah_get_dlid(attr)) == HFI1_PKT_TYPE_16B)
2619*4882a593Smuzhiyun 		return HFI1_PKT_TYPE_16B;
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun 	return hfi1_get_packet_type(lid);
2622*4882a593Smuzhiyun }
2623*4882a593Smuzhiyun 
hfi1_make_ext_grh(struct hfi1_packet * packet,struct ib_grh * grh,u32 slid,u32 dlid)2624*4882a593Smuzhiyun static inline void hfi1_make_ext_grh(struct hfi1_packet *packet,
2625*4882a593Smuzhiyun 				     struct ib_grh *grh, u32 slid,
2626*4882a593Smuzhiyun 				     u32 dlid)
2627*4882a593Smuzhiyun {
2628*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = &packet->rcd->ppd->ibport_data;
2629*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2630*4882a593Smuzhiyun 
2631*4882a593Smuzhiyun 	if (!ibp)
2632*4882a593Smuzhiyun 		return;
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun 	grh->hop_limit = 1;
2635*4882a593Smuzhiyun 	grh->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
2636*4882a593Smuzhiyun 	if (slid == opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B))
2637*4882a593Smuzhiyun 		grh->sgid.global.interface_id =
2638*4882a593Smuzhiyun 			OPA_MAKE_ID(be32_to_cpu(OPA_LID_PERMISSIVE));
2639*4882a593Smuzhiyun 	else
2640*4882a593Smuzhiyun 		grh->sgid.global.interface_id = OPA_MAKE_ID(slid);
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun 	/*
2643*4882a593Smuzhiyun 	 * Upper layers (like mad) may compare the dgid in the
2644*4882a593Smuzhiyun 	 * wc that is obtained here with the sgid_index in
2645*4882a593Smuzhiyun 	 * the wr. Since sgid_index in wr is always 0 for
2646*4882a593Smuzhiyun 	 * extended lids, set the dgid here to the default
2647*4882a593Smuzhiyun 	 * IB gid.
2648*4882a593Smuzhiyun 	 */
2649*4882a593Smuzhiyun 	grh->dgid.global.subnet_prefix = ibp->rvp.gid_prefix;
2650*4882a593Smuzhiyun 	grh->dgid.global.interface_id =
2651*4882a593Smuzhiyun 		cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
2652*4882a593Smuzhiyun }
2653*4882a593Smuzhiyun 
hfi1_get_16b_padding(u32 hdr_size,u32 payload)2654*4882a593Smuzhiyun static inline int hfi1_get_16b_padding(u32 hdr_size, u32 payload)
2655*4882a593Smuzhiyun {
2656*4882a593Smuzhiyun 	return -(hdr_size + payload + (SIZE_OF_CRC << 2) +
2657*4882a593Smuzhiyun 		     SIZE_OF_LT) & 0x7;
2658*4882a593Smuzhiyun }
2659*4882a593Smuzhiyun 
hfi1_make_ib_hdr(struct ib_header * hdr,u16 lrh0,u16 len,u16 dlid,u16 slid)2660*4882a593Smuzhiyun static inline void hfi1_make_ib_hdr(struct ib_header *hdr,
2661*4882a593Smuzhiyun 				    u16 lrh0, u16 len,
2662*4882a593Smuzhiyun 				    u16 dlid, u16 slid)
2663*4882a593Smuzhiyun {
2664*4882a593Smuzhiyun 	hdr->lrh[0] = cpu_to_be16(lrh0);
2665*4882a593Smuzhiyun 	hdr->lrh[1] = cpu_to_be16(dlid);
2666*4882a593Smuzhiyun 	hdr->lrh[2] = cpu_to_be16(len);
2667*4882a593Smuzhiyun 	hdr->lrh[3] = cpu_to_be16(slid);
2668*4882a593Smuzhiyun }
2669*4882a593Smuzhiyun 
hfi1_make_16b_hdr(struct hfi1_16b_header * hdr,u32 slid,u32 dlid,u16 len,u16 pkey,bool becn,bool fecn,u8 l4,u8 sc)2670*4882a593Smuzhiyun static inline void hfi1_make_16b_hdr(struct hfi1_16b_header *hdr,
2671*4882a593Smuzhiyun 				     u32 slid, u32 dlid,
2672*4882a593Smuzhiyun 				     u16 len, u16 pkey,
2673*4882a593Smuzhiyun 				     bool becn, bool fecn, u8 l4,
2674*4882a593Smuzhiyun 				     u8 sc)
2675*4882a593Smuzhiyun {
2676*4882a593Smuzhiyun 	u32 lrh0 = 0;
2677*4882a593Smuzhiyun 	u32 lrh1 = 0x40000000;
2678*4882a593Smuzhiyun 	u32 lrh2 = 0;
2679*4882a593Smuzhiyun 	u32 lrh3 = 0;
2680*4882a593Smuzhiyun 
2681*4882a593Smuzhiyun 	lrh0 = (lrh0 & ~OPA_16B_BECN_MASK) | (becn << OPA_16B_BECN_SHIFT);
2682*4882a593Smuzhiyun 	lrh0 = (lrh0 & ~OPA_16B_LEN_MASK) | (len << OPA_16B_LEN_SHIFT);
2683*4882a593Smuzhiyun 	lrh0 = (lrh0 & ~OPA_16B_LID_MASK)  | (slid & OPA_16B_LID_MASK);
2684*4882a593Smuzhiyun 	lrh1 = (lrh1 & ~OPA_16B_FECN_MASK) | (fecn << OPA_16B_FECN_SHIFT);
2685*4882a593Smuzhiyun 	lrh1 = (lrh1 & ~OPA_16B_SC_MASK) | (sc << OPA_16B_SC_SHIFT);
2686*4882a593Smuzhiyun 	lrh1 = (lrh1 & ~OPA_16B_LID_MASK) | (dlid & OPA_16B_LID_MASK);
2687*4882a593Smuzhiyun 	lrh2 = (lrh2 & ~OPA_16B_SLID_MASK) |
2688*4882a593Smuzhiyun 		((slid >> OPA_16B_SLID_SHIFT) << OPA_16B_SLID_HIGH_SHIFT);
2689*4882a593Smuzhiyun 	lrh2 = (lrh2 & ~OPA_16B_DLID_MASK) |
2690*4882a593Smuzhiyun 		((dlid >> OPA_16B_DLID_SHIFT) << OPA_16B_DLID_HIGH_SHIFT);
2691*4882a593Smuzhiyun 	lrh2 = (lrh2 & ~OPA_16B_PKEY_MASK) | ((u32)pkey << OPA_16B_PKEY_SHIFT);
2692*4882a593Smuzhiyun 	lrh2 = (lrh2 & ~OPA_16B_L4_MASK) | l4;
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun 	hdr->lrh[0] = lrh0;
2695*4882a593Smuzhiyun 	hdr->lrh[1] = lrh1;
2696*4882a593Smuzhiyun 	hdr->lrh[2] = lrh2;
2697*4882a593Smuzhiyun 	hdr->lrh[3] = lrh3;
2698*4882a593Smuzhiyun }
2699*4882a593Smuzhiyun #endif                          /* _HFI1_KERNEL_H */
2700