Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 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

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

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