[BNX2]: Update version and rel date.
[linux-2.6] / drivers / net / ibmveth.c
1 /**************************************************************************/
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.   */
27 /*                                                                        */
28 /**************************************************************************/
29 /*
30   TODO:
31   - remove frag processing code - no longer needed
32   - add support for sysfs
33   - possibly remove procfs support
34 */
35
36 #include <linux/module.h>
37 #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>
50 #include <asm/semaphore.h>
51 #include <asm/hvcall.h>
52 #include <asm/atomic.h>
53 #include <asm/iommu.h>
54 #include <asm/vio.h>
55 #include <asm/uaccess.h>
56 #include <linux/seq_file.h>
57
58 #include "ibmveth.h"
59
60 #undef DEBUG
61
62 #define ibmveth_printk(fmt, args...) \
63   printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
64
65 #define ibmveth_error_printk(fmt, args...) \
66   printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
67
68 #ifdef DEBUG
69 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
70   printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
71 #define ibmveth_debug_printk(fmt, args...) \
72   printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
73 #define ibmveth_assert(expr) \
74   if(!(expr)) {                                   \
75     printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
76     BUG(); \
77   }
78 #else
79 #define ibmveth_debug_printk_no_adapter(fmt, args...)
80 #define ibmveth_debug_printk(fmt, args...)
81 #define ibmveth_assert(expr)
82 #endif
83
84 static int ibmveth_open(struct net_device *dev);
85 static int ibmveth_close(struct net_device *dev);
86 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
87 static int ibmveth_poll(struct net_device *dev, int *budget);
88 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
89 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
90 static void ibmveth_set_multicast_list(struct net_device *dev);
91 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
92 static void ibmveth_proc_register_driver(void);
93 static void ibmveth_proc_unregister_driver(void);
94 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
95 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
96 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
97 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
98 static struct kobj_type ktype_veth_pool;
99
100 #ifdef CONFIG_PROC_FS
101 #define IBMVETH_PROC_DIR "net/ibmveth"
102 static struct proc_dir_entry *ibmveth_proc_dir;
103 #endif
104
105 static const char ibmveth_driver_name[] = "ibmveth";
106 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
107 #define ibmveth_driver_version "1.03"
108
109 MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
110 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
111 MODULE_LICENSE("GPL");
112 MODULE_VERSION(ibmveth_driver_version);
113
114 /* simple methods of getting data from the current rxq entry */
115 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
116 {
117         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
118 }
119
120 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
121 {
122         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
123 }
124
125 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
126 {
127         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
128 }
129
130 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
131 {
132         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
133 }
134
135 /* setup the initial settings for a buffer pool */
136 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
137 {
138         pool->size = pool_size;
139         pool->index = pool_index;
140         pool->buff_size = buff_size;
141         pool->threshold = pool_size / 2;
142         pool->active = pool_active;
143 }
144
145 /* allocate and setup an buffer pool - called during open */
146 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
147 {
148         int i;
149
150         pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
151
152         if(!pool->free_map) {
153                 return -1;
154         }
155
156         pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
157         if(!pool->dma_addr) {
158                 kfree(pool->free_map);
159                 pool->free_map = NULL;
160                 return -1;
161         }
162
163         pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
164
165         if(!pool->skbuff) {
166                 kfree(pool->dma_addr);
167                 pool->dma_addr = NULL;
168
169                 kfree(pool->free_map);
170                 pool->free_map = NULL;
171                 return -1;
172         }
173
174         memset(pool->skbuff, 0, sizeof(void*) * pool->size);
175         memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
176
177         for(i = 0; i < pool->size; ++i) {
178                 pool->free_map[i] = i;
179         }
180
181         atomic_set(&pool->available, 0);
182         pool->producer_index = 0;
183         pool->consumer_index = 0;
184
185         return 0;
186 }
187
188 /* replenish the buffers for a pool.  note that we don't need to
189  * skb_reserve these since they are used for incoming...
190  */
191 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
192 {
193         u32 i;
194         u32 count = pool->size - atomic_read(&pool->available);
195         u32 buffers_added = 0;
196
197         mb();
198
199         for(i = 0; i < count; ++i) {
200                 struct sk_buff *skb;
201                 unsigned int free_index, index;
202                 u64 correlator;
203                 union ibmveth_buf_desc desc;
204                 unsigned long lpar_rc;
205                 dma_addr_t dma_addr;
206
207                 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
208
209                 if(!skb) {
210                         ibmveth_debug_printk("replenish: unable to allocate skb\n");
211                         adapter->replenish_no_mem++;
212                         break;
213                 }
214
215                 free_index = pool->consumer_index;
216                 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
217                 index = pool->free_map[free_index];
218
219                 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
220                 ibmveth_assert(pool->skbuff[index] == NULL);
221
222                 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
223                                 pool->buff_size, DMA_FROM_DEVICE);
224
225                 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
226                 pool->dma_addr[index] = dma_addr;
227                 pool->skbuff[index] = skb;
228
229                 correlator = ((u64)pool->index << 32) | index;
230                 *(u64*)skb->data = correlator;
231
232                 desc.desc = 0;
233                 desc.fields.valid = 1;
234                 desc.fields.length = pool->buff_size;
235                 desc.fields.address = dma_addr;
236
237                 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
238
239                 if(lpar_rc != H_SUCCESS) {
240                         pool->free_map[free_index] = index;
241                         pool->skbuff[index] = NULL;
242                         if (pool->consumer_index == 0)
243                                 pool->consumer_index = pool->size - 1;
244                         else
245                                 pool->consumer_index--;
246                         dma_unmap_single(&adapter->vdev->dev,
247                                         pool->dma_addr[index], pool->buff_size,
248                                         DMA_FROM_DEVICE);
249                         dev_kfree_skb_any(skb);
250                         adapter->replenish_add_buff_failure++;
251                         break;
252                 } else {
253                         buffers_added++;
254                         adapter->replenish_add_buff_success++;
255                 }
256         }
257
258         mb();
259         atomic_add(buffers_added, &(pool->available));
260 }
261
262 /* replenish routine */
263 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
264 {
265         int i;
266
267         adapter->replenish_task_cycles++;
268
269         for(i = 0; i < IbmVethNumBufferPools; i++)
270                 if(adapter->rx_buff_pool[i].active)
271                         ibmveth_replenish_buffer_pool(adapter,
272                                                      &adapter->rx_buff_pool[i]);
273
274         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
275 }
276
277 /* empty and free ana buffer pool - also used to do cleanup in error paths */
278 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
279 {
280         int i;
281
282         kfree(pool->free_map);
283         pool->free_map = NULL;
284
285         if(pool->skbuff && pool->dma_addr) {
286                 for(i = 0; i < pool->size; ++i) {
287                         struct sk_buff *skb = pool->skbuff[i];
288                         if(skb) {
289                                 dma_unmap_single(&adapter->vdev->dev,
290                                                  pool->dma_addr[i],
291                                                  pool->buff_size,
292                                                  DMA_FROM_DEVICE);
293                                 dev_kfree_skb_any(skb);
294                                 pool->skbuff[i] = NULL;
295                         }
296                 }
297         }
298
299         if(pool->dma_addr) {
300                 kfree(pool->dma_addr);
301                 pool->dma_addr = NULL;
302         }
303
304         if(pool->skbuff) {
305                 kfree(pool->skbuff);
306                 pool->skbuff = NULL;
307         }
308 }
309
310 /* remove a buffer from a pool */
311 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
312 {
313         unsigned int pool  = correlator >> 32;
314         unsigned int index = correlator & 0xffffffffUL;
315         unsigned int free_index;
316         struct sk_buff *skb;
317
318         ibmveth_assert(pool < IbmVethNumBufferPools);
319         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
320
321         skb = adapter->rx_buff_pool[pool].skbuff[index];
322
323         ibmveth_assert(skb != NULL);
324
325         adapter->rx_buff_pool[pool].skbuff[index] = NULL;
326
327         dma_unmap_single(&adapter->vdev->dev,
328                          adapter->rx_buff_pool[pool].dma_addr[index],
329                          adapter->rx_buff_pool[pool].buff_size,
330                          DMA_FROM_DEVICE);
331
332         free_index = adapter->rx_buff_pool[pool].producer_index;
333         adapter->rx_buff_pool[pool].producer_index
334                 = (adapter->rx_buff_pool[pool].producer_index + 1)
335                 % adapter->rx_buff_pool[pool].size;
336         adapter->rx_buff_pool[pool].free_map[free_index] = index;
337
338         mb();
339
340         atomic_dec(&(adapter->rx_buff_pool[pool].available));
341 }
342
343 /* get the current buffer on the rx queue */
344 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
345 {
346         u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
347         unsigned int pool = correlator >> 32;
348         unsigned int index = correlator & 0xffffffffUL;
349
350         ibmveth_assert(pool < IbmVethNumBufferPools);
351         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
352
353         return adapter->rx_buff_pool[pool].skbuff[index];
354 }
355
356 /* recycle the current buffer on the rx queue */
357 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
358 {
359         u32 q_index = adapter->rx_queue.index;
360         u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
361         unsigned int pool = correlator >> 32;
362         unsigned int index = correlator & 0xffffffffUL;
363         union ibmveth_buf_desc desc;
364         unsigned long lpar_rc;
365
366         ibmveth_assert(pool < IbmVethNumBufferPools);
367         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
368
369         if(!adapter->rx_buff_pool[pool].active) {
370                 ibmveth_rxq_harvest_buffer(adapter);
371                 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
372                 return;
373         }
374
375         desc.desc = 0;
376         desc.fields.valid = 1;
377         desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
378         desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
379
380         lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
381
382         if(lpar_rc != H_SUCCESS) {
383                 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
384                 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
385         }
386
387         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
388                 adapter->rx_queue.index = 0;
389                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
390         }
391 }
392
393 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
394 {
395         ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
396
397         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
398                 adapter->rx_queue.index = 0;
399                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
400         }
401 }
402
403 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
404 {
405         int i;
406
407         if(adapter->buffer_list_addr != NULL) {
408                 if(!dma_mapping_error(adapter->buffer_list_dma)) {
409                         dma_unmap_single(&adapter->vdev->dev,
410                                         adapter->buffer_list_dma, 4096,
411                                         DMA_BIDIRECTIONAL);
412                         adapter->buffer_list_dma = DMA_ERROR_CODE;
413                 }
414                 free_page((unsigned long)adapter->buffer_list_addr);
415                 adapter->buffer_list_addr = NULL;
416         }
417
418         if(adapter->filter_list_addr != NULL) {
419                 if(!dma_mapping_error(adapter->filter_list_dma)) {
420                         dma_unmap_single(&adapter->vdev->dev,
421                                         adapter->filter_list_dma, 4096,
422                                         DMA_BIDIRECTIONAL);
423                         adapter->filter_list_dma = DMA_ERROR_CODE;
424                 }
425                 free_page((unsigned long)adapter->filter_list_addr);
426                 adapter->filter_list_addr = NULL;
427         }
428
429         if(adapter->rx_queue.queue_addr != NULL) {
430                 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
431                         dma_unmap_single(&adapter->vdev->dev,
432                                         adapter->rx_queue.queue_dma,
433                                         adapter->rx_queue.queue_len,
434                                         DMA_BIDIRECTIONAL);
435                         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
436                 }
437                 kfree(adapter->rx_queue.queue_addr);
438                 adapter->rx_queue.queue_addr = NULL;
439         }
440
441         for(i = 0; i<IbmVethNumBufferPools; i++)
442                 if (adapter->rx_buff_pool[i].active)
443                         ibmveth_free_buffer_pool(adapter,
444                                                  &adapter->rx_buff_pool[i]);
445 }
446
447 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
448         union ibmveth_buf_desc rxq_desc, u64 mac_address)
449 {
450         int rc, try_again = 1;
451
452         /* After a kexec the adapter will still be open, so our attempt to
453         * open it will fail. So if we get a failure we free the adapter and
454         * try again, but only once. */
455 retry:
456         rc = h_register_logical_lan(adapter->vdev->unit_address,
457                                     adapter->buffer_list_dma, rxq_desc.desc,
458                                     adapter->filter_list_dma, mac_address);
459
460         if (rc != H_SUCCESS && try_again) {
461                 do {
462                         rc = h_free_logical_lan(adapter->vdev->unit_address);
463                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
464
465                 try_again = 0;
466                 goto retry;
467         }
468
469         return rc;
470 }
471
472 static int ibmveth_open(struct net_device *netdev)
473 {
474         struct ibmveth_adapter *adapter = netdev->priv;
475         u64 mac_address = 0;
476         int rxq_entries = 1;
477         unsigned long lpar_rc;
478         int rc;
479         union ibmveth_buf_desc rxq_desc;
480         int i;
481
482         ibmveth_debug_printk("open starting\n");
483
484         for(i = 0; i<IbmVethNumBufferPools; i++)
485                 rxq_entries += adapter->rx_buff_pool[i].size;
486
487         adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
488         adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
489
490         if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
491                 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
492                 ibmveth_cleanup(adapter);
493                 return -ENOMEM;
494         }
495
496         adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
497         adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
498
499         if(!adapter->rx_queue.queue_addr) {
500                 ibmveth_error_printk("unable to allocate rx queue pages\n");
501                 ibmveth_cleanup(adapter);
502                 return -ENOMEM;
503         }
504
505         adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
506                         adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
507         adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
508                         adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
509         adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
510                         adapter->rx_queue.queue_addr,
511                         adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
512
513         if((dma_mapping_error(adapter->buffer_list_dma) ) ||
514            (dma_mapping_error(adapter->filter_list_dma)) ||
515            (dma_mapping_error(adapter->rx_queue.queue_dma))) {
516                 ibmveth_error_printk("unable to map filter or buffer list pages\n");
517                 ibmveth_cleanup(adapter);
518                 return -ENOMEM;
519         }
520
521         adapter->rx_queue.index = 0;
522         adapter->rx_queue.num_slots = rxq_entries;
523         adapter->rx_queue.toggle = 1;
524
525         memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
526         mac_address = mac_address >> 16;
527
528         rxq_desc.desc = 0;
529         rxq_desc.fields.valid = 1;
530         rxq_desc.fields.length = adapter->rx_queue.queue_len;
531         rxq_desc.fields.address = adapter->rx_queue.queue_dma;
532
533         ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
534         ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
535         ibmveth_debug_printk("receive q   @ 0x%p\n", adapter->rx_queue.queue_addr);
536
537         h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
538
539         lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
540
541         if(lpar_rc != H_SUCCESS) {
542                 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
543                 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
544                                      adapter->buffer_list_dma,
545                                      adapter->filter_list_dma,
546                                      rxq_desc.desc,
547                                      mac_address);
548                 ibmveth_cleanup(adapter);
549                 return -ENONET;
550         }
551
552         for(i = 0; i<IbmVethNumBufferPools; i++) {
553                 if(!adapter->rx_buff_pool[i].active)
554                         continue;
555                 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
556                         ibmveth_error_printk("unable to alloc pool\n");
557                         adapter->rx_buff_pool[i].active = 0;
558                         ibmveth_cleanup(adapter);
559                         return -ENOMEM ;
560                 }
561         }
562
563         ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
564         if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
565                 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
566                 do {
567                         rc = h_free_logical_lan(adapter->vdev->unit_address);
568                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
569
570                 ibmveth_cleanup(adapter);
571                 return rc;
572         }
573
574         ibmveth_debug_printk("initial replenish cycle\n");
575         ibmveth_interrupt(netdev->irq, netdev);
576
577         netif_start_queue(netdev);
578
579         ibmveth_debug_printk("open complete\n");
580
581         return 0;
582 }
583
584 static int ibmveth_close(struct net_device *netdev)
585 {
586         struct ibmveth_adapter *adapter = netdev->priv;
587         long lpar_rc;
588
589         ibmveth_debug_printk("close starting\n");
590
591         if (!adapter->pool_config)
592                 netif_stop_queue(netdev);
593
594         free_irq(netdev->irq, netdev);
595
596         do {
597                 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
598         } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
599
600         if(lpar_rc != H_SUCCESS)
601         {
602                 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
603                                      lpar_rc);
604         }
605
606         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
607
608         ibmveth_cleanup(adapter);
609
610         ibmveth_debug_printk("close complete\n");
611
612         return 0;
613 }
614
615 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
616         cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
617         cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
618         cmd->speed = SPEED_1000;
619         cmd->duplex = DUPLEX_FULL;
620         cmd->port = PORT_FIBRE;
621         cmd->phy_address = 0;
622         cmd->transceiver = XCVR_INTERNAL;
623         cmd->autoneg = AUTONEG_ENABLE;
624         cmd->maxtxpkt = 0;
625         cmd->maxrxpkt = 1;
626         return 0;
627 }
628
629 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
630         strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
631         strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
632 }
633
634 static u32 netdev_get_link(struct net_device *dev) {
635         return 1;
636 }
637
638 static const struct ethtool_ops netdev_ethtool_ops = {
639         .get_drvinfo            = netdev_get_drvinfo,
640         .get_settings           = netdev_get_settings,
641         .get_link               = netdev_get_link,
642         .get_sg                 = ethtool_op_get_sg,
643         .get_tx_csum            = ethtool_op_get_tx_csum,
644 };
645
646 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
647 {
648         return -EOPNOTSUPP;
649 }
650
651 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
652
653 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
654 {
655         struct ibmveth_adapter *adapter = netdev->priv;
656         union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
657         unsigned long lpar_rc;
658         int nfrags = 0, curfrag;
659         unsigned long correlator;
660         unsigned long flags;
661         unsigned int retry_count;
662         unsigned int tx_dropped = 0;
663         unsigned int tx_bytes = 0;
664         unsigned int tx_packets = 0;
665         unsigned int tx_send_failed = 0;
666         unsigned int tx_map_failed = 0;
667
668
669         if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
670                 tx_dropped++;
671                 goto out;
672         }
673
674         memset(&desc, 0, sizeof(desc));
675
676         /* nfrags = number of frags after the initial fragment */
677         nfrags = skb_shinfo(skb)->nr_frags;
678
679         if(nfrags)
680                 adapter->tx_multidesc_send++;
681
682         /* map the initial fragment */
683         desc[0].fields.length  = nfrags ? skb->len - skb->data_len : skb->len;
684         desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
685                                         desc[0].fields.length, DMA_TO_DEVICE);
686         desc[0].fields.valid   = 1;
687
688         if(dma_mapping_error(desc[0].fields.address)) {
689                 ibmveth_error_printk("tx: unable to map initial fragment\n");
690                 tx_map_failed++;
691                 tx_dropped++;
692                 goto out;
693         }
694
695         curfrag = nfrags;
696
697         /* map fragments past the initial portion if there are any */
698         while(curfrag--) {
699                 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
700                 desc[curfrag+1].fields.address
701                         = dma_map_single(&adapter->vdev->dev,
702                                 page_address(frag->page) + frag->page_offset,
703                                 frag->size, DMA_TO_DEVICE);
704                 desc[curfrag+1].fields.length = frag->size;
705                 desc[curfrag+1].fields.valid  = 1;
706
707                 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
708                         ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
709                         tx_map_failed++;
710                         tx_dropped++;
711                         /* Free all the mappings we just created */
712                         while(curfrag < nfrags) {
713                                 dma_unmap_single(&adapter->vdev->dev,
714                                                  desc[curfrag+1].fields.address,
715                                                  desc[curfrag+1].fields.length,
716                                                  DMA_TO_DEVICE);
717                                 curfrag++;
718                         }
719                         goto out;
720                 }
721         }
722
723         /* send the frame. Arbitrarily set retrycount to 1024 */
724         correlator = 0;
725         retry_count = 1024;
726         do {
727                 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
728                                              desc[0].desc,
729                                              desc[1].desc,
730                                              desc[2].desc,
731                                              desc[3].desc,
732                                              desc[4].desc,
733                                              desc[5].desc,
734                                              correlator,
735                                              &correlator);
736         } while ((lpar_rc == H_BUSY) && (retry_count--));
737
738         if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
739                 int i;
740                 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
741                 for(i = 0; i < 6; i++) {
742                         ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
743                                              desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
744                 }
745                 tx_send_failed++;
746                 tx_dropped++;
747         } else {
748                 tx_packets++;
749                 tx_bytes += skb->len;
750                 netdev->trans_start = jiffies;
751         }
752
753         do {
754                 dma_unmap_single(&adapter->vdev->dev,
755                                 desc[nfrags].fields.address,
756                                 desc[nfrags].fields.length, DMA_TO_DEVICE);
757         } while(--nfrags >= 0);
758
759 out:    spin_lock_irqsave(&adapter->stats_lock, flags);
760         adapter->stats.tx_dropped += tx_dropped;
761         adapter->stats.tx_bytes += tx_bytes;
762         adapter->stats.tx_packets += tx_packets;
763         adapter->tx_send_failed += tx_send_failed;
764         adapter->tx_map_failed += tx_map_failed;
765         spin_unlock_irqrestore(&adapter->stats_lock, flags);
766
767         dev_kfree_skb(skb);
768         return 0;
769 }
770
771 static int ibmveth_poll(struct net_device *netdev, int *budget)
772 {
773         struct ibmveth_adapter *adapter = netdev->priv;
774         int max_frames_to_process = netdev->quota;
775         int frames_processed = 0;
776         int more_work = 1;
777         unsigned long lpar_rc;
778
779  restart_poll:
780         do {
781                 struct net_device *netdev = adapter->netdev;
782
783                 if(ibmveth_rxq_pending_buffer(adapter)) {
784                         struct sk_buff *skb;
785
786                         rmb();
787
788                         if(!ibmveth_rxq_buffer_valid(adapter)) {
789                                 wmb(); /* suggested by larson1 */
790                                 adapter->rx_invalid_buffer++;
791                                 ibmveth_debug_printk("recycling invalid buffer\n");
792                                 ibmveth_rxq_recycle_buffer(adapter);
793                         } else {
794                                 int length = ibmveth_rxq_frame_length(adapter);
795                                 int offset = ibmveth_rxq_frame_offset(adapter);
796                                 skb = ibmveth_rxq_get_buffer(adapter);
797
798                                 ibmveth_rxq_harvest_buffer(adapter);
799
800                                 skb_reserve(skb, offset);
801                                 skb_put(skb, length);
802                                 skb->dev = netdev;
803                                 skb->protocol = eth_type_trans(skb, netdev);
804
805                                 netif_receive_skb(skb); /* send it up */
806
807                                 adapter->stats.rx_packets++;
808                                 adapter->stats.rx_bytes += length;
809                                 frames_processed++;
810                                 netdev->last_rx = jiffies;
811                         }
812                 } else {
813                         more_work = 0;
814                 }
815         } while(more_work && (frames_processed < max_frames_to_process));
816
817         ibmveth_replenish_task(adapter);
818
819         if(more_work) {
820                 /* more work to do - return that we are not done yet */
821                 netdev->quota -= frames_processed;
822                 *budget -= frames_processed;
823                 return 1;
824         }
825
826         /* we think we are done - reenable interrupts, then check once more to make sure we are done */
827         lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
828
829         ibmveth_assert(lpar_rc == H_SUCCESS);
830
831         netif_rx_complete(netdev);
832
833         if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
834         {
835                 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
836                 ibmveth_assert(lpar_rc == H_SUCCESS);
837                 more_work = 1;
838                 goto restart_poll;
839         }
840
841         netdev->quota -= frames_processed;
842         *budget -= frames_processed;
843
844         /* we really are done */
845         return 0;
846 }
847
848 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
849 {
850         struct net_device *netdev = dev_instance;
851         struct ibmveth_adapter *adapter = netdev->priv;
852         unsigned long lpar_rc;
853
854         if(netif_rx_schedule_prep(netdev)) {
855                 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
856                 ibmveth_assert(lpar_rc == H_SUCCESS);
857                 __netif_rx_schedule(netdev);
858         }
859         return IRQ_HANDLED;
860 }
861
862 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
863 {
864         struct ibmveth_adapter *adapter = dev->priv;
865         return &adapter->stats;
866 }
867
868 static void ibmveth_set_multicast_list(struct net_device *netdev)
869 {
870         struct ibmveth_adapter *adapter = netdev->priv;
871         unsigned long lpar_rc;
872
873         if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
874                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
875                                            IbmVethMcastEnableRecv |
876                                            IbmVethMcastDisableFiltering,
877                                            0);
878                 if(lpar_rc != H_SUCCESS) {
879                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
880                 }
881         } else {
882                 struct dev_mc_list *mclist = netdev->mc_list;
883                 int i;
884                 /* clear the filter table & disable filtering */
885                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
886                                            IbmVethMcastEnableRecv |
887                                            IbmVethMcastDisableFiltering |
888                                            IbmVethMcastClearFilterTable,
889                                            0);
890                 if(lpar_rc != H_SUCCESS) {
891                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
892                 }
893                 /* add the addresses to the filter table */
894                 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
895                         // add the multicast address to the filter table
896                         unsigned long mcast_addr = 0;
897                         memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
898                         lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
899                                                    IbmVethMcastAddFilter,
900                                                    mcast_addr);
901                         if(lpar_rc != H_SUCCESS) {
902                                 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
903                         }
904                 }
905
906                 /* re-enable filtering */
907                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
908                                            IbmVethMcastEnableFiltering,
909                                            0);
910                 if(lpar_rc != H_SUCCESS) {
911                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
912                 }
913         }
914 }
915
916 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
917 {
918         struct ibmveth_adapter *adapter = dev->priv;
919         int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
920         int i;
921
922         if (new_mtu < IBMVETH_MAX_MTU)
923                 return -EINVAL;
924
925         /* Look for an active buffer pool that can hold the new MTU */
926         for(i = 0; i<IbmVethNumBufferPools; i++) {
927                 if (!adapter->rx_buff_pool[i].active)
928                         continue;
929                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
930                         dev->mtu = new_mtu;
931                         return 0;
932                 }
933         }
934         return -EINVAL;
935 }
936
937 #ifdef CONFIG_NET_POLL_CONTROLLER
938 static void ibmveth_poll_controller(struct net_device *dev)
939 {
940         ibmveth_replenish_task(dev->priv);
941         ibmveth_interrupt(dev->irq, dev);
942 }
943 #endif
944
945 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
946 {
947         int rc, i;
948         struct net_device *netdev;
949         struct ibmveth_adapter *adapter = NULL;
950
951         unsigned char *mac_addr_p;
952         unsigned int *mcastFilterSize_p;
953
954
955         ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
956                                         dev->unit_address);
957
958         mac_addr_p = (unsigned char *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
959         if(!mac_addr_p) {
960                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
961                                 "attribute\n", __FILE__, __LINE__);
962                 return 0;
963         }
964
965         mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
966         if(!mcastFilterSize_p) {
967                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
968                                 "VETH_MCAST_FILTER_SIZE attribute\n",
969                                 __FILE__, __LINE__);
970                 return 0;
971         }
972
973         netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
974
975         if(!netdev)
976                 return -ENOMEM;
977
978         SET_MODULE_OWNER(netdev);
979
980         adapter = netdev->priv;
981         memset(adapter, 0, sizeof(adapter));
982         dev->dev.driver_data = netdev;
983
984         adapter->vdev = dev;
985         adapter->netdev = netdev;
986         adapter->mcastFilterSize= *mcastFilterSize_p;
987         adapter->pool_config = 0;
988
989         /*      Some older boxes running PHYP non-natively have an OF that
990                 returns a 8-byte local-mac-address field (and the first
991                 2 bytes have to be ignored) while newer boxes' OF return
992                 a 6-byte field. Note that IEEE 1275 specifies that
993                 local-mac-address must be a 6-byte field.
994                 The RPA doc specifies that the first byte must be 10b, so
995                 we'll just look for it to solve this 8 vs. 6 byte field issue */
996
997         if ((*mac_addr_p & 0x3) != 0x02)
998                 mac_addr_p += 2;
999
1000         adapter->mac_addr = 0;
1001         memcpy(&adapter->mac_addr, mac_addr_p, 6);
1002
1003         adapter->liobn = dev->iommu_table->it_index;
1004
1005         netdev->irq = dev->irq;
1006         netdev->open               = ibmveth_open;
1007         netdev->poll               = ibmveth_poll;
1008         netdev->weight             = 16;
1009         netdev->stop               = ibmveth_close;
1010         netdev->hard_start_xmit    = ibmveth_start_xmit;
1011         netdev->get_stats          = ibmveth_get_stats;
1012         netdev->set_multicast_list = ibmveth_set_multicast_list;
1013         netdev->do_ioctl           = ibmveth_ioctl;
1014         netdev->ethtool_ops           = &netdev_ethtool_ops;
1015         netdev->change_mtu         = ibmveth_change_mtu;
1016         SET_NETDEV_DEV(netdev, &dev->dev);
1017 #ifdef CONFIG_NET_POLL_CONTROLLER
1018         netdev->poll_controller = ibmveth_poll_controller;
1019 #endif
1020         netdev->features |= NETIF_F_LLTX;
1021         spin_lock_init(&adapter->stats_lock);
1022
1023         memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1024
1025         for(i = 0; i<IbmVethNumBufferPools; i++) {
1026                 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1027                 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1028                                          pool_count[i], pool_size[i],
1029                                          pool_active[i]);
1030                 kobj->parent = &dev->dev.kobj;
1031                 sprintf(kobj->name, "pool%d", i);
1032                 kobj->ktype = &ktype_veth_pool;
1033                 kobject_register(kobj);
1034         }
1035
1036         ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1037
1038         adapter->buffer_list_dma = DMA_ERROR_CODE;
1039         adapter->filter_list_dma = DMA_ERROR_CODE;
1040         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1041
1042         ibmveth_debug_printk("registering netdev...\n");
1043
1044         rc = register_netdev(netdev);
1045
1046         if(rc) {
1047                 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1048                 free_netdev(netdev);
1049                 return rc;
1050         }
1051
1052         ibmveth_debug_printk("registered\n");
1053
1054         ibmveth_proc_register_adapter(adapter);
1055
1056         return 0;
1057 }
1058
1059 static int __devexit ibmveth_remove(struct vio_dev *dev)
1060 {
1061         struct net_device *netdev = dev->dev.driver_data;
1062         struct ibmveth_adapter *adapter = netdev->priv;
1063         int i;
1064
1065         for(i = 0; i<IbmVethNumBufferPools; i++)
1066                 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
1067
1068         unregister_netdev(netdev);
1069
1070         ibmveth_proc_unregister_adapter(adapter);
1071
1072         free_netdev(netdev);
1073         return 0;
1074 }
1075
1076 #ifdef CONFIG_PROC_FS
1077 static void ibmveth_proc_register_driver(void)
1078 {
1079         ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
1080         if (ibmveth_proc_dir) {
1081                 SET_MODULE_OWNER(ibmveth_proc_dir);
1082         }
1083 }
1084
1085 static void ibmveth_proc_unregister_driver(void)
1086 {
1087         remove_proc_entry(IBMVETH_PROC_DIR, NULL);
1088 }
1089
1090 static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
1091 {
1092         if (*pos == 0) {
1093                 return (void *)1;
1094         } else {
1095                 return NULL;
1096         }
1097 }
1098
1099 static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1100 {
1101         ++*pos;
1102         return NULL;
1103 }
1104
1105 static void ibmveth_seq_stop(struct seq_file *seq, void *v)
1106 {
1107 }
1108
1109 static int ibmveth_seq_show(struct seq_file *seq, void *v)
1110 {
1111         struct ibmveth_adapter *adapter = seq->private;
1112         char *current_mac = ((char*) &adapter->netdev->dev_addr);
1113         char *firmware_mac = ((char*) &adapter->mac_addr) ;
1114
1115         seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1116
1117         seq_printf(seq, "Unit Address:    0x%x\n", adapter->vdev->unit_address);
1118         seq_printf(seq, "LIOBN:           0x%lx\n", adapter->liobn);
1119         seq_printf(seq, "Current MAC:     %02X:%02X:%02X:%02X:%02X:%02X\n",
1120                    current_mac[0], current_mac[1], current_mac[2],
1121                    current_mac[3], current_mac[4], current_mac[5]);
1122         seq_printf(seq, "Firmware MAC:    %02X:%02X:%02X:%02X:%02X:%02X\n",
1123                    firmware_mac[0], firmware_mac[1], firmware_mac[2],
1124                    firmware_mac[3], firmware_mac[4], firmware_mac[5]);
1125
1126         seq_printf(seq, "\nAdapter Statistics:\n");
1127         seq_printf(seq, "  TX:  skbuffs linearized:          %ld\n", adapter->tx_linearized);
1128         seq_printf(seq, "       multi-descriptor sends:      %ld\n", adapter->tx_multidesc_send);
1129         seq_printf(seq, "       skb_linearize failures:      %ld\n", adapter->tx_linearize_failed);
1130         seq_printf(seq, "       vio_map_single failres:      %ld\n", adapter->tx_map_failed);
1131         seq_printf(seq, "       send failures:               %ld\n", adapter->tx_send_failed);
1132         seq_printf(seq, "  RX:  replenish task cycles:       %ld\n", adapter->replenish_task_cycles);
1133         seq_printf(seq, "       alloc_skb_failures:          %ld\n", adapter->replenish_no_mem);
1134         seq_printf(seq, "       add buffer failures:         %ld\n", adapter->replenish_add_buff_failure);
1135         seq_printf(seq, "       invalid buffers:             %ld\n", adapter->rx_invalid_buffer);
1136         seq_printf(seq, "       no buffers:                  %ld\n", adapter->rx_no_buffer);
1137
1138         return 0;
1139 }
1140 static struct seq_operations ibmveth_seq_ops = {
1141         .start = ibmveth_seq_start,
1142         .next  = ibmveth_seq_next,
1143         .stop  = ibmveth_seq_stop,
1144         .show  = ibmveth_seq_show,
1145 };
1146
1147 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1148 {
1149         struct seq_file *seq;
1150         struct proc_dir_entry *proc;
1151         int rc;
1152
1153         rc = seq_open(file, &ibmveth_seq_ops);
1154         if (!rc) {
1155                 /* recover the pointer buried in proc_dir_entry data */
1156                 seq = file->private_data;
1157                 proc = PDE(inode);
1158                 seq->private = proc->data;
1159         }
1160         return rc;
1161 }
1162
1163 static struct file_operations ibmveth_proc_fops = {
1164         .owner   = THIS_MODULE,
1165         .open    = ibmveth_proc_open,
1166         .read    = seq_read,
1167         .llseek  = seq_lseek,
1168         .release = seq_release,
1169 };
1170
1171 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1172 {
1173         struct proc_dir_entry *entry;
1174         if (ibmveth_proc_dir) {
1175                 char u_addr[10];
1176                 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1177                 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
1178                 if (!entry) {
1179                         ibmveth_error_printk("Cannot create adapter proc entry");
1180                 } else {
1181                         entry->data = (void *) adapter;
1182                         entry->proc_fops = &ibmveth_proc_fops;
1183                         SET_MODULE_OWNER(entry);
1184                 }
1185         }
1186         return;
1187 }
1188
1189 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1190 {
1191         if (ibmveth_proc_dir) {
1192                 char u_addr[10];
1193                 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1194                 remove_proc_entry(u_addr, ibmveth_proc_dir);
1195         }
1196 }
1197
1198 #else /* CONFIG_PROC_FS */
1199 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1200 {
1201 }
1202
1203 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1204 {
1205 }
1206 static void ibmveth_proc_register_driver(void)
1207 {
1208 }
1209
1210 static void ibmveth_proc_unregister_driver(void)
1211 {
1212 }
1213 #endif /* CONFIG_PROC_FS */
1214
1215 static struct attribute veth_active_attr;
1216 static struct attribute veth_num_attr;
1217 static struct attribute veth_size_attr;
1218
1219 static ssize_t veth_pool_show(struct kobject * kobj,
1220                               struct attribute * attr, char * buf)
1221 {
1222         struct ibmveth_buff_pool *pool = container_of(kobj,
1223                                                       struct ibmveth_buff_pool,
1224                                                       kobj);
1225
1226         if (attr == &veth_active_attr)
1227                 return sprintf(buf, "%d\n", pool->active);
1228         else if (attr == &veth_num_attr)
1229                 return sprintf(buf, "%d\n", pool->size);
1230         else if (attr == &veth_size_attr)
1231                 return sprintf(buf, "%d\n", pool->buff_size);
1232         return 0;
1233 }
1234
1235 static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1236 const char * buf, size_t count)
1237 {
1238         struct ibmveth_buff_pool *pool = container_of(kobj,
1239                                                       struct ibmveth_buff_pool,
1240                                                       kobj);
1241         struct net_device *netdev =
1242             container_of(kobj->parent, struct device, kobj)->driver_data;
1243         struct ibmveth_adapter *adapter = netdev->priv;
1244         long value = simple_strtol(buf, NULL, 10);
1245         long rc;
1246
1247         if (attr == &veth_active_attr) {
1248                 if (value && !pool->active) {
1249                         if(ibmveth_alloc_buffer_pool(pool)) {
1250                                 ibmveth_error_printk("unable to alloc pool\n");
1251                                 return -ENOMEM;
1252                         }
1253                         pool->active = 1;
1254                         adapter->pool_config = 1;
1255                         ibmveth_close(netdev);
1256                         adapter->pool_config = 0;
1257                         if ((rc = ibmveth_open(netdev)))
1258                                 return rc;
1259                 } else if (!value && pool->active) {
1260                         int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1261                         int i;
1262                         /* Make sure there is a buffer pool with buffers that
1263                            can hold a packet of the size of the MTU */
1264                         for(i = 0; i<IbmVethNumBufferPools; i++) {
1265                                 if (pool == &adapter->rx_buff_pool[i])
1266                                         continue;
1267                                 if (!adapter->rx_buff_pool[i].active)
1268                                         continue;
1269                                 if (mtu < adapter->rx_buff_pool[i].buff_size) {
1270                                         pool->active = 0;
1271                                         h_free_logical_lan_buffer(adapter->
1272                                                                   vdev->
1273                                                                   unit_address,
1274                                                                   pool->
1275                                                                   buff_size);
1276                                 }
1277                         }
1278                         if (pool->active) {
1279                                 ibmveth_error_printk("no active pool >= MTU\n");
1280                                 return -EPERM;
1281                         }
1282                 }
1283         } else if (attr == &veth_num_attr) {
1284                 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1285                         return -EINVAL;
1286                 else {
1287                         adapter->pool_config = 1;
1288                         ibmveth_close(netdev);
1289                         adapter->pool_config = 0;
1290                         pool->size = value;
1291                         if ((rc = ibmveth_open(netdev)))
1292                                 return rc;
1293                 }
1294         } else if (attr == &veth_size_attr) {
1295                 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1296                         return -EINVAL;
1297                 else {
1298                         adapter->pool_config = 1;
1299                         ibmveth_close(netdev);
1300                         adapter->pool_config = 0;
1301                         pool->buff_size = value;
1302                         if ((rc = ibmveth_open(netdev)))
1303                                 return rc;
1304                 }
1305         }
1306
1307         /* kick the interrupt handler to allocate/deallocate pools */
1308         ibmveth_interrupt(netdev->irq, netdev);
1309         return count;
1310 }
1311
1312
1313 #define ATTR(_name, _mode)      \
1314         struct attribute veth_##_name##_attr = {               \
1315         .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE \
1316         };
1317
1318 static ATTR(active, 0644);
1319 static ATTR(num, 0644);
1320 static ATTR(size, 0644);
1321
1322 static struct attribute * veth_pool_attrs[] = {
1323         &veth_active_attr,
1324         &veth_num_attr,
1325         &veth_size_attr,
1326         NULL,
1327 };
1328
1329 static struct sysfs_ops veth_pool_ops = {
1330         .show   = veth_pool_show,
1331         .store  = veth_pool_store,
1332 };
1333
1334 static struct kobj_type ktype_veth_pool = {
1335         .release        = NULL,
1336         .sysfs_ops      = &veth_pool_ops,
1337         .default_attrs  = veth_pool_attrs,
1338 };
1339
1340
1341 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1342         { "network", "IBM,l-lan"},
1343         { "", "" }
1344 };
1345 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1346
1347 static struct vio_driver ibmveth_driver = {
1348         .id_table       = ibmveth_device_table,
1349         .probe          = ibmveth_probe,
1350         .remove         = ibmveth_remove,
1351         .driver         = {
1352                 .name   = ibmveth_driver_name,
1353                 .owner  = THIS_MODULE,
1354         }
1355 };
1356
1357 static int __init ibmveth_module_init(void)
1358 {
1359         ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1360
1361         ibmveth_proc_register_driver();
1362
1363         return vio_register_driver(&ibmveth_driver);
1364 }
1365
1366 static void __exit ibmveth_module_exit(void)
1367 {
1368         vio_unregister_driver(&ibmveth_driver);
1369         ibmveth_proc_unregister_driver();
1370 }
1371
1372 module_init(ibmveth_module_init);
1373 module_exit(ibmveth_module_exit);