ibmveth.c 49.5 KB
Newer Older
Santiago Leon's avatar
Santiago Leon committed
1
/*
2
 * IBM Power Virtual Ethernet Device Driver
Santiago Leon's avatar
Santiago Leon committed
3
 *
4 5 6 7
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Santiago Leon's avatar
Santiago Leon committed
8
 *
9 10 11 12
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Santiago Leon's avatar
Santiago Leon committed
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Santiago Leon's avatar
Santiago Leon committed
16
 *
17 18 19 20 21 22 23
 * Copyright (C) IBM Corporation, 2003, 2010
 *
 * Authors: Dave Larson <larson1@us.ibm.com>
 *	    Santiago Leon <santil@linux.vnet.ibm.com>
 *	    Brian King <brking@linux.vnet.ibm.com>
 *	    Robert Jennings <rcj@linux.vnet.ibm.com>
 *	    Anton Blanchard <anton@au.ibm.com>
Santiago Leon's avatar
Santiago Leon committed
24
 */
Linus Torvalds's avatar
Linus Torvalds committed
25 26

#include <linux/module.h>
27
#include <linux/moduleparam.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31 32 33 34 35
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/dma-mapping.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>
36
#include <linux/interrupt.h>
Linus Torvalds's avatar
Linus Torvalds committed
37
#include <linux/mm.h>
38
#include <linux/pm.h>
Linus Torvalds's avatar
Linus Torvalds committed
39
#include <linux/ethtool.h>
40 41
#include <linux/in.h>
#include <linux/ip.h>
42
#include <linux/ipv6.h>
43
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
44
#include <asm/hvcall.h>
Arun Sharma's avatar
Arun Sharma committed
45
#include <linux/atomic.h>
Linus Torvalds's avatar
Linus Torvalds committed
46
#include <asm/vio.h>
47 48
#include <asm/iommu.h>
#include <asm/firmware.h>
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51

#include "ibmveth.h"

52
static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
53
static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
54
static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
55

56
static struct kobj_type ktype_veth_pool;
Linus Torvalds's avatar
Linus Torvalds committed
57

58

Linus Torvalds's avatar
Linus Torvalds committed
59
static const char ibmveth_driver_name[] = "ibmveth";
60
static const char ibmveth_driver_string[] = "IBM Power Virtual Ethernet Driver";
61
#define ibmveth_driver_version "1.06"
Linus Torvalds's avatar
Linus Torvalds committed
62

63 64
MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
Linus Torvalds's avatar
Linus Torvalds committed
65 66 67
MODULE_LICENSE("GPL");
MODULE_VERSION(ibmveth_driver_version);

Santiago Leon's avatar
Santiago Leon committed
68 69 70 71 72
static unsigned int tx_copybreak __read_mostly = 128;
module_param(tx_copybreak, uint, 0644);
MODULE_PARM_DESC(tx_copybreak,
	"Maximum size of packet that is copied to a new buffer on transmit");

Santiago Leon's avatar
Santiago Leon committed
73 74 75 76 77
static unsigned int rx_copybreak __read_mostly = 128;
module_param(rx_copybreak, uint, 0644);
MODULE_PARM_DESC(rx_copybreak,
	"Maximum size of packet that is copied to a new buffer on receive");

78 79 80 81
static unsigned int rx_flush __read_mostly = 0;
module_param(rx_flush, uint, 0644);
MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");

Thomas Falcon's avatar
Thomas Falcon committed
82 83 84 85 86
static bool old_large_send __read_mostly;
module_param(old_large_send, bool, S_IRUGO);
MODULE_PARM_DESC(old_large_send,
	"Use old large send method on firmware that supports the new method");

87 88 89 90 91 92 93 94 95 96 97
struct ibmveth_stat {
	char name[ETH_GSTRING_LEN];
	int offset;
};

#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))

