xref: /linux/drivers/infiniband/hw/irdma/utils.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2015 - 2021 Intel Corporation */
3 #include "main.h"
4 
5 /**
6  * irdma_arp_table -manage arp table
7  * @rf: RDMA PCI function
8  * @ip_addr: ip address for device
9  * @ipv4: IPv4 flag
10  * @mac_addr: mac address ptr
11  * @action: modify, delete or add
12  */
13 int irdma_arp_table(struct irdma_pci_f *rf, u32 *ip_addr, bool ipv4,
14 		    const u8 *mac_addr, u32 action)
15 {
16 	unsigned long flags;
17 	int arp_index;
18 	u32 ip[4] = {};
19 
20 	if (ipv4)
21 		ip[0] = *ip_addr;
22 	else
23 		memcpy(ip, ip_addr, sizeof(ip));
24 
25 	spin_lock_irqsave(&rf->arp_lock, flags);
26 	for (arp_index = 0; (u32)arp_index < rf->arp_table_size; arp_index++) {
27 		if (!memcmp(rf->arp_table[arp_index].ip_addr, ip, sizeof(ip)))
28 			break;
29 	}
30 
31 	switch (action) {
32 	case IRDMA_ARP_ADD:
33 		if (arp_index != rf->arp_table_size) {
34 			arp_index = -1;
35 			break;
36 		}
37 
38 		arp_index = 0;
39 		if (irdma_alloc_rsrc(rf, rf->allocated_arps, rf->arp_table_size,
40 				     (u32 *)&arp_index, &rf->next_arp_index)) {
41 			arp_index = -1;
42 			break;
43 		}
44 
45 		memcpy(rf->arp_table[arp_index].ip_addr, ip,
46 		       sizeof(rf->arp_table[arp_index].ip_addr));
47 		ether_addr_copy(rf->arp_table[arp_index].mac_addr, mac_addr);
48 		break;
49 	case IRDMA_ARP_RESOLVE:
50 		if (arp_index == rf->arp_table_size)
51 			arp_index = -1;
52 		break;
53 	case IRDMA_ARP_DELETE:
54 		if (arp_index == rf->arp_table_size) {
55 			arp_index = -1;
56 			break;
57 		}
58 
59 		memset(rf->arp_table[arp_index].ip_addr, 0,
60 		       sizeof(rf->arp_table[arp_index].ip_addr));
61 		eth_zero_addr(rf->arp_table[arp_index].mac_addr);
62 		irdma_free_rsrc(rf, rf->allocated_arps, arp_index);
63 		break;
64 	default:
65 		arp_index = -1;
66 		break;
67 	}
68 
69 	spin_unlock_irqrestore(&rf->arp_lock, flags);
70 	return arp_index;
71 }
72 
73 /**
74  * irdma_add_arp - add a new arp entry if needed
75  * @rf: RDMA function
76  * @ip: IP address
77  * @ipv4: IPv4 flag
78  * @mac: MAC address
79  */
80 int irdma_add_arp(struct irdma_pci_f *rf, u32 *ip, bool ipv4, const u8 *mac)
81 {
82 	int arpidx;
83 
84 	arpidx = irdma_arp_table(rf, &ip[0], ipv4, NULL, IRDMA_ARP_RESOLVE);
85 	if (arpidx >= 0) {
86 		if (ether_addr_equal(rf->arp_table[arpidx].mac_addr, mac))
87 			return arpidx;
88 
89 		irdma_manage_arp_cache(rf, rf->arp_table[arpidx].mac_addr, ip,
90 				       ipv4, IRDMA_ARP_DELETE);
91 	}
92 
93 	irdma_manage_arp_cache(rf, mac, ip, ipv4, IRDMA_ARP_ADD);
94 
95 	return irdma_arp_table(rf, ip, ipv4, NULL, IRDMA_ARP_RESOLVE);
96 }
97 
98 /**
99  * wr32 - write 32 bits to hw register
100  * @hw: hardware information including registers
101  * @reg: register offset
102  * @val: value to write to register
103  */
104 inline void wr32(struct irdma_hw *hw, u32 reg, u32 val)
105 {
106 	writel(val, hw->hw_addr + reg);
107 }
108 
109 /**
110  * rd32 - read a 32 bit hw register
111  * @hw: hardware information including registers
112  * @reg: register offset
113  *
114  * Return value of register content
115  */
116 inline u32 rd32(struct irdma_hw *hw, u32 reg)
117 {
118 	return readl(hw->hw_addr + reg);
119 }
120 
121 /**
122  * rd64 - read a 64 bit hw register
123  * @hw: hardware information including registers
124  * @reg: register offset
125  *
126  * Return value of register content
127  */
128 inline u64 rd64(struct irdma_hw *hw, u32 reg)
129 {
130 	return readq(hw->hw_addr + reg);
131 }
132 
133 static void irdma_gid_change_event(struct ib_device *ibdev)
134 {
135 	struct ib_event ib_event;
136 
137 	ib_event.event = IB_EVENT_GID_CHANGE;
138 	ib_event.device = ibdev;
139 	ib_event.element.port_num = 1;
140 	ib_dispatch_event(&ib_event);
141 }
142 
143 /**
144  * irdma_inetaddr_event - system notifier for ipv4 addr events
145  * @notifier: not used
146  * @event: event for notifier
147  * @ptr: if address
148  */
149 int irdma_inetaddr_event(struct notifier_block *notifier, unsigned long event,
150 			 void *ptr)
151 {
152 	struct in_ifaddr *ifa = ptr;
153 	struct net_device *real_dev, *netdev = ifa->ifa_dev->dev;
154 	struct irdma_device *iwdev;
155 	struct ib_device *ibdev;
156 	u32 local_ipaddr;
157 
158 	real_dev = rdma_vlan_dev_real_dev(netdev);
159 	if (!real_dev)
160 		real_dev = netdev;
161 
162 	ibdev = ib_device_get_by_netdev(real_dev, RDMA_DRIVER_IRDMA);
163 	if (!ibdev)
164 		return NOTIFY_DONE;
165 
166 	iwdev = to_iwdev(ibdev);
167 	local_ipaddr = ntohl(ifa->ifa_address);
168 	ibdev_dbg(&iwdev->ibdev,
169 		  "DEV: netdev %p event %lu local_ip=%pI4 MAC=%pM\n", real_dev,
170 		  event, &local_ipaddr, real_dev->dev_addr);
171 	switch (event) {
172 	case NETDEV_DOWN:
173 		irdma_manage_arp_cache(iwdev->rf, real_dev->dev_addr,
174 				       &local_ipaddr, true, IRDMA_ARP_DELETE);
175 		irdma_if_notify(iwdev, real_dev, &local_ipaddr, true, false);
176 		irdma_gid_change_event(&iwdev->ibdev);
177 		break;
178 	case NETDEV_UP:
179 	case NETDEV_CHANGEADDR:
180 		irdma_add_arp(iwdev->rf, &local_ipaddr, true, real_dev->dev_addr);
181 		irdma_if_notify(iwdev, real_dev, &local_ipaddr, true, true);
182 		irdma_gid_change_event(&iwdev->ibdev);
183 		break;
184 	default:
185 		break;
186 	}
187 
188 	ib_device_put(ibdev);
189 
190 	return NOTIFY_DONE;
191 }
192 
193 /**
194  * irdma_inet6addr_event - system notifier for ipv6 addr events
195  * @notifier: not used
196  * @event: event for notifier
197  * @ptr: if address
198  */
199 int irdma_inet6addr_event(struct notifier_block *notifier, unsigned long event,
200 			  void *ptr)
201 {
202 	struct inet6_ifaddr *ifa = ptr;
203 	struct net_device *real_dev, *netdev = ifa->idev->dev;
204 	struct irdma_device *iwdev;
205 	struct ib_device *ibdev;
206 	u32 local_ipaddr6[4];
207 
208 	real_dev = rdma_vlan_dev_real_dev(netdev);
209 	if (!real_dev)
210 		real_dev = netdev;
211 
212 	ibdev = ib_device_get_by_netdev(real_dev, RDMA_DRIVER_IRDMA);
213 	if (!ibdev)
214 		return NOTIFY_DONE;
215 
216 	iwdev = to_iwdev(ibdev);
217 	irdma_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
218 	ibdev_dbg(&iwdev->ibdev,
219 		  "DEV: netdev %p event %lu local_ip=%pI6 MAC=%pM\n", real_dev,
220 		  event, local_ipaddr6, real_dev->dev_addr);
221 	switch (event) {
222 	case NETDEV_DOWN:
223 		irdma_manage_arp_cache(iwdev->rf, real_dev->dev_addr,
224 				       local_ipaddr6, false, IRDMA_ARP_DELETE);
225 		irdma_if_notify(iwdev, real_dev, local_ipaddr6, false, false);
226 		irdma_gid_change_event(&iwdev->ibdev);
227 		break;
228 	case NETDEV_UP:
229 	case NETDEV_CHANGEADDR:
230 		irdma_add_arp(iwdev->rf, local_ipaddr6, false,
231 			      real_dev->dev_addr);
232 		irdma_if_notify(iwdev, real_dev, local_ipaddr6, false, true);
233 		irdma_gid_change_event(&iwdev->ibdev);
234 		break;
235 	default:
236 		break;
237 	}
238 
239 	ib_device_put(ibdev);
240 
241 	return NOTIFY_DONE;
242 }
243 
244 /**
245  * irdma_net_event - system notifier for net events
246  * @notifier: not used
247  * @event: event for notifier
248  * @ptr: neighbor
249  */
250 int irdma_net_event(struct notifier_block *notifier, unsigned long event,
251 		    void *ptr)
252 {
253 	struct neighbour *neigh = ptr;
254 	struct net_device *real_dev, *netdev;
255 	struct irdma_device *iwdev;
256 	struct ib_device *ibdev;
257 	__be32 *p;
258 	u32 local_ipaddr[4] = {};
259 	bool ipv4 = true;
260 
261 	switch (event) {
262 	case NETEVENT_NEIGH_UPDATE:
263 		netdev = neigh->dev;
264 		real_dev = rdma_vlan_dev_real_dev(netdev);
265 		if (!real_dev)
266 			real_dev = netdev;
267 		ibdev = ib_device_get_by_netdev(real_dev, RDMA_DRIVER_IRDMA);
268 		if (!ibdev)
269 			return NOTIFY_DONE;
270 
271 		iwdev = to_iwdev(ibdev);
272 		p = (__be32 *)neigh->primary_key;
273 		if (neigh->tbl->family == AF_INET6) {
274 			ipv4 = false;
275 			irdma_copy_ip_ntohl(local_ipaddr, p);
276 		} else {
277 			local_ipaddr[0] = ntohl(*p);
278 		}
279 
280 		ibdev_dbg(&iwdev->ibdev,
281 			  "DEV: netdev %p state %d local_ip=%pI4 MAC=%pM\n",
282 			  iwdev->netdev, neigh->nud_state, local_ipaddr,
283 			  neigh->ha);
284 
285 		if (neigh->nud_state & NUD_VALID)
286 			irdma_add_arp(iwdev->rf, local_ipaddr, ipv4, neigh->ha);
287 
288 		else
289 			irdma_manage_arp_cache(iwdev->rf, neigh->ha,
290 					       local_ipaddr, ipv4,
291 					       IRDMA_ARP_DELETE);
292 		ib_device_put(ibdev);
293 		break;
294 	default:
295 		break;
296 	}
297 
298 	return NOTIFY_DONE;
299 }
300 
301 /**
302  * irdma_netdevice_event - system notifier for netdev events
303  * @notifier: not used
304  * @event: event for notifier
305  * @ptr: netdev
306  */
307 int irdma_netdevice_event(struct notifier_block *notifier, unsigned long event,
308 			  void *ptr)
309 {
310 	struct irdma_device *iwdev;
311 	struct ib_device *ibdev;
312 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
313 
314 	ibdev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_IRDMA);
315 	if (!ibdev)
316 		return NOTIFY_DONE;
317 
318 	iwdev = to_iwdev(ibdev);
319 	iwdev->iw_status = 1;
320 	switch (event) {
321 	case NETDEV_DOWN:
322 		iwdev->iw_status = 0;
323 		fallthrough;
324 	default:
325 		break;
326 	}
327 	ib_device_put(ibdev);
328 
329 	return NOTIFY_DONE;
330 }
331 
332 /**
333  * irdma_add_ipv6_addr - add ipv6 address to the hw arp table
334  * @iwdev: irdma device
335  */
336 static void irdma_add_ipv6_addr(struct irdma_device *iwdev)
337 {
338 	struct net_device *ip_dev;
339 	struct inet6_dev *idev;
340 	struct inet6_ifaddr *ifp, *tmp;
341 	u32 local_ipaddr6[4];
342 
343 	rcu_read_lock();
344 	for_each_netdev_rcu (&init_net, ip_dev) {
345 		if (((rdma_vlan_dev_vlan_id(ip_dev) < 0xFFFF &&
346 		      rdma_vlan_dev_real_dev(ip_dev) == iwdev->netdev) ||
347 		      ip_dev == iwdev->netdev) &&
348 		      (READ_ONCE(ip_dev->flags) & IFF_UP)) {
349 			idev = __in6_dev_get(ip_dev);
350 			if (!idev) {
351 				ibdev_err(&iwdev->ibdev, "ipv6 inet device not found\n");
352 				break;
353 			}
354 			list_for_each_entry_safe (ifp, tmp, &idev->addr_list,
355 						  if_list) {
356 				ibdev_dbg(&iwdev->ibdev,
357 					  "INIT: IP=%pI6, vlan_id=%d, MAC=%pM\n",
358 					  &ifp->addr,
359 					  rdma_vlan_dev_vlan_id(ip_dev),
360 					  ip_dev->dev_addr);
361 
362 				irdma_copy_ip_ntohl(local_ipaddr6,
363 						    ifp->addr.in6_u.u6_addr32);
364 				irdma_manage_arp_cache(iwdev->rf,
365 						       ip_dev->dev_addr,
366 						       local_ipaddr6, false,
367 						       IRDMA_ARP_ADD);
368 			}
369 		}
370 	}
371 	rcu_read_unlock();
372 }
373 
374 /**
375  * irdma_add_ipv4_addr - add ipv4 address to the hw arp table
376  * @iwdev: irdma device
377  */
378 static void irdma_add_ipv4_addr(struct irdma_device *iwdev)
379 {
380 	struct net_device *dev;
381 	struct in_device *idev;
382 	u32 ip_addr;
383 
384 	rcu_read_lock();
385 	for_each_netdev_rcu (&init_net, dev) {
386 		if (((rdma_vlan_dev_vlan_id(dev) < 0xFFFF &&
387 		      rdma_vlan_dev_real_dev(dev) == iwdev->netdev) ||
388 		      dev == iwdev->netdev) && (READ_ONCE(dev->flags) & IFF_UP)) {
389 			const struct in_ifaddr *ifa;
390 
391 			idev = __in_dev_get_rcu(dev);
392 			if (!idev)
393 				continue;
394 
395 			in_dev_for_each_ifa_rcu(ifa, idev) {
396 				ibdev_dbg(&iwdev->ibdev, "CM: IP=%pI4, vlan_id=%d, MAC=%pM\n",
397 					  &ifa->ifa_address, rdma_vlan_dev_vlan_id(dev),
398 					  dev->dev_addr);
399 
400 				ip_addr = ntohl(ifa->ifa_address);
401 				irdma_manage_arp_cache(iwdev->rf, dev->dev_addr,
402 						       &ip_addr, true,
403 						       IRDMA_ARP_ADD);
404 			}
405 		}
406 	}
407 	rcu_read_unlock();
408 }
409 
410 /**
411  * irdma_add_ip - add ip addresses
412  * @iwdev: irdma device
413  *
414  * Add ipv4/ipv6 addresses to the arp cache
415  */
416 void irdma_add_ip(struct irdma_device *iwdev)
417 {
418 	irdma_add_ipv4_addr(iwdev);
419 	irdma_add_ipv6_addr(iwdev);
420 }
421 
422 /**
423  * irdma_alloc_and_get_cqp_request - get cqp struct
424  * @cqp: device cqp ptr
425  * @wait: cqp to be used in wait mode
426  */
427 struct irdma_cqp_request *irdma_alloc_and_get_cqp_request(struct irdma_cqp *cqp,
428 							  bool wait)
429 {
430 	struct irdma_cqp_request *cqp_request = NULL;
431 	unsigned long flags;
432 
433 	spin_lock_irqsave(&cqp->req_lock, flags);
434 	if (!list_empty(&cqp->cqp_avail_reqs)) {
435 		cqp_request = list_first_entry(&cqp->cqp_avail_reqs,
436 					       struct irdma_cqp_request, list);
437 		list_del_init(&cqp_request->list);
438 	}
439 	spin_unlock_irqrestore(&cqp->req_lock, flags);
440 	if (!cqp_request) {
441 		cqp_request = kzalloc_obj(*cqp_request, GFP_ATOMIC);
442 		if (cqp_request) {
443 			cqp_request->dynamic = true;
444 			if (wait)
445 				init_completion(&cqp_request->comp);
446 		}
447 	}
448 	if (!cqp_request) {
449 		ibdev_dbg(to_ibdev(cqp->sc_cqp.dev), "ERR: CQP Request Fail: No Memory");
450 		return NULL;
451 	}
452 
453 	cqp_request->waiting = wait;
454 	refcount_set(&cqp_request->refcnt, 1);
455 	memset(&cqp_request->compl_info, 0, sizeof(cqp_request->compl_info));
456 	memset(&cqp_request->info, 0, sizeof(cqp_request->info));
457 
458 	return cqp_request;
459 }
460 
461 /**
462  * irdma_get_cqp_request - increase refcount for cqp_request
463  * @cqp_request: pointer to cqp_request instance
464  */
465 static inline void irdma_get_cqp_request(struct irdma_cqp_request *cqp_request)
466 {
467 	refcount_inc(&cqp_request->refcnt);
468 }
469 
470 /**
471  * irdma_free_cqp_request - free cqp request
472  * @cqp: cqp ptr
473  * @cqp_request: to be put back in cqp list
474  */
475 void irdma_free_cqp_request(struct irdma_cqp *cqp,
476 			    struct irdma_cqp_request *cqp_request)
477 {
478 	unsigned long flags;
479 
480 	if (cqp_request->dynamic) {
481 		kfree(cqp_request);
482 	} else {
483 		reinit_completion(&cqp_request->comp);
484 		cqp_request->callback_fcn = NULL;
485 		cqp_request->waiting = false;
486 		cqp_request->pending = false;
487 
488 		spin_lock_irqsave(&cqp->req_lock, flags);
489 		list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
490 		spin_unlock_irqrestore(&cqp->req_lock, flags);
491 	}
492 	wake_up(&cqp->remove_wq);
493 }
494 
495 /**
496  * irdma_put_cqp_request - dec ref count and free if 0
497  * @cqp: cqp ptr
498  * @cqp_request: to be put back in cqp list
499  */
500 void irdma_put_cqp_request(struct irdma_cqp *cqp,
501 			   struct irdma_cqp_request *cqp_request)
502 {
503 	if (refcount_dec_and_test(&cqp_request->refcnt))
504 		irdma_free_cqp_request(cqp, cqp_request);
505 }
506 
507 /**
508  * irdma_free_pending_cqp_request -free pending cqp request objs
509  * @cqp: cqp ptr
510  * @cqp_request: to be put back in cqp list
511  */
512 static void
513 irdma_free_pending_cqp_request(struct irdma_cqp *cqp,
514 			       struct irdma_cqp_request *cqp_request)
515 {
516 	if (cqp_request->waiting) {
517 		cqp_request->compl_info.error = true;
518 		complete_all(&cqp_request->comp);
519 	}
520 	wait_event_timeout(cqp->remove_wq,
521 			   refcount_read(&cqp_request->refcnt) == 1, 1000);
522 	irdma_put_cqp_request(cqp, cqp_request);
523 }
524 
525 /**
526  * irdma_cleanup_deferred_cqp_ops - clean-up cqp with no completions
527  * @dev: sc_dev
528  * @cqp: cqp
529  */
530 static void irdma_cleanup_deferred_cqp_ops(struct irdma_sc_dev *dev,
531 					   struct irdma_cqp *cqp)
532 {
533 	u64 scratch;
534 
535 	/* process all CQP requests with deferred/pending completions */
536 	while ((scratch = irdma_sc_cqp_cleanup_handler(dev)))
537 		irdma_free_pending_cqp_request(cqp, (struct irdma_cqp_request *)
538 						    (uintptr_t)scratch);
539 }
540 
541 /**
542  * irdma_cleanup_pending_cqp_op - clean-up cqp with no
543  * completions
544  * @rf: RDMA PCI function
545  */
546 void irdma_cleanup_pending_cqp_op(struct irdma_pci_f *rf)
547 {
548 	struct irdma_sc_dev *dev = &rf->sc_dev;
549 	struct irdma_cqp *cqp = &rf->cqp;
550 	struct irdma_cqp_request *cqp_request = NULL;
551 	struct cqp_cmds_info *pcmdinfo = NULL;
552 	u32 i, pending_work, wqe_idx;
553 
554 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3)
555 		irdma_cleanup_deferred_cqp_ops(dev, cqp);
556 	pending_work = IRDMA_RING_USED_QUANTA(cqp->sc_cqp.sq_ring);
557 	wqe_idx = IRDMA_RING_CURRENT_TAIL(cqp->sc_cqp.sq_ring);
558 	for (i = 0; i < pending_work; i++) {
559 		cqp_request = (struct irdma_cqp_request *)(unsigned long)
560 				      cqp->scratch_array[wqe_idx];
561 		if (cqp_request)
562 			irdma_free_pending_cqp_request(cqp, cqp_request);
563 		wqe_idx = (wqe_idx + 1) % IRDMA_RING_SIZE(cqp->sc_cqp.sq_ring);
564 	}
565 
566 	while (!list_empty(&dev->cqp_cmd_head)) {
567 		pcmdinfo = irdma_remove_cqp_head(dev);
568 		cqp_request =
569 			container_of(pcmdinfo, struct irdma_cqp_request, info);
570 		if (cqp_request)
571 			irdma_free_pending_cqp_request(cqp, cqp_request);
572 	}
573 }
574 
575 int irdma_get_timeout_threshold(struct irdma_sc_dev *dev)
576 {
577 	u16 time_s = dev->vc_caps.cqp_timeout_s;
578 
579 	if (!time_s)
580 		return CQP_TIMEOUT_THRESHOLD;
581 
582 	return time_s * 1000 / dev->hw_attrs.max_cqp_compl_wait_time_ms;
583 }
584 
585 static int irdma_get_def_timeout_threshold(struct irdma_sc_dev *dev)
586 {
587 	u16 time_s = dev->vc_caps.cqp_def_timeout_s;
588 
589 	if (!time_s)
590 		return CQP_DEF_CMPL_TIMEOUT_THRESHOLD;
591 
592 	return time_s * 1000 / dev->hw_attrs.max_cqp_compl_wait_time_ms;
593 }
594 
595 /**
596  * irdma_wait_event - wait for completion
597  * @rf: RDMA PCI function
598  * @cqp_request: cqp request to wait
599  */
600 static int irdma_wait_event(struct irdma_pci_f *rf,
601 			    struct irdma_cqp_request *cqp_request)
602 {
603 	struct irdma_cqp_timeout cqp_timeout = {};
604 	int timeout_threshold = irdma_get_timeout_threshold(&rf->sc_dev);
605 	bool cqp_error = false;
606 	int err_code = 0;
607 
608 	cqp_timeout.compl_cqp_cmds = atomic64_read(&rf->sc_dev.cqp->completed_ops);
609 	do {
610 		irdma_cqp_ce_handler(rf, &rf->ccq.sc_cq);
611 		if (wait_for_completion_timeout(&cqp_request->comp,
612 					msecs_to_jiffies(CQP_COMPL_WAIT_TIME_MS)))
613 			break;
614 
615 		if (cqp_request->pending)
616 			/* There was a deferred or pending completion
617 			 * received for this CQP request, so we need
618 			 * to wait longer than usual.
619 			 */
620 			timeout_threshold =
621 				irdma_get_def_timeout_threshold(&rf->sc_dev);
622 
623 		irdma_check_cqp_progress(&cqp_timeout, &rf->sc_dev);
624 
625 		if (cqp_timeout.count < timeout_threshold)
626 			continue;
627 
628 		if (!rf->reset) {
629 			rf->reset = true;
630 			rf->gen_ops.request_reset(rf);
631 		}
632 		return -ETIMEDOUT;
633 	} while (1);
634 
635 	cqp_error = cqp_request->compl_info.error;
636 	if (cqp_error) {
637 		err_code = -EIO;
638 		if (cqp_request->compl_info.maj_err_code == 0xFFFF) {
639 			if (cqp_request->compl_info.min_err_code == 0x8002)
640 				err_code = -EBUSY;
641 			else if (cqp_request->compl_info.min_err_code == 0x8029) {
642 				if (!rf->reset) {
643 					rf->reset = true;
644 					rf->gen_ops.request_reset(rf);
645 				}
646 			}
647 		}
648 	}
649 
650 	return err_code;
651 }
652 
653 static const char *const irdma_cqp_cmd_names[IRDMA_MAX_CQP_OPS] = {
654 	[IRDMA_OP_CEQ_DESTROY] = "Destroy CEQ Cmd",
655 	[IRDMA_OP_AEQ_DESTROY] = "Destroy AEQ Cmd",
656 	[IRDMA_OP_DELETE_ARP_CACHE_ENTRY] = "Delete ARP Cache Cmd",
657 	[IRDMA_OP_MANAGE_APBVT_ENTRY] = "Manage APBV Table Entry Cmd",
658 	[IRDMA_OP_CEQ_CREATE] = "CEQ Create Cmd",
659 	[IRDMA_OP_AEQ_CREATE] = "AEQ Destroy Cmd",
660 	[IRDMA_OP_MANAGE_QHASH_TABLE_ENTRY] = "Manage Quad Hash Table Entry Cmd",
661 	[IRDMA_OP_QP_MODIFY] = "Modify QP Cmd",
662 	[IRDMA_OP_QP_UPLOAD_CONTEXT] = "Upload Context Cmd",
663 	[IRDMA_OP_CQ_CREATE] = "Create CQ Cmd",
664 	[IRDMA_OP_CQ_DESTROY] = "Destroy CQ Cmd",
665 	[IRDMA_OP_QP_CREATE] = "Create QP Cmd",
666 	[IRDMA_OP_QP_DESTROY] = "Destroy QP Cmd",
667 	[IRDMA_OP_ALLOC_STAG] = "Allocate STag Cmd",
668 	[IRDMA_OP_MR_REG_NON_SHARED] = "Register Non-Shared MR Cmd",
669 	[IRDMA_OP_DEALLOC_STAG] = "Deallocate STag Cmd",
670 	[IRDMA_OP_MW_ALLOC] = "Allocate Memory Window Cmd",
671 	[IRDMA_OP_QP_FLUSH_WQES] = "Flush QP Cmd",
672 	[IRDMA_OP_ADD_ARP_CACHE_ENTRY] = "Add ARP Cache Cmd",
673 	[IRDMA_OP_MANAGE_PUSH_PAGE] = "Manage Push Page Cmd",
674 	[IRDMA_OP_UPDATE_PE_SDS] = "Update PE SDs Cmd",
675 	[IRDMA_OP_MANAGE_HMC_PM_FUNC_TABLE] = "Manage HMC PM Function Table Cmd",
676 	[IRDMA_OP_SUSPEND] = "Suspend QP Cmd",
677 	[IRDMA_OP_RESUME] = "Resume QP Cmd",
678 	[IRDMA_OP_MANAGE_VF_PBLE_BP] = "Manage VF PBLE Backing Pages Cmd",
679 	[IRDMA_OP_QUERY_FPM_VAL] = "Query FPM Values Cmd",
680 	[IRDMA_OP_COMMIT_FPM_VAL] = "Commit FPM Values Cmd",
681 	[IRDMA_OP_AH_CREATE] = "Create Address Handle Cmd",
682 	[IRDMA_OP_AH_MODIFY] = "Modify Address Handle Cmd",
683 	[IRDMA_OP_AH_DESTROY] = "Destroy Address Handle Cmd",
684 	[IRDMA_OP_MC_CREATE] = "Create Multicast Group Cmd",
685 	[IRDMA_OP_MC_DESTROY] = "Destroy Multicast Group Cmd",
686 	[IRDMA_OP_MC_MODIFY] = "Modify Multicast Group Cmd",
687 	[IRDMA_OP_STATS_ALLOCATE] = "Add Statistics Instance Cmd",
688 	[IRDMA_OP_STATS_FREE] = "Free Statistics Instance Cmd",
689 	[IRDMA_OP_STATS_GATHER] = "Gather Statistics Cmd",
690 	[IRDMA_OP_WS_ADD_NODE] = "Add Work Scheduler Node Cmd",
691 	[IRDMA_OP_WS_MODIFY_NODE] = "Modify Work Scheduler Node Cmd",
692 	[IRDMA_OP_WS_DELETE_NODE] = "Delete Work Scheduler Node Cmd",
693 	[IRDMA_OP_SET_UP_MAP] = "Set UP-UP Mapping Cmd",
694 	[IRDMA_OP_GEN_AE] = "Generate AE Cmd",
695 	[IRDMA_OP_QUERY_RDMA_FEATURES] = "RDMA Get Features Cmd",
696 	[IRDMA_OP_ALLOC_LOCAL_MAC_ENTRY] = "Allocate Local MAC Entry Cmd",
697 	[IRDMA_OP_ADD_LOCAL_MAC_ENTRY] = "Add Local MAC Entry Cmd",
698 	[IRDMA_OP_DELETE_LOCAL_MAC_ENTRY] = "Delete Local MAC Entry Cmd",
699 	[IRDMA_OP_CQ_MODIFY] = "CQ Modify Cmd",
700 	[IRDMA_OP_SRQ_CREATE] = "Create SRQ Cmd",
701 	[IRDMA_OP_SRQ_MODIFY] = "Modify SRQ Cmd",
702 	[IRDMA_OP_SRQ_DESTROY] = "Destroy SRQ Cmd",
703 };
704 
705 static const struct irdma_cqp_err_info irdma_noncrit_err_list[] = {
706 	{0xffff, 0x8002, "Invalid State"},
707 	{0xffff, 0x8006, "Flush No Wqe Pending"},
708 	{0xffff, 0x8007, "Modify QP Bad Close"},
709 	{0xffff, 0x8009, "LLP Closed"},
710 	{0xffff, 0x800a, "Reset Not Sent"}
711 };
712 
713 /**
714  * irdma_cqp_crit_err - check if CQP error is critical
715  * @dev: pointer to dev structure
716  * @cqp_cmd: code for last CQP operation
717  * @maj_err_code: major error code
718  * @min_err_code: minot error code
719  */
720 bool irdma_cqp_crit_err(struct irdma_sc_dev *dev, u8 cqp_cmd,
721 			u16 maj_err_code, u16 min_err_code)
722 {
723 	int i;
724 
725 	for (i = 0; i < ARRAY_SIZE(irdma_noncrit_err_list); ++i) {
726 		if (maj_err_code == irdma_noncrit_err_list[i].maj &&
727 		    min_err_code == irdma_noncrit_err_list[i].min) {
728 			ibdev_dbg(to_ibdev(dev),
729 				  "CQP: [%s Error][%s] maj=0x%x min=0x%x\n",
730 				  irdma_noncrit_err_list[i].desc,
731 				  irdma_cqp_cmd_names[cqp_cmd], maj_err_code,
732 				  min_err_code);
733 			return false;
734 		}
735 	}
736 	return true;
737 }
738 
739 /**
740  * irdma_handle_cqp_op - process cqp command
741  * @rf: RDMA PCI function
742  * @cqp_request: cqp request to process
743  */
744 int irdma_handle_cqp_op(struct irdma_pci_f *rf,
745 			struct irdma_cqp_request *cqp_request)
746 {
747 	struct irdma_sc_dev *dev = &rf->sc_dev;
748 	struct cqp_cmds_info *info = &cqp_request->info;
749 	int status;
750 	bool put_cqp_request = true;
751 
752 	if (rf->reset)
753 		return -EBUSY;
754 
755 	irdma_get_cqp_request(cqp_request);
756 	status = irdma_process_cqp_cmd(dev, info);
757 	if (status)
758 		goto err;
759 
760 	if (cqp_request->waiting) {
761 		put_cqp_request = false;
762 		status = irdma_wait_event(rf, cqp_request);
763 		if (status)
764 			goto err;
765 	}
766 
767 	return 0;
768 
769 err:
770 	if (irdma_cqp_crit_err(dev, info->cqp_cmd,
771 			       cqp_request->compl_info.maj_err_code,
772 			       cqp_request->compl_info.min_err_code))
773 		ibdev_err(&rf->iwdev->ibdev,
774 			  "[%s Error][op_code=%d] status=%d waiting=%d completion_err=%d maj=0x%x min=0x%x\n",
775 			  irdma_cqp_cmd_names[info->cqp_cmd], info->cqp_cmd, status, cqp_request->waiting,
776 			  cqp_request->compl_info.error, cqp_request->compl_info.maj_err_code,
777 			  cqp_request->compl_info.min_err_code);
778 
779 	if (put_cqp_request)
780 		irdma_put_cqp_request(&rf->cqp, cqp_request);
781 
782 	return status;
783 }
784 
785 void irdma_qp_add_ref(struct ib_qp *ibqp)
786 {
787 	struct irdma_qp *iwqp = (struct irdma_qp *)ibqp;
788 
789 	refcount_inc(&iwqp->refcnt);
790 }
791 
792 void irdma_qp_rem_ref(struct ib_qp *ibqp)
793 {
794 	struct irdma_qp *iwqp = to_iwqp(ibqp);
795 	struct irdma_device *iwdev = iwqp->iwdev;
796 	u32 qp_num;
797 	unsigned long flags;
798 
799 	spin_lock_irqsave(&iwdev->rf->qptable_lock, flags);
800 	if (!refcount_dec_and_test(&iwqp->refcnt)) {
801 		spin_unlock_irqrestore(&iwdev->rf->qptable_lock, flags);
802 		return;
803 	}
804 
805 	qp_num = iwqp->ibqp.qp_num;
806 	iwdev->rf->qp_table[qp_num] = NULL;
807 	spin_unlock_irqrestore(&iwdev->rf->qptable_lock, flags);
808 	complete(&iwqp->free_qp);
809 }
810 
811 void irdma_cq_add_ref(struct ib_cq *ibcq)
812 {
813 	struct irdma_cq *iwcq = to_iwcq(ibcq);
814 
815 	refcount_inc(&iwcq->refcnt);
816 }
817 
818 void irdma_cq_rem_ref(struct ib_cq *ibcq)
819 {
820 	struct ib_device *ibdev = ibcq->device;
821 	struct irdma_device *iwdev = to_iwdev(ibdev);
822 	struct irdma_cq *iwcq = to_iwcq(ibcq);
823 	unsigned long flags;
824 
825 	spin_lock_irqsave(&iwdev->rf->cqtable_lock, flags);
826 	if (!refcount_dec_and_test(&iwcq->refcnt)) {
827 		spin_unlock_irqrestore(&iwdev->rf->cqtable_lock, flags);
828 		return;
829 	}
830 
831 	/* May be asynchronously sampled by CEQ ISR without holding tbl lock. */
832 	WRITE_ONCE(iwdev->rf->cq_table[iwcq->cq_num], NULL);
833 	spin_unlock_irqrestore(&iwdev->rf->cqtable_lock, flags);
834 	complete(&iwcq->free_cq);
835 }
836 
837 struct ib_device *to_ibdev(struct irdma_sc_dev *dev)
838 {
839 	return &(container_of(dev, struct irdma_pci_f, sc_dev))->iwdev->ibdev;
840 }
841 
842 /**
843  * irdma_get_qp - get qp address
844  * @device: iwarp device
845  * @qpn: qp number
846  */
847 struct ib_qp *irdma_get_qp(struct ib_device *device, int qpn)
848 {
849 	struct irdma_device *iwdev = to_iwdev(device);
850 
851 	if (qpn < IW_FIRST_QPN || qpn >= iwdev->rf->max_qp)
852 		return NULL;
853 
854 	return &iwdev->rf->qp_table[qpn]->ibqp;
855 }
856 
857 /**
858  * irdma_remove_cqp_head - return head entry and remove
859  * @dev: device
860  */
861 void *irdma_remove_cqp_head(struct irdma_sc_dev *dev)
862 {
863 	struct list_head *entry;
864 	struct list_head *list = &dev->cqp_cmd_head;
865 
866 	if (list_empty(list))
867 		return NULL;
868 
869 	entry = list->next;
870 	list_del(entry);
871 
872 	return entry;
873 }
874 
875 /**
876  * irdma_cqp_sds_cmd - create cqp command for sd
877  * @dev: hardware control device structure
878  * @sdinfo: information for sd cqp
879  *
880  */
881 int irdma_cqp_sds_cmd(struct irdma_sc_dev *dev,
882 		      struct irdma_update_sds_info *sdinfo)
883 {
884 	struct irdma_cqp_request *cqp_request;
885 	struct cqp_cmds_info *cqp_info;
886 	struct irdma_pci_f *rf = dev_to_rf(dev);
887 	int status;
888 
889 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, true);
890 	if (!cqp_request)
891 		return -ENOMEM;
892 
893 	cqp_info = &cqp_request->info;
894 	memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
895 	       sizeof(cqp_info->in.u.update_pe_sds.info));
896 	cqp_info->cqp_cmd = IRDMA_OP_UPDATE_PE_SDS;
897 	cqp_info->post_sq = 1;
898 	cqp_info->in.u.update_pe_sds.dev = dev;
899 	cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
900 
901 	status = irdma_handle_cqp_op(rf, cqp_request);
902 	irdma_put_cqp_request(&rf->cqp, cqp_request);
903 
904 	return status;
905 }
906 
907 /**
908  * irdma_cqp_qp_suspend_resume - cqp command for suspend/resume
909  * @qp: hardware control qp
910  * @op: suspend or resume
911  */
912 int irdma_cqp_qp_suspend_resume(struct irdma_sc_qp *qp, u8 op)
913 {
914 	struct irdma_sc_dev *dev = qp->dev;
915 	struct irdma_cqp_request *cqp_request;
916 	struct irdma_sc_cqp *cqp = dev->cqp;
917 	struct cqp_cmds_info *cqp_info;
918 	struct irdma_pci_f *rf = dev_to_rf(dev);
919 	int status;
920 
921 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, false);
922 	if (!cqp_request)
923 		return -ENOMEM;
924 
925 	cqp_info = &cqp_request->info;
926 	cqp_info->cqp_cmd = op;
927 	cqp_info->in.u.suspend_resume.cqp = cqp;
928 	cqp_info->in.u.suspend_resume.qp = qp;
929 	cqp_info->in.u.suspend_resume.scratch = (uintptr_t)cqp_request;
930 
931 	status = irdma_handle_cqp_op(rf, cqp_request);
932 	irdma_put_cqp_request(&rf->cqp, cqp_request);
933 
934 	return status;
935 }
936 
937 /**
938  * irdma_term_modify_qp - modify qp for term message
939  * @qp: hardware control qp
940  * @next_state: qp's next state
941  * @term: terminate code
942  * @term_len: length
943  */
944 void irdma_term_modify_qp(struct irdma_sc_qp *qp, u8 next_state, u8 term,
945 			  u8 term_len)
946 {
947 	struct irdma_qp *iwqp;
948 
949 	iwqp = qp->qp_uk.back_qp;
950 	irdma_next_iw_state(iwqp, next_state, 0, term, term_len);
951 };
952 
953 /**
954  * irdma_terminate_done - after terminate is completed
955  * @qp: hardware control qp
956  * @timeout_occurred: indicates if terminate timer expired
957  */
958 void irdma_terminate_done(struct irdma_sc_qp *qp, int timeout_occurred)
959 {
960 	struct irdma_qp *iwqp;
961 	u8 hte = 0;
962 	bool first_time;
963 	unsigned long flags;
964 
965 	iwqp = qp->qp_uk.back_qp;
966 	spin_lock_irqsave(&iwqp->lock, flags);
967 	if (iwqp->hte_added) {
968 		iwqp->hte_added = 0;
969 		hte = 1;
970 	}
971 	first_time = !(qp->term_flags & IRDMA_TERM_DONE);
972 	qp->term_flags |= IRDMA_TERM_DONE;
973 	spin_unlock_irqrestore(&iwqp->lock, flags);
974 	if (first_time) {
975 		if (!timeout_occurred)
976 			irdma_terminate_del_timer(qp);
977 
978 		irdma_next_iw_state(iwqp, IRDMA_QP_STATE_ERROR, hte, 0, 0);
979 		irdma_cm_disconn(iwqp);
980 	}
981 }
982 
983 static void irdma_terminate_timeout(struct timer_list *t)
984 {
985 	struct irdma_qp *iwqp = timer_container_of(iwqp, t, terminate_timer);
986 	struct irdma_sc_qp *qp = &iwqp->sc_qp;
987 
988 	irdma_terminate_done(qp, 1);
989 	irdma_qp_rem_ref(&iwqp->ibqp);
990 }
991 
992 /**
993  * irdma_terminate_start_timer - start terminate timeout
994  * @qp: hardware control qp
995  */
996 void irdma_terminate_start_timer(struct irdma_sc_qp *qp)
997 {
998 	struct irdma_qp *iwqp;
999 
1000 	iwqp = qp->qp_uk.back_qp;
1001 	irdma_qp_add_ref(&iwqp->ibqp);
1002 	timer_setup(&iwqp->terminate_timer, irdma_terminate_timeout, 0);
1003 	iwqp->terminate_timer.expires = jiffies + HZ;
1004 
1005 	add_timer(&iwqp->terminate_timer);
1006 }
1007 
1008 /**
1009  * irdma_terminate_del_timer - delete terminate timeout
1010  * @qp: hardware control qp
1011  */
1012 void irdma_terminate_del_timer(struct irdma_sc_qp *qp)
1013 {
1014 	struct irdma_qp *iwqp;
1015 	int ret;
1016 
1017 	iwqp = qp->qp_uk.back_qp;
1018 	ret = timer_delete(&iwqp->terminate_timer);
1019 	if (ret)
1020 		irdma_qp_rem_ref(&iwqp->ibqp);
1021 }
1022 
1023 /**
1024  * irdma_cqp_cq_create_cmd - create a cq for the cqp
1025  * @dev: device pointer
1026  * @cq: pointer to created cq
1027  */
1028 int irdma_cqp_cq_create_cmd(struct irdma_sc_dev *dev, struct irdma_sc_cq *cq)
1029 {
1030 	struct irdma_pci_f *rf = dev_to_rf(dev);
1031 	struct irdma_cqp *iwcqp = &rf->cqp;
1032 	struct irdma_cqp_request *cqp_request;
1033 	struct cqp_cmds_info *cqp_info;
1034 	int status;
1035 
1036 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, true);
1037 	if (!cqp_request)
1038 		return -ENOMEM;
1039 
1040 	cqp_info = &cqp_request->info;
1041 	cqp_info->cqp_cmd = IRDMA_OP_CQ_CREATE;
1042 	cqp_info->post_sq = 1;
1043 	cqp_info->in.u.cq_create.cq = cq;
1044 	cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
1045 
1046 	status = irdma_handle_cqp_op(rf, cqp_request);
1047 	irdma_put_cqp_request(iwcqp, cqp_request);
1048 
1049 	return status;
1050 }
1051 
1052 /**
1053  * irdma_cqp_qp_create_cmd - create a qp for the cqp
1054  * @dev: device pointer
1055  * @qp: pointer to created qp
1056  */
1057 int irdma_cqp_qp_create_cmd(struct irdma_sc_dev *dev, struct irdma_sc_qp *qp)
1058 {
1059 	struct irdma_pci_f *rf = dev_to_rf(dev);
1060 	struct irdma_cqp *iwcqp = &rf->cqp;
1061 	struct irdma_cqp_request *cqp_request;
1062 	struct cqp_cmds_info *cqp_info;
1063 	struct irdma_create_qp_info *qp_info;
1064 	int status;
1065 
1066 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, true);
1067 	if (!cqp_request)
1068 		return -ENOMEM;
1069 
1070 	cqp_info = &cqp_request->info;
1071 	qp_info = &cqp_request->info.in.u.qp_create.info;
1072 	qp_info->cq_num_valid = true;
1073 	qp_info->next_iwarp_state = IRDMA_QP_STATE_RTS;
1074 	cqp_info->cqp_cmd = IRDMA_OP_QP_CREATE;
1075 	cqp_info->post_sq = 1;
1076 	cqp_info->in.u.qp_create.qp = qp;
1077 	cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
1078 
1079 	status = irdma_handle_cqp_op(rf, cqp_request);
1080 	irdma_put_cqp_request(iwcqp, cqp_request);
1081 
1082 	return status;
1083 }
1084 
1085 /**
1086  * irdma_dealloc_push_page - free a push page for qp
1087  * @rf: RDMA PCI function
1088  * @qp: hardware control qp
1089  */
1090 static void irdma_dealloc_push_page(struct irdma_pci_f *rf,
1091 				    struct irdma_sc_qp *qp)
1092 {
1093 	struct irdma_cqp_request *cqp_request;
1094 	struct cqp_cmds_info *cqp_info;
1095 	int status;
1096 
1097 	if (qp->push_idx == IRDMA_INVALID_PUSH_PAGE_INDEX)
1098 		return;
1099 
1100 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, false);
1101 	if (!cqp_request)
1102 		return;
1103 
1104 	cqp_info = &cqp_request->info;
1105 	cqp_info->cqp_cmd = IRDMA_OP_MANAGE_PUSH_PAGE;
1106 	cqp_info->post_sq = 1;
1107 	cqp_info->in.u.manage_push_page.info.push_idx = qp->push_idx;
1108 	cqp_info->in.u.manage_push_page.info.qs_handle = qp->qs_handle;
1109 	cqp_info->in.u.manage_push_page.info.free_page = 1;
1110 	cqp_info->in.u.manage_push_page.info.push_page_type = 0;
1111 	cqp_info->in.u.manage_push_page.cqp = &rf->cqp.sc_cqp;
1112 	cqp_info->in.u.manage_push_page.scratch = (uintptr_t)cqp_request;
1113 	status = irdma_handle_cqp_op(rf, cqp_request);
1114 	if (!status)
1115 		qp->push_idx = IRDMA_INVALID_PUSH_PAGE_INDEX;
1116 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1117 }
1118 
1119 static void irdma_free_gsi_qp_rsrc(struct irdma_qp *iwqp, u32 qp_num)
1120 {
1121 	struct irdma_device *iwdev = iwqp->iwdev;
1122 	struct irdma_pci_f *rf = iwdev->rf;
1123 	unsigned long flags;
1124 
1125 	if (rf->sc_dev.hw_attrs.uk_attrs.hw_rev < IRDMA_GEN_3)
1126 		return;
1127 
1128 	irdma_vchnl_req_del_vport(&rf->sc_dev, iwdev->vport_id, qp_num);
1129 
1130 	if (qp_num == 1) {
1131 		spin_lock_irqsave(&rf->rsrc_lock, flags);
1132 		rf->hwqp1_rsvd = false;
1133 		spin_unlock_irqrestore(&rf->rsrc_lock, flags);
1134 	} else if (qp_num > 2) {
1135 		irdma_free_rsrc(rf, rf->allocated_qps, qp_num);
1136 	}
1137 }
1138 
1139 /**
1140  * irdma_free_qp_rsrc - free up memory resources for qp
1141  * @iwqp: qp ptr (user or kernel)
1142  */
1143 void irdma_free_qp_rsrc(struct irdma_qp *iwqp)
1144 {
1145 	struct irdma_device *iwdev = iwqp->iwdev;
1146 	struct irdma_pci_f *rf = iwdev->rf;
1147 	u32 qp_num = iwqp->sc_qp.qp_uk.qp_id;
1148 
1149 	irdma_ieq_cleanup_qp(iwdev->vsi.ieq, &iwqp->sc_qp);
1150 	irdma_dealloc_push_page(rf, &iwqp->sc_qp);
1151 	if (iwqp->sc_qp.vsi) {
1152 		irdma_qp_rem_qos(&iwqp->sc_qp);
1153 		iwqp->sc_qp.dev->ws_remove(iwqp->sc_qp.vsi,
1154 					   iwqp->sc_qp.user_pri);
1155 	}
1156 
1157 	if (iwqp->ibqp.qp_type == IB_QPT_GSI) {
1158 		irdma_free_gsi_qp_rsrc(iwqp, qp_num);
1159 	} else {
1160 		if (qp_num > 2)
1161 			irdma_free_rsrc(rf, rf->allocated_qps, qp_num);
1162 	}
1163 	dma_free_coherent(rf->sc_dev.hw->device, iwqp->q2_ctx_mem.size,
1164 			  iwqp->q2_ctx_mem.va, iwqp->q2_ctx_mem.pa);
1165 	iwqp->q2_ctx_mem.va = NULL;
1166 	dma_free_coherent(rf->sc_dev.hw->device, iwqp->kqp.dma_mem.size,
1167 			  iwqp->kqp.dma_mem.va, iwqp->kqp.dma_mem.pa);
1168 	iwqp->kqp.dma_mem.va = NULL;
1169 	kfree(iwqp->kqp.sq_wrid_mem);
1170 	kfree(iwqp->kqp.rq_wrid_mem);
1171 
1172 	if (iwqp->user_mode && iwqp->iwpbl) {
1173 		struct irdma_mr *iwmr = iwqp->iwpbl->iwmr;
1174 
1175 		refcount_dec(&iwmr->user_ring_refs);
1176 	}
1177 }
1178 
1179 /**
1180  * irdma_srq_wq_destroy - send srq destroy cqp
1181  * @rf: RDMA PCI function
1182  * @srq: hardware control srq
1183  */
1184 void irdma_srq_wq_destroy(struct irdma_pci_f *rf, struct irdma_sc_srq *srq)
1185 {
1186 	struct irdma_cqp_request *cqp_request;
1187 	struct cqp_cmds_info *cqp_info;
1188 
1189 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, true);
1190 	if (!cqp_request)
1191 		return;
1192 
1193 	cqp_info = &cqp_request->info;
1194 	cqp_info->cqp_cmd = IRDMA_OP_SRQ_DESTROY;
1195 	cqp_info->post_sq = 1;
1196 	cqp_info->in.u.srq_destroy.srq = srq;
1197 	cqp_info->in.u.srq_destroy.scratch = (uintptr_t)cqp_request;
1198 
1199 	irdma_handle_cqp_op(rf, cqp_request);
1200 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1201 }
1202 
1203 /**
1204  * irdma_cq_wq_destroy - send cq destroy cqp
1205  * @rf: RDMA PCI function
1206  * @cq: hardware control cq
1207  */
1208 void irdma_cq_wq_destroy(struct irdma_pci_f *rf, struct irdma_sc_cq *cq)
1209 {
1210 	struct irdma_cqp_request *cqp_request;
1211 	struct cqp_cmds_info *cqp_info;
1212 
1213 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, true);
1214 	if (!cqp_request)
1215 		return;
1216 
1217 	cqp_info = &cqp_request->info;
1218 	cqp_info->cqp_cmd = IRDMA_OP_CQ_DESTROY;
1219 	cqp_info->post_sq = 1;
1220 	cqp_info->in.u.cq_destroy.cq = cq;
1221 	cqp_info->in.u.cq_destroy.scratch = (uintptr_t)cqp_request;
1222 
1223 	irdma_handle_cqp_op(rf, cqp_request);
1224 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1225 }
1226 
1227 /**
1228  * irdma_hw_modify_qp_callback - handle state for modifyQPs that don't wait
1229  * @cqp_request: modify QP completion
1230  */
1231 static void irdma_hw_modify_qp_callback(struct irdma_cqp_request *cqp_request)
1232 {
1233 	struct cqp_cmds_info *cqp_info;
1234 	struct irdma_qp *iwqp;
1235 
1236 	cqp_info = &cqp_request->info;
1237 	iwqp = cqp_info->in.u.qp_modify.qp->qp_uk.back_qp;
1238 	atomic_dec(&iwqp->hw_mod_qp_pend);
1239 	wake_up(&iwqp->mod_qp_waitq);
1240 }
1241 
1242 /**
1243  * irdma_hw_modify_qp - setup cqp for modify qp
1244  * @iwdev: RDMA device
1245  * @iwqp: qp ptr (user or kernel)
1246  * @info: info for modify qp
1247  * @wait: flag to wait or not for modify qp completion
1248  */
1249 int irdma_hw_modify_qp(struct irdma_device *iwdev, struct irdma_qp *iwqp,
1250 		       struct irdma_modify_qp_info *info, bool wait)
1251 {
1252 	int status;
1253 	struct irdma_pci_f *rf = iwdev->rf;
1254 	struct irdma_cqp_request *cqp_request;
1255 	struct cqp_cmds_info *cqp_info;
1256 	struct irdma_modify_qp_info *m_info;
1257 
1258 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, wait);
1259 	if (!cqp_request)
1260 		return -ENOMEM;
1261 
1262 	if (!wait) {
1263 		cqp_request->callback_fcn = irdma_hw_modify_qp_callback;
1264 		atomic_inc(&iwqp->hw_mod_qp_pend);
1265 	}
1266 	cqp_info = &cqp_request->info;
1267 	m_info = &cqp_info->in.u.qp_modify.info;
1268 	memcpy(m_info, info, sizeof(*m_info));
1269 	cqp_info->cqp_cmd = IRDMA_OP_QP_MODIFY;
1270 	cqp_info->post_sq = 1;
1271 	cqp_info->in.u.qp_modify.qp = &iwqp->sc_qp;
1272 	cqp_info->in.u.qp_modify.scratch = (uintptr_t)cqp_request;
1273 	status = irdma_handle_cqp_op(rf, cqp_request);
1274 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1275 	if (status) {
1276 		if (rdma_protocol_roce(&iwdev->ibdev, 1))
1277 			return status;
1278 
1279 		switch (m_info->next_iwarp_state) {
1280 			struct irdma_gen_ae_info ae_info;
1281 
1282 		case IRDMA_QP_STATE_RTS:
1283 		case IRDMA_QP_STATE_IDLE:
1284 		case IRDMA_QP_STATE_TERMINATE:
1285 		case IRDMA_QP_STATE_CLOSING:
1286 			if (info->curr_iwarp_state == IRDMA_QP_STATE_IDLE)
1287 				irdma_send_reset(iwqp->cm_node);
1288 			else
1289 				iwqp->sc_qp.term_flags = IRDMA_TERM_DONE;
1290 			if (!wait) {
1291 				ae_info.ae_code = IRDMA_AE_BAD_CLOSE;
1292 				ae_info.ae_src = 0;
1293 				irdma_gen_ae(rf, &iwqp->sc_qp, &ae_info, false);
1294 			} else {
1295 				cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp,
1296 									      wait);
1297 				if (!cqp_request)
1298 					return -ENOMEM;
1299 
1300 				cqp_info = &cqp_request->info;
1301 				m_info = &cqp_info->in.u.qp_modify.info;
1302 				memcpy(m_info, info, sizeof(*m_info));
1303 				cqp_info->cqp_cmd = IRDMA_OP_QP_MODIFY;
1304 				cqp_info->post_sq = 1;
1305 				cqp_info->in.u.qp_modify.qp = &iwqp->sc_qp;
1306 				cqp_info->in.u.qp_modify.scratch = (uintptr_t)cqp_request;
1307 				m_info->next_iwarp_state = IRDMA_QP_STATE_ERROR;
1308 				m_info->reset_tcp_conn = true;
1309 				irdma_handle_cqp_op(rf, cqp_request);
1310 				irdma_put_cqp_request(&rf->cqp, cqp_request);
1311 			}
1312 			break;
1313 		case IRDMA_QP_STATE_ERROR:
1314 		default:
1315 			break;
1316 		}
1317 	}
1318 
1319 	return status;
1320 }
1321 
1322 /**
1323  * irdma_cqp_cq_destroy_cmd - destroy the cqp cq
1324  * @dev: device pointer
1325  * @cq: pointer to cq
1326  */
1327 void irdma_cqp_cq_destroy_cmd(struct irdma_sc_dev *dev, struct irdma_sc_cq *cq)
1328 {
1329 	struct irdma_pci_f *rf = dev_to_rf(dev);
1330 
1331 	irdma_cq_wq_destroy(rf, cq);
1332 }
1333 
1334 /**
1335  * irdma_cqp_qp_destroy_cmd - destroy the cqp
1336  * @dev: device pointer
1337  * @qp: pointer to qp
1338  */
1339 int irdma_cqp_qp_destroy_cmd(struct irdma_sc_dev *dev, struct irdma_sc_qp *qp)
1340 {
1341 	struct irdma_pci_f *rf = dev_to_rf(dev);
1342 	struct irdma_cqp *iwcqp = &rf->cqp;
1343 	struct irdma_cqp_request *cqp_request;
1344 	struct cqp_cmds_info *cqp_info;
1345 	int status;
1346 
1347 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, true);
1348 	if (!cqp_request)
1349 		return -ENOMEM;
1350 
1351 	cqp_info = &cqp_request->info;
1352 	cqp_info->cqp_cmd = IRDMA_OP_QP_DESTROY;
1353 	cqp_info->post_sq = 1;
1354 	cqp_info->in.u.qp_destroy.qp = qp;
1355 	cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
1356 	cqp_info->in.u.qp_destroy.remove_hash_idx = true;
1357 
1358 	status = irdma_handle_cqp_op(rf, cqp_request);
1359 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1360 
1361 	return status;
1362 }
1363 
1364 /**
1365  * irdma_ieq_mpa_crc_ae - generate AE for crc error
1366  * @dev: hardware control device structure
1367  * @qp: hardware control qp
1368  */
1369 void irdma_ieq_mpa_crc_ae(struct irdma_sc_dev *dev, struct irdma_sc_qp *qp)
1370 {
1371 	struct irdma_gen_ae_info info = {};
1372 	struct irdma_pci_f *rf = dev_to_rf(dev);
1373 
1374 	ibdev_dbg(&rf->iwdev->ibdev, "AEQ: Generate MPA CRC AE\n");
1375 	info.ae_code = IRDMA_AE_LLP_RECEIVED_MPA_CRC_ERROR;
1376 	info.ae_src = IRDMA_AE_SOURCE_RQ;
1377 	irdma_gen_ae(rf, qp, &info, false);
1378 }
1379 
1380 /**
1381  * irdma_ieq_check_mpacrc - check if mpa crc is OK
1382  * @addr: address of buffer for crc
1383  * @len: length of buffer
1384  * @val: value to be compared
1385  */
1386 int irdma_ieq_check_mpacrc(const void *addr, u32 len, u32 val)
1387 {
1388 	if ((__force u32)cpu_to_le32(~crc32c(~0, addr, len)) != val)
1389 		return -EINVAL;
1390 
1391 	return 0;
1392 }
1393 
1394 /**
1395  * irdma_ieq_get_qp - get qp based on quad in puda buffer
1396  * @dev: hardware control device structure
1397  * @buf: receive puda buffer on exception q
1398  */
1399 struct irdma_sc_qp *irdma_ieq_get_qp(struct irdma_sc_dev *dev,
1400 				     struct irdma_puda_buf *buf)
1401 {
1402 	struct irdma_qp *iwqp;
1403 	struct irdma_cm_node *cm_node;
1404 	struct irdma_device *iwdev = buf->vsi->back_vsi;
1405 	u32 loc_addr[4] = {};
1406 	u32 rem_addr[4] = {};
1407 	u16 loc_port, rem_port;
1408 	struct ipv6hdr *ip6h;
1409 	struct iphdr *iph = (struct iphdr *)buf->iph;
1410 	struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1411 
1412 	if (iph->version == 4) {
1413 		loc_addr[0] = ntohl(iph->daddr);
1414 		rem_addr[0] = ntohl(iph->saddr);
1415 	} else {
1416 		ip6h = (struct ipv6hdr *)buf->iph;
1417 		irdma_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1418 		irdma_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1419 	}
1420 	loc_port = ntohs(tcph->dest);
1421 	rem_port = ntohs(tcph->source);
1422 	cm_node = irdma_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
1423 				  loc_addr, buf->vlan_valid ? buf->vlan_id : 0xFFFF);
1424 	if (!cm_node)
1425 		return NULL;
1426 
1427 	iwqp = cm_node->iwqp;
1428 	irdma_rem_ref_cm_node(cm_node);
1429 
1430 	return &iwqp->sc_qp;
1431 }
1432 
1433 /**
1434  * irdma_send_ieq_ack - ACKs for duplicate or OOO partials FPDUs
1435  * @qp: qp ptr
1436  */
1437 void irdma_send_ieq_ack(struct irdma_sc_qp *qp)
1438 {
1439 	struct irdma_cm_node *cm_node = ((struct irdma_qp *)qp->qp_uk.back_qp)->cm_node;
1440 	struct irdma_puda_buf *buf = qp->pfpdu.lastrcv_buf;
1441 	struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1442 
1443 	cm_node->tcp_cntxt.rcv_nxt = qp->pfpdu.nextseqnum;
1444 	cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1445 
1446 	irdma_send_ack(cm_node);
1447 }
1448 
1449 /**
1450  * irdma_puda_ieq_get_ah_info - get AH info from IEQ buffer
1451  * @qp: qp pointer
1452  * @ah_info: AH info pointer
1453  */
1454 void irdma_puda_ieq_get_ah_info(struct irdma_sc_qp *qp,
1455 				struct irdma_ah_info *ah_info)
1456 {
1457 	struct irdma_puda_buf *buf = qp->pfpdu.ah_buf;
1458 	struct iphdr *iph;
1459 	struct ipv6hdr *ip6h;
1460 
1461 	memset(ah_info, 0, sizeof(*ah_info));
1462 	ah_info->do_lpbk = true;
1463 	ah_info->vlan_tag = buf->vlan_id;
1464 	ah_info->insert_vlan_tag = buf->vlan_valid;
1465 	ah_info->ipv4_valid = buf->ipv4;
1466 	ah_info->vsi = qp->vsi;
1467 
1468 	if (buf->smac_valid)
1469 		ether_addr_copy(ah_info->mac_addr, buf->smac);
1470 
1471 	if (buf->ipv4) {
1472 		ah_info->ipv4_valid = true;
1473 		iph = (struct iphdr *)buf->iph;
1474 		ah_info->hop_ttl = iph->ttl;
1475 		ah_info->tc_tos = iph->tos;
1476 		ah_info->dest_ip_addr[0] = ntohl(iph->daddr);
1477 		ah_info->src_ip_addr[0] = ntohl(iph->saddr);
1478 	} else {
1479 		ip6h = (struct ipv6hdr *)buf->iph;
1480 		ah_info->hop_ttl = ip6h->hop_limit;
1481 		ah_info->tc_tos = ip6h->priority;
1482 		irdma_copy_ip_ntohl(ah_info->dest_ip_addr,
1483 				    ip6h->daddr.in6_u.u6_addr32);
1484 		irdma_copy_ip_ntohl(ah_info->src_ip_addr,
1485 				    ip6h->saddr.in6_u.u6_addr32);
1486 	}
1487 
1488 	ah_info->dst_arpindex = irdma_arp_table(dev_to_rf(qp->dev),
1489 						ah_info->dest_ip_addr,
1490 						ah_info->ipv4_valid,
1491 						NULL, IRDMA_ARP_RESOLVE);
1492 }
1493 
1494 /**
1495  * irdma_gen1_ieq_update_tcpip_info - update tcpip in the buffer
1496  * @buf: puda to update
1497  * @len: length of buffer
1498  * @seqnum: seq number for tcp
1499  */
1500 static void irdma_gen1_ieq_update_tcpip_info(struct irdma_puda_buf *buf,
1501 					     u16 len, u32 seqnum)
1502 {
1503 	struct tcphdr *tcph;
1504 	struct iphdr *iph;
1505 	u16 iphlen;
1506 	u16 pktsize;
1507 	u8 *addr = buf->mem.va;
1508 
1509 	iphlen = (buf->ipv4) ? 20 : 40;
1510 	iph = (struct iphdr *)(addr + buf->maclen);
1511 	tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1512 	pktsize = len + buf->tcphlen + iphlen;
1513 	iph->tot_len = htons(pktsize);
1514 	tcph->seq = htonl(seqnum);
1515 }
1516 
1517 /**
1518  * irdma_ieq_update_tcpip_info - update tcpip in the buffer
1519  * @buf: puda to update
1520  * @len: length of buffer
1521  * @seqnum: seq number for tcp
1522  */
1523 void irdma_ieq_update_tcpip_info(struct irdma_puda_buf *buf, u16 len,
1524 				 u32 seqnum)
1525 {
1526 	struct tcphdr *tcph;
1527 	u8 *addr;
1528 
1529 	if (buf->vsi->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
1530 		return irdma_gen1_ieq_update_tcpip_info(buf, len, seqnum);
1531 
1532 	addr = buf->mem.va;
1533 	tcph = (struct tcphdr *)addr;
1534 	tcph->seq = htonl(seqnum);
1535 }
1536 
1537 /**
1538  * irdma_gen1_puda_get_tcpip_info - get tcpip info from puda
1539  * buffer
1540  * @info: to get information
1541  * @buf: puda buffer
1542  */
1543 static int irdma_gen1_puda_get_tcpip_info(struct irdma_puda_cmpl_info *info,
1544 					  struct irdma_puda_buf *buf)
1545 {
1546 	struct iphdr *iph;
1547 	struct ipv6hdr *ip6h;
1548 	struct tcphdr *tcph;
1549 	u16 iphlen;
1550 	u16 pkt_len;
1551 	u8 *mem = buf->mem.va;
1552 	struct ethhdr *ethh = buf->mem.va;
1553 
1554 	if (ethh->h_proto == htons(0x8100)) {
1555 		info->vlan_valid = true;
1556 		buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) &
1557 			       VLAN_VID_MASK;
1558 	}
1559 
1560 	buf->maclen = (info->vlan_valid) ? 18 : 14;
1561 	iphlen = (info->l3proto) ? 40 : 20;
1562 	buf->ipv4 = (info->l3proto) ? false : true;
1563 	buf->iph = mem + buf->maclen;
1564 	iph = (struct iphdr *)buf->iph;
1565 	buf->tcph = buf->iph + iphlen;
1566 	tcph = (struct tcphdr *)buf->tcph;
1567 
1568 	if (buf->ipv4) {
1569 		pkt_len = ntohs(iph->tot_len);
1570 	} else {
1571 		ip6h = (struct ipv6hdr *)buf->iph;
1572 		pkt_len = ntohs(ip6h->payload_len) + iphlen;
1573 	}
1574 
1575 	buf->totallen = pkt_len + buf->maclen;
1576 
1577 	if (info->payload_len < buf->totallen) {
1578 		ibdev_dbg(to_ibdev(buf->vsi->dev),
1579 			  "ERR: payload_len = 0x%x totallen expected0x%x\n",
1580 			  info->payload_len, buf->totallen);
1581 		return -EINVAL;
1582 	}
1583 
1584 	buf->tcphlen = tcph->doff << 2;
1585 	buf->datalen = pkt_len - iphlen - buf->tcphlen;
1586 	buf->data = buf->datalen ? buf->tcph + buf->tcphlen : NULL;
1587 	buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1588 	buf->seqnum = ntohl(tcph->seq);
1589 
1590 	return 0;
1591 }
1592 
1593 /**
1594  * irdma_puda_get_tcpip_info - get tcpip info from puda buffer
1595  * @info: to get information
1596  * @buf: puda buffer
1597  */
1598 int irdma_puda_get_tcpip_info(struct irdma_puda_cmpl_info *info,
1599 			      struct irdma_puda_buf *buf)
1600 {
1601 	struct tcphdr *tcph;
1602 	u32 pkt_len;
1603 	u8 *mem;
1604 
1605 	if (buf->vsi->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
1606 		return irdma_gen1_puda_get_tcpip_info(info, buf);
1607 
1608 	mem = buf->mem.va;
1609 	buf->vlan_valid = info->vlan_valid;
1610 	if (info->vlan_valid)
1611 		buf->vlan_id = info->vlan;
1612 
1613 	buf->ipv4 = info->ipv4;
1614 	if (buf->ipv4)
1615 		buf->iph = mem + IRDMA_IPV4_PAD;
1616 	else
1617 		buf->iph = mem;
1618 
1619 	buf->tcph = mem + IRDMA_TCP_OFFSET;
1620 	tcph = (struct tcphdr *)buf->tcph;
1621 	pkt_len = info->payload_len;
1622 	buf->totallen = pkt_len;
1623 	buf->tcphlen = tcph->doff << 2;
1624 	buf->datalen = pkt_len - IRDMA_TCP_OFFSET - buf->tcphlen;
1625 	buf->data = buf->datalen ? buf->tcph + buf->tcphlen : NULL;
1626 	buf->hdrlen = IRDMA_TCP_OFFSET + buf->tcphlen;
1627 	buf->seqnum = ntohl(tcph->seq);
1628 
1629 	if (info->smac_valid) {
1630 		ether_addr_copy(buf->smac, info->smac);
1631 		buf->smac_valid = true;
1632 	}
1633 
1634 	return 0;
1635 }
1636 
1637 /**
1638  * irdma_hw_stats_timeout - Stats timer-handler which updates all HW stats
1639  * @t: timer_list pointer
1640  */
1641 static void irdma_hw_stats_timeout(struct timer_list *t)
1642 {
1643 	struct irdma_vsi_pestat *pf_devstat =
1644 		timer_container_of(pf_devstat, t, stats_timer);
1645 	struct irdma_sc_vsi *sc_vsi = pf_devstat->vsi;
1646 
1647 	if (sc_vsi->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_2)
1648 		irdma_cqp_gather_stats_cmd(sc_vsi->dev, sc_vsi->pestat, false);
1649 	else
1650 		irdma_cqp_gather_stats_gen1(sc_vsi->dev, sc_vsi->pestat);
1651 
1652 	mod_timer(&pf_devstat->stats_timer,
1653 		  jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1654 }
1655 
1656 /**
1657  * irdma_hw_stats_start_timer - Start periodic stats timer
1658  * @vsi: vsi structure pointer
1659  */
1660 void irdma_hw_stats_start_timer(struct irdma_sc_vsi *vsi)
1661 {
1662 	struct irdma_vsi_pestat *devstat = vsi->pestat;
1663 
1664 	timer_setup(&devstat->stats_timer, irdma_hw_stats_timeout, 0);
1665 	mod_timer(&devstat->stats_timer,
1666 		  jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1667 }
1668 
1669 /**
1670  * irdma_hw_stats_stop_timer - Delete periodic stats timer
1671  * @vsi: pointer to vsi structure
1672  */
1673 void irdma_hw_stats_stop_timer(struct irdma_sc_vsi *vsi)
1674 {
1675 	struct irdma_vsi_pestat *devstat = vsi->pestat;
1676 
1677 	timer_delete_sync(&devstat->stats_timer);
1678 }
1679 
1680 /**
1681  * irdma_process_stats - Checking for wrap and update stats
1682  * @pestat: stats structure pointer
1683  */
1684 static inline void irdma_process_stats(struct irdma_vsi_pestat *pestat)
1685 {
1686 	sc_vsi_update_stats(pestat->vsi);
1687 }
1688 
1689 /**
1690  * irdma_cqp_gather_stats_gen1 - Gather stats
1691  * @dev: pointer to device structure
1692  * @pestat: statistics structure
1693  */
1694 void irdma_cqp_gather_stats_gen1(struct irdma_sc_dev *dev,
1695 				 struct irdma_vsi_pestat *pestat)
1696 {
1697 	struct irdma_gather_stats *gather_stats =
1698 		pestat->gather_info.gather_stats_va;
1699 	const struct irdma_hw_stat_map *map = dev->hw_stats_map;
1700 	u16 max_stats_idx = dev->hw_attrs.max_stat_idx;
1701 	u32 stats_inst_offset_32;
1702 	u32 stats_inst_offset_64;
1703 	u64 new_val;
1704 	u16 i;
1705 
1706 	stats_inst_offset_32 = (pestat->gather_info.use_stats_inst) ?
1707 				pestat->gather_info.stats_inst_index :
1708 				pestat->hw->hmc.hmc_fn_id;
1709 	stats_inst_offset_32 *= 4;
1710 	stats_inst_offset_64 = stats_inst_offset_32 * 2;
1711 
1712 	for (i = 0; i < max_stats_idx; i++) {
1713 		if (map[i].bitmask <= IRDMA_MAX_STATS_32)
1714 			new_val = rd32(dev->hw,
1715 				       dev->hw_stats_regs[i] + stats_inst_offset_32);
1716 		else
1717 			new_val = rd64(dev->hw,
1718 				       dev->hw_stats_regs[i] + stats_inst_offset_64);
1719 		gather_stats->val[map[i].byteoff / sizeof(u64)] = new_val;
1720 	}
1721 
1722 	irdma_process_stats(pestat);
1723 }
1724 
1725 /**
1726  * irdma_process_cqp_stats - Checking for wrap and update stats
1727  * @cqp_request: cqp_request structure pointer
1728  */
1729 static void irdma_process_cqp_stats(struct irdma_cqp_request *cqp_request)
1730 {
1731 	struct irdma_vsi_pestat *pestat = cqp_request->param;
1732 
1733 	irdma_process_stats(pestat);
1734 }
1735 
1736 /**
1737  * irdma_cqp_gather_stats_cmd - Gather stats
1738  * @dev: pointer to device structure
1739  * @pestat: pointer to stats info
1740  * @wait: flag to wait or not wait for stats
1741  */
1742 int irdma_cqp_gather_stats_cmd(struct irdma_sc_dev *dev,
1743 			       struct irdma_vsi_pestat *pestat, bool wait)
1744 
1745 {
1746 	struct irdma_pci_f *rf = dev_to_rf(dev);
1747 	struct irdma_cqp *iwcqp = &rf->cqp;
1748 	struct irdma_cqp_request *cqp_request;
1749 	struct cqp_cmds_info *cqp_info;
1750 	int status;
1751 
1752 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, wait);
1753 	if (!cqp_request)
1754 		return -ENOMEM;
1755 
1756 	cqp_info = &cqp_request->info;
1757 	cqp_info->cqp_cmd = IRDMA_OP_STATS_GATHER;
1758 	cqp_info->post_sq = 1;
1759 	cqp_info->in.u.stats_gather.info = pestat->gather_info;
1760 	cqp_info->in.u.stats_gather.scratch = (uintptr_t)cqp_request;
1761 	cqp_info->in.u.stats_gather.cqp = &rf->cqp.sc_cqp;
1762 	cqp_request->param = pestat;
1763 	if (!wait)
1764 		cqp_request->callback_fcn = irdma_process_cqp_stats;
1765 	status = irdma_handle_cqp_op(rf, cqp_request);
1766 	if (wait)
1767 		irdma_process_stats(pestat);
1768 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1769 
1770 	return status;
1771 }
1772 
1773 /**
1774  * irdma_cqp_stats_inst_cmd - Allocate/free stats instance
1775  * @vsi: pointer to vsi structure
1776  * @cmd: command to allocate or free
1777  * @stats_info: pointer to allocate stats info
1778  */
1779 int irdma_cqp_stats_inst_cmd(struct irdma_sc_vsi *vsi, u8 cmd,
1780 			     struct irdma_stats_inst_info *stats_info)
1781 {
1782 	struct irdma_pci_f *rf = dev_to_rf(vsi->dev);
1783 	struct irdma_cqp *iwcqp = &rf->cqp;
1784 	struct irdma_cqp_request *cqp_request;
1785 	struct cqp_cmds_info *cqp_info;
1786 	int status;
1787 	bool wait = false;
1788 
1789 	if (cmd == IRDMA_OP_STATS_ALLOCATE)
1790 		wait = true;
1791 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, wait);
1792 	if (!cqp_request)
1793 		return -ENOMEM;
1794 
1795 	cqp_info = &cqp_request->info;
1796 	cqp_info->cqp_cmd = cmd;
1797 	cqp_info->post_sq = 1;
1798 	cqp_info->in.u.stats_manage.info = *stats_info;
1799 	cqp_info->in.u.stats_manage.scratch = (uintptr_t)cqp_request;
1800 	cqp_info->in.u.stats_manage.cqp = &rf->cqp.sc_cqp;
1801 	status = irdma_handle_cqp_op(rf, cqp_request);
1802 	if (wait)
1803 		stats_info->stats_idx = cqp_request->compl_info.op_ret_val;
1804 	irdma_put_cqp_request(iwcqp, cqp_request);
1805 
1806 	return status;
1807 }
1808 
1809 /**
1810  * irdma_cqp_ceq_cmd - Create/Destroy CEQ's after CEQ 0
1811  * @dev: pointer to device info
1812  * @sc_ceq: pointer to ceq structure
1813  * @op: Create or Destroy
1814  */
1815 int irdma_cqp_ceq_cmd(struct irdma_sc_dev *dev, struct irdma_sc_ceq *sc_ceq,
1816 		      u8 op)
1817 {
1818 	struct irdma_cqp_request *cqp_request;
1819 	struct cqp_cmds_info *cqp_info;
1820 	struct irdma_pci_f *rf = dev_to_rf(dev);
1821 	int status;
1822 
1823 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, true);
1824 	if (!cqp_request)
1825 		return -ENOMEM;
1826 
1827 	cqp_info = &cqp_request->info;
1828 	cqp_info->post_sq = 1;
1829 	cqp_info->cqp_cmd = op;
1830 	cqp_info->in.u.ceq_create.ceq = sc_ceq;
1831 	cqp_info->in.u.ceq_create.scratch = (uintptr_t)cqp_request;
1832 
1833 	status = irdma_handle_cqp_op(rf, cqp_request);
1834 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1835 
1836 	return status;
1837 }
1838 
1839 /**
1840  * irdma_cqp_aeq_cmd - Create/Destroy AEQ
1841  * @dev: pointer to device info
1842  * @sc_aeq: pointer to aeq structure
1843  * @op: Create or Destroy
1844  */
1845 int irdma_cqp_aeq_cmd(struct irdma_sc_dev *dev, struct irdma_sc_aeq *sc_aeq,
1846 		      u8 op)
1847 {
1848 	struct irdma_cqp_request *cqp_request;
1849 	struct cqp_cmds_info *cqp_info;
1850 	struct irdma_pci_f *rf = dev_to_rf(dev);
1851 	int status;
1852 
1853 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, true);
1854 	if (!cqp_request)
1855 		return -ENOMEM;
1856 
1857 	cqp_info = &cqp_request->info;
1858 	cqp_info->post_sq = 1;
1859 	cqp_info->cqp_cmd = op;
1860 	cqp_info->in.u.aeq_create.aeq = sc_aeq;
1861 	cqp_info->in.u.aeq_create.scratch = (uintptr_t)cqp_request;
1862 
1863 	status = irdma_handle_cqp_op(rf, cqp_request);
1864 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1865 
1866 	return status;
1867 }
1868 
1869 /**
1870  * irdma_cqp_ws_node_cmd - Add/modify/delete ws node
1871  * @dev: pointer to device structure
1872  * @cmd: Add, modify or delete
1873  * @node_info: pointer to ws node info
1874  */
1875 int irdma_cqp_ws_node_cmd(struct irdma_sc_dev *dev, u8 cmd,
1876 			  struct irdma_ws_node_info *node_info)
1877 {
1878 	struct irdma_pci_f *rf = dev_to_rf(dev);
1879 	struct irdma_cqp *iwcqp = &rf->cqp;
1880 	struct irdma_sc_cqp *cqp = &iwcqp->sc_cqp;
1881 	struct irdma_cqp_request *cqp_request;
1882 	struct cqp_cmds_info *cqp_info;
1883 	int status;
1884 	bool poll;
1885 
1886 	if (!rf->sc_dev.ceq_valid)
1887 		poll = true;
1888 	else
1889 		poll = false;
1890 
1891 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, !poll);
1892 	if (!cqp_request)
1893 		return -ENOMEM;
1894 
1895 	cqp_info = &cqp_request->info;
1896 	cqp_info->cqp_cmd = cmd;
1897 	cqp_info->post_sq = 1;
1898 	cqp_info->in.u.ws_node.info = *node_info;
1899 	cqp_info->in.u.ws_node.cqp = cqp;
1900 	cqp_info->in.u.ws_node.scratch = (uintptr_t)cqp_request;
1901 	status = irdma_handle_cqp_op(rf, cqp_request);
1902 	if (status)
1903 		goto exit;
1904 
1905 	if (poll) {
1906 		struct irdma_ccq_cqe_info compl_info;
1907 
1908 		status = irdma_sc_poll_for_cqp_op_done(cqp, IRDMA_CQP_OP_WORK_SCHED_NODE,
1909 						       &compl_info);
1910 		node_info->qs_handle = compl_info.op_ret_val;
1911 		ibdev_dbg(&rf->iwdev->ibdev, "DCB: opcode=%d, compl_info.retval=%d\n",
1912 			  compl_info.op_code, compl_info.op_ret_val);
1913 	} else {
1914 		node_info->qs_handle = cqp_request->compl_info.op_ret_val;
1915 	}
1916 
1917 exit:
1918 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1919 
1920 	return status;
1921 }
1922 
1923 /**
1924  * irdma_ah_cqp_op - perform an AH cqp operation
1925  * @rf: RDMA PCI function
1926  * @sc_ah: address handle
1927  * @cmd: AH operation
1928  * @wait: wait if true
1929  * @callback_fcn: Callback function on CQP op completion
1930  * @cb_param: parameter for callback function
1931  *
1932  * returns errno
1933  */
1934 int irdma_ah_cqp_op(struct irdma_pci_f *rf, struct irdma_sc_ah *sc_ah, u8 cmd,
1935 		    bool wait,
1936 		    void (*callback_fcn)(struct irdma_cqp_request *),
1937 		    void *cb_param)
1938 {
1939 	struct irdma_cqp_request *cqp_request;
1940 	struct cqp_cmds_info *cqp_info;
1941 	int status;
1942 
1943 	if (cmd != IRDMA_OP_AH_CREATE && cmd != IRDMA_OP_AH_DESTROY)
1944 		return -EINVAL;
1945 
1946 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, wait);
1947 	if (!cqp_request)
1948 		return -ENOMEM;
1949 
1950 	cqp_info = &cqp_request->info;
1951 	cqp_info->cqp_cmd = cmd;
1952 	cqp_info->post_sq = 1;
1953 	if (cmd == IRDMA_OP_AH_CREATE) {
1954 		cqp_info->in.u.ah_create.info = sc_ah->ah_info;
1955 		cqp_info->in.u.ah_create.scratch = (uintptr_t)cqp_request;
1956 		cqp_info->in.u.ah_create.cqp = &rf->cqp.sc_cqp;
1957 	} else if (cmd == IRDMA_OP_AH_DESTROY) {
1958 		cqp_info->in.u.ah_destroy.info = sc_ah->ah_info;
1959 		cqp_info->in.u.ah_destroy.scratch = (uintptr_t)cqp_request;
1960 		cqp_info->in.u.ah_destroy.cqp = &rf->cqp.sc_cqp;
1961 	}
1962 
1963 	if (!wait) {
1964 		cqp_request->callback_fcn = callback_fcn;
1965 		cqp_request->param = cb_param;
1966 	}
1967 	status = irdma_handle_cqp_op(rf, cqp_request);
1968 	irdma_put_cqp_request(&rf->cqp, cqp_request);
1969 
1970 	if (status)
1971 		return -ENOMEM;
1972 
1973 	if (wait)
1974 		sc_ah->ah_info.ah_valid = (cmd == IRDMA_OP_AH_CREATE);
1975 
1976 	return 0;
1977 }
1978 
1979 /**
1980  * irdma_ieq_ah_cb - callback after creation of AH for IEQ
1981  * @cqp_request: pointer to cqp_request of create AH
1982  */
1983 static void irdma_ieq_ah_cb(struct irdma_cqp_request *cqp_request)
1984 {
1985 	struct irdma_sc_qp *qp = cqp_request->param;
1986 	struct irdma_sc_ah *sc_ah = qp->pfpdu.ah;
1987 	unsigned long flags;
1988 
1989 	spin_lock_irqsave(&qp->pfpdu.lock, flags);
1990 	if (!cqp_request->compl_info.op_ret_val) {
1991 		sc_ah->ah_info.ah_valid = true;
1992 		irdma_ieq_process_fpdus(qp, qp->vsi->ieq);
1993 	} else {
1994 		sc_ah->ah_info.ah_valid = false;
1995 		irdma_ieq_cleanup_qp(qp->vsi->ieq, qp);
1996 	}
1997 	spin_unlock_irqrestore(&qp->pfpdu.lock, flags);
1998 }
1999 
2000 /**
2001  * irdma_ilq_ah_cb - callback after creation of AH for ILQ
2002  * @cqp_request: pointer to cqp_request of create AH
2003  */
2004 static void irdma_ilq_ah_cb(struct irdma_cqp_request *cqp_request)
2005 {
2006 	struct irdma_cm_node *cm_node = cqp_request->param;
2007 	struct irdma_sc_ah *sc_ah = cm_node->ah;
2008 
2009 	sc_ah->ah_info.ah_valid = !cqp_request->compl_info.op_ret_val;
2010 	irdma_add_conn_est_qh(cm_node);
2011 }
2012 
2013 /**
2014  * irdma_puda_create_ah - create AH for ILQ/IEQ qp's
2015  * @dev: device pointer
2016  * @ah_info: Address handle info
2017  * @wait: When true will wait for operation to complete
2018  * @type: ILQ/IEQ
2019  * @cb_param: Callback param when not waiting
2020  * @ah_ret: Returned pointer to address handle if created
2021  *
2022  */
2023 int irdma_puda_create_ah(struct irdma_sc_dev *dev,
2024 			 struct irdma_ah_info *ah_info, bool wait,
2025 			 enum puda_rsrc_type type, void *cb_param,
2026 			 struct irdma_sc_ah **ah_ret)
2027 {
2028 	struct irdma_sc_ah *ah;
2029 	struct irdma_pci_f *rf = dev_to_rf(dev);
2030 	int err;
2031 
2032 	ah = kzalloc_obj(*ah, GFP_ATOMIC);
2033 	*ah_ret = ah;
2034 	if (!ah)
2035 		return -ENOMEM;
2036 
2037 	err = irdma_alloc_rsrc(rf, rf->allocated_ahs, rf->max_ah,
2038 			       &ah_info->ah_idx, &rf->next_ah);
2039 	if (err)
2040 		goto err_free;
2041 
2042 	ah->dev = dev;
2043 	ah->ah_info = *ah_info;
2044 
2045 	if (type == IRDMA_PUDA_RSRC_TYPE_ILQ)
2046 		err = irdma_ah_cqp_op(rf, ah, IRDMA_OP_AH_CREATE, wait,
2047 				      irdma_ilq_ah_cb, cb_param);
2048 	else
2049 		err = irdma_ah_cqp_op(rf, ah, IRDMA_OP_AH_CREATE, wait,
2050 				      irdma_ieq_ah_cb, cb_param);
2051 
2052 	if (err)
2053 		goto error;
2054 	return 0;
2055 
2056 error:
2057 	irdma_free_rsrc(rf, rf->allocated_ahs, ah->ah_info.ah_idx);
2058 err_free:
2059 	kfree(ah);
2060 	*ah_ret = NULL;
2061 	return -ENOMEM;
2062 }
2063 
2064 /**
2065  * irdma_puda_free_ah - free a puda address handle
2066  * @dev: device pointer
2067  * @ah: The address handle to free
2068  */
2069 void irdma_puda_free_ah(struct irdma_sc_dev *dev, struct irdma_sc_ah *ah)
2070 {
2071 	struct irdma_pci_f *rf = dev_to_rf(dev);
2072 
2073 	if (!ah)
2074 		return;
2075 
2076 	if (ah->ah_info.ah_valid) {
2077 		irdma_ah_cqp_op(rf, ah, IRDMA_OP_AH_DESTROY, false, NULL, NULL);
2078 		irdma_free_rsrc(rf, rf->allocated_ahs, ah->ah_info.ah_idx);
2079 	}
2080 
2081 	kfree(ah);
2082 }
2083 
2084 /**
2085  * irdma_gsi_ud_qp_ah_cb - callback after creation of AH for GSI/ID QP
2086  * @cqp_request: pointer to cqp_request of create AH
2087  */
2088 void irdma_gsi_ud_qp_ah_cb(struct irdma_cqp_request *cqp_request)
2089 {
2090 	struct irdma_sc_ah *sc_ah = cqp_request->param;
2091 
2092 	if (!cqp_request->compl_info.op_ret_val)
2093 		sc_ah->ah_info.ah_valid = true;
2094 	else
2095 		sc_ah->ah_info.ah_valid = false;
2096 }
2097 
2098 /**
2099  * irdma_prm_add_pble_mem - add moemory to pble resources
2100  * @pprm: pble resource manager
2101  * @pchunk: chunk of memory to add
2102  */
2103 int irdma_prm_add_pble_mem(struct irdma_pble_prm *pprm,
2104 			   struct irdma_chunk *pchunk)
2105 {
2106 	u64 sizeofbitmap;
2107 
2108 	if (pchunk->size & 0xfff)
2109 		return -EINVAL;
2110 
2111 	sizeofbitmap = (u64)pchunk->size >> pprm->pble_shift;
2112 
2113 	pchunk->bitmapbuf = bitmap_zalloc(sizeofbitmap, GFP_KERNEL);
2114 	if (!pchunk->bitmapbuf)
2115 		return -ENOMEM;
2116 
2117 	pchunk->sizeofbitmap = sizeofbitmap;
2118 	/* each pble is 8 bytes hence shift by 3 */
2119 	pprm->total_pble_alloc += pchunk->size >> 3;
2120 	pprm->free_pble_cnt += pchunk->size >> 3;
2121 
2122 	return 0;
2123 }
2124 
2125 /**
2126  * irdma_prm_get_pbles - get pble's from prm
2127  * @pprm: pble resource manager
2128  * @chunkinfo: nformation about chunk where pble's were acquired
2129  * @mem_size: size of pble memory needed
2130  * @vaddr: returns virtual address of pble memory
2131  * @fpm_addr: returns fpm address of pble memory
2132  */
2133 int irdma_prm_get_pbles(struct irdma_pble_prm *pprm,
2134 			struct irdma_pble_chunkinfo *chunkinfo, u64 mem_size,
2135 			u64 **vaddr, u64 *fpm_addr)
2136 {
2137 	u64 bits_needed;
2138 	u64 bit_idx = PBLE_INVALID_IDX;
2139 	struct irdma_chunk *pchunk = NULL;
2140 	struct list_head *chunk_entry = pprm->clist.next;
2141 	u32 offset;
2142 	unsigned long flags;
2143 	*vaddr = NULL;
2144 	*fpm_addr = 0;
2145 
2146 	bits_needed = DIV_ROUND_UP_ULL(mem_size, BIT_ULL(pprm->pble_shift));
2147 
2148 	spin_lock_irqsave(&pprm->prm_lock, flags);
2149 	while (chunk_entry != &pprm->clist) {
2150 		pchunk = (struct irdma_chunk *)chunk_entry;
2151 		bit_idx = bitmap_find_next_zero_area(pchunk->bitmapbuf,
2152 						     pchunk->sizeofbitmap, 0,
2153 						     bits_needed, 0);
2154 		if (bit_idx < pchunk->sizeofbitmap)
2155 			break;
2156 
2157 		/* list.next used macro */
2158 		chunk_entry = pchunk->list.next;
2159 	}
2160 
2161 	if (!pchunk || bit_idx >= pchunk->sizeofbitmap) {
2162 		spin_unlock_irqrestore(&pprm->prm_lock, flags);
2163 		return -ENOMEM;
2164 	}
2165 
2166 	bitmap_set(pchunk->bitmapbuf, bit_idx, bits_needed);
2167 	offset = bit_idx << pprm->pble_shift;
2168 	*vaddr = pchunk->vaddr + offset;
2169 	*fpm_addr = pchunk->fpm_addr + offset;
2170 
2171 	chunkinfo->pchunk = pchunk;
2172 	chunkinfo->bit_idx = bit_idx;
2173 	chunkinfo->bits_used = bits_needed;
2174 	/* 3 is sizeof pble divide */
2175 	pprm->free_pble_cnt -= chunkinfo->bits_used << (pprm->pble_shift - 3);
2176 	spin_unlock_irqrestore(&pprm->prm_lock, flags);
2177 
2178 	return 0;
2179 }
2180 
2181 /**
2182  * irdma_prm_return_pbles - return pbles back to prm
2183  * @pprm: pble resource manager
2184  * @chunkinfo: chunk where pble's were acquired and to be freed
2185  */
2186 void irdma_prm_return_pbles(struct irdma_pble_prm *pprm,
2187 			    struct irdma_pble_chunkinfo *chunkinfo)
2188 {
2189 	unsigned long flags;
2190 
2191 	spin_lock_irqsave(&pprm->prm_lock, flags);
2192 	pprm->free_pble_cnt += chunkinfo->bits_used << (pprm->pble_shift - 3);
2193 	bitmap_clear(chunkinfo->pchunk->bitmapbuf, chunkinfo->bit_idx,
2194 		     chunkinfo->bits_used);
2195 	spin_unlock_irqrestore(&pprm->prm_lock, flags);
2196 }
2197 
2198 int irdma_map_vm_page_list(struct irdma_hw *hw, void *va, dma_addr_t *pg_dma,
2199 			   u32 pg_cnt)
2200 {
2201 	struct page *vm_page;
2202 	int i;
2203 	u8 *addr;
2204 
2205 	addr = (u8 *)(uintptr_t)va;
2206 	for (i = 0; i < pg_cnt; i++) {
2207 		vm_page = vmalloc_to_page(addr);
2208 		if (!vm_page)
2209 			goto err;
2210 
2211 		pg_dma[i] = dma_map_page(hw->device, vm_page, 0, PAGE_SIZE,
2212 					 DMA_BIDIRECTIONAL);
2213 		if (dma_mapping_error(hw->device, pg_dma[i]))
2214 			goto err;
2215 
2216 		addr += PAGE_SIZE;
2217 	}
2218 
2219 	return 0;
2220 
2221 err:
2222 	irdma_unmap_vm_page_list(hw, pg_dma, i);
2223 	return -ENOMEM;
2224 }
2225 
2226 void irdma_unmap_vm_page_list(struct irdma_hw *hw, dma_addr_t *pg_dma, u32 pg_cnt)
2227 {
2228 	int i;
2229 
2230 	for (i = 0; i < pg_cnt; i++)
2231 		dma_unmap_page(hw->device, pg_dma[i], PAGE_SIZE, DMA_BIDIRECTIONAL);
2232 }
2233 
2234 /**
2235  * irdma_pble_free_paged_mem - free virtual paged memory
2236  * @chunk: chunk to free with paged memory
2237  */
2238 void irdma_pble_free_paged_mem(struct irdma_chunk *chunk)
2239 {
2240 	if (!chunk->pg_cnt)
2241 		goto done;
2242 
2243 	irdma_unmap_vm_page_list(chunk->dev->hw, chunk->dmainfo.dmaaddrs,
2244 				 chunk->pg_cnt);
2245 
2246 done:
2247 	kvfree(chunk->dmainfo.dmaaddrs);
2248 	chunk->dmainfo.dmaaddrs = NULL;
2249 	vfree(chunk->vaddr);
2250 	chunk->vaddr = NULL;
2251 	chunk->type = 0;
2252 }
2253 
2254 /**
2255  * irdma_pble_get_paged_mem -allocate paged memory for pbles
2256  * @chunk: chunk to add for paged memory
2257  * @pg_cnt: number of pages needed
2258  */
2259 int irdma_pble_get_paged_mem(struct irdma_chunk *chunk, u32 pg_cnt)
2260 {
2261 	u32 size;
2262 	void *va;
2263 
2264 	chunk->dmainfo.dmaaddrs = kvzalloc(pg_cnt << 3, GFP_KERNEL);
2265 	if (!chunk->dmainfo.dmaaddrs)
2266 		return -ENOMEM;
2267 
2268 	size = PAGE_SIZE * pg_cnt;
2269 	va = vmalloc(size);
2270 	if (!va)
2271 		goto err;
2272 
2273 	if (irdma_map_vm_page_list(chunk->dev->hw, va, chunk->dmainfo.dmaaddrs,
2274 				   pg_cnt)) {
2275 		vfree(va);
2276 		goto err;
2277 	}
2278 	chunk->vaddr = va;
2279 	chunk->size = size;
2280 	chunk->pg_cnt = pg_cnt;
2281 	chunk->type = PBLE_SD_PAGED;
2282 
2283 	return 0;
2284 err:
2285 	kvfree(chunk->dmainfo.dmaaddrs);
2286 	chunk->dmainfo.dmaaddrs = NULL;
2287 
2288 	return -ENOMEM;
2289 }
2290 
2291 /**
2292  * irdma_alloc_ws_node_id - Allocate a tx scheduler node ID
2293  * @dev: device pointer
2294  */
2295 u16 irdma_alloc_ws_node_id(struct irdma_sc_dev *dev)
2296 {
2297 	struct irdma_pci_f *rf = dev_to_rf(dev);
2298 	u32 next = 1;
2299 	u32 node_id;
2300 
2301 	if (irdma_alloc_rsrc(rf, rf->allocated_ws_nodes, rf->max_ws_node_id,
2302 			     &node_id, &next))
2303 		return IRDMA_WS_NODE_INVALID;
2304 
2305 	return (u16)node_id;
2306 }
2307 
2308 /**
2309  * irdma_free_ws_node_id - Free a tx scheduler node ID
2310  * @dev: device pointer
2311  * @node_id: Work scheduler node ID
2312  */
2313 void irdma_free_ws_node_id(struct irdma_sc_dev *dev, u16 node_id)
2314 {
2315 	struct irdma_pci_f *rf = dev_to_rf(dev);
2316 
2317 	irdma_free_rsrc(rf, rf->allocated_ws_nodes, (u32)node_id);
2318 }
2319 
2320 /**
2321  * irdma_modify_qp_to_err - Modify a QP to error
2322  * @sc_qp: qp structure
2323  */
2324 void irdma_modify_qp_to_err(struct irdma_sc_qp *sc_qp)
2325 {
2326 	struct irdma_qp *qp = sc_qp->qp_uk.back_qp;
2327 	struct ib_qp_attr attr;
2328 
2329 	attr.qp_state = IB_QPS_ERR;
2330 
2331 	if (rdma_protocol_roce(qp->ibqp.device, 1))
2332 		irdma_modify_qp_roce(&qp->ibqp, &attr, IB_QP_STATE, NULL);
2333 	else
2334 		irdma_modify_qp(&qp->ibqp, &attr, IB_QP_STATE, NULL);
2335 }
2336 
2337 void irdma_ib_qp_event(struct irdma_qp *iwqp, enum irdma_qp_event_type event)
2338 {
2339 	struct ib_event ibevent;
2340 
2341 	if (!iwqp->ibqp.event_handler)
2342 		return;
2343 
2344 	switch (event) {
2345 	case IRDMA_QP_EVENT_CATASTROPHIC:
2346 		ibevent.event = IB_EVENT_QP_FATAL;
2347 		break;
2348 	case IRDMA_QP_EVENT_ACCESS_ERR:
2349 		ibevent.event = IB_EVENT_QP_ACCESS_ERR;
2350 		break;
2351 	case IRDMA_QP_EVENT_REQ_ERR:
2352 		ibevent.event = IB_EVENT_QP_REQ_ERR;
2353 		break;
2354 	}
2355 	ibevent.device = iwqp->ibqp.device;
2356 	ibevent.element.qp = &iwqp->ibqp;
2357 	iwqp->ibqp.event_handler(&ibevent, iwqp->ibqp.qp_context);
2358 }
2359 
2360 void irdma_remove_cmpls_list(struct irdma_cq *iwcq)
2361 {
2362 	struct irdma_cmpl_gen *cmpl_node;
2363 	struct list_head *tmp_node, *list_node;
2364 
2365 	list_for_each_safe (list_node, tmp_node, &iwcq->cmpl_generated) {
2366 		cmpl_node = list_entry(list_node, struct irdma_cmpl_gen, list);
2367 		list_del(&cmpl_node->list);
2368 		kfree(cmpl_node);
2369 	}
2370 }
2371 
2372 int irdma_generated_cmpls(struct irdma_cq *iwcq, struct irdma_cq_poll_info *cq_poll_info)
2373 {
2374 	struct irdma_cmpl_gen *cmpl;
2375 
2376 	if (list_empty(&iwcq->cmpl_generated))
2377 		return -ENOENT;
2378 	cmpl = list_first_entry_or_null(&iwcq->cmpl_generated, struct irdma_cmpl_gen, list);
2379 	list_del(&cmpl->list);
2380 	memcpy(cq_poll_info, &cmpl->cpi, sizeof(*cq_poll_info));
2381 	kfree(cmpl);
2382 
2383 	ibdev_dbg(iwcq->ibcq.device,
2384 		  "VERBS: %s: Poll artificially generated completion for QP 0x%X, op %u, wr_id=0x%llx\n",
2385 		  __func__, cq_poll_info->qp_id, cq_poll_info->op_type,
2386 		  cq_poll_info->wr_id);
2387 
2388 	return 0;
2389 }
2390 
2391 /**
2392  * irdma_set_cpi_common_values - fill in values for polling info struct
2393  * @cpi: resulting structure of cq_poll_info type
2394  * @qp: QPair
2395  * @qp_num: id of the QP
2396  */
2397 static void irdma_set_cpi_common_values(struct irdma_cq_poll_info *cpi,
2398 					struct irdma_qp_uk *qp, u32 qp_num)
2399 {
2400 	cpi->comp_status = IRDMA_COMPL_STATUS_FLUSHED;
2401 	cpi->error = true;
2402 	cpi->major_err = IRDMA_FLUSH_MAJOR_ERR;
2403 	cpi->minor_err = FLUSH_GENERAL_ERR;
2404 	cpi->qp_handle = (irdma_qp_handle)(uintptr_t)qp;
2405 	cpi->qp_id = qp_num;
2406 }
2407 
2408 static inline void irdma_comp_handler(struct irdma_cq *cq)
2409 {
2410 	if (!cq->ibcq.comp_handler)
2411 		return;
2412 	if (atomic_cmpxchg(&cq->armed, 1, 0))
2413 		cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
2414 }
2415 
2416 void irdma_generate_flush_completions(struct irdma_qp *iwqp)
2417 {
2418 	struct irdma_qp_uk *qp = &iwqp->sc_qp.qp_uk;
2419 	struct irdma_ring *sq_ring = &qp->sq_ring;
2420 	struct irdma_ring *rq_ring = &qp->rq_ring;
2421 	struct irdma_cq *iwscq = iwqp->iwscq;
2422 	struct irdma_cq *iwrcq = iwqp->iwrcq;
2423 	struct irdma_cmpl_gen *cmpl;
2424 	__le64 *sw_wqe;
2425 	u64 wqe_qword;
2426 	u32 wqe_idx;
2427 	bool compl_generated = false;
2428 	unsigned long flags1;
2429 
2430 	spin_lock_irqsave(&iwscq->lock, flags1);
2431 	if (irdma_uk_cq_empty(&iwscq->sc_cq.cq_uk)) {
2432 		unsigned long flags2;
2433 
2434 		spin_lock_irqsave(&iwqp->lock, flags2);
2435 		while (IRDMA_RING_MORE_WORK(*sq_ring)) {
2436 			cmpl = kzalloc_obj(*cmpl, GFP_ATOMIC);
2437 			if (!cmpl) {
2438 				spin_unlock_irqrestore(&iwqp->lock, flags2);
2439 				spin_unlock_irqrestore(&iwscq->lock, flags1);
2440 				return;
2441 			}
2442 
2443 			wqe_idx = sq_ring->tail;
2444 			irdma_set_cpi_common_values(&cmpl->cpi, qp, qp->qp_id);
2445 
2446 			cmpl->cpi.wr_id = qp->sq_wrtrk_array[wqe_idx].wrid;
2447 			sw_wqe = qp->sq_base[wqe_idx].elem;
2448 			get_64bit_val(sw_wqe, 24, &wqe_qword);
2449 			cmpl->cpi.op_type = (u8)FIELD_GET(IRDMAQPSQ_OPCODE, wqe_qword);
2450 			cmpl->cpi.q_type = IRDMA_CQE_QTYPE_SQ;
2451 			/* remove the SQ WR by moving SQ tail*/
2452 			IRDMA_RING_SET_TAIL(*sq_ring,
2453 				sq_ring->tail + qp->sq_wrtrk_array[sq_ring->tail].quanta);
2454 			if (cmpl->cpi.op_type == IRDMAQP_OP_NOP) {
2455 				kfree(cmpl);
2456 				continue;
2457 			}
2458 			ibdev_dbg(iwscq->ibcq.device,
2459 				  "DEV: %s: adding wr_id = 0x%llx SQ Completion to list qp_id=%d\n",
2460 				  __func__, cmpl->cpi.wr_id, qp->qp_id);
2461 			list_add_tail(&cmpl->list, &iwscq->cmpl_generated);
2462 			compl_generated = true;
2463 		}
2464 		spin_unlock_irqrestore(&iwqp->lock, flags2);
2465 		spin_unlock_irqrestore(&iwscq->lock, flags1);
2466 		if (compl_generated)
2467 			irdma_comp_handler(iwscq);
2468 	} else {
2469 		spin_unlock_irqrestore(&iwscq->lock, flags1);
2470 		mod_delayed_work(iwqp->iwdev->cleanup_wq, &iwqp->dwork_flush,
2471 				 msecs_to_jiffies(IRDMA_FLUSH_DELAY_MS));
2472 	}
2473 
2474 	spin_lock_irqsave(&iwrcq->lock, flags1);
2475 	if (irdma_uk_cq_empty(&iwrcq->sc_cq.cq_uk)) {
2476 		unsigned long flags2;
2477 
2478 		spin_lock_irqsave(&iwqp->lock, flags2);
2479 		while (IRDMA_RING_MORE_WORK(*rq_ring)) {
2480 			cmpl = kzalloc_obj(*cmpl, GFP_ATOMIC);
2481 			if (!cmpl) {
2482 				spin_unlock_irqrestore(&iwqp->lock, flags2);
2483 				spin_unlock_irqrestore(&iwrcq->lock, flags1);
2484 				return;
2485 			}
2486 
2487 			wqe_idx = rq_ring->tail;
2488 			irdma_set_cpi_common_values(&cmpl->cpi, qp, qp->qp_id);
2489 
2490 			cmpl->cpi.wr_id = qp->rq_wrid_array[wqe_idx];
2491 			cmpl->cpi.op_type = IRDMA_OP_TYPE_REC;
2492 			cmpl->cpi.q_type = IRDMA_CQE_QTYPE_RQ;
2493 			/* remove the RQ WR by moving RQ tail */
2494 			IRDMA_RING_SET_TAIL(*rq_ring, rq_ring->tail + 1);
2495 			ibdev_dbg(iwrcq->ibcq.device,
2496 				  "DEV: %s: adding wr_id = 0x%llx RQ Completion to list qp_id=%d, wqe_idx=%d\n",
2497 				  __func__, cmpl->cpi.wr_id, qp->qp_id,
2498 				  wqe_idx);
2499 			list_add_tail(&cmpl->list, &iwrcq->cmpl_generated);
2500 
2501 			compl_generated = true;
2502 		}
2503 		spin_unlock_irqrestore(&iwqp->lock, flags2);
2504 		spin_unlock_irqrestore(&iwrcq->lock, flags1);
2505 		if (compl_generated)
2506 			irdma_comp_handler(iwrcq);
2507 	} else {
2508 		spin_unlock_irqrestore(&iwrcq->lock, flags1);
2509 		mod_delayed_work(iwqp->iwdev->cleanup_wq, &iwqp->dwork_flush,
2510 				 msecs_to_jiffies(IRDMA_FLUSH_DELAY_MS));
2511 	}
2512 }
2513