Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1/**************************************************************************/
   2/*                                                                        */
   3/*  IBM System i and System p Virtual NIC Device Driver                   */
   4/*  Copyright (C) 2014 IBM Corp.                                          */
   5/*  Santiago Leon (santi_leon@yahoo.com)                                  */
   6/*  Thomas Falcon (tlfalcon@linux.vnet.ibm.com)                           */
   7/*  John Allen (jallen@linux.vnet.ibm.com)                                */
   8/*                                                                        */
   9/*  This program is free software; you can redistribute it and/or modify  */
  10/*  it under the terms of the GNU General Public License as published by  */
  11/*  the Free Software Foundation; either version 2 of the License, or     */
  12/*  (at your option) any later version.                                   */
  13/*                                                                        */
  14/*  This program is distributed in the hope that it will be useful,       */
  15/*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
  16/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
  17/*  GNU General Public License for more details.                          */
  18/*                                                                        */
  19/*  You should have received a copy of the GNU General Public License     */
  20/*  along with this program.                                              */
  21/*                                                                        */
  22/* This module contains the implementation of a virtual ethernet device   */
  23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN    */
  24/* option of the RS/6000 Platform Architecture to interface with virtual  */
  25/* ethernet NICs that are presented to the partition by the hypervisor.   */
  26/*									   */
  27/* Messages are passed between the VNIC driver and the VNIC server using  */
  28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to  */
  29/* issue and receive commands that initiate communication with the server */
  30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but    */
  31/* are used by the driver to notify the server that a packet is           */
  32/* ready for transmission or that a buffer has been added to receive a    */
  33/* packet. Subsequently, sCRQs are used by the server to notify the       */
  34/* driver that a packet transmission has been completed or that a packet  */
  35/* has been received and placed in a waiting buffer.                      */
  36/*                                                                        */
  37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in    */
  38/* which skbs are DMA mapped and immediately unmapped when the transmit   */
  39/* or receive has been completed, the VNIC driver is required to use      */
  40/* "long term mapping". This entails that large, continuous DMA mapped    */
  41/* buffers are allocated on driver initialization and these buffers are   */
  42/* then continuously reused to pass skbs to and from the VNIC server.     */
  43/*                                                                        */
  44/**************************************************************************/
  45
  46#include <linux/module.h>
  47#include <linux/moduleparam.h>
  48#include <linux/types.h>
  49#include <linux/errno.h>
  50#include <linux/completion.h>
  51#include <linux/ioport.h>
  52#include <linux/dma-mapping.h>
  53#include <linux/kernel.h>
  54#include <linux/netdevice.h>
  55#include <linux/etherdevice.h>
  56#include <linux/skbuff.h>
  57#include <linux/init.h>
  58#include <linux/delay.h>
  59#include <linux/mm.h>
  60#include <linux/ethtool.h>
  61#include <linux/proc_fs.h>
  62#include <linux/if_arp.h>
  63#include <linux/in.h>
  64#include <linux/ip.h>
  65#include <linux/ipv6.h>
  66#include <linux/irq.h>
  67#include <linux/kthread.h>
  68#include <linux/seq_file.h>
  69#include <linux/interrupt.h>
  70#include <net/net_namespace.h>
  71#include <asm/hvcall.h>
  72#include <linux/atomic.h>
  73#include <asm/vio.h>
  74#include <asm/iommu.h>
  75#include <linux/uaccess.h>
  76#include <asm/firmware.h>
  77#include <linux/workqueue.h>
  78#include <linux/if_vlan.h>
  79#include <linux/utsname.h>
  80
  81#include "ibmvnic.h"
  82
  83static const char ibmvnic_driver_name[] = "ibmvnic";
  84static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
  85
  86MODULE_AUTHOR("Santiago Leon");
  87MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
  88MODULE_LICENSE("GPL");
  89MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
  90
  91static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
  92static int ibmvnic_remove(struct vio_dev *);
  93static void release_sub_crqs(struct ibmvnic_adapter *, bool);
  94static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
  95static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
  96static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
  97static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
  98static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
  99		       union sub_crq *sub_crq);
 100static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
 101static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
 102static int enable_scrq_irq(struct ibmvnic_adapter *,
 103			   struct ibmvnic_sub_crq_queue *);
 104static int disable_scrq_irq(struct ibmvnic_adapter *,
 105			    struct ibmvnic_sub_crq_queue *);
 106static int pending_scrq(struct ibmvnic_adapter *,
 107			struct ibmvnic_sub_crq_queue *);
 108static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
 109					struct ibmvnic_sub_crq_queue *);
 110static int ibmvnic_poll(struct napi_struct *napi, int data);
 111static void send_map_query(struct ibmvnic_adapter *adapter);
 112static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
 113static void send_request_unmap(struct ibmvnic_adapter *, u8);
 114static int send_login(struct ibmvnic_adapter *adapter);
 115static void send_cap_queries(struct ibmvnic_adapter *adapter);
 116static int init_sub_crqs(struct ibmvnic_adapter *);
 117static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
 118static int ibmvnic_init(struct ibmvnic_adapter *);
 119static void release_crq_queue(struct ibmvnic_adapter *);
 120static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
 121static int init_crq_queue(struct ibmvnic_adapter *adapter);
 122
 123struct ibmvnic_stat {
 124	char name[ETH_GSTRING_LEN];
 125	int offset;
 126};
 127
 128#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
 129			     offsetof(struct ibmvnic_statistics, stat))
 130#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
 131
 132static const struct ibmvnic_stat ibmvnic_stats[] = {
 133	{"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
 134	{"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
 135	{"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
 136	{"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
 137	{"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
 138	{"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
 139	{"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
 140	{"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
 141	{"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
 142	{"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
 143	{"align_errors", IBMVNIC_STAT_OFF(align_errors)},
 144	{"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
 145	{"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
 146	{"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
 147	{"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
 148	{"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
 149	{"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
 150	{"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
 151	{"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
 152	{"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
 153	{"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
 154	{"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
 155};
 156
 157static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
 158			  unsigned long length, unsigned long *number,
 159			  unsigned long *irq)
 160{
 161	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
 162	long rc;
 163
 164	rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
 165	*number = retbuf[0];
 166	*irq = retbuf[1];
 167
 168	return rc;
 169}
 170
 171static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
 172				struct ibmvnic_long_term_buff *ltb, int size)
 173{
 174	struct device *dev = &adapter->vdev->dev;
 175
 176	ltb->size = size;
 177	ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
 178				       GFP_KERNEL);
 179
 180	if (!ltb->buff) {
 181		dev_err(dev, "Couldn't alloc long term buffer\n");
 182		return -ENOMEM;
 183	}
 184	ltb->map_id = adapter->map_id;
 185	adapter->map_id++;
 186
 187	init_completion(&adapter->fw_done);
 188	send_request_map(adapter, ltb->addr,
 189			 ltb->size, ltb->map_id);
 190	wait_for_completion(&adapter->fw_done);
 191
 192	if (adapter->fw_done_rc) {
 193		dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
 194			adapter->fw_done_rc);
 195		dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
 196		return -1;
 197	}
 198	return 0;
 199}
 200
 201static void free_long_term_buff(struct ibmvnic_adapter *adapter,
 202				struct ibmvnic_long_term_buff *ltb)
 203{
 204	struct device *dev = &adapter->vdev->dev;
 205
 206	if (!ltb->buff)
 207		return;
 208
 209	if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
 210	    adapter->reset_reason != VNIC_RESET_MOBILITY)
 211		send_request_unmap(adapter, ltb->map_id);
 212	dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
 213}
 214
 215static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
 216				struct ibmvnic_long_term_buff *ltb)
 217{
 218	memset(ltb->buff, 0, ltb->size);
 219
 220	init_completion(&adapter->fw_done);
 221	send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
 222	wait_for_completion(&adapter->fw_done);
 223
 224	if (adapter->fw_done_rc) {
 225		dev_info(&adapter->vdev->dev,
 226			 "Reset failed, attempting to free and reallocate buffer\n");
 227		free_long_term_buff(adapter, ltb);
 228		return alloc_long_term_buff(adapter, ltb, ltb->size);
 229	}
 230	return 0;
 231}
 232
 233static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
 234{
 235	int i;
 236
 237	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
 238	     i++)
 239		adapter->rx_pool[i].active = 0;
 240}
 241
 242static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
 243			      struct ibmvnic_rx_pool *pool)
 244{
 245	int count = pool->size - atomic_read(&pool->available);
 246	struct device *dev = &adapter->vdev->dev;
 247	int buffers_added = 0;
 248	unsigned long lpar_rc;
 249	union sub_crq sub_crq;
 250	struct sk_buff *skb;
 251	unsigned int offset;
 252	dma_addr_t dma_addr;
 253	unsigned char *dst;
 254	u64 *handle_array;
 255	int shift = 0;
 256	int index;
 257	int i;
 258
 259	if (!pool->active)
 260		return;
 261
 262	handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
 263				      be32_to_cpu(adapter->login_rsp_buf->
 264				      off_rxadd_subcrqs));
 265
 266	for (i = 0; i < count; ++i) {
 267		skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
 268		if (!skb) {
 269			dev_err(dev, "Couldn't replenish rx buff\n");
 270			adapter->replenish_no_mem++;
 271			break;
 272		}
 273
 274		index = pool->free_map[pool->next_free];
 275
 276		if (pool->rx_buff[index].skb)
 277			dev_err(dev, "Inconsistent free_map!\n");
 278
 279		/* Copy the skb to the long term mapped DMA buffer */
 280		offset = index * pool->buff_size;
 281		dst = pool->long_term_buff.buff + offset;
 282		memset(dst, 0, pool->buff_size);
 283		dma_addr = pool->long_term_buff.addr + offset;
 284		pool->rx_buff[index].data = dst;
 285
 286		pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
 287		pool->rx_buff[index].dma = dma_addr;
 288		pool->rx_buff[index].skb = skb;
 289		pool->rx_buff[index].pool_index = pool->index;
 290		pool->rx_buff[index].size = pool->buff_size;
 291
 292		memset(&sub_crq, 0, sizeof(sub_crq));
 293		sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
 294		sub_crq.rx_add.correlator =
 295		    cpu_to_be64((u64)&pool->rx_buff[index]);
 296		sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
 297		sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
 298
 299		/* The length field of the sCRQ is defined to be 24 bits so the
 300		 * buffer size needs to be left shifted by a byte before it is
 301		 * converted to big endian to prevent the last byte from being
 302		 * truncated.
 303		 */
 304#ifdef __LITTLE_ENDIAN__
 305		shift = 8;
 306#endif
 307		sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
 308
 309		lpar_rc = send_subcrq(adapter, handle_array[pool->index],
 310				      &sub_crq);
 311		if (lpar_rc != H_SUCCESS)
 312			goto failure;
 313
 314		buffers_added++;
 315		adapter->replenish_add_buff_success++;
 316		pool->next_free = (pool->next_free + 1) % pool->size;
 317	}
 318	atomic_add(buffers_added, &pool->available);
 319	return;
 320
 321failure:
 322	dev_info(dev, "replenish pools failure\n");
 323	pool->free_map[pool->next_free] = index;
 324	pool->rx_buff[index].skb = NULL;
 325
 326	dev_kfree_skb_any(skb);
 327	adapter->replenish_add_buff_failure++;
 328	atomic_add(buffers_added, &pool->available);
 329
 330	if (lpar_rc == H_CLOSED || adapter->failover_pending) {
 331		/* Disable buffer pool replenishment and report carrier off if
 332		 * queue is closed or pending failover.
 333		 * Firmware guarantees that a signal will be sent to the
 334		 * driver, triggering a reset.
 335		 */
 336		deactivate_rx_pools(adapter);
 337		netif_carrier_off(adapter->netdev);
 338	}
 339}
 340
 341static void replenish_pools(struct ibmvnic_adapter *adapter)
 342{
 343	int i;
 344
 345	adapter->replenish_task_cycles++;
 346	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
 347	     i++) {
 348		if (adapter->rx_pool[i].active)
 349			replenish_rx_pool(adapter, &adapter->rx_pool[i]);
 350	}
 351}
 352
 353static void release_stats_buffers(struct ibmvnic_adapter *adapter)
 354{
 355	kfree(adapter->tx_stats_buffers);
 356	kfree(adapter->rx_stats_buffers);
 357	adapter->tx_stats_buffers = NULL;
 358	adapter->rx_stats_buffers = NULL;
 359}
 360
 361static int init_stats_buffers(struct ibmvnic_adapter *adapter)
 362{
 363	adapter->tx_stats_buffers =
 364				kcalloc(IBMVNIC_MAX_QUEUES,
 365					sizeof(struct ibmvnic_tx_queue_stats),
 366					GFP_KERNEL);
 367	if (!adapter->tx_stats_buffers)
 368		return -ENOMEM;
 369
 370	adapter->rx_stats_buffers =
 371				kcalloc(IBMVNIC_MAX_QUEUES,
 372					sizeof(struct ibmvnic_rx_queue_stats),
 373					GFP_KERNEL);
 374	if (!adapter->rx_stats_buffers)
 375		return -ENOMEM;
 376
 377	return 0;
 378}
 379
 380static void release_stats_token(struct ibmvnic_adapter *adapter)
 381{
 382	struct device *dev = &adapter->vdev->dev;
 383
 384	if (!adapter->stats_token)
 385		return;
 386
 387	dma_unmap_single(dev, adapter->stats_token,
 388			 sizeof(struct ibmvnic_statistics),
 389			 DMA_FROM_DEVICE);
 390	adapter->stats_token = 0;
 391}
 392
 393static int init_stats_token(struct ibmvnic_adapter *adapter)
 394{
 395	struct device *dev = &adapter->vdev->dev;
 396	dma_addr_t stok;
 397
 398	stok = dma_map_single(dev, &adapter->stats,
 399			      sizeof(struct ibmvnic_statistics),
 400			      DMA_FROM_DEVICE);
 401	if (dma_mapping_error(dev, stok)) {
 402		dev_err(dev, "Couldn't map stats buffer\n");
 403		return -1;
 404	}
 405
 406	adapter->stats_token = stok;
 407	netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
 408	return 0;
 409}
 410
 411static int reset_rx_pools(struct ibmvnic_adapter *adapter)
 412{
 413	struct ibmvnic_rx_pool *rx_pool;
 414	int rx_scrqs;
 415	int i, j, rc;
 416	u64 *size_array;
 417
 418	size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
 419		be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
 420
 421	rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
 422	for (i = 0; i < rx_scrqs; i++) {
 423		rx_pool = &adapter->rx_pool[i];
 424
 425		netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
 426
 427		if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
 428			free_long_term_buff(adapter, &rx_pool->long_term_buff);
 429			rx_pool->buff_size = be64_to_cpu(size_array[i]);
 430			alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
 431					     rx_pool->size *
 432					     rx_pool->buff_size);
 433		} else {
 434			rc = reset_long_term_buff(adapter,
 435						  &rx_pool->long_term_buff);
 436		}
 437
 438		if (rc)
 439			return rc;
 440
 441		for (j = 0; j < rx_pool->size; j++)
 442			rx_pool->free_map[j] = j;
 443
 444		memset(rx_pool->rx_buff, 0,
 445		       rx_pool->size * sizeof(struct ibmvnic_rx_buff));
 446
 447		atomic_set(&rx_pool->available, 0);
 448		rx_pool->next_alloc = 0;
 449		rx_pool->next_free = 0;
 450		rx_pool->active = 1;
 451	}
 452
 453	return 0;
 454}
 455
 456static void release_rx_pools(struct ibmvnic_adapter *adapter)
 457{
 458	struct ibmvnic_rx_pool *rx_pool;
 459	int i, j;
 460
 461	if (!adapter->rx_pool)
 462		return;
 463
 464	for (i = 0; i < adapter->num_active_rx_pools; i++) {
 465		rx_pool = &adapter->rx_pool[i];
 466
 467		netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
 468
 469		kfree(rx_pool->free_map);
 470		free_long_term_buff(adapter, &rx_pool->long_term_buff);
 471
 472		if (!rx_pool->rx_buff)
 473			continue;
 474
 475		for (j = 0; j < rx_pool->size; j++) {
 476			if (rx_pool->rx_buff[j].skb) {
 477				dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
 478				rx_pool->rx_buff[i].skb = NULL;
 479			}
 480		}
 481
 482		kfree(rx_pool->rx_buff);
 483	}
 484
 485	kfree(adapter->rx_pool);
 486	adapter->rx_pool = NULL;
 487	adapter->num_active_rx_pools = 0;
 488}
 489
 490static int init_rx_pools(struct net_device *netdev)
 491{
 492	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 493	struct device *dev = &adapter->vdev->dev;
 494	struct ibmvnic_rx_pool *rx_pool;
 495	int rxadd_subcrqs;
 496	u64 *size_array;
 497	int i, j;
 498
 499	rxadd_subcrqs =
 500		be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
 501	size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
 502		be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
 503
 504	adapter->rx_pool = kcalloc(rxadd_subcrqs,
 505				   sizeof(struct ibmvnic_rx_pool),
 506				   GFP_KERNEL);
 507	if (!adapter->rx_pool) {
 508		dev_err(dev, "Failed to allocate rx pools\n");
 509		return -1;
 510	}
 511
 512	adapter->num_active_rx_pools = rxadd_subcrqs;
 513
 514	for (i = 0; i < rxadd_subcrqs; i++) {
 515		rx_pool = &adapter->rx_pool[i];
 516
 517		netdev_dbg(adapter->netdev,
 518			   "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
 519			   i, adapter->req_rx_add_entries_per_subcrq,
 520			   be64_to_cpu(size_array[i]));
 521
 522		rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
 523		rx_pool->index = i;
 524		rx_pool->buff_size = be64_to_cpu(size_array[i]);
 525		rx_pool->active = 1;
 526
 527		rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
 528					    GFP_KERNEL);
 529		if (!rx_pool->free_map) {
 530			release_rx_pools(adapter);
 531			return -1;
 532		}
 533
 534		rx_pool->rx_buff = kcalloc(rx_pool->size,
 535					   sizeof(struct ibmvnic_rx_buff),
 536					   GFP_KERNEL);
 537		if (!rx_pool->rx_buff) {
 538			dev_err(dev, "Couldn't alloc rx buffers\n");
 539			release_rx_pools(adapter);
 540			return -1;
 541		}
 542
 543		if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
 544					 rx_pool->size * rx_pool->buff_size)) {
 545			release_rx_pools(adapter);
 546			return -1;
 547		}
 548
 549		for (j = 0; j < rx_pool->size; ++j)
 550			rx_pool->free_map[j] = j;
 551
 552		atomic_set(&rx_pool->available, 0);
 553		rx_pool->next_alloc = 0;
 554		rx_pool->next_free = 0;
 555	}
 556
 557	return 0;
 558}
 559
 560static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
 561			     struct ibmvnic_tx_pool *tx_pool)
 562{
 563	int rc, i;
 564
 565	rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
 566	if (rc)
 567		return rc;
 568
 569	memset(tx_pool->tx_buff, 0,
 570	       tx_pool->num_buffers *
 571	       sizeof(struct ibmvnic_tx_buff));
 572
 573	for (i = 0; i < tx_pool->num_buffers; i++)
 574		tx_pool->free_map[i] = i;
 575
 576	tx_pool->consumer_index = 0;
 577	tx_pool->producer_index = 0;
 578
 579	return 0;
 580}
 581
 582static int reset_tx_pools(struct ibmvnic_adapter *adapter)
 583{
 584	int tx_scrqs;
 585	int i, rc;
 586
 587	tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
 588	for (i = 0; i < tx_scrqs; i++) {
 589		rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
 590		if (rc)
 591			return rc;
 592		rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
 593		if (rc)
 594			return rc;
 595	}
 596
 597	return 0;
 598}
 599
 600static void release_vpd_data(struct ibmvnic_adapter *adapter)
 601{
 602	if (!adapter->vpd)
 603		return;
 604
 605	kfree(adapter->vpd->buff);
 606	kfree(adapter->vpd);
 607
 608	adapter->vpd = NULL;
 609}
 610
 611static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
 612				struct ibmvnic_tx_pool *tx_pool)
 613{
 614	kfree(tx_pool->tx_buff);
 615	kfree(tx_pool->free_map);
 616	free_long_term_buff(adapter, &tx_pool->long_term_buff);
 617}
 618
 619static void release_tx_pools(struct ibmvnic_adapter *adapter)
 620{
 621	int i;
 622
 623	if (!adapter->tx_pool)
 624		return;
 625
 626	for (i = 0; i < adapter->num_active_tx_pools; i++) {
 627		release_one_tx_pool(adapter, &adapter->tx_pool[i]);
 628		release_one_tx_pool(adapter, &adapter->tso_pool[i]);
 629	}
 630
 631	kfree(adapter->tx_pool);
 632	adapter->tx_pool = NULL;
 633	kfree(adapter->tso_pool);
 634	adapter->tso_pool = NULL;
 635	adapter->num_active_tx_pools = 0;
 636}
 637
 638static int init_one_tx_pool(struct net_device *netdev,
 639			    struct ibmvnic_tx_pool *tx_pool,
 640			    int num_entries, int buf_size)
 641{
 642	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 643	int i;
 644
 645	tx_pool->tx_buff = kcalloc(num_entries,
 646				   sizeof(struct ibmvnic_tx_buff),
 647				   GFP_KERNEL);
 648	if (!tx_pool->tx_buff)
 649		return -1;
 650
 651	if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
 652				 num_entries * buf_size))
 653		return -1;
 654
 655	tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
 656	if (!tx_pool->free_map)
 657		return -1;
 658
 659	for (i = 0; i < num_entries; i++)
 660		tx_pool->free_map[i] = i;
 661
 662	tx_pool->consumer_index = 0;
 663	tx_pool->producer_index = 0;
 664	tx_pool->num_buffers = num_entries;
 665	tx_pool->buf_size = buf_size;
 666
 667	return 0;
 668}
 669
 670static int init_tx_pools(struct net_device *netdev)
 671{
 672	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 673	int tx_subcrqs;
 674	int i, rc;
 675
 676	tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
 677	adapter->tx_pool = kcalloc(tx_subcrqs,
 678				   sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
 679	if (!adapter->tx_pool)
 680		return -1;
 681
 682	adapter->tso_pool = kcalloc(tx_subcrqs,
 683				    sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
 684	if (!adapter->tso_pool)
 685		return -1;
 686
 687	adapter->num_active_tx_pools = tx_subcrqs;
 688
 689	for (i = 0; i < tx_subcrqs; i++) {
 690		rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
 691				      adapter->req_tx_entries_per_subcrq,
 692				      adapter->req_mtu + VLAN_HLEN);
 693		if (rc) {
 694			release_tx_pools(adapter);
 695			return rc;
 696		}
 697
 698		init_one_tx_pool(netdev, &adapter->tso_pool[i],
 699				 IBMVNIC_TSO_BUFS,
 700				 IBMVNIC_TSO_BUF_SZ);
 701		if (rc) {
 702			release_tx_pools(adapter);
 703			return rc;
 704		}
 705	}
 706
 707	return 0;
 708}
 709
 710static void release_error_buffers(struct ibmvnic_adapter *adapter)
 711{
 712	struct device *dev = &adapter->vdev->dev;
 713	struct ibmvnic_error_buff *error_buff, *tmp;
 714	unsigned long flags;
 715
 716	spin_lock_irqsave(&adapter->error_list_lock, flags);
 717	list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
 718		list_del(&error_buff->list);
 719		dma_unmap_single(dev, error_buff->dma, error_buff->len,
 720				 DMA_FROM_DEVICE);
 721		kfree(error_buff->buff);
 722		kfree(error_buff);
 723	}
 724	spin_unlock_irqrestore(&adapter->error_list_lock, flags);
 725}
 726
 727static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
 728{
 729	int i;
 730
 731	if (adapter->napi_enabled)
 732		return;
 733
 734	for (i = 0; i < adapter->req_rx_queues; i++)
 735		napi_enable(&adapter->napi[i]);
 736
 737	adapter->napi_enabled = true;
 738}
 739
 740static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
 741{
 742	int i;
 743
 744	if (!adapter->napi_enabled)
 745		return;
 746
 747	for (i = 0; i < adapter->req_rx_queues; i++) {
 748		netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
 749		napi_disable(&adapter->napi[i]);
 750	}
 751
 752	adapter->napi_enabled = false;
 753}
 754
 755static int init_napi(struct ibmvnic_adapter *adapter)
 756{
 757	int i;
 758
 759	adapter->napi = kcalloc(adapter->req_rx_queues,
 760				sizeof(struct napi_struct), GFP_KERNEL);
 761	if (!adapter->napi)
 762		return -ENOMEM;
 763
 764	for (i = 0; i < adapter->req_rx_queues; i++) {
 765		netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
 766		netif_napi_add(adapter->netdev, &adapter->napi[i],
 767			       ibmvnic_poll, NAPI_POLL_WEIGHT);
 768	}
 769
 770	adapter->num_active_rx_napi = adapter->req_rx_queues;
 771	return 0;
 772}
 773
 774static void release_napi(struct ibmvnic_adapter *adapter)
 775{
 776	int i;
 777
 778	if (!adapter->napi)
 779		return;
 780
 781	for (i = 0; i < adapter->num_active_rx_napi; i++) {
 782		if (&adapter->napi[i]) {
 783			netdev_dbg(adapter->netdev,
 784				   "Releasing napi[%d]\n", i);
 785			netif_napi_del(&adapter->napi[i]);
 786		}
 787	}
 788
 789	kfree(adapter->napi);
 790	adapter->napi = NULL;
 791	adapter->num_active_rx_napi = 0;
 792}
 793
 794static int ibmvnic_login(struct net_device *netdev)
 795{
 796	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 797	unsigned long timeout = msecs_to_jiffies(30000);
 798	int retry_count = 0;
 799	bool retry;
 800	int rc;
 801
 802	do {
 803		retry = false;
 804		if (retry_count > IBMVNIC_MAX_QUEUES) {
 805			netdev_warn(netdev, "Login attempts exceeded\n");
 806			return -1;
 807		}
 808
 809		adapter->init_done_rc = 0;
 810		reinit_completion(&adapter->init_done);
 811		rc = send_login(adapter);
 812		if (rc) {
 813			netdev_warn(netdev, "Unable to login\n");
 814			return rc;
 815		}
 816
 817		if (!wait_for_completion_timeout(&adapter->init_done,
 818						 timeout)) {
 819			netdev_warn(netdev, "Login timed out\n");
 820			return -1;
 821		}
 822
 823		if (adapter->init_done_rc == PARTIALSUCCESS) {
 824			retry_count++;
 825			release_sub_crqs(adapter, 1);
 826
 827			retry = true;
 828			netdev_dbg(netdev,
 829				   "Received partial success, retrying...\n");
 830			adapter->init_done_rc = 0;
 831			reinit_completion(&adapter->init_done);
 832			send_cap_queries(adapter);
 833			if (!wait_for_completion_timeout(&adapter->init_done,
 834							 timeout)) {
 835				netdev_warn(netdev,
 836					    "Capabilities query timed out\n");
 837				return -1;
 838			}
 839
 840			rc = init_sub_crqs(adapter);
 841			if (rc) {
 842				netdev_warn(netdev,
 843					    "SCRQ initialization failed\n");
 844				return -1;
 845			}
 846
 847			rc = init_sub_crq_irqs(adapter);
 848			if (rc) {
 849				netdev_warn(netdev,
 850					    "SCRQ irq initialization failed\n");
 851				return -1;
 852			}
 853		} else if (adapter->init_done_rc) {
 854			netdev_warn(netdev, "Adapter login failed\n");
 855			return -1;
 856		}
 857	} while (retry);
 858
 859	/* handle pending MAC address changes after successful login */
 860	if (adapter->mac_change_pending) {
 861		__ibmvnic_set_mac(netdev, &adapter->desired.mac);
 862		adapter->mac_change_pending = false;
 863	}
 864
 865	return 0;
 866}
 867
 868static void release_login_buffer(struct ibmvnic_adapter *adapter)
 869{
 870	kfree(adapter->login_buf);
 871	adapter->login_buf = NULL;
 872}
 873
 874static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
 875{
 876	kfree(adapter->login_rsp_buf);
 877	adapter->login_rsp_buf = NULL;
 878}
 879
 880static void release_resources(struct ibmvnic_adapter *adapter)
 881{
 882	release_vpd_data(adapter);
 883
 884	release_tx_pools(adapter);
 885	release_rx_pools(adapter);
 886
 887	release_error_buffers(adapter);
 888	release_napi(adapter);
 889	release_login_rsp_buffer(adapter);
 890}
 891
 892static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
 893{
 894	struct net_device *netdev = adapter->netdev;
 895	unsigned long timeout = msecs_to_jiffies(30000);
 896	union ibmvnic_crq crq;
 897	bool resend;
 898	int rc;
 899
 900	netdev_dbg(netdev, "setting link state %d\n", link_state);
 901
 902	memset(&crq, 0, sizeof(crq));
 903	crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
 904	crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
 905	crq.logical_link_state.link_state = link_state;
 906
 907	do {
 908		resend = false;
 909
 910		reinit_completion(&adapter->init_done);
 911		rc = ibmvnic_send_crq(adapter, &crq);
 912		if (rc) {
 913			netdev_err(netdev, "Failed to set link state\n");
 914			return rc;
 915		}
 916
 917		if (!wait_for_completion_timeout(&adapter->init_done,
 918						 timeout)) {
 919			netdev_err(netdev, "timeout setting link state\n");
 920			return -1;
 921		}
 922
 923		if (adapter->init_done_rc == 1) {
 924			/* Partuial success, delay and re-send */
 925			mdelay(1000);
 926			resend = true;
 927		}
 928	} while (resend);
 929
 930	return 0;
 931}
 932
 933static int set_real_num_queues(struct net_device *netdev)
 934{
 935	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 936	int rc;
 937
 938	netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
 939		   adapter->req_tx_queues, adapter->req_rx_queues);
 940
 941	rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
 942	if (rc) {
 943		netdev_err(netdev, "failed to set the number of tx queues\n");
 944		return rc;
 945	}
 946
 947	rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
 948	if (rc)
 949		netdev_err(netdev, "failed to set the number of rx queues\n");
 950
 951	return rc;
 952}
 953
 954static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
 955{
 956	struct device *dev = &adapter->vdev->dev;
 957	union ibmvnic_crq crq;
 958	int len = 0;
 959
 960	if (adapter->vpd->buff)
 961		len = adapter->vpd->len;
 962
 963	init_completion(&adapter->fw_done);
 964	crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
 965	crq.get_vpd_size.cmd = GET_VPD_SIZE;
 966	ibmvnic_send_crq(adapter, &crq);
 967	wait_for_completion(&adapter->fw_done);
 968
 969	if (!adapter->vpd->len)
 970		return -ENODATA;
 971
 972	if (!adapter->vpd->buff)
 973		adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
 974	else if (adapter->vpd->len != len)
 975		adapter->vpd->buff =
 976			krealloc(adapter->vpd->buff,
 977				 adapter->vpd->len, GFP_KERNEL);
 978
 979	if (!adapter->vpd->buff) {
 980		dev_err(dev, "Could allocate VPD buffer\n");
 981		return -ENOMEM;
 982	}
 983
 984	adapter->vpd->dma_addr =
 985		dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
 986			       DMA_FROM_DEVICE);
 987	if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
 988		dev_err(dev, "Could not map VPD buffer\n");
 989		kfree(adapter->vpd->buff);
 990		adapter->vpd->buff = NULL;
 991		return -ENOMEM;
 992	}
 993
 994	reinit_completion(&adapter->fw_done);
 995	crq.get_vpd.first = IBMVNIC_CRQ_CMD;
 996	crq.get_vpd.cmd = GET_VPD;
 997	crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
 998	crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
 999	ibmvnic_send_crq(adapter, &crq);