struct ibmveth_stat ibmveth_stats[] = {
	{ "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
	{ "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
Santiago Leon's avatar
Santiago Leon committed
98 99 100 101
	{ "replenish_add_buff_failure",
			IBMVETH_STAT_OFF(replenish_add_buff_failure) },
	{ "replenish_add_buff_success",
			IBMVETH_STAT_OFF(replenish_add_buff_success) },
102 103 104 105
	{ "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
	{ "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
	{ "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
	{ "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
106 107
	{ "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
	{ "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
Thomas Falcon's avatar
Thomas Falcon committed
108
	{ "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets) },
Thomas Falcon's avatar
Thomas Falcon committed
109 110
	{ "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets) },
	{ "fw_enabled_large_send", IBMVETH_STAT_OFF(fw_large_send_support) }
111 112
};

Linus Torvalds's avatar
Linus Torvalds committed
113
/* simple methods of getting data from the current rxq entry */
Brian King's avatar
Brian King committed
114 115
static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
{
116
	return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
Brian King's avatar
Brian King committed
117 118 119 120
}

static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
{
Santiago Leon's avatar
Santiago Leon committed
121 122
	return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
			IBMVETH_RXQ_TOGGLE_SHIFT;
Brian King's avatar
Brian King committed
123 124
}

Linus Torvalds's avatar
Linus Torvalds committed
125 126
static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
{
Santiago Leon's avatar
Santiago Leon committed
127
	return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131
}

static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
{
Santiago Leon's avatar
Santiago Leon committed
132
	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
Linus Torvalds's avatar
Linus Torvalds committed
133 134 135 136
}

static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
{
Santiago Leon's avatar
Santiago Leon committed
137
	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
138 139
}

140 141 142 143 144
static inline int ibmveth_rxq_large_packet(struct ibmveth_adapter *adapter)
{
	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_LRG_PKT;
}

Linus Torvalds's avatar
Linus Torvalds committed
145 146
static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
{
147
	return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
Linus Torvalds's avatar
Linus Torvalds committed
148 149
}

150 151
static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
{
Santiago Leon's avatar
Santiago Leon committed
152
	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
153 154
}

Linus Torvalds's avatar
Linus Torvalds committed
155
/* setup the initial settings for a buffer pool */
Santiago Leon's avatar
Santiago Leon committed
156 157 158
static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
				     u32 pool_index, u32 pool_size,
				     u32 buff_size, u32 pool_active)
Linus Torvalds's avatar
Linus Torvalds committed
159 160 161 162
{
	pool->size = pool_size;
	pool->index = pool_index;
	pool->buff_size = buff_size;
163
	pool->threshold = pool_size * 7 / 8;
164
	pool->active = pool_active;
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169 170 171
}

/* allocate and setup an buffer pool - called during open */
static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
{
	int i;

172
	pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
173

Santiago Leon's avatar
Santiago Leon committed
174
	if (!pool->free_map)
Linus Torvalds's avatar
Linus Torvalds committed
175 176
		return -1;

177
	pool->dma_addr = kcalloc(pool->size, sizeof(dma_addr_t), GFP_KERNEL);
Santiago Leon's avatar
Santiago Leon committed
178
	if (!pool->dma_addr) {
Linus Torvalds's avatar
Linus Torvalds committed
179 180 181 182 183
		kfree(pool->free_map);
		pool->free_map = NULL;
		return -1;
	}

184
	pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
185

Santiago Leon's avatar
Santiago Leon committed
186
	if (!pool->skbuff) {
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190 191 192 193 194
		kfree(pool->dma_addr);
		pool->dma_addr = NULL;

		kfree(pool->free_map);
		pool->free_map = NULL;
		return -1;
	}

Santiago Leon's avatar
Santiago Leon committed
195
	for (i = 0; i < pool->size; ++i)
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200 201 202 203 204
		pool->free_map[i] = i;

	atomic_set(&pool->available, 0);
	pool->producer_index = 0;
	pool->consumer_index = 0;

	return 0;
}

205 206 207 208 209 210 211 212
static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
{
	unsigned long offset;

	for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
		asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
}

Linus Torvalds's avatar
Linus Torvalds committed
213 214 215
/* replenish the buffers for a pool.  note that we don't need to
 * skb_reserve these since they are used for incoming...
 */
Santiago Leon's avatar
Santiago Leon committed
216 217
static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
					  struct ibmveth_buff_pool *pool)
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221
{
	u32 i;
	u32 count = pool->size - atomic_read(&pool->available);
	u32 buffers_added = 0;
222 223 224 225 226
	struct sk_buff *skb;
	unsigned int free_index, index;
	u64 correlator;
	unsigned long lpar_rc;
	dma_addr_t dma_addr;
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229

	mb();

Santiago Leon's avatar
Santiago Leon committed
230
	for (i = 0; i < count; ++i) {
Linus Torvalds's avatar
Linus Torvalds committed
231 232
		union ibmveth_buf_desc desc;

233
		skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Linus Torvalds's avatar
Linus Torvalds committed
234

Santiago Leon's avatar
Santiago Leon committed
235
		if (!skb) {
236 237
			netdev_dbg(adapter->netdev,
				   "replenish: unable to allocate skb\n");
Linus Torvalds's avatar
Linus Torvalds committed
238 239 240 241
			adapter->replenish_no_mem++;
			break;
		}

242
		free_index = pool->consumer_index;
243 244 245
		pool->consumer_index++;
		if (pool->consumer_index >= pool->size)
			pool->consumer_index = 0;
Linus Torvalds's avatar
Linus Torvalds committed
246
		index = pool->free_map[free_index];
247

248 249
		BUG_ON(index == IBM_VETH_INVALID_MAP);
		BUG_ON(pool->skbuff[index] != NULL);
Linus Torvalds's avatar
Linus Torvalds committed
250 251 252 253

		dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
				pool->buff_size, DMA_FROM_DEVICE);

254
		if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
255 256
			goto failure;

Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261
		pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
		pool->dma_addr[index] = dma_addr;
		pool->skbuff[index] = skb;

		correlator = ((u64)pool->index << 32) | index;
Santiago Leon's avatar
Santiago Leon committed
262
		*(u64 *)skb->data = correlator;
Linus Torvalds's avatar
Linus Torvalds committed
263

Brian King's avatar
Brian King committed
264
		desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
265
		desc.fields.address = dma_addr;
Linus Torvalds's avatar
Linus Torvalds committed
266

267 268 269 270 271 272
		if (rx_flush) {
			unsigned int len = min(pool->buff_size,
						adapter->netdev->mtu +
						IBMVETH_BUFF_OH);
			ibmveth_flush_buffer(skb->data, len);
		}
Santiago Leon's avatar
Santiago Leon committed
273 274
		lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
						   desc.desc);
275

Santiago Leon's avatar
Santiago Leon committed
276
		if (lpar_rc != H_SUCCESS) {
277
			goto failure;
Santiago Leon's avatar
Santiago Leon committed
278
		} else {
Linus Torvalds's avatar
Linus Torvalds committed
279 280 281 282
			buffers_added++;
			adapter->replenish_add_buff_success++;
		}
	}
283

284 285 286 287 288 289 290 291 292 293 294
	mb();
	atomic_add(buffers_added, &(pool->available));
	return;

failure:
	pool->free_map[free_index] = index;
	pool->skbuff[index] = NULL;
	if (pool->consumer_index == 0)
		pool->consumer_index = pool->size - 1;
	else
		pool->consumer_index--;
295
	if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
296 297 298 299 300 301
		dma_unmap_single(&adapter->vdev->dev,
		                 pool->dma_addr[index], pool->buff_size,
		                 DMA_FROM_DEVICE);
	dev_kfree_skb_any(skb);
	adapter->replenish_add_buff_failure++;

Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305
	mb();
	atomic_add(buffers_added, &(pool->available));
}

306 307 308 309 310 311 312 313 314 315 316 317
/*
 * The final 8 bytes of the buffer list is a counter of frames dropped
 * because there was not a buffer in the buffer list capable of holding
 * the frame.
 */
static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
{
	__be64 *p = adapter->buffer_list_addr + 4096 - 8;

	adapter->rx_no_buffer = be64_to_cpup(p);
}

318
/* replenish routine */
319
static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds's avatar
Linus Torvalds committed
320
{
321 322
	int i;

Linus Torvalds's avatar
Linus Torvalds committed
323 324
	adapter->replenish_task_cycles++;

Santiago Leon's avatar
Santiago Leon committed
325
	for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
326 327 328 329 330 331
		struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];

		if (pool->active &&
		    (atomic_read(&pool->available) < pool->threshold))
			ibmveth_replenish_buffer_pool(adapter, pool);
	}
Linus Torvalds's avatar
Linus Torvalds committed
332

333
	ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds's avatar
Linus Torvalds committed
334 335 336
}

/* empty and free ana buffer pool - also used to do cleanup in error paths */
Santiago Leon's avatar
Santiago Leon committed
337 338
static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
				     struct ibmveth_buff_pool *pool)
Linus Torvalds's avatar
Linus Torvalds committed
339 340 341
{
	int i;

342 343
	kfree(pool->free_map);
	pool->free_map = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
344

Santiago Leon's avatar
Santiago Leon committed
345 346
	if (pool->skbuff && pool->dma_addr) {
		for (i = 0; i < pool->size; ++i) {
Linus Torvalds's avatar
Linus Torvalds committed
347
			struct sk_buff *skb = pool->skbuff[i];
Santiago Leon's avatar
Santiago Leon committed
348
			if (skb) {
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352 353 354 355 356 357 358
				dma_unmap_single(&adapter->vdev->dev,
						 pool->dma_addr[i],
						 pool->buff_size,
						 DMA_FROM_DEVICE);
				dev_kfree_skb_any(skb);
				pool->skbuff[i] = NULL;
			}
		}
	}

Santiago Leon's avatar
Santiago Leon committed
359
	if (pool->dma_addr) {
Linus Torvalds's avatar
Linus Torvalds committed
360 361 362 363
		kfree(pool->dma_addr);
		pool->dma_addr = NULL;
	}

Santiago Leon's avatar
Santiago Leon committed
364
	if (pool->skbuff) {
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367 368 369 370
		kfree(pool->skbuff);
		pool->skbuff = NULL;
	}
}

/* remove a buffer from a pool */
Santiago Leon's avatar
Santiago Leon committed
371 372
static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
					    u64 correlator)
Linus Torvalds's avatar
Linus Torvalds committed
373 374 375 376 377 378
{
	unsigned int pool  = correlator >> 32;
	unsigned int index = correlator & 0xffffffffUL;
	unsigned int free_index;
	struct sk_buff *skb;

379 380
	BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
	BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383

	skb = adapter->rx_buff_pool[pool].skbuff[index];

384
	BUG_ON(skb == NULL);
Linus Torvalds's avatar
Linus Torvalds committed
385 386 387 388 389 390 391 392

	adapter->rx_buff_pool[pool].skbuff[index] = NULL;

	dma_unmap_single(&adapter->vdev->dev,
			 adapter->rx_buff_pool[pool].dma_addr[index],
			 adapter->rx_buff_pool[pool].buff_size,
			 DMA_FROM_DEVICE);

393
	free_index = adapter->rx_buff_pool[pool].producer_index;
394 395 396 397
	adapter->rx_buff_pool[pool].producer_index++;
	if (adapter->rx_buff_pool[pool].producer_index >=
	    adapter->rx_buff_pool[pool].size)
		adapter->rx_buff_pool[pool].producer_index = 0;
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411
	adapter->rx_buff_pool[pool].free_map[free_index] = index;

	mb();

	atomic_dec(&(adapter->rx_buff_pool[pool].available));
}

/* get the current buffer on the rx queue */
static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
{
	u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
	unsigned int pool = correlator >> 32;
	unsigned int index = correlator & 0xffffffffUL;

412 413
	BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
	BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds's avatar
Linus Torvalds committed
414 415 416 417 418

	return adapter->rx_buff_pool[pool].skbuff[index];
}

/* recycle the current buffer on the rx queue */
419
static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds's avatar
Linus Torvalds committed
420 421 422 423 424 425 426
{
	u32 q_index = adapter->rx_queue.index;
	u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
	unsigned int pool = correlator >> 32;
	unsigned int index = correlator & 0xffffffffUL;
	union ibmveth_buf_desc desc;
	unsigned long lpar_rc;
427
	int ret = 1;
Linus Torvalds's avatar
Linus Torvalds committed
428

429 430
	BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
	BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds's avatar
Linus Torvalds committed
431

Santiago Leon's avatar
Santiago Leon committed
432
	if (!adapter->rx_buff_pool[pool].active) {
433 434
		ibmveth_rxq_harvest_buffer(adapter);
		ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
435
		goto out;
436 437
	}

Brian King's avatar
Brian King committed
438 439
	desc.fields.flags_len = IBMVETH_BUF_VALID |
		adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds's avatar
Linus Torvalds committed
440 441 442
	desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];

	lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
443

Santiago Leon's avatar
Santiago Leon committed
444
	if (lpar_rc != H_SUCCESS) {
445 446
		netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
			   "during recycle rc=%ld", lpar_rc);
Linus Torvalds's avatar
Linus Torvalds committed
447
		ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
448
		ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
449 450
	}

Santiago Leon's avatar
Santiago Leon committed
451
	if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds's avatar
Linus Torvalds committed
452 453 454
		adapter->rx_queue.index = 0;
		adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
	}
455 456 457

out:
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
458 459
}

460
static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463
{
	ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);

Santiago Leon's avatar
Santiago Leon committed
464
	if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds's avatar
Linus Torvalds committed
465 466 467 468 469 470 471
		adapter->rx_queue.index = 0;
		adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
	}
}

static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
{
472
	int i;
473
	struct device *dev = &adapter->vdev->dev;
474

Santiago Leon's avatar
Santiago Leon committed
475
	if (adapter->buffer_list_addr != NULL) {
476 477
		if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
			dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
Linus Torvalds's avatar
Linus Torvalds committed
478 479 480 481 482
					DMA_BIDIRECTIONAL);
			adapter->buffer_list_dma = DMA_ERROR_CODE;
		}
		free_page((unsigned long)adapter->buffer_list_addr);
		adapter->buffer_list_addr = NULL;
