1 /* QLogic qedr NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32 #include <net/ip.h>
33 #include <net/ipv6.h>
34 #include <net/udp.h>
35 #include <net/addrconf.h>
36 #include <net/route.h>
37 #include <net/ip6_route.h>
38 #include <net/flow.h>
39 #include "qedr.h"
40 #include "qedr_iw_cm.h"
41 
42 static inline void
43 qedr_fill_sockaddr4(const struct qed_iwarp_cm_info *cm_info,
44 		    struct iw_cm_event *event)
45 {
46 	struct sockaddr_in *laddr = (struct sockaddr_in *)&event->local_addr;
47 	struct sockaddr_in *raddr = (struct sockaddr_in *)&event->remote_addr;
48 
49 	laddr->sin_family = AF_INET;
50 	raddr->sin_family = AF_INET;
51 
52 	laddr->sin_port = htons(cm_info->local_port);
53 	raddr->sin_port = htons(cm_info->remote_port);
54 
55 	laddr->sin_addr.s_addr = htonl(cm_info->local_ip[0]);
56 	raddr->sin_addr.s_addr = htonl(cm_info->remote_ip[0]);
57 }
58 
59 static inline void
60 qedr_fill_sockaddr6(const struct qed_iwarp_cm_info *cm_info,
61 		    struct iw_cm_event *event)
62 {
63 	struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)&event->local_addr;
64 	struct sockaddr_in6 *raddr6 =
65 	    (struct sockaddr_in6 *)&event->remote_addr;
66 	int i;
67 
68 	laddr6->sin6_family = AF_INET6;
69 	raddr6->sin6_family = AF_INET6;
70 
71 	laddr6->sin6_port = htons(cm_info->local_port);
72 	raddr6->sin6_port = htons(cm_info->remote_port);
73 
74 	for (i = 0; i < 4; i++) {
75 		laddr6->sin6_addr.in6_u.u6_addr32[i] =
76 		    htonl(cm_info->local_ip[i]);
77 		raddr6->sin6_addr.in6_u.u6_addr32[i] =
78 		    htonl(cm_info->remote_ip[i]);
79 	}
80 }
81 
82 static void
83 qedr_iw_mpa_request(void *context, struct qed_iwarp_cm_event_params *params)
84 {
85 	struct qedr_iw_listener *listener = (struct qedr_iw_listener *)context;
86 	struct qedr_dev *dev = listener->dev;
87 	struct iw_cm_event event;
88 	struct qedr_iw_ep *ep;
89 
90 	ep = kzalloc(sizeof(*ep), GFP_ATOMIC);
91 	if (!ep)
92 		return;
93 
94 	ep->dev = dev;
95 	ep->qed_context = params->ep_context;
96 
97 	memset(&event, 0, sizeof(event));
98 	event.event = IW_CM_EVENT_CONNECT_REQUEST;
99 	event.status = params->status;
100 
101 	if (!IS_ENABLED(CONFIG_IPV6) ||
102 	    params->cm_info->ip_version == QED_TCP_IPV4)
103 		qedr_fill_sockaddr4(params->cm_info, &event);
104 	else
105 		qedr_fill_sockaddr6(params->cm_info, &event);
106 
107 	event.provider_data = (void *)ep;
108 	event.private_data = (void *)params->cm_info->private_data;
109 	event.private_data_len = (u8)params->cm_info->private_data_len;
110 	event.ord = params->cm_info->ord;
111 	event.ird = params->cm_info->ird;
112 
113 	listener->cm_id->event_handler(listener->cm_id, &event);
114 }
115 
116 static void
117 qedr_iw_issue_event(void *context,
118 		    struct qed_iwarp_cm_event_params *params,
119 		    enum iw_cm_event_type event_type)
120 {
121 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
122 	struct iw_cm_event event;
123 
124 	memset(&event, 0, sizeof(event));
125 	event.status = params->status;
126 	event.event = event_type;
127 
128 	if (params->cm_info) {
129 		event.ird = params->cm_info->ird;
130 		event.ord = params->cm_info->ord;
131 		event.private_data_len = params->cm_info->private_data_len;
132 		event.private_data = (void *)params->cm_info->private_data;
133 	}
134 
135 	if (ep->cm_id)
136 		ep->cm_id->event_handler(ep->cm_id, &event);
137 }
138 
139 static void
140 qedr_iw_close_event(void *context, struct qed_iwarp_cm_event_params *params)
141 {
142 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
143 
144 	if (ep->cm_id) {
145 		qedr_iw_issue_event(context, params, IW_CM_EVENT_CLOSE);
146 
147 		ep->cm_id->rem_ref(ep->cm_id);
148 		ep->cm_id = NULL;
149 	}
150 }
151 
152 static void
153 qedr_iw_qp_event(void *context,
154 		 struct qed_iwarp_cm_event_params *params,
155 		 enum ib_event_type ib_event, char *str)
156 {
157 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
158 	struct qedr_dev *dev = ep->dev;
159 	struct ib_qp *ibqp = &ep->qp->ibqp;
160 	struct ib_event event;
161 
162 	DP_NOTICE(dev, "QP error received: %s\n", str);
163 
164 	if (ibqp->event_handler) {
165 		event.event = ib_event;
166 		event.device = ibqp->device;
167 		event.element.qp = ibqp;
168 		ibqp->event_handler(&event, ibqp->qp_context);
169 	}
170 }
171 
172 struct qedr_discon_work {
173 	struct work_struct		work;
174 	struct qedr_iw_ep		*ep;
175 	enum qed_iwarp_event_type	event;
176 	int				status;
177 };
178 
179 static void qedr_iw_disconnect_worker(struct work_struct *work)
180 {
181 	struct qedr_discon_work *dwork =
182 	    container_of(work, struct qedr_discon_work, work);
183 	struct qed_rdma_modify_qp_in_params qp_params = { 0 };
184 	struct qedr_iw_ep *ep = dwork->ep;
185 	struct qedr_dev *dev = ep->dev;
186 	struct qedr_qp *qp = ep->qp;
187 	struct iw_cm_event event;
188 
189 	if (qp->destroyed) {
190 		kfree(dwork);
191 		qedr_iw_qp_rem_ref(&qp->ibqp);
192 		return;
193 	}
194 
195 	memset(&event, 0, sizeof(event));
196 	event.status = dwork->status;
197 	event.event = IW_CM_EVENT_DISCONNECT;
198 
199 	/* Success means graceful disconnect was requested. modifying
200 	 * to SQD is translated to graceful disconnect. O/w reset is sent
201 	 */
202 	if (dwork->status)
203 		qp_params.new_state = QED_ROCE_QP_STATE_ERR;
204 	else
205 		qp_params.new_state = QED_ROCE_QP_STATE_SQD;
206 
207 	kfree(dwork);
208 
209 	if (ep->cm_id)
210 		ep->cm_id->event_handler(ep->cm_id, &event);
211 
212 	SET_FIELD(qp_params.modify_flags,
213 		  QED_RDMA_MODIFY_QP_VALID_NEW_STATE, 1);
214 
215 	dev->ops->rdma_modify_qp(dev->rdma_ctx, qp->qed_qp, &qp_params);
216 
217 	qedr_iw_qp_rem_ref(&qp->ibqp);
218 }
219 
220 static void
221 qedr_iw_disconnect_event(void *context,
222 			 struct qed_iwarp_cm_event_params *params)
223 {
224 	struct qedr_discon_work *work;
225 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
226 	struct qedr_dev *dev = ep->dev;
227 	struct qedr_qp *qp = ep->qp;
228 
229 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
230 	if (!work)
231 		return;
232 
233 	qedr_iw_qp_add_ref(&qp->ibqp);
234 	work->ep = ep;
235 	work->event = params->event;
236 	work->status = params->status;
237 
238 	INIT_WORK(&work->work, qedr_iw_disconnect_worker);
239 	queue_work(dev->iwarp_wq, &work->work);
240 }
241 
242 static void
243 qedr_iw_passive_complete(void *context,
244 			 struct qed_iwarp_cm_event_params *params)
245 {
246 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
247 	struct qedr_dev *dev = ep->dev;
248 
249 	/* We will only reach the following state if MPA_REJECT was called on
250 	 * passive. In this case there will be no associated QP.
251 	 */
252 	if ((params->status == -ECONNREFUSED) && (!ep->qp)) {
253 		DP_DEBUG(dev, QEDR_MSG_IWARP,
254 			 "PASSIVE connection refused releasing ep...\n");
255 		kfree(ep);
256 		return;
257 	}
258 
259 	qedr_iw_issue_event(context, params, IW_CM_EVENT_ESTABLISHED);
260 
261 	if (params->status < 0)
262 		qedr_iw_close_event(context, params);
263 }
264 
265 static int
266 qedr_iw_mpa_reply(void *context, struct qed_iwarp_cm_event_params *params)
267 {
268 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
269 	struct qedr_dev *dev = ep->dev;
270 	struct qed_iwarp_send_rtr_in rtr_in;
271 
272 	rtr_in.ep_context = params->ep_context;
273 
274 	return dev->ops->iwarp_send_rtr(dev->rdma_ctx, &rtr_in);
275 }
276 
277 static int
278 qedr_iw_event_handler(void *context, struct qed_iwarp_cm_event_params *params)
279 {
280 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
281 	struct qedr_dev *dev = ep->dev;
282 
283 	switch (params->event) {
284 	case QED_IWARP_EVENT_MPA_REQUEST:
285 		qedr_iw_mpa_request(context, params);
286 		break;
287 	case QED_IWARP_EVENT_ACTIVE_MPA_REPLY:
288 		qedr_iw_mpa_reply(context, params);
289 		break;
290 	case QED_IWARP_EVENT_PASSIVE_COMPLETE:
291 		ep->during_connect = 0;
292 		qedr_iw_passive_complete(context, params);
293 		break;
294 
295 	case QED_IWARP_EVENT_ACTIVE_COMPLETE:
296 		ep->during_connect = 0;
297 		qedr_iw_issue_event(context,
298 				    params,
299 				    IW_CM_EVENT_CONNECT_REPLY);
300 		if (params->status < 0) {
301 			struct qedr_iw_ep *ep = (struct qedr_iw_ep *)context;
302 
303 			ep->cm_id->rem_ref(ep->cm_id);
304 			ep->cm_id = NULL;
305 		}
306 		break;
307 	case QED_IWARP_EVENT_DISCONNECT:
308 		qedr_iw_disconnect_event(context, params);
309 		break;
310 	case QED_IWARP_EVENT_CLOSE:
311 		ep->during_connect = 0;
312 		qedr_iw_close_event(context, params);
313 		break;
314 	case QED_IWARP_EVENT_RQ_EMPTY:
315 		qedr_iw_qp_event(context, params, IB_EVENT_QP_FATAL,
316 				 "QED_IWARP_EVENT_RQ_EMPTY");
317 		break;
318 	case QED_IWARP_EVENT_IRQ_FULL:
319 		qedr_iw_qp_event(context, params, IB_EVENT_QP_FATAL,
320 				 "QED_IWARP_EVENT_IRQ_FULL");
321 		break;
322 	case QED_IWARP_EVENT_LLP_TIMEOUT:
323 		qedr_iw_qp_event(context, params, IB_EVENT_QP_FATAL,
324 				 "QED_IWARP_EVENT_LLP_TIMEOUT");
325 		break;
326 	case QED_IWARP_EVENT_REMOTE_PROTECTION_ERROR:
327 		qedr_iw_qp_event(context, params, IB_EVENT_QP_ACCESS_ERR,
328 				 "QED_IWARP_EVENT_REMOTE_PROTECTION_ERROR");
329 		break;
330 	case QED_IWARP_EVENT_CQ_OVERFLOW:
331 		qedr_iw_qp_event(context, params, IB_EVENT_QP_FATAL,
332 				 "QED_IWARP_EVENT_CQ_OVERFLOW");
333 		break;
334 	case QED_IWARP_EVENT_QP_CATASTROPHIC:
335 		qedr_iw_qp_event(context, params, IB_EVENT_QP_FATAL,
336 				 "QED_IWARP_EVENT_QP_CATASTROPHIC");
337 		break;
338 	case QED_IWARP_EVENT_LOCAL_ACCESS_ERROR:
339 		qedr_iw_qp_event(context, params, IB_EVENT_QP_ACCESS_ERR,
340 				 "QED_IWARP_EVENT_LOCAL_ACCESS_ERROR");
341 		break;
342 	case QED_IWARP_EVENT_REMOTE_OPERATION_ERROR:
343 		qedr_iw_qp_event(context, params, IB_EVENT_QP_FATAL,
344 				 "QED_IWARP_EVENT_REMOTE_OPERATION_ERROR");
345 		break;
346 	case QED_IWARP_EVENT_TERMINATE_RECEIVED:
347 		DP_NOTICE(dev, "Got terminate message\n");
348 		break;
349 	default:
350 		DP_NOTICE(dev, "Unknown event received %d\n", params->event);
351 		break;
352 	};
353 	return 0;
354 }
355 
356 static u16 qedr_iw_get_vlan_ipv4(struct qedr_dev *dev, u32 *addr)
357 {
358 	struct net_device *ndev;
359 	u16 vlan_id = 0;
360 
361 	ndev = ip_dev_find(&init_net, htonl(addr[0]));
362 
363 	if (ndev) {
364 		vlan_id = rdma_vlan_dev_vlan_id(ndev);
365 		dev_put(ndev);
366 	}
367 	if (vlan_id == 0xffff)
368 		vlan_id = 0;
369 	return vlan_id;
370 }
371 
372 static u16 qedr_iw_get_vlan_ipv6(u32 *addr)
373 {
374 	struct net_device *ndev = NULL;
375 	struct in6_addr laddr6;
376 	u16 vlan_id = 0;
377 	int i;
378 
379 	if (!IS_ENABLED(CONFIG_IPV6))
380 		return vlan_id;
381 
382 	for (i = 0; i < 4; i++)
383 		laddr6.in6_u.u6_addr32[i] = htonl(addr[i]);
384 
385 	rcu_read_lock();
386 	for_each_netdev_rcu(&init_net, ndev) {
387 		if (ipv6_chk_addr(&init_net, &laddr6, ndev, 1)) {
388 			vlan_id = rdma_vlan_dev_vlan_id(ndev);
389 			break;
390 		}
391 	}
392 
393 	rcu_read_unlock();
394 	if (vlan_id == 0xffff)
395 		vlan_id = 0;
396 
397 	return vlan_id;
398 }
399 
400 static int
401 qedr_addr4_resolve(struct qedr_dev *dev,
402 		   struct sockaddr_in *src_in,
403 		   struct sockaddr_in *dst_in, u8 *dst_mac)
404 {
405 	__be32 src_ip = src_in->sin_addr.s_addr;
406 	__be32 dst_ip = dst_in->sin_addr.s_addr;
407 	struct neighbour *neigh = NULL;
408 	struct rtable *rt = NULL;
409 	int rc = 0;
410 
411 	rt = ip_route_output(&init_net, dst_ip, src_ip, 0, 0);
412 	if (IS_ERR(rt)) {
413 		DP_ERR(dev, "ip_route_output returned error\n");
414 		return -EINVAL;
415 	}
416 
417 	neigh = dst_neigh_lookup(&rt->dst, &dst_ip);
418 
419 	if (neigh) {
420 		rcu_read_lock();
421 		if (neigh->nud_state & NUD_VALID) {
422 			ether_addr_copy(dst_mac, neigh->ha);
423 			DP_DEBUG(dev, QEDR_MSG_QP, "mac_addr=[%pM]\n", dst_mac);
424 		} else {
425 			neigh_event_send(neigh, NULL);
426 		}
427 		rcu_read_unlock();
428 		neigh_release(neigh);
429 	}
430 
431 	ip_rt_put(rt);
432 
433 	return rc;
434 }
435 
436 static int
437 qedr_addr6_resolve(struct qedr_dev *dev,
438 		   struct sockaddr_in6 *src_in,
439 		   struct sockaddr_in6 *dst_in, u8 *dst_mac)
440 {
441 	struct neighbour *neigh = NULL;
442 	struct dst_entry *dst;
443 	struct flowi6 fl6;
444 	int rc = 0;
445 
446 	memset(&fl6, 0, sizeof(fl6));
447 	fl6.daddr = dst_in->sin6_addr;
448 	fl6.saddr = src_in->sin6_addr;
449 
450 	dst = ip6_route_output(&init_net, NULL, &fl6);
451 
452 	if ((!dst) || dst->error) {
453 		if (dst) {
454 			dst_release(dst);
455 			DP_ERR(dev,
456 			       "ip6_route_output returned dst->error = %d\n",
457 			       dst->error);
458 		}
459 		return -EINVAL;
460 	}
461 	neigh = dst_neigh_lookup(dst, &dst_in);
462 
463 	if (neigh) {
464 		rcu_read_lock();
465 		if (neigh->nud_state & NUD_VALID) {
466 			ether_addr_copy(dst_mac, neigh->ha);
467 			DP_DEBUG(dev, QEDR_MSG_QP, "mac_addr=[%pM]\n", dst_mac);
468 		} else {
469 			neigh_event_send(neigh, NULL);
470 		}
471 		rcu_read_unlock();
472 		neigh_release(neigh);
473 	}
474 
475 	dst_release(dst);
476 
477 	return rc;
478 }
479 
480 int qedr_iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
481 {
482 	struct qedr_dev *dev = get_qedr_dev(cm_id->device);
483 	struct qed_iwarp_connect_out out_params;
484 	struct qed_iwarp_connect_in in_params;
485 	struct qed_iwarp_cm_info *cm_info;
486 	struct sockaddr_in6 *laddr6;
487 	struct sockaddr_in6 *raddr6;
488 	struct sockaddr_in *laddr;
489 	struct sockaddr_in *raddr;
490 	struct qedr_iw_ep *ep;
491 	struct qedr_qp *qp;
492 	int rc = 0;
493 	int i;
494 
495 	qp = idr_find(&dev->qpidr, conn_param->qpn);
496 
497 	laddr = (struct sockaddr_in *)&cm_id->local_addr;
498 	raddr = (struct sockaddr_in *)&cm_id->remote_addr;
499 	laddr6 = (struct sockaddr_in6 *)&cm_id->local_addr;
500 	raddr6 = (struct sockaddr_in6 *)&cm_id->remote_addr;
501 
502 	DP_DEBUG(dev, QEDR_MSG_IWARP,
503 		 "Connect source address: %pISpc, remote address: %pISpc\n",
504 		 &cm_id->local_addr, &cm_id->remote_addr);
505 
506 	if (!laddr->sin_port || !raddr->sin_port)
507 		return -EINVAL;
508 
509 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
510 	if (!ep)
511 		return -ENOMEM;
512 
513 	ep->dev = dev;
514 	ep->qp = qp;
515 	qp->ep = ep;
516 	cm_id->add_ref(cm_id);
517 	ep->cm_id = cm_id;
518 
519 	in_params.event_cb = qedr_iw_event_handler;
520 	in_params.cb_context = ep;
521 
522 	cm_info = &in_params.cm_info;
523 	memset(cm_info->local_ip, 0, sizeof(cm_info->local_ip));
524 	memset(cm_info->remote_ip, 0, sizeof(cm_info->remote_ip));
525 
526 	if (!IS_ENABLED(CONFIG_IPV6) ||
527 	    cm_id->remote_addr.ss_family == AF_INET) {
528 		cm_info->ip_version = QED_TCP_IPV4;
529 
530 		cm_info->remote_ip[0] = ntohl(raddr->sin_addr.s_addr);
531 		cm_info->local_ip[0] = ntohl(laddr->sin_addr.s_addr);
532 		cm_info->remote_port = ntohs(raddr->sin_port);
533 		cm_info->local_port = ntohs(laddr->sin_port);
534 		cm_info->vlan = qedr_iw_get_vlan_ipv4(dev, cm_info->local_ip);
535 
536 		rc = qedr_addr4_resolve(dev, laddr, raddr,
537 					(u8 *)in_params.remote_mac_addr);
538 
539 		in_params.mss = dev->iwarp_max_mtu -
540 		    (sizeof(struct iphdr) + sizeof(struct tcphdr));
541 
542 	} else {
543 		in_params.cm_info.ip_version = QED_TCP_IPV6;
544 
545 		for (i = 0; i < 4; i++) {
546 			cm_info->remote_ip[i] =
547 			    ntohl(raddr6->sin6_addr.in6_u.u6_addr32[i]);
548 			cm_info->local_ip[i] =
549 			    ntohl(laddr6->sin6_addr.in6_u.u6_addr32[i]);
550 		}
551 
552 		cm_info->local_port = ntohs(laddr6->sin6_port);
553 		cm_info->remote_port = ntohs(raddr6->sin6_port);
554 
555 		in_params.mss = dev->iwarp_max_mtu -
556 		    (sizeof(struct ipv6hdr) + sizeof(struct tcphdr));
557 
558 		cm_info->vlan = qedr_iw_get_vlan_ipv6(cm_info->local_ip);
559 
560 		rc = qedr_addr6_resolve(dev, laddr6, raddr6,
561 					(u8 *)in_params.remote_mac_addr);
562 	}
563 	if (rc)
564 		goto err;
565 
566 	DP_DEBUG(dev, QEDR_MSG_IWARP,
567 		 "ord = %d ird=%d private_data=%p private_data_len=%d rq_psn=%d\n",
568 		 conn_param->ord, conn_param->ird, conn_param->private_data,
569 		 conn_param->private_data_len, qp->rq_psn);
570 
571 	cm_info->ord = conn_param->ord;
572 	cm_info->ird = conn_param->ird;
573 	cm_info->private_data = conn_param->private_data;
574 	cm_info->private_data_len = conn_param->private_data_len;
575 	in_params.qp = qp->qed_qp;
576 	memcpy(in_params.local_mac_addr, dev->ndev->dev_addr, ETH_ALEN);
577 
578 	ep->during_connect = 1;
579 	rc = dev->ops->iwarp_connect(dev->rdma_ctx, &in_params, &out_params);
580 	if (rc)
581 		goto err;
582 
583 	return rc;
584 
585 err:
586 	cm_id->rem_ref(cm_id);
587 	kfree(ep);
588 	return rc;
589 }
590 
591 int qedr_iw_create_listen(struct iw_cm_id *cm_id, int backlog)
592 {
593 	struct qedr_dev *dev = get_qedr_dev(cm_id->device);
594 	struct qedr_iw_listener *listener;
595 	struct qed_iwarp_listen_in iparams;
596 	struct qed_iwarp_listen_out oparams;
597 	struct sockaddr_in *laddr;
598 	struct sockaddr_in6 *laddr6;
599 	int rc;
600 	int i;
601 
602 	laddr = (struct sockaddr_in *)&cm_id->local_addr;
603 	laddr6 = (struct sockaddr_in6 *)&cm_id->local_addr;
604 
605 	DP_DEBUG(dev, QEDR_MSG_IWARP,
606 		 "Create Listener address: %pISpc\n", &cm_id->local_addr);
607 
608 	listener = kzalloc(sizeof(*listener), GFP_KERNEL);
609 	if (!listener)
610 		return -ENOMEM;
611 
612 	listener->dev = dev;
613 	cm_id->add_ref(cm_id);
614 	listener->cm_id = cm_id;
615 	listener->backlog = backlog;
616 
617 	iparams.cb_context = listener;
618 	iparams.event_cb = qedr_iw_event_handler;
619 	iparams.max_backlog = backlog;
620 
621 	if (!IS_ENABLED(CONFIG_IPV6) ||
622 	    cm_id->local_addr.ss_family == AF_INET) {
623 		iparams.ip_version = QED_TCP_IPV4;
624 		memset(iparams.ip_addr, 0, sizeof(iparams.ip_addr));
625 
626 		iparams.ip_addr[0] = ntohl(laddr->sin_addr.s_addr);
627 		iparams.port = ntohs(laddr->sin_port);
628 		iparams.vlan = qedr_iw_get_vlan_ipv4(dev, iparams.ip_addr);
629 	} else {
630 		iparams.ip_version = QED_TCP_IPV6;
631 
632 		for (i = 0; i < 4; i++) {
633 			iparams.ip_addr[i] =
634 			    ntohl(laddr6->sin6_addr.in6_u.u6_addr32[i]);
635 		}
636 
637 		iparams.port = ntohs(laddr6->sin6_port);
638 
639 		iparams.vlan = qedr_iw_get_vlan_ipv6(iparams.ip_addr);
640 	}
641 	rc = dev->ops->iwarp_create_listen(dev->rdma_ctx, &iparams, &oparams);
642 	if (rc)
643 		goto err;
644 
645 	listener->qed_handle = oparams.handle;
646 	cm_id->provider_data = listener;
647 	return rc;
648 
649 err:
650 	cm_id->rem_ref(cm_id);
651 	kfree(listener);
652 	return rc;
653 }
654 
655 int qedr_iw_destroy_listen(struct iw_cm_id *cm_id)
656 {
657 	struct qedr_iw_listener *listener = cm_id->provider_data;
658 	struct qedr_dev *dev = get_qedr_dev(cm_id->device);
659 	int rc = 0;
660 
661 	if (listener->qed_handle)
662 		rc = dev->ops->iwarp_destroy_listen(dev->rdma_ctx,
663 						    listener->qed_handle);
664 
665 	cm_id->rem_ref(cm_id);
666 	return rc;
667 }
668 
669 int qedr_iw_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
670 {
671 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)cm_id->provider_data;
672 	struct qedr_dev *dev = ep->dev;
673 	struct qedr_qp *qp;
674 	struct qed_iwarp_accept_in params;
675 	int rc;
676 
677 	DP_DEBUG(dev, QEDR_MSG_IWARP, "Accept on qpid=%d\n", conn_param->qpn);
678 
679 	qp = idr_find(&dev->qpidr, conn_param->qpn);
680 	if (!qp) {
681 		DP_ERR(dev, "Invalid QP number %d\n", conn_param->qpn);
682 		return -EINVAL;
683 	}
684 
685 	ep->qp = qp;
686 	qp->ep = ep;
687 	cm_id->add_ref(cm_id);
688 	ep->cm_id = cm_id;
689 
690 	params.ep_context = ep->qed_context;
691 	params.cb_context = ep;
692 	params.qp = ep->qp->qed_qp;
693 	params.private_data = conn_param->private_data;
694 	params.private_data_len = conn_param->private_data_len;
695 	params.ird = conn_param->ird;
696 	params.ord = conn_param->ord;
697 
698 	ep->during_connect = 1;
699 	rc = dev->ops->iwarp_accept(dev->rdma_ctx, &params);
700 	if (rc)
701 		goto err;
702 
703 	return rc;
704 err:
705 	ep->during_connect = 0;
706 	cm_id->rem_ref(cm_id);
707 	return rc;
708 }
709 
710 int qedr_iw_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
711 {
712 	struct qedr_iw_ep *ep = (struct qedr_iw_ep *)cm_id->provider_data;
713 	struct qedr_dev *dev = ep->dev;
714 	struct qed_iwarp_reject_in params;
715 
716 	params.ep_context = ep->qed_context;
717 	params.cb_context = ep;
718 	params.private_data = pdata;
719 	params.private_data_len = pdata_len;
720 	ep->qp = NULL;
721 
722 	return dev->ops->iwarp_reject(dev->rdma_ctx, &params);
723 }
724 
725 void qedr_iw_qp_add_ref(struct ib_qp *ibqp)
726 {
727 	struct qedr_qp *qp = get_qedr_qp(ibqp);
728 
729 	atomic_inc(&qp->refcnt);
730 }
731 
732 void qedr_iw_qp_rem_ref(struct ib_qp *ibqp)
733 {
734 	struct qedr_qp *qp = get_qedr_qp(ibqp);
735 
736 	if (atomic_dec_and_test(&qp->refcnt)) {
737 		spin_lock_irq(&qp->dev->idr_lock);
738 		idr_remove(&qp->dev->qpidr, qp->qp_id);
739 		spin_unlock_irq(&qp->dev->idr_lock);
740 		kfree(qp);
741 	}
742 }
743 
744 struct ib_qp *qedr_iw_get_qp(struct ib_device *ibdev, int qpn)
745 {
746 	struct qedr_dev *dev = get_qedr_dev(ibdev);
747 
748 	return idr_find(&dev->qpidr, qpn);
749 }
750