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