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