18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2004 Topspin Communications.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
78c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
138c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
148c2ecf20Sopenharmony_ci *     conditions are met:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
178c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
188c2ecf20Sopenharmony_ci *        disclaimer.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
218c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
228c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
238c2ecf20Sopenharmony_ci *        provided with the distribution.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328c2ecf20Sopenharmony_ci * SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#ifndef MTHCA_PROVIDER_H
368c2ecf20Sopenharmony_ci#define MTHCA_PROVIDER_H
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <rdma/ib_verbs.h>
398c2ecf20Sopenharmony_ci#include <rdma/ib_pack.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define MTHCA_MPT_FLAG_ATOMIC        (1 << 14)
428c2ecf20Sopenharmony_ci#define MTHCA_MPT_FLAG_REMOTE_WRITE  (1 << 13)
438c2ecf20Sopenharmony_ci#define MTHCA_MPT_FLAG_REMOTE_READ   (1 << 12)
448c2ecf20Sopenharmony_ci#define MTHCA_MPT_FLAG_LOCAL_WRITE   (1 << 11)
458c2ecf20Sopenharmony_ci#define MTHCA_MPT_FLAG_LOCAL_READ    (1 << 10)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistruct mthca_buf_list {
488c2ecf20Sopenharmony_ci	void *buf;
498c2ecf20Sopenharmony_ci	DEFINE_DMA_UNMAP_ADDR(mapping);
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciunion mthca_buf {
538c2ecf20Sopenharmony_ci	struct mthca_buf_list direct;
548c2ecf20Sopenharmony_ci	struct mthca_buf_list *page_list;
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct mthca_uar {
588c2ecf20Sopenharmony_ci	unsigned long pfn;
598c2ecf20Sopenharmony_ci	int           index;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct mthca_user_db_table;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct mthca_ucontext {
658c2ecf20Sopenharmony_ci	struct ib_ucontext          ibucontext;
668c2ecf20Sopenharmony_ci	struct mthca_uar            uar;
678c2ecf20Sopenharmony_ci	struct mthca_user_db_table *db_tab;
688c2ecf20Sopenharmony_ci	int			    reg_mr_warned;
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistruct mthca_mtt;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct mthca_mr {
748c2ecf20Sopenharmony_ci	struct ib_mr      ibmr;
758c2ecf20Sopenharmony_ci	struct ib_umem   *umem;
768c2ecf20Sopenharmony_ci	struct mthca_mtt *mtt;
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistruct mthca_pd {
808c2ecf20Sopenharmony_ci	struct ib_pd    ibpd;
818c2ecf20Sopenharmony_ci	u32             pd_num;
828c2ecf20Sopenharmony_ci	atomic_t        sqp_count;
838c2ecf20Sopenharmony_ci	struct mthca_mr ntmr;
848c2ecf20Sopenharmony_ci	int             privileged;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct mthca_eq {
888c2ecf20Sopenharmony_ci	struct mthca_dev      *dev;
898c2ecf20Sopenharmony_ci	int                    eqn;
908c2ecf20Sopenharmony_ci	u32                    eqn_mask;
918c2ecf20Sopenharmony_ci	u32                    cons_index;
928c2ecf20Sopenharmony_ci	u16                    msi_x_vector;
938c2ecf20Sopenharmony_ci	u16                    msi_x_entry;
948c2ecf20Sopenharmony_ci	int                    have_irq;
958c2ecf20Sopenharmony_ci	int                    nent;
968c2ecf20Sopenharmony_ci	struct mthca_buf_list *page_list;
978c2ecf20Sopenharmony_ci	struct mthca_mr        mr;
988c2ecf20Sopenharmony_ci	char		       irq_name[IB_DEVICE_NAME_MAX];
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistruct mthca_av;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cienum mthca_ah_type {
1048c2ecf20Sopenharmony_ci	MTHCA_AH_ON_HCA,
1058c2ecf20Sopenharmony_ci	MTHCA_AH_PCI_POOL,
1068c2ecf20Sopenharmony_ci	MTHCA_AH_KMALLOC
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistruct mthca_ah {
1108c2ecf20Sopenharmony_ci	struct ib_ah       ibah;
1118c2ecf20Sopenharmony_ci	enum mthca_ah_type type;
1128c2ecf20Sopenharmony_ci	u32                key;
1138c2ecf20Sopenharmony_ci	struct mthca_av   *av;
1148c2ecf20Sopenharmony_ci	dma_addr_t         avdma;
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/*
1188c2ecf20Sopenharmony_ci * Quick description of our CQ/QP locking scheme:
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * We have one global lock that protects dev->cq/qp_table.  Each
1218c2ecf20Sopenharmony_ci * struct mthca_cq/qp also has its own lock.  An individual qp lock
1228c2ecf20Sopenharmony_ci * may be taken inside of an individual cq lock.  Both cqs attached to
1238c2ecf20Sopenharmony_ci * a qp may be locked, with the cq with the lower cqn locked first.
1248c2ecf20Sopenharmony_ci * No other nesting should be done.
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * Each struct mthca_cq/qp also has an ref count, protected by the
1278c2ecf20Sopenharmony_ci * corresponding table lock.  The pointer from the cq/qp_table to the
1288c2ecf20Sopenharmony_ci * struct counts as one reference.  This reference also is good for
1298c2ecf20Sopenharmony_ci * access through the consumer API, so modifying the CQ/QP etc doesn't
1308c2ecf20Sopenharmony_ci * need to take another reference.  Access to a QP because of a
1318c2ecf20Sopenharmony_ci * completion being polled does not need a reference either.
1328c2ecf20Sopenharmony_ci *
1338c2ecf20Sopenharmony_ci * Finally, each struct mthca_cq/qp has a wait_queue_head_t for the
1348c2ecf20Sopenharmony_ci * destroy function to sleep on.
1358c2ecf20Sopenharmony_ci *
1368c2ecf20Sopenharmony_ci * This means that access from the consumer API requires nothing but
1378c2ecf20Sopenharmony_ci * taking the struct's lock.
1388c2ecf20Sopenharmony_ci *
1398c2ecf20Sopenharmony_ci * Access because of a completion event should go as follows:
1408c2ecf20Sopenharmony_ci * - lock cq/qp_table and look up struct
1418c2ecf20Sopenharmony_ci * - increment ref count in struct
1428c2ecf20Sopenharmony_ci * - drop cq/qp_table lock
1438c2ecf20Sopenharmony_ci * - lock struct, do your thing, and unlock struct
1448c2ecf20Sopenharmony_ci * - decrement ref count; if zero, wake up waiters
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * To destroy a CQ/QP, we can do the following:
1478c2ecf20Sopenharmony_ci * - lock cq/qp_table
1488c2ecf20Sopenharmony_ci * - remove pointer and decrement ref count
1498c2ecf20Sopenharmony_ci * - unlock cq/qp_table lock
1508c2ecf20Sopenharmony_ci * - wait_event until ref count is zero
1518c2ecf20Sopenharmony_ci *
1528c2ecf20Sopenharmony_ci * It is the consumer's responsibilty to make sure that no QP
1538c2ecf20Sopenharmony_ci * operations (WQE posting or state modification) are pending when a
1548c2ecf20Sopenharmony_ci * QP is destroyed.  Also, the consumer must make sure that calls to
1558c2ecf20Sopenharmony_ci * qp_modify are serialized.  Similarly, the consumer is responsible
1568c2ecf20Sopenharmony_ci * for ensuring that no CQ resize operations are pending when a CQ
1578c2ecf20Sopenharmony_ci * is destroyed.
1588c2ecf20Sopenharmony_ci *
1598c2ecf20Sopenharmony_ci * Possible optimizations (wait for profile data to see if/where we
1608c2ecf20Sopenharmony_ci * have locks bouncing between CPUs):
1618c2ecf20Sopenharmony_ci * - split cq/qp table lock into n separate (cache-aligned) locks,
1628c2ecf20Sopenharmony_ci *   indexed (say) by the page in the table
1638c2ecf20Sopenharmony_ci * - split QP struct lock into three (one for common info, one for the
1648c2ecf20Sopenharmony_ci *   send queue and one for the receive queue)
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistruct mthca_cq_buf {
1688c2ecf20Sopenharmony_ci	union mthca_buf		queue;
1698c2ecf20Sopenharmony_ci	struct mthca_mr		mr;
1708c2ecf20Sopenharmony_ci	int			is_direct;
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistruct mthca_cq_resize {
1748c2ecf20Sopenharmony_ci	struct mthca_cq_buf	buf;
1758c2ecf20Sopenharmony_ci	int			cqe;
1768c2ecf20Sopenharmony_ci	enum {
1778c2ecf20Sopenharmony_ci		CQ_RESIZE_ALLOC,
1788c2ecf20Sopenharmony_ci		CQ_RESIZE_READY,
1798c2ecf20Sopenharmony_ci		CQ_RESIZE_SWAPPED
1808c2ecf20Sopenharmony_ci	}			state;
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistruct mthca_cq {
1848c2ecf20Sopenharmony_ci	struct ib_cq		ibcq;
1858c2ecf20Sopenharmony_ci	spinlock_t		lock;
1868c2ecf20Sopenharmony_ci	int			refcount;
1878c2ecf20Sopenharmony_ci	int			cqn;
1888c2ecf20Sopenharmony_ci	u32			cons_index;
1898c2ecf20Sopenharmony_ci	struct mthca_cq_buf	buf;
1908c2ecf20Sopenharmony_ci	struct mthca_cq_resize *resize_buf;
1918c2ecf20Sopenharmony_ci	int			is_kernel;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Next fields are Arbel only */
1948c2ecf20Sopenharmony_ci	int			set_ci_db_index;
1958c2ecf20Sopenharmony_ci	__be32		       *set_ci_db;
1968c2ecf20Sopenharmony_ci	int			arm_db_index;
1978c2ecf20Sopenharmony_ci	__be32		       *arm_db;
1988c2ecf20Sopenharmony_ci	int			arm_sn;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	wait_queue_head_t	wait;
2018c2ecf20Sopenharmony_ci	struct mutex		mutex;
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistruct mthca_srq {
2058c2ecf20Sopenharmony_ci	struct ib_srq		ibsrq;
2068c2ecf20Sopenharmony_ci	spinlock_t		lock;
2078c2ecf20Sopenharmony_ci	int			refcount;
2088c2ecf20Sopenharmony_ci	int			srqn;
2098c2ecf20Sopenharmony_ci	int			max;
2108c2ecf20Sopenharmony_ci	int			max_gs;
2118c2ecf20Sopenharmony_ci	int			wqe_shift;
2128c2ecf20Sopenharmony_ci	int			first_free;
2138c2ecf20Sopenharmony_ci	int			last_free;
2148c2ecf20Sopenharmony_ci	u16			counter;  /* Arbel only */
2158c2ecf20Sopenharmony_ci	int			db_index; /* Arbel only */
2168c2ecf20Sopenharmony_ci	__be32		       *db;       /* Arbel only */
2178c2ecf20Sopenharmony_ci	void		       *last;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	int			is_direct;
2208c2ecf20Sopenharmony_ci	u64		       *wrid;
2218c2ecf20Sopenharmony_ci	union mthca_buf		queue;
2228c2ecf20Sopenharmony_ci	struct mthca_mr		mr;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	wait_queue_head_t	wait;
2258c2ecf20Sopenharmony_ci	struct mutex		mutex;
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistruct mthca_wq {
2298c2ecf20Sopenharmony_ci	spinlock_t lock;
2308c2ecf20Sopenharmony_ci	int        max;
2318c2ecf20Sopenharmony_ci	unsigned   next_ind;
2328c2ecf20Sopenharmony_ci	unsigned   last_comp;
2338c2ecf20Sopenharmony_ci	unsigned   head;
2348c2ecf20Sopenharmony_ci	unsigned   tail;
2358c2ecf20Sopenharmony_ci	void      *last;
2368c2ecf20Sopenharmony_ci	int        max_gs;
2378c2ecf20Sopenharmony_ci	int        wqe_shift;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	int        db_index;	/* Arbel only */
2408c2ecf20Sopenharmony_ci	__be32    *db;
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistruct mthca_sqp {
2448c2ecf20Sopenharmony_ci	int             pkey_index;
2458c2ecf20Sopenharmony_ci	u32             qkey;
2468c2ecf20Sopenharmony_ci	u32             send_psn;
2478c2ecf20Sopenharmony_ci	struct ib_ud_header ud_header;
2488c2ecf20Sopenharmony_ci	int             header_buf_size;
2498c2ecf20Sopenharmony_ci	void           *header_buf;
2508c2ecf20Sopenharmony_ci	dma_addr_t      header_dma;
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistruct mthca_qp {
2548c2ecf20Sopenharmony_ci	struct ib_qp           ibqp;
2558c2ecf20Sopenharmony_ci	int                    refcount;
2568c2ecf20Sopenharmony_ci	u32                    qpn;
2578c2ecf20Sopenharmony_ci	int                    is_direct;
2588c2ecf20Sopenharmony_ci	u8                     port; /* for SQP and memfree use only */
2598c2ecf20Sopenharmony_ci	u8                     alt_port; /* for memfree use only */
2608c2ecf20Sopenharmony_ci	u8                     transport;
2618c2ecf20Sopenharmony_ci	u8                     state;
2628c2ecf20Sopenharmony_ci	u8                     atomic_rd_en;
2638c2ecf20Sopenharmony_ci	u8                     resp_depth;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	struct mthca_mr        mr;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	struct mthca_wq        rq;
2688c2ecf20Sopenharmony_ci	struct mthca_wq        sq;
2698c2ecf20Sopenharmony_ci	enum ib_sig_type       sq_policy;
2708c2ecf20Sopenharmony_ci	int                    send_wqe_offset;
2718c2ecf20Sopenharmony_ci	int                    max_inline_data;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	u64                   *wrid;
2748c2ecf20Sopenharmony_ci	union mthca_buf	       queue;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	wait_queue_head_t      wait;
2778c2ecf20Sopenharmony_ci	struct mutex	       mutex;
2788c2ecf20Sopenharmony_ci	struct mthca_sqp *sqp;
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic inline struct mthca_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	return container_of(ibucontext, struct mthca_ucontext, ibucontext);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic inline struct mthca_mr *to_mmr(struct ib_mr *ibmr)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	return container_of(ibmr, struct mthca_mr, ibmr);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic inline struct mthca_pd *to_mpd(struct ib_pd *ibpd)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	return container_of(ibpd, struct mthca_pd, ibpd);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic inline struct mthca_ah *to_mah(struct ib_ah *ibah)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	return container_of(ibah, struct mthca_ah, ibah);
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic inline struct mthca_cq *to_mcq(struct ib_cq *ibcq)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	return container_of(ibcq, struct mthca_cq, ibcq);
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic inline struct mthca_srq *to_msrq(struct ib_srq *ibsrq)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	return container_of(ibsrq, struct mthca_srq, ibsrq);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic inline struct mthca_qp *to_mqp(struct ib_qp *ibqp)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	return container_of(ibqp, struct mthca_qp, ibqp);
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci#endif /* MTHCA_PROVIDER_H */
317