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