ibmvnic.c 149.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/**************************************************************************/
/*                                                                        */
/*  IBM System i and System p Virtual NIC Device Driver                   */
/*  Copyright (C) 2014 IBM Corp.                                          */
/*  Santiago Leon (santi_leon@yahoo.com)                                  */
/*  Thomas Falcon (tlfalcon@linux.vnet.ibm.com)                           */
/*  John Allen (jallen@linux.vnet.ibm.com)                                */
/*                                                                        */
/*                                                                        */
/* This module contains the implementation of a virtual ethernet device   */
/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN    */
/* option of the RS/6000 Platform Architecture to interface with virtual  */
/* ethernet NICs that are presented to the partition by the hypervisor.   */
/*									   */
/* Messages are passed between the VNIC driver and the VNIC server using  */
/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to  */
/* issue and receive commands that initiate communication with the server */
/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but    */
/* are used by the driver to notify the server that a packet is           */
/* ready for transmission or that a buffer has been added to receive a    */
/* packet. Subsequently, sCRQs are used by the server to notify the       */
/* driver that a packet transmission has been completed or that a packet  */
/* has been received and placed in a waiting buffer.                      */
/*                                                                        */
/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in    */
/* which skbs are DMA mapped and immediately unmapped when the transmit   */
/* or receive has been completed, the VNIC driver is required to use      */
/* "long term mapping". This entails that large, continuous DMA mapped    */
/* buffers are allocated on driver initialization and these buffers are   */
/* then continuously reused to pass skbs to and from the VNIC server.     */
/*                                                                        */
/**************************************************************************/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/completion.h>
#include <linux/ioport.h>
#include <linux/dma-mapping.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/ethtool.h>
#include <linux/proc_fs.h>
51
#include <linux/if_arp.h>
52 53
#include <linux/in.h>
#include <linux/ip.h>
54
#include <linux/ipv6.h>
55 56 57 58 59 60 61 62 63 64 65
#include <linux/irq.h>
#include <linux/kthread.h>
#include <linux/seq_file.h>
#include <linux/interrupt.h>
#include <net/net_namespace.h>
#include <asm/hvcall.h>
#include <linux/atomic.h>
#include <asm/vio.h>
#include <asm/iommu.h>
#include <linux/uaccess.h>
#include <asm/firmware.h>
66
#include <linux/workqueue.h>
67
#include <linux/if_vlan.h>
68
#include <linux/utsname.h>
69 70 71 72 73 74

#include "ibmvnic.h"

static const char ibmvnic_driver_name[] = "ibmvnic";
static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";

75
MODULE_AUTHOR("Santiago Leon");
76 77 78 79 80 81
MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(IBMVNIC_DRIVER_VERSION);

static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
static int ibmvnic_remove(struct vio_dev *);
82
static void release_sub_crqs(struct ibmvnic_adapter *, bool);
83 84 85 86
static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
87
static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
88 89 90 91 92 93 94 95 96 97
static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
static int enable_scrq_irq(struct ibmvnic_adapter *,
			   struct ibmvnic_sub_crq_queue *);
static int disable_scrq_irq(struct ibmvnic_adapter *,
			    struct ibmvnic_sub_crq_queue *);
static int pending_scrq(struct ibmvnic_adapter *,
			struct ibmvnic_sub_crq_queue *);
static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
					struct ibmvnic_sub_crq_queue *);
static int ibmvnic_poll(struct napi_struct *napi, int data);
98
static void send_query_map(struct ibmvnic_adapter *adapter);
99 100
static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
static int send_request_unmap(struct ibmvnic_adapter *, u8);
101
static int send_login(struct ibmvnic_adapter *adapter);
102
static void send_query_cap(struct ibmvnic_adapter *adapter);
103
static int init_sub_crqs(struct ibmvnic_adapter *);
104
static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
105
static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
106
static void release_crq_queue(struct ibmvnic_adapter *);
107
static int __ibmvnic_set_mac(struct net_device *, u8 *);
108
static int init_crq_queue(struct ibmvnic_adapter *adapter);
109
static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

struct ibmvnic_stat {
	char name[ETH_GSTRING_LEN];
	int offset;
};

#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
			     offsetof(struct ibmvnic_statistics, stat))
#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))

