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