483
	}
Linus Torvalds's avatar
Linus Torvalds committed
484

Santiago Leon's avatar
Santiago Leon committed
485
	if (adapter->filter_list_addr != NULL) {
486 487
		if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
			dma_unmap_single(dev, adapter->filter_list_dma, 4096,
Linus Torvalds's avatar
Linus Torvalds committed
488 489 490 491 492 493 494
					DMA_BIDIRECTIONAL);
			adapter->filter_list_dma = DMA_ERROR_CODE;
		}
		free_page((unsigned long)adapter->filter_list_addr);
		adapter->filter_list_addr = NULL;
	}

Santiago Leon's avatar
Santiago Leon committed
495
	if (adapter->rx_queue.queue_addr != NULL) {
496 497 498
		dma_free_coherent(dev, adapter->rx_queue.queue_len,
				  adapter->rx_queue.queue_addr,
				  adapter->rx_queue.queue_dma);
Linus Torvalds's avatar
Linus Torvalds committed
499 500 501
		adapter->rx_queue.queue_addr = NULL;
	}

Santiago Leon's avatar
Santiago Leon committed
502
	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
503
		if (adapter->rx_buff_pool[i].active)
504
			ibmveth_free_buffer_pool(adapter,
505
						 &adapter->rx_buff_pool[i]);
