1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3219820Sjeff * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4219820Sjeff * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff */
34219820Sjeff
35219820Sjeff#ifndef MTHCA_PROVIDER_H
36219820Sjeff#define MTHCA_PROVIDER_H
37219820Sjeff
38219820Sjeff#include <rdma/ib_verbs.h>
39219820Sjeff#include <rdma/ib_pack.h>
40219820Sjeff
41219820Sjeff#define MTHCA_MPT_FLAG_ATOMIC        (1 << 14)
42219820Sjeff#define MTHCA_MPT_FLAG_REMOTE_WRITE  (1 << 13)
43219820Sjeff#define MTHCA_MPT_FLAG_REMOTE_READ   (1 << 12)
44219820Sjeff#define MTHCA_MPT_FLAG_LOCAL_WRITE   (1 << 11)
45219820Sjeff#define MTHCA_MPT_FLAG_LOCAL_READ    (1 << 10)
46219820Sjeff
47219820Sjeffstruct mthca_buf_list {
48219820Sjeff	void *buf;
49219820Sjeff	DECLARE_PCI_UNMAP_ADDR(mapping)
50219820Sjeff};
51219820Sjeff
52219820Sjeffunion mthca_buf {
53219820Sjeff	struct mthca_buf_list direct;
54219820Sjeff	struct mthca_buf_list *page_list;
55219820Sjeff};
56219820Sjeff
57219820Sjeffstruct mthca_uar {
58219820Sjeff	unsigned long pfn;
59219820Sjeff	int           index;
60219820Sjeff};
61219820Sjeff
62219820Sjeffstruct mthca_user_db_table;
63219820Sjeff
64219820Sjeffstruct mthca_ucontext {
65219820Sjeff	struct ib_ucontext          ibucontext;
66219820Sjeff	struct mthca_uar            uar;
67219820Sjeff	struct mthca_user_db_table *db_tab;
68219820Sjeff	int			    reg_mr_warned;
69219820Sjeff};
70219820Sjeff
71219820Sjeffstruct mthca_mtt;
72219820Sjeff
73219820Sjeffstruct mthca_mr {
74219820Sjeff	struct ib_mr      ibmr;
75219820Sjeff	struct ib_umem   *umem;
76219820Sjeff	struct mthca_mtt *mtt;
77219820Sjeff};
78219820Sjeff
79219820Sjeffstruct mthca_fmr {
80219820Sjeff	struct ib_fmr      ibmr;
81219820Sjeff	struct ib_fmr_attr attr;
82219820Sjeff	struct mthca_mtt  *mtt;
83219820Sjeff	int                maps;
84219820Sjeff	union {
85219820Sjeff		struct {
86219820Sjeff			struct mthca_mpt_entry __iomem *mpt;
87219820Sjeff			u64 __iomem *mtts;
88219820Sjeff		} tavor;
89219820Sjeff		struct {
90219820Sjeff			struct mthca_mpt_entry *mpt;
91219820Sjeff			__be64 *mtts;
92219820Sjeff			dma_addr_t dma_handle;
93219820Sjeff		} arbel;
94219820Sjeff	} mem;
95219820Sjeff};
96219820Sjeff
97219820Sjeffstruct mthca_pd {
98219820Sjeff	struct ib_pd    ibpd;
99219820Sjeff	u32             pd_num;
100219820Sjeff	atomic_t        sqp_count;
101219820Sjeff	struct mthca_mr ntmr;
102219820Sjeff	int             privileged;
103219820Sjeff};
104219820Sjeff
105219820Sjeffstruct mthca_eq {
106219820Sjeff	struct mthca_dev      *dev;
107219820Sjeff	int                    eqn;
108219820Sjeff	u32                    eqn_mask;
109219820Sjeff	u32                    cons_index;
110219820Sjeff	u16                    msi_x_vector;
111219820Sjeff	u16                    msi_x_entry;
112219820Sjeff	int                    have_irq;
113219820Sjeff	int                    nent;
114219820Sjeff	struct mthca_buf_list *page_list;
115219820Sjeff	struct mthca_mr        mr;
116219820Sjeff};
117219820Sjeff
118219820Sjeffstruct mthca_av;
119219820Sjeff
120219820Sjeffenum mthca_ah_type {
121219820Sjeff	MTHCA_AH_ON_HCA,
122219820Sjeff	MTHCA_AH_PCI_POOL,
123219820Sjeff	MTHCA_AH_KMALLOC
124219820Sjeff};
125219820Sjeff
126219820Sjeffstruct mthca_ah {
127219820Sjeff	struct ib_ah       ibah;
128219820Sjeff	enum mthca_ah_type type;
129219820Sjeff	u32                key;
130219820Sjeff	struct mthca_av   *av;
131219820Sjeff	dma_addr_t         avdma;
132219820Sjeff};
133219820Sjeff
134219820Sjeff/*
135219820Sjeff * Quick description of our CQ/QP locking scheme:
136219820Sjeff *
137219820Sjeff * We have one global lock that protects dev->cq/qp_table.  Each
138219820Sjeff * struct mthca_cq/qp also has its own lock.  An individual qp lock
139219820Sjeff * may be taken inside of an individual cq lock.  Both cqs attached to
140219820Sjeff * a qp may be locked, with the cq with the lower cqn locked first.
141219820Sjeff * No other nesting should be done.
142219820Sjeff *
143219820Sjeff * Each struct mthca_cq/qp also has an ref count, protected by the
144219820Sjeff * corresponding table lock.  The pointer from the cq/qp_table to the
145219820Sjeff * struct counts as one reference.  This reference also is good for
146219820Sjeff * access through the consumer API, so modifying the CQ/QP etc doesn't
147219820Sjeff * need to take another reference.  Access to a QP because of a
148219820Sjeff * completion being polled does not need a reference either.
149219820Sjeff *
150219820Sjeff * Finally, each struct mthca_cq/qp has a wait_queue_head_t for the
151219820Sjeff * destroy function to sleep on.
152219820Sjeff *
153219820Sjeff * This means that access from the consumer API requires nothing but
154219820Sjeff * taking the struct's lock.
155219820Sjeff *
156219820Sjeff * Access because of a completion event should go as follows:
157219820Sjeff * - lock cq/qp_table and look up struct
158219820Sjeff * - increment ref count in struct
159219820Sjeff * - drop cq/qp_table lock
160219820Sjeff * - lock struct, do your thing, and unlock struct
161219820Sjeff * - decrement ref count; if zero, wake up waiters
162219820Sjeff *
163219820Sjeff * To destroy a CQ/QP, we can do the following:
164219820Sjeff * - lock cq/qp_table
165219820Sjeff * - remove pointer and decrement ref count
166219820Sjeff * - unlock cq/qp_table lock
167219820Sjeff * - wait_event until ref count is zero
168219820Sjeff *
169219820Sjeff * It is the consumer's responsibilty to make sure that no QP
170219820Sjeff * operations (WQE posting or state modification) are pending when a
171219820Sjeff * QP is destroyed.  Also, the consumer must make sure that calls to
172219820Sjeff * qp_modify are serialized.  Similarly, the consumer is responsible
173219820Sjeff * for ensuring that no CQ resize operations are pending when a CQ
174219820Sjeff * is destroyed.
175219820Sjeff *
176219820Sjeff * Possible optimizations (wait for profile data to see if/where we
177219820Sjeff * have locks bouncing between CPUs):
178219820Sjeff * - split cq/qp table lock into n separate (cache-aligned) locks,
179219820Sjeff *   indexed (say) by the page in the table
180219820Sjeff * - split QP struct lock into three (one for common info, one for the
181219820Sjeff *   send queue and one for the receive queue)
182219820Sjeff */
183219820Sjeff
184219820Sjeffstruct mthca_cq_buf {
185219820Sjeff	union mthca_buf		queue;
186219820Sjeff	struct mthca_mr		mr;
187219820Sjeff	int			is_direct;
188219820Sjeff};
189219820Sjeff
190219820Sjeffstruct mthca_cq_resize {
191219820Sjeff	struct mthca_cq_buf	buf;
192219820Sjeff	int			cqe;
193219820Sjeff	enum {
194219820Sjeff		CQ_RESIZE_ALLOC,
195219820Sjeff		CQ_RESIZE_READY,
196219820Sjeff		CQ_RESIZE_SWAPPED
197219820Sjeff	}			state;
198219820Sjeff};
199219820Sjeff
200219820Sjeffstruct mthca_cq {
201219820Sjeff	struct ib_cq		ibcq;
202219820Sjeff	spinlock_t		lock;
203219820Sjeff	int			refcount;
204219820Sjeff	int			cqn;
205219820Sjeff	u32			cons_index;
206219820Sjeff	struct mthca_cq_buf	buf;
207219820Sjeff	struct mthca_cq_resize *resize_buf;
208219820Sjeff	int			is_kernel;
209219820Sjeff
210219820Sjeff	/* Next fields are Arbel only */
211219820Sjeff	int			set_ci_db_index;
212219820Sjeff	__be32		       *set_ci_db;
213219820Sjeff	int			arm_db_index;
214219820Sjeff	__be32		       *arm_db;
215219820Sjeff	int			arm_sn;
216219820Sjeff
217219820Sjeff	wait_queue_head_t	wait;
218219820Sjeff	struct mutex		mutex;
219219820Sjeff};
220219820Sjeff
221219820Sjeffstruct mthca_srq {
222219820Sjeff	struct ib_srq		ibsrq;
223219820Sjeff	spinlock_t		lock;
224219820Sjeff	int			refcount;
225219820Sjeff	int			srqn;
226219820Sjeff	int			max;
227219820Sjeff	int			max_gs;
228219820Sjeff	int			wqe_shift;
229219820Sjeff	int			first_free;
230219820Sjeff	int			last_free;
231219820Sjeff	u16			counter;  /* Arbel only */
232219820Sjeff	int			db_index; /* Arbel only */
233219820Sjeff	__be32		       *db;       /* Arbel only */
234219820Sjeff	void		       *last;
235219820Sjeff
236219820Sjeff	int			is_direct;
237219820Sjeff	u64		       *wrid;
238219820Sjeff	union mthca_buf		queue;
239219820Sjeff	struct mthca_mr		mr;
240219820Sjeff
241219820Sjeff	wait_queue_head_t	wait;
242219820Sjeff	struct mutex		mutex;
243219820Sjeff};
244219820Sjeff
245219820Sjeffstruct mthca_wq {
246219820Sjeff	spinlock_t lock;
247219820Sjeff	int        max;
248219820Sjeff	unsigned   next_ind;
249219820Sjeff	unsigned   last_comp;
250219820Sjeff	unsigned   head;
251219820Sjeff	unsigned   tail;
252219820Sjeff	void      *last;
253219820Sjeff	int        max_gs;
254219820Sjeff	int        wqe_shift;
255219820Sjeff
256219820Sjeff	int        db_index;	/* Arbel only */
257219820Sjeff	__be32    *db;
258219820Sjeff};
259219820Sjeff
260219820Sjeffstruct mthca_qp {
261219820Sjeff	struct ib_qp           ibqp;
262219820Sjeff	int                    refcount;
263219820Sjeff	u32                    qpn;
264219820Sjeff	int                    is_direct;
265219820Sjeff	u8                     port; /* for SQP and memfree use only */
266219820Sjeff	u8                     alt_port; /* for memfree use only */
267219820Sjeff	u8                     transport;
268219820Sjeff	u8                     state;
269219820Sjeff	u8                     atomic_rd_en;
270219820Sjeff	u8                     resp_depth;
271219820Sjeff
272219820Sjeff	struct mthca_mr        mr;
273219820Sjeff
274219820Sjeff	struct mthca_wq        rq;
275219820Sjeff	struct mthca_wq        sq;
276219820Sjeff	enum ib_sig_type       sq_policy;
277219820Sjeff	int                    send_wqe_offset;
278219820Sjeff	int                    max_inline_data;
279219820Sjeff
280219820Sjeff	u64                   *wrid;
281219820Sjeff	union mthca_buf	       queue;
282219820Sjeff
283219820Sjeff	wait_queue_head_t      wait;
284219820Sjeff	struct mutex	       mutex;
285219820Sjeff};
286219820Sjeff
287219820Sjeffstruct mthca_sqp {
288219820Sjeff	struct mthca_qp qp;
289219820Sjeff	int             pkey_index;
290219820Sjeff	u32             qkey;
291219820Sjeff	u32             send_psn;
292219820Sjeff	struct ib_ud_header ud_header;
293219820Sjeff	int             header_buf_size;
294219820Sjeff	void           *header_buf;
295219820Sjeff	dma_addr_t      header_dma;
296219820Sjeff};
297219820Sjeff
298219820Sjeffstatic inline struct mthca_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
299219820Sjeff{
300219820Sjeff	return container_of(ibucontext, struct mthca_ucontext, ibucontext);
301219820Sjeff}
302219820Sjeff
303219820Sjeffstatic inline struct mthca_fmr *to_mfmr(struct ib_fmr *ibmr)
304219820Sjeff{
305219820Sjeff	return container_of(ibmr, struct mthca_fmr, ibmr);
306219820Sjeff}
307219820Sjeff
308219820Sjeffstatic inline struct mthca_mr *to_mmr(struct ib_mr *ibmr)
309219820Sjeff{
310219820Sjeff	return container_of(ibmr, struct mthca_mr, ibmr);
311219820Sjeff}
312219820Sjeff
313219820Sjeffstatic inline struct mthca_pd *to_mpd(struct ib_pd *ibpd)
314219820Sjeff{
315219820Sjeff	return container_of(ibpd, struct mthca_pd, ibpd);
316219820Sjeff}
317219820Sjeff
318219820Sjeffstatic inline struct mthca_ah *to_mah(struct ib_ah *ibah)
319219820Sjeff{
320219820Sjeff	return container_of(ibah, struct mthca_ah, ibah);
321219820Sjeff}
322219820Sjeff
323219820Sjeffstatic inline struct mthca_cq *to_mcq(struct ib_cq *ibcq)
324219820Sjeff{
325219820Sjeff	return container_of(ibcq, struct mthca_cq, ibcq);
326219820Sjeff}
327219820Sjeff
328219820Sjeffstatic inline struct mthca_srq *to_msrq(struct ib_srq *ibsrq)
329219820Sjeff{
330219820Sjeff	return container_of(ibsrq, struct mthca_srq, ibsrq);
331219820Sjeff}
332219820Sjeff
333219820Sjeffstatic inline struct mthca_qp *to_mqp(struct ib_qp *ibqp)
334219820Sjeff{
335219820Sjeff	return container_of(ibqp, struct mthca_qp, ibqp);
336219820Sjeff}
337219820Sjeff
338219820Sjeffstatic inline struct mthca_sqp *to_msqp(struct mthca_qp *qp)
339219820Sjeff{
340219820Sjeff	return container_of(qp, struct mthca_sqp, qp);
341219820Sjeff}
342219820Sjeff
343219820Sjeff#endif /* MTHCA_PROVIDER_H */
344