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