162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2004 Topspin Communications.  All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#ifndef MTHCA_PROVIDER_H
3662306a36Sopenharmony_ci#define MTHCA_PROVIDER_H
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include <rdma/ib_verbs.h>
3962306a36Sopenharmony_ci#include <rdma/ib_pack.h>
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define MTHCA_MPT_FLAG_ATOMIC        (1 << 14)
4262306a36Sopenharmony_ci#define MTHCA_MPT_FLAG_REMOTE_WRITE  (1 << 13)
4362306a36Sopenharmony_ci#define MTHCA_MPT_FLAG_REMOTE_READ   (1 << 12)
4462306a36Sopenharmony_ci#define MTHCA_MPT_FLAG_LOCAL_WRITE   (1 << 11)
4562306a36Sopenharmony_ci#define MTHCA_MPT_FLAG_LOCAL_READ    (1 << 10)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistruct mthca_buf_list {
4862306a36Sopenharmony_ci	void *buf;
4962306a36Sopenharmony_ci	DEFINE_DMA_UNMAP_ADDR(mapping);
5062306a36Sopenharmony_ci};
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciunion mthca_buf {
5362306a36Sopenharmony_ci	struct mthca_buf_list direct;
5462306a36Sopenharmony_ci	struct mthca_buf_list *page_list;
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistruct mthca_uar {
5862306a36Sopenharmony_ci	unsigned long pfn;
5962306a36Sopenharmony_ci	int           index;
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistruct mthca_user_db_table;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistruct mthca_ucontext {
6562306a36Sopenharmony_ci	struct ib_ucontext          ibucontext;
6662306a36Sopenharmony_ci	struct mthca_uar            uar;
6762306a36Sopenharmony_ci	struct mthca_user_db_table *db_tab;
6862306a36Sopenharmony_ci	int			    reg_mr_warned;
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistruct mthca_mtt;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct mthca_mr {
7462306a36Sopenharmony_ci	struct ib_mr      ibmr;
7562306a36Sopenharmony_ci	struct ib_umem   *umem;
7662306a36Sopenharmony_ci	struct mthca_mtt *mtt;
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistruct mthca_pd {
8062306a36Sopenharmony_ci	struct ib_pd    ibpd;
8162306a36Sopenharmony_ci	u32             pd_num;
8262306a36Sopenharmony_ci	atomic_t        sqp_count;
8362306a36Sopenharmony_ci	struct mthca_mr ntmr;
8462306a36Sopenharmony_ci	int             privileged;
8562306a36Sopenharmony_ci};
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_cistruct mthca_eq {
8862306a36Sopenharmony_ci	struct mthca_dev      *dev;
8962306a36Sopenharmony_ci	int                    eqn;
9062306a36Sopenharmony_ci	u32                    eqn_mask;
9162306a36Sopenharmony_ci	u32                    cons_index;
9262306a36Sopenharmony_ci	u16                    msi_x_vector;
9362306a36Sopenharmony_ci	u16                    msi_x_entry;
9462306a36Sopenharmony_ci	int                    have_irq;
9562306a36Sopenharmony_ci	int                    nent;
9662306a36Sopenharmony_ci	struct mthca_buf_list *page_list;
9762306a36Sopenharmony_ci	struct mthca_mr        mr;
9862306a36Sopenharmony_ci	char		       irq_name[IB_DEVICE_NAME_MAX];
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistruct mthca_av;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cienum mthca_ah_type {
10462306a36Sopenharmony_ci	MTHCA_AH_ON_HCA,
10562306a36Sopenharmony_ci	MTHCA_AH_PCI_POOL,
10662306a36Sopenharmony_ci	MTHCA_AH_KMALLOC
10762306a36Sopenharmony_ci};
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_cistruct mthca_ah {
11062306a36Sopenharmony_ci	struct ib_ah       ibah;
11162306a36Sopenharmony_ci	enum mthca_ah_type type;
11262306a36Sopenharmony_ci	u32                key;
11362306a36Sopenharmony_ci	struct mthca_av   *av;
11462306a36Sopenharmony_ci	dma_addr_t         avdma;
11562306a36Sopenharmony_ci};
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/*
11862306a36Sopenharmony_ci * Quick description of our CQ/QP locking scheme:
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci * We have one global lock that protects dev->cq/qp_table.  Each
12162306a36Sopenharmony_ci * struct mthca_cq/qp also has its own lock.  An individual qp lock
12262306a36Sopenharmony_ci * may be taken inside of an individual cq lock.  Both cqs attached to
12362306a36Sopenharmony_ci * a qp may be locked, with the cq with the lower cqn locked first.
12462306a36Sopenharmony_ci * No other nesting should be done.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * Each struct mthca_cq/qp also has an ref count, protected by the
12762306a36Sopenharmony_ci * corresponding table lock.  The pointer from the cq/qp_table to the
12862306a36Sopenharmony_ci * struct counts as one reference.  This reference also is good for
12962306a36Sopenharmony_ci * access through the consumer API, so modifying the CQ/QP etc doesn't
13062306a36Sopenharmony_ci * need to take another reference.  Access to a QP because of a
13162306a36Sopenharmony_ci * completion being polled does not need a reference either.
13262306a36Sopenharmony_ci *
13362306a36Sopenharmony_ci * Finally, each struct mthca_cq/qp has a wait_queue_head_t for the
13462306a36Sopenharmony_ci * destroy function to sleep on.
13562306a36Sopenharmony_ci *
13662306a36Sopenharmony_ci * This means that access from the consumer API requires nothing but
13762306a36Sopenharmony_ci * taking the struct's lock.
13862306a36Sopenharmony_ci *
13962306a36Sopenharmony_ci * Access because of a completion event should go as follows:
14062306a36Sopenharmony_ci * - lock cq/qp_table and look up struct
14162306a36Sopenharmony_ci * - increment ref count in struct
14262306a36Sopenharmony_ci * - drop cq/qp_table lock
14362306a36Sopenharmony_ci * - lock struct, do your thing, and unlock struct
14462306a36Sopenharmony_ci * - decrement ref count; if zero, wake up waiters
14562306a36Sopenharmony_ci *
14662306a36Sopenharmony_ci * To destroy a CQ/QP, we can do the following:
14762306a36Sopenharmony_ci * - lock cq/qp_table
14862306a36Sopenharmony_ci * - remove pointer and decrement ref count
14962306a36Sopenharmony_ci * - unlock cq/qp_table lock
15062306a36Sopenharmony_ci * - wait_event until ref count is zero
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci * It is the consumer's responsibilty to make sure that no QP
15362306a36Sopenharmony_ci * operations (WQE posting or state modification) are pending when a
15462306a36Sopenharmony_ci * QP is destroyed.  Also, the consumer must make sure that calls to
15562306a36Sopenharmony_ci * qp_modify are serialized.  Similarly, the consumer is responsible
15662306a36Sopenharmony_ci * for ensuring that no CQ resize operations are pending when a CQ
15762306a36Sopenharmony_ci * is destroyed.
15862306a36Sopenharmony_ci *
15962306a36Sopenharmony_ci * Possible optimizations (wait for profile data to see if/where we
16062306a36Sopenharmony_ci * have locks bouncing between CPUs):
16162306a36Sopenharmony_ci * - split cq/qp table lock into n separate (cache-aligned) locks,
16262306a36Sopenharmony_ci *   indexed (say) by the page in the table
16362306a36Sopenharmony_ci * - split QP struct lock into three (one for common info, one for the
16462306a36Sopenharmony_ci *   send queue and one for the receive queue)
16562306a36Sopenharmony_ci */
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistruct mthca_cq_buf {
16862306a36Sopenharmony_ci	union mthca_buf		queue;
16962306a36Sopenharmony_ci	struct mthca_mr		mr;
17062306a36Sopenharmony_ci	int			is_direct;
17162306a36Sopenharmony_ci};
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistruct mthca_cq_resize {
17462306a36Sopenharmony_ci	struct mthca_cq_buf	buf;
17562306a36Sopenharmony_ci	int			cqe;
17662306a36Sopenharmony_ci	enum {
17762306a36Sopenharmony_ci		CQ_RESIZE_ALLOC,
17862306a36Sopenharmony_ci		CQ_RESIZE_READY,
17962306a36Sopenharmony_ci		CQ_RESIZE_SWAPPED
18062306a36Sopenharmony_ci	}			state;
18162306a36Sopenharmony_ci};
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cistruct mthca_cq {
18462306a36Sopenharmony_ci	struct ib_cq		ibcq;
18562306a36Sopenharmony_ci	spinlock_t		lock;
18662306a36Sopenharmony_ci	int			refcount;
18762306a36Sopenharmony_ci	int			cqn;
18862306a36Sopenharmony_ci	u32			cons_index;
18962306a36Sopenharmony_ci	struct mthca_cq_buf	buf;
19062306a36Sopenharmony_ci	struct mthca_cq_resize *resize_buf;
19162306a36Sopenharmony_ci	int			is_kernel;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	/* Next fields are Arbel only */
19462306a36Sopenharmony_ci	int			set_ci_db_index;
19562306a36Sopenharmony_ci	__be32		       *set_ci_db;
19662306a36Sopenharmony_ci	int			arm_db_index;
19762306a36Sopenharmony_ci	__be32		       *arm_db;
19862306a36Sopenharmony_ci	int			arm_sn;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	wait_queue_head_t	wait;
20162306a36Sopenharmony_ci	struct mutex		mutex;
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistruct mthca_srq {
20562306a36Sopenharmony_ci	struct ib_srq		ibsrq;
20662306a36Sopenharmony_ci	spinlock_t		lock;
20762306a36Sopenharmony_ci	int			refcount;
20862306a36Sopenharmony_ci	int			srqn;
20962306a36Sopenharmony_ci	int			max;
21062306a36Sopenharmony_ci	int			max_gs;
21162306a36Sopenharmony_ci	int			wqe_shift;
21262306a36Sopenharmony_ci	int			first_free;
21362306a36Sopenharmony_ci	int			last_free;
21462306a36Sopenharmony_ci	u16			counter;  /* Arbel only */
21562306a36Sopenharmony_ci	int			db_index; /* Arbel only */
21662306a36Sopenharmony_ci	__be32		       *db;       /* Arbel only */
21762306a36Sopenharmony_ci	void		       *last;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	int			is_direct;
22062306a36Sopenharmony_ci	u64		       *wrid;
22162306a36Sopenharmony_ci	union mthca_buf		queue;
22262306a36Sopenharmony_ci	struct mthca_mr		mr;
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	wait_queue_head_t	wait;
22562306a36Sopenharmony_ci	struct mutex		mutex;
22662306a36Sopenharmony_ci};
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_cistruct mthca_wq {
22962306a36Sopenharmony_ci	spinlock_t lock;
23062306a36Sopenharmony_ci	int        max;
23162306a36Sopenharmony_ci	unsigned   next_ind;
23262306a36Sopenharmony_ci	unsigned   last_comp;
23362306a36Sopenharmony_ci	unsigned   head;
23462306a36Sopenharmony_ci	unsigned   tail;
23562306a36Sopenharmony_ci	void      *last;
23662306a36Sopenharmony_ci	int        max_gs;
23762306a36Sopenharmony_ci	int        wqe_shift;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	int        db_index;	/* Arbel only */
24062306a36Sopenharmony_ci	__be32    *db;
24162306a36Sopenharmony_ci};
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cistruct mthca_sqp {
24462306a36Sopenharmony_ci	int             pkey_index;
24562306a36Sopenharmony_ci	u32             qkey;
24662306a36Sopenharmony_ci	u32             send_psn;
24762306a36Sopenharmony_ci	struct ib_ud_header ud_header;
24862306a36Sopenharmony_ci	int             header_buf_size;
24962306a36Sopenharmony_ci	void           *header_buf;
25062306a36Sopenharmony_ci	dma_addr_t      header_dma;
25162306a36Sopenharmony_ci};
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_cistruct mthca_qp {
25462306a36Sopenharmony_ci	struct ib_qp           ibqp;
25562306a36Sopenharmony_ci	int                    refcount;
25662306a36Sopenharmony_ci	u32                    qpn;
25762306a36Sopenharmony_ci	int                    is_direct;
25862306a36Sopenharmony_ci	u8                     port; /* for SQP and memfree use only */
25962306a36Sopenharmony_ci	u8                     alt_port; /* for memfree use only */
26062306a36Sopenharmony_ci	u8                     transport;
26162306a36Sopenharmony_ci	u8                     state;
26262306a36Sopenharmony_ci	u8                     atomic_rd_en;
26362306a36Sopenharmony_ci	u8                     resp_depth;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	struct mthca_mr        mr;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	struct mthca_wq        rq;
26862306a36Sopenharmony_ci	struct mthca_wq        sq;
26962306a36Sopenharmony_ci	enum ib_sig_type       sq_policy;
27062306a36Sopenharmony_ci	int                    send_wqe_offset;
27162306a36Sopenharmony_ci	int                    max_inline_data;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	u64                   *wrid;
27462306a36Sopenharmony_ci	union mthca_buf	       queue;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	wait_queue_head_t      wait;
27762306a36Sopenharmony_ci	struct mutex	       mutex;
27862306a36Sopenharmony_ci	struct mthca_sqp *sqp;
27962306a36Sopenharmony_ci};
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_cistatic inline struct mthca_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
28262306a36Sopenharmony_ci{
28362306a36Sopenharmony_ci	return container_of(ibucontext, struct mthca_ucontext, ibucontext);
28462306a36Sopenharmony_ci}
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_cistatic inline struct mthca_mr *to_mmr(struct ib_mr *ibmr)
28762306a36Sopenharmony_ci{
28862306a36Sopenharmony_ci	return container_of(ibmr, struct mthca_mr, ibmr);
28962306a36Sopenharmony_ci}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistatic inline struct mthca_pd *to_mpd(struct ib_pd *ibpd)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	return container_of(ibpd, struct mthca_pd, ibpd);
29462306a36Sopenharmony_ci}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistatic inline struct mthca_ah *to_mah(struct ib_ah *ibah)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	return container_of(ibah, struct mthca_ah, ibah);
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic inline struct mthca_cq *to_mcq(struct ib_cq *ibcq)
30262306a36Sopenharmony_ci{
30362306a36Sopenharmony_ci	return container_of(ibcq, struct mthca_cq, ibcq);
30462306a36Sopenharmony_ci}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic inline struct mthca_srq *to_msrq(struct ib_srq *ibsrq)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	return container_of(ibsrq, struct mthca_srq, ibsrq);
30962306a36Sopenharmony_ci}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic inline struct mthca_qp *to_mqp(struct ib_qp *ibqp)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	return container_of(ibqp, struct mthca_qp, ibqp);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci#endif /* MTHCA_PROVIDER_H */
317