blob: 9353890dcda0e4ef4f99e632094994c038df8fc6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**************************************************************************/
2/* */
3/* IBM eServer i/pSeries Virtual Ethernet Device Driver */
4/* Copyright (C) 2003 IBM Corp. */
5/* Originally written by Dave Larson (larson1@us.ibm.com) */
6/* Maintained by Santiago Leon (santil@us.ibm.com) */
7/* */
8/* This program is free software; you can redistribute it and/or modify */
9/* it under the terms of the GNU General Public License as published by */
10/* the Free Software Foundation; either version 2 of the License, or */
11/* (at your option) any later version. */
12/* */
13/* This program is distributed in the hope that it will be useful, */
14/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16/* GNU General Public License for more details. */
17/* */
18/* You should have received a copy of the GNU General Public License */
19/* along with this program; if not, write to the Free Software */
20/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 */
21/* USA */
22/* */
23/* This module contains the implementation of a virtual ethernet device */
24/* for use with IBM i/pSeries LPAR Linux. It utilizes the logical LAN */
25/* option of the RS/6000 Platform Architechture to interface with virtual */
26/* ethernet NICs that are presented to the partition by the hypervisor. */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -040027/* */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/**************************************************************************/
29/*
30 TODO:
31 - remove frag processing code - no longer needed
32 - add support for sysfs
33 - possibly remove procfs support
34*/
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/types.h>
38#include <linux/errno.h>
39#include <linux/ioport.h>
40#include <linux/dma-mapping.h>
41#include <linux/kernel.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
45#include <linux/init.h>
46#include <linux/delay.h>
47#include <linux/mm.h>
48#include <linux/ethtool.h>
49#include <linux/proc_fs.h>
Brian Kingf4ff2872007-09-15 13:36:07 -070050#include <linux/in.h>
51#include <linux/ip.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020052#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/semaphore.h>
54#include <asm/hvcall.h>
55#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/vio.h>
57#include <asm/uaccess.h>
58#include <linux/seq_file.h>
59
60#include "ibmveth.h"
61
Anton Blanchard6af37fa2005-11-11 14:02:04 +110062#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define ibmveth_printk(fmt, args...) \
Olof Johanssonf98baff2006-04-12 15:22:29 -050065 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#define ibmveth_error_printk(fmt, args...) \
68 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
69
70#ifdef DEBUG
71#define ibmveth_debug_printk_no_adapter(fmt, args...) \
72 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
73#define ibmveth_debug_printk(fmt, args...) \
74 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
75#define ibmveth_assert(expr) \
76 if(!(expr)) { \
77 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
78 BUG(); \
79 }
80#else
81#define ibmveth_debug_printk_no_adapter(fmt, args...)
82#define ibmveth_debug_printk(fmt, args...)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -040083#define ibmveth_assert(expr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
86static int ibmveth_open(struct net_device *dev);
87static int ibmveth_close(struct net_device *dev);
88static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070089static int ibmveth_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
91static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
92static void ibmveth_set_multicast_list(struct net_device *dev);
93static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
94static void ibmveth_proc_register_driver(void);
95static void ibmveth_proc_unregister_driver(void);
96static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
97static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
David Howells7d12e782006-10-05 14:55:46 +010098static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
Michael Ellerman493a6842007-04-17 13:12:55 +100099static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Santiago Leon860f2422006-04-25 11:19:59 -0500100static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200103#define IBMVETH_PROC_DIR "ibmveth"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static struct proc_dir_entry *ibmveth_proc_dir;
105#endif
106
107static const char ibmveth_driver_name[] = "ibmveth";
108static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
109#define ibmveth_driver_version "1.03"
110
111MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
112MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
113MODULE_LICENSE("GPL");
114MODULE_VERSION(ibmveth_driver_version);
115
116/* simple methods of getting data from the current rxq entry */
117static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
118{
119 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
120}
121
122static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
123{
124 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
125}
126
127static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
128{
129 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
130}
131
132static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
133{
134 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
135}
136
Brian Kingf4ff2872007-09-15 13:36:07 -0700137static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
138{
139 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].csum_good);
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* setup the initial settings for a buffer pool */
Santiago Leon860f2422006-04-25 11:19:59 -0500143static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 pool->size = pool_size;
146 pool->index = pool_index;
147 pool->buff_size = buff_size;
148 pool->threshold = pool_size / 2;
Santiago Leon860f2422006-04-25 11:19:59 -0500149 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/* allocate and setup an buffer pool - called during open */
153static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
154{
155 int i;
156
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400157 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if(!pool->free_map) {
160 return -1;
161 }
162
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400163 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if(!pool->dma_addr) {
165 kfree(pool->free_map);
166 pool->free_map = NULL;
167 return -1;
168 }
169
170 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
171
172 if(!pool->skbuff) {
173 kfree(pool->dma_addr);
174 pool->dma_addr = NULL;
175
176 kfree(pool->free_map);
177 pool->free_map = NULL;
178 return -1;
179 }
180
181 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
182 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
183
184 for(i = 0; i < pool->size; ++i) {
185 pool->free_map[i] = i;
186 }
187
188 atomic_set(&pool->available, 0);
189 pool->producer_index = 0;
190 pool->consumer_index = 0;
191
192 return 0;
193}
194
195/* replenish the buffers for a pool. note that we don't need to
196 * skb_reserve these since they are used for incoming...
197 */
198static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
199{
200 u32 i;
201 u32 count = pool->size - atomic_read(&pool->available);
202 u32 buffers_added = 0;
203
204 mb();
205
206 for(i = 0; i < count; ++i) {
207 struct sk_buff *skb;
208 unsigned int free_index, index;
209 u64 correlator;
210 union ibmveth_buf_desc desc;
211 unsigned long lpar_rc;
212 dma_addr_t dma_addr;
213
214 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
215
216 if(!skb) {
217 ibmveth_debug_printk("replenish: unable to allocate skb\n");
218 adapter->replenish_no_mem++;
219 break;
220 }
221
David Gibson047a66d2006-10-21 10:24:13 -0700222 free_index = pool->consumer_index;
223 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
227 ibmveth_assert(pool->skbuff[index] == NULL);
228
229 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
230 pool->buff_size, DMA_FROM_DEVICE);
231
232 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
233 pool->dma_addr[index] = dma_addr;
234 pool->skbuff[index] = skb;
235
236 correlator = ((u64)pool->index << 32) | index;
237 *(u64*)skb->data = correlator;
238
239 desc.desc = 0;
240 desc.fields.valid = 1;
241 desc.fields.length = pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400242 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400245
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200246 if(lpar_rc != H_SUCCESS) {
Santiago Leon82702d32005-10-26 10:47:23 -0600247 pool->free_map[free_index] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 pool->skbuff[index] = NULL;
Santiago Leon751ae212006-10-03 12:24:45 -0500249 if (pool->consumer_index == 0)
250 pool->consumer_index = pool->size - 1;
251 else
252 pool->consumer_index--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 dma_unmap_single(&adapter->vdev->dev,
254 pool->dma_addr[index], pool->buff_size,
255 DMA_FROM_DEVICE);
256 dev_kfree_skb_any(skb);
257 adapter->replenish_add_buff_failure++;
258 break;
259 } else {
260 buffers_added++;
261 adapter->replenish_add_buff_success++;
262 }
263 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 mb();
266 atomic_add(buffers_added, &(pool->available));
267}
268
Santiago Leone2adbcb2005-10-26 10:47:08 -0600269/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400270static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Santiago Leonb6d35182005-10-26 10:47:01 -0600272 int i;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 adapter->replenish_task_cycles++;
275
Santiago Leonb6d35182005-10-26 10:47:01 -0600276 for(i = 0; i < IbmVethNumBufferPools; i++)
277 if(adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400278 ibmveth_replenish_buffer_pool(adapter,
Santiago Leonb6d35182005-10-26 10:47:01 -0600279 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/* empty and free ana buffer pool - also used to do cleanup in error paths */
285static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
286{
287 int i;
288
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400289 kfree(pool->free_map);
290 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if(pool->skbuff && pool->dma_addr) {
293 for(i = 0; i < pool->size; ++i) {
294 struct sk_buff *skb = pool->skbuff[i];
295 if(skb) {
296 dma_unmap_single(&adapter->vdev->dev,
297 pool->dma_addr[i],
298 pool->buff_size,
299 DMA_FROM_DEVICE);
300 dev_kfree_skb_any(skb);
301 pool->skbuff[i] = NULL;
302 }
303 }
304 }
305
306 if(pool->dma_addr) {
307 kfree(pool->dma_addr);
308 pool->dma_addr = NULL;
309 }
310
311 if(pool->skbuff) {
312 kfree(pool->skbuff);
313 pool->skbuff = NULL;
314 }
315}
316
317/* remove a buffer from a pool */
318static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
319{
320 unsigned int pool = correlator >> 32;
321 unsigned int index = correlator & 0xffffffffUL;
322 unsigned int free_index;
323 struct sk_buff *skb;
324
325 ibmveth_assert(pool < IbmVethNumBufferPools);
326 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
327
328 skb = adapter->rx_buff_pool[pool].skbuff[index];
329
330 ibmveth_assert(skb != NULL);
331
332 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
333
334 dma_unmap_single(&adapter->vdev->dev,
335 adapter->rx_buff_pool[pool].dma_addr[index],
336 adapter->rx_buff_pool[pool].buff_size,
337 DMA_FROM_DEVICE);
338
David Gibson047a66d2006-10-21 10:24:13 -0700339 free_index = adapter->rx_buff_pool[pool].producer_index;
340 adapter->rx_buff_pool[pool].producer_index
341 = (adapter->rx_buff_pool[pool].producer_index + 1)
342 % adapter->rx_buff_pool[pool].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 adapter->rx_buff_pool[pool].free_map[free_index] = index;
344
345 mb();
346
347 atomic_dec(&(adapter->rx_buff_pool[pool].available));
348}
349
350/* get the current buffer on the rx queue */
351static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
352{
353 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
354 unsigned int pool = correlator >> 32;
355 unsigned int index = correlator & 0xffffffffUL;
356
357 ibmveth_assert(pool < IbmVethNumBufferPools);
358 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
359
360 return adapter->rx_buff_pool[pool].skbuff[index];
361}
362
363/* recycle the current buffer on the rx queue */
364static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
365{
366 u32 q_index = adapter->rx_queue.index;
367 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
368 unsigned int pool = correlator >> 32;
369 unsigned int index = correlator & 0xffffffffUL;
370 union ibmveth_buf_desc desc;
371 unsigned long lpar_rc;
372
373 ibmveth_assert(pool < IbmVethNumBufferPools);
374 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
375
Santiago Leonb6d35182005-10-26 10:47:01 -0600376 if(!adapter->rx_buff_pool[pool].active) {
377 ibmveth_rxq_harvest_buffer(adapter);
378 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
379 return;
380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 desc.desc = 0;
383 desc.fields.valid = 1;
384 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
385 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
386
387 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400388
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200389 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
391 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
392 }
393
394 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
395 adapter->rx_queue.index = 0;
396 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
397 }
398}
399
Michael Ellerman493a6842007-04-17 13:12:55 +1000400static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
403
404 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
405 adapter->rx_queue.index = 0;
406 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
407 }
408}
409
410static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
411{
Santiago Leonb6d35182005-10-26 10:47:01 -0600412 int i;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if(adapter->buffer_list_addr != NULL) {
415 if(!dma_mapping_error(adapter->buffer_list_dma)) {
416 dma_unmap_single(&adapter->vdev->dev,
417 adapter->buffer_list_dma, 4096,
418 DMA_BIDIRECTIONAL);
419 adapter->buffer_list_dma = DMA_ERROR_CODE;
420 }
421 free_page((unsigned long)adapter->buffer_list_addr);
422 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if(adapter->filter_list_addr != NULL) {
426 if(!dma_mapping_error(adapter->filter_list_dma)) {
427 dma_unmap_single(&adapter->vdev->dev,
428 adapter->filter_list_dma, 4096,
429 DMA_BIDIRECTIONAL);
430 adapter->filter_list_dma = DMA_ERROR_CODE;
431 }
432 free_page((unsigned long)adapter->filter_list_addr);
433 adapter->filter_list_addr = NULL;
434 }
435
436 if(adapter->rx_queue.queue_addr != NULL) {
437 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
438 dma_unmap_single(&adapter->vdev->dev,
439 adapter->rx_queue.queue_dma,
440 adapter->rx_queue.queue_len,
441 DMA_BIDIRECTIONAL);
442 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
443 }
444 kfree(adapter->rx_queue.queue_addr);
445 adapter->rx_queue.queue_addr = NULL;
446 }
447
Santiago Leonb6d35182005-10-26 10:47:01 -0600448 for(i = 0; i<IbmVethNumBufferPools; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500449 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400450 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500451 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500454static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
455 union ibmveth_buf_desc rxq_desc, u64 mac_address)
456{
457 int rc, try_again = 1;
458
459 /* After a kexec the adapter will still be open, so our attempt to
460 * open it will fail. So if we get a failure we free the adapter and
461 * try again, but only once. */
462retry:
463 rc = h_register_logical_lan(adapter->vdev->unit_address,
464 adapter->buffer_list_dma, rxq_desc.desc,
465 adapter->filter_list_dma, mac_address);
466
467 if (rc != H_SUCCESS && try_again) {
468 do {
469 rc = h_free_logical_lan(adapter->vdev->unit_address);
470 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
471
472 try_again = 0;
473 goto retry;
474 }
475
476 return rc;
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static int ibmveth_open(struct net_device *netdev)
480{
481 struct ibmveth_adapter *adapter = netdev->priv;
482 u64 mac_address = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600483 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 unsigned long lpar_rc;
485 int rc;
486 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600487 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 ibmveth_debug_printk("open starting\n");
490
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700491 napi_enable(&adapter->napi);
492
Santiago Leonb6d35182005-10-26 10:47:01 -0600493 for(i = 0; i<IbmVethNumBufferPools; i++)
494 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
497 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
500 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
501 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700502 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -ENOMEM;
504 }
505
506 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
507 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
508
509 if(!adapter->rx_queue.queue_addr) {
510 ibmveth_error_printk("unable to allocate rx queue pages\n");
511 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700512 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -ENOMEM;
514 }
515
516 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
517 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
518 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
519 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
520 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
521 adapter->rx_queue.queue_addr,
522 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
523
524 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
525 (dma_mapping_error(adapter->filter_list_dma)) ||
526 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
527 ibmveth_error_printk("unable to map filter or buffer list pages\n");
528 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700529 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -ENOMEM;
531 }
532
533 adapter->rx_queue.index = 0;
534 adapter->rx_queue.num_slots = rxq_entries;
535 adapter->rx_queue.toggle = 1;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
538 mac_address = mac_address >> 16;
539
540 rxq_desc.desc = 0;
541 rxq_desc.fields.valid = 1;
542 rxq_desc.fields.length = adapter->rx_queue.queue_len;
543 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
544
545 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
546 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
547 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
548
Santiago Leon4347ef12006-10-03 12:24:34 -0500549 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
550
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500551 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200553 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
Stephen Rothwell8168f902005-10-25 16:56:43 +1000555 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 adapter->buffer_list_dma,
557 adapter->filter_list_dma,
558 rxq_desc.desc,
559 mac_address);
560 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700561 napi_disable(&adapter->napi);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400562 return -ENONET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
Santiago Leon860f2422006-04-25 11:19:59 -0500565 for(i = 0; i<IbmVethNumBufferPools; i++) {
566 if(!adapter->rx_buff_pool[i].active)
567 continue;
568 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
569 ibmveth_error_printk("unable to alloc pool\n");
570 adapter->rx_buff_pool[i].active = 0;
571 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700572 napi_disable(&adapter->napi);
Santiago Leon860f2422006-04-25 11:19:59 -0500573 return -ENOMEM ;
574 }
575 }
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
578 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
579 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
580 do {
581 rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200582 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700585 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return rc;
587 }
588
Santiago Leone2adbcb2005-10-26 10:47:08 -0600589 ibmveth_debug_printk("initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100590 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Santiago Leone2adbcb2005-10-26 10:47:08 -0600592 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 ibmveth_debug_printk("open complete\n");
595
596 return 0;
597}
598
599static int ibmveth_close(struct net_device *netdev)
600{
601 struct ibmveth_adapter *adapter = netdev->priv;
602 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ibmveth_debug_printk("close starting\n");
605
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700606 napi_disable(&adapter->napi);
607
Santiago Leon860f2422006-04-25 11:19:59 -0500608 if (!adapter->pool_config)
609 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 free_irq(netdev->irq, netdev);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 do {
614 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200615 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200617 if(lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 {
619 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
620 lpar_rc);
621 }
622
623 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
624
625 ibmveth_cleanup(adapter);
626
627 ibmveth_debug_printk("close complete\n");
628
629 return 0;
630}
631
632static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
633 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
634 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
635 cmd->speed = SPEED_1000;
636 cmd->duplex = DUPLEX_FULL;
637 cmd->port = PORT_FIBRE;
638 cmd->phy_address = 0;
639 cmd->transceiver = XCVR_INTERNAL;
640 cmd->autoneg = AUTONEG_ENABLE;
641 cmd->maxtxpkt = 0;
642 cmd->maxrxpkt = 1;
643 return 0;
644}
645
646static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
647 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
648 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
649}
650
651static u32 netdev_get_link(struct net_device *dev) {
652 return 1;
653}
654
Jeff Garzik7282d492006-09-13 14:30:00 -0400655static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 .get_drvinfo = netdev_get_drvinfo,
657 .get_settings = netdev_get_settings,
658 .get_link = netdev_get_link,
659 .get_sg = ethtool_op_get_sg,
660 .get_tx_csum = ethtool_op_get_tx_csum,
661};
662
663static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
664{
665 return -EOPNOTSUPP;
666}
667
668#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
669
670static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
671{
672 struct ibmveth_adapter *adapter = netdev->priv;
673 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
674 unsigned long lpar_rc;
675 int nfrags = 0, curfrag;
676 unsigned long correlator;
Santiago Leon60296d92005-10-26 10:47:16 -0600677 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int retry_count;
Santiago Leon60296d92005-10-26 10:47:16 -0600679 unsigned int tx_dropped = 0;
680 unsigned int tx_bytes = 0;
681 unsigned int tx_packets = 0;
682 unsigned int tx_send_failed = 0;
683 unsigned int tx_map_failed = 0;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
Santiago Leon60296d92005-10-26 10:47:16 -0600687 tx_dropped++;
688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
691 memset(&desc, 0, sizeof(desc));
692
693 /* nfrags = number of frags after the initial fragment */
694 nfrags = skb_shinfo(skb)->nr_frags;
695
696 if(nfrags)
697 adapter->tx_multidesc_send++;
698
699 /* map the initial fragment */
700 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
701 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
702 desc[0].fields.length, DMA_TO_DEVICE);
703 desc[0].fields.valid = 1;
704
Brian Kingf4ff2872007-09-15 13:36:07 -0700705 if (skb->ip_summed == CHECKSUM_PARTIAL &&
706 ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
707 ibmveth_error_printk("tx: failed to checksum packet\n");
708 tx_dropped++;
709 goto out;
710 }
711
712 if (skb->ip_summed == CHECKSUM_PARTIAL) {
713 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
714
715 desc[0].fields.no_csum = 1;
716 desc[0].fields.csum_good = 1;
717
718 /* Need to zero out the checksum */
719 buf[0] = 0;
720 buf[1] = 0;
721 }
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if(dma_mapping_error(desc[0].fields.address)) {
724 ibmveth_error_printk("tx: unable to map initial fragment\n");
Santiago Leon60296d92005-10-26 10:47:16 -0600725 tx_map_failed++;
726 tx_dropped++;
727 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
730 curfrag = nfrags;
731
732 /* map fragments past the initial portion if there are any */
733 while(curfrag--) {
734 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
735 desc[curfrag+1].fields.address
736 = dma_map_single(&adapter->vdev->dev,
737 page_address(frag->page) + frag->page_offset,
738 frag->size, DMA_TO_DEVICE);
739 desc[curfrag+1].fields.length = frag->size;
740 desc[curfrag+1].fields.valid = 1;
Brian Kingf4ff2872007-09-15 13:36:07 -0700741 if (skb->ip_summed == CHECKSUM_PARTIAL) {
742 desc[curfrag+1].fields.no_csum = 1;
743 desc[curfrag+1].fields.csum_good = 1;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
747 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
Santiago Leon60296d92005-10-26 10:47:16 -0600748 tx_map_failed++;
749 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Free all the mappings we just created */
751 while(curfrag < nfrags) {
752 dma_unmap_single(&adapter->vdev->dev,
753 desc[curfrag+1].fields.address,
754 desc[curfrag+1].fields.length,
755 DMA_TO_DEVICE);
756 curfrag++;
757 }
Santiago Leon60296d92005-10-26 10:47:16 -0600758 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 }
761
762 /* send the frame. Arbitrarily set retrycount to 1024 */
763 correlator = 0;
764 retry_count = 1024;
765 do {
766 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
767 desc[0].desc,
768 desc[1].desc,
769 desc[2].desc,
770 desc[3].desc,
771 desc[4].desc,
772 desc[5].desc,
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000773 correlator,
774 &correlator);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200775 } while ((lpar_rc == H_BUSY) && (retry_count--));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400776
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200777 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int i;
779 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
780 for(i = 0; i < 6; i++) {
781 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
782 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
783 }
Santiago Leon60296d92005-10-26 10:47:16 -0600784 tx_send_failed++;
785 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 } else {
Santiago Leon60296d92005-10-26 10:47:16 -0600787 tx_packets++;
788 tx_bytes += skb->len;
Santiago Leon0abe7912005-10-26 10:46:53 -0600789 netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 do {
793 dma_unmap_single(&adapter->vdev->dev,
794 desc[nfrags].fields.address,
795 desc[nfrags].fields.length, DMA_TO_DEVICE);
796 } while(--nfrags >= 0);
797
Santiago Leon60296d92005-10-26 10:47:16 -0600798out: spin_lock_irqsave(&adapter->stats_lock, flags);
799 adapter->stats.tx_dropped += tx_dropped;
800 adapter->stats.tx_bytes += tx_bytes;
801 adapter->stats.tx_packets += tx_packets;
802 adapter->tx_send_failed += tx_send_failed;
803 adapter->tx_map_failed += tx_map_failed;
804 spin_unlock_irqrestore(&adapter->stats_lock, flags);
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 dev_kfree_skb(skb);
807 return 0;
808}
809
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700810static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700812 struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
813 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 unsigned long lpar_rc;
816
817 restart_poll:
818 do {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700819 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700821 if (!ibmveth_rxq_pending_buffer(adapter))
822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700824 rmb();
825 if (!ibmveth_rxq_buffer_valid(adapter)) {
826 wmb(); /* suggested by larson1 */
827 adapter->rx_invalid_buffer++;
828 ibmveth_debug_printk("recycling invalid buffer\n");
829 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 } else {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700831 int length = ibmveth_rxq_frame_length(adapter);
832 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -0700833 int csum_good = ibmveth_rxq_csum_good(adapter);
834
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700835 skb = ibmveth_rxq_get_buffer(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -0700836 if (csum_good)
837 skb->ip_summed = CHECKSUM_UNNECESSARY;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700838
839 ibmveth_rxq_harvest_buffer(adapter);
840
841 skb_reserve(skb, offset);
842 skb_put(skb, length);
843 skb->protocol = eth_type_trans(skb, netdev);
844
845 netif_receive_skb(skb); /* send it up */
846
847 adapter->stats.rx_packets++;
848 adapter->stats.rx_bytes += length;
849 frames_processed++;
850 netdev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700852 } while (frames_processed < budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Santiago Leone2adbcb2005-10-26 10:47:08 -0600854 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700856 if (frames_processed < budget) {
857 /* We think we are done - reenable interrupts,
858 * then check once more to make sure we are done.
859 */
860 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
861 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200863 ibmveth_assert(lpar_rc == H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700864
865 netif_rx_complete(netdev, napi);
866
867 if (ibmveth_rxq_pending_buffer(adapter) &&
868 netif_rx_reschedule(netdev, napi)) {
869 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
870 VIO_IRQ_DISABLE);
871 goto restart_poll;
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700875 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
David Howells7d12e782006-10-05 14:55:46 +0100878static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400879{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct net_device *netdev = dev_instance;
881 struct ibmveth_adapter *adapter = netdev->priv;
882 unsigned long lpar_rc;
883
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700884 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
885 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
886 VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200887 ibmveth_assert(lpar_rc == H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700888 __netif_rx_schedule(netdev, &adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 return IRQ_HANDLED;
891}
892
893static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
894{
895 struct ibmveth_adapter *adapter = dev->priv;
896 return &adapter->stats;
897}
898
899static void ibmveth_set_multicast_list(struct net_device *netdev)
900{
901 struct ibmveth_adapter *adapter = netdev->priv;
902 unsigned long lpar_rc;
903
904 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
905 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
906 IbmVethMcastEnableRecv |
907 IbmVethMcastDisableFiltering,
908 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200909 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
911 }
912 } else {
913 struct dev_mc_list *mclist = netdev->mc_list;
914 int i;
915 /* clear the filter table & disable filtering */
916 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
917 IbmVethMcastEnableRecv |
918 IbmVethMcastDisableFiltering |
919 IbmVethMcastClearFilterTable,
920 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200921 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
923 }
924 /* add the addresses to the filter table */
925 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
926 // add the multicast address to the filter table
927 unsigned long mcast_addr = 0;
928 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
929 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
930 IbmVethMcastAddFilter,
931 mcast_addr);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200932 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
934 }
935 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* re-enable filtering */
938 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
939 IbmVethMcastEnableFiltering,
940 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200941 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
943 }
944 }
945}
946
947static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
948{
Santiago Leonb6d35182005-10-26 10:47:01 -0600949 struct ibmveth_adapter *adapter = dev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -0500950 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Brian Kingce6eea52007-06-08 14:05:17 -0500951 int reinit = 0;
952 int i, rc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600953
Santiago Leon860f2422006-04-25 11:19:59 -0500954 if (new_mtu < IBMVETH_MAX_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -0600956
Brian Kingce6eea52007-06-08 14:05:17 -0500957 for (i = 0; i < IbmVethNumBufferPools; i++)
958 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
959 break;
960
961 if (i == IbmVethNumBufferPools)
962 return -EINVAL;
963
Santiago Leon860f2422006-04-25 11:19:59 -0500964 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonb6d35182005-10-26 10:47:01 -0600965 for(i = 0; i<IbmVethNumBufferPools; i++) {
Brian Kingce6eea52007-06-08 14:05:17 -0500966 if (!adapter->rx_buff_pool[i].active) {
967 adapter->rx_buff_pool[i].active = 1;
968 reinit = 1;
969 }
970
Santiago Leon860f2422006-04-25 11:19:59 -0500971 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
Brian Kingce6eea52007-06-08 14:05:17 -0500972 if (reinit && netif_running(adapter->netdev)) {
973 adapter->pool_config = 1;
974 ibmveth_close(adapter->netdev);
975 adapter->pool_config = 0;
976 dev->mtu = new_mtu;
977 if ((rc = ibmveth_open(adapter->netdev)))
978 return rc;
979 } else
980 dev->mtu = new_mtu;
Santiago Leon860f2422006-04-25 11:19:59 -0500981 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600982 }
Santiago Leonb6d35182005-10-26 10:47:01 -0600983 }
Santiago Leon860f2422006-04-25 11:19:59 -0500984 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Santiago Leon6b422372006-10-03 12:24:28 -0500987#ifdef CONFIG_NET_POLL_CONTROLLER
988static void ibmveth_poll_controller(struct net_device *dev)
989{
990 ibmveth_replenish_task(dev->priv);
Andrew Morton5f771132006-10-10 14:33:30 -0700991 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -0500992}
993#endif
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
996{
Santiago Leonb6d35182005-10-26 10:47:01 -0600997 int rc, i;
Brian Kingf4ff2872007-09-15 13:36:07 -0700998 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001000 struct ibmveth_adapter *adapter;
Brian Kingf4ff2872007-09-15 13:36:07 -07001001 union ibmveth_illan_attributes set_attr, ret_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 unsigned char *mac_addr_p;
1004 unsigned int *mcastFilterSize_p;
1005
1006
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001007 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dev->unit_address);
1009
Michael Ellerman493a6842007-04-17 13:12:55 +10001010 mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1011 VETH_MAC_ADDR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if(!mac_addr_p) {
1013 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1014 "attribute\n", __FILE__, __LINE__);
1015 return 0;
1016 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001017
Michael Ellerman493a6842007-04-17 13:12:55 +10001018 mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1019 VETH_MCAST_FILTER_SIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if(!mcastFilterSize_p) {
1021 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1022 "VETH_MCAST_FILTER_SIZE attribute\n",
1023 __FILE__, __LINE__);
1024 return 0;
1025 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1028
1029 if(!netdev)
1030 return -ENOMEM;
1031
1032 SET_MODULE_OWNER(netdev);
1033
1034 adapter = netdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dev->dev.driver_data = netdev;
1036
1037 adapter->vdev = dev;
1038 adapter->netdev = netdev;
1039 adapter->mcastFilterSize= *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001040 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001041
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001042 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Some older boxes running PHYP non-natively have an OF that
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001045 returns a 8-byte local-mac-address field (and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 2 bytes have to be ignored) while newer boxes' OF return
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001047 a 6-byte field. Note that IEEE 1275 specifies that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 local-mac-address must be a 6-byte field.
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001049 The RPA doc specifies that the first byte must be 10b, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 we'll just look for it to solve this 8 vs. 6 byte field issue */
1051
1052 if ((*mac_addr_p & 0x3) != 0x02)
1053 mac_addr_p += 2;
1054
1055 adapter->mac_addr = 0;
1056 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 netdev->irq = dev->irq;
1059 netdev->open = ibmveth_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 netdev->stop = ibmveth_close;
1061 netdev->hard_start_xmit = ibmveth_start_xmit;
1062 netdev->get_stats = ibmveth_get_stats;
1063 netdev->set_multicast_list = ibmveth_set_multicast_list;
1064 netdev->do_ioctl = ibmveth_ioctl;
1065 netdev->ethtool_ops = &netdev_ethtool_ops;
1066 netdev->change_mtu = ibmveth_change_mtu;
1067 SET_NETDEV_DEV(netdev, &dev->dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001068#ifdef CONFIG_NET_POLL_CONTROLLER
1069 netdev->poll_controller = ibmveth_poll_controller;
1070#endif
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001071 netdev->features |= NETIF_F_LLTX;
Santiago Leon60296d92005-10-26 10:47:16 -06001072 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1075
Santiago Leon860f2422006-04-25 11:19:59 -05001076 for(i = 0; i<IbmVethNumBufferPools; i++) {
1077 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001078 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1079 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001080 pool_active[i]);
1081 kobj->parent = &dev->dev.kobj;
1082 sprintf(kobj->name, "pool%d", i);
1083 kobj->ktype = &ktype_veth_pool;
1084 kobject_register(kobj);
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 adapter->buffer_list_dma = DMA_ERROR_CODE;
1090 adapter->filter_list_dma = DMA_ERROR_CODE;
1091 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ibmveth_debug_printk("registering netdev...\n");
1094
Brian Kingf4ff2872007-09-15 13:36:07 -07001095 ret = h_illan_attributes(dev->unit_address, 0, 0, &ret_attr.desc);
1096
1097 if (ret == H_SUCCESS && !ret_attr.fields.active_trunk &&
1098 !ret_attr.fields.trunk_priority &&
1099 ret_attr.fields.csum_offload_padded_pkt_support) {
1100 set_attr.desc = 0;
1101 set_attr.fields.tcp_csum_offload_ipv4 = 1;
1102
1103 ret = h_illan_attributes(dev->unit_address, 0, set_attr.desc,
1104 &ret_attr.desc);
1105
1106 if (ret == H_SUCCESS)
1107 netdev->features |= NETIF_F_IP_CSUM;
1108 else
1109 ret = h_illan_attributes(dev->unit_address, set_attr.desc,
1110 0, &ret_attr.desc);
1111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 rc = register_netdev(netdev);
1114
1115 if(rc) {
1116 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1117 free_netdev(netdev);
1118 return rc;
1119 }
1120
1121 ibmveth_debug_printk("registered\n");
1122
1123 ibmveth_proc_register_adapter(adapter);
1124
1125 return 0;
1126}
1127
1128static int __devexit ibmveth_remove(struct vio_dev *dev)
1129{
1130 struct net_device *netdev = dev->dev.driver_data;
1131 struct ibmveth_adapter *adapter = netdev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001132 int i;
1133
1134 for(i = 0; i<IbmVethNumBufferPools; i++)
1135 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 unregister_netdev(netdev);
1138
1139 ibmveth_proc_unregister_adapter(adapter);
1140
1141 free_netdev(netdev);
1142 return 0;
1143}
1144
1145#ifdef CONFIG_PROC_FS
1146static void ibmveth_proc_register_driver(void)
1147{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001148 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (ibmveth_proc_dir) {
1150 SET_MODULE_OWNER(ibmveth_proc_dir);
1151 }
1152}
1153
1154static void ibmveth_proc_unregister_driver(void)
1155{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001156 remove_proc_entry(IBMVETH_PROC_DIR, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001159static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 if (*pos == 0) {
1162 return (void *)1;
1163 } else {
1164 return NULL;
1165 }
1166}
1167
1168static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1169{
1170 ++*pos;
1171 return NULL;
1172}
1173
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001174static void ibmveth_seq_stop(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176}
1177
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001178static int ibmveth_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct ibmveth_adapter *adapter = seq->private;
1181 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1182 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1183
1184 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1188 current_mac[0], current_mac[1], current_mac[2],
1189 current_mac[3], current_mac[4], current_mac[5]);
1190 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1191 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1192 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 seq_printf(seq, "\nAdapter Statistics:\n");
1195 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1196 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1197 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1198 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1199 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1200 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1201 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1202 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1203 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1204 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return 0;
1207}
1208static struct seq_operations ibmveth_seq_ops = {
1209 .start = ibmveth_seq_start,
1210 .next = ibmveth_seq_next,
1211 .stop = ibmveth_seq_stop,
1212 .show = ibmveth_seq_show,
1213};
1214
1215static int ibmveth_proc_open(struct inode *inode, struct file *file)
1216{
1217 struct seq_file *seq;
1218 struct proc_dir_entry *proc;
1219 int rc;
1220
1221 rc = seq_open(file, &ibmveth_seq_ops);
1222 if (!rc) {
1223 /* recover the pointer buried in proc_dir_entry data */
1224 seq = file->private_data;
1225 proc = PDE(inode);
1226 seq->private = proc->data;
1227 }
1228 return rc;
1229}
1230
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001231static const struct file_operations ibmveth_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 .owner = THIS_MODULE,
1233 .open = ibmveth_proc_open,
1234 .read = seq_read,
1235 .llseek = seq_lseek,
1236 .release = seq_release,
1237};
1238
1239static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1240{
1241 struct proc_dir_entry *entry;
1242 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001243 char u_addr[10];
1244 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1245 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!entry) {
1247 ibmveth_error_printk("Cannot create adapter proc entry");
1248 } else {
1249 entry->data = (void *) adapter;
1250 entry->proc_fops = &ibmveth_proc_fops;
1251 SET_MODULE_OWNER(entry);
1252 }
1253 }
1254 return;
1255}
1256
1257static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1258{
1259 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001260 char u_addr[10];
1261 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1262 remove_proc_entry(u_addr, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264}
1265
1266#else /* CONFIG_PROC_FS */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001267static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269}
1270
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001271static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273}
1274static void ibmveth_proc_register_driver(void)
1275{
1276}
1277
1278static void ibmveth_proc_unregister_driver(void)
1279{
1280}
1281#endif /* CONFIG_PROC_FS */
1282
Santiago Leon860f2422006-04-25 11:19:59 -05001283static struct attribute veth_active_attr;
1284static struct attribute veth_num_attr;
1285static struct attribute veth_size_attr;
1286
1287static ssize_t veth_pool_show(struct kobject * kobj,
1288 struct attribute * attr, char * buf)
1289{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001290 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001291 struct ibmveth_buff_pool,
1292 kobj);
1293
1294 if (attr == &veth_active_attr)
1295 return sprintf(buf, "%d\n", pool->active);
1296 else if (attr == &veth_num_attr)
1297 return sprintf(buf, "%d\n", pool->size);
1298 else if (attr == &veth_size_attr)
1299 return sprintf(buf, "%d\n", pool->buff_size);
1300 return 0;
1301}
1302
1303static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1304const char * buf, size_t count)
1305{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001306 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001307 struct ibmveth_buff_pool,
1308 kobj);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001309 struct net_device *netdev =
Santiago Leon860f2422006-04-25 11:19:59 -05001310 container_of(kobj->parent, struct device, kobj)->driver_data;
1311 struct ibmveth_adapter *adapter = netdev->priv;
1312 long value = simple_strtol(buf, NULL, 10);
1313 long rc;
1314
1315 if (attr == &veth_active_attr) {
1316 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001317 if (netif_running(netdev)) {
1318 if(ibmveth_alloc_buffer_pool(pool)) {
1319 ibmveth_error_printk("unable to alloc pool\n");
1320 return -ENOMEM;
1321 }
1322 pool->active = 1;
1323 adapter->pool_config = 1;
1324 ibmveth_close(netdev);
1325 adapter->pool_config = 0;
1326 if ((rc = ibmveth_open(netdev)))
1327 return rc;
1328 } else
1329 pool->active = 1;
Santiago Leon860f2422006-04-25 11:19:59 -05001330 } else if (!value && pool->active) {
1331 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1332 int i;
1333 /* Make sure there is a buffer pool with buffers that
1334 can hold a packet of the size of the MTU */
Brian King76b9cfc2007-08-03 13:55:19 +10001335 for (i = 0; i < IbmVethNumBufferPools; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001336 if (pool == &adapter->rx_buff_pool[i])
1337 continue;
1338 if (!adapter->rx_buff_pool[i].active)
1339 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001340 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1341 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001342 }
Brian King76b9cfc2007-08-03 13:55:19 +10001343
1344 if (i == IbmVethNumBufferPools) {
Santiago Leon860f2422006-04-25 11:19:59 -05001345 ibmveth_error_printk("no active pool >= MTU\n");
1346 return -EPERM;
1347 }
Brian King76b9cfc2007-08-03 13:55:19 +10001348
1349 pool->active = 0;
1350 if (netif_running(netdev)) {
1351 adapter->pool_config = 1;
1352 ibmveth_close(netdev);
1353 adapter->pool_config = 0;
1354 if ((rc = ibmveth_open(netdev)))
1355 return rc;
1356 }
Santiago Leon860f2422006-04-25 11:19:59 -05001357 }
1358 } else if (attr == &veth_num_attr) {
1359 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1360 return -EINVAL;
1361 else {
Brian King4aa9c932007-06-08 14:05:16 -05001362 if (netif_running(netdev)) {
1363 adapter->pool_config = 1;
1364 ibmveth_close(netdev);
1365 adapter->pool_config = 0;
1366 pool->size = value;
1367 if ((rc = ibmveth_open(netdev)))
1368 return rc;
1369 } else
1370 pool->size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001371 }
1372 } else if (attr == &veth_size_attr) {
1373 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1374 return -EINVAL;
1375 else {
Brian King4aa9c932007-06-08 14:05:16 -05001376 if (netif_running(netdev)) {
1377 adapter->pool_config = 1;
1378 ibmveth_close(netdev);
1379 adapter->pool_config = 0;
1380 pool->buff_size = value;
1381 if ((rc = ibmveth_open(netdev)))
1382 return rc;
1383 } else
1384 pool->buff_size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001385 }
1386 }
1387
1388 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001389 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001390 return count;
1391}
1392
1393
1394#define ATTR(_name, _mode) \
1395 struct attribute veth_##_name##_attr = { \
Tejun Heo7b595752007-06-14 03:45:17 +09001396 .name = __stringify(_name), .mode = _mode, \
Santiago Leon860f2422006-04-25 11:19:59 -05001397 };
1398
1399static ATTR(active, 0644);
1400static ATTR(num, 0644);
1401static ATTR(size, 0644);
1402
1403static struct attribute * veth_pool_attrs[] = {
1404 &veth_active_attr,
1405 &veth_num_attr,
1406 &veth_size_attr,
1407 NULL,
1408};
1409
1410static struct sysfs_ops veth_pool_ops = {
1411 .show = veth_pool_show,
1412 .store = veth_pool_store,
1413};
1414
1415static struct kobj_type ktype_veth_pool = {
1416 .release = NULL,
1417 .sysfs_ops = &veth_pool_ops,
1418 .default_attrs = veth_pool_attrs,
1419};
1420
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1423 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001424 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1427
1428static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001429 .id_table = ibmveth_device_table,
1430 .probe = ibmveth_probe,
1431 .remove = ibmveth_remove,
1432 .driver = {
1433 .name = ibmveth_driver_name,
Stephen Rothwell915124d2005-10-24 15:12:22 +10001434 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436};
1437
1438static int __init ibmveth_module_init(void)
1439{
1440 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1441
1442 ibmveth_proc_register_driver();
1443
1444 return vio_register_driver(&ibmveth_driver);
1445}
1446
1447static void __exit ibmveth_module_exit(void)
1448{
1449 vio_unregister_driver(&ibmveth_driver);
1450 ibmveth_proc_unregister_driver();
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453module_init(ibmveth_module_init);
1454module_exit(ibmveth_module_exit);