11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29aa32835SJeff Kirsher /*
39aa32835SJeff Kirsher * IBM Power Virtual Ethernet Device Driver
49aa32835SJeff Kirsher *
59aa32835SJeff Kirsher * Copyright (C) IBM Corporation, 2003, 2010
69aa32835SJeff Kirsher *
79aa32835SJeff Kirsher * Authors: Dave Larson <larson1@us.ibm.com>
89aa32835SJeff Kirsher * Santiago Leon <santil@linux.vnet.ibm.com>
99aa32835SJeff Kirsher * Brian King <brking@linux.vnet.ibm.com>
109aa32835SJeff Kirsher * Robert Jennings <rcj@linux.vnet.ibm.com>
119aa32835SJeff Kirsher * Anton Blanchard <anton@au.ibm.com>
129aa32835SJeff Kirsher */
139aa32835SJeff Kirsher
149aa32835SJeff Kirsher #include <linux/module.h>
159aa32835SJeff Kirsher #include <linux/types.h>
169aa32835SJeff Kirsher #include <linux/errno.h>
179aa32835SJeff Kirsher #include <linux/dma-mapping.h>
189aa32835SJeff Kirsher #include <linux/kernel.h>
199aa32835SJeff Kirsher #include <linux/netdevice.h>
209aa32835SJeff Kirsher #include <linux/etherdevice.h>
219aa32835SJeff Kirsher #include <linux/skbuff.h>
229aa32835SJeff Kirsher #include <linux/init.h>
239aa32835SJeff Kirsher #include <linux/interrupt.h>
249aa32835SJeff Kirsher #include <linux/mm.h>
259aa32835SJeff Kirsher #include <linux/pm.h>
269aa32835SJeff Kirsher #include <linux/ethtool.h>
279aa32835SJeff Kirsher #include <linux/in.h>
289aa32835SJeff Kirsher #include <linux/ip.h>
299aa32835SJeff Kirsher #include <linux/ipv6.h>
309aa32835SJeff Kirsher #include <linux/slab.h>
319aa32835SJeff Kirsher #include <asm/hvcall.h>
329aa32835SJeff Kirsher #include <linux/atomic.h>
339aa32835SJeff Kirsher #include <asm/vio.h>
349aa32835SJeff Kirsher #include <asm/iommu.h>
359aa32835SJeff Kirsher #include <asm/firmware.h>
3666aa0678SSivakumar Krishnasamy #include <net/tcp.h>
3766aa0678SSivakumar Krishnasamy #include <net/ip6_checksum.h>
389aa32835SJeff Kirsher
399aa32835SJeff Kirsher #include "ibmveth.h"
409aa32835SJeff Kirsher
419aa32835SJeff Kirsher static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
429aa32835SJeff Kirsher static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
439aa32835SJeff Kirsher static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
449aa32835SJeff Kirsher
459aa32835SJeff Kirsher static struct kobj_type ktype_veth_pool;
469aa32835SJeff Kirsher
479aa32835SJeff Kirsher
489aa32835SJeff Kirsher static const char ibmveth_driver_name[] = "ibmveth";
499aa32835SJeff Kirsher static const char ibmveth_driver_string[] = "IBM Power Virtual Ethernet Driver";
507b596738SThomas Falcon #define ibmveth_driver_version "1.06"
519aa32835SJeff Kirsher
529aa32835SJeff Kirsher MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
539aa32835SJeff Kirsher MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
549aa32835SJeff Kirsher MODULE_LICENSE("GPL");
559aa32835SJeff Kirsher MODULE_VERSION(ibmveth_driver_version);
569aa32835SJeff Kirsher
579aa32835SJeff Kirsher static unsigned int tx_copybreak __read_mostly = 128;
589aa32835SJeff Kirsher module_param(tx_copybreak, uint, 0644);
599aa32835SJeff Kirsher MODULE_PARM_DESC(tx_copybreak,
609aa32835SJeff Kirsher "Maximum size of packet that is copied to a new buffer on transmit");
619aa32835SJeff Kirsher
629aa32835SJeff Kirsher static unsigned int rx_copybreak __read_mostly = 128;
639aa32835SJeff Kirsher module_param(rx_copybreak, uint, 0644);
649aa32835SJeff Kirsher MODULE_PARM_DESC(rx_copybreak,
659aa32835SJeff Kirsher "Maximum size of packet that is copied to a new buffer on receive");
669aa32835SJeff Kirsher
679aa32835SJeff Kirsher static unsigned int rx_flush __read_mostly = 0;
689aa32835SJeff Kirsher module_param(rx_flush, uint, 0644);
699aa32835SJeff Kirsher MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");
709aa32835SJeff Kirsher
7107e6a97dSThomas Falcon static bool old_large_send __read_mostly;
72d3757ba4SJoe Perches module_param(old_large_send, bool, 0444);
7307e6a97dSThomas Falcon MODULE_PARM_DESC(old_large_send,
7407e6a97dSThomas Falcon "Use old large send method on firmware that supports the new method");
7507e6a97dSThomas Falcon
769aa32835SJeff Kirsher struct ibmveth_stat {
779aa32835SJeff Kirsher char name[ETH_GSTRING_LEN];
789aa32835SJeff Kirsher int offset;
799aa32835SJeff Kirsher };
809aa32835SJeff Kirsher
819aa32835SJeff Kirsher #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
829aa32835SJeff Kirsher #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
839aa32835SJeff Kirsher
84a0cfa79fSYueHaibing static struct ibmveth_stat ibmveth_stats[] = {
859aa32835SJeff Kirsher { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
869aa32835SJeff Kirsher { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
879aa32835SJeff Kirsher { "replenish_add_buff_failure",
889aa32835SJeff Kirsher IBMVETH_STAT_OFF(replenish_add_buff_failure) },
899aa32835SJeff Kirsher { "replenish_add_buff_success",
909aa32835SJeff Kirsher IBMVETH_STAT_OFF(replenish_add_buff_success) },
919aa32835SJeff Kirsher { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
929aa32835SJeff Kirsher { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
939aa32835SJeff Kirsher { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
949aa32835SJeff Kirsher { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
959aa32835SJeff Kirsher { "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
969aa32835SJeff Kirsher { "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
978641dd85SThomas Falcon { "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets) },
9807e6a97dSThomas Falcon { "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets) },
9907e6a97dSThomas Falcon { "fw_enabled_large_send", IBMVETH_STAT_OFF(fw_large_send_support) }
1009aa32835SJeff Kirsher };
1019aa32835SJeff Kirsher
1029aa32835SJeff Kirsher /* simple methods of getting data from the current rxq entry */
ibmveth_rxq_flags(struct ibmveth_adapter * adapter)1039aa32835SJeff Kirsher static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
1049aa32835SJeff Kirsher {
1050b536be7SAnton Blanchard return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
1069aa32835SJeff Kirsher }
1079aa32835SJeff Kirsher
ibmveth_rxq_toggle(struct ibmveth_adapter * adapter)1089aa32835SJeff Kirsher static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
1099aa32835SJeff Kirsher {
1109aa32835SJeff Kirsher return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
1119aa32835SJeff Kirsher IBMVETH_RXQ_TOGGLE_SHIFT;
1129aa32835SJeff Kirsher }
1139aa32835SJeff Kirsher
ibmveth_rxq_pending_buffer(struct ibmveth_adapter * adapter)1149aa32835SJeff Kirsher static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
1159aa32835SJeff Kirsher {
1169aa32835SJeff Kirsher return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
1179aa32835SJeff Kirsher }
1189aa32835SJeff Kirsher
ibmveth_rxq_buffer_valid(struct ibmveth_adapter * adapter)1199aa32835SJeff Kirsher static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
1209aa32835SJeff Kirsher {
1219aa32835SJeff Kirsher return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
1229aa32835SJeff Kirsher }
1239aa32835SJeff Kirsher
ibmveth_rxq_frame_offset(struct ibmveth_adapter * adapter)1249aa32835SJeff Kirsher static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
1259aa32835SJeff Kirsher {
1269aa32835SJeff Kirsher return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
1279aa32835SJeff Kirsher }
1289aa32835SJeff Kirsher
ibmveth_rxq_large_packet(struct ibmveth_adapter * adapter)1297b596738SThomas Falcon static inline int ibmveth_rxq_large_packet(struct ibmveth_adapter *adapter)
1307b596738SThomas Falcon {
1317b596738SThomas Falcon return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_LRG_PKT;
1327b596738SThomas Falcon }
1337b596738SThomas Falcon
ibmveth_rxq_frame_length(struct ibmveth_adapter * adapter)1349aa32835SJeff Kirsher static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
1359aa32835SJeff Kirsher {
1360b536be7SAnton Blanchard return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
1379aa32835SJeff Kirsher }
1389aa32835SJeff Kirsher
ibmveth_rxq_csum_good(struct ibmveth_adapter * adapter)1399aa32835SJeff Kirsher static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
1409aa32835SJeff Kirsher {
1419aa32835SJeff Kirsher return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
1429aa32835SJeff Kirsher }
1439aa32835SJeff Kirsher
ibmveth_real_max_tx_queues(void)14410c2aba8SNick Child static unsigned int ibmveth_real_max_tx_queues(void)
14510c2aba8SNick Child {
14610c2aba8SNick Child unsigned int n_cpu = num_online_cpus();
14710c2aba8SNick Child
14810c2aba8SNick Child return min(n_cpu, IBMVETH_MAX_QUEUES);
14910c2aba8SNick Child }
15010c2aba8SNick Child
1519aa32835SJeff Kirsher /* setup the initial settings for a buffer pool */
ibmveth_init_buffer_pool(struct ibmveth_buff_pool * pool,u32 pool_index,u32 pool_size,u32 buff_size,u32 pool_active)1529aa32835SJeff Kirsher static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
1539aa32835SJeff Kirsher u32 pool_index, u32 pool_size,
1549aa32835SJeff Kirsher u32 buff_size, u32 pool_active)
1559aa32835SJeff Kirsher {
1569aa32835SJeff Kirsher pool->size = pool_size;
1579aa32835SJeff Kirsher pool->index = pool_index;
1589aa32835SJeff Kirsher pool->buff_size = buff_size;
1599aa32835SJeff Kirsher pool->threshold = pool_size * 7 / 8;
1609aa32835SJeff Kirsher pool->active = pool_active;
1619aa32835SJeff Kirsher }
1629aa32835SJeff Kirsher
1639aa32835SJeff Kirsher /* allocate and setup an buffer pool - called during open */
ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool * pool)1649aa32835SJeff Kirsher static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
1659aa32835SJeff Kirsher {
1669aa32835SJeff Kirsher int i;
1679aa32835SJeff Kirsher
1686da2ec56SKees Cook pool->free_map = kmalloc_array(pool->size, sizeof(u16), GFP_KERNEL);
1699aa32835SJeff Kirsher
1709aa32835SJeff Kirsher if (!pool->free_map)
1719aa32835SJeff Kirsher return -1;
1729aa32835SJeff Kirsher
173076ef440SNicholas Mc Guire pool->dma_addr = kcalloc(pool->size, sizeof(dma_addr_t), GFP_KERNEL);
1749aa32835SJeff Kirsher if (!pool->dma_addr) {
1759aa32835SJeff Kirsher kfree(pool->free_map);
1769aa32835SJeff Kirsher pool->free_map = NULL;
1779aa32835SJeff Kirsher return -1;
1789aa32835SJeff Kirsher }
1799aa32835SJeff Kirsher
1809aa32835SJeff Kirsher pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
1819aa32835SJeff Kirsher
1829aa32835SJeff Kirsher if (!pool->skbuff) {
1839aa32835SJeff Kirsher kfree(pool->dma_addr);
1849aa32835SJeff Kirsher pool->dma_addr = NULL;
1859aa32835SJeff Kirsher
1869aa32835SJeff Kirsher kfree(pool->free_map);
1879aa32835SJeff Kirsher pool->free_map = NULL;
1889aa32835SJeff Kirsher return -1;
1899aa32835SJeff Kirsher }
1909aa32835SJeff Kirsher
1919aa32835SJeff Kirsher for (i = 0; i < pool->size; ++i)
1929aa32835SJeff Kirsher pool->free_map[i] = i;
1939aa32835SJeff Kirsher
1949aa32835SJeff Kirsher atomic_set(&pool->available, 0);
1959aa32835SJeff Kirsher pool->producer_index = 0;
1969aa32835SJeff Kirsher pool->consumer_index = 0;
1979aa32835SJeff Kirsher
1989aa32835SJeff Kirsher return 0;
1999aa32835SJeff Kirsher }
2009aa32835SJeff Kirsher
ibmveth_flush_buffer(void * addr,unsigned long length)2019aa32835SJeff Kirsher static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
2029aa32835SJeff Kirsher {
2039aa32835SJeff Kirsher unsigned long offset;
2049aa32835SJeff Kirsher
2059aa32835SJeff Kirsher for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
206bfedba3bSMichael Ellerman asm("dcbf %0,%1,1" :: "b" (addr), "r" (offset));
2079aa32835SJeff Kirsher }
2089aa32835SJeff Kirsher
2099aa32835SJeff Kirsher /* replenish the buffers for a pool. note that we don't need to
2109aa32835SJeff Kirsher * skb_reserve these since they are used for incoming...
2119aa32835SJeff Kirsher */
ibmveth_replenish_buffer_pool(struct ibmveth_adapter * adapter,struct ibmveth_buff_pool * pool)2129aa32835SJeff Kirsher static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
2139aa32835SJeff Kirsher struct ibmveth_buff_pool *pool)
2149aa32835SJeff Kirsher {
2159aa32835SJeff Kirsher u32 i;
2169aa32835SJeff Kirsher u32 count = pool->size - atomic_read(&pool->available);
2179aa32835SJeff Kirsher u32 buffers_added = 0;
2189aa32835SJeff Kirsher struct sk_buff *skb;
2199aa32835SJeff Kirsher unsigned int free_index, index;
2209aa32835SJeff Kirsher u64 correlator;
2219aa32835SJeff Kirsher unsigned long lpar_rc;
2229aa32835SJeff Kirsher dma_addr_t dma_addr;
2239aa32835SJeff Kirsher
2249aa32835SJeff Kirsher mb();
2259aa32835SJeff Kirsher
2269aa32835SJeff Kirsher for (i = 0; i < count; ++i) {
2279aa32835SJeff Kirsher union ibmveth_buf_desc desc;
2289aa32835SJeff Kirsher
2299aa32835SJeff Kirsher skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
2309aa32835SJeff Kirsher
2319aa32835SJeff Kirsher if (!skb) {
2329aa32835SJeff Kirsher netdev_dbg(adapter->netdev,
2339aa32835SJeff Kirsher "replenish: unable to allocate skb\n");
2349aa32835SJeff Kirsher adapter->replenish_no_mem++;
2359aa32835SJeff Kirsher break;
2369aa32835SJeff Kirsher }
2379aa32835SJeff Kirsher
2389aa32835SJeff Kirsher free_index = pool->consumer_index;
2399aa32835SJeff Kirsher pool->consumer_index++;
2409aa32835SJeff Kirsher if (pool->consumer_index >= pool->size)
2419aa32835SJeff Kirsher pool->consumer_index = 0;
2429aa32835SJeff Kirsher index = pool->free_map[free_index];
2439aa32835SJeff Kirsher
2449aa32835SJeff Kirsher BUG_ON(index == IBM_VETH_INVALID_MAP);
2459aa32835SJeff Kirsher BUG_ON(pool->skbuff[index] != NULL);
2469aa32835SJeff Kirsher
2479aa32835SJeff Kirsher dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
2489aa32835SJeff Kirsher pool->buff_size, DMA_FROM_DEVICE);
2499aa32835SJeff Kirsher
2509aa32835SJeff Kirsher if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
2519aa32835SJeff Kirsher goto failure;
2529aa32835SJeff Kirsher
2539aa32835SJeff Kirsher pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
2549aa32835SJeff Kirsher pool->dma_addr[index] = dma_addr;
2559aa32835SJeff Kirsher pool->skbuff[index] = skb;
2569aa32835SJeff Kirsher
2579aa32835SJeff Kirsher correlator = ((u64)pool->index << 32) | index;
2589aa32835SJeff Kirsher *(u64 *)skb->data = correlator;
2599aa32835SJeff Kirsher
2609aa32835SJeff Kirsher desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
2619aa32835SJeff Kirsher desc.fields.address = dma_addr;
2629aa32835SJeff Kirsher
2639aa32835SJeff Kirsher if (rx_flush) {
2649aa32835SJeff Kirsher unsigned int len = min(pool->buff_size,
2659aa32835SJeff Kirsher adapter->netdev->mtu +
2669aa32835SJeff Kirsher IBMVETH_BUFF_OH);
2679aa32835SJeff Kirsher ibmveth_flush_buffer(skb->data, len);
2689aa32835SJeff Kirsher }
2699aa32835SJeff Kirsher lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
2709aa32835SJeff Kirsher desc.desc);
2719aa32835SJeff Kirsher
2729aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
2739aa32835SJeff Kirsher goto failure;
2749aa32835SJeff Kirsher } else {
2759aa32835SJeff Kirsher buffers_added++;
2769aa32835SJeff Kirsher adapter->replenish_add_buff_success++;
2779aa32835SJeff Kirsher }
2789aa32835SJeff Kirsher }
2799aa32835SJeff Kirsher
2809aa32835SJeff Kirsher mb();
2819aa32835SJeff Kirsher atomic_add(buffers_added, &(pool->available));
2829aa32835SJeff Kirsher return;
2839aa32835SJeff Kirsher
2849aa32835SJeff Kirsher failure:
2859aa32835SJeff Kirsher pool->free_map[free_index] = index;
2869aa32835SJeff Kirsher pool->skbuff[index] = NULL;
2879aa32835SJeff Kirsher if (pool->consumer_index == 0)
2889aa32835SJeff Kirsher pool->consumer_index = pool->size - 1;
2899aa32835SJeff Kirsher else
2909aa32835SJeff Kirsher pool->consumer_index--;
2919aa32835SJeff Kirsher if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
2929aa32835SJeff Kirsher dma_unmap_single(&adapter->vdev->dev,
2939aa32835SJeff Kirsher pool->dma_addr[index], pool->buff_size,
2949aa32835SJeff Kirsher DMA_FROM_DEVICE);
2959aa32835SJeff Kirsher dev_kfree_skb_any(skb);
2969aa32835SJeff Kirsher adapter->replenish_add_buff_failure++;
2979aa32835SJeff Kirsher
2989aa32835SJeff Kirsher mb();
2999aa32835SJeff Kirsher atomic_add(buffers_added, &(pool->available));
3009aa32835SJeff Kirsher }
3019aa32835SJeff Kirsher
302cbd52281SAnton Blanchard /*
303cbd52281SAnton Blanchard * The final 8 bytes of the buffer list is a counter of frames dropped
304cbd52281SAnton Blanchard * because there was not a buffer in the buffer list capable of holding
305cbd52281SAnton Blanchard * the frame.
306cbd52281SAnton Blanchard */
ibmveth_update_rx_no_buffer(struct ibmveth_adapter * adapter)307cbd52281SAnton Blanchard static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
308cbd52281SAnton Blanchard {
309cbd52281SAnton Blanchard __be64 *p = adapter->buffer_list_addr + 4096 - 8;
310cbd52281SAnton Blanchard
311cbd52281SAnton Blanchard adapter->rx_no_buffer = be64_to_cpup(p);
312cbd52281SAnton Blanchard }
313cbd52281SAnton Blanchard
3149aa32835SJeff Kirsher /* replenish routine */
ibmveth_replenish_task(struct ibmveth_adapter * adapter)3159aa32835SJeff Kirsher static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
3169aa32835SJeff Kirsher {
3179aa32835SJeff Kirsher int i;
3189aa32835SJeff Kirsher
3199aa32835SJeff Kirsher adapter->replenish_task_cycles++;
3209aa32835SJeff Kirsher
3219aa32835SJeff Kirsher for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
3229aa32835SJeff Kirsher struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
3239aa32835SJeff Kirsher
3249aa32835SJeff Kirsher if (pool->active &&
3259aa32835SJeff Kirsher (atomic_read(&pool->available) < pool->threshold))
3269aa32835SJeff Kirsher ibmveth_replenish_buffer_pool(adapter, pool);
3279aa32835SJeff Kirsher }
3289aa32835SJeff Kirsher
329cbd52281SAnton Blanchard ibmveth_update_rx_no_buffer(adapter);
3309aa32835SJeff Kirsher }
3319aa32835SJeff Kirsher
3329aa32835SJeff Kirsher /* empty and free ana buffer pool - also used to do cleanup in error paths */
ibmveth_free_buffer_pool(struct ibmveth_adapter * adapter,struct ibmveth_buff_pool * pool)3339aa32835SJeff Kirsher static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
3349aa32835SJeff Kirsher struct ibmveth_buff_pool *pool)
3359aa32835SJeff Kirsher {
3369aa32835SJeff Kirsher int i;
3379aa32835SJeff Kirsher
3389aa32835SJeff Kirsher kfree(pool->free_map);
3399aa32835SJeff Kirsher pool->free_map = NULL;
3409aa32835SJeff Kirsher
3419aa32835SJeff Kirsher if (pool->skbuff && pool->dma_addr) {
3429aa32835SJeff Kirsher for (i = 0; i < pool->size; ++i) {
3439aa32835SJeff Kirsher struct sk_buff *skb = pool->skbuff[i];
3449aa32835SJeff Kirsher if (skb) {
3459aa32835SJeff Kirsher dma_unmap_single(&adapter->vdev->dev,
3469aa32835SJeff Kirsher pool->dma_addr[i],
3479aa32835SJeff Kirsher pool->buff_size,
3489aa32835SJeff Kirsher DMA_FROM_DEVICE);
3499aa32835SJeff Kirsher dev_kfree_skb_any(skb);
3509aa32835SJeff Kirsher pool->skbuff[i] = NULL;
3519aa32835SJeff Kirsher }
3529aa32835SJeff Kirsher }
3539aa32835SJeff Kirsher }
3549aa32835SJeff Kirsher
3559aa32835SJeff Kirsher if (pool->dma_addr) {
3569aa32835SJeff Kirsher kfree(pool->dma_addr);
3579aa32835SJeff Kirsher pool->dma_addr = NULL;
3589aa32835SJeff Kirsher }
3599aa32835SJeff Kirsher
3609aa32835SJeff Kirsher if (pool->skbuff) {
3619aa32835SJeff Kirsher kfree(pool->skbuff);
3629aa32835SJeff Kirsher pool->skbuff = NULL;
3639aa32835SJeff Kirsher }
3649aa32835SJeff Kirsher }
3659aa32835SJeff Kirsher
3669aa32835SJeff Kirsher /* remove a buffer from a pool */
ibmveth_remove_buffer_from_pool(struct ibmveth_adapter * adapter,u64 correlator)3679aa32835SJeff Kirsher static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
3689aa32835SJeff Kirsher u64 correlator)
3699aa32835SJeff Kirsher {
3709aa32835SJeff Kirsher unsigned int pool = correlator >> 32;
3719aa32835SJeff Kirsher unsigned int index = correlator & 0xffffffffUL;
3729aa32835SJeff Kirsher unsigned int free_index;
3739aa32835SJeff Kirsher struct sk_buff *skb;
3749aa32835SJeff Kirsher
3759aa32835SJeff Kirsher BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
3769aa32835SJeff Kirsher BUG_ON(index >= adapter->rx_buff_pool[pool].size);
3779aa32835SJeff Kirsher
3789aa32835SJeff Kirsher skb = adapter->rx_buff_pool[pool].skbuff[index];
3799aa32835SJeff Kirsher
3809aa32835SJeff Kirsher BUG_ON(skb == NULL);
3819aa32835SJeff Kirsher
3829aa32835SJeff Kirsher adapter->rx_buff_pool[pool].skbuff[index] = NULL;
3839aa32835SJeff Kirsher
3849aa32835SJeff Kirsher dma_unmap_single(&adapter->vdev->dev,
3859aa32835SJeff Kirsher adapter->rx_buff_pool[pool].dma_addr[index],
3869aa32835SJeff Kirsher adapter->rx_buff_pool[pool].buff_size,
3879aa32835SJeff Kirsher DMA_FROM_DEVICE);
3889aa32835SJeff Kirsher
3899aa32835SJeff Kirsher free_index = adapter->rx_buff_pool[pool].producer_index;
3909aa32835SJeff Kirsher adapter->rx_buff_pool[pool].producer_index++;
3919aa32835SJeff Kirsher if (adapter->rx_buff_pool[pool].producer_index >=
3929aa32835SJeff Kirsher adapter->rx_buff_pool[pool].size)
3939aa32835SJeff Kirsher adapter->rx_buff_pool[pool].producer_index = 0;
3949aa32835SJeff Kirsher adapter->rx_buff_pool[pool].free_map[free_index] = index;
3959aa32835SJeff Kirsher
3969aa32835SJeff Kirsher mb();
3979aa32835SJeff Kirsher
3989aa32835SJeff Kirsher atomic_dec(&(adapter->rx_buff_pool[pool].available));
3999aa32835SJeff Kirsher }
4009aa32835SJeff Kirsher
4019aa32835SJeff Kirsher /* get the current buffer on the rx queue */
ibmveth_rxq_get_buffer(struct ibmveth_adapter * adapter)4029aa32835SJeff Kirsher static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
4039aa32835SJeff Kirsher {
4049aa32835SJeff Kirsher u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
4059aa32835SJeff Kirsher unsigned int pool = correlator >> 32;
4069aa32835SJeff Kirsher unsigned int index = correlator & 0xffffffffUL;
4079aa32835SJeff Kirsher
4089aa32835SJeff Kirsher BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
4099aa32835SJeff Kirsher BUG_ON(index >= adapter->rx_buff_pool[pool].size);
4109aa32835SJeff Kirsher
4119aa32835SJeff Kirsher return adapter->rx_buff_pool[pool].skbuff[index];
4129aa32835SJeff Kirsher }
4139aa32835SJeff Kirsher
4149aa32835SJeff Kirsher /* recycle the current buffer on the rx queue */
ibmveth_rxq_recycle_buffer(struct ibmveth_adapter * adapter)4158decf868SDavid S. Miller static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
4169aa32835SJeff Kirsher {
4179aa32835SJeff Kirsher u32 q_index = adapter->rx_queue.index;
4189aa32835SJeff Kirsher u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
4199aa32835SJeff Kirsher unsigned int pool = correlator >> 32;
4209aa32835SJeff Kirsher unsigned int index = correlator & 0xffffffffUL;
4219aa32835SJeff Kirsher union ibmveth_buf_desc desc;
4229aa32835SJeff Kirsher unsigned long lpar_rc;
4238decf868SDavid S. Miller int ret = 1;
4249aa32835SJeff Kirsher
4259aa32835SJeff Kirsher BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
4269aa32835SJeff Kirsher BUG_ON(index >= adapter->rx_buff_pool[pool].size);
4279aa32835SJeff Kirsher
4289aa32835SJeff Kirsher if (!adapter->rx_buff_pool[pool].active) {
4299aa32835SJeff Kirsher ibmveth_rxq_harvest_buffer(adapter);
4309aa32835SJeff Kirsher ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
4318decf868SDavid S. Miller goto out;
4329aa32835SJeff Kirsher }
4339aa32835SJeff Kirsher
4349aa32835SJeff Kirsher desc.fields.flags_len = IBMVETH_BUF_VALID |
4359aa32835SJeff Kirsher adapter->rx_buff_pool[pool].buff_size;
4369aa32835SJeff Kirsher desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
4379aa32835SJeff Kirsher
4389aa32835SJeff Kirsher lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
4399aa32835SJeff Kirsher
4409aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
4419aa32835SJeff Kirsher netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
4429aa32835SJeff Kirsher "during recycle rc=%ld", lpar_rc);
4439aa32835SJeff Kirsher ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
4448decf868SDavid S. Miller ret = 0;
4459aa32835SJeff Kirsher }
4469aa32835SJeff Kirsher
4479aa32835SJeff Kirsher if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
4489aa32835SJeff Kirsher adapter->rx_queue.index = 0;
4499aa32835SJeff Kirsher adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
4509aa32835SJeff Kirsher }
4518decf868SDavid S. Miller
4528decf868SDavid S. Miller out:
4538decf868SDavid S. Miller return ret;
4549aa32835SJeff Kirsher }
4559aa32835SJeff Kirsher
ibmveth_rxq_harvest_buffer(struct ibmveth_adapter * adapter)4569aa32835SJeff Kirsher static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
4579aa32835SJeff Kirsher {
4589aa32835SJeff Kirsher ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
4599aa32835SJeff Kirsher
4609aa32835SJeff Kirsher if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
4619aa32835SJeff Kirsher adapter->rx_queue.index = 0;
4629aa32835SJeff Kirsher adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
4639aa32835SJeff Kirsher }
4649aa32835SJeff Kirsher }
4659aa32835SJeff Kirsher
ibmveth_free_tx_ltb(struct ibmveth_adapter * adapter,int idx)46610c2aba8SNick Child static void ibmveth_free_tx_ltb(struct ibmveth_adapter *adapter, int idx)
46710c2aba8SNick Child {
46810c2aba8SNick Child dma_unmap_single(&adapter->vdev->dev, adapter->tx_ltb_dma[idx],
46910c2aba8SNick Child adapter->tx_ltb_size, DMA_TO_DEVICE);
47010c2aba8SNick Child kfree(adapter->tx_ltb_ptr[idx]);
47110c2aba8SNick Child adapter->tx_ltb_ptr[idx] = NULL;
47210c2aba8SNick Child }
47310c2aba8SNick Child
ibmveth_allocate_tx_ltb(struct ibmveth_adapter * adapter,int idx)47410c2aba8SNick Child static int ibmveth_allocate_tx_ltb(struct ibmveth_adapter *adapter, int idx)
47510c2aba8SNick Child {
47610c2aba8SNick Child adapter->tx_ltb_ptr[idx] = kzalloc(adapter->tx_ltb_size,
47710c2aba8SNick Child GFP_KERNEL);
47810c2aba8SNick Child if (!adapter->tx_ltb_ptr[idx]) {
47910c2aba8SNick Child netdev_err(adapter->netdev,
48010c2aba8SNick Child "unable to allocate tx long term buffer\n");
48110c2aba8SNick Child return -ENOMEM;
48210c2aba8SNick Child }
48310c2aba8SNick Child adapter->tx_ltb_dma[idx] = dma_map_single(&adapter->vdev->dev,
48410c2aba8SNick Child adapter->tx_ltb_ptr[idx],
48510c2aba8SNick Child adapter->tx_ltb_size,
48610c2aba8SNick Child DMA_TO_DEVICE);
48710c2aba8SNick Child if (dma_mapping_error(&adapter->vdev->dev, adapter->tx_ltb_dma[idx])) {
48810c2aba8SNick Child netdev_err(adapter->netdev,
48910c2aba8SNick Child "unable to DMA map tx long term buffer\n");
49010c2aba8SNick Child kfree(adapter->tx_ltb_ptr[idx]);
49110c2aba8SNick Child adapter->tx_ltb_ptr[idx] = NULL;
49210c2aba8SNick Child return -ENOMEM;
49310c2aba8SNick Child }
49410c2aba8SNick Child
49510c2aba8SNick Child return 0;
49610c2aba8SNick Child }
49710c2aba8SNick Child
ibmveth_register_logical_lan(struct ibmveth_adapter * adapter,union ibmveth_buf_desc rxq_desc,u64 mac_address)4989aa32835SJeff Kirsher static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
4999aa32835SJeff Kirsher union ibmveth_buf_desc rxq_desc, u64 mac_address)
5009aa32835SJeff Kirsher {
5019aa32835SJeff Kirsher int rc, try_again = 1;
5029aa32835SJeff Kirsher
5039aa32835SJeff Kirsher /*
5049aa32835SJeff Kirsher * After a kexec the adapter will still be open, so our attempt to
5059aa32835SJeff Kirsher * open it will fail. So if we get a failure we free the adapter and
5069aa32835SJeff Kirsher * try again, but only once.
5079aa32835SJeff Kirsher */
5089aa32835SJeff Kirsher retry:
5099aa32835SJeff Kirsher rc = h_register_logical_lan(adapter->vdev->unit_address,
5109aa32835SJeff Kirsher adapter->buffer_list_dma, rxq_desc.desc,
5119aa32835SJeff Kirsher adapter->filter_list_dma, mac_address);
5129aa32835SJeff Kirsher
5139aa32835SJeff Kirsher if (rc != H_SUCCESS && try_again) {
5149aa32835SJeff Kirsher do {
5159aa32835SJeff Kirsher rc = h_free_logical_lan(adapter->vdev->unit_address);
5169aa32835SJeff Kirsher } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
5179aa32835SJeff Kirsher
5189aa32835SJeff Kirsher try_again = 0;
5199aa32835SJeff Kirsher goto retry;
5209aa32835SJeff Kirsher }
5219aa32835SJeff Kirsher
5229aa32835SJeff Kirsher return rc;
5239aa32835SJeff Kirsher }
5249aa32835SJeff Kirsher
ibmveth_open(struct net_device * netdev)5259aa32835SJeff Kirsher static int ibmveth_open(struct net_device *netdev)
5269aa32835SJeff Kirsher {
5279aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
528d746ca95SAnton Blanchard u64 mac_address;
5299aa32835SJeff Kirsher int rxq_entries = 1;
5309aa32835SJeff Kirsher unsigned long lpar_rc;
5319aa32835SJeff Kirsher int rc;
5329aa32835SJeff Kirsher union ibmveth_buf_desc rxq_desc;
5339aa32835SJeff Kirsher int i;
5349aa32835SJeff Kirsher struct device *dev;
5359aa32835SJeff Kirsher
5369aa32835SJeff Kirsher netdev_dbg(netdev, "open starting\n");
5379aa32835SJeff Kirsher
5389aa32835SJeff Kirsher napi_enable(&adapter->napi);
5399aa32835SJeff Kirsher
5409aa32835SJeff Kirsher for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
5419aa32835SJeff Kirsher rxq_entries += adapter->rx_buff_pool[i].size;
5429aa32835SJeff Kirsher
5439aa32835SJeff Kirsher rc = -ENOMEM;
544d43732ceSChristoph Hellwig adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
545d43732ceSChristoph Hellwig if (!adapter->buffer_list_addr) {
546d43732ceSChristoph Hellwig netdev_err(netdev, "unable to allocate list pages\n");
547d43732ceSChristoph Hellwig goto out;
548d43732ceSChristoph Hellwig }
549d43732ceSChristoph Hellwig
550d43732ceSChristoph Hellwig adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
551d43732ceSChristoph Hellwig if (!adapter->filter_list_addr) {
552d43732ceSChristoph Hellwig netdev_err(netdev, "unable to allocate filter pages\n");
553d43732ceSChristoph Hellwig goto out_free_buffer_list;
5549aa32835SJeff Kirsher }
5559aa32835SJeff Kirsher
556d90c92feSSantiago Leon dev = &adapter->vdev->dev;
557d90c92feSSantiago Leon
5589aa32835SJeff Kirsher adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
5599aa32835SJeff Kirsher rxq_entries;
560d90c92feSSantiago Leon adapter->rx_queue.queue_addr =
561d90c92feSSantiago Leon dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
562d90c92feSSantiago Leon &adapter->rx_queue.queue_dma, GFP_KERNEL);
563d43732ceSChristoph Hellwig if (!adapter->rx_queue.queue_addr)
564d43732ceSChristoph Hellwig goto out_free_filter_list;
5659aa32835SJeff Kirsher
5669aa32835SJeff Kirsher adapter->buffer_list_dma = dma_map_single(dev,
5679aa32835SJeff Kirsher adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
568d43732ceSChristoph Hellwig if (dma_mapping_error(dev, adapter->buffer_list_dma)) {
569d43732ceSChristoph Hellwig netdev_err(netdev, "unable to map buffer list pages\n");
570d43732ceSChristoph Hellwig goto out_free_queue_mem;
571d43732ceSChristoph Hellwig }
572d43732ceSChristoph Hellwig
5739aa32835SJeff Kirsher adapter->filter_list_dma = dma_map_single(dev,
5749aa32835SJeff Kirsher adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
575d43732ceSChristoph Hellwig if (dma_mapping_error(dev, adapter->filter_list_dma)) {
576d43732ceSChristoph Hellwig netdev_err(netdev, "unable to map filter list pages\n");
577d43732ceSChristoph Hellwig goto out_unmap_buffer_list;
5789aa32835SJeff Kirsher }
5799aa32835SJeff Kirsher
58010c2aba8SNick Child for (i = 0; i < netdev->real_num_tx_queues; i++) {
58110c2aba8SNick Child if (ibmveth_allocate_tx_ltb(adapter, i))
58210c2aba8SNick Child goto out_free_tx_ltb;
583d926793cSNick Child }
584d6832ca4SNick Child
5859aa32835SJeff Kirsher adapter->rx_queue.index = 0;
5869aa32835SJeff Kirsher adapter->rx_queue.num_slots = rxq_entries;
5879aa32835SJeff Kirsher adapter->rx_queue.toggle = 1;
5889aa32835SJeff Kirsher
5895c8b3485SJakub Kicinski mac_address = ether_addr_to_u64(netdev->dev_addr);
5909aa32835SJeff Kirsher
5919aa32835SJeff Kirsher rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
5929aa32835SJeff Kirsher adapter->rx_queue.queue_len;
5939aa32835SJeff Kirsher rxq_desc.fields.address = adapter->rx_queue.queue_dma;
5949aa32835SJeff Kirsher
5959aa32835SJeff Kirsher netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
5969aa32835SJeff Kirsher netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
5979aa32835SJeff Kirsher netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
5989aa32835SJeff Kirsher
5999aa32835SJeff Kirsher h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
6009aa32835SJeff Kirsher
6019aa32835SJeff Kirsher lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
6029aa32835SJeff Kirsher
6039aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
6049aa32835SJeff Kirsher netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
6059aa32835SJeff Kirsher lpar_rc);
6069aa32835SJeff Kirsher netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
6079aa32835SJeff Kirsher "desc:0x%llx MAC:0x%llx\n",
6089aa32835SJeff Kirsher adapter->buffer_list_dma,
6099aa32835SJeff Kirsher adapter->filter_list_dma,
6109aa32835SJeff Kirsher rxq_desc.desc,
6119aa32835SJeff Kirsher mac_address);
6129aa32835SJeff Kirsher rc = -ENONET;
613d43732ceSChristoph Hellwig goto out_unmap_filter_list;
6149aa32835SJeff Kirsher }
6159aa32835SJeff Kirsher
6169aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
6179aa32835SJeff Kirsher if (!adapter->rx_buff_pool[i].active)
6189aa32835SJeff Kirsher continue;
6199aa32835SJeff Kirsher if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
6209aa32835SJeff Kirsher netdev_err(netdev, "unable to alloc pool\n");
6219aa32835SJeff Kirsher adapter->rx_buff_pool[i].active = 0;
6229aa32835SJeff Kirsher rc = -ENOMEM;
623d43732ceSChristoph Hellwig goto out_free_buffer_pools;
6249aa32835SJeff Kirsher }
6259aa32835SJeff Kirsher }
6269aa32835SJeff Kirsher
6279aa32835SJeff Kirsher netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
6289aa32835SJeff Kirsher rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
6299aa32835SJeff Kirsher netdev);
6309aa32835SJeff Kirsher if (rc != 0) {
6319aa32835SJeff Kirsher netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
6329aa32835SJeff Kirsher netdev->irq, rc);
6339aa32835SJeff Kirsher do {
63488c5100cSDavid S. Miller lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
63588c5100cSDavid S. Miller } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
6369aa32835SJeff Kirsher
637d43732ceSChristoph Hellwig goto out_free_buffer_pools;
6389aa32835SJeff Kirsher }
6399aa32835SJeff Kirsher
640d43732ceSChristoph Hellwig rc = -ENOMEM;
641d43732ceSChristoph Hellwig
6429aa32835SJeff Kirsher netdev_dbg(netdev, "initial replenish cycle\n");
6439aa32835SJeff Kirsher ibmveth_interrupt(netdev->irq, netdev);
6449aa32835SJeff Kirsher
645d926793cSNick Child netif_tx_start_all_queues(netdev);
6469aa32835SJeff Kirsher
6479aa32835SJeff Kirsher netdev_dbg(netdev, "open complete\n");
6489aa32835SJeff Kirsher
6499aa32835SJeff Kirsher return 0;
6509aa32835SJeff Kirsher
651d43732ceSChristoph Hellwig out_free_buffer_pools:
652d43732ceSChristoph Hellwig while (--i >= 0) {
653d43732ceSChristoph Hellwig if (adapter->rx_buff_pool[i].active)
654d43732ceSChristoph Hellwig ibmveth_free_buffer_pool(adapter,
655d43732ceSChristoph Hellwig &adapter->rx_buff_pool[i]);
656d43732ceSChristoph Hellwig }
657d43732ceSChristoph Hellwig out_unmap_filter_list:
658d43732ceSChristoph Hellwig dma_unmap_single(dev, adapter->filter_list_dma, 4096,
659d43732ceSChristoph Hellwig DMA_BIDIRECTIONAL);
660d6832ca4SNick Child
66110c2aba8SNick Child out_free_tx_ltb:
662d926793cSNick Child while (--i >= 0) {
66310c2aba8SNick Child ibmveth_free_tx_ltb(adapter, i);
664d926793cSNick Child }
665d6832ca4SNick Child
666d43732ceSChristoph Hellwig out_unmap_buffer_list:
667d43732ceSChristoph Hellwig dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
668d43732ceSChristoph Hellwig DMA_BIDIRECTIONAL);
669d43732ceSChristoph Hellwig out_free_queue_mem:
670d43732ceSChristoph Hellwig dma_free_coherent(dev, adapter->rx_queue.queue_len,
671d43732ceSChristoph Hellwig adapter->rx_queue.queue_addr,
672d43732ceSChristoph Hellwig adapter->rx_queue.queue_dma);
673d43732ceSChristoph Hellwig out_free_filter_list:
674d43732ceSChristoph Hellwig free_page((unsigned long)adapter->filter_list_addr);
675d43732ceSChristoph Hellwig out_free_buffer_list:
676d43732ceSChristoph Hellwig free_page((unsigned long)adapter->buffer_list_addr);
677d43732ceSChristoph Hellwig out:
6789aa32835SJeff Kirsher napi_disable(&adapter->napi);
6799aa32835SJeff Kirsher return rc;
6809aa32835SJeff Kirsher }
6819aa32835SJeff Kirsher
ibmveth_close(struct net_device * netdev)6829aa32835SJeff Kirsher static int ibmveth_close(struct net_device *netdev)
6839aa32835SJeff Kirsher {
6849aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
685d43732ceSChristoph Hellwig struct device *dev = &adapter->vdev->dev;
6869aa32835SJeff Kirsher long lpar_rc;
687d43732ceSChristoph Hellwig int i;
6889aa32835SJeff Kirsher
6899aa32835SJeff Kirsher netdev_dbg(netdev, "close starting\n");
6909aa32835SJeff Kirsher
6919aa32835SJeff Kirsher napi_disable(&adapter->napi);
6929aa32835SJeff Kirsher
693d926793cSNick Child netif_tx_stop_all_queues(netdev);
6949aa32835SJeff Kirsher
6959aa32835SJeff Kirsher h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
6969aa32835SJeff Kirsher
6979aa32835SJeff Kirsher do {
6989aa32835SJeff Kirsher lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
6999aa32835SJeff Kirsher } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
7009aa32835SJeff Kirsher
7019aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
7029aa32835SJeff Kirsher netdev_err(netdev, "h_free_logical_lan failed with %lx, "
7039aa32835SJeff Kirsher "continuing with close\n", lpar_rc);
7049aa32835SJeff Kirsher }
7059aa32835SJeff Kirsher
7069aa32835SJeff Kirsher free_irq(netdev->irq, netdev);
7079aa32835SJeff Kirsher
708cbd52281SAnton Blanchard ibmveth_update_rx_no_buffer(adapter);
7099aa32835SJeff Kirsher
710d43732ceSChristoph Hellwig dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
711d43732ceSChristoph Hellwig DMA_BIDIRECTIONAL);
712d43732ceSChristoph Hellwig free_page((unsigned long)adapter->buffer_list_addr);
713d43732ceSChristoph Hellwig
714d43732ceSChristoph Hellwig dma_unmap_single(dev, adapter->filter_list_dma, 4096,
715d43732ceSChristoph Hellwig DMA_BIDIRECTIONAL);
716d43732ceSChristoph Hellwig free_page((unsigned long)adapter->filter_list_addr);
717d43732ceSChristoph Hellwig
718d43732ceSChristoph Hellwig dma_free_coherent(dev, adapter->rx_queue.queue_len,
719d43732ceSChristoph Hellwig adapter->rx_queue.queue_addr,
720d43732ceSChristoph Hellwig adapter->rx_queue.queue_dma);
721d43732ceSChristoph Hellwig
722d43732ceSChristoph Hellwig for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
723d43732ceSChristoph Hellwig if (adapter->rx_buff_pool[i].active)
724d43732ceSChristoph Hellwig ibmveth_free_buffer_pool(adapter,
725d43732ceSChristoph Hellwig &adapter->rx_buff_pool[i]);
726d43732ceSChristoph Hellwig
72710c2aba8SNick Child for (i = 0; i < netdev->real_num_tx_queues; i++)
72810c2aba8SNick Child ibmveth_free_tx_ltb(adapter, i);
7299aa32835SJeff Kirsher
7309aa32835SJeff Kirsher netdev_dbg(netdev, "close complete\n");
7319aa32835SJeff Kirsher
7329aa32835SJeff Kirsher return 0;
7339aa32835SJeff Kirsher }
7349aa32835SJeff Kirsher
ibmveth_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)7359aedc6e2SCris Forno static int ibmveth_set_link_ksettings(struct net_device *dev,
7369aedc6e2SCris Forno const struct ethtool_link_ksettings *cmd)
7379aedc6e2SCris Forno {
7389aedc6e2SCris Forno struct ibmveth_adapter *adapter = netdev_priv(dev);
7399aedc6e2SCris Forno
7409aedc6e2SCris Forno return ethtool_virtdev_set_link_ksettings(dev, cmd,
7419aedc6e2SCris Forno &adapter->speed,
7429aedc6e2SCris Forno &adapter->duplex);
7439aedc6e2SCris Forno }
7449aedc6e2SCris Forno
ibmveth_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)7459aedc6e2SCris Forno static int ibmveth_get_link_ksettings(struct net_device *dev,
7469ce8c2dfSPhilippe Reynes struct ethtool_link_ksettings *cmd)
7479aa32835SJeff Kirsher {
7489aedc6e2SCris Forno struct ibmveth_adapter *adapter = netdev_priv(dev);
7499ce8c2dfSPhilippe Reynes
7509aedc6e2SCris Forno cmd->base.speed = adapter->speed;
7519aedc6e2SCris Forno cmd->base.duplex = adapter->duplex;
7529aedc6e2SCris Forno cmd->base.port = PORT_OTHER;
7539ce8c2dfSPhilippe Reynes
7549aa32835SJeff Kirsher return 0;
7559aa32835SJeff Kirsher }
7569aa32835SJeff Kirsher
ibmveth_init_link_settings(struct net_device * dev)7579aedc6e2SCris Forno static void ibmveth_init_link_settings(struct net_device *dev)
7589aedc6e2SCris Forno {
7599aedc6e2SCris Forno struct ibmveth_adapter *adapter = netdev_priv(dev);
7609aedc6e2SCris Forno
7619aedc6e2SCris Forno adapter->speed = SPEED_1000;
7629aedc6e2SCris Forno adapter->duplex = DUPLEX_FULL;
7639aedc6e2SCris Forno }
7649aedc6e2SCris Forno
netdev_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)7659aa32835SJeff Kirsher static void netdev_get_drvinfo(struct net_device *dev,
7669aa32835SJeff Kirsher struct ethtool_drvinfo *info)
7679aa32835SJeff Kirsher {
768f029c781SWolfram Sang strscpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
769f029c781SWolfram Sang strscpy(info->version, ibmveth_driver_version, sizeof(info->version));
7709aa32835SJeff Kirsher }
7719aa32835SJeff Kirsher
ibmveth_fix_features(struct net_device * dev,netdev_features_t features)772c8f44affSMichał Mirosław static netdev_features_t ibmveth_fix_features(struct net_device *dev,
773c8f44affSMichał Mirosław netdev_features_t features)
7749aa32835SJeff Kirsher {
7759aa32835SJeff Kirsher /*
7769aa32835SJeff Kirsher * Since the ibmveth firmware interface does not have the
7779aa32835SJeff Kirsher * concept of separate tx/rx checksum offload enable, if rx
7789aa32835SJeff Kirsher * checksum is disabled we also have to disable tx checksum
7799aa32835SJeff Kirsher * offload. Once we disable rx checksum offload, we are no
7809aa32835SJeff Kirsher * longer allowed to send tx buffers that are not properly
7819aa32835SJeff Kirsher * checksummed.
7829aa32835SJeff Kirsher */
7839aa32835SJeff Kirsher
7849aa32835SJeff Kirsher if (!(features & NETIF_F_RXCSUM))
785a188222bSTom Herbert features &= ~NETIF_F_CSUM_MASK;
7869aa32835SJeff Kirsher
7879aa32835SJeff Kirsher return features;
7889aa32835SJeff Kirsher }
7899aa32835SJeff Kirsher
ibmveth_set_csum_offload(struct net_device * dev,u32 data)7909aa32835SJeff Kirsher static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
7919aa32835SJeff Kirsher {
7929aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(dev);
7939aa32835SJeff Kirsher unsigned long set_attr, clr_attr, ret_attr;
7949aa32835SJeff Kirsher unsigned long set_attr6, clr_attr6;
7958decf868SDavid S. Miller long ret, ret4, ret6;
7969aa32835SJeff Kirsher int rc1 = 0, rc2 = 0;
7979aa32835SJeff Kirsher int restart = 0;
7989aa32835SJeff Kirsher
7999aa32835SJeff Kirsher if (netif_running(dev)) {
8009aa32835SJeff Kirsher restart = 1;
8019aa32835SJeff Kirsher ibmveth_close(dev);
8029aa32835SJeff Kirsher }
8039aa32835SJeff Kirsher
8049aa32835SJeff Kirsher set_attr = 0;
8059aa32835SJeff Kirsher clr_attr = 0;
8068decf868SDavid S. Miller set_attr6 = 0;
8078decf868SDavid S. Miller clr_attr6 = 0;
8089aa32835SJeff Kirsher
8099aa32835SJeff Kirsher if (data) {
8109aa32835SJeff Kirsher set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
8119aa32835SJeff Kirsher set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
8129aa32835SJeff Kirsher } else {
8139aa32835SJeff Kirsher clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
8149aa32835SJeff Kirsher clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
8159aa32835SJeff Kirsher }
8169aa32835SJeff Kirsher
8179aa32835SJeff Kirsher ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
8189aa32835SJeff Kirsher
81966aa0678SSivakumar Krishnasamy if (ret == H_SUCCESS &&
8209aa32835SJeff Kirsher (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
8218decf868SDavid S. Miller ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
8229aa32835SJeff Kirsher set_attr, &ret_attr);
8239aa32835SJeff Kirsher
8248decf868SDavid S. Miller if (ret4 != H_SUCCESS) {
8259aa32835SJeff Kirsher netdev_err(dev, "unable to change IPv4 checksum "
8269aa32835SJeff Kirsher "offload settings. %d rc=%ld\n",
8278decf868SDavid S. Miller data, ret4);
8289aa32835SJeff Kirsher
8298decf868SDavid S. Miller h_illan_attributes(adapter->vdev->unit_address,
8309aa32835SJeff Kirsher set_attr, clr_attr, &ret_attr);
8318decf868SDavid S. Miller
8328decf868SDavid S. Miller if (data == 1)
8338decf868SDavid S. Miller dev->features &= ~NETIF_F_IP_CSUM;
8348decf868SDavid S. Miller
8359aa32835SJeff Kirsher } else {
8369aa32835SJeff Kirsher adapter->fw_ipv4_csum_support = data;
8379aa32835SJeff Kirsher }
8389aa32835SJeff Kirsher
8399aa32835SJeff Kirsher ret6 = h_illan_attributes(adapter->vdev->unit_address,
8409aa32835SJeff Kirsher clr_attr6, set_attr6, &ret_attr);
8419aa32835SJeff Kirsher
8429aa32835SJeff Kirsher if (ret6 != H_SUCCESS) {
8439aa32835SJeff Kirsher netdev_err(dev, "unable to change IPv6 checksum "
8449aa32835SJeff Kirsher "offload settings. %d rc=%ld\n",
8458decf868SDavid S. Miller data, ret6);
8469aa32835SJeff Kirsher
8478decf868SDavid S. Miller h_illan_attributes(adapter->vdev->unit_address,
8488decf868SDavid S. Miller set_attr6, clr_attr6, &ret_attr);
8498decf868SDavid S. Miller
8508decf868SDavid S. Miller if (data == 1)
8518decf868SDavid S. Miller dev->features &= ~NETIF_F_IPV6_CSUM;
8528decf868SDavid S. Miller
8539aa32835SJeff Kirsher } else
8549aa32835SJeff Kirsher adapter->fw_ipv6_csum_support = data;
8559aa32835SJeff Kirsher
8568decf868SDavid S. Miller if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
8579aa32835SJeff Kirsher adapter->rx_csum = data;
8589aa32835SJeff Kirsher else
8599aa32835SJeff Kirsher rc1 = -EIO;
8609aa32835SJeff Kirsher } else {
8619aa32835SJeff Kirsher rc1 = -EIO;
8629aa32835SJeff Kirsher netdev_err(dev, "unable to change checksum offload settings."
8639aa32835SJeff Kirsher " %d rc=%ld ret_attr=%lx\n", data, ret,
8649aa32835SJeff Kirsher ret_attr);
8659aa32835SJeff Kirsher }
8669aa32835SJeff Kirsher
8679aa32835SJeff Kirsher if (restart)
8689aa32835SJeff Kirsher rc2 = ibmveth_open(dev);
8699aa32835SJeff Kirsher
8709aa32835SJeff Kirsher return rc1 ? rc1 : rc2;
8719aa32835SJeff Kirsher }
8729aa32835SJeff Kirsher
ibmveth_set_tso(struct net_device * dev,u32 data)87307e6a97dSThomas Falcon static int ibmveth_set_tso(struct net_device *dev, u32 data)
87407e6a97dSThomas Falcon {
87507e6a97dSThomas Falcon struct ibmveth_adapter *adapter = netdev_priv(dev);
87607e6a97dSThomas Falcon unsigned long set_attr, clr_attr, ret_attr;
87707e6a97dSThomas Falcon long ret1, ret2;
87807e6a97dSThomas Falcon int rc1 = 0, rc2 = 0;
87907e6a97dSThomas Falcon int restart = 0;
88007e6a97dSThomas Falcon
88107e6a97dSThomas Falcon if (netif_running(dev)) {
88207e6a97dSThomas Falcon restart = 1;
88307e6a97dSThomas Falcon ibmveth_close(dev);
88407e6a97dSThomas Falcon }
88507e6a97dSThomas Falcon
88607e6a97dSThomas Falcon set_attr = 0;
88707e6a97dSThomas Falcon clr_attr = 0;
88807e6a97dSThomas Falcon
88907e6a97dSThomas Falcon if (data)
89007e6a97dSThomas Falcon set_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
89107e6a97dSThomas Falcon else
89207e6a97dSThomas Falcon clr_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
89307e6a97dSThomas Falcon
89407e6a97dSThomas Falcon ret1 = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
89507e6a97dSThomas Falcon
89607e6a97dSThomas Falcon if (ret1 == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
89707e6a97dSThomas Falcon !old_large_send) {
89807e6a97dSThomas Falcon ret2 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
89907e6a97dSThomas Falcon set_attr, &ret_attr);
90007e6a97dSThomas Falcon
90107e6a97dSThomas Falcon if (ret2 != H_SUCCESS) {
90207e6a97dSThomas Falcon netdev_err(dev, "unable to change tso settings. %d rc=%ld\n",
90307e6a97dSThomas Falcon data, ret2);
90407e6a97dSThomas Falcon
90507e6a97dSThomas Falcon h_illan_attributes(adapter->vdev->unit_address,
90607e6a97dSThomas Falcon set_attr, clr_attr, &ret_attr);
90707e6a97dSThomas Falcon
90807e6a97dSThomas Falcon if (data == 1)
90907e6a97dSThomas Falcon dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
91007e6a97dSThomas Falcon rc1 = -EIO;
91107e6a97dSThomas Falcon
91207e6a97dSThomas Falcon } else {
91307e6a97dSThomas Falcon adapter->fw_large_send_support = data;
91407e6a97dSThomas Falcon adapter->large_send = data;
91507e6a97dSThomas Falcon }
91607e6a97dSThomas Falcon } else {
91707e6a97dSThomas Falcon /* Older firmware version of large send offload does not
91807e6a97dSThomas Falcon * support tcp6/ipv6
91907e6a97dSThomas Falcon */
92007e6a97dSThomas Falcon if (data == 1) {
92107e6a97dSThomas Falcon dev->features &= ~NETIF_F_TSO6;
92207e6a97dSThomas Falcon netdev_info(dev, "TSO feature requires all partitions to have updated driver");
92307e6a97dSThomas Falcon }
92407e6a97dSThomas Falcon adapter->large_send = data;
92507e6a97dSThomas Falcon }
92607e6a97dSThomas Falcon
92707e6a97dSThomas Falcon if (restart)
92807e6a97dSThomas Falcon rc2 = ibmveth_open(dev);
92907e6a97dSThomas Falcon
93007e6a97dSThomas Falcon return rc1 ? rc1 : rc2;
93107e6a97dSThomas Falcon }
93207e6a97dSThomas Falcon
ibmveth_set_features(struct net_device * dev,netdev_features_t features)933c8f44affSMichał Mirosław static int ibmveth_set_features(struct net_device *dev,
934c8f44affSMichał Mirosław netdev_features_t features)
9359aa32835SJeff Kirsher {
9369aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(dev);
9379aa32835SJeff Kirsher int rx_csum = !!(features & NETIF_F_RXCSUM);
93807e6a97dSThomas Falcon int large_send = !!(features & (NETIF_F_TSO | NETIF_F_TSO6));
93907e6a97dSThomas Falcon int rc1 = 0, rc2 = 0;
9408641dd85SThomas Falcon
94107e6a97dSThomas Falcon if (rx_csum != adapter->rx_csum) {
94207e6a97dSThomas Falcon rc1 = ibmveth_set_csum_offload(dev, rx_csum);
94307e6a97dSThomas Falcon if (rc1 && !adapter->rx_csum)
94407e6a97dSThomas Falcon dev->features =
945a188222bSTom Herbert features & ~(NETIF_F_CSUM_MASK |
946a188222bSTom Herbert NETIF_F_RXCSUM);
94707e6a97dSThomas Falcon }
9489aa32835SJeff Kirsher
94907e6a97dSThomas Falcon if (large_send != adapter->large_send) {
95007e6a97dSThomas Falcon rc2 = ibmveth_set_tso(dev, large_send);
95107e6a97dSThomas Falcon if (rc2 && !adapter->large_send)
95207e6a97dSThomas Falcon dev->features =
95307e6a97dSThomas Falcon features & ~(NETIF_F_TSO | NETIF_F_TSO6);
95407e6a97dSThomas Falcon }
9559aa32835SJeff Kirsher
95607e6a97dSThomas Falcon return rc1 ? rc1 : rc2;
9579aa32835SJeff Kirsher }
9589aa32835SJeff Kirsher
ibmveth_get_strings(struct net_device * dev,u32 stringset,u8 * data)9599aa32835SJeff Kirsher static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
9609aa32835SJeff Kirsher {
9619aa32835SJeff Kirsher int i;
9629aa32835SJeff Kirsher
9639aa32835SJeff Kirsher if (stringset != ETH_SS_STATS)
9649aa32835SJeff Kirsher return;
9659aa32835SJeff Kirsher
9669aa32835SJeff Kirsher for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
9679aa32835SJeff Kirsher memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
9689aa32835SJeff Kirsher }
9699aa32835SJeff Kirsher
ibmveth_get_sset_count(struct net_device * dev,int sset)9709aa32835SJeff Kirsher static int ibmveth_get_sset_count(struct net_device *dev, int sset)
9719aa32835SJeff Kirsher {
9729aa32835SJeff Kirsher switch (sset) {
9739aa32835SJeff Kirsher case ETH_SS_STATS:
9749aa32835SJeff Kirsher return ARRAY_SIZE(ibmveth_stats);
9759aa32835SJeff Kirsher default:
9769aa32835SJeff Kirsher return -EOPNOTSUPP;
9779aa32835SJeff Kirsher }
9789aa32835SJeff Kirsher }
9799aa32835SJeff Kirsher
ibmveth_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)9809aa32835SJeff Kirsher static void ibmveth_get_ethtool_stats(struct net_device *dev,
9819aa32835SJeff Kirsher struct ethtool_stats *stats, u64 *data)
9829aa32835SJeff Kirsher {
9839aa32835SJeff Kirsher int i;
9849aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(dev);
9859aa32835SJeff Kirsher
9869aa32835SJeff Kirsher for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
9879aa32835SJeff Kirsher data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
9889aa32835SJeff Kirsher }
9899aa32835SJeff Kirsher
ibmveth_get_channels(struct net_device * netdev,struct ethtool_channels * channels)99010c2aba8SNick Child static void ibmveth_get_channels(struct net_device *netdev,
99110c2aba8SNick Child struct ethtool_channels *channels)
99210c2aba8SNick Child {
99310c2aba8SNick Child channels->max_tx = ibmveth_real_max_tx_queues();
99410c2aba8SNick Child channels->tx_count = netdev->real_num_tx_queues;
99510c2aba8SNick Child
99610c2aba8SNick Child channels->max_rx = netdev->real_num_rx_queues;
99710c2aba8SNick Child channels->rx_count = netdev->real_num_rx_queues;
99810c2aba8SNick Child }
99910c2aba8SNick Child
ibmveth_set_channels(struct net_device * netdev,struct ethtool_channels * channels)100010c2aba8SNick Child static int ibmveth_set_channels(struct net_device *netdev,
100110c2aba8SNick Child struct ethtool_channels *channels)
100210c2aba8SNick Child {
100310c2aba8SNick Child struct ibmveth_adapter *adapter = netdev_priv(netdev);
100410c2aba8SNick Child unsigned int old = netdev->real_num_tx_queues,
100510c2aba8SNick Child goal = channels->tx_count;
100610c2aba8SNick Child int rc, i;
100710c2aba8SNick Child
100810c2aba8SNick Child /* If ndo_open has not been called yet then don't allocate, just set
100910c2aba8SNick Child * desired netdev_queue's and return
101010c2aba8SNick Child */
101110c2aba8SNick Child if (!(netdev->flags & IFF_UP))
101210c2aba8SNick Child return netif_set_real_num_tx_queues(netdev, goal);
101310c2aba8SNick Child
101410c2aba8SNick Child /* We have IBMVETH_MAX_QUEUES netdev_queue's allocated
101510c2aba8SNick Child * but we may need to alloc/free the ltb's.
101610c2aba8SNick Child */
101710c2aba8SNick Child netif_tx_stop_all_queues(netdev);
101810c2aba8SNick Child
101910c2aba8SNick Child /* Allocate any queue that we need */
102010c2aba8SNick Child for (i = old; i < goal; i++) {
102110c2aba8SNick Child if (adapter->tx_ltb_ptr[i])
102210c2aba8SNick Child continue;
102310c2aba8SNick Child
102410c2aba8SNick Child rc = ibmveth_allocate_tx_ltb(adapter, i);
102510c2aba8SNick Child if (!rc)
102610c2aba8SNick Child continue;
102710c2aba8SNick Child
102810c2aba8SNick Child /* if something goes wrong, free everything we just allocated */
102910c2aba8SNick Child netdev_err(netdev, "Failed to allocate more tx queues, returning to %d queues\n",
103010c2aba8SNick Child old);
103110c2aba8SNick Child goal = old;
103210c2aba8SNick Child old = i;
103310c2aba8SNick Child break;
103410c2aba8SNick Child }
103510c2aba8SNick Child rc = netif_set_real_num_tx_queues(netdev, goal);
103610c2aba8SNick Child if (rc) {
103710c2aba8SNick Child netdev_err(netdev, "Failed to set real tx queues, returning to %d queues\n",
103810c2aba8SNick Child old);
103910c2aba8SNick Child goal = old;
104010c2aba8SNick Child old = i;
104110c2aba8SNick Child }
104210c2aba8SNick Child /* Free any that are no longer needed */
104310c2aba8SNick Child for (i = old; i > goal; i--) {
104410c2aba8SNick Child if (adapter->tx_ltb_ptr[i - 1])
104510c2aba8SNick Child ibmveth_free_tx_ltb(adapter, i - 1);
104610c2aba8SNick Child }
104710c2aba8SNick Child
104810c2aba8SNick Child netif_tx_wake_all_queues(netdev);
104910c2aba8SNick Child
105010c2aba8SNick Child return rc;
105110c2aba8SNick Child }
105210c2aba8SNick Child
10539aa32835SJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops = {
10549aa32835SJeff Kirsher .get_drvinfo = netdev_get_drvinfo,
10559aa32835SJeff Kirsher .get_link = ethtool_op_get_link,
10569aa32835SJeff Kirsher .get_strings = ibmveth_get_strings,
10579aa32835SJeff Kirsher .get_sset_count = ibmveth_get_sset_count,
10589aa32835SJeff Kirsher .get_ethtool_stats = ibmveth_get_ethtool_stats,
10599aedc6e2SCris Forno .get_link_ksettings = ibmveth_get_link_ksettings,
10609aedc6e2SCris Forno .set_link_ksettings = ibmveth_set_link_ksettings,
106110c2aba8SNick Child .get_channels = ibmveth_get_channels,
106210c2aba8SNick Child .set_channels = ibmveth_set_channels
10639aa32835SJeff Kirsher };
10649aa32835SJeff Kirsher
ibmveth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)10659aa32835SJeff Kirsher static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10669aa32835SJeff Kirsher {
10679aa32835SJeff Kirsher return -EOPNOTSUPP;
10689aa32835SJeff Kirsher }
10699aa32835SJeff Kirsher
ibmveth_send(struct ibmveth_adapter * adapter,unsigned long desc,unsigned long mss)10709aa32835SJeff Kirsher static int ibmveth_send(struct ibmveth_adapter *adapter,
1071d6832ca4SNick Child unsigned long desc, unsigned long mss)
10729aa32835SJeff Kirsher {
10739aa32835SJeff Kirsher unsigned long correlator;
10749aa32835SJeff Kirsher unsigned int retry_count;
10759aa32835SJeff Kirsher unsigned long ret;
10769aa32835SJeff Kirsher
10779aa32835SJeff Kirsher /*
10789aa32835SJeff Kirsher * The retry count sets a maximum for the number of broadcast and
10799aa32835SJeff Kirsher * multicast destinations within the system.
10809aa32835SJeff Kirsher */
10819aa32835SJeff Kirsher retry_count = 1024;
10829aa32835SJeff Kirsher correlator = 0;
10839aa32835SJeff Kirsher do {
1084d6832ca4SNick Child ret = h_send_logical_lan(adapter->vdev->unit_address, desc,
108507e6a97dSThomas Falcon correlator, &correlator, mss,
108607e6a97dSThomas Falcon adapter->fw_large_send_support);
10879aa32835SJeff Kirsher } while ((ret == H_BUSY) && (retry_count--));
10889aa32835SJeff Kirsher
10899aa32835SJeff Kirsher if (ret != H_SUCCESS && ret != H_DROPPED) {
10909aa32835SJeff Kirsher netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
10919aa32835SJeff Kirsher "with rc=%ld\n", ret);
10929aa32835SJeff Kirsher return 1;
10939aa32835SJeff Kirsher }
10949aa32835SJeff Kirsher
10959aa32835SJeff Kirsher return 0;
10969aa32835SJeff Kirsher }
10979aa32835SJeff Kirsher
ibmveth_is_packet_unsupported(struct sk_buff * skb,struct net_device * netdev)10986f227543SCris Forno static int ibmveth_is_packet_unsupported(struct sk_buff *skb,
10996f227543SCris Forno struct net_device *netdev)
11006f227543SCris Forno {
11016f227543SCris Forno struct ethhdr *ether_header;
11026f227543SCris Forno int ret = 0;
11036f227543SCris Forno
11046f227543SCris Forno ether_header = eth_hdr(skb);
11056f227543SCris Forno
11066f227543SCris Forno if (ether_addr_equal(ether_header->h_dest, netdev->dev_addr)) {
11076f227543SCris Forno netdev_dbg(netdev, "veth doesn't support loopback packets, dropping packet.\n");
11086f227543SCris Forno netdev->stats.tx_dropped++;
11096f227543SCris Forno ret = -EOPNOTSUPP;
11106f227543SCris Forno }
11116f227543SCris Forno
11126f227543SCris Forno return ret;
11136f227543SCris Forno }
11146f227543SCris Forno
ibmveth_start_xmit(struct sk_buff * skb,struct net_device * netdev)11159aa32835SJeff Kirsher static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
11169aa32835SJeff Kirsher struct net_device *netdev)
11179aa32835SJeff Kirsher {
11189aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
1119d926793cSNick Child unsigned int desc_flags, total_bytes;
1120d6832ca4SNick Child union ibmveth_buf_desc desc;
1121d926793cSNick Child int i, queue_num = skb_get_queue_mapping(skb);
112207e6a97dSThomas Falcon unsigned long mss = 0;
11239aa32835SJeff Kirsher
11246f227543SCris Forno if (ibmveth_is_packet_unsupported(skb, netdev))
11256f227543SCris Forno goto out;
11269aa32835SJeff Kirsher /* veth can't checksum offload UDP */
11279aa32835SJeff Kirsher if (skb->ip_summed == CHECKSUM_PARTIAL &&
11289aa32835SJeff Kirsher ((skb->protocol == htons(ETH_P_IP) &&
11299aa32835SJeff Kirsher ip_hdr(skb)->protocol != IPPROTO_TCP) ||
11309aa32835SJeff Kirsher (skb->protocol == htons(ETH_P_IPV6) &&
11319aa32835SJeff Kirsher ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
11329aa32835SJeff Kirsher skb_checksum_help(skb)) {
11339aa32835SJeff Kirsher
11349aa32835SJeff Kirsher netdev_err(netdev, "tx: failed to checksum packet\n");
11359aa32835SJeff Kirsher netdev->stats.tx_dropped++;
11369aa32835SJeff Kirsher goto out;
11379aa32835SJeff Kirsher }
11389aa32835SJeff Kirsher
11399aa32835SJeff Kirsher desc_flags = IBMVETH_BUF_VALID;
11409aa32835SJeff Kirsher
11419aa32835SJeff Kirsher if (skb->ip_summed == CHECKSUM_PARTIAL) {
11429aa32835SJeff Kirsher unsigned char *buf = skb_transport_header(skb) +
11439aa32835SJeff Kirsher skb->csum_offset;
11449aa32835SJeff Kirsher
11459aa32835SJeff Kirsher desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
11469aa32835SJeff Kirsher
11479aa32835SJeff Kirsher /* Need to zero out the checksum */
11489aa32835SJeff Kirsher buf[0] = 0;
11499aa32835SJeff Kirsher buf[1] = 0;
115066aa0678SSivakumar Krishnasamy
115166aa0678SSivakumar Krishnasamy if (skb_is_gso(skb) && adapter->fw_large_send_support)
115266aa0678SSivakumar Krishnasamy desc_flags |= IBMVETH_BUF_LRG_SND;
11539aa32835SJeff Kirsher }
11549aa32835SJeff Kirsher
115566aa0678SSivakumar Krishnasamy if (skb->ip_summed == CHECKSUM_PARTIAL && skb_is_gso(skb)) {
115607e6a97dSThomas Falcon if (adapter->fw_large_send_support) {
115707e6a97dSThomas Falcon mss = (unsigned long)skb_shinfo(skb)->gso_size;
115807e6a97dSThomas Falcon adapter->tx_large_packets++;
115907e6a97dSThomas Falcon } else if (!skb_is_gso_v6(skb)) {
11608641dd85SThomas Falcon /* Put -1 in the IP checksum to tell phyp it
116107e6a97dSThomas Falcon * is a largesend packet. Put the mss in
116207e6a97dSThomas Falcon * the TCP checksum.
11638641dd85SThomas Falcon */
11648641dd85SThomas Falcon ip_hdr(skb)->check = 0xffff;
116507e6a97dSThomas Falcon tcp_hdr(skb)->check =
116607e6a97dSThomas Falcon cpu_to_be16(skb_shinfo(skb)->gso_size);
11678641dd85SThomas Falcon adapter->tx_large_packets++;
11688641dd85SThomas Falcon }
116907e6a97dSThomas Falcon }
11708641dd85SThomas Falcon
1171d6832ca4SNick Child /* Copy header into mapped buffer */
1172d6832ca4SNick Child if (unlikely(skb->len > adapter->tx_ltb_size)) {
1173d6832ca4SNick Child netdev_err(adapter->netdev, "tx: packet size (%u) exceeds ltb (%u)\n",
1174d6832ca4SNick Child skb->len, adapter->tx_ltb_size);
1175d6832ca4SNick Child netdev->stats.tx_dropped++;
1176d6832ca4SNick Child goto out;
1177d6832ca4SNick Child }
1178d926793cSNick Child memcpy(adapter->tx_ltb_ptr[queue_num], skb->data, skb_headlen(skb));
1179d6832ca4SNick Child total_bytes = skb_headlen(skb);
1180d6832ca4SNick Child /* Copy frags into mapped buffers */
1181d6832ca4SNick Child for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1182d6832ca4SNick Child const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1183d6832ca4SNick Child
1184d926793cSNick Child memcpy(adapter->tx_ltb_ptr[queue_num] + total_bytes,
1185d926793cSNick Child skb_frag_address_safe(frag), skb_frag_size(frag));
1186d6832ca4SNick Child total_bytes += skb_frag_size(frag);
1187d6832ca4SNick Child }
1188d6832ca4SNick Child
1189d6832ca4SNick Child if (unlikely(total_bytes != skb->len)) {
1190d6832ca4SNick Child netdev_err(adapter->netdev, "tx: incorrect packet len copied into ltb (%u != %u)\n",
1191d6832ca4SNick Child skb->len, total_bytes);
1192d6832ca4SNick Child netdev->stats.tx_dropped++;
1193d6832ca4SNick Child goto out;
1194d6832ca4SNick Child }
1195d6832ca4SNick Child desc.fields.flags_len = desc_flags | skb->len;
1196d926793cSNick Child desc.fields.address = adapter->tx_ltb_dma[queue_num];
1197d6832ca4SNick Child /* finish writing to long_term_buff before VIOS accessing it */
1198d6832ca4SNick Child dma_wmb();
1199d6832ca4SNick Child
1200d6832ca4SNick Child if (ibmveth_send(adapter, desc.desc, mss)) {
12019aa32835SJeff Kirsher adapter->tx_send_failed++;
12029aa32835SJeff Kirsher netdev->stats.tx_dropped++;
12039aa32835SJeff Kirsher } else {
12049aa32835SJeff Kirsher netdev->stats.tx_packets++;
12059aa32835SJeff Kirsher netdev->stats.tx_bytes += skb->len;
12069aa32835SJeff Kirsher }
12079aa32835SJeff Kirsher
12089aa32835SJeff Kirsher out:
120926faa9d7SEric W. Biederman dev_consume_skb_any(skb);
12109aa32835SJeff Kirsher return NETDEV_TX_OK;
12119aa32835SJeff Kirsher
12129aa32835SJeff Kirsher
12139aa32835SJeff Kirsher }
12149aa32835SJeff Kirsher
ibmveth_rx_mss_helper(struct sk_buff * skb,u16 mss,int lrg_pkt)12157b596738SThomas Falcon static void ibmveth_rx_mss_helper(struct sk_buff *skb, u16 mss, int lrg_pkt)
12167b596738SThomas Falcon {
121794acf164SThomas Falcon struct tcphdr *tcph;
12187b596738SThomas Falcon int offset = 0;
121994acf164SThomas Falcon int hdr_len;
12207b596738SThomas Falcon
12217b596738SThomas Falcon /* only TCP packets will be aggregated */
12227b596738SThomas Falcon if (skb->protocol == htons(ETH_P_IP)) {
12237b596738SThomas Falcon struct iphdr *iph = (struct iphdr *)skb->data;
12247b596738SThomas Falcon
12257b596738SThomas Falcon if (iph->protocol == IPPROTO_TCP) {
12267b596738SThomas Falcon offset = iph->ihl * 4;
12277b596738SThomas Falcon skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
12287b596738SThomas Falcon } else {
12297b596738SThomas Falcon return;
12307b596738SThomas Falcon }
12317b596738SThomas Falcon } else if (skb->protocol == htons(ETH_P_IPV6)) {
12327b596738SThomas Falcon struct ipv6hdr *iph6 = (struct ipv6hdr *)skb->data;
12337b596738SThomas Falcon
12347b596738SThomas Falcon if (iph6->nexthdr == IPPROTO_TCP) {
12357b596738SThomas Falcon offset = sizeof(struct ipv6hdr);
12367b596738SThomas Falcon skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
12377b596738SThomas Falcon } else {
12387b596738SThomas Falcon return;
12397b596738SThomas Falcon }
12407b596738SThomas Falcon } else {
12417b596738SThomas Falcon return;
12427b596738SThomas Falcon }
12437b596738SThomas Falcon /* if mss is not set through Large Packet bit/mss in rx buffer,
12447b596738SThomas Falcon * expect that the mss will be written to the tcp header checksum.
12457b596738SThomas Falcon */
124694acf164SThomas Falcon tcph = (struct tcphdr *)(skb->data + offset);
12477b596738SThomas Falcon if (lrg_pkt) {
12487b596738SThomas Falcon skb_shinfo(skb)->gso_size = mss;
12497b596738SThomas Falcon } else if (offset) {
12507b596738SThomas Falcon skb_shinfo(skb)->gso_size = ntohs(tcph->check);
12517b596738SThomas Falcon tcph->check = 0;
12527b596738SThomas Falcon }
125394acf164SThomas Falcon
125494acf164SThomas Falcon if (skb_shinfo(skb)->gso_size) {
125594acf164SThomas Falcon hdr_len = offset + tcph->doff * 4;
125694acf164SThomas Falcon skb_shinfo(skb)->gso_segs =
125794acf164SThomas Falcon DIV_ROUND_UP(skb->len - hdr_len,
125894acf164SThomas Falcon skb_shinfo(skb)->gso_size);
125994acf164SThomas Falcon }
12607b596738SThomas Falcon }
12617b596738SThomas Falcon
ibmveth_rx_csum_helper(struct sk_buff * skb,struct ibmveth_adapter * adapter)126266aa0678SSivakumar Krishnasamy static void ibmveth_rx_csum_helper(struct sk_buff *skb,
126366aa0678SSivakumar Krishnasamy struct ibmveth_adapter *adapter)
126466aa0678SSivakumar Krishnasamy {
126566aa0678SSivakumar Krishnasamy struct iphdr *iph = NULL;
126666aa0678SSivakumar Krishnasamy struct ipv6hdr *iph6 = NULL;
126766aa0678SSivakumar Krishnasamy __be16 skb_proto = 0;
126866aa0678SSivakumar Krishnasamy u16 iphlen = 0;
126966aa0678SSivakumar Krishnasamy u16 iph_proto = 0;
127066aa0678SSivakumar Krishnasamy u16 tcphdrlen = 0;
127166aa0678SSivakumar Krishnasamy
127266aa0678SSivakumar Krishnasamy skb_proto = be16_to_cpu(skb->protocol);
127366aa0678SSivakumar Krishnasamy
127466aa0678SSivakumar Krishnasamy if (skb_proto == ETH_P_IP) {
127566aa0678SSivakumar Krishnasamy iph = (struct iphdr *)skb->data;
127666aa0678SSivakumar Krishnasamy
127766aa0678SSivakumar Krishnasamy /* If the IP checksum is not offloaded and if the packet
127866aa0678SSivakumar Krishnasamy * is large send, the checksum must be rebuilt.
127966aa0678SSivakumar Krishnasamy */
128066aa0678SSivakumar Krishnasamy if (iph->check == 0xffff) {
128166aa0678SSivakumar Krishnasamy iph->check = 0;
128266aa0678SSivakumar Krishnasamy iph->check = ip_fast_csum((unsigned char *)iph,
128366aa0678SSivakumar Krishnasamy iph->ihl);
128466aa0678SSivakumar Krishnasamy }
128566aa0678SSivakumar Krishnasamy
128666aa0678SSivakumar Krishnasamy iphlen = iph->ihl * 4;
128766aa0678SSivakumar Krishnasamy iph_proto = iph->protocol;
128866aa0678SSivakumar Krishnasamy } else if (skb_proto == ETH_P_IPV6) {
128966aa0678SSivakumar Krishnasamy iph6 = (struct ipv6hdr *)skb->data;
129066aa0678SSivakumar Krishnasamy iphlen = sizeof(struct ipv6hdr);
129166aa0678SSivakumar Krishnasamy iph_proto = iph6->nexthdr;
129266aa0678SSivakumar Krishnasamy }
129366aa0678SSivakumar Krishnasamy
12947525de25SDavid Wilder /* When CSO is enabled the TCP checksum may have be set to NULL by
12957525de25SDavid Wilder * the sender given that we zeroed out TCP checksum field in
12967525de25SDavid Wilder * transmit path (refer ibmveth_start_xmit routine). In this case set
12977525de25SDavid Wilder * up CHECKSUM_PARTIAL. If the packet is forwarded, the checksum will
12987525de25SDavid Wilder * then be recalculated by the destination NIC (CSO must be enabled
12997525de25SDavid Wilder * on the destination NIC).
13007525de25SDavid Wilder *
13017525de25SDavid Wilder * In an OVS environment, when a flow is not cached, specifically for a
13027525de25SDavid Wilder * new TCP connection, the first packet information is passed up to
130366aa0678SSivakumar Krishnasamy * the user space for finding a flow. During this process, OVS computes
130466aa0678SSivakumar Krishnasamy * checksum on the first packet when CHECKSUM_PARTIAL flag is set.
130566aa0678SSivakumar Krishnasamy *
1306*51e7a666SDavid Wilder * So, re-compute TCP pseudo header checksum.
130766aa0678SSivakumar Krishnasamy */
1308*51e7a666SDavid Wilder
13097525de25SDavid Wilder if (iph_proto == IPPROTO_TCP) {
131066aa0678SSivakumar Krishnasamy struct tcphdr *tcph = (struct tcphdr *)(skb->data + iphlen);
1311*51e7a666SDavid Wilder
13127525de25SDavid Wilder if (tcph->check == 0x0000) {
131366aa0678SSivakumar Krishnasamy /* Recompute TCP pseudo header checksum */
13147525de25SDavid Wilder tcphdrlen = skb->len - iphlen;
131566aa0678SSivakumar Krishnasamy if (skb_proto == ETH_P_IP)
13167525de25SDavid Wilder tcph->check =
13177525de25SDavid Wilder ~csum_tcpudp_magic(iph->saddr,
131866aa0678SSivakumar Krishnasamy iph->daddr, tcphdrlen, iph_proto, 0);
131966aa0678SSivakumar Krishnasamy else if (skb_proto == ETH_P_IPV6)
13207525de25SDavid Wilder tcph->check =
13217525de25SDavid Wilder ~csum_ipv6_magic(&iph6->saddr,
132266aa0678SSivakumar Krishnasamy &iph6->daddr, tcphdrlen, iph_proto, 0);
132366aa0678SSivakumar Krishnasamy /* Setup SKB fields for checksum offload */
132466aa0678SSivakumar Krishnasamy skb_partial_csum_set(skb, iphlen,
132566aa0678SSivakumar Krishnasamy offsetof(struct tcphdr, check));
132666aa0678SSivakumar Krishnasamy skb_reset_network_header(skb);
132766aa0678SSivakumar Krishnasamy }
132866aa0678SSivakumar Krishnasamy }
13297525de25SDavid Wilder }
133066aa0678SSivakumar Krishnasamy
ibmveth_poll(struct napi_struct * napi,int budget)13319aa32835SJeff Kirsher static int ibmveth_poll(struct napi_struct *napi, int budget)
13329aa32835SJeff Kirsher {
13339aa32835SJeff Kirsher struct ibmveth_adapter *adapter =
13349aa32835SJeff Kirsher container_of(napi, struct ibmveth_adapter, napi);
13359aa32835SJeff Kirsher struct net_device *netdev = adapter->netdev;
13369aa32835SJeff Kirsher int frames_processed = 0;
13379aa32835SJeff Kirsher unsigned long lpar_rc;
13387b596738SThomas Falcon u16 mss = 0;
13399aa32835SJeff Kirsher
1340cb013ea1SEric W. Biederman while (frames_processed < budget) {
13419aa32835SJeff Kirsher if (!ibmveth_rxq_pending_buffer(adapter))
13429aa32835SJeff Kirsher break;
13439aa32835SJeff Kirsher
13449aa32835SJeff Kirsher smp_rmb();
13459aa32835SJeff Kirsher if (!ibmveth_rxq_buffer_valid(adapter)) {
13469aa32835SJeff Kirsher wmb(); /* suggested by larson1 */
13479aa32835SJeff Kirsher adapter->rx_invalid_buffer++;
13489aa32835SJeff Kirsher netdev_dbg(netdev, "recycling invalid buffer\n");
13499aa32835SJeff Kirsher ibmveth_rxq_recycle_buffer(adapter);
13509aa32835SJeff Kirsher } else {
13519aa32835SJeff Kirsher struct sk_buff *skb, *new_skb;
13529aa32835SJeff Kirsher int length = ibmveth_rxq_frame_length(adapter);
13539aa32835SJeff Kirsher int offset = ibmveth_rxq_frame_offset(adapter);
13549aa32835SJeff Kirsher int csum_good = ibmveth_rxq_csum_good(adapter);
13557b596738SThomas Falcon int lrg_pkt = ibmveth_rxq_large_packet(adapter);
1356413f142cSDavid Wilder __sum16 iph_check = 0;
13579aa32835SJeff Kirsher
13589aa32835SJeff Kirsher skb = ibmveth_rxq_get_buffer(adapter);
13599aa32835SJeff Kirsher
13607b596738SThomas Falcon /* if the large packet bit is set in the rx queue
13617b596738SThomas Falcon * descriptor, the mss will be written by PHYP eight
13627b596738SThomas Falcon * bytes from the start of the rx buffer, which is
13637b596738SThomas Falcon * skb->data at this stage
13647b596738SThomas Falcon */
13657b596738SThomas Falcon if (lrg_pkt) {
13667b596738SThomas Falcon __be64 *rxmss = (__be64 *)(skb->data + 8);
13677b596738SThomas Falcon
13687b596738SThomas Falcon mss = (u16)be64_to_cpu(*rxmss);
13697b596738SThomas Falcon }
13707b596738SThomas Falcon
13719aa32835SJeff Kirsher new_skb = NULL;
13729aa32835SJeff Kirsher if (length < rx_copybreak)
13739aa32835SJeff Kirsher new_skb = netdev_alloc_skb(netdev, length);
13749aa32835SJeff Kirsher
13759aa32835SJeff Kirsher if (new_skb) {
13769aa32835SJeff Kirsher skb_copy_to_linear_data(new_skb,
13779aa32835SJeff Kirsher skb->data + offset,
13789aa32835SJeff Kirsher length);
13799aa32835SJeff Kirsher if (rx_flush)
13809aa32835SJeff Kirsher ibmveth_flush_buffer(skb->data,
13819aa32835SJeff Kirsher length + offset);
13828decf868SDavid S. Miller if (!ibmveth_rxq_recycle_buffer(adapter))
13838decf868SDavid S. Miller kfree_skb(skb);
13849aa32835SJeff Kirsher skb = new_skb;
13859aa32835SJeff Kirsher } else {
13869aa32835SJeff Kirsher ibmveth_rxq_harvest_buffer(adapter);
13879aa32835SJeff Kirsher skb_reserve(skb, offset);
13889aa32835SJeff Kirsher }
13899aa32835SJeff Kirsher
13909aa32835SJeff Kirsher skb_put(skb, length);
13919aa32835SJeff Kirsher skb->protocol = eth_type_trans(skb, netdev);
13929aa32835SJeff Kirsher
1393413f142cSDavid Wilder /* PHYP without PLSO support places a -1 in the ip
1394413f142cSDavid Wilder * checksum for large send frames.
1395413f142cSDavid Wilder */
1396413f142cSDavid Wilder if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1397413f142cSDavid Wilder struct iphdr *iph = (struct iphdr *)skb->data;
1398413f142cSDavid Wilder
1399413f142cSDavid Wilder iph_check = iph->check;
1400413f142cSDavid Wilder }
1401413f142cSDavid Wilder
1402413f142cSDavid Wilder if ((length > netdev->mtu + ETH_HLEN) ||
1403413f142cSDavid Wilder lrg_pkt || iph_check == 0xffff) {
14047b596738SThomas Falcon ibmveth_rx_mss_helper(skb, mss, lrg_pkt);
14059c7e8bc5SThomas Falcon adapter->rx_large_packets++;
14069c7e8bc5SThomas Falcon }
14079aa32835SJeff Kirsher
14085ce9ad81SDavid Wilder if (csum_good) {
14095ce9ad81SDavid Wilder skb->ip_summed = CHECKSUM_UNNECESSARY;
14105ce9ad81SDavid Wilder ibmveth_rx_csum_helper(skb, adapter);
14115ce9ad81SDavid Wilder }
14125ce9ad81SDavid Wilder
141392ec8279SThomas Falcon napi_gro_receive(napi, skb); /* send it up */
14149aa32835SJeff Kirsher
14159aa32835SJeff Kirsher netdev->stats.rx_packets++;
14169aa32835SJeff Kirsher netdev->stats.rx_bytes += length;
14179aa32835SJeff Kirsher frames_processed++;
14189aa32835SJeff Kirsher }
1419cb013ea1SEric W. Biederman }
14209aa32835SJeff Kirsher
14219aa32835SJeff Kirsher ibmveth_replenish_task(adapter);
14229aa32835SJeff Kirsher
14239aa32835SJeff Kirsher if (frames_processed < budget) {
14246ad20165SEric Dumazet napi_complete_done(napi, frames_processed);
14254736edc7SYongbae Park
14269aa32835SJeff Kirsher /* We think we are done - reenable interrupts,
14279aa32835SJeff Kirsher * then check once more to make sure we are done.
14289aa32835SJeff Kirsher */
14299aa32835SJeff Kirsher lpar_rc = h_vio_signal(adapter->vdev->unit_address,
14309aa32835SJeff Kirsher VIO_IRQ_ENABLE);
14319aa32835SJeff Kirsher
14329aa32835SJeff Kirsher BUG_ON(lpar_rc != H_SUCCESS);
14339aa32835SJeff Kirsher
14349aa32835SJeff Kirsher if (ibmveth_rxq_pending_buffer(adapter) &&
14359aa32835SJeff Kirsher napi_reschedule(napi)) {
14369aa32835SJeff Kirsher lpar_rc = h_vio_signal(adapter->vdev->unit_address,
14379aa32835SJeff Kirsher VIO_IRQ_DISABLE);
14389aa32835SJeff Kirsher }
14399aa32835SJeff Kirsher }
14409aa32835SJeff Kirsher
14419aa32835SJeff Kirsher return frames_processed;
14429aa32835SJeff Kirsher }
14439aa32835SJeff Kirsher
ibmveth_interrupt(int irq,void * dev_instance)14449aa32835SJeff Kirsher static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
14459aa32835SJeff Kirsher {
14469aa32835SJeff Kirsher struct net_device *netdev = dev_instance;
14479aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
14489aa32835SJeff Kirsher unsigned long lpar_rc;
14499aa32835SJeff Kirsher
14509aa32835SJeff Kirsher if (napi_schedule_prep(&adapter->napi)) {
14519aa32835SJeff Kirsher lpar_rc = h_vio_signal(adapter->vdev->unit_address,
14529aa32835SJeff Kirsher VIO_IRQ_DISABLE);
14539aa32835SJeff Kirsher BUG_ON(lpar_rc != H_SUCCESS);
14549aa32835SJeff Kirsher __napi_schedule(&adapter->napi);
14559aa32835SJeff Kirsher }
14569aa32835SJeff Kirsher return IRQ_HANDLED;
14579aa32835SJeff Kirsher }
14589aa32835SJeff Kirsher
ibmveth_set_multicast_list(struct net_device * netdev)14599aa32835SJeff Kirsher static void ibmveth_set_multicast_list(struct net_device *netdev)
14609aa32835SJeff Kirsher {
14619aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
14629aa32835SJeff Kirsher unsigned long lpar_rc;
14639aa32835SJeff Kirsher
14649aa32835SJeff Kirsher if ((netdev->flags & IFF_PROMISC) ||
14659aa32835SJeff Kirsher (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
14669aa32835SJeff Kirsher lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
14679aa32835SJeff Kirsher IbmVethMcastEnableRecv |
14689aa32835SJeff Kirsher IbmVethMcastDisableFiltering,
14699aa32835SJeff Kirsher 0);
14709aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
14719aa32835SJeff Kirsher netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
14729aa32835SJeff Kirsher "entering promisc mode\n", lpar_rc);
14739aa32835SJeff Kirsher }
14749aa32835SJeff Kirsher } else {
14759aa32835SJeff Kirsher struct netdev_hw_addr *ha;
14769aa32835SJeff Kirsher /* clear the filter table & disable filtering */
14779aa32835SJeff Kirsher lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
14789aa32835SJeff Kirsher IbmVethMcastEnableRecv |
14799aa32835SJeff Kirsher IbmVethMcastDisableFiltering |
14809aa32835SJeff Kirsher IbmVethMcastClearFilterTable,
14819aa32835SJeff Kirsher 0);
14829aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
14839aa32835SJeff Kirsher netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
14849aa32835SJeff Kirsher "attempting to clear filter table\n",
14859aa32835SJeff Kirsher lpar_rc);
14869aa32835SJeff Kirsher }
14879aa32835SJeff Kirsher /* add the addresses to the filter table */
14889aa32835SJeff Kirsher netdev_for_each_mc_addr(ha, netdev) {
14899aa32835SJeff Kirsher /* add the multicast address to the filter table */
1490d746ca95SAnton Blanchard u64 mcast_addr;
14915c8b3485SJakub Kicinski mcast_addr = ether_addr_to_u64(ha->addr);
14929aa32835SJeff Kirsher lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
14939aa32835SJeff Kirsher IbmVethMcastAddFilter,
14949aa32835SJeff Kirsher mcast_addr);
14959aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
14969aa32835SJeff Kirsher netdev_err(netdev, "h_multicast_ctrl rc=%ld "
14979aa32835SJeff Kirsher "when adding an entry to the filter "
14989aa32835SJeff Kirsher "table\n", lpar_rc);
14999aa32835SJeff Kirsher }
15009aa32835SJeff Kirsher }
15019aa32835SJeff Kirsher
15029aa32835SJeff Kirsher /* re-enable filtering */
15039aa32835SJeff Kirsher lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
15049aa32835SJeff Kirsher IbmVethMcastEnableFiltering,
15059aa32835SJeff Kirsher 0);
15069aa32835SJeff Kirsher if (lpar_rc != H_SUCCESS) {
15079aa32835SJeff Kirsher netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
15089aa32835SJeff Kirsher "enabling filtering\n", lpar_rc);
15099aa32835SJeff Kirsher }
15109aa32835SJeff Kirsher }
15119aa32835SJeff Kirsher }
15129aa32835SJeff Kirsher
ibmveth_change_mtu(struct net_device * dev,int new_mtu)15139aa32835SJeff Kirsher static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
15149aa32835SJeff Kirsher {
15159aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(dev);
15169aa32835SJeff Kirsher struct vio_dev *viodev = adapter->vdev;
15179aa32835SJeff Kirsher int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
15189aa32835SJeff Kirsher int i, rc;
15199aa32835SJeff Kirsher int need_restart = 0;
15209aa32835SJeff Kirsher
15219aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
15224fce1482SDavid Gibson if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
15239aa32835SJeff Kirsher break;
15249aa32835SJeff Kirsher
15259aa32835SJeff Kirsher if (i == IBMVETH_NUM_BUFF_POOLS)
15269aa32835SJeff Kirsher return -EINVAL;
15279aa32835SJeff Kirsher
15289aa32835SJeff Kirsher /* Deactivate all the buffer pools so that the next loop can activate
15299aa32835SJeff Kirsher only the buffer pools necessary to hold the new MTU */
15309aa32835SJeff Kirsher if (netif_running(adapter->netdev)) {
15319aa32835SJeff Kirsher need_restart = 1;
15329aa32835SJeff Kirsher ibmveth_close(adapter->netdev);
15339aa32835SJeff Kirsher }
15349aa32835SJeff Kirsher
15359aa32835SJeff Kirsher /* Look for an active buffer pool that can hold the new MTU */
15369aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
15379aa32835SJeff Kirsher adapter->rx_buff_pool[i].active = 1;
15389aa32835SJeff Kirsher
15394fce1482SDavid Gibson if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
15409aa32835SJeff Kirsher dev->mtu = new_mtu;
15419aa32835SJeff Kirsher vio_cmo_set_dev_desired(viodev,
15429aa32835SJeff Kirsher ibmveth_get_desired_dma
15439aa32835SJeff Kirsher (viodev));
15449aa32835SJeff Kirsher if (need_restart) {
15459aa32835SJeff Kirsher return ibmveth_open(adapter->netdev);
15469aa32835SJeff Kirsher }
15479aa32835SJeff Kirsher return 0;
15489aa32835SJeff Kirsher }
15499aa32835SJeff Kirsher }
15509aa32835SJeff Kirsher
15519aa32835SJeff Kirsher if (need_restart && (rc = ibmveth_open(adapter->netdev)))
15529aa32835SJeff Kirsher return rc;
15539aa32835SJeff Kirsher
15549aa32835SJeff Kirsher return -EINVAL;
15559aa32835SJeff Kirsher }
15569aa32835SJeff Kirsher
15579aa32835SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
ibmveth_poll_controller(struct net_device * dev)15589aa32835SJeff Kirsher static void ibmveth_poll_controller(struct net_device *dev)
15599aa32835SJeff Kirsher {
15609aa32835SJeff Kirsher ibmveth_replenish_task(netdev_priv(dev));
15619aa32835SJeff Kirsher ibmveth_interrupt(dev->irq, dev);
15629aa32835SJeff Kirsher }
15639aa32835SJeff Kirsher #endif
15649aa32835SJeff Kirsher
15659aa32835SJeff Kirsher /**
15669aa32835SJeff Kirsher * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
15679aa32835SJeff Kirsher *
15689aa32835SJeff Kirsher * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
15699aa32835SJeff Kirsher *
15709aa32835SJeff Kirsher * Return value:
15719aa32835SJeff Kirsher * Number of bytes of IO data the driver will need to perform well.
15729aa32835SJeff Kirsher */
ibmveth_get_desired_dma(struct vio_dev * vdev)15739aa32835SJeff Kirsher static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
15749aa32835SJeff Kirsher {
15759aa32835SJeff Kirsher struct net_device *netdev = dev_get_drvdata(&vdev->dev);
15769aa32835SJeff Kirsher struct ibmveth_adapter *adapter;
1577d0847757SAlistair Popple struct iommu_table *tbl;
15789aa32835SJeff Kirsher unsigned long ret;
15799aa32835SJeff Kirsher int i;
15809aa32835SJeff Kirsher int rxqentries = 1;
15819aa32835SJeff Kirsher
1582d0847757SAlistair Popple tbl = get_iommu_table_base(&vdev->dev);
1583d0847757SAlistair Popple
15849aa32835SJeff Kirsher /* netdev inits at probe time along with the structures we need below*/
15859aa32835SJeff Kirsher if (netdev == NULL)
1586d0847757SAlistair Popple return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
15879aa32835SJeff Kirsher
15889aa32835SJeff Kirsher adapter = netdev_priv(netdev);
15899aa32835SJeff Kirsher
15909aa32835SJeff Kirsher ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
1591d0847757SAlistair Popple ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
1592d6832ca4SNick Child /* add size of mapped tx buffers */
1593d6832ca4SNick Child ret += IOMMU_PAGE_ALIGN(IBMVETH_MAX_TX_BUF_SIZE, tbl);
15949aa32835SJeff Kirsher
15959aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
15969aa32835SJeff Kirsher /* add the size of the active receive buffers */
15979aa32835SJeff Kirsher if (adapter->rx_buff_pool[i].active)
15989aa32835SJeff Kirsher ret +=
15999aa32835SJeff Kirsher adapter->rx_buff_pool[i].size *
16009aa32835SJeff Kirsher IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
1601d0847757SAlistair Popple buff_size, tbl);
16029aa32835SJeff Kirsher rxqentries += adapter->rx_buff_pool[i].size;
16039aa32835SJeff Kirsher }
16049aa32835SJeff Kirsher /* add the size of the receive queue entries */
1605d0847757SAlistair Popple ret += IOMMU_PAGE_ALIGN(
1606d0847757SAlistair Popple rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
16079aa32835SJeff Kirsher
16089aa32835SJeff Kirsher return ret;
16099aa32835SJeff Kirsher }
16109aa32835SJeff Kirsher
ibmveth_set_mac_addr(struct net_device * dev,void * p)1611c77c761fSThomas Falcon static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1612c77c761fSThomas Falcon {
1613c77c761fSThomas Falcon struct ibmveth_adapter *adapter = netdev_priv(dev);
1614c77c761fSThomas Falcon struct sockaddr *addr = p;
1615c77c761fSThomas Falcon u64 mac_address;
1616c77c761fSThomas Falcon int rc;
1617c77c761fSThomas Falcon
1618c77c761fSThomas Falcon if (!is_valid_ether_addr(addr->sa_data))
1619c77c761fSThomas Falcon return -EADDRNOTAVAIL;
1620c77c761fSThomas Falcon
16215c8b3485SJakub Kicinski mac_address = ether_addr_to_u64(addr->sa_data);
1622c77c761fSThomas Falcon rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1623c77c761fSThomas Falcon if (rc) {
1624c77c761fSThomas Falcon netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1625c77c761fSThomas Falcon return rc;
1626c77c761fSThomas Falcon }
1627c77c761fSThomas Falcon
1628f3956ebbSJakub Kicinski eth_hw_addr_set(dev, addr->sa_data);
1629c77c761fSThomas Falcon
1630c77c761fSThomas Falcon return 0;
1631c77c761fSThomas Falcon }
1632c77c761fSThomas Falcon
16339aa32835SJeff Kirsher static const struct net_device_ops ibmveth_netdev_ops = {
16349aa32835SJeff Kirsher .ndo_open = ibmveth_open,
16359aa32835SJeff Kirsher .ndo_stop = ibmveth_close,
16369aa32835SJeff Kirsher .ndo_start_xmit = ibmveth_start_xmit,
1637afc4b13dSJiri Pirko .ndo_set_rx_mode = ibmveth_set_multicast_list,
1638a7605370SArnd Bergmann .ndo_eth_ioctl = ibmveth_ioctl,
16399aa32835SJeff Kirsher .ndo_change_mtu = ibmveth_change_mtu,
16409aa32835SJeff Kirsher .ndo_fix_features = ibmveth_fix_features,
16419aa32835SJeff Kirsher .ndo_set_features = ibmveth_set_features,
16429aa32835SJeff Kirsher .ndo_validate_addr = eth_validate_addr,
1643c77c761fSThomas Falcon .ndo_set_mac_address = ibmveth_set_mac_addr,
16449aa32835SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
16459aa32835SJeff Kirsher .ndo_poll_controller = ibmveth_poll_controller,
16469aa32835SJeff Kirsher #endif
16479aa32835SJeff Kirsher };
16489aa32835SJeff Kirsher
ibmveth_probe(struct vio_dev * dev,const struct vio_device_id * id)16491dd06ae8SGreg Kroah-Hartman static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
16509aa32835SJeff Kirsher {
165113f85203SBenjamin Herrenschmidt int rc, i, mac_len;
16529aa32835SJeff Kirsher struct net_device *netdev;
16539aa32835SJeff Kirsher struct ibmveth_adapter *adapter;
16549aa32835SJeff Kirsher unsigned char *mac_addr_p;
165566cf4710SThomas Falcon __be32 *mcastFilterSize_p;
165607e6a97dSThomas Falcon long ret;
165707e6a97dSThomas Falcon unsigned long ret_attr;
16589aa32835SJeff Kirsher
16599aa32835SJeff Kirsher dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
16609aa32835SJeff Kirsher dev->unit_address);
16619aa32835SJeff Kirsher
16629aa32835SJeff Kirsher mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
166313f85203SBenjamin Herrenschmidt &mac_len);
16649aa32835SJeff Kirsher if (!mac_addr_p) {
16659aa32835SJeff Kirsher dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
16669aa32835SJeff Kirsher return -EINVAL;
16679aa32835SJeff Kirsher }
166813f85203SBenjamin Herrenschmidt /* Workaround for old/broken pHyp */
166913f85203SBenjamin Herrenschmidt if (mac_len == 8)
167013f85203SBenjamin Herrenschmidt mac_addr_p += 2;
167113f85203SBenjamin Herrenschmidt else if (mac_len != 6) {
167213f85203SBenjamin Herrenschmidt dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
167313f85203SBenjamin Herrenschmidt mac_len);
167413f85203SBenjamin Herrenschmidt return -EINVAL;
167513f85203SBenjamin Herrenschmidt }
16769aa32835SJeff Kirsher
167766cf4710SThomas Falcon mcastFilterSize_p = (__be32 *)vio_get_attribute(dev,
167866cf4710SThomas Falcon VETH_MCAST_FILTER_SIZE,
167966cf4710SThomas Falcon NULL);
16809aa32835SJeff Kirsher if (!mcastFilterSize_p) {
16819aa32835SJeff Kirsher dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
16829aa32835SJeff Kirsher "attribute\n");
16839aa32835SJeff Kirsher return -EINVAL;
16849aa32835SJeff Kirsher }
16859aa32835SJeff Kirsher
1686d926793cSNick Child netdev = alloc_etherdev_mqs(sizeof(struct ibmveth_adapter), IBMVETH_MAX_QUEUES, 1);
16879aa32835SJeff Kirsher if (!netdev)
16889aa32835SJeff Kirsher return -ENOMEM;
16899aa32835SJeff Kirsher
16909aa32835SJeff Kirsher adapter = netdev_priv(netdev);
16919aa32835SJeff Kirsher dev_set_drvdata(&dev->dev, netdev);
16929aa32835SJeff Kirsher
16939aa32835SJeff Kirsher adapter->vdev = dev;
16949aa32835SJeff Kirsher adapter->netdev = netdev;
169566cf4710SThomas Falcon adapter->mcastFilterSize = be32_to_cpu(*mcastFilterSize_p);
16969aedc6e2SCris Forno ibmveth_init_link_settings(netdev);
16979aa32835SJeff Kirsher
1698b707b89fSJakub Kicinski netif_napi_add_weight(netdev, &adapter->napi, ibmveth_poll, 16);
16999aa32835SJeff Kirsher
17009aa32835SJeff Kirsher netdev->irq = dev->irq;
17019aa32835SJeff Kirsher netdev->netdev_ops = &ibmveth_netdev_ops;
17029aa32835SJeff Kirsher netdev->ethtool_ops = &netdev_ethtool_ops;
17039aa32835SJeff Kirsher SET_NETDEV_DEV(netdev, &dev->dev);
170423d28a85SThomas Huth netdev->hw_features = NETIF_F_SG;
170523d28a85SThomas Huth if (vio_get_attribute(dev, "ibm,illan-options", NULL) != NULL) {
170623d28a85SThomas Huth netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
170723d28a85SThomas Huth NETIF_F_RXCSUM;
170823d28a85SThomas Huth }
170907e6a97dSThomas Falcon
17109aa32835SJeff Kirsher netdev->features |= netdev->hw_features;
17119aa32835SJeff Kirsher
171207e6a97dSThomas Falcon ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
171307e6a97dSThomas Falcon
171407e6a97dSThomas Falcon /* If running older firmware, TSO should not be enabled by default */
171507e6a97dSThomas Falcon if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
171607e6a97dSThomas Falcon !old_large_send) {
171707e6a97dSThomas Falcon netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
171807e6a97dSThomas Falcon netdev->features |= netdev->hw_features;
171907e6a97dSThomas Falcon } else {
17208641dd85SThomas Falcon netdev->hw_features |= NETIF_F_TSO;
172107e6a97dSThomas Falcon }
17228641dd85SThomas Falcon
172366aa0678SSivakumar Krishnasamy adapter->is_active_trunk = false;
172466aa0678SSivakumar Krishnasamy if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK)) {
172566aa0678SSivakumar Krishnasamy adapter->is_active_trunk = true;
172666aa0678SSivakumar Krishnasamy netdev->hw_features |= NETIF_F_FRAGLIST;
172766aa0678SSivakumar Krishnasamy netdev->features |= NETIF_F_FRAGLIST;
172866aa0678SSivakumar Krishnasamy }
172966aa0678SSivakumar Krishnasamy
1730d894be57SJarod Wilson netdev->min_mtu = IBMVETH_MIN_MTU;
17315948378bSThomas Falcon netdev->max_mtu = ETH_MAX_MTU - IBMVETH_BUFF_OH;
1732d894be57SJarod Wilson
1733a96d317fSJakub Kicinski eth_hw_addr_set(netdev, mac_addr_p);
17349aa32835SJeff Kirsher
1735cd7c7ec3SThomas Falcon if (firmware_has_feature(FW_FEATURE_CMO))
1736cd7c7ec3SThomas Falcon memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
1737cd7c7ec3SThomas Falcon
17389aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
17399aa32835SJeff Kirsher struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
17409aa32835SJeff Kirsher int error;
17419aa32835SJeff Kirsher
17429aa32835SJeff Kirsher ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
17439aa32835SJeff Kirsher pool_count[i], pool_size[i],
17449aa32835SJeff Kirsher pool_active[i]);
17459aa32835SJeff Kirsher error = kobject_init_and_add(kobj, &ktype_veth_pool,
17469aa32835SJeff Kirsher &dev->dev.kobj, "pool%d", i);
17479aa32835SJeff Kirsher if (!error)
17489aa32835SJeff Kirsher kobject_uevent(kobj, KOBJ_ADD);
17499aa32835SJeff Kirsher }
17509aa32835SJeff Kirsher
1751742c60e1SNick Child rc = netif_set_real_num_tx_queues(netdev, min(num_online_cpus(),
1752742c60e1SNick Child IBMVETH_DEFAULT_QUEUES));
175310c2aba8SNick Child if (rc) {
175410c2aba8SNick Child netdev_dbg(netdev, "failed to set number of tx queues rc=%d\n",
175510c2aba8SNick Child rc);
175610c2aba8SNick Child free_netdev(netdev);
175710c2aba8SNick Child return rc;
175810c2aba8SNick Child }
1759d926793cSNick Child adapter->tx_ltb_size = PAGE_ALIGN(IBMVETH_MAX_TX_BUF_SIZE);
176010c2aba8SNick Child for (i = 0; i < IBMVETH_MAX_QUEUES; i++)
176110c2aba8SNick Child adapter->tx_ltb_ptr[i] = NULL;
1762d926793cSNick Child
17639aa32835SJeff Kirsher netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
17649aa32835SJeff Kirsher netdev_dbg(netdev, "registering netdev...\n");
17659aa32835SJeff Kirsher
17669aa32835SJeff Kirsher ibmveth_set_features(netdev, netdev->features);
17679aa32835SJeff Kirsher
17689aa32835SJeff Kirsher rc = register_netdev(netdev);
17699aa32835SJeff Kirsher
17709aa32835SJeff Kirsher if (rc) {
17719aa32835SJeff Kirsher netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
17729aa32835SJeff Kirsher free_netdev(netdev);
17739aa32835SJeff Kirsher return rc;
17749aa32835SJeff Kirsher }
17759aa32835SJeff Kirsher
17769aa32835SJeff Kirsher netdev_dbg(netdev, "registered\n");
17779aa32835SJeff Kirsher
17789aa32835SJeff Kirsher return 0;
17799aa32835SJeff Kirsher }
17809aa32835SJeff Kirsher
ibmveth_remove(struct vio_dev * dev)1781386a966fSUwe Kleine-König static void ibmveth_remove(struct vio_dev *dev)
17829aa32835SJeff Kirsher {
17839aa32835SJeff Kirsher struct net_device *netdev = dev_get_drvdata(&dev->dev);
17849aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
17859aa32835SJeff Kirsher int i;
17869aa32835SJeff Kirsher
17879aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
17889aa32835SJeff Kirsher kobject_put(&adapter->rx_buff_pool[i].kobj);
17899aa32835SJeff Kirsher
17909aa32835SJeff Kirsher unregister_netdev(netdev);
17919aa32835SJeff Kirsher
17929aa32835SJeff Kirsher free_netdev(netdev);
17939aa32835SJeff Kirsher dev_set_drvdata(&dev->dev, NULL);
17949aa32835SJeff Kirsher }
17959aa32835SJeff Kirsher
17969aa32835SJeff Kirsher static struct attribute veth_active_attr;
17979aa32835SJeff Kirsher static struct attribute veth_num_attr;
17989aa32835SJeff Kirsher static struct attribute veth_size_attr;
17999aa32835SJeff Kirsher
veth_pool_show(struct kobject * kobj,struct attribute * attr,char * buf)18009aa32835SJeff Kirsher static ssize_t veth_pool_show(struct kobject *kobj,
18019aa32835SJeff Kirsher struct attribute *attr, char *buf)
18029aa32835SJeff Kirsher {
18039aa32835SJeff Kirsher struct ibmveth_buff_pool *pool = container_of(kobj,
18049aa32835SJeff Kirsher struct ibmveth_buff_pool,
18059aa32835SJeff Kirsher kobj);
18069aa32835SJeff Kirsher
18079aa32835SJeff Kirsher if (attr == &veth_active_attr)
18089aa32835SJeff Kirsher return sprintf(buf, "%d\n", pool->active);
18099aa32835SJeff Kirsher else if (attr == &veth_num_attr)
18109aa32835SJeff Kirsher return sprintf(buf, "%d\n", pool->size);
18119aa32835SJeff Kirsher else if (attr == &veth_size_attr)
18129aa32835SJeff Kirsher return sprintf(buf, "%d\n", pool->buff_size);
18139aa32835SJeff Kirsher return 0;
18149aa32835SJeff Kirsher }
18159aa32835SJeff Kirsher
veth_pool_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)18169aa32835SJeff Kirsher static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
18179aa32835SJeff Kirsher const char *buf, size_t count)
18189aa32835SJeff Kirsher {
18199aa32835SJeff Kirsher struct ibmveth_buff_pool *pool = container_of(kobj,
18209aa32835SJeff Kirsher struct ibmveth_buff_pool,
18219aa32835SJeff Kirsher kobj);
18221756055dSYueHaibing struct net_device *netdev = dev_get_drvdata(kobj_to_dev(kobj->parent));
18239aa32835SJeff Kirsher struct ibmveth_adapter *adapter = netdev_priv(netdev);
18249aa32835SJeff Kirsher long value = simple_strtol(buf, NULL, 10);
18259aa32835SJeff Kirsher long rc;
18269aa32835SJeff Kirsher
18279aa32835SJeff Kirsher if (attr == &veth_active_attr) {
18289aa32835SJeff Kirsher if (value && !pool->active) {
18299aa32835SJeff Kirsher if (netif_running(netdev)) {
18309aa32835SJeff Kirsher if (ibmveth_alloc_buffer_pool(pool)) {
18319aa32835SJeff Kirsher netdev_err(netdev,
18329aa32835SJeff Kirsher "unable to alloc pool\n");
18339aa32835SJeff Kirsher return -ENOMEM;
18349aa32835SJeff Kirsher }
18359aa32835SJeff Kirsher pool->active = 1;
18369aa32835SJeff Kirsher ibmveth_close(netdev);
18379aa32835SJeff Kirsher if ((rc = ibmveth_open(netdev)))
18389aa32835SJeff Kirsher return rc;
18399aa32835SJeff Kirsher } else {
18409aa32835SJeff Kirsher pool->active = 1;
18419aa32835SJeff Kirsher }
18429aa32835SJeff Kirsher } else if (!value && pool->active) {
18439aa32835SJeff Kirsher int mtu = netdev->mtu + IBMVETH_BUFF_OH;
18449aa32835SJeff Kirsher int i;
18459aa32835SJeff Kirsher /* Make sure there is a buffer pool with buffers that
18469aa32835SJeff Kirsher can hold a packet of the size of the MTU */
18479aa32835SJeff Kirsher for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
18489aa32835SJeff Kirsher if (pool == &adapter->rx_buff_pool[i])
18499aa32835SJeff Kirsher continue;
18509aa32835SJeff Kirsher if (!adapter->rx_buff_pool[i].active)
18519aa32835SJeff Kirsher continue;
18529aa32835SJeff Kirsher if (mtu <= adapter->rx_buff_pool[i].buff_size)
18539aa32835SJeff Kirsher break;
18549aa32835SJeff Kirsher }
18559aa32835SJeff Kirsher
18569aa32835SJeff Kirsher if (i == IBMVETH_NUM_BUFF_POOLS) {
18579aa32835SJeff Kirsher netdev_err(netdev, "no active pool >= MTU\n");
18589aa32835SJeff Kirsher return -EPERM;
18599aa32835SJeff Kirsher }
18609aa32835SJeff Kirsher
18619aa32835SJeff Kirsher if (netif_running(netdev)) {
18629aa32835SJeff Kirsher ibmveth_close(netdev);
18639aa32835SJeff Kirsher pool->active = 0;
18649aa32835SJeff Kirsher if ((rc = ibmveth_open(netdev)))
18659aa32835SJeff Kirsher return rc;
18669aa32835SJeff Kirsher }
18679aa32835SJeff Kirsher pool->active = 0;
18689aa32835SJeff Kirsher }
18699aa32835SJeff Kirsher } else if (attr == &veth_num_attr) {
18709aa32835SJeff Kirsher if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
18719aa32835SJeff Kirsher return -EINVAL;
18729aa32835SJeff Kirsher } else {
18739aa32835SJeff Kirsher if (netif_running(netdev)) {
18749aa32835SJeff Kirsher ibmveth_close(netdev);
18759aa32835SJeff Kirsher pool->size = value;
18769aa32835SJeff Kirsher if ((rc = ibmveth_open(netdev)))
18779aa32835SJeff Kirsher return rc;
18789aa32835SJeff Kirsher } else {
18799aa32835SJeff Kirsher pool->size = value;
18809aa32835SJeff Kirsher }
18819aa32835SJeff Kirsher }
18829aa32835SJeff Kirsher } else if (attr == &veth_size_attr) {
18839aa32835SJeff Kirsher if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
18849aa32835SJeff Kirsher return -EINVAL;
18859aa32835SJeff Kirsher } else {
18869aa32835SJeff Kirsher if (netif_running(netdev)) {
18879aa32835SJeff Kirsher ibmveth_close(netdev);
18889aa32835SJeff Kirsher pool->buff_size = value;
18899aa32835SJeff Kirsher if ((rc = ibmveth_open(netdev)))
18909aa32835SJeff Kirsher return rc;
18919aa32835SJeff Kirsher } else {
18929aa32835SJeff Kirsher pool->buff_size = value;
18939aa32835SJeff Kirsher }
18949aa32835SJeff Kirsher }
18959aa32835SJeff Kirsher }
18969aa32835SJeff Kirsher
18979aa32835SJeff Kirsher /* kick the interrupt handler to allocate/deallocate pools */
18989aa32835SJeff Kirsher ibmveth_interrupt(netdev->irq, netdev);
18999aa32835SJeff Kirsher return count;
19009aa32835SJeff Kirsher }
19019aa32835SJeff Kirsher
19029aa32835SJeff Kirsher
19039aa32835SJeff Kirsher #define ATTR(_name, _mode) \
19049aa32835SJeff Kirsher struct attribute veth_##_name##_attr = { \
19059aa32835SJeff Kirsher .name = __stringify(_name), .mode = _mode, \
19069aa32835SJeff Kirsher };
19079aa32835SJeff Kirsher
19089aa32835SJeff Kirsher static ATTR(active, 0644);
19099aa32835SJeff Kirsher static ATTR(num, 0644);
19109aa32835SJeff Kirsher static ATTR(size, 0644);
19119aa32835SJeff Kirsher
19129aa32835SJeff Kirsher static struct attribute *veth_pool_attrs[] = {
19139aa32835SJeff Kirsher &veth_active_attr,
19149aa32835SJeff Kirsher &veth_num_attr,
19159aa32835SJeff Kirsher &veth_size_attr,
19169aa32835SJeff Kirsher NULL,
19179aa32835SJeff Kirsher };
1918c288bc0dSGreg Kroah-Hartman ATTRIBUTE_GROUPS(veth_pool);
19199aa32835SJeff Kirsher
19209aa32835SJeff Kirsher static const struct sysfs_ops veth_pool_ops = {
19219aa32835SJeff Kirsher .show = veth_pool_show,
19229aa32835SJeff Kirsher .store = veth_pool_store,
19239aa32835SJeff Kirsher };
19249aa32835SJeff Kirsher
19259aa32835SJeff Kirsher static struct kobj_type ktype_veth_pool = {
19269aa32835SJeff Kirsher .release = NULL,
19279aa32835SJeff Kirsher .sysfs_ops = &veth_pool_ops,
1928c288bc0dSGreg Kroah-Hartman .default_groups = veth_pool_groups,
19299aa32835SJeff Kirsher };
19309aa32835SJeff Kirsher
ibmveth_resume(struct device * dev)19319aa32835SJeff Kirsher static int ibmveth_resume(struct device *dev)
19329aa32835SJeff Kirsher {
19339aa32835SJeff Kirsher struct net_device *netdev = dev_get_drvdata(dev);
19349aa32835SJeff Kirsher ibmveth_interrupt(netdev->irq, netdev);
19359aa32835SJeff Kirsher return 0;
19369aa32835SJeff Kirsher }
19379aa32835SJeff Kirsher
193871450804SArvind Yadav static const struct vio_device_id ibmveth_device_table[] = {
19399aa32835SJeff Kirsher { "network", "IBM,l-lan"},
19409aa32835SJeff Kirsher { "", "" }
19419aa32835SJeff Kirsher };
19429aa32835SJeff Kirsher MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
19439aa32835SJeff Kirsher
1944eb60a73dSArvind Yadav static const struct dev_pm_ops ibmveth_pm_ops = {
19459aa32835SJeff Kirsher .resume = ibmveth_resume
19469aa32835SJeff Kirsher };
19479aa32835SJeff Kirsher
19489aa32835SJeff Kirsher static struct vio_driver ibmveth_driver = {
19499aa32835SJeff Kirsher .id_table = ibmveth_device_table,
19509aa32835SJeff Kirsher .probe = ibmveth_probe,
19519aa32835SJeff Kirsher .remove = ibmveth_remove,
19529aa32835SJeff Kirsher .get_desired_dma = ibmveth_get_desired_dma,
19539aa32835SJeff Kirsher .name = ibmveth_driver_name,
19549aa32835SJeff Kirsher .pm = &ibmveth_pm_ops,
19559aa32835SJeff Kirsher };
19569aa32835SJeff Kirsher
ibmveth_module_init(void)19579aa32835SJeff Kirsher static int __init ibmveth_module_init(void)
19589aa32835SJeff Kirsher {
19599aa32835SJeff Kirsher printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
19609aa32835SJeff Kirsher ibmveth_driver_string, ibmveth_driver_version);
19619aa32835SJeff Kirsher
19629aa32835SJeff Kirsher return vio_register_driver(&ibmveth_driver);
19639aa32835SJeff Kirsher }
19649aa32835SJeff Kirsher
ibmveth_module_exit(void)19659aa32835SJeff Kirsher static void __exit ibmveth_module_exit(void)
19669aa32835SJeff Kirsher {
19679aa32835SJeff Kirsher vio_unregister_driver(&ibmveth_driver);
19689aa32835SJeff Kirsher }
19699aa32835SJeff Kirsher
19709aa32835SJeff Kirsher module_init(ibmveth_module_init);
19719aa32835SJeff Kirsher module_exit(ibmveth_module_exit);
1972