1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
7 #include <linux/skbuff.h>
8 #include <linux/delay.h>
9 #include <linux/sched.h>
10 #include <linux/vmalloc.h>
11 #include <rdma/uverbs_ioctl.h>
15 #include "rxe_queue.h"
18 static int rxe_qp_chk_cap(struct rxe_dev *rxe, struct ib_qp_cap *cap,
21 if (cap->max_send_wr > rxe->attr.max_qp_wr) {
22 rxe_dbg_dev(rxe, "invalid send wr = %u > %d\n",
23 cap->max_send_wr, rxe->attr.max_qp_wr);
27 if (cap->max_send_sge > rxe->attr.max_send_sge) {
28 rxe_dbg_dev(rxe, "invalid send sge = %u > %d\n",
29 cap->max_send_sge, rxe->attr.max_send_sge);
34 if (cap->max_recv_wr > rxe->attr.max_qp_wr) {
35 rxe_dbg_dev(rxe, "invalid recv wr = %u > %d\n",
36 cap->max_recv_wr, rxe->attr.max_qp_wr);
40 if (cap->max_recv_sge > rxe->attr.max_recv_sge) {
41 rxe_dbg_dev(rxe, "invalid recv sge = %u > %d\n",
42 cap->max_recv_sge, rxe->attr.max_recv_sge);
47 if (cap->max_inline_data > rxe->max_inline_data) {
48 rxe_dbg_dev(rxe, "invalid max inline data = %u > %d\n",
49 cap->max_inline_data, rxe->max_inline_data);
59 int rxe_qp_chk_init(struct rxe_dev *rxe, struct ib_qp_init_attr *init)
61 struct ib_qp_cap *cap = &init->cap;
62 struct rxe_port *port;
63 int port_num = init->port_num;
65 switch (init->qp_type) {
75 if (!init->recv_cq || !init->send_cq) {
76 rxe_dbg_dev(rxe, "missing cq\n");
80 if (rxe_qp_chk_cap(rxe, cap, !!init->srq))
83 if (init->qp_type == IB_QPT_GSI) {
84 if (!rdma_is_port_valid(&rxe->ib_dev, port_num)) {
85 rxe_dbg_dev(rxe, "invalid port = %d\n", port_num);
91 if (init->qp_type == IB_QPT_GSI && port->qp_gsi_index) {
92 rxe_dbg_dev(rxe, "GSI QP exists for port %d\n", port_num);
103 static int alloc_rd_atomic_resources(struct rxe_qp *qp, unsigned int n)
105 qp->resp.res_head = 0;
106 qp->resp.res_tail = 0;
107 qp->resp.resources = kcalloc(n, sizeof(struct resp_res), GFP_KERNEL);
109 if (!qp->resp.resources)
115 static void free_rd_atomic_resources(struct rxe_qp *qp)
117 if (qp->resp.resources) {
120 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
121 struct resp_res *res = &qp->resp.resources[i];
123 free_rd_atomic_resource(res);
125 kfree(qp->resp.resources);
126 qp->resp.resources = NULL;
130 void free_rd_atomic_resource(struct resp_res *res)
135 static void cleanup_rd_atomic_resources(struct rxe_qp *qp)
138 struct resp_res *res;
140 if (qp->resp.resources) {
141 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
142 res = &qp->resp.resources[i];
143 free_rd_atomic_resource(res);
148 static void rxe_qp_init_misc(struct rxe_dev *rxe, struct rxe_qp *qp,
149 struct ib_qp_init_attr *init)
151 struct rxe_port *port;
154 qp->sq_sig_type = init->sq_sig_type;
155 qp->attr.path_mtu = 1;
156 qp->mtu = ib_mtu_enum_to_int(qp->attr.path_mtu);
158 qpn = qp->elem.index;
161 switch (init->qp_type) {
164 port->qp_gsi_index = qpn;
165 qp->attr.port_num = init->port_num;
169 qp->ibqp.qp_num = qpn;
173 spin_lock_init(&qp->state_lock);
175 spin_lock_init(&qp->sq.sq_lock);
176 spin_lock_init(&qp->rq.producer_lock);
177 spin_lock_init(&qp->rq.consumer_lock);
179 skb_queue_head_init(&qp->req_pkts);
180 skb_queue_head_init(&qp->resp_pkts);
182 atomic_set(&qp->ssn, 0);
183 atomic_set(&qp->skb_out, 0);
186 static int rxe_init_sq(struct rxe_qp *qp, struct ib_qp_init_attr *init,
187 struct ib_udata *udata,
188 struct rxe_create_qp_resp __user *uresp)
190 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
194 qp->sq.max_wr = init->cap.max_send_wr;
195 wqe_size = max_t(int, init->cap.max_send_sge * sizeof(struct ib_sge),
196 init->cap.max_inline_data);
197 qp->sq.max_sge = wqe_size / sizeof(struct ib_sge);
198 qp->sq.max_inline = wqe_size;
199 wqe_size += sizeof(struct rxe_send_wqe);
201 qp->sq.queue = rxe_queue_init(rxe, &qp->sq.max_wr, wqe_size,
202 QUEUE_TYPE_FROM_CLIENT);
204 rxe_err_qp(qp, "Unable to allocate send queue");
209 /* prepare info for caller to mmap send queue if user space qp */
210 err = do_mmap_info(rxe, uresp ? &uresp->sq_mi : NULL, udata,
211 qp->sq.queue->buf, qp->sq.queue->buf_size,
214 rxe_err_qp(qp, "do_mmap_info failed, err = %d", err);
218 /* return actual capabilities to caller which may be larger
221 init->cap.max_send_wr = qp->sq.max_wr;
222 init->cap.max_send_sge = qp->sq.max_sge;
223 init->cap.max_inline_data = qp->sq.max_inline;
228 vfree(qp->sq.queue->buf);
235 static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,
236 struct ib_qp_init_attr *init, struct ib_udata *udata,
237 struct rxe_create_qp_resp __user *uresp)
241 /* if we don't finish qp create make sure queue is valid */
242 skb_queue_head_init(&qp->req_pkts);
244 err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk);
247 qp->sk->sk->sk_user_data = qp;
249 /* pick a source UDP port number for this QP based on
250 * the source QPN. this spreads traffic for different QPs
251 * across different NIC RX queues (while using a single
252 * flow for a given QP to maintain packet order).
253 * the port number must be in the Dynamic Ports range
256 qp->src_port = RXE_ROCE_V2_SPORT + (hash_32(qp_num(qp), 14) & 0x3fff);
258 err = rxe_init_sq(qp, init, udata, uresp);
262 qp->req.wqe_index = queue_get_producer(qp->sq.queue,
263 QUEUE_TYPE_FROM_CLIENT);
266 qp->comp.opcode = -1;
268 rxe_init_task(&qp->req.task, qp, rxe_requester);
269 rxe_init_task(&qp->comp.task, qp, rxe_completer);
271 qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */
272 if (init->qp_type == IB_QPT_RC) {
273 timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0);
274 timer_setup(&qp->retrans_timer, retransmit_timer, 0);
279 static int rxe_init_rq(struct rxe_qp *qp, struct ib_qp_init_attr *init,
280 struct ib_udata *udata,
281 struct rxe_create_qp_resp __user *uresp)
283 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
287 qp->rq.max_wr = init->cap.max_recv_wr;
288 qp->rq.max_sge = init->cap.max_recv_sge;
289 wqe_size = sizeof(struct rxe_recv_wqe) +
290 qp->rq.max_sge*sizeof(struct ib_sge);
292 qp->rq.queue = rxe_queue_init(rxe, &qp->rq.max_wr, wqe_size,
293 QUEUE_TYPE_FROM_CLIENT);
295 rxe_err_qp(qp, "Unable to allocate recv queue");
300 /* prepare info for caller to mmap recv queue if user space qp */
301 err = do_mmap_info(rxe, uresp ? &uresp->rq_mi : NULL, udata,
302 qp->rq.queue->buf, qp->rq.queue->buf_size,
305 rxe_err_qp(qp, "do_mmap_info failed, err = %d", err);
309 /* return actual capabilities to caller which may be larger
312 init->cap.max_recv_wr = qp->rq.max_wr;
317 vfree(qp->rq.queue->buf);
324 static int rxe_qp_init_resp(struct rxe_dev *rxe, struct rxe_qp *qp,
325 struct ib_qp_init_attr *init,
326 struct ib_udata *udata,
327 struct rxe_create_qp_resp __user *uresp)
331 /* if we don't finish qp create make sure queue is valid */
332 skb_queue_head_init(&qp->resp_pkts);
335 err = rxe_init_rq(qp, init, udata, uresp);
340 rxe_init_task(&qp->resp.task, qp, rxe_responder);
342 qp->resp.opcode = OPCODE_NONE;
348 /* called by the create qp verb */
349 int rxe_qp_from_init(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_pd *pd,
350 struct ib_qp_init_attr *init,
351 struct rxe_create_qp_resp __user *uresp,
353 struct ib_udata *udata)
356 struct rxe_cq *rcq = to_rcq(init->recv_cq);
357 struct rxe_cq *scq = to_rcq(init->send_cq);
358 struct rxe_srq *srq = init->srq ? to_rsrq(init->srq) : NULL;
372 atomic_inc(&rcq->num_wq);
373 atomic_inc(&scq->num_wq);
375 rxe_qp_init_misc(rxe, qp, init);
377 err = rxe_qp_init_req(rxe, qp, init, udata, uresp);
381 err = rxe_qp_init_resp(rxe, qp, init, udata, uresp);
385 spin_lock_irqsave(&qp->state_lock, flags);
386 qp->attr.qp_state = IB_QPS_RESET;
388 spin_unlock_irqrestore(&qp->state_lock, flags);
393 rxe_queue_cleanup(qp->sq.queue);
396 atomic_dec(&rcq->num_wq);
397 atomic_dec(&scq->num_wq);
413 /* called by the query qp verb */
414 int rxe_qp_to_init(struct rxe_qp *qp, struct ib_qp_init_attr *init)
416 init->event_handler = qp->ibqp.event_handler;
417 init->qp_context = qp->ibqp.qp_context;
418 init->send_cq = qp->ibqp.send_cq;
419 init->recv_cq = qp->ibqp.recv_cq;
420 init->srq = qp->ibqp.srq;
422 init->cap.max_send_wr = qp->sq.max_wr;
423 init->cap.max_send_sge = qp->sq.max_sge;
424 init->cap.max_inline_data = qp->sq.max_inline;
427 init->cap.max_recv_wr = qp->rq.max_wr;
428 init->cap.max_recv_sge = qp->rq.max_sge;
431 init->sq_sig_type = qp->sq_sig_type;
433 init->qp_type = qp->ibqp.qp_type;
439 int rxe_qp_chk_attr(struct rxe_dev *rxe, struct rxe_qp *qp,
440 struct ib_qp_attr *attr, int mask)
442 if (mask & IB_QP_PORT) {
443 if (!rdma_is_port_valid(&rxe->ib_dev, attr->port_num)) {
444 rxe_dbg_qp(qp, "invalid port %d\n", attr->port_num);
449 if (mask & IB_QP_CAP && rxe_qp_chk_cap(rxe, &attr->cap, !!qp->srq))
452 if (mask & IB_QP_ACCESS_FLAGS) {
453 if (!(qp_type(qp) == IB_QPT_RC || qp_type(qp) == IB_QPT_UC))
455 if (attr->qp_access_flags & ~RXE_ACCESS_SUPPORTED_QP)
459 if (mask & IB_QP_AV && rxe_av_chk_attr(qp, &attr->ah_attr))
462 if (mask & IB_QP_ALT_PATH) {
463 if (rxe_av_chk_attr(qp, &attr->alt_ah_attr))
465 if (!rdma_is_port_valid(&rxe->ib_dev, attr->alt_port_num)) {
466 rxe_dbg_qp(qp, "invalid alt port %d\n", attr->alt_port_num);
469 if (attr->alt_timeout > 31) {
470 rxe_dbg_qp(qp, "invalid alt timeout %d > 31\n",
476 if (mask & IB_QP_PATH_MTU) {
477 struct rxe_port *port = &rxe->port;
479 enum ib_mtu max_mtu = port->attr.max_mtu;
480 enum ib_mtu mtu = attr->path_mtu;
483 rxe_dbg_qp(qp, "invalid mtu (%d) > (%d)\n",
484 ib_mtu_enum_to_int(mtu),
485 ib_mtu_enum_to_int(max_mtu));
490 if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
491 if (attr->max_rd_atomic > rxe->attr.max_qp_rd_atom) {
492 rxe_dbg_qp(qp, "invalid max_rd_atomic %d > %d\n",
494 rxe->attr.max_qp_rd_atom);
499 if (mask & IB_QP_TIMEOUT) {
500 if (attr->timeout > 31) {
501 rxe_dbg_qp(qp, "invalid timeout %d > 31\n",
513 /* move the qp to the reset state */
514 static void rxe_qp_reset(struct rxe_qp *qp)
516 /* stop tasks from running */
517 rxe_disable_task(&qp->resp.task);
518 rxe_disable_task(&qp->comp.task);
519 rxe_disable_task(&qp->req.task);
521 /* drain work and packet queuesc */
527 rxe_queue_reset(qp->rq.queue);
529 rxe_queue_reset(qp->sq.queue);
531 /* cleanup attributes */
532 atomic_set(&qp->ssn, 0);
534 qp->req.need_retry = 0;
535 qp->req.wait_for_rnr_timer = 0;
536 qp->req.noack_pkts = 0;
538 qp->resp.opcode = -1;
539 qp->resp.drop_msg = 0;
540 qp->resp.goto_error = 0;
541 qp->resp.sent_psn_nak = 0;
544 rxe_put(qp->resp.mr);
548 cleanup_rd_atomic_resources(qp);
551 rxe_enable_task(&qp->resp.task);
552 rxe_enable_task(&qp->comp.task);
553 rxe_enable_task(&qp->req.task);
556 /* move the qp to the error state */
557 void rxe_qp_error(struct rxe_qp *qp)
561 spin_lock_irqsave(&qp->state_lock, flags);
562 qp->attr.qp_state = IB_QPS_ERR;
564 /* drain work and packet queues */
565 rxe_sched_task(&qp->resp.task);
566 rxe_sched_task(&qp->comp.task);
567 rxe_sched_task(&qp->req.task);
568 spin_unlock_irqrestore(&qp->state_lock, flags);
571 static void rxe_qp_sqd(struct rxe_qp *qp, struct ib_qp_attr *attr,
576 spin_lock_irqsave(&qp->state_lock, flags);
577 qp->attr.sq_draining = 1;
578 rxe_sched_task(&qp->comp.task);
579 rxe_sched_task(&qp->req.task);
580 spin_unlock_irqrestore(&qp->state_lock, flags);
583 /* caller should hold qp->state_lock */
584 static int __qp_chk_state(struct rxe_qp *qp, struct ib_qp_attr *attr,
587 enum ib_qp_state cur_state;
588 enum ib_qp_state new_state;
590 cur_state = (mask & IB_QP_CUR_STATE) ?
591 attr->cur_qp_state : qp->attr.qp_state;
592 new_state = (mask & IB_QP_STATE) ?
593 attr->qp_state : cur_state;
595 if (!ib_modify_qp_is_ok(cur_state, new_state, qp_type(qp), mask))
598 if (mask & IB_QP_STATE && cur_state == IB_QPS_SQD) {
599 if (qp->attr.sq_draining && new_state != IB_QPS_ERR)
606 static const char *const qps2str[] = {
607 [IB_QPS_RESET] = "RESET",
608 [IB_QPS_INIT] = "INIT",
609 [IB_QPS_RTR] = "RTR",
610 [IB_QPS_RTS] = "RTS",
611 [IB_QPS_SQD] = "SQD",
612 [IB_QPS_SQE] = "SQE",
613 [IB_QPS_ERR] = "ERR",
616 /* called by the modify qp verb */
617 int rxe_qp_from_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask,
618 struct ib_udata *udata)
622 if (mask & IB_QP_CUR_STATE)
623 qp->attr.cur_qp_state = attr->qp_state;
625 if (mask & IB_QP_STATE) {
628 spin_lock_irqsave(&qp->state_lock, flags);
629 err = __qp_chk_state(qp, attr, mask);
631 qp->attr.qp_state = attr->qp_state;
632 rxe_dbg_qp(qp, "state -> %s\n",
633 qps2str[attr->qp_state]);
635 spin_unlock_irqrestore(&qp->state_lock, flags);
640 switch (attr->qp_state) {
645 rxe_qp_sqd(qp, attr, mask);
655 if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
656 int max_rd_atomic = attr->max_rd_atomic ?
657 roundup_pow_of_two(attr->max_rd_atomic) : 0;
659 qp->attr.max_rd_atomic = max_rd_atomic;
660 atomic_set(&qp->req.rd_atomic, max_rd_atomic);
663 if (mask & IB_QP_MAX_DEST_RD_ATOMIC) {
664 int max_dest_rd_atomic = attr->max_dest_rd_atomic ?
665 roundup_pow_of_two(attr->max_dest_rd_atomic) : 0;
667 qp->attr.max_dest_rd_atomic = max_dest_rd_atomic;
669 free_rd_atomic_resources(qp);
671 err = alloc_rd_atomic_resources(qp, max_dest_rd_atomic);
676 if (mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
677 qp->attr.en_sqd_async_notify = attr->en_sqd_async_notify;
679 if (mask & IB_QP_ACCESS_FLAGS)
680 qp->attr.qp_access_flags = attr->qp_access_flags;
682 if (mask & IB_QP_PKEY_INDEX)
683 qp->attr.pkey_index = attr->pkey_index;
685 if (mask & IB_QP_PORT)
686 qp->attr.port_num = attr->port_num;
688 if (mask & IB_QP_QKEY)
689 qp->attr.qkey = attr->qkey;
692 rxe_init_av(&attr->ah_attr, &qp->pri_av);
694 if (mask & IB_QP_ALT_PATH) {
695 rxe_init_av(&attr->alt_ah_attr, &qp->alt_av);
696 qp->attr.alt_port_num = attr->alt_port_num;
697 qp->attr.alt_pkey_index = attr->alt_pkey_index;
698 qp->attr.alt_timeout = attr->alt_timeout;
701 if (mask & IB_QP_PATH_MTU) {
702 qp->attr.path_mtu = attr->path_mtu;
703 qp->mtu = ib_mtu_enum_to_int(attr->path_mtu);
706 if (mask & IB_QP_TIMEOUT) {
707 qp->attr.timeout = attr->timeout;
708 if (attr->timeout == 0) {
709 qp->qp_timeout_jiffies = 0;
711 /* According to the spec, timeout = 4.096 * 2 ^ attr->timeout [us] */
712 int j = nsecs_to_jiffies(4096ULL << attr->timeout);
714 qp->qp_timeout_jiffies = j ? j : 1;
718 if (mask & IB_QP_RETRY_CNT) {
719 qp->attr.retry_cnt = attr->retry_cnt;
720 qp->comp.retry_cnt = attr->retry_cnt;
721 rxe_dbg_qp(qp, "set retry count = %d\n", attr->retry_cnt);
724 if (mask & IB_QP_RNR_RETRY) {
725 qp->attr.rnr_retry = attr->rnr_retry;
726 qp->comp.rnr_retry = attr->rnr_retry;
727 rxe_dbg_qp(qp, "set rnr retry count = %d\n", attr->rnr_retry);
730 if (mask & IB_QP_RQ_PSN) {
731 qp->attr.rq_psn = (attr->rq_psn & BTH_PSN_MASK);
732 qp->resp.psn = qp->attr.rq_psn;
733 rxe_dbg_qp(qp, "set resp psn = 0x%x\n", qp->resp.psn);
736 if (mask & IB_QP_MIN_RNR_TIMER) {
737 qp->attr.min_rnr_timer = attr->min_rnr_timer;
738 rxe_dbg_qp(qp, "set min rnr timer = 0x%x\n",
739 attr->min_rnr_timer);
742 if (mask & IB_QP_SQ_PSN) {
743 qp->attr.sq_psn = (attr->sq_psn & BTH_PSN_MASK);
744 qp->req.psn = qp->attr.sq_psn;
745 qp->comp.psn = qp->attr.sq_psn;
746 rxe_dbg_qp(qp, "set req psn = 0x%x\n", qp->req.psn);
749 if (mask & IB_QP_PATH_MIG_STATE)
750 qp->attr.path_mig_state = attr->path_mig_state;
752 if (mask & IB_QP_DEST_QPN)
753 qp->attr.dest_qp_num = attr->dest_qp_num;
758 /* called by the query qp verb */
759 int rxe_qp_to_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask)
765 attr->rq_psn = qp->resp.psn;
766 attr->sq_psn = qp->req.psn;
768 attr->cap.max_send_wr = qp->sq.max_wr;
769 attr->cap.max_send_sge = qp->sq.max_sge;
770 attr->cap.max_inline_data = qp->sq.max_inline;
773 attr->cap.max_recv_wr = qp->rq.max_wr;
774 attr->cap.max_recv_sge = qp->rq.max_sge;
777 rxe_av_to_attr(&qp->pri_av, &attr->ah_attr);
778 rxe_av_to_attr(&qp->alt_av, &attr->alt_ah_attr);
780 /* Applications that get this state typically spin on it.
781 * Yield the processor
783 spin_lock_irqsave(&qp->state_lock, flags);
784 if (qp->attr.sq_draining) {
785 spin_unlock_irqrestore(&qp->state_lock, flags);
788 spin_unlock_irqrestore(&qp->state_lock, flags);
794 int rxe_qp_chk_destroy(struct rxe_qp *qp)
797 * An attempt to destroy a QP while attached to a mcast group
798 * will fail immediately.
800 if (atomic_read(&qp->mcg_num)) {
801 rxe_dbg_qp(qp, "Attempt to destroy while attached to multicast group\n");
808 /* called when the last reference to the qp is dropped */
809 static void rxe_qp_do_cleanup(struct work_struct *work)
811 struct rxe_qp *qp = container_of(work, typeof(*qp), cleanup_work.work);
814 spin_lock_irqsave(&qp->state_lock, flags);
816 spin_unlock_irqrestore(&qp->state_lock, flags);
817 qp->qp_timeout_jiffies = 0;
819 if (qp_type(qp) == IB_QPT_RC) {
820 del_timer_sync(&qp->retrans_timer);
821 del_timer_sync(&qp->rnr_nak_timer);
824 if (qp->resp.task.func)
825 rxe_cleanup_task(&qp->resp.task);
827 if (qp->req.task.func)
828 rxe_cleanup_task(&qp->req.task);
830 if (qp->comp.task.func)
831 rxe_cleanup_task(&qp->comp.task);
833 /* flush out any receive wr's or pending requests */
839 rxe_queue_cleanup(qp->sq.queue);
845 rxe_queue_cleanup(qp->rq.queue);
848 atomic_dec(&qp->scq->num_wq);
853 atomic_dec(&qp->rcq->num_wq);
861 rxe_put(qp->resp.mr);
863 free_rd_atomic_resources(qp);
866 if (qp_type(qp) == IB_QPT_RC)
867 sk_dst_reset(qp->sk->sk);
869 kernel_sock_shutdown(qp->sk, SHUT_RDWR);
870 sock_release(qp->sk);
874 /* called when the last reference to the qp is dropped */
875 void rxe_qp_cleanup(struct rxe_pool_elem *elem)
877 struct rxe_qp *qp = container_of(elem, typeof(*qp), elem);
879 execute_in_process_context(rxe_qp_do_cleanup, &qp->cleanup_work);