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