blob: 6ff88c8250f6320eeb01671fe66406fe79f2be29 [file] [log] [blame]
Moni Shoua8700e3e2016-06-16 16:45:23 +03001/*
2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34#include <linux/skbuff.h>
35#include <linux/delay.h>
36#include <linux/sched.h>
37
38#include "rxe.h"
39#include "rxe_loc.h"
40#include "rxe_queue.h"
41#include "rxe_task.h"
42
Moni Shoua8700e3e2016-06-16 16:45:23 +030043static int rxe_qp_chk_cap(struct rxe_dev *rxe, struct ib_qp_cap *cap,
44 int has_srq)
45{
46 if (cap->max_send_wr > rxe->attr.max_qp_wr) {
47 pr_warn("invalid send wr = %d > %d\n",
48 cap->max_send_wr, rxe->attr.max_qp_wr);
49 goto err1;
50 }
51
Steve Wise33023fb2018-06-18 08:05:26 -070052 if (cap->max_send_sge > rxe->attr.max_send_sge) {
Moni Shoua8700e3e2016-06-16 16:45:23 +030053 pr_warn("invalid send sge = %d > %d\n",
Steve Wise33023fb2018-06-18 08:05:26 -070054 cap->max_send_sge, rxe->attr.max_send_sge);
Moni Shoua8700e3e2016-06-16 16:45:23 +030055 goto err1;
56 }
57
58 if (!has_srq) {
59 if (cap->max_recv_wr > rxe->attr.max_qp_wr) {
60 pr_warn("invalid recv wr = %d > %d\n",
61 cap->max_recv_wr, rxe->attr.max_qp_wr);
62 goto err1;
63 }
64
Steve Wise33023fb2018-06-18 08:05:26 -070065 if (cap->max_recv_sge > rxe->attr.max_recv_sge) {
Moni Shoua8700e3e2016-06-16 16:45:23 +030066 pr_warn("invalid recv sge = %d > %d\n",
Steve Wise33023fb2018-06-18 08:05:26 -070067 cap->max_recv_sge, rxe->attr.max_recv_sge);
Moni Shoua8700e3e2016-06-16 16:45:23 +030068 goto err1;
69 }
70 }
71
72 if (cap->max_inline_data > rxe->max_inline_data) {
73 pr_warn("invalid max inline data = %d > %d\n",
74 cap->max_inline_data, rxe->max_inline_data);
75 goto err1;
76 }
77
78 return 0;
79
80err1:
81 return -EINVAL;
82}
83
84int rxe_qp_chk_init(struct rxe_dev *rxe, struct ib_qp_init_attr *init)
85{
86 struct ib_qp_cap *cap = &init->cap;
87 struct rxe_port *port;
88 int port_num = init->port_num;
89
90 if (!init->recv_cq || !init->send_cq) {
91 pr_warn("missing cq\n");
92 goto err1;
93 }
94
95 if (rxe_qp_chk_cap(rxe, cap, !!init->srq))
96 goto err1;
97
98 if (init->qp_type == IB_QPT_SMI || init->qp_type == IB_QPT_GSI) {
99 if (port_num != 1) {
100 pr_warn("invalid port = %d\n", port_num);
101 goto err1;
102 }
103
104 port = &rxe->port;
105
106 if (init->qp_type == IB_QPT_SMI && port->qp_smi_index) {
107 pr_warn("SMI QP exists for port %d\n", port_num);
108 goto err1;
109 }
110
111 if (init->qp_type == IB_QPT_GSI && port->qp_gsi_index) {
112 pr_warn("GSI QP exists for port %d\n", port_num);
113 goto err1;
114 }
115 }
116
117 return 0;
118
119err1:
120 return -EINVAL;
121}
122
123static int alloc_rd_atomic_resources(struct rxe_qp *qp, unsigned int n)
124{
125 qp->resp.res_head = 0;
126 qp->resp.res_tail = 0;
127 qp->resp.resources = kcalloc(n, sizeof(struct resp_res), GFP_KERNEL);
128
129 if (!qp->resp.resources)
130 return -ENOMEM;
131
132 return 0;
133}
134
135static void free_rd_atomic_resources(struct rxe_qp *qp)
136{
137 if (qp->resp.resources) {
138 int i;
139
Parav Panditb6bbee02016-09-28 20:26:44 +0000140 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300141 struct resp_res *res = &qp->resp.resources[i];
142
143 free_rd_atomic_resource(qp, res);
144 }
145 kfree(qp->resp.resources);
146 qp->resp.resources = NULL;
147 }
148}
149
150void free_rd_atomic_resource(struct rxe_qp *qp, struct resp_res *res)
151{
152 if (res->type == RXE_ATOMIC_MASK) {
153 rxe_drop_ref(qp);
154 kfree_skb(res->atomic.skb);
155 } else if (res->type == RXE_READ_MASK) {
156 if (res->read.mr)
157 rxe_drop_ref(res->read.mr);
158 }
159 res->type = 0;
160}
161
162static void cleanup_rd_atomic_resources(struct rxe_qp *qp)
163{
164 int i;
165 struct resp_res *res;
166
167 if (qp->resp.resources) {
Parav Panditb6bbee02016-09-28 20:26:44 +0000168 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300169 res = &qp->resp.resources[i];
170 free_rd_atomic_resource(qp, res);
171 }
172 }
173}
174
175static void rxe_qp_init_misc(struct rxe_dev *rxe, struct rxe_qp *qp,
176 struct ib_qp_init_attr *init)
177{
178 struct rxe_port *port;
179 u32 qpn;
180
181 qp->sq_sig_type = init->sq_sig_type;
182 qp->attr.path_mtu = 1;
183 qp->mtu = ib_mtu_enum_to_int(qp->attr.path_mtu);
184
185 qpn = qp->pelem.index;
186 port = &rxe->port;
187
188 switch (init->qp_type) {
189 case IB_QPT_SMI:
190 qp->ibqp.qp_num = 0;
191 port->qp_smi_index = qpn;
192 qp->attr.port_num = init->port_num;
193 break;
194
195 case IB_QPT_GSI:
196 qp->ibqp.qp_num = 1;
197 port->qp_gsi_index = qpn;
198 qp->attr.port_num = init->port_num;
199 break;
200
201 default:
202 qp->ibqp.qp_num = qpn;
203 break;
204 }
205
206 INIT_LIST_HEAD(&qp->grp_list);
207
208 skb_queue_head_init(&qp->send_pkts);
209
210 spin_lock_init(&qp->grp_lock);
211 spin_lock_init(&qp->state_lock);
212
213 atomic_set(&qp->ssn, 0);
214 atomic_set(&qp->skb_out, 0);
215}
216
217static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,
218 struct ib_qp_init_attr *init,
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600219 struct ib_ucontext *context,
220 struct rxe_create_qp_resp __user *uresp)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300221{
222 int err;
223 int wqe_size;
224
225 err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk);
226 if (err < 0)
227 return err;
228 qp->sk->sk->sk_user_data = qp;
229
Vijay Immanueld3c04a32018-07-05 18:43:47 -0700230 /* pick a source UDP port number for this QP based on
231 * the source QPN. this spreads traffic for different QPs
232 * across different NIC RX queues (while using a single
233 * flow for a given QP to maintain packet order).
234 * the port number must be in the Dynamic Ports range
235 * (0xc000 - 0xffff).
236 */
237 qp->src_port = RXE_ROCE_V2_SPORT +
238 (hash_32_generic(qp_num(qp), 14) & 0x3fff);
239
Moni Shoua8700e3e2016-06-16 16:45:23 +0300240 qp->sq.max_wr = init->cap.max_send_wr;
241 qp->sq.max_sge = init->cap.max_send_sge;
242 qp->sq.max_inline = init->cap.max_inline_data;
243
244 wqe_size = max_t(int, sizeof(struct rxe_send_wqe) +
245 qp->sq.max_sge * sizeof(struct ib_sge),
246 sizeof(struct rxe_send_wqe) +
247 qp->sq.max_inline);
248
249 qp->sq.queue = rxe_queue_init(rxe,
250 &qp->sq.max_wr,
251 wqe_size);
252 if (!qp->sq.queue)
253 return -ENOMEM;
254
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600255 err = do_mmap_info(rxe, uresp ? &uresp->sq_mi : NULL, context,
256 qp->sq.queue->buf, qp->sq.queue->buf_size,
257 &qp->sq.queue->ip);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300258
259 if (err) {
260 kvfree(qp->sq.queue->buf);
261 kfree(qp->sq.queue);
262 return err;
263 }
264
265 qp->req.wqe_index = producer_index(qp->sq.queue);
266 qp->req.state = QP_STATE_RESET;
267 qp->req.opcode = -1;
268 qp->comp.opcode = -1;
269
270 spin_lock_init(&qp->sq.sq_lock);
271 skb_queue_head_init(&qp->req_pkts);
272
273 rxe_init_task(rxe, &qp->req.task, qp,
274 rxe_requester, "req");
275 rxe_init_task(rxe, &qp->comp.task, qp,
276 rxe_completer, "comp");
277
Moni Shoua8700e3e2016-06-16 16:45:23 +0300278 qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */
Parav Pandit99fc12f2017-03-19 11:20:56 +0200279 if (init->qp_type == IB_QPT_RC) {
Kees Cook3bfbea72017-10-24 03:28:27 -0700280 timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0);
281 timer_setup(&qp->retrans_timer, retransmit_timer, 0);
Parav Pandit99fc12f2017-03-19 11:20:56 +0200282 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300283 return 0;
284}
285
286static int rxe_qp_init_resp(struct rxe_dev *rxe, struct rxe_qp *qp,
287 struct ib_qp_init_attr *init,
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600288 struct ib_ucontext *context,
289 struct rxe_create_qp_resp __user *uresp)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300290{
291 int err;
292 int wqe_size;
293
294 if (!qp->srq) {
295 qp->rq.max_wr = init->cap.max_recv_wr;
296 qp->rq.max_sge = init->cap.max_recv_sge;
297
298 wqe_size = rcv_wqe_size(qp->rq.max_sge);
299
Parav Pandite404f942016-09-28 20:26:26 +0000300 pr_debug("qp#%d max_wr = %d, max_sge = %d, wqe_size = %d\n",
301 qp_num(qp), qp->rq.max_wr, qp->rq.max_sge, wqe_size);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300302
303 qp->rq.queue = rxe_queue_init(rxe,
304 &qp->rq.max_wr,
305 wqe_size);
306 if (!qp->rq.queue)
307 return -ENOMEM;
308
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600309 err = do_mmap_info(rxe, uresp ? &uresp->rq_mi : NULL, context,
310 qp->rq.queue->buf, qp->rq.queue->buf_size,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300311 &qp->rq.queue->ip);
312 if (err) {
313 kvfree(qp->rq.queue->buf);
314 kfree(qp->rq.queue);
315 return err;
316 }
317 }
318
319 spin_lock_init(&qp->rq.producer_lock);
320 spin_lock_init(&qp->rq.consumer_lock);
321
322 skb_queue_head_init(&qp->resp_pkts);
323
324 rxe_init_task(rxe, &qp->resp.task, qp,
325 rxe_responder, "resp");
326
327 qp->resp.opcode = OPCODE_NONE;
328 qp->resp.msn = 0;
329 qp->resp.state = QP_STATE_RESET;
330
331 return 0;
332}
333
334/* called by the create qp verb */
335int rxe_qp_from_init(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_pd *pd,
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600336 struct ib_qp_init_attr *init,
337 struct rxe_create_qp_resp __user *uresp,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300338 struct ib_pd *ibpd)
339{
340 int err;
341 struct rxe_cq *rcq = to_rcq(init->recv_cq);
342 struct rxe_cq *scq = to_rcq(init->send_cq);
343 struct rxe_srq *srq = init->srq ? to_rsrq(init->srq) : NULL;
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600344 struct ib_ucontext *context = ibpd->uobject ? ibpd->uobject->context : NULL;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300345
346 rxe_add_ref(pd);
347 rxe_add_ref(rcq);
348 rxe_add_ref(scq);
349 if (srq)
350 rxe_add_ref(srq);
351
352 qp->pd = pd;
353 qp->rcq = rcq;
354 qp->scq = scq;
355 qp->srq = srq;
356
357 rxe_qp_init_misc(rxe, qp, init);
358
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600359 err = rxe_qp_init_req(rxe, qp, init, context, uresp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300360 if (err)
361 goto err1;
362
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600363 err = rxe_qp_init_resp(rxe, qp, init, context, uresp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300364 if (err)
365 goto err2;
366
367 qp->attr.qp_state = IB_QPS_RESET;
368 qp->valid = 1;
369
370 return 0;
371
372err2:
373 rxe_queue_cleanup(qp->sq.queue);
374err1:
375 if (srq)
376 rxe_drop_ref(srq);
377 rxe_drop_ref(scq);
378 rxe_drop_ref(rcq);
379 rxe_drop_ref(pd);
380
381 return err;
382}
383
384/* called by the query qp verb */
385int rxe_qp_to_init(struct rxe_qp *qp, struct ib_qp_init_attr *init)
386{
387 init->event_handler = qp->ibqp.event_handler;
388 init->qp_context = qp->ibqp.qp_context;
389 init->send_cq = qp->ibqp.send_cq;
390 init->recv_cq = qp->ibqp.recv_cq;
391 init->srq = qp->ibqp.srq;
392
393 init->cap.max_send_wr = qp->sq.max_wr;
394 init->cap.max_send_sge = qp->sq.max_sge;
395 init->cap.max_inline_data = qp->sq.max_inline;
396
397 if (!qp->srq) {
398 init->cap.max_recv_wr = qp->rq.max_wr;
399 init->cap.max_recv_sge = qp->rq.max_sge;
400 }
401
402 init->sq_sig_type = qp->sq_sig_type;
403
404 init->qp_type = qp->ibqp.qp_type;
405 init->port_num = 1;
406
407 return 0;
408}
409
410/* called by the modify qp verb, this routine checks all the parameters before
411 * making any changes
412 */
413int rxe_qp_chk_attr(struct rxe_dev *rxe, struct rxe_qp *qp,
414 struct ib_qp_attr *attr, int mask)
415{
416 enum ib_qp_state cur_state = (mask & IB_QP_CUR_STATE) ?
417 attr->cur_qp_state : qp->attr.qp_state;
418 enum ib_qp_state new_state = (mask & IB_QP_STATE) ?
419 attr->qp_state : cur_state;
420
421 if (!ib_modify_qp_is_ok(cur_state, new_state, qp_type(qp), mask,
422 IB_LINK_LAYER_ETHERNET)) {
423 pr_warn("invalid mask or state for qp\n");
424 goto err1;
425 }
426
427 if (mask & IB_QP_STATE) {
428 if (cur_state == IB_QPS_SQD) {
429 if (qp->req.state == QP_STATE_DRAIN &&
430 new_state != IB_QPS_ERR)
431 goto err1;
432 }
433 }
434
435 if (mask & IB_QP_PORT) {
436 if (attr->port_num != 1) {
437 pr_warn("invalid port %d\n", attr->port_num);
438 goto err1;
439 }
440 }
441
442 if (mask & IB_QP_CAP && rxe_qp_chk_cap(rxe, &attr->cap, !!qp->srq))
443 goto err1;
444
445 if (mask & IB_QP_AV && rxe_av_chk_attr(rxe, &attr->ah_attr))
446 goto err1;
447
448 if (mask & IB_QP_ALT_PATH) {
449 if (rxe_av_chk_attr(rxe, &attr->alt_ah_attr))
450 goto err1;
451 if (attr->alt_port_num != 1) {
452 pr_warn("invalid alt port %d\n", attr->alt_port_num);
453 goto err1;
454 }
455 if (attr->alt_timeout > 31) {
456 pr_warn("invalid QP alt timeout %d > 31\n",
457 attr->alt_timeout);
458 goto err1;
459 }
460 }
461
462 if (mask & IB_QP_PATH_MTU) {
463 struct rxe_port *port = &rxe->port;
464
465 enum ib_mtu max_mtu = port->attr.max_mtu;
466 enum ib_mtu mtu = attr->path_mtu;
467
468 if (mtu > max_mtu) {
469 pr_debug("invalid mtu (%d) > (%d)\n",
470 ib_mtu_enum_to_int(mtu),
471 ib_mtu_enum_to_int(max_mtu));
472 goto err1;
473 }
474 }
475
476 if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
477 if (attr->max_rd_atomic > rxe->attr.max_qp_rd_atom) {
478 pr_warn("invalid max_rd_atomic %d > %d\n",
479 attr->max_rd_atomic,
480 rxe->attr.max_qp_rd_atom);
481 goto err1;
482 }
483 }
484
485 if (mask & IB_QP_TIMEOUT) {
486 if (attr->timeout > 31) {
487 pr_warn("invalid QP timeout %d > 31\n",
488 attr->timeout);
489 goto err1;
490 }
491 }
492
493 return 0;
494
495err1:
496 return -EINVAL;
497}
498
499/* move the qp to the reset state */
500static void rxe_qp_reset(struct rxe_qp *qp)
501{
502 /* stop tasks from running */
503 rxe_disable_task(&qp->resp.task);
504
505 /* stop request/comp */
506 if (qp->sq.queue) {
507 if (qp_type(qp) == IB_QPT_RC)
508 rxe_disable_task(&qp->comp.task);
509 rxe_disable_task(&qp->req.task);
510 }
511
512 /* move qp to the reset state */
513 qp->req.state = QP_STATE_RESET;
514 qp->resp.state = QP_STATE_RESET;
515
516 /* let state machines reset themselves drain work and packet queues
517 * etc.
518 */
519 __rxe_do_task(&qp->resp.task);
520
521 if (qp->sq.queue) {
522 __rxe_do_task(&qp->comp.task);
523 __rxe_do_task(&qp->req.task);
Yonatan Cohenaa75b072016-11-16 10:39:17 +0200524 rxe_queue_reset(qp->sq.queue);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300525 }
526
527 /* cleanup attributes */
528 atomic_set(&qp->ssn, 0);
529 qp->req.opcode = -1;
530 qp->req.need_retry = 0;
531 qp->req.noack_pkts = 0;
532 qp->resp.msn = 0;
533 qp->resp.opcode = -1;
534 qp->resp.drop_msg = 0;
535 qp->resp.goto_error = 0;
536 qp->resp.sent_psn_nak = 0;
537
538 if (qp->resp.mr) {
539 rxe_drop_ref(qp->resp.mr);
540 qp->resp.mr = NULL;
541 }
542
543 cleanup_rd_atomic_resources(qp);
544
545 /* reenable tasks */
546 rxe_enable_task(&qp->resp.task);
547
548 if (qp->sq.queue) {
549 if (qp_type(qp) == IB_QPT_RC)
550 rxe_enable_task(&qp->comp.task);
551
552 rxe_enable_task(&qp->req.task);
553 }
554}
555
556/* drain the send queue */
557static void rxe_qp_drain(struct rxe_qp *qp)
558{
559 if (qp->sq.queue) {
560 if (qp->req.state != QP_STATE_DRAINED) {
561 qp->req.state = QP_STATE_DRAIN;
562 if (qp_type(qp) == IB_QPT_RC)
563 rxe_run_task(&qp->comp.task, 1);
564 else
565 __rxe_do_task(&qp->comp.task);
566 rxe_run_task(&qp->req.task, 1);
567 }
568 }
569}
570
571/* move the qp to the error state */
572void rxe_qp_error(struct rxe_qp *qp)
573{
574 qp->req.state = QP_STATE_ERROR;
575 qp->resp.state = QP_STATE_ERROR;
Yonatan Cohen6d931302016-11-16 10:39:18 +0200576 qp->attr.qp_state = IB_QPS_ERR;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300577
578 /* drain work and packet queues */
579 rxe_run_task(&qp->resp.task, 1);
580
581 if (qp_type(qp) == IB_QPT_RC)
582 rxe_run_task(&qp->comp.task, 1);
583 else
584 __rxe_do_task(&qp->comp.task);
585 rxe_run_task(&qp->req.task, 1);
586}
587
588/* called by the modify qp verb */
589int rxe_qp_from_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask,
590 struct ib_udata *udata)
591{
592 int err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300593
594 if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
595 int max_rd_atomic = __roundup_pow_of_two(attr->max_rd_atomic);
596
Moni Shoua8700e3e2016-06-16 16:45:23 +0300597 qp->attr.max_rd_atomic = max_rd_atomic;
598 atomic_set(&qp->req.rd_atomic, max_rd_atomic);
599 }
600
Parav Panditb6bbee02016-09-28 20:26:44 +0000601 if (mask & IB_QP_MAX_DEST_RD_ATOMIC) {
602 int max_dest_rd_atomic =
603 __roundup_pow_of_two(attr->max_dest_rd_atomic);
604
605 qp->attr.max_dest_rd_atomic = max_dest_rd_atomic;
606
607 free_rd_atomic_resources(qp);
608
609 err = alloc_rd_atomic_resources(qp, max_dest_rd_atomic);
610 if (err)
611 return err;
612 }
613
Moni Shoua8700e3e2016-06-16 16:45:23 +0300614 if (mask & IB_QP_CUR_STATE)
615 qp->attr.cur_qp_state = attr->qp_state;
616
617 if (mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
618 qp->attr.en_sqd_async_notify = attr->en_sqd_async_notify;
619
620 if (mask & IB_QP_ACCESS_FLAGS)
621 qp->attr.qp_access_flags = attr->qp_access_flags;
622
623 if (mask & IB_QP_PKEY_INDEX)
624 qp->attr.pkey_index = attr->pkey_index;
625
626 if (mask & IB_QP_PORT)
627 qp->attr.port_num = attr->port_num;
628
629 if (mask & IB_QP_QKEY)
630 qp->attr.qkey = attr->qkey;
631
632 if (mask & IB_QP_AV) {
Zhu Yanjun1a241db2018-01-31 06:06:54 -0500633 rxe_av_from_attr(attr->port_num, &qp->pri_av, &attr->ah_attr);
Parav Pandit47ec3862018-06-13 10:22:06 +0300634 rxe_av_fill_ip_info(&qp->pri_av, &attr->ah_attr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300635 }
636
637 if (mask & IB_QP_ALT_PATH) {
Zhu Yanjun1a241db2018-01-31 06:06:54 -0500638 rxe_av_from_attr(attr->alt_port_num, &qp->alt_av,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300639 &attr->alt_ah_attr);
Parav Pandit47ec3862018-06-13 10:22:06 +0300640 rxe_av_fill_ip_info(&qp->alt_av, &attr->alt_ah_attr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300641 qp->attr.alt_port_num = attr->alt_port_num;
642 qp->attr.alt_pkey_index = attr->alt_pkey_index;
643 qp->attr.alt_timeout = attr->alt_timeout;
644 }
645
646 if (mask & IB_QP_PATH_MTU) {
647 qp->attr.path_mtu = attr->path_mtu;
648 qp->mtu = ib_mtu_enum_to_int(attr->path_mtu);
649 }
650
651 if (mask & IB_QP_TIMEOUT) {
652 qp->attr.timeout = attr->timeout;
653 if (attr->timeout == 0) {
654 qp->qp_timeout_jiffies = 0;
655 } else {
656 /* According to the spec, timeout = 4.096 * 2 ^ attr->timeout [us] */
657 int j = nsecs_to_jiffies(4096ULL << attr->timeout);
658
659 qp->qp_timeout_jiffies = j ? j : 1;
660 }
661 }
662
663 if (mask & IB_QP_RETRY_CNT) {
664 qp->attr.retry_cnt = attr->retry_cnt;
665 qp->comp.retry_cnt = attr->retry_cnt;
Parav Pandite404f942016-09-28 20:26:26 +0000666 pr_debug("qp#%d set retry count = %d\n", qp_num(qp),
667 attr->retry_cnt);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300668 }
669
670 if (mask & IB_QP_RNR_RETRY) {
671 qp->attr.rnr_retry = attr->rnr_retry;
672 qp->comp.rnr_retry = attr->rnr_retry;
Parav Pandite404f942016-09-28 20:26:26 +0000673 pr_debug("qp#%d set rnr retry count = %d\n", qp_num(qp),
674 attr->rnr_retry);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300675 }
676
677 if (mask & IB_QP_RQ_PSN) {
678 qp->attr.rq_psn = (attr->rq_psn & BTH_PSN_MASK);
679 qp->resp.psn = qp->attr.rq_psn;
Parav Pandite404f942016-09-28 20:26:26 +0000680 pr_debug("qp#%d set resp psn = 0x%x\n", qp_num(qp),
681 qp->resp.psn);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300682 }
683
684 if (mask & IB_QP_MIN_RNR_TIMER) {
685 qp->attr.min_rnr_timer = attr->min_rnr_timer;
Parav Pandite404f942016-09-28 20:26:26 +0000686 pr_debug("qp#%d set min rnr timer = 0x%x\n", qp_num(qp),
Moni Shoua8700e3e2016-06-16 16:45:23 +0300687 attr->min_rnr_timer);
688 }
689
690 if (mask & IB_QP_SQ_PSN) {
691 qp->attr.sq_psn = (attr->sq_psn & BTH_PSN_MASK);
692 qp->req.psn = qp->attr.sq_psn;
693 qp->comp.psn = qp->attr.sq_psn;
Parav Pandite404f942016-09-28 20:26:26 +0000694 pr_debug("qp#%d set req psn = 0x%x\n", qp_num(qp), qp->req.psn);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300695 }
696
Moni Shoua8700e3e2016-06-16 16:45:23 +0300697 if (mask & IB_QP_PATH_MIG_STATE)
698 qp->attr.path_mig_state = attr->path_mig_state;
699
700 if (mask & IB_QP_DEST_QPN)
701 qp->attr.dest_qp_num = attr->dest_qp_num;
702
703 if (mask & IB_QP_STATE) {
704 qp->attr.qp_state = attr->qp_state;
705
706 switch (attr->qp_state) {
707 case IB_QPS_RESET:
Parav Pandite404f942016-09-28 20:26:26 +0000708 pr_debug("qp#%d state -> RESET\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300709 rxe_qp_reset(qp);
710 break;
711
712 case IB_QPS_INIT:
Parav Pandite404f942016-09-28 20:26:26 +0000713 pr_debug("qp#%d state -> INIT\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300714 qp->req.state = QP_STATE_INIT;
715 qp->resp.state = QP_STATE_INIT;
716 break;
717
718 case IB_QPS_RTR:
Parav Pandite404f942016-09-28 20:26:26 +0000719 pr_debug("qp#%d state -> RTR\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300720 qp->resp.state = QP_STATE_READY;
721 break;
722
723 case IB_QPS_RTS:
Parav Pandite404f942016-09-28 20:26:26 +0000724 pr_debug("qp#%d state -> RTS\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300725 qp->req.state = QP_STATE_READY;
726 break;
727
728 case IB_QPS_SQD:
Parav Pandite404f942016-09-28 20:26:26 +0000729 pr_debug("qp#%d state -> SQD\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300730 rxe_qp_drain(qp);
731 break;
732
733 case IB_QPS_SQE:
Parav Pandite404f942016-09-28 20:26:26 +0000734 pr_warn("qp#%d state -> SQE !!?\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300735 /* Not possible from modify_qp. */
736 break;
737
738 case IB_QPS_ERR:
Parav Pandite404f942016-09-28 20:26:26 +0000739 pr_debug("qp#%d state -> ERR\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300740 rxe_qp_error(qp);
741 break;
742 }
743 }
744
745 return 0;
746}
747
748/* called by the query qp verb */
749int rxe_qp_to_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask)
750{
Moni Shoua8700e3e2016-06-16 16:45:23 +0300751 *attr = qp->attr;
752
753 attr->rq_psn = qp->resp.psn;
754 attr->sq_psn = qp->req.psn;
755
756 attr->cap.max_send_wr = qp->sq.max_wr;
757 attr->cap.max_send_sge = qp->sq.max_sge;
758 attr->cap.max_inline_data = qp->sq.max_inline;
759
760 if (!qp->srq) {
761 attr->cap.max_recv_wr = qp->rq.max_wr;
762 attr->cap.max_recv_sge = qp->rq.max_sge;
763 }
764
Zhu Yanjun9c96f3d2018-01-31 06:06:56 -0500765 rxe_av_to_attr(&qp->pri_av, &attr->ah_attr);
766 rxe_av_to_attr(&qp->alt_av, &attr->alt_ah_attr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300767
768 if (qp->req.state == QP_STATE_DRAIN) {
769 attr->sq_draining = 1;
770 /* applications that get this state
771 * typically spin on it. yield the
772 * processor
773 */
774 cond_resched();
775 } else {
776 attr->sq_draining = 0;
777 }
778
779 pr_debug("attr->sq_draining = %d\n", attr->sq_draining);
780
781 return 0;
782}
783
784/* called by the destroy qp verb */
785void rxe_qp_destroy(struct rxe_qp *qp)
786{
787 qp->valid = 0;
788 qp->qp_timeout_jiffies = 0;
789 rxe_cleanup_task(&qp->resp.task);
790
Parav Pandit99fc12f2017-03-19 11:20:56 +0200791 if (qp_type(qp) == IB_QPT_RC) {
792 del_timer_sync(&qp->retrans_timer);
793 del_timer_sync(&qp->rnr_nak_timer);
794 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300795
796 rxe_cleanup_task(&qp->req.task);
Yonatan Cohen2d4b21e2017-01-19 15:25:59 +0200797 rxe_cleanup_task(&qp->comp.task);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300798
799 /* flush out any receive wr's or pending requests */
800 __rxe_do_task(&qp->req.task);
801 if (qp->sq.queue) {
802 __rxe_do_task(&qp->comp.task);
803 __rxe_do_task(&qp->req.task);
804 }
805}
806
807/* called when the last reference to the qp is dropped */
Bart Van Asschebb3ffb72018-01-12 15:11:59 -0800808static void rxe_qp_do_cleanup(struct work_struct *work)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300809{
Bart Van Asschebb3ffb72018-01-12 15:11:59 -0800810 struct rxe_qp *qp = container_of(work, typeof(*qp), cleanup_work.work);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300811
812 rxe_drop_all_mcast_groups(qp);
813
814 if (qp->sq.queue)
815 rxe_queue_cleanup(qp->sq.queue);
816
817 if (qp->srq)
818 rxe_drop_ref(qp->srq);
819
820 if (qp->rq.queue)
821 rxe_queue_cleanup(qp->rq.queue);
822
823 if (qp->scq)
824 rxe_drop_ref(qp->scq);
825 if (qp->rcq)
826 rxe_drop_ref(qp->rcq);
827 if (qp->pd)
828 rxe_drop_ref(qp->pd);
829
830 if (qp->resp.mr) {
831 rxe_drop_ref(qp->resp.mr);
832 qp->resp.mr = NULL;
833 }
834
Andrew Boyer825a51a2017-08-28 16:11:55 -0400835 if (qp_type(qp) == IB_QPT_RC)
836 sk_dst_reset(qp->sk->sk);
yonatanc4ed6ad12017-04-20 20:55:56 +0300837
Moni Shoua8700e3e2016-06-16 16:45:23 +0300838 free_rd_atomic_resources(qp);
839
840 kernel_sock_shutdown(qp->sk, SHUT_RDWR);
Bart Van Asschee2599342016-12-15 17:15:07 +0100841 sock_release(qp->sk);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300842}
Bart Van Asschebb3ffb72018-01-12 15:11:59 -0800843
844/* called when the last reference to the qp is dropped */
845void rxe_qp_cleanup(struct rxe_pool_entry *arg)
846{
847 struct rxe_qp *qp = container_of(arg, typeof(*qp), pelem);
848
849 execute_in_process_context(rxe_qp_do_cleanup, &qp->cleanup_work);
850}