2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/module.h>
34 #include <rdma/ib_umem.h>
35 #include <rdma/ib_umem_odp.h>
37 #include <linux/jiffies.h>
40 * Fill in a physical address list. ib_umem_num_dma_blocks() entries will be
41 * filled in the pas array.
43 void mlx5_ib_populate_pas(struct ib_umem
*umem
, size_t page_size
, __be64
*pas
,
46 struct ib_block_iter biter
;
48 rdma_umem_for_each_dma_block (umem
, &biter
, page_size
) {
49 *pas
= cpu_to_be64(rdma_block_iter_dma_address(&biter
) |
56 * Compute the page shift and page_offset for mailboxes that use a quantized
57 * page_offset. The granulatity of the page offset scales according to page
60 unsigned long __mlx5_umem_find_best_quantized_pgoff(
61 struct ib_umem
*umem
, unsigned long pgsz_bitmap
,
62 unsigned int page_offset_bits
, u64 pgoff_bitmask
, unsigned int scale
,
63 unsigned int *page_offset_quantized
)
65 const u64 page_offset_mask
= (1UL << page_offset_bits
) - 1;
66 unsigned long page_size
;
69 page_size
= ib_umem_find_best_pgoff(umem
, pgsz_bitmap
, pgoff_bitmask
);
74 * page size is the largest possible page size.
76 * Reduce the page_size, and thus the page_offset and quanta, until the
77 * page_offset fits into the mailbox field. Once page_size < scale this
78 * loop is guaranteed to terminate.
80 page_offset
= ib_umem_dma_offset(umem
, page_size
);
81 while (page_offset
& ~(u64
)(page_offset_mask
* (page_size
/ scale
))) {
83 page_offset
= ib_umem_dma_offset(umem
, page_size
);
87 * The address is not aligned, or otherwise cannot be represented by the
90 if (!(pgsz_bitmap
& page_size
))
93 *page_offset_quantized
=
94 (unsigned long)page_offset
/ (page_size
/ scale
);
95 if (WARN_ON(*page_offset_quantized
> page_offset_mask
))
100 #define WR_ID_BF 0xBF
101 #define WR_ID_END 0xBAD
102 #define TEST_WC_NUM_WQES 255
103 #define TEST_WC_POLLING_MAX_TIME_JIFFIES msecs_to_jiffies(100)
104 static int post_send_nop(struct mlx5_ib_dev
*dev
, struct ib_qp
*ibqp
, u64 wr_id
,
107 struct mlx5_ib_qp
*qp
= to_mqp(ibqp
);
108 struct mlx5_wqe_ctrl_seg
*ctrl
;
109 struct mlx5_bf
*bf
= &qp
->bf
;
110 __be32 mmio_wqe
[16] = {};
115 if (unlikely(dev
->mdev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
))
118 spin_lock_irqsave(&qp
->sq
.lock
, flags
);
120 idx
= qp
->sq
.cur_post
& (qp
->sq
.wqe_cnt
- 1);
121 ctrl
= mlx5_frag_buf_get_wqe(&qp
->sq
.fbc
, idx
);
123 memset(ctrl
, 0, sizeof(struct mlx5_wqe_ctrl_seg
));
124 ctrl
->fm_ce_se
= signaled
? MLX5_WQE_CTRL_CQ_UPDATE
: 0;
125 ctrl
->opmod_idx_opcode
=
126 cpu_to_be32(((u32
)(qp
->sq
.cur_post
) << 8) | MLX5_OPCODE_NOP
);
127 ctrl
->qpn_ds
= cpu_to_be32((sizeof(struct mlx5_wqe_ctrl_seg
) / 16) |
128 (qp
->trans_qp
.base
.mqp
.qpn
<< 8));
130 qp
->sq
.wrid
[idx
] = wr_id
;
131 qp
->sq
.w_list
[idx
].opcode
= MLX5_OPCODE_NOP
;
132 qp
->sq
.wqe_head
[idx
] = qp
->sq
.head
+ 1;
133 qp
->sq
.cur_post
+= DIV_ROUND_UP(sizeof(struct mlx5_wqe_ctrl_seg
),
135 qp
->sq
.w_list
[idx
].next
= qp
->sq
.cur_post
;
138 memcpy(mmio_wqe
, ctrl
, sizeof(*ctrl
));
139 ((struct mlx5_wqe_ctrl_seg
*)&mmio_wqe
)->fm_ce_se
|=
140 MLX5_WQE_CTRL_CQ_UPDATE
;
142 /* Make sure that descriptors are written before
143 * updating doorbell record and ringing the doorbell
147 qp
->db
.db
[MLX5_SND_DBR
] = cpu_to_be32(qp
->sq
.cur_post
);
149 /* Make sure doorbell record is visible to the HCA before
153 for (i
= 0; i
< 8; i
++)
154 mlx5_write64(&mmio_wqe
[i
* 2],
155 bf
->bfreg
->map
+ bf
->offset
+ i
* 8);
157 bf
->offset
^= bf
->buf_size
;
159 spin_unlock_irqrestore(&qp
->sq
.lock
, flags
);
164 static int test_wc_poll_cq_result(struct mlx5_ib_dev
*dev
, struct ib_cq
*cq
)
167 struct ib_wc wc
= {};
168 unsigned long end
= jiffies
+ TEST_WC_POLLING_MAX_TIME_JIFFIES
;
171 ret
= ib_poll_cq(cq
, 1, &wc
);
172 if (ret
< 0 || wc
.status
)
173 return ret
< 0 ? ret
: -EINVAL
;
176 } while (!time_after(jiffies
, end
));
181 if (wc
.wr_id
!= WR_ID_BF
)
187 static int test_wc_do_send(struct mlx5_ib_dev
*dev
, struct ib_qp
*qp
)
191 for (i
= 0; i
< TEST_WC_NUM_WQES
; i
++) {
192 err
= post_send_nop(dev
, qp
, WR_ID_BF
, false);
197 return post_send_nop(dev
, qp
, WR_ID_END
, true);
200 int mlx5_ib_test_wc(struct mlx5_ib_dev
*dev
)
202 struct ib_cq_init_attr cq_attr
= { .cqe
= TEST_WC_NUM_WQES
+ 1 };
203 int port_type_cap
= MLX5_CAP_GEN(dev
->mdev
, port_type
);
204 struct ib_qp_init_attr qp_init_attr
= {
205 .cap
= { .max_send_wr
= TEST_WC_NUM_WQES
},
206 .qp_type
= IB_QPT_UD
,
207 .sq_sig_type
= IB_SIGNAL_REQ_WR
,
208 .create_flags
= MLX5_IB_QP_CREATE_WC_TEST
,
210 struct ib_qp_attr qp_attr
= { .port_num
= 1 };
211 struct ib_device
*ibdev
= &dev
->ib_dev
;
217 if (!MLX5_CAP_GEN(dev
->mdev
, bf
))
220 if (!dev
->mdev
->roce
.roce_en
&&
221 port_type_cap
== MLX5_CAP_PORT_TYPE_ETH
) {
222 if (mlx5_core_is_pf(dev
->mdev
))
223 dev
->wc_support
= arch_can_pci_mmap_wc();
227 ret
= mlx5_alloc_bfreg(dev
->mdev
, &dev
->wc_bfreg
, true, false);
231 if (!dev
->wc_bfreg
.wc
)
234 pd
= ib_alloc_pd(ibdev
, 0);
240 cq
= ib_create_cq(ibdev
, NULL
, NULL
, NULL
, &cq_attr
);
246 qp_init_attr
.recv_cq
= cq
;
247 qp_init_attr
.send_cq
= cq
;
248 qp
= ib_create_qp(pd
, &qp_init_attr
);
254 qp_attr
.qp_state
= IB_QPS_INIT
;
255 ret
= ib_modify_qp(qp
, &qp_attr
,
256 IB_QP_STATE
| IB_QP_PORT
| IB_QP_PKEY_INDEX
|
261 qp_attr
.qp_state
= IB_QPS_RTR
;
262 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
266 qp_attr
.qp_state
= IB_QPS_RTS
;
267 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
271 ret
= test_wc_do_send(dev
, qp
);
275 ret
= test_wc_poll_cq_result(dev
, cq
);
277 dev
->wc_support
= true;
288 mlx5_free_bfreg(dev
->mdev
, &dev
->wc_bfreg
);
293 "Error %d while trying to test write-combining support\n",