506 507

	if (adapter->bounce_buffer != NULL) {
508
		if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
509 510 511 512 513 514 515 516 517
			dma_unmap_single(&adapter->vdev->dev,
					adapter->bounce_buffer_dma,
					adapter->netdev->mtu + IBMVETH_BUFF_OH,
					DMA_BIDIRECTIONAL);
			adapter->bounce_buffer_dma = DMA_ERROR_CODE;
		}
		kfree(adapter->bounce_buffer);
		adapter->bounce_buffer = NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
518 519
}

520 521 522 523 524
static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
        union ibmveth_buf_desc rxq_desc, u64 mac_address)
{
	int rc, try_again = 1;

Santiago Leon's avatar
Santiago Leon committed
525 526 527 528 529
	/*
	 * After a kexec the adapter will still be open, so our attempt to
	 * open it will fail. So if we get a failure we free the adapter and
	 * try again, but only once.
	 */
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
retry:
	rc = h_register_logical_lan(adapter->vdev->unit_address,
				    adapter->buffer_list_dma, rxq_desc.desc,
				    adapter->filter_list_dma, mac_address);

	if (rc != H_SUCCESS && try_again) {
		do {
			rc = h_free_logical_lan(adapter->vdev->unit_address);
		} while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));

		try_again = 0;
		goto retry;
	}

	return rc;
}

547 548 549 550 551 552 553 554 555 556 557
static u64 ibmveth_encode_mac_addr(u8 *mac)
{
	int i;
	u64 encoded = 0;

	for (i = 0; i < ETH_ALEN; i++)
		encoded = (encoded << 8) | mac[i];

	return encoded;
}

Linus Torvalds's avatar
Linus Torvalds committed
558 559
static int ibmveth_open(struct net_device *netdev)
{
560
	struct ibmveth_adapter *adapter = netdev_priv(netdev);
561
	u64 mac_address;
562
	int rxq_entries = 1;
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565
	unsigned long lpar_rc;
	int rc;
	union ibmveth_buf_desc rxq_desc;
566
	int i;
567
	struct device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
568

569
	netdev_dbg(netdev, "open starting\n");
Linus Torvalds's avatar
Linus Torvalds committed
570

571 572
	napi_enable(&adapter->napi);

Santiago Leon's avatar
Santiago Leon committed
573
	for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
574
		rxq_entries += adapter->rx_buff_pool[i].size;
575

Linus Torvalds's avatar
Linus Torvalds committed
576 577
	adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
	adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
578

Santiago Leon's avatar
Santiago Leon committed
579
	if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
580 581
		netdev_err(netdev, "unable to allocate filter or buffer list "
			   "pages\n");
582 583
		rc = -ENOMEM;
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
584 585
	}

586 587
	dev = &adapter->vdev->dev;

Santiago Leon's avatar
Santiago Leon committed
588 589
	adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
						rxq_entries;
590
	adapter->rx_queue.queue_addr =
591 592
		dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
				   &adapter->rx_queue.queue_dma, GFP_KERNEL);
Santiago Leon's avatar
Santiago Leon committed
593
	if (!adapter->rx_queue.queue_addr) {
594 595
		rc = -ENOMEM;
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
596 597
	}

598
	adapter->buffer_list_dma = dma_map_single(dev,
Linus Torvalds's avatar
Linus Torvalds committed
599
			adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
600
	adapter->filter_list_dma = dma_map_single(dev,
Linus Torvalds's avatar
Linus Torvalds committed
601 602
			adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);

603
	if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
604
	    (dma_mapping_error(dev, adapter->filter_list_dma))) {
605 606
		netdev_err(netdev, "unable to map filter or buffer list "
			   "pages\n");
607 608
		rc = -ENOMEM;
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
609 610 611 612 613 614
	}

	adapter->rx_queue.index = 0;
	adapter->rx_queue.num_slots = rxq_entries;
	adapter->rx_queue.toggle = 1;

615
	mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
Linus Torvalds's avatar
Linus Torvalds committed
616

Santiago Leon's avatar
Santiago Leon committed
617 618
	rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
					adapter->rx_queue.queue_len;
Linus Torvalds's avatar
Linus Torvalds committed
619 620
	rxq_desc.fields.address = adapter->rx_queue.queue_dma;

621 622 623
	netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
	netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
	netdev_dbg(netdev, "receive q   @ 0x%p\n", adapter->rx_queue.queue_addr);
Linus Torvalds's avatar
Linus Torvalds committed
624

625 626
	h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);

