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