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