• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2 *
3 * Copyright (c) 2015-2016 Intel Corporation.  All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenFabrics.org BSD license below:
10 *
11 *   Redistribution and use in source and binary forms, with or
12 *   without modification, are permitted provided that the following
13 *   conditions are met:
14 *
15 *    - Redistributions of source code must retain the above
16 *	copyright notice, this list of conditions and the following
17 *	disclaimer.
18 *
19 *    - Redistributions in binary form must reproduce the above
20 *	copyright notice, this list of conditions and the following
21 *	disclaimer in the documentation and/or other materials
22 *	provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 *******************************************************************************/
34 
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ethtool.h>
40 #include <linux/mii.h>
41 #include <linux/if_vlan.h>
42 #include <linux/crc32.h>
43 #include <linux/in.h>
44 #include <linux/ip.h>
45 #include <linux/tcp.h>
46 #include <linux/init.h>
47 #include <linux/io.h>
48 #include <asm/irq.h>
49 #include <asm/byteorder.h>
50 #include <net/netevent.h>
51 #include <net/neighbour.h>
52 #include "i40iw.h"
53 
54 /**
55  * i40iw_arp_table - manage arp table
56  * @iwdev: iwarp device
57  * @ip_addr: ip address for device
58  * @mac_addr: mac address ptr
59  * @action: modify, delete or add
60  */
i40iw_arp_table(struct i40iw_device * iwdev,u32 * ip_addr,bool ipv4,u8 * mac_addr,u32 action)61 int i40iw_arp_table(struct i40iw_device *iwdev,
62 		    u32 *ip_addr,
63 		    bool ipv4,
64 		    u8 *mac_addr,
65 		    u32 action)
66 {
67 	int arp_index;
68 	int err;
69 	u32 ip[4];
70 
71 	if (ipv4) {
72 		memset(ip, 0, sizeof(ip));
73 		ip[0] = *ip_addr;
74 	} else {
75 		memcpy(ip, ip_addr, sizeof(ip));
76 	}
77 
78 	for (arp_index = 0; (u32)arp_index < iwdev->arp_table_size; arp_index++)
79 		if (memcmp(iwdev->arp_table[arp_index].ip_addr, ip, sizeof(ip)) == 0)
80 			break;
81 	switch (action) {
82 	case I40IW_ARP_ADD:
83 		if (arp_index != iwdev->arp_table_size)
84 			return -1;
85 
86 		arp_index = 0;
87 		err = i40iw_alloc_resource(iwdev, iwdev->allocated_arps,
88 					   iwdev->arp_table_size,
89 					   (u32 *)&arp_index,
90 					   &iwdev->next_arp_index);
91 
92 		if (err)
93 			return err;
94 
95 		memcpy(iwdev->arp_table[arp_index].ip_addr, ip, sizeof(ip));
96 		ether_addr_copy(iwdev->arp_table[arp_index].mac_addr, mac_addr);
97 		break;
98 	case I40IW_ARP_RESOLVE:
99 		if (arp_index == iwdev->arp_table_size)
100 			return -1;
101 		break;
102 	case I40IW_ARP_DELETE:
103 		if (arp_index == iwdev->arp_table_size)
104 			return -1;
105 		memset(iwdev->arp_table[arp_index].ip_addr, 0,
106 		       sizeof(iwdev->arp_table[arp_index].ip_addr));
107 		eth_zero_addr(iwdev->arp_table[arp_index].mac_addr);
108 		i40iw_free_resource(iwdev, iwdev->allocated_arps, arp_index);
109 		break;
110 	default:
111 		return -1;
112 	}
113 	return arp_index;
114 }
115 
116 /**
117  * i40iw_wr32 - write 32 bits to hw register
118  * @hw: hardware information including registers
119  * @reg: register offset
120  * @value: vvalue to write to register
121  */
i40iw_wr32(struct i40iw_hw * hw,u32 reg,u32 value)122 inline void i40iw_wr32(struct i40iw_hw *hw, u32 reg, u32 value)
123 {
124 	writel(value, hw->hw_addr + reg);
125 }
126 
127 /**
128  * i40iw_rd32 - read a 32 bit hw register
129  * @hw: hardware information including registers
130  * @reg: register offset
131  *
132  * Return value of register content
133  */
i40iw_rd32(struct i40iw_hw * hw,u32 reg)134 inline u32 i40iw_rd32(struct i40iw_hw *hw, u32 reg)
135 {
136 	return readl(hw->hw_addr + reg);
137 }
138 
139 /**
140  * i40iw_inetaddr_event - system notifier for ipv4 addr events
141  * @notfier: not used
142  * @event: event for notifier
143  * @ptr: if address
144  */
i40iw_inetaddr_event(struct notifier_block * notifier,unsigned long event,void * ptr)145 int i40iw_inetaddr_event(struct notifier_block *notifier,
146 			 unsigned long event,
147 			 void *ptr)
148 {
149 	struct in_ifaddr *ifa = ptr;
150 	struct net_device *event_netdev = ifa->ifa_dev->dev;
151 	struct net_device *netdev;
152 	struct net_device *upper_dev;
153 	struct i40iw_device *iwdev;
154 	struct i40iw_handler *hdl;
155 	u32 local_ipaddr;
156 	u32 action = I40IW_ARP_ADD;
157 
158 	hdl = i40iw_find_netdev(event_netdev);
159 	if (!hdl)
160 		return NOTIFY_DONE;
161 
162 	iwdev = &hdl->device;
163 	if (iwdev->init_state < IP_ADDR_REGISTERED || iwdev->closing)
164 		return NOTIFY_DONE;
165 
166 	netdev = iwdev->ldev->netdev;
167 	upper_dev = netdev_master_upper_dev_get(netdev);
168 	if (netdev != event_netdev)
169 		return NOTIFY_DONE;
170 
171 	if (upper_dev) {
172 		struct in_device *in;
173 
174 		rcu_read_lock();
175 		in = __in_dev_get_rcu(upper_dev);
176 
177 		if (!in->ifa_list)
178 			local_ipaddr = 0;
179 		else
180 			local_ipaddr = ntohl(in->ifa_list->ifa_address);
181 
182 		rcu_read_unlock();
183 	} else {
184 		local_ipaddr = ntohl(ifa->ifa_address);
185 	}
186 	switch (event) {
187 	case NETDEV_DOWN:
188 		action = I40IW_ARP_DELETE;
189 		/* Fall through */
190 	case NETDEV_UP:
191 		/* Fall through */
192 	case NETDEV_CHANGEADDR:
193 
194 		/* Just skip if no need to handle ARP cache */
195 		if (!local_ipaddr)
196 			break;
197 
198 		i40iw_manage_arp_cache(iwdev,
199 				       netdev->dev_addr,
200 				       &local_ipaddr,
201 				       true,
202 				       action);
203 		i40iw_if_notify(iwdev, netdev, &local_ipaddr, true,
204 				(action == I40IW_ARP_ADD) ? true : false);
205 		break;
206 	default:
207 		break;
208 	}
209 	return NOTIFY_DONE;
210 }
211 
212 /**
213  * i40iw_inet6addr_event - system notifier for ipv6 addr events
214  * @notfier: not used
215  * @event: event for notifier
216  * @ptr: if address
217  */
i40iw_inet6addr_event(struct notifier_block * notifier,unsigned long event,void * ptr)218 int i40iw_inet6addr_event(struct notifier_block *notifier,
219 			  unsigned long event,
220 			  void *ptr)
221 {
222 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
223 	struct net_device *event_netdev = ifa->idev->dev;
224 	struct net_device *netdev;
225 	struct i40iw_device *iwdev;
226 	struct i40iw_handler *hdl;
227 	u32 local_ipaddr6[4];
228 	u32 action = I40IW_ARP_ADD;
229 
230 	hdl = i40iw_find_netdev(event_netdev);
231 	if (!hdl)
232 		return NOTIFY_DONE;
233 
234 	iwdev = &hdl->device;
235 	if (iwdev->init_state < IP_ADDR_REGISTERED || iwdev->closing)
236 		return NOTIFY_DONE;
237 
238 	netdev = iwdev->ldev->netdev;
239 	if (netdev != event_netdev)
240 		return NOTIFY_DONE;
241 
242 	i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
243 	switch (event) {
244 	case NETDEV_DOWN:
245 		action = I40IW_ARP_DELETE;
246 		/* Fall through */
247 	case NETDEV_UP:
248 		/* Fall through */
249 	case NETDEV_CHANGEADDR:
250 		i40iw_manage_arp_cache(iwdev,
251 				       netdev->dev_addr,
252 				       local_ipaddr6,
253 				       false,
254 				       action);
255 		i40iw_if_notify(iwdev, netdev, local_ipaddr6, false,
256 				(action == I40IW_ARP_ADD) ? true : false);
257 		break;
258 	default:
259 		break;
260 	}
261 	return NOTIFY_DONE;
262 }
263 
264 /**
265  * i40iw_net_event - system notifier for netevents
266  * @notfier: not used
267  * @event: event for notifier
268  * @ptr: neighbor
269  */
i40iw_net_event(struct notifier_block * notifier,unsigned long event,void * ptr)270 int i40iw_net_event(struct notifier_block *notifier, unsigned long event, void *ptr)
271 {
272 	struct neighbour *neigh = ptr;
273 	struct i40iw_device *iwdev;
274 	struct i40iw_handler *iwhdl;
275 	__be32 *p;
276 	u32 local_ipaddr[4];
277 
278 	switch (event) {
279 	case NETEVENT_NEIGH_UPDATE:
280 		iwhdl = i40iw_find_netdev((struct net_device *)neigh->dev);
281 		if (!iwhdl)
282 			return NOTIFY_DONE;
283 		iwdev = &iwhdl->device;
284 		if (iwdev->init_state < IP_ADDR_REGISTERED || iwdev->closing)
285 			return NOTIFY_DONE;
286 		p = (__be32 *)neigh->primary_key;
287 		i40iw_copy_ip_ntohl(local_ipaddr, p);
288 		if (neigh->nud_state & NUD_VALID) {
289 			i40iw_manage_arp_cache(iwdev,
290 					       neigh->ha,
291 					       local_ipaddr,
292 					       false,
293 					       I40IW_ARP_ADD);
294 
295 		} else {
296 			i40iw_manage_arp_cache(iwdev,
297 					       neigh->ha,
298 					       local_ipaddr,
299 					       false,
300 					       I40IW_ARP_DELETE);
301 		}
302 		break;
303 	default:
304 		break;
305 	}
306 	return NOTIFY_DONE;
307 }
308 
309 /**
310  * i40iw_netdevice_event - system notifier for netdev events
311  * @notfier: not used
312  * @event: event for notifier
313  * @ptr: netdev
314  */
i40iw_netdevice_event(struct notifier_block * notifier,unsigned long event,void * ptr)315 int i40iw_netdevice_event(struct notifier_block *notifier,
316 			  unsigned long event,
317 			  void *ptr)
318 {
319 	struct net_device *event_netdev;
320 	struct net_device *netdev;
321 	struct i40iw_device *iwdev;
322 	struct i40iw_handler *hdl;
323 
324 	event_netdev = netdev_notifier_info_to_dev(ptr);
325 
326 	hdl = i40iw_find_netdev(event_netdev);
327 	if (!hdl)
328 		return NOTIFY_DONE;
329 
330 	iwdev = &hdl->device;
331 	if (iwdev->init_state < RDMA_DEV_REGISTERED || iwdev->closing)
332 		return NOTIFY_DONE;
333 
334 	netdev = iwdev->ldev->netdev;
335 	if (netdev != event_netdev)
336 		return NOTIFY_DONE;
337 
338 	iwdev->iw_status = 1;
339 
340 	switch (event) {
341 	case NETDEV_DOWN:
342 		iwdev->iw_status = 0;
343 		/* Fall through */
344 	case NETDEV_UP:
345 		i40iw_port_ibevent(iwdev);
346 		break;
347 	default:
348 		break;
349 	}
350 	return NOTIFY_DONE;
351 }
352 
353 /**
354  * i40iw_get_cqp_request - get cqp struct
355  * @cqp: device cqp ptr
356  * @wait: cqp to be used in wait mode
357  */
i40iw_get_cqp_request(struct i40iw_cqp * cqp,bool wait)358 struct i40iw_cqp_request *i40iw_get_cqp_request(struct i40iw_cqp *cqp, bool wait)
359 {
360 	struct i40iw_cqp_request *cqp_request = NULL;
361 	unsigned long flags;
362 
363 	spin_lock_irqsave(&cqp->req_lock, flags);
364 	if (!list_empty(&cqp->cqp_avail_reqs)) {
365 		cqp_request = list_entry(cqp->cqp_avail_reqs.next,
366 					 struct i40iw_cqp_request, list);
367 		list_del_init(&cqp_request->list);
368 	}
369 	spin_unlock_irqrestore(&cqp->req_lock, flags);
370 	if (!cqp_request) {
371 		cqp_request = kzalloc(sizeof(*cqp_request), GFP_ATOMIC);
372 		if (cqp_request) {
373 			cqp_request->dynamic = true;
374 			INIT_LIST_HEAD(&cqp_request->list);
375 			init_waitqueue_head(&cqp_request->waitq);
376 		}
377 	}
378 	if (!cqp_request) {
379 		i40iw_pr_err("CQP Request Fail: No Memory");
380 		return NULL;
381 	}
382 
383 	if (wait) {
384 		atomic_set(&cqp_request->refcount, 2);
385 		cqp_request->waiting = true;
386 	} else {
387 		atomic_set(&cqp_request->refcount, 1);
388 	}
389 	return cqp_request;
390 }
391 
392 /**
393  * i40iw_free_cqp_request - free cqp request
394  * @cqp: cqp ptr
395  * @cqp_request: to be put back in cqp list
396  */
i40iw_free_cqp_request(struct i40iw_cqp * cqp,struct i40iw_cqp_request * cqp_request)397 void i40iw_free_cqp_request(struct i40iw_cqp *cqp, struct i40iw_cqp_request *cqp_request)
398 {
399 	struct i40iw_device *iwdev = container_of(cqp, struct i40iw_device, cqp);
400 	unsigned long flags;
401 
402 	if (cqp_request->dynamic) {
403 		kfree(cqp_request);
404 	} else {
405 		cqp_request->request_done = false;
406 		cqp_request->callback_fcn = NULL;
407 		cqp_request->waiting = false;
408 
409 		spin_lock_irqsave(&cqp->req_lock, flags);
410 		list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
411 		spin_unlock_irqrestore(&cqp->req_lock, flags);
412 	}
413 	wake_up(&iwdev->close_wq);
414 }
415 
416 /**
417  * i40iw_put_cqp_request - dec ref count and free if 0
418  * @cqp: cqp ptr
419  * @cqp_request: to be put back in cqp list
420  */
i40iw_put_cqp_request(struct i40iw_cqp * cqp,struct i40iw_cqp_request * cqp_request)421 void i40iw_put_cqp_request(struct i40iw_cqp *cqp,
422 			   struct i40iw_cqp_request *cqp_request)
423 {
424 	if (atomic_dec_and_test(&cqp_request->refcount))
425 		i40iw_free_cqp_request(cqp, cqp_request);
426 }
427 
428 /**
429  * i40iw_free_pending_cqp_request -free pending cqp request objs
430  * @cqp: cqp ptr
431  * @cqp_request: to be put back in cqp list
432  */
i40iw_free_pending_cqp_request(struct i40iw_cqp * cqp,struct i40iw_cqp_request * cqp_request)433 static void i40iw_free_pending_cqp_request(struct i40iw_cqp *cqp,
434 					   struct i40iw_cqp_request *cqp_request)
435 {
436 	struct i40iw_device *iwdev = container_of(cqp, struct i40iw_device, cqp);
437 
438 	if (cqp_request->waiting) {
439 		cqp_request->compl_info.error = true;
440 		cqp_request->request_done = true;
441 		wake_up(&cqp_request->waitq);
442 	}
443 	i40iw_put_cqp_request(cqp, cqp_request);
444 	wait_event_timeout(iwdev->close_wq,
445 			   !atomic_read(&cqp_request->refcount),
446 			   1000);
447 }
448 
449 /**
450  * i40iw_cleanup_pending_cqp_op - clean-up cqp with no completions
451  * @iwdev: iwarp device
452  */
i40iw_cleanup_pending_cqp_op(struct i40iw_device * iwdev)453 void i40iw_cleanup_pending_cqp_op(struct i40iw_device *iwdev)
454 {
455 	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
456 	struct i40iw_cqp *cqp = &iwdev->cqp;
457 	struct i40iw_cqp_request *cqp_request = NULL;
458 	struct cqp_commands_info *pcmdinfo = NULL;
459 	u32 i, pending_work, wqe_idx;
460 
461 	pending_work = I40IW_RING_WORK_AVAILABLE(cqp->sc_cqp.sq_ring);
462 	wqe_idx = I40IW_RING_GETCURRENT_TAIL(cqp->sc_cqp.sq_ring);
463 	for (i = 0; i < pending_work; i++) {
464 		cqp_request = (struct i40iw_cqp_request *)(unsigned long)cqp->scratch_array[wqe_idx];
465 		if (cqp_request)
466 			i40iw_free_pending_cqp_request(cqp, cqp_request);
467 		wqe_idx = (wqe_idx + 1) % I40IW_RING_GETSIZE(cqp->sc_cqp.sq_ring);
468 	}
469 
470 	while (!list_empty(&dev->cqp_cmd_head)) {
471 		pcmdinfo = (struct cqp_commands_info *)i40iw_remove_head(&dev->cqp_cmd_head);
472 		cqp_request = container_of(pcmdinfo, struct i40iw_cqp_request, info);
473 		if (cqp_request)
474 			i40iw_free_pending_cqp_request(cqp, cqp_request);
475 	}
476 }
477 
478 /**
479  * i40iw_free_qp - callback after destroy cqp completes
480  * @cqp_request: cqp request for destroy qp
481  * @num: not used
482  */
i40iw_free_qp(struct i40iw_cqp_request * cqp_request,u32 num)483 static void i40iw_free_qp(struct i40iw_cqp_request *cqp_request, u32 num)
484 {
485 	struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)cqp_request->param;
486 	struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
487 	struct i40iw_device *iwdev;
488 	u32 qp_num = iwqp->ibqp.qp_num;
489 
490 	iwdev = iwqp->iwdev;
491 
492 	i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
493 	i40iw_free_qp_resources(iwdev, iwqp, qp_num);
494 	i40iw_rem_devusecount(iwdev);
495 }
496 
497 /**
498  * i40iw_wait_event - wait for completion
499  * @iwdev: iwarp device
500  * @cqp_request: cqp request to wait
501  */
i40iw_wait_event(struct i40iw_device * iwdev,struct i40iw_cqp_request * cqp_request)502 static int i40iw_wait_event(struct i40iw_device *iwdev,
503 			    struct i40iw_cqp_request *cqp_request)
504 {
505 	struct cqp_commands_info *info = &cqp_request->info;
506 	struct i40iw_cqp *iwcqp = &iwdev->cqp;
507 	struct i40iw_cqp_timeout cqp_timeout;
508 	bool cqp_error = false;
509 	int err_code = 0;
510 	memset(&cqp_timeout, 0, sizeof(cqp_timeout));
511 	cqp_timeout.compl_cqp_cmds = iwdev->sc_dev.cqp_cmd_stats[OP_COMPLETED_COMMANDS];
512 	do {
513 		if (wait_event_timeout(cqp_request->waitq,
514 				       cqp_request->request_done, CQP_COMPL_WAIT_TIME))
515 			break;
516 
517 		i40iw_check_cqp_progress(&cqp_timeout, &iwdev->sc_dev);
518 
519 		if (cqp_timeout.count < CQP_TIMEOUT_THRESHOLD)
520 			continue;
521 
522 		i40iw_pr_err("error cqp command 0x%x timed out", info->cqp_cmd);
523 		err_code = -ETIME;
524 		if (!iwdev->reset) {
525 			iwdev->reset = true;
526 			i40iw_request_reset(iwdev);
527 		}
528 		goto done;
529 	} while (1);
530 	cqp_error = cqp_request->compl_info.error;
531 	if (cqp_error) {
532 		i40iw_pr_err("error cqp command 0x%x completion maj = 0x%x min=0x%x\n",
533 			     info->cqp_cmd, cqp_request->compl_info.maj_err_code,
534 			     cqp_request->compl_info.min_err_code);
535 		err_code = -EPROTO;
536 		goto done;
537 	}
538 done:
539 	i40iw_put_cqp_request(iwcqp, cqp_request);
540 	return err_code;
541 }
542 
543 /**
544  * i40iw_handle_cqp_op - process cqp command
545  * @iwdev: iwarp device
546  * @cqp_request: cqp request to process
547  */
i40iw_handle_cqp_op(struct i40iw_device * iwdev,struct i40iw_cqp_request * cqp_request)548 enum i40iw_status_code i40iw_handle_cqp_op(struct i40iw_device *iwdev,
549 					   struct i40iw_cqp_request
550 					   *cqp_request)
551 {
552 	struct i40iw_sc_dev *dev = &iwdev->sc_dev;
553 	enum i40iw_status_code status;
554 	struct cqp_commands_info *info = &cqp_request->info;
555 	int err_code = 0;
556 
557 	if (iwdev->reset) {
558 		i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
559 		return I40IW_ERR_CQP_COMPL_ERROR;
560 	}
561 
562 	status = i40iw_process_cqp_cmd(dev, info);
563 	if (status) {
564 		i40iw_pr_err("error cqp command 0x%x failed\n", info->cqp_cmd);
565 		i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
566 		return status;
567 	}
568 	if (cqp_request->waiting)
569 		err_code = i40iw_wait_event(iwdev, cqp_request);
570 	if (err_code)
571 		status = I40IW_ERR_CQP_COMPL_ERROR;
572 	return status;
573 }
574 
575 /**
576  * i40iw_add_devusecount - add dev refcount
577  * @iwdev: dev for refcount
578  */
i40iw_add_devusecount(struct i40iw_device * iwdev)579 void i40iw_add_devusecount(struct i40iw_device *iwdev)
580 {
581 	atomic64_inc(&iwdev->use_count);
582 }
583 
584 /**
585  * i40iw_rem_devusecount - decrement refcount for dev
586  * @iwdev: device
587  */
i40iw_rem_devusecount(struct i40iw_device * iwdev)588 void i40iw_rem_devusecount(struct i40iw_device *iwdev)
589 {
590 	if (!atomic64_dec_and_test(&iwdev->use_count))
591 		return;
592 	wake_up(&iwdev->close_wq);
593 }
594 
595 /**
596  * i40iw_add_pdusecount - add pd refcount
597  * @iwpd: pd for refcount
598  */
i40iw_add_pdusecount(struct i40iw_pd * iwpd)599 void i40iw_add_pdusecount(struct i40iw_pd *iwpd)
600 {
601 	atomic_inc(&iwpd->usecount);
602 }
603 
604 /**
605  * i40iw_rem_pdusecount - decrement refcount for pd and free if 0
606  * @iwpd: pd for refcount
607  * @iwdev: iwarp device
608  */
i40iw_rem_pdusecount(struct i40iw_pd * iwpd,struct i40iw_device * iwdev)609 void i40iw_rem_pdusecount(struct i40iw_pd *iwpd, struct i40iw_device *iwdev)
610 {
611 	if (!atomic_dec_and_test(&iwpd->usecount))
612 		return;
613 	i40iw_free_resource(iwdev, iwdev->allocated_pds, iwpd->sc_pd.pd_id);
614 	kfree(iwpd);
615 }
616 
617 /**
618  * i40iw_add_ref - add refcount for qp
619  * @ibqp: iqarp qp
620  */
i40iw_add_ref(struct ib_qp * ibqp)621 void i40iw_add_ref(struct ib_qp *ibqp)
622 {
623 	struct i40iw_qp *iwqp = (struct i40iw_qp *)ibqp;
624 
625 	atomic_inc(&iwqp->refcount);
626 }
627 
628 /**
629  * i40iw_rem_ref - rem refcount for qp and free if 0
630  * @ibqp: iqarp qp
631  */
i40iw_rem_ref(struct ib_qp * ibqp)632 void i40iw_rem_ref(struct ib_qp *ibqp)
633 {
634 	struct i40iw_qp *iwqp;
635 	enum i40iw_status_code status;
636 	struct i40iw_cqp_request *cqp_request;
637 	struct cqp_commands_info *cqp_info;
638 	struct i40iw_device *iwdev;
639 	u32 qp_num;
640 	unsigned long flags;
641 
642 	iwqp = to_iwqp(ibqp);
643 	iwdev = iwqp->iwdev;
644 	spin_lock_irqsave(&iwdev->qptable_lock, flags);
645 	if (!atomic_dec_and_test(&iwqp->refcount)) {
646 		spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
647 		return;
648 	}
649 
650 	qp_num = iwqp->ibqp.qp_num;
651 	iwdev->qp_table[qp_num] = NULL;
652 	spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
653 	cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
654 	if (!cqp_request)
655 		return;
656 
657 	cqp_request->callback_fcn = i40iw_free_qp;
658 	cqp_request->param = (void *)&iwqp->sc_qp;
659 	cqp_info = &cqp_request->info;
660 	cqp_info->cqp_cmd = OP_QP_DESTROY;
661 	cqp_info->post_sq = 1;
662 	cqp_info->in.u.qp_destroy.qp = &iwqp->sc_qp;
663 	cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
664 	cqp_info->in.u.qp_destroy.remove_hash_idx = true;
665 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
666 	if (!status)
667 		return;
668 
669 	i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
670 	i40iw_free_qp_resources(iwdev, iwqp, qp_num);
671 	i40iw_rem_devusecount(iwdev);
672 }
673 
674 /**
675  * i40iw_get_qp - get qp address
676  * @device: iwarp device
677  * @qpn: qp number
678  */
i40iw_get_qp(struct ib_device * device,int qpn)679 struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
680 {
681 	struct i40iw_device *iwdev = to_iwdev(device);
682 
683 	if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
684 		return NULL;
685 
686 	return &iwdev->qp_table[qpn]->ibqp;
687 }
688 
689 /**
690  * i40iw_debug_buf - print debug msg and buffer is mask set
691  * @dev: hardware control device structure
692  * @mask: mask to compare if to print debug buffer
693  * @buf: points buffer addr
694  * @size: saize of buffer to print
695  */
i40iw_debug_buf(struct i40iw_sc_dev * dev,enum i40iw_debug_flag mask,char * desc,u64 * buf,u32 size)696 void i40iw_debug_buf(struct i40iw_sc_dev *dev,
697 		     enum i40iw_debug_flag mask,
698 		     char *desc,
699 		     u64 *buf,
700 		     u32 size)
701 {
702 	u32 i;
703 
704 	if (!(dev->debug_mask & mask))
705 		return;
706 	i40iw_debug(dev, mask, "%s\n", desc);
707 	i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
708 		    (unsigned long long)virt_to_phys(buf));
709 
710 	for (i = 0; i < size; i += 8)
711 		i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
712 }
713 
714 /**
715  * i40iw_get_hw_addr - return hw addr
716  * @par: points to shared dev
717  */
i40iw_get_hw_addr(void * par)718 u8 __iomem *i40iw_get_hw_addr(void *par)
719 {
720 	struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
721 
722 	return dev->hw->hw_addr;
723 }
724 
725 /**
726  * i40iw_remove_head - return head entry and remove from list
727  * @list: list for entry
728  */
i40iw_remove_head(struct list_head * list)729 void *i40iw_remove_head(struct list_head *list)
730 {
731 	struct list_head *entry;
732 
733 	if (list_empty(list))
734 		return NULL;
735 
736 	entry = (void *)list->next;
737 	list_del(entry);
738 	return (void *)entry;
739 }
740 
741 /**
742  * i40iw_allocate_dma_mem - Memory alloc helper fn
743  * @hw:   pointer to the HW structure
744  * @mem:  ptr to mem struct to fill out
745  * @size: size of memory requested
746  * @alignment: what to align the allocation to
747  */
i40iw_allocate_dma_mem(struct i40iw_hw * hw,struct i40iw_dma_mem * mem,u64 size,u32 alignment)748 enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
749 					      struct i40iw_dma_mem *mem,
750 					      u64 size,
751 					      u32 alignment)
752 {
753 	struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
754 
755 	if (!mem)
756 		return I40IW_ERR_PARAM;
757 	mem->size = ALIGN(size, alignment);
758 	mem->va = dma_zalloc_coherent(&pcidev->dev, mem->size,
759 				      (dma_addr_t *)&mem->pa, GFP_KERNEL);
760 	if (!mem->va)
761 		return I40IW_ERR_NO_MEMORY;
762 	return 0;
763 }
764 
765 /**
766  * i40iw_free_dma_mem - Memory free helper fn
767  * @hw:   pointer to the HW structure
768  * @mem:  ptr to mem struct to free
769  */
i40iw_free_dma_mem(struct i40iw_hw * hw,struct i40iw_dma_mem * mem)770 void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
771 {
772 	struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
773 
774 	if (!mem || !mem->va)
775 		return;
776 
777 	dma_free_coherent(&pcidev->dev, mem->size,
778 			  mem->va, (dma_addr_t)mem->pa);
779 	mem->va = NULL;
780 }
781 
782 /**
783  * i40iw_allocate_virt_mem - virtual memory alloc helper fn
784  * @hw:   pointer to the HW structure
785  * @mem:  ptr to mem struct to fill out
786  * @size: size of memory requested
787  */
i40iw_allocate_virt_mem(struct i40iw_hw * hw,struct i40iw_virt_mem * mem,u32 size)788 enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
789 					       struct i40iw_virt_mem *mem,
790 					       u32 size)
791 {
792 	if (!mem)
793 		return I40IW_ERR_PARAM;
794 
795 	mem->size = size;
796 	mem->va = kzalloc(size, GFP_KERNEL);
797 
798 	if (mem->va)
799 		return 0;
800 	else
801 		return I40IW_ERR_NO_MEMORY;
802 }
803 
804 /**
805  * i40iw_free_virt_mem - virtual memory free helper fn
806  * @hw:   pointer to the HW structure
807  * @mem:  ptr to mem struct to free
808  */
i40iw_free_virt_mem(struct i40iw_hw * hw,struct i40iw_virt_mem * mem)809 enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
810 					   struct i40iw_virt_mem *mem)
811 {
812 	if (!mem)
813 		return I40IW_ERR_PARAM;
814 	/*
815 	 * mem->va points to the parent of mem, so both mem and mem->va
816 	 * can not be touched once mem->va is freed
817 	 */
818 	kfree(mem->va);
819 	return 0;
820 }
821 
822 /**
823  * i40iw_cqp_sds_cmd - create cqp command for sd
824  * @dev: hardware control device structure
825  * @sd_info: information  for sd cqp
826  *
827  */
i40iw_cqp_sds_cmd(struct i40iw_sc_dev * dev,struct i40iw_update_sds_info * sdinfo)828 enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
829 					 struct i40iw_update_sds_info *sdinfo)
830 {
831 	enum i40iw_status_code status;
832 	struct i40iw_cqp_request *cqp_request;
833 	struct cqp_commands_info *cqp_info;
834 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
835 
836 	cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
837 	if (!cqp_request)
838 		return I40IW_ERR_NO_MEMORY;
839 	cqp_info = &cqp_request->info;
840 	memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
841 	       sizeof(cqp_info->in.u.update_pe_sds.info));
842 	cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
843 	cqp_info->post_sq = 1;
844 	cqp_info->in.u.update_pe_sds.dev = dev;
845 	cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
846 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
847 	if (status)
848 		i40iw_pr_err("CQP-OP Update SD's fail");
849 	return status;
850 }
851 
852 /**
853  * i40iw_qp_suspend_resume - cqp command for suspend/resume
854  * @dev: hardware control device structure
855  * @qp: hardware control qp
856  * @suspend: flag if suspend or resume
857  */
i40iw_qp_suspend_resume(struct i40iw_sc_dev * dev,struct i40iw_sc_qp * qp,bool suspend)858 void i40iw_qp_suspend_resume(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp, bool suspend)
859 {
860 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
861 	struct i40iw_cqp_request *cqp_request;
862 	struct i40iw_sc_cqp *cqp = dev->cqp;
863 	struct cqp_commands_info *cqp_info;
864 	enum i40iw_status_code status;
865 
866 	cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
867 	if (!cqp_request)
868 		return;
869 
870 	cqp_info = &cqp_request->info;
871 	cqp_info->cqp_cmd = (suspend) ? OP_SUSPEND : OP_RESUME;
872 	cqp_info->in.u.suspend_resume.cqp = cqp;
873 	cqp_info->in.u.suspend_resume.qp = qp;
874 	cqp_info->in.u.suspend_resume.scratch = (uintptr_t)cqp_request;
875 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
876 	if (status)
877 		i40iw_pr_err("CQP-OP QP Suspend/Resume fail");
878 }
879 
880 /**
881  * i40iw_term_modify_qp - modify qp for term message
882  * @qp: hardware control qp
883  * @next_state: qp's next state
884  * @term: terminate code
885  * @term_len: length
886  */
i40iw_term_modify_qp(struct i40iw_sc_qp * qp,u8 next_state,u8 term,u8 term_len)887 void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
888 {
889 	struct i40iw_qp *iwqp;
890 
891 	iwqp = (struct i40iw_qp *)qp->back_qp;
892 	i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
893 };
894 
895 /**
896  * i40iw_terminate_done - after terminate is completed
897  * @qp: hardware control qp
898  * @timeout_occurred: indicates if terminate timer expired
899  */
i40iw_terminate_done(struct i40iw_sc_qp * qp,int timeout_occurred)900 void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
901 {
902 	struct i40iw_qp *iwqp;
903 	u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
904 	u8 hte = 0;
905 	bool first_time;
906 	unsigned long flags;
907 
908 	iwqp = (struct i40iw_qp *)qp->back_qp;
909 	spin_lock_irqsave(&iwqp->lock, flags);
910 	if (iwqp->hte_added) {
911 		iwqp->hte_added = 0;
912 		hte = 1;
913 	}
914 	first_time = !(qp->term_flags & I40IW_TERM_DONE);
915 	qp->term_flags |= I40IW_TERM_DONE;
916 	spin_unlock_irqrestore(&iwqp->lock, flags);
917 	if (first_time) {
918 		if (!timeout_occurred)
919 			i40iw_terminate_del_timer(qp);
920 		else
921 			next_iwarp_state = I40IW_QP_STATE_CLOSING;
922 
923 		i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
924 		i40iw_cm_disconn(iwqp);
925 	}
926 }
927 
928 /**
929  * i40iw_terminate_imeout - timeout happened
930  * @context: points to iwarp qp
931  */
i40iw_terminate_timeout(struct timer_list * t)932 static void i40iw_terminate_timeout(struct timer_list *t)
933 {
934 	struct i40iw_qp *iwqp = from_timer(iwqp, t, terminate_timer);
935 	struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
936 
937 	i40iw_terminate_done(qp, 1);
938 	i40iw_rem_ref(&iwqp->ibqp);
939 }
940 
941 /**
942  * i40iw_terminate_start_timer - start terminate timeout
943  * @qp: hardware control qp
944  */
i40iw_terminate_start_timer(struct i40iw_sc_qp * qp)945 void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
946 {
947 	struct i40iw_qp *iwqp;
948 
949 	iwqp = (struct i40iw_qp *)qp->back_qp;
950 	i40iw_add_ref(&iwqp->ibqp);
951 	timer_setup(&iwqp->terminate_timer, i40iw_terminate_timeout, 0);
952 	iwqp->terminate_timer.expires = jiffies + HZ;
953 	add_timer(&iwqp->terminate_timer);
954 }
955 
956 /**
957  * i40iw_terminate_del_timer - delete terminate timeout
958  * @qp: hardware control qp
959  */
i40iw_terminate_del_timer(struct i40iw_sc_qp * qp)960 void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
961 {
962 	struct i40iw_qp *iwqp;
963 
964 	iwqp = (struct i40iw_qp *)qp->back_qp;
965 	if (del_timer(&iwqp->terminate_timer))
966 		i40iw_rem_ref(&iwqp->ibqp);
967 }
968 
969 /**
970  * i40iw_cqp_generic_worker - generic worker for cqp
971  * @work: work pointer
972  */
i40iw_cqp_generic_worker(struct work_struct * work)973 static void i40iw_cqp_generic_worker(struct work_struct *work)
974 {
975 	struct i40iw_virtchnl_work_info *work_info =
976 	    &((struct virtchnl_work *)work)->work_info;
977 
978 	if (work_info->worker_vf_dev)
979 		work_info->callback_fcn(work_info->worker_vf_dev);
980 }
981 
982 /**
983  * i40iw_cqp_spawn_worker - spawn worket thread
984  * @iwdev: device struct pointer
985  * @work_info: work request info
986  * @iw_vf_idx: virtual function index
987  */
i40iw_cqp_spawn_worker(struct i40iw_sc_dev * dev,struct i40iw_virtchnl_work_info * work_info,u32 iw_vf_idx)988 void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
989 			    struct i40iw_virtchnl_work_info *work_info,
990 			    u32 iw_vf_idx)
991 {
992 	struct virtchnl_work *work;
993 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
994 
995 	work = &iwdev->virtchnl_w[iw_vf_idx];
996 	memcpy(&work->work_info, work_info, sizeof(*work_info));
997 	INIT_WORK(&work->work, i40iw_cqp_generic_worker);
998 	queue_work(iwdev->virtchnl_wq, &work->work);
999 }
1000 
1001 /**
1002  * i40iw_cqp_manage_hmc_fcn_worker -
1003  * @work: work pointer for hmc info
1004  */
i40iw_cqp_manage_hmc_fcn_worker(struct work_struct * work)1005 static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
1006 {
1007 	struct i40iw_cqp_request *cqp_request =
1008 	    ((struct virtchnl_work *)work)->cqp_request;
1009 	struct i40iw_ccq_cqe_info ccq_cqe_info;
1010 	struct i40iw_hmc_fcn_info *hmcfcninfo =
1011 			&cqp_request->info.in.u.manage_hmc_pm.info;
1012 	struct i40iw_device *iwdev =
1013 	    (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
1014 
1015 	ccq_cqe_info.cqp = NULL;
1016 	ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
1017 	ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
1018 	ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
1019 	ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
1020 	ccq_cqe_info.scratch = 0;
1021 	ccq_cqe_info.error = cqp_request->compl_info.error;
1022 	hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
1023 				 hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
1024 	i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
1025 }
1026 
1027 /**
1028  * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
1029  * @cqp_request: cqp request info struct for hmc fun
1030  * @unused: unused param of callback
1031  */
i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request * cqp_request,u32 unused)1032 static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
1033 					      u32 unused)
1034 {
1035 	struct virtchnl_work *work;
1036 	struct i40iw_hmc_fcn_info *hmcfcninfo =
1037 	    &cqp_request->info.in.u.manage_hmc_pm.info;
1038 	struct i40iw_device *iwdev =
1039 	    (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
1040 	    back_dev;
1041 
1042 	if (hmcfcninfo && hmcfcninfo->callback_fcn) {
1043 		i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
1044 		atomic_inc(&cqp_request->refcount);
1045 		work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
1046 		work->cqp_request = cqp_request;
1047 		INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
1048 		queue_work(iwdev->virtchnl_wq, &work->work);
1049 		i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
1050 	} else {
1051 		i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
1052 	}
1053 }
1054 
1055 /**
1056  * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
1057  * @dev: hardware control device structure
1058  * @hmcfcninfo: info for hmc
1059  */
i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev * dev,struct i40iw_hmc_fcn_info * hmcfcninfo)1060 enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
1061 						    struct i40iw_hmc_fcn_info *hmcfcninfo)
1062 {
1063 	enum i40iw_status_code status;
1064 	struct i40iw_cqp_request *cqp_request;
1065 	struct cqp_commands_info *cqp_info;
1066 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1067 
1068 	i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
1069 	cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
1070 	if (!cqp_request)
1071 		return I40IW_ERR_NO_MEMORY;
1072 	cqp_info = &cqp_request->info;
1073 	cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
1074 	cqp_request->param = hmcfcninfo;
1075 	memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
1076 	       sizeof(*hmcfcninfo));
1077 	cqp_info->in.u.manage_hmc_pm.dev = dev;
1078 	cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
1079 	cqp_info->post_sq = 1;
1080 	cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
1081 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
1082 	if (status)
1083 		i40iw_pr_err("CQP-OP Manage HMC fail");
1084 	return status;
1085 }
1086 
1087 /**
1088  * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
1089  * @iwdev: function device struct
1090  * @values_mem: buffer for fpm
1091  * @hmc_fn_id: function id for fpm
1092  */
i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev * dev,struct i40iw_dma_mem * values_mem,u8 hmc_fn_id)1093 enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
1094 						      struct i40iw_dma_mem *values_mem,
1095 						      u8 hmc_fn_id)
1096 {
1097 	enum i40iw_status_code status;
1098 	struct i40iw_cqp_request *cqp_request;
1099 	struct cqp_commands_info *cqp_info;
1100 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1101 
1102 	cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1103 	if (!cqp_request)
1104 		return I40IW_ERR_NO_MEMORY;
1105 	cqp_info = &cqp_request->info;
1106 	cqp_request->param = NULL;
1107 	cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
1108 	cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
1109 	cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
1110 	cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
1111 	cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
1112 	cqp_info->post_sq = 1;
1113 	cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
1114 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
1115 	if (status)
1116 		i40iw_pr_err("CQP-OP Query FPM fail");
1117 	return status;
1118 }
1119 
1120 /**
1121  * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
1122  * @dev: hardware control device structure
1123  * @values_mem: buffer with fpm values
1124  * @hmc_fn_id: function id for fpm
1125  */
i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev * dev,struct i40iw_dma_mem * values_mem,u8 hmc_fn_id)1126 enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
1127 						       struct i40iw_dma_mem *values_mem,
1128 						       u8 hmc_fn_id)
1129 {
1130 	enum i40iw_status_code status;
1131 	struct i40iw_cqp_request *cqp_request;
1132 	struct cqp_commands_info *cqp_info;
1133 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1134 
1135 	cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1136 	if (!cqp_request)
1137 		return I40IW_ERR_NO_MEMORY;
1138 	cqp_info = &cqp_request->info;
1139 	cqp_request->param = NULL;
1140 	cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
1141 	cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
1142 	cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
1143 	cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
1144 	cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
1145 	cqp_info->post_sq = 1;
1146 	cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
1147 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
1148 	if (status)
1149 		i40iw_pr_err("CQP-OP Commit FPM fail");
1150 	return status;
1151 }
1152 
1153 /**
1154  * i40iw_vf_wait_vchnl_resp - wait for channel msg
1155  * @iwdev: function's device struct
1156  */
i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev * dev)1157 enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
1158 {
1159 	struct i40iw_device *iwdev = dev->back_dev;
1160 	int timeout_ret;
1161 
1162 	i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
1163 		    __func__, __LINE__, dev, iwdev);
1164 
1165 	atomic_set(&iwdev->vchnl_msgs, 2);
1166 	timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
1167 					 (atomic_read(&iwdev->vchnl_msgs) == 1),
1168 					 I40IW_VCHNL_EVENT_TIMEOUT);
1169 	atomic_dec(&iwdev->vchnl_msgs);
1170 	if (!timeout_ret) {
1171 		i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
1172 		atomic_set(&iwdev->vchnl_msgs, 0);
1173 		dev->vchnl_up = false;
1174 		return I40IW_ERR_TIMEOUT;
1175 	}
1176 	wake_up(&dev->vf_reqs);
1177 	return 0;
1178 }
1179 
1180 /**
1181  * i40iw_cqp_cq_create_cmd - create a cq for the cqp
1182  * @dev: device pointer
1183  * @cq: pointer to created cq
1184  */
i40iw_cqp_cq_create_cmd(struct i40iw_sc_dev * dev,struct i40iw_sc_cq * cq)1185 enum i40iw_status_code i40iw_cqp_cq_create_cmd(struct i40iw_sc_dev *dev,
1186 					       struct i40iw_sc_cq *cq)
1187 {
1188 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1189 	struct i40iw_cqp *iwcqp = &iwdev->cqp;
1190 	struct i40iw_cqp_request *cqp_request;
1191 	struct cqp_commands_info *cqp_info;
1192 	enum i40iw_status_code status;
1193 
1194 	cqp_request = i40iw_get_cqp_request(iwcqp, true);
1195 	if (!cqp_request)
1196 		return I40IW_ERR_NO_MEMORY;
1197 
1198 	cqp_info = &cqp_request->info;
1199 	cqp_info->cqp_cmd = OP_CQ_CREATE;
1200 	cqp_info->post_sq = 1;
1201 	cqp_info->in.u.cq_create.cq = cq;
1202 	cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
1203 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
1204 	if (status)
1205 		i40iw_pr_err("CQP-OP Create QP fail");
1206 
1207 	return status;
1208 }
1209 
1210 /**
1211  * i40iw_cqp_qp_create_cmd - create a qp for the cqp
1212  * @dev: device pointer
1213  * @qp: pointer to created qp
1214  */
i40iw_cqp_qp_create_cmd(struct i40iw_sc_dev * dev,struct i40iw_sc_qp * qp)1215 enum i40iw_status_code i40iw_cqp_qp_create_cmd(struct i40iw_sc_dev *dev,
1216 					       struct i40iw_sc_qp *qp)
1217 {
1218 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1219 	struct i40iw_cqp *iwcqp = &iwdev->cqp;
1220 	struct i40iw_cqp_request *cqp_request;
1221 	struct cqp_commands_info *cqp_info;
1222 	struct i40iw_create_qp_info *qp_info;
1223 	enum i40iw_status_code status;
1224 
1225 	cqp_request = i40iw_get_cqp_request(iwcqp, true);
1226 	if (!cqp_request)
1227 		return I40IW_ERR_NO_MEMORY;
1228 
1229 	cqp_info = &cqp_request->info;
1230 	qp_info = &cqp_request->info.in.u.qp_create.info;
1231 
1232 	memset(qp_info, 0, sizeof(*qp_info));
1233 
1234 	qp_info->cq_num_valid = true;
1235 	qp_info->next_iwarp_state = I40IW_QP_STATE_RTS;
1236 
1237 	cqp_info->cqp_cmd = OP_QP_CREATE;
1238 	cqp_info->post_sq = 1;
1239 	cqp_info->in.u.qp_create.qp = qp;
1240 	cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
1241 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
1242 	if (status)
1243 		i40iw_pr_err("CQP-OP QP create fail");
1244 	return status;
1245 }
1246 
1247 /**
1248  * i40iw_cqp_cq_destroy_cmd - destroy the cqp cq
1249  * @dev: device pointer
1250  * @cq: pointer to cq
1251  */
i40iw_cqp_cq_destroy_cmd(struct i40iw_sc_dev * dev,struct i40iw_sc_cq * cq)1252 void i40iw_cqp_cq_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
1253 {
1254 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1255 
1256 	i40iw_cq_wq_destroy(iwdev, cq);
1257 }
1258 
1259 /**
1260  * i40iw_cqp_qp_destroy_cmd - destroy the cqp
1261  * @dev: device pointer
1262  * @qp: pointer to qp
1263  */
i40iw_cqp_qp_destroy_cmd(struct i40iw_sc_dev * dev,struct i40iw_sc_qp * qp)1264 void i40iw_cqp_qp_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1265 {
1266 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1267 	struct i40iw_cqp *iwcqp = &iwdev->cqp;
1268 	struct i40iw_cqp_request *cqp_request;
1269 	struct cqp_commands_info *cqp_info;
1270 	enum i40iw_status_code status;
1271 
1272 	cqp_request = i40iw_get_cqp_request(iwcqp, true);
1273 	if (!cqp_request)
1274 		return;
1275 
1276 	cqp_info = &cqp_request->info;
1277 	memset(cqp_info, 0, sizeof(*cqp_info));
1278 
1279 	cqp_info->cqp_cmd = OP_QP_DESTROY;
1280 	cqp_info->post_sq = 1;
1281 	cqp_info->in.u.qp_destroy.qp = qp;
1282 	cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
1283 	cqp_info->in.u.qp_destroy.remove_hash_idx = true;
1284 	status = i40iw_handle_cqp_op(iwdev, cqp_request);
1285 	if (status)
1286 		i40iw_pr_err("CQP QP_DESTROY fail");
1287 }
1288 
1289 
1290 /**
1291  * i40iw_ieq_mpa_crc_ae - generate AE for crc error
1292  * @dev: hardware control device structure
1293  * @qp: hardware control qp
1294  */
i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev * dev,struct i40iw_sc_qp * qp)1295 void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1296 {
1297 	struct i40iw_gen_ae_info info;
1298 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1299 
1300 	i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
1301 	info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
1302 	info.ae_source = I40IW_AE_SOURCE_RQ;
1303 	i40iw_gen_ae(iwdev, qp, &info, false);
1304 }
1305 
1306 /**
1307  * i40iw_init_hash_desc - initialize hash for crc calculation
1308  * @desc: cryption type
1309  */
i40iw_init_hash_desc(struct shash_desc ** desc)1310 enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
1311 {
1312 	struct crypto_shash *tfm;
1313 	struct shash_desc *tdesc;
1314 
1315 	tfm = crypto_alloc_shash("crc32c", 0, 0);
1316 	if (IS_ERR(tfm))
1317 		return I40IW_ERR_MPA_CRC;
1318 
1319 	tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
1320 			GFP_KERNEL);
1321 	if (!tdesc) {
1322 		crypto_free_shash(tfm);
1323 		return I40IW_ERR_MPA_CRC;
1324 	}
1325 	tdesc->tfm = tfm;
1326 	*desc = tdesc;
1327 
1328 	return 0;
1329 }
1330 
1331 /**
1332  * i40iw_free_hash_desc - free hash desc
1333  * @desc: to be freed
1334  */
i40iw_free_hash_desc(struct shash_desc * desc)1335 void i40iw_free_hash_desc(struct shash_desc *desc)
1336 {
1337 	if (desc) {
1338 		crypto_free_shash(desc->tfm);
1339 		kfree(desc);
1340 	}
1341 }
1342 
1343 /**
1344  * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
1345  * @dev: hardware control device structure
1346  * @mem: buffer ptr for fpm to be allocated
1347  * @return: memory allocation status
1348  */
i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev * dev,struct i40iw_dma_mem * mem)1349 enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
1350 						 struct i40iw_dma_mem *mem)
1351 {
1352 	enum i40iw_status_code status;
1353 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1354 
1355 	status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
1356 				       I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1357 	return status;
1358 }
1359 
1360 /**
1361  * i40iw_ieq_check_mpacrc - check if mpa crc is OK
1362  * @desc: desc for hash
1363  * @addr: address of buffer for crc
1364  * @length: length of buffer
1365  * @value: value to be compared
1366  */
i40iw_ieq_check_mpacrc(struct shash_desc * desc,void * addr,u32 length,u32 value)1367 enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
1368 					      void *addr,
1369 					      u32 length,
1370 					      u32 value)
1371 {
1372 	u32 crc = 0;
1373 	int ret;
1374 	enum i40iw_status_code ret_code = 0;
1375 
1376 	crypto_shash_init(desc);
1377 	ret = crypto_shash_update(desc, addr, length);
1378 	if (!ret)
1379 		crypto_shash_final(desc, (u8 *)&crc);
1380 	if (crc != value) {
1381 		i40iw_pr_err("mpa crc check fail\n");
1382 		ret_code = I40IW_ERR_MPA_CRC;
1383 	}
1384 	return ret_code;
1385 }
1386 
1387 /**
1388  * i40iw_ieq_get_qp - get qp based on quad in puda buffer
1389  * @dev: hardware control device structure
1390  * @buf: receive puda buffer on exception q
1391  */
i40iw_ieq_get_qp(struct i40iw_sc_dev * dev,struct i40iw_puda_buf * buf)1392 struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
1393 				     struct i40iw_puda_buf *buf)
1394 {
1395 	struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1396 	struct i40iw_qp *iwqp;
1397 	struct i40iw_cm_node *cm_node;
1398 	u32 loc_addr[4], rem_addr[4];
1399 	u16 loc_port, rem_port;
1400 	struct ipv6hdr *ip6h;
1401 	struct iphdr *iph = (struct iphdr *)buf->iph;
1402 	struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1403 
1404 	if (iph->version == 4) {
1405 		memset(loc_addr, 0, sizeof(loc_addr));
1406 		loc_addr[0] = ntohl(iph->daddr);
1407 		memset(rem_addr, 0, sizeof(rem_addr));
1408 		rem_addr[0] = ntohl(iph->saddr);
1409 	} else {
1410 		ip6h = (struct ipv6hdr *)buf->iph;
1411 		i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1412 		i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1413 	}
1414 	loc_port = ntohs(tcph->dest);
1415 	rem_port = ntohs(tcph->source);
1416 
1417 	cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
1418 				  loc_addr, false, true);
1419 	if (!cm_node)
1420 		return NULL;
1421 	iwqp = cm_node->iwqp;
1422 	return &iwqp->sc_qp;
1423 }
1424 
1425 /**
1426  * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
1427  * @buf: puda to update
1428  * @length: length of buffer
1429  * @seqnum: seq number for tcp
1430  */
i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf * buf,u16 length,u32 seqnum)1431 void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
1432 {
1433 	struct tcphdr *tcph;
1434 	struct iphdr *iph;
1435 	u16 iphlen;
1436 	u16 packetsize;
1437 	u8 *addr = (u8 *)buf->mem.va;
1438 
1439 	iphlen = (buf->ipv4) ? 20 : 40;
1440 	iph = (struct iphdr *)(addr + buf->maclen);
1441 	tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1442 	packetsize = length + buf->tcphlen + iphlen;
1443 
1444 	iph->tot_len = htons(packetsize);
1445 	tcph->seq = htonl(seqnum);
1446 }
1447 
1448 /**
1449  * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
1450  * @info: to get information
1451  * @buf: puda buffer
1452  */
i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info * info,struct i40iw_puda_buf * buf)1453 enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
1454 						 struct i40iw_puda_buf *buf)
1455 {
1456 	struct iphdr *iph;
1457 	struct ipv6hdr *ip6h;
1458 	struct tcphdr *tcph;
1459 	u16 iphlen;
1460 	u16 pkt_len;
1461 	u8 *mem = (u8 *)buf->mem.va;
1462 	struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
1463 
1464 	if (ethh->h_proto == htons(0x8100)) {
1465 		info->vlan_valid = true;
1466 		buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
1467 	}
1468 	buf->maclen = (info->vlan_valid) ? 18 : 14;
1469 	iphlen = (info->l3proto) ? 40 : 20;
1470 	buf->ipv4 = (info->l3proto) ? false : true;
1471 	buf->iph = mem + buf->maclen;
1472 	iph = (struct iphdr *)buf->iph;
1473 
1474 	buf->tcph = buf->iph + iphlen;
1475 	tcph = (struct tcphdr *)buf->tcph;
1476 
1477 	if (buf->ipv4) {
1478 		pkt_len = ntohs(iph->tot_len);
1479 	} else {
1480 		ip6h = (struct ipv6hdr *)buf->iph;
1481 		pkt_len = ntohs(ip6h->payload_len) + iphlen;
1482 	}
1483 
1484 	buf->totallen = pkt_len + buf->maclen;
1485 
1486 	if (info->payload_len < buf->totallen) {
1487 		i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
1488 			     info->payload_len, buf->totallen);
1489 		return I40IW_ERR_INVALID_SIZE;
1490 	}
1491 
1492 	buf->tcphlen = (tcph->doff) << 2;
1493 	buf->datalen = pkt_len - iphlen - buf->tcphlen;
1494 	buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
1495 	buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1496 	buf->seqnum = ntohl(tcph->seq);
1497 	return 0;
1498 }
1499 
1500 /**
1501  * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
1502  * @vsi: pointer to the vsi structure
1503  */
i40iw_hw_stats_timeout(struct timer_list * t)1504 static void i40iw_hw_stats_timeout(struct timer_list *t)
1505 {
1506 	struct i40iw_vsi_pestat *pf_devstat = from_timer(pf_devstat, t,
1507 						       stats_timer);
1508 	struct i40iw_sc_vsi *sc_vsi = pf_devstat->vsi;
1509 	struct i40iw_sc_dev *pf_dev = sc_vsi->dev;
1510 	struct i40iw_vsi_pestat *vf_devstat = NULL;
1511 	u16 iw_vf_idx;
1512 	unsigned long flags;
1513 
1514 	/*PF*/
1515 	i40iw_hw_stats_read_all(pf_devstat, &pf_devstat->hw_stats);
1516 
1517 	for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
1518 		spin_lock_irqsave(&pf_devstat->lock, flags);
1519 		if (pf_dev->vf_dev[iw_vf_idx]) {
1520 			if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
1521 				vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->pestat;
1522 				i40iw_hw_stats_read_all(vf_devstat, &vf_devstat->hw_stats);
1523 			}
1524 		}
1525 		spin_unlock_irqrestore(&pf_devstat->lock, flags);
1526 	}
1527 
1528 	mod_timer(&pf_devstat->stats_timer,
1529 		  jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1530 }
1531 
1532 /**
1533  * i40iw_hw_stats_start_timer - Start periodic stats timer
1534  * @vsi: pointer to the vsi structure
1535  */
i40iw_hw_stats_start_timer(struct i40iw_sc_vsi * vsi)1536 void i40iw_hw_stats_start_timer(struct i40iw_sc_vsi *vsi)
1537 {
1538 	struct i40iw_vsi_pestat *devstat = vsi->pestat;
1539 
1540 	timer_setup(&devstat->stats_timer, i40iw_hw_stats_timeout, 0);
1541 	mod_timer(&devstat->stats_timer,
1542 		  jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1543 }
1544 
1545 /**
1546  * i40iw_hw_stats_stop_timer - Delete periodic stats timer
1547  * @vsi: pointer to the vsi structure
1548  */
i40iw_hw_stats_stop_timer(struct i40iw_sc_vsi * vsi)1549 void i40iw_hw_stats_stop_timer(struct i40iw_sc_vsi *vsi)
1550 {
1551 	struct i40iw_vsi_pestat *devstat = vsi->pestat;
1552 
1553 	del_timer_sync(&devstat->stats_timer);
1554 }
1555