2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include <infiniband/driver.h>
40 #include <infiniband/arch.h>
42 #ifdef HAVE_VALGRIND_MEMCHECK_H
44 # include <valgrind/memcheck.h>
46 # if !defined(VALGRIND_MAKE_MEM_DEFINED) || !defined(VALGRIND_MAKE_MEM_UNDEFINED)
47 # warning "Valgrind support requested, but VALGRIND_MAKE_MEM_(UN)DEFINED not available"
50 #endif /* HAVE_VALGRIND_MEMCHECK_H */
52 #ifndef VALGRIND_MAKE_MEM_DEFINED
53 # define VALGRIND_MAKE_MEM_DEFINED(addr,len)
56 #ifndef VALGRIND_MAKE_MEM_UNDEFINED
57 # define VALGRIND_MAKE_MEM_UNDEFINED(addr,len)
68 #define HIDDEN __attribute__((visibility ("hidden")))
78 MTHCA_CQ_ENTRY_SIZE = 0x20
82 MTHCA_QP_TABLE_BITS = 8,
83 MTHCA_QP_TABLE_SIZE = 1 << MTHCA_QP_TABLE_BITS,
84 MTHCA_QP_TABLE_MASK = MTHCA_QP_TABLE_SIZE - 1
88 MTHCA_DB_REC_PAGE_SIZE = 4096,
89 MTHCA_DB_REC_PER_PAGE = MTHCA_DB_REC_PAGE_SIZE / 8
93 MTHCA_DB_TYPE_INVALID = 0x0,
94 MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
95 MTHCA_DB_TYPE_CQ_ARM = 0x2,
96 MTHCA_DB_TYPE_SQ = 0x3,
97 MTHCA_DB_TYPE_RQ = 0x4,
98 MTHCA_DB_TYPE_SRQ = 0x5,
99 MTHCA_DB_TYPE_GROUP_SEP = 0x7
103 MTHCA_OPCODE_NOP = 0x00,
104 MTHCA_OPCODE_RDMA_WRITE = 0x08,
105 MTHCA_OPCODE_RDMA_WRITE_IMM = 0x09,
106 MTHCA_OPCODE_SEND = 0x0a,
107 MTHCA_OPCODE_SEND_IMM = 0x0b,
108 MTHCA_OPCODE_RDMA_READ = 0x10,
109 MTHCA_OPCODE_ATOMIC_CS = 0x11,
110 MTHCA_OPCODE_ATOMIC_FA = 0x12,
111 MTHCA_OPCODE_BIND_MW = 0x18,
112 MTHCA_OPCODE_INVALID = 0xff
115 struct mthca_ah_page;
117 struct mthca_device {
118 struct ibv_device ibv_dev;
119 enum mthca_hca_type hca_type;
123 struct mthca_db_table;
125 struct mthca_context {
126 struct ibv_context ibv_ctx;
128 pthread_spinlock_t uar_lock;
129 struct mthca_db_table *db_tab;
132 struct mthca_qp **table;
134 } qp_table[MTHCA_QP_TABLE_SIZE];
135 pthread_mutex_t qp_table_mutex;
147 struct ibv_pd ibv_pd;
148 struct mthca_ah_page *ah_list;
149 pthread_mutex_t ah_mutex;
154 struct ibv_cq ibv_cq;
155 struct mthca_buf buf;
156 pthread_spinlock_t lock;
161 /* Next fields are mem-free only */
170 struct ibv_srq ibv_srq;
171 struct mthca_buf buf;
173 pthread_spinlock_t lock;
184 /* Next fields are mem-free only */
191 pthread_spinlock_t lock;
201 /* Next fields are mem-free only */
207 struct ibv_qp ibv_qp;
208 struct mthca_buf buf;
228 uint32_t sl_tclass_flowlabel;
233 struct ibv_ah ibv_ah;
235 struct mthca_ah_page *page;
239 static inline unsigned long align(unsigned long val, unsigned long align)
241 return (val + align - 1) & ~(align - 1);
244 static inline uintptr_t db_align(uint32_t *db)
246 return (uintptr_t) db & ~((uintptr_t) MTHCA_DB_REC_PAGE_SIZE - 1);
249 #define to_mxxx(xxx, type) \
250 ((struct mthca_##type *) \
251 ((void *) ib##xxx - offsetof(struct mthca_##type, ibv_##xxx)))
253 static inline struct mthca_device *to_mdev(struct ibv_device *ibdev)
255 return to_mxxx(dev, device);
258 static inline struct mthca_context *to_mctx(struct ibv_context *ibctx)
260 return to_mxxx(ctx, context);
263 static inline struct mthca_pd *to_mpd(struct ibv_pd *ibpd)
265 return to_mxxx(pd, pd);
268 static inline struct mthca_cq *to_mcq(struct ibv_cq *ibcq)
270 return to_mxxx(cq, cq);
273 static inline struct mthca_srq *to_msrq(struct ibv_srq *ibsrq)
275 return to_mxxx(srq, srq);
278 static inline struct mthca_qp *to_mqp(struct ibv_qp *ibqp)
280 return to_mxxx(qp, qp);
283 static inline struct mthca_ah *to_mah(struct ibv_ah *ibah)
285 return to_mxxx(ah, ah);
288 static inline int mthca_is_memfree(struct ibv_context *ibctx)
290 return to_mdev(ibctx->device)->hca_type == MTHCA_ARBEL;
293 int mthca_alloc_buf(struct mthca_buf *buf, size_t size, int page_size);
294 void mthca_free_buf(struct mthca_buf *buf);
296 int mthca_alloc_db(struct mthca_db_table *db_tab, enum mthca_db_type type,
298 void mthca_set_db_qn(uint32_t *db, enum mthca_db_type type, uint32_t qn);
299 void mthca_free_db(struct mthca_db_table *db_tab, enum mthca_db_type type, int db_index);
300 struct mthca_db_table *mthca_alloc_db_tab(int uarc_size);
301 void mthca_free_db_tab(struct mthca_db_table *db_tab);
303 int mthca_query_device(struct ibv_context *context,
304 struct ibv_device_attr *attr);
305 int mthca_query_port(struct ibv_context *context, uint8_t port,
306 struct ibv_port_attr *attr);
308 struct ibv_pd *mthca_alloc_pd(struct ibv_context *context);
309 int mthca_free_pd(struct ibv_pd *pd);
311 struct ibv_mr *mthca_reg_mr(struct ibv_pd *pd, void *addr,
312 size_t length, enum ibv_access_flags access);
313 int mthca_dereg_mr(struct ibv_mr *mr);
315 struct ibv_cq *mthca_create_cq(struct ibv_context *context, int cqe,
316 struct ibv_comp_channel *channel,
318 int mthca_resize_cq(struct ibv_cq *cq, int cqe);
319 int mthca_destroy_cq(struct ibv_cq *cq);
320 int mthca_poll_cq(struct ibv_cq *cq, int ne, struct ibv_wc *wc);
321 int mthca_tavor_arm_cq(struct ibv_cq *cq, int solicited);
322 int mthca_arbel_arm_cq(struct ibv_cq *cq, int solicited);
323 void mthca_arbel_cq_event(struct ibv_cq *cq);
324 void __mthca_cq_clean(struct mthca_cq *cq, uint32_t qpn, struct mthca_srq *srq);
325 void mthca_cq_clean(struct mthca_cq *cq, uint32_t qpn, struct mthca_srq *srq);
326 void mthca_cq_resize_copy_cqes(struct mthca_cq *cq, void *buf, int new_cqe);
327 int mthca_alloc_cq_buf(struct mthca_device *dev, struct mthca_buf *buf, int nent);
329 struct ibv_srq *mthca_create_srq(struct ibv_pd *pd,
330 struct ibv_srq_init_attr *attr);
331 int mthca_modify_srq(struct ibv_srq *srq,
332 struct ibv_srq_attr *attr,
333 enum ibv_srq_attr_mask mask);
334 int mthca_query_srq(struct ibv_srq *srq,
335 struct ibv_srq_attr *attr);
336 int mthca_destroy_srq(struct ibv_srq *srq);
337 int mthca_alloc_srq_buf(struct ibv_pd *pd, struct ibv_srq_attr *attr,
338 struct mthca_srq *srq);
339 void mthca_free_srq_wqe(struct mthca_srq *srq, int ind);
340 int mthca_tavor_post_srq_recv(struct ibv_srq *ibsrq,
341 struct ibv_recv_wr *wr,
342 struct ibv_recv_wr **bad_wr);
343 int mthca_arbel_post_srq_recv(struct ibv_srq *ibsrq,
344 struct ibv_recv_wr *wr,
345 struct ibv_recv_wr **bad_wr);
347 struct ibv_qp *mthca_create_qp(struct ibv_pd *pd, struct ibv_qp_init_attr *attr);
348 int mthca_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
349 enum ibv_qp_attr_mask attr_mask,
350 struct ibv_qp_init_attr *init_attr);
351 int mthca_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
352 enum ibv_qp_attr_mask attr_mask);
353 int mthca_destroy_qp(struct ibv_qp *qp);
354 void mthca_init_qp_indices(struct mthca_qp *qp);
355 int mthca_tavor_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr,
356 struct ibv_send_wr **bad_wr);
357 int mthca_tavor_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr,
358 struct ibv_recv_wr **bad_wr);
359 int mthca_arbel_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr,
360 struct ibv_send_wr **bad_wr);
361 int mthca_arbel_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr,
362 struct ibv_recv_wr **bad_wr);
363 int mthca_alloc_qp_buf(struct ibv_pd *pd, struct ibv_qp_cap *cap,
364 enum ibv_qp_type type, struct mthca_qp *qp);
365 struct mthca_qp *mthca_find_qp(struct mthca_context *ctx, uint32_t qpn);
366 int mthca_store_qp(struct mthca_context *ctx, uint32_t qpn, struct mthca_qp *qp);
367 void mthca_clear_qp(struct mthca_context *ctx, uint32_t qpn);
368 int mthca_free_err_wqe(struct mthca_qp *qp, int is_send,
369 int index, int *dbd, uint32_t *new_wqe);
370 struct ibv_ah *mthca_create_ah(struct ibv_pd *pd, struct ibv_ah_attr *attr);
371 int mthca_destroy_ah(struct ibv_ah *ah);
372 int mthca_alloc_av(struct mthca_pd *pd, struct ibv_ah_attr *attr,
373 struct mthca_ah *ah);
374 void mthca_free_av(struct mthca_ah *ah);
375 int mthca_attach_mcast(struct ibv_qp *qp, union ibv_gid *gid, uint16_t lid);
376 int mthca_detach_mcast(struct ibv_qp *qp, union ibv_gid *gid, uint16_t lid);