xref: /linux/drivers/infiniband/sw/rxe/rxe_qp.c (revision 2da68a77)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5  */
6 
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>
12 
13 #include "rxe.h"
14 #include "rxe_loc.h"
15 #include "rxe_queue.h"
16 #include "rxe_task.h"
17 
18 static int rxe_qp_chk_cap(struct rxe_dev *rxe, struct ib_qp_cap *cap,
19 			  int has_srq)
20 {
21 	if (cap->max_send_wr > rxe->attr.max_qp_wr) {
22 		pr_debug("invalid send wr = %u > %d\n",
23 			 cap->max_send_wr, rxe->attr.max_qp_wr);
24 		goto err1;
25 	}
26 
27 	if (cap->max_send_sge > rxe->attr.max_send_sge) {
28 		pr_debug("invalid send sge = %u > %d\n",
29 			 cap->max_send_sge, rxe->attr.max_send_sge);
30 		goto err1;
31 	}
32 
33 	if (!has_srq) {
34 		if (cap->max_recv_wr > rxe->attr.max_qp_wr) {
35 			pr_debug("invalid recv wr = %u > %d\n",
36 				 cap->max_recv_wr, rxe->attr.max_qp_wr);
37 			goto err1;
38 		}
39 
40 		if (cap->max_recv_sge > rxe->attr.max_recv_sge) {
41 			pr_debug("invalid recv sge = %u > %d\n",
42 				 cap->max_recv_sge, rxe->attr.max_recv_sge);
43 			goto err1;
44 		}
45 	}
46 
47 	if (cap->max_inline_data > rxe->max_inline_data) {
48 		pr_debug("invalid max inline data = %u > %d\n",
49 			 cap->max_inline_data, rxe->max_inline_data);
50 		goto err1;
51 	}
52 
53 	return 0;
54 
55 err1:
56 	return -EINVAL;
57 }
58 
59 int rxe_qp_chk_init(struct rxe_dev *rxe, struct ib_qp_init_attr *init)
60 {
61 	struct ib_qp_cap *cap = &init->cap;
62 	struct rxe_port *port;
63 	int port_num = init->port_num;
64 
65 	switch (init->qp_type) {
66 	case IB_QPT_GSI:
67 	case IB_QPT_RC:
68 	case IB_QPT_UC:
69 	case IB_QPT_UD:
70 		break;
71 	default:
72 		return -EOPNOTSUPP;
73 	}
74 
75 	if (!init->recv_cq || !init->send_cq) {
76 		pr_debug("missing cq\n");
77 		goto err1;
78 	}
79 
80 	if (rxe_qp_chk_cap(rxe, cap, !!init->srq))
81 		goto err1;
82 
83 	if (init->qp_type == IB_QPT_GSI) {
84 		if (!rdma_is_port_valid(&rxe->ib_dev, port_num)) {
85 			pr_debug("invalid port = %d\n", port_num);
86 			goto err1;
87 		}
88 
89 		port = &rxe->port;
90 
91 		if (init->qp_type == IB_QPT_GSI && port->qp_gsi_index) {
92 			pr_debug("GSI QP exists for port %d\n", port_num);
93 			goto err1;
94 		}
95 	}
96 
97 	return 0;
98 
99 err1:
100 	return -EINVAL;
101 }
102 
103 static int alloc_rd_atomic_resources(struct rxe_qp *qp, unsigned int n)
104 {
105 	qp->resp.res_head = 0;
106 	qp->resp.res_tail = 0;
107 	qp->resp.resources = kcalloc(n, sizeof(struct resp_res), GFP_KERNEL);
108 
109 	if (!qp->resp.resources)
110 		return -ENOMEM;
111 
112 	return 0;
113 }
114 
115 static void free_rd_atomic_resources(struct rxe_qp *qp)
116 {
117 	if (qp->resp.resources) {
118 		int i;
119 
120 		for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
121 			struct resp_res *res = &qp->resp.resources[i];
122 
123 			free_rd_atomic_resource(res);
124 		}
125 		kfree(qp->resp.resources);
126 		qp->resp.resources = NULL;
127 	}
128 }
129 
130 void free_rd_atomic_resource(struct resp_res *res)
131 {
132 	res->type = 0;
133 }
134 
135 static void cleanup_rd_atomic_resources(struct rxe_qp *qp)
136 {
137 	int i;
138 	struct resp_res *res;
139 
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);
144 		}
145 	}
146 }
147 
148 static void rxe_qp_init_misc(struct rxe_dev *rxe, struct rxe_qp *qp,
149 			     struct ib_qp_init_attr *init)
150 {
151 	struct rxe_port *port;
152 	u32 qpn;
153 
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);
157 
158 	qpn			= qp->elem.index;
159 	port			= &rxe->port;
160 
161 	switch (init->qp_type) {
162 	case IB_QPT_GSI:
163 		qp->ibqp.qp_num		= 1;
164 		port->qp_gsi_index	= qpn;
165 		qp->attr.port_num	= init->port_num;
166 		break;
167 
168 	default:
169 		qp->ibqp.qp_num		= qpn;
170 		break;
171 	}
172 
173 	spin_lock_init(&qp->state_lock);
174 
175 	spin_lock_init(&qp->req.task.state_lock);
176 	spin_lock_init(&qp->resp.task.state_lock);
177 	spin_lock_init(&qp->comp.task.state_lock);
178 
179 	spin_lock_init(&qp->sq.sq_lock);
180 	spin_lock_init(&qp->rq.producer_lock);
181 	spin_lock_init(&qp->rq.consumer_lock);
182 
183 	atomic_set(&qp->ssn, 0);
184 	atomic_set(&qp->skb_out, 0);
185 }
186 
187 static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,
188 			   struct ib_qp_init_attr *init, struct ib_udata *udata,
189 			   struct rxe_create_qp_resp __user *uresp)
190 {
191 	int err;
192 	int wqe_size;
193 	enum queue_type type;
194 
195 	err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk);
196 	if (err < 0)
197 		return err;
198 	qp->sk->sk->sk_user_data = qp;
199 
200 	/* pick a source UDP port number for this QP based on
201 	 * the source QPN. this spreads traffic for different QPs
202 	 * across different NIC RX queues (while using a single
203 	 * flow for a given QP to maintain packet order).
204 	 * the port number must be in the Dynamic Ports range
205 	 * (0xc000 - 0xffff).
206 	 */
207 	qp->src_port = RXE_ROCE_V2_SPORT + (hash_32(qp_num(qp), 14) & 0x3fff);
208 	qp->sq.max_wr		= init->cap.max_send_wr;
209 
210 	/* These caps are limited by rxe_qp_chk_cap() done by the caller */
211 	wqe_size = max_t(int, init->cap.max_send_sge * sizeof(struct ib_sge),
212 			 init->cap.max_inline_data);
213 	qp->sq.max_sge = init->cap.max_send_sge =
214 		wqe_size / sizeof(struct ib_sge);
215 	qp->sq.max_inline = init->cap.max_inline_data = wqe_size;
216 	wqe_size += sizeof(struct rxe_send_wqe);
217 
218 	type = QUEUE_TYPE_FROM_CLIENT;
219 	qp->sq.queue = rxe_queue_init(rxe, &qp->sq.max_wr,
220 				wqe_size, type);
221 	if (!qp->sq.queue)
222 		return -ENOMEM;
223 
224 	err = do_mmap_info(rxe, uresp ? &uresp->sq_mi : NULL, udata,
225 			   qp->sq.queue->buf, qp->sq.queue->buf_size,
226 			   &qp->sq.queue->ip);
227 
228 	if (err) {
229 		vfree(qp->sq.queue->buf);
230 		kfree(qp->sq.queue);
231 		qp->sq.queue = NULL;
232 		return err;
233 	}
234 
235 	qp->req.wqe_index = queue_get_producer(qp->sq.queue,
236 					       QUEUE_TYPE_FROM_CLIENT);
237 
238 	qp->req.state		= QP_STATE_RESET;
239 	qp->comp.state		= QP_STATE_RESET;
240 	qp->req.opcode		= -1;
241 	qp->comp.opcode		= -1;
242 
243 	skb_queue_head_init(&qp->req_pkts);
244 
245 	rxe_init_task(&qp->req.task, qp,
246 		      rxe_requester, "req");
247 	rxe_init_task(&qp->comp.task, qp,
248 		      rxe_completer, "comp");
249 
250 	qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */
251 	if (init->qp_type == IB_QPT_RC) {
252 		timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0);
253 		timer_setup(&qp->retrans_timer, retransmit_timer, 0);
254 	}
255 	return 0;
256 }
257 
258 static int rxe_qp_init_resp(struct rxe_dev *rxe, struct rxe_qp *qp,
259 			    struct ib_qp_init_attr *init,
260 			    struct ib_udata *udata,
261 			    struct rxe_create_qp_resp __user *uresp)
262 {
263 	int err;
264 	int wqe_size;
265 	enum queue_type type;
266 
267 	if (!qp->srq) {
268 		qp->rq.max_wr		= init->cap.max_recv_wr;
269 		qp->rq.max_sge		= init->cap.max_recv_sge;
270 
271 		wqe_size = rcv_wqe_size(qp->rq.max_sge);
272 
273 		pr_debug("qp#%d max_wr = %d, max_sge = %d, wqe_size = %d\n",
274 			 qp_num(qp), qp->rq.max_wr, qp->rq.max_sge, wqe_size);
275 
276 		type = QUEUE_TYPE_FROM_CLIENT;
277 		qp->rq.queue = rxe_queue_init(rxe, &qp->rq.max_wr,
278 					wqe_size, type);
279 		if (!qp->rq.queue)
280 			return -ENOMEM;
281 
282 		err = do_mmap_info(rxe, uresp ? &uresp->rq_mi : NULL, udata,
283 				   qp->rq.queue->buf, qp->rq.queue->buf_size,
284 				   &qp->rq.queue->ip);
285 		if (err) {
286 			vfree(qp->rq.queue->buf);
287 			kfree(qp->rq.queue);
288 			qp->rq.queue = NULL;
289 			return err;
290 		}
291 	}
292 
293 	skb_queue_head_init(&qp->resp_pkts);
294 
295 	rxe_init_task(&qp->resp.task, qp,
296 		      rxe_responder, "resp");
297 
298 	qp->resp.opcode		= OPCODE_NONE;
299 	qp->resp.msn		= 0;
300 	qp->resp.state		= QP_STATE_RESET;
301 
302 	return 0;
303 }
304 
305 /* called by the create qp verb */
306 int rxe_qp_from_init(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_pd *pd,
307 		     struct ib_qp_init_attr *init,
308 		     struct rxe_create_qp_resp __user *uresp,
309 		     struct ib_pd *ibpd,
310 		     struct ib_udata *udata)
311 {
312 	int err;
313 	struct rxe_cq *rcq = to_rcq(init->recv_cq);
314 	struct rxe_cq *scq = to_rcq(init->send_cq);
315 	struct rxe_srq *srq = init->srq ? to_rsrq(init->srq) : NULL;
316 
317 	rxe_get(pd);
318 	rxe_get(rcq);
319 	rxe_get(scq);
320 	if (srq)
321 		rxe_get(srq);
322 
323 	qp->pd			= pd;
324 	qp->rcq			= rcq;
325 	qp->scq			= scq;
326 	qp->srq			= srq;
327 
328 	atomic_inc(&rcq->num_wq);
329 	atomic_inc(&scq->num_wq);
330 
331 	rxe_qp_init_misc(rxe, qp, init);
332 
333 	err = rxe_qp_init_req(rxe, qp, init, udata, uresp);
334 	if (err)
335 		goto err1;
336 
337 	err = rxe_qp_init_resp(rxe, qp, init, udata, uresp);
338 	if (err)
339 		goto err2;
340 
341 	qp->attr.qp_state = IB_QPS_RESET;
342 	qp->valid = 1;
343 
344 	return 0;
345 
346 err2:
347 	rxe_queue_cleanup(qp->sq.queue);
348 	qp->sq.queue = NULL;
349 err1:
350 	atomic_dec(&rcq->num_wq);
351 	atomic_dec(&scq->num_wq);
352 
353 	qp->pd = NULL;
354 	qp->rcq = NULL;
355 	qp->scq = NULL;
356 	qp->srq = NULL;
357 
358 	if (srq)
359 		rxe_put(srq);
360 	rxe_put(scq);
361 	rxe_put(rcq);
362 	rxe_put(pd);
363 
364 	return err;
365 }
366 
367 /* called by the query qp verb */
368 int rxe_qp_to_init(struct rxe_qp *qp, struct ib_qp_init_attr *init)
369 {
370 	init->event_handler		= qp->ibqp.event_handler;
371 	init->qp_context		= qp->ibqp.qp_context;
372 	init->send_cq			= qp->ibqp.send_cq;
373 	init->recv_cq			= qp->ibqp.recv_cq;
374 	init->srq			= qp->ibqp.srq;
375 
376 	init->cap.max_send_wr		= qp->sq.max_wr;
377 	init->cap.max_send_sge		= qp->sq.max_sge;
378 	init->cap.max_inline_data	= qp->sq.max_inline;
379 
380 	if (!qp->srq) {
381 		init->cap.max_recv_wr		= qp->rq.max_wr;
382 		init->cap.max_recv_sge		= qp->rq.max_sge;
383 	}
384 
385 	init->sq_sig_type		= qp->sq_sig_type;
386 
387 	init->qp_type			= qp->ibqp.qp_type;
388 	init->port_num			= 1;
389 
390 	return 0;
391 }
392 
393 /* called by the modify qp verb, this routine checks all the parameters before
394  * making any changes
395  */
396 int rxe_qp_chk_attr(struct rxe_dev *rxe, struct rxe_qp *qp,
397 		    struct ib_qp_attr *attr, int mask)
398 {
399 	enum ib_qp_state cur_state = (mask & IB_QP_CUR_STATE) ?
400 					attr->cur_qp_state : qp->attr.qp_state;
401 	enum ib_qp_state new_state = (mask & IB_QP_STATE) ?
402 					attr->qp_state : cur_state;
403 
404 	if (!ib_modify_qp_is_ok(cur_state, new_state, qp_type(qp), mask)) {
405 		pr_debug("invalid mask or state for qp\n");
406 		goto err1;
407 	}
408 
409 	if (mask & IB_QP_STATE) {
410 		if (cur_state == IB_QPS_SQD) {
411 			if (qp->req.state == QP_STATE_DRAIN &&
412 			    new_state != IB_QPS_ERR)
413 				goto err1;
414 		}
415 	}
416 
417 	if (mask & IB_QP_PORT) {
418 		if (!rdma_is_port_valid(&rxe->ib_dev, attr->port_num)) {
419 			pr_debug("invalid port %d\n", attr->port_num);
420 			goto err1;
421 		}
422 	}
423 
424 	if (mask & IB_QP_CAP && rxe_qp_chk_cap(rxe, &attr->cap, !!qp->srq))
425 		goto err1;
426 
427 	if (mask & IB_QP_AV && rxe_av_chk_attr(rxe, &attr->ah_attr))
428 		goto err1;
429 
430 	if (mask & IB_QP_ALT_PATH) {
431 		if (rxe_av_chk_attr(rxe, &attr->alt_ah_attr))
432 			goto err1;
433 		if (!rdma_is_port_valid(&rxe->ib_dev, attr->alt_port_num))  {
434 			pr_debug("invalid alt port %d\n", attr->alt_port_num);
435 			goto err1;
436 		}
437 		if (attr->alt_timeout > 31) {
438 			pr_debug("invalid QP alt timeout %d > 31\n",
439 				 attr->alt_timeout);
440 			goto err1;
441 		}
442 	}
443 
444 	if (mask & IB_QP_PATH_MTU) {
445 		struct rxe_port *port = &rxe->port;
446 
447 		enum ib_mtu max_mtu = port->attr.max_mtu;
448 		enum ib_mtu mtu = attr->path_mtu;
449 
450 		if (mtu > max_mtu) {
451 			pr_debug("invalid mtu (%d) > (%d)\n",
452 				 ib_mtu_enum_to_int(mtu),
453 				 ib_mtu_enum_to_int(max_mtu));
454 			goto err1;
455 		}
456 	}
457 
458 	if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
459 		if (attr->max_rd_atomic > rxe->attr.max_qp_rd_atom) {
460 			pr_debug("invalid max_rd_atomic %d > %d\n",
461 				 attr->max_rd_atomic,
462 				 rxe->attr.max_qp_rd_atom);
463 			goto err1;
464 		}
465 	}
466 
467 	if (mask & IB_QP_TIMEOUT) {
468 		if (attr->timeout > 31) {
469 			pr_debug("invalid QP timeout %d > 31\n", attr->timeout);
470 			goto err1;
471 		}
472 	}
473 
474 	return 0;
475 
476 err1:
477 	return -EINVAL;
478 }
479 
480 /* move the qp to the reset state */
481 static void rxe_qp_reset(struct rxe_qp *qp)
482 {
483 	/* stop tasks from running */
484 	rxe_disable_task(&qp->resp.task);
485 
486 	/* stop request/comp */
487 	if (qp->sq.queue) {
488 		if (qp_type(qp) == IB_QPT_RC)
489 			rxe_disable_task(&qp->comp.task);
490 		rxe_disable_task(&qp->req.task);
491 	}
492 
493 	/* move qp to the reset state */
494 	qp->req.state = QP_STATE_RESET;
495 	qp->comp.state = QP_STATE_RESET;
496 	qp->resp.state = QP_STATE_RESET;
497 
498 	/* let state machines reset themselves drain work and packet queues
499 	 * etc.
500 	 */
501 	__rxe_do_task(&qp->resp.task);
502 
503 	if (qp->sq.queue) {
504 		__rxe_do_task(&qp->comp.task);
505 		__rxe_do_task(&qp->req.task);
506 		rxe_queue_reset(qp->sq.queue);
507 	}
508 
509 	/* cleanup attributes */
510 	atomic_set(&qp->ssn, 0);
511 	qp->req.opcode = -1;
512 	qp->req.need_retry = 0;
513 	qp->req.wait_for_rnr_timer = 0;
514 	qp->req.noack_pkts = 0;
515 	qp->resp.msn = 0;
516 	qp->resp.opcode = -1;
517 	qp->resp.drop_msg = 0;
518 	qp->resp.goto_error = 0;
519 	qp->resp.sent_psn_nak = 0;
520 
521 	if (qp->resp.mr) {
522 		rxe_put(qp->resp.mr);
523 		qp->resp.mr = NULL;
524 	}
525 
526 	cleanup_rd_atomic_resources(qp);
527 
528 	/* reenable tasks */
529 	rxe_enable_task(&qp->resp.task);
530 
531 	if (qp->sq.queue) {
532 		if (qp_type(qp) == IB_QPT_RC)
533 			rxe_enable_task(&qp->comp.task);
534 
535 		rxe_enable_task(&qp->req.task);
536 	}
537 }
538 
539 /* drain the send queue */
540 static void rxe_qp_drain(struct rxe_qp *qp)
541 {
542 	if (qp->sq.queue) {
543 		if (qp->req.state != QP_STATE_DRAINED) {
544 			qp->req.state = QP_STATE_DRAIN;
545 			if (qp_type(qp) == IB_QPT_RC)
546 				rxe_run_task(&qp->comp.task, 1);
547 			else
548 				__rxe_do_task(&qp->comp.task);
549 			rxe_run_task(&qp->req.task, 1);
550 		}
551 	}
552 }
553 
554 /* move the qp to the error state */
555 void rxe_qp_error(struct rxe_qp *qp)
556 {
557 	qp->req.state = QP_STATE_ERROR;
558 	qp->resp.state = QP_STATE_ERROR;
559 	qp->comp.state = QP_STATE_ERROR;
560 	qp->attr.qp_state = IB_QPS_ERR;
561 
562 	/* drain work and packet queues */
563 	rxe_run_task(&qp->resp.task, 1);
564 
565 	if (qp_type(qp) == IB_QPT_RC)
566 		rxe_run_task(&qp->comp.task, 1);
567 	else
568 		__rxe_do_task(&qp->comp.task);
569 	rxe_run_task(&qp->req.task, 1);
570 }
571 
572 /* called by the modify qp verb */
573 int rxe_qp_from_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask,
574 		     struct ib_udata *udata)
575 {
576 	int err;
577 
578 	if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
579 		int max_rd_atomic = attr->max_rd_atomic ?
580 			roundup_pow_of_two(attr->max_rd_atomic) : 0;
581 
582 		qp->attr.max_rd_atomic = max_rd_atomic;
583 		atomic_set(&qp->req.rd_atomic, max_rd_atomic);
584 	}
585 
586 	if (mask & IB_QP_MAX_DEST_RD_ATOMIC) {
587 		int max_dest_rd_atomic = attr->max_dest_rd_atomic ?
588 			roundup_pow_of_two(attr->max_dest_rd_atomic) : 0;
589 
590 		qp->attr.max_dest_rd_atomic = max_dest_rd_atomic;
591 
592 		free_rd_atomic_resources(qp);
593 
594 		err = alloc_rd_atomic_resources(qp, max_dest_rd_atomic);
595 		if (err)
596 			return err;
597 	}
598 
599 	if (mask & IB_QP_CUR_STATE)
600 		qp->attr.cur_qp_state = attr->qp_state;
601 
602 	if (mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
603 		qp->attr.en_sqd_async_notify = attr->en_sqd_async_notify;
604 
605 	if (mask & IB_QP_ACCESS_FLAGS)
606 		qp->attr.qp_access_flags = attr->qp_access_flags;
607 
608 	if (mask & IB_QP_PKEY_INDEX)
609 		qp->attr.pkey_index = attr->pkey_index;
610 
611 	if (mask & IB_QP_PORT)
612 		qp->attr.port_num = attr->port_num;
613 
614 	if (mask & IB_QP_QKEY)
615 		qp->attr.qkey = attr->qkey;
616 
617 	if (mask & IB_QP_AV)
618 		rxe_init_av(&attr->ah_attr, &qp->pri_av);
619 
620 	if (mask & IB_QP_ALT_PATH) {
621 		rxe_init_av(&attr->alt_ah_attr, &qp->alt_av);
622 		qp->attr.alt_port_num = attr->alt_port_num;
623 		qp->attr.alt_pkey_index = attr->alt_pkey_index;
624 		qp->attr.alt_timeout = attr->alt_timeout;
625 	}
626 
627 	if (mask & IB_QP_PATH_MTU) {
628 		qp->attr.path_mtu = attr->path_mtu;
629 		qp->mtu = ib_mtu_enum_to_int(attr->path_mtu);
630 	}
631 
632 	if (mask & IB_QP_TIMEOUT) {
633 		qp->attr.timeout = attr->timeout;
634 		if (attr->timeout == 0) {
635 			qp->qp_timeout_jiffies = 0;
636 		} else {
637 			/* According to the spec, timeout = 4.096 * 2 ^ attr->timeout [us] */
638 			int j = nsecs_to_jiffies(4096ULL << attr->timeout);
639 
640 			qp->qp_timeout_jiffies = j ? j : 1;
641 		}
642 	}
643 
644 	if (mask & IB_QP_RETRY_CNT) {
645 		qp->attr.retry_cnt = attr->retry_cnt;
646 		qp->comp.retry_cnt = attr->retry_cnt;
647 		pr_debug("qp#%d set retry count = %d\n", qp_num(qp),
648 			 attr->retry_cnt);
649 	}
650 
651 	if (mask & IB_QP_RNR_RETRY) {
652 		qp->attr.rnr_retry = attr->rnr_retry;
653 		qp->comp.rnr_retry = attr->rnr_retry;
654 		pr_debug("qp#%d set rnr retry count = %d\n", qp_num(qp),
655 			 attr->rnr_retry);
656 	}
657 
658 	if (mask & IB_QP_RQ_PSN) {
659 		qp->attr.rq_psn = (attr->rq_psn & BTH_PSN_MASK);
660 		qp->resp.psn = qp->attr.rq_psn;
661 		pr_debug("qp#%d set resp psn = 0x%x\n", qp_num(qp),
662 			 qp->resp.psn);
663 	}
664 
665 	if (mask & IB_QP_MIN_RNR_TIMER) {
666 		qp->attr.min_rnr_timer = attr->min_rnr_timer;
667 		pr_debug("qp#%d set min rnr timer = 0x%x\n", qp_num(qp),
668 			 attr->min_rnr_timer);
669 	}
670 
671 	if (mask & IB_QP_SQ_PSN) {
672 		qp->attr.sq_psn = (attr->sq_psn & BTH_PSN_MASK);
673 		qp->req.psn = qp->attr.sq_psn;
674 		qp->comp.psn = qp->attr.sq_psn;
675 		pr_debug("qp#%d set req psn = 0x%x\n", qp_num(qp), qp->req.psn);
676 	}
677 
678 	if (mask & IB_QP_PATH_MIG_STATE)
679 		qp->attr.path_mig_state = attr->path_mig_state;
680 
681 	if (mask & IB_QP_DEST_QPN)
682 		qp->attr.dest_qp_num = attr->dest_qp_num;
683 
684 	if (mask & IB_QP_STATE) {
685 		qp->attr.qp_state = attr->qp_state;
686 
687 		switch (attr->qp_state) {
688 		case IB_QPS_RESET:
689 			pr_debug("qp#%d state -> RESET\n", qp_num(qp));
690 			rxe_qp_reset(qp);
691 			break;
692 
693 		case IB_QPS_INIT:
694 			pr_debug("qp#%d state -> INIT\n", qp_num(qp));
695 			qp->req.state = QP_STATE_INIT;
696 			qp->resp.state = QP_STATE_INIT;
697 			qp->comp.state = QP_STATE_INIT;
698 			break;
699 
700 		case IB_QPS_RTR:
701 			pr_debug("qp#%d state -> RTR\n", qp_num(qp));
702 			qp->resp.state = QP_STATE_READY;
703 			break;
704 
705 		case IB_QPS_RTS:
706 			pr_debug("qp#%d state -> RTS\n", qp_num(qp));
707 			qp->req.state = QP_STATE_READY;
708 			qp->comp.state = QP_STATE_READY;
709 			break;
710 
711 		case IB_QPS_SQD:
712 			pr_debug("qp#%d state -> SQD\n", qp_num(qp));
713 			rxe_qp_drain(qp);
714 			break;
715 
716 		case IB_QPS_SQE:
717 			pr_warn("qp#%d state -> SQE !!?\n", qp_num(qp));
718 			/* Not possible from modify_qp. */
719 			break;
720 
721 		case IB_QPS_ERR:
722 			pr_debug("qp#%d state -> ERR\n", qp_num(qp));
723 			rxe_qp_error(qp);
724 			break;
725 		}
726 	}
727 
728 	return 0;
729 }
730 
731 /* called by the query qp verb */
732 int rxe_qp_to_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask)
733 {
734 	*attr = qp->attr;
735 
736 	attr->rq_psn				= qp->resp.psn;
737 	attr->sq_psn				= qp->req.psn;
738 
739 	attr->cap.max_send_wr			= qp->sq.max_wr;
740 	attr->cap.max_send_sge			= qp->sq.max_sge;
741 	attr->cap.max_inline_data		= qp->sq.max_inline;
742 
743 	if (!qp->srq) {
744 		attr->cap.max_recv_wr		= qp->rq.max_wr;
745 		attr->cap.max_recv_sge		= qp->rq.max_sge;
746 	}
747 
748 	rxe_av_to_attr(&qp->pri_av, &attr->ah_attr);
749 	rxe_av_to_attr(&qp->alt_av, &attr->alt_ah_attr);
750 
751 	if (qp->req.state == QP_STATE_DRAIN) {
752 		attr->sq_draining = 1;
753 		/* applications that get this state
754 		 * typically spin on it. yield the
755 		 * processor
756 		 */
757 		cond_resched();
758 	} else {
759 		attr->sq_draining = 0;
760 	}
761 
762 	pr_debug("attr->sq_draining = %d\n", attr->sq_draining);
763 
764 	return 0;
765 }
766 
767 int rxe_qp_chk_destroy(struct rxe_qp *qp)
768 {
769 	/* See IBA o10-2.2.3
770 	 * An attempt to destroy a QP while attached to a mcast group
771 	 * will fail immediately.
772 	 */
773 	if (atomic_read(&qp->mcg_num)) {
774 		pr_debug("Attempt to destroy QP while attached to multicast group\n");
775 		return -EBUSY;
776 	}
777 
778 	return 0;
779 }
780 
781 /* called when the last reference to the qp is dropped */
782 static void rxe_qp_do_cleanup(struct work_struct *work)
783 {
784 	struct rxe_qp *qp = container_of(work, typeof(*qp), cleanup_work.work);
785 
786 	qp->valid = 0;
787 	qp->qp_timeout_jiffies = 0;
788 	rxe_cleanup_task(&qp->resp.task);
789 
790 	if (qp_type(qp) == IB_QPT_RC) {
791 		del_timer_sync(&qp->retrans_timer);
792 		del_timer_sync(&qp->rnr_nak_timer);
793 	}
794 
795 	rxe_cleanup_task(&qp->req.task);
796 	rxe_cleanup_task(&qp->comp.task);
797 
798 	/* flush out any receive wr's or pending requests */
799 	if (qp->req.task.func)
800 		__rxe_do_task(&qp->req.task);
801 
802 	if (qp->sq.queue) {
803 		__rxe_do_task(&qp->comp.task);
804 		__rxe_do_task(&qp->req.task);
805 	}
806 
807 	if (qp->sq.queue)
808 		rxe_queue_cleanup(qp->sq.queue);
809 
810 	if (qp->srq)
811 		rxe_put(qp->srq);
812 
813 	if (qp->rq.queue)
814 		rxe_queue_cleanup(qp->rq.queue);
815 
816 	if (qp->scq) {
817 		atomic_dec(&qp->scq->num_wq);
818 		rxe_put(qp->scq);
819 	}
820 
821 	if (qp->rcq) {
822 		atomic_dec(&qp->rcq->num_wq);
823 		rxe_put(qp->rcq);
824 	}
825 
826 	if (qp->pd)
827 		rxe_put(qp->pd);
828 
829 	if (qp->resp.mr)
830 		rxe_put(qp->resp.mr);
831 
832 	if (qp_type(qp) == IB_QPT_RC)
833 		sk_dst_reset(qp->sk->sk);
834 
835 	free_rd_atomic_resources(qp);
836 
837 	if (qp->sk) {
838 		kernel_sock_shutdown(qp->sk, SHUT_RDWR);
839 		sock_release(qp->sk);
840 	}
841 }
842 
843 /* called when the last reference to the qp is dropped */
844 void rxe_qp_cleanup(struct rxe_pool_elem *elem)
845 {
846 	struct rxe_qp *qp = container_of(elem, typeof(*qp), elem);
847 
848 	execute_in_process_context(rxe_qp_do_cleanup, &qp->cleanup_work);
849 }
850