static const struct ibmvnic_stat ibmvnic_stats[] = {
	{"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
	{"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
	{"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
	{"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
	{"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
	{"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
	{"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
	{"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
	{"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
	{"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
	{"align_errors", IBMVNIC_STAT_OFF(align_errors)},
	{"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
	{"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
	{"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
	{"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
	{"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
	{"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
	{"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
	{"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
	{"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
	{"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
	{"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
};

static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
			  unsigned long length, unsigned long *number,
			  unsigned long *irq)
{
	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
	long rc;

	rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
	*number = retbuf[0];
	*irq = retbuf[1];

	return rc;
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * ibmvnic_wait_for_completion - Check device state and wait for completion
 * @adapter: private device data
 * @comp_done: completion structure to wait for
 * @timeout: time to wait in milliseconds
 *
 * Wait for a completion signal or until the timeout limit is reached
 * while checking that the device is still active.
 */
static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
				       struct completion *comp_done,
				       unsigned long timeout)
{
	struct net_device *netdev;
	unsigned long div_timeout;
	u8 retry;

	netdev = adapter->netdev;
	retry = 5;
	div_timeout = msecs_to_jiffies(timeout / retry);
	while (true) {
		if (!adapter->crq.active) {
			netdev_err(netdev, "Device down!\n");
			return -ENODEV;
		}
184
		if (!retry--)
185 186 187 188 189 190 191 192
			break;
		if (wait_for_completion_timeout(comp_done, div_timeout))
			return 0;
	}
	netdev_err(netdev, "Operation timed out.\n");
	return -ETIMEDOUT;
}

193 194 195 196
static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
				struct ibmvnic_long_term_buff *ltb, int size)
{
	struct device *dev = &adapter->vdev->dev;
197
	int rc;
198 199 200 201 202 203 204 205 206 207 208

	ltb->size = size;
	ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
				       GFP_KERNEL);

	if (!ltb->buff) {
		dev_err(dev, "Couldn't alloc long term buffer\n");
		return -ENOMEM;
	}
	ltb->map_id = adapter->map_id;
	adapter->map_id++;
209

T
Thomas Falcon 已提交
210 211
	mutex_lock(&adapter->fw_lock);
	adapter->fw_done_rc = 0;
212
	reinit_completion(&adapter->fw_done);
213 214 215 216
	rc = send_request_map(adapter, ltb->addr,
			      ltb->size, ltb->map_id);
	if (rc) {
		dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
T
Thomas Falcon 已提交
217
		mutex_unlock(&adapter->fw_lock);
218 219
		return rc;
	}
220 221 222 223 224 225 226

	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
	if (rc) {
		dev_err(dev,
			"Long term map request aborted or timed out,rc = %d\n",
			rc);
		dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
T
Thomas Falcon 已提交
227
		mutex_unlock(&adapter->fw_lock);
228 229
		return rc;
	}
230 231 232 233

	if (adapter->fw_done_rc) {
		dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
			adapter->fw_done_rc);
234
		dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
T
Thomas Falcon 已提交
235
		mutex_unlock(&adapter->fw_lock);
236 237
		return -1;
	}
T
Thomas Falcon 已提交
238
	mutex_unlock(&adapter->fw_lock);
239 240 241 242 243 244 245 246
	return 0;
}

static void free_long_term_buff(struct ibmvnic_adapter *adapter,
				struct ibmvnic_long_term_buff *ltb)
{
	struct device *dev = &adapter->vdev->dev;

247 248 249
	if (!ltb->buff)
		return;

250 251
	if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
	    adapter->reset_reason != VNIC_RESET_MOBILITY)
252
		send_request_unmap(adapter, ltb->map_id);
253
	dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
254 255
}

256 257 258
static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
				struct ibmvnic_long_term_buff *ltb)
{
259
	struct device *dev = &adapter->vdev->dev;
260 261
	int rc;

262 263
	memset(ltb->buff, 0, ltb->size);

T
Thomas Falcon 已提交
264 265 266
	mutex_lock(&adapter->fw_lock);
	adapter->fw_done_rc = 0;

267
	reinit_completion(&adapter->fw_done);
268
	rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
T
Thomas Falcon 已提交
269 270
	if (rc) {
		mutex_unlock(&adapter->fw_lock);
271
		return rc;
T
Thomas Falcon 已提交
272
	}
273 274 275 276 277

	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
	if (rc) {
		dev_info(dev,
			 "Reset failed, long term map request timed out or aborted\n");
T
Thomas Falcon 已提交
278
		mutex_unlock(&adapter->fw_lock);
279 280
		return rc;
	}
281 282

	if (adapter->fw_done_rc) {
283
		dev_info(dev,
284 285
			 "Reset failed, attempting to free and reallocate buffer\n");
		free_long_term_buff(adapter, ltb);
T
Thomas Falcon 已提交
286
		mutex_unlock(&adapter->fw_lock);
287 288
		return alloc_long_term_buff(adapter, ltb, ltb->size);
	}
T
Thomas Falcon 已提交
289
	mutex_unlock(&adapter->fw_lock);
290 291 292
	return 0;
}

293 294 295 296
static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
{
	int i;

297
	for (i = 0; i < adapter->num_active_rx_pools; i++)
298 299 300
		adapter->rx_pool[i].active = 0;
}

301 302 303 304
static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
			      struct ibmvnic_rx_pool *pool)
{
	int count = pool->size - atomic_read(&pool->available);
305
	u64 handle = adapter->rx_scrq[pool->index]->handle;
306
	struct device *dev = &adapter->vdev->dev;
307 308 309
	struct ibmvnic_ind_xmit_queue *ind_bufp;
	struct ibmvnic_sub_crq_queue *rx_scrq;
	union sub_crq *sub_crq;
310 311 312 313 314 315 316 317 318 319
	int buffers_added = 0;
	unsigned long lpar_rc;
	struct sk_buff *skb;
	unsigned int offset;
	dma_addr_t dma_addr;
	unsigned char *dst;
	int shift = 0;
	int index;
	int i;

320 321 322
	if (!pool->active)
		return;

323 324
	rx_scrq = adapter->rx_scrq[pool->index];
	ind_bufp = &rx_scrq->ind_buf;
325
	for (i = 0; i < count; ++i) {
326
		skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
		if (!skb) {
			dev_err(dev, "Couldn't replenish rx buff\n");
			adapter->replenish_no_mem++;
			break;
		}

		index = pool->free_map[pool->next_free];

		if (pool->rx_buff[index].skb)
			dev_err(dev, "Inconsistent free_map!\n");

		/* Copy the skb to the long term mapped DMA buffer */
		offset = index * pool->buff_size;
		dst = pool->long_term_buff.buff + offset;
		memset(dst, 0, pool->buff_size);
		dma_addr = pool->long_term_buff.addr + offset;
		pool->rx_buff[index].data = dst;

		pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
		pool->rx_buff[index].dma = dma_addr;
		pool->rx_buff[index].skb = skb;
		pool->rx_buff[index].pool_index = pool->index;
		pool->rx_buff[index].size = pool->buff_size;

351 352 353 354
		sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
		memset(sub_crq, 0, sizeof(*sub_crq));
		sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
		sub_crq->rx_add.correlator =
355
		    cpu_to_be64((u64)&pool->rx_buff[index]);
356 357
		sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
		sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
358 359 360 361 362 363 364 365 366

		/* The length field of the sCRQ is defined to be 24 bits so the
		 * buffer size needs to be left shifted by a byte before it is
		 * converted to big endian to prevent the last byte from being
		 * truncated.
		 */
#ifdef __LITTLE_ENDIAN__
		shift = 8;
#endif
367
		sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
368
		pool->next_free = (pool->next_free + 1) % pool->size;
369 370 371 372 373 374 375 376 377 378 379 380
		if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
		    i == count - 1) {
			lpar_rc =
				send_subcrq_indirect(adapter, handle,
						     (u64)ind_bufp->indir_dma,
						     (u64)ind_bufp->index);
			if (lpar_rc != H_SUCCESS)
				goto failure;
			buffers_added += ind_bufp->index;
			adapter->replenish_add_buff_success += ind_bufp->index;
			ind_bufp->index = 0;
		}
381 382 383 384 385
	}
	atomic_add(buffers_added, &pool->available);
	return;

failure:
386 387
	if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
		dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
388 389 390 391 392 393 394 395 396 397 398 399 400
	for (i = ind_bufp->index - 1; i >= 0; --i) {
		struct ibmvnic_rx_buff *rx_buff;

		pool->next_free = pool->next_free == 0 ?
				  pool->size - 1 : pool->next_free - 1;
		sub_crq = &ind_bufp->indir_arr[i];
		rx_buff = (struct ibmvnic_rx_buff *)
				be64_to_cpu(sub_crq->rx_add.correlator);
		index = (int)(rx_buff - pool->rx_buff);
		pool->free_map[pool->next_free] = index;
		dev_kfree_skb_any(pool->rx_buff[index].skb);
		pool->rx_buff[index].skb = NULL;
	}
401 402
	adapter->replenish_add_buff_failure += ind_bufp->index;
	atomic_add(buffers_added, &pool->available);
403
	ind_bufp->index = 0;
404
	if (lpar_rc == H_CLOSED || adapter->failover_pending) {
405
		/* Disable buffer pool replenishment and report carrier off if
406 407 408
		 * queue is closed or pending failover.
		 * Firmware guarantees that a signal will be sent to the
		 * driver, triggering a reset.
409 410 411 412
		 */
		deactivate_rx_pools(adapter);
		netif_carrier_off(adapter->netdev);
	}
413 414 415 416 417 418 419
}

static void replenish_pools(struct ibmvnic_adapter *adapter)
{
	int i;

	adapter->replenish_task_cycles++;
420
	for (i = 0; i < adapter->num_active_rx_pools; i++) {
421 422 423
		if (adapter->rx_pool[i].active)
			replenish_rx_pool(adapter, &adapter->rx_pool[i]);
	}
S
Sukadev Bhattiprolu 已提交
424 425

	netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
426 427
}

428 429 430 431
static void release_stats_buffers(struct ibmvnic_adapter *adapter)
{
	kfree(adapter->tx_stats_buffers);
	kfree(adapter->rx_stats_buffers);
432 433
	adapter->tx_stats_buffers = NULL;
	adapter->rx_stats_buffers = NULL;
434 435 436 437 438
}

static int init_stats_buffers(struct ibmvnic_adapter *adapter)
{
	adapter->tx_stats_buffers =
439
				kcalloc(IBMVNIC_MAX_QUEUES,
440 441 442 443 444 445
					sizeof(struct ibmvnic_tx_queue_stats),
					GFP_KERNEL);
	if (!adapter->tx_stats_buffers)
		return -ENOMEM;

	adapter->rx_stats_buffers =
446
				kcalloc(IBMVNIC_MAX_QUEUES,
447 448 449 450 451 452 453 454
					sizeof(struct ibmvnic_rx_queue_stats),
					GFP_KERNEL);
	if (!adapter->rx_stats_buffers)
		return -ENOMEM;

	return 0;
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
static void release_stats_token(struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;

	if (!adapter->stats_token)
		return;

	dma_unmap_single(dev, adapter->stats_token,
			 sizeof(struct ibmvnic_statistics),
			 DMA_FROM_DEVICE);
	adapter->stats_token = 0;
}

static int init_stats_token(struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
	dma_addr_t stok;

	stok = dma_map_single(dev, &adapter->stats,
			      sizeof(struct ibmvnic_statistics),
			      DMA_FROM_DEVICE);
	if (dma_mapping_error(dev, stok)) {
		dev_err(dev, "Couldn't map stats buffer\n");
		return -1;
	}

	adapter->stats_token = stok;
482
	netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
483 484 485
	return 0;
}

486 487 488
static int reset_rx_pools(struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_rx_pool *rx_pool;
489
	u64 buff_size;
490
	int rx_scrqs;
491
	int i, j, rc;
492

493 494 495
	if (!adapter->rx_pool)
		return -1;

496 497
	buff_size = adapter->cur_rx_buf_sz;
	rx_scrqs = adapter->num_active_rx_pools;
498 499 500
	for (i = 0; i < rx_scrqs; i++) {
		rx_pool = &adapter->rx_pool[i];

501 502
		netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);

503
		if (rx_pool->buff_size != buff_size) {
504
			free_long_term_buff(adapter, &rx_pool->long_term_buff);
505
			rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
506 507 508 509
			rc = alloc_long_term_buff(adapter,
						  &rx_pool->long_term_buff,
						  rx_pool->size *
						  rx_pool->buff_size);
510 511 512 513 514
		} else {
			rc = reset_long_term_buff(adapter,
						  &rx_pool->long_term_buff);
		}

515 516
		if (rc)
			return rc;
517 518 519 520 521 522 523 524 525 526

		for (j = 0; j < rx_pool->size; j++)
			rx_pool->free_map[j] = j;

		memset(rx_pool->rx_buff, 0,
		       rx_pool->size * sizeof(struct ibmvnic_rx_buff));

		atomic_set(&rx_pool->available, 0);
		rx_pool->next_alloc = 0;
		rx_pool->next_free = 0;
527
		rx_pool->active = 1;
528 529 530 531 532
	}

	return 0;
}

533
static void release_rx_pools(struct ibmvnic_adapter *adapter)
534
{
535 536
	struct ibmvnic_rx_pool *rx_pool;
	int i, j;
537

538
	if (!adapter->rx_pool)
539 540
		return;

541
	for (i = 0; i < adapter->num_active_rx_pools; i++) {
542 543
		rx_pool = &adapter->rx_pool[i];

544 545
		netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);

546 547 548 549
		kfree(rx_pool->free_map);
		free_long_term_buff(adapter, &rx_pool->long_term_buff);

		if (!rx_pool->rx_buff)
550
			continue;
551 552 553

		for (j = 0; j < rx_pool->size; j++) {
			if (rx_pool->rx_buff[j].skb) {
554 555
				dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
				rx_pool->rx_buff[j].skb = NULL;
556 557 558 559 560 561 562 563
			}
		}

		kfree(rx_pool->rx_buff);
	}

	kfree(adapter->rx_pool);
	adapter->rx_pool = NULL;
564
	adapter->num_active_rx_pools = 0;
565 566 567 568 569 570 571 572
}

static int init_rx_pools(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	struct device *dev = &adapter->vdev->dev;
	struct ibmvnic_rx_pool *rx_pool;
	int rxadd_subcrqs;
573
	u64 buff_size;
574 575
	int i, j;

576 577
	rxadd_subcrqs = adapter->num_active_rx_scrqs;
	buff_size = adapter->cur_rx_buf_sz;
578 579 580 581 582 583 584 585 586

	adapter->rx_pool = kcalloc(rxadd_subcrqs,
				   sizeof(struct ibmvnic_rx_pool),
				   GFP_KERNEL);
	if (!adapter->rx_pool) {
		dev_err(dev, "Failed to allocate rx pools\n");
		return -1;
	}

587 588
	adapter->num_active_rx_pools = rxadd_subcrqs;

589 590 591 592
	for (i = 0; i < rxadd_subcrqs; i++) {
		rx_pool = &adapter->rx_pool[i];

		netdev_dbg(adapter->netdev,
593
			   "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
594
			   i, adapter->req_rx_add_entries_per_subcrq,
595
			   buff_size);
596 597 598

		rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
		rx_pool->index = i;
599
		rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
600 601 602 603 604 605 606
		rx_pool->active = 1;

		rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
					    GFP_KERNEL);
		if (!rx_pool->free_map) {
			release_rx_pools(adapter);
			return -1;
607
		}
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

		rx_pool->rx_buff = kcalloc(rx_pool->size,
					   sizeof(struct ibmvnic_rx_buff),
					   GFP_KERNEL);
		if (!rx_pool->rx_buff) {
			dev_err(dev, "Couldn't alloc rx buffers\n");
			release_rx_pools(adapter);
			return -1;
		}

		if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
					 rx_pool->size * rx_pool->buff_size)) {
			release_rx_pools(adapter);
			return -1;
		}

		for (j = 0; j < rx_pool->size; ++j)
			rx_pool->free_map[j] = j;

		atomic_set(&rx_pool->available, 0);
		rx_pool->next_alloc = 0;
		rx_pool->next_free = 0;
630
	}
631 632

	return 0;
633 634
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
			     struct ibmvnic_tx_pool *tx_pool)
{
	int rc, i;

	rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
	if (rc)
		return rc;

	memset(tx_pool->tx_buff, 0,
	       tx_pool->num_buffers *
	       sizeof(struct ibmvnic_tx_buff));

	for (i = 0; i < tx_pool->num_buffers; i++)
		tx_pool->free_map[i] = i;

	tx_pool->consumer_index = 0;
	tx_pool->producer_index = 0;

	return 0;
}

657 658 659
static int reset_tx_pools(struct ibmvnic_adapter *adapter)
{
	int tx_scrqs;
660
	int i, rc;
661

662 663 664
	if (!adapter->tx_pool)
		return -1;

665
	tx_scrqs = adapter->num_active_tx_pools;
666
	for (i = 0; i < tx_scrqs; i++) {
667
		rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
668 669
		if (rc)
			return rc;
670
		rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
T
Thomas Falcon 已提交
671 672
		if (rc)
			return rc;
673 674 675 676 677
	}

	return 0;
}

678 679 680 681 682 683 684
static void release_vpd_data(struct ibmvnic_adapter *adapter)
{
	if (!adapter->vpd)
		return;

	kfree(adapter->vpd->buff);
	kfree(adapter->vpd);
685 686

	adapter->vpd = NULL;
687 688
}

689 690 691 692 693 694 695 696
static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
				struct ibmvnic_tx_pool *tx_pool)
{
	kfree(tx_pool->tx_buff);
	kfree(tx_pool->free_map);
	free_long_term_buff(adapter, &tx_pool->long_term_buff);
}

697 698
static void release_tx_pools(struct ibmvnic_adapter *adapter)
{
699
	int i;
700 701 702 703

	if (!adapter->tx_pool)
		return;

704
	for (i = 0; i < adapter->num_active_tx_pools; i++) {
705 706
		release_one_tx_pool(adapter, &adapter->tx_pool[i]);
		release_one_tx_pool(adapter, &adapter->tso_pool[i]);
707 708 709 710
	}

	kfree(adapter->tx_pool);
	adapter->tx_pool = NULL;
711 712
	kfree(adapter->tso_pool);
	adapter->tso_pool = NULL;
713
	adapter->num_active_tx_pools = 0;
714 715
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
static int init_one_tx_pool(struct net_device *netdev,
			    struct ibmvnic_tx_pool *tx_pool,
			    int num_entries, int buf_size)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int i;

	tx_pool->tx_buff = kcalloc(num_entries,
				   sizeof(struct ibmvnic_tx_buff),
				   GFP_KERNEL);
	if (!tx_pool->tx_buff)
		return -1;

	if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
				 num_entries * buf_size))
		return -1;

	tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
	if (!tx_pool->free_map)
		return -1;

	for (i = 0; i < num_entries; i++)
		tx_pool->free_map[i] = i;

	tx_pool->consumer_index = 0;
	tx_pool->producer_index = 0;
	tx_pool->num_buffers = num_entries;
	tx_pool->buf_size = buf_size;

	return 0;
}

748 749 750 751
static int init_tx_pools(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int tx_subcrqs;
752
	u64 buff_size;
753
	int i, rc;
754

755
	tx_subcrqs = adapter->num_active_tx_scrqs;
756 757 758 759 760
	adapter->tx_pool = kcalloc(tx_subcrqs,
				   sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
	if (!adapter->tx_pool)
		return -1;

761 762 763 764 765
	adapter->tso_pool = kcalloc(tx_subcrqs,
				    sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
	if (!adapter->tso_pool)
		return -1;

766 767
	adapter->num_active_tx_pools = tx_subcrqs;

768
	for (i = 0; i < tx_subcrqs; i++) {
769 770
		buff_size = adapter->req_mtu + VLAN_HLEN;
		buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
771 772
		rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
				      adapter->req_tx_entries_per_subcrq,
773
				      buff_size);
774
		if (rc) {
775
			release_tx_pools(adapter);
776
			return rc;
T
Thomas Falcon 已提交
777 778
		}

779 780 781
		rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
				      IBMVNIC_TSO_BUFS,
				      IBMVNIC_TSO_BUF_SZ);
782
		if (rc) {
783
			release_tx_pools(adapter);
784
			return rc;
785 786 787 788 789 790
		}
	}

	return 0;
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
{
	int i;

	if (adapter->napi_enabled)
		return;

	for (i = 0; i < adapter->req_rx_queues; i++)
		napi_enable(&adapter->napi[i]);

	adapter->napi_enabled = true;
}

static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
{
	int i;

	if (!adapter->napi_enabled)
		return;

811 812
	for (i = 0; i < adapter->req_rx_queues; i++) {
		netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
813
		napi_disable(&adapter->napi[i]);
814
	}
815 816 817 818

	adapter->napi_enabled = false;
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
static int init_napi(struct ibmvnic_adapter *adapter)
{
	int i;

	adapter->napi = kcalloc(adapter->req_rx_queues,
				sizeof(struct napi_struct), GFP_KERNEL);
	if (!adapter->napi)
		return -ENOMEM;

	for (i = 0; i < adapter->req_rx_queues; i++) {
		netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
		netif_napi_add(adapter->netdev, &adapter->napi[i],
			       ibmvnic_poll, NAPI_POLL_WEIGHT);
	}

834
	adapter->num_active_rx_napi = adapter->req_rx_queues;
835 836 837 838 839 840 841 842 843 844
	return 0;
}

static void release_napi(struct ibmvnic_adapter *adapter)
{
	int i;

	if (!adapter->napi)
		return;

845
	for (i = 0; i < adapter->num_active_rx_napi; i++) {
846 847
		netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
		netif_napi_del(&adapter->napi[i]);
848 849 850 851
	}

	kfree(adapter->napi);
	adapter->napi = NULL;
852
	adapter->num_active_rx_napi = 0;
853
	adapter->napi_enabled = false;
854 855
}

856
static int ibmvnic_login(struct net_device *netdev)
857 858
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
859
	unsigned long timeout = msecs_to_jiffies(20000);
860
	int retry_count = 0;
861
	int retries = 10;
862
	bool retry;
863
	int rc;
864

865
	do {
866
		retry = false;
867
		if (retry_count > retries) {
868 869 870 871 872 873 874
			netdev_warn(netdev, "Login attempts exceeded\n");
			return -1;
		}

		adapter->init_done_rc = 0;
		reinit_completion(&adapter->init_done);
		rc = send_login(adapter);
875
		if (rc)
876 877 878 879
			return rc;

		if (!wait_for_completion_timeout(&adapter->init_done,
						 timeout)) {
880 881 882 883 884
			netdev_warn(netdev, "Login timed out, retrying...\n");
			retry = true;
			adapter->init_done_rc = 0;
			retry_count++;
			continue;
885 886
		}

887 888 889 890 891 892 893 894 895 896
		if (adapter->init_done_rc == ABORTED) {
			netdev_warn(netdev, "Login aborted, retrying...\n");
			retry = true;
			adapter->init_done_rc = 0;
			retry_count++;
			/* FW or device may be busy, so
			 * wait a bit before retrying login
			 */
			msleep(500);
		} else if (adapter->init_done_rc == PARTIALSUCCESS) {
897
			retry_count++;
898
			release_sub_crqs(adapter, 1);
899

900 901 902
			retry = true;
			netdev_dbg(netdev,
				   "Received partial success, retrying...\n");
903
			adapter->init_done_rc = 0;
904
			reinit_completion(&adapter->init_done);
905
			send_query_cap(adapter);
906 907
			if (!wait_for_completion_timeout(&adapter->init_done,
							 timeout)) {
908 909
				netdev_warn(netdev,
					    "Capabilities query timed out\n");
910 911
				return -1;
			}
912

913 914
			rc = init_sub_crqs(adapter);
			if (rc) {
915 916
				netdev_warn(netdev,
					    "SCRQ initialization failed\n");
917 918
				return -1;
			}
919

920 921
			rc = init_sub_crq_irqs(adapter);
			if (rc) {
922 923
				netdev_warn(netdev,
					    "SCRQ irq initialization failed\n");
924 925
				return -1;
			}
926 927
		} else if (adapter->init_done_rc) {
			netdev_warn(netdev, "Adapter login failed\n");
928 929
			return -1;
		}
930
	} while (retry);
931

932
	__ibmvnic_set_mac(netdev, adapter->mac_addr);
933

S
Sukadev Bhattiprolu 已提交
934
	netdev_dbg(netdev, "[S:%d] Login succeeded\n", adapter->state);
935 936 937
	return 0;
}

938 939 940 941 942 943 944 945 946 947 948 949
static void release_login_buffer(struct ibmvnic_adapter *adapter)
{
	kfree(adapter->login_buf);
	adapter->login_buf = NULL;
}

static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
{
	kfree(adapter->login_rsp_buf);
	adapter->login_rsp_buf = NULL;
}

950 951
static void release_resources(struct ibmvnic_adapter *adapter)
{
952 953
	release_vpd_data(adapter);

954 955 956
	release_tx_pools(adapter);
	release_rx_pools(adapter);

957
	release_napi(adapter);
L
Lijun Pan 已提交
958
	release_login_buffer(adapter);
959
	release_login_rsp_buffer(adapter);
960 961
}

962 963 964
static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
{
	struct net_device *netdev = adapter->netdev;
965
	unsigned long timeout = msecs_to_jiffies(20000);
966 967 968 969
	union ibmvnic_crq crq;
	bool resend;
	int rc;

970 971
	netdev_dbg(netdev, "setting link state %d\n", link_state);

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	memset(&crq, 0, sizeof(crq));
	crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
	crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
	crq.logical_link_state.link_state = link_state;

	do {
		resend = false;

		reinit_completion(&adapter->init_done);
		rc = ibmvnic_send_crq(adapter, &crq);
		if (rc) {
			netdev_err(netdev, "Failed to set link state\n");
			return rc;
		}

		if (!wait_for_completion_timeout(&adapter->init_done,
						 timeout)) {
			netdev_err(netdev, "timeout setting link state\n");
			return -1;
		}

993
		if (adapter->init_done_rc == PARTIALSUCCESS) {
994 995 996
			/* Partuial success, delay and re-send */
			mdelay(1000);
			resend = true;
997 998 999 1000
		} else if (adapter->init_done_rc) {
			netdev_warn(netdev, "Unable to set link state, rc=%d\n",
				    adapter->init_done_rc);
			return adapter->init_done_rc;
1001 1002 1003 1004 1005 1006
		}
	} while (resend);

	return 0;
}

1007 1008 1009 1010 1011
static int set_real_num_queues(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int rc;

1012 1013 1014
	netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
		   adapter->req_tx_queues, adapter->req_rx_queues);

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
	if (rc) {
		netdev_err(netdev, "failed to set the number of tx queues\n");
		return rc;
	}

	rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
	if (rc)
		netdev_err(netdev, "failed to set the number of rx queues\n");

	return rc;
}

1028 1029 1030 1031 1032
static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
	union ibmvnic_crq crq;
	int len = 0;
1033
	int rc;
1034 1035 1036 1037

	if (adapter->vpd->buff)
		len = adapter->vpd->len;

T
Thomas Falcon 已提交
1038 1039
	mutex_lock(&adapter->fw_lock);
	adapter->fw_done_rc = 0;
1040
	reinit_completion(&adapter->fw_done);
T
Thomas Falcon 已提交
1041

1042 1043
	crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
	crq.get_vpd_size.cmd = GET_VPD_SIZE;
1044
	rc = ibmvnic_send_crq(adapter, &crq);
T
Thomas Falcon 已提交
1045 1046
	if (rc) {
		mutex_unlock(&adapter->fw_lock);
1047
		return rc;
T
Thomas Falcon 已提交
1048
	}
1049 1050 1051 1052

	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
	if (rc) {
		dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
T
Thomas Falcon 已提交
1053
		mutex_unlock(&adapter->fw_lock);
1054 1055
		return rc;
	}
T
Thomas Falcon 已提交
1056
	mutex_unlock(&adapter->fw_lock);
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

	if (!adapter->vpd->len)
		return -ENODATA;

	if (!adapter->vpd->buff)
		adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
	else if (adapter->vpd->len != len)
		adapter->vpd->buff =
			krealloc(adapter->vpd->buff,
				 adapter->vpd->len, GFP_KERNEL);

	if (!adapter->vpd->buff) {
		dev_err(dev, "Could allocate VPD buffer\n");
		return -ENOMEM;
	}

	adapter->vpd->dma_addr =
		dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
			       DMA_FROM_DEVICE);
1076
	if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
1077 1078
		dev_err(dev, "Could not map VPD buffer\n");
		kfree(adapter->vpd->buff);
1079
		adapter->vpd->buff = NULL;
1080 1081 1082
		return -ENOMEM;
	}

T
Thomas Falcon 已提交
1083 1084
	mutex_lock(&adapter->fw_lock);
	adapter->fw_done_rc = 0;
1085
	reinit_completion(&adapter->fw_done);
T
Thomas Falcon 已提交
1086

1087 1088 1089 1090
	crq.get_vpd.first = IBMVNIC_CRQ_CMD;
	crq.get_vpd.cmd = GET_VPD;
	crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
	crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1091 1092 1093 1094
	rc = ibmvnic_send_crq(adapter, &crq);
	if (rc) {
		kfree(adapter->vpd->buff);
		adapter->vpd->buff = NULL;
T
Thomas Falcon 已提交
1095
		mutex_unlock(&adapter->fw_lock);
1096 1097
		return rc;
	}
1098 1099 1100 1101 1102 1103

	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
	if (rc) {
		dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
		kfree(adapter->vpd->buff);
		adapter->vpd->buff = NULL;
T
Thomas Falcon 已提交
1104
		mutex_unlock(&adapter->fw_lock);
1105 1106
		return rc;
	}
1107

T
Thomas Falcon 已提交
1108
	mutex_unlock(&adapter->fw_lock);
1109 1110 1111
	return 0;
}

1112
static int init_resources(struct ibmvnic_adapter *adapter)
1113
{
1114
	struct net_device *netdev = adapter->netdev;
1115
	int rc;
1116

1117 1118 1119
	rc = set_real_num_queues(netdev);
	if (rc)
		return rc;
1120

1121 1122 1123 1124
	adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
	if (!adapter->vpd)
		return -ENOMEM;

1125 1126 1127 1128 1129 1130 1131
	/* Vital Product Data (VPD) */
	rc = ibmvnic_get_vpd(adapter);
	if (rc) {
		netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
		return rc;
	}

1132
	adapter->map_id = 1;
1133

1134 1135 1136
	rc = init_napi(adapter);
	if (rc)
		return rc;
1137

1138
	send_query_map(adapter);
1139 1140 1141

	rc = init_rx_pools(netdev);
	if (rc)
1142
		return rc;
1143

1144
	rc = init_tx_pools(netdev);
1145 1146 1147
	return rc;
}

1148
static int __ibmvnic_open(struct net_device *netdev)
1149 1150
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1151
	enum vnic_state prev_state = adapter->state;
1152 1153
	int i, rc;

1154
	adapter->state = VNIC_OPENING;
1155
	replenish_pools(adapter);
1156
	ibmvnic_napi_enable(adapter);
1157

1158 1159 1160
	/* We're ready to receive frames, enable the sub-crq interrupts and
	 * set the logical link state to up
	 */
1161
	for (i = 0; i < adapter->req_rx_queues; i++) {
1162
		netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1163 1164
		if (prev_state == VNIC_CLOSED)
			enable_irq(adapter->rx_scrq[i]->irq);
1165
		enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1166
	}
1167

1168
	for (i = 0; i < adapter->req_tx_queues; i++) {
1169
		netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1170 1171
		if (prev_state == VNIC_CLOSED)
			enable_irq(adapter->tx_scrq[i]->irq);
1172
		enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1173
		netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
1174
	}
1175

1176
	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1177 1178 1179 1180
	if (rc) {
		for (i = 0; i < adapter->req_rx_queues; i++)
			napi_disable(&adapter->napi[i]);
		release_resources(adapter);
1181
		return rc;
1182
	}
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	netif_tx_start_all_queues(netdev);

	if (prev_state == VNIC_CLOSED) {
		for (i = 0; i < adapter->req_rx_queues; i++)
			napi_schedule(&adapter->napi[i]);
	}

	adapter->state = VNIC_OPEN;
	return rc;
}

static int ibmvnic_open(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1198
	int rc;
1199

1200 1201 1202 1203 1204 1205 1206 1207
	/* If device failover is pending, just set device state and return.
	 * Device operation will be handled by reset routine.
	 */
	if (adapter->failover_pending) {
		adapter->state = VNIC_OPEN;
		return 0;
	}

1208 1209
	if (adapter->state != VNIC_CLOSED) {
		rc = ibmvnic_login(netdev);
1210
		if (rc)
1211
			goto out;
1212 1213 1214 1215 1216

		rc = init_resources(adapter);
		if (rc) {
			netdev_err(netdev, "failed to initialize resources\n");
			release_resources(adapter);
1217
			goto out;
1218 1219 1220 1221
		}
	}

	rc = __ibmvnic_open(netdev);
1222

1223 1224 1225 1226 1227 1228 1229 1230 1231
out:
	/*
	 * If open fails due to a pending failover, set device state and
	 * return. Device operation will be handled by reset routine.
	 */
	if (rc && adapter->failover_pending) {
		adapter->state = VNIC_OPEN;
		rc = 0;
	}
1232
	return rc;
1233 1234
}

1235 1236 1237
static void clean_rx_pools(struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_rx_pool *rx_pool;
1238
	struct ibmvnic_rx_buff *rx_buff;
1239 1240 1241 1242 1243 1244 1245
	u64 rx_entries;
	int rx_scrqs;
	int i, j;

	if (!adapter->rx_pool)
		return;

1246
	rx_scrqs = adapter->num_active_rx_pools;
1247 1248 1249 1250 1251
	rx_entries = adapter->req_rx_add_entries_per_subcrq;

	/* Free any remaining skbs in the rx buffer pools */
	for (i = 0; i < rx_scrqs; i++) {
		rx_pool = &adapter->rx_pool[i];
1252
		if (!rx_pool || !rx_pool->rx_buff)
1253 1254 1255 1256
			continue;

		netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
		for (j = 0; j < rx_entries; j++) {
1257 1258 1259 1260
			rx_buff = &rx_pool->rx_buff[j];
			if (rx_buff && rx_buff->skb) {
				dev_kfree_skb_any(rx_buff->skb);
				rx_buff->skb = NULL;
1261 1262 1263 1264 1265
			}
		}
	}
}

1266 1267
static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
			      struct ibmvnic_tx_pool *tx_pool)
1268
{
1269
	struct ibmvnic_tx_buff *tx_buff;
1270
	u64 tx_entries;
1271 1272
	int i;

1273
	if (!tx_pool || !tx_pool->tx_buff)
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		return;

	tx_entries = tx_pool->num_buffers;

	for (i = 0; i < tx_entries; i++) {
		tx_buff = &tx_pool->tx_buff[i];
		if (tx_buff && tx_buff->skb) {
			dev_kfree_skb_any(tx_buff->skb);
			tx_buff->skb = NULL;
		}
	}
}

static void clean_tx_pools(struct ibmvnic_adapter *adapter)
{
1289
	int tx_scrqs;
1290
	int i;
1291

1292
	if (!adapter->tx_pool || !adapter->tso_pool)
1293 1294
		return;

1295
	tx_scrqs = adapter->num_active_tx_pools;
1296 1297 1298

	/* Free any remaining skbs in the tx buffer pools */
	for (i = 0; i < tx_scrqs; i++) {
1299
		netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1300 1301
		clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
		clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1302 1303 1304
	}
}

1305
static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1306
{
1307
	struct net_device *netdev = adapter->netdev;
1308 1309
	int i;

1310 1311
	if (adapter->tx_scrq) {
		for (i = 0; i < adapter->req_tx_queues; i++)
1312
			if (adapter->tx_scrq[i]->irq) {
T
Thomas Falcon 已提交
1313
				netdev_dbg(netdev,
1314
					   "Disabling tx_scrq[%d] irq\n", i);
1315
				disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1316
				disable_irq(adapter->tx_scrq[i]->irq);
1317
			}
1318 1319 1320 1321
	}

	if (adapter->rx_scrq) {
		for (i = 0; i < adapter->req_rx_queues; i++) {
1322
			if (adapter->rx_scrq[i]->irq) {
T
Thomas Falcon 已提交
1323
				netdev_dbg(netdev,
1324
					   "Disabling rx_scrq[%d] irq\n", i);
1325
				disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1326
				disable_irq(adapter->rx_scrq[i]->irq);
1327
			}
1328 1329
		}
	}
1330 1331 1332 1333 1334 1335 1336
}

static void ibmvnic_cleanup(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

	/* ensure that transmissions are stopped if called by do_reset */
1337
	if (test_bit(0, &adapter->resetting))
1338 1339 1340 1341 1342 1343 1344
		netif_tx_disable(netdev);
	else
		netif_tx_stop_all_queues(netdev);

	ibmvnic_napi_disable(adapter);
	ibmvnic_disable_irqs(adapter);

1345
	clean_rx_pools(adapter);
1346
	clean_tx_pools(adapter);
T
Thomas Falcon 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
}

static int __ibmvnic_close(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int rc = 0;

	adapter->state = VNIC_CLOSING;
	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
	if (rc)
		return rc;
1358
	adapter->state = VNIC_CLOSED;
T
Thomas Falcon 已提交
1359
	return 0;
1360 1361
}

1362 1363 1364 1365 1366
static int ibmvnic_close(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int rc;

S
Sukadev Bhattiprolu 已提交
1367 1368 1369 1370
	netdev_dbg(netdev, "[S:%d FOP:%d FRR:%d] Closing\n",
		   adapter->state, adapter->failover_pending,
		   adapter->force_reset_recovery);

1371 1372 1373 1374 1375 1376 1377 1378
	/* If device failover is pending, just set device state and return.
	 * Device operation will be handled by reset routine.
	 */
	if (adapter->failover_pending) {
		adapter->state = VNIC_CLOSED;
		return 0;
	}

1379
	rc = __ibmvnic_close(netdev);
1380
	ibmvnic_cleanup(netdev);
1381 1382 1383 1384

	return rc;
}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
/**
 * build_hdr_data - creates L2/L3/L4 header data buffer
 * @hdr_field - bitfield determining needed headers
 * @skb - socket buffer
 * @hdr_len - array of header lengths
 * @tot_len - total length of data
 *
 * Reads hdr_field to determine which headers are needed by firmware.
 * Builds a buffer containing these headers.  Saves individual header
 * lengths and total buffer length to be used to build descriptors.
 */
static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
			  int *hdr_len, u8 *hdr_data)
{
	int len = 0;
	u8 *hdr;

1402 1403 1404 1405
	if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
		hdr_len[0] = sizeof(struct vlan_ethhdr);
	else
		hdr_len[0] = sizeof(struct ethhdr);
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418

	if (skb->protocol == htons(ETH_P_IP)) {
		hdr_len[1] = ip_hdr(skb)->ihl * 4;
		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
			hdr_len[2] = tcp_hdrlen(skb);
		else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
			hdr_len[2] = sizeof(struct udphdr);
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		hdr_len[1] = sizeof(struct ipv6hdr);
		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
			hdr_len[2] = tcp_hdrlen(skb);
		else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
			hdr_len[2] = sizeof(struct udphdr);
1419 1420 1421
	} else if (skb->protocol == htons(ETH_P_ARP)) {
		hdr_len[1] = arp_hdr_len(skb->dev);
		hdr_len[2] = 0;
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
	}

	memset(hdr_data, 0, 120);
	if ((hdr_field >> 6) & 1) {
		hdr = skb_mac_header(skb);
		memcpy(hdr_data, hdr, hdr_len[0]);
		len += hdr_len[0];
	}

	if ((hdr_field >> 5) & 1) {
		hdr = skb_network_header(skb);
		memcpy(hdr_data + len, hdr, hdr_len[1]);
		len += hdr_len[1];
	}

	if ((hdr_field >> 4) & 1) {
		hdr = skb_transport_header(skb);
		memcpy(hdr_data + len, hdr, hdr_len[2]);
		len += hdr_len[2];
	}
	return len;
}

