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