627
	lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds's avatar
Linus Torvalds committed
628

Santiago Leon's avatar
Santiago Leon committed
629
	if (lpar_rc != H_SUCCESS) {
630 631 632 633
		netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
			   lpar_rc);
		netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
			   "desc:0x%llx MAC:0x%llx\n",
Linus Torvalds's avatar
Linus Torvalds committed
634 635 636 637
				     adapter->buffer_list_dma,
				     adapter->filter_list_dma,
				     rxq_desc.desc,
				     mac_address);
638 639
		rc = -ENONET;
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
640 641
	}

Santiago Leon's avatar
Santiago Leon committed
642 643
	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
		if (!adapter->rx_buff_pool[i].active)
644 645
			continue;
		if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
646
			netdev_err(netdev, "unable to alloc pool\n");
647
			adapter->rx_buff_pool[i].active = 0;
648 649
			rc = -ENOMEM;
			goto err_out;
650 651 652
		}
	}

653
	netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
Santiago Leon's avatar
Santiago Leon committed
654 655 656
	rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
			 netdev);
	if (rc != 0) {
657 658
		netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
			   netdev->irq, rc);
Linus Torvalds's avatar
Linus Torvalds committed
659
		do {
660 661
			lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
		} while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds's avatar
Linus Torvalds committed
662

663
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
664 665
	}

666 667 668
	adapter->bounce_buffer =
	    kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
	if (!adapter->bounce_buffer) {
669
		rc = -ENOMEM;
670
		goto err_out_free_irq;
671 672 673 674
	}
	adapter->bounce_buffer_dma =
	    dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
			   netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
675
	if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
676
		netdev_err(netdev, "unable to map bounce buffer\n");
677
		rc = -ENOMEM;
678
		goto err_out_free_irq;
679 680
	}

681
	netdev_dbg(netdev, "initial replenish cycle\n");
682
	ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds's avatar
Linus Torvalds committed
683

684
	netif_start_queue(netdev);
Linus Torvalds's avatar
Linus Torvalds committed
685

686
	netdev_dbg(netdev, "open complete\n");
Linus Torvalds's avatar
Linus Torvalds committed
687 688

	return 0;
689

690 691
err_out_free_irq:
	free_irq(netdev->irq, netdev);
692 693 694 695
err_out:
	ibmveth_cleanup(adapter);
	napi_disable(&adapter->napi);
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698 699
}

static int ibmveth_close(struct net_device *netdev)
{
700
	struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds's avatar
Linus Torvalds committed
701
	long lpar_rc;
702

703
	netdev_dbg(netdev, "close starting\n");
Linus Torvalds's avatar
Linus Torvalds committed
704

705 706
	napi_disable(&adapter->napi);

707 708
	if (!adapter->pool_config)
		netif_stop_queue(netdev);
Linus Torvalds's avatar
Linus Torvalds committed
709

710
	h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Linus Torvalds's avatar
Linus Torvalds committed
711 712 713

	do {
		lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
714
	} while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds's avatar
Linus Torvalds committed
715

Santiago Leon's avatar
Santiago Leon committed
716
	if (lpar_rc != H_SUCCESS) {
717 718
		netdev_err(netdev, "h_free_logical_lan failed with %lx, "
			   "continuing with close\n", lpar_rc);
Linus Torvalds's avatar
Linus Torvalds committed
719 720
	}

721 722
	free_irq(netdev->irq, netdev);

723
	ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds's avatar
Linus Torvalds committed
724 725 726

	ibmveth_cleanup(adapter);

727
	netdev_dbg(netdev, "close complete\n");
Linus Torvalds's avatar
Linus Torvalds committed
728 729 730 731

	return 0;
}

Santiago Leon's avatar
Santiago Leon committed
732 733 734 735 736 737
static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
				SUPPORTED_FIBRE);
	cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
				ADVERTISED_FIBRE);
738
	ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds's avatar
Linus Torvalds committed
739 740 741 742 743 744 745 746 747 748
	cmd->duplex = DUPLEX_FULL;
	cmd->port = PORT_FIBRE;
	cmd->phy_address = 0;
	cmd->transceiver = XCVR_INTERNAL;
	cmd->autoneg = AUTONEG_ENABLE;
	cmd->maxtxpkt = 0;
	cmd->maxrxpkt = 1;
	return 0;
}

Santiago Leon's avatar
Santiago Leon committed
749 750 751
static void netdev_get_drvinfo(struct net_device *dev,
			       struct ethtool_drvinfo *info)
{
752 753
	strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
	strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
Linus Torvalds's avatar
Linus Torvalds committed
754 755
}

756 757
static netdev_features_t ibmveth_fix_features(struct net_device *dev,
	netdev_features_t features)