/**
 * create_hdr_descs - create header and header extension descriptors
 * @hdr_field - bitfield determining needed headers
 * @data - buffer containing header data
 * @len - length of data buffer
 * @hdr_len - array of individual header lengths
 * @scrq_arr - descriptor array
 *
 * Creates header and, if needed, header extension descriptors and
 * places them in a descriptor array, scrq_arr
 */

1457 1458
static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
			    union sub_crq *scrq_arr)
1459 1460 1461
{
	union sub_crq hdr_desc;
	int tmp_len = len;
1462
	int num_descs = 0;
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
	u8 *data, *cur;
	int tmp;

	while (tmp_len > 0) {
		cur = hdr_data + len - tmp_len;

		memset(&hdr_desc, 0, sizeof(hdr_desc));
		if (cur != hdr_data) {
			data = hdr_desc.hdr_ext.data;
			tmp = tmp_len > 29 ? 29 : tmp_len;
			hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
			hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
			hdr_desc.hdr_ext.len = tmp;
		} else {
			data = hdr_desc.hdr.data;
			tmp = tmp_len > 24 ? 24 : tmp_len;
			hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
			hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
			hdr_desc.hdr.len = tmp;
			hdr_desc.hdr.l2_len = (u8)hdr_len[0];
			hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
			hdr_desc.hdr.l4_len = (u8)hdr_len[2];
			hdr_desc.hdr.flag = hdr_field << 1;
		}
		memcpy(data, cur, tmp);
		tmp_len -= tmp;
		*scrq_arr = hdr_desc;
		scrq_arr++;
1491
		num_descs++;
1492
	}
1493 1494

	return num_descs;
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
}

/**
 * build_hdr_descs_arr - build a header descriptor array
 * @skb - socket buffer
 * @num_entries - number of descriptors to be sent
 * @subcrq - first TX descriptor
 * @hdr_field - bit field determining which headers will be sent
 *
 * This function will build a TX descriptor array with applicable
 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
 */

1508 1509
static void build_hdr_descs_arr(struct sk_buff *skb,
				union sub_crq *indir_arr,
1510 1511 1512
				int *num_entries, u8 hdr_field)
{
	int hdr_len[3] = {0, 0, 0};
1513
	u8 hdr_data[140] = {0};
1514
	int tot_len;
1515

1516 1517
	tot_len = build_hdr_data(hdr_field, skb, hdr_len,
				 hdr_data);
1518
	*num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1519
					 indir_arr + 1);
1520 1521
}

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
				    struct net_device *netdev)
{
	/* For some backing devices, mishandling of small packets
	 * can result in a loss of connection or TX stall. Device
	 * architects recommend that no packet should be smaller
	 * than the minimum MTU value provided to the driver, so
	 * pad any packets to that length
	 */
	if (skb->len < netdev->min_mtu)
		return skb_put_padto(skb, netdev->min_mtu);
1533 1534

	return 0;
1535 1536
}

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
					 struct ibmvnic_sub_crq_queue *tx_scrq)
{
	struct ibmvnic_ind_xmit_queue *ind_bufp;
	struct ibmvnic_tx_buff *tx_buff;
	struct ibmvnic_tx_pool *tx_pool;
	union sub_crq tx_scrq_entry;
	int queue_num;
	int entries;
	int index;
	int i;

	ind_bufp = &tx_scrq->ind_buf;
	entries = (u64)ind_bufp->index;
	queue_num = tx_scrq->pool_index;

	for (i = entries - 1; i >= 0; --i) {
		tx_scrq_entry = ind_bufp->indir_arr[i];
		if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
			continue;
		index = be32_to_cpu(tx_scrq_entry.v1.correlator);
		if (index & IBMVNIC_TSO_POOL_MASK) {
			tx_pool = &adapter->tso_pool[queue_num];
			index &= ~IBMVNIC_TSO_POOL_MASK;
		} else {
			tx_pool = &adapter->tx_pool[queue_num];
		}
		tx_pool->free_map[tx_pool->consumer_index] = index;
		tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
					  tx_pool->num_buffers - 1 :
					  tx_pool->consumer_index - 1;
		tx_buff = &tx_pool->tx_buff[index];
		adapter->netdev->stats.tx_packets--;
		adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
		adapter->tx_stats_buffers[queue_num].packets--;
		adapter->tx_stats_buffers[queue_num].bytes -=
						tx_buff->skb->len;
		dev_kfree_skb_any(tx_buff->skb);
		tx_buff->skb = NULL;
		adapter->netdev->stats.tx_dropped++;
	}
	ind_bufp->index = 0;
	if (atomic_sub_return(entries, &tx_scrq->used) <=
	    (adapter->req_tx_entries_per_subcrq / 2) &&
	    __netif_subqueue_stopped(adapter->netdev, queue_num)) {
		netif_wake_subqueue(adapter->netdev, queue_num);
		netdev_dbg(adapter->netdev, "Started queue %d\n",
			   queue_num);
	}
}

static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
				 struct ibmvnic_sub_crq_queue *tx_scrq)
{
	struct ibmvnic_ind_xmit_queue *ind_bufp;
	u64 dma_addr;
	u64 entries;
	u64 handle;
	int rc;

	ind_bufp = &tx_scrq->ind_buf;
	dma_addr = (u64)ind_bufp->indir_dma;
	entries = (u64)ind_bufp->index;
	handle = tx_scrq->handle;

	if (!entries)
		return 0;
	rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
	if (rc)
		ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
	else
		ind_bufp->index = 0;
	return 0;
}

1612
static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1613 1614 1615
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int queue_num = skb_get_queue_mapping(skb);
1616
	u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1617
	struct device *dev = &adapter->vdev->dev;
1618
	struct ibmvnic_ind_xmit_queue *ind_bufp;
1619
	struct ibmvnic_tx_buff *tx_buff = NULL;
1620
	struct ibmvnic_sub_crq_queue *tx_scrq;
1621 1622
	struct ibmvnic_tx_pool *tx_pool;
	unsigned int tx_send_failed = 0;
1623
	netdev_tx_t ret = NETDEV_TX_OK;
1624
	unsigned int tx_map_failed = 0;
1625
	union sub_crq indir_arr[16];
1626 1627 1628 1629 1630 1631 1632 1633
	unsigned int tx_dropped = 0;
	unsigned int tx_packets = 0;
	unsigned int tx_bytes = 0;
	dma_addr_t data_dma_addr;
	struct netdev_queue *txq;
	unsigned long lpar_rc;
	union sub_crq tx_crq;
	unsigned int offset;
1634
	int num_entries = 1;
1635 1636
	unsigned char *dst;
	int index = 0;
1637
	u8 proto = 0;
1638 1639 1640 1641

	tx_scrq = adapter->tx_scrq[queue_num];
	txq = netdev_get_tx_queue(netdev, queue_num);
	ind_bufp = &tx_scrq->ind_buf;
1642

1643
	if (test_bit(0, &adapter->resetting)) {
1644 1645 1646 1647
		if (!netif_subqueue_stopped(netdev, skb))
			netif_stop_subqueue(netdev, queue_num);
		dev_kfree_skb_any(skb);

1648 1649
		tx_send_failed++;
		tx_dropped++;
1650
		ret = NETDEV_TX_OK;
1651
		ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1652 1653 1654
		goto out;
	}

1655
	if (ibmvnic_xmit_workarounds(skb, netdev)) {
1656 1657 1658
		tx_dropped++;
		tx_send_failed++;
		ret = NETDEV_TX_OK;
1659
		ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1660 1661
		goto out;
	}
1662 1663 1664 1665
	if (skb_is_gso(skb))
		tx_pool = &adapter->tso_pool[queue_num];
	else
		tx_pool = &adapter->tx_pool[queue_num];
1666

1667
	index = tx_pool->free_map[tx_pool->consumer_index];
T
Thomas Falcon 已提交
1668

1669 1670 1671 1672 1673
	if (index == IBMVNIC_INVALID_MAP) {
		dev_kfree_skb_any(skb);
		tx_send_failed++;
		tx_dropped++;
		ret = NETDEV_TX_OK;
1674
		ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1675 1676 1677 1678 1679
		goto out;
	}

	tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;

1680 1681 1682 1683
	offset = index * tx_pool->buf_size;
	dst = tx_pool->long_term_buff.buff + offset;
	memset(dst, 0, tx_pool->buf_size);
	data_dma_addr = tx_pool->long_term_buff.addr + offset;
1684

1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
	if (skb_shinfo(skb)->nr_frags) {
		int cur, i;

		/* Copy the head */
		skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
		cur = skb_headlen(skb);

		/* Copy the frags */
		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];

			memcpy(dst + cur,
			       page_address(skb_frag_page(frag)) +
J
Jonathan Lemon 已提交
1698
			       skb_frag_off(frag), skb_frag_size(frag));
1699 1700 1701 1702 1703 1704
			cur += skb_frag_size(frag);
		}
	} else {
		skb_copy_from_linear_data(skb, dst, skb->len);
	}

1705
	tx_pool->consumer_index =
1706
	    (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

	tx_buff = &tx_pool->tx_buff[index];
	tx_buff->skb = skb;
	tx_buff->index = index;
	tx_buff->pool_index = queue_num;

	memset(&tx_crq, 0, sizeof(tx_crq));
	tx_crq.v1.first = IBMVNIC_CRQ_CMD;
	tx_crq.v1.type = IBMVNIC_TX_DESC;
	tx_crq.v1.n_crq_elem = 1;
	tx_crq.v1.n_sge = 1;
	tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1719

T
Thomas Falcon 已提交
1720
	if (skb_is_gso(skb))
1721 1722
		tx_crq.v1.correlator =
			cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
T
Thomas Falcon 已提交
1723
	else
1724 1725
		tx_crq.v1.correlator = cpu_to_be32(index);
	tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1726 1727 1728
	tx_crq.v1.sge_len = cpu_to_be32(skb->len);
	tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);

1729
	if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
1730 1731 1732 1733 1734
		tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
		tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
	}

	if (skb->protocol == htons(ETH_P_IP)) {
1735 1736 1737 1738 1739
		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
		proto = ip_hdr(skb)->protocol;
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
		proto = ipv6_hdr(skb)->nexthdr;
1740 1741
	}

1742 1743 1744 1745 1746
	if (proto == IPPROTO_TCP)
		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
	else if (proto == IPPROTO_UDP)
		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;

1747
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1748
		tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1749 1750
		hdrs += 2;
	}
T
Thomas Falcon 已提交
1751 1752 1753 1754 1755
	if (skb_is_gso(skb)) {
		tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
		tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
		hdrs += 2;
	}
1756 1757

	if ((*hdrs >> 7) & 1)
1758
		build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
1759

1760 1761 1762 1763 1764 1765 1766 1767
	tx_crq.v1.n_crq_elem = num_entries;
	tx_buff->num_entries = num_entries;
	/* flush buffer if current entry can not fit */
	if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
		if (lpar_rc != H_SUCCESS)
			goto tx_flush_err;
	}
1768

1769 1770
	indir_arr[0] = tx_crq;
	memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
1771 1772 1773 1774 1775 1776 1777 1778
	       num_entries * sizeof(struct ibmvnic_generic_scrq));
	ind_bufp->index += num_entries;
	if (__netdev_tx_sent_queue(txq, skb->len,
				   netdev_xmit_more() &&
				   ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
		if (lpar_rc != H_SUCCESS)
			goto tx_err;
1779
	}
1780

1781
	if (atomic_add_return(num_entries, &tx_scrq->used)
B
Brian King 已提交
1782
					>= adapter->req_tx_entries_per_subcrq) {
1783
		netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1784 1785 1786
		netif_stop_subqueue(netdev, queue_num);
	}

1787 1788 1789 1790
	tx_packets++;
	tx_bytes += skb->len;
	txq->trans_start = jiffies;
	ret = NETDEV_TX_OK;
1791
	goto out;
1792

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
tx_flush_err:
	dev_kfree_skb_any(skb);
	tx_buff->skb = NULL;
	tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
				  tx_pool->num_buffers - 1 :
				  tx_pool->consumer_index - 1;
	tx_dropped++;
tx_err:
	if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
		dev_err_ratelimited(dev, "tx: send failed\n");

	if (lpar_rc == H_CLOSED || adapter->failover_pending) {
		/* Disable TX and report carrier off if queue is closed
		 * or pending failover.
		 * Firmware guarantees that a signal will be sent to the
		 * driver, triggering a reset or some other action.
		 */
		netif_tx_stop_all_queues(netdev);
		netif_carrier_off(netdev);
	}
1813 1814 1815 1816 1817 1818
out:
	netdev->stats.tx_dropped += tx_dropped;
	netdev->stats.tx_bytes += tx_bytes;
	netdev->stats.tx_packets += tx_packets;
	adapter->tx_send_failed += tx_send_failed;
	adapter->tx_map_failed += tx_map_failed;
1819 1820 1821
	adapter->tx_stats_buffers[queue_num].packets += tx_packets;
	adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
	adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868

	return ret;
}

static void ibmvnic_set_multi(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	struct netdev_hw_addr *ha;
	union ibmvnic_crq crq;

	memset(&crq, 0, sizeof(crq));
	crq.request_capability.first = IBMVNIC_CRQ_CMD;
	crq.request_capability.cmd = REQUEST_CAPABILITY;

	if (netdev->flags & IFF_PROMISC) {
		if (!adapter->promisc_supported)
			return;
	} else {
		if (netdev->flags & IFF_ALLMULTI) {
			/* Accept all multicast */
			memset(&crq, 0, sizeof(crq));
			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
			crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
			ibmvnic_send_crq(adapter, &crq);
		} else if (netdev_mc_empty(netdev)) {
			/* Reject all multicast */
			memset(&crq, 0, sizeof(crq));
			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
			crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
			ibmvnic_send_crq(adapter, &crq);
		} else {
			/* Accept one or more multicast(s) */
			netdev_for_each_mc_addr(ha, netdev) {
				memset(&crq, 0, sizeof(crq));
				crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
				crq.multicast_ctrl.cmd = MULTICAST_CTRL;
				crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
				ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
						ha->addr);
				ibmvnic_send_crq(adapter, &crq);
			}
		}
	}
}

1869
static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
1870 1871 1872
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	union ibmvnic_crq crq;
1873
	int rc;
1874

1875 1876 1877 1878
	if (!is_valid_ether_addr(dev_addr)) {
		rc = -EADDRNOTAVAIL;
		goto err;
	}
1879 1880 1881 1882

	memset(&crq, 0, sizeof(crq));
	crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
	crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1883
	ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
1884

T
Thomas Falcon 已提交
1885 1886
	mutex_lock(&adapter->fw_lock);
	adapter->fw_done_rc = 0;
1887
	reinit_completion(&adapter->fw_done);
T
Thomas Falcon 已提交
1888

1889
	rc = ibmvnic_send_crq(adapter, &crq);
1890 1891
	if (rc) {
		rc = -EIO;
T
Thomas Falcon 已提交
1892
		mutex_unlock(&adapter->fw_lock);
1893 1894 1895
		goto err;
	}

1896
	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1897
	/* netdev->dev_addr is changed in handle_change_mac_rsp function */
1898
	if (rc || adapter->fw_done_rc) {
1899
		rc = -EIO;
T
Thomas Falcon 已提交
1900
		mutex_unlock(&adapter->fw_lock);
1901 1902
		goto err;
	}
T
Thomas Falcon 已提交
1903
	mutex_unlock(&adapter->fw_lock);
1904 1905 1906 1907
	return 0;
err:
	ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
	return rc;
1908 1909
}

1910 1911 1912 1913
static int ibmvnic_set_mac(struct net_device *netdev, void *p)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	struct sockaddr *addr = p;
1914
	int rc;
1915

1916
	rc = 0;
L
Lijun Pan 已提交
1917 1918 1919 1920 1921
	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	if (adapter->state != VNIC_PROBED) {
		ether_addr_copy(adapter->mac_addr, addr->sa_data);
1922
		rc = __ibmvnic_set_mac(netdev, addr->sa_data);
L
Lijun Pan 已提交
1923
	}
1924

1925
	return rc;
1926 1927
}

1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
/**
 * do_change_param_reset returns zero if we are able to keep processing reset
 * events, or non-zero if we hit a fatal error and must halt.
 */
static int do_change_param_reset(struct ibmvnic_adapter *adapter,
				 struct ibmvnic_rwi *rwi,
				 u32 reset_state)
{
	struct net_device *netdev = adapter->netdev;
	int i, rc;

	netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
		   rwi->reset_reason);

	netif_carrier_off(netdev);
	adapter->reset_reason = rwi->reset_reason;

	ibmvnic_cleanup(netdev);

	if (reset_state == VNIC_OPEN) {
		rc = __ibmvnic_close(netdev);
		if (rc)
1950
			goto out;
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
	}

	release_resources(adapter);
	release_sub_crqs(adapter, 1);
	release_crq_queue(adapter);

	adapter->state = VNIC_PROBED;

	rc = init_crq_queue(adapter);

	if (rc) {
		netdev_err(adapter->netdev,
			   "Couldn't initialize crq. rc=%d\n", rc);
		return rc;
	}

1967
	rc = ibmvnic_reset_init(adapter, true);
1968 1969 1970 1971
	if (rc) {
		rc = IBMVNIC_INIT_FAILED;
		goto out;
	}
1972 1973 1974 1975 1976

	/* If the adapter was in PROBE state prior to the reset,
	 * exit here.
	 */
	if (reset_state == VNIC_PROBED)
1977
		goto out;
1978 1979 1980

	rc = ibmvnic_login(netdev);
	if (rc) {
1981
		goto out;
1982 1983 1984 1985
	}

	rc = init_resources(adapter);
	if (rc)
1986
		goto out;
1987 1988 1989 1990 1991 1992 1993 1994 1995

	ibmvnic_disable_irqs(adapter);

	adapter->state = VNIC_CLOSED;

	if (reset_state == VNIC_CLOSED)
		return 0;

	rc = __ibmvnic_open(netdev);
1996 1997 1998 1999
	if (rc) {
		rc = IBMVNIC_OPEN_FAILED;
		goto out;
	}
2000 2001 2002 2003 2004 2005 2006 2007

	/* refresh device's multicast list */
	ibmvnic_set_multi(netdev);

	/* kick napi */
	for (i = 0; i < adapter->req_rx_queues; i++)
		napi_schedule(&adapter->napi[i]);

2008 2009 2010 2011
out:
	if (rc)
		adapter->state = reset_state;
	return rc;
2012 2013
}

2014 2015 2016 2017 2018 2019
/**
 * do_reset returns zero if we are able to keep processing reset events, or
 * non-zero if we hit a fatal error and must halt.
 */
static int do_reset(struct ibmvnic_adapter *adapter,
		    struct ibmvnic_rwi *rwi, u32 reset_state)
2020
{
2021
	u64 old_num_rx_queues, old_num_tx_queues;
2022
	u64 old_num_rx_slots, old_num_tx_slots;
2023 2024 2025
	struct net_device *netdev = adapter->netdev;
	int i, rc;

S
Sukadev Bhattiprolu 已提交
2026 2027 2028 2029
	netdev_dbg(adapter->netdev,
		   "[S:%d FOP:%d] Reset reason %d, reset_state %d\n",
		   adapter->state, adapter->failover_pending,
		   rwi->reset_reason, reset_state);
2030

2031
	rtnl_lock();
2032 2033 2034 2035 2036 2037 2038
	/*
	 * Now that we have the rtnl lock, clear any pending failover.
	 * This will ensure ibmvnic_open() has either completed or will
	 * block until failover is complete.
	 */
	if (rwi->reset_reason == VNIC_RESET_FAILOVER)
		adapter->failover_pending = false;
2039

2040 2041 2042
	netif_carrier_off(netdev);
	adapter->reset_reason = rwi->reset_reason;

2043 2044
	old_num_rx_queues = adapter->req_rx_queues;
	old_num_tx_queues = adapter->req_tx_queues;
2045 2046
	old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
	old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
2047

2048 2049
	ibmvnic_cleanup(netdev);

2050 2051
	if (reset_state == VNIC_OPEN &&
	    adapter->reset_reason != VNIC_RESET_MOBILITY &&
2052
	    adapter->reset_reason != VNIC_RESET_FAILOVER) {
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
		adapter->state = VNIC_CLOSING;

		/* Release the RTNL lock before link state change and
		 * re-acquire after the link state change to allow
		 * linkwatch_event to grab the RTNL lock and run during
		 * a reset.
		 */
		rtnl_unlock();
		rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
		rtnl_lock();
2063
		if (rc)
2064
			goto out;
2065

2066 2067 2068 2069 2070 2071
		if (adapter->state != VNIC_CLOSING) {
			rc = -1;
			goto out;
		}

		adapter->state = VNIC_CLOSED;
2072 2073
	}

J
John Allen 已提交
2074 2075 2076 2077 2078
	if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
		/* remove the closed state so when we call open it appears
		 * we are coming from the probed state.
		 */
		adapter->state = VNIC_PROBED;
2079

2080
		if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
2081 2082 2083 2084
			rc = ibmvnic_reenable_crq_queue(adapter);
			release_sub_crqs(adapter, 1);
		} else {
			rc = ibmvnic_reset_crq(adapter);
2085
			if (rc == H_CLOSED || rc == H_SUCCESS) {
2086
				rc = vio_enable_interrupts(adapter->vdev);
2087 2088 2089 2090 2091
				if (rc)
					netdev_err(adapter->netdev,
						   "Reset failed to enable interrupts. rc=%d\n",
						   rc);
			}
2092 2093 2094 2095
		}

		if (rc) {
			netdev_err(adapter->netdev,
2096
				   "Reset couldn't initialize crq. rc=%d\n", rc);
2097
			goto out;
2098 2099
		}

