]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/infiniband/hw/i40iw/i40iw_utils.c
Merge branch 'WIP.x86/process' into perf/core
[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                 i40iw_pr_err("CQP-OP Destroy QP fail");
551 }
552
553 /**
554  * i40iw_get_qp - get qp address
555  * @device: iwarp device
556  * @qpn: qp number
557  */
558 struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
559 {
560         struct i40iw_device *iwdev = to_iwdev(device);
561
562         if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
563                 return NULL;
564
565         return &iwdev->qp_table[qpn]->ibqp;
566 }
567
568 /**
569  * i40iw_debug_buf - print debug msg and buffer is mask set
570  * @dev: hardware control device structure
571  * @mask: mask to compare if to print debug buffer
572  * @buf: points buffer addr
573  * @size: saize of buffer to print
574  */
575 void i40iw_debug_buf(struct i40iw_sc_dev *dev,
576                      enum i40iw_debug_flag mask,
577                      char *desc,
578                      u64 *buf,
579                      u32 size)
580 {
581         u32 i;
582
583         if (!(dev->debug_mask & mask))
584                 return;
585         i40iw_debug(dev, mask, "%s\n", desc);
586         i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
587                     (unsigned long long)virt_to_phys(buf));
588
589         for (i = 0; i < size; i += 8)
590                 i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
591 }
592
593 /**
594  * i40iw_get_hw_addr - return hw addr
595  * @par: points to shared dev
596  */
597 u8 __iomem *i40iw_get_hw_addr(void *par)
598 {
599         struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
600
601         return dev->hw->hw_addr;
602 }
603
604 /**
605  * i40iw_remove_head - return head entry and remove from list
606  * @list: list for entry
607  */
608 void *i40iw_remove_head(struct list_head *list)
609 {
610         struct list_head *entry;
611
612         if (list_empty(list))
613                 return NULL;
614
615         entry = (void *)list->next;
616         list_del(entry);
617         return (void *)entry;
618 }
619
620 /**
621  * i40iw_allocate_dma_mem - Memory alloc helper fn
622  * @hw:   pointer to the HW structure
623  * @mem:  ptr to mem struct to fill out
624  * @size: size of memory requested
625  * @alignment: what to align the allocation to
626  */
627 enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
628                                               struct i40iw_dma_mem *mem,
629                                               u64 size,
630                                               u32 alignment)
631 {
632         struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
633
634         if (!mem)
635                 return I40IW_ERR_PARAM;
636         mem->size = ALIGN(size, alignment);
637         mem->va = dma_zalloc_coherent(&pcidev->dev, mem->size,
638                                       (dma_addr_t *)&mem->pa, GFP_KERNEL);
639         if (!mem->va)
640                 return I40IW_ERR_NO_MEMORY;
641         return 0;
642 }
643
644 /**
645  * i40iw_free_dma_mem - Memory free helper fn
646  * @hw:   pointer to the HW structure
647  * @mem:  ptr to mem struct to free
648  */
649 void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
650 {
651         struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
652
653         if (!mem || !mem->va)
654                 return;
655
656         dma_free_coherent(&pcidev->dev, mem->size,
657                           mem->va, (dma_addr_t)mem->pa);
658         mem->va = NULL;
659 }
660
661 /**
662  * i40iw_allocate_virt_mem - virtual memory alloc helper fn
663  * @hw:   pointer to the HW structure
664  * @mem:  ptr to mem struct to fill out
665  * @size: size of memory requested
666  */
667 enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
668                                                struct i40iw_virt_mem *mem,
669                                                u32 size)
670 {
671         if (!mem)
672                 return I40IW_ERR_PARAM;
673
674         mem->size = size;
675         mem->va = kzalloc(size, GFP_KERNEL);
676
677         if (mem->va)
678                 return 0;
679         else
680                 return I40IW_ERR_NO_MEMORY;
681 }
682
683 /**
684  * i40iw_free_virt_mem - virtual memory free helper fn
685  * @hw:   pointer to the HW structure
686  * @mem:  ptr to mem struct to free
687  */
688 enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
689                                            struct i40iw_virt_mem *mem)
690 {
691         if (!mem)
692                 return I40IW_ERR_PARAM;
693         /*
694          * mem->va points to the parent of mem, so both mem and mem->va
695          * can not be touched once mem->va is freed
696          */
697         kfree(mem->va);
698         return 0;
699 }
700
701 /**
702  * i40iw_cqp_sds_cmd - create cqp command for sd
703  * @dev: hardware control device structure
704  * @sd_info: information  for sd cqp
705  *
706  */
707 enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
708                                          struct i40iw_update_sds_info *sdinfo)
709 {
710         enum i40iw_status_code status;
711         struct i40iw_cqp_request *cqp_request;
712         struct cqp_commands_info *cqp_info;
713         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
714
715         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
716         if (!cqp_request)
717                 return I40IW_ERR_NO_MEMORY;
718         cqp_info = &cqp_request->info;
719         memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
720                sizeof(cqp_info->in.u.update_pe_sds.info));
721         cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
722         cqp_info->post_sq = 1;
723         cqp_info->in.u.update_pe_sds.dev = dev;
724         cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
725         status = i40iw_handle_cqp_op(iwdev, cqp_request);
726         if (status)
727                 i40iw_pr_err("CQP-OP Update SD's fail");
728         return status;
729 }
730
731 /**
732  * i40iw_qp_suspend_resume - cqp command for suspend/resume
733  * @dev: hardware control device structure
734  * @qp: hardware control qp
735  * @suspend: flag if suspend or resume
736  */
737 void i40iw_qp_suspend_resume(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp, bool suspend)
738 {
739         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
740         struct i40iw_cqp_request *cqp_request;
741         struct i40iw_sc_cqp *cqp = dev->cqp;
742         struct cqp_commands_info *cqp_info;
743         enum i40iw_status_code status;
744
745         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
746         if (!cqp_request)
747                 return;
748
749         cqp_info = &cqp_request->info;
750         cqp_info->cqp_cmd = (suspend) ? OP_SUSPEND : OP_RESUME;
751         cqp_info->in.u.suspend_resume.cqp = cqp;
752         cqp_info->in.u.suspend_resume.qp = qp;
753         cqp_info->in.u.suspend_resume.scratch = (uintptr_t)cqp_request;
754         status = i40iw_handle_cqp_op(iwdev, cqp_request);
755         if (status)
756                 i40iw_pr_err("CQP-OP QP Suspend/Resume fail");
757 }
758
759 /**
760  * i40iw_qp_mss_modify - modify mss for qp
761  * @dev: hardware control device structure
762  * @qp: hardware control qp
763  */
764 void i40iw_qp_mss_modify(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
765 {
766         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
767         struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
768         struct i40iw_modify_qp_info info;
769
770         memset(&info, 0, sizeof(info));
771         info.mss_change = true;
772         info.new_mss = qp->vsi->mss;
773         i40iw_hw_modify_qp(iwdev, iwqp, &info, false);
774 }
775
776 /**
777  * i40iw_term_modify_qp - modify qp for term message
778  * @qp: hardware control qp
779  * @next_state: qp's next state
780  * @term: terminate code
781  * @term_len: length
782  */
783 void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
784 {
785         struct i40iw_qp *iwqp;
786
787         iwqp = (struct i40iw_qp *)qp->back_qp;
788         i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
789 };
790
791 /**
792  * i40iw_terminate_done - after terminate is completed
793  * @qp: hardware control qp
794  * @timeout_occurred: indicates if terminate timer expired
795  */
796 void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
797 {
798         struct i40iw_qp *iwqp;
799         u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
800         u8 hte = 0;
801         bool first_time;
802         unsigned long flags;
803
804         iwqp = (struct i40iw_qp *)qp->back_qp;
805         spin_lock_irqsave(&iwqp->lock, flags);
806         if (iwqp->hte_added) {
807                 iwqp->hte_added = 0;
808                 hte = 1;
809         }
810         first_time = !(qp->term_flags & I40IW_TERM_DONE);
811         qp->term_flags |= I40IW_TERM_DONE;
812         spin_unlock_irqrestore(&iwqp->lock, flags);
813         if (first_time) {
814                 if (!timeout_occurred)
815                         i40iw_terminate_del_timer(qp);
816                 else
817                         next_iwarp_state = I40IW_QP_STATE_CLOSING;
818
819                 i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
820                 i40iw_cm_disconn(iwqp);
821         }
822 }
823
824 /**
825  * i40iw_terminate_imeout - timeout happened
826  * @context: points to iwarp qp
827  */
828 static void i40iw_terminate_timeout(unsigned long context)
829 {
830         struct i40iw_qp *iwqp = (struct i40iw_qp *)context;
831         struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
832
833         i40iw_terminate_done(qp, 1);
834         i40iw_rem_ref(&iwqp->ibqp);
835 }
836
837 /**
838  * i40iw_terminate_start_timer - start terminate timeout
839  * @qp: hardware control qp
840  */
841 void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
842 {
843         struct i40iw_qp *iwqp;
844
845         iwqp = (struct i40iw_qp *)qp->back_qp;
846         i40iw_add_ref(&iwqp->ibqp);
847         init_timer(&iwqp->terminate_timer);
848         iwqp->terminate_timer.function = i40iw_terminate_timeout;
849         iwqp->terminate_timer.expires = jiffies + HZ;
850         iwqp->terminate_timer.data = (unsigned long)iwqp;
851         add_timer(&iwqp->terminate_timer);
852 }
853
854 /**
855  * i40iw_terminate_del_timer - delete terminate timeout
856  * @qp: hardware control qp
857  */
858 void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
859 {
860         struct i40iw_qp *iwqp;
861
862         iwqp = (struct i40iw_qp *)qp->back_qp;
863         if (del_timer(&iwqp->terminate_timer))
864                 i40iw_rem_ref(&iwqp->ibqp);
865 }
866
867 /**
868  * i40iw_cqp_generic_worker - generic worker for cqp
869  * @work: work pointer
870  */
871 static void i40iw_cqp_generic_worker(struct work_struct *work)
872 {
873         struct i40iw_virtchnl_work_info *work_info =
874             &((struct virtchnl_work *)work)->work_info;
875
876         if (work_info->worker_vf_dev)
877                 work_info->callback_fcn(work_info->worker_vf_dev);
878 }
879
880 /**
881  * i40iw_cqp_spawn_worker - spawn worket thread
882  * @iwdev: device struct pointer
883  * @work_info: work request info
884  * @iw_vf_idx: virtual function index
885  */
886 void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
887                             struct i40iw_virtchnl_work_info *work_info,
888                             u32 iw_vf_idx)
889 {
890         struct virtchnl_work *work;
891         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
892
893         work = &iwdev->virtchnl_w[iw_vf_idx];
894         memcpy(&work->work_info, work_info, sizeof(*work_info));
895         INIT_WORK(&work->work, i40iw_cqp_generic_worker);
896         queue_work(iwdev->virtchnl_wq, &work->work);
897 }
898
899 /**
900  * i40iw_cqp_manage_hmc_fcn_worker -
901  * @work: work pointer for hmc info
902  */
903 static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
904 {
905         struct i40iw_cqp_request *cqp_request =
906             ((struct virtchnl_work *)work)->cqp_request;
907         struct i40iw_ccq_cqe_info ccq_cqe_info;
908         struct i40iw_hmc_fcn_info *hmcfcninfo =
909                         &cqp_request->info.in.u.manage_hmc_pm.info;
910         struct i40iw_device *iwdev =
911             (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
912
913         ccq_cqe_info.cqp = NULL;
914         ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
915         ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
916         ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
917         ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
918         ccq_cqe_info.scratch = 0;
919         ccq_cqe_info.error = cqp_request->compl_info.error;
920         hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
921                                  hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
922         i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
923 }
924
925 /**
926  * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
927  * @cqp_request: cqp request info struct for hmc fun
928  * @unused: unused param of callback
929  */
930 static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
931                                               u32 unused)
932 {
933         struct virtchnl_work *work;
934         struct i40iw_hmc_fcn_info *hmcfcninfo =
935             &cqp_request->info.in.u.manage_hmc_pm.info;
936         struct i40iw_device *iwdev =
937             (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
938             back_dev;
939
940         if (hmcfcninfo && hmcfcninfo->callback_fcn) {
941                 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
942                 atomic_inc(&cqp_request->refcount);
943                 work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
944                 work->cqp_request = cqp_request;
945                 INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
946                 queue_work(iwdev->virtchnl_wq, &work->work);
947                 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
948         } else {
949                 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
950         }
951 }
952
953 /**
954  * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
955  * @dev: hardware control device structure
956  * @hmcfcninfo: info for hmc
957  */
958 enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
959                                                     struct i40iw_hmc_fcn_info *hmcfcninfo)
960 {
961         enum i40iw_status_code status;
962         struct i40iw_cqp_request *cqp_request;
963         struct cqp_commands_info *cqp_info;
964         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
965
966         i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
967         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
968         if (!cqp_request)
969                 return I40IW_ERR_NO_MEMORY;
970         cqp_info = &cqp_request->info;
971         cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
972         cqp_request->param = hmcfcninfo;
973         memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
974                sizeof(*hmcfcninfo));
975         cqp_info->in.u.manage_hmc_pm.dev = dev;
976         cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
977         cqp_info->post_sq = 1;
978         cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
979         status = i40iw_handle_cqp_op(iwdev, cqp_request);
980         if (status)
981                 i40iw_pr_err("CQP-OP Manage HMC fail");
982         return status;
983 }
984
985 /**
986  * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
987  * @iwdev: function device struct
988  * @values_mem: buffer for fpm
989  * @hmc_fn_id: function id for fpm
990  */
991 enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
992                                                       struct i40iw_dma_mem *values_mem,
993                                                       u8 hmc_fn_id)
994 {
995         enum i40iw_status_code status;
996         struct i40iw_cqp_request *cqp_request;
997         struct cqp_commands_info *cqp_info;
998         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
999
1000         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1001         if (!cqp_request)
1002                 return I40IW_ERR_NO_MEMORY;
1003         cqp_info = &cqp_request->info;
1004         cqp_request->param = NULL;
1005         cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
1006         cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
1007         cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
1008         cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
1009         cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
1010         cqp_info->post_sq = 1;
1011         cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
1012         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1013         if (status)
1014                 i40iw_pr_err("CQP-OP Query FPM fail");
1015         return status;
1016 }
1017
1018 /**
1019  * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
1020  * @dev: hardware control device structure
1021  * @values_mem: buffer with fpm values
1022  * @hmc_fn_id: function id for fpm
1023  */
1024 enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
1025                                                        struct i40iw_dma_mem *values_mem,
1026                                                        u8 hmc_fn_id)
1027 {
1028         enum i40iw_status_code status;
1029         struct i40iw_cqp_request *cqp_request;
1030         struct cqp_commands_info *cqp_info;
1031         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1032
1033         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1034         if (!cqp_request)
1035                 return I40IW_ERR_NO_MEMORY;
1036         cqp_info = &cqp_request->info;
1037         cqp_request->param = NULL;
1038         cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
1039         cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
1040         cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
1041         cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
1042         cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
1043         cqp_info->post_sq = 1;
1044         cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
1045         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1046         if (status)
1047                 i40iw_pr_err("CQP-OP Commit FPM fail");
1048         return status;
1049 }
1050
1051 /**
1052  * i40iw_vf_wait_vchnl_resp - wait for channel msg
1053  * @iwdev: function's device struct
1054  */
1055 enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
1056 {
1057         struct i40iw_device *iwdev = dev->back_dev;
1058         int timeout_ret;
1059
1060         i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
1061                     __func__, __LINE__, dev, iwdev);
1062
1063         atomic_set(&iwdev->vchnl_msgs, 2);
1064         timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
1065                                          (atomic_read(&iwdev->vchnl_msgs) == 1),
1066                                          I40IW_VCHNL_EVENT_TIMEOUT);
1067         atomic_dec(&iwdev->vchnl_msgs);
1068         if (!timeout_ret) {
1069                 i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
1070                 atomic_set(&iwdev->vchnl_msgs, 0);
1071                 dev->vchnl_up = false;
1072                 return I40IW_ERR_TIMEOUT;
1073         }
1074         wake_up(&dev->vf_reqs);
1075         return 0;
1076 }
1077
1078 /**
1079  * i40iw_cqp_cq_create_cmd - create a cq for the cqp
1080  * @dev: device pointer
1081  * @cq: pointer to created cq
1082  */
1083 enum i40iw_status_code i40iw_cqp_cq_create_cmd(struct i40iw_sc_dev *dev,
1084                                                struct i40iw_sc_cq *cq)
1085 {
1086         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1087         struct i40iw_cqp *iwcqp = &iwdev->cqp;
1088         struct i40iw_cqp_request *cqp_request;
1089         struct cqp_commands_info *cqp_info;
1090         enum i40iw_status_code status;
1091
1092         cqp_request = i40iw_get_cqp_request(iwcqp, true);
1093         if (!cqp_request)
1094                 return I40IW_ERR_NO_MEMORY;
1095
1096         cqp_info = &cqp_request->info;
1097         cqp_info->cqp_cmd = OP_CQ_CREATE;
1098         cqp_info->post_sq = 1;
1099         cqp_info->in.u.cq_create.cq = cq;
1100         cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
1101         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1102         if (status)
1103                 i40iw_pr_err("CQP-OP Create QP fail");
1104
1105         return status;
1106 }
1107
1108 /**
1109  * i40iw_cqp_qp_create_cmd - create a qp for the cqp
1110  * @dev: device pointer
1111  * @qp: pointer to created qp
1112  */
1113 enum i40iw_status_code i40iw_cqp_qp_create_cmd(struct i40iw_sc_dev *dev,
1114                                                struct i40iw_sc_qp *qp)
1115 {
1116         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1117         struct i40iw_cqp *iwcqp = &iwdev->cqp;
1118         struct i40iw_cqp_request *cqp_request;
1119         struct cqp_commands_info *cqp_info;
1120         struct i40iw_create_qp_info *qp_info;
1121         enum i40iw_status_code status;
1122
1123         cqp_request = i40iw_get_cqp_request(iwcqp, true);
1124         if (!cqp_request)
1125                 return I40IW_ERR_NO_MEMORY;
1126
1127         cqp_info = &cqp_request->info;
1128         qp_info = &cqp_request->info.in.u.qp_create.info;
1129
1130         memset(qp_info, 0, sizeof(*qp_info));
1131
1132         qp_info->cq_num_valid = true;
1133         qp_info->next_iwarp_state = I40IW_QP_STATE_RTS;
1134
1135         cqp_info->cqp_cmd = OP_QP_CREATE;
1136         cqp_info->post_sq = 1;
1137         cqp_info->in.u.qp_create.qp = qp;
1138         cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
1139         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1140         if (status)
1141                 i40iw_pr_err("CQP-OP QP create fail");
1142         return status;
1143 }
1144
1145 /**
1146  * i40iw_cqp_cq_destroy_cmd - destroy the cqp cq
1147  * @dev: device pointer
1148  * @cq: pointer to cq
1149  */
1150 void i40iw_cqp_cq_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
1151 {
1152         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1153
1154         i40iw_cq_wq_destroy(iwdev, cq);
1155 }
1156
1157 /**
1158  * i40iw_cqp_qp_destroy_cmd - destroy the cqp
1159  * @dev: device pointer
1160  * @qp: pointer to qp
1161  */
1162 void i40iw_cqp_qp_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1163 {
1164         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1165         struct i40iw_cqp *iwcqp = &iwdev->cqp;
1166         struct i40iw_cqp_request *cqp_request;
1167         struct cqp_commands_info *cqp_info;
1168         enum i40iw_status_code status;
1169
1170         cqp_request = i40iw_get_cqp_request(iwcqp, true);
1171         if (!cqp_request)
1172                 return;
1173
1174         cqp_info = &cqp_request->info;
1175         memset(cqp_info, 0, sizeof(*cqp_info));
1176
1177         cqp_info->cqp_cmd = OP_QP_DESTROY;
1178         cqp_info->post_sq = 1;
1179         cqp_info->in.u.qp_destroy.qp = qp;
1180         cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
1181         cqp_info->in.u.qp_destroy.remove_hash_idx = true;
1182         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1183         if (status)
1184                 i40iw_pr_err("CQP QP_DESTROY fail");
1185 }
1186
1187
1188 /**
1189  * i40iw_ieq_mpa_crc_ae - generate AE for crc error
1190  * @dev: hardware control device structure
1191  * @qp: hardware control qp
1192  */
1193 void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1194 {
1195         struct i40iw_qp_flush_info info;
1196         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1197
1198         i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
1199         memset(&info, 0, sizeof(info));
1200         info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
1201         info.generate_ae = true;
1202         info.ae_source = 0x3;
1203         (void)i40iw_hw_flush_wqes(iwdev, qp, &info, false);
1204 }
1205
1206 /**
1207  * i40iw_init_hash_desc - initialize hash for crc calculation
1208  * @desc: cryption type
1209  */
1210 enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
1211 {
1212         struct crypto_shash *tfm;
1213         struct shash_desc *tdesc;
1214
1215         tfm = crypto_alloc_shash("crc32c", 0, 0);
1216         if (IS_ERR(tfm))
1217                 return I40IW_ERR_MPA_CRC;
1218
1219         tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
1220                         GFP_KERNEL);
1221         if (!tdesc) {
1222                 crypto_free_shash(tfm);
1223                 return I40IW_ERR_MPA_CRC;
1224         }
1225         tdesc->tfm = tfm;
1226         *desc = tdesc;
1227
1228         return 0;
1229 }
1230
1231 /**
1232  * i40iw_free_hash_desc - free hash desc
1233  * @desc: to be freed
1234  */
1235 void i40iw_free_hash_desc(struct shash_desc *desc)
1236 {
1237         if (desc) {
1238                 crypto_free_shash(desc->tfm);
1239                 kfree(desc);
1240         }
1241 }
1242
1243 /**
1244  * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
1245  * @dev: hardware control device structure
1246  * @mem: buffer ptr for fpm to be allocated
1247  * @return: memory allocation status
1248  */
1249 enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
1250                                                  struct i40iw_dma_mem *mem)
1251 {
1252         enum i40iw_status_code status;
1253         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1254
1255         status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
1256                                        I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1257         return status;
1258 }
1259
1260 /**
1261  * i40iw_ieq_check_mpacrc - check if mpa crc is OK
1262  * @desc: desc for hash
1263  * @addr: address of buffer for crc
1264  * @length: length of buffer
1265  * @value: value to be compared
1266  */
1267 enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
1268                                               void *addr,
1269                                               u32 length,
1270                                               u32 value)
1271 {
1272         u32 crc = 0;
1273         int ret;
1274         enum i40iw_status_code ret_code = 0;
1275
1276         crypto_shash_init(desc);
1277         ret = crypto_shash_update(desc, addr, length);
1278         if (!ret)
1279                 crypto_shash_final(desc, (u8 *)&crc);
1280         if (crc != value) {
1281                 i40iw_pr_err("mpa crc check fail\n");
1282                 ret_code = I40IW_ERR_MPA_CRC;
1283         }
1284         return ret_code;
1285 }
1286
1287 /**
1288  * i40iw_ieq_get_qp - get qp based on quad in puda buffer
1289  * @dev: hardware control device structure
1290  * @buf: receive puda buffer on exception q
1291  */
1292 struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
1293                                      struct i40iw_puda_buf *buf)
1294 {
1295         struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1296         struct i40iw_qp *iwqp;
1297         struct i40iw_cm_node *cm_node;
1298         u32 loc_addr[4], rem_addr[4];
1299         u16 loc_port, rem_port;
1300         struct ipv6hdr *ip6h;
1301         struct iphdr *iph = (struct iphdr *)buf->iph;
1302         struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1303
1304         if (iph->version == 4) {
1305                 memset(loc_addr, 0, sizeof(loc_addr));
1306                 loc_addr[0] = ntohl(iph->daddr);
1307                 memset(rem_addr, 0, sizeof(rem_addr));
1308                 rem_addr[0] = ntohl(iph->saddr);
1309         } else {
1310                 ip6h = (struct ipv6hdr *)buf->iph;
1311                 i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1312                 i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1313         }
1314         loc_port = ntohs(tcph->dest);
1315         rem_port = ntohs(tcph->source);
1316
1317         cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
1318                                   loc_addr, false);
1319         if (!cm_node)
1320                 return NULL;
1321         iwqp = cm_node->iwqp;
1322         return &iwqp->sc_qp;
1323 }
1324
1325 /**
1326  * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
1327  * @buf: puda to update
1328  * @length: length of buffer
1329  * @seqnum: seq number for tcp
1330  */
1331 void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
1332 {
1333         struct tcphdr *tcph;
1334         struct iphdr *iph;
1335         u16 iphlen;
1336         u16 packetsize;
1337         u8 *addr = (u8 *)buf->mem.va;
1338
1339         iphlen = (buf->ipv4) ? 20 : 40;
1340         iph = (struct iphdr *)(addr + buf->maclen);
1341         tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1342         packetsize = length + buf->tcphlen + iphlen;
1343
1344         iph->tot_len = htons(packetsize);
1345         tcph->seq = htonl(seqnum);
1346 }
1347
1348 /**
1349  * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
1350  * @info: to get information
1351  * @buf: puda buffer
1352  */
1353 enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
1354                                                  struct i40iw_puda_buf *buf)
1355 {
1356         struct iphdr *iph;
1357         struct ipv6hdr *ip6h;
1358         struct tcphdr *tcph;
1359         u16 iphlen;
1360         u16 pkt_len;
1361         u8 *mem = (u8 *)buf->mem.va;
1362         struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
1363
1364         if (ethh->h_proto == htons(0x8100)) {
1365                 info->vlan_valid = true;
1366                 buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
1367         }
1368         buf->maclen = (info->vlan_valid) ? 18 : 14;
1369         iphlen = (info->l3proto) ? 40 : 20;
1370         buf->ipv4 = (info->l3proto) ? false : true;
1371         buf->iph = mem + buf->maclen;
1372         iph = (struct iphdr *)buf->iph;
1373
1374         buf->tcph = buf->iph + iphlen;
1375         tcph = (struct tcphdr *)buf->tcph;
1376
1377         if (buf->ipv4) {
1378                 pkt_len = ntohs(iph->tot_len);
1379         } else {
1380                 ip6h = (struct ipv6hdr *)buf->iph;
1381                 pkt_len = ntohs(ip6h->payload_len) + iphlen;
1382         }
1383
1384         buf->totallen = pkt_len + buf->maclen;
1385
1386         if (info->payload_len < buf->totallen) {
1387                 i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
1388                              info->payload_len, buf->totallen);
1389                 return I40IW_ERR_INVALID_SIZE;
1390         }
1391
1392         buf->tcphlen = (tcph->doff) << 2;
1393         buf->datalen = pkt_len - iphlen - buf->tcphlen;
1394         buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
1395         buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1396         buf->seqnum = ntohl(tcph->seq);
1397         return 0;
1398 }
1399
1400 /**
1401  * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
1402  * @vsi: pointer to the vsi structure
1403  */
1404 static void i40iw_hw_stats_timeout(unsigned long vsi)
1405 {
1406         struct i40iw_sc_vsi *sc_vsi =  (struct i40iw_sc_vsi *)vsi;
1407         struct i40iw_sc_dev *pf_dev = sc_vsi->dev;
1408         struct i40iw_vsi_pestat *pf_devstat = sc_vsi->pestat;
1409         struct i40iw_vsi_pestat *vf_devstat = NULL;
1410         u16 iw_vf_idx;
1411         unsigned long flags;
1412
1413         /*PF*/
1414         i40iw_hw_stats_read_all(pf_devstat, &pf_devstat->hw_stats);
1415
1416         for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
1417                 spin_lock_irqsave(&pf_devstat->lock, flags);
1418                 if (pf_dev->vf_dev[iw_vf_idx]) {
1419                         if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
1420                                 vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->pestat;
1421                                 i40iw_hw_stats_read_all(vf_devstat, &vf_devstat->hw_stats);
1422                         }
1423                 }
1424                 spin_unlock_irqrestore(&pf_devstat->lock, flags);
1425         }
1426
1427         mod_timer(&pf_devstat->stats_timer,
1428                   jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1429 }
1430
1431 /**
1432  * i40iw_hw_stats_start_timer - Start periodic stats timer
1433  * @vsi: pointer to the vsi structure
1434  */
1435 void i40iw_hw_stats_start_timer(struct i40iw_sc_vsi *vsi)
1436 {
1437         struct i40iw_vsi_pestat *devstat = vsi->pestat;
1438
1439         init_timer(&devstat->stats_timer);
1440         devstat->stats_timer.function = i40iw_hw_stats_timeout;
1441         devstat->stats_timer.data = (unsigned long)vsi;
1442         mod_timer(&devstat->stats_timer,
1443                   jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1444 }
1445
1446 /**
1447  * i40iw_hw_stats_stop_timer - Delete periodic stats timer
1448  * @vsi: pointer to the vsi structure
1449  */
1450 void i40iw_hw_stats_stop_timer(struct i40iw_sc_vsi *vsi)
1451 {
1452         struct i40iw_vsi_pestat *devstat = vsi->pestat;
1453
1454         del_timer_sync(&devstat->stats_timer);
1455 }