1/*
2 * Copyright(c) 2015 - 2018 Intel Corporation.
3 *
4 * This file is provided under a dual BSD/GPLv2 license.  When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 *  - Redistributions of source code must retain the above copyright
25 *    notice, this list of conditions and the following disclaimer.
26 *  - Redistributions in binary form must reproduce the above copyright
27 *    notice, this list of conditions and the following disclaimer in
28 *    the documentation and/or other materials provided with the
29 *    distribution.
30 *  - Neither the name of Intel Corporation nor the names of its
31 *    contributors may be used to endorse or promote products derived
32 *    from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#ifndef HFI1_VERBS_H
49#define HFI1_VERBS_H
50
51#include <linux/types.h>
52#include <linux/seqlock.h>
53#include <linux/kernel.h>
54#include <linux/interrupt.h>
55#include <linux/kref.h>
56#include <linux/workqueue.h>
57#include <linux/kthread.h>
58#include <linux/completion.h>
59#include <linux/slab.h>
60#include <rdma/ib_pack.h>
61#include <rdma/ib_user_verbs.h>
62#include <rdma/ib_mad.h>
63#include <rdma/ib_hdrs.h>
64#include <rdma/rdma_vt.h>
65#include <rdma/rdmavt_qp.h>
66#include <rdma/rdmavt_cq.h>
67
68struct hfi1_ctxtdata;
69struct hfi1_pportdata;
70struct hfi1_devdata;
71struct hfi1_packet;
72
73#include "iowait.h"
74#include "tid_rdma.h"
75#include "opfn.h"
76
77#define HFI1_MAX_RDMA_ATOMIC     16
78
79/*
80 * Increment this value if any changes that break userspace ABI
81 * compatibility are made.
82 */
83#define HFI1_UVERBS_ABI_VERSION       2
84
85/* IB Performance Manager status values */
86#define IB_PMA_SAMPLE_STATUS_DONE       0x00
87#define IB_PMA_SAMPLE_STATUS_STARTED    0x01
88#define IB_PMA_SAMPLE_STATUS_RUNNING    0x02
89
90/* Mandatory IB performance counter select values. */
91#define IB_PMA_PORT_XMIT_DATA   cpu_to_be16(0x0001)
92#define IB_PMA_PORT_RCV_DATA    cpu_to_be16(0x0002)
93#define IB_PMA_PORT_XMIT_PKTS   cpu_to_be16(0x0003)
94#define IB_PMA_PORT_RCV_PKTS    cpu_to_be16(0x0004)
95#define IB_PMA_PORT_XMIT_WAIT   cpu_to_be16(0x0005)
96
97#define HFI1_VENDOR_IPG		cpu_to_be16(0xFFA0)
98
99#define IB_DEFAULT_GID_PREFIX	cpu_to_be64(0xfe80000000000000ULL)
100#define OPA_BTH_MIG_REQ		BIT(31)
101
102#define RC_OP(x) IB_OPCODE_RC_##x
103#define UC_OP(x) IB_OPCODE_UC_##x
104
105/* flags passed by hfi1_ib_rcv() */
106enum {
107	HFI1_HAS_GRH = (1 << 0),
108};
109
110#define LRH_16B_BYTES (sizeof_field(struct hfi1_16b_header, lrh))
111#define LRH_16B_DWORDS (LRH_16B_BYTES / sizeof(u32))
112#define LRH_9B_BYTES (sizeof_field(struct ib_header, lrh))
113#define LRH_9B_DWORDS (LRH_9B_BYTES / sizeof(u32))
114
115/* 24Bits for qpn, upper 8Bits reserved */
116struct opa_16b_mgmt {
117	__be32 dest_qpn;
118	__be32 src_qpn;
119};
120
121struct hfi1_16b_header {
122	u32 lrh[4];
123	union {
124		struct {
125			struct ib_grh grh;
126			struct ib_other_headers oth;
127		} l;
128		struct ib_other_headers oth;
129		struct opa_16b_mgmt mgmt;
130	} u;
131} __packed;
132
133struct hfi1_opa_header {
134	union {
135		struct ib_header ibh; /* 9B header */
136		struct hfi1_16b_header opah; /* 16B header */
137	};
138	u8 hdr_type; /* 9B or 16B */
139} __packed;
140
141struct hfi1_ahg_info {
142	u32 ahgdesc[2];
143	u16 tx_flags;
144	u8 ahgcount;
145	u8 ahgidx;
146};
147
148struct hfi1_sdma_header {
149	__le64 pbc;
150	struct hfi1_opa_header hdr;
151} __packed;
152
153/*
154 * hfi1 specific data structures that will be hidden from rvt after the queue
155 * pair is made common
156 */
157struct hfi1_qp_priv {
158	struct hfi1_ahg_info *s_ahg;              /* ahg info for next header */
159	struct sdma_engine *s_sde;                /* current sde */
160	struct send_context *s_sendcontext;       /* current sendcontext */
161	struct hfi1_ctxtdata *rcd;                /* QP's receive context */
162	struct page **pages;                      /* for TID page scan */
163	u32 tid_enqueue;                          /* saved when tid waited */
164	u8 s_sc;		                  /* SC[0..4] for next packet */
165	struct iowait s_iowait;
166	struct timer_list s_tid_timer;            /* for timing tid wait */
167	struct timer_list s_tid_retry_timer;      /* for timing tid ack */
168	struct list_head tid_wait;                /* for queueing tid space */
169	struct hfi1_opfn_data opfn;
170	struct tid_flow_state flow_state;
171	struct tid_rdma_qp_params tid_rdma;
172	struct rvt_qp *owner;
173	u16 s_running_pkt_size;
174	u8 hdr_type; /* 9B or 16B */
175	struct rvt_sge_state tid_ss;       /* SGE state pointer for 2nd leg */
176	atomic_t n_requests;               /* # of TID RDMA requests in the */
177					   /* queue */
178	atomic_t n_tid_requests;            /* # of sent TID RDMA requests */
179	unsigned long tid_timer_timeout_jiffies;
180	unsigned long tid_retry_timeout_jiffies;
181
182	/* variables for the TID RDMA SE state machine */
183	u8 s_state;
184	u8 s_retry;
185	u8 rnr_nak_state;       /* RNR NAK state */
186	u8 s_nak_state;
187	u32 s_nak_psn;
188	u32 s_flags;
189	u32 s_tid_cur;
190	u32 s_tid_head;
191	u32 s_tid_tail;
192	u32 r_tid_head;     /* Most recently added TID RDMA request */
193	u32 r_tid_tail;     /* the last completed TID RDMA request */
194	u32 r_tid_ack;      /* the TID RDMA request to be ACK'ed */
195	u32 r_tid_alloc;    /* Request for which we are allocating resources */
196	u32 pending_tid_w_segs; /* Num of pending tid write segments */
197	u32 pending_tid_w_resp; /* Num of pending tid write responses */
198	u32 alloc_w_segs;       /* Number of segments for which write */
199			       /* resources have been allocated for this QP */
200
201	/* For TID RDMA READ */
202	u32 tid_r_reqs;         /* Num of tid reads requested */
203	u32 tid_r_comp;         /* Num of tid reads completed */
204	u32 pending_tid_r_segs; /* Num of pending tid read segments */
205	u16 pkts_ps;            /* packets per segment */
206	u8 timeout_shift;       /* account for number of packets per segment */
207
208	u32 r_next_psn_kdeth;
209	u32 r_next_psn_kdeth_save;
210	u32 s_resync_psn;
211	u8 sync_pt;           /* Set when QP reaches sync point */
212	u8 resync;
213};
214
215#define HFI1_QP_WQE_INVALID   ((u32)-1)
216
217struct hfi1_swqe_priv {
218	struct tid_rdma_request tid_req;
219	struct rvt_sge_state ss;  /* Used for TID RDMA READ Request */
220};
221
222struct hfi1_ack_priv {
223	struct rvt_sge_state ss;               /* used for TID WRITE RESP */
224	struct tid_rdma_request tid_req;
225};
226
227/*
228 * This structure is used to hold commonly lookedup and computed values during
229 * the send engine progress.
230 */
231struct iowait_work;
232struct hfi1_pkt_state {
233	struct hfi1_ibdev *dev;
234	struct hfi1_ibport *ibp;
235	struct hfi1_pportdata *ppd;
236	struct verbs_txreq *s_txreq;
237	struct iowait_work *wait;
238	unsigned long flags;
239	unsigned long timeout;
240	unsigned long timeout_int;
241	int cpu;
242	u8 opcode;
243	bool in_thread;
244	bool pkts_sent;
245};
246
247#define HFI1_PSN_CREDIT  16
248
249struct hfi1_opcode_stats {
250	u64 n_packets;          /* number of packets */
251	u64 n_bytes;            /* total number of bytes */
252};
253
254struct hfi1_opcode_stats_perctx {
255	struct hfi1_opcode_stats stats[256];
256};
257
258static inline void inc_opstats(
259	u32 tlen,
260	struct hfi1_opcode_stats *stats)
261{
262#ifdef CONFIG_DEBUG_FS
263	stats->n_bytes += tlen;
264	stats->n_packets++;
265#endif
266}
267
268struct hfi1_ibport {
269	struct rvt_qp __rcu *qp[2];
270	struct rvt_ibport rvp;
271
272	/* the first 16 entries are sl_to_vl for !OPA */
273	u8 sl_to_sc[32];
274	u8 sc_to_sl[32];
275};
276
277struct hfi1_ibdev {
278	struct rvt_dev_info rdi; /* Must be first */
279
280	/* QP numbers are shared by all IB ports */
281	/* protect txwait list */
282	seqlock_t txwait_lock ____cacheline_aligned_in_smp;
283	struct list_head txwait;        /* list for wait verbs_txreq */
284	struct list_head memwait;       /* list for wait kernel memory */
285	struct kmem_cache *verbs_txreq_cache;
286	u64 n_txwait;
287	u64 n_kmem_wait;
288	u64 n_tidwait;
289
290	/* protect iowait lists */
291	seqlock_t iowait_lock ____cacheline_aligned_in_smp;
292	u64 n_piowait;
293	u64 n_piodrain;
294	struct timer_list mem_timer;
295
296#ifdef CONFIG_DEBUG_FS
297	/* per HFI debugfs */
298	struct dentry *hfi1_ibdev_dbg;
299	/* per HFI symlinks to above */
300	struct dentry *hfi1_ibdev_link;
301#ifdef CONFIG_FAULT_INJECTION
302	struct fault *fault;
303#endif
304#endif
305};
306
307static inline struct hfi1_ibdev *to_idev(struct ib_device *ibdev)
308{
309	struct rvt_dev_info *rdi;
310
311	rdi = container_of(ibdev, struct rvt_dev_info, ibdev);
312	return container_of(rdi, struct hfi1_ibdev, rdi);
313}
314
315static inline struct rvt_qp *iowait_to_qp(struct iowait *s_iowait)
316{
317	struct hfi1_qp_priv *priv;
318
319	priv = container_of(s_iowait, struct hfi1_qp_priv, s_iowait);
320	return priv->owner;
321}
322
323/*
324 * This must be called with s_lock held.
325 */
326void hfi1_bad_pkey(struct hfi1_ibport *ibp, u32 key, u32 sl,
327		   u32 qp1, u32 qp2, u32 lid1, u32 lid2);
328void hfi1_cap_mask_chg(struct rvt_dev_info *rdi, u8 port_num);
329void hfi1_sys_guid_chg(struct hfi1_ibport *ibp);
330void hfi1_node_desc_chg(struct hfi1_ibport *ibp);
331int hfi1_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
332		     const struct ib_wc *in_wc, const struct ib_grh *in_grh,
333		     const struct ib_mad *in_mad, struct ib_mad *out_mad,
334		     size_t *out_mad_size, u16 *out_mad_pkey_index);
335
336/*
337 * The PSN_MASK and PSN_SHIFT allow for
338 * 1) comparing two PSNs
339 * 2) returning the PSN with any upper bits masked
340 * 3) returning the difference between to PSNs
341 *
342 * The number of significant bits in the PSN must
343 * necessarily be at least one bit less than
344 * the container holding the PSN.
345 */
346#define PSN_MASK 0x7FFFFFFF
347#define PSN_SHIFT 1
348#define PSN_MODIFY_MASK 0xFFFFFF
349
350/*
351 * Compare two PSNs
352 * Returns an integer <, ==, or > than zero.
353 */
354static inline int cmp_psn(u32 a, u32 b)
355{
356	return (((int)a) - ((int)b)) << PSN_SHIFT;
357}
358
359/*
360 * Return masked PSN
361 */
362static inline u32 mask_psn(u32 a)
363{
364	return a & PSN_MASK;
365}
366
367/*
368 * Return delta between two PSNs
369 */
370static inline u32 delta_psn(u32 a, u32 b)
371{
372	return (((int)a - (int)b) << PSN_SHIFT) >> PSN_SHIFT;
373}
374
375static inline struct tid_rdma_request *wqe_to_tid_req(struct rvt_swqe *wqe)
376{
377	return &((struct hfi1_swqe_priv *)wqe->priv)->tid_req;
378}
379
380static inline struct tid_rdma_request *ack_to_tid_req(struct rvt_ack_entry *e)
381{
382	return &((struct hfi1_ack_priv *)e->priv)->tid_req;
383}
384
385/*
386 * Look through all the active flows for a TID RDMA request and find
387 * the one (if it exists) that contains the specified PSN.
388 */
389static inline u32 __full_flow_psn(struct flow_state *state, u32 psn)
390{
391	return mask_psn((state->generation << HFI1_KDETH_BTH_SEQ_SHIFT) |
392			(psn & HFI1_KDETH_BTH_SEQ_MASK));
393}
394
395static inline u32 full_flow_psn(struct tid_rdma_flow *flow, u32 psn)
396{
397	return __full_flow_psn(&flow->flow_state, psn);
398}
399
400struct verbs_txreq;
401void hfi1_put_txreq(struct verbs_txreq *tx);
402
403int hfi1_verbs_send(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
404
405void hfi1_cnp_rcv(struct hfi1_packet *packet);
406
407void hfi1_uc_rcv(struct hfi1_packet *packet);
408
409void hfi1_rc_rcv(struct hfi1_packet *packet);
410
411void hfi1_rc_hdrerr(
412	struct hfi1_ctxtdata *rcd,
413	struct hfi1_packet *packet,
414	struct rvt_qp *qp);
415
416u8 ah_to_sc(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr);
417
418void hfi1_rc_verbs_aborted(struct rvt_qp *qp, struct hfi1_opa_header *opah);
419void hfi1_rc_send_complete(struct rvt_qp *qp, struct hfi1_opa_header *opah);
420
421void hfi1_ud_rcv(struct hfi1_packet *packet);
422
423int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey);
424
425void hfi1_migrate_qp(struct rvt_qp *qp);
426
427int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
428			 int attr_mask, struct ib_udata *udata);
429
430void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
431		    int attr_mask, struct ib_udata *udata);
432void hfi1_restart_rc(struct rvt_qp *qp, u32 psn, int wait);
433int hfi1_setup_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe,
434		   bool *call_send);
435
436extern const u32 rc_only_opcode;
437extern const u32 uc_only_opcode;
438
439int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_packet *packet);
440
441u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
442		  const struct ib_global_route *grh, u32 hwords, u32 nwords);
443
444void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
445			  u32 bth0, u32 bth1, u32 bth2, int middle,
446			  struct hfi1_pkt_state *ps);
447
448bool hfi1_schedule_send_yield(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
449			      bool tid);
450
451void _hfi1_do_send(struct work_struct *work);
452
453void hfi1_do_send_from_rvt(struct rvt_qp *qp);
454
455void hfi1_do_send(struct rvt_qp *qp, bool in_thread);
456
457void hfi1_send_rc_ack(struct hfi1_packet *packet, bool is_fecn);
458
459int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
460
461int hfi1_make_uc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
462
463int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
464
465int hfi1_register_ib_device(struct hfi1_devdata *);
466
467void hfi1_unregister_ib_device(struct hfi1_devdata *);
468
469void hfi1_kdeth_eager_rcv(struct hfi1_packet *packet);
470
471void hfi1_kdeth_expected_rcv(struct hfi1_packet *packet);
472
473void hfi1_ib_rcv(struct hfi1_packet *packet);
474
475void hfi1_16B_rcv(struct hfi1_packet *packet);
476
477unsigned hfi1_get_npkeys(struct hfi1_devdata *);
478
479int hfi1_verbs_send_dma(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
480			u64 pbc);
481
482int hfi1_verbs_send_pio(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
483			u64 pbc);
484
485static inline bool opa_bth_is_migration(struct ib_other_headers *ohdr)
486{
487	return ohdr->bth[1] & cpu_to_be32(OPA_BTH_MIG_REQ);
488}
489
490void hfi1_wait_kmem(struct rvt_qp *qp);
491
492static inline void hfi1_trdma_send_complete(struct rvt_qp *qp,
493					    struct rvt_swqe *wqe,
494					    enum ib_wc_status status)
495{
496	trdma_clean_swqe(qp, wqe);
497	rvt_send_complete(qp, wqe, status);
498}
499
500extern const enum ib_wc_opcode ib_hfi1_wc_opcode[];
501
502extern const u8 hdr_len_by_opcode[];
503
504extern const int ib_rvt_state_ops[];
505
506extern __be64 ib_hfi1_sys_image_guid;    /* in network order */
507
508extern unsigned int hfi1_max_cqes;
509
510extern unsigned int hfi1_max_cqs;
511
512extern unsigned int hfi1_max_qp_wrs;
513
514extern unsigned int hfi1_max_qps;
515
516extern unsigned int hfi1_max_sges;
517
518extern unsigned int hfi1_max_mcast_grps;
519
520extern unsigned int hfi1_max_mcast_qp_attached;
521
522extern unsigned int hfi1_max_srqs;
523
524extern unsigned int hfi1_max_srq_sges;
525
526extern unsigned int hfi1_max_srq_wrs;
527
528extern unsigned short piothreshold;
529
530extern const u32 ib_hfi1_rnr_table[];
531
532#endif                          /* HFI1_VERBS_H */
533