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