2100
		rc = ibmvnic_reset_init(adapter, true);
2101 2102 2103 2104
		if (rc) {
			rc = IBMVNIC_INIT_FAILED;
			goto out;
		}
2105

J
John Allen 已提交
2106 2107 2108
		/* If the adapter was in PROBE state prior to the reset,
		 * exit here.
		 */
2109 2110 2111 2112
		if (reset_state == VNIC_PROBED) {
			rc = 0;
			goto out;
		}
2113

J
John Allen 已提交
2114 2115
		rc = ibmvnic_login(netdev);
		if (rc) {
2116
			goto out;
J
John Allen 已提交
2117
		}
2118

2119 2120 2121 2122 2123
		if (adapter->req_rx_queues != old_num_rx_queues ||
		    adapter->req_tx_queues != old_num_tx_queues ||
		    adapter->req_rx_add_entries_per_subcrq !=
		    old_num_rx_slots ||
		    adapter->req_tx_entries_per_subcrq !=
2124 2125 2126 2127
		    old_num_tx_slots ||
		    !adapter->rx_pool ||
		    !adapter->tso_pool ||
		    !adapter->tx_pool) {
2128 2129
			release_rx_pools(adapter);
			release_tx_pools(adapter);
2130
			release_napi(adapter);
2131 2132 2133
			release_vpd_data(adapter);

			rc = init_resources(adapter);
2134
			if (rc)
2135
				goto out;
2136

2137 2138
		} else {
			rc = reset_tx_pools(adapter);
2139
			if (rc) {
2140 2141
				netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
						rc);
2142
				goto out;
2143
			}
2144

2145
			rc = reset_rx_pools(adapter);
2146
			if (rc) {
2147 2148
				netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
						rc);
2149
				goto out;
2150
			}
2151
		}
2152
		ibmvnic_disable_irqs(adapter);
J
John Allen 已提交
2153
	}
2154 2155
	adapter->state = VNIC_CLOSED;

2156 2157 2158 2159
	if (reset_state == VNIC_CLOSED) {
		rc = 0;
		goto out;
	}
2160

2161 2162
	rc = __ibmvnic_open(netdev);
	if (rc) {
2163 2164
		rc = IBMVNIC_OPEN_FAILED;
		goto out;
2165 2166
	}

2167 2168 2169
	/* refresh device's multicast list */
	ibmvnic_set_multi(netdev);

2170 2171 2172 2173
	/* kick napi */
	for (i = 0; i < adapter->req_rx_queues; i++)
		napi_schedule(&adapter->napi[i]);

2174
	if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
L
Lijun Pan 已提交
2175 2176
	    adapter->reset_reason == VNIC_RESET_MOBILITY)
		__netdev_notify_peers(netdev);
2177

2178 2179 2180
	rc = 0;

out:
2181 2182 2183
	/* restore the adapter state if reset failed */
	if (rc)
		adapter->state = reset_state;
2184 2185
	rtnl_unlock();

S
Sukadev Bhattiprolu 已提交
2186 2187
	netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Reset done, rc %d\n",
		   adapter->state, adapter->failover_pending, rc);
2188
	return rc;
2189 2190
}

2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
static int do_hard_reset(struct ibmvnic_adapter *adapter,
			 struct ibmvnic_rwi *rwi, u32 reset_state)
{
	struct net_device *netdev = adapter->netdev;
	int rc;

	netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
		   rwi->reset_reason);

	netif_carrier_off(netdev);
	adapter->reset_reason = rwi->reset_reason;

	ibmvnic_cleanup(netdev);
	release_resources(adapter);
	release_sub_crqs(adapter, 0);
	release_crq_queue(adapter);

	/* remove the closed state so when we call open it appears
	 * we are coming from the probed state.
	 */
	adapter->state = VNIC_PROBED;

2213
	reinit_completion(&adapter->init_done);
2214 2215 2216 2217
	rc = init_crq_queue(adapter);
	if (rc) {
		netdev_err(adapter->netdev,
			   "Couldn't initialize crq. rc=%d\n", rc);
2218
		goto out;
2219 2220
	}

2221
	rc = ibmvnic_reset_init(adapter, false);
2222
	if (rc)
2223
		goto out;
2224 2225 2226 2227 2228

	/* If the adapter was in PROBE state prior to the reset,
	 * exit here.
	 */
	if (reset_state == VNIC_PROBED)
2229
		goto out;
2230 2231

	rc = ibmvnic_login(netdev);
2232 2233
	if (rc)
		goto out;
2234 2235

	rc = init_resources(adapter);
2236
	if (rc)
2237
		goto out;
2238 2239 2240 2241 2242

	ibmvnic_disable_irqs(adapter);
	adapter->state = VNIC_CLOSED;

	if (reset_state == VNIC_CLOSED)
2243
		goto out;
2244 2245

	rc = __ibmvnic_open(netdev);
2246 2247 2248 2249
	if (rc) {
		rc = IBMVNIC_OPEN_FAILED;
		goto out;
	}
2250

L
Lijun Pan 已提交
2251
	__netdev_notify_peers(netdev);
2252 2253 2254 2255
out:
	/* restore adapter state if reset failed */
	if (rc)
		adapter->state = reset_state;
S
Sukadev Bhattiprolu 已提交
2256 2257
	netdev_dbg(adapter->netdev, "[S:%d FOP:%d] Hard reset done, rc %d\n",
		   adapter->state, adapter->failover_pending, rc);
2258
	return rc;
2259 2260
}

2261 2262 2263
static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_rwi *rwi;
2264
	unsigned long flags;
2265

2266
	spin_lock_irqsave(&adapter->rwi_lock, flags);
2267 2268 2269 2270 2271 2272 2273 2274 2275

	if (!list_empty(&adapter->rwi_list)) {
		rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
				       list);
		list_del(&rwi->list);
	} else {
		rwi = NULL;
	}

2276
	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2277 2278 2279 2280 2281 2282 2283
	return rwi;
}

static void __ibmvnic_reset(struct work_struct *work)
{
	struct ibmvnic_rwi *rwi;
	struct ibmvnic_adapter *adapter;
2284 2285
	bool saved_state = false;
	unsigned long flags;
2286
	u32 reset_state;
2287
	int rc = 0;
2288 2289 2290

	adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);

2291 2292 2293 2294 2295 2296
	if (test_and_set_bit_lock(0, &adapter->resetting)) {
		schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
				      IBMVNIC_RESET_DELAY);
		return;
	}

2297 2298
	rwi = get_next_rwi(adapter);
	while (rwi) {
2299 2300
		spin_lock_irqsave(&adapter->state_lock, flags);

2301
		if (adapter->state == VNIC_REMOVING ||
2302
		    adapter->state == VNIC_REMOVED) {
2303
			spin_unlock_irqrestore(&adapter->state_lock, flags);
2304 2305 2306 2307
			kfree(rwi);
			rc = EBUSY;
			break;
		}
2308

2309 2310 2311 2312 2313 2314
		if (!saved_state) {
			reset_state = adapter->state;
			saved_state = true;
		}
		spin_unlock_irqrestore(&adapter->state_lock, flags);

2315 2316 2317 2318
		if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
			/* CHANGE_PARAM requestor holds rtnl_lock */
			rc = do_change_param_reset(adapter, rwi, reset_state);
		} else if (adapter->force_reset_recovery) {
2319 2320 2321 2322 2323 2324 2325
			/*
			 * Since we are doing a hard reset now, clear the
			 * failover_pending flag so we don't ignore any
			 * future MOBILITY or other resets.
			 */
			adapter->failover_pending = false;

2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
			/* Transport event occurred during previous reset */
			if (adapter->wait_for_reset) {
				/* Previous was CHANGE_PARAM; caller locked */
				adapter->force_reset_recovery = false;
				rc = do_hard_reset(adapter, rwi, reset_state);
			} else {
				rtnl_lock();
				adapter->force_reset_recovery = false;
				rc = do_hard_reset(adapter, rwi, reset_state);
				rtnl_unlock();
			}
2337 2338 2339 2340 2341 2342 2343 2344
			if (rc) {
				/* give backing device time to settle down */
				netdev_dbg(adapter->netdev,
					   "[S:%d] Hard reset failed, waiting 60 secs\n",
					   adapter->state);
				set_current_state(TASK_UNINTERRUPTIBLE);
				schedule_timeout(60 * HZ);
			}
2345
		} else {
2346 2347
			rc = do_reset(adapter, rwi, reset_state);
		}
2348
		kfree(rwi);
2349
		adapter->last_reset_time = jiffies;
2350

2351 2352
		if (rc)
			netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
2353 2354

		rwi = get_next_rwi(adapter);
2355 2356 2357 2358

		if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
			    rwi->reset_reason == VNIC_RESET_MOBILITY))
			adapter->force_reset_recovery = true;
2359 2360
	}

2361 2362 2363 2364 2365
	if (adapter->wait_for_reset) {
		adapter->reset_done_rc = rc;
		complete(&adapter->reset_done);
	}

2366
	clear_bit_unlock(0, &adapter->resetting);
S
Sukadev Bhattiprolu 已提交
2367 2368 2369 2370 2371

	netdev_dbg(adapter->netdev,
		   "[S:%d FRR:%d WFR:%d] Done processing resets\n",
		   adapter->state, adapter->force_reset_recovery,
		   adapter->wait_for_reset);
2372 2373 2374 2375 2376 2377 2378 2379 2380
}

static void __ibmvnic_delayed_reset(struct work_struct *work)
{
	struct ibmvnic_adapter *adapter;

	adapter = container_of(work, struct ibmvnic_adapter,
			       ibmvnic_delayed_reset.work);
	__ibmvnic_reset(&adapter->ibmvnic_reset);
2381 2382
}

2383 2384
static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
			 enum ibmvnic_reset_reason reason)
2385
{
2386
	struct list_head *entry, *tmp_entry;
2387 2388
	struct ibmvnic_rwi *rwi, *tmp;
	struct net_device *netdev = adapter->netdev;
2389
	unsigned long flags;
2390
	int ret;
2391

2392 2393 2394 2395 2396 2397
	/*
	 * If failover is pending don't schedule any other reset.
	 * Instead let the failover complete. If there is already a
	 * a failover reset scheduled, we will detect and drop the
	 * duplicate reset when walking the ->rwi_list below.
	 */
2398
	if (adapter->state == VNIC_REMOVING ||
2399
	    adapter->state == VNIC_REMOVED ||
2400
	    (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
2401
		ret = EBUSY;
2402
		netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
2403
		goto err;
2404 2405
	}

2406 2407
	if (adapter->state == VNIC_PROBING) {
		netdev_warn(netdev, "Adapter reset during probe\n");
2408 2409
		ret = adapter->init_done_rc = EAGAIN;
		goto err;
2410 2411
	}

2412
	spin_lock_irqsave(&adapter->rwi_lock, flags);
2413 2414 2415 2416

	list_for_each(entry, &adapter->rwi_list) {
		tmp = list_entry(entry, struct ibmvnic_rwi, list);
		if (tmp->reset_reason == reason) {
S
Sukadev Bhattiprolu 已提交
2417 2418
			netdev_dbg(netdev, "Skipping matching reset, reason=%d\n",
				   reason);
2419
			spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2420 2421
			ret = EBUSY;
			goto err;
2422 2423 2424
		}
	}

2425
	rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
2426
	if (!rwi) {
2427
		spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2428
		ibmvnic_close(netdev);
2429 2430
		ret = ENOMEM;
		goto err;
2431
	}
2432 2433 2434 2435 2436 2437 2438
	/* if we just received a transport event,
	 * flush reset queue and process this reset
	 */
	if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
		list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
			list_del(entry);
	}
2439 2440
	rwi->reset_reason = reason;
	list_add_tail(&rwi->list, &adapter->rwi_list);
2441
	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2442
	netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
2443
	schedule_work(&adapter->ibmvnic_reset);
2444 2445 2446 2447

	return 0;
err:
	return -ret;
2448 2449
}

2450
static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
2451 2452 2453
{
	struct ibmvnic_adapter *adapter = netdev_priv(dev);

2454 2455 2456 2457 2458
	if (test_bit(0, &adapter->resetting)) {
		netdev_err(adapter->netdev,
			   "Adapter is resetting, skip timeout reset\n");
		return;
	}
2459 2460 2461 2462 2463 2464 2465
	/* No queuing up reset until at least 5 seconds (default watchdog val)
	 * after last reset
	 */
	if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
		netdev_dbg(dev, "Not yet time to tx timeout.\n");
		return;
	}
2466
	ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
}

static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
				  struct ibmvnic_rx_buff *rx_buff)
{
	struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];

	rx_buff->skb = NULL;

	pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
	pool->next_alloc = (pool->next_alloc + 1) % pool->size;

	atomic_dec(&pool->available);
}

static int ibmvnic_poll(struct napi_struct *napi, int budget)
{
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
	struct ibmvnic_sub_crq_queue *rx_scrq;
	struct ibmvnic_adapter *adapter;
	struct net_device *netdev;
	int frames_processed;
	int scrq_num;

	netdev = napi->dev;
	adapter = netdev_priv(netdev);
	scrq_num = (int)(napi - adapter->napi);
	frames_processed = 0;
	rx_scrq = adapter->rx_scrq[scrq_num];
2495

2496 2497 2498 2499 2500 2501 2502 2503 2504
restart_poll:
	while (frames_processed < budget) {
		struct sk_buff *skb;
		struct ibmvnic_rx_buff *rx_buff;
		union sub_crq *next;
		u32 length;
		u16 offset;
		u8 flags = 0;

2505
		if (unlikely(test_bit(0, &adapter->resetting) &&
2506
			     adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2507
			enable_scrq_irq(adapter, rx_scrq);
2508 2509 2510 2511
			napi_complete_done(napi, frames_processed);
			return frames_processed;
		}

2512
		if (!pending_scrq(adapter, rx_scrq))
2513
			break;
2514 2515 2516 2517 2518 2519
		/* The queue entry at the current index is peeked at above
		 * to determine that there is a valid descriptor awaiting
		 * processing. We want to be sure that the current slot
		 * holds a valid descriptor before reading its contents.
		 */
		dma_rmb();
2520
		next = ibmvnic_next_scrq(adapter, rx_scrq);
2521 2522 2523 2524 2525
		rx_buff =
		    (struct ibmvnic_rx_buff *)be64_to_cpu(next->
							  rx_comp.correlator);
		/* do error checking */
		if (next->rx_comp.rc) {
2526 2527
			netdev_dbg(netdev, "rx buffer returned with rc %x\n",
				   be16_to_cpu(next->rx_comp.rc));
2528 2529
			/* free the entry */
			next->rx_comp.first = 0;
2530
			dev_kfree_skb_any(rx_buff->skb);
2531
			remove_buff_from_pool(adapter, rx_buff);
2532
			continue;
2533 2534 2535 2536 2537
		} else if (!rx_buff->skb) {
			/* free the entry */
			next->rx_comp.first = 0;
			remove_buff_from_pool(adapter, rx_buff);
			continue;
2538 2539 2540 2541 2542 2543 2544 2545
		}

		length = be32_to_cpu(next->rx_comp.len);
		offset = be16_to_cpu(next->rx_comp.off_frame_data);
		flags = next->rx_comp.flags;
		skb = rx_buff->skb;
		skb_copy_to_linear_data(skb, rx_buff->data + offset,
					length);
2546 2547 2548 2549 2550 2551 2552 2553 2554

		/* VLAN Header has been stripped by the system firmware and
		 * needs to be inserted by the driver
		 */
		if (adapter->rx_vlan_header_insertion &&
		    (flags & IBMVNIC_VLAN_STRIPPED))
			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
					       ntohs(next->rx_comp.vlan_tci));

2555 2556 2557 2558 2559 2560
		/* free the entry */
		next->rx_comp.first = 0;
		remove_buff_from_pool(adapter, rx_buff);

		skb_put(skb, length);
		skb->protocol = eth_type_trans(skb, netdev);
2561
		skb_record_rx_queue(skb, scrq_num);
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571

		if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
		    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		}

		length = skb->len;
		napi_gro_receive(napi, skb); /* send it up */
		netdev->stats.rx_packets++;
		netdev->stats.rx_bytes += length;
2572 2573
		adapter->rx_stats_buffers[scrq_num].packets++;
		adapter->rx_stats_buffers[scrq_num].bytes += length;
2574 2575
		frames_processed++;
	}
2576

2577 2578 2579 2580
	if (adapter->state != VNIC_CLOSING &&
	    ((atomic_read(&adapter->rx_pool[scrq_num].available) <
	      adapter->req_rx_add_entries_per_subcrq / 2) ||
	      frames_processed < budget))
2581
		replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2582
	if (frames_processed < budget) {
2583 2584 2585 2586 2587 2588 2589 2590 2591
		if (napi_complete_done(napi, frames_processed)) {
			enable_scrq_irq(adapter, rx_scrq);
			if (pending_scrq(adapter, rx_scrq)) {
				rmb();
				if (napi_reschedule(napi)) {
					disable_scrq_irq(adapter, rx_scrq);
					goto restart_poll;
				}
			}
2592 2593 2594 2595 2596
		}
	}
	return frames_processed;
}

2597 2598
static int wait_for_reset(struct ibmvnic_adapter *adapter)
{
2599 2600
	int rc, ret;

2601 2602 2603 2604 2605 2606
	adapter->fallback.mtu = adapter->req_mtu;
	adapter->fallback.rx_queues = adapter->req_rx_queues;
	adapter->fallback.tx_queues = adapter->req_tx_queues;
	adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
	adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;

2607
	reinit_completion(&adapter->reset_done);
2608
	adapter->wait_for_reset = true;
2609
	rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619

	if (rc) {
		ret = rc;
		goto out;
	}
	rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
	if (rc) {
		ret = -ENODEV;
		goto out;
	}
2620

2621
	ret = 0;
2622
	if (adapter->reset_done_rc) {
2623
		ret = -EIO;
2624 2625 2626 2627 2628 2629
		adapter->desired.mtu = adapter->fallback.mtu;
		adapter->desired.rx_queues = adapter->fallback.rx_queues;
		adapter->desired.tx_queues = adapter->fallback.tx_queues;
		adapter->desired.rx_entries = adapter->fallback.rx_entries;
		adapter->desired.tx_entries = adapter->fallback.tx_entries;

2630
		reinit_completion(&adapter->reset_done);
2631 2632
		adapter->wait_for_reset = true;
		rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
		if (rc) {
			ret = rc;
			goto out;
		}
		rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
						 60000);
		if (rc) {
			ret = -ENODEV;
			goto out;
		}
2643
	}
2644
out:
2645 2646
	adapter->wait_for_reset = false;

2647
	return ret;
2648 2649
}

2650 2651
static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
{
2652 2653 2654 2655 2656
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

	adapter->desired.mtu = new_mtu + ETH_HLEN;

	return wait_for_reset(adapter);
2657 2658
}

2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
						struct net_device *dev,
						netdev_features_t features)
{
	/* Some backing hardware adapters can not
	 * handle packets with a MSS less than 224
	 * or with only one segment.
	 */
	if (skb_is_gso(skb)) {
		if (skb_shinfo(skb)->gso_size < 224 ||
		    skb_shinfo(skb)->gso_segs == 1)
			features &= ~NETIF_F_GSO_MASK;
	}

	return features;
}

2676 2677 2678 2679 2680 2681 2682 2683
static const struct net_device_ops ibmvnic_netdev_ops = {
	.ndo_open		= ibmvnic_open,
	.ndo_stop		= ibmvnic_close,
	.ndo_start_xmit		= ibmvnic_xmit,
	.ndo_set_rx_mode	= ibmvnic_set_multi,
	.ndo_set_mac_address	= ibmvnic_set_mac,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_tx_timeout		= ibmvnic_tx_timeout,
2684
	.ndo_change_mtu		= ibmvnic_change_mtu,
2685
	.ndo_features_check     = ibmvnic_features_check,
2686 2687 2688 2689
};

/* ethtool functions */

2690 2691
static int ibmvnic_get_link_ksettings(struct net_device *netdev,
				      struct ethtool_link_ksettings *cmd)
2692
{
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	int rc;

	rc = send_query_phys_parms(adapter);
	if (rc) {
		adapter->speed = SPEED_UNKNOWN;
		adapter->duplex = DUPLEX_UNKNOWN;
	}
	cmd->base.speed = adapter->speed;
	cmd->base.duplex = adapter->duplex;
2703 2704 2705 2706
	cmd->base.port = PORT_FIBRE;
	cmd->base.phy_address = 0;
	cmd->base.autoneg = AUTONEG_ENABLE;

2707 2708 2709
	return 0;
}

2710
static void ibmvnic_get_drvinfo(struct net_device *netdev,
2711 2712
				struct ethtool_drvinfo *info)
{
2713 2714
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

2715 2716
	strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
	strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2717 2718
	strlcpy(info->fw_version, adapter->fw_version,
		sizeof(info->fw_version));
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
}

static u32 ibmvnic_get_msglevel(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

	return adapter->msg_enable;
}

