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