ibmveth: Add suspend/resume support
[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 = kmalloc(sizeof(void*) * pool->size, 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->skbuff, 0, sizeof(void*) * pool->size);
215         memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
216
217         for(i = 0; i < pool->size; ++i) {
218                 pool->free_map[i] = i;
219         }
220
221         atomic_set(&pool->available, 0);
222         pool->producer_index = 0;
223         pool->consumer_index = 0;
224
225         return 0;
226 }
227
228 /* replenish the buffers for a pool.  note that we don't need to
229  * skb_reserve these since they are used for incoming...
230  */
231 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
232 {
233         u32 i;
234         u32 count = pool->size - atomic_read(&pool->available);
235         u32 buffers_added = 0;
236         struct sk_buff *skb;
237         unsigned int free_index, index;
238         u64 correlator;
239         unsigned long lpar_rc;
240         dma_addr_t dma_addr;
241
242         mb();
243
244         for(i = 0; i < count; ++i) {
245                 union ibmveth_buf_desc desc;
246
247                 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
248
249                 if(!skb) {
250                         ibmveth_debug_printk("replenish: unable to allocate skb\n");
251                         adapter->replenish_no_mem++;
252                         break;
253                 }
254
255                 free_index = pool->consumer_index;
256                 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
257                 index = pool->free_map[free_index];
258
259                 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
260                 ibmveth_assert(pool->skbuff[index] == NULL);
261
262                 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
263                                 pool->buff_size, DMA_FROM_DEVICE);
264
265                 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
266                         goto failure;
267
268                 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
269                 pool->dma_addr[index] = dma_addr;
270                 pool->skbuff[index] = skb;
271
272                 correlator = ((u64)pool->index << 32) | index;
273                 *(u64*)skb->data = correlator;
274
275                 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
276                 desc.fields.address = dma_addr;
277
278                 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
279
280                 if (lpar_rc != H_SUCCESS)
281                         goto failure;
282                 else {
283                         buffers_added++;
284                         adapter->replenish_add_buff_success++;
285                 }
286         }
287
288         mb();
289         atomic_add(buffers_added, &(pool->available));
290         return;
291
292 failure:
293         pool->free_map[free_index] = index;
294         pool->skbuff[index] = NULL;
295         if (pool->consumer_index == 0)
296                 pool->consumer_index = pool->size - 1;
297         else
298                 pool->consumer_index--;
299         if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
300                 dma_unmap_single(&adapter->vdev->dev,
301                                  pool->dma_addr[index], pool->buff_size,
302                                  DMA_FROM_DEVICE);
303         dev_kfree_skb_any(skb);
304         adapter->replenish_add_buff_failure++;
305
306         mb();
307         atomic_add(buffers_added, &(pool->available));
308 }
309
310 /* replenish routine */
311 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
312 {
313         int i;
314
315         adapter->replenish_task_cycles++;
316
317         for (i = (IbmVethNumBufferPools - 1); i >= 0; i--)
318                 if(adapter->rx_buff_pool[i].active)
319                         ibmveth_replenish_buffer_pool(adapter,
320                                                      &adapter->rx_buff_pool[i]);
321
322         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
323 }
324
325 /* empty and free ana buffer pool - also used to do cleanup in error paths */
326 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
327 {
328         int i;
329
330         kfree(pool->free_map);
331         pool->free_map = NULL;
332
333         if(pool->skbuff && pool->dma_addr) {
334                 for(i = 0; i < pool->size; ++i) {
335                         struct sk_buff *skb = pool->skbuff[i];
336                         if(skb) {
337                                 dma_unmap_single(&adapter->vdev->dev,
338                                                  pool->dma_addr[i],
339                                                  pool->buff_size,
340                                                  DMA_FROM_DEVICE);
341                                 dev_kfree_skb_any(skb);
342                                 pool->skbuff[i] = NULL;
343                         }
344                 }
345         }
346
347         if(pool->dma_addr) {
348                 kfree(pool->dma_addr);
349                 pool->dma_addr = NULL;
350         }
351
352         if(pool->skbuff) {
353                 kfree(pool->skbuff);
354                 pool->skbuff = NULL;
355         }
356 }
357
358 /* remove a buffer from a pool */
359 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
360 {
361         unsigned int pool  = correlator >> 32;
362         unsigned int index = correlator & 0xffffffffUL;
363         unsigned int free_index;
364         struct sk_buff *skb;
365
366         ibmveth_assert(pool < IbmVethNumBufferPools);
367         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
368
369         skb = adapter->rx_buff_pool[pool].skbuff[index];
370
371         ibmveth_assert(skb != NULL);
372
373         adapter->rx_buff_pool[pool].skbuff[index] = NULL;
374
375         dma_unmap_single(&adapter->vdev->dev,
376                          adapter->rx_buff_pool[pool].dma_addr[index],
377                          adapter->rx_buff_pool[pool].buff_size,
378                          DMA_FROM_DEVICE);
379
380         free_index = adapter->rx_buff_pool[pool].producer_index;
381         adapter->rx_buff_pool[pool].producer_index
382                 = (adapter->rx_buff_pool[pool].producer_index + 1)
383                 % adapter->rx_buff_pool[pool].size;
384         adapter->rx_buff_pool[pool].free_map[free_index] = index;
385
386         mb();
387
388         atomic_dec(&(adapter->rx_buff_pool[pool].available));
389 }
390
391 /* get the current buffer on the rx queue */
392 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
393 {
394         u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
395         unsigned int pool = correlator >> 32;
396         unsigned int index = correlator & 0xffffffffUL;
397
398         ibmveth_assert(pool < IbmVethNumBufferPools);
399         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
400
401         return adapter->rx_buff_pool[pool].skbuff[index];
402 }
403
404 /* recycle the current buffer on the rx queue */
405 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
406 {
407         u32 q_index = adapter->rx_queue.index;
408         u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
409         unsigned int pool = correlator >> 32;
410         unsigned int index = correlator & 0xffffffffUL;
411         union ibmveth_buf_desc desc;
412         unsigned long lpar_rc;
413
414         ibmveth_assert(pool < IbmVethNumBufferPools);
415         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
416
417         if(!adapter->rx_buff_pool[pool].active) {
418                 ibmveth_rxq_harvest_buffer(adapter);
419                 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
420                 return;
421         }
422
423         desc.fields.flags_len = IBMVETH_BUF_VALID |
424                 adapter->rx_buff_pool[pool].buff_size;
425         desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
426
427         lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
428
429         if(lpar_rc != H_SUCCESS) {
430                 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
431                 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
432         }
433
434         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
435                 adapter->rx_queue.index = 0;
436                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
437         }
438 }
439
440 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
441 {
442         ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
443
444         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
445                 adapter->rx_queue.index = 0;
446                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
447         }
448 }
449
450 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
451 {
452         int i;
453         struct device *dev = &adapter->vdev->dev;
454
455         if(adapter->buffer_list_addr != NULL) {
456                 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
457                         dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
458                                         DMA_BIDIRECTIONAL);
459                         adapter->buffer_list_dma = DMA_ERROR_CODE;
460                 }
461                 free_page((unsigned long)adapter->buffer_list_addr);
462                 adapter->buffer_list_addr = NULL;
463         }
464
465         if(adapter->filter_list_addr != NULL) {
466                 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
467                         dma_unmap_single(dev, adapter->filter_list_dma, 4096,
468                                         DMA_BIDIRECTIONAL);
469                         adapter->filter_list_dma = DMA_ERROR_CODE;
470                 }
471                 free_page((unsigned long)adapter->filter_list_addr);
472                 adapter->filter_list_addr = NULL;
473         }
474
475         if(adapter->rx_queue.queue_addr != NULL) {
476                 if (!dma_mapping_error(dev, adapter->rx_queue.queue_dma)) {
477                         dma_unmap_single(dev,
478                                         adapter->rx_queue.queue_dma,
479                                         adapter->rx_queue.queue_len,
480                                         DMA_BIDIRECTIONAL);
481                         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
482                 }
483                 kfree(adapter->rx_queue.queue_addr);
484                 adapter->rx_queue.queue_addr = NULL;
485         }
486
487         for(i = 0; i<IbmVethNumBufferPools; i++)
488                 if (adapter->rx_buff_pool[i].active)
489                         ibmveth_free_buffer_pool(adapter,
490                                                  &adapter->rx_buff_pool[i]);
491
492         if (adapter->bounce_buffer != NULL) {
493                 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
494                         dma_unmap_single(&adapter->vdev->dev,
495                                         adapter->bounce_buffer_dma,
496                                         adapter->netdev->mtu + IBMVETH_BUFF_OH,
497                                         DMA_BIDIRECTIONAL);
498                         adapter->bounce_buffer_dma = DMA_ERROR_CODE;
499                 }
500                 kfree(adapter->bounce_buffer);
501                 adapter->bounce_buffer = NULL;
502         }
503 }
504
505 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
506         union ibmveth_buf_desc rxq_desc, u64 mac_address)
507 {
508         int rc, try_again = 1;
509
510         /* After a kexec the adapter will still be open, so our attempt to
511         * open it will fail. So if we get a failure we free the adapter and
512         * try again, but only once. */
513 retry:
514         rc = h_register_logical_lan(adapter->vdev->unit_address,
515                                     adapter->buffer_list_dma, rxq_desc.desc,
516                                     adapter->filter_list_dma, mac_address);
517
518         if (rc != H_SUCCESS && try_again) {
519                 do {
520                         rc = h_free_logical_lan(adapter->vdev->unit_address);
521                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
522
523                 try_again = 0;
524                 goto retry;
525         }
526
527         return rc;
528 }
529
530 static int ibmveth_open(struct net_device *netdev)
531 {
532         struct ibmveth_adapter *adapter = netdev_priv(netdev);
533         u64 mac_address = 0;
534         int rxq_entries = 1;
535         unsigned long lpar_rc;
536         int rc;
537         union ibmveth_buf_desc rxq_desc;
538         int i;
539         struct device *dev;
540
541         ibmveth_debug_printk("open starting\n");
542
543         napi_enable(&adapter->napi);
544
545         for(i = 0; i<IbmVethNumBufferPools; i++)
546                 rxq_entries += adapter->rx_buff_pool[i].size;
547
548         adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
549         adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
550
551         if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
552                 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
553                 ibmveth_cleanup(adapter);
554                 napi_disable(&adapter->napi);
555                 return -ENOMEM;
556         }
557
558         adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
559         adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
560
561         if(!adapter->rx_queue.queue_addr) {
562                 ibmveth_error_printk("unable to allocate rx queue pages\n");
563                 ibmveth_cleanup(adapter);
564                 napi_disable(&adapter->napi);
565                 return -ENOMEM;
566         }
567
568         dev = &adapter->vdev->dev;
569
570         adapter->buffer_list_dma = dma_map_single(dev,
571                         adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
572         adapter->filter_list_dma = dma_map_single(dev,
573                         adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
574         adapter->rx_queue.queue_dma = dma_map_single(dev,
575                         adapter->rx_queue.queue_addr,
576                         adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
577
578         if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
579             (dma_mapping_error(dev, adapter->filter_list_dma)) ||
580             (dma_mapping_error(dev, adapter->rx_queue.queue_dma))) {
581                 ibmveth_error_printk("unable to map filter or buffer list pages\n");
582                 ibmveth_cleanup(adapter);
583                 napi_disable(&adapter->napi);
584                 return -ENOMEM;
585         }
586
587         adapter->rx_queue.index = 0;
588         adapter->rx_queue.num_slots = rxq_entries;
589         adapter->rx_queue.toggle = 1;
590
591         memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
592         mac_address = mac_address >> 16;
593
594         rxq_desc.fields.flags_len = IBMVETH_BUF_VALID | adapter->rx_queue.queue_len;
595         rxq_desc.fields.address = adapter->rx_queue.queue_dma;
596
597         ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
598         ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
599         ibmveth_debug_printk("receive q   @ 0x%p\n", adapter->rx_queue.queue_addr);
600
601         h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
602
603         lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
604
605         if(lpar_rc != H_SUCCESS) {
606                 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
607                 ibmveth_error_printk("buffer TCE:0x%llx filter TCE:0x%llx rxq desc:0x%llx MAC:0x%llx\n",
608                                      adapter->buffer_list_dma,
609                                      adapter->filter_list_dma,
610                                      rxq_desc.desc,
611                                      mac_address);
612                 ibmveth_cleanup(adapter);
613                 napi_disable(&adapter->napi);
614                 return -ENONET;
615         }
616
617         for(i = 0; i<IbmVethNumBufferPools; i++) {
618                 if(!adapter->rx_buff_pool[i].active)
619                         continue;
620                 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
621                         ibmveth_error_printk("unable to alloc pool\n");
622                         adapter->rx_buff_pool[i].active = 0;
623                         ibmveth_cleanup(adapter);
624                         napi_disable(&adapter->napi);
625                         return -ENOMEM ;
626                 }
627         }
628
629         ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
630         if((rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
631                 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
632                 do {
633                         rc = h_free_logical_lan(adapter->vdev->unit_address);
634                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
635
636                 ibmveth_cleanup(adapter);
637                 napi_disable(&adapter->napi);
638                 return rc;
639         }
640
641         adapter->bounce_buffer =
642             kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
643         if (!adapter->bounce_buffer) {
644                 ibmveth_error_printk("unable to allocate bounce buffer\n");
645                 ibmveth_cleanup(adapter);
646                 napi_disable(&adapter->napi);
647                 return -ENOMEM;
648         }
649         adapter->bounce_buffer_dma =
650             dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
651                            netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
652         if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
653                 ibmveth_error_printk("unable to map bounce buffer\n");
654                 ibmveth_cleanup(adapter);
655                 napi_disable(&adapter->napi);
656                 return -ENOMEM;
657         }
658
659         ibmveth_debug_printk("initial replenish cycle\n");
660         ibmveth_interrupt(netdev->irq, netdev);
661
662         netif_start_queue(netdev);
663
664         ibmveth_debug_printk("open complete\n");
665
666         return 0;
667 }
668
669 static int ibmveth_close(struct net_device *netdev)
670 {
671         struct ibmveth_adapter *adapter = netdev_priv(netdev);
672         long lpar_rc;
673
674         ibmveth_debug_printk("close starting\n");
675
676         napi_disable(&adapter->napi);
677
678         if (!adapter->pool_config)
679                 netif_stop_queue(netdev);
680
681         free_irq(netdev->irq, netdev);
682
683         do {
684                 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
685         } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
686
687         if(lpar_rc != H_SUCCESS)
688         {
689                 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
690                                      lpar_rc);
691         }
692
693         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
694
695         ibmveth_cleanup(adapter);
696
697         ibmveth_debug_printk("close complete\n");
698
699         return 0;
700 }
701
702 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
703         cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
704         cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
705         cmd->speed = SPEED_1000;
706         cmd->duplex = DUPLEX_FULL;
707         cmd->port = PORT_FIBRE;
708         cmd->phy_address = 0;
709         cmd->transceiver = XCVR_INTERNAL;
710         cmd->autoneg = AUTONEG_ENABLE;
711         cmd->maxtxpkt = 0;
712         cmd->maxrxpkt = 1;
713         return 0;
714 }
715
716 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
717         strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
718         strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
719 }
720
721 static u32 netdev_get_link(struct net_device *dev) {
722         return 1;
723 }
724
725 static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
726 {
727         struct ibmveth_adapter *adapter = netdev_priv(dev);
728
729         if (data)
730                 adapter->rx_csum = 1;
731         else {
732                 /*
733                  * Since the ibmveth firmware interface does not have the concept of
734                  * separate tx/rx checksum offload enable, if rx checksum is disabled
735                  * we also have to disable tx checksum offload. Once we disable rx
736                  * checksum offload, we are no longer allowed to send tx buffers that
737                  * are not properly checksummed.
738                  */
739                 adapter->rx_csum = 0;
740                 dev->features &= ~NETIF_F_IP_CSUM;
741         }
742 }
743
744 static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
745 {
746         struct ibmveth_adapter *adapter = netdev_priv(dev);
747
748         if (data) {
749                 dev->features |= NETIF_F_IP_CSUM;
750                 adapter->rx_csum = 1;
751         } else
752                 dev->features &= ~NETIF_F_IP_CSUM;
753 }
754
755 static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
756                                     void (*done) (struct net_device *, u32))
757 {
758         struct ibmveth_adapter *adapter = netdev_priv(dev);
759         unsigned long set_attr, clr_attr, ret_attr;
760         long ret;
761         int rc1 = 0, rc2 = 0;
762         int restart = 0;
763
764         if (netif_running(dev)) {
765                 restart = 1;
766                 adapter->pool_config = 1;
767                 ibmveth_close(dev);
768                 adapter->pool_config = 0;
769         }
770
771         set_attr = 0;
772         clr_attr = 0;
773
774         if (data)
775                 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
776         else
777                 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
778
779         ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
780
781         if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
782             !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
783             (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
784                 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
785                                          set_attr, &ret_attr);
786
787                 if (ret != H_SUCCESS) {
788                         rc1 = -EIO;
789                         ibmveth_error_printk("unable to change checksum offload settings."
790                                              " %d rc=%ld\n", data, ret);
791
792                         ret = h_illan_attributes(adapter->vdev->unit_address,
793                                                  set_attr, clr_attr, &ret_attr);
794                 } else
795                         done(dev, data);
796         } else {
797                 rc1 = -EIO;
798                 ibmveth_error_printk("unable to change checksum offload settings."
799                                      " %d rc=%ld ret_attr=%lx\n", data, ret, ret_attr);
800         }
801
802         if (restart)
803                 rc2 = ibmveth_open(dev);
804
805         return rc1 ? rc1 : rc2;
806 }
807
808 static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
809 {
810         struct ibmveth_adapter *adapter = netdev_priv(dev);
811
812         if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
813                 return 0;
814
815         return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
816 }
817
818 static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
819 {
820         struct ibmveth_adapter *adapter = netdev_priv(dev);
821         int rc = 0;
822
823         if (data && (dev->features & NETIF_F_IP_CSUM))
824                 return 0;
825         if (!data && !(dev->features & NETIF_F_IP_CSUM))
826                 return 0;
827
828         if (data && !adapter->rx_csum)
829                 rc = ibmveth_set_csum_offload(dev, data, ibmveth_set_tx_csum_flags);
830         else
831                 ibmveth_set_tx_csum_flags(dev, data);
832
833         return rc;
834 }
835
836 static u32 ibmveth_get_rx_csum(struct net_device *dev)
837 {
838         struct ibmveth_adapter *adapter = netdev_priv(dev);
839         return adapter->rx_csum;
840 }
841
842 static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
843 {
844         int i;
845
846         if (stringset != ETH_SS_STATS)
847                 return;
848
849         for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
850                 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
851 }
852
853 static int ibmveth_get_sset_count(struct net_device *dev, int sset)
854 {
855         switch (sset) {
856         case ETH_SS_STATS:
857                 return ARRAY_SIZE(ibmveth_stats);
858         default:
859                 return -EOPNOTSUPP;
860         }
861 }
862
863 static void ibmveth_get_ethtool_stats(struct net_device *dev,
864                                       struct ethtool_stats *stats, u64 *data)
865 {
866         int i;
867         struct ibmveth_adapter *adapter = netdev_priv(dev);
868
869         for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
870                 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
871 }
872
873 static const struct ethtool_ops netdev_ethtool_ops = {
874         .get_drvinfo            = netdev_get_drvinfo,
875         .get_settings           = netdev_get_settings,
876         .get_link               = netdev_get_link,
877         .set_tx_csum            = ibmveth_set_tx_csum,
878         .get_rx_csum            = ibmveth_get_rx_csum,
879         .set_rx_csum            = ibmveth_set_rx_csum,
880         .get_strings            = ibmveth_get_strings,
881         .get_sset_count         = ibmveth_get_sset_count,
882         .get_ethtool_stats      = ibmveth_get_ethtool_stats,
883 };
884
885 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
886 {
887         return -EOPNOTSUPP;
888 }
889
890 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
891
892 static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
893                                       struct net_device *netdev)
894 {
895         struct ibmveth_adapter *adapter = netdev_priv(netdev);
896         union ibmveth_buf_desc desc;
897         unsigned long lpar_rc;
898         unsigned long correlator;
899         unsigned long flags;
900         unsigned int retry_count;
901         unsigned int tx_dropped = 0;
902         unsigned int tx_bytes = 0;
903         unsigned int tx_packets = 0;
904         unsigned int tx_send_failed = 0;
905         unsigned int tx_map_failed = 0;
906         int used_bounce = 0;
907         unsigned long data_dma_addr;
908
909         desc.fields.flags_len = IBMVETH_BUF_VALID | skb->len;
910
911         if (skb->ip_summed == CHECKSUM_PARTIAL &&
912             ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
913                 ibmveth_error_printk("tx: failed to checksum packet\n");
914                 tx_dropped++;
915                 goto out;
916         }
917
918         if (skb->ip_summed == CHECKSUM_PARTIAL) {
919                 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
920
921                 desc.fields.flags_len |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
922
923                 /* Need to zero out the checksum */
924                 buf[0] = 0;
925                 buf[1] = 0;
926         }
927
928         data_dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
929                                        skb->len, DMA_TO_DEVICE);
930         if (dma_mapping_error(&adapter->vdev->dev, data_dma_addr)) {
931                 if (!firmware_has_feature(FW_FEATURE_CMO))
932                         ibmveth_error_printk("tx: unable to map xmit buffer\n");
933                 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
934                                           skb->len);
935                 desc.fields.address = adapter->bounce_buffer_dma;
936                 tx_map_failed++;
937                 used_bounce = 1;
938                 wmb();
939         } else
940                 desc.fields.address = data_dma_addr;
941
942         /* send the frame. Arbitrarily set retrycount to 1024 */
943         correlator = 0;
944         retry_count = 1024;
945         do {
946                 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
947                                              desc.desc, 0, 0, 0, 0, 0,
948                                              correlator, &correlator);
949         } while ((lpar_rc == H_BUSY) && (retry_count--));
950
951         if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
952                 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
953                 ibmveth_error_printk("tx: valid=%d, len=%d, address=0x%08x\n",
954                                      (desc.fields.flags_len & IBMVETH_BUF_VALID) ? 1 : 0,
955                                      skb->len, desc.fields.address);
956                 tx_send_failed++;
957                 tx_dropped++;
958         } else {
959                 tx_packets++;
960                 tx_bytes += skb->len;
961                 netdev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
962         }
963
964         if (!used_bounce)
965                 dma_unmap_single(&adapter->vdev->dev, data_dma_addr,
966                                  skb->len, DMA_TO_DEVICE);
967
968 out:    spin_lock_irqsave(&adapter->stats_lock, flags);
969         netdev->stats.tx_dropped += tx_dropped;
970         netdev->stats.tx_bytes += tx_bytes;
971         netdev->stats.tx_packets += tx_packets;
972         adapter->tx_send_failed += tx_send_failed;
973         adapter->tx_map_failed += tx_map_failed;
974         spin_unlock_irqrestore(&adapter->stats_lock, flags);
975
976         dev_kfree_skb(skb);
977         return NETDEV_TX_OK;
978 }
979
980 static int ibmveth_poll(struct napi_struct *napi, int budget)
981 {
982         struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
983         struct net_device *netdev = adapter->netdev;
984         int frames_processed = 0;
985         unsigned long lpar_rc;
986
987  restart_poll:
988         do {
989                 struct sk_buff *skb;
990
991                 if (!ibmveth_rxq_pending_buffer(adapter))
992                         break;
993
994                 rmb();
995                 if (!ibmveth_rxq_buffer_valid(adapter)) {
996                         wmb(); /* suggested by larson1 */
997                         adapter->rx_invalid_buffer++;
998                         ibmveth_debug_printk("recycling invalid buffer\n");
999                         ibmveth_rxq_recycle_buffer(adapter);
1000                 } else {
1001                         int length = ibmveth_rxq_frame_length(adapter);
1002                         int offset = ibmveth_rxq_frame_offset(adapter);
1003                         int csum_good = ibmveth_rxq_csum_good(adapter);
1004
1005                         skb = ibmveth_rxq_get_buffer(adapter);
1006                         if (csum_good)
1007                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1008
1009                         ibmveth_rxq_harvest_buffer(adapter);
1010
1011                         skb_reserve(skb, offset);
1012                         skb_put(skb, length);
1013                         skb->protocol = eth_type_trans(skb, netdev);
1014
1015                         netif_receive_skb(skb); /* send it up */
1016
1017                         netdev->stats.rx_packets++;
1018                         netdev->stats.rx_bytes += length;
1019                         frames_processed++;
1020                 }
1021         } while (frames_processed < budget);
1022
1023         ibmveth_replenish_task(adapter);
1024
1025         if (frames_processed < budget) {
1026                 /* We think we are done - reenable interrupts,
1027                  * then check once more to make sure we are done.
1028                  */
1029                 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1030                                        VIO_IRQ_ENABLE);
1031
1032                 ibmveth_assert(lpar_rc == H_SUCCESS);
1033
1034                 napi_complete(napi);
1035
1036                 if (ibmveth_rxq_pending_buffer(adapter) &&
1037                     napi_reschedule(napi)) {
1038                         lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1039                                                VIO_IRQ_DISABLE);
1040                         goto restart_poll;
1041                 }
1042         }
1043
1044         return frames_processed;
1045 }
1046
1047 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
1048 {
1049         struct net_device *netdev = dev_instance;
1050         struct ibmveth_adapter *adapter = netdev_priv(netdev);
1051         unsigned long lpar_rc;
1052
1053         if (napi_schedule_prep(&adapter->napi)) {
1054                 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1055                                        VIO_IRQ_DISABLE);
1056                 ibmveth_assert(lpar_rc == H_SUCCESS);
1057                 __napi_schedule(&adapter->napi);
1058         }
1059         return IRQ_HANDLED;
1060 }
1061
1062 static void ibmveth_set_multicast_list(struct net_device *netdev)
1063 {
1064         struct ibmveth_adapter *adapter = netdev_priv(netdev);
1065         unsigned long lpar_rc;
1066
1067         if ((netdev->flags & IFF_PROMISC) ||
1068             (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
1069                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1070                                            IbmVethMcastEnableRecv |
1071                                            IbmVethMcastDisableFiltering,
1072                                            0);
1073                 if(lpar_rc != H_SUCCESS) {
1074                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
1075                 }
1076         } else {
1077                 struct netdev_hw_addr *ha;
1078                 /* clear the filter table & disable filtering */
1079                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1080                                            IbmVethMcastEnableRecv |
1081                                            IbmVethMcastDisableFiltering |
1082                                            IbmVethMcastClearFilterTable,
1083                                            0);
1084                 if(lpar_rc != H_SUCCESS) {
1085                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
1086                 }
1087                 /* add the addresses to the filter table */
1088                 netdev_for_each_mc_addr(ha, netdev) {
1089                         // add the multicast address to the filter table
1090                         unsigned long mcast_addr = 0;
1091                         memcpy(((char *)&mcast_addr)+2, ha->addr, 6);
1092                         lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1093                                                    IbmVethMcastAddFilter,
1094                                                    mcast_addr);
1095                         if(lpar_rc != H_SUCCESS) {
1096                                 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
1097                         }
1098                 }
1099
1100                 /* re-enable filtering */
1101                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1102                                            IbmVethMcastEnableFiltering,
1103                                            0);
1104                 if(lpar_rc != H_SUCCESS) {
1105                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
1106                 }
1107         }
1108 }
1109
1110 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1111 {
1112         struct ibmveth_adapter *adapter = netdev_priv(dev);
1113         struct vio_dev *viodev = adapter->vdev;
1114         int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
1115         int i;
1116
1117         if (new_mtu < IBMVETH_MAX_MTU)
1118                 return -EINVAL;
1119
1120         for (i = 0; i < IbmVethNumBufferPools; i++)
1121                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1122                         break;
1123
1124         if (i == IbmVethNumBufferPools)
1125                 return -EINVAL;
1126
1127         /* Deactivate all the buffer pools so that the next loop can activate
1128            only the buffer pools necessary to hold the new MTU */
1129         for (i = 0; i < IbmVethNumBufferPools; i++)
1130                 if (adapter->rx_buff_pool[i].active) {
1131                         ibmveth_free_buffer_pool(adapter,
1132                                                  &adapter->rx_buff_pool[i]);
1133                         adapter->rx_buff_pool[i].active = 0;
1134                 }
1135
1136         /* Look for an active buffer pool that can hold the new MTU */
1137         for(i = 0; i<IbmVethNumBufferPools; i++) {
1138                 adapter->rx_buff_pool[i].active = 1;
1139
1140                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
1141                         if (netif_running(adapter->netdev)) {
1142                                 adapter->pool_config = 1;
1143                                 ibmveth_close(adapter->netdev);
1144                                 adapter->pool_config = 0;
1145                                 dev->mtu = new_mtu;
1146                                 vio_cmo_set_dev_desired(viodev,
1147                                                 ibmveth_get_desired_dma
1148                                                 (viodev));
1149                                 return ibmveth_open(adapter->netdev);
1150                         }
1151                         dev->mtu = new_mtu;
1152                         vio_cmo_set_dev_desired(viodev,
1153                                                 ibmveth_get_desired_dma
1154                                                 (viodev));
1155                         return 0;
1156                 }
1157         }
1158         return -EINVAL;
1159 }
1160
1161 #ifdef CONFIG_NET_POLL_CONTROLLER
1162 static void ibmveth_poll_controller(struct net_device *dev)
1163 {
1164         ibmveth_replenish_task(netdev_priv(dev));
1165         ibmveth_interrupt(dev->irq, dev);
1166 }
1167 #endif
1168
1169 /**
1170  * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1171  *
1172  * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1173  *
1174  * Return value:
1175  *      Number of bytes of IO data the driver will need to perform well.
1176  */
1177 static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1178 {
1179         struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1180         struct ibmveth_adapter *adapter;
1181         unsigned long ret;
1182         int i;
1183         int rxqentries = 1;
1184
1185         /* netdev inits at probe time along with the structures we need below*/
1186         if (netdev == NULL)
1187                 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT);
1188
1189         adapter = netdev_priv(netdev);
1190
1191         ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
1192         ret += IOMMU_PAGE_ALIGN(netdev->mtu);
1193
1194         for (i = 0; i < IbmVethNumBufferPools; i++) {
1195                 /* add the size of the active receive buffers */
1196                 if (adapter->rx_buff_pool[i].active)
1197                         ret +=
1198                             adapter->rx_buff_pool[i].size *
1199                             IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
1200                                     buff_size);
1201                 rxqentries += adapter->rx_buff_pool[i].size;
1202         }
1203         /* add the size of the receive queue entries */
1204         ret += IOMMU_PAGE_ALIGN(rxqentries * sizeof(struct ibmveth_rx_q_entry));
1205
1206         return ret;
1207 }
1208
1209 static const struct net_device_ops ibmveth_netdev_ops = {
1210         .ndo_open               = ibmveth_open,
1211         .ndo_stop               = ibmveth_close,
1212         .ndo_start_xmit         = ibmveth_start_xmit,
1213         .ndo_set_multicast_list = ibmveth_set_multicast_list,
1214         .ndo_do_ioctl           = ibmveth_ioctl,
1215         .ndo_change_mtu         = ibmveth_change_mtu,
1216         .ndo_validate_addr      = eth_validate_addr,
1217         .ndo_set_mac_address    = eth_mac_addr,
1218 #ifdef CONFIG_NET_POLL_CONTROLLER
1219         .ndo_poll_controller    = ibmveth_poll_controller,
1220 #endif
1221 };
1222
1223 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1224 {
1225         int rc, i;
1226         long ret;
1227         struct net_device *netdev;
1228         struct ibmveth_adapter *adapter;
1229         unsigned long set_attr, ret_attr;
1230
1231         unsigned char *mac_addr_p;
1232         unsigned int *mcastFilterSize_p;
1233
1234
1235         ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
1236                                         dev->unit_address);
1237
1238         mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1239                                                 VETH_MAC_ADDR, NULL);
1240         if(!mac_addr_p) {
1241                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1242                                 "attribute\n", __FILE__, __LINE__);
1243                 return 0;
1244         }
1245
1246         mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1247                                                 VETH_MCAST_FILTER_SIZE, NULL);
1248         if(!mcastFilterSize_p) {
1249                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1250                                 "VETH_MCAST_FILTER_SIZE attribute\n",
1251                                 __FILE__, __LINE__);
1252                 return 0;
1253         }
1254
1255         netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1256
1257         if(!netdev)
1258                 return -ENOMEM;
1259
1260         adapter = netdev_priv(netdev);
1261         dev_set_drvdata(&dev->dev, netdev);
1262
1263         adapter->vdev = dev;
1264         adapter->netdev = netdev;
1265         adapter->mcastFilterSize= *mcastFilterSize_p;
1266         adapter->pool_config = 0;
1267
1268         netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1269
1270         /*      Some older boxes running PHYP non-natively have an OF that
1271                 returns a 8-byte local-mac-address field (and the first
1272                 2 bytes have to be ignored) while newer boxes' OF return
1273                 a 6-byte field. Note that IEEE 1275 specifies that
1274                 local-mac-address must be a 6-byte field.
1275                 The RPA doc specifies that the first byte must be 10b, so
1276                 we'll just look for it to solve this 8 vs. 6 byte field issue */
1277
1278         if ((*mac_addr_p & 0x3) != 0x02)
1279                 mac_addr_p += 2;
1280
1281         adapter->mac_addr = 0;
1282         memcpy(&adapter->mac_addr, mac_addr_p, 6);
1283
1284         netdev->irq = dev->irq;
1285         netdev->netdev_ops = &ibmveth_netdev_ops;
1286         netdev->ethtool_ops = &netdev_ethtool_ops;
1287         SET_NETDEV_DEV(netdev, &dev->dev);
1288         netdev->features |= NETIF_F_LLTX;
1289         spin_lock_init(&adapter->stats_lock);
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);