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