758
{
759 760 761 762 763 764 765 766
	/*
	 * Since the ibmveth firmware interface does not have the
	 * concept of separate tx/rx checksum offload enable, if rx
	 * checksum is disabled we also have to disable tx checksum
	 * offload. Once we disable rx checksum offload, we are no
	 * longer allowed to send tx buffers that are not properly
	 * checksummed.
	 */
767

768
	if (!(features & NETIF_F_RXCSUM))
769
		features &= ~NETIF_F_CSUM_MASK;
770

771
	return features;
772 773
}

774
static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
775
{
776
	struct ibmveth_adapter *adapter = netdev_priv(dev);
777
	unsigned long set_attr, clr_attr, ret_attr;
778
	unsigned long set_attr6, clr_attr6;
779
	long ret, ret4, ret6;
780 781 782 783 784 785 786 787 788 789
	int rc1 = 0, rc2 = 0;
	int restart = 0;

	if (netif_running(dev)) {
		restart = 1;
		adapter->pool_config = 1;
		ibmveth_close(dev);
		adapter->pool_config = 0;
	}

Brian King's avatar
Brian King committed
790 791
	set_attr = 0;
	clr_attr = 0;
792 793
	set_attr6 = 0;
	clr_attr6 = 0;
794

795
	if (data) {
Brian King's avatar
Brian King committed
796
		set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
797 798
		set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
	} else {
Brian King's avatar
Brian King committed
799
		clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
800 801
		clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
	}
802

Brian King's avatar
Brian King committed
803
	ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
804

Brian King's avatar
Brian King committed
805 806 807
	if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
	    !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
	    (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
808
		ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
Brian King's avatar
Brian King committed
809
					 set_attr, &ret_attr);
810

811
		if (ret4 != H_SUCCESS) {
812 813
			netdev_err(dev, "unable to change IPv4 checksum "
					"offload settings. %d rc=%ld\n",
814 815 816 817 818 819 820
					data, ret4);

			h_illan_attributes(adapter->vdev->unit_address,
					   set_attr, clr_attr, &ret_attr);

			if (data == 1)
				dev->features &= ~NETIF_F_IP_CSUM;
821

Santiago Leon's avatar
Santiago Leon committed
822
		} else {
823
			adapter->fw_ipv4_csum_support = data;
Santiago Leon's avatar
Santiago Leon committed
824
		}
825 826 827 828 829

		ret6 = h_illan_attributes(adapter->vdev->unit_address,
					 clr_attr6, set_attr6, &ret_attr);

		if (ret6 != H_SUCCESS) {
830 831
			netdev_err(dev, "unable to change IPv6 checksum "
					"offload settings. %d rc=%ld\n",
832 833 834 835 836 837 838
					data, ret6);

			h_illan_attributes(adapter->vdev->unit_address,
					   set_attr6, clr_attr6, &ret_attr);

			if (data == 1)
				dev->features &= ~NETIF_F_IPV6_CSUM;
839 840 841 842

		} else
			adapter->fw_ipv6_csum_support = data;

843
		if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
844
			adapter->rx_csum = data;
845 846
		else
			rc1 = -EIO;
847 848
	} else {
		rc1 = -EIO;
849 850 851
		netdev_err(dev, "unable to change checksum offload settings."
				     " %d rc=%ld ret_attr=%lx\n", data, ret,
				     ret_attr);
852 853 854 855 856 857 858 859
	}

	if (restart)
		rc2 = ibmveth_open(dev);

	return rc1 ? rc1 : rc2;
}

Thomas Falcon's avatar
Thomas Falcon committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
static int ibmveth_set_tso(struct net_device *dev, u32 data)
{
	struct ibmveth_adapter *adapter = netdev_priv(dev);
	unsigned long set_attr, clr_attr, ret_attr;
	long ret1, ret2;
	int rc1 = 0, rc2 = 0;
	int restart = 0;

	if (netif_running(dev)) {
		restart = 1;
		adapter->pool_config = 1;
		ibmveth_close(dev);
		adapter->pool_config = 0;
	}

	set_attr = 0;
	clr_attr = 0;

	if (data)
		set_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
	else
		clr_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;

	ret1 = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);

	if (ret1 == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
	    !old_large_send) {
		ret2 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
					  set_attr, &ret_attr);

		if (ret2 != H_SUCCESS) {
			netdev_err(dev, "unable to change tso settings. %d rc=%ld\n",
				   data, ret2);

			h_illan_attributes(adapter->vdev->unit_address,
					   set_attr, clr_attr, &ret_attr);

			if (data == 1)
				dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
			rc1 = -EIO;

		} else {
			adapter->fw_large_send_support = data;
			adapter->large_send = data;
		}
	} else {
		/* Older firmware version of large send offload does not
		 * support tcp6/ipv6
		 */
		if (data == 1) {
			dev->features &= ~NETIF_F_TSO6;
			netdev_info(dev, "TSO feature requires all partitions to have updated driver");
		}
		adapter->large_send = data;
	}

	if (restart)
		rc2 = ibmveth_open(dev);

	return rc1 ? rc1 : rc2;
}

