Linux Audio

Check our new training course

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