static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

	adapter->msg_enable = data;
}

static u32 ibmvnic_get_link(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

	/* Don't need to send a query because we request a logical link up at
	 * init and then we wait for link state indications
	 */
	return adapter->logical_link_state;
}

static void ibmvnic_get_ringparam(struct net_device *netdev,
				  struct ethtool_ringparam *ring)
{
J
John Allen 已提交
2748 2749
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

2750 2751 2752 2753 2754 2755 2756
	if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
		ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
		ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
	} else {
		ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
		ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
	}
2757 2758
	ring->rx_mini_max_pending = 0;
	ring->rx_jumbo_max_pending = 0;
J
John Allen 已提交
2759 2760
	ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
	ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2761 2762 2763 2764
	ring->rx_mini_pending = 0;
	ring->rx_jumbo_pending = 0;
}

2765 2766 2767 2768
static int ibmvnic_set_ringparam(struct net_device *netdev,
				 struct ethtool_ringparam *ring)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2769
	int ret;
2770

2771
	ret = 0;
2772 2773 2774
	adapter->desired.rx_entries = ring->rx_pending;
	adapter->desired.tx_entries = ring->tx_pending;

2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
	ret = wait_for_reset(adapter);

	if (!ret &&
	    (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
	     adapter->req_tx_entries_per_subcrq != ring->tx_pending))
		netdev_info(netdev,
			    "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
			    ring->rx_pending, ring->tx_pending,
			    adapter->req_rx_add_entries_per_subcrq,
			    adapter->req_tx_entries_per_subcrq);
	return ret;
2786 2787
}

J
John Allen 已提交
2788 2789 2790 2791 2792
static void ibmvnic_get_channels(struct net_device *netdev,
				 struct ethtool_channels *channels)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

2793 2794 2795 2796 2797 2798 2799 2800
	if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
		channels->max_rx = adapter->max_rx_queues;
		channels->max_tx = adapter->max_tx_queues;
	} else {
		channels->max_rx = IBMVNIC_MAX_QUEUES;
		channels->max_tx = IBMVNIC_MAX_QUEUES;
	}

J
John Allen 已提交
2801 2802 2803 2804 2805 2806 2807 2808
	channels->max_other = 0;
	channels->max_combined = 0;
	channels->rx_count = adapter->req_rx_queues;
	channels->tx_count = adapter->req_tx_queues;
	channels->other_count = 0;
	channels->combined_count = 0;
}

2809 2810 2811 2812
static int ibmvnic_set_channels(struct net_device *netdev,
				struct ethtool_channels *channels)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2813
	int ret;
2814

2815
	ret = 0;
2816 2817 2818
	adapter->desired.rx_queues = channels->rx_count;
	adapter->desired.tx_queues = channels->tx_count;

2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
	ret = wait_for_reset(adapter);

	if (!ret &&
	    (adapter->req_rx_queues != channels->rx_count ||
	     adapter->req_tx_queues != channels->tx_count))
		netdev_info(netdev,
			    "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
			    channels->rx_count, channels->tx_count,
			    adapter->req_rx_queues, adapter->req_tx_queues);
	return ret;

2830 2831
}

2832 2833
static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
{
2834
	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2835 2836
	int i;

2837 2838 2839 2840 2841
	switch (stringset) {
	case ETH_SS_STATS:
		for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
				i++, data += ETH_GSTRING_LEN)
			memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2842

2843 2844 2845
		for (i = 0; i < adapter->req_tx_queues; i++) {
			snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
			data += ETH_GSTRING_LEN;
2846

2847 2848
			snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
			data += ETH_GSTRING_LEN;
2849

2850 2851 2852 2853
			snprintf(data, ETH_GSTRING_LEN,
				 "tx%d_dropped_packets", i);
			data += ETH_GSTRING_LEN;
		}
2854

2855 2856 2857
		for (i = 0; i < adapter->req_rx_queues; i++) {
			snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
			data += ETH_GSTRING_LEN;
2858

2859 2860
			snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
			data += ETH_GSTRING_LEN;
2861

2862 2863 2864 2865
			snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
			data += ETH_GSTRING_LEN;
		}
		break;
2866

2867 2868 2869 2870 2871 2872 2873
	case ETH_SS_PRIV_FLAGS:
		for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
			strcpy(data + i * ETH_GSTRING_LEN,
			       ibmvnic_priv_flags[i]);
		break;
	default:
		return;
2874
	}
2875 2876 2877 2878
}

static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
{
2879 2880
	struct ibmvnic_adapter *adapter = netdev_priv(dev);

2881 2882
	switch (sset) {
	case ETH_SS_STATS:
2883 2884 2885
		return ARRAY_SIZE(ibmvnic_stats) +
		       adapter->req_tx_queues * NUM_TX_STATS +
		       adapter->req_rx_queues * NUM_RX_STATS;
2886 2887
	case ETH_SS_PRIV_FLAGS:
		return ARRAY_SIZE(ibmvnic_priv_flags);
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
	default:
		return -EOPNOTSUPP;
	}
}

static void ibmvnic_get_ethtool_stats(struct net_device *dev,
				      struct ethtool_stats *stats, u64 *data)
{
	struct ibmvnic_adapter *adapter = netdev_priv(dev);
	union ibmvnic_crq crq;
2898
	int i, j;
2899
	int rc;
2900 2901 2902 2903 2904 2905 2906 2907 2908

	memset(&crq, 0, sizeof(crq));
	crq.request_statistics.first = IBMVNIC_CRQ_CMD;
	crq.request_statistics.cmd = REQUEST_STATISTICS;
	crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
	crq.request_statistics.len =
	    cpu_to_be32(sizeof(struct ibmvnic_statistics));

	/* Wait for data to be written */
2909
	reinit_completion(&adapter->stats_done);
2910 2911 2912
	rc = ibmvnic_send_crq(adapter, &crq);
	if (rc)
		return;
2913 2914 2915
	rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
	if (rc)
		return;
2916 2917

	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2918 2919
		data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
						ibmvnic_stats[i].offset));
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937

	for (j = 0; j < adapter->req_tx_queues; j++) {
		data[i] = adapter->tx_stats_buffers[j].packets;
		i++;
		data[i] = adapter->tx_stats_buffers[j].bytes;
		i++;
		data[i] = adapter->tx_stats_buffers[j].dropped_packets;
		i++;
	}

	for (j = 0; j < adapter->req_rx_queues; j++) {
		data[i] = adapter->rx_stats_buffers[j].packets;
		i++;
		data[i] = adapter->rx_stats_buffers[j].bytes;
		i++;
		data[i] = adapter->rx_stats_buffers[j].interrupts;
		i++;
	}
2938 2939
}

2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

	return adapter->priv_flags;
}

static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
{
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);

	if (which_maxes)
		adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
	else
		adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;

	return 0;
}
2959 2960 2961 2962 2963 2964
static const struct ethtool_ops ibmvnic_ethtool_ops = {
	.get_drvinfo		= ibmvnic_get_drvinfo,
	.get_msglevel		= ibmvnic_get_msglevel,
	.set_msglevel		= ibmvnic_set_msglevel,
	.get_link		= ibmvnic_get_link,
	.get_ringparam		= ibmvnic_get_ringparam,
2965
	.set_ringparam		= ibmvnic_set_ringparam,
J
John Allen 已提交
2966
	.get_channels		= ibmvnic_get_channels,
2967
	.set_channels		= ibmvnic_set_channels,
2968 2969 2970
	.get_strings            = ibmvnic_get_strings,
	.get_sset_count         = ibmvnic_get_sset_count,
	.get_ethtool_stats	= ibmvnic_get_ethtool_stats,
2971
	.get_link_ksettings	= ibmvnic_get_link_ksettings,
2972 2973
	.get_priv_flags		= ibmvnic_get_priv_flags,
	.set_priv_flags		= ibmvnic_set_priv_flags,
2974 2975 2976 2977
};

/* Routines for managing CRQs/sCRQs  */

2978 2979 2980 2981 2982
static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
				   struct ibmvnic_sub_crq_queue *scrq)
{
	int rc;

D
Dany Madden 已提交
2983
	if (!scrq) {
2984
		netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
D
Dany Madden 已提交
2985 2986 2987
		return -EINVAL;
	}

2988 2989 2990 2991 2992 2993
	if (scrq->irq) {
		free_irq(scrq->irq, scrq);
		irq_dispose_mapping(scrq->irq);
		scrq->irq = 0;
	}

D
Dany Madden 已提交
2994 2995 2996 2997
	if (scrq->msgs) {
		memset(scrq->msgs, 0, 4 * PAGE_SIZE);
		atomic_set(&scrq->used, 0);
		scrq->cur = 0;
2998
		scrq->ind_buf.index = 0;
D
Dany Madden 已提交
2999 3000 3001 3002
	} else {
		netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
		return -EINVAL;
	}
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012

	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
	return rc;
}

static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
{
	int i, rc;

3013 3014 3015
	if (!adapter->tx_scrq || !adapter->rx_scrq)
		return -EINVAL;

3016
	for (i = 0; i < adapter->req_tx_queues; i++) {
3017
		netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
3018 3019 3020 3021 3022 3023
		rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
		if (rc)
			return rc;
	}

	for (i = 0; i < adapter->req_rx_queues; i++) {
3024
		netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
3025 3026 3027 3028 3029 3030 3031 3032
		rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
		if (rc)
			return rc;
	}

	return rc;
}

3033
static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
3034 3035
				  struct ibmvnic_sub_crq_queue *scrq,
				  bool do_h_free)
3036 3037 3038 3039 3040 3041
{
	struct device *dev = &adapter->vdev->dev;
	long rc;

	netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");

3042 3043 3044 3045 3046 3047 3048
	if (do_h_free) {
		/* Close the sub-crqs */
		do {
			rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
						adapter->vdev->unit_address,
						scrq->crq_num);
		} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3049

3050 3051 3052 3053 3054
		if (rc) {
			netdev_err(adapter->netdev,
				   "Failed to release sub-CRQ %16lx, rc = %ld\n",
				   scrq->crq_num, rc);
		}
3055 3056
	}

3057 3058 3059 3060 3061
	dma_free_coherent(dev,
			  IBMVNIC_IND_ARR_SZ,
			  scrq->ind_buf.indir_arr,
			  scrq->ind_buf.indir_dma);

3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074
	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
			 DMA_BIDIRECTIONAL);
	free_pages((unsigned long)scrq->msgs, 2);
	kfree(scrq);
}

static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
							*adapter)
{
	struct device *dev = &adapter->vdev->dev;
	struct ibmvnic_sub_crq_queue *scrq;
	int rc;

3075
	scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
3076 3077 3078
	if (!scrq)
		return NULL;

3079
	scrq->msgs =
3080
		(union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107
	if (!scrq->msgs) {
		dev_warn(dev, "Couldn't allocate crq queue messages page\n");
		goto zero_page_failed;
	}

	scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
					 DMA_BIDIRECTIONAL);
	if (dma_mapping_error(dev, scrq->msg_token)) {
		dev_warn(dev, "Couldn't map crq queue messages page\n");
		goto map_failed;
	}

	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);

	if (rc == H_RESOURCE)
		rc = ibmvnic_reset_crq(adapter);

	if (rc == H_CLOSED) {
		dev_warn(dev, "Partner adapter not ready, waiting.\n");
	} else if (rc) {
		dev_warn(dev, "Error %d registering sub-crq\n", rc);
		goto reg_failed;
	}

	scrq->adapter = adapter;
	scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118
	scrq->ind_buf.index = 0;

	scrq->ind_buf.indir_arr =
		dma_alloc_coherent(dev,
				   IBMVNIC_IND_ARR_SZ,
				   &scrq->ind_buf.indir_dma,
				   GFP_KERNEL);

	if (!scrq->ind_buf.indir_arr)
		goto indir_failed;

3119 3120 3121 3122 3123 3124 3125 3126
	spin_lock_init(&scrq->lock);

	netdev_dbg(adapter->netdev,
		   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
		   scrq->crq_num, scrq->hw_irq, scrq->irq);

	return scrq;

3127 3128 3129 3130 3131 3132
indir_failed:
	do {
		rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
					adapter->vdev->unit_address,
					scrq->crq_num);
	} while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
reg_failed:
	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
			 DMA_BIDIRECTIONAL);
map_failed:
	free_pages((unsigned long)scrq->msgs, 2);
zero_page_failed:
	kfree(scrq);

	return NULL;
}

3144
static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
3145 3146 3147 3148
{
	int i;

	if (adapter->tx_scrq) {
3149
		for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
3150 3151 3152
			if (!adapter->tx_scrq[i])
				continue;

3153 3154
			netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
				   i);
3155
			if (adapter->tx_scrq[i]->irq) {
3156 3157
				free_irq(adapter->tx_scrq[i]->irq,
					 adapter->tx_scrq[i]);
T
Thomas Falcon 已提交
3158
				irq_dispose_mapping(adapter->tx_scrq[i]->irq);
3159
				adapter->tx_scrq[i]->irq = 0;
3160
			}
3161

3162 3163
			release_sub_crq_queue(adapter, adapter->tx_scrq[i],
					      do_h_free);
3164 3165
		}

3166
		kfree(adapter->tx_scrq);
3167
		adapter->tx_scrq = NULL;
3168
		adapter->num_active_tx_scrqs = 0;
3169 3170 3171
	}

	if (adapter->rx_scrq) {
3172
		for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
3173 3174 3175
			if (!adapter->rx_scrq[i])
				continue;

3176 3177
			netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
				   i);
3178
			if (adapter->rx_scrq[i]->irq) {
3179 3180
				free_irq(adapter->rx_scrq[i]->irq,
					 adapter->rx_scrq[i]);
T
Thomas Falcon 已提交
3181
				irq_dispose_mapping(adapter->rx_scrq[i]->irq);
3182
				adapter->rx_scrq[i]->irq = 0;
3183 3184
			}

3185 3186
			release_sub_crq_queue(adapter, adapter->rx_scrq[i],
					      do_h_free);
3187
		}
3188

3189
		kfree(adapter->rx_scrq);
3190
		adapter->rx_scrq = NULL;
3191
		adapter->num_active_rx_scrqs = 0;
3192 3193 3194
	}
}

3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
			    struct ibmvnic_sub_crq_queue *scrq)
{
	struct device *dev = &adapter->vdev->dev;
	unsigned long rc;

	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
				H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
	if (rc)
		dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
			scrq->hw_irq, rc);
	return rc;
}

static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
			   struct ibmvnic_sub_crq_queue *scrq)
{
	struct device *dev = &adapter->vdev->dev;
	unsigned long rc;

	if (scrq->hw_irq > 0x100000000ULL) {
		dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
		return 1;
	}

3220
	if (test_bit(0, &adapter->resetting) &&
3221
	    adapter->reset_reason == VNIC_RESET_MOBILITY) {
3222
		u64 val = (0xff000000) | scrq->hw_irq;
3223

3224
		rc = plpar_hcall_norets(H_EOI, val);
3225 3226 3227 3228
		/* H_EOI would fail with rc = H_FUNCTION when running
		 * in XIVE mode which is expected, but not an error.
		 */
		if (rc && (rc != H_FUNCTION))
3229 3230
			dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
				val, rc);
3231
	}
3232

3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
				H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
	if (rc)
		dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
			scrq->hw_irq, rc);
	return rc;
}

static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
			       struct ibmvnic_sub_crq_queue *scrq)
{
	struct device *dev = &adapter->vdev->dev;
3245
	struct ibmvnic_tx_pool *tx_pool;
3246
	struct ibmvnic_tx_buff *txbuff;
3247
	struct netdev_queue *txq;
3248 3249
	union sub_crq *next;
	int index;
3250
	int i;
3251 3252 3253 3254

restart_loop:
	while (pending_scrq(adapter, scrq)) {
		unsigned int pool = scrq->pool_index;
3255
		int num_entries = 0;
3256 3257
		int total_bytes = 0;
		int num_packets = 0;
3258

3259 3260 3261 3262 3263 3264 3265
		/* The queue entry at the current index is peeked at above
		 * to determine that there is a valid descriptor awaiting
		 * processing. We want to be sure that the current slot
		 * holds a valid descriptor before reading its contents.
		 */
		dma_rmb();

3266 3267
		next = ibmvnic_next_scrq(adapter, scrq);
		for (i = 0; i < next->tx_comp.num_comps; i++) {
3268
			if (next->tx_comp.rcs[i])
3269 3270 3271
				dev_err(dev, "tx error %x\n",
					next->tx_comp.rcs[i]);
			index = be32_to_cpu(next->tx_comp.correlators[i]);
3272 3273 3274 3275 3276 3277 3278 3279
			if (index & IBMVNIC_TSO_POOL_MASK) {
				tx_pool = &adapter->tso_pool[pool];
				index &= ~IBMVNIC_TSO_POOL_MASK;
			} else {
				tx_pool = &adapter->tx_pool[pool];
			}

			txbuff = &tx_pool->tx_buff[index];
3280 3281 3282 3283 3284
			num_packets++;
			num_entries += txbuff->num_entries;
			if (txbuff->skb) {
				total_bytes += txbuff->skb->len;
				dev_consume_skb_irq(txbuff->skb);
3285
				txbuff->skb = NULL;
3286 3287 3288
			} else {
				netdev_warn(adapter->netdev,
					    "TX completion received with NULL socket buffer\n");
3289
			}
3290 3291 3292 3293
			tx_pool->free_map[tx_pool->producer_index] = index;
			tx_pool->producer_index =
				(tx_pool->producer_index + 1) %
					tx_pool->num_buffers;
3294 3295 3296
		}
		/* remove tx_comp scrq*/
		next->tx_comp.first = 0;
3297

3298 3299 3300
		txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
		netdev_tx_completed_queue(txq, num_packets, total_bytes);

3301
		if (atomic_sub_return(num_entries, &scrq->used) <=
3302 3303 3304 3305
		    (adapter->req_tx_entries_per_subcrq / 2) &&
		    __netif_subqueue_stopped(adapter->netdev,
					     scrq->pool_index)) {
			netif_wake_subqueue(adapter->netdev, scrq->pool_index);
3306 3307
			netdev_dbg(adapter->netdev, "Started queue %d\n",
				   scrq->pool_index);
3308
		}
3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
	}

	enable_scrq_irq(adapter, scrq);

	if (pending_scrq(adapter, scrq)) {
		disable_scrq_irq(adapter, scrq);
		goto restart_loop;
	}

	return 0;
}

static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
{
	struct ibmvnic_sub_crq_queue *scrq = instance;
	struct ibmvnic_adapter *adapter = scrq->adapter;

	disable_scrq_irq(adapter, scrq);
	ibmvnic_complete_tx(adapter, scrq);

	return IRQ_HANDLED;
}

static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
{
	struct ibmvnic_sub_crq_queue *scrq = instance;
	struct ibmvnic_adapter *adapter = scrq->adapter;

3337 3338 3339 3340 3341 3342
	/* When booting a kdump kernel we can hit pending interrupts
	 * prior to completing driver initialization.
	 */
	if (unlikely(adapter->state != VNIC_OPEN))
		return IRQ_NONE;

3343 3344
	adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;

3345 3346 3347 3348 3349 3350 3351 3352
	if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
		disable_scrq_irq(adapter, scrq);
		__napi_schedule(&adapter->napi[scrq->scrq_num]);
	}

	return IRQ_HANDLED;
}

3353 3354 3355 3356 3357 3358 3359 3360
static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
	struct ibmvnic_sub_crq_queue *scrq;
	int i = 0, j = 0;
	int rc = 0;

	for (i = 0; i < adapter->req_tx_queues; i++) {
3361 3362
		netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
			   i);
3363 3364 3365
		scrq = adapter->tx_scrq[i];
		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);

3366
		if (!scrq->irq) {
3367 3368 3369 3370 3371
			rc = -EINVAL;
			dev_err(dev, "Error mapping irq\n");
			goto req_tx_irq_failed;
		}

3372 3373
		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
			 adapter->vdev->unit_address, i);
3374
		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
3375
				 0, scrq->name, scrq);
3376 3377 3378 3379 3380

		if (rc) {
			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
				scrq->irq, rc);
			irq_dispose_mapping(scrq->irq);
3381
			goto req_tx_irq_failed;
3382 3383 3384 3385
		}
	}

	for (i = 0; i < adapter->req_rx_queues; i++) {
3386 3387
		netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
			   i);
3388 3389
		scrq = adapter->rx_scrq[i];
		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3390
		if (!scrq->irq) {
3391 3392 3393 3394
			rc = -EINVAL;
			dev_err(dev, "Error mapping irq\n");
			goto req_rx_irq_failed;
		}
3395 3396
		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
			 adapter->vdev->unit_address, i);
3397
		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
3398
				 0, scrq->name, scrq);
3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
		if (rc) {
			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
				scrq->irq, rc);
			irq_dispose_mapping(scrq->irq);
			goto req_rx_irq_failed;
		}
	}
	return rc;

req_rx_irq_failed:
3409
	for (j = 0; j < i; j++) {
3410 3411
		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
3412
	}
3413 3414
	i = adapter->req_tx_queues;
req_tx_irq_failed:
3415
	for (j = 0; j < i; j++) {
3416
		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
3417
		irq_dispose_mapping(adapter->tx_scrq[j]->irq);
3418
	}
3419
	release_sub_crqs(adapter, 1);
3420 3421 3422
	return rc;
}

