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