1/*
2 * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#ifndef MTHCA_PROVIDER_H
36#define MTHCA_PROVIDER_H
37
38#include <rdma/ib_verbs.h>
39#include <rdma/ib_pack.h>
40
41#define MTHCA_MPT_FLAG_ATOMIC        (1 << 14)
42#define MTHCA_MPT_FLAG_REMOTE_WRITE  (1 << 13)
43#define MTHCA_MPT_FLAG_REMOTE_READ   (1 << 12)
44#define MTHCA_MPT_FLAG_LOCAL_WRITE   (1 << 11)
45#define MTHCA_MPT_FLAG_LOCAL_READ    (1 << 10)
46
47struct mthca_buf_list {
48	void *buf;
49	DECLARE_PCI_UNMAP_ADDR(mapping)
50};
51
52union mthca_buf {
53	struct mthca_buf_list direct;
54	struct mthca_buf_list *page_list;
55};
56
57struct mthca_uar {
58	unsigned long pfn;
59	int           index;
60};
61
62struct mthca_user_db_table;
63
64struct mthca_ucontext {
65	struct ib_ucontext          ibucontext;
66	struct mthca_uar            uar;
67	struct mthca_user_db_table *db_tab;
68	int			    reg_mr_warned;
69};
70
71struct mthca_mtt;
72
73struct mthca_mr {
74	struct ib_mr      ibmr;
75	struct ib_umem   *umem;
76	struct mthca_mtt *mtt;
77};
78
79struct mthca_fmr {
80	struct ib_fmr      ibmr;
81	struct ib_fmr_attr attr;
82	struct mthca_mtt  *mtt;
83	int                maps;
84	union {
85		struct {
86			struct mthca_mpt_entry __iomem *mpt;
87			u64 __iomem *mtts;
88		} tavor;
89		struct {
90			struct mthca_mpt_entry *mpt;
91			__be64 *mtts;
92			dma_addr_t dma_handle;
93		} arbel;
94	} mem;
95};
96
97struct mthca_pd {
98	struct ib_pd    ibpd;
99	u32             pd_num;
100	atomic_t        sqp_count;
101	struct mthca_mr ntmr;
102	int             privileged;
103};
104
105struct mthca_eq {
106	struct mthca_dev      *dev;
107	int                    eqn;
108	u32                    eqn_mask;
109	u32                    cons_index;
110	u16                    msi_x_vector;
111	u16                    msi_x_entry;
112	int                    have_irq;
113	int                    nent;
114	struct mthca_buf_list *page_list;
115	struct mthca_mr        mr;
116};
117
118struct mthca_av;
119
120enum mthca_ah_type {
121	MTHCA_AH_ON_HCA,
122	MTHCA_AH_PCI_POOL,
123	MTHCA_AH_KMALLOC
124};
125
126struct mthca_ah {
127	struct ib_ah       ibah;
128	enum mthca_ah_type type;
129	u32                key;
130	struct mthca_av   *av;
131	dma_addr_t         avdma;
132};
133
134/*
135 * Quick description of our CQ/QP locking scheme:
136 *
137 * We have one global lock that protects dev->cq/qp_table.  Each
138 * struct mthca_cq/qp also has its own lock.  An individual qp lock
139 * may be taken inside of an individual cq lock.  Both cqs attached to
140 * a qp may be locked, with the cq with the lower cqn locked first.
141 * No other nesting should be done.
142 *
143 * Each struct mthca_cq/qp also has an ref count, protected by the
144 * corresponding table lock.  The pointer from the cq/qp_table to the
145 * struct counts as one reference.  This reference also is good for
146 * access through the consumer API, so modifying the CQ/QP etc doesn't
147 * need to take another reference.  Access to a QP because of a
148 * completion being polled does not need a reference either.
149 *
150 * Finally, each struct mthca_cq/qp has a wait_queue_head_t for the
151 * destroy function to sleep on.
152 *
153 * This means that access from the consumer API requires nothing but
154 * taking the struct's lock.
155 *
156 * Access because of a completion event should go as follows:
157 * - lock cq/qp_table and look up struct
158 * - increment ref count in struct
159 * - drop cq/qp_table lock
160 * - lock struct, do your thing, and unlock struct
161 * - decrement ref count; if zero, wake up waiters
162 *
163 * To destroy a CQ/QP, we can do the following:
164 * - lock cq/qp_table
165 * - remove pointer and decrement ref count
166 * - unlock cq/qp_table lock
167 * - wait_event until ref count is zero
168 *
169 * It is the consumer's responsibilty to make sure that no QP
170 * operations (WQE posting or state modification) are pending when a
171 * QP is destroyed.  Also, the consumer must make sure that calls to
172 * qp_modify are serialized.  Similarly, the consumer is responsible
173 * for ensuring that no CQ resize operations are pending when a CQ
174 * is destroyed.
175 *
176 * Possible optimizations (wait for profile data to see if/where we
177 * have locks bouncing between CPUs):
178 * - split cq/qp table lock into n separate (cache-aligned) locks,
179 *   indexed (say) by the page in the table
180 * - split QP struct lock into three (one for common info, one for the
181 *   send queue and one for the receive queue)
182 */
183
184struct mthca_cq_buf {
185	union mthca_buf		queue;
186	struct mthca_mr		mr;
187	int			is_direct;
188};
189
190struct mthca_cq_resize {
191	struct mthca_cq_buf	buf;
192	int			cqe;
193	enum {
194		CQ_RESIZE_ALLOC,
195		CQ_RESIZE_READY,
196		CQ_RESIZE_SWAPPED
197	}			state;
198};
199
200struct mthca_cq {
201	struct ib_cq		ibcq;
202	spinlock_t		lock;
203	int			refcount;
204	int			cqn;
205	u32			cons_index;
206	struct mthca_cq_buf	buf;
207	struct mthca_cq_resize *resize_buf;
208	int			is_kernel;
209
210	/* Next fields are Arbel only */
211	int			set_ci_db_index;
212	__be32		       *set_ci_db;
213	int			arm_db_index;
214	__be32		       *arm_db;
215	int			arm_sn;
216
217	wait_queue_head_t	wait;
218	struct mutex		mutex;
219};
220
221struct mthca_srq {
222	struct ib_srq		ibsrq;
223	spinlock_t		lock;
224	int			refcount;
225	int			srqn;
226	int			max;
227	int			max_gs;
228	int			wqe_shift;
229	int			first_free;
230	int			last_free;
231	u16			counter;  /* Arbel only */
232	int			db_index; /* Arbel only */
233	__be32		       *db;       /* Arbel only */
234	void		       *last;
235
236	int			is_direct;
237	u64		       *wrid;
238	union mthca_buf		queue;
239	struct mthca_mr		mr;
240
241	wait_queue_head_t	wait;
242	struct mutex		mutex;
243};
244
245struct mthca_wq {
246	spinlock_t lock;
247	int        max;
248	unsigned   next_ind;
249	unsigned   last_comp;
250	unsigned   head;
251	unsigned   tail;
252	void      *last;
253	int        max_gs;
254	int        wqe_shift;
255
256	int        db_index;	/* Arbel only */
257	__be32    *db;
258};
259
260struct mthca_qp {
261	struct ib_qp           ibqp;
262	int                    refcount;
263	u32                    qpn;
264	int                    is_direct;
265	u8                     port; /* for SQP and memfree use only */
266	u8                     alt_port; /* for memfree use only */
267	u8                     transport;
268	u8                     state;
269	u8                     atomic_rd_en;
270	u8                     resp_depth;
271
272	struct mthca_mr        mr;
273
274	struct mthca_wq        rq;
275	struct mthca_wq        sq;
276	enum ib_sig_type       sq_policy;
277	int                    send_wqe_offset;
278	int                    max_inline_data;
279
280	u64                   *wrid;
281	union mthca_buf	       queue;
282
283	wait_queue_head_t      wait;
284	struct mutex	       mutex;
285};
286
287struct mthca_sqp {
288	struct mthca_qp qp;
289	int             pkey_index;
290	u32             qkey;
291	u32             send_psn;
292	struct ib_ud_header ud_header;
293	int             header_buf_size;
294	void           *header_buf;
295	dma_addr_t      header_dma;
296};
297
298static inline struct mthca_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
299{
300	return container_of(ibucontext, struct mthca_ucontext, ibucontext);
301}
302
303static inline struct mthca_fmr *to_mfmr(struct ib_fmr *ibmr)
304{
305	return container_of(ibmr, struct mthca_fmr, ibmr);
306}
307
308static inline struct mthca_mr *to_mmr(struct ib_mr *ibmr)
309{
310	return container_of(ibmr, struct mthca_mr, ibmr);
311}
312
313static inline struct mthca_pd *to_mpd(struct ib_pd *ibpd)
314{
315	return container_of(ibpd, struct mthca_pd, ibpd);
316}
317
318static inline struct mthca_ah *to_mah(struct ib_ah *ibah)
319{
320	return container_of(ibah, struct mthca_ah, ibah);
321}
322
323static inline struct mthca_cq *to_mcq(struct ib_cq *ibcq)
324{
325	return container_of(ibcq, struct mthca_cq, ibcq);
326}
327
328static inline struct mthca_srq *to_msrq(struct ib_srq *ibsrq)
329{
330	return container_of(ibsrq, struct mthca_srq, ibsrq);
331}
332
333static inline struct mthca_qp *to_mqp(struct ib_qp *ibqp)
334{
335	return container_of(ibqp, struct mthca_qp, ibqp);
336}
337
338static inline struct mthca_sqp *to_msqp(struct mthca_qp *qp)
339{
340	return container_of(qp, struct mthca_sqp, qp);
341}
342
343#endif /* MTHCA_PROVIDER_H */
344