blob: dfa2ee7a92c8537c333982bc65bbdda4af17228e [file] [log] [blame]
Santiago Leonf148f612010-09-03 18:29:30 +00001/*
2 * IBM eServer i/pSeries Virtual Ethernet Device Driver
3 * Copyright (C) 2003 IBM Corp.
4 * Originally written by Dave Larson (larson1@us.ibm.com)
5 * Maintained by Santiago Leon (santil@us.ibm.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * This module contains the implementation of a virtual ethernet device
23 * for use with IBM i/pSeries LPAR Linux. It utilizes the logical LAN
24 * option of the RS/6000 Platform Architechture to interface with virtual
25 * ethernet NICs that are presented to the partition by the hypervisor.
26 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
Robert Jennings1096d632008-07-24 04:34:52 +100029#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/dma-mapping.h>
33#include <linux/kernel.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
37#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mm.h>
Brian Kinge7a3af52010-05-07 08:56:08 +000039#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ethtool.h>
Brian Kingf4ff2872007-09-15 13:36:07 -070041#include <linux/in.h>
42#include <linux/ip.h>
Santiago Leonab78df72010-09-03 18:28:52 +000043#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/hvcall.h>
46#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/vio.h>
Robert Jennings1096d632008-07-24 04:34:52 +100048#include <asm/iommu.h>
Robert Jennings1096d632008-07-24 04:34:52 +100049#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "ibmveth.h"
52
David Howells7d12e782006-10-05 14:55:46 +010053static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
Michael Ellerman493a6842007-04-17 13:12:55 +100054static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Robert Jennings1096d632008-07-24 04:34:52 +100055static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
Santiago Leone295fe82010-09-03 18:29:08 +000056
Santiago Leon860f2422006-04-25 11:19:59 -050057static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Robert Jennings1096d632008-07-24 04:34:52 +100059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static const char ibmveth_driver_name[] = "ibmveth";
Santiago Leonf148f612010-09-03 18:29:30 +000061static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet "
62 "Driver";
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define ibmveth_driver_version "1.03"
64
65MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
66MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
67MODULE_LICENSE("GPL");
68MODULE_VERSION(ibmveth_driver_version);
69
Santiago Leonc08cc3c2010-09-03 18:28:20 +000070static unsigned int tx_copybreak __read_mostly = 128;
71module_param(tx_copybreak, uint, 0644);
72MODULE_PARM_DESC(tx_copybreak,
73 "Maximum size of packet that is copied to a new buffer on transmit");
74
Santiago Leon8d86c612010-09-03 18:28:25 +000075static unsigned int rx_copybreak __read_mostly = 128;
76module_param(rx_copybreak, uint, 0644);
77MODULE_PARM_DESC(rx_copybreak,
78 "Maximum size of packet that is copied to a new buffer on receive");
79
Santiago Leon0c26b672010-09-03 18:28:41 +000080static unsigned int rx_flush __read_mostly = 0;
81module_param(rx_flush, uint, 0644);
82MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");
83
Brian Kingddbb4de2007-08-17 09:16:43 -050084struct ibmveth_stat {
85 char name[ETH_GSTRING_LEN];
86 int offset;
87};
88
89#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
90#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
91
92struct ibmveth_stat ibmveth_stats[] = {
93 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
94 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
Santiago Leonf148f612010-09-03 18:29:30 +000095 { "replenish_add_buff_failure",
96 IBMVETH_STAT_OFF(replenish_add_buff_failure) },
97 { "replenish_add_buff_success",
98 IBMVETH_STAT_OFF(replenish_add_buff_success) },
Brian Kingddbb4de2007-08-17 09:16:43 -050099 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
100 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500101 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
102 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
Santiago Leonab78df72010-09-03 18:28:52 +0000103 { "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
104 { "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500108static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
109{
110 return adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off;
111}
112
113static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
114{
Santiago Leonf148f612010-09-03 18:29:30 +0000115 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
116 IBMVETH_RXQ_TOGGLE_SHIFT;
Brian King79ef4a42007-08-17 09:16:56 -0500117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
120{
Santiago Leonf148f612010-09-03 18:29:30 +0000121 return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
125{
Santiago Leonf148f612010-09-03 18:29:30 +0000126 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
130{
Santiago Leonf148f612010-09-03 18:29:30 +0000131 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
135{
Santiago Leonf148f612010-09-03 18:29:30 +0000136 return adapter->rx_queue.queue_addr[adapter->rx_queue.index].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Brian Kingf4ff2872007-09-15 13:36:07 -0700139static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
140{
Santiago Leonf148f612010-09-03 18:29:30 +0000141 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
Brian Kingf4ff2872007-09-15 13:36:07 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* setup the initial settings for a buffer pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000145static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
146 u32 pool_index, u32 pool_size,
147 u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 pool->size = pool_size;
150 pool->index = pool_index;
151 pool->buff_size = buff_size;
Santiago Leonc033a6d2010-09-03 18:28:09 +0000152 pool->threshold = pool_size * 7 / 8;
Santiago Leon860f2422006-04-25 11:19:59 -0500153 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/* allocate and setup an buffer pool - called during open */
157static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
158{
159 int i;
160
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400161 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Santiago Leonf148f612010-09-03 18:29:30 +0000163 if (!pool->free_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400166 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000167 if (!pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 kfree(pool->free_map);
169 pool->free_map = NULL;
170 return -1;
171 }
172
Julia Lawalla05abcb2010-05-13 10:06:01 +0000173 pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Santiago Leonf148f612010-09-03 18:29:30 +0000175 if (!pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(pool->dma_addr);
177 pool->dma_addr = NULL;
178
179 kfree(pool->free_map);
180 pool->free_map = NULL;
181 return -1;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
185
Santiago Leonf148f612010-09-03 18:29:30 +0000186 for (i = 0; i < pool->size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 pool->free_map[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 atomic_set(&pool->available, 0);
190 pool->producer_index = 0;
191 pool->consumer_index = 0;
192
193 return 0;
194}
195
Santiago Leon0c26b672010-09-03 18:28:41 +0000196static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
197{
198 unsigned long offset;
199
200 for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
201 asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* replenish the buffers for a pool. note that we don't need to
205 * skb_reserve these since they are used for incoming...
206 */
Santiago Leonf148f612010-09-03 18:29:30 +0000207static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
208 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 u32 i;
211 u32 count = pool->size - atomic_read(&pool->available);
212 u32 buffers_added = 0;
Robert Jennings1096d632008-07-24 04:34:52 +1000213 struct sk_buff *skb;
214 unsigned int free_index, index;
215 u64 correlator;
216 unsigned long lpar_rc;
217 dma_addr_t dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 mb();
220
Santiago Leonf148f612010-09-03 18:29:30 +0000221 for (i = 0; i < count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Santiago Leon003212c2010-09-03 18:29:03 +0000224 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Santiago Leonf148f612010-09-03 18:29:30 +0000226 if (!skb) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000227 netdev_dbg(adapter->netdev,
228 "replenish: unable to allocate skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 adapter->replenish_no_mem++;
230 break;
231 }
232
David Gibson047a66d2006-10-21 10:24:13 -0700233 free_index = pool->consumer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000234 pool->consumer_index++;
235 if (pool->consumer_index >= pool->size)
236 pool->consumer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400238
Santiago Leon64859112010-09-03 18:29:41 +0000239 BUG_ON(index == IBM_VETH_INVALID_MAP);
240 BUG_ON(pool->skbuff[index] != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
243 pool->buff_size, DMA_FROM_DEVICE);
244
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000245 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000246 goto failure;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
249 pool->dma_addr[index] = dma_addr;
250 pool->skbuff[index] = skb;
251
252 correlator = ((u64)pool->index << 32) | index;
Santiago Leonf148f612010-09-03 18:29:30 +0000253 *(u64 *)skb->data = correlator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Brian King79ef4a42007-08-17 09:16:56 -0500255 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400256 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Santiago Leon0c26b672010-09-03 18:28:41 +0000258 if (rx_flush) {
259 unsigned int len = min(pool->buff_size,
260 adapter->netdev->mtu +
261 IBMVETH_BUFF_OH);
262 ibmveth_flush_buffer(skb->data, len);
263 }
Santiago Leonf148f612010-09-03 18:29:30 +0000264 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
265 desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400266
Santiago Leonf148f612010-09-03 18:29:30 +0000267 if (lpar_rc != H_SUCCESS) {
Robert Jennings1096d632008-07-24 04:34:52 +1000268 goto failure;
Santiago Leonf148f612010-09-03 18:29:30 +0000269 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 buffers_added++;
271 adapter->replenish_add_buff_success++;
272 }
273 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 mb();
276 atomic_add(buffers_added, &(pool->available));
Robert Jennings1096d632008-07-24 04:34:52 +1000277 return;
278
279failure:
280 pool->free_map[free_index] = index;
281 pool->skbuff[index] = NULL;
282 if (pool->consumer_index == 0)
283 pool->consumer_index = pool->size - 1;
284 else
285 pool->consumer_index--;
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000286 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000287 dma_unmap_single(&adapter->vdev->dev,
288 pool->dma_addr[index], pool->buff_size,
289 DMA_FROM_DEVICE);
290 dev_kfree_skb_any(skb);
291 adapter->replenish_add_buff_failure++;
292
293 mb();
294 atomic_add(buffers_added, &(pool->available));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Santiago Leone2adbcb2005-10-26 10:47:08 -0600297/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400298static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Santiago Leonb6d35182005-10-26 10:47:01 -0600300 int i;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 adapter->replenish_task_cycles++;
303
Santiago Leon517e80e2010-09-03 18:29:25 +0000304 for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
Santiago Leonc033a6d2010-09-03 18:28:09 +0000305 struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
306
307 if (pool->active &&
308 (atomic_read(&pool->available) < pool->threshold))
309 ibmveth_replenish_buffer_pool(adapter, pool);
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Santiago Leonf148f612010-09-03 18:29:30 +0000312 adapter->rx_no_buffer = *(u64 *)(((char*)adapter->buffer_list_addr) +
313 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/* empty and free ana buffer pool - also used to do cleanup in error paths */
Santiago Leonf148f612010-09-03 18:29:30 +0000317static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
318 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int i;
321
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400322 kfree(pool->free_map);
323 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Santiago Leonf148f612010-09-03 18:29:30 +0000325 if (pool->skbuff && pool->dma_addr) {
326 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct sk_buff *skb = pool->skbuff[i];
Santiago Leonf148f612010-09-03 18:29:30 +0000328 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 dma_unmap_single(&adapter->vdev->dev,
330 pool->dma_addr[i],
331 pool->buff_size,
332 DMA_FROM_DEVICE);
333 dev_kfree_skb_any(skb);
334 pool->skbuff[i] = NULL;
335 }
336 }
337 }
338
Santiago Leonf148f612010-09-03 18:29:30 +0000339 if (pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 kfree(pool->dma_addr);
341 pool->dma_addr = NULL;
342 }
343
Santiago Leonf148f612010-09-03 18:29:30 +0000344 if (pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 kfree(pool->skbuff);
346 pool->skbuff = NULL;
347 }
348}
349
350/* remove a buffer from a pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000351static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
352 u64 correlator)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 unsigned int pool = correlator >> 32;
355 unsigned int index = correlator & 0xffffffffUL;
356 unsigned int free_index;
357 struct sk_buff *skb;
358
Santiago Leon64859112010-09-03 18:29:41 +0000359 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
360 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 skb = adapter->rx_buff_pool[pool].skbuff[index];
363
Santiago Leon64859112010-09-03 18:29:41 +0000364 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
367
368 dma_unmap_single(&adapter->vdev->dev,
369 adapter->rx_buff_pool[pool].dma_addr[index],
370 adapter->rx_buff_pool[pool].buff_size,
371 DMA_FROM_DEVICE);
372
David Gibson047a66d2006-10-21 10:24:13 -0700373 free_index = adapter->rx_buff_pool[pool].producer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000374 adapter->rx_buff_pool[pool].producer_index++;
375 if (adapter->rx_buff_pool[pool].producer_index >=
376 adapter->rx_buff_pool[pool].size)
377 adapter->rx_buff_pool[pool].producer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 adapter->rx_buff_pool[pool].free_map[free_index] = index;
379
380 mb();
381
382 atomic_dec(&(adapter->rx_buff_pool[pool].available));
383}
384
385/* get the current buffer on the rx queue */
386static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
387{
388 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
389 unsigned int pool = correlator >> 32;
390 unsigned int index = correlator & 0xffffffffUL;
391
Santiago Leon64859112010-09-03 18:29:41 +0000392 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
393 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return adapter->rx_buff_pool[pool].skbuff[index];
396}
397
398/* recycle the current buffer on the rx queue */
399static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
400{
401 u32 q_index = adapter->rx_queue.index;
402 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
403 unsigned int pool = correlator >> 32;
404 unsigned int index = correlator & 0xffffffffUL;
405 union ibmveth_buf_desc desc;
406 unsigned long lpar_rc;
407
Santiago Leon64859112010-09-03 18:29:41 +0000408 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
409 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Santiago Leonf148f612010-09-03 18:29:30 +0000411 if (!adapter->rx_buff_pool[pool].active) {
Santiago Leonb6d35182005-10-26 10:47:01 -0600412 ibmveth_rxq_harvest_buffer(adapter);
413 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
414 return;
415 }
416
Brian King79ef4a42007-08-17 09:16:56 -0500417 desc.fields.flags_len = IBMVETH_BUF_VALID |
418 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
420
421 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400422
Santiago Leonf148f612010-09-03 18:29:30 +0000423 if (lpar_rc != H_SUCCESS) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000424 netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
425 "during recycle rc=%ld", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
427 }
428
Santiago Leonf148f612010-09-03 18:29:30 +0000429 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 adapter->rx_queue.index = 0;
431 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
432 }
433}
434
Michael Ellerman493a6842007-04-17 13:12:55 +1000435static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
438
Santiago Leonf148f612010-09-03 18:29:30 +0000439 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 adapter->rx_queue.index = 0;
441 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
442 }
443}
444
445static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
446{
Santiago Leonb6d35182005-10-26 10:47:01 -0600447 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700448 struct device *dev = &adapter->vdev->dev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600449
Santiago Leonf148f612010-09-03 18:29:30 +0000450 if (adapter->buffer_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700451 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
452 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 DMA_BIDIRECTIONAL);
454 adapter->buffer_list_dma = DMA_ERROR_CODE;
455 }
456 free_page((unsigned long)adapter->buffer_list_addr);
457 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Santiago Leonf148f612010-09-03 18:29:30 +0000460 if (adapter->filter_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700461 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
462 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 DMA_BIDIRECTIONAL);
464 adapter->filter_list_dma = DMA_ERROR_CODE;
465 }
466 free_page((unsigned long)adapter->filter_list_addr);
467 adapter->filter_list_addr = NULL;
468 }
469
Santiago Leonf148f612010-09-03 18:29:30 +0000470 if (adapter->rx_queue.queue_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700471 if (!dma_mapping_error(dev, adapter->rx_queue.queue_dma)) {
472 dma_unmap_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 adapter->rx_queue.queue_dma,
474 adapter->rx_queue.queue_len,
475 DMA_BIDIRECTIONAL);
476 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
477 }
478 kfree(adapter->rx_queue.queue_addr);
479 adapter->rx_queue.queue_addr = NULL;
480 }
481
Santiago Leonf148f612010-09-03 18:29:30 +0000482 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500483 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400484 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500485 &adapter->rx_buff_pool[i]);
Robert Jennings1096d632008-07-24 04:34:52 +1000486
487 if (adapter->bounce_buffer != NULL) {
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000488 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Robert Jennings1096d632008-07-24 04:34:52 +1000489 dma_unmap_single(&adapter->vdev->dev,
490 adapter->bounce_buffer_dma,
491 adapter->netdev->mtu + IBMVETH_BUFF_OH,
492 DMA_BIDIRECTIONAL);
493 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
494 }
495 kfree(adapter->bounce_buffer);
496 adapter->bounce_buffer = NULL;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500500static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
501 union ibmveth_buf_desc rxq_desc, u64 mac_address)
502{
503 int rc, try_again = 1;
504
Santiago Leonf148f612010-09-03 18:29:30 +0000505 /*
506 * After a kexec the adapter will still be open, so our attempt to
507 * open it will fail. So if we get a failure we free the adapter and
508 * try again, but only once.
509 */
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500510retry:
511 rc = h_register_logical_lan(adapter->vdev->unit_address,
512 adapter->buffer_list_dma, rxq_desc.desc,
513 adapter->filter_list_dma, mac_address);
514
515 if (rc != H_SUCCESS && try_again) {
516 do {
517 rc = h_free_logical_lan(adapter->vdev->unit_address);
518 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
519
520 try_again = 0;
521 goto retry;
522 }
523
524 return rc;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static int ibmveth_open(struct net_device *netdev)
528{
Wang Chen4cf16532008-11-12 23:38:14 -0800529 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 u64 mac_address = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600531 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned long lpar_rc;
533 int rc;
534 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600535 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700536 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Santiago Leonc43ced12010-09-03 18:29:14 +0000538 netdev_dbg(netdev, "open starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700540 napi_enable(&adapter->napi);
541
Santiago Leon517e80e2010-09-03 18:29:25 +0000542 for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leonb6d35182005-10-26 10:47:01 -0600543 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
546 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400547
Santiago Leonf148f612010-09-03 18:29:30 +0000548 if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000549 netdev_err(netdev, "unable to allocate filter or buffer list "
550 "pages\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700552 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -ENOMEM;
554 }
555
Santiago Leonf148f612010-09-03 18:29:30 +0000556 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
557 rxq_entries;
558 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len,
559 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Santiago Leonf148f612010-09-03 18:29:30 +0000561 if (!adapter->rx_queue.queue_addr) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000562 netdev_err(netdev, "unable to allocate rx queue pages\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700564 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -ENOMEM;
566 }
567
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700568 dev = &adapter->vdev->dev;
569
570 adapter->buffer_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700572 adapter->filter_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700574 adapter->rx_queue.queue_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 adapter->rx_queue.queue_addr,
576 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
577
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700578 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
579 (dma_mapping_error(dev, adapter->filter_list_dma)) ||
580 (dma_mapping_error(dev, adapter->rx_queue.queue_dma))) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000581 netdev_err(netdev, "unable to map filter or buffer list "
582 "pages\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700584 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ENOMEM;
586 }
587
588 adapter->rx_queue.index = 0;
589 adapter->rx_queue.num_slots = rxq_entries;
590 adapter->rx_queue.toggle = 1;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
593 mac_address = mac_address >> 16;
594
Santiago Leonf148f612010-09-03 18:29:30 +0000595 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
596 adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
598
Santiago Leonc43ced12010-09-03 18:29:14 +0000599 netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
600 netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
601 netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Santiago Leon4347ef12006-10-03 12:24:34 -0500603 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
604
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500605 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Santiago Leonf148f612010-09-03 18:29:30 +0000607 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000608 netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
609 lpar_rc);
610 netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
611 "desc:0x%llx MAC:0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 adapter->buffer_list_dma,
613 adapter->filter_list_dma,
614 rxq_desc.desc,
615 mac_address);
616 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700617 napi_disable(&adapter->napi);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400618 return -ENONET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Santiago Leonf148f612010-09-03 18:29:30 +0000621 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
622 if (!adapter->rx_buff_pool[i].active)
Santiago Leon860f2422006-04-25 11:19:59 -0500623 continue;
624 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000625 netdev_err(netdev, "unable to alloc pool\n");
Santiago Leon860f2422006-04-25 11:19:59 -0500626 adapter->rx_buff_pool[i].active = 0;
627 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700628 napi_disable(&adapter->napi);
Santiago Leon860f2422006-04-25 11:19:59 -0500629 return -ENOMEM ;
630 }
631 }
632
Santiago Leonc43ced12010-09-03 18:29:14 +0000633 netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
Santiago Leonf148f612010-09-03 18:29:30 +0000634 rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
635 netdev);
636 if (rc != 0) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000637 netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
638 netdev->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 do {
640 rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200641 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700644 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return rc;
646 }
647
Robert Jennings1096d632008-07-24 04:34:52 +1000648 adapter->bounce_buffer =
649 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
650 if (!adapter->bounce_buffer) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000651 netdev_err(netdev, "unable to allocate bounce buffer\n");
Robert Jennings1096d632008-07-24 04:34:52 +1000652 ibmveth_cleanup(adapter);
653 napi_disable(&adapter->napi);
654 return -ENOMEM;
655 }
656 adapter->bounce_buffer_dma =
657 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
658 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700659 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000660 netdev_err(netdev, "unable to map bounce buffer\n");
Robert Jennings1096d632008-07-24 04:34:52 +1000661 ibmveth_cleanup(adapter);
662 napi_disable(&adapter->napi);
663 return -ENOMEM;
664 }
665
Santiago Leonc43ced12010-09-03 18:29:14 +0000666 netdev_dbg(netdev, "initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100667 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Santiago Leone2adbcb2005-10-26 10:47:08 -0600669 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Santiago Leonc43ced12010-09-03 18:29:14 +0000671 netdev_dbg(netdev, "open complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 return 0;
674}
675
676static int ibmveth_close(struct net_device *netdev)
677{
Wang Chen4cf16532008-11-12 23:38:14 -0800678 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400680
Santiago Leonc43ced12010-09-03 18:29:14 +0000681 netdev_dbg(netdev, "close starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700683 napi_disable(&adapter->napi);
684
Santiago Leon860f2422006-04-25 11:19:59 -0500685 if (!adapter->pool_config)
686 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Robert Jenningsee2e6112010-07-16 04:57:25 +0000688 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 do {
691 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200692 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Santiago Leonf148f612010-09-03 18:29:30 +0000694 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000695 netdev_err(netdev, "h_free_logical_lan failed with %lx, "
696 "continuing with close\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Robert Jenningsee2e6112010-07-16 04:57:25 +0000699 free_irq(netdev->irq, netdev);
700
Santiago Leonf148f612010-09-03 18:29:30 +0000701 adapter->rx_no_buffer = *(u64 *)(((char *)adapter->buffer_list_addr) +
702 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 ibmveth_cleanup(adapter);
705
Santiago Leonc43ced12010-09-03 18:29:14 +0000706 netdev_dbg(netdev, "close complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 return 0;
709}
710
Santiago Leonf148f612010-09-03 18:29:30 +0000711static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
712{
713 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
714 SUPPORTED_FIBRE);
715 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
716 ADVERTISED_FIBRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cmd->speed = SPEED_1000;
718 cmd->duplex = DUPLEX_FULL;
719 cmd->port = PORT_FIBRE;
720 cmd->phy_address = 0;
721 cmd->transceiver = XCVR_INTERNAL;
722 cmd->autoneg = AUTONEG_ENABLE;
723 cmd->maxtxpkt = 0;
724 cmd->maxrxpkt = 1;
725 return 0;
726}
727
Santiago Leonf148f612010-09-03 18:29:30 +0000728static void netdev_get_drvinfo(struct net_device *dev,
729 struct ethtool_drvinfo *info)
730{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
Santiago Leonf148f612010-09-03 18:29:30 +0000732 strncpy(info->version, ibmveth_driver_version,
733 sizeof(info->version) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Santiago Leonf148f612010-09-03 18:29:30 +0000736static u32 netdev_get_link(struct net_device *dev)
737{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 1;
739}
740
Brian King5fc7e012007-08-17 09:16:31 -0500741static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
742{
Wang Chen4cf16532008-11-12 23:38:14 -0800743 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian King5fc7e012007-08-17 09:16:31 -0500744
Santiago Leonf148f612010-09-03 18:29:30 +0000745 if (data) {
Brian King5fc7e012007-08-17 09:16:31 -0500746 adapter->rx_csum = 1;
Santiago Leonf148f612010-09-03 18:29:30 +0000747 } else {
Brian King5fc7e012007-08-17 09:16:31 -0500748 /*
Santiago Leonf148f612010-09-03 18:29:30 +0000749 * Since the ibmveth firmware interface does not have the
750 * concept of separate tx/rx checksum offload enable, if rx
751 * checksum is disabled we also have to disable tx checksum
752 * offload. Once we disable rx checksum offload, we are no
753 * longer allowed to send tx buffers that are not properly
754 * checksummed.
Brian King5fc7e012007-08-17 09:16:31 -0500755 */
756 adapter->rx_csum = 0;
757 dev->features &= ~NETIF_F_IP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000758 dev->features &= ~NETIF_F_IPV6_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -0500759 }
760}
761
762static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
763{
Wang Chen4cf16532008-11-12 23:38:14 -0800764 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian King5fc7e012007-08-17 09:16:31 -0500765
766 if (data) {
Santiago Leonab78df72010-09-03 18:28:52 +0000767 if (adapter->fw_ipv4_csum_support)
768 dev->features |= NETIF_F_IP_CSUM;
769 if (adapter->fw_ipv6_csum_support)
770 dev->features |= NETIF_F_IPV6_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -0500771 adapter->rx_csum = 1;
Santiago Leonab78df72010-09-03 18:28:52 +0000772 } else {
Brian King5fc7e012007-08-17 09:16:31 -0500773 dev->features &= ~NETIF_F_IP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000774 dev->features &= ~NETIF_F_IPV6_CSUM;
775 }
Brian King5fc7e012007-08-17 09:16:31 -0500776}
777
778static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
779 void (*done) (struct net_device *, u32))
780{
Wang Chen4cf16532008-11-12 23:38:14 -0800781 struct ibmveth_adapter *adapter = netdev_priv(dev);
Stephen Rothwellff5bfc32009-01-06 10:47:44 -0800782 unsigned long set_attr, clr_attr, ret_attr;
Santiago Leonab78df72010-09-03 18:28:52 +0000783 unsigned long set_attr6, clr_attr6;
784 long ret, ret6;
Brian King5fc7e012007-08-17 09:16:31 -0500785 int rc1 = 0, rc2 = 0;
786 int restart = 0;
787
788 if (netif_running(dev)) {
789 restart = 1;
790 adapter->pool_config = 1;
791 ibmveth_close(dev);
792 adapter->pool_config = 0;
793 }
794
Brian King79ef4a42007-08-17 09:16:56 -0500795 set_attr = 0;
796 clr_attr = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500797
Santiago Leonab78df72010-09-03 18:28:52 +0000798 if (data) {
Brian King79ef4a42007-08-17 09:16:56 -0500799 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000800 set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
801 } else {
Brian King79ef4a42007-08-17 09:16:56 -0500802 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000803 clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
804 }
Brian King5fc7e012007-08-17 09:16:31 -0500805
Brian King79ef4a42007-08-17 09:16:56 -0500806 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500807
Brian King79ef4a42007-08-17 09:16:56 -0500808 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
809 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
810 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
811 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
812 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500813
814 if (ret != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000815 netdev_err(dev, "unable to change IPv4 checksum "
816 "offload settings. %d rc=%ld\n",
817 data, ret);
Brian King5fc7e012007-08-17 09:16:31 -0500818
819 ret = h_illan_attributes(adapter->vdev->unit_address,
Brian King79ef4a42007-08-17 09:16:56 -0500820 set_attr, clr_attr, &ret_attr);
Santiago Leonf148f612010-09-03 18:29:30 +0000821 } else {
Santiago Leonab78df72010-09-03 18:28:52 +0000822 adapter->fw_ipv4_csum_support = data;
Santiago Leonf148f612010-09-03 18:29:30 +0000823 }
Santiago Leonab78df72010-09-03 18:28:52 +0000824
825 ret6 = h_illan_attributes(adapter->vdev->unit_address,
826 clr_attr6, set_attr6, &ret_attr);
827
828 if (ret6 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000829 netdev_err(dev, "unable to change IPv6 checksum "
830 "offload settings. %d rc=%ld\n",
831 data, ret);
Santiago Leonab78df72010-09-03 18:28:52 +0000832
833 ret = h_illan_attributes(adapter->vdev->unit_address,
834 set_attr6, clr_attr6,
835 &ret_attr);
836 } else
837 adapter->fw_ipv6_csum_support = data;
838
839 if (ret == H_SUCCESS || ret6 == H_SUCCESS)
Brian King5fc7e012007-08-17 09:16:31 -0500840 done(dev, data);
Santiago Leonab78df72010-09-03 18:28:52 +0000841 else
842 rc1 = -EIO;
Brian King5fc7e012007-08-17 09:16:31 -0500843 } else {
844 rc1 = -EIO;
Santiago Leon21c2dec2010-09-03 18:29:19 +0000845 netdev_err(dev, "unable to change checksum offload settings."
846 " %d rc=%ld ret_attr=%lx\n", data, ret,
847 ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500848 }
849
850 if (restart)
851 rc2 = ibmveth_open(dev);
852
853 return rc1 ? rc1 : rc2;
854}
855
856static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
857{
Wang Chen4cf16532008-11-12 23:38:14 -0800858 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian King5fc7e012007-08-17 09:16:31 -0500859
860 if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
861 return 0;
862
863 return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
864}
865
866static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
867{
Wang Chen4cf16532008-11-12 23:38:14 -0800868 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian King5fc7e012007-08-17 09:16:31 -0500869 int rc = 0;
870
Santiago Leonab78df72010-09-03 18:28:52 +0000871 if (data && (dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Brian King5fc7e012007-08-17 09:16:31 -0500872 return 0;
Santiago Leonab78df72010-09-03 18:28:52 +0000873 if (!data && !(dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Brian King5fc7e012007-08-17 09:16:31 -0500874 return 0;
875
876 if (data && !adapter->rx_csum)
Santiago Leonf148f612010-09-03 18:29:30 +0000877 rc = ibmveth_set_csum_offload(dev, data,
878 ibmveth_set_tx_csum_flags);
Brian King5fc7e012007-08-17 09:16:31 -0500879 else
880 ibmveth_set_tx_csum_flags(dev, data);
881
882 return rc;
883}
884
885static u32 ibmveth_get_rx_csum(struct net_device *dev)
886{
Wang Chen4cf16532008-11-12 23:38:14 -0800887 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian King5fc7e012007-08-17 09:16:31 -0500888 return adapter->rx_csum;
889}
890
Brian Kingddbb4de2007-08-17 09:16:43 -0500891static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
892{
893 int i;
894
895 if (stringset != ETH_SS_STATS)
896 return;
897
898 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
899 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
900}
901
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700902static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500903{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700904 switch (sset) {
905 case ETH_SS_STATS:
906 return ARRAY_SIZE(ibmveth_stats);
907 default:
908 return -EOPNOTSUPP;
909 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500910}
911
912static void ibmveth_get_ethtool_stats(struct net_device *dev,
913 struct ethtool_stats *stats, u64 *data)
914{
915 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800916 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian Kingddbb4de2007-08-17 09:16:43 -0500917
918 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
919 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
920}
921
Jeff Garzik7282d492006-09-13 14:30:00 -0400922static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 .get_drvinfo = netdev_get_drvinfo,
924 .get_settings = netdev_get_settings,
925 .get_link = netdev_get_link,
Brian King5fc7e012007-08-17 09:16:31 -0500926 .set_tx_csum = ibmveth_set_tx_csum,
927 .get_rx_csum = ibmveth_get_rx_csum,
928 .set_rx_csum = ibmveth_set_rx_csum,
Brian Kingddbb4de2007-08-17 09:16:43 -0500929 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700930 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500931 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Santiago Leon6e8ab302010-09-03 18:28:36 +0000932 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933};
934
935static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
936{
937 return -EOPNOTSUPP;
938}
939
940#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
941
Santiago Leon6e8ab302010-09-03 18:28:36 +0000942static int ibmveth_send(struct ibmveth_adapter *adapter,
943 union ibmveth_buf_desc *descs)
944{
945 unsigned long correlator;
946 unsigned int retry_count;
947 unsigned long ret;
948
949 /*
950 * The retry count sets a maximum for the number of broadcast and
951 * multicast destinations within the system.
952 */
953 retry_count = 1024;
954 correlator = 0;
955 do {
956 ret = h_send_logical_lan(adapter->vdev->unit_address,
957 descs[0].desc, descs[1].desc,
958 descs[2].desc, descs[3].desc,
959 descs[4].desc, descs[5].desc,
960 correlator, &correlator);
961 } while ((ret == H_BUSY) && (retry_count--));
962
963 if (ret != H_SUCCESS && ret != H_DROPPED) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000964 netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
965 "with rc=%ld\n", ret);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000966 return 1;
967 }
968
969 return 0;
970}
971
Stephen Hemminger613573252009-08-31 19:50:58 +0000972static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
973 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Wang Chen4cf16532008-11-12 23:38:14 -0800975 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000976 unsigned int desc_flags;
977 union ibmveth_buf_desc descs[6];
978 int last, i;
979 int force_bounce = 0;
Santiago Leon60296d92005-10-26 10:47:16 -0600980
Santiago Leon6e8ab302010-09-03 18:28:36 +0000981 /*
982 * veth handles a maximum of 6 segments including the header, so
983 * we have to linearize the skb if there are more than this.
984 */
985 if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
986 netdev->stats.tx_dropped++;
Brian Kingf4ff2872007-09-15 13:36:07 -0700987 goto out;
988 }
989
Santiago Leon6e8ab302010-09-03 18:28:36 +0000990 /* veth can't checksum offload UDP */
991 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Santiago Leonab78df72010-09-03 18:28:52 +0000992 ((skb->protocol == htons(ETH_P_IP) &&
993 ip_hdr(skb)->protocol != IPPROTO_TCP) ||
994 (skb->protocol == htons(ETH_P_IPV6) &&
995 ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
996 skb_checksum_help(skb)) {
997
Santiago Leon21c2dec2010-09-03 18:29:19 +0000998 netdev_err(netdev, "tx: failed to checksum packet\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +0000999 netdev->stats.tx_dropped++;
1000 goto out;
1001 }
Brian Kingf4ff2872007-09-15 13:36:07 -07001002
Santiago Leon6e8ab302010-09-03 18:28:36 +00001003 desc_flags = IBMVETH_BUF_VALID;
1004
1005 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1006 unsigned char *buf = skb_transport_header(skb) +
1007 skb->csum_offset;
1008
1009 desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -07001010
1011 /* Need to zero out the checksum */
1012 buf[0] = 0;
1013 buf[1] = 0;
1014 }
1015
Santiago Leon6e8ab302010-09-03 18:28:36 +00001016retry_bounce:
1017 memset(descs, 0, sizeof(descs));
Santiago Leonc08cc3c2010-09-03 18:28:20 +00001018
Santiago Leon6e8ab302010-09-03 18:28:36 +00001019 /*
1020 * If a linear packet is below the rx threshold then
1021 * copy it into the static bounce buffer. This avoids the
1022 * cost of a TCE insert and remove.
1023 */
1024 if (force_bounce || (!skb_is_nonlinear(skb) &&
1025 (skb->len < tx_copybreak))) {
Robert Jennings1096d632008-07-24 04:34:52 +10001026 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
1027 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Santiago Leon6e8ab302010-09-03 18:28:36 +00001029 descs[0].fields.flags_len = desc_flags | skb->len;
1030 descs[0].fields.address = adapter->bounce_buffer_dma;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001031
Santiago Leon6e8ab302010-09-03 18:28:36 +00001032 if (ibmveth_send(adapter, descs)) {
1033 adapter->tx_send_failed++;
1034 netdev->stats.tx_dropped++;
1035 } else {
1036 netdev->stats.tx_packets++;
1037 netdev->stats.tx_bytes += skb->len;
1038 }
1039
1040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
Santiago Leon6e8ab302010-09-03 18:28:36 +00001043 /* Map the header */
1044 descs[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
1045 skb_headlen(skb),
1046 DMA_TO_DEVICE);
1047 if (dma_mapping_error(&adapter->vdev->dev, descs[0].fields.address))
1048 goto map_failed;
1049
1050 descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
1051
1052 /* Map the frags */
1053 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1054 unsigned long dma_addr;
1055 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1056
1057 dma_addr = dma_map_page(&adapter->vdev->dev, frag->page,
1058 frag->page_offset, frag->size,
1059 DMA_TO_DEVICE);
1060
1061 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1062 goto map_failed_frags;
1063
1064 descs[i+1].fields.flags_len = desc_flags | frag->size;
1065 descs[i+1].fields.address = dma_addr;
1066 }
1067
1068 if (ibmveth_send(adapter, descs)) {
1069 adapter->tx_send_failed++;
1070 netdev->stats.tx_dropped++;
1071 } else {
1072 netdev->stats.tx_packets++;
1073 netdev->stats.tx_bytes += skb->len;
1074 }
1075
1076 for (i = 0; i < skb_shinfo(skb)->nr_frags + 1; i++)
1077 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1078 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1079 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Santiago Leone8cb7eb2010-09-03 18:28:15 +00001081out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001083 return NETDEV_TX_OK;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001084
1085map_failed_frags:
1086 last = i+1;
1087 for (i = 0; i < last; i++)
1088 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1089 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1090 DMA_TO_DEVICE);
1091
1092map_failed:
1093 if (!firmware_has_feature(FW_FEATURE_CMO))
Santiago Leon21c2dec2010-09-03 18:29:19 +00001094 netdev_err(netdev, "tx: unable to map xmit buffer\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +00001095 adapter->tx_map_failed++;
1096 skb_linearize(skb);
1097 force_bounce = 1;
1098 goto retry_bounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001101static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Santiago Leonf148f612010-09-03 18:29:30 +00001103 struct ibmveth_adapter *adapter =
1104 container_of(napi, struct ibmveth_adapter, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001105 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unsigned long lpar_rc;
1108
Santiago Leonf148f612010-09-03 18:29:30 +00001109restart_poll:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 do {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001111 if (!ibmveth_rxq_pending_buffer(adapter))
1112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Anton Blanchardf89e49e2010-09-06 18:21:41 -07001114 smp_rmb();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001115 if (!ibmveth_rxq_buffer_valid(adapter)) {
1116 wmb(); /* suggested by larson1 */
1117 adapter->rx_invalid_buffer++;
Santiago Leonc43ced12010-09-03 18:29:14 +00001118 netdev_dbg(netdev, "recycling invalid buffer\n");
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001119 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 } else {
Santiago Leon8d86c612010-09-03 18:28:25 +00001121 struct sk_buff *skb, *new_skb;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001122 int length = ibmveth_rxq_frame_length(adapter);
1123 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -07001124 int csum_good = ibmveth_rxq_csum_good(adapter);
1125
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001126 skb = ibmveth_rxq_get_buffer(adapter);
1127
Santiago Leon8d86c612010-09-03 18:28:25 +00001128 new_skb = NULL;
1129 if (length < rx_copybreak)
1130 new_skb = netdev_alloc_skb(netdev, length);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001131
Santiago Leon8d86c612010-09-03 18:28:25 +00001132 if (new_skb) {
1133 skb_copy_to_linear_data(new_skb,
1134 skb->data + offset,
1135 length);
Santiago Leon0c26b672010-09-03 18:28:41 +00001136 if (rx_flush)
1137 ibmveth_flush_buffer(skb->data,
1138 length + offset);
Santiago Leon8d86c612010-09-03 18:28:25 +00001139 skb = new_skb;
1140 ibmveth_rxq_recycle_buffer(adapter);
1141 } else {
1142 ibmveth_rxq_harvest_buffer(adapter);
1143 skb_reserve(skb, offset);
1144 }
1145
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001146 skb_put(skb, length);
1147 skb->protocol = eth_type_trans(skb, netdev);
1148
Santiago Leon8d86c612010-09-03 18:28:25 +00001149 if (csum_good)
1150 skb->ip_summed = CHECKSUM_UNNECESSARY;
1151
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001152 netif_receive_skb(skb); /* send it up */
1153
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001154 netdev->stats.rx_packets++;
1155 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001156 frames_processed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001158 } while (frames_processed < budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Santiago Leone2adbcb2005-10-26 10:47:08 -06001160 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001162 if (frames_processed < budget) {
1163 /* We think we are done - reenable interrupts,
1164 * then check once more to make sure we are done.
1165 */
1166 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1167 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Santiago Leon64859112010-09-03 18:29:41 +00001169 BUG_ON(lpar_rc != H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001170
Ben Hutchings288379f2009-01-19 16:43:59 -08001171 napi_complete(napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001172
1173 if (ibmveth_rxq_pending_buffer(adapter) &&
Ben Hutchings288379f2009-01-19 16:43:59 -08001174 napi_reschedule(napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001175 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1176 VIO_IRQ_DISABLE);
1177 goto restart_poll;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
David Howells7d12e782006-10-05 14:55:46 +01001184static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct net_device *netdev = dev_instance;
Wang Chen4cf16532008-11-12 23:38:14 -08001187 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 unsigned long lpar_rc;
1189
Ben Hutchings288379f2009-01-19 16:43:59 -08001190 if (napi_schedule_prep(&adapter->napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001191 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1192 VIO_IRQ_DISABLE);
Santiago Leon64859112010-09-03 18:29:41 +00001193 BUG_ON(lpar_rc != H_SUCCESS);
Ben Hutchings288379f2009-01-19 16:43:59 -08001194 __napi_schedule(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 return IRQ_HANDLED;
1197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static void ibmveth_set_multicast_list(struct net_device *netdev)
1200{
Wang Chen4cf16532008-11-12 23:38:14 -08001201 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 unsigned long lpar_rc;
1203
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001204 if ((netdev->flags & IFF_PROMISC) ||
1205 (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1207 IbmVethMcastEnableRecv |
1208 IbmVethMcastDisableFiltering,
1209 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001210 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001211 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1212 "entering promisc mode\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001215 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* clear the filter table & disable filtering */
1217 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1218 IbmVethMcastEnableRecv |
1219 IbmVethMcastDisableFiltering |
1220 IbmVethMcastClearFilterTable,
1221 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001222 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001223 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1224 "attempting to clear filter table\n",
1225 lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 /* add the addresses to the filter table */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001228 netdev_for_each_mc_addr(ha, netdev) {
Santiago Leonf148f612010-09-03 18:29:30 +00001229 /* add the multicast address to the filter table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 unsigned long mcast_addr = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001231 memcpy(((char *)&mcast_addr)+2, ha->addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1233 IbmVethMcastAddFilter,
1234 mcast_addr);
Santiago Leonf148f612010-09-03 18:29:30 +00001235 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001236 netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1237 "when adding an entry to the filter "
1238 "table\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /* re-enable filtering */
1243 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1244 IbmVethMcastEnableFiltering,
1245 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001246 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001247 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1248 "enabling filtering\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 }
1251}
1252
1253static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1254{
Wang Chen4cf16532008-11-12 23:38:14 -08001255 struct ibmveth_adapter *adapter = netdev_priv(dev);
Robert Jennings1096d632008-07-24 04:34:52 +10001256 struct vio_dev *viodev = adapter->vdev;
Santiago Leon860f2422006-04-25 11:19:59 -05001257 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Robert Jennings0645bab2010-08-17 09:15:45 +00001258 int i, rc;
1259 int need_restart = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001260
Santiago Leon517e80e2010-09-03 18:29:25 +00001261 if (new_mtu < IBMVETH_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001263
Santiago Leon517e80e2010-09-03 18:29:25 +00001264 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Brian Kingce6eea52007-06-08 14:05:17 -05001265 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1266 break;
1267
Santiago Leon517e80e2010-09-03 18:29:25 +00001268 if (i == IBMVETH_NUM_BUFF_POOLS)
Brian Kingce6eea52007-06-08 14:05:17 -05001269 return -EINVAL;
1270
Santiago Leonea866e62008-07-24 04:34:23 +10001271 /* Deactivate all the buffer pools so that the next loop can activate
1272 only the buffer pools necessary to hold the new MTU */
Robert Jennings0645bab2010-08-17 09:15:45 +00001273 if (netif_running(adapter->netdev)) {
1274 need_restart = 1;
1275 adapter->pool_config = 1;
1276 ibmveth_close(adapter->netdev);
1277 adapter->pool_config = 0;
1278 }
Brian Kingce6eea52007-06-08 14:05:17 -05001279
Santiago Leonea866e62008-07-24 04:34:23 +10001280 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonf148f612010-09-03 18:29:30 +00001281 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leonea866e62008-07-24 04:34:23 +10001282 adapter->rx_buff_pool[i].active = 1;
1283
Santiago Leon860f2422006-04-25 11:19:59 -05001284 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
Robert Jennings1096d632008-07-24 04:34:52 +10001285 dev->mtu = new_mtu;
1286 vio_cmo_set_dev_desired(viodev,
1287 ibmveth_get_desired_dma
1288 (viodev));
Robert Jennings0645bab2010-08-17 09:15:45 +00001289 if (need_restart) {
1290 return ibmveth_open(adapter->netdev);
1291 }
Santiago Leon860f2422006-04-25 11:19:59 -05001292 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001293 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001294 }
Robert Jennings0645bab2010-08-17 09:15:45 +00001295
1296 if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1297 return rc;
1298
Santiago Leon860f2422006-04-25 11:19:59 -05001299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Santiago Leon6b422372006-10-03 12:24:28 -05001302#ifdef CONFIG_NET_POLL_CONTROLLER
1303static void ibmveth_poll_controller(struct net_device *dev)
1304{
Wang Chen4cf16532008-11-12 23:38:14 -08001305 ibmveth_replenish_task(netdev_priv(dev));
Andrew Morton5f771132006-10-10 14:33:30 -07001306 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001307}
1308#endif
1309
Robert Jennings1096d632008-07-24 04:34:52 +10001310/**
1311 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1312 *
1313 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1314 *
1315 * Return value:
1316 * Number of bytes of IO data the driver will need to perform well.
1317 */
1318static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1319{
1320 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1321 struct ibmveth_adapter *adapter;
1322 unsigned long ret;
1323 int i;
1324 int rxqentries = 1;
1325
1326 /* netdev inits at probe time along with the structures we need below*/
1327 if (netdev == NULL)
1328 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT);
1329
1330 adapter = netdev_priv(netdev);
1331
1332 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
1333 ret += IOMMU_PAGE_ALIGN(netdev->mtu);
1334
Santiago Leon517e80e2010-09-03 18:29:25 +00001335 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Robert Jennings1096d632008-07-24 04:34:52 +10001336 /* add the size of the active receive buffers */
1337 if (adapter->rx_buff_pool[i].active)
1338 ret +=
1339 adapter->rx_buff_pool[i].size *
1340 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
1341 buff_size);
1342 rxqentries += adapter->rx_buff_pool[i].size;
1343 }
1344 /* add the size of the receive queue entries */
1345 ret += IOMMU_PAGE_ALIGN(rxqentries * sizeof(struct ibmveth_rx_q_entry));
1346
1347 return ret;
1348}
1349
Alexander Beregalove186d172009-04-15 12:52:39 +00001350static const struct net_device_ops ibmveth_netdev_ops = {
1351 .ndo_open = ibmveth_open,
1352 .ndo_stop = ibmveth_close,
1353 .ndo_start_xmit = ibmveth_start_xmit,
1354 .ndo_set_multicast_list = ibmveth_set_multicast_list,
1355 .ndo_do_ioctl = ibmveth_ioctl,
1356 .ndo_change_mtu = ibmveth_change_mtu,
1357 .ndo_validate_addr = eth_validate_addr,
1358 .ndo_set_mac_address = eth_mac_addr,
1359#ifdef CONFIG_NET_POLL_CONTROLLER
1360 .ndo_poll_controller = ibmveth_poll_controller,
1361#endif
1362};
1363
Santiago Leonf148f612010-09-03 18:29:30 +00001364static int __devinit ibmveth_probe(struct vio_dev *dev,
1365 const struct vio_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Santiago Leonb6d35182005-10-26 10:47:01 -06001367 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001369 struct ibmveth_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 unsigned char *mac_addr_p;
1371 unsigned int *mcastFilterSize_p;
1372
Santiago Leonc43ced12010-09-03 18:29:14 +00001373 dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1374 dev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Santiago Leonf148f612010-09-03 18:29:30 +00001376 mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
1377 NULL);
1378 if (!mac_addr_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001379 dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001380 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001382
Santiago Leonf148f612010-09-03 18:29:30 +00001383 mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
Michael Ellerman493a6842007-04-17 13:12:55 +10001384 VETH_MCAST_FILTER_SIZE, NULL);
Santiago Leonf148f612010-09-03 18:29:30 +00001385 if (!mcastFilterSize_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001386 dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1387 "attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1392
Santiago Leonf148f612010-09-03 18:29:30 +00001393 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -ENOMEM;
1395
Wang Chen4cf16532008-11-12 23:38:14 -08001396 adapter = netdev_priv(netdev);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001397 dev_set_drvdata(&dev->dev, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 adapter->vdev = dev;
1400 adapter->netdev = netdev;
Santiago Leonf148f612010-09-03 18:29:30 +00001401 adapter->mcastFilterSize = *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001402 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001403
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001404 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1405
Santiago Leonf148f612010-09-03 18:29:30 +00001406 /*
1407 * Some older boxes running PHYP non-natively have an OF that returns
1408 * a 8-byte local-mac-address field (and the first 2 bytes have to be
1409 * ignored) while newer boxes' OF return a 6-byte field. Note that
1410 * IEEE 1275 specifies that local-mac-address must be a 6-byte field.
1411 * The RPA doc specifies that the first byte must be 10b, so we'll
1412 * just look for it to solve this 8 vs. 6 byte field issue
1413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if ((*mac_addr_p & 0x3) != 0x02)
1415 mac_addr_p += 2;
1416
1417 adapter->mac_addr = 0;
1418 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 netdev->irq = dev->irq;
Alexander Beregalove186d172009-04-15 12:52:39 +00001421 netdev->netdev_ops = &ibmveth_netdev_ops;
1422 netdev->ethtool_ops = &netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 SET_NETDEV_DEV(netdev, &dev->dev);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001424 netdev->features |= NETIF_F_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Stephen Rothwelld44b5e02009-05-11 21:44:51 +00001426 memcpy(netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Santiago Leonf148f612010-09-03 18:29:30 +00001428 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001429 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001430 int error;
1431
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001432 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1433 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001434 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001435 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1436 &dev->dev.kobj, "pool%d", i);
1437 if (!error)
1438 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Santiago Leonc43ced12010-09-03 18:29:14 +00001441 netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 adapter->buffer_list_dma = DMA_ERROR_CODE;
1444 adapter->filter_list_dma = DMA_ERROR_CODE;
1445 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1446
Santiago Leonc43ced12010-09-03 18:29:14 +00001447 netdev_dbg(netdev, "registering netdev...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Santiago Leonbc4c6f52010-09-03 18:28:47 +00001449 ibmveth_set_csum_offload(netdev, 1, ibmveth_set_tx_csum_flags);
Brian Kingf4ff2872007-09-15 13:36:07 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 rc = register_netdev(netdev);
1452
Santiago Leonf148f612010-09-03 18:29:30 +00001453 if (rc) {
Santiago Leonc43ced12010-09-03 18:29:14 +00001454 netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 free_netdev(netdev);
1456 return rc;
1457 }
1458
Santiago Leonc43ced12010-09-03 18:29:14 +00001459 netdev_dbg(netdev, "registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
1463
1464static int __devexit ibmveth_remove(struct vio_dev *dev)
1465{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001466 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Wang Chen4cf16532008-11-12 23:38:14 -08001467 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001468 int i;
1469
Santiago Leonf148f612010-09-03 18:29:30 +00001470 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001471 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 unregister_netdev(netdev);
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 free_netdev(netdev);
Robert Jennings1096d632008-07-24 04:34:52 +10001476 dev_set_drvdata(&dev->dev, NULL);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return 0;
1479}
1480
Santiago Leon860f2422006-04-25 11:19:59 -05001481static struct attribute veth_active_attr;
1482static struct attribute veth_num_attr;
1483static struct attribute veth_size_attr;
1484
Santiago Leonf148f612010-09-03 18:29:30 +00001485static ssize_t veth_pool_show(struct kobject *kobj,
1486 struct attribute *attr, char *buf)
Santiago Leon860f2422006-04-25 11:19:59 -05001487{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001488 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001489 struct ibmveth_buff_pool,
1490 kobj);
1491
1492 if (attr == &veth_active_attr)
1493 return sprintf(buf, "%d\n", pool->active);
1494 else if (attr == &veth_num_attr)
1495 return sprintf(buf, "%d\n", pool->size);
1496 else if (attr == &veth_size_attr)
1497 return sprintf(buf, "%d\n", pool->buff_size);
1498 return 0;
1499}
1500
Santiago Leonf148f612010-09-03 18:29:30 +00001501static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1502 const char *buf, size_t count)
Santiago Leon860f2422006-04-25 11:19:59 -05001503{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001504 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001505 struct ibmveth_buff_pool,
1506 kobj);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001507 struct net_device *netdev = dev_get_drvdata(
1508 container_of(kobj->parent, struct device, kobj));
Wang Chen4cf16532008-11-12 23:38:14 -08001509 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001510 long value = simple_strtol(buf, NULL, 10);
1511 long rc;
1512
1513 if (attr == &veth_active_attr) {
1514 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001515 if (netif_running(netdev)) {
Santiago Leonf148f612010-09-03 18:29:30 +00001516 if (ibmveth_alloc_buffer_pool(pool)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001517 netdev_err(netdev,
1518 "unable to alloc pool\n");
Brian King4aa9c932007-06-08 14:05:16 -05001519 return -ENOMEM;
1520 }
1521 pool->active = 1;
1522 adapter->pool_config = 1;
1523 ibmveth_close(netdev);
1524 adapter->pool_config = 0;
1525 if ((rc = ibmveth_open(netdev)))
1526 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001527 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001528 pool->active = 1;
Santiago Leonf148f612010-09-03 18:29:30 +00001529 }
Santiago Leon860f2422006-04-25 11:19:59 -05001530 } else if (!value && pool->active) {
1531 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1532 int i;
1533 /* Make sure there is a buffer pool with buffers that
1534 can hold a packet of the size of the MTU */
Santiago Leon517e80e2010-09-03 18:29:25 +00001535 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001536 if (pool == &adapter->rx_buff_pool[i])
1537 continue;
1538 if (!adapter->rx_buff_pool[i].active)
1539 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001540 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1541 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001542 }
Brian King76b9cfc2007-08-03 13:55:19 +10001543
Santiago Leon517e80e2010-09-03 18:29:25 +00001544 if (i == IBMVETH_NUM_BUFF_POOLS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001545 netdev_err(netdev, "no active pool >= MTU\n");
Santiago Leon860f2422006-04-25 11:19:59 -05001546 return -EPERM;
1547 }
Brian King76b9cfc2007-08-03 13:55:19 +10001548
Brian King76b9cfc2007-08-03 13:55:19 +10001549 if (netif_running(netdev)) {
1550 adapter->pool_config = 1;
1551 ibmveth_close(netdev);
Santiago Leonea866e62008-07-24 04:34:23 +10001552 pool->active = 0;
Brian King76b9cfc2007-08-03 13:55:19 +10001553 adapter->pool_config = 0;
1554 if ((rc = ibmveth_open(netdev)))
1555 return rc;
1556 }
Santiago Leonea866e62008-07-24 04:34:23 +10001557 pool->active = 0;
Santiago Leon860f2422006-04-25 11:19:59 -05001558 }
1559 } else if (attr == &veth_num_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001560 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
Santiago Leon860f2422006-04-25 11:19:59 -05001561 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001562 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001563 if (netif_running(netdev)) {
1564 adapter->pool_config = 1;
1565 ibmveth_close(netdev);
1566 adapter->pool_config = 0;
1567 pool->size = value;
1568 if ((rc = ibmveth_open(netdev)))
1569 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001570 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001571 pool->size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001572 }
Santiago Leon860f2422006-04-25 11:19:59 -05001573 }
1574 } else if (attr == &veth_size_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001575 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
Santiago Leon860f2422006-04-25 11:19:59 -05001576 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001577 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001578 if (netif_running(netdev)) {
1579 adapter->pool_config = 1;
1580 ibmveth_close(netdev);
1581 adapter->pool_config = 0;
1582 pool->buff_size = value;
1583 if ((rc = ibmveth_open(netdev)))
1584 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001585 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001586 pool->buff_size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001587 }
Santiago Leon860f2422006-04-25 11:19:59 -05001588 }
1589 }
1590
1591 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001592 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001593 return count;
1594}
1595
1596
Santiago Leonf148f612010-09-03 18:29:30 +00001597#define ATTR(_name, _mode) \
1598 struct attribute veth_##_name##_attr = { \
1599 .name = __stringify(_name), .mode = _mode, \
1600 };
Santiago Leon860f2422006-04-25 11:19:59 -05001601
1602static ATTR(active, 0644);
1603static ATTR(num, 0644);
1604static ATTR(size, 0644);
1605
Santiago Leonf148f612010-09-03 18:29:30 +00001606static struct attribute *veth_pool_attrs[] = {
Santiago Leon860f2422006-04-25 11:19:59 -05001607 &veth_active_attr,
1608 &veth_num_attr,
1609 &veth_size_attr,
1610 NULL,
1611};
1612
Emese Revfy52cf25d2010-01-19 02:58:23 +01001613static const struct sysfs_ops veth_pool_ops = {
Santiago Leon860f2422006-04-25 11:19:59 -05001614 .show = veth_pool_show,
1615 .store = veth_pool_store,
1616};
1617
1618static struct kobj_type ktype_veth_pool = {
1619 .release = NULL,
1620 .sysfs_ops = &veth_pool_ops,
1621 .default_attrs = veth_pool_attrs,
1622};
1623
Brian Kinge7a3af52010-05-07 08:56:08 +00001624static int ibmveth_resume(struct device *dev)
1625{
1626 struct net_device *netdev = dev_get_drvdata(dev);
1627 ibmveth_interrupt(netdev->irq, netdev);
1628 return 0;
1629}
Santiago Leon860f2422006-04-25 11:19:59 -05001630
Santiago Leonf148f612010-09-03 18:29:30 +00001631static struct vio_device_id ibmveth_device_table[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001633 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1636
Brian Kinge7a3af52010-05-07 08:56:08 +00001637static struct dev_pm_ops ibmveth_pm_ops = {
1638 .resume = ibmveth_resume
1639};
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001642 .id_table = ibmveth_device_table,
1643 .probe = ibmveth_probe,
1644 .remove = ibmveth_remove,
Robert Jennings1096d632008-07-24 04:34:52 +10001645 .get_desired_dma = ibmveth_get_desired_dma,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001646 .driver = {
1647 .name = ibmveth_driver_name,
Stephen Rothwell915124d2005-10-24 15:12:22 +10001648 .owner = THIS_MODULE,
Brian Kinge7a3af52010-05-07 08:56:08 +00001649 .pm = &ibmveth_pm_ops,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651};
1652
1653static int __init ibmveth_module_init(void)
1654{
Santiago Leon21c2dec2010-09-03 18:29:19 +00001655 printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1656 ibmveth_driver_string, ibmveth_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return vio_register_driver(&ibmveth_driver);
1659}
1660
1661static void __exit ibmveth_module_exit(void)
1662{
1663 vio_unregister_driver(&ibmveth_driver);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001664}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666module_init(ibmveth_module_init);
1667module_exit(ibmveth_module_exit);