Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us