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