3423
static int init_sub_crqs(struct ibmvnic_adapter *adapter)
3424 3425 3426 3427 3428 3429
{
	struct device *dev = &adapter->vdev->dev;
	struct ibmvnic_sub_crq_queue **allqueues;
	int registered_queues = 0;
	int total_queues;
	int more = 0;
3430
	int i;
3431 3432 3433

	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;

3434
	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
3435
	if (!allqueues)
3436
		return -1;
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473

	for (i = 0; i < total_queues; i++) {
		allqueues[i] = init_sub_crq_queue(adapter);
		if (!allqueues[i]) {
			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
			break;
		}
		registered_queues++;
	}

	/* Make sure we were able to register the minimum number of queues */
	if (registered_queues <
	    adapter->min_tx_queues + adapter->min_rx_queues) {
		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
		goto tx_failed;
	}

	/* Distribute the failed allocated queues*/
	for (i = 0; i < total_queues - registered_queues + more ; i++) {
		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
		switch (i % 3) {
		case 0:
			if (adapter->req_rx_queues > adapter->min_rx_queues)
				adapter->req_rx_queues--;
			else
				more++;
			break;
		case 1:
			if (adapter->req_tx_queues > adapter->min_tx_queues)
				adapter->req_tx_queues--;
			else
				more++;
			break;
		}
	}

	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3474
				   sizeof(*adapter->tx_scrq), GFP_KERNEL);
3475 3476 3477 3478 3479 3480
	if (!adapter->tx_scrq)
		goto tx_failed;

	for (i = 0; i < adapter->req_tx_queues; i++) {
		adapter->tx_scrq[i] = allqueues[i];
		adapter->tx_scrq[i]->pool_index = i;
3481
		adapter->num_active_tx_scrqs++;
3482 3483 3484
	}

	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3485
				   sizeof(*adapter->rx_scrq), GFP_KERNEL);
3486 3487 3488 3489 3490 3491
	if (!adapter->rx_scrq)
		goto rx_failed;

	for (i = 0; i < adapter->req_rx_queues; i++) {
		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
		adapter->rx_scrq[i]->scrq_num = i;
3492
		adapter->num_active_rx_scrqs++;
3493 3494
	}

3495 3496 3497 3498 3499 3500 3501 3502
	kfree(allqueues);
	return 0;

rx_failed:
	kfree(adapter->tx_scrq);
	adapter->tx_scrq = NULL;
tx_failed:
	for (i = 0; i < registered_queues; i++)
3503
		release_sub_crq_queue(adapter, allqueues[i], 1);
3504 3505 3506 3507
	kfree(allqueues);
	return -1;
}

3508
static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
3509 3510 3511
{
	struct device *dev = &adapter->vdev->dev;
	union ibmvnic_crq crq;
3512
	int max_entries;
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523

	if (!retry) {
		/* Sub-CRQ entries are 32 byte long */
		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);

		if (adapter->min_tx_entries_per_subcrq > entries_page ||
		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
			return;
		}

3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542
		if (adapter->desired.mtu)
			adapter->req_mtu = adapter->desired.mtu;
		else
			adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;

		if (!adapter->desired.tx_entries)
			adapter->desired.tx_entries =
					adapter->max_tx_entries_per_subcrq;
		if (!adapter->desired.rx_entries)
			adapter->desired.rx_entries =
					adapter->max_rx_add_entries_per_subcrq;

		max_entries = IBMVNIC_MAX_LTB_SIZE /
			      (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);

		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
			adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
			adapter->desired.tx_entries = max_entries;
		}
3543

3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577
		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
			adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
			adapter->desired.rx_entries = max_entries;
		}

		if (adapter->desired.tx_entries)
			adapter->req_tx_entries_per_subcrq =
					adapter->desired.tx_entries;
		else
			adapter->req_tx_entries_per_subcrq =
					adapter->max_tx_entries_per_subcrq;

		if (adapter->desired.rx_entries)
			adapter->req_rx_add_entries_per_subcrq =
					adapter->desired.rx_entries;
		else
			adapter->req_rx_add_entries_per_subcrq =
					adapter->max_rx_add_entries_per_subcrq;

		if (adapter->desired.tx_queues)
			adapter->req_tx_queues =
					adapter->desired.tx_queues;
		else
			adapter->req_tx_queues =
					adapter->opt_tx_comp_sub_queues;

		if (adapter->desired.rx_queues)
			adapter->req_rx_queues =
					adapter->desired.rx_queues;
		else
			adapter->req_rx_queues =
					adapter->opt_rx_comp_queues;

		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3578 3579
	}

3580 3581 3582 3583 3584
	memset(&crq, 0, sizeof(crq));
	crq.request_capability.first = IBMVNIC_CRQ_CMD;
	crq.request_capability.cmd = REQUEST_CAPABILITY;

	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3585
	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3586
	atomic_inc(&adapter->running_cap_crqs);
3587 3588 3589
	ibmvnic_send_crq(adapter, &crq);

	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3590
	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3591
	atomic_inc(&adapter->running_cap_crqs);
3592 3593 3594
	ibmvnic_send_crq(adapter, &crq);

	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3595
	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3596
	atomic_inc(&adapter->running_cap_crqs);
3597 3598 3599 3600 3601
	ibmvnic_send_crq(adapter, &crq);

	crq.request_capability.capability =
	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
	crq.request_capability.number =
3602
	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3603
	atomic_inc(&adapter->running_cap_crqs);
3604 3605 3606 3607 3608
	ibmvnic_send_crq(adapter, &crq);

	crq.request_capability.capability =
	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
	crq.request_capability.number =
3609
	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3610
	atomic_inc(&adapter->running_cap_crqs);
3611 3612 3613
	ibmvnic_send_crq(adapter, &crq);

	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3614
	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3615
	atomic_inc(&adapter->running_cap_crqs);
3616 3617 3618 3619 3620 3621
	ibmvnic_send_crq(adapter, &crq);

	if (adapter->netdev->flags & IFF_PROMISC) {
		if (adapter->promisc_supported) {
			crq.request_capability.capability =
			    cpu_to_be16(PROMISC_REQUESTED);
3622
			crq.request_capability.number = cpu_to_be64(1);
3623
			atomic_inc(&adapter->running_cap_crqs);
3624 3625 3626 3627 3628
			ibmvnic_send_crq(adapter, &crq);
		}
	} else {
		crq.request_capability.capability =
		    cpu_to_be16(PROMISC_REQUESTED);
3629
		crq.request_capability.number = cpu_to_be64(0);
3630
		atomic_inc(&adapter->running_cap_crqs);
3631 3632 3633 3634 3635 3636 3637 3638 3639
		ibmvnic_send_crq(adapter, &crq);
	}
}

static int pending_scrq(struct ibmvnic_adapter *adapter,
			struct ibmvnic_sub_crq_queue *scrq)
{
	union sub_crq *entry = &scrq->msgs[scrq->cur];

3640
	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
		return 1;
	else
		return 0;
}

static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
					struct ibmvnic_sub_crq_queue *scrq)
{
	union sub_crq *entry;
	unsigned long flags;

	spin_lock_irqsave(&scrq->lock, flags);
	entry = &scrq->msgs[scrq->cur];
	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
		if (++scrq->cur == scrq->size)
			scrq->cur = 0;
	} else {
		entry = NULL;
	}
	spin_unlock_irqrestore(&scrq->lock, flags);

3662 3663 3664 3665 3666
	/* Ensure that the entire buffer descriptor has been
	 * loaded before reading its contents
	 */
	dma_rmb();

3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
	return entry;
}

static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_crq_queue *queue = &adapter->crq;
	union ibmvnic_crq *crq;

	crq = &queue->msgs[queue->cur];
	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
		if (++queue->cur == queue->size)
			queue->cur = 0;
	} else {
		crq = NULL;
	}

	return crq;
}

3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704
static void print_subcrq_error(struct device *dev, int rc, const char *func)
{
	switch (rc) {
	case H_PARAMETER:
		dev_warn_ratelimited(dev,
				     "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
				     func, rc);
		break;
	case H_CLOSED:
		dev_warn_ratelimited(dev,
				     "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
				     func, rc);
		break;
	default:
		dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
		break;
	}
}

3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
				u64 remote_handle, u64 ioba, u64 num_entries)
{
	unsigned int ua = adapter->vdev->unit_address;
	struct device *dev = &adapter->vdev->dev;
	int rc;

	/* Make sure the hypervisor sees the complete request */
	mb();
	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
				cpu_to_be64(remote_handle),
				ioba, num_entries);

3718 3719
	if (rc)
		print_subcrq_error(dev, rc, __func__);
3720 3721 3722 3723

	return rc;
}

3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735
static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
			    union ibmvnic_crq *crq)
{
	unsigned int ua = adapter->vdev->unit_address;
	struct device *dev = &adapter->vdev->dev;
	u64 *u64_crq = (u64 *)crq;
	int rc;

	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
		   (unsigned long int)cpu_to_be64(u64_crq[0]),
		   (unsigned long int)cpu_to_be64(u64_crq[1]));

3736 3737 3738 3739 3740 3741
	if (!adapter->crq.active &&
	    crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
		dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
		return -EINVAL;
	}

3742 3743 3744 3745 3746 3747 3748 3749
	/* Make sure the hypervisor sees the complete request */
	mb();

	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
				cpu_to_be64(u64_crq[0]),
				cpu_to_be64(u64_crq[1]));

	if (rc) {
3750
		if (rc == H_CLOSED) {
3751
			dev_warn(dev, "CRQ Queue closed\n");
3752
			/* do not reset, report the fail, wait for passive init from server */
3753 3754
		}

3755 3756 3757 3758 3759 3760 3761 3762
		dev_warn(dev, "Send error (rc=%d)\n", rc);
	}

	return rc;
}

static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
{
3763
	struct device *dev = &adapter->vdev->dev;
3764
	union ibmvnic_crq crq;
3765 3766
	int retries = 100;
	int rc;
3767 3768 3769 3770 3771 3772

	memset(&crq, 0, sizeof(crq));
	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
	crq.generic.cmd = IBMVNIC_CRQ_INIT;
	netdev_dbg(adapter->netdev, "Sending CRQ init\n");

3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787
	do {
		rc = ibmvnic_send_crq(adapter, &crq);
		if (rc != H_CLOSED)
			break;
		retries--;
		msleep(50);

	} while (retries > 0);

	if (rc) {
		dev_err(dev, "Failed to send init request, rc = %d\n", rc);
		return rc;
	}

	return 0;
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
}

static int send_version_xchg(struct ibmvnic_adapter *adapter)
{
	union ibmvnic_crq crq;

	memset(&crq, 0, sizeof(crq));
	crq.version_exchange.first = IBMVNIC_CRQ_CMD;
	crq.version_exchange.cmd = VERSION_EXCHANGE;
	crq.version_exchange.version = cpu_to_be16(ibmvnic_version);

	return ibmvnic_send_crq(adapter, &crq);
}

3802 3803 3804
struct vnic_login_client_data {
	u8	type;
	__be16	len;
3805
	char	name[];
3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833
} __packed;

static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
{
	int len;

	/* Calculate the amount of buffer space needed for the
	 * vnic client data in the login buffer. There are four entries,
	 * OS name, LPAR name, device name, and a null last entry.
	 */
	len = 4 * sizeof(struct vnic_login_client_data);
	len += 6; /* "Linux" plus NULL */
	len += strlen(utsname()->nodename) + 1;
	len += strlen(adapter->netdev->name) + 1;

	return len;
}

static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
				 struct vnic_login_client_data *vlcd)
{
	const char *os_name = "Linux";
	int len;

	/* Type 1 - LPAR OS */
	vlcd->type = 1;
	len = strlen(os_name) + 1;
	vlcd->len = cpu_to_be16(len);
3834 3835
	strncpy(vlcd->name, os_name, len);
	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3836 3837 3838 3839 3840

	/* Type 2 - LPAR name */
	vlcd->type = 2;
	len = strlen(utsname()->nodename) + 1;
	vlcd->len = cpu_to_be16(len);
3841 3842
	strncpy(vlcd->name, utsname()->nodename, len);
	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3843 3844 3845 3846 3847

	/* Type 3 - device name */
	vlcd->type = 3;
	len = strlen(adapter->netdev->name) + 1;
	vlcd->len = cpu_to_be16(len);
3848
	strncpy(vlcd->name, adapter->netdev->name, len);
3849 3850
}

3851
static int send_login(struct ibmvnic_adapter *adapter)
3852 3853 3854 3855
{
	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
	struct ibmvnic_login_buffer *login_buffer;
	struct device *dev = &adapter->vdev->dev;
3856
	struct vnic_login_client_data *vlcd;
3857 3858 3859 3860
	dma_addr_t rsp_buffer_token;
	dma_addr_t buffer_token;
	size_t rsp_buffer_size;
	union ibmvnic_crq crq;
3861
	int client_data_len;
3862 3863 3864
	size_t buffer_size;
	__be64 *tx_list_p;
	__be64 *rx_list_p;
3865
	int rc;
3866 3867
	int i;

3868 3869 3870 3871 3872 3873
	if (!adapter->tx_scrq || !adapter->rx_scrq) {
		netdev_err(adapter->netdev,
			   "RX or TX queues are not allocated, device login failed\n");
		return -1;
	}

L
Lijun Pan 已提交
3874
	release_login_buffer(adapter);
3875
	release_login_rsp_buffer(adapter);
L
Lijun Pan 已提交
3876

3877 3878
	client_data_len = vnic_client_data_len(adapter);

3879 3880
	buffer_size =
	    sizeof(struct ibmvnic_login_buffer) +
3881 3882
	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
	    client_data_len;
3883

3884
	login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
	if (!login_buffer)
		goto buf_alloc_failed;

	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
				      DMA_TO_DEVICE);
	if (dma_mapping_error(dev, buffer_token)) {
		dev_err(dev, "Couldn't map login buffer\n");
		goto buf_map_failed;
	}

3895 3896 3897 3898 3899
	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
			  sizeof(u64) * adapter->req_tx_queues +
			  sizeof(u64) * adapter->req_rx_queues +
			  sizeof(u64) * adapter->req_rx_queues +
			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910

	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
	if (!login_rsp_buffer)
		goto buf_rsp_alloc_failed;

	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
					  rsp_buffer_size, DMA_FROM_DEVICE);
	if (dma_mapping_error(dev, rsp_buffer_token)) {
		dev_err(dev, "Couldn't map login rsp buffer\n");
		goto buf_rsp_map_failed;
	}
N
Nathan Fontenot 已提交
3911

3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950
	adapter->login_buf = login_buffer;
	adapter->login_buf_token = buffer_token;
	adapter->login_buf_sz = buffer_size;
	adapter->login_rsp_buf = login_rsp_buffer;
	adapter->login_rsp_buf_token = rsp_buffer_token;
	adapter->login_rsp_buf_sz = rsp_buffer_size;

	login_buffer->len = cpu_to_be32(buffer_size);
	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
	login_buffer->off_txcomp_subcrqs =
	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
	login_buffer->off_rxcomp_subcrqs =
	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
			sizeof(u64) * adapter->req_tx_queues);
	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);

	tx_list_p = (__be64 *)((char *)login_buffer +
				      sizeof(struct ibmvnic_login_buffer));
	rx_list_p = (__be64 *)((char *)login_buffer +
				      sizeof(struct ibmvnic_login_buffer) +
				      sizeof(u64) * adapter->req_tx_queues);

	for (i = 0; i < adapter->req_tx_queues; i++) {
		if (adapter->tx_scrq[i]) {
			tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
						   crq_num);
		}
	}

	for (i = 0; i < adapter->req_rx_queues; i++) {
		if (adapter->rx_scrq[i]) {
			rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
						   crq_num);
		}
	}

3951 3952 3953 3954 3955 3956 3957 3958 3959
	/* Insert vNIC login client data */
	vlcd = (struct vnic_login_client_data *)
		((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
	login_buffer->client_data_offset =
			cpu_to_be32((char *)vlcd - (char *)login_buffer);
	login_buffer->client_data_len = cpu_to_be32(client_data_len);

	vnic_add_client_data(adapter, vlcd);

3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
	netdev_dbg(adapter->netdev, "Login Buffer:\n");
	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
		netdev_dbg(adapter->netdev, "%016lx\n",
			   ((unsigned long int *)(adapter->login_buf))[i]);
	}

	memset(&crq, 0, sizeof(crq));
	crq.login.first = IBMVNIC_CRQ_CMD;
	crq.login.cmd = LOGIN;
	crq.login.ioba = cpu_to_be32(buffer_token);
	crq.login.len = cpu_to_be32(buffer_size);
S
Sukadev Bhattiprolu 已提交
3971 3972

	adapter->login_pending = true;
3973 3974 3975 3976 3977 3978
	rc = ibmvnic_send_crq(adapter, &crq);
	if (rc) {
		adapter->login_pending = false;
		netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
		goto buf_rsp_map_failed;
	}
3979

3980
	return 0;
3981 3982 3983

buf_rsp_map_failed:
	kfree(login_rsp_buffer);
3984
	adapter->login_rsp_buf = NULL;
3985 3986 3987 3988
buf_rsp_alloc_failed:
	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
buf_map_failed:
	kfree(login_buffer);
3989
	adapter->login_buf = NULL;
3990
buf_alloc_failed:
3991
	return -1;
3992 3993
}

3994 3995
static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
			    u32 len, u8 map_id)
3996 3997 3998 3999 4000 4001 4002 4003 4004
{
	union ibmvnic_crq crq;

	memset(&crq, 0, sizeof(crq));
	crq.request_map.first = IBMVNIC_CRQ_CMD;
	crq.request_map.cmd = REQUEST_MAP;
	crq.request_map.map_id = map_id;
	crq.request_map.ioba = cpu_to_be32(addr);
	crq.request_map.len = cpu_to_be32(len);
4005
	return ibmvnic_send_crq(adapter, &crq);
4006 4007
}

4008
static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
4009 4010 4011 4012 4013 4014 4015
{
	union ibmvnic_crq crq;

	memset(&crq, 0, sizeof(crq));
	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
	crq.request_unmap.cmd = REQUEST_UNMAP;
	crq.request_unmap.map_id = map_id;
4016
	return ibmvnic_send_crq(adapter, &crq);
4017 4018
}

4019
static void send_query_map(struct ibmvnic_adapter *adapter)
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
{
	union ibmvnic_crq crq;

	memset(&crq, 0, sizeof(crq));
	crq.query_map.first = IBMVNIC_CRQ_CMD;
	crq.query_map.cmd = QUERY_MAP;
	ibmvnic_send_crq(adapter, &crq);
}

/* Send a series of CRQs requesting various capabilities of the VNIC server */
4030
static void send_query_cap(struct ibmvnic_adapter *adapter)
4031 4032 4033
{
	union ibmvnic_crq crq;

4034
	atomic_set(&adapter->running_cap_crqs, 0);
4035 4036 4037 4038 4039
	memset(&crq, 0, sizeof(crq));
	crq.query_capability.first = IBMVNIC_CRQ_CMD;
	crq.query_capability.cmd = QUERY_CAPABILITY;

	crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
4040
	atomic_inc(&adapter->running_cap_crqs);
4041 4042 4043
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
4044
	atomic_inc(&adapter->running_cap_crqs);
4045 4046 4047
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
4048
	atomic_inc(&adapter->running_cap_crqs);
4049 4050 4051
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
4052
	atomic_inc(&adapter->running_cap_crqs);
4053 4054 4055
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
4056
	atomic_inc(&adapter->running_cap_crqs);
4057 4058 4059
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
4060
	atomic_inc(&adapter->running_cap_crqs);
4061 4062 4063 4064
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
4065
	atomic_inc(&adapter->running_cap_crqs);
4066 4067 4068 4069
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
4070
	atomic_inc(&adapter->running_cap_crqs);
4071 4072 4073 4074
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
4075
	atomic_inc(&adapter->running_cap_crqs);
4076 4077 4078 4079
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
4080
	atomic_inc(&adapter->running_cap_crqs);
4081 4082 4083
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
4084
	atomic_inc(&adapter->running_cap_crqs);
4085 4086 4087
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
4088
	atomic_inc(&adapter->running_cap_crqs);
4089 4090 4091
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
4092
	atomic_inc(&adapter->running_cap_crqs);
4093 4094 4095
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
4096
	atomic_inc(&adapter->running_cap_crqs);
4097 4098 4099
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
4100
	atomic_inc(&adapter->running_cap_crqs);
4101 4102 4103
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
4104
	atomic_inc(&adapter->running_cap_crqs);
4105 4106
	ibmvnic_send_crq(adapter, &crq);

4107 4108 4109 4110
	crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
	atomic_inc(&adapter->running_cap_crqs);
	ibmvnic_send_crq(adapter, &crq);

4111
	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
4112
	atomic_inc(&adapter->running_cap_crqs);
4113 4114 4115
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
4116
	atomic_inc(&adapter->running_cap_crqs);
4117 4118 4119
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
4120
	atomic_inc(&adapter->running_cap_crqs);
4121 4122 4123
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
4124
	atomic_inc(&adapter->running_cap_crqs);
4125 4126 4127 4128
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
4129
	atomic_inc(&adapter->running_cap_crqs);
4130 4131 4132 4133
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
4134
	atomic_inc(&adapter->running_cap_crqs);
4135 4136 4137 4138
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability =
			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
4139
	atomic_inc(&adapter->running_cap_crqs);
4140 4141 4142
	ibmvnic_send_crq(adapter, &crq);

	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
4143
	atomic_inc(&adapter->running_cap_crqs);
4144 4145 4146
	ibmvnic_send_crq(adapter, &crq);
}

4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174
static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
{
	int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
	struct device *dev = &adapter->vdev->dev;
	union ibmvnic_crq crq;

	adapter->ip_offload_tok =
		dma_map_single(dev,
			       &adapter->ip_offload_buf,
			       buf_sz,
			       DMA_FROM_DEVICE);

	if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
		if (!firmware_has_feature(FW_FEATURE_CMO))
			dev_err(dev, "Couldn't map offload buffer\n");
		return;
	}

	memset(&crq, 0, sizeof(crq));
	crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
	crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
	crq.query_ip_offload.len = cpu_to_be32(buf_sz);
	crq.query_ip_offload.ioba =
	    cpu_to_be32(adapter->ip_offload_tok);

	ibmvnic_send_crq(adapter, &crq);
}

