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