]> err.no Git - linux-2.6/blob - drivers/net/ibmveth.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jk/spufs
[linux-2.6] / drivers / net / ibmveth.c
1 /**************************************************************************/
2 /*                                                                        */
3 /* IBM eServer i/pSeries Virtual Ethernet Device Driver                   */
4 /* Copyright (C) 2003 IBM Corp.                                           */
5 /*  Originally written by Dave Larson (larson1@us.ibm.com)                */
6 /*  Maintained by Santiago Leon (santil@us.ibm.com)                       */
7 /*                                                                        */
8 /*  This program is free software; you can redistribute it and/or modify  */
9 /*  it under the terms of the GNU General Public License as published by  */
10 /*  the Free Software Foundation; either version 2 of the License, or     */
11 /*  (at your option) any later version.                                   */
12 /*                                                                        */
13 /*  This program is distributed in the hope that it will be useful,       */
14 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
15 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
16 /*  GNU General Public License for more details.                          */
17 /*                                                                        */
18 /*  You should have received a copy of the GNU General Public License     */
19 /*  along with this program; if not, write to the Free Software           */
20 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  */
21 /*                                                                   USA  */
22 /*                                                                        */
23 /* This module contains the implementation of a virtual ethernet device   */
24 /* for use with IBM i/pSeries LPAR Linux.  It utilizes the logical LAN    */
25 /* option of the RS/6000 Platform Architechture to interface with virtual */
26 /* ethernet NICs that are presented to the partition by the hypervisor.   */
27 /*                                                                        */
28 /**************************************************************************/
29 /*
30   TODO:
31   - add support for sysfs
32   - possibly remove procfs support
33 */
34
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/errno.h>
38 #include <linux/ioport.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/kernel.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/init.h>
45 #include <linux/delay.h>
46 #include <linux/mm.h>
47 #include <linux/ethtool.h>
48 #include <linux/proc_fs.h>
49 #include <linux/in.h>
50 #include <linux/ip.h>
51 #include <net/net_namespace.h>
52 #include <asm/hvcall.h>
53 #include <asm/atomic.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 napi_struct *napi, int budget);
88 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
89 static void ibmveth_set_multicast_list(struct net_device *dev);
90 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
91 static void ibmveth_proc_register_driver(void);
92 static void ibmveth_proc_unregister_driver(void);
93 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
94 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
95 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
96 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
97 static struct kobj_type ktype_veth_pool;
98
99 #ifdef CONFIG_PROC_FS
100 #define IBMVETH_PROC_DIR "ibmveth"
101 static struct proc_dir_entry *ibmveth_proc_dir;
102 #endif
103
104 static const char ibmveth_driver_name[] = "ibmveth";
105 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
106 #define ibmveth_driver_version "1.03"
107
108 MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
109 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
110 MODULE_LICENSE("GPL");
111 MODULE_VERSION(ibmveth_driver_version);
112
113 struct ibmveth_stat {
114         char name[ETH_GSTRING_LEN];
115         int offset;
116 };
117
118 #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
119 #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
120
121 struct ibmveth_stat ibmveth_stats[] = {
122         { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
123         { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
124         { "replenish_add_buff_failure", IBMVETH_STAT_OFF(replenish_add_buff_failure) },
125         { "replenish_add_buff_success", IBMVETH_STAT_OFF(replenish_add_buff_success) },
126         { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
127         { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
128         { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
129         { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
130 };
131
132 /* simple methods of getting data from the current rxq entry */
133 static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
134 {
135         return adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off;
136 }
137
138 static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
139 {
140         return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >> IBMVETH_RXQ_TOGGLE_SHIFT;
141 }
142
143 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
144 {
145         return (ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle);
146 }
147
148 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
149 {
150         return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID);
151 }
152
153 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
154 {
155         return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK);
156 }
157
158 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
159 {
160         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
161 }
162
163 static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
164 {
165         return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD);
166 }
167
168 /* setup the initial settings for a buffer pool */
169 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
170 {
171         pool->size = pool_size;
172         pool->index = pool_index;
173         pool->buff_size = buff_size;
174         pool->threshold = pool_size / 2;
175         pool->active = pool_active;
176 }
177
178 /* allocate and setup an buffer pool - called during open */
179 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
180 {
181         int i;
182
183         pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
184
185         if(!pool->free_map) {
186                 return -1;
187         }
188
189         pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
190         if(!pool->dma_addr) {
191                 kfree(pool->free_map);
192                 pool->free_map = NULL;
193                 return -1;
194         }
195
196         pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
197
198         if(!pool->skbuff) {
199                 kfree(pool->dma_addr);
200                 pool->dma_addr = NULL;
201
202                 kfree(pool->free_map);
203                 pool->free_map = NULL;
204                 return -1;
205         }
206
207         memset(pool->skbuff, 0, sizeof(void*) * pool->size);
208         memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
209
210         for(i = 0; i < pool->size; ++i) {
211                 pool->free_map[i] = i;
212         }
213
214         atomic_set(&pool->available, 0);
215         pool->producer_index = 0;
216         pool->consumer_index = 0;
217
218         return 0;
219 }
220
221 /* replenish the buffers for a pool.  note that we don't need to
222  * skb_reserve these since they are used for incoming...
223  */
224 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
225 {
226         u32 i;
227         u32 count = pool->size - atomic_read(&pool->available);
228         u32 buffers_added = 0;
229
230         mb();
231
232         for(i = 0; i < count; ++i) {
233                 struct sk_buff *skb;
234                 unsigned int free_index, index;
235                 u64 correlator;
236                 union ibmveth_buf_desc desc;
237                 unsigned long lpar_rc;
238                 dma_addr_t dma_addr;
239
240                 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
241
242                 if(!skb) {
243                         ibmveth_debug_printk("replenish: unable to allocate skb\n");
244                         adapter->replenish_no_mem++;
245                         break;
246                 }
247
248                 free_index = pool->consumer_index;
249                 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
250                 index = pool->free_map[free_index];
251
252                 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
253                 ibmveth_assert(pool->skbuff[index] == NULL);
254
255                 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
256                                 pool->buff_size, DMA_FROM_DEVICE);
257
258                 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
259                 pool->dma_addr[index] = dma_addr;
260                 pool->skbuff[index] = skb;
261
262                 correlator = ((u64)pool->index << 32) | index;
263                 *(u64*)skb->data = correlator;
264
265                 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
266                 desc.fields.address = dma_addr;
267
268                 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
269
270                 if(lpar_rc != H_SUCCESS) {
271                         pool->free_map[free_index] = index;
272                         pool->skbuff[index] = NULL;
273                         if (pool->consumer_index == 0)
274                                 pool->consumer_index = pool->size - 1;
275                         else
276                                 pool->consumer_index--;
277                         dma_unmap_single(&adapter->vdev->dev,
278                                         pool->dma_addr[index], pool->buff_size,
279                                         DMA_FROM_DEVICE);
280                         dev_kfree_skb_any(skb);
281                         adapter->replenish_add_buff_failure++;
282                         break;
283                 } else {
284                         buffers_added++;
285                         adapter->replenish_add_buff_success++;
286                 }
287         }
288
289         mb();
290         atomic_add(buffers_added, &(pool->available));
291 }
292
293 /* replenish routine */
294 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
295 {
296         int i;
297
298         adapter->replenish_task_cycles++;
299
300         for(i = 0; i < IbmVethNumBufferPools; i++)
301                 if(adapter->rx_buff_pool[i].active)
302                         ibmveth_replenish_buffer_pool(adapter,
303                                                      &adapter->rx_buff_pool[i]);
304
305         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
306 }
307
308 /* empty and free ana buffer pool - also used to do cleanup in error paths */
309 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
310 {
311         int i;
312
313         kfree(pool->free_map);
314         pool->free_map = NULL;
315
316         if(pool->skbuff && pool->dma_addr) {
317                 for(i = 0; i < pool->size; ++i) {
318                         struct sk_buff *skb = pool->skbuff[i];
319                         if(skb) {
320                                 dma_unmap_single(&adapter->vdev->dev,
321                                                  pool->dma_addr[i],
322                                                  pool->buff_size,
323                                                  DMA_FROM_DEVICE);
324                                 dev_kfree_skb_any(skb);
325                                 pool->skbuff[i] = NULL;
326                         }
327                 }
328         }
329
330         if(pool->dma_addr) {
331                 kfree(pool->dma_addr);
332                 pool->dma_addr = NULL;
333         }
334
335         if(pool->skbuff) {
336                 kfree(pool->skbuff);
337                 pool->skbuff = NULL;
338         }
339 }
340
341 /* remove a buffer from a pool */
342 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
343 {
344         unsigned int pool  = correlator >> 32;
345         unsigned int index = correlator & 0xffffffffUL;
346         unsigned int free_index;
347         struct sk_buff *skb;
348
349         ibmveth_assert(pool < IbmVethNumBufferPools);
350         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
351
352         skb = adapter->rx_buff_pool[pool].skbuff[index];
353
354         ibmveth_assert(skb != NULL);
355
356         adapter->rx_buff_pool[pool].skbuff[index] = NULL;
357
358         dma_unmap_single(&adapter->vdev->dev,
359                          adapter->rx_buff_pool[pool].dma_addr[index],
360                          adapter->rx_buff_pool[pool].buff_size,
361                          DMA_FROM_DEVICE);
362
363         free_index = adapter->rx_buff_pool[pool].producer_index;
364         adapter->rx_buff_pool[pool].producer_index
365                 = (adapter->rx_buff_pool[pool].producer_index + 1)
366                 % adapter->rx_buff_pool[pool].size;
367         adapter->rx_buff_pool[pool].free_map[free_index] = index;
368
369         mb();
370
371         atomic_dec(&(adapter->rx_buff_pool[pool].available));
372 }
373
374 /* get the current buffer on the rx queue */
375 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
376 {
377         u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
378         unsigned int pool = correlator >> 32;
379         unsigned int index = correlator & 0xffffffffUL;
380
381         ibmveth_assert(pool < IbmVethNumBufferPools);
382         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
383
384         return adapter->rx_buff_pool[pool].skbuff[index];
385 }
386
387 /* recycle the current buffer on the rx queue */
388 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
389 {
390         u32 q_index = adapter->rx_queue.index;
391         u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
392         unsigned int pool = correlator >> 32;
393         unsigned int index = correlator & 0xffffffffUL;
394         union ibmveth_buf_desc desc;
395         unsigned long lpar_rc;
396
397         ibmveth_assert(pool < IbmVethNumBufferPools);
398         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
399
400         if(!adapter->rx_buff_pool[pool].active) {
401                 ibmveth_rxq_harvest_buffer(adapter);
402                 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
403                 return;
404         }
405
406         desc.fields.flags_len = IBMVETH_BUF_VALID |
407                 adapter->rx_buff_pool[pool].buff_size;
408         desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
409
410         lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
411
412         if(lpar_rc != H_SUCCESS) {
413                 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
414                 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
415         }
416
417         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
418                 adapter->rx_queue.index = 0;
419                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
420         }
421 }
422
423 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
424 {
425         ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
426
427         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
428                 adapter->rx_queue.index = 0;
429                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
430         }
431 }
432
433 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
434 {
435         int i;
436
437         if(adapter->buffer_list_addr != NULL) {
438                 if(!dma_mapping_error(adapter->buffer_list_dma)) {
439                         dma_unmap_single(&adapter->vdev->dev,
440                                         adapter->buffer_list_dma, 4096,
441                                         DMA_BIDIRECTIONAL);
442                         adapter->buffer_list_dma = DMA_ERROR_CODE;
443                 }
444                 free_page((unsigned long)adapter->buffer_list_addr);
445                 adapter->buffer_list_addr = NULL;
446         }
447
448         if(adapter->filter_list_addr != NULL) {
449                 if(!dma_mapping_error(adapter->filter_list_dma)) {
450                         dma_unmap_single(&adapter->vdev->dev,
451                                         adapter->filter_list_dma, 4096,
452                                         DMA_BIDIRECTIONAL);
453                         adapter->filter_list_dma = DMA_ERROR_CODE;
454                 }
455                 free_page((unsigned long)adapter->filter_list_addr);
456                 adapter->filter_list_addr = NULL;
457         }
458
459         if(adapter->rx_queue.queue_addr != NULL) {
460                 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
461                         dma_unmap_single(&adapter->vdev->dev,
462                                         adapter->rx_queue.queue_dma,
463                                         adapter->rx_queue.queue_len,
464                                         DMA_BIDIRECTIONAL);
465                         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
466                 }
467                 kfree(adapter->rx_queue.queue_addr);
468                 adapter->rx_queue.queue_addr = NULL;
469         }
470
471         for(i = 0; i<IbmVethNumBufferPools; i++)
472                 if (adapter->rx_buff_pool[i].active)
473                         ibmveth_free_buffer_pool(adapter,
474                                                  &adapter->rx_buff_pool[i]);
475 }
476
477 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
478         union ibmveth_buf_desc rxq_desc, u64 mac_address)
479 {
480         int rc, try_again = 1;
481
482         /* After a kexec the adapter will still be open, so our attempt to
483         * open it will fail. So if we get a failure we free the adapter and
484         * try again, but only once. */
485 retry:
486         rc = h_register_logical_lan(adapter->vdev->unit_address,
487                                     adapter->buffer_list_dma, rxq_desc.desc,
488                                     adapter->filter_list_dma, mac_address);
489
490         if (rc != H_SUCCESS && try_again) {
491                 do {
492                         rc = h_free_logical_lan(adapter->vdev->unit_address);
493                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
494
495                 try_again = 0;
496                 goto retry;
497         }
498
499         return rc;
500 }
501
502 static int ibmveth_open(struct net_device *netdev)
503 {
504         struct ibmveth_adapter *adapter = netdev->priv;
505         u64 mac_address = 0;
506         int rxq_entries = 1;
507         unsigned long lpar_rc;
508         int rc;
509         union ibmveth_buf_desc rxq_desc;
510         int i;
511
512         ibmveth_debug_printk("open starting\n");
513
514         napi_enable(&adapter->napi);
515
516         for(i = 0; i<IbmVethNumBufferPools; i++)
517                 rxq_entries += adapter->rx_buff_pool[i].size;
518
519         adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
520         adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
521
522         if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
523                 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
524                 ibmveth_cleanup(adapter);
525                 napi_disable(&adapter->napi);
526                 return -ENOMEM;
527         }
528
529         adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
530         adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
531
532         if(!adapter->rx_queue.queue_addr) {
533                 ibmveth_error_printk("unable to allocate rx queue pages\n");
534                 ibmveth_cleanup(adapter);
535                 napi_disable(&adapter->napi);
536                 return -ENOMEM;
537         }
538
539         adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
540                         adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
541         adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
542                         adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
543         adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
544                         adapter->rx_queue.queue_addr,
545                         adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
546
547         if((dma_mapping_error(adapter->buffer_list_dma) ) ||
548            (dma_mapping_error(adapter->filter_list_dma)) ||
549            (dma_mapping_error(adapter->rx_queue.queue_dma))) {
550                 ibmveth_error_printk("unable to map filter or buffer list pages\n");
551                 ibmveth_cleanup(adapter);
552                 napi_disable(&adapter->napi);
553                 return -ENOMEM;
554         }
555
556         adapter->rx_queue.index = 0;
557         adapter->rx_queue.num_slots = rxq_entries;
558         adapter->rx_queue.toggle = 1;
559
560         memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
561         mac_address = mac_address >> 16;
562
563         rxq_desc.fields.flags_len = IBMVETH_BUF_VALID | adapter->rx_queue.queue_len;
564         rxq_desc.fields.address = adapter->rx_queue.queue_dma;
565
566         ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
567         ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
568         ibmveth_debug_printk("receive q   @ 0x%p\n", adapter->rx_queue.queue_addr);
569
570         h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
571
572         lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
573
574         if(lpar_rc != H_SUCCESS) {
575                 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
576                 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
577                                      adapter->buffer_list_dma,
578                                      adapter->filter_list_dma,
579                                      rxq_desc.desc,
580                                      mac_address);
581                 ibmveth_cleanup(adapter);
582                 napi_disable(&adapter->napi);
583                 return -ENONET;
584         }
585
586         for(i = 0; i<IbmVethNumBufferPools; i++) {
587                 if(!adapter->rx_buff_pool[i].active)
588                         continue;
589                 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
590                         ibmveth_error_printk("unable to alloc pool\n");
591                         adapter->rx_buff_pool[i].active = 0;
592                         ibmveth_cleanup(adapter);
593                         napi_disable(&adapter->napi);
594                         return -ENOMEM ;
595                 }
596         }
597
598         ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
599         if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
600                 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
601                 do {
602                         rc = h_free_logical_lan(adapter->vdev->unit_address);
603                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
604
605                 ibmveth_cleanup(adapter);
606                 napi_disable(&adapter->napi);
607                 return rc;
608         }
609
610         ibmveth_debug_printk("initial replenish cycle\n");
611         ibmveth_interrupt(netdev->irq, netdev);
612
613         netif_start_queue(netdev);
614
615         ibmveth_debug_printk("open complete\n");
616
617         return 0;
618 }
619
620 static int ibmveth_close(struct net_device *netdev)
621 {
622         struct ibmveth_adapter *adapter = netdev->priv;
623         long lpar_rc;
624
625         ibmveth_debug_printk("close starting\n");
626
627         napi_disable(&adapter->napi);
628
629         if (!adapter->pool_config)
630                 netif_stop_queue(netdev);
631
632         free_irq(netdev->irq, netdev);
633
634         do {
635                 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
636         } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
637
638         if(lpar_rc != H_SUCCESS)
639         {
640                 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
641                                      lpar_rc);
642         }
643
644         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
645
646         ibmveth_cleanup(adapter);
647
648         ibmveth_debug_printk("close complete\n");
649
650         return 0;
651 }
652
653 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
654         cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
655         cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
656         cmd->speed = SPEED_1000;
657         cmd->duplex = DUPLEX_FULL;
658         cmd->port = PORT_FIBRE;
659         cmd->phy_address = 0;
660         cmd->transceiver = XCVR_INTERNAL;
661         cmd->autoneg = AUTONEG_ENABLE;
662         cmd->maxtxpkt = 0;
663         cmd->maxrxpkt = 1;
664         return 0;
665 }
666
667 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
668         strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
669         strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
670 }
671
672 static u32 netdev_get_link(struct net_device *dev) {
673         return 1;
674 }
675
676 static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
677 {
678         struct ibmveth_adapter *adapter = dev->priv;
679
680         if (data)
681                 adapter->rx_csum = 1;
682         else {
683                 /*
684                  * Since the ibmveth firmware interface does not have the concept of
685                  * separate tx/rx checksum offload enable, if rx checksum is disabled
686                  * we also have to disable tx checksum offload. Once we disable rx
687                  * checksum offload, we are no longer allowed to send tx buffers that
688                  * are not properly checksummed.
689                  */
690                 adapter->rx_csum = 0;
691                 dev->features &= ~NETIF_F_IP_CSUM;
692         }
693 }
694
695 static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
696 {
697         struct ibmveth_adapter *adapter = dev->priv;
698
699         if (data) {
700                 dev->features |= NETIF_F_IP_CSUM;
701                 adapter->rx_csum = 1;
702         } else
703                 dev->features &= ~NETIF_F_IP_CSUM;
704 }
705
706 static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
707                                     void (*done) (struct net_device *, u32))
708 {
709         struct ibmveth_adapter *adapter = dev->priv;
710         u64 set_attr, clr_attr, ret_attr;
711         long ret;
712         int rc1 = 0, rc2 = 0;
713         int restart = 0;
714
715         if (netif_running(dev)) {
716                 restart = 1;
717                 adapter->pool_config = 1;
718                 ibmveth_close(dev);
719                 adapter->pool_config = 0;
720         }
721
722         set_attr = 0;
723         clr_attr = 0;
724
725         if (data)
726                 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
727         else
728                 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
729
730         ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
731
732         if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
733             !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
734             (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
735                 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
736                                          set_attr, &ret_attr);
737
738                 if (ret != H_SUCCESS) {
739                         rc1 = -EIO;
740                         ibmveth_error_printk("unable to change checksum offload settings."
741                                              " %d rc=%ld\n", data, ret);
742
743                         ret = h_illan_attributes(adapter->vdev->unit_address,
744                                                  set_attr, clr_attr, &ret_attr);
745                 } else
746                         done(dev, data);
747         } else {
748                 rc1 = -EIO;
749                 ibmveth_error_printk("unable to change checksum offload settings."
750                                      " %d rc=%ld ret_attr=%lx\n", data, ret, ret_attr);
751         }
752
753         if (restart)
754                 rc2 = ibmveth_open(dev);
755
756         return rc1 ? rc1 : rc2;
757 }
758
759 static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
760 {
761         struct ibmveth_adapter *adapter = dev->priv;
762
763         if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
764                 return 0;
765
766         return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
767 }
768
769 static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
770 {
771         struct ibmveth_adapter *adapter = dev->priv;
772         int rc = 0;
773
774         if (data && (dev->features & NETIF_F_IP_CSUM))
775                 return 0;
776         if (!data && !(dev->features & NETIF_F_IP_CSUM))
777                 return 0;
778
779         if (data && !adapter->rx_csum)
780                 rc = ibmveth_set_csum_offload(dev, data, ibmveth_set_tx_csum_flags);
781         else
782                 ibmveth_set_tx_csum_flags(dev, data);
783
784         return rc;
785 }
786
787 static u32 ibmveth_get_rx_csum(struct net_device *dev)
788 {
789         struct ibmveth_adapter *adapter = dev->priv;
790         return adapter->rx_csum;
791 }
792
793 static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
794 {
795         int i;
796
797         if (stringset != ETH_SS_STATS)
798                 return;
799
800         for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
801                 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
802 }
803
804 static int ibmveth_get_sset_count(struct net_device *dev, int sset)
805 {
806         switch (sset) {
807         case ETH_SS_STATS:
808                 return ARRAY_SIZE(ibmveth_stats);
809         default:
810                 return -EOPNOTSUPP;
811         }
812 }
813
814 static void ibmveth_get_ethtool_stats(struct net_device *dev,
815                                       struct ethtool_stats *stats, u64 *data)
816 {
817         int i;
818         struct ibmveth_adapter *adapter = dev->priv;
819
820         for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
821                 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
822 }
823
824 static const struct ethtool_ops netdev_ethtool_ops = {
825         .get_drvinfo            = netdev_get_drvinfo,
826         .get_settings           = netdev_get_settings,
827         .get_link               = netdev_get_link,
828         .set_tx_csum            = ibmveth_set_tx_csum,
829         .get_rx_csum            = ibmveth_get_rx_csum,
830         .set_rx_csum            = ibmveth_set_rx_csum,
831         .get_strings            = ibmveth_get_strings,
832         .get_sset_count         = ibmveth_get_sset_count,
833         .get_ethtool_stats      = ibmveth_get_ethtool_stats,
834 };
835
836 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
837 {
838         return -EOPNOTSUPP;
839 }
840
841 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
842
843 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
844 {
845         struct ibmveth_adapter *adapter = netdev->priv;
846         union ibmveth_buf_desc desc;
847         unsigned long lpar_rc;
848         unsigned long correlator;
849         unsigned long flags;
850         unsigned int retry_count;
851         unsigned int tx_dropped = 0;
852         unsigned int tx_bytes = 0;
853         unsigned int tx_packets = 0;
854         unsigned int tx_send_failed = 0;
855         unsigned int tx_map_failed = 0;
856
857         desc.fields.flags_len = IBMVETH_BUF_VALID | skb->len;
858         desc.fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
859                                              skb->len, DMA_TO_DEVICE);
860
861         if (skb->ip_summed == CHECKSUM_PARTIAL &&
862             ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
863                 ibmveth_error_printk("tx: failed to checksum packet\n");
864                 tx_dropped++;
865                 goto out;
866         }
867
868         if (skb->ip_summed == CHECKSUM_PARTIAL) {
869                 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
870
871                 desc.fields.flags_len |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
872
873                 /* Need to zero out the checksum */
874                 buf[0] = 0;
875                 buf[1] = 0;
876         }
877
878         if (dma_mapping_error(desc.fields.address)) {
879                 ibmveth_error_printk("tx: unable to map xmit buffer\n");
880                 tx_map_failed++;
881                 tx_dropped++;
882                 goto out;
883         }
884
885         /* send the frame. Arbitrarily set retrycount to 1024 */
886         correlator = 0;
887         retry_count = 1024;
888         do {
889                 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
890                                              desc.desc, 0, 0, 0, 0, 0,
891                                              correlator, &correlator);
892         } while ((lpar_rc == H_BUSY) && (retry_count--));
893
894         if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
895                 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
896                 ibmveth_error_printk("tx: valid=%d, len=%d, address=0x%08x\n",
897                                      (desc.fields.flags_len & IBMVETH_BUF_VALID) ? 1 : 0,
898                                      skb->len, desc.fields.address);
899                 tx_send_failed++;
900                 tx_dropped++;
901         } else {
902                 tx_packets++;
903                 tx_bytes += skb->len;
904                 netdev->trans_start = jiffies;
905         }
906
907         dma_unmap_single(&adapter->vdev->dev, desc.fields.address,
908                          skb->len, DMA_TO_DEVICE);
909
910 out:    spin_lock_irqsave(&adapter->stats_lock, flags);
911         netdev->stats.tx_dropped += tx_dropped;
912         netdev->stats.tx_bytes += tx_bytes;
913         netdev->stats.tx_packets += tx_packets;
914         adapter->tx_send_failed += tx_send_failed;
915         adapter->tx_map_failed += tx_map_failed;
916         spin_unlock_irqrestore(&adapter->stats_lock, flags);
917
918         dev_kfree_skb(skb);
919         return 0;
920 }
921
922 static int ibmveth_poll(struct napi_struct *napi, int budget)
923 {
924         struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
925         struct net_device *netdev = adapter->netdev;
926         int frames_processed = 0;
927         unsigned long lpar_rc;
928
929  restart_poll:
930         do {
931                 struct sk_buff *skb;
932
933                 if (!ibmveth_rxq_pending_buffer(adapter))
934                         break;
935
936                 rmb();
937                 if (!ibmveth_rxq_buffer_valid(adapter)) {
938                         wmb(); /* suggested by larson1 */
939                         adapter->rx_invalid_buffer++;
940                         ibmveth_debug_printk("recycling invalid buffer\n");
941                         ibmveth_rxq_recycle_buffer(adapter);
942                 } else {
943                         int length = ibmveth_rxq_frame_length(adapter);
944                         int offset = ibmveth_rxq_frame_offset(adapter);
945                         int csum_good = ibmveth_rxq_csum_good(adapter);
946
947                         skb = ibmveth_rxq_get_buffer(adapter);
948                         if (csum_good)
949                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
950
951                         ibmveth_rxq_harvest_buffer(adapter);
952
953                         skb_reserve(skb, offset);
954                         skb_put(skb, length);
955                         skb->protocol = eth_type_trans(skb, netdev);
956
957                         netif_receive_skb(skb); /* send it up */
958
959                         netdev->stats.rx_packets++;
960                         netdev->stats.rx_bytes += length;
961                         frames_processed++;
962                         netdev->last_rx = jiffies;
963                 }
964         } while (frames_processed < budget);
965
966         ibmveth_replenish_task(adapter);
967
968         if (frames_processed < budget) {
969                 /* We think we are done - reenable interrupts,
970                  * then check once more to make sure we are done.
971                  */
972                 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
973                                        VIO_IRQ_ENABLE);
974
975                 ibmveth_assert(lpar_rc == H_SUCCESS);
976
977                 netif_rx_complete(netdev, napi);
978
979                 if (ibmveth_rxq_pending_buffer(adapter) &&
980                     netif_rx_reschedule(netdev, napi)) {
981                         lpar_rc = h_vio_signal(adapter->vdev->unit_address,
982                                                VIO_IRQ_DISABLE);
983                         goto restart_poll;
984                 }
985         }
986
987         return frames_processed;
988 }
989
990 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
991 {
992         struct net_device *netdev = dev_instance;
993         struct ibmveth_adapter *adapter = netdev->priv;
994         unsigned long lpar_rc;
995
996         if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
997                 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
998                                        VIO_IRQ_DISABLE);
999                 ibmveth_assert(lpar_rc == H_SUCCESS);
1000                 __netif_rx_schedule(netdev, &adapter->napi);
1001         }
1002         return IRQ_HANDLED;
1003 }
1004
1005 static void ibmveth_set_multicast_list(struct net_device *netdev)
1006 {
1007         struct ibmveth_adapter *adapter = netdev->priv;
1008         unsigned long lpar_rc;
1009
1010         if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
1011                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1012                                            IbmVethMcastEnableRecv |
1013                                            IbmVethMcastDisableFiltering,
1014                                            0);
1015                 if(lpar_rc != H_SUCCESS) {
1016                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
1017                 }
1018         } else {
1019                 struct dev_mc_list *mclist = netdev->mc_list;
1020                 int i;
1021                 /* clear the filter table & disable filtering */
1022                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1023                                            IbmVethMcastEnableRecv |
1024                                            IbmVethMcastDisableFiltering |
1025                                            IbmVethMcastClearFilterTable,
1026                                            0);
1027                 if(lpar_rc != H_SUCCESS) {
1028                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
1029                 }
1030                 /* add the addresses to the filter table */
1031                 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
1032                         // add the multicast address to the filter table
1033                         unsigned long mcast_addr = 0;
1034                         memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
1035                         lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1036                                                    IbmVethMcastAddFilter,
1037                                                    mcast_addr);
1038                         if(lpar_rc != H_SUCCESS) {
1039                                 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
1040                         }
1041                 }
1042
1043                 /* re-enable filtering */
1044                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1045                                            IbmVethMcastEnableFiltering,
1046                                            0);
1047                 if(lpar_rc != H_SUCCESS) {
1048                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
1049                 }
1050         }
1051 }
1052
1053 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1054 {
1055         struct ibmveth_adapter *adapter = dev->priv;
1056         int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
1057         int reinit = 0;
1058         int i, rc;
1059
1060         if (new_mtu < IBMVETH_MAX_MTU)
1061                 return -EINVAL;
1062
1063         for (i = 0; i < IbmVethNumBufferPools; i++)
1064                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1065                         break;
1066
1067         if (i == IbmVethNumBufferPools)
1068                 return -EINVAL;
1069
1070         /* Look for an active buffer pool that can hold the new MTU */
1071         for(i = 0; i<IbmVethNumBufferPools; i++) {
1072                 if (!adapter->rx_buff_pool[i].active) {
1073                         adapter->rx_buff_pool[i].active = 1;
1074                         reinit = 1;
1075                 }
1076
1077                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
1078                         if (reinit && netif_running(adapter->netdev)) {
1079                                 adapter->pool_config = 1;
1080                                 ibmveth_close(adapter->netdev);
1081                                 adapter->pool_config = 0;
1082                                 dev->mtu = new_mtu;
1083                                 if ((rc = ibmveth_open(adapter->netdev)))
1084                                         return rc;
1085                         } else
1086                                 dev->mtu = new_mtu;
1087                         return 0;
1088                 }
1089         }
1090         return -EINVAL;
1091 }
1092
1093 #ifdef CONFIG_NET_POLL_CONTROLLER
1094 static void ibmveth_poll_controller(struct net_device *dev)
1095 {
1096         ibmveth_replenish_task(dev->priv);
1097         ibmveth_interrupt(dev->irq, dev);
1098 }
1099 #endif
1100
1101 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1102 {
1103         int rc, i;
1104         long ret;
1105         struct net_device *netdev;
1106         struct ibmveth_adapter *adapter;
1107         u64 set_attr, ret_attr;
1108
1109         unsigned char *mac_addr_p;
1110         unsigned int *mcastFilterSize_p;
1111
1112
1113         ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
1114                                         dev->unit_address);
1115
1116         mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1117                                                 VETH_MAC_ADDR, NULL);
1118         if(!mac_addr_p) {
1119                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1120                                 "attribute\n", __FILE__, __LINE__);
1121                 return 0;
1122         }
1123
1124         mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1125                                                 VETH_MCAST_FILTER_SIZE, NULL);
1126         if(!mcastFilterSize_p) {
1127                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1128                                 "VETH_MCAST_FILTER_SIZE attribute\n",
1129                                 __FILE__, __LINE__);
1130                 return 0;
1131         }
1132
1133         netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1134
1135         if(!netdev)
1136                 return -ENOMEM;
1137
1138         adapter = netdev->priv;
1139         dev->dev.driver_data = netdev;
1140
1141         adapter->vdev = dev;
1142         adapter->netdev = netdev;
1143         adapter->mcastFilterSize= *mcastFilterSize_p;
1144         adapter->pool_config = 0;
1145
1146         netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1147
1148         /*      Some older boxes running PHYP non-natively have an OF that
1149                 returns a 8-byte local-mac-address field (and the first
1150                 2 bytes have to be ignored) while newer boxes' OF return
1151                 a 6-byte field. Note that IEEE 1275 specifies that
1152                 local-mac-address must be a 6-byte field.
1153                 The RPA doc specifies that the first byte must be 10b, so
1154                 we'll just look for it to solve this 8 vs. 6 byte field issue */
1155
1156         if ((*mac_addr_p & 0x3) != 0x02)
1157                 mac_addr_p += 2;
1158
1159         adapter->mac_addr = 0;
1160         memcpy(&adapter->mac_addr, mac_addr_p, 6);
1161
1162         netdev->irq = dev->irq;
1163         netdev->open               = ibmveth_open;
1164         netdev->stop               = ibmveth_close;
1165         netdev->hard_start_xmit    = ibmveth_start_xmit;
1166         netdev->set_multicast_list = ibmveth_set_multicast_list;
1167         netdev->do_ioctl           = ibmveth_ioctl;
1168         netdev->ethtool_ops           = &netdev_ethtool_ops;
1169         netdev->change_mtu         = ibmveth_change_mtu;
1170         SET_NETDEV_DEV(netdev, &dev->dev);
1171 #ifdef CONFIG_NET_POLL_CONTROLLER
1172         netdev->poll_controller = ibmveth_poll_controller;
1173 #endif
1174         netdev->features |= NETIF_F_LLTX;
1175         spin_lock_init(&adapter->stats_lock);
1176
1177         memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1178
1179         for(i = 0; i<IbmVethNumBufferPools; i++) {
1180                 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1181                 int error;
1182
1183                 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1184                                          pool_count[i], pool_size[i],
1185                                          pool_active[i]);
1186                 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1187                                              &dev->dev.kobj, "pool%d", i);
1188                 if (!error)
1189                         kobject_uevent(kobj, KOBJ_ADD);
1190         }
1191
1192         ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1193
1194         adapter->buffer_list_dma = DMA_ERROR_CODE;
1195         adapter->filter_list_dma = DMA_ERROR_CODE;
1196         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1197
1198         ibmveth_debug_printk("registering netdev...\n");
1199
1200         ret = h_illan_attributes(dev->unit_address, 0, 0, &ret_attr);
1201
1202         if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
1203             !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
1204             (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
1205                 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
1206
1207                 ret = h_illan_attributes(dev->unit_address, 0, set_attr, &ret_attr);
1208
1209                 if (ret == H_SUCCESS) {
1210                         adapter->rx_csum = 1;
1211                         netdev->features |= NETIF_F_IP_CSUM;
1212                 } else
1213                         ret = h_illan_attributes(dev->unit_address, set_attr, 0, &ret_attr);
1214         }
1215
1216         rc = register_netdev(netdev);
1217
1218         if(rc) {
1219                 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1220                 free_netdev(netdev);
1221                 return rc;
1222         }
1223
1224         ibmveth_debug_printk("registered\n");
1225
1226         ibmveth_proc_register_adapter(adapter);
1227
1228         return 0;
1229 }
1230
1231 static int __devexit ibmveth_remove(struct vio_dev *dev)
1232 {
1233         struct net_device *netdev = dev->dev.driver_data;
1234         struct ibmveth_adapter *adapter = netdev->priv;
1235         int i;
1236
1237         for(i = 0; i<IbmVethNumBufferPools; i++)
1238                 kobject_put(&adapter->rx_buff_pool[i].kobj);
1239
1240         unregister_netdev(netdev);
1241
1242         ibmveth_proc_unregister_adapter(adapter);
1243
1244         free_netdev(netdev);
1245         return 0;
1246 }
1247
1248 #ifdef CONFIG_PROC_FS
1249 static void ibmveth_proc_register_driver(void)
1250 {
1251         ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, init_net.proc_net);
1252         if (ibmveth_proc_dir) {
1253         }
1254 }
1255
1256 static void ibmveth_proc_unregister_driver(void)
1257 {
1258         remove_proc_entry(IBMVETH_PROC_DIR, init_net.proc_net);
1259 }
1260
1261 static int ibmveth_show(struct seq_file *seq, void *v)
1262 {
1263         struct ibmveth_adapter *adapter = seq->private;
1264         char *current_mac = ((char*) &adapter->netdev->dev_addr);
1265         char *firmware_mac = ((char*) &adapter->mac_addr) ;
1266         DECLARE_MAC_BUF(mac);
1267
1268         seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1269
1270         seq_printf(seq, "Unit Address:    0x%x\n", adapter->vdev->unit_address);
1271         seq_printf(seq, "Current MAC:     %s\n", print_mac(mac, current_mac));
1272         seq_printf(seq, "Firmware MAC:    %s\n", print_mac(mac, firmware_mac));
1273
1274         seq_printf(seq, "\nAdapter Statistics:\n");
1275         seq_printf(seq, "  TX:  vio_map_single failres:      %ld\n", adapter->tx_map_failed);
1276         seq_printf(seq, "       send failures:               %ld\n", adapter->tx_send_failed);
1277         seq_printf(seq, "  RX:  replenish task cycles:       %ld\n", adapter->replenish_task_cycles);
1278         seq_printf(seq, "       alloc_skb_failures:          %ld\n", adapter->replenish_no_mem);
1279         seq_printf(seq, "       add buffer failures:         %ld\n", adapter->replenish_add_buff_failure);
1280         seq_printf(seq, "       invalid buffers:             %ld\n", adapter->rx_invalid_buffer);
1281         seq_printf(seq, "       no buffers:                  %ld\n", adapter->rx_no_buffer);
1282
1283         return 0;
1284 }
1285
1286 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1287 {
1288         return single_open(file, ibmveth_show, PDE(inode)->data);
1289 }
1290
1291 static const struct file_operations ibmveth_proc_fops = {
1292         .owner   = THIS_MODULE,
1293         .open    = ibmveth_proc_open,
1294         .read    = seq_read,
1295         .llseek  = seq_lseek,
1296         .release = single_release,
1297 };
1298
1299 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1300 {
1301         struct proc_dir_entry *entry;
1302         if (ibmveth_proc_dir) {
1303                 char u_addr[10];
1304                 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1305                 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
1306                 if (!entry) {
1307                         ibmveth_error_printk("Cannot create adapter proc entry");
1308                 } else {
1309                         entry->data = (void *) adapter;
1310                         entry->proc_fops = &ibmveth_proc_fops;
1311                 }
1312         }
1313         return;
1314 }
1315
1316 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1317 {
1318         if (ibmveth_proc_dir) {
1319                 char u_addr[10];
1320                 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1321                 remove_proc_entry(u_addr, ibmveth_proc_dir);
1322         }
1323 }
1324
1325 #else /* CONFIG_PROC_FS */
1326 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1327 {
1328 }
1329
1330 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1331 {
1332 }
1333 static void ibmveth_proc_register_driver(void)
1334 {
1335 }
1336
1337 static void ibmveth_proc_unregister_driver(void)
1338 {
1339 }
1340 #endif /* CONFIG_PROC_FS */
1341
1342 static struct attribute veth_active_attr;
1343 static struct attribute veth_num_attr;
1344 static struct attribute veth_size_attr;
1345
1346 static ssize_t veth_pool_show(struct kobject * kobj,
1347                               struct attribute * attr, char * buf)
1348 {
1349         struct ibmveth_buff_pool *pool = container_of(kobj,
1350                                                       struct ibmveth_buff_pool,
1351                                                       kobj);
1352
1353         if (attr == &veth_active_attr)
1354                 return sprintf(buf, "%d\n", pool->active);
1355         else if (attr == &veth_num_attr)
1356                 return sprintf(buf, "%d\n", pool->size);
1357         else if (attr == &veth_size_attr)
1358                 return sprintf(buf, "%d\n", pool->buff_size);
1359         return 0;
1360 }
1361
1362 static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1363 const char * buf, size_t count)
1364 {
1365         struct ibmveth_buff_pool *pool = container_of(kobj,
1366                                                       struct ibmveth_buff_pool,
1367                                                       kobj);
1368         struct net_device *netdev =
1369             container_of(kobj->parent, struct device, kobj)->driver_data;
1370         struct ibmveth_adapter *adapter = netdev->priv;
1371         long value = simple_strtol(buf, NULL, 10);
1372         long rc;
1373
1374         if (attr == &veth_active_attr) {
1375                 if (value && !pool->active) {
1376                         if (netif_running(netdev)) {
1377                                 if(ibmveth_alloc_buffer_pool(pool)) {
1378                                         ibmveth_error_printk("unable to alloc pool\n");
1379                                         return -ENOMEM;
1380                                 }
1381                                 pool->active = 1;
1382                                 adapter->pool_config = 1;
1383                                 ibmveth_close(netdev);
1384                                 adapter->pool_config = 0;
1385                                 if ((rc = ibmveth_open(netdev)))
1386                                         return rc;
1387                         } else
1388                                 pool->active = 1;
1389                 } else if (!value && pool->active) {
1390                         int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1391                         int i;
1392                         /* Make sure there is a buffer pool with buffers that
1393                            can hold a packet of the size of the MTU */
1394                         for (i = 0; i < IbmVethNumBufferPools; i++) {
1395                                 if (pool == &adapter->rx_buff_pool[i])
1396                                         continue;
1397                                 if (!adapter->rx_buff_pool[i].active)
1398                                         continue;
1399                                 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1400                                         break;
1401                         }
1402
1403                         if (i == IbmVethNumBufferPools) {
1404                                 ibmveth_error_printk("no active pool >= MTU\n");
1405                                 return -EPERM;
1406                         }
1407
1408                         pool->active = 0;
1409                         if (netif_running(netdev)) {
1410                                 adapter->pool_config = 1;
1411                                 ibmveth_close(netdev);
1412                                 adapter->pool_config = 0;
1413                                 if ((rc = ibmveth_open(netdev)))
1414                                         return rc;
1415                         }
1416                 }
1417         } else if (attr == &veth_num_attr) {
1418                 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1419                         return -EINVAL;
1420                 else {
1421                         if (netif_running(netdev)) {
1422                                 adapter->pool_config = 1;
1423                                 ibmveth_close(netdev);
1424                                 adapter->pool_config = 0;
1425                                 pool->size = value;
1426                                 if ((rc = ibmveth_open(netdev)))
1427                                         return rc;
1428                         } else
1429                                 pool->size = value;
1430                 }
1431         } else if (attr == &veth_size_attr) {
1432                 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1433                         return -EINVAL;
1434                 else {
1435                         if (netif_running(netdev)) {
1436                                 adapter->pool_config = 1;
1437                                 ibmveth_close(netdev);
1438                                 adapter->pool_config = 0;
1439                                 pool->buff_size = value;
1440                                 if ((rc = ibmveth_open(netdev)))
1441                                         return rc;
1442                         } else
1443                                 pool->buff_size = value;
1444                 }
1445         }
1446
1447         /* kick the interrupt handler to allocate/deallocate pools */
1448         ibmveth_interrupt(netdev->irq, netdev);
1449         return count;
1450 }
1451
1452
1453 #define ATTR(_name, _mode)      \
1454         struct attribute veth_##_name##_attr = {               \
1455         .name = __stringify(_name), .mode = _mode, \
1456         };
1457
1458 static ATTR(active, 0644);
1459 static ATTR(num, 0644);
1460 static ATTR(size, 0644);
1461
1462 static struct attribute * veth_pool_attrs[] = {
1463         &veth_active_attr,
1464         &veth_num_attr,
1465         &veth_size_attr,
1466         NULL,
1467 };
1468
1469 static struct sysfs_ops veth_pool_ops = {
1470         .show   = veth_pool_show,
1471         .store  = veth_pool_store,
1472 };
1473
1474 static struct kobj_type ktype_veth_pool = {
1475         .release        = NULL,
1476         .sysfs_ops      = &veth_pool_ops,
1477         .default_attrs  = veth_pool_attrs,
1478 };
1479
1480
1481 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1482         { "network", "IBM,l-lan"},
1483         { "", "" }
1484 };
1485 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1486
1487 static struct vio_driver ibmveth_driver = {
1488         .id_table       = ibmveth_device_table,
1489         .probe          = ibmveth_probe,
1490         .remove         = ibmveth_remove,
1491         .driver         = {
1492                 .name   = ibmveth_driver_name,
1493                 .owner  = THIS_MODULE,
1494         }
1495 };
1496
1497 static int __init ibmveth_module_init(void)
1498 {
1499         ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1500
1501         ibmveth_proc_register_driver();
1502
1503         return vio_register_driver(&ibmveth_driver);
1504 }
1505
1506 static void __exit ibmveth_module_exit(void)
1507 {
1508         vio_unregister_driver(&ibmveth_driver);
1509         ibmveth_proc_unregister_driver();
1510 }
1511
1512 module_init(ibmveth_module_init);
1513 module_exit(ibmveth_module_exit);