922 923
static int ibmveth_set_features(struct net_device *dev,
	netdev_features_t features)
924
{
925
	struct ibmveth_adapter *adapter = netdev_priv(dev);
926
	int rx_csum = !!(features & NETIF_F_RXCSUM);
Thomas Falcon's avatar
Thomas Falcon committed
927 928
	int large_send = !!(features & (NETIF_F_TSO | NETIF_F_TSO6));
	int rc1 = 0, rc2 = 0;
929

Thomas Falcon's avatar
Thomas Falcon committed
930 931 932 933
	if (rx_csum != adapter->rx_csum) {
		rc1 = ibmveth_set_csum_offload(dev, rx_csum);
		if (rc1 && !adapter->rx_csum)
			dev->features =
934 935
				features & ~(NETIF_F_CSUM_MASK |
					     NETIF_F_RXCSUM);
Thomas Falcon's avatar
Thomas Falcon committed
936
	}
937

Thomas Falcon's avatar
Thomas Falcon committed
938 939 940 941 942 943
	if (large_send != adapter->large_send) {
		rc2 = ibmveth_set_tso(dev, large_send);
		if (rc2 && !adapter->large_send)
			dev->features =
				features & ~(NETIF_F_TSO | NETIF_F_TSO6);
	}
944

Thomas Falcon's avatar
Thomas Falcon committed
945
	return rc1 ? rc1 : rc2;
946 947
}

948 949 950 951 952 953 954 955 956 957 958
static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
{
	int i;

	if (stringset != ETH_SS_STATS)
		return;

	for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
		memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
}

959
static int ibmveth_get_sset_count(struct net_device *dev, int sset)
960
{
961 962 963 964 965 966
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(ibmveth_stats);
	default:
		return -EOPNOTSUPP;
	}
967 968 969 970 971 972
}

static void ibmveth_get_ethtool_stats(struct net_device *dev,
				      struct ethtool_stats *stats, u64 *data)
{
	int i;
973
	struct ibmveth_adapter *adapter = netdev_priv(dev);
974 975 976 977 978

	for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
		data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
}

979
static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
980 981
	.get_drvinfo		= netdev_get_drvinfo,
	.get_settings		= netdev_get_settings,
982
	.get_link		= ethtool_op_get_link,
983
	.get_strings		= ibmveth_get_strings,
984
	.get_sset_count		= ibmveth_get_sset_count,
985
	.get_ethtool_stats	= ibmveth_get_ethtool_stats,
Linus Torvalds's avatar
Linus Torvalds committed
986 987 988 989 990 991 992 993 994
};

static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	return -EOPNOTSUPP;
}

#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))

995
static int ibmveth_send(struct ibmveth_adapter *adapter,
Thomas Falcon's avatar
Thomas Falcon committed
996
			union ibmveth_buf_desc *descs, unsigned long mss)
Linus Torvalds's avatar
Linus Torvalds committed
997 998 999
{
	unsigned long correlator;
	unsigned int retry_count;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	unsigned long ret;

	/*
	 * The retry count sets a maximum for the number of broadcast and
	 * multicast destinations within the system.
	 */
	retry_count = 1024;
	correlator = 0;
	do {
		ret = h_send_logical_lan(adapter->vdev->unit_address,
					     descs[0].desc, descs[1].desc,
					     descs[2].desc, descs[3].desc,
					     descs[4].desc, descs[5].desc,
Thomas Falcon's avatar
Thomas Falcon committed
1013 1014
					     correlator, &correlator, mss,
					     adapter->fw_large_send_support);
1015 1016 1017
	} while ((ret == H_BUSY) && (retry_count--));

	if (ret != H_SUCCESS && ret != H_DROPPED) {
1018 1019
		netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
			   "with rc=%ld\n", ret);
1020 1021 1022 1023 1024
		return 1;
	}

	return 0;
}
Santiago Leon's avatar
Santiago Leon committed
1025

1026 1027 1028 1029 1030 1031 1032 1033
static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
				      struct net_device *netdev)
{
	struct ibmveth_adapter *adapter = netdev_priv(netdev);
	unsigned int desc_flags;
	union ibmveth_buf_desc descs[6];
	int last, i;
	int force_bounce = 0;
1034
	dma_addr_t dma_addr;
Thomas Falcon's avatar
Thomas Falcon committed
1035
	unsigned long mss = 0;
1036 1037 1038 1039 1040 1041 1042 1043 1044

	/*
	 * veth handles a maximum of 6 segments including the header, so
	 * we have to linearize the skb if there are more than this.
	 */
	if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
		netdev->stats.tx_dropped++;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1045

1046
	/* veth can't checksum offload UDP */
Brian King's avatar