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