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