xref: /openbmc/linux/drivers/net/ethernet/ibm/ibmveth.c (revision dfd4f649)
1 /*
2  * IBM Power Virtual Ethernet Device Driver
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
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/>.
16  *
17  * Copyright (C) IBM Corporation, 2003, 2010
18  *
19  * Authors: Dave Larson <larson1@us.ibm.com>
20  *	    Santiago Leon <santil@linux.vnet.ibm.com>
21  *	    Brian King <brking@linux.vnet.ibm.com>
22  *	    Robert Jennings <rcj@linux.vnet.ibm.com>
23  *	    Anton Blanchard <anton@au.ibm.com>
24  */
25 
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/kernel.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/mm.h>
37 #include <linux/pm.h>
38 #include <linux/ethtool.h>
39 #include <linux/in.h>
40 #include <linux/ip.h>
41 #include <linux/ipv6.h>
42 #include <linux/slab.h>
43 #include <asm/hvcall.h>
44 #include <linux/atomic.h>
45 #include <asm/vio.h>
46 #include <asm/iommu.h>
47 #include <asm/firmware.h>
48 #include <net/tcp.h>
49 #include <net/ip6_checksum.h>
50 
51 #include "ibmveth.h"
52 
53 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
54 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
55 static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
56 
57 static struct kobj_type ktype_veth_pool;
58 
59 
60 static const char ibmveth_driver_name[] = "ibmveth";
61 static const char ibmveth_driver_string[] = "IBM Power Virtual Ethernet Driver";
62 #define ibmveth_driver_version "1.06"
63 
64 MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
65 MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
66 MODULE_LICENSE("GPL");
67 MODULE_VERSION(ibmveth_driver_version);
68 
69 static unsigned int tx_copybreak __read_mostly = 128;
70 module_param(tx_copybreak, uint, 0644);
71 MODULE_PARM_DESC(tx_copybreak,
72 	"Maximum size of packet that is copied to a new buffer on transmit");
73 
74 static unsigned int rx_copybreak __read_mostly = 128;
75 module_param(rx_copybreak, uint, 0644);
76 MODULE_PARM_DESC(rx_copybreak,
77 	"Maximum size of packet that is copied to a new buffer on receive");
78 
79 static unsigned int rx_flush __read_mostly = 0;
80 module_param(rx_flush, uint, 0644);
81 MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");
82 
83 static bool old_large_send __read_mostly;
84 module_param(old_large_send, bool, 0444);
85 MODULE_PARM_DESC(old_large_send,
86 	"Use old large send method on firmware that supports the new method");
87 
88 struct ibmveth_stat {
89 	char name[ETH_GSTRING_LEN];
90 	int offset;
91 };
92 
93 #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
94 #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
95 
96 struct ibmveth_stat ibmveth_stats[] = {
97 	{ "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
98 	{ "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
99 	{ "replenish_add_buff_failure",
100 			IBMVETH_STAT_OFF(replenish_add_buff_failure) },
101 	{ "replenish_add_buff_success",
102 			IBMVETH_STAT_OFF(replenish_add_buff_success) },
103 	{ "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
104 	{ "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
105 	{ "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
106 	{ "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
107 	{ "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
108 	{ "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
109 	{ "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets) },
110 	{ "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets) },
111 	{ "fw_enabled_large_send", IBMVETH_STAT_OFF(fw_large_send_support) }
112 };
113 
114 /* simple methods of getting data from the current rxq entry */
115 static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
116 {
117 	return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
118 }
119 
120 static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
121 {
122 	return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
123 			IBMVETH_RXQ_TOGGLE_SHIFT;
124 }
125 
126 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
127 {
128 	return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
129 }
130 
131 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
132 {
133 	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
134 }
135 
136 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
137 {
138 	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
139 }
140 
141 static inline int ibmveth_rxq_large_packet(struct ibmveth_adapter *adapter)
142 {
143 	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_LRG_PKT;
144 }
145 
146 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
147 {
148 	return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
149 }
150 
151 static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
152 {
153 	return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
154 }
155 
156 /* setup the initial settings for a buffer pool */
157 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
158 				     u32 pool_index, u32 pool_size,
159 				     u32 buff_size, u32 pool_active)
160 {
161 	pool->size = pool_size;
162 	pool->index = pool_index;
163 	pool->buff_size = buff_size;
164 	pool->threshold = pool_size * 7 / 8;
165 	pool->active = pool_active;
166 }
167 
168 /* allocate and setup an buffer pool - called during open */
169 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
170 {
171 	int i;
172 
173 	pool->free_map = kmalloc_array(pool->size, sizeof(u16), GFP_KERNEL);
174 
175 	if (!pool->free_map)
176 		return -1;
177 
178 	pool->dma_addr = kcalloc(pool->size, sizeof(dma_addr_t), GFP_KERNEL);
179 	if (!pool->dma_addr) {
180 		kfree(pool->free_map);
181 		pool->free_map = NULL;
182 		return -1;
183 	}
184 
185 	pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
186 
187 	if (!pool->skbuff) {
188 		kfree(pool->dma_addr);
189 		pool->dma_addr = NULL;
190 
191 		kfree(pool->free_map);
192 		pool->free_map = NULL;
193 		return -1;
194 	}
195 
196 	for (i = 0; i < pool->size; ++i)
197 		pool->free_map[i] = i;
198 
199 	atomic_set(&pool->available, 0);
200 	pool->producer_index = 0;
201 	pool->consumer_index = 0;
202 
203 	return 0;
204 }
205 
206 static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
207 {
208 	unsigned long offset;
209 
210 	for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
211 		asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
212 }
213 
214 /* replenish the buffers for a pool.  note that we don't need to
215  * skb_reserve these since they are used for incoming...
216  */
217 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
218 					  struct ibmveth_buff_pool *pool)
219 {
220 	u32 i;
221 	u32 count = pool->size - atomic_read(&pool->available);
222 	u32 buffers_added = 0;
223 	struct sk_buff *skb;
224 	unsigned int free_index, index;
225 	u64 correlator;
226 	unsigned long lpar_rc;
227 	dma_addr_t dma_addr;
228 
229 	mb();
230 
231 	for (i = 0; i < count; ++i) {
232 		union ibmveth_buf_desc desc;
233 
234 		skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
235 
236 		if (!skb) {
237 			netdev_dbg(adapter->netdev,
238 				   "replenish: unable to allocate skb\n");
239 			adapter->replenish_no_mem++;
240 			break;
241 		}
242 
243 		free_index = pool->consumer_index;
244 		pool->consumer_index++;
245 		if (pool->consumer_index >= pool->size)
246 			pool->consumer_index = 0;
247 		index = pool->free_map[free_index];
248 
249 		BUG_ON(index == IBM_VETH_INVALID_MAP);
250 		BUG_ON(pool->skbuff[index] != NULL);
251 
252 		dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
253 				pool->buff_size, DMA_FROM_DEVICE);
254 
255 		if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
256 			goto failure;
257 
258 		pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
259 		pool->dma_addr[index] = dma_addr;
260 		pool->skbuff[index] = skb;
261 
262 		correlator = ((u64)pool->index << 32) | index;
263 		*(u64 *)skb->data = correlator;
264 
265 		desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
266 		desc.fields.address = dma_addr;
267 
268 		if (rx_flush) {
269 			unsigned int len = min(pool->buff_size,
270 						adapter->netdev->mtu +
271 						IBMVETH_BUFF_OH);
272 			ibmveth_flush_buffer(skb->data, len);
273 		}
274 		lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
275 						   desc.desc);
276 
277 		if (lpar_rc != H_SUCCESS) {
278 			goto failure;
279 		} else {
280 			buffers_added++;
281 			adapter->replenish_add_buff_success++;
282 		}
283 	}
284 
285 	mb();
286 	atomic_add(buffers_added, &(pool->available));
287 	return;
288 
289 failure:
290 	pool->free_map[free_index] = index;
291 	pool->skbuff[index] = NULL;
292 	if (pool->consumer_index == 0)
293 		pool->consumer_index = pool->size - 1;
294 	else
295 		pool->consumer_index--;
296 	if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
297 		dma_unmap_single(&adapter->vdev->dev,
298 		                 pool->dma_addr[index], pool->buff_size,
299 		                 DMA_FROM_DEVICE);
300 	dev_kfree_skb_any(skb);
301 	adapter->replenish_add_buff_failure++;
302 
303 	mb();
304 	atomic_add(buffers_added, &(pool->available));
305 }
306 
307 /*
308  * The final 8 bytes of the buffer list is a counter of frames dropped
309  * because there was not a buffer in the buffer list capable of holding
310  * the frame.
311  */
312 static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
313 {
314 	__be64 *p = adapter->buffer_list_addr + 4096 - 8;
315 
316 	adapter->rx_no_buffer = be64_to_cpup(p);
317 }
318 
319 /* replenish routine */
320 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
321 {
322 	int i;
323 
324 	adapter->replenish_task_cycles++;
325 
326 	for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
327 		struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
328 
329 		if (pool->active &&
330 		    (atomic_read(&pool->available) < pool->threshold))
331 			ibmveth_replenish_buffer_pool(adapter, pool);
332 	}
333 
334 	ibmveth_update_rx_no_buffer(adapter);
335 }
336 
337 /* empty and free ana buffer pool - also used to do cleanup in error paths */
338 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
339 				     struct ibmveth_buff_pool *pool)
340 {
341 	int i;
342 
343 	kfree(pool->free_map);
344 	pool->free_map = NULL;
345 
346 	if (pool->skbuff && pool->dma_addr) {
347 		for (i = 0; i < pool->size; ++i) {
348 			struct sk_buff *skb = pool->skbuff[i];
349 			if (skb) {
350 				dma_unmap_single(&adapter->vdev->dev,
351 						 pool->dma_addr[i],
352 						 pool->buff_size,
353 						 DMA_FROM_DEVICE);
354 				dev_kfree_skb_any(skb);
355 				pool->skbuff[i] = NULL;
356 			}
357 		}
358 	}
359 
360 	if (pool->dma_addr) {
361 		kfree(pool->dma_addr);
362 		pool->dma_addr = NULL;
363 	}
364 
365 	if (pool->skbuff) {
366 		kfree(pool->skbuff);
367 		pool->skbuff = NULL;
368 	}
369 }
370 
371 /* remove a buffer from a pool */
372 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
373 					    u64 correlator)
374 {
375 	unsigned int pool  = correlator >> 32;
376 	unsigned int index = correlator & 0xffffffffUL;
377 	unsigned int free_index;
378 	struct sk_buff *skb;
379 
380 	BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
381 	BUG_ON(index >= adapter->rx_buff_pool[pool].size);
382 
383 	skb = adapter->rx_buff_pool[pool].skbuff[index];
384 
385 	BUG_ON(skb == NULL);
386 
387 	adapter->rx_buff_pool[pool].skbuff[index] = NULL;
388 
389 	dma_unmap_single(&adapter->vdev->dev,
390 			 adapter->rx_buff_pool[pool].dma_addr[index],
391 			 adapter->rx_buff_pool[pool].buff_size,
392 			 DMA_FROM_DEVICE);
393 
394 	free_index = adapter->rx_buff_pool[pool].producer_index;
395 	adapter->rx_buff_pool[pool].producer_index++;
396 	if (adapter->rx_buff_pool[pool].producer_index >=
397 	    adapter->rx_buff_pool[pool].size)
398 		adapter->rx_buff_pool[pool].producer_index = 0;
399 	adapter->rx_buff_pool[pool].free_map[free_index] = index;
400 
401 	mb();
402 
403 	atomic_dec(&(adapter->rx_buff_pool[pool].available));
404 }
405 
406 /* get the current buffer on the rx queue */
407 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
408 {
409 	u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
410 	unsigned int pool = correlator >> 32;
411 	unsigned int index = correlator & 0xffffffffUL;
412 
413 	BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
414 	BUG_ON(index >= adapter->rx_buff_pool[pool].size);
415 
416 	return adapter->rx_buff_pool[pool].skbuff[index];
417 }
418 
419 /* recycle the current buffer on the rx queue */
420 static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
421 {
422 	u32 q_index = adapter->rx_queue.index;
423 	u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
424 	unsigned int pool = correlator >> 32;
425 	unsigned int index = correlator & 0xffffffffUL;
426 	union ibmveth_buf_desc desc;
427 	unsigned long lpar_rc;
428 	int ret = 1;
429 
430 	BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
431 	BUG_ON(index >= adapter->rx_buff_pool[pool].size);
432 
433 	if (!adapter->rx_buff_pool[pool].active) {
434 		ibmveth_rxq_harvest_buffer(adapter);
435 		ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
436 		goto out;
437 	}
438 
439 	desc.fields.flags_len = IBMVETH_BUF_VALID |
440 		adapter->rx_buff_pool[pool].buff_size;
441 	desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
442 
443 	lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
444 
445 	if (lpar_rc != H_SUCCESS) {
446 		netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
447 			   "during recycle rc=%ld", lpar_rc);
448 		ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
449 		ret = 0;
450 	}
451 
452 	if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
453 		adapter->rx_queue.index = 0;
454 		adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
455 	}
456 
457 out:
458 	return ret;
459 }
460 
461 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
462 {
463 	ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
464 
465 	if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
466 		adapter->rx_queue.index = 0;
467 		adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
468 	}
469 }
470 
471 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
472         union ibmveth_buf_desc rxq_desc, u64 mac_address)
473 {
474 	int rc, try_again = 1;
475 
476 	/*
477 	 * After a kexec the adapter will still be open, so our attempt to
478 	 * open it will fail. So if we get a failure we free the adapter and
479 	 * try again, but only once.
480 	 */
481 retry:
482 	rc = h_register_logical_lan(adapter->vdev->unit_address,
483 				    adapter->buffer_list_dma, rxq_desc.desc,
484 				    adapter->filter_list_dma, mac_address);
485 
486 	if (rc != H_SUCCESS && try_again) {
487 		do {
488 			rc = h_free_logical_lan(adapter->vdev->unit_address);
489 		} while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
490 
491 		try_again = 0;
492 		goto retry;
493 	}
494 
495 	return rc;
496 }
497 
498 static u64 ibmveth_encode_mac_addr(u8 *mac)
499 {
500 	int i;
501 	u64 encoded = 0;
502 
503 	for (i = 0; i < ETH_ALEN; i++)
504 		encoded = (encoded << 8) | mac[i];
505 
506 	return encoded;
507 }
508 
509 static int ibmveth_open(struct net_device *netdev)
510 {
511 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
512 	u64 mac_address;
513 	int rxq_entries = 1;
514 	unsigned long lpar_rc;
515 	int rc;
516 	union ibmveth_buf_desc rxq_desc;
517 	int i;
518 	struct device *dev;
519 
520 	netdev_dbg(netdev, "open starting\n");
521 
522 	napi_enable(&adapter->napi);
523 
524 	for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
525 		rxq_entries += adapter->rx_buff_pool[i].size;
526 
527 	rc = -ENOMEM;
528 	adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
529 	if (!adapter->buffer_list_addr) {
530 		netdev_err(netdev, "unable to allocate list pages\n");
531 		goto out;
532 	}
533 
534 	adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
535 	if (!adapter->filter_list_addr) {
536 		netdev_err(netdev, "unable to allocate filter pages\n");
537 		goto out_free_buffer_list;
538 	}
539 
540 	dev = &adapter->vdev->dev;
541 
542 	adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
543 						rxq_entries;
544 	adapter->rx_queue.queue_addr =
545 		dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
546 				   &adapter->rx_queue.queue_dma, GFP_KERNEL);
547 	if (!adapter->rx_queue.queue_addr)
548 		goto out_free_filter_list;
549 
550 	adapter->buffer_list_dma = dma_map_single(dev,
551 			adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
552 	if (dma_mapping_error(dev, adapter->buffer_list_dma)) {
553 		netdev_err(netdev, "unable to map buffer list pages\n");
554 		goto out_free_queue_mem;
555 	}
556 
557 	adapter->filter_list_dma = dma_map_single(dev,
558 			adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
559 	if (dma_mapping_error(dev, adapter->filter_list_dma)) {
560 		netdev_err(netdev, "unable to map filter list pages\n");
561 		goto out_unmap_buffer_list;
562 	}
563 
564 	adapter->rx_queue.index = 0;
565 	adapter->rx_queue.num_slots = rxq_entries;
566 	adapter->rx_queue.toggle = 1;
567 
568 	mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
569 
570 	rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
571 					adapter->rx_queue.queue_len;
572 	rxq_desc.fields.address = adapter->rx_queue.queue_dma;
573 
574 	netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
575 	netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
576 	netdev_dbg(netdev, "receive q   @ 0x%p\n", adapter->rx_queue.queue_addr);
577 
578 	h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
579 
580 	lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
581 
582 	if (lpar_rc != H_SUCCESS) {
583 		netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
584 			   lpar_rc);
585 		netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
586 			   "desc:0x%llx MAC:0x%llx\n",
587 				     adapter->buffer_list_dma,
588 				     adapter->filter_list_dma,
589 				     rxq_desc.desc,
590 				     mac_address);
591 		rc = -ENONET;
592 		goto out_unmap_filter_list;
593 	}
594 
595 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
596 		if (!adapter->rx_buff_pool[i].active)
597 			continue;
598 		if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
599 			netdev_err(netdev, "unable to alloc pool\n");
600 			adapter->rx_buff_pool[i].active = 0;
601 			rc = -ENOMEM;
602 			goto out_free_buffer_pools;
603 		}
604 	}
605 
606 	netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
607 	rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
608 			 netdev);
609 	if (rc != 0) {
610 		netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
611 			   netdev->irq, rc);
612 		do {
613 			lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
614 		} while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
615 
616 		goto out_free_buffer_pools;
617 	}
618 
619 	rc = -ENOMEM;
620 	adapter->bounce_buffer =
621 	    kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
622 	if (!adapter->bounce_buffer)
623 		goto out_free_irq;
624 
625 	adapter->bounce_buffer_dma =
626 	    dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
627 			   netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
628 	if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
629 		netdev_err(netdev, "unable to map bounce buffer\n");
630 		goto out_free_bounce_buffer;
631 	}
632 
633 	netdev_dbg(netdev, "initial replenish cycle\n");
634 	ibmveth_interrupt(netdev->irq, netdev);
635 
636 	netif_start_queue(netdev);
637 
638 	netdev_dbg(netdev, "open complete\n");
639 
640 	return 0;
641 
642 out_free_bounce_buffer:
643 	kfree(adapter->bounce_buffer);
644 out_free_irq:
645 	free_irq(netdev->irq, netdev);
646 out_free_buffer_pools:
647 	while (--i >= 0) {
648 		if (adapter->rx_buff_pool[i].active)
649 			ibmveth_free_buffer_pool(adapter,
650 						 &adapter->rx_buff_pool[i]);
651 	}
652 out_unmap_filter_list:
653 	dma_unmap_single(dev, adapter->filter_list_dma, 4096,
654 			 DMA_BIDIRECTIONAL);
655 out_unmap_buffer_list:
656 	dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
657 			 DMA_BIDIRECTIONAL);
658 out_free_queue_mem:
659 	dma_free_coherent(dev, adapter->rx_queue.queue_len,
660 			  adapter->rx_queue.queue_addr,
661 			  adapter->rx_queue.queue_dma);
662 out_free_filter_list:
663 	free_page((unsigned long)adapter->filter_list_addr);
664 out_free_buffer_list:
665 	free_page((unsigned long)adapter->buffer_list_addr);
666 out:
667 	napi_disable(&adapter->napi);
668 	return rc;
669 }
670 
671 static int ibmveth_close(struct net_device *netdev)
672 {
673 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
674 	struct device *dev = &adapter->vdev->dev;
675 	long lpar_rc;
676 	int i;
677 
678 	netdev_dbg(netdev, "close starting\n");
679 
680 	napi_disable(&adapter->napi);
681 
682 	if (!adapter->pool_config)
683 		netif_stop_queue(netdev);
684 
685 	h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
686 
687 	do {
688 		lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
689 	} while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
690 
691 	if (lpar_rc != H_SUCCESS) {
692 		netdev_err(netdev, "h_free_logical_lan failed with %lx, "
693 			   "continuing with close\n", lpar_rc);
694 	}
695 
696 	free_irq(netdev->irq, netdev);
697 
698 	ibmveth_update_rx_no_buffer(adapter);
699 
700 	dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
701 			 DMA_BIDIRECTIONAL);
702 	free_page((unsigned long)adapter->buffer_list_addr);
703 
704 	dma_unmap_single(dev, adapter->filter_list_dma, 4096,
705 			 DMA_BIDIRECTIONAL);
706 	free_page((unsigned long)adapter->filter_list_addr);
707 
708 	dma_free_coherent(dev, adapter->rx_queue.queue_len,
709 			  adapter->rx_queue.queue_addr,
710 			  adapter->rx_queue.queue_dma);
711 
712 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
713 		if (adapter->rx_buff_pool[i].active)
714 			ibmveth_free_buffer_pool(adapter,
715 						 &adapter->rx_buff_pool[i]);
716 
717 	dma_unmap_single(&adapter->vdev->dev, adapter->bounce_buffer_dma,
718 			 adapter->netdev->mtu + IBMVETH_BUFF_OH,
719 			 DMA_BIDIRECTIONAL);
720 	kfree(adapter->bounce_buffer);
721 
722 	netdev_dbg(netdev, "close complete\n");
723 
724 	return 0;
725 }
726 
727 static int netdev_get_link_ksettings(struct net_device *dev,
728 				     struct ethtool_link_ksettings *cmd)
729 {
730 	u32 supported, advertising;
731 
732 	supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
733 				SUPPORTED_FIBRE);
734 	advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
735 				ADVERTISED_FIBRE);
736 	cmd->base.speed = SPEED_1000;
737 	cmd->base.duplex = DUPLEX_FULL;
738 	cmd->base.port = PORT_FIBRE;
739 	cmd->base.phy_address = 0;
740 	cmd->base.autoneg = AUTONEG_ENABLE;
741 
742 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
743 						supported);
744 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
745 						advertising);
746 
747 	return 0;
748 }
749 
750 static void netdev_get_drvinfo(struct net_device *dev,
751 			       struct ethtool_drvinfo *info)
752 {
753 	strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
754 	strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
755 }
756 
757 static netdev_features_t ibmveth_fix_features(struct net_device *dev,
758 	netdev_features_t features)
759 {
760 	/*
761 	 * Since the ibmveth firmware interface does not have the
762 	 * concept of separate tx/rx checksum offload enable, if rx
763 	 * checksum is disabled we also have to disable tx checksum
764 	 * offload. Once we disable rx checksum offload, we are no
765 	 * longer allowed to send tx buffers that are not properly
766 	 * checksummed.
767 	 */
768 
769 	if (!(features & NETIF_F_RXCSUM))
770 		features &= ~NETIF_F_CSUM_MASK;
771 
772 	return features;
773 }
774 
775 static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
776 {
777 	struct ibmveth_adapter *adapter = netdev_priv(dev);
778 	unsigned long set_attr, clr_attr, ret_attr;
779 	unsigned long set_attr6, clr_attr6;
780 	long ret, ret4, ret6;
781 	int rc1 = 0, rc2 = 0;
782 	int restart = 0;
783 
784 	if (netif_running(dev)) {
785 		restart = 1;
786 		adapter->pool_config = 1;
787 		ibmveth_close(dev);
788 		adapter->pool_config = 0;
789 	}
790 
791 	set_attr = 0;
792 	clr_attr = 0;
793 	set_attr6 = 0;
794 	clr_attr6 = 0;
795 
796 	if (data) {
797 		set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
798 		set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
799 	} else {
800 		clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
801 		clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
802 	}
803 
804 	ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
805 
806 	if (ret == H_SUCCESS &&
807 	    (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
808 		ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
809 					 set_attr, &ret_attr);
810 
811 		if (ret4 != H_SUCCESS) {
812 			netdev_err(dev, "unable to change IPv4 checksum "
813 					"offload settings. %d rc=%ld\n",
814 					data, ret4);
815 
816 			h_illan_attributes(adapter->vdev->unit_address,
817 					   set_attr, clr_attr, &ret_attr);
818 
819 			if (data == 1)
820 				dev->features &= ~NETIF_F_IP_CSUM;
821 
822 		} else {
823 			adapter->fw_ipv4_csum_support = data;
824 		}
825 
826 		ret6 = h_illan_attributes(adapter->vdev->unit_address,
827 					 clr_attr6, set_attr6, &ret_attr);
828 
829 		if (ret6 != H_SUCCESS) {
830 			netdev_err(dev, "unable to change IPv6 checksum "
831 					"offload settings. %d rc=%ld\n",
832 					data, ret6);
833 
834 			h_illan_attributes(adapter->vdev->unit_address,
835 					   set_attr6, clr_attr6, &ret_attr);
836 
837 			if (data == 1)
838 				dev->features &= ~NETIF_F_IPV6_CSUM;
839 
840 		} else
841 			adapter->fw_ipv6_csum_support = data;
842 
843 		if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
844 			adapter->rx_csum = data;
845 		else
846 			rc1 = -EIO;
847 	} else {
848 		rc1 = -EIO;
849 		netdev_err(dev, "unable to change checksum offload settings."
850 				     " %d rc=%ld ret_attr=%lx\n", data, ret,
851 				     ret_attr);
852 	}
853 
854 	if (restart)
855 		rc2 = ibmveth_open(dev);
856 
857 	return rc1 ? rc1 : rc2;
858 }
859 
860 static int ibmveth_set_tso(struct net_device *dev, u32 data)
861 {
862 	struct ibmveth_adapter *adapter = netdev_priv(dev);
863 	unsigned long set_attr, clr_attr, ret_attr;
864 	long ret1, ret2;
865 	int rc1 = 0, rc2 = 0;
866 	int restart = 0;
867 
868 	if (netif_running(dev)) {
869 		restart = 1;
870 		adapter->pool_config = 1;
871 		ibmveth_close(dev);
872 		adapter->pool_config = 0;
873 	}
874 
875 	set_attr = 0;
876 	clr_attr = 0;
877 
878 	if (data)
879 		set_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
880 	else
881 		clr_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
882 
883 	ret1 = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
884 
885 	if (ret1 == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
886 	    !old_large_send) {
887 		ret2 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
888 					  set_attr, &ret_attr);
889 
890 		if (ret2 != H_SUCCESS) {
891 			netdev_err(dev, "unable to change tso settings. %d rc=%ld\n",
892 				   data, ret2);
893 
894 			h_illan_attributes(adapter->vdev->unit_address,
895 					   set_attr, clr_attr, &ret_attr);
896 
897 			if (data == 1)
898 				dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
899 			rc1 = -EIO;
900 
901 		} else {
902 			adapter->fw_large_send_support = data;
903 			adapter->large_send = data;
904 		}
905 	} else {
906 		/* Older firmware version of large send offload does not
907 		 * support tcp6/ipv6
908 		 */
909 		if (data == 1) {
910 			dev->features &= ~NETIF_F_TSO6;
911 			netdev_info(dev, "TSO feature requires all partitions to have updated driver");
912 		}
913 		adapter->large_send = data;
914 	}
915 
916 	if (restart)
917 		rc2 = ibmveth_open(dev);
918 
919 	return rc1 ? rc1 : rc2;
920 }
921 
922 static int ibmveth_set_features(struct net_device *dev,
923 	netdev_features_t features)
924 {
925 	struct ibmveth_adapter *adapter = netdev_priv(dev);
926 	int rx_csum = !!(features & NETIF_F_RXCSUM);
927 	int large_send = !!(features & (NETIF_F_TSO | NETIF_F_TSO6));
928 	int rc1 = 0, rc2 = 0;
929 
930 	if (rx_csum != adapter->rx_csum) {
931 		rc1 = ibmveth_set_csum_offload(dev, rx_csum);
932 		if (rc1 && !adapter->rx_csum)
933 			dev->features =
934 				features & ~(NETIF_F_CSUM_MASK |
935 					     NETIF_F_RXCSUM);
936 	}
937 
938 	if (large_send != adapter->large_send) {
939 		rc2 = ibmveth_set_tso(dev, large_send);
940 		if (rc2 && !adapter->large_send)
941 			dev->features =
942 				features & ~(NETIF_F_TSO | NETIF_F_TSO6);
943 	}
944 
945 	return rc1 ? rc1 : rc2;
946 }
947 
948 static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
949 {
950 	int i;
951 
952 	if (stringset != ETH_SS_STATS)
953 		return;
954 
955 	for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
956 		memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
957 }
958 
959 static int ibmveth_get_sset_count(struct net_device *dev, int sset)
960 {
961 	switch (sset) {
962 	case ETH_SS_STATS:
963 		return ARRAY_SIZE(ibmveth_stats);
964 	default:
965 		return -EOPNOTSUPP;
966 	}
967 }
968 
969 static void ibmveth_get_ethtool_stats(struct net_device *dev,
970 				      struct ethtool_stats *stats, u64 *data)
971 {
972 	int i;
973 	struct ibmveth_adapter *adapter = netdev_priv(dev);
974 
975 	for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
976 		data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
977 }
978 
979 static const struct ethtool_ops netdev_ethtool_ops = {
980 	.get_drvinfo		= netdev_get_drvinfo,
981 	.get_link		= ethtool_op_get_link,
982 	.get_strings		= ibmveth_get_strings,
983 	.get_sset_count		= ibmveth_get_sset_count,
984 	.get_ethtool_stats	= ibmveth_get_ethtool_stats,
985 	.get_link_ksettings	= netdev_get_link_ksettings,
986 };
987 
988 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
989 {
990 	return -EOPNOTSUPP;
991 }
992 
993 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
994 
995 static int ibmveth_send(struct ibmveth_adapter *adapter,
996 			union ibmveth_buf_desc *descs, unsigned long mss)
997 {
998 	unsigned long correlator;
999 	unsigned int retry_count;
1000 	unsigned long ret;
1001 
1002 	/*
1003 	 * The retry count sets a maximum for the number of broadcast and
1004 	 * multicast destinations within the system.
1005 	 */
1006 	retry_count = 1024;
1007 	correlator = 0;
1008 	do {
1009 		ret = h_send_logical_lan(adapter->vdev->unit_address,
1010 					     descs[0].desc, descs[1].desc,
1011 					     descs[2].desc, descs[3].desc,
1012 					     descs[4].desc, descs[5].desc,
1013 					     correlator, &correlator, mss,
1014 					     adapter->fw_large_send_support);
1015 	} while ((ret == H_BUSY) && (retry_count--));
1016 
1017 	if (ret != H_SUCCESS && ret != H_DROPPED) {
1018 		netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
1019 			   "with rc=%ld\n", ret);
1020 		return 1;
1021 	}
1022 
1023 	return 0;
1024 }
1025 
1026 static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
1027 				      struct net_device *netdev)
1028 {
1029 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
1030 	unsigned int desc_flags;
1031 	union ibmveth_buf_desc descs[6];
1032 	int last, i;
1033 	int force_bounce = 0;
1034 	dma_addr_t dma_addr;
1035 	unsigned long mss = 0;
1036 
1037 	/* veth doesn't handle frag_list, so linearize the skb.
1038 	 * When GRO is enabled SKB's can have frag_list.
1039 	 */
1040 	if (adapter->is_active_trunk &&
1041 	    skb_has_frag_list(skb) && __skb_linearize(skb)) {
1042 		netdev->stats.tx_dropped++;
1043 		goto out;
1044 	}
1045 
1046 	/*
1047 	 * veth handles a maximum of 6 segments including the header, so
1048 	 * we have to linearize the skb if there are more than this.
1049 	 */
1050 	if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
1051 		netdev->stats.tx_dropped++;
1052 		goto out;
1053 	}
1054 
1055 	/* veth can't checksum offload UDP */
1056 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
1057 	    ((skb->protocol == htons(ETH_P_IP) &&
1058 	      ip_hdr(skb)->protocol != IPPROTO_TCP) ||
1059 	     (skb->protocol == htons(ETH_P_IPV6) &&
1060 	      ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
1061 	    skb_checksum_help(skb)) {
1062 
1063 		netdev_err(netdev, "tx: failed to checksum packet\n");
1064 		netdev->stats.tx_dropped++;
1065 		goto out;
1066 	}
1067 
1068 	desc_flags = IBMVETH_BUF_VALID;
1069 
1070 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1071 		unsigned char *buf = skb_transport_header(skb) +
1072 						skb->csum_offset;
1073 
1074 		desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
1075 
1076 		/* Need to zero out the checksum */
1077 		buf[0] = 0;
1078 		buf[1] = 0;
1079 
1080 		if (skb_is_gso(skb) && adapter->fw_large_send_support)
1081 			desc_flags |= IBMVETH_BUF_LRG_SND;
1082 	}
1083 
1084 retry_bounce:
1085 	memset(descs, 0, sizeof(descs));
1086 
1087 	/*
1088 	 * If a linear packet is below the rx threshold then
1089 	 * copy it into the static bounce buffer. This avoids the
1090 	 * cost of a TCE insert and remove.
1091 	 */
1092 	if (force_bounce || (!skb_is_nonlinear(skb) &&
1093 				(skb->len < tx_copybreak))) {
1094 		skb_copy_from_linear_data(skb, adapter->bounce_buffer,
1095 					  skb->len);
1096 
1097 		descs[0].fields.flags_len = desc_flags | skb->len;
1098 		descs[0].fields.address = adapter->bounce_buffer_dma;
1099 
1100 		if (ibmveth_send(adapter, descs, 0)) {
1101 			adapter->tx_send_failed++;
1102 			netdev->stats.tx_dropped++;
1103 		} else {
1104 			netdev->stats.tx_packets++;
1105 			netdev->stats.tx_bytes += skb->len;
1106 		}
1107 
1108 		goto out;
1109 	}
1110 
1111 	/* Map the header */
1112 	dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
1113 				  skb_headlen(skb), DMA_TO_DEVICE);
1114 	if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1115 		goto map_failed;
1116 
1117 	descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
1118 	descs[0].fields.address = dma_addr;
1119 
1120 	/* Map the frags */
1121 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1122 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1123 
1124 		dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
1125 					    skb_frag_size(frag), DMA_TO_DEVICE);
1126 
1127 		if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1128 			goto map_failed_frags;
1129 
1130 		descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
1131 		descs[i+1].fields.address = dma_addr;
1132 	}
1133 
1134 	if (skb->ip_summed == CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1135 		if (adapter->fw_large_send_support) {
1136 			mss = (unsigned long)skb_shinfo(skb)->gso_size;
1137 			adapter->tx_large_packets++;
1138 		} else if (!skb_is_gso_v6(skb)) {
1139 			/* Put -1 in the IP checksum to tell phyp it
1140 			 * is a largesend packet. Put the mss in
1141 			 * the TCP checksum.
1142 			 */
1143 			ip_hdr(skb)->check = 0xffff;
1144 			tcp_hdr(skb)->check =
1145 				cpu_to_be16(skb_shinfo(skb)->gso_size);
1146 			adapter->tx_large_packets++;
1147 		}
1148 	}
1149 
1150 	if (ibmveth_send(adapter, descs, mss)) {
1151 		adapter->tx_send_failed++;
1152 		netdev->stats.tx_dropped++;
1153 	} else {
1154 		netdev->stats.tx_packets++;
1155 		netdev->stats.tx_bytes += skb->len;
1156 	}
1157 
1158 	dma_unmap_single(&adapter->vdev->dev,
1159 			 descs[0].fields.address,
1160 			 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1161 			 DMA_TO_DEVICE);
1162 
1163 	for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
1164 		dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1165 			       descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1166 			       DMA_TO_DEVICE);
1167 
1168 out:
1169 	dev_consume_skb_any(skb);
1170 	return NETDEV_TX_OK;
1171 
1172 map_failed_frags:
1173 	last = i+1;
1174 	for (i = 1; i < last; i++)
1175 		dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1176 			       descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1177 			       DMA_TO_DEVICE);
1178 
1179 	dma_unmap_single(&adapter->vdev->dev,
1180 			 descs[0].fields.address,
1181 			 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1182 			 DMA_TO_DEVICE);
1183 map_failed:
1184 	if (!firmware_has_feature(FW_FEATURE_CMO))
1185 		netdev_err(netdev, "tx: unable to map xmit buffer\n");
1186 	adapter->tx_map_failed++;
1187 	if (skb_linearize(skb)) {
1188 		netdev->stats.tx_dropped++;
1189 		goto out;
1190 	}
1191 	force_bounce = 1;
1192 	goto retry_bounce;
1193 }
1194 
1195 static void ibmveth_rx_mss_helper(struct sk_buff *skb, u16 mss, int lrg_pkt)
1196 {
1197 	struct tcphdr *tcph;
1198 	int offset = 0;
1199 	int hdr_len;
1200 
1201 	/* only TCP packets will be aggregated */
1202 	if (skb->protocol == htons(ETH_P_IP)) {
1203 		struct iphdr *iph = (struct iphdr *)skb->data;
1204 
1205 		if (iph->protocol == IPPROTO_TCP) {
1206 			offset = iph->ihl * 4;
1207 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1208 		} else {
1209 			return;
1210 		}
1211 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1212 		struct ipv6hdr *iph6 = (struct ipv6hdr *)skb->data;
1213 
1214 		if (iph6->nexthdr == IPPROTO_TCP) {
1215 			offset = sizeof(struct ipv6hdr);
1216 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1217 		} else {
1218 			return;
1219 		}
1220 	} else {
1221 		return;
1222 	}
1223 	/* if mss is not set through Large Packet bit/mss in rx buffer,
1224 	 * expect that the mss will be written to the tcp header checksum.
1225 	 */
1226 	tcph = (struct tcphdr *)(skb->data + offset);
1227 	if (lrg_pkt) {
1228 		skb_shinfo(skb)->gso_size = mss;
1229 	} else if (offset) {
1230 		skb_shinfo(skb)->gso_size = ntohs(tcph->check);
1231 		tcph->check = 0;
1232 	}
1233 
1234 	if (skb_shinfo(skb)->gso_size) {
1235 		hdr_len = offset + tcph->doff * 4;
1236 		skb_shinfo(skb)->gso_segs =
1237 				DIV_ROUND_UP(skb->len - hdr_len,
1238 					     skb_shinfo(skb)->gso_size);
1239 	}
1240 }
1241 
1242 static void ibmveth_rx_csum_helper(struct sk_buff *skb,
1243 				   struct ibmveth_adapter *adapter)
1244 {
1245 	struct iphdr *iph = NULL;
1246 	struct ipv6hdr *iph6 = NULL;
1247 	__be16 skb_proto = 0;
1248 	u16 iphlen = 0;
1249 	u16 iph_proto = 0;
1250 	u16 tcphdrlen = 0;
1251 
1252 	skb_proto = be16_to_cpu(skb->protocol);
1253 
1254 	if (skb_proto == ETH_P_IP) {
1255 		iph = (struct iphdr *)skb->data;
1256 
1257 		/* If the IP checksum is not offloaded and if the packet
1258 		 *  is large send, the checksum must be rebuilt.
1259 		 */
1260 		if (iph->check == 0xffff) {
1261 			iph->check = 0;
1262 			iph->check = ip_fast_csum((unsigned char *)iph,
1263 						  iph->ihl);
1264 		}
1265 
1266 		iphlen = iph->ihl * 4;
1267 		iph_proto = iph->protocol;
1268 	} else if (skb_proto == ETH_P_IPV6) {
1269 		iph6 = (struct ipv6hdr *)skb->data;
1270 		iphlen = sizeof(struct ipv6hdr);
1271 		iph_proto = iph6->nexthdr;
1272 	}
1273 
1274 	/* In OVS environment, when a flow is not cached, specifically for a
1275 	 * new TCP connection, the first packet information is passed up
1276 	 * the user space for finding a flow. During this process, OVS computes
1277 	 * checksum on the first packet when CHECKSUM_PARTIAL flag is set.
1278 	 *
1279 	 * Given that we zeroed out TCP checksum field in transmit path
1280 	 * (refer ibmveth_start_xmit routine) as we set "no checksum bit",
1281 	 * OVS computed checksum will be incorrect w/o TCP pseudo checksum
1282 	 * in the packet. This leads to OVS dropping the packet and hence
1283 	 * TCP retransmissions are seen.
1284 	 *
1285 	 * So, re-compute TCP pseudo header checksum.
1286 	 */
1287 	if (iph_proto == IPPROTO_TCP && adapter->is_active_trunk) {
1288 		struct tcphdr *tcph = (struct tcphdr *)(skb->data + iphlen);
1289 
1290 		tcphdrlen = skb->len - iphlen;
1291 
1292 		/* Recompute TCP pseudo header checksum */
1293 		if (skb_proto == ETH_P_IP)
1294 			tcph->check = ~csum_tcpudp_magic(iph->saddr,
1295 					iph->daddr, tcphdrlen, iph_proto, 0);
1296 		else if (skb_proto == ETH_P_IPV6)
1297 			tcph->check = ~csum_ipv6_magic(&iph6->saddr,
1298 					&iph6->daddr, tcphdrlen, iph_proto, 0);
1299 
1300 		/* Setup SKB fields for checksum offload */
1301 		skb_partial_csum_set(skb, iphlen,
1302 				     offsetof(struct tcphdr, check));
1303 		skb_reset_network_header(skb);
1304 	}
1305 }
1306 
1307 static int ibmveth_poll(struct napi_struct *napi, int budget)
1308 {
1309 	struct ibmveth_adapter *adapter =
1310 			container_of(napi, struct ibmveth_adapter, napi);
1311 	struct net_device *netdev = adapter->netdev;
1312 	int frames_processed = 0;
1313 	unsigned long lpar_rc;
1314 	u16 mss = 0;
1315 
1316 	while (frames_processed < budget) {
1317 		if (!ibmveth_rxq_pending_buffer(adapter))
1318 			break;
1319 
1320 		smp_rmb();
1321 		if (!ibmveth_rxq_buffer_valid(adapter)) {
1322 			wmb(); /* suggested by larson1 */
1323 			adapter->rx_invalid_buffer++;
1324 			netdev_dbg(netdev, "recycling invalid buffer\n");
1325 			ibmveth_rxq_recycle_buffer(adapter);
1326 		} else {
1327 			struct sk_buff *skb, *new_skb;
1328 			int length = ibmveth_rxq_frame_length(adapter);
1329 			int offset = ibmveth_rxq_frame_offset(adapter);
1330 			int csum_good = ibmveth_rxq_csum_good(adapter);
1331 			int lrg_pkt = ibmveth_rxq_large_packet(adapter);
1332 
1333 			skb = ibmveth_rxq_get_buffer(adapter);
1334 
1335 			/* if the large packet bit is set in the rx queue
1336 			 * descriptor, the mss will be written by PHYP eight
1337 			 * bytes from the start of the rx buffer, which is
1338 			 * skb->data at this stage
1339 			 */
1340 			if (lrg_pkt) {
1341 				__be64 *rxmss = (__be64 *)(skb->data + 8);
1342 
1343 				mss = (u16)be64_to_cpu(*rxmss);
1344 			}
1345 
1346 			new_skb = NULL;
1347 			if (length < rx_copybreak)
1348 				new_skb = netdev_alloc_skb(netdev, length);
1349 
1350 			if (new_skb) {
1351 				skb_copy_to_linear_data(new_skb,
1352 							skb->data + offset,
1353 							length);
1354 				if (rx_flush)
1355 					ibmveth_flush_buffer(skb->data,
1356 						length + offset);
1357 				if (!ibmveth_rxq_recycle_buffer(adapter))
1358 					kfree_skb(skb);
1359 				skb = new_skb;
1360 			} else {
1361 				ibmveth_rxq_harvest_buffer(adapter);
1362 				skb_reserve(skb, offset);
1363 			}
1364 
1365 			skb_put(skb, length);
1366 			skb->protocol = eth_type_trans(skb, netdev);
1367 
1368 			if (csum_good) {
1369 				skb->ip_summed = CHECKSUM_UNNECESSARY;
1370 				ibmveth_rx_csum_helper(skb, adapter);
1371 			}
1372 
1373 			if (length > netdev->mtu + ETH_HLEN) {
1374 				ibmveth_rx_mss_helper(skb, mss, lrg_pkt);
1375 				adapter->rx_large_packets++;
1376 			}
1377 
1378 			napi_gro_receive(napi, skb);	/* send it up */
1379 
1380 			netdev->stats.rx_packets++;
1381 			netdev->stats.rx_bytes += length;
1382 			frames_processed++;
1383 		}
1384 	}
1385 
1386 	ibmveth_replenish_task(adapter);
1387 
1388 	if (frames_processed < budget) {
1389 		napi_complete_done(napi, frames_processed);
1390 
1391 		/* We think we are done - reenable interrupts,
1392 		 * then check once more to make sure we are done.
1393 		 */
1394 		lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1395 				       VIO_IRQ_ENABLE);
1396 
1397 		BUG_ON(lpar_rc != H_SUCCESS);
1398 
1399 		if (ibmveth_rxq_pending_buffer(adapter) &&
1400 		    napi_reschedule(napi)) {
1401 			lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1402 					       VIO_IRQ_DISABLE);
1403 		}
1404 	}
1405 
1406 	return frames_processed;
1407 }
1408 
1409 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
1410 {
1411 	struct net_device *netdev = dev_instance;
1412 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
1413 	unsigned long lpar_rc;
1414 
1415 	if (napi_schedule_prep(&adapter->napi)) {
1416 		lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1417 				       VIO_IRQ_DISABLE);
1418 		BUG_ON(lpar_rc != H_SUCCESS);
1419 		__napi_schedule(&adapter->napi);
1420 	}
1421 	return IRQ_HANDLED;
1422 }
1423 
1424 static void ibmveth_set_multicast_list(struct net_device *netdev)
1425 {
1426 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
1427 	unsigned long lpar_rc;
1428 
1429 	if ((netdev->flags & IFF_PROMISC) ||
1430 	    (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
1431 		lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1432 					   IbmVethMcastEnableRecv |
1433 					   IbmVethMcastDisableFiltering,
1434 					   0);
1435 		if (lpar_rc != H_SUCCESS) {
1436 			netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1437 				   "entering promisc mode\n", lpar_rc);
1438 		}
1439 	} else {
1440 		struct netdev_hw_addr *ha;
1441 		/* clear the filter table & disable filtering */
1442 		lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1443 					   IbmVethMcastEnableRecv |
1444 					   IbmVethMcastDisableFiltering |
1445 					   IbmVethMcastClearFilterTable,
1446 					   0);
1447 		if (lpar_rc != H_SUCCESS) {
1448 			netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1449 				   "attempting to clear filter table\n",
1450 				   lpar_rc);
1451 		}
1452 		/* add the addresses to the filter table */
1453 		netdev_for_each_mc_addr(ha, netdev) {
1454 			/* add the multicast address to the filter table */
1455 			u64 mcast_addr;
1456 			mcast_addr = ibmveth_encode_mac_addr(ha->addr);
1457 			lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1458 						   IbmVethMcastAddFilter,
1459 						   mcast_addr);
1460 			if (lpar_rc != H_SUCCESS) {
1461 				netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1462 					   "when adding an entry to the filter "
1463 					   "table\n", lpar_rc);
1464 			}
1465 		}
1466 
1467 		/* re-enable filtering */
1468 		lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1469 					   IbmVethMcastEnableFiltering,
1470 					   0);
1471 		if (lpar_rc != H_SUCCESS) {
1472 			netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1473 				   "enabling filtering\n", lpar_rc);
1474 		}
1475 	}
1476 }
1477 
1478 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1479 {
1480 	struct ibmveth_adapter *adapter = netdev_priv(dev);
1481 	struct vio_dev *viodev = adapter->vdev;
1482 	int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
1483 	int i, rc;
1484 	int need_restart = 0;
1485 
1486 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
1487 		if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
1488 			break;
1489 
1490 	if (i == IBMVETH_NUM_BUFF_POOLS)
1491 		return -EINVAL;
1492 
1493 	/* Deactivate all the buffer pools so that the next loop can activate
1494 	   only the buffer pools necessary to hold the new MTU */
1495 	if (netif_running(adapter->netdev)) {
1496 		need_restart = 1;
1497 		adapter->pool_config = 1;
1498 		ibmveth_close(adapter->netdev);
1499 		adapter->pool_config = 0;
1500 	}
1501 
1502 	/* Look for an active buffer pool that can hold the new MTU */
1503 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1504 		adapter->rx_buff_pool[i].active = 1;
1505 
1506 		if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
1507 			dev->mtu = new_mtu;
1508 			vio_cmo_set_dev_desired(viodev,
1509 						ibmveth_get_desired_dma
1510 						(viodev));
1511 			if (need_restart) {
1512 				return ibmveth_open(adapter->netdev);
1513 			}
1514 			return 0;
1515 		}
1516 	}
1517 
1518 	if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1519 		return rc;
1520 
1521 	return -EINVAL;
1522 }
1523 
1524 #ifdef CONFIG_NET_POLL_CONTROLLER
1525 static void ibmveth_poll_controller(struct net_device *dev)
1526 {
1527 	ibmveth_replenish_task(netdev_priv(dev));
1528 	ibmveth_interrupt(dev->irq, dev);
1529 }
1530 #endif
1531 
1532 /**
1533  * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1534  *
1535  * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1536  *
1537  * Return value:
1538  *	Number of bytes of IO data the driver will need to perform well.
1539  */
1540 static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1541 {
1542 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1543 	struct ibmveth_adapter *adapter;
1544 	struct iommu_table *tbl;
1545 	unsigned long ret;
1546 	int i;
1547 	int rxqentries = 1;
1548 
1549 	tbl = get_iommu_table_base(&vdev->dev);
1550 
1551 	/* netdev inits at probe time along with the structures we need below*/
1552 	if (netdev == NULL)
1553 		return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
1554 
1555 	adapter = netdev_priv(netdev);
1556 
1557 	ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
1558 	ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
1559 
1560 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1561 		/* add the size of the active receive buffers */
1562 		if (adapter->rx_buff_pool[i].active)
1563 			ret +=
1564 			    adapter->rx_buff_pool[i].size *
1565 			    IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
1566 					     buff_size, tbl);
1567 		rxqentries += adapter->rx_buff_pool[i].size;
1568 	}
1569 	/* add the size of the receive queue entries */
1570 	ret += IOMMU_PAGE_ALIGN(
1571 		rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
1572 
1573 	return ret;
1574 }
1575 
1576 static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1577 {
1578 	struct ibmveth_adapter *adapter = netdev_priv(dev);
1579 	struct sockaddr *addr = p;
1580 	u64 mac_address;
1581 	int rc;
1582 
1583 	if (!is_valid_ether_addr(addr->sa_data))
1584 		return -EADDRNOTAVAIL;
1585 
1586 	mac_address = ibmveth_encode_mac_addr(addr->sa_data);
1587 	rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1588 	if (rc) {
1589 		netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1590 		return rc;
1591 	}
1592 
1593 	ether_addr_copy(dev->dev_addr, addr->sa_data);
1594 
1595 	return 0;
1596 }
1597 
1598 static const struct net_device_ops ibmveth_netdev_ops = {
1599 	.ndo_open		= ibmveth_open,
1600 	.ndo_stop		= ibmveth_close,
1601 	.ndo_start_xmit		= ibmveth_start_xmit,
1602 	.ndo_set_rx_mode	= ibmveth_set_multicast_list,
1603 	.ndo_do_ioctl		= ibmveth_ioctl,
1604 	.ndo_change_mtu		= ibmveth_change_mtu,
1605 	.ndo_fix_features	= ibmveth_fix_features,
1606 	.ndo_set_features	= ibmveth_set_features,
1607 	.ndo_validate_addr	= eth_validate_addr,
1608 	.ndo_set_mac_address    = ibmveth_set_mac_addr,
1609 #ifdef CONFIG_NET_POLL_CONTROLLER
1610 	.ndo_poll_controller	= ibmveth_poll_controller,
1611 #endif
1612 };
1613 
1614 static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1615 {
1616 	int rc, i, mac_len;
1617 	struct net_device *netdev;
1618 	struct ibmveth_adapter *adapter;
1619 	unsigned char *mac_addr_p;
1620 	unsigned int *mcastFilterSize_p;
1621 	long ret;
1622 	unsigned long ret_attr;
1623 
1624 	dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1625 		dev->unit_address);
1626 
1627 	mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
1628 							&mac_len);
1629 	if (!mac_addr_p) {
1630 		dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
1631 		return -EINVAL;
1632 	}
1633 	/* Workaround for old/broken pHyp */
1634 	if (mac_len == 8)
1635 		mac_addr_p += 2;
1636 	else if (mac_len != 6) {
1637 		dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
1638 			mac_len);
1639 		return -EINVAL;
1640 	}
1641 
1642 	mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
1643 						VETH_MCAST_FILTER_SIZE, NULL);
1644 	if (!mcastFilterSize_p) {
1645 		dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1646 			"attribute\n");
1647 		return -EINVAL;
1648 	}
1649 
1650 	netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1651 
1652 	if (!netdev)
1653 		return -ENOMEM;
1654 
1655 	adapter = netdev_priv(netdev);
1656 	dev_set_drvdata(&dev->dev, netdev);
1657 
1658 	adapter->vdev = dev;
1659 	adapter->netdev = netdev;
1660 	adapter->mcastFilterSize = *mcastFilterSize_p;
1661 	adapter->pool_config = 0;
1662 
1663 	netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1664 
1665 	netdev->irq = dev->irq;
1666 	netdev->netdev_ops = &ibmveth_netdev_ops;
1667 	netdev->ethtool_ops = &netdev_ethtool_ops;
1668 	SET_NETDEV_DEV(netdev, &dev->dev);
1669 	netdev->hw_features = NETIF_F_SG;
1670 	if (vio_get_attribute(dev, "ibm,illan-options", NULL) != NULL) {
1671 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1672 				       NETIF_F_RXCSUM;
1673 	}
1674 
1675 	netdev->features |= netdev->hw_features;
1676 
1677 	ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
1678 
1679 	/* If running older firmware, TSO should not be enabled by default */
1680 	if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
1681 	    !old_large_send) {
1682 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1683 		netdev->features |= netdev->hw_features;
1684 	} else {
1685 		netdev->hw_features |= NETIF_F_TSO;
1686 	}
1687 
1688 	adapter->is_active_trunk = false;
1689 	if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK)) {
1690 		adapter->is_active_trunk = true;
1691 		netdev->hw_features |= NETIF_F_FRAGLIST;
1692 		netdev->features |= NETIF_F_FRAGLIST;
1693 	}
1694 
1695 	netdev->min_mtu = IBMVETH_MIN_MTU;
1696 	netdev->max_mtu = ETH_MAX_MTU;
1697 
1698 	memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
1699 
1700 	if (firmware_has_feature(FW_FEATURE_CMO))
1701 		memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
1702 
1703 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1704 		struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1705 		int error;
1706 
1707 		ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1708 					 pool_count[i], pool_size[i],
1709 					 pool_active[i]);
1710 		error = kobject_init_and_add(kobj, &ktype_veth_pool,
1711 					     &dev->dev.kobj, "pool%d", i);
1712 		if (!error)
1713 			kobject_uevent(kobj, KOBJ_ADD);
1714 	}
1715 
1716 	netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
1717 	netdev_dbg(netdev, "registering netdev...\n");
1718 
1719 	ibmveth_set_features(netdev, netdev->features);
1720 
1721 	rc = register_netdev(netdev);
1722 
1723 	if (rc) {
1724 		netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
1725 		free_netdev(netdev);
1726 		return rc;
1727 	}
1728 
1729 	netdev_dbg(netdev, "registered\n");
1730 
1731 	return 0;
1732 }
1733 
1734 static int ibmveth_remove(struct vio_dev *dev)
1735 {
1736 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
1737 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
1738 	int i;
1739 
1740 	for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
1741 		kobject_put(&adapter->rx_buff_pool[i].kobj);
1742 
1743 	unregister_netdev(netdev);
1744 
1745 	free_netdev(netdev);
1746 	dev_set_drvdata(&dev->dev, NULL);
1747 
1748 	return 0;
1749 }
1750 
1751 static struct attribute veth_active_attr;
1752 static struct attribute veth_num_attr;
1753 static struct attribute veth_size_attr;
1754 
1755 static ssize_t veth_pool_show(struct kobject *kobj,
1756 			      struct attribute *attr, char *buf)
1757 {
1758 	struct ibmveth_buff_pool *pool = container_of(kobj,
1759 						      struct ibmveth_buff_pool,
1760 						      kobj);
1761 
1762 	if (attr == &veth_active_attr)
1763 		return sprintf(buf, "%d\n", pool->active);
1764 	else if (attr == &veth_num_attr)
1765 		return sprintf(buf, "%d\n", pool->size);
1766 	else if (attr == &veth_size_attr)
1767 		return sprintf(buf, "%d\n", pool->buff_size);
1768 	return 0;
1769 }
1770 
1771 static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1772 			       const char *buf, size_t count)
1773 {
1774 	struct ibmveth_buff_pool *pool = container_of(kobj,
1775 						      struct ibmveth_buff_pool,
1776 						      kobj);
1777 	struct net_device *netdev = dev_get_drvdata(
1778 	    container_of(kobj->parent, struct device, kobj));
1779 	struct ibmveth_adapter *adapter = netdev_priv(netdev);
1780 	long value = simple_strtol(buf, NULL, 10);
1781 	long rc;
1782 
1783 	if (attr == &veth_active_attr) {
1784 		if (value && !pool->active) {
1785 			if (netif_running(netdev)) {
1786 				if (ibmveth_alloc_buffer_pool(pool)) {
1787 					netdev_err(netdev,
1788 						   "unable to alloc pool\n");
1789 					return -ENOMEM;
1790 				}
1791 				pool->active = 1;
1792 				adapter->pool_config = 1;
1793 				ibmveth_close(netdev);
1794 				adapter->pool_config = 0;
1795 				if ((rc = ibmveth_open(netdev)))
1796 					return rc;
1797 			} else {
1798 				pool->active = 1;
1799 			}
1800 		} else if (!value && pool->active) {
1801 			int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1802 			int i;
1803 			/* Make sure there is a buffer pool with buffers that
1804 			   can hold a packet of the size of the MTU */
1805 			for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1806 				if (pool == &adapter->rx_buff_pool[i])
1807 					continue;
1808 				if (!adapter->rx_buff_pool[i].active)
1809 					continue;
1810 				if (mtu <= adapter->rx_buff_pool[i].buff_size)
1811 					break;
1812 			}
1813 
1814 			if (i == IBMVETH_NUM_BUFF_POOLS) {
1815 				netdev_err(netdev, "no active pool >= MTU\n");
1816 				return -EPERM;
1817 			}
1818 
1819 			if (netif_running(netdev)) {
1820 				adapter->pool_config = 1;
1821 				ibmveth_close(netdev);
1822 				pool->active = 0;
1823 				adapter->pool_config = 0;
1824 				if ((rc = ibmveth_open(netdev)))
1825 					return rc;
1826 			}
1827 			pool->active = 0;
1828 		}
1829 	} else if (attr == &veth_num_attr) {
1830 		if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
1831 			return -EINVAL;
1832 		} else {
1833 			if (netif_running(netdev)) {
1834 				adapter->pool_config = 1;
1835 				ibmveth_close(netdev);
1836 				adapter->pool_config = 0;
1837 				pool->size = value;
1838 				if ((rc = ibmveth_open(netdev)))
1839 					return rc;
1840 			} else {
1841 				pool->size = value;
1842 			}
1843 		}
1844 	} else if (attr == &veth_size_attr) {
1845 		if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
1846 			return -EINVAL;
1847 		} else {
1848 			if (netif_running(netdev)) {
1849 				adapter->pool_config = 1;
1850 				ibmveth_close(netdev);
1851 				adapter->pool_config = 0;
1852 				pool->buff_size = value;
1853 				if ((rc = ibmveth_open(netdev)))
1854 					return rc;
1855 			} else {
1856 				pool->buff_size = value;
1857 			}
1858 		}
1859 	}
1860 
1861 	/* kick the interrupt handler to allocate/deallocate pools */
1862 	ibmveth_interrupt(netdev->irq, netdev);
1863 	return count;
1864 }
1865 
1866 
1867 #define ATTR(_name, _mode)				\
1868 	struct attribute veth_##_name##_attr = {	\
1869 	.name = __stringify(_name), .mode = _mode,	\
1870 	};
1871 
1872 static ATTR(active, 0644);
1873 static ATTR(num, 0644);
1874 static ATTR(size, 0644);
1875 
1876 static struct attribute *veth_pool_attrs[] = {
1877 	&veth_active_attr,
1878 	&veth_num_attr,
1879 	&veth_size_attr,
1880 	NULL,
1881 };
1882 
1883 static const struct sysfs_ops veth_pool_ops = {
1884 	.show   = veth_pool_show,
1885 	.store  = veth_pool_store,
1886 };
1887 
1888 static struct kobj_type ktype_veth_pool = {
1889 	.release        = NULL,
1890 	.sysfs_ops      = &veth_pool_ops,
1891 	.default_attrs  = veth_pool_attrs,
1892 };
1893 
1894 static int ibmveth_resume(struct device *dev)
1895 {
1896 	struct net_device *netdev = dev_get_drvdata(dev);
1897 	ibmveth_interrupt(netdev->irq, netdev);
1898 	return 0;
1899 }
1900 
1901 static const struct vio_device_id ibmveth_device_table[] = {
1902 	{ "network", "IBM,l-lan"},
1903 	{ "", "" }
1904 };
1905 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1906 
1907 static const struct dev_pm_ops ibmveth_pm_ops = {
1908 	.resume = ibmveth_resume
1909 };
1910 
1911 static struct vio_driver ibmveth_driver = {
1912 	.id_table	= ibmveth_device_table,
1913 	.probe		= ibmveth_probe,
1914 	.remove		= ibmveth_remove,
1915 	.get_desired_dma = ibmveth_get_desired_dma,
1916 	.name		= ibmveth_driver_name,
1917 	.pm		= &ibmveth_pm_ops,
1918 };
1919 
1920 static int __init ibmveth_module_init(void)
1921 {
1922 	printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1923 	       ibmveth_driver_string, ibmveth_driver_version);
1924 
1925 	return vio_register_driver(&ibmveth_driver);
1926 }
1927 
1928 static void __exit ibmveth_module_exit(void)
1929 {
1930 	vio_unregister_driver(&ibmveth_driver);
1931 }
1932 
1933 module_init(ibmveth_module_init);
1934 module_exit(ibmveth_module_exit);
1935