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