1000	wait_for_completion(&adapter->fw_done);
1001
1002	return 0;
1003}
1004
1005static int init_resources(struct ibmvnic_adapter *adapter)
1006{
1007	struct net_device *netdev = adapter->netdev;
1008	int rc;
1009
1010	rc = set_real_num_queues(netdev);
1011	if (rc)
1012		return rc;
1013
1014	adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1015	if (!adapter->vpd)
1016		return -ENOMEM;
1017
1018	/* Vital Product Data (VPD) */
1019	rc = ibmvnic_get_vpd(adapter);
1020	if (rc) {
1021		netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1022		return rc;
1023	}
1024
1025	adapter->map_id = 1;
1026
1027	rc = init_napi(adapter);
1028	if (rc)
1029		return rc;
1030
1031	send_map_query(adapter);
1032
1033	rc = init_rx_pools(netdev);
1034	if (rc)
1035		return rc;
1036
1037	rc = init_tx_pools(netdev);
1038	return rc;
1039}
1040
1041static int __ibmvnic_open(struct net_device *netdev)
1042{
1043	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1044	enum vnic_state prev_state = adapter->state;
1045	int i, rc;
1046
1047	adapter->state = VNIC_OPENING;
1048	replenish_pools(adapter);
1049	ibmvnic_napi_enable(adapter);
1050
1051	/* We're ready to receive frames, enable the sub-crq interrupts and
1052	 * set the logical link state to up
1053	 */
1054	for (i = 0; i < adapter->req_rx_queues; i++) {
1055		netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1056		if (prev_state == VNIC_CLOSED)
1057			enable_irq(adapter->rx_scrq[i]->irq);
1058		enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1059	}
1060
1061	for (i = 0; i < adapter->req_tx_queues; i++) {
1062		netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1063		if (prev_state == VNIC_CLOSED)
1064			enable_irq(adapter->tx_scrq[i]->irq);
1065		enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1066	}
1067
1068	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1069	if (rc) {
1070		for (i = 0; i < adapter->req_rx_queues; i++)
1071			napi_disable(&adapter->napi[i]);
1072		release_resources(adapter);
1073		return rc;
1074	}
1075
1076	netif_tx_start_all_queues(netdev);
1077
1078	if (prev_state == VNIC_CLOSED) {
1079		for (i = 0; i < adapter->req_rx_queues; i++)
1080			napi_schedule(&adapter->napi[i]);
1081	}
1082
1083	adapter->state = VNIC_OPEN;
1084	return rc;
1085}
1086
1087static int ibmvnic_open(struct net_device *netdev)
1088{
1089	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1090	int rc;
1091
1092	/* If device failover is pending, just set device state and return.
1093	 * Device operation will be handled by reset routine.
1094	 */
1095	if (adapter->failover_pending) {
1096		adapter->state = VNIC_OPEN;
1097		return 0;
1098	}
1099
1100	mutex_lock(&adapter->reset_lock);
1101
1102	if (adapter->state != VNIC_CLOSED) {
1103		rc = ibmvnic_login(netdev);
1104		if (rc) {
1105			mutex_unlock(&adapter->reset_lock);
1106			return rc;
1107		}
1108
1109		rc = init_resources(adapter);
1110		if (rc) {
1111			netdev_err(netdev, "failed to initialize resources\n");
1112			release_resources(adapter);
1113			mutex_unlock(&adapter->reset_lock);
1114			return rc;
1115		}
1116	}
1117
1118	rc = __ibmvnic_open(netdev);
1119	netif_carrier_on(netdev);
1120
1121	mutex_unlock(&adapter->reset_lock);
1122
1123	return rc;
1124}
1125
1126static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1127{
1128	struct ibmvnic_rx_pool *rx_pool;
1129	struct ibmvnic_rx_buff *rx_buff;
1130	u64 rx_entries;
1131	int rx_scrqs;
1132	int i, j;
1133
1134	if (!adapter->rx_pool)
1135		return;
1136
1137	rx_scrqs = adapter->num_active_rx_pools;
1138	rx_entries = adapter->req_rx_add_entries_per_subcrq;
1139
1140	/* Free any remaining skbs in the rx buffer pools */
1141	for (i = 0; i < rx_scrqs; i++) {
1142		rx_pool = &adapter->rx_pool[i];
1143		if (!rx_pool || !rx_pool->rx_buff)
1144			continue;
1145
1146		netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1147		for (j = 0; j < rx_entries; j++) {
1148			rx_buff = &rx_pool->rx_buff[j];
1149			if (rx_buff && rx_buff->skb) {
1150				dev_kfree_skb_any(rx_buff->skb);
1151				rx_buff->skb = NULL;
1152			}
1153		}
1154	}
1155}
1156
1157static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1158			      struct ibmvnic_tx_pool *tx_pool)
1159{
1160	struct ibmvnic_tx_buff *tx_buff;
1161	u64 tx_entries;
1162	int i;
1163
1164	if (!tx_pool || !tx_pool->tx_buff)
1165		return;
1166
1167	tx_entries = tx_pool->num_buffers;
1168
1169	for (i = 0; i < tx_entries; i++) {
1170		tx_buff = &tx_pool->tx_buff[i];
1171		if (tx_buff && tx_buff->skb) {
1172			dev_kfree_skb_any(tx_buff->skb);
1173			tx_buff->skb = NULL;
1174		}
1175	}
1176}
1177
1178static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1179{
1180	int tx_scrqs;
1181	int i;
1182
1183	if (!adapter->tx_pool || !adapter->tso_pool)
1184		return;
1185
1186	tx_scrqs = adapter->num_active_tx_pools;
1187
1188	/* Free any remaining skbs in the tx buffer pools */
1189	for (i = 0; i < tx_scrqs; i++) {
1190		netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1191		clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1192		clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1193	}
1194}
1195
1196static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1197{
1198	struct net_device *netdev = adapter->netdev;
1199	int i;
1200
1201	if (adapter->tx_scrq) {
1202		for (i = 0; i < adapter->req_tx_queues; i++)
1203			if (adapter->tx_scrq[i]->irq) {
1204				netdev_dbg(netdev,
1205					   "Disabling tx_scrq[%d] irq\n", i);
1206				disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1207				disable_irq(adapter->tx_scrq[i]->irq);
1208			}
1209	}
1210
1211	if (adapter->rx_scrq) {
1212		for (i = 0; i < adapter->req_rx_queues; i++) {
1213			if (adapter->rx_scrq[i]->irq) {
1214				netdev_dbg(netdev,
1215					   "Disabling rx_scrq[%d] irq\n", i);
1216				disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1217				disable_irq(adapter->rx_scrq[i]->irq);
1218			}
1219		}
1220	}
1221}
1222
1223static void ibmvnic_cleanup(struct net_device *netdev)
1224{
1225	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1226
1227	/* ensure that transmissions are stopped if called by do_reset */
1228	if (adapter->resetting)
1229		netif_tx_disable(netdev);
1230	else
1231		netif_tx_stop_all_queues(netdev);
1232
1233	ibmvnic_napi_disable(adapter);
1234	ibmvnic_disable_irqs(adapter);
1235
1236	clean_rx_pools(adapter);
1237	clean_tx_pools(adapter);
1238}
1239
1240static int __ibmvnic_close(struct net_device *netdev)
1241{
1242	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1243	int rc = 0;
1244
1245	adapter->state = VNIC_CLOSING;
1246	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1247	if (rc)
1248		return rc;
1249	adapter->state = VNIC_CLOSED;
1250	return 0;
1251}
1252
1253static int ibmvnic_close(struct net_device *netdev)
1254{
1255	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1256	int rc;
1257
1258	/* If device failover is pending, just set device state and return.
1259	 * Device operation will be handled by reset routine.
1260	 */
1261	if (adapter->failover_pending) {
1262		adapter->state = VNIC_CLOSED;
1263		return 0;
1264	}
1265
1266	mutex_lock(&adapter->reset_lock);
1267	rc = __ibmvnic_close(netdev);
1268	ibmvnic_cleanup(netdev);
1269	mutex_unlock(&adapter->reset_lock);
1270
1271	return rc;
1272}
1273
1274/**
1275 * build_hdr_data - creates L2/L3/L4 header data buffer
1276 * @hdr_field - bitfield determining needed headers
1277 * @skb - socket buffer
1278 * @hdr_len - array of header lengths
1279 * @tot_len - total length of data
1280 *
1281 * Reads hdr_field to determine which headers are needed by firmware.
1282 * Builds a buffer containing these headers.  Saves individual header
1283 * lengths and total buffer length to be used to build descriptors.
1284 */
1285static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1286			  int *hdr_len, u8 *hdr_data)
1287{
1288	int len = 0;
1289	u8 *hdr;
1290
1291	if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1292		hdr_len[0] = sizeof(struct vlan_ethhdr);
1293	else
1294		hdr_len[0] = sizeof(struct ethhdr);
1295
1296	if (skb->protocol == htons(ETH_P_IP)) {
1297		hdr_len[1] = ip_hdr(skb)->ihl * 4;
1298		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1299			hdr_len[2] = tcp_hdrlen(skb);
1300		else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1301			hdr_len[2] = sizeof(struct udphdr);
1302	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1303		hdr_len[1] = sizeof(struct ipv6hdr);
1304		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1305			hdr_len[2] = tcp_hdrlen(skb);
1306		else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1307			hdr_len[2] = sizeof(struct udphdr);
1308	} else if (skb->protocol == htons(ETH_P_ARP)) {
1309		hdr_len[1] = arp_hdr_len(skb->dev);
1310		hdr_len[2] = 0;
1311	}
1312
1313	memset(hdr_data, 0, 120);
1314	if ((hdr_field >> 6) & 1) {
1315		hdr = skb_mac_header(skb);
1316		memcpy(hdr_data, hdr, hdr_len[0]);
1317		len += hdr_len[0];
1318	}
1319
1320	if ((hdr_field >> 5) & 1) {
1321		hdr = skb_network_header(skb);
1322		memcpy(hdr_data + len, hdr, hdr_len[1]);
1323		len += hdr_len[1];
1324	}
1325
1326	if ((hdr_field >> 4) & 1) {
1327		hdr = skb_transport_header(skb);
1328		memcpy(hdr_data + len, hdr, hdr_len[2]);
1329		len += hdr_len[2];
1330	}
1331	return len;
1332}
1333
1334/**
1335 * create_hdr_descs - create header and header extension descriptors
1336 * @hdr_field - bitfield determining needed headers
1337 * @data - buffer containing header data
1338 * @len - length of data buffer
1339 * @hdr_len - array of individual header lengths
1340 * @scrq_arr - descriptor array
1341 *
1342 * Creates header and, if needed, header extension descriptors and
1343 * places them in a descriptor array, scrq_arr
1344 */
1345
1346static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1347			    union sub_crq *scrq_arr)
1348{
1349	union sub_crq hdr_desc;
1350	int tmp_len = len;
1351	int num_descs = 0;
1352	u8 *data, *cur;
1353	int tmp;
1354
1355	while (tmp_len > 0) {
1356		cur = hdr_data + len - tmp_len;
1357
1358		memset(&hdr_desc, 0, sizeof(hdr_desc));
1359		if (cur != hdr_data) {
1360			data = hdr_desc.hdr_ext.data;
1361			tmp = tmp_len > 29 ? 29 : tmp_len;
1362			hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1363			hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1364			hdr_desc.hdr_ext.len = tmp;
1365		} else {
1366			data = hdr_desc.hdr.data;
1367			tmp = tmp_len > 24 ? 24 : tmp_len;
1368			hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1369			hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1370			hdr_desc.hdr.len = tmp;
1371			hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1372			hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1373			hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1374			hdr_desc.hdr.flag = hdr_field << 1;
1375		}
1376		memcpy(data, cur, tmp);
1377		tmp_len -= tmp;
1378		*scrq_arr = hdr_desc;
1379		scrq_arr++;
1380		num_descs++;
1381	}
1382
1383	return num_descs;
1384}
1385
1386/**
1387 * build_hdr_descs_arr - build a header descriptor array
1388 * @skb - socket buffer
1389 * @num_entries - number of descriptors to be sent
1390 * @subcrq - first TX descriptor
1391 * @hdr_field - bit field determining which headers will be sent
1392 *
1393 * This function will build a TX descriptor array with applicable
1394 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1395 */
1396
1397static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1398				int *num_entries, u8 hdr_field)
1399{
1400	int hdr_len[3] = {0, 0, 0};
1401	int tot_len;
1402	u8 *hdr_data = txbuff->hdr_data;
1403
1404	tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1405				 txbuff->hdr_data);
1406	*num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1407			 txbuff->indir_arr + 1);
1408}
1409
1410static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1411				    struct net_device *netdev)
1412{
1413	/* For some backing devices, mishandling of small packets
1414	 * can result in a loss of connection or TX stall. Device
1415	 * architects recommend that no packet should be smaller
1416	 * than the minimum MTU value provided to the driver, so
1417	 * pad any packets to that length
1418	 */
1419	if (skb->len < netdev->min_mtu)
1420		return skb_put_padto(skb, netdev->min_mtu);
1421
1422	return 0;
1423}
1424
1425static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1426{
1427	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1428	int queue_num = skb_get_queue_mapping(skb);
1429	u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1430	struct device *dev = &adapter->vdev->dev;
1431	struct ibmvnic_tx_buff *tx_buff = NULL;
1432	struct ibmvnic_sub_crq_queue *tx_scrq;
1433	struct ibmvnic_tx_pool *tx_pool;
1434	unsigned int tx_send_failed = 0;
1435	unsigned int tx_map_failed = 0;
1436	unsigned int tx_dropped = 0;
1437	unsigned int tx_packets = 0;
1438	unsigned int tx_bytes = 0;
1439	dma_addr_t data_dma_addr;
1440	struct netdev_queue *txq;
1441	unsigned long lpar_rc;
1442	union sub_crq tx_crq;
1443	unsigned int offset;
1444	int num_entries = 1;
1445	unsigned char *dst;
1446	u64 *handle_array;
1447	int index = 0;
1448	u8 proto = 0;
1449	int ret = 0;
1450
1451	if (adapter->resetting) {
1452		if (!netif_subqueue_stopped(netdev, skb))
1453			netif_stop_subqueue(netdev, queue_num);
1454		dev_kfree_skb_any(skb);
1455
1456		tx_send_failed++;
1457		tx_dropped++;
1458		ret = NETDEV_TX_OK;
1459		goto out;
1460	}
1461
1462	if (ibmvnic_xmit_workarounds(skb, netdev)) {
1463		tx_dropped++;
1464		tx_send_failed++;
1465		ret = NETDEV_TX_OK;
1466		goto out;
1467	}
1468	if (skb_is_gso(skb))
1469		tx_pool = &adapter->tso_pool[queue_num];
1470	else
1471		tx_pool = &adapter->tx_pool[queue_num];
1472
1473	tx_scrq = adapter->tx_scrq[queue_num];
1474	txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1475	handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1476		be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1477
1478	index = tx_pool->free_map[tx_pool->consumer_index];
1479
1480	if (index == IBMVNIC_INVALID_MAP) {
1481		dev_kfree_skb_any(skb);
1482		tx_send_failed++;
1483		tx_dropped++;
1484		ret = NETDEV_TX_OK;
1485		goto out;
1486	}
1487
1488	tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1489
1490	offset = index * tx_pool->buf_size;
1491	dst = tx_pool->long_term_buff.buff + offset;
1492	memset(dst, 0, tx_pool->buf_size);
1493	data_dma_addr = tx_pool->long_term_buff.addr + offset;
1494
1495	if (skb_shinfo(skb)->nr_frags) {
1496		int cur, i;
1497
1498		/* Copy the head */
1499		skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1500		cur = skb_headlen(skb);
1501
1502		/* Copy the frags */
1503		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1504			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1505
1506			memcpy(dst + cur,
1507			       page_address(skb_frag_page(frag)) +
1508			       frag->page_offset, skb_frag_size(frag));
1509			cur += skb_frag_size(frag);
1510		}
1511	} else {
1512		skb_copy_from_linear_data(skb, dst, skb->len);
1513	}
1514
1515	tx_pool->consumer_index =
1516	    (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1517
1518	tx_buff = &tx_pool->tx_buff[index];
1519	tx_buff->skb = skb;
1520	tx_buff->data_dma[0] = data_dma_addr;
1521	tx_buff->data_len[0] = skb->len;
1522	tx_buff->index = index;
1523	tx_buff->pool_index = queue_num;
1524	tx_buff->last_frag = true;
1525
1526	memset(&tx_crq, 0, sizeof(tx_crq));
1527	tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1528	tx_crq.v1.type = IBMVNIC_TX_DESC;
1529	tx_crq.v1.n_crq_elem = 1;
1530	tx_crq.v1.n_sge = 1;
1531	tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1532
1533	if (skb_is_gso(skb))
1534		tx_crq.v1.correlator =
1535			cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1536	else
1537		tx_crq.v1.correlator = cpu_to_be32(index);
1538	tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1539	tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1540	tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1541
1542	if (adapter->vlan_header_insertion) {
1543		tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1544		tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1545	}
1546
1547	if (skb->protocol == htons(ETH_P_IP)) {
1548		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1549		proto = ip_hdr(skb)->protocol;
1550	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1551		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1552		proto = ipv6_hdr(skb)->nexthdr;
1553	}
1554
1555	if (proto == IPPROTO_TCP)
1556		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1557	else if (proto == IPPROTO_UDP)
1558		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1559
1560	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1561		tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1562		hdrs += 2;
1563	}
1564	if (skb_is_gso(skb)) {
1565		tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1566		tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1567		hdrs += 2;
1568	}
1569	/* determine if l2/3/4 headers are sent to firmware */
1570	if ((*hdrs >> 7) & 1) {
1571		build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1572		tx_crq.v1.n_crq_elem = num_entries;
1573		tx_buff->num_entries = num_entries;
1574		tx_buff->indir_arr[0] = tx_crq;
1575		tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1576						    sizeof(tx_buff->indir_arr),
1577						    DMA_TO_DEVICE);
1578		if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1579			dev_kfree_skb_any(skb);
1580			tx_buff->skb = NULL;
1581			if (!firmware_has_feature(FW_FEATURE_CMO))
1582				dev_err(dev, "tx: unable to map descriptor array\n");
1583			tx_map_failed++;
1584			tx_dropped++;
1585			ret = NETDEV_TX_OK;
1586			goto tx_err_out;
1587		}
1588		lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1589					       (u64)tx_buff->indir_dma,
1590					       (u64)num_entries);
1591	} else {
1592		tx_buff->num_entries = num_entries;
1593		lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1594				      &tx_crq);
1595	}
1596	if (lpar_rc != H_SUCCESS) {
1597		dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1598		dev_kfree_skb_any(skb);
1599		tx_buff->skb = NULL;
1600
1601		if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1602			/* Disable TX and report carrier off if queue is closed
1603			 * or pending failover.
1604			 * Firmware guarantees that a signal will be sent to the
1605			 * driver, triggering a reset or some other action.
1606			 */
1607			netif_tx_stop_all_queues(netdev);
1608			netif_carrier_off(netdev);
1609		}
1610
1611		tx_send_failed++;
1612		tx_dropped++;
1613		ret = NETDEV_TX_OK;
1614		goto tx_err_out;
1615	}
1616
1617	if (atomic_add_return(num_entries, &tx_scrq->used)
1618					>= adapter->req_tx_entries_per_subcrq) {
1619		netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1620		netif_stop_subqueue(netdev, queue_num);
1621	}
1622
1623	tx_packets++;
1624	tx_bytes += skb->len;
1625	txq->trans_start = jiffies;
1626	ret = NETDEV_TX_OK;
1627	goto out;
1628
1629tx_err_out:
1630	/* roll back consumer index and map array*/
1631	if (tx_pool->consumer_index == 0)
1632		tx_pool->consumer_index =
1633			tx_pool->num_buffers - 1;
1634	else
1635		tx_pool->consumer_index--;
1636	tx_pool->free_map[tx_pool->consumer_index] = index;
1637out:
1638	netdev->stats.tx_dropped += tx_dropped;
1639	netdev->stats.tx_bytes += tx_bytes;
1640	netdev->stats.tx_packets += tx_packets;
1641	adapter->tx_send_failed += tx_send_failed;
1642	adapter->tx_map_failed += tx_map_failed;
1643	adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1644	adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1645	adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1646
1647	return ret;
1648}
1649
1650static void ibmvnic_set_multi(struct net_device *netdev)
1651{
1652	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1653	struct netdev_hw_addr *ha;
1654	union ibmvnic_crq crq;
1655
1656	memset(&crq, 0, sizeof(crq));
1657	crq.request_capability.first = IBMVNIC_CRQ_CMD;
1658	crq.request_capability.cmd = REQUEST_CAPABILITY;
1659
1660	if (netdev->flags & IFF_PROMISC) {
1661		if (!adapter->promisc_supported)
1662			return;
1663	} else {
1664		if (netdev->flags & IFF_ALLMULTI) {
1665			/* Accept all multicast */
1666			memset(&crq, 0, sizeof(crq));
1667			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1668			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1669			crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1670			ibmvnic_send_crq(adapter, &crq);
1671		} else if (netdev_mc_empty(netdev)) {
1672			/* Reject all multicast */
1673			memset(&crq, 0, sizeof(crq));
1674			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1675			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1676			crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1677			ibmvnic_send_crq(adapter, &crq);
1678		} else {
1679			/* Accept one or more multicast(s) */
1680			netdev_for_each_mc_addr(ha, netdev) {
1681				memset(&crq, 0, sizeof(crq));
1682				crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1683				crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1684				crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1685				ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1686						ha->addr);
1687				ibmvnic_send_crq(adapter, &crq);
1688			}
1689		}
1690	}
1691}
1692
1693static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1694{
1695	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1696	struct sockaddr *addr = p;
1697	union ibmvnic_crq crq;
1698
1699	if (!is_valid_ether_addr(addr->sa_data))
1700		return -EADDRNOTAVAIL;
1701
1702	memset(&crq, 0, sizeof(crq));
1703	crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1704	crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1705	ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1706
1707	init_completion(&adapter->fw_done);
1708	ibmvnic_send_crq(adapter, &crq);
1709	wait_for_completion(&adapter->fw_done);
1710	/* netdev->dev_addr is changed in handle_change_mac_rsp function */
1711	return adapter->fw_done_rc ? -EIO : 0;
1712}
1713
1714static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1715{
1716	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1717	struct sockaddr *addr = p;
1718	int rc;
1719
1720	if (adapter->state == VNIC_PROBED) {
1721		memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1722		adapter->mac_change_pending = true;
1723		return 0;
1724	}
1725
1726	rc = __ibmvnic_set_mac(netdev, addr);
1727
1728	return rc;
1729}
1730
1731/**
1732 * do_reset returns zero if we are able to keep processing reset events, or
1733 * non-zero if we hit a fatal error and must halt.
1734 */
1735static int do_reset(struct ibmvnic_adapter *adapter,
1736		    struct ibmvnic_rwi *rwi, u32 reset_state)
1737{
1738	u64 old_num_rx_queues, old_num_tx_queues;
1739	struct net_device *netdev = adapter->netdev;
1740	int i, rc;
1741
1742	netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1743		   rwi->reset_reason);
1744
1745	netif_carrier_off(netdev);
1746	adapter->reset_reason = rwi->reset_reason;
1747
1748	old_num_rx_queues = adapter->req_rx_queues;
1749	old_num_tx_queues = adapter->req_tx_queues;
1750
1751	ibmvnic_cleanup(netdev);
1752
1753	if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1754	    adapter->reset_reason != VNIC_RESET_FAILOVER) {
1755		rc = __ibmvnic_close(netdev);
1756		if (rc)
1757			return rc;
1758	}
1759
1760	if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1761	    adapter->wait_for_reset) {
1762		release_resources(adapter);
1763		release_sub_crqs(adapter, 1);
1764		release_crq_queue(adapter);
1765	}
1766
1767	if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1768		/* remove the closed state so when we call open it appears
1769		 * we are coming from the probed state.
1770		 */
1771		adapter->state = VNIC_PROBED;
1772
1773		if (adapter->wait_for_reset) {
1774			rc = init_crq_queue(adapter);
1775		} else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1776			rc = ibmvnic_reenable_crq_queue(adapter);
1777			release_sub_crqs(adapter, 1);
1778		} else {
1779			rc = ibmvnic_reset_crq(adapter);
1780			if (!rc)
1781				rc = vio_enable_interrupts(adapter->vdev);
1782		}
1783
1784		if (rc) {
1785			netdev_err(adapter->netdev,
1786				   "Couldn't initialize crq. rc=%d\n", rc);
1787			return rc;
1788		}
1789
1790		rc = ibmvnic_init(adapter);
1791		if (rc)
1792			return IBMVNIC_INIT_FAILED;
1793
1794		/* If the adapter was in PROBE state prior to the reset,
1795		 * exit here.
1796		 */
1797		if (reset_state == VNIC_PROBED)
1798			return 0;
1799
1800		rc = ibmvnic_login(netdev);
1801		if (rc) {
1802			adapter->state = VNIC_PROBED;
1803			return 0;
1804		}
1805
1806		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1807		    adapter->wait_for_reset) {
1808			rc = init_resources(adapter);
1809			if (rc)
1810				return rc;
1811		} else if (adapter->req_rx_queues != old_num_rx_queues ||
1812			   adapter->req_tx_queues != old_num_tx_queues) {
1813			adapter->map_id = 1;
1814			release_rx_pools(adapter);
1815			release_tx_pools(adapter);
1816			init_rx_pools(netdev);
1817			init_tx_pools(netdev);
1818
1819			release_napi(adapter);
1820			init_napi(adapter);
1821		} else {
1822			rc = reset_tx_pools(adapter);
1823			if (rc)
1824				return rc;
1825
1826			rc = reset_rx_pools(adapter);
1827			if (rc)
1828				return rc;
1829		}
1830		ibmvnic_disable_irqs(adapter);
1831	}
1832	adapter->state = VNIC_CLOSED;
1833
1834	if (reset_state == VNIC_CLOSED)
1835		return 0;
1836
1837	rc = __ibmvnic_open(netdev);
1838	if (rc) {
1839		if (list_empty(&adapter->rwi_list))
1840			adapter->state = VNIC_CLOSED;
1841		else
1842			adapter->state = reset_state;
1843
1844		return 0;
1845	}
1846
1847	/* kick napi */
1848	for (i = 0; i < adapter->req_rx_queues; i++)
1849		napi_schedule(&adapter->napi[i]);
1850
1851	if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1852	    adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1853		netdev_notify_peers(netdev);
1854
1855	netif_carrier_on(netdev);
1856
1857	return 0;
1858}
1859
1860static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1861{
1862	struct ibmvnic_rwi *rwi;
1863
1864	mutex_lock(&adapter->rwi_lock);
1865
1866	if (!list_empty(&adapter->rwi_list)) {
1867		rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1868				       list);
1869		list_del(&rwi->list);
1870	} else {
1871		rwi = NULL;
1872	}
1873
1874	mutex_unlock(&adapter->rwi_lock);
1875	return rwi;
1876}
1877
1878static void free_all_rwi(struct ibmvnic_adapter *adapter)
1879{
1880	struct ibmvnic_rwi *rwi;
1881
1882	rwi = get_next_rwi(adapter);
1883	while (rwi) {
1884		kfree(rwi);
1885		rwi = get_next_rwi(adapter);
1886	}
1887}
1888
1889static void __ibmvnic_reset(struct work_struct *work)
1890{
1891	struct ibmvnic_rwi *rwi;
1892	struct ibmvnic_adapter *adapter;
1893	struct net_device *netdev;
1894	u32 reset_state;
1895	int rc = 0;
1896
1897	adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1898	netdev = adapter->netdev;
1899
1900	mutex_lock(&adapter->reset_lock);
1901	adapter->resetting = true;
1902	reset_state = adapter->state;
1903
1904	rwi = get_next_rwi(adapter);
1905	while (rwi) {
1906		rc = do_reset(adapter, rwi, reset_state);
1907		kfree(rwi);
1908		if (rc && rc != IBMVNIC_INIT_FAILED)
1909			break;
1910
1911		rwi = get_next_rwi(adapter);
1912	}
1913
1914	if (adapter->wait_for_reset) {
1915		adapter->wait_for_reset = false;
1916		adapter->reset_done_rc = rc;
1917		complete(&adapter->reset_done);
1918	}
1919
1920	if (rc) {
1921		netdev_dbg(adapter->netdev, "Reset failed\n");
1922		free_all_rwi(adapter);
1923		mutex_unlock(&adapter->reset_lock);
1924		return;
1925	}
1926
1927	adapter->resetting = false;
1928	mutex_unlock(&adapter->reset_lock);
1929}
1930
1931static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
1932			 enum ibmvnic_reset_reason reason)
1933{
1934	struct ibmvnic_rwi *rwi, *tmp;
1935	struct net_device *netdev = adapter->netdev;
1936	struct list_head *entry;
1937	int ret;
1938
1939	if (adapter->state == VNIC_REMOVING ||
1940	    adapter->state == VNIC_REMOVED ||
1941	    adapter->failover_pending) {
1942		ret = EBUSY;
1943		netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
1944		goto err;
1945	}
1946
1947	if (adapter->state == VNIC_PROBING) {
1948		netdev_warn(netdev, "Adapter reset during probe\n");
1949		ret = adapter->init_done_rc = EAGAIN;
1950		goto err;
1951	}
1952
1953	mutex_lock(&adapter->rwi_lock);
1954
1955	list_for_each(entry, &adapter->rwi_list) {
1956		tmp = list_entry(entry, struct ibmvnic_rwi, list);
1957		if (tmp->reset_reason == reason) {
1958			netdev_dbg(netdev, "Skipping matching reset\n");
1959			mutex_unlock(&adapter->rwi_lock);
1960			ret = EBUSY;
1961			goto err;
1962		}
1963	}
1964
1965	rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1966	if (!rwi) {
1967		mutex_unlock(&adapter->rwi_lock);
1968		ibmvnic_close(netdev);
1969		ret = ENOMEM;
1970		goto err;
1971	}
1972
1973	rwi->reset_reason = reason;
1974	list_add_tail(&rwi->list, &adapter->rwi_list);
1975	mutex_unlock(&adapter->rwi_lock);
1976
1977	netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1978	schedule_work(&adapter->ibmvnic_reset);
1979
1980	return 0;
1981err:
1982	if (adapter->wait_for_reset)
1983		adapter->wait_for_reset = false;
1984	return -ret;
1985}
1986
1987static void ibmvnic_tx_timeout(struct net_device *dev)
1988{
1989	struct ibmvnic_adapter *adapter = netdev_priv(dev);
1990
1991	ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1992}
1993
1994static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1995				  struct ibmvnic_rx_buff *rx_buff)
1996{
1997	struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1998
1999	rx_buff->skb = NULL;
2000
2001	pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2002	pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2003
2004	atomic_dec(&pool->available);
2005}
2006
2007static int ibmvnic_poll(struct napi_struct *napi, int budget)
2008{
2009	struct net_device *netdev = napi->dev;
2010	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2011	int scrq_num = (int)(napi - adapter->napi);
2012	int frames_processed = 0;
2013
2014restart_poll:
2015	while (frames_processed < budget) {
2016		struct sk_buff *skb;
2017		struct ibmvnic_rx_buff *rx_buff;
2018		union sub_crq *next;
2019		u32 length;
2020		u16 offset;
2021		u8 flags = 0;
2022
2023		if (unlikely(adapter->resetting &&
2024			     adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2025			enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2026			napi_complete_done(napi, frames_processed);
2027			return frames_processed;
2028		}
2029
2030		if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2031			break;
2032		next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2033		rx_buff =
2034		    (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2035							  rx_comp.correlator);
2036		/* do error checking */
2037		if (next->rx_comp.rc) {
2038			netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2039				   be16_to_cpu(next->rx_comp.rc));
2040			/* free the entry */
2041			next->rx_comp.first = 0;
2042			dev_kfree_skb_any(rx_buff->skb);
2043			remove_buff_from_pool(adapter, rx_buff);
2044			continue;
2045		} else if (!rx_buff->skb) {
2046			/* free the entry */
2047			next->rx_comp.first = 0;
2048			remove_buff_from_pool(adapter, rx_buff);
2049			continue;
2050		}
2051
2052		length = be32_to_cpu(next->rx_comp.len);
2053		offset = be16_to_cpu(next->rx_comp.off_frame_data);
2054		flags = next->rx_comp.flags;
2055		skb = rx_buff->skb;
2056		skb_copy_to_linear_data(skb, rx_buff->data + offset,
2057					length);
2058
2059		/* VLAN Header has been stripped by the system firmware and
2060		 * needs to be inserted by the driver
2061		 */
2062		if (adapter->rx_vlan_header_insertion &&
2063		    (flags & IBMVNIC_VLAN_STRIPPED))
2064			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2065					       ntohs(next->rx_comp.vlan_tci));
2066
2067		/* free the entry */
2068		next->rx_comp.first = 0;
2069		remove_buff_from_pool(adapter, rx_buff);
2070
2071		skb_put(skb, length);
2072		skb->protocol = eth_type_trans(skb, netdev);
2073		skb_record_rx_queue(skb, scrq_num);
2074
2075		if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2076		    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2077			skb->ip_summed = CHECKSUM_UNNECESSARY;
2078		}
2079
2080		length = skb->len;
2081		napi_gro_receive(napi, skb); /* send it up */
2082		netdev->stats.rx_packets++;
2083		netdev->stats.rx_bytes += length;
2084		adapter->rx_stats_buffers[scrq_num].packets++;
2085		adapter->rx_stats_buffers[scrq_num].bytes += length;
2086		frames_processed++;
2087	}
2088
2089	if (adapter->state != VNIC_CLOSING)
2090		replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2091
2092	if (frames_processed < budget) {
2093		enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2094		napi_complete_done(napi, frames_processed);
2095		if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2096		    napi_reschedule(napi)) {
2097			disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2098			goto restart_poll;
2099		}
2100	}
2101	return frames_processed;
2102}
2103
2104#ifdef CONFIG_NET_POLL_CONTROLLER
2105static void ibmvnic_netpoll_controller(struct net_device *dev)
2106{
2107	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2108	int i;
2109
2110	replenish_pools(netdev_priv(dev));
2111	for (i = 0; i < adapter->req_rx_queues; i++)
2112		ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2113				     adapter->rx_scrq[i]);
2114}
2115#endif
2116
2117static int wait_for_reset(struct ibmvnic_adapter *adapter)
2118{
2119	int rc, ret;
2120
2121	adapter->fallback.mtu = adapter->req_mtu;
2122	adapter->fallback.rx_queues = adapter->req_rx_queues;
2123	adapter->fallback.tx_queues = adapter->req_tx_queues;
2124	adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2125	adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2126
2127	init_completion(&adapter->reset_done);
2128	adapter->wait_for_reset = true;
2129	rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2130	if (rc)
2131		return rc;
2132	wait_for_completion(&adapter->reset_done);
2133
2134	ret = 0;
2135	if (adapter->reset_done_rc) {
2136		ret = -EIO;
2137		adapter->desired.mtu = adapter->fallback.mtu;
2138		adapter->desired.rx_queues = adapter->fallback.rx_queues;
2139		adapter->desired.tx_queues = adapter->fallback.tx_queues;
2140		adapter->desired.rx_entries = adapter->fallback.rx_entries;
2141		adapter->desired.tx_entries = adapter->fallback.tx_entries;
2142
2143		init_completion(&adapter->reset_done);
2144		adapter->wait_for_reset = true;
2145		rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2146		if (rc)
2147			return ret;
2148		wait_for_completion(&adapter->reset_done);
2149	}
2150	adapter->wait_for_reset = false;
2151
2152	return ret;
2153}
2154
2155static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2156{
2157	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2158
2159	adapter->desired.mtu = new_mtu + ETH_HLEN;
2160
2161	return wait_for_reset(adapter);
2162}
2163
2164static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2165						struct net_device *dev,
2166						netdev_features_t features)
2167{
2168	/* Some backing hardware adapters can not
2169	 * handle packets with a MSS less than 224
2170	 * or with only one segment.
2171	 */
2172	if (skb_is_gso(skb)) {
2173		if (skb_shinfo(skb)->gso_size < 224 ||
2174		    skb_shinfo(skb)->gso_segs == 1)
2175			features &= ~NETIF_F_GSO_MASK;
2176	}
2177
2178	return features;
2179}
2180
2181static const struct net_device_ops ibmvnic_netdev_ops = {
2182	.ndo_open		= ibmvnic_open,
2183	.ndo_stop		= ibmvnic_close,
2184	.ndo_start_xmit		= ibmvnic_xmit,
2185	.ndo_set_rx_mode	= ibmvnic_set_multi,
2186	.ndo_set_mac_address	= ibmvnic_set_mac,
2187	.ndo_validate_addr	= eth_validate_addr,
2188	.ndo_tx_timeout		= ibmvnic_tx_timeout,
2189#ifdef CONFIG_NET_POLL_CONTROLLER
2190	.ndo_poll_controller	= ibmvnic_netpoll_controller,
2191#endif
2192	.ndo_change_mtu		= ibmvnic_change_mtu,
2193	.ndo_features_check     = ibmvnic_features_check,
2194};
2195
2196/* ethtool functions */
2197
2198static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2199				      struct ethtool_link_ksettings *cmd)
2200{
2201	u32 supported, advertising;
2202
2203	supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2204			  SUPPORTED_FIBRE);
2205	advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2206			    ADVERTISED_FIBRE);
2207	cmd->base.speed = SPEED_1000;
2208	cmd->base.duplex = DUPLEX_FULL;
2209	cmd->base.port = PORT_FIBRE;
2210	cmd->base.phy_address = 0;
2211	cmd->base.autoneg = AUTONEG_ENABLE;
2212
2213	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2214						supported);
2215	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2216						advertising);
2217
2218	return 0;
2219}
2220
2221static void ibmvnic_get_drvinfo(struct net_device *netdev,
2222				struct ethtool_drvinfo *info)
2223{
2224	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2225
2226	strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2227	strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2228	strlcpy(info->fw_version, adapter->fw_version,
2229		sizeof(info->fw_version));
2230}
2231
2232static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2233{
2234	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2235
2236	return adapter->msg_enable;
2237}
2238
2239static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2240{
2241	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2242
2243	adapter->msg_enable = data;
2244}
2245
2246static u32 ibmvnic_get_link(struct net_device *netdev)
2247{
2248	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2249
2250	/* Don't need to send a query because we request a logical link up at
2251	 * init and then we wait for link state indications
2252	 */
2253	return adapter->logical_link_state;
2254}
2255
2256static void ibmvnic_get_ringparam(struct net_device *netdev,
2257				  struct ethtool_ringparam *ring)
2258{
2259	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2260
2261	ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2262	ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2263	ring->rx_mini_max_pending = 0;
2264	ring->rx_jumbo_max_pending = 0;
2265	ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2266	ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2267	ring->rx_mini_pending = 0;
2268	ring->rx_jumbo_pending = 0;
2269}
2270
2271static int ibmvnic_set_ringparam(struct net_device *netdev,
2272				 struct ethtool_ringparam *ring)
2273{
2274	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2275
2276	if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq  ||
2277	    ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2278		netdev_err(netdev, "Invalid request.\n");
2279		netdev_err(netdev, "Max tx buffers = %llu\n",
2280			   adapter->max_rx_add_entries_per_subcrq);
2281		netdev_err(netdev, "Max rx buffers = %llu\n",
2282			   adapter->max_tx_entries_per_subcrq);
2283		return -EINVAL;
2284	}
2285
2286	adapter->desired.rx_entries = ring->rx_pending;
2287	adapter->desired.tx_entries = ring->tx_pending;
2288
2289	return wait_for_reset(adapter);
2290}
2291
2292static void ibmvnic_get_channels(struct net_device *netdev,
2293				 struct ethtool_channels *channels)
2294{
2295	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2296
2297	channels->max_rx = adapter->max_rx_queues;
2298	channels->max_tx = adapter->max_tx_queues;
2299	channels->max_other = 0;
2300	channels->max_combined = 0;
2301	channels->rx_count = adapter->req_rx_queues;
2302	channels->tx_count = adapter->req_tx_queues;
2303	channels->other_count = 0;
2304	channels->combined_count = 0;
2305}
2306
2307static int ibmvnic_set_channels(struct net_device *netdev,
2308				struct ethtool_channels *channels)
2309{
2310	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2311
2312	adapter->desired.rx_queues = channels->rx_count;
2313	adapter->desired.tx_queues = channels->tx_count;
2314
2315	return wait_for_reset(adapter);
2316}
2317
2318static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2319{
2320	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2321	int i;
2322
2323	if (stringset != ETH_SS_STATS)
2324		return;
2325
2326	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2327		memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2328
2329	for (i = 0; i < adapter->req_tx_queues; i++) {
2330		snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2331		data += ETH_GSTRING_LEN;
2332
2333		snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2334		data += ETH_GSTRING_LEN;
2335
2336		snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2337		data += ETH_GSTRING_LEN;
2338	}
2339
2340	for (i = 0; i < adapter->req_rx_queues; i++) {
2341		snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2342		data += ETH_GSTRING_LEN;
2343
2344		snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2345		data += ETH_GSTRING_LEN;
2346
2347		snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2348		data += ETH_GSTRING_LEN;
2349	}
2350}
2351
2352static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2353{
2354	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2355
2356	switch (sset) {
2357	case ETH_SS_STATS:
2358		return ARRAY_SIZE(ibmvnic_stats) +
2359		       adapter->req_tx_queues * NUM_TX_STATS +
2360		       adapter->req_rx_queues * NUM_RX_STATS;
2361	default:
2362		return -EOPNOTSUPP;
2363	}
2364}
2365
2366static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2367				      struct ethtool_stats *stats, u64 *data)
2368{
2369	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2370	union ibmvnic_crq crq;
2371	int i, j;
2372
2373	memset(&crq, 0, sizeof(crq));
2374	crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2375	crq.request_statistics.cmd = REQUEST_STATISTICS;
2376	crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2377	crq.request_statistics.len =
2378	    cpu_to_be32(sizeof(struct ibmvnic_statistics));
2379
2380	/* Wait for data to be written */
2381	init_completion(&adapter->stats_done);
2382	ibmvnic_send_crq(adapter, &crq);
2383	wait_for_completion(&adapter->stats_done);
2384
2385	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2386		data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2387						ibmvnic_stats[i].offset));
2388
2389	for (j = 0; j < adapter->req_tx_queues; j++) {
2390		data[i] = adapter->tx_stats_buffers[j].packets;
2391		i++;
2392		data[i] = adapter->tx_stats_buffers[j].bytes;
2393		i++;
2394		data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2395		i++;
2396	}
2397
2398	for (j = 0; j < adapter->req_rx_queues; j++) {
2399		data[i] = adapter->rx_stats_buffers[j].packets;
2400		i++;
2401		data[i] = adapter->rx_stats_buffers[j].bytes;
2402		i++;
2403		data[i] = adapter->rx_stats_buffers[j].interrupts;
2404		i++;
2405	}
2406}
2407
2408static const struct ethtool_ops ibmvnic_ethtool_ops = {
2409	.get_drvinfo		= ibmvnic_get_drvinfo,
2410	.get_msglevel		= ibmvnic_get_msglevel,
2411	.set_msglevel		= ibmvnic_set_msglevel,
2412	.get_link		= ibmvnic_get_link,
2413	.get_ringparam		= ibmvnic_get_ringparam,
2414	.set_ringparam		= ibmvnic_set_ringparam,
2415	.get_channels		= ibmvnic_get_channels,
2416	.set_channels		= ibmvnic_set_channels,
2417	.get_strings            = ibmvnic_get_strings,
2418	.get_sset_count         = ibmvnic_get_sset_count,
2419	.get_ethtool_stats	= ibmvnic_get_ethtool_stats,
2420	.get_link_ksettings	= ibmvnic_get_link_ksettings,
2421};
2422
2423/* Routines for managing CRQs/sCRQs  */
2424
2425static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2426				   struct ibmvnic_sub_crq_queue *scrq)
2427{
2428	int rc;
2429
2430	if (scrq->irq) {
2431		free_irq(scrq->irq, scrq);
2432		irq_dispose_mapping(scrq->irq);
2433		scrq->irq = 0;
2434	}
2435
2436	memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2437	atomic_set(&scrq->used, 0);
2438	scrq->cur = 0;
2439
2440	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2441			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2442	return rc;
2443}
2444
2445static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2446{
2447	int i, rc;
2448
2449	for (i = 0; i < adapter->req_tx_queues; i++) {
2450		netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2451		rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2452		if (rc)
2453			return rc;
2454	}
2455
2456	for (i = 0; i < adapter->req_rx_queues; i++) {
2457		netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2458		rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2459		if (rc)
2460			return rc;
2461	}
2462
2463	return rc;
2464}
2465
2466static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2467				  struct ibmvnic_sub_crq_queue *scrq,
2468				  bool do_h_free)
2469{
2470	struct device *dev = &adapter->vdev->dev;
2471	long rc;
2472
2473	netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2474
2475	if (do_h_free) {
2476		/* Close the sub-crqs */
2477		do {
2478			rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2479						adapter->vdev->unit_address,
2480						scrq->crq_num);
2481		} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2482
2483		if (rc) {
2484			netdev_err(adapter->netdev,
2485				   "Failed to release sub-CRQ %16lx, rc = %ld\n",
2486				   scrq->crq_num, rc);
2487		}
2488	}
2489
2490	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2491			 DMA_BIDIRECTIONAL);
2492	free_pages((unsigned long)scrq->msgs, 2);
2493	kfree(scrq);
2494}
2495
2496static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2497							*adapter)
2498{
2499	struct device *dev = &adapter->vdev->dev;
2500	struct ibmvnic_sub_crq_queue *scrq;
2501	int rc;
2502
2503	scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2504	if (!scrq)
2505		return NULL;
2506
2507	scrq->msgs =
2508		(union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2509	if (!scrq->msgs) {
2510		dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2511		goto zero_page_failed;
2512	}
2513
2514	scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2515					 DMA_BIDIRECTIONAL);
2516	if (dma_mapping_error(dev, scrq->msg_token)) {
2517		dev_warn(dev, "Couldn't map crq queue messages page\n");
2518		goto map_failed;
2519	}
2520
2521	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2522			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2523
2524	if (rc == H_RESOURCE)
2525		rc = ibmvnic_reset_crq(adapter);
2526
2527	if (rc == H_CLOSED) {
2528		dev_warn(dev, "Partner adapter not ready, waiting.\n");
2529	} else if (rc) {
2530		dev_warn(dev, "Error %d registering sub-crq\n", rc);
2531		goto reg_failed;
2532	}
2533
2534	scrq->adapter = adapter;
2535	scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2536	spin_lock_init(&scrq->lock);
2537
2538	netdev_dbg(adapter->netdev,
2539		   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2540		   scrq->crq_num, scrq->hw_irq, scrq->irq);
2541
2542	return scrq;
2543
2544reg_failed:
2545	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2546			 DMA_BIDIRECTIONAL);
2547map_failed:
2548	free_pages((unsigned long)scrq->msgs, 2);
2549zero_page_failed:
2550	kfree(scrq);
2551
2552	return NULL;
2553}
2554
2555static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2556{
2557	int i;
2558
2559	if (adapter->tx_scrq) {
2560		for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2561			if (!adapter->tx_scrq[i])
2562				continue;
2563
2564			netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2565				   i);
2566			if (adapter->tx_scrq[i]->irq) {
2567				free_irq(adapter->tx_scrq[i]->irq,
2568					 adapter->tx_scrq[i]);
2569				irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2570				adapter->tx_scrq[i]->irq = 0;
2571			}
2572
2573			release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2574					      do_h_free);
2575		}
2576
2577		kfree(adapter->tx_scrq);
2578		adapter->tx_scrq = NULL;
2579		adapter->num_active_tx_scrqs = 0;
2580	}
2581
2582	if (adapter->rx_scrq) {
2583		for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2584			if (!adapter->rx_scrq[i])
2585				continue;
2586
2587			netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2588				   i);
2589			if (adapter->rx_scrq[i]->irq) {
2590				free_irq(adapter->rx_scrq[i]->irq,
2591					 adapter->rx_scrq[i]);
2592				irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2593				adapter->rx_scrq[i]->irq = 0;
2594			}
2595
2596			release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2597					      do_h_free);
2598		}
2599
2600		kfree(adapter->rx_scrq);
2601		adapter->rx_scrq = NULL;
2602		adapter->num_active_rx_scrqs = 0;
2603	}
2604}
2605
2606static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2607			    struct ibmvnic_sub_crq_queue *scrq)
2608{
2609	struct device *dev = &adapter->vdev->dev;
2610	unsigned long rc;
2611
2612	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2613				H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2614	if (rc)
2615		dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2616			scrq->hw_irq, rc);
2617	return rc;
2618}
2619
2620static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2621			   struct ibmvnic_sub_crq_queue *scrq)
2622{
2623	struct device *dev = &adapter->vdev->dev;
2624	unsigned long rc;
2625
2626	if (scrq->hw_irq > 0x100000000ULL) {
2627		dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2628		return 1;
2629	}
2630
2631	if (adapter->resetting &&
2632	    adapter->reset_reason == VNIC_RESET_MOBILITY) {
2633		u64 val = (0xff000000) | scrq->hw_irq;
2634
2635		rc = plpar_hcall_norets(H_EOI, val);
2636		if (rc)
2637			dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2638				val, rc);
2639	}
2640
2641	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2642				H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2643	if (rc)
2644		dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2645			scrq->hw_irq, rc);
2646	return rc;
2647}
2648
2649static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2650			       struct ibmvnic_sub_crq_queue *scrq)
2651{
2652	struct device *dev = &adapter->vdev->dev;
2653	struct ibmvnic_tx_pool *tx_pool;
2654	struct ibmvnic_tx_buff *txbuff;
2655	union sub_crq *next;
2656	int index;
2657	int i, j;
2658	u8 *first;
2659
2660restart_loop:
2661	while (pending_scrq(adapter, scrq)) {
2662		unsigned int pool = scrq->pool_index;
2663		int num_entries = 0;
2664
2665		next = ibmvnic_next_scrq(adapter, scrq);
2666		for (i = 0; i < next->tx_comp.num_comps; i++) {
2667			if (next->tx_comp.rcs[i]) {
2668				dev_err(dev, "tx error %x\n",
2669					next->tx_comp.rcs[i]);
2670				continue;
2671			}
2672			index = be32_to_cpu(next->tx_comp.correlators[i]);
2673			if (index & IBMVNIC_TSO_POOL_MASK) {
2674				tx_pool = &adapter->tso_pool[pool];
2675				index &= ~IBMVNIC_TSO_POOL_MASK;
2676			} else {
2677				tx_pool = &adapter->tx_pool[pool];
2678			}
2679
2680			txbuff = &tx_pool->tx_buff[index];
2681
2682			for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2683				if (!txbuff->data_dma[j])
2684					continue;
2685
2686				txbuff->data_dma[j] = 0;
2687			}
2688			/* if sub_crq was sent indirectly */
2689			first = &txbuff->indir_arr[0].generic.first;
2690			if (*first == IBMVNIC_CRQ_CMD) {
2691				dma_unmap_single(dev, txbuff->indir_dma,
2692						 sizeof(txbuff->indir_arr),
2693						 DMA_TO_DEVICE);
2694				*first = 0;
2695			}
2696
2697			if (txbuff->last_frag) {
2698				dev_kfree_skb_any(txbuff->skb);
2699				txbuff->skb = NULL;
2700			}
2701
2702			num_entries += txbuff->num_entries;
2703
2704			tx_pool->free_map[tx_pool->producer_index] = index;
2705			tx_pool->producer_index =
2706				(tx_pool->producer_index + 1) %
2707					tx_pool->num_buffers;
2708		}
2709		/* remove tx_comp scrq*/
2710		next->tx_comp.first = 0;
2711
2712		if (atomic_sub_return(num_entries, &scrq->used) <=
2713		    (adapter->req_tx_entries_per_subcrq / 2) &&
2714		    __netif_subqueue_stopped(adapter->netdev,
2715					     scrq->pool_index)) {
2716			netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2717			netdev_dbg(adapter->netdev, "Started queue %d\n",
2718				   scrq->pool_index);
2719		}
2720	}
2721
2722	enable_scrq_irq(adapter, scrq);
2723
2724	if (pending_scrq(adapter, scrq)) {
2725		disable_scrq_irq(adapter, scrq);
2726		goto restart_loop;
2727	}
2728
2729	return 0;
2730}
2731
2732static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2733{
2734	struct ibmvnic_sub_crq_queue *scrq = instance;
2735	struct ibmvnic_adapter *adapter = scrq->adapter;
2736
2737	disable_scrq_irq(adapter, scrq);
2738	ibmvnic_complete_tx(adapter, scrq);
2739
2740	return IRQ_HANDLED;
2741}
2742
2743static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2744{
2745	struct ibmvnic_sub_crq_queue *scrq = instance;
2746	struct ibmvnic_adapter *adapter = scrq->adapter;
2747
2748	/* When booting a kdump kernel we can hit pending interrupts
2749	 * prior to completing driver initialization.
2750	 */
2751	if (unlikely(adapter->state != VNIC_OPEN))
2752		return IRQ_NONE;
2753
2754	adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2755
2756	if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2757		disable_scrq_irq(adapter, scrq);
2758		__napi_schedule(&adapter->napi[scrq->scrq_num]);
2759	}
2760
2761	return IRQ_HANDLED;
2762}
2763
2764static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2765{
2766	struct device *dev = &adapter->vdev->dev;
2767	struct ibmvnic_sub_crq_queue *scrq;
2768	int i = 0, j = 0;
2769	int rc = 0;
2770
2771	for (i = 0; i < adapter->req_tx_queues; i++) {
2772		netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2773			   i);
2774		scrq = adapter->tx_scrq[i];
2775		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2776
2777		if (!scrq->irq) {
2778			rc = -EINVAL;
2779			dev_err(dev, "Error mapping irq\n");
2780			goto req_tx_irq_failed;
2781		}
2782
2783		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2784				 0, "ibmvnic_tx", scrq);
2785
2786		if (rc) {
2787			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2788				scrq->irq, rc);
2789			irq_dispose_mapping(scrq->irq);
2790			goto req_tx_irq_failed;
2791		}
2792	}
2793
2794	for (i = 0; i < adapter->req_rx_queues; i++) {
2795		netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2796			   i);
2797		scrq = adapter->rx_scrq[i];
2798		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2799		if (!scrq->irq) {
2800			rc = -EINVAL;
2801			dev_err(dev, "Error mapping irq\n");
2802			goto req_rx_irq_failed;
2803		}
2804		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2805				 0, "ibmvnic_rx", scrq);
2806		if (rc) {
2807			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2808				scrq->irq, rc);
2809			irq_dispose_mapping(scrq->irq);
2810			goto req_rx_irq_failed;
2811		}
2812	}
2813	return rc;
2814
2815req_rx_irq_failed:
2816	for (j = 0; j < i; j++) {
2817		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2818		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2819	}
2820	i = adapter->req_tx_queues;
2821req_tx_irq_failed:
2822	for (j = 0; j < i; j++) {
2823		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2824		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2825	}
2826	release_sub_crqs(adapter, 1);
2827	return rc;
2828}
2829
2830static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2831{
2832	struct device *dev = &adapter->vdev->dev;
2833	struct ibmvnic_sub_crq_queue **allqueues;
2834	int registered_queues = 0;
2835	int total_queues;
2836	int more = 0;
2837	int i;
2838
2839	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2840
2841	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2842	if (!allqueues)
2843		return -1;
2844
2845	for (i = 0; i < total_queues; i++) {
2846		allqueues[i] = init_sub_crq_queue(adapter);
2847		if (!allqueues[i]) {
2848			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2849			break;
2850		}
2851		registered_queues++;
2852	}
2853
2854	/* Make sure we were able to register the minimum number of queues */
2855	if (registered_queues <
2856	    adapter->min_tx_queues + adapter->min_rx_queues) {
2857		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
2858		goto tx_failed;
2859	}
2860
2861	/* Distribute the failed allocated queues*/
2862	for (i = 0; i < total_queues - registered_queues + more ; i++) {
2863		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2864		switch (i % 3) {
2865		case 0:
2866			if (adapter->req_rx_queues > adapter->min_rx_queues)
2867				adapter->req_rx_queues--;
2868			else
2869				more++;
2870			break;
2871		case 1:
2872			if (adapter->req_tx_queues > adapter->min_tx_queues)
2873				adapter->req_tx_queues--;
2874			else
2875				more++;
2876			break;
2877		}
2878	}
2879
2880	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2881				   sizeof(*adapter->tx_scrq), GFP_KERNEL);
2882	if (!adapter->tx_scrq)
2883		goto tx_failed;
2884
2885	for (i = 0; i < adapter->req_tx_queues; i++) {
2886		adapter->tx_scrq[i] = allqueues[i];
2887		adapter->tx_scrq[i]->pool_index = i;
2888		adapter->num_active_tx_scrqs++;
2889	}
2890
2891	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2892				   sizeof(*adapter->rx_scrq), GFP_KERNEL);
2893	if (!adapter->rx_scrq)
2894		goto rx_failed;
2895
2896	for (i = 0; i < adapter->req_rx_queues; i++) {
2897		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2898		adapter->rx_scrq[i]->scrq_num = i;
2899		adapter->num_active_rx_scrqs++;
2900	}
2901
2902	kfree(allqueues);
2903	return 0;
2904
2905rx_failed:
2906	kfree(adapter->tx_scrq);
2907	adapter->tx_scrq = NULL;
2908tx_failed:
2909	for (i = 0; i < registered_queues; i++)
2910		release_sub_crq_queue(adapter, allqueues[i], 1);
2911	kfree(allqueues);
2912	return -1;
2913}
2914
2915static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2916{
2917	struct device *dev = &adapter->vdev->dev;
2918	union ibmvnic_crq crq;
2919	int max_entries;
2920
2921	if (!retry) {
2922		/* Sub-CRQ entries are 32 byte long */
2923		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2924
2925		if (adapter->min_tx_entries_per_subcrq > entries_page ||
2926		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
2927			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2928			return;
2929		}
2930
2931		if (adapter->desired.mtu)
2932			adapter->req_mtu = adapter->desired.mtu;
2933		else
2934			adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2935
2936		if (!adapter->desired.tx_entries)
2937			adapter->desired.tx_entries =
2938					adapter->max_tx_entries_per_subcrq;
2939		if (!adapter->desired.rx_entries)
2940			adapter->desired.rx_entries =
2941					adapter->max_rx_add_entries_per_subcrq;
2942
2943		max_entries = IBMVNIC_MAX_LTB_SIZE /
2944			      (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2945
2946		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2947			adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2948			adapter->desired.tx_entries = max_entries;
2949		}
2950
2951		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2952			adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2953			adapter->desired.rx_entries = max_entries;
2954		}
2955
2956		if (adapter->desired.tx_entries)
2957			adapter->req_tx_entries_per_subcrq =
2958					adapter->desired.tx_entries;
2959		else
2960			adapter->req_tx_entries_per_subcrq =
2961					adapter->max_tx_entries_per_subcrq;
2962
2963		if (adapter->desired.rx_entries)
2964			adapter->req_rx_add_entries_per_subcrq =
2965					adapter->desired.rx_entries;
2966		else
2967			adapter->req_rx_add_entries_per_subcrq =
2968					adapter->max_rx_add_entries_per_subcrq;
2969
2970		if (adapter->desired.tx_queues)
2971			adapter->req_tx_queues =
2972					adapter->desired.tx_queues;
2973		else
2974			adapter->req_tx_queues =
2975					adapter->opt_tx_comp_sub_queues;
2976
2977		if (adapter->desired.rx_queues)
2978			adapter->req_rx_queues =
2979					adapter->desired.rx_queues;
2980		else
2981			adapter->req_rx_queues =
2982					adapter->opt_rx_comp_queues;
2983
2984		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2985	}
2986
2987	memset(&crq, 0, sizeof(crq));
2988	crq.request_capability.first = IBMVNIC_CRQ_CMD;
2989	crq.request_capability.cmd = REQUEST_CAPABILITY;
2990
2991	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2992	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2993	atomic_inc(&adapter->running_cap_crqs);
2994	ibmvnic_send_crq(adapter, &crq);
2995
2996	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2997	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2998	atomic_inc(&adapter->running_cap_crqs);
2999	ibmvnic_send_crq(adapter, &crq);
3000
3001	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3002	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3003	atomic_inc(&adapter->running_cap_crqs);
3004	ibmvnic_send_crq(adapter, &crq);
3005
3006	crq.request_capability.capability =
3007	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3008	crq.request_capability.number =
3009	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3010	atomic_inc(&adapter->running_cap_crqs);
3011	ibmvnic_send_crq(adapter, &crq);
3012
3013	crq.request_capability.capability =
3014	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3015	crq.request_capability.number =
3016	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3017	atomic_inc(&adapter->running_cap_crqs);
3018	ibmvnic_send_crq(adapter, &crq);
3019
3020	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3021	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3022	atomic_inc(&adapter->running_cap_crqs);
3023	ibmvnic_send_crq(adapter, &crq);
3024
3025	if (adapter->netdev->flags & IFF_PROMISC) {
3026		if (adapter->promisc_supported) {
3027			crq.request_capability.capability =
3028			    cpu_to_be16(PROMISC_REQUESTED);
3029			crq.request_capability.number = cpu_to_be64(1);
3030			atomic_inc(&adapter->running_cap_crqs);
3031			ibmvnic_send_crq(adapter, &crq);
3032		}
3033	} else {
3034		crq.request_capability.capability =
3035		    cpu_to_be16(PROMISC_REQUESTED);
3036		crq.request_capability.number = cpu_to_be64(0);
3037		atomic_inc(&adapter->running_cap_crqs);
3038		ibmvnic_send_crq(adapter, &crq);
3039	}
3040}
3041
3042static int pending_scrq(struct ibmvnic_adapter *adapter,
3043			struct ibmvnic_sub_crq_queue *scrq)
3044{
3045	union sub_crq *entry = &scrq->msgs[scrq->cur];
3046
3047	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3048		return 1;
3049	else
3050		return 0;
3051}
3052
3053static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3054					struct ibmvnic_sub_crq_queue *scrq)
3055{
3056	union sub_crq *entry;
3057	unsigned long flags;
3058
3059	spin_lock_irqsave(&scrq->lock, flags);
3060	entry = &scrq->msgs[scrq->cur];
3061	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3062		if (++scrq->cur == scrq->size)
3063			scrq->cur = 0;
3064	} else {
3065		entry = NULL;
3066	}
3067	spin_unlock_irqrestore(&scrq->lock, flags);
3068
3069	return entry;
3070}
3071
3072static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3073{
3074	struct ibmvnic_crq_queue *queue = &adapter->crq;
3075	union ibmvnic_crq *crq;
3076
3077	crq = &queue->msgs[queue->cur];
3078	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3079		if (++queue->cur == queue->size)
3080			queue->cur = 0;
3081	} else {
3082		crq = NULL;
3083	}
3084
3085	return crq;
3086}
3087
3088static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3089		       union sub_crq *sub_crq)
3090{
3091	unsigned int ua = adapter->vdev->unit_address;
3092	struct device *dev = &adapter->vdev->dev;
3093	u64 *u64_crq = (u64 *)sub_crq;
3094	int rc;
3095
3096	netdev_dbg(adapter->netdev,
3097		   "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3098		   (unsigned long int)cpu_to_be64(remote_handle),
3099		   (unsigned long int)cpu_to_be64(u64_crq[0]),
3100		   (unsigned long int)cpu_to_be64(u64_crq[1]),
3101		   (unsigned long int)cpu_to_be64(u64_crq[2]),
3102		   (unsigned long int)cpu_to_be64(u64_crq[3]));
3103
3104	/* Make sure the hypervisor sees the complete request */
3105	mb();
3106
3107	rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3108				cpu_to_be64(remote_handle),
3109				cpu_to_be64(u64_crq[0]),
3110				cpu_to_be64(u64_crq[1]),
3111				cpu_to_be64(u64_crq[2]),
3112				cpu_to_be64(u64_crq[3]));
3113
3114	if (rc) {
3115		if (rc == H_CLOSED)
3116			dev_warn(dev, "CRQ Queue closed\n");
3117		dev_err(dev, "Send error (rc=%d)\n", rc);
3118	}
3119
3120	return rc;
3121}
3122
3123static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3124				u64 remote_handle, u64 ioba, u64 num_entries)
3125{
3126	unsigned int ua = adapter->vdev->unit_address;
3127	struct device *dev = &adapter->vdev->dev;
3128	int rc;
3129
3130	/* Make sure the hypervisor sees the complete request */
3131	mb();
3132	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3133				cpu_to_be64(remote_handle),
3134				ioba, num_entries);
3135
3136	if (rc) {
3137		if (rc == H_CLOSED)
3138			dev_warn(dev, "CRQ Queue closed\n");
3139		dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3140	}
3141
3142	return rc;
3143}
3144
3145static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3146			    union ibmvnic_crq *crq)
3147{
3148	unsigned int ua = adapter->vdev->unit_address;
3149	struct device *dev = &adapter->vdev->dev;
3150	u64 *u64_crq = (u64 *)crq;
3151	int rc;
3152
3153	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3154		   (unsigned long int)cpu_to_be64(u64_crq[0]),
3155		   (unsigned long int)cpu_to_be64(u64_crq[1]));
3156
3157	/* Make sure the hypervisor sees the complete request */
3158	mb();
3159
3160	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3161				cpu_to_be64(u64_crq[0]),
3162				cpu_to_be64(u64_crq[1]));
3163
3164	if (rc) {
3165		if (rc == H_CLOSED) {
3166			dev_warn(dev, "CRQ Queue closed\n");
3167			if (adapter->resetting)
3168				ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3169		}
3170
3171		dev_warn(dev, "Send error (rc=%d)\n", rc);
3172	}
3173
3174	return rc;
3175}
3176
3177static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3178{
3179	union ibmvnic_crq crq;
3180
3181	memset(&crq, 0, sizeof(crq));
3182	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3183	crq.generic.cmd = IBMVNIC_CRQ_INIT;
3184	netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3185
3186	return ibmvnic_send_crq(adapter, &crq);
3187}
3188
3189static int send_version_xchg(struct ibmvnic_adapter *adapter)
3190{
3191	union ibmvnic_crq crq;
3192
3193	memset(&crq, 0, sizeof(crq));
3194	crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3195	crq.version_exchange.cmd = VERSION_EXCHANGE;
3196	crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3197
3198	return ibmvnic_send_crq(adapter, &crq);
3199}
3200
3201struct vnic_login_client_data {
3202	u8	type;
3203	__be16	len;
3204	char	name[];
3205} __packed;
3206
3207static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3208{
3209	int len;
3210
3211	/* Calculate the amount of buffer space needed for the
3212	 * vnic client data in the login buffer. There are four entries,
3213	 * OS name, LPAR name, device name, and a null last entry.
3214	 */
3215	len = 4 * sizeof(struct vnic_login_client_data);
3216	len += 6; /* "Linux" plus NULL */
3217	len += strlen(utsname()->nodename) + 1;
3218	len += strlen(adapter->netdev->name) + 1;
3219
3220	return len;
3221}
3222
3223static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3224				 struct vnic_login_client_data *vlcd)
3225{
3226	const char *os_name = "Linux";
3227	int len;
3228
3229	/* Type 1 - LPAR OS */
3230	vlcd->type = 1;
3231	len = strlen(os_name) + 1;
3232	vlcd->len = cpu_to_be16(len);
3233	strncpy(vlcd->name, os_name, len);
3234	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3235
3236	/* Type 2 - LPAR name */
3237	vlcd->type = 2;
3238	len = strlen(utsname()->nodename) + 1;
3239	vlcd->len = cpu_to_be16(len);
3240	strncpy(vlcd->name, utsname()->nodename, len);
3241	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3242
3243	/* Type 3 - device name */
3244	vlcd->type = 3;
3245	len = strlen(adapter->netdev->name) + 1;
3246	vlcd->len = cpu_to_be16(len);
3247	strncpy(vlcd->name, adapter->netdev->name, len);
3248}
3249
3250static int send_login(struct ibmvnic_adapter *adapter)
3251{
3252	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3253	struct ibmvnic_login_buffer *login_buffer;
3254	struct device *dev = &adapter->vdev->dev;
3255	dma_addr_t rsp_buffer_token;
3256	dma_addr_t buffer_token;
3257	size_t rsp_buffer_size;
3258	union ibmvnic_crq crq;
3259	size_t buffer_size;
3260	__be64 *tx_list_p;
3261	__be64 *rx_list_p;
3262	int client_data_len;
3263	struct vnic_login_client_data *vlcd;
3264	int i;
3265
3266	if (!adapter->tx_scrq || !adapter->rx_scrq) {
3267		netdev_err(adapter->netdev,
3268			   "RX or TX queues are not allocated, device login failed\n");
3269		return -1;
3270	}
3271
3272	release_login_rsp_buffer(adapter);
3273	client_data_len = vnic_client_data_len(adapter);
3274
3275	buffer_size =
3276	    sizeof(struct ibmvnic_login_buffer) +
3277	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3278	    client_data_len;
3279
3280	login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3281	if (!login_buffer)
3282		goto buf_alloc_failed;
3283
3284	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3285				      DMA_TO_DEVICE);
3286	if (dma_mapping_error(dev, buffer_token)) {
3287		dev_err(dev, "Couldn't map login buffer\n");
3288		goto buf_map_failed;
3289	}
3290
3291	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3292			  sizeof(u64) * adapter->req_tx_queues +
3293			  sizeof(u64) * adapter->req_rx_queues +
3294			  sizeof(u64) * adapter->req_rx_queues +
3295			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3296
3297	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3298	if (!login_rsp_buffer)
3299		goto buf_rsp_alloc_failed;
3300
3301	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3302					  rsp_buffer_size, DMA_FROM_DEVICE);
3303	if (dma_mapping_error(dev, rsp_buffer_token)) {
3304		dev_err(dev, "Couldn't map login rsp buffer\n");
3305		goto buf_rsp_map_failed;
3306	}
3307
3308	adapter->login_buf = login_buffer;
3309	adapter->login_buf_token = buffer_token;
3310	adapter->login_buf_sz = buffer_size;
3311	adapter->login_rsp_buf = login_rsp_buffer;
3312	adapter->login_rsp_buf_token = rsp_buffer_token;
3313	adapter->login_rsp_buf_sz = rsp_buffer_size;
3314
3315	login_buffer->len = cpu_to_be32(buffer_size);
3316	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3317	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3318	login_buffer->off_txcomp_subcrqs =
3319	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3320	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3321	login_buffer->off_rxcomp_subcrqs =
3322	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3323			sizeof(u64) * adapter->req_tx_queues);
3324	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3325	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3326
3327	tx_list_p = (__be64 *)((char *)login_buffer +
3328				      sizeof(struct ibmvnic_login_buffer));
3329	rx_list_p = (__be64 *)((char *)login_buffer +
3330				      sizeof(struct ibmvnic_login_buffer) +
3331				      sizeof(u64) * adapter->req_tx_queues);
3332
3333	for (i = 0; i < adapter->req_tx_queues; i++) {
3334		if (adapter->tx_scrq[i]) {
3335			tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3336						   crq_num);
3337		}
3338	}
3339
3340	for (i = 0; i < adapter->req_rx_queues; i++) {
3341		if (adapter->rx_scrq[i]) {
3342			rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3343						   crq_num);
3344		}
3345	}
3346
3347	/* Insert vNIC login client data */
3348	vlcd = (struct vnic_login_client_data *)
3349		((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3350	login_buffer->client_data_offset =
3351			cpu_to_be32((char *)vlcd - (char *)login_buffer);
3352	login_buffer->client_data_len = cpu_to_be32(client_data_len);
3353
3354	vnic_add_client_data(adapter, vlcd);
3355
3356	netdev_dbg(adapter->netdev, "Login Buffer:\n");
3357	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3358		netdev_dbg(adapter->netdev, "%016lx\n",
3359			   ((unsigned long int *)(adapter->login_buf))[i]);
3360	}
3361
3362	memset(&crq, 0, sizeof(crq));
3363	crq.login.first = IBMVNIC_CRQ_CMD;
3364	crq.login.cmd = LOGIN;
3365	crq.login.ioba = cpu_to_be32(buffer_token);
3366	crq.login.len = cpu_to_be32(buffer_size);
3367	ibmvnic_send_crq(adapter, &crq);
3368
3369	return 0;
3370
3371buf_rsp_map_failed:
3372	kfree(login_rsp_buffer);
3373buf_rsp_alloc_failed:
3374	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3375buf_map_failed:
3376	kfree(login_buffer);
3377buf_alloc_failed:
3378	return -1;
3379}
3380
3381static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3382			     u32 len, u8 map_id)
3383{
3384	union ibmvnic_crq crq;
3385
3386	memset(&crq, 0, sizeof(crq));
3387	crq.request_map.first = IBMVNIC_CRQ_CMD;
3388	crq.request_map.cmd = REQUEST_MAP;
3389	crq.request_map.map_id = map_id;
3390	crq.request_map.ioba = cpu_to_be32(addr);
3391	crq.request_map.len = cpu_to_be32(len);
3392	ibmvnic_send_crq(adapter, &crq);
3393}
3394
3395static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3396{
3397	union ibmvnic_crq crq;
3398
3399	memset(&crq, 0, sizeof(crq));
3400	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3401	crq.request_unmap.cmd = REQUEST_UNMAP;
3402	crq.request_unmap.map_id = map_id;
3403	ibmvnic_send_crq(adapter, &crq);
3404}
3405
3406static void send_map_query(struct ibmvnic_adapter *adapter)
3407{
3408	union ibmvnic_crq crq;
3409
3410	memset(&crq, 0, sizeof(crq));
3411	crq.query_map.first = IBMVNIC_CRQ_CMD;
3412	crq.query_map.cmd = QUERY_MAP;
3413	ibmvnic_send_crq(adapter, &crq);
3414}
3415
3416/* Send a series of CRQs requesting various capabilities of the VNIC server */
3417static void send_cap_queries(struct ibmvnic_adapter *adapter)
3418{
3419	union ibmvnic_crq crq;
3420
3421	atomic_set(&adapter->running_cap_crqs, 0);
3422	memset(&crq, 0, sizeof(crq));
3423	crq.query_capability.first = IBMVNIC_CRQ_CMD;
3424	crq.query_capability.cmd = QUERY_CAPABILITY;
3425
3426	crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3427	atomic_inc(&adapter->running_cap_crqs);
3428	ibmvnic_send_crq(adapter, &crq);
3429
3430	crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3431	atomic_inc(&adapter->running_cap_crqs);
3432	ibmvnic_send_crq(adapter, &crq);
3433
3434	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3435	atomic_inc(&adapter->running_cap_crqs);
3436	ibmvnic_send_crq(adapter, &crq);
3437
3438	crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3439	atomic_inc(&adapter->running_cap_crqs);
3440	ibmvnic_send_crq(adapter, &crq);
3441
3442	crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3443	atomic_inc(&adapter->running_cap_crqs);
3444	ibmvnic_send_crq(adapter, &crq);
3445
3446	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3447	atomic_inc(&adapter->running_cap_crqs);
3448	ibmvnic_send_crq(adapter, &crq);
3449
3450	crq.query_capability.capability =
3451	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3452	atomic_inc(&adapter->running_cap_crqs);
3453	ibmvnic_send_crq(adapter, &crq);
3454
3455	crq.query_capability.capability =
3456	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3457	atomic_inc(&adapter->running_cap_crqs);
3458	ibmvnic_send_crq(adapter, &crq);
3459
3460	crq.query_capability.capability =
3461	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3462	atomic_inc(&adapter->running_cap_crqs);
3463	ibmvnic_send_crq(adapter, &crq);
3464
3465	crq.query_capability.capability =
3466	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3467	atomic_inc(&adapter->running_cap_crqs);
3468	ibmvnic_send_crq(adapter, &crq);
3469
3470	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3471	atomic_inc(&adapter->running_cap_crqs);
3472	ibmvnic_send_crq(adapter, &crq);
3473
3474	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3475	atomic_inc(&adapter->running_cap_crqs);
3476	ibmvnic_send_crq(adapter, &crq);
3477
3478	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3479	atomic_inc(&adapter->running_cap_crqs);
3480	ibmvnic_send_crq(adapter, &crq);
3481
3482	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3483	atomic_inc(&adapter->running_cap_crqs);
3484	ibmvnic_send_crq(adapter, &crq);
3485
3486	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3487	atomic_inc(&adapter->running_cap_crqs);
3488	ibmvnic_send_crq(adapter, &crq);
3489
3490	crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3491	atomic_inc(&adapter->running_cap_crqs);
3492	ibmvnic_send_crq(adapter, &crq);
3493
3494	crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3495	atomic_inc(&adapter->running_cap_crqs);
3496	ibmvnic_send_crq(adapter, &crq);
3497
3498	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3499	atomic_inc(&adapter->running_cap_crqs);
3500	ibmvnic_send_crq(adapter, &crq);
3501
3502	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3503	atomic_inc(&adapter->running_cap_crqs);
3504	ibmvnic_send_crq(adapter, &crq);
3505
3506	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3507	atomic_inc(&adapter->running_cap_crqs);
3508	ibmvnic_send_crq(adapter, &crq);
3509
3510	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3511	atomic_inc(&adapter->running_cap_crqs);
3512	ibmvnic_send_crq(adapter, &crq);
3513
3514	crq.query_capability.capability =
3515			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3516	atomic_inc(&adapter->running_cap_crqs);
3517	ibmvnic_send_crq(adapter, &crq);
3518
3519	crq.query_capability.capability =
3520			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3521	atomic_inc(&adapter->running_cap_crqs);
3522	ibmvnic_send_crq(adapter, &crq);
3523
3524	crq.query_capability.capability =
3525			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3526	atomic_inc(&adapter->running_cap_crqs);
3527	ibmvnic_send_crq(adapter, &crq);
3528
3529	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3530	atomic_inc(&adapter->running_cap_crqs);
3531	ibmvnic_send_crq(adapter, &crq);
3532}
3533
3534static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3535				struct ibmvnic_adapter *adapter)
3536{
3537	struct device *dev = &adapter->vdev->dev;
3538
3539	if (crq->get_vpd_size_rsp.rc.code) {
3540		dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3541			crq->get_vpd_size_rsp.rc.code);
3542		complete(&adapter->fw_done);
3543		return;
3544	}
3545
3546	adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3547	complete(&adapter->fw_done);
3548}
3549
3550static void handle_vpd_rsp(union ibmvnic_crq *crq,
3551			   struct ibmvnic_adapter *adapter)
3552{
3553	struct device *dev = &adapter->vdev->dev;
3554	unsigned char *substr = NULL;
3555	u8 fw_level_len = 0;
3556
3557	memset(adapter->fw_version, 0, 32);
3558
3559	dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3560			 DMA_FROM_DEVICE);
3561
3562	if (crq->get_vpd_rsp.rc.code) {
3563		dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3564			crq->get_vpd_rsp.rc.code);
3565		goto complete;
3566	}
3567
3568	/* get the position of the firmware version info
3569	 * located after the ASCII 'RM' substring in the buffer
3570	 */
3571	substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3572	if (!substr) {
3573		dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3574		goto complete;
3575	}
3576
3577	/* get length of firmware level ASCII substring */
3578	if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3579		fw_level_len = *(substr + 2);
3580	} else {
3581		dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3582		goto complete;
3583	}
3584
3585	/* copy firmware version string from vpd into adapter */
3586	if ((substr + 3 + fw_level_len) <
3587	    (adapter->vpd->buff + adapter->vpd->len)) {
3588		strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3589	} else {
3590		dev_info(dev, "FW substr extrapolated VPD buff\n");
3591	}
3592
3593complete:
3594	if (adapter->fw_version[0] == '\0')
3595		strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3596	complete(&adapter->fw_done);
3597}
3598
3599static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3600{
3601	struct device *dev = &adapter->vdev->dev;
3602	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3603	union ibmvnic_crq crq;
3604	int i;
3605
3606	dma_unmap_single(dev, adapter->ip_offload_tok,
3607			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3608
3609	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3610	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3611		netdev_dbg(adapter->netdev, "%016lx\n",
3612			   ((unsigned long int *)(buf))[i]);
3613
3614	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3615	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3616	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3617		   buf->tcp_ipv4_chksum);
3618	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3619		   buf->tcp_ipv6_chksum);
3620	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3621		   buf->udp_ipv4_chksum);
3622	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3623		   buf->udp_ipv6_chksum);
3624	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3625		   buf->large_tx_ipv4);
3626	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3627		   buf->large_tx_ipv6);
3628	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3629		   buf->large_rx_ipv4);
3630	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3631		   buf->large_rx_ipv6);
3632	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3633		   buf->max_ipv4_header_size);
3634	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3635		   buf->max_ipv6_header_size);
3636	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3637		   buf->max_tcp_header_size);
3638	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3639		   buf->max_udp_header_size);
3640	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3641		   buf->max_large_tx_size);
3642	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3643		   buf->max_large_rx_size);
3644	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3645		   buf->ipv6_extension_header);
3646	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3647		   buf->tcp_pseudosum_req);
3648	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3649		   buf->num_ipv6_ext_headers);
3650	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3651		   buf->off_ipv6_ext_headers);
3652
3653	adapter->ip_offload_ctrl_tok =
3654	    dma_map_single(dev, &adapter->ip_offload_ctrl,
3655			   sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3656
3657	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3658		dev_err(dev, "Couldn't map ip offload control buffer\n");
3659		return;
3660	}
3661
3662	adapter->ip_offload_ctrl.len =
3663	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3664	adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3665	adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3666	adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3667	adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3668	adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3669	adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3670	adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3671	adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3672	adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3673
3674	/* large_rx disabled for now, additional features needed */
3675	adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3676	adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3677
3678	adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3679
3680	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3681		adapter->netdev->features |= NETIF_F_IP_CSUM;
3682
3683	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3684		adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3685
3686	if ((adapter->netdev->features &
3687	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3688		adapter->netdev->features |= NETIF_F_RXCSUM;
3689
3690	if (buf->large_tx_ipv4)
3691		adapter->netdev->features |= NETIF_F_TSO;
3692	if (buf->large_tx_ipv6)
3693		adapter->netdev->features |= NETIF_F_TSO6;
3694
3695	adapter->netdev->hw_features |= adapter->netdev->features;
3696
3697	memset(&crq, 0, sizeof(crq));
3698	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3699	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3700	crq.control_ip_offload.len =
3701	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3702	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3703	ibmvnic_send_crq(adapter, &crq);
3704}
3705
3706static void handle_error_info_rsp(union ibmvnic_crq *crq,
3707				  struct ibmvnic_adapter *adapter)
3708{
3709	struct device *dev = &adapter->vdev->dev;
3710	struct ibmvnic_error_buff *error_buff, *tmp;
3711	unsigned long flags;
3712	bool found = false;
3713	int i;
3714
3715	if (!crq->request_error_rsp.rc.code) {
3716		dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3717			 crq->request_error_rsp.rc.code);
3718		return;
3719	}
3720
3721	spin_lock_irqsave(&adapter->error_list_lock, flags);
3722	list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3723		if (error_buff->error_id == crq->request_error_rsp.error_id) {
3724			found = true;
3725			list_del(&error_buff->list);
3726			break;
3727		}
3728	spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3729
3730	if (!found) {
3731		dev_err(dev, "Couldn't find error id %x\n",
3732			be32_to_cpu(crq->request_error_rsp.error_id));
3733		return;
3734	}
3735
3736	dev_err(dev, "Detailed info for error id %x:",
3737		be32_to_cpu(crq->request_error_rsp.error_id));
3738
3739	for (i = 0; i < error_buff->len; i++) {
3740		pr_cont("%02x", (int)error_buff->buff[i]);
3741		if (i % 8 == 7)
3742			pr_cont(" ");
3743	}
3744	pr_cont("\n");
3745
3746	dma_unmap_single(dev, error_buff->dma, error_buff->len,
3747			 DMA_FROM_DEVICE);
3748	kfree(error_buff->buff);
3749	kfree(error_buff);
3750}
3751
3752static void request_error_information(struct ibmvnic_adapter *adapter,
3753				      union ibmvnic_crq *err_crq)
3754{
3755	struct device *dev = &adapter->vdev->dev;
3756	struct net_device *netdev = adapter->netdev;
3757	struct ibmvnic_error_buff *error_buff;
3758	unsigned long timeout = msecs_to_jiffies(30000);
3759	union ibmvnic_crq crq;
3760	unsigned long flags;
3761	int rc, detail_len;
3762
3763	error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3764	if (!error_buff)
3765		return;
3766
3767	detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3768	error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3769	if (!error_buff->buff) {
3770		kfree(error_buff);
3771		return;
3772	}
3773
3774	error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3775					 DMA_FROM_DEVICE);
3776	if (dma_mapping_error(dev, error_buff->dma)) {
3777		netdev_err(netdev, "Couldn't map error buffer\n");
3778		kfree(error_buff->buff);
3779		kfree(error_buff);
3780		return;
3781	}
3782
3783	error_buff->len = detail_len;
3784	error_buff->error_id = err_crq->error_indication.error_id;
3785
3786	spin_lock_irqsave(&adapter->error_list_lock, flags);
3787	list_add_tail(&error_buff->list, &adapter->errors);
3788	spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3789
3790	memset(&crq, 0, sizeof(crq));
3791	crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3792	crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3793	crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3794	crq.request_error_info.len = cpu_to_be32(detail_len);
3795	crq.request_error_info.error_id = err_crq->error_indication.error_id;
3796
3797	rc = ibmvnic_send_crq(adapter, &crq);
3798	if (rc) {
3799		netdev_err(netdev, "failed to request error information\n");
3800		goto err_info_fail;
3801	}
3802
3803	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3804		netdev_err(netdev, "timeout waiting for error information\n");
3805		goto err_info_fail;
3806	}
3807
3808	return;
3809
3810err_info_fail:
3811	spin_lock_irqsave(&adapter->error_list_lock, flags);
3812	list_del(&error_buff->list);
3813	spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3814
3815	kfree(error_buff->buff);
3816	kfree(error_buff);
3817}
3818
3819static void handle_error_indication(union ibmvnic_crq *crq,
3820				    struct ibmvnic_adapter *adapter)
3821{
3822	struct device *dev = &adapter->vdev->dev;
3823
3824	dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3825		crq->error_indication.flags
3826			& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3827		be32_to_cpu(crq->error_indication.error_id),
3828		be16_to_cpu(crq->error_indication.error_cause));
3829
3830	if (be32_to_cpu(crq->error_indication.error_id))
3831		request_error_information(adapter, crq);
3832
3833	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3834		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3835	else
3836		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3837}
3838
3839static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3840				 struct ibmvnic_adapter *adapter)
3841{
3842	struct net_device *netdev = adapter->netdev;
3843	struct device *dev = &adapter->vdev->dev;
3844	long rc;
3845
3846	rc = crq->change_mac_addr_rsp.rc.code;
3847	if (rc) {
3848		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3849		goto out;
3850	}
3851	memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3852	       ETH_ALEN);
3853out:
3854	complete(&adapter->fw_done);
3855	return rc;
3856}
3857
3858static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3859				   struct ibmvnic_adapter *adapter)
3860{
3861	struct device *dev = &adapter->vdev->dev;
3862	u64 *req_value;
3863	char *name;
3864
3865	atomic_dec(&adapter->running_cap_crqs);
3866	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3867	case REQ_TX_QUEUES:
3868		req_value = &adapter->req_tx_queues;
3869		name = "tx";
3870		break;
3871	case REQ_RX_QUEUES:
3872		req_value = &adapter->req_rx_queues;
3873		name = "rx";
3874		break;
3875	case REQ_RX_ADD_QUEUES:
3876		req_value = &adapter->req_rx_add_queues;
3877		name = "rx_add";
3878		break;
3879	case REQ_TX_ENTRIES_PER_SUBCRQ:
3880		req_value = &adapter->req_tx_entries_per_subcrq;
3881		name = "tx_entries_per_subcrq";
3882		break;
3883	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3884		req_value = &adapter->req_rx_add_entries_per_subcrq;
3885		name = "rx_add_entries_per_subcrq";
3886		break;
3887	case REQ_MTU:
3888		req_value = &adapter->req_mtu;
3889		name = "mtu";
3890		break;
3891	case PROMISC_REQUESTED:
3892		req_value = &adapter->promisc;
3893		name = "promisc";
3894		break;
3895	default:
3896		dev_err(dev, "Got invalid cap request rsp %d\n",
3897			crq->request_capability.capability);
3898		return;
3899	}
3900
3901	switch (crq->request_capability_rsp.rc.code) {
3902	case SUCCESS:
3903		break;
3904	case PARTIALSUCCESS:
3905		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3906			 *req_value,
3907			 (long int)be64_to_cpu(crq->request_capability_rsp.
3908					       number), name);
3909
3910		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3911		    REQ_MTU) {
3912			pr_err("mtu of %llu is not supported. Reverting.\n",
3913			       *req_value);
3914			*req_value = adapter->fallback.mtu;
3915		} else {
3916			*req_value =
3917				be64_to_cpu(crq->request_capability_rsp.number);
3918		}
3919
3920		ibmvnic_send_req_caps(adapter, 1);
3921		return;
3922	default:
3923		dev_err(dev, "Error %d in request cap rsp\n",
3924			crq->request_capability_rsp.rc.code);
3925		return;
3926	}
3927
3928	/* Done receiving requested capabilities, query IP offload support */
3929	if (atomic_read(&adapter->running_cap_crqs) == 0) {
3930		union ibmvnic_crq newcrq;
3931		int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3932		struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3933		    &adapter->ip_offload_buf;
3934
3935		adapter->wait_capability = false;
3936		adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3937							 buf_sz,
3938							 DMA_FROM_DEVICE);
3939
3940		if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3941			if (!firmware_has_feature(FW_FEATURE_CMO))
3942				dev_err(dev, "Couldn't map offload buffer\n");
3943			return;
3944		}
3945
3946		memset(&newcrq, 0, sizeof(newcrq));
3947		newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3948		newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3949		newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3950		newcrq.query_ip_offload.ioba =
3951		    cpu_to_be32(adapter->ip_offload_tok);
3952
3953		ibmvnic_send_crq(adapter, &newcrq);
3954	}
3955}
3956
3957static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3958			    struct ibmvnic_adapter *adapter)
3959{
3960	struct device *dev = &adapter->vdev->dev;
3961	struct net_device *netdev = adapter->netdev;
3962	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3963	struct ibmvnic_login_buffer *login = adapter->login_buf;
3964	int i;
3965
3966	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3967			 DMA_TO_DEVICE);
3968	dma_unmap_single(dev, adapter->login_rsp_buf_token,
3969			 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
3970
3971	/* If the number of queues requested can't be allocated by the
3972	 * server, the login response will return with code 1. We will need
3973	 * to resend the login buffer with fewer queues requested.
3974	 */
3975	if (login_rsp_crq->generic.rc.code) {
3976		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
3977		complete(&adapter->init_done);
3978		return 0;
3979	}
3980
3981	netdev->mtu = adapter->req_mtu - ETH_HLEN;
3982
3983	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3984	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3985		netdev_dbg(adapter->netdev, "%016lx\n",
3986			   ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3987	}
3988
3989	/* Sanity checks */
3990	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3991	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
3992	     adapter->req_rx_add_queues !=
3993	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3994		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3995		ibmvnic_remove(adapter->vdev);
3996		return -EIO;
3997	}
3998	release_login_buffer(adapter);
3999	complete(&adapter->init_done);
4000
4001	return 0;
4002}
4003
4004static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4005				     struct ibmvnic_adapter *adapter)
4006{
4007	struct device *dev = &adapter->vdev->dev;
4008	long rc;
4009
4010	rc = crq->request_unmap_rsp.rc.code;
4011	if (rc)
4012		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4013}
4014
4015static void handle_query_map_rsp(union ibmvnic_crq *crq,
4016				 struct ibmvnic_adapter *adapter)
4017{
4018	struct net_device *netdev = adapter->netdev;
4019	struct device *dev = &adapter->vdev->dev;
4020	long rc;
4021
4022	rc = crq->query_map_rsp.rc.code;
4023	if (rc) {
4024		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4025		return;
4026	}
4027	netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4028		   crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4029		   crq->query_map_rsp.free_pages);
4030}
4031
4032static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4033				 struct ibmvnic_adapter *adapter)
4034{
4035	struct net_device *netdev = adapter->netdev;
4036	struct device *dev = &adapter->vdev->dev;
4037	long rc;
4038
4039	atomic_dec(&adapter->running_cap_crqs);
4040	netdev_dbg(netdev, "Outstanding queries: %d\n",
4041		   atomic_read(&adapter->running_cap_crqs));
4042	rc = crq->query_capability.rc.code;
4043	if (rc) {
4044		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4045		goto out;
4046	}
4047
4048	switch (be16_to_cpu(crq->query_capability.capability)) {
4049	case MIN_TX_QUEUES:
4050		adapter->min_tx_queues =
4051		    be64_to_cpu(crq->query_capability.number);
4052		netdev_dbg(netdev, "min_tx_queues = %lld\n",
4053			   adapter->min_tx_queues);
4054		break;
4055	case MIN_RX_QUEUES:
4056		adapter->min_rx_queues =
4057		    be64_to_cpu(crq->query_capability.number);
4058		netdev_dbg(netdev, "min_rx_queues = %lld\n",
4059			   adapter->min_rx_queues);
4060		break;
4061	case MIN_RX_ADD_QUEUES:
4062		adapter->min_rx_add_queues =
4063		    be64_to_cpu(crq->query_capability.number);
4064		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4065			   adapter->min_rx_add_queues);
4066		break;
4067	case MAX_TX_QUEUES:
4068		adapter->max_tx_queues =
4069		    be64_to_cpu(crq->query_capability.number);
4070		netdev_dbg(netdev, "max_tx_queues = %lld\n",
4071			   adapter->max_tx_queues);
4072		break;
4073	case MAX_RX_QUEUES:
4074		adapter->max_rx_queues =
4075		    be64_to_cpu(crq->query_capability.number);
4076		netdev_dbg(netdev, "max_rx_queues = %lld\n",
4077			   adapter->max_rx_queues);
4078		break;
4079	case MAX_RX_ADD_QUEUES:
4080		adapter->max_rx_add_queues =
4081		    be64_to_cpu(crq->query_capability.number);
4082		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4083			   adapter->max_rx_add_queues);
4084		break;
4085	case MIN_TX_ENTRIES_PER_SUBCRQ:
4086		adapter->min_tx_entries_per_subcrq =
4087		    be64_to_cpu(crq->query_capability.number);
4088		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4089			   adapter->min_tx_entries_per_subcrq);
4090		break;
4091	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4092		adapter->min_rx_add_entries_per_subcrq =
4093		    be64_to_cpu(crq->query_capability.number);
4094		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4095			   adapter->min_rx_add_entries_per_subcrq);
4096		break;
4097	case MAX_TX_ENTRIES_PER_SUBCRQ:
4098		adapter->max_tx_entries_per_subcrq =
4099		    be64_to_cpu(crq->query_capability.number);
4100		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4101			   adapter->max_tx_entries_per_subcrq);
4102		break;
4103	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4104		adapter->max_rx_add_entries_per_subcrq =
4105		    be64_to_cpu(crq->query_capability.number);
4106		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4107			   adapter->max_rx_add_entries_per_subcrq);
4108		break;
4109	case TCP_IP_OFFLOAD:
4110		adapter->tcp_ip_offload =
4111		    be64_to_cpu(crq->query_capability.number);
4112		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4113			   adapter->tcp_ip_offload);
4114		break;
4115	case PROMISC_SUPPORTED:
4116		adapter->promisc_supported =
4117		    be64_to_cpu(crq->query_capability.number);
4118		netdev_dbg(netdev, "promisc_supported = %lld\n",
4119			   adapter->promisc_supported);
4120		break;
4121	case MIN_MTU:
4122		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4123		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4124		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4125		break;
4126	case MAX_MTU:
4127		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4128		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4129		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4130		break;
4131	case MAX_MULTICAST_FILTERS:
4132		adapter->max_multicast_filters =
4133		    be64_to_cpu(crq->query_capability.number);
4134		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4135			   adapter->max_multicast_filters);
4136		break;
4137	case VLAN_HEADER_INSERTION:
4138		adapter->vlan_header_insertion =
4139		    be64_to_cpu(crq->query_capability.number);
4140		if (adapter->vlan_header_insertion)
4141			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4142		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4143			   adapter->vlan_header_insertion);
4144		break;
4145	case RX_VLAN_HEADER_INSERTION:
4146		adapter->rx_vlan_header_insertion =
4147		    be64_to_cpu(crq->query_capability.number);
4148		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4149			   adapter->rx_vlan_header_insertion);
4150		break;
4151	case MAX_TX_SG_ENTRIES:
4152		adapter->max_tx_sg_entries =
4153		    be64_to_cpu(crq->query_capability.number);
4154		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4155			   adapter->max_tx_sg_entries);
4156		break;
4157	case RX_SG_SUPPORTED:
4158		adapter->rx_sg_supported =
4159		    be64_to_cpu(crq->query_capability.number);
4160		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4161			   adapter->rx_sg_supported);
4162		break;
4163	case OPT_TX_COMP_SUB_QUEUES:
4164		adapter->opt_tx_comp_sub_queues =
4165		    be64_to_cpu(crq->query_capability.number);
4166		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4167			   adapter->opt_tx_comp_sub_queues);
4168		break;
4169	case OPT_RX_COMP_QUEUES:
4170		adapter->opt_rx_comp_queues =
4171		    be64_to_cpu(crq->query_capability.number);
4172		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4173			   adapter->opt_rx_comp_queues);
4174		break;
4175	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4176		adapter->opt_rx_bufadd_q_per_rx_comp_q =
4177		    be64_to_cpu(crq->query_capability.number);
4178		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4179			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
4180		break;
4181	case OPT_TX_ENTRIES_PER_SUBCRQ:
4182		adapter->opt_tx_entries_per_subcrq =
4183		    be64_to_cpu(crq->query_capability.number);
4184		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4185			   adapter->opt_tx_entries_per_subcrq);
4186		break;
4187	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4188		adapter->opt_rxba_entries_per_subcrq =
4189		    be64_to_cpu(crq->query_capability.number);
4190		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4191			   adapter->opt_rxba_entries_per_subcrq);
4192		break;
4193	case TX_RX_DESC_REQ:
4194		adapter->tx_rx_desc_req = crq->query_capability.number;
4195		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4196			   adapter->tx_rx_desc_req);
4197		break;
4198
4199	default:
4200		netdev_err(netdev, "Got invalid cap rsp %d\n",
4201			   crq->query_capability.capability);
4202	}
4203
4204out:
4205	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4206		adapter->wait_capability = false;
4207		ibmvnic_send_req_caps(adapter, 0);
4208	}
4209}
4210
4211static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4212			       struct ibmvnic_adapter *adapter)
4213{
4214	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4215	struct net_device *netdev = adapter->netdev;
4216	struct device *dev = &adapter->vdev->dev;
4217	u64 *u64_crq = (u64 *)crq;
4218	long rc;
4219
4220	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4221		   (unsigned long int)cpu_to_be64(u64_crq[0]),
4222		   (unsigned long int)cpu_to_be64(u64_crq[1]));
4223	switch (gen_crq->first) {
4224	case IBMVNIC_CRQ_INIT_RSP:
4225		switch (gen_crq->cmd) {
4226		case IBMVNIC_CRQ_INIT:
4227			dev_info(dev, "Partner initialized\n");
4228			adapter->from_passive_init = true;
4229			adapter->failover_pending = false;
4230			complete(&adapter->init_done);
4231			ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4232			break;
4233		case IBMVNIC_CRQ_INIT_COMPLETE:
4234			dev_info(dev, "Partner initialization complete\n");
4235			send_version_xchg(adapter);
4236			break;
4237		default:
4238			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4239		}
4240		return;
4241	case IBMVNIC_CRQ_XPORT_EVENT:
4242		netif_carrier_off(netdev);
4243		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4244			dev_info(dev, "Migrated, re-enabling adapter\n");
4245			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4246		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4247			dev_info(dev, "Backing device failover detected\n");
4248			adapter->failover_pending = true;
4249		} else {
4250			/* The adapter lost the connection */
4251			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4252				gen_crq->cmd);
4253			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4254		}
4255		return;
4256	case IBMVNIC_CRQ_CMD_RSP:
4257		break;
4258	default:
4259		dev_err(dev, "Got an invalid msg type 0x%02x\n",
4260			gen_crq->first);
4261		return;
4262	}
4263
4264	switch (gen_crq->cmd) {
4265	case VERSION_EXCHANGE_RSP:
4266		rc = crq->version_exchange_rsp.rc.code;
4267		if (rc) {
4268			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4269			break;
4270		}
4271		dev_info(dev, "Partner protocol version is %d\n",
4272			 crq->version_exchange_rsp.version);
4273		if (be16_to_cpu(crq->version_exchange_rsp.version) <
4274		    ibmvnic_version)
4275			ibmvnic_version =
4276			    be16_to_cpu(crq->version_exchange_rsp.version);
4277		send_cap_queries(adapter);
4278		break;
4279	case QUERY_CAPABILITY_RSP:
4280		handle_query_cap_rsp(crq, adapter);
4281		break;
4282	case QUERY_MAP_RSP:
4283		handle_query_map_rsp(crq, adapter);
4284		break;
4285	case REQUEST_MAP_RSP:
4286		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4287		complete(&adapter->fw_done);
4288		break;
4289	case REQUEST_UNMAP_RSP:
4290		handle_request_unmap_rsp(crq, adapter);
4291		break;
4292	case REQUEST_CAPABILITY_RSP:
4293		handle_request_cap_rsp(crq, adapter);
4294		break;
4295	case LOGIN_RSP:
4296		netdev_dbg(netdev, "Got Login Response\n");
4297		handle_login_rsp(crq, adapter);
4298		break;
4299	case LOGICAL_LINK_STATE_RSP:
4300		netdev_dbg(netdev,
4301			   "Got Logical Link State Response, state: %d rc: %d\n",
4302			   crq->logical_link_state_rsp.link_state,
4303			   crq->logical_link_state_rsp.rc.code);
4304		adapter->logical_link_state =
4305		    crq->logical_link_state_rsp.link_state;
4306		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4307		complete(&adapter->init_done);
4308		break;
4309	case LINK_STATE_INDICATION:
4310		netdev_dbg(netdev, "Got Logical Link State Indication\n");
4311		adapter->phys_link_state =
4312		    crq->link_state_indication.phys_link_state;
4313		adapter->logical_link_state =
4314		    crq->link_state_indication.logical_link_state;
4315		break;
4316	case CHANGE_MAC_ADDR_RSP:
4317		netdev_dbg(netdev, "Got MAC address change Response\n");
4318		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4319		break;
4320	case ERROR_INDICATION:
4321		netdev_dbg(netdev, "Got Error Indication\n");
4322		handle_error_indication(crq, adapter);
4323		break;
4324	case REQUEST_ERROR_RSP:
4325		netdev_dbg(netdev, "Got Error Detail Response\n");
4326		handle_error_info_rsp(crq, adapter);
4327		break;
4328	case REQUEST_STATISTICS_RSP:
4329		netdev_dbg(netdev, "Got Statistics Response\n");
4330		complete(&adapter->stats_done);
4331		break;
4332	case QUERY_IP_OFFLOAD_RSP:
4333		netdev_dbg(netdev, "Got Query IP offload Response\n");
4334		handle_query_ip_offload_rsp(adapter);
4335		break;
4336	case MULTICAST_CTRL_RSP:
4337		netdev_dbg(netdev, "Got multicast control Response\n");
4338		break;
4339	case CONTROL_IP_OFFLOAD_RSP:
4340		netdev_dbg(netdev, "Got Control IP offload Response\n");
4341		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4342				 sizeof(adapter->ip_offload_ctrl),
4343				 DMA_TO_DEVICE);
4344		complete(&adapter->init_done);
4345		break;
4346	case COLLECT_FW_TRACE_RSP:
4347		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4348		complete(&adapter->fw_done);
4349		break;
4350	case GET_VPD_SIZE_RSP:
4351		handle_vpd_size_rsp(crq, adapter);
4352		break;
4353	case GET_VPD_RSP:
4354		handle_vpd_rsp(crq, adapter);
4355		break;
4356	default:
4357		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4358			   gen_crq->cmd);
4359	}
4360}
4361
4362static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4363{
4364	struct ibmvnic_adapter *adapter = instance;
4365
4366	tasklet_schedule(&adapter->tasklet);
4367	return IRQ_HANDLED;
4368}
4369
4370static void ibmvnic_tasklet(void *data)
4371{
4372	struct ibmvnic_adapter *adapter = data;
4373	struct ibmvnic_crq_queue *queue = &adapter->crq;
4374	union ibmvnic_crq *crq;
4375	unsigned long flags;
4376	bool done = false;
4377
4378	spin_lock_irqsave(&queue->lock, flags);
4379	while (!done) {
4380		/* Pull all the valid messages off the CRQ */
4381		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4382			ibmvnic_handle_crq(crq, adapter);
4383			crq->generic.first = 0;
4384		}
4385
4386		/* remain in tasklet until all
4387		 * capabilities responses are received
4388		 */
4389		if (!adapter->wait_capability)
4390			done = true;
4391	}
4392	/* if capabilities CRQ's were sent in this tasklet, the following
4393	 * tasklet must wait until all responses are received
4394	 */
4395	if (atomic_read(&adapter->running_cap_crqs) != 0)
4396		adapter->wait_capability = true;
4397	spin_unlock_irqrestore(&queue->lock, flags);
4398}
4399
4400static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4401{
4402	struct vio_dev *vdev = adapter->vdev;
4403	int rc;
4404
4405	do {
4406		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4407	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4408
4409	if (rc)
4410		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4411
4412	return rc;
4413}
4414
4415static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4416{
4417	struct ibmvnic_crq_queue *crq = &adapter->crq;
4418	struct device *dev = &adapter->vdev->dev;
4419	struct vio_dev *vdev = adapter->vdev;
4420	int rc;
4421
4422	/* Close the CRQ */
4423	do {
4424		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4425	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4426
4427	/* Clean out the queue */
4428	memset(crq->msgs, 0, PAGE_SIZE);
4429	crq->cur = 0;
4430
4431	/* And re-open it again */
4432	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4433				crq->msg_token, PAGE_SIZE);
4434
4435	if (rc == H_CLOSED)
4436		/* Adapter is good, but other end is not ready */
4437		dev_warn(dev, "Partner adapter not ready\n");
4438	else if (rc != 0)
4439		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4440
4441	return rc;
4442}
4443
4444static void release_crq_queue(struct ibmvnic_adapter *adapter)
4445{
4446	struct ibmvnic_crq_queue *crq = &adapter->crq;
4447	struct vio_dev *vdev = adapter->vdev;
4448	long rc;
4449
4450	if (!crq->msgs)
4451		return;
4452
4453	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4454	free_irq(vdev->irq, adapter);
4455	tasklet_kill(&adapter->tasklet);
4456	do {
4457		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4458	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4459
4460	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4461			 DMA_BIDIRECTIONAL);
4462	free_page((unsigned long)crq->msgs);
4463	crq->msgs = NULL;
4464}
4465
4466static int init_crq_queue(struct ibmvnic_adapter *adapter)
4467{
4468	struct ibmvnic_crq_queue *crq = &adapter->crq;
4469	struct device *dev = &adapter->vdev->dev;
4470	struct vio_dev *vdev = adapter->vdev;
4471	int rc, retrc = -ENOMEM;
4472
4473	if (crq->msgs)
4474		return 0;
4475
4476	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4477	/* Should we allocate more than one page? */
4478
4479	if (!crq->msgs)
4480		return -ENOMEM;
4481
4482	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4483	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4484					DMA_BIDIRECTIONAL);
4485	if (dma_mapping_error(dev, crq->msg_token))
4486		goto map_failed;
4487
4488	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4489				crq->msg_token, PAGE_SIZE);
4490
4491	if (rc == H_RESOURCE)
4492		/* maybe kexecing and resource is busy. try a reset */
4493		rc = ibmvnic_reset_crq(adapter);
4494	retrc = rc;
4495
4496	if (rc == H_CLOSED) {
4497		dev_warn(dev, "Partner adapter not ready\n");
4498	} else if (rc) {
4499		dev_warn(dev, "Error %d opening adapter\n", rc);
4500		goto reg_crq_failed;
4501	}
4502
4503	retrc = 0;
4504
4505	tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4506		     (unsigned long)adapter);
4507
4508	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4509	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4510			 adapter);
4511	if (rc) {
4512		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4513			vdev->irq, rc);
4514		goto req_irq_failed;
4515	}
4516
4517	rc = vio_enable_interrupts(vdev);
4518	if (rc) {
4519		dev_err(dev, "Error %d enabling interrupts\n", rc);
4520		goto req_irq_failed;
4521	}
4522
4523	crq->cur = 0;
4524	spin_lock_init(&crq->lock);
4525
4526	return retrc;
4527
4528req_irq_failed:
4529	tasklet_kill(&adapter->tasklet);
4530	do {
4531		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4532	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4533reg_crq_failed:
4534	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4535map_failed:
4536	free_page((unsigned long)crq->msgs);
4537	crq->msgs = NULL;
4538	return retrc;
4539}
4540
4541static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4542{
4543	struct device *dev = &adapter->vdev->dev;
4544	unsigned long timeout = msecs_to_jiffies(30000);
4545	u64 old_num_rx_queues, old_num_tx_queues;
4546	int rc;
4547
4548	adapter->from_passive_init = false;
4549
4550	old_num_rx_queues = adapter->req_rx_queues;
4551	old_num_tx_queues = adapter->req_tx_queues;
4552
4553	init_completion(&adapter->init_done);
4554	adapter->init_done_rc = 0;
4555	ibmvnic_send_crq_init(adapter);
4556	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4557		dev_err(dev, "Initialization sequence timed out\n");
4558		return -1;
4559	}
4560
4561	if (adapter->init_done_rc) {
4562		release_crq_queue(adapter);
4563		return adapter->init_done_rc;
4564	}
4565
4566	if (adapter->from_passive_init) {
4567		adapter->state = VNIC_OPEN;
4568		adapter->from_passive_init = false;
4569		return -1;
4570	}
4571
4572	if (adapter->resetting && !adapter->wait_for_reset &&
4573	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
4574		if (adapter->req_rx_queues != old_num_rx_queues ||
4575		    adapter->req_tx_queues != old_num_tx_queues) {
4576			release_sub_crqs(adapter, 0);
4577			rc = init_sub_crqs(adapter);
4578		} else {
4579			rc = reset_sub_crq_queues(adapter);
4580		}
4581	} else {
4582		rc = init_sub_crqs(adapter);
4583	}
4584
4585	if (rc) {
4586		dev_err(dev, "Initialization of sub crqs failed\n");
4587		release_crq_queue(adapter);
4588		return rc;
4589	}
4590
4591	rc = init_sub_crq_irqs(adapter);
4592	if (rc) {
4593		dev_err(dev, "Failed to initialize sub crq irqs\n");
4594		release_crq_queue(adapter);
4595	}
4596
4597	return rc;
4598}
4599
4600static struct device_attribute dev_attr_failover;
4601
4602static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4603{
4604	struct ibmvnic_adapter *adapter;
4605	struct net_device *netdev;
4606	unsigned char *mac_addr_p;
4607	int rc;
4608
4609	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4610		dev->unit_address);
4611
4612	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4613							VETH_MAC_ADDR, NULL);
4614	if (!mac_addr_p) {
4615		dev_err(&dev->dev,
4616			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4617			__FILE__, __LINE__);
4618		return 0;
4619	}
4620
4621	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4622				   IBMVNIC_MAX_QUEUES);
4623	if (!netdev)
4624		return -ENOMEM;
4625
4626	adapter = netdev_priv(netdev);
4627	adapter->state = VNIC_PROBING;
4628	dev_set_drvdata(&dev->dev, netdev);
4629	adapter->vdev = dev;
4630	adapter->netdev = netdev;
4631
4632	ether_addr_copy(adapter->mac_addr, mac_addr_p);
4633	ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4634	netdev->irq = dev->irq;
4635	netdev->netdev_ops = &ibmvnic_netdev_ops;
4636	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4637	SET_NETDEV_DEV(netdev, &dev->dev);
4638
4639	spin_lock_init(&adapter->stats_lock);
4640
4641	INIT_LIST_HEAD(&adapter->errors);
4642	spin_lock_init(&adapter->error_list_lock);
4643
4644	INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4645	INIT_LIST_HEAD(&adapter->rwi_list);
4646	mutex_init(&adapter->reset_lock);
4647	mutex_init(&adapter->rwi_lock);
4648	adapter->resetting = false;
4649
4650	adapter->mac_change_pending = false;
4651
4652	do {
4653		rc = init_crq_queue(adapter);
4654		if (rc) {
4655			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4656				rc);
4657			goto ibmvnic_init_fail;
4658		}
4659
4660		rc = ibmvnic_init(adapter);
4661		if (rc && rc != EAGAIN)
4662			goto ibmvnic_init_fail;
4663	} while (rc == EAGAIN);
4664
4665	rc = init_stats_buffers(adapter);
4666	if (rc)
4667		goto ibmvnic_init_fail;
4668
4669	rc = init_stats_token(adapter);
4670	if (rc)
4671		goto ibmvnic_stats_fail;
4672
4673	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4674	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4675	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4676
4677	rc = device_create_file(&dev->dev, &dev_attr_failover);
4678	if (rc)
4679		goto ibmvnic_dev_file_err;
4680
4681	netif_carrier_off(netdev);
4682	rc = register_netdev(netdev);
4683	if (rc) {
4684		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4685		goto ibmvnic_register_fail;
4686	}
4687	dev_info(&dev->dev, "ibmvnic registered\n");
4688
4689	adapter->state = VNIC_PROBED;
4690
4691	adapter->wait_for_reset = false;
4692
4693	return 0;
4694
4695ibmvnic_register_fail:
4696	device_remove_file(&dev->dev, &dev_attr_failover);
4697
4698ibmvnic_dev_file_err:
4699	release_stats_token(adapter);
4700
4701ibmvnic_stats_fail:
4702	release_stats_buffers(adapter);
4703
4704ibmvnic_init_fail:
4705	release_sub_crqs(adapter, 1);
4706	release_crq_queue(adapter);
4707	free_netdev(netdev);
4708
4709	return rc;
4710}
4711
4712static int ibmvnic_remove(struct vio_dev *dev)
4713{
4714	struct net_device *netdev = dev_get_drvdata(&dev->dev);
4715	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4716
4717	adapter->state = VNIC_REMOVING;
4718	unregister_netdev(netdev);
4719	mutex_lock(&adapter->reset_lock);
4720
4721	release_resources(adapter);
4722	release_sub_crqs(adapter, 1);
4723	release_crq_queue(adapter);
4724
4725	release_stats_token(adapter);
4726	release_stats_buffers(adapter);
4727
4728	adapter->state = VNIC_REMOVED;
4729
4730	mutex_unlock(&adapter->reset_lock);
4731	device_remove_file(&dev->dev, &dev_attr_failover);
4732	free_netdev(netdev);
4733	dev_set_drvdata(&dev->dev, NULL);
4734
4735	return 0;
4736}
4737
4738static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4739			      const char *buf, size_t count)
4740{
4741	struct net_device *netdev = dev_get_drvdata(dev);
4742	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4743	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4744	__be64 session_token;
4745	long rc;
4746
4747	if (!sysfs_streq(buf, "1"))
4748		return -EINVAL;
4749
4750	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4751			 H_GET_SESSION_TOKEN, 0, 0, 0);
4752	if (rc) {
4753		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4754			   rc);
4755		return -EINVAL;
4756	}
4757
4758	session_token = (__be64)retbuf[0];
4759	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4760		   be64_to_cpu(session_token));
4761	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4762				H_SESSION_ERR_DETECTED, session_token, 0, 0);
4763	if (rc) {
4764		netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4765			   rc);
4766		return -EINVAL;
4767	}
4768
4769	return count;
4770}
4771
4772static DEVICE_ATTR_WO(failover);
4773
4774static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4775{
4776	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4777	struct ibmvnic_adapter *adapter;
4778	struct iommu_table *tbl;
4779	unsigned long ret = 0;
4780	int i;
4781
4782	tbl = get_iommu_table_base(&vdev->dev);
4783
4784	/* netdev inits at probe time along with the structures we need below*/
4785	if (!netdev)
4786		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4787
4788	adapter = netdev_priv(netdev);
4789
4790	ret += PAGE_SIZE; /* the crq message queue */
4791	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4792
4793	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4794		ret += 4 * PAGE_SIZE; /* the scrq message queue */
4795
4796	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4797	     i++)
4798		ret += adapter->rx_pool[i].size *
4799		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4800
4801	return ret;
4802}
4803
4804static int ibmvnic_resume(struct device *dev)
4805{
4806	struct net_device *netdev = dev_get_drvdata(dev);
4807	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4808
4809	if (adapter->state != VNIC_OPEN)
4810		return 0;
4811
4812	tasklet_schedule(&adapter->tasklet);
4813
4814	return 0;
4815}
4816
4817static const struct vio_device_id ibmvnic_device_table[] = {
4818	{"network", "IBM,vnic"},
4819	{"", "" }
4820};
4821MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4822
4823static const struct dev_pm_ops ibmvnic_pm_ops = {
4824	.resume = ibmvnic_resume
4825};
4826
4827static struct vio_driver ibmvnic_driver = {
4828	.id_table       = ibmvnic_device_table,
4829	.probe          = ibmvnic_probe,
4830	.remove         = ibmvnic_remove,
4831	.get_desired_dma = ibmvnic_get_desired_dma,
4832	.name		= ibmvnic_driver_name,
4833	.pm		= &ibmvnic_pm_ops,
4834};
4835
4836/* module functions */
4837static int __init ibmvnic_module_init(void)
4838{
4839	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4840		IBMVNIC_DRIVER_VERSION);
4841
4842	return vio_register_driver(&ibmvnic_driver);
4843}
4844
4845static void __exit ibmvnic_module_exit(void)
4846{
4847	vio_unregister_driver(&ibmvnic_driver);
4848}
4849
4850module_init(ibmvnic_module_init);
4851module_exit(ibmvnic_module_exit);