4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253
static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
	struct device *dev = &adapter->vdev->dev;
	netdev_features_t old_hw_features = 0;
	union ibmvnic_crq crq;

	adapter->ip_offload_ctrl_tok =
		dma_map_single(dev,
			       ctrl_buf,
			       sizeof(adapter->ip_offload_ctrl),
			       DMA_TO_DEVICE);

	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
		dev_err(dev, "Couldn't map ip offload control buffer\n");
		return;
	}

	ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
	ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
	ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
	ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
	ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
	ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
	ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
	ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
	ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
	ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;

	/* large_rx disabled for now, additional features needed */
	ctrl_buf->large_rx_ipv4 = 0;
	ctrl_buf->large_rx_ipv6 = 0;

	if (adapter->state != VNIC_PROBING) {
		old_hw_features = adapter->netdev->hw_features;
		adapter->netdev->hw_features = 0;
	}

	adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;

	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
		adapter->netdev->hw_features |= NETIF_F_IP_CSUM;

	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
		adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;

	if ((adapter->netdev->features &
	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
		adapter->netdev->hw_features |= NETIF_F_RXCSUM;

	if (buf->large_tx_ipv4)
		adapter->netdev->hw_features |= NETIF_F_TSO;
	if (buf->large_tx_ipv6)
		adapter->netdev->hw_features |= NETIF_F_TSO6;

	if (adapter->state == VNIC_PROBING) {
		adapter->netdev->features |= adapter->netdev->hw_features;
	} else if (old_hw_features != adapter->netdev->hw_features) {
		netdev_features_t tmp = 0;

		/* disable features no longer supported */
		adapter->netdev->features &= adapter->netdev->hw_features;
		/* turn on features now supported if previously enabled */
		tmp = (old_hw_features ^ adapter->netdev->hw_features) &
			adapter->netdev->hw_features;
		adapter->netdev->features |=
				tmp & adapter->netdev->wanted_features;
	}

	memset(&crq, 0, sizeof(crq));
	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
	crq.control_ip_offload.len =
	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
	ibmvnic_send_crq(adapter, &crq);
}

4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273
static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
				struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;

	if (crq->get_vpd_size_rsp.rc.code) {
		dev_err(dev, "Error retrieving VPD size, rc=%x\n",
			crq->get_vpd_size_rsp.rc.code);
		complete(&adapter->fw_done);
		return;
	}

	adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
	complete(&adapter->fw_done);
}

static void handle_vpd_rsp(union ibmvnic_crq *crq,
			   struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
4274
	unsigned char *substr = NULL;
4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292
	u8 fw_level_len = 0;

	memset(adapter->fw_version, 0, 32);

	dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
			 DMA_FROM_DEVICE);

	if (crq->get_vpd_rsp.rc.code) {
		dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
			crq->get_vpd_rsp.rc.code);
		goto complete;
	}

	/* get the position of the firmware version info
	 * located after the ASCII 'RM' substring in the buffer
	 */
	substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
	if (!substr) {
4293
		dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307
		goto complete;
	}

	/* get length of firmware level ASCII substring */
	if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
		fw_level_len = *(substr + 2);
	} else {
		dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
		goto complete;
	}

	/* copy firmware version string from vpd into adapter */
	if ((substr + 3 + fw_level_len) <
	    (adapter->vpd->buff + adapter->vpd->len)) {
4308
		strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
4309 4310 4311 4312 4313
	} else {
		dev_info(dev, "FW substr extrapolated VPD buff\n");
	}

complete:
4314 4315
	if (adapter->fw_version[0] == '\0')
		strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
4316 4317 4318
	complete(&adapter->fw_done);
}

4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371
static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
	int i;

	dma_unmap_single(dev, adapter->ip_offload_tok,
			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);

	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
		netdev_dbg(adapter->netdev, "%016lx\n",
			   ((unsigned long int *)(buf))[i]);

	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
		   buf->tcp_ipv4_chksum);
	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
		   buf->tcp_ipv6_chksum);
	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
		   buf->udp_ipv4_chksum);
	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
		   buf->udp_ipv6_chksum);
	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
		   buf->large_tx_ipv4);
	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
		   buf->large_tx_ipv6);
	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
		   buf->large_rx_ipv4);
	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
		   buf->large_rx_ipv6);
	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
		   buf->max_ipv4_header_size);
	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
		   buf->max_ipv6_header_size);
	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
		   buf->max_tcp_header_size);
	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
		   buf->max_udp_header_size);
	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
		   buf->max_large_tx_size);
	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
		   buf->max_large_rx_size);
	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
		   buf->ipv6_extension_header);
	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
		   buf->tcp_pseudosum_req);
	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
		   buf->num_ipv6_ext_headers);
	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
		   buf->off_ipv6_ext_headers);

4372
	send_control_ip_offload(adapter);
4373 4374
}

4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396
static const char *ibmvnic_fw_err_cause(u16 cause)
{
	switch (cause) {
	case ADAPTER_PROBLEM:
		return "adapter problem";
	case BUS_PROBLEM:
		return "bus problem";
	case FW_PROBLEM:
		return "firmware problem";
	case DD_PROBLEM:
		return "device driver problem";
	case EEH_RECOVERY:
		return "EEH recovery";
	case FW_UPDATED:
		return "firmware updated";
	case LOW_MEMORY:
		return "low Memory";
	default:
		return "unknown";
	}
}

4397 4398 4399 4400
static void handle_error_indication(union ibmvnic_crq *crq,
				    struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
4401 4402 4403
	u16 cause;

	cause = be16_to_cpu(crq->error_indication.error_cause);
4404

4405 4406 4407 4408 4409
	dev_warn_ratelimited(dev,
			     "Firmware reports %serror, cause: %s. Starting recovery...\n",
			     crq->error_indication.flags
				& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
			     ibmvnic_fw_err_cause(cause));
4410

4411 4412
	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
J
John Allen 已提交
4413 4414
	else
		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
4415 4416
}

4417 4418
static int handle_change_mac_rsp(union ibmvnic_crq *crq,
				 struct ibmvnic_adapter *adapter)
4419 4420 4421 4422 4423 4424 4425 4426
{
	struct net_device *netdev = adapter->netdev;
	struct device *dev = &adapter->vdev->dev;
	long rc;

	rc = crq->change_mac_addr_rsp.rc.code;
	if (rc) {
		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
4427
		goto out;
4428
	}
4429 4430 4431
	/* crq->change_mac_addr.mac_addr is the requested one
	 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
	 */
4432 4433
	ether_addr_copy(netdev->dev_addr,
			&crq->change_mac_addr_rsp.mac_addr[0]);
4434 4435
	ether_addr_copy(adapter->mac_addr,
			&crq->change_mac_addr_rsp.mac_addr[0]);
4436 4437 4438
out:
	complete(&adapter->fw_done);
	return rc;
4439 4440 4441 4442 4443 4444 4445 4446 4447
}

static void handle_request_cap_rsp(union ibmvnic_crq *crq,
				   struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
	u64 *req_value;
	char *name;

4448
	atomic_dec(&adapter->running_cap_crqs);
4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489
	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
	case REQ_TX_QUEUES:
		req_value = &adapter->req_tx_queues;
		name = "tx";
		break;
	case REQ_RX_QUEUES:
		req_value = &adapter->req_rx_queues;
		name = "rx";
		break;
	case REQ_RX_ADD_QUEUES:
		req_value = &adapter->req_rx_add_queues;
		name = "rx_add";
		break;
	case REQ_TX_ENTRIES_PER_SUBCRQ:
		req_value = &adapter->req_tx_entries_per_subcrq;
		name = "tx_entries_per_subcrq";
		break;
	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
		req_value = &adapter->req_rx_add_entries_per_subcrq;
		name = "rx_add_entries_per_subcrq";
		break;
	case REQ_MTU:
		req_value = &adapter->req_mtu;
		name = "mtu";
		break;
	case PROMISC_REQUESTED:
		req_value = &adapter->promisc;
		name = "promisc";
		break;
	default:
		dev_err(dev, "Got invalid cap request rsp %d\n",
			crq->request_capability.capability);
		return;
	}

	switch (crq->request_capability_rsp.rc.code) {
	case SUCCESS:
		break;
	case PARTIALSUCCESS:
		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
			 *req_value,
4490
			 (long int)be64_to_cpu(crq->request_capability_rsp.
4491
					       number), name);
4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502

		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
		    REQ_MTU) {
			pr_err("mtu of %llu is not supported. Reverting.\n",
			       *req_value);
			*req_value = adapter->fallback.mtu;
		} else {
			*req_value =
				be64_to_cpu(crq->request_capability_rsp.number);
		}

4503
		send_request_cap(adapter, 1);
4504 4505 4506 4507 4508 4509 4510 4511
		return;
	default:
		dev_err(dev, "Error %d in request cap rsp\n",
			crq->request_capability_rsp.rc.code);
		return;
	}

	/* Done receiving requested capabilities, query IP offload support */
4512
	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4513
		adapter->wait_capability = false;
4514
		send_query_ip_offload(adapter);
4515 4516 4517 4518 4519 4520 4521
	}
}

static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
			    struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
4522
	struct net_device *netdev = adapter->netdev;
4523 4524
	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
	struct ibmvnic_login_buffer *login = adapter->login_buf;
4525 4526 4527 4528
	u64 *tx_handle_array;
	u64 *rx_handle_array;
	int num_tx_pools;
	int num_rx_pools;
4529
	u64 *size_array;
4530 4531
	int i;

S
Sukadev Bhattiprolu 已提交
4532 4533 4534 4535 4536 4537 4538 4539 4540
	/* CHECK: Test/set of login_pending does not need to be atomic
	 * because only ibmvnic_tasklet tests/clears this.
	 */
	if (!adapter->login_pending) {
		netdev_warn(netdev, "Ignoring unexpected login response\n");
		return 0;
	}
	adapter->login_pending = false;

4541
	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
4542
			 DMA_TO_DEVICE);
4543
	dma_unmap_single(dev, adapter->login_rsp_buf_token,
4544
			 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
4545

4546 4547 4548 4549 4550
	/* If the number of queues requested can't be allocated by the
	 * server, the login response will return with code 1. We will need
	 * to resend the login buffer with fewer queues requested.
	 */
	if (login_rsp_crq->generic.rc.code) {
4551
		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4552 4553 4554 4555
		complete(&adapter->init_done);
		return 0;
	}

4556 4557
	netdev->mtu = adapter->req_mtu - ETH_HLEN;

4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569
	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
		netdev_dbg(adapter->netdev, "%016lx\n",
			   ((unsigned long int *)(adapter->login_rsp_buf))[i]);
	}

	/* Sanity checks */
	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
	     adapter->req_rx_add_queues !=
	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4570
		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4571 4572
		return -EIO;
	}
4573 4574 4575 4576 4577 4578
	size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
		be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
	/* variable buffer sizes are not supported, so just read the
	 * first entry.
	 */
	adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593

	num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
	num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);

	tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
				  be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
	rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
				  be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));

	for (i = 0; i < num_tx_pools; i++)
		adapter->tx_scrq[i]->handle = tx_handle_array[i];

	for (i = 0; i < num_rx_pools; i++)
		adapter->rx_scrq[i]->handle = rx_handle_array[i];

4594 4595
	adapter->num_active_tx_scrqs = num_tx_pools;
	adapter->num_active_rx_scrqs = num_rx_pools;
4596
	release_login_rsp_buffer(adapter);
4597
	release_login_buffer(adapter);
4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
	complete(&adapter->init_done);

	return 0;
}

static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
				     struct ibmvnic_adapter *adapter)
{
	struct device *dev = &adapter->vdev->dev;
	long rc;

	rc = crq->request_unmap_rsp.rc.code;
	if (rc)
		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
}

static void handle_query_map_rsp(union ibmvnic_crq *crq,
				 struct ibmvnic_adapter *adapter)
{
	struct net_device *netdev = adapter->netdev;
	struct device *dev = &adapter->vdev->dev;
	long rc;

	rc = crq->query_map_rsp.rc.code;
	if (rc) {
		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
		return;
	}
	netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
		   crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
		   crq->query_map_rsp.free_pages);
}

static void handle_query_cap_rsp(union ibmvnic_crq *crq,
				 struct ibmvnic_adapter *adapter)
{
	struct net_device *netdev = adapter->netdev;
	struct device *dev = &adapter->vdev->dev;
	long rc;

4638
	atomic_dec(&adapter->running_cap_crqs);
4639
	netdev_dbg(netdev, "Outstanding queries: %d\n",
4640
		   atomic_read(&adapter->running_cap_crqs));
4641 4642 4643 4644 4645 4646 4647 4648 4649
	rc = crq->query_capability.rc.code;
	if (rc) {
		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
		goto out;
	}

	switch (be16_to_cpu(crq->query_capability.capability)) {
	case MIN_TX_QUEUES:
		adapter->min_tx_queues =
4650
		    be64_to_cpu(crq->query_capability.number);
4651 4652 4653 4654 4655
		netdev_dbg(netdev, "min_tx_queues = %lld\n",
			   adapter->min_tx_queues);
		break;
	case MIN_RX_QUEUES:
		adapter->min_rx_queues =
4656
		    be64_to_cpu(crq->query_capability.number);
4657 4658 4659 4660 4661
		netdev_dbg(netdev, "min_rx_queues = %lld\n",
			   adapter->min_rx_queues);
		break;
	case MIN_RX_ADD_QUEUES:
		adapter->min_rx_add_queues =
4662
		    be64_to_cpu(crq->query_capability.number);
4663 4664 4665 4666 4667
		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
			   adapter->min_rx_add_queues);
		break;
	case MAX_TX_QUEUES:
		adapter->max_tx_queues =
4668
		    be64_to_cpu(crq->query_capability.number);
4669 4670 4671 4672 4673
		netdev_dbg(netdev, "max_tx_queues = %lld\n",
			   adapter->max_tx_queues);
		break;
	case MAX_RX_QUEUES:
		adapter->max_rx_queues =
4674
		    be64_to_cpu(crq->query_capability.number);
4675 4676 4677 4678 4679
		netdev_dbg(netdev, "max_rx_queues = %lld\n",
			   adapter->max_rx_queues);
		break;
	case MAX_RX_ADD_QUEUES:
		adapter->max_rx_add_queues =
4680
		    be64_to_cpu(crq->query_capability.number);
4681 4682 4683 4684 4685
		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
			   adapter->max_rx_add_queues);
		break;
	case MIN_TX_ENTRIES_PER_SUBCRQ:
		adapter->min_tx_entries_per_subcrq =
4686
		    be64_to_cpu(crq->query_capability.number);
4687 4688 4689 4690 4691
		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
			   adapter->min_tx_entries_per_subcrq);
		break;
	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
		adapter->min_rx_add_entries_per_subcrq =
4692
		    be64_to_cpu(crq->query_capability.number);
4693 4694 4695 4696 4697
		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
			   adapter->min_rx_add_entries_per_subcrq);
		break;
	case MAX_TX_ENTRIES_PER_SUBCRQ:
		adapter->max_tx_entries_per_subcrq =
4698
		    be64_to_cpu(crq->query_capability.number);
4699 4700 4701 4702 4703
		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
			   adapter->max_tx_entries_per_subcrq);
		break;
	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
		adapter->max_rx_add_entries_per_subcrq =
4704
		    be64_to_cpu(crq->query_capability.number);
4705 4706 4707 4708 4709
		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
			   adapter->max_rx_add_entries_per_subcrq);
		break;
	case TCP_IP_OFFLOAD:
		adapter->tcp_ip_offload =
4710
		    be64_to_cpu(crq->query_capability.number);
4711 4712 4713 4714 4715
		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
			   adapter->tcp_ip_offload);
		break;
	case PROMISC_SUPPORTED:
		adapter->promisc_supported =
4716
		    be64_to_cpu(crq->query_capability.number);
4717 4718 4719 4720
		netdev_dbg(netdev, "promisc_supported = %lld\n",
			   adapter->promisc_supported);
		break;
	case MIN_MTU:
4721
		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4722
		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4723 4724 4725
		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
		break;
	case MAX_MTU:
4726
		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4727
		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4728 4729 4730 4731
		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
		break;
	case MAX_MULTICAST_FILTERS:
		adapter->max_multicast_filters =
4732
		    be64_to_cpu(crq->query_capability.number);
4733 4734 4735 4736 4737
		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
			   adapter->max_multicast_filters);
		break;
	case VLAN_HEADER_INSERTION:
		adapter->vlan_header_insertion =
4738
		    be64_to_cpu(crq->query_capability.number);
4739 4740 4741 4742 4743
		if (adapter->vlan_header_insertion)
			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
			   adapter->vlan_header_insertion);
		break;
4744 4745 4746 4747 4748 4749
	case RX_VLAN_HEADER_INSERTION:
		adapter->rx_vlan_header_insertion =
		    be64_to_cpu(crq->query_capability.number);
		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
			   adapter->rx_vlan_header_insertion);
		break;
4750 4751
	case MAX_TX_SG_ENTRIES:
		adapter->max_tx_sg_entries =
4752
		    be64_to_cpu(crq->query_capability.number);
4753 4754 4755 4756 4757
		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
			   adapter->max_tx_sg_entries);
		break;
	case RX_SG_SUPPORTED:
		adapter->rx_sg_supported =
4758
		    be64_to_cpu(crq->query_capability.number);
4759 4760 4761 4762 4763
		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
			   adapter->rx_sg_supported);
		break;
	case OPT_TX_COMP_SUB_QUEUES:
		adapter->opt_tx_comp_sub_queues =
4764
		    be64_to_cpu(crq->query_capability.number);
4765 4766 4767 4768 4769
		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
			   adapter->opt_tx_comp_sub_queues);
		break;
	case OPT_RX_COMP_QUEUES:
		adapter->opt_rx_comp_queues =
4770
		    be64_to_cpu(crq->query_capability.number);
4771 4772 4773 4774 4775
		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
			   adapter->opt_rx_comp_queues);
		break;
	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
		adapter->opt_rx_bufadd_q_per_rx_comp_q =
4776
		    be64_to_cpu(crq->query_capability.number);
4777 4778 4779 4780 4781
		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
		break;
	case OPT_TX_ENTRIES_PER_SUBCRQ:
		adapter->opt_tx_entries_per_subcrq =
4782
		    be64_to_cpu(crq->query_capability.number);
4783 4784 4785 4786 4787
		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
			   adapter->opt_tx_entries_per_subcrq);
		break;
	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
		adapter->opt_rxba_entries_per_subcrq =
4788
		    be64_to_cpu(crq->query_capability.number);
4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803
		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
			   adapter->opt_rxba_entries_per_subcrq);
		break;
	case TX_RX_DESC_REQ:
		adapter->tx_rx_desc_req = crq->query_capability.number;
		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
			   adapter->tx_rx_desc_req);
		break;

	default:
		netdev_err(netdev, "Got invalid cap rsp %d\n",
			   crq->query_capability.capability);
	}

out:
4804 4805
	if (atomic_read(&adapter->running_cap_crqs) == 0) {
		adapter->wait_capability = false;
4806
		send_request_cap(adapter, 0);
4807
	}
4808 4809
}

4810 4811 4812 4813 4814 4815 4816 4817
static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
{
	union ibmvnic_crq crq;
	int rc;

	memset(&crq, 0, sizeof(crq));
	crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
	crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
T
Thomas Falcon 已提交
4818 4819 4820

	mutex_lock(&adapter->fw_lock);
	adapter->fw_done_rc = 0;
4821
	reinit_completion(&adapter->fw_done);
T
Thomas Falcon 已提交
4822

4823
	rc = ibmvnic_send_crq(adapter, &crq);
T
Thomas Falcon 已提交
4824 4825
	if (rc) {
		mutex_unlock(&adapter->fw_lock);
4826
		return rc;
T
Thomas Falcon 已提交
4827
	}
4828 4829

	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
T
Thomas Falcon 已提交
4830 4831
	if (rc) {
		mutex_unlock(&adapter->fw_lock);
4832
		return rc;
T
Thomas Falcon 已提交
4833
	}
4834

T
Thomas Falcon 已提交
4835
	mutex_unlock(&adapter->fw_lock);
4836 4837 4838 4839 4840 4841 4842 4843
	return adapter->fw_done_rc ? -EIO : 0;
}

static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
				       struct ibmvnic_adapter *adapter)
{
	struct net_device *netdev = adapter->netdev;
	int rc;
4844
	__be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
4845 4846 4847 4848 4849 4850

	rc = crq->query_phys_parms_rsp.rc.code;
	if (rc) {
		netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
		return rc;
	}
4851
	switch (rspeed) {
4852 4853 4854 4855 4856 4857 4858 4859 4860
	case IBMVNIC_10MBPS:
		adapter->speed = SPEED_10;
		break;
	case IBMVNIC_100MBPS:
		adapter->speed = SPEED_100;
		break;
	case IBMVNIC_1GBPS:
		adapter->speed = SPEED_1000;
		break;
L
Lijun Pan 已提交
4861
	case IBMVNIC_10GBPS:
4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875
		adapter->speed = SPEED_10000;
		break;
	case IBMVNIC_25GBPS:
		adapter->speed = SPEED_25000;
		break;
	case IBMVNIC_40GBPS:
		adapter->speed = SPEED_40000;
		break;
	case IBMVNIC_50GBPS:
		adapter->speed = SPEED_50000;
		break;
	case IBMVNIC_100GBPS:
		adapter->speed = SPEED_100000;
		break;
L
Lijun Pan 已提交
4876 4877 4878
	case IBMVNIC_200GBPS:
		adapter->speed = SPEED_200000;
		break;
4879
	default:
4880 4881
		if (netif_carrier_ok(netdev))
			netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893
		adapter->speed = SPEED_UNKNOWN;
	}
	if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
		adapter->duplex = DUPLEX_FULL;
	else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
		adapter->duplex = DUPLEX_HALF;
	else
		adapter->duplex = DUPLEX_UNKNOWN;

