Linux Audio

Check our new training course

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