	return rc;
}

4894 4895 4896 4897 4898 4899
static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
			       struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
	struct net_device *netdev = adapter->netdev;
	struct device *dev = &adapter->vdev->dev;
4900
	u64 *u64_crq = (u64 *)crq;
4901 4902 4903
	long rc;

	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4904 4905
		   (unsigned long int)cpu_to_be64(u64_crq[0]),
		   (unsigned long int)cpu_to_be64(u64_crq[1]));
4906 4907 4908 4909 4910
	switch (gen_crq->first) {
	case IBMVNIC_CRQ_INIT_RSP:
		switch (gen_crq->cmd) {
		case IBMVNIC_CRQ_INIT:
			dev_info(dev, "Partner initialized\n");
4911
			adapter->from_passive_init = true;
S
Sukadev Bhattiprolu 已提交
4912 4913 4914 4915 4916
			/* Discard any stale login responses from prev reset.
			 * CHECK: should we clear even on INIT_COMPLETE?
			 */
			adapter->login_pending = false;

4917 4918 4919 4920
			if (!completion_done(&adapter->init_done)) {
				complete(&adapter->init_done);
				adapter->init_done_rc = -EIO;
			}
4921
			ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4922 4923 4924
			break;
		case IBMVNIC_CRQ_INIT_COMPLETE:
			dev_info(dev, "Partner initialization complete\n");
4925
			adapter->crq.active = true;
4926 4927 4928 4929 4930 4931 4932
			send_version_xchg(adapter);
			break;
		default:
			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
		}
		return;
	case IBMVNIC_CRQ_XPORT_EVENT:
4933
		netif_carrier_off(netdev);
4934
		adapter->crq.active = false;
4935 4936 4937 4938 4939 4940 4941 4942 4943
		/* terminate any thread waiting for a response
		 * from the device
		 */
		if (!completion_done(&adapter->fw_done)) {
			adapter->fw_done_rc = -EIO;
			complete(&adapter->fw_done);
		}
		if (!completion_done(&adapter->stats_done))
			complete(&adapter->stats_done);
4944
		if (test_bit(0, &adapter->resetting))
4945
			adapter->force_reset_recovery = true;
4946
		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4947 4948
			dev_info(dev, "Migrated, re-enabling adapter\n");
			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4949 4950
		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
			dev_info(dev, "Backing device failover detected\n");
4951
			adapter->failover_pending = true;
4952 4953 4954 4955
		} else {
			/* The adapter lost the connection */
			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
				gen_crq->cmd);
4956
			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973
		}
		return;
	case IBMVNIC_CRQ_CMD_RSP:
		break;
	default:
		dev_err(dev, "Got an invalid msg type 0x%02x\n",
			gen_crq->first);
		return;
	}

	switch (gen_crq->cmd) {
	case VERSION_EXCHANGE_RSP:
		rc = crq->version_exchange_rsp.rc.code;
		if (rc) {
			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
			break;
		}
4974
		ibmvnic_version =
4975
			    be16_to_cpu(crq->version_exchange_rsp.version);
4976 4977
		dev_info(dev, "Partner protocol version is %d\n",
			 ibmvnic_version);
4978
		send_query_cap(adapter);
4979 4980 4981 4982 4983 4984 4985 4986
		break;
	case QUERY_CAPABILITY_RSP:
		handle_query_cap_rsp(crq, adapter);
		break;
	case QUERY_MAP_RSP:
		handle_query_map_rsp(crq, adapter);
		break;
	case REQUEST_MAP_RSP:
4987 4988
		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
		complete(&adapter->fw_done);
4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000
		break;
	case REQUEST_UNMAP_RSP:
		handle_request_unmap_rsp(crq, adapter);
		break;
	case REQUEST_CAPABILITY_RSP:
		handle_request_cap_rsp(crq, adapter);
		break;
	case LOGIN_RSP:
		netdev_dbg(netdev, "Got Login Response\n");
		handle_login_rsp(crq, adapter);
		break;
	case LOGICAL_LINK_STATE_RSP:
5001 5002 5003 5004
		netdev_dbg(netdev,
			   "Got Logical Link State Response, state: %d rc: %d\n",
			   crq->logical_link_state_rsp.link_state,
			   crq->logical_link_state_rsp.rc.code);
5005 5006
		adapter->logical_link_state =
		    crq->logical_link_state_rsp.link_state;
5007 5008
		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
		complete(&adapter->init_done);
5009 5010 5011 5012 5013 5014 5015
		break;
	case LINK_STATE_INDICATION:
		netdev_dbg(netdev, "Got Logical Link State Indication\n");
		adapter->phys_link_state =
		    crq->link_state_indication.phys_link_state;
		adapter->logical_link_state =
		    crq->link_state_indication.logical_link_state;
5016 5017 5018 5019
		if (adapter->phys_link_state && adapter->logical_link_state)
			netif_carrier_on(netdev);
		else
			netif_carrier_off(netdev);
5020 5021 5022
		break;
	case CHANGE_MAC_ADDR_RSP:
		netdev_dbg(netdev, "Got MAC address change Response\n");
5023
		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044
		break;
	case ERROR_INDICATION:
		netdev_dbg(netdev, "Got Error Indication\n");
		handle_error_indication(crq, adapter);
		break;
	case REQUEST_STATISTICS_RSP:
		netdev_dbg(netdev, "Got Statistics Response\n");
		complete(&adapter->stats_done);
		break;
	case QUERY_IP_OFFLOAD_RSP:
		netdev_dbg(netdev, "Got Query IP offload Response\n");
		handle_query_ip_offload_rsp(adapter);
		break;
	case MULTICAST_CTRL_RSP:
		netdev_dbg(netdev, "Got multicast control Response\n");
		break;
	case CONTROL_IP_OFFLOAD_RSP:
		netdev_dbg(netdev, "Got Control IP offload Response\n");
		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
				 sizeof(adapter->ip_offload_ctrl),
				 DMA_TO_DEVICE);
5045
		complete(&adapter->init_done);
5046 5047 5048 5049 5050
		break;
	case COLLECT_FW_TRACE_RSP:
		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
		complete(&adapter->fw_done);
		break;
5051 5052 5053 5054 5055 5056
	case GET_VPD_SIZE_RSP:
		handle_vpd_size_rsp(crq, adapter);
		break;
	case GET_VPD_RSP:
		handle_vpd_rsp(crq, adapter);
		break;
5057 5058 5059 5060
	case QUERY_PHYS_PARMS_RSP:
		adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
		complete(&adapter->fw_done);
		break;
5061 5062 5063 5064 5065 5066 5067 5068 5069
	default:
		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
			   gen_crq->cmd);
	}
}

static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
{
	struct ibmvnic_adapter *adapter = instance;
5070 5071 5072 5073 5074

	tasklet_schedule(&adapter->tasklet);
	return IRQ_HANDLED;
}

5075
static void ibmvnic_tasklet(struct tasklet_struct *t)
5076
{
5077
	struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089
	struct ibmvnic_crq_queue *queue = &adapter->crq;
	union ibmvnic_crq *crq;
	unsigned long flags;
	bool done = false;

	spin_lock_irqsave(&queue->lock, flags);
	while (!done) {
		/* Pull all the valid messages off the CRQ */
		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
			ibmvnic_handle_crq(crq, adapter);
			crq->generic.first = 0;
		}
5090 5091 5092 5093 5094 5095

		/* remain in tasklet until all
		 * capabilities responses are received
		 */
		if (!adapter->wait_capability)
			done = true;
5096
	}
5097 5098 5099 5100 5101
	/* if capabilities CRQ's were sent in this tasklet, the following
	 * tasklet must wait until all responses are received
	 */
	if (atomic_read(&adapter->running_cap_crqs) != 0)
		adapter->wait_capability = true;
5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132
	spin_unlock_irqrestore(&queue->lock, flags);
}

static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
{
	struct vio_dev *vdev = adapter->vdev;
	int rc;

	do {
		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));

	if (rc)
		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);

	return rc;
}

static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
{
	struct ibmvnic_crq_queue *crq = &adapter->crq;
	struct device *dev = &adapter->vdev->dev;
	struct vio_dev *vdev = adapter->vdev;
	int rc;

	/* Close the CRQ */
	do {
		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));

	/* Clean out the queue */
5133 5134 5135
	if (!crq->msgs)
		return -EINVAL;

5136 5137
	memset(crq->msgs, 0, PAGE_SIZE);
	crq->cur = 0;
5138
	crq->active = false;
5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152

	/* And re-open it again */
	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
				crq->msg_token, PAGE_SIZE);

	if (rc == H_CLOSED)
		/* Adapter is good, but other end is not ready */
		dev_warn(dev, "Partner adapter not ready\n");
	else if (rc != 0)
		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);

	return rc;
}

5153
static void release_crq_queue(struct ibmvnic_adapter *adapter)
5154 5155 5156 5157 5158
{
	struct ibmvnic_crq_queue *crq = &adapter->crq;
	struct vio_dev *vdev = adapter->vdev;
	long rc;

5159 5160 5161
	if (!crq->msgs)
		return;

5162 5163
	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
	free_irq(vdev->irq, adapter);
5164
	tasklet_kill(&adapter->tasklet);
5165 5166 5167 5168 5169 5170 5171
	do {
		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));

	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
			 DMA_BIDIRECTIONAL);
	free_page((unsigned long)crq->msgs);
5172
	crq->msgs = NULL;
5173
	crq->active = false;
5174 5175
}

5176
static int init_crq_queue(struct ibmvnic_adapter *adapter)
5177 5178 5179 5180 5181 5182
{
	struct ibmvnic_crq_queue *crq = &adapter->crq;
	struct device *dev = &adapter->vdev->dev;
	struct vio_dev *vdev = adapter->vdev;
	int rc, retrc = -ENOMEM;

5183 5184 5185
	if (crq->msgs)
		return 0;

5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214
	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
	/* Should we allocate more than one page? */

	if (!crq->msgs)
		return -ENOMEM;

	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
					DMA_BIDIRECTIONAL);
	if (dma_mapping_error(dev, crq->msg_token))
		goto map_failed;

	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
				crq->msg_token, PAGE_SIZE);

	if (rc == H_RESOURCE)
		/* maybe kexecing and resource is busy. try a reset */
		rc = ibmvnic_reset_crq(adapter);
	retrc = rc;

	if (rc == H_CLOSED) {
		dev_warn(dev, "Partner adapter not ready\n");
	} else if (rc) {
		dev_warn(dev, "Error %d opening adapter\n", rc);
		goto reg_crq_failed;
	}

	retrc = 0;

5215
	tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
5216

5217
	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
5218 5219 5220
	snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
		 adapter->vdev->unit_address);
	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238
	if (rc) {
		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
			vdev->irq, rc);
		goto req_irq_failed;
	}

	rc = vio_enable_interrupts(vdev);
	if (rc) {
		dev_err(dev, "Error %d enabling interrupts\n", rc);
		goto req_irq_failed;
	}

	crq->cur = 0;
	spin_lock_init(&crq->lock);

	return retrc;

req_irq_failed:
5239
	tasklet_kill(&adapter->tasklet);
5240 5241 5242 5243 5244 5245 5246
	do {
		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
reg_crq_failed:
	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
map_failed:
	free_page((unsigned long)crq->msgs);
5247
	crq->msgs = NULL;
5248 5249 5250
	return retrc;
}

5251
static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
5252
{
5253
	struct device *dev = &adapter->vdev->dev;
5254
	unsigned long timeout = msecs_to_jiffies(20000);
5255
	u64 old_num_rx_queues, old_num_tx_queues;
5256 5257
	int rc;

5258 5259
	adapter->from_passive_init = false;

5260 5261 5262 5263 5264
	if (reset) {
		old_num_rx_queues = adapter->req_rx_queues;
		old_num_tx_queues = adapter->req_tx_queues;
		reinit_completion(&adapter->init_done);
	}
5265

5266
	adapter->init_done_rc = 0;
5267 5268 5269 5270 5271 5272
	rc = ibmvnic_send_crq_init(adapter);
	if (rc) {
		dev_err(dev, "Send crq init failed with error %d\n", rc);
		return rc;
	}

5273 5274
	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
		dev_err(dev, "Initialization sequence timed out\n");
5275 5276 5277
		return -1;
	}

5278 5279 5280 5281 5282
	if (adapter->init_done_rc) {
		release_crq_queue(adapter);
		return adapter->init_done_rc;
	}

5283 5284 5285 5286 5287 5288
	if (adapter->from_passive_init) {
		adapter->state = VNIC_OPEN;
		adapter->from_passive_init = false;
		return -1;
	}

5289 5290
	if (reset &&
	    test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
5291
	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
5292 5293 5294 5295 5296 5297 5298 5299
		if (adapter->req_rx_queues != old_num_rx_queues ||
		    adapter->req_tx_queues != old_num_tx_queues) {
			release_sub_crqs(adapter, 0);
			rc = init_sub_crqs(adapter);
		} else {
			rc = reset_sub_crq_queues(adapter);
		}
	} else {
5300
		rc = init_sub_crqs(adapter);
5301 5302
	}

5303 5304 5305
	if (rc) {
		dev_err(dev, "Initialization of sub crqs failed\n");
		release_crq_queue(adapter);
5306 5307 5308 5309 5310 5311 5312
		return rc;
	}

	rc = init_sub_crq_irqs(adapter);
	if (rc) {
		dev_err(dev, "Failed to initialize sub crq irqs\n");
		release_crq_queue(adapter);
5313 5314 5315
	}

	return rc;
5316 5317
}

5318 5319
static struct device_attribute dev_attr_failover;

5320 5321
static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
{
5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339
	struct ibmvnic_adapter *adapter;
	struct net_device *netdev;
	unsigned char *mac_addr_p;
	int rc;

	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
		dev->unit_address);

	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
							VETH_MAC_ADDR, NULL);
	if (!mac_addr_p) {
		dev_err(&dev->dev,
			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
			__FILE__, __LINE__);
		return 0;
	}

	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
5340
				   IBMVNIC_MAX_QUEUES);
5341 5342 5343 5344
	if (!netdev)
		return -ENOMEM;

	adapter = netdev_priv(netdev);
5345
	adapter->state = VNIC_PROBING;
5346 5347 5348
	dev_set_drvdata(&dev->dev, netdev);
	adapter->vdev = dev;
	adapter->netdev = netdev;
S
Sukadev Bhattiprolu 已提交
5349
	adapter->login_pending = false;
5350 5351 5352 5353 5354 5355 5356 5357 5358 5359

	ether_addr_copy(adapter->mac_addr, mac_addr_p);
	ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
	netdev->irq = dev->irq;
	netdev->netdev_ops = &ibmvnic_netdev_ops;
	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
	SET_NETDEV_DEV(netdev, &dev->dev);

	spin_lock_init(&adapter->stats_lock);

5360
	INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
5361 5362
	INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
			  __ibmvnic_delayed_reset);
5363
	INIT_LIST_HEAD(&adapter->rwi_list);
5364
	spin_lock_init(&adapter->rwi_lock);
5365
	spin_lock_init(&adapter->state_lock);
T
Thomas Falcon 已提交
5366
	mutex_init(&adapter->fw_lock);
5367
	init_completion(&adapter->init_done);
5368 5369 5370
	init_completion(&adapter->fw_done);
	init_completion(&adapter->reset_done);
	init_completion(&adapter->stats_done);
5371
	clear_bit(0, &adapter->resetting);
5372

5373
	do {
5374 5375 5376 5377 5378 5379 5380
		rc = init_crq_queue(adapter);
		if (rc) {
			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
				rc);
			goto ibmvnic_init_fail;
		}

5381
		rc = ibmvnic_reset_init(adapter, false);
5382 5383
		if (rc && rc != EAGAIN)
			goto ibmvnic_init_fail;
5384
	} while (rc == EAGAIN);
5385

5386 5387 5388 5389 5390 5391 5392 5393
	rc = init_stats_buffers(adapter);
	if (rc)
		goto ibmvnic_init_fail;

	rc = init_stats_token(adapter);
	if (rc)
		goto ibmvnic_stats_fail;

5394
	netdev->mtu = adapter->req_mtu - ETH_HLEN;
5395 5396
	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5397

5398
	rc = device_create_file(&dev->dev, &dev_attr_failover);
5399
	if (rc)
5400
		goto ibmvnic_dev_file_err;
5401

M
Mick Tarsel 已提交
5402
	netif_carrier_off(netdev);
5403 5404 5405
	rc = register_netdev(netdev);
	if (rc) {
		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
5406
		goto ibmvnic_register_fail;
5407 5408 5409
	}
	dev_info(&dev->dev, "ibmvnic registered\n");

5410
	adapter->state = VNIC_PROBED;
5411 5412

	adapter->wait_for_reset = false;
5413
	adapter->last_reset_time = jiffies;
5414
	return 0;
5415 5416 5417 5418

ibmvnic_register_fail:
	device_remove_file(&dev->dev, &dev_attr_failover);

5419 5420 5421 5422 5423 5424
ibmvnic_dev_file_err:
	release_stats_token(adapter);

ibmvnic_stats_fail:
	release_stats_buffers(adapter);

5425
ibmvnic_init_fail:
5426
	release_sub_crqs(adapter, 1);
5427
	release_crq_queue(adapter);
T
Thomas Falcon 已提交
5428
	mutex_destroy(&adapter->fw_lock);
5429 5430 5431
	free_netdev(netdev);

	return rc;
5432 5433 5434 5435 5436
}

static int ibmvnic_remove(struct vio_dev *dev)
{
	struct net_device *netdev = dev_get_drvdata(&dev->dev);
5437
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5438 5439 5440
	unsigned long flags;

	spin_lock_irqsave(&adapter->state_lock, flags);
5441
	if (test_bit(0, &adapter->resetting)) {
5442 5443 5444
		spin_unlock_irqrestore(&adapter->state_lock, flags);
		return -EBUSY;
	}
5445

5446
	adapter->state = VNIC_REMOVING;
5447 5448
	spin_unlock_irqrestore(&adapter->state_lock, flags);

5449 5450 5451
	flush_work(&adapter->ibmvnic_reset);
	flush_delayed_work(&adapter->ibmvnic_delayed_reset);

5452 5453
	rtnl_lock();
	unregister_netdevice(netdev);
5454 5455

	release_resources(adapter);
5456
	release_sub_crqs(adapter, 1);
5457 5458
	release_crq_queue(adapter);

5459 5460 5461
	release_stats_token(adapter);
	release_stats_buffers(adapter);

5462 5463
	adapter->state = VNIC_REMOVED;

5464
	rtnl_unlock();
T
Thomas Falcon 已提交
5465
	mutex_destroy(&adapter->fw_lock);
5466
	device_remove_file(&dev->dev, &dev_attr_failover);
5467 5468 5469 5470 5471 5472
	free_netdev(netdev);
	dev_set_drvdata(&dev->dev, NULL);

	return 0;
}

5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
			      const char *buf, size_t count)
{
	struct net_device *netdev = dev_get_drvdata(dev);
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
	__be64 session_token;
	long rc;

	if (!sysfs_streq(buf, "1"))
		return -EINVAL;

	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
			 H_GET_SESSION_TOKEN, 0, 0, 0);
	if (rc) {
		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
			   rc);
		return -EINVAL;
	}

	session_token = (__be64)retbuf[0];
	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
		   be64_to_cpu(session_token));
	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
				H_SESSION_ERR_DETECTED, session_token, 0, 0);
	if (rc) {
		netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
			   rc);
		return -EINVAL;
	}

	return count;
}

J
Joe Perches 已提交
5507
static DEVICE_ATTR_WO(failover);
5508

5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530
static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
{
	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
	struct ibmvnic_adapter *adapter;
	struct iommu_table *tbl;
	unsigned long ret = 0;
	int i;

	tbl = get_iommu_table_base(&vdev->dev);

	/* netdev inits at probe time along with the structures we need below*/
	if (!netdev)
		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);

	adapter = netdev_priv(netdev);

	ret += PAGE_SIZE; /* the crq message queue */
	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);

	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
		ret += 4 * PAGE_SIZE; /* the scrq message queue */

5531
	for (i = 0; i < adapter->num_active_rx_pools; i++)
5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
		ret += adapter->rx_pool[i].size *
		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);

	return ret;
}

static int ibmvnic_resume(struct device *dev)
{
	struct net_device *netdev = dev_get_drvdata(dev);
	struct ibmvnic_adapter *adapter = netdev_priv(netdev);

5543 5544 5545
	if (adapter->state != VNIC_OPEN)
		return 0;

5546
	tasklet_schedule(&adapter->tasklet);
5547 5548 5549 5550

	return 0;
}

5551
static const struct vio_device_id ibmvnic_device_table[] = {
5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585
	{"network", "IBM,vnic"},
	{"", "" }
};
MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);

static const struct dev_pm_ops ibmvnic_pm_ops = {
	.resume = ibmvnic_resume
};

static struct vio_driver ibmvnic_driver = {
	.id_table       = ibmvnic_device_table,
	.probe          = ibmvnic_probe,
	.remove         = ibmvnic_remove,
	.get_desired_dma = ibmvnic_get_desired_dma,
	.name		= ibmvnic_driver_name,
	.pm		= &ibmvnic_pm_ops,
};

/* module functions */
static int __init ibmvnic_module_init(void)
{
	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
		IBMVNIC_DRIVER_VERSION);

	return vio_register_driver(&ibmvnic_driver);
}

static void __exit ibmvnic_module_exit(void)
{
	vio_unregister_driver(&ibmvnic_driver);
}

module_init(ibmvnic_module_init);
module_exit(ibmvnic_module_exit);