linux/drivers/net/ethernet/ibm/ibmvnic.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/**************************************************************************/
   3/*                                                                        */
   4/*  IBM System i and System p Virtual NIC Device Driver                   */
   5/*  Copyright (C) 2014 IBM Corp.                                          */
   6/*  Santiago Leon (santi_leon@yahoo.com)                                  */
   7/*  Thomas Falcon (tlfalcon@linux.vnet.ibm.com)                           */
   8/*  John Allen (jallen@linux.vnet.ibm.com)                                */
   9/*                                                                        */
  10/*                                                                        */
  11/* This module contains the implementation of a virtual ethernet device   */
  12/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN    */
  13/* option of the RS/6000 Platform Architecture to interface with virtual  */
  14/* ethernet NICs that are presented to the partition by the hypervisor.   */
  15/*                                                                         */
  16/* Messages are passed between the VNIC driver and the VNIC server using  */
  17/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to  */
  18/* issue and receive commands that initiate communication with the server */
  19/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but    */
  20/* are used by the driver to notify the server that a packet is           */
  21/* ready for transmission or that a buffer has been added to receive a    */
  22/* packet. Subsequently, sCRQs are used by the server to notify the       */
  23/* driver that a packet transmission has been completed or that a packet  */
  24/* has been received and placed in a waiting buffer.                      */
  25/*                                                                        */
  26/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in    */
  27/* which skbs are DMA mapped and immediately unmapped when the transmit   */
  28/* or receive has been completed, the VNIC driver is required to use      */
  29/* "long term mapping". This entails that large, continuous DMA mapped    */
  30/* buffers are allocated on driver initialization and these buffers are   */
  31/* then continuously reused to pass skbs to and from the VNIC server.     */
  32/*                                                                        */
  33/**************************************************************************/
  34
  35#include <linux/module.h>
  36#include <linux/moduleparam.h>
  37#include <linux/types.h>
  38#include <linux/errno.h>
  39#include <linux/completion.h>
  40#include <linux/ioport.h>
  41#include <linux/dma-mapping.h>
  42#include <linux/kernel.h>
  43#include <linux/netdevice.h>
  44#include <linux/etherdevice.h>
  45#include <linux/skbuff.h>
  46#include <linux/init.h>
  47#include <linux/delay.h>
  48#include <linux/mm.h>
  49#include <linux/ethtool.h>
  50#include <linux/proc_fs.h>
  51#include <linux/if_arp.h>
  52#include <linux/in.h>
  53#include <linux/ip.h>
  54#include <linux/ipv6.h>
  55#include <linux/irq.h>
  56#include <linux/kthread.h>
  57#include <linux/seq_file.h>
  58#include <linux/interrupt.h>
  59#include <net/net_namespace.h>
  60#include <asm/hvcall.h>
  61#include <linux/atomic.h>
  62#include <asm/vio.h>
  63#include <asm/iommu.h>
  64#include <linux/uaccess.h>
  65#include <asm/firmware.h>
  66#include <linux/workqueue.h>
  67#include <linux/if_vlan.h>
  68#include <linux/utsname.h>
  69
  70#include "ibmvnic.h"
  71
  72static const char ibmvnic_driver_name[] = "ibmvnic";
  73static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
  74
  75MODULE_AUTHOR("Santiago Leon");
  76MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
  77MODULE_LICENSE("GPL");
  78MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
  79
  80static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
  81static void release_sub_crqs(struct ibmvnic_adapter *, bool);
  82static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
  83static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
  84static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
  85static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
  86static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
  87static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
  88static int enable_scrq_irq(struct ibmvnic_adapter *,
  89                           struct ibmvnic_sub_crq_queue *);
  90static int disable_scrq_irq(struct ibmvnic_adapter *,
  91                            struct ibmvnic_sub_crq_queue *);
  92static int pending_scrq(struct ibmvnic_adapter *,
  93                        struct ibmvnic_sub_crq_queue *);
  94static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
  95                                        struct ibmvnic_sub_crq_queue *);
  96static int ibmvnic_poll(struct napi_struct *napi, int data);
  97static void send_query_map(struct ibmvnic_adapter *adapter);
  98static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
  99static int send_request_unmap(struct ibmvnic_adapter *, u8);
 100static int send_login(struct ibmvnic_adapter *adapter);
 101static void send_query_cap(struct ibmvnic_adapter *adapter);
 102static int init_sub_crqs(struct ibmvnic_adapter *);
 103static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
 104static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
 105static void release_crq_queue(struct ibmvnic_adapter *);
 106static int __ibmvnic_set_mac(struct net_device *, u8 *);
 107static int init_crq_queue(struct ibmvnic_adapter *adapter);
 108static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
 109static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
 110                                         struct ibmvnic_sub_crq_queue *tx_scrq);
 111
 112struct ibmvnic_stat {
 113        char name[ETH_GSTRING_LEN];
 114        int offset;
 115};
 116
 117#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
 118                             offsetof(struct ibmvnic_statistics, stat))
 119#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
 120
 121static const struct ibmvnic_stat ibmvnic_stats[] = {
 122        {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
 123        {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
 124        {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
 125        {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
 126        {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
 127        {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
 128        {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
 129        {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
 130        {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
 131        {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
 132        {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
 133        {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
 134        {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
 135        {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
 136        {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
 137        {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
 138        {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
 139        {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
 140        {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
 141        {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
 142        {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
 143        {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
 144};
 145
 146static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
 147                          unsigned long length, unsigned long *number,
 148                          unsigned long *irq)
 149{
 150        unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
 151        long rc;
 152
 153        rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
 154        *number = retbuf[0];
 155        *irq = retbuf[1];
 156
 157        return rc;
 158}
 159
 160/**
 161 * ibmvnic_wait_for_completion - Check device state and wait for completion
 162 * @adapter: private device data
 163 * @comp_done: completion structure to wait for
 164 * @timeout: time to wait in milliseconds
 165 *
 166 * Wait for a completion signal or until the timeout limit is reached
 167 * while checking that the device is still active.
 168 */
 169static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
 170                                       struct completion *comp_done,
 171                                       unsigned long timeout)
 172{
 173        struct net_device *netdev;
 174        unsigned long div_timeout;
 175        u8 retry;
 176
 177        netdev = adapter->netdev;
 178        retry = 5;
 179        div_timeout = msecs_to_jiffies(timeout / retry);
 180        while (true) {
 181                if (!adapter->crq.active) {
 182                        netdev_err(netdev, "Device down!\n");
 183                        return -ENODEV;
 184                }
 185                if (!retry--)
 186                        break;
 187                if (wait_for_completion_timeout(comp_done, div_timeout))
 188                        return 0;
 189        }
 190        netdev_err(netdev, "Operation timed out.\n");
 191        return -ETIMEDOUT;
 192}
 193
 194static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
 195                                struct ibmvnic_long_term_buff *ltb, int size)
 196{
 197        struct device *dev = &adapter->vdev->dev;
 198        int rc;
 199
 200        ltb->size = size;
 201        ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
 202                                       GFP_KERNEL);
 203
 204        if (!ltb->buff) {
 205                dev_err(dev, "Couldn't alloc long term buffer\n");
 206                return -ENOMEM;
 207        }
 208        ltb->map_id = adapter->map_id;
 209        adapter->map_id++;
 210
 211        mutex_lock(&adapter->fw_lock);
 212        adapter->fw_done_rc = 0;
 213        reinit_completion(&adapter->fw_done);
 214
 215        rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
 216        if (rc) {
 217                dev_err(dev, "send_request_map failed, rc = %d\n", rc);
 218                goto out;
 219        }
 220
 221        rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
 222        if (rc) {
 223                dev_err(dev,
 224                        "Long term map request aborted or timed out,rc = %d\n",
 225                        rc);
 226                goto out;
 227        }
 228
 229        if (adapter->fw_done_rc) {
 230                dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
 231                        adapter->fw_done_rc);
 232                rc = -1;
 233                goto out;
 234        }
 235        rc = 0;
 236out:
 237        if (rc) {
 238                dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
 239                ltb->buff = NULL;
 240        }
 241        mutex_unlock(&adapter->fw_lock);
 242        return rc;
 243}
 244
 245static void free_long_term_buff(struct ibmvnic_adapter *adapter,
 246                                struct ibmvnic_long_term_buff *ltb)
 247{
 248        struct device *dev = &adapter->vdev->dev;
 249
 250        if (!ltb->buff)
 251                return;
 252
 253        /* VIOS automatically unmaps the long term buffer at remote
 254         * end for the following resets:
 255         * FAILOVER, MOBILITY, TIMEOUT.
 256         */
 257        if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
 258            adapter->reset_reason != VNIC_RESET_MOBILITY &&
 259            adapter->reset_reason != VNIC_RESET_TIMEOUT)
 260                send_request_unmap(adapter, ltb->map_id);
 261        dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
 262        ltb->buff = NULL;
 263        ltb->map_id = 0;
 264}
 265
 266static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
 267                                struct ibmvnic_long_term_buff *ltb)
 268{
 269        struct device *dev = &adapter->vdev->dev;
 270        int rc;
 271
 272        memset(ltb->buff, 0, ltb->size);
 273
 274        mutex_lock(&adapter->fw_lock);
 275        adapter->fw_done_rc = 0;
 276
 277        reinit_completion(&adapter->fw_done);
 278        rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
 279        if (rc) {
 280                mutex_unlock(&adapter->fw_lock);
 281                return rc;
 282        }
 283
 284        rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
 285        if (rc) {
 286                dev_info(dev,
 287                         "Reset failed, long term map request timed out or aborted\n");
 288                mutex_unlock(&adapter->fw_lock);
 289                return rc;
 290        }
 291
 292        if (adapter->fw_done_rc) {
 293                dev_info(dev,
 294                         "Reset failed, attempting to free and reallocate buffer\n");
 295                free_long_term_buff(adapter, ltb);
 296                mutex_unlock(&adapter->fw_lock);
 297                return alloc_long_term_buff(adapter, ltb, ltb->size);
 298        }
 299        mutex_unlock(&adapter->fw_lock);
 300        return 0;
 301}
 302
 303static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
 304{
 305        int i;
 306
 307        for (i = 0; i < adapter->num_active_rx_pools; i++)
 308                adapter->rx_pool[i].active = 0;
 309}
 310
 311static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
 312                              struct ibmvnic_rx_pool *pool)
 313{
 314        int count = pool->size - atomic_read(&pool->available);
 315        u64 handle = adapter->rx_scrq[pool->index]->handle;
 316        struct device *dev = &adapter->vdev->dev;
 317        struct ibmvnic_ind_xmit_queue *ind_bufp;
 318        struct ibmvnic_sub_crq_queue *rx_scrq;
 319        union sub_crq *sub_crq;
 320        int buffers_added = 0;
 321        unsigned long lpar_rc;
 322        struct sk_buff *skb;
 323        unsigned int offset;
 324        dma_addr_t dma_addr;
 325        unsigned char *dst;
 326        int shift = 0;
 327        int index;
 328        int i;
 329
 330        if (!pool->active)
 331                return;
 332
 333        rx_scrq = adapter->rx_scrq[pool->index];
 334        ind_bufp = &rx_scrq->ind_buf;
 335
 336        /* netdev_skb_alloc() could have failed after we saved a few skbs
 337         * in the indir_buf and we would not have sent them to VIOS yet.
 338         * To account for them, start the loop at ind_bufp->index rather
 339         * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will
 340         * be 0.
 341         */
 342        for (i = ind_bufp->index; i < count; ++i) {
 343                skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
 344                if (!skb) {
 345                        dev_err(dev, "Couldn't replenish rx buff\n");
 346                        adapter->replenish_no_mem++;
 347                        break;
 348                }
 349
 350                index = pool->free_map[pool->next_free];
 351
 352                if (pool->rx_buff[index].skb)
 353                        dev_err(dev, "Inconsistent free_map!\n");
 354
 355                /* Copy the skb to the long term mapped DMA buffer */
 356                offset = index * pool->buff_size;
 357                dst = pool->long_term_buff.buff + offset;
 358                memset(dst, 0, pool->buff_size);
 359                dma_addr = pool->long_term_buff.addr + offset;
 360                pool->rx_buff[index].data = dst;
 361
 362                pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
 363                pool->rx_buff[index].dma = dma_addr;
 364                pool->rx_buff[index].skb = skb;
 365                pool->rx_buff[index].pool_index = pool->index;
 366                pool->rx_buff[index].size = pool->buff_size;
 367
 368                sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
 369                memset(sub_crq, 0, sizeof(*sub_crq));
 370                sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
 371                sub_crq->rx_add.correlator =
 372                    cpu_to_be64((u64)&pool->rx_buff[index]);
 373                sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
 374                sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
 375
 376                /* The length field of the sCRQ is defined to be 24 bits so the
 377                 * buffer size needs to be left shifted by a byte before it is
 378                 * converted to big endian to prevent the last byte from being
 379                 * truncated.
 380                 */
 381#ifdef __LITTLE_ENDIAN__
 382                shift = 8;
 383#endif
 384                sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
 385                pool->next_free = (pool->next_free + 1) % pool->size;
 386                if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
 387                    i == count - 1) {
 388                        lpar_rc =
 389                                send_subcrq_indirect(adapter, handle,
 390                                                     (u64)ind_bufp->indir_dma,
 391                                                     (u64)ind_bufp->index);
 392                        if (lpar_rc != H_SUCCESS)
 393                                goto failure;
 394                        buffers_added += ind_bufp->index;
 395                        adapter->replenish_add_buff_success += ind_bufp->index;
 396                        ind_bufp->index = 0;
 397                }
 398        }
 399        atomic_add(buffers_added, &pool->available);
 400        return;
 401
 402failure:
 403        if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
 404                dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
 405        for (i = ind_bufp->index - 1; i >= 0; --i) {
 406                struct ibmvnic_rx_buff *rx_buff;
 407
 408                pool->next_free = pool->next_free == 0 ?
 409                                  pool->size - 1 : pool->next_free - 1;
 410                sub_crq = &ind_bufp->indir_arr[i];
 411                rx_buff = (struct ibmvnic_rx_buff *)
 412                                be64_to_cpu(sub_crq->rx_add.correlator);
 413                index = (int)(rx_buff - pool->rx_buff);
 414                pool->free_map[pool->next_free] = index;
 415                dev_kfree_skb_any(pool->rx_buff[index].skb);
 416                pool->rx_buff[index].skb = NULL;
 417        }
 418        adapter->replenish_add_buff_failure += ind_bufp->index;
 419        atomic_add(buffers_added, &pool->available);
 420        ind_bufp->index = 0;
 421        if (lpar_rc == H_CLOSED || adapter->failover_pending) {
 422                /* Disable buffer pool replenishment and report carrier off if
 423                 * queue is closed or pending failover.
 424                 * Firmware guarantees that a signal will be sent to the
 425                 * driver, triggering a reset.
 426                 */
 427                deactivate_rx_pools(adapter);
 428                netif_carrier_off(adapter->netdev);
 429        }
 430}
 431
 432static void replenish_pools(struct ibmvnic_adapter *adapter)
 433{
 434        int i;
 435
 436        adapter->replenish_task_cycles++;
 437        for (i = 0; i < adapter->num_active_rx_pools; i++) {
 438                if (adapter->rx_pool[i].active)
 439                        replenish_rx_pool(adapter, &adapter->rx_pool[i]);
 440        }
 441
 442        netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
 443}
 444
 445static void release_stats_buffers(struct ibmvnic_adapter *adapter)
 446{
 447        kfree(adapter->tx_stats_buffers);
 448        kfree(adapter->rx_stats_buffers);
 449        adapter->tx_stats_buffers = NULL;
 450        adapter->rx_stats_buffers = NULL;
 451}
 452
 453static int init_stats_buffers(struct ibmvnic_adapter *adapter)
 454{
 455        adapter->tx_stats_buffers =
 456                                kcalloc(IBMVNIC_MAX_QUEUES,
 457                                        sizeof(struct ibmvnic_tx_queue_stats),
 458                                        GFP_KERNEL);
 459        if (!adapter->tx_stats_buffers)
 460                return -ENOMEM;
 461
 462        adapter->rx_stats_buffers =
 463                                kcalloc(IBMVNIC_MAX_QUEUES,
 464                                        sizeof(struct ibmvnic_rx_queue_stats),
 465                                        GFP_KERNEL);
 466        if (!adapter->rx_stats_buffers)
 467                return -ENOMEM;
 468
 469        return 0;
 470}
 471
 472static void release_stats_token(struct ibmvnic_adapter *adapter)
 473{
 474        struct device *dev = &adapter->vdev->dev;
 475
 476        if (!adapter->stats_token)
 477                return;
 478
 479        dma_unmap_single(dev, adapter->stats_token,
 480                         sizeof(struct ibmvnic_statistics),
 481                         DMA_FROM_DEVICE);
 482        adapter->stats_token = 0;
 483}
 484
 485static int init_stats_token(struct ibmvnic_adapter *adapter)
 486{
 487        struct device *dev = &adapter->vdev->dev;
 488        dma_addr_t stok;
 489
 490        stok = dma_map_single(dev, &adapter->stats,
 491                              sizeof(struct ibmvnic_statistics),
 492                              DMA_FROM_DEVICE);
 493        if (dma_mapping_error(dev, stok)) {
 494                dev_err(dev, "Couldn't map stats buffer\n");
 495                return -1;
 496        }
 497
 498        adapter->stats_token = stok;
 499        netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
 500        return 0;
 501}
 502
 503static int reset_rx_pools(struct ibmvnic_adapter *adapter)
 504{
 505        struct ibmvnic_rx_pool *rx_pool;
 506        u64 buff_size;
 507        int rx_scrqs;
 508        int i, j, rc;
 509
 510        if (!adapter->rx_pool)
 511                return -1;
 512
 513        buff_size = adapter->cur_rx_buf_sz;
 514        rx_scrqs = adapter->num_active_rx_pools;
 515        for (i = 0; i < rx_scrqs; i++) {
 516                rx_pool = &adapter->rx_pool[i];
 517
 518                netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
 519
 520                if (rx_pool->buff_size != buff_size) {
 521                        free_long_term_buff(adapter, &rx_pool->long_term_buff);
 522                        rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
 523                        rc = alloc_long_term_buff(adapter,
 524                                                  &rx_pool->long_term_buff,
 525                                                  rx_pool->size *
 526                                                  rx_pool->buff_size);
 527                } else {
 528                        rc = reset_long_term_buff(adapter,
 529                                                  &rx_pool->long_term_buff);
 530                }
 531
 532                if (rc)
 533                        return rc;
 534
 535                for (j = 0; j < rx_pool->size; j++)
 536                        rx_pool->free_map[j] = j;
 537
 538                memset(rx_pool->rx_buff, 0,
 539                       rx_pool->size * sizeof(struct ibmvnic_rx_buff));
 540
 541                atomic_set(&rx_pool->available, 0);
 542                rx_pool->next_alloc = 0;
 543                rx_pool->next_free = 0;
 544                rx_pool->active = 1;
 545        }
 546
 547        return 0;
 548}
 549
 550static void release_rx_pools(struct ibmvnic_adapter *adapter)
 551{
 552        struct ibmvnic_rx_pool *rx_pool;
 553        int i, j;
 554
 555        if (!adapter->rx_pool)
 556                return;
 557
 558        for (i = 0; i < adapter->num_active_rx_pools; i++) {
 559                rx_pool = &adapter->rx_pool[i];
 560
 561                netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
 562
 563                kfree(rx_pool->free_map);
 564                free_long_term_buff(adapter, &rx_pool->long_term_buff);
 565
 566                if (!rx_pool->rx_buff)
 567                        continue;
 568
 569                for (j = 0; j < rx_pool->size; j++) {
 570                        if (rx_pool->rx_buff[j].skb) {
 571                                dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
 572                                rx_pool->rx_buff[j].skb = NULL;
 573                        }
 574                }
 575
 576                kfree(rx_pool->rx_buff);
 577        }
 578
 579        kfree(adapter->rx_pool);
 580        adapter->rx_pool = NULL;
 581        adapter->num_active_rx_pools = 0;
 582}
 583
 584static int init_rx_pools(struct net_device *netdev)
 585{
 586        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 587        struct device *dev = &adapter->vdev->dev;
 588        struct ibmvnic_rx_pool *rx_pool;
 589        int rxadd_subcrqs;
 590        u64 buff_size;
 591        int i, j;
 592
 593        rxadd_subcrqs = adapter->num_active_rx_scrqs;
 594        buff_size = adapter->cur_rx_buf_sz;
 595
 596        adapter->rx_pool = kcalloc(rxadd_subcrqs,
 597                                   sizeof(struct ibmvnic_rx_pool),
 598                                   GFP_KERNEL);
 599        if (!adapter->rx_pool) {
 600                dev_err(dev, "Failed to allocate rx pools\n");
 601                return -1;
 602        }
 603
 604        adapter->num_active_rx_pools = rxadd_subcrqs;
 605
 606        for (i = 0; i < rxadd_subcrqs; i++) {
 607                rx_pool = &adapter->rx_pool[i];
 608
 609                netdev_dbg(adapter->netdev,
 610                           "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
 611                           i, adapter->req_rx_add_entries_per_subcrq,
 612                           buff_size);
 613
 614                rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
 615                rx_pool->index = i;
 616                rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
 617                rx_pool->active = 1;
 618
 619                rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
 620                                            GFP_KERNEL);
 621                if (!rx_pool->free_map) {
 622                        release_rx_pools(adapter);
 623                        return -1;
 624                }
 625
 626                rx_pool->rx_buff = kcalloc(rx_pool->size,
 627                                           sizeof(struct ibmvnic_rx_buff),
 628                                           GFP_KERNEL);
 629                if (!rx_pool->rx_buff) {
 630                        dev_err(dev, "Couldn't alloc rx buffers\n");
 631                        release_rx_pools(adapter);
 632                        return -1;
 633                }
 634
 635                if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
 636                                         rx_pool->size * rx_pool->buff_size)) {
 637                        release_rx_pools(adapter);
 638                        return -1;
 639                }
 640
 641                for (j = 0; j < rx_pool->size; ++j)
 642                        rx_pool->free_map[j] = j;
 643
 644                atomic_set(&rx_pool->available, 0);
 645                rx_pool->next_alloc = 0;
 646                rx_pool->next_free = 0;
 647        }
 648
 649        return 0;
 650}
 651
 652static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
 653                             struct ibmvnic_tx_pool *tx_pool)
 654{
 655        int rc, i;
 656
 657        rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
 658        if (rc)
 659                return rc;
 660
 661        memset(tx_pool->tx_buff, 0,
 662               tx_pool->num_buffers *
 663               sizeof(struct ibmvnic_tx_buff));
 664
 665        for (i = 0; i < tx_pool->num_buffers; i++)
 666                tx_pool->free_map[i] = i;
 667
 668        tx_pool->consumer_index = 0;
 669        tx_pool->producer_index = 0;
 670
 671        return 0;
 672}
 673
 674static int reset_tx_pools(struct ibmvnic_adapter *adapter)
 675{
 676        int tx_scrqs;
 677        int i, rc;
 678
 679        if (!adapter->tx_pool)
 680                return -1;
 681
 682        tx_scrqs = adapter->num_active_tx_pools;
 683        for (i = 0; i < tx_scrqs; i++) {
 684                ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
 685                rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
 686                if (rc)
 687                        return rc;
 688                rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
 689                if (rc)
 690                        return rc;
 691        }
 692
 693        return 0;
 694}
 695
 696static void release_vpd_data(struct ibmvnic_adapter *adapter)
 697{
 698        if (!adapter->vpd)
 699                return;
 700
 701        kfree(adapter->vpd->buff);
 702        kfree(adapter->vpd);
 703
 704        adapter->vpd = NULL;
 705}
 706
 707static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
 708                                struct ibmvnic_tx_pool *tx_pool)
 709{
 710        kfree(tx_pool->tx_buff);
 711        kfree(tx_pool->free_map);
 712        free_long_term_buff(adapter, &tx_pool->long_term_buff);
 713}
 714
 715static void release_tx_pools(struct ibmvnic_adapter *adapter)
 716{
 717        int i;
 718
 719        if (!adapter->tx_pool)
 720                return;
 721
 722        for (i = 0; i < adapter->num_active_tx_pools; i++) {
 723                release_one_tx_pool(adapter, &adapter->tx_pool[i]);
 724                release_one_tx_pool(adapter, &adapter->tso_pool[i]);
 725        }
 726
 727        kfree(adapter->tx_pool);
 728        adapter->tx_pool = NULL;
 729        kfree(adapter->tso_pool);
 730        adapter->tso_pool = NULL;
 731        adapter->num_active_tx_pools = 0;
 732}
 733
 734static int init_one_tx_pool(struct net_device *netdev,
 735                            struct ibmvnic_tx_pool *tx_pool,
 736                            int num_entries, int buf_size)
 737{
 738        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 739        int i;
 740
 741        tx_pool->tx_buff = kcalloc(num_entries,
 742                                   sizeof(struct ibmvnic_tx_buff),
 743                                   GFP_KERNEL);
 744        if (!tx_pool->tx_buff)
 745                return -1;
 746
 747        if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
 748                                 num_entries * buf_size))
 749                return -1;
 750
 751        tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
 752        if (!tx_pool->free_map)
 753                return -1;
 754
 755        for (i = 0; i < num_entries; i++)
 756                tx_pool->free_map[i] = i;
 757
 758        tx_pool->consumer_index = 0;
 759        tx_pool->producer_index = 0;
 760        tx_pool->num_buffers = num_entries;
 761        tx_pool->buf_size = buf_size;
 762
 763        return 0;
 764}
 765
 766static int init_tx_pools(struct net_device *netdev)
 767{
 768        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 769        int tx_subcrqs;
 770        u64 buff_size;
 771        int i, rc;
 772
 773        tx_subcrqs = adapter->num_active_tx_scrqs;
 774        adapter->tx_pool = kcalloc(tx_subcrqs,
 775                                   sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
 776        if (!adapter->tx_pool)
 777                return -1;
 778
 779        adapter->tso_pool = kcalloc(tx_subcrqs,
 780                                    sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
 781        if (!adapter->tso_pool) {
 782                kfree(adapter->tx_pool);
 783                adapter->tx_pool = NULL;
 784                return -1;
 785        }
 786
 787        adapter->num_active_tx_pools = tx_subcrqs;
 788
 789        for (i = 0; i < tx_subcrqs; i++) {
 790                buff_size = adapter->req_mtu + VLAN_HLEN;
 791                buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
 792                rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
 793                                      adapter->req_tx_entries_per_subcrq,
 794                                      buff_size);
 795                if (rc) {
 796                        release_tx_pools(adapter);
 797                        return rc;
 798                }
 799
 800                rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
 801                                      IBMVNIC_TSO_BUFS,
 802                                      IBMVNIC_TSO_BUF_SZ);
 803                if (rc) {
 804                        release_tx_pools(adapter);
 805                        return rc;
 806                }
 807        }
 808
 809        return 0;
 810}
 811
 812static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
 813{
 814        int i;
 815
 816        if (adapter->napi_enabled)
 817                return;
 818
 819        for (i = 0; i < adapter->req_rx_queues; i++)
 820                napi_enable(&adapter->napi[i]);
 821
 822        adapter->napi_enabled = true;
 823}
 824
 825static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
 826{
 827        int i;
 828
 829        if (!adapter->napi_enabled)
 830                return;
 831
 832        for (i = 0; i < adapter->req_rx_queues; i++) {
 833                netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
 834                napi_disable(&adapter->napi[i]);
 835        }
 836
 837        adapter->napi_enabled = false;
 838}
 839
 840static int init_napi(struct ibmvnic_adapter *adapter)
 841{
 842        int i;
 843
 844        adapter->napi = kcalloc(adapter->req_rx_queues,
 845                                sizeof(struct napi_struct), GFP_KERNEL);
 846        if (!adapter->napi)
 847                return -ENOMEM;
 848
 849        for (i = 0; i < adapter->req_rx_queues; i++) {
 850                netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
 851                netif_napi_add(adapter->netdev, &adapter->napi[i],
 852                               ibmvnic_poll, NAPI_POLL_WEIGHT);
 853        }
 854
 855        adapter->num_active_rx_napi = adapter->req_rx_queues;
 856        return 0;
 857}
 858
 859static void release_napi(struct ibmvnic_adapter *adapter)
 860{
 861        int i;
 862
 863        if (!adapter->napi)
 864                return;
 865
 866        for (i = 0; i < adapter->num_active_rx_napi; i++) {
 867                netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
 868                netif_napi_del(&adapter->napi[i]);
 869        }
 870
 871        kfree(adapter->napi);
 872        adapter->napi = NULL;
 873        adapter->num_active_rx_napi = 0;
 874        adapter->napi_enabled = false;
 875}
 876
 877static const char *adapter_state_to_string(enum vnic_state state)
 878{
 879        switch (state) {
 880        case VNIC_PROBING:
 881                return "PROBING";
 882        case VNIC_PROBED:
 883                return "PROBED";
 884        case VNIC_OPENING:
 885                return "OPENING";
 886        case VNIC_OPEN:
 887                return "OPEN";
 888        case VNIC_CLOSING:
 889                return "CLOSING";
 890        case VNIC_CLOSED:
 891                return "CLOSED";
 892        case VNIC_REMOVING:
 893                return "REMOVING";
 894        case VNIC_REMOVED:
 895                return "REMOVED";
 896        default:
 897                return "UNKNOWN";
 898        }
 899}
 900
 901static int ibmvnic_login(struct net_device *netdev)
 902{
 903        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
 904        unsigned long timeout = msecs_to_jiffies(20000);
 905        int retry_count = 0;
 906        int retries = 10;
 907        bool retry;
 908        int rc;
 909
 910        do {
 911                retry = false;
 912                if (retry_count > retries) {
 913                        netdev_warn(netdev, "Login attempts exceeded\n");
 914                        return -1;
 915                }
 916
 917                adapter->init_done_rc = 0;
 918                reinit_completion(&adapter->init_done);
 919                rc = send_login(adapter);
 920                if (rc)
 921                        return rc;
 922
 923                if (!wait_for_completion_timeout(&adapter->init_done,
 924                                                 timeout)) {
 925                        netdev_warn(netdev, "Login timed out, retrying...\n");
 926                        retry = true;
 927                        adapter->init_done_rc = 0;
 928                        retry_count++;
 929                        continue;
 930                }
 931
 932                if (adapter->init_done_rc == ABORTED) {
 933                        netdev_warn(netdev, "Login aborted, retrying...\n");
 934                        retry = true;
 935                        adapter->init_done_rc = 0;
 936                        retry_count++;
 937                        /* FW or device may be busy, so
 938                         * wait a bit before retrying login
 939                         */
 940                        msleep(500);
 941                } else if (adapter->init_done_rc == PARTIALSUCCESS) {
 942                        retry_count++;
 943                        release_sub_crqs(adapter, 1);
 944
 945                        retry = true;
 946                        netdev_dbg(netdev,
 947                                   "Received partial success, retrying...\n");
 948                        adapter->init_done_rc = 0;
 949                        reinit_completion(&adapter->init_done);
 950                        send_query_cap(adapter);
 951                        if (!wait_for_completion_timeout(&adapter->init_done,
 952                                                         timeout)) {
 953                                netdev_warn(netdev,
 954                                            "Capabilities query timed out\n");
 955                                return -1;
 956                        }
 957
 958                        rc = init_sub_crqs(adapter);
 959                        if (rc) {
 960                                netdev_warn(netdev,
 961                                            "SCRQ initialization failed\n");
 962                                return -1;
 963                        }
 964
 965                        rc = init_sub_crq_irqs(adapter);
 966                        if (rc) {
 967                                netdev_warn(netdev,
 968                                            "SCRQ irq initialization failed\n");
 969                                return -1;
 970                        }
 971                } else if (adapter->init_done_rc) {
 972                        netdev_warn(netdev, "Adapter login failed\n");
 973                        return -1;
 974                }
 975        } while (retry);
 976
 977        __ibmvnic_set_mac(netdev, adapter->mac_addr);
 978
 979        netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
 980        return 0;
 981}
 982
 983static void release_login_buffer(struct ibmvnic_adapter *adapter)
 984{
 985        kfree(adapter->login_buf);
 986        adapter->login_buf = NULL;
 987}
 988
 989static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
 990{
 991        kfree(adapter->login_rsp_buf);
 992        adapter->login_rsp_buf = NULL;
 993}
 994
 995static void release_resources(struct ibmvnic_adapter *adapter)
 996{
 997        release_vpd_data(adapter);
 998
 999        release_tx_pools(adapter);
1000        release_rx_pools(adapter);
1001
1002        release_napi(adapter);
1003        release_login_buffer(adapter);
1004        release_login_rsp_buffer(adapter);
1005}
1006
1007static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1008{
1009        struct net_device *netdev = adapter->netdev;
1010        unsigned long timeout = msecs_to_jiffies(20000);
1011        union ibmvnic_crq crq;
1012        bool resend;
1013        int rc;
1014
1015        netdev_dbg(netdev, "setting link state %d\n", link_state);
1016
1017        memset(&crq, 0, sizeof(crq));
1018        crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1019        crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1020        crq.logical_link_state.link_state = link_state;
1021
1022        do {
1023                resend = false;
1024
1025                reinit_completion(&adapter->init_done);
1026                rc = ibmvnic_send_crq(adapter, &crq);
1027                if (rc) {
1028                        netdev_err(netdev, "Failed to set link state\n");
1029                        return rc;
1030                }
1031
1032                if (!wait_for_completion_timeout(&adapter->init_done,
1033                                                 timeout)) {
1034                        netdev_err(netdev, "timeout setting link state\n");
1035                        return -1;
1036                }
1037
1038                if (adapter->init_done_rc == PARTIALSUCCESS) {
1039                        /* Partuial success, delay and re-send */
1040                        mdelay(1000);
1041                        resend = true;
1042                } else if (adapter->init_done_rc) {
1043                        netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1044                                    adapter->init_done_rc);
1045                        return adapter->init_done_rc;
1046                }
1047        } while (resend);
1048
1049        return 0;
1050}
1051
1052static int set_real_num_queues(struct net_device *netdev)
1053{
1054        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1055        int rc;
1056
1057        netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1058                   adapter->req_tx_queues, adapter->req_rx_queues);
1059
1060        rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1061        if (rc) {
1062                netdev_err(netdev, "failed to set the number of tx queues\n");
1063                return rc;
1064        }
1065
1066        rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1067        if (rc)
1068                netdev_err(netdev, "failed to set the number of rx queues\n");
1069
1070        return rc;
1071}
1072
1073static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1074{
1075        struct device *dev = &adapter->vdev->dev;
1076        union ibmvnic_crq crq;
1077        int len = 0;
1078        int rc;
1079
1080        if (adapter->vpd->buff)
1081                len = adapter->vpd->len;
1082
1083        mutex_lock(&adapter->fw_lock);
1084        adapter->fw_done_rc = 0;
1085        reinit_completion(&adapter->fw_done);
1086
1087        crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1088        crq.get_vpd_size.cmd = GET_VPD_SIZE;
1089        rc = ibmvnic_send_crq(adapter, &crq);
1090        if (rc) {
1091                mutex_unlock(&adapter->fw_lock);
1092                return rc;
1093        }
1094
1095        rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1096        if (rc) {
1097                dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
1098                mutex_unlock(&adapter->fw_lock);
1099                return rc;
1100        }
1101        mutex_unlock(&adapter->fw_lock);
1102
1103        if (!adapter->vpd->len)
1104                return -ENODATA;
1105
1106        if (!adapter->vpd->buff)
1107                adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1108        else if (adapter->vpd->len != len)
1109                adapter->vpd->buff =
1110                        krealloc(adapter->vpd->buff,
1111                                 adapter->vpd->len, GFP_KERNEL);
1112
1113        if (!adapter->vpd->buff) {
1114                dev_err(dev, "Could allocate VPD buffer\n");
1115                return -ENOMEM;
1116        }
1117
1118        adapter->vpd->dma_addr =
1119                dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1120                               DMA_FROM_DEVICE);
1121        if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
1122                dev_err(dev, "Could not map VPD buffer\n");
1123                kfree(adapter->vpd->buff);
1124                adapter->vpd->buff = NULL;
1125                return -ENOMEM;
1126        }
1127
1128        mutex_lock(&adapter->fw_lock);
1129        adapter->fw_done_rc = 0;
1130        reinit_completion(&adapter->fw_done);
1131
1132        crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1133        crq.get_vpd.cmd = GET_VPD;
1134        crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1135        crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1136        rc = ibmvnic_send_crq(adapter, &crq);
1137        if (rc) {
1138                kfree(adapter->vpd->buff);
1139                adapter->vpd->buff = NULL;
1140                mutex_unlock(&adapter->fw_lock);
1141                return rc;
1142        }
1143
1144        rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1145        if (rc) {
1146                dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1147                kfree(adapter->vpd->buff);
1148                adapter->vpd->buff = NULL;
1149                mutex_unlock(&adapter->fw_lock);
1150                return rc;
1151        }
1152
1153        mutex_unlock(&adapter->fw_lock);
1154        return 0;
1155}
1156
1157static int init_resources(struct ibmvnic_adapter *adapter)
1158{
1159        struct net_device *netdev = adapter->netdev;
1160        int rc;
1161
1162        rc = set_real_num_queues(netdev);
1163        if (rc)
1164                return rc;
1165
1166        adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1167        if (!adapter->vpd)
1168                return -ENOMEM;
1169
1170        /* Vital Product Data (VPD) */
1171        rc = ibmvnic_get_vpd(adapter);
1172        if (rc) {
1173                netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1174                return rc;
1175        }
1176
1177        adapter->map_id = 1;
1178
1179        rc = init_napi(adapter);
1180        if (rc)
1181                return rc;
1182
1183        send_query_map(adapter);
1184
1185        rc = init_rx_pools(netdev);
1186        if (rc)
1187                return rc;
1188
1189        rc = init_tx_pools(netdev);
1190        return rc;
1191}
1192
1193static int __ibmvnic_open(struct net_device *netdev)
1194{
1195        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1196        enum vnic_state prev_state = adapter->state;
1197        int i, rc;
1198
1199        adapter->state = VNIC_OPENING;
1200        replenish_pools(adapter);
1201        ibmvnic_napi_enable(adapter);
1202
1203        /* We're ready to receive frames, enable the sub-crq interrupts and
1204         * set the logical link state to up
1205         */
1206        for (i = 0; i < adapter->req_rx_queues; i++) {
1207                netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1208                if (prev_state == VNIC_CLOSED)
1209                        enable_irq(adapter->rx_scrq[i]->irq);
1210                enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1211        }
1212
1213        for (i = 0; i < adapter->req_tx_queues; i++) {
1214                netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1215                if (prev_state == VNIC_CLOSED)
1216                        enable_irq(adapter->tx_scrq[i]->irq);
1217                enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1218                netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
1219        }
1220
1221        rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1222        if (rc) {
1223                ibmvnic_napi_disable(adapter);
1224                release_resources(adapter);
1225                return rc;
1226        }
1227
1228        netif_tx_start_all_queues(netdev);
1229
1230        if (prev_state == VNIC_CLOSED) {
1231                for (i = 0; i < adapter->req_rx_queues; i++)
1232                        napi_schedule(&adapter->napi[i]);
1233        }
1234
1235        adapter->state = VNIC_OPEN;
1236        return rc;
1237}
1238
1239static int ibmvnic_open(struct net_device *netdev)
1240{
1241        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1242        int rc;
1243
1244        ASSERT_RTNL();
1245
1246        /* If device failover is pending or we are about to reset, just set
1247         * device state and return. Device operation will be handled by reset
1248         * routine.
1249         *
1250         * It should be safe to overwrite the adapter->state here. Since
1251         * we hold the rtnl, either the reset has not actually started or
1252         * the rtnl got dropped during the set_link_state() in do_reset().
1253         * In the former case, no one else is changing the state (again we
1254         * have the rtnl) and in the latter case, do_reset() will detect and
1255         * honor our setting below.
1256         */
1257        if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
1258                netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1259                           adapter_state_to_string(adapter->state),
1260                           adapter->failover_pending);
1261                adapter->state = VNIC_OPEN;
1262                rc = 0;
1263                goto out;
1264        }
1265
1266        if (adapter->state != VNIC_CLOSED) {
1267                rc = ibmvnic_login(netdev);
1268                if (rc)
1269                        goto out;
1270
1271                rc = init_resources(adapter);
1272                if (rc) {
1273                        netdev_err(netdev, "failed to initialize resources\n");
1274                        release_resources(adapter);
1275                        goto out;
1276                }
1277        }
1278
1279        rc = __ibmvnic_open(netdev);
1280
1281out:
1282        /* If open failed and there is a pending failover or in-progress reset,
1283         * set device state and return. Device operation will be handled by
1284         * reset routine. See also comments above regarding rtnl.
1285         */
1286        if (rc &&
1287            (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
1288                adapter->state = VNIC_OPEN;
1289                rc = 0;
1290        }
1291        return rc;
1292}
1293
1294static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1295{
1296        struct ibmvnic_rx_pool *rx_pool;
1297        struct ibmvnic_rx_buff *rx_buff;
1298        u64 rx_entries;
1299        int rx_scrqs;
1300        int i, j;
1301
1302        if (!adapter->rx_pool)
1303                return;
1304
1305        rx_scrqs = adapter->num_active_rx_pools;
1306        rx_entries = adapter->req_rx_add_entries_per_subcrq;
1307
1308        /* Free any remaining skbs in the rx buffer pools */
1309        for (i = 0; i < rx_scrqs; i++) {
1310                rx_pool = &adapter->rx_pool[i];
1311                if (!rx_pool || !rx_pool->rx_buff)
1312                        continue;
1313
1314                netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1315                for (j = 0; j < rx_entries; j++) {
1316                        rx_buff = &rx_pool->rx_buff[j];
1317                        if (rx_buff && rx_buff->skb) {
1318                                dev_kfree_skb_any(rx_buff->skb);
1319                                rx_buff->skb = NULL;
1320                        }
1321                }
1322        }
1323}
1324
1325static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1326                              struct ibmvnic_tx_pool *tx_pool)
1327{
1328        struct ibmvnic_tx_buff *tx_buff;
1329        u64 tx_entries;
1330        int i;
1331
1332        if (!tx_pool || !tx_pool->tx_buff)
1333                return;
1334
1335        tx_entries = tx_pool->num_buffers;
1336
1337        for (i = 0; i < tx_entries; i++) {
1338                tx_buff = &tx_pool->tx_buff[i];
1339                if (tx_buff && tx_buff->skb) {
1340                        dev_kfree_skb_any(tx_buff->skb);
1341                        tx_buff->skb = NULL;
1342                }
1343        }
1344}
1345
1346static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1347{
1348        int tx_scrqs;
1349        int i;
1350
1351        if (!adapter->tx_pool || !adapter->tso_pool)
1352                return;
1353
1354        tx_scrqs = adapter->num_active_tx_pools;
1355
1356        /* Free any remaining skbs in the tx buffer pools */
1357        for (i = 0; i < tx_scrqs; i++) {
1358                netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1359                clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1360                clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1361        }
1362}
1363
1364static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1365{
1366        struct net_device *netdev = adapter->netdev;
1367        int i;
1368
1369        if (adapter->tx_scrq) {
1370                for (i = 0; i < adapter->req_tx_queues; i++)
1371                        if (adapter->tx_scrq[i]->irq) {
1372                                netdev_dbg(netdev,
1373                                           "Disabling tx_scrq[%d] irq\n", i);
1374                                disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1375                                disable_irq(adapter->tx_scrq[i]->irq);
1376                        }
1377        }
1378
1379        if (adapter->rx_scrq) {
1380                for (i = 0; i < adapter->req_rx_queues; i++) {
1381                        if (adapter->rx_scrq[i]->irq) {
1382                                netdev_dbg(netdev,
1383                                           "Disabling rx_scrq[%d] irq\n", i);
1384                                disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1385                                disable_irq(adapter->rx_scrq[i]->irq);
1386                        }
1387                }
1388        }
1389}
1390
1391static void ibmvnic_cleanup(struct net_device *netdev)
1392{
1393        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1394
1395        /* ensure that transmissions are stopped if called by do_reset */
1396        if (test_bit(0, &adapter->resetting))
1397                netif_tx_disable(netdev);
1398        else
1399                netif_tx_stop_all_queues(netdev);
1400
1401        ibmvnic_napi_disable(adapter);
1402        ibmvnic_disable_irqs(adapter);
1403
1404        clean_rx_pools(adapter);
1405        clean_tx_pools(adapter);
1406}
1407
1408static int __ibmvnic_close(struct net_device *netdev)
1409{
1410        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1411        int rc = 0;
1412
1413        adapter->state = VNIC_CLOSING;
1414        rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1415        adapter->state = VNIC_CLOSED;
1416        return rc;
1417}
1418
1419static int ibmvnic_close(struct net_device *netdev)
1420{
1421        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1422        int rc;
1423
1424        netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1425                   adapter_state_to_string(adapter->state),
1426                   adapter->failover_pending,
1427                   adapter->force_reset_recovery);
1428
1429        /* If device failover is pending, just set device state and return.
1430         * Device operation will be handled by reset routine.
1431         */
1432        if (adapter->failover_pending) {
1433                adapter->state = VNIC_CLOSED;
1434                return 0;
1435        }
1436
1437        rc = __ibmvnic_close(netdev);
1438        ibmvnic_cleanup(netdev);
1439
1440        return rc;
1441}
1442
1443/**
1444 * build_hdr_data - creates L2/L3/L4 header data buffer
1445 * @hdr_field: bitfield determining needed headers
1446 * @skb: socket buffer
1447 * @hdr_len: array of header lengths
1448 * @hdr_data: buffer to write the header to
1449 *
1450 * Reads hdr_field to determine which headers are needed by firmware.
1451 * Builds a buffer containing these headers.  Saves individual header
1452 * lengths and total buffer length to be used to build descriptors.
1453 */
1454static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1455                          int *hdr_len, u8 *hdr_data)
1456{
1457        int len = 0;
1458        u8 *hdr;
1459
1460        if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1461                hdr_len[0] = sizeof(struct vlan_ethhdr);
1462        else
1463                hdr_len[0] = sizeof(struct ethhdr);
1464
1465        if (skb->protocol == htons(ETH_P_IP)) {
1466                hdr_len[1] = ip_hdr(skb)->ihl * 4;
1467                if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1468                        hdr_len[2] = tcp_hdrlen(skb);
1469                else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1470                        hdr_len[2] = sizeof(struct udphdr);
1471        } else if (skb->protocol == htons(ETH_P_IPV6)) {
1472                hdr_len[1] = sizeof(struct ipv6hdr);
1473                if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1474                        hdr_len[2] = tcp_hdrlen(skb);
1475                else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1476                        hdr_len[2] = sizeof(struct udphdr);
1477        } else if (skb->protocol == htons(ETH_P_ARP)) {
1478                hdr_len[1] = arp_hdr_len(skb->dev);
1479                hdr_len[2] = 0;
1480        }
1481
1482        memset(hdr_data, 0, 120);
1483        if ((hdr_field >> 6) & 1) {
1484                hdr = skb_mac_header(skb);
1485                memcpy(hdr_data, hdr, hdr_len[0]);
1486                len += hdr_len[0];
1487        }
1488
1489        if ((hdr_field >> 5) & 1) {
1490                hdr = skb_network_header(skb);
1491                memcpy(hdr_data + len, hdr, hdr_len[1]);
1492                len += hdr_len[1];
1493        }
1494
1495        if ((hdr_field >> 4) & 1) {
1496                hdr = skb_transport_header(skb);
1497                memcpy(hdr_data + len, hdr, hdr_len[2]);
1498                len += hdr_len[2];
1499        }
1500        return len;
1501}
1502
1503/**
1504 * create_hdr_descs - create header and header extension descriptors
1505 * @hdr_field: bitfield determining needed headers
1506 * @hdr_data: buffer containing header data
1507 * @len: length of data buffer
1508 * @hdr_len: array of individual header lengths
1509 * @scrq_arr: descriptor array
1510 *
1511 * Creates header and, if needed, header extension descriptors and
1512 * places them in a descriptor array, scrq_arr
1513 */
1514
1515static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1516                            union sub_crq *scrq_arr)
1517{
1518        union sub_crq hdr_desc;
1519        int tmp_len = len;
1520        int num_descs = 0;
1521        u8 *data, *cur;
1522        int tmp;
1523
1524        while (tmp_len > 0) {
1525                cur = hdr_data + len - tmp_len;
1526
1527                memset(&hdr_desc, 0, sizeof(hdr_desc));
1528                if (cur != hdr_data) {
1529                        data = hdr_desc.hdr_ext.data;
1530                        tmp = tmp_len > 29 ? 29 : tmp_len;
1531                        hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1532                        hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1533                        hdr_desc.hdr_ext.len = tmp;
1534                } else {
1535                        data = hdr_desc.hdr.data;
1536                        tmp = tmp_len > 24 ? 24 : tmp_len;
1537                        hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1538                        hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1539                        hdr_desc.hdr.len = tmp;
1540                        hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1541                        hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1542                        hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1543                        hdr_desc.hdr.flag = hdr_field << 1;
1544                }
1545                memcpy(data, cur, tmp);
1546                tmp_len -= tmp;
1547                *scrq_arr = hdr_desc;
1548                scrq_arr++;
1549                num_descs++;
1550        }
1551
1552        return num_descs;
1553}
1554
1555/**
1556 * build_hdr_descs_arr - build a header descriptor array
1557 * @skb: tx socket buffer
1558 * @indir_arr: indirect array
1559 * @num_entries: number of descriptors to be sent
1560 * @hdr_field: bit field determining which headers will be sent
1561 *
1562 * This function will build a TX descriptor array with applicable
1563 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1564 */
1565
1566static void build_hdr_descs_arr(struct sk_buff *skb,
1567                                union sub_crq *indir_arr,
1568                                int *num_entries, u8 hdr_field)
1569{
1570        int hdr_len[3] = {0, 0, 0};
1571        u8 hdr_data[140] = {0};
1572        int tot_len;
1573
1574        tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1575                                 hdr_data);
1576        *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1577                                         indir_arr + 1);
1578}
1579
1580static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1581                                    struct net_device *netdev)
1582{
1583        /* For some backing devices, mishandling of small packets
1584         * can result in a loss of connection or TX stall. Device
1585         * architects recommend that no packet should be smaller
1586         * than the minimum MTU value provided to the driver, so
1587         * pad any packets to that length
1588         */
1589        if (skb->len < netdev->min_mtu)
1590                return skb_put_padto(skb, netdev->min_mtu);
1591
1592        return 0;
1593}
1594
1595static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1596                                         struct ibmvnic_sub_crq_queue *tx_scrq)
1597{
1598        struct ibmvnic_ind_xmit_queue *ind_bufp;
1599        struct ibmvnic_tx_buff *tx_buff;
1600        struct ibmvnic_tx_pool *tx_pool;
1601        union sub_crq tx_scrq_entry;
1602        int queue_num;
1603        int entries;
1604        int index;
1605        int i;
1606
1607        ind_bufp = &tx_scrq->ind_buf;
1608        entries = (u64)ind_bufp->index;
1609        queue_num = tx_scrq->pool_index;
1610
1611        for (i = entries - 1; i >= 0; --i) {
1612                tx_scrq_entry = ind_bufp->indir_arr[i];
1613                if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1614                        continue;
1615                index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1616                if (index & IBMVNIC_TSO_POOL_MASK) {
1617                        tx_pool = &adapter->tso_pool[queue_num];
1618                        index &= ~IBMVNIC_TSO_POOL_MASK;
1619                } else {
1620                        tx_pool = &adapter->tx_pool[queue_num];
1621                }
1622                tx_pool->free_map[tx_pool->consumer_index] = index;
1623                tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1624                                          tx_pool->num_buffers - 1 :
1625                                          tx_pool->consumer_index - 1;
1626                tx_buff = &tx_pool->tx_buff[index];
1627                adapter->netdev->stats.tx_packets--;
1628                adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1629                adapter->tx_stats_buffers[queue_num].packets--;
1630                adapter->tx_stats_buffers[queue_num].bytes -=
1631                                                tx_buff->skb->len;
1632                dev_kfree_skb_any(tx_buff->skb);
1633                tx_buff->skb = NULL;
1634                adapter->netdev->stats.tx_dropped++;
1635        }
1636        ind_bufp->index = 0;
1637        if (atomic_sub_return(entries, &tx_scrq->used) <=
1638            (adapter->req_tx_entries_per_subcrq / 2) &&
1639            __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1640            !test_bit(0, &adapter->resetting)) {
1641                netif_wake_subqueue(adapter->netdev, queue_num);
1642                netdev_dbg(adapter->netdev, "Started queue %d\n",
1643                           queue_num);
1644        }
1645}
1646
1647static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1648                                 struct ibmvnic_sub_crq_queue *tx_scrq)
1649{
1650        struct ibmvnic_ind_xmit_queue *ind_bufp;
1651        u64 dma_addr;
1652        u64 entries;
1653        u64 handle;
1654        int rc;
1655
1656        ind_bufp = &tx_scrq->ind_buf;
1657        dma_addr = (u64)ind_bufp->indir_dma;
1658        entries = (u64)ind_bufp->index;
1659        handle = tx_scrq->handle;
1660
1661        if (!entries)
1662                return 0;
1663        rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1664        if (rc)
1665                ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1666        else
1667                ind_bufp->index = 0;
1668        return 0;
1669}
1670
1671static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1672{
1673        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1674        int queue_num = skb_get_queue_mapping(skb);
1675        u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1676        struct device *dev = &adapter->vdev->dev;
1677        struct ibmvnic_ind_xmit_queue *ind_bufp;
1678        struct ibmvnic_tx_buff *tx_buff = NULL;
1679        struct ibmvnic_sub_crq_queue *tx_scrq;
1680        struct ibmvnic_tx_pool *tx_pool;
1681        unsigned int tx_send_failed = 0;
1682        netdev_tx_t ret = NETDEV_TX_OK;
1683        unsigned int tx_map_failed = 0;
1684        union sub_crq indir_arr[16];
1685        unsigned int tx_dropped = 0;
1686        unsigned int tx_packets = 0;
1687        unsigned int tx_bytes = 0;
1688        dma_addr_t data_dma_addr;
1689        struct netdev_queue *txq;
1690        unsigned long lpar_rc;
1691        union sub_crq tx_crq;
1692        unsigned int offset;
1693        int num_entries = 1;
1694        unsigned char *dst;
1695        int index = 0;
1696        u8 proto = 0;
1697
1698        tx_scrq = adapter->tx_scrq[queue_num];
1699        txq = netdev_get_tx_queue(netdev, queue_num);
1700        ind_bufp = &tx_scrq->ind_buf;
1701
1702        if (test_bit(0, &adapter->resetting)) {
1703                if (!netif_subqueue_stopped(netdev, skb))
1704                        netif_stop_subqueue(netdev, queue_num);
1705                dev_kfree_skb_any(skb);
1706
1707                tx_send_failed++;
1708                tx_dropped++;
1709                ret = NETDEV_TX_OK;
1710                goto out;
1711        }
1712
1713        if (ibmvnic_xmit_workarounds(skb, netdev)) {
1714                tx_dropped++;
1715                tx_send_failed++;
1716                ret = NETDEV_TX_OK;
1717                ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1718                goto out;
1719        }
1720        if (skb_is_gso(skb))
1721                tx_pool = &adapter->tso_pool[queue_num];
1722        else
1723                tx_pool = &adapter->tx_pool[queue_num];
1724
1725        index = tx_pool->free_map[tx_pool->consumer_index];
1726
1727        if (index == IBMVNIC_INVALID_MAP) {
1728                dev_kfree_skb_any(skb);
1729                tx_send_failed++;
1730                tx_dropped++;
1731                ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1732                ret = NETDEV_TX_OK;
1733                goto out;
1734        }
1735
1736        tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1737
1738        offset = index * tx_pool->buf_size;
1739        dst = tx_pool->long_term_buff.buff + offset;
1740        memset(dst, 0, tx_pool->buf_size);
1741        data_dma_addr = tx_pool->long_term_buff.addr + offset;
1742
1743        if (skb_shinfo(skb)->nr_frags) {
1744                int cur, i;
1745
1746                /* Copy the head */
1747                skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1748                cur = skb_headlen(skb);
1749
1750                /* Copy the frags */
1751                for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1752                        const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1753
1754                        memcpy(dst + cur, skb_frag_address(frag),
1755                               skb_frag_size(frag));
1756                        cur += skb_frag_size(frag);
1757                }
1758        } else {
1759                skb_copy_from_linear_data(skb, dst, skb->len);
1760        }
1761
1762        /* post changes to long_term_buff *dst before VIOS accessing it */
1763        dma_wmb();
1764
1765        tx_pool->consumer_index =
1766            (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1767
1768        tx_buff = &tx_pool->tx_buff[index];
1769        tx_buff->skb = skb;
1770        tx_buff->index = index;
1771        tx_buff->pool_index = queue_num;
1772
1773        memset(&tx_crq, 0, sizeof(tx_crq));
1774        tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1775        tx_crq.v1.type = IBMVNIC_TX_DESC;
1776        tx_crq.v1.n_crq_elem = 1;
1777        tx_crq.v1.n_sge = 1;
1778        tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1779
1780        if (skb_is_gso(skb))
1781                tx_crq.v1.correlator =
1782                        cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1783        else
1784                tx_crq.v1.correlator = cpu_to_be32(index);
1785        tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1786        tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1787        tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1788
1789        if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
1790                tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1791                tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1792        }
1793
1794        if (skb->protocol == htons(ETH_P_IP)) {
1795                tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1796                proto = ip_hdr(skb)->protocol;
1797        } else if (skb->protocol == htons(ETH_P_IPV6)) {
1798                tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1799                proto = ipv6_hdr(skb)->nexthdr;
1800        }
1801
1802        if (proto == IPPROTO_TCP)
1803                tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1804        else if (proto == IPPROTO_UDP)
1805                tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1806
1807        if (skb->ip_summed == CHECKSUM_PARTIAL) {
1808                tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1809                hdrs += 2;
1810        }
1811        if (skb_is_gso(skb)) {
1812                tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1813                tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1814                hdrs += 2;
1815        }
1816
1817        if ((*hdrs >> 7) & 1)
1818                build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
1819
1820        tx_crq.v1.n_crq_elem = num_entries;
1821        tx_buff->num_entries = num_entries;
1822        /* flush buffer if current entry can not fit */
1823        if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
1824                lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1825                if (lpar_rc != H_SUCCESS)
1826                        goto tx_flush_err;
1827        }
1828
1829        indir_arr[0] = tx_crq;
1830        memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
1831               num_entries * sizeof(struct ibmvnic_generic_scrq));
1832        ind_bufp->index += num_entries;
1833        if (__netdev_tx_sent_queue(txq, skb->len,
1834                                   netdev_xmit_more() &&
1835                                   ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
1836                lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1837                if (lpar_rc != H_SUCCESS)
1838                        goto tx_err;
1839        }
1840
1841        if (atomic_add_return(num_entries, &tx_scrq->used)
1842                                        >= adapter->req_tx_entries_per_subcrq) {
1843                netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1844                netif_stop_subqueue(netdev, queue_num);
1845        }
1846
1847        tx_packets++;
1848        tx_bytes += skb->len;
1849        txq->trans_start = jiffies;
1850        ret = NETDEV_TX_OK;
1851        goto out;
1852
1853tx_flush_err:
1854        dev_kfree_skb_any(skb);
1855        tx_buff->skb = NULL;
1856        tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1857                                  tx_pool->num_buffers - 1 :
1858                                  tx_pool->consumer_index - 1;
1859        tx_dropped++;
1860tx_err:
1861        if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1862                dev_err_ratelimited(dev, "tx: send failed\n");
1863
1864        if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1865                /* Disable TX and report carrier off if queue is closed
1866                 * or pending failover.
1867                 * Firmware guarantees that a signal will be sent to the
1868                 * driver, triggering a reset or some other action.
1869                 */
1870                netif_tx_stop_all_queues(netdev);
1871                netif_carrier_off(netdev);
1872        }
1873out:
1874        netdev->stats.tx_dropped += tx_dropped;
1875        netdev->stats.tx_bytes += tx_bytes;
1876        netdev->stats.tx_packets += tx_packets;
1877        adapter->tx_send_failed += tx_send_failed;
1878        adapter->tx_map_failed += tx_map_failed;
1879        adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1880        adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1881        adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1882
1883        return ret;
1884}
1885
1886static void ibmvnic_set_multi(struct net_device *netdev)
1887{
1888        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1889        struct netdev_hw_addr *ha;
1890        union ibmvnic_crq crq;
1891
1892        memset(&crq, 0, sizeof(crq));
1893        crq.request_capability.first = IBMVNIC_CRQ_CMD;
1894        crq.request_capability.cmd = REQUEST_CAPABILITY;
1895
1896        if (netdev->flags & IFF_PROMISC) {
1897                if (!adapter->promisc_supported)
1898                        return;
1899        } else {
1900                if (netdev->flags & IFF_ALLMULTI) {
1901                        /* Accept all multicast */
1902                        memset(&crq, 0, sizeof(crq));
1903                        crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1904                        crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1905                        crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1906                        ibmvnic_send_crq(adapter, &crq);
1907                } else if (netdev_mc_empty(netdev)) {
1908                        /* Reject all multicast */
1909                        memset(&crq, 0, sizeof(crq));
1910                        crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1911                        crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1912                        crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1913                        ibmvnic_send_crq(adapter, &crq);
1914                } else {
1915                        /* Accept one or more multicast(s) */
1916                        netdev_for_each_mc_addr(ha, netdev) {
1917                                memset(&crq, 0, sizeof(crq));
1918                                crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1919                                crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1920                                crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1921                                ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1922                                                ha->addr);
1923                                ibmvnic_send_crq(adapter, &crq);
1924                        }
1925                }
1926        }
1927}
1928
1929static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
1930{
1931        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1932        union ibmvnic_crq crq;
1933        int rc;
1934
1935        if (!is_valid_ether_addr(dev_addr)) {
1936                rc = -EADDRNOTAVAIL;
1937                goto err;
1938        }
1939
1940        memset(&crq, 0, sizeof(crq));
1941        crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1942        crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1943        ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
1944
1945        mutex_lock(&adapter->fw_lock);
1946        adapter->fw_done_rc = 0;
1947        reinit_completion(&adapter->fw_done);
1948
1949        rc = ibmvnic_send_crq(adapter, &crq);
1950        if (rc) {
1951                rc = -EIO;
1952                mutex_unlock(&adapter->fw_lock);
1953                goto err;
1954        }
1955
1956        rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1957        /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1958        if (rc || adapter->fw_done_rc) {
1959                rc = -EIO;
1960                mutex_unlock(&adapter->fw_lock);
1961                goto err;
1962        }
1963        mutex_unlock(&adapter->fw_lock);
1964        return 0;
1965err:
1966        ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1967        return rc;
1968}
1969
1970static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1971{
1972        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1973        struct sockaddr *addr = p;
1974        int rc;
1975
1976        rc = 0;
1977        if (!is_valid_ether_addr(addr->sa_data))
1978                return -EADDRNOTAVAIL;
1979
1980        ether_addr_copy(adapter->mac_addr, addr->sa_data);
1981        if (adapter->state != VNIC_PROBED)
1982                rc = __ibmvnic_set_mac(netdev, addr->sa_data);
1983
1984        return rc;
1985}
1986
1987static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
1988{
1989        switch (reason) {
1990        case VNIC_RESET_FAILOVER:
1991                return "FAILOVER";
1992        case VNIC_RESET_MOBILITY:
1993                return "MOBILITY";
1994        case VNIC_RESET_FATAL:
1995                return "FATAL";
1996        case VNIC_RESET_NON_FATAL:
1997                return "NON_FATAL";
1998        case VNIC_RESET_TIMEOUT:
1999                return "TIMEOUT";
2000        case VNIC_RESET_CHANGE_PARAM:
2001                return "CHANGE_PARAM";
2002        default:
2003                return "UNKNOWN";
2004        }
2005}
2006
2007/*
2008 * do_reset returns zero if we are able to keep processing reset events, or
2009 * non-zero if we hit a fatal error and must halt.
2010 */
2011static int do_reset(struct ibmvnic_adapter *adapter,
2012                    struct ibmvnic_rwi *rwi, u32 reset_state)
2013{
2014        u64 old_num_rx_queues, old_num_tx_queues;
2015        u64 old_num_rx_slots, old_num_tx_slots;
2016        struct net_device *netdev = adapter->netdev;
2017        int rc;
2018
2019        netdev_dbg(adapter->netdev,
2020                   "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2021                   adapter_state_to_string(adapter->state),
2022                   adapter->failover_pending,
2023                   reset_reason_to_string(rwi->reset_reason),
2024                   adapter_state_to_string(reset_state));
2025
2026        adapter->reset_reason = rwi->reset_reason;
2027        /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2028        if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2029                rtnl_lock();
2030
2031        /* Now that we have the rtnl lock, clear any pending failover.
2032         * This will ensure ibmvnic_open() has either completed or will
2033         * block until failover is complete.
2034         */
2035        if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2036                adapter->failover_pending = false;
2037
2038        /* read the state and check (again) after getting rtnl */
2039        reset_state = adapter->state;
2040
2041        if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2042                rc = -EBUSY;
2043                goto out;
2044        }
2045
2046        netif_carrier_off(netdev);
2047
2048        old_num_rx_queues = adapter->req_rx_queues;
2049        old_num_tx_queues = adapter->req_tx_queues;
2050        old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2051        old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
2052
2053        ibmvnic_cleanup(netdev);
2054
2055        if (reset_state == VNIC_OPEN &&
2056            adapter->reset_reason != VNIC_RESET_MOBILITY &&
2057            adapter->reset_reason != VNIC_RESET_FAILOVER) {
2058                if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2059                        rc = __ibmvnic_close(netdev);
2060                        if (rc)
2061                                goto out;
2062                } else {
2063                        adapter->state = VNIC_CLOSING;
2064
2065                        /* Release the RTNL lock before link state change and
2066                         * re-acquire after the link state change to allow
2067                         * linkwatch_event to grab the RTNL lock and run during
2068                         * a reset.
2069                         */
2070                        rtnl_unlock();
2071                        rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2072                        rtnl_lock();
2073                        if (rc)
2074                                goto out;
2075
2076                        if (adapter->state == VNIC_OPEN) {
2077                                /* When we dropped rtnl, ibmvnic_open() got
2078                                 * it and noticed that we are resetting and
2079                                 * set the adapter state to OPEN. Update our
2080                                 * new "target" state, and resume the reset
2081                                 * from VNIC_CLOSING state.
2082                                 */
2083                                netdev_dbg(netdev,
2084                                           "Open changed state from %s, updating.\n",
2085                                           adapter_state_to_string(reset_state));
2086                                reset_state = VNIC_OPEN;
2087                                adapter->state = VNIC_CLOSING;
2088                        }
2089
2090                        if (adapter->state != VNIC_CLOSING) {
2091                                /* If someone else changed the adapter state
2092                                 * when we dropped the rtnl, fail the reset
2093                                 */
2094                                rc = -1;
2095                                goto out;
2096                        }
2097                        adapter->state = VNIC_CLOSED;
2098                }
2099        }
2100
2101        if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2102                release_resources(adapter);
2103                release_sub_crqs(adapter, 1);
2104                release_crq_queue(adapter);
2105        }
2106
2107        if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2108                /* remove the closed state so when we call open it appears
2109                 * we are coming from the probed state.
2110                 */
2111                adapter->state = VNIC_PROBED;
2112
2113                if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2114                        rc = init_crq_queue(adapter);
2115                } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
2116                        rc = ibmvnic_reenable_crq_queue(adapter);
2117                        release_sub_crqs(adapter, 1);
2118                } else {
2119                        rc = ibmvnic_reset_crq(adapter);
2120                        if (rc == H_CLOSED || rc == H_SUCCESS) {
2121                                rc = vio_enable_interrupts(adapter->vdev);
2122                                if (rc)
2123                                        netdev_err(adapter->netdev,
2124                                                   "Reset failed to enable interrupts. rc=%d\n",
2125                                                   rc);
2126                        }
2127                }
2128
2129                if (rc) {
2130                        netdev_err(adapter->netdev,
2131                                   "Reset couldn't initialize crq. rc=%d\n", rc);
2132                        goto out;
2133                }
2134
2135                rc = ibmvnic_reset_init(adapter, true);
2136                if (rc) {
2137                        rc = IBMVNIC_INIT_FAILED;
2138                        goto out;
2139                }
2140
2141                /* If the adapter was in PROBE state prior to the reset,
2142                 * exit here.
2143                 */
2144                if (reset_state == VNIC_PROBED) {
2145                        rc = 0;
2146                        goto out;
2147                }
2148
2149                rc = ibmvnic_login(netdev);
2150                if (rc)
2151                        goto out;
2152
2153                if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2154                        rc = init_resources(adapter);
2155                        if (rc)
2156                                goto out;
2157                } else if (adapter->req_rx_queues != old_num_rx_queues ||
2158                    adapter->req_tx_queues != old_num_tx_queues ||
2159                    adapter->req_rx_add_entries_per_subcrq !=
2160                    old_num_rx_slots ||
2161                    adapter->req_tx_entries_per_subcrq !=
2162                    old_num_tx_slots ||
2163                    !adapter->rx_pool ||
2164                    !adapter->tso_pool ||
2165                    !adapter->tx_pool) {
2166                        release_rx_pools(adapter);
2167                        release_tx_pools(adapter);
2168                        release_napi(adapter);
2169                        release_vpd_data(adapter);
2170
2171                        rc = init_resources(adapter);
2172                        if (rc)
2173                                goto out;
2174
2175                } else {
2176                        rc = reset_tx_pools(adapter);
2177                        if (rc) {
2178                                netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2179                                           rc);
2180                                goto out;
2181                        }
2182
2183                        rc = reset_rx_pools(adapter);
2184                        if (rc) {
2185                                netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2186                                           rc);
2187                                goto out;
2188                        }
2189                }
2190                ibmvnic_disable_irqs(adapter);
2191        }
2192        adapter->state = VNIC_CLOSED;
2193
2194        if (reset_state == VNIC_CLOSED) {
2195                rc = 0;
2196                goto out;
2197        }
2198
2199        rc = __ibmvnic_open(netdev);
2200        if (rc) {
2201                rc = IBMVNIC_OPEN_FAILED;
2202                goto out;
2203        }
2204
2205        /* refresh device's multicast list */
2206        ibmvnic_set_multi(netdev);
2207
2208        if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
2209            adapter->reset_reason == VNIC_RESET_MOBILITY)
2210                __netdev_notify_peers(netdev);
2211
2212        rc = 0;
2213
2214out:
2215        /* restore the adapter state if reset failed */
2216        if (rc)
2217                adapter->state = reset_state;
2218        /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2219        if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2220                rtnl_unlock();
2221
2222        netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2223                   adapter_state_to_string(adapter->state),
2224                   adapter->failover_pending, rc);
2225        return rc;
2226}
2227
2228static int do_hard_reset(struct ibmvnic_adapter *adapter,
2229                         struct ibmvnic_rwi *rwi, u32 reset_state)
2230{
2231        struct net_device *netdev = adapter->netdev;
2232        int rc;
2233
2234        netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2235                   reset_reason_to_string(rwi->reset_reason));
2236
2237        /* read the state and check (again) after getting rtnl */
2238        reset_state = adapter->state;
2239
2240        if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2241                rc = -EBUSY;
2242                goto out;
2243        }
2244
2245        netif_carrier_off(netdev);
2246        adapter->reset_reason = rwi->reset_reason;
2247
2248        ibmvnic_cleanup(netdev);
2249        release_resources(adapter);
2250        release_sub_crqs(adapter, 0);
2251        release_crq_queue(adapter);
2252
2253        /* remove the closed state so when we call open it appears
2254         * we are coming from the probed state.
2255         */
2256        adapter->state = VNIC_PROBED;
2257
2258        reinit_completion(&adapter->init_done);
2259        rc = init_crq_queue(adapter);
2260        if (rc) {
2261                netdev_err(adapter->netdev,
2262                           "Couldn't initialize crq. rc=%d\n", rc);
2263                goto out;
2264        }
2265
2266        rc = ibmvnic_reset_init(adapter, false);
2267        if (rc)
2268                goto out;
2269
2270        /* If the adapter was in PROBE state prior to the reset,
2271         * exit here.
2272         */
2273        if (reset_state == VNIC_PROBED)
2274                goto out;
2275
2276        rc = ibmvnic_login(netdev);
2277        if (rc)
2278                goto out;
2279
2280        rc = init_resources(adapter);
2281        if (rc)
2282                goto out;
2283
2284        ibmvnic_disable_irqs(adapter);
2285        adapter->state = VNIC_CLOSED;
2286
2287        if (reset_state == VNIC_CLOSED)
2288                goto out;
2289
2290        rc = __ibmvnic_open(netdev);
2291        if (rc) {
2292                rc = IBMVNIC_OPEN_FAILED;
2293                goto out;
2294        }
2295
2296        __netdev_notify_peers(netdev);
2297out:
2298        /* restore adapter state if reset failed */
2299        if (rc)
2300                adapter->state = reset_state;
2301        netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2302                   adapter_state_to_string(adapter->state),
2303                   adapter->failover_pending, rc);
2304        return rc;
2305}
2306
2307static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2308{
2309        struct ibmvnic_rwi *rwi;
2310        unsigned long flags;
2311
2312        spin_lock_irqsave(&adapter->rwi_lock, flags);
2313
2314        if (!list_empty(&adapter->rwi_list)) {
2315                rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2316                                       list);
2317                list_del(&rwi->list);
2318        } else {
2319                rwi = NULL;
2320        }
2321
2322        spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2323        return rwi;
2324}
2325
2326static void __ibmvnic_reset(struct work_struct *work)
2327{
2328        struct ibmvnic_rwi *rwi;
2329        struct ibmvnic_adapter *adapter;
2330        bool saved_state = false;
2331        unsigned long flags;
2332        u32 reset_state;
2333        int rc = 0;
2334
2335        adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
2336
2337        if (test_and_set_bit_lock(0, &adapter->resetting)) {
2338                queue_delayed_work(system_long_wq,
2339                                   &adapter->ibmvnic_delayed_reset,
2340                                   IBMVNIC_RESET_DELAY);
2341                return;
2342        }
2343
2344        rwi = get_next_rwi(adapter);
2345        while (rwi) {
2346                spin_lock_irqsave(&adapter->state_lock, flags);
2347
2348                if (adapter->state == VNIC_REMOVING ||
2349                    adapter->state == VNIC_REMOVED) {
2350                        spin_unlock_irqrestore(&adapter->state_lock, flags);
2351                        kfree(rwi);
2352                        rc = EBUSY;
2353                        break;
2354                }
2355
2356                if (!saved_state) {
2357                        reset_state = adapter->state;
2358                        saved_state = true;
2359                }
2360                spin_unlock_irqrestore(&adapter->state_lock, flags);
2361
2362                if (adapter->force_reset_recovery) {
2363                        /* Since we are doing a hard reset now, clear the
2364                         * failover_pending flag so we don't ignore any
2365                         * future MOBILITY or other resets.
2366                         */
2367                        adapter->failover_pending = false;
2368
2369                        /* Transport event occurred during previous reset */
2370                        if (adapter->wait_for_reset) {
2371                                /* Previous was CHANGE_PARAM; caller locked */
2372                                adapter->force_reset_recovery = false;
2373                                rc = do_hard_reset(adapter, rwi, reset_state);
2374                        } else {
2375                                rtnl_lock();
2376                                adapter->force_reset_recovery = false;
2377                                rc = do_hard_reset(adapter, rwi, reset_state);
2378                                rtnl_unlock();
2379                        }
2380                        if (rc) {
2381                                /* give backing device time to settle down */
2382                                netdev_dbg(adapter->netdev,
2383                                           "[S:%s] Hard reset failed, waiting 60 secs\n",
2384                                           adapter_state_to_string(adapter->state));
2385                                set_current_state(TASK_UNINTERRUPTIBLE);
2386                                schedule_timeout(60 * HZ);
2387                        }
2388                } else {
2389                        rc = do_reset(adapter, rwi, reset_state);
2390                }
2391                kfree(rwi);
2392                adapter->last_reset_time = jiffies;
2393
2394                if (rc)
2395                        netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
2396
2397                rwi = get_next_rwi(adapter);
2398
2399                if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2400                            rwi->reset_reason == VNIC_RESET_MOBILITY))
2401                        adapter->force_reset_recovery = true;
2402        }
2403
2404        if (adapter->wait_for_reset) {
2405                adapter->reset_done_rc = rc;
2406                complete(&adapter->reset_done);
2407        }
2408
2409        clear_bit_unlock(0, &adapter->resetting);
2410
2411        netdev_dbg(adapter->netdev,
2412                   "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2413                   adapter_state_to_string(adapter->state),
2414                   adapter->force_reset_recovery,
2415                   adapter->wait_for_reset);
2416}
2417
2418static void __ibmvnic_delayed_reset(struct work_struct *work)
2419{
2420        struct ibmvnic_adapter *adapter;
2421
2422        adapter = container_of(work, struct ibmvnic_adapter,
2423                               ibmvnic_delayed_reset.work);
2424        __ibmvnic_reset(&adapter->ibmvnic_reset);
2425}
2426
2427static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2428                         enum ibmvnic_reset_reason reason)
2429{
2430        struct list_head *entry, *tmp_entry;
2431        struct ibmvnic_rwi *rwi, *tmp;
2432        struct net_device *netdev = adapter->netdev;
2433        unsigned long flags;
2434        int ret;
2435
2436        spin_lock_irqsave(&adapter->rwi_lock, flags);
2437
2438        /* If failover is pending don't schedule any other reset.
2439         * Instead let the failover complete. If there is already a
2440         * a failover reset scheduled, we will detect and drop the
2441         * duplicate reset when walking the ->rwi_list below.
2442         */
2443        if (adapter->state == VNIC_REMOVING ||
2444            adapter->state == VNIC_REMOVED ||
2445            (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
2446                ret = EBUSY;
2447                netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
2448                goto err;
2449        }
2450
2451        if (adapter->state == VNIC_PROBING) {
2452                netdev_warn(netdev, "Adapter reset during probe\n");
2453                adapter->init_done_rc = EAGAIN;
2454                ret = EAGAIN;
2455                goto err;
2456        }
2457
2458        list_for_each(entry, &adapter->rwi_list) {
2459                tmp = list_entry(entry, struct ibmvnic_rwi, list);
2460                if (tmp->reset_reason == reason) {
2461                        netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2462                                   reset_reason_to_string(reason));
2463                        ret = EBUSY;
2464                        goto err;
2465                }
2466        }
2467
2468        rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
2469        if (!rwi) {
2470                ret = ENOMEM;
2471                goto err;
2472        }
2473        /* if we just received a transport event,
2474         * flush reset queue and process this reset
2475         */
2476        if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2477                list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2478                        list_del(entry);
2479        }
2480        rwi->reset_reason = reason;
2481        list_add_tail(&rwi->list, &adapter->rwi_list);
2482        netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2483                   reset_reason_to_string(reason));
2484        queue_work(system_long_wq, &adapter->ibmvnic_reset);
2485
2486        ret = 0;
2487err:
2488        /* ibmvnic_close() below can block, so drop the lock first */
2489        spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2490
2491        if (ret == ENOMEM)
2492                ibmvnic_close(netdev);
2493
2494        return -ret;
2495}
2496
2497static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
2498{
2499        struct ibmvnic_adapter *adapter = netdev_priv(dev);
2500
2501        if (test_bit(0, &adapter->resetting)) {
2502                netdev_err(adapter->netdev,
2503                           "Adapter is resetting, skip timeout reset\n");
2504                return;
2505        }
2506        /* No queuing up reset until at least 5 seconds (default watchdog val)
2507         * after last reset
2508         */
2509        if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2510                netdev_dbg(dev, "Not yet time to tx timeout.\n");
2511                return;
2512        }
2513        ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2514}
2515
2516static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2517                                  struct ibmvnic_rx_buff *rx_buff)
2518{
2519        struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2520
2521        rx_buff->skb = NULL;
2522
2523        pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2524        pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2525
2526        atomic_dec(&pool->available);
2527}
2528
2529static int ibmvnic_poll(struct napi_struct *napi, int budget)
2530{
2531        struct ibmvnic_sub_crq_queue *rx_scrq;
2532        struct ibmvnic_adapter *adapter;
2533        struct net_device *netdev;
2534        int frames_processed;
2535        int scrq_num;
2536
2537        netdev = napi->dev;
2538        adapter = netdev_priv(netdev);
2539        scrq_num = (int)(napi - adapter->napi);
2540        frames_processed = 0;
2541        rx_scrq = adapter->rx_scrq[scrq_num];
2542
2543restart_poll:
2544        while (frames_processed < budget) {
2545                struct sk_buff *skb;
2546                struct ibmvnic_rx_buff *rx_buff;
2547                union sub_crq *next;
2548                u32 length;
2549                u16 offset;
2550                u8 flags = 0;
2551
2552                if (unlikely(test_bit(0, &adapter->resetting) &&
2553                             adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2554                        enable_scrq_irq(adapter, rx_scrq);
2555                        napi_complete_done(napi, frames_processed);
2556                        return frames_processed;
2557                }
2558
2559                if (!pending_scrq(adapter, rx_scrq))
2560                        break;
2561                next = ibmvnic_next_scrq(adapter, rx_scrq);
2562                rx_buff = (struct ibmvnic_rx_buff *)
2563                          be64_to_cpu(next->rx_comp.correlator);
2564                /* do error checking */
2565                if (next->rx_comp.rc) {
2566                        netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2567                                   be16_to_cpu(next->rx_comp.rc));
2568                        /* free the entry */
2569                        next->rx_comp.first = 0;
2570                        dev_kfree_skb_any(rx_buff->skb);
2571                        remove_buff_from_pool(adapter, rx_buff);
2572                        continue;
2573                } else if (!rx_buff->skb) {
2574                        /* free the entry */
2575                        next->rx_comp.first = 0;
2576                        remove_buff_from_pool(adapter, rx_buff);
2577                        continue;
2578                }
2579
2580                length = be32_to_cpu(next->rx_comp.len);
2581                offset = be16_to_cpu(next->rx_comp.off_frame_data);
2582                flags = next->rx_comp.flags;
2583                skb = rx_buff->skb;
2584                /* load long_term_buff before copying to skb */
2585                dma_rmb();
2586                skb_copy_to_linear_data(skb, rx_buff->data + offset,
2587                                        length);
2588
2589                /* VLAN Header has been stripped by the system firmware and
2590                 * needs to be inserted by the driver
2591                 */
2592                if (adapter->rx_vlan_header_insertion &&
2593                    (flags & IBMVNIC_VLAN_STRIPPED))
2594                        __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2595                                               ntohs(next->rx_comp.vlan_tci));
2596
2597                /* free the entry */
2598                next->rx_comp.first = 0;
2599                remove_buff_from_pool(adapter, rx_buff);
2600
2601                skb_put(skb, length);
2602                skb->protocol = eth_type_trans(skb, netdev);
2603                skb_record_rx_queue(skb, scrq_num);
2604
2605                if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2606                    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2607                        skb->ip_summed = CHECKSUM_UNNECESSARY;
2608                }
2609
2610                length = skb->len;
2611                napi_gro_receive(napi, skb); /* send it up */
2612                netdev->stats.rx_packets++;
2613                netdev->stats.rx_bytes += length;
2614                adapter->rx_stats_buffers[scrq_num].packets++;
2615                adapter->rx_stats_buffers[scrq_num].bytes += length;
2616                frames_processed++;
2617        }
2618
2619        if (adapter->state != VNIC_CLOSING &&
2620            ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2621              adapter->req_rx_add_entries_per_subcrq / 2) ||
2622              frames_processed < budget))
2623                replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2624        if (frames_processed < budget) {
2625                if (napi_complete_done(napi, frames_processed)) {
2626                        enable_scrq_irq(adapter, rx_scrq);
2627                        if (pending_scrq(adapter, rx_scrq)) {
2628                                if (napi_reschedule(napi)) {
2629                                        disable_scrq_irq(adapter, rx_scrq);
2630                                        goto restart_poll;
2631                                }
2632                        }
2633                }
2634        }
2635        return frames_processed;
2636}
2637
2638static int wait_for_reset(struct ibmvnic_adapter *adapter)
2639{
2640        int rc, ret;
2641
2642        adapter->fallback.mtu = adapter->req_mtu;
2643        adapter->fallback.rx_queues = adapter->req_rx_queues;
2644        adapter->fallback.tx_queues = adapter->req_tx_queues;
2645        adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2646        adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2647
2648        reinit_completion(&adapter->reset_done);
2649        adapter->wait_for_reset = true;
2650        rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2651
2652        if (rc) {
2653                ret = rc;
2654                goto out;
2655        }
2656        rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2657        if (rc) {
2658                ret = -ENODEV;
2659                goto out;
2660        }
2661
2662        ret = 0;
2663        if (adapter->reset_done_rc) {
2664                ret = -EIO;
2665                adapter->desired.mtu = adapter->fallback.mtu;
2666                adapter->desired.rx_queues = adapter->fallback.rx_queues;
2667                adapter->desired.tx_queues = adapter->fallback.tx_queues;
2668                adapter->desired.rx_entries = adapter->fallback.rx_entries;
2669                adapter->desired.tx_entries = adapter->fallback.tx_entries;
2670
2671                reinit_completion(&adapter->reset_done);
2672                adapter->wait_for_reset = true;
2673                rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2674                if (rc) {
2675                        ret = rc;
2676                        goto out;
2677                }
2678                rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2679                                                 60000);
2680                if (rc) {
2681                        ret = -ENODEV;
2682                        goto out;
2683                }
2684        }
2685out:
2686        adapter->wait_for_reset = false;
2687
2688        return ret;
2689}
2690
2691static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2692{
2693        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2694
2695        adapter->desired.mtu = new_mtu + ETH_HLEN;
2696
2697        return wait_for_reset(adapter);
2698}
2699
2700static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2701                                                struct net_device *dev,
2702                                                netdev_features_t features)
2703{
2704        /* Some backing hardware adapters can not
2705         * handle packets with a MSS less than 224
2706         * or with only one segment.
2707         */
2708        if (skb_is_gso(skb)) {
2709                if (skb_shinfo(skb)->gso_size < 224 ||
2710                    skb_shinfo(skb)->gso_segs == 1)
2711                        features &= ~NETIF_F_GSO_MASK;
2712        }
2713
2714        return features;
2715}
2716
2717static const struct net_device_ops ibmvnic_netdev_ops = {
2718        .ndo_open               = ibmvnic_open,
2719        .ndo_stop               = ibmvnic_close,
2720        .ndo_start_xmit         = ibmvnic_xmit,
2721        .ndo_set_rx_mode        = ibmvnic_set_multi,
2722        .ndo_set_mac_address    = ibmvnic_set_mac,
2723        .ndo_validate_addr      = eth_validate_addr,
2724        .ndo_tx_timeout         = ibmvnic_tx_timeout,
2725        .ndo_change_mtu         = ibmvnic_change_mtu,
2726        .ndo_features_check     = ibmvnic_features_check,
2727};
2728
2729/* ethtool functions */
2730
2731static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2732                                      struct ethtool_link_ksettings *cmd)
2733{
2734        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2735        int rc;
2736
2737        rc = send_query_phys_parms(adapter);
2738        if (rc) {
2739                adapter->speed = SPEED_UNKNOWN;
2740                adapter->duplex = DUPLEX_UNKNOWN;
2741        }
2742        cmd->base.speed = adapter->speed;
2743        cmd->base.duplex = adapter->duplex;
2744        cmd->base.port = PORT_FIBRE;
2745        cmd->base.phy_address = 0;
2746        cmd->base.autoneg = AUTONEG_ENABLE;
2747
2748        return 0;
2749}
2750
2751static void ibmvnic_get_drvinfo(struct net_device *netdev,
2752                                struct ethtool_drvinfo *info)
2753{
2754        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2755
2756        strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2757        strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2758        strscpy(info->fw_version, adapter->fw_version,
2759                sizeof(info->fw_version));
2760}
2761
2762static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2763{
2764        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2765
2766        return adapter->msg_enable;
2767}
2768
2769static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2770{
2771        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2772
2773        adapter->msg_enable = data;
2774}
2775
2776static u32 ibmvnic_get_link(struct net_device *netdev)
2777{
2778        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2779
2780        /* Don't need to send a query because we request a logical link up at
2781         * init and then we wait for link state indications
2782         */
2783        return adapter->logical_link_state;
2784}
2785
2786static void ibmvnic_get_ringparam(struct net_device *netdev,
2787                                  struct ethtool_ringparam *ring)
2788{
2789        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2790
2791        if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2792                ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2793                ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2794        } else {
2795                ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2796                ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2797        }
2798        ring->rx_mini_max_pending = 0;
2799        ring->rx_jumbo_max_pending = 0;
2800        ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2801        ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2802        ring->rx_mini_pending = 0;
2803        ring->rx_jumbo_pending = 0;
2804}
2805
2806static int ibmvnic_set_ringparam(struct net_device *netdev,
2807                                 struct ethtool_ringparam *ring)
2808{
2809        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2810        int ret;
2811
2812        ret = 0;
2813        adapter->desired.rx_entries = ring->rx_pending;
2814        adapter->desired.tx_entries = ring->tx_pending;
2815
2816        ret = wait_for_reset(adapter);
2817
2818        if (!ret &&
2819            (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2820             adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2821                netdev_info(netdev,
2822                            "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2823                            ring->rx_pending, ring->tx_pending,
2824                            adapter->req_rx_add_entries_per_subcrq,
2825                            adapter->req_tx_entries_per_subcrq);
2826        return ret;
2827}
2828
2829static void ibmvnic_get_channels(struct net_device *netdev,
2830                                 struct ethtool_channels *channels)
2831{
2832        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2833
2834        if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2835                channels->max_rx = adapter->max_rx_queues;
2836                channels->max_tx = adapter->max_tx_queues;
2837        } else {
2838                channels->max_rx = IBMVNIC_MAX_QUEUES;
2839                channels->max_tx = IBMVNIC_MAX_QUEUES;
2840        }
2841
2842        channels->max_other = 0;
2843        channels->max_combined = 0;
2844        channels->rx_count = adapter->req_rx_queues;
2845        channels->tx_count = adapter->req_tx_queues;
2846        channels->other_count = 0;
2847        channels->combined_count = 0;
2848}
2849
2850static int ibmvnic_set_channels(struct net_device *netdev,
2851                                struct ethtool_channels *channels)
2852{
2853        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2854        int ret;
2855
2856        ret = 0;
2857        adapter->desired.rx_queues = channels->rx_count;
2858        adapter->desired.tx_queues = channels->tx_count;
2859
2860        ret = wait_for_reset(adapter);
2861
2862        if (!ret &&
2863            (adapter->req_rx_queues != channels->rx_count ||
2864             adapter->req_tx_queues != channels->tx_count))
2865                netdev_info(netdev,
2866                            "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2867                            channels->rx_count, channels->tx_count,
2868                            adapter->req_rx_queues, adapter->req_tx_queues);
2869        return ret;
2870}
2871
2872static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2873{
2874        struct ibmvnic_adapter *adapter = netdev_priv(dev);
2875        int i;
2876
2877        switch (stringset) {
2878        case ETH_SS_STATS:
2879                for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2880                                i++, data += ETH_GSTRING_LEN)
2881                        memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2882
2883                for (i = 0; i < adapter->req_tx_queues; i++) {
2884                        snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2885                        data += ETH_GSTRING_LEN;
2886
2887                        snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2888                        data += ETH_GSTRING_LEN;
2889
2890                        snprintf(data, ETH_GSTRING_LEN,
2891                                 "tx%d_dropped_packets", i);
2892                        data += ETH_GSTRING_LEN;
2893                }
2894
2895                for (i = 0; i < adapter->req_rx_queues; i++) {
2896                        snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2897                        data += ETH_GSTRING_LEN;
2898
2899                        snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2900                        data += ETH_GSTRING_LEN;
2901
2902                        snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2903                        data += ETH_GSTRING_LEN;
2904                }
2905                break;
2906
2907        case ETH_SS_PRIV_FLAGS:
2908                for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2909                        strcpy(data + i * ETH_GSTRING_LEN,
2910                               ibmvnic_priv_flags[i]);
2911                break;
2912        default:
2913                return;
2914        }
2915}
2916
2917static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2918{
2919        struct ibmvnic_adapter *adapter = netdev_priv(dev);
2920
2921        switch (sset) {
2922        case ETH_SS_STATS:
2923                return ARRAY_SIZE(ibmvnic_stats) +
2924                       adapter->req_tx_queues * NUM_TX_STATS +
2925                       adapter->req_rx_queues * NUM_RX_STATS;
2926        case ETH_SS_PRIV_FLAGS:
2927                return ARRAY_SIZE(ibmvnic_priv_flags);
2928        default:
2929                return -EOPNOTSUPP;
2930        }
2931}
2932
2933static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2934                                      struct ethtool_stats *stats, u64 *data)
2935{
2936        struct ibmvnic_adapter *adapter = netdev_priv(dev);
2937        union ibmvnic_crq crq;
2938        int i, j;
2939        int rc;
2940
2941        memset(&crq, 0, sizeof(crq));
2942        crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2943        crq.request_statistics.cmd = REQUEST_STATISTICS;
2944        crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2945        crq.request_statistics.len =
2946            cpu_to_be32(sizeof(struct ibmvnic_statistics));
2947
2948        /* Wait for data to be written */
2949        reinit_completion(&adapter->stats_done);
2950        rc = ibmvnic_send_crq(adapter, &crq);
2951        if (rc)
2952                return;
2953        rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2954        if (rc)
2955                return;
2956
2957        for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2958                data[i] = be64_to_cpu(IBMVNIC_GET_STAT
2959                                      (adapter, ibmvnic_stats[i].offset));
2960
2961        for (j = 0; j < adapter->req_tx_queues; j++) {
2962                data[i] = adapter->tx_stats_buffers[j].packets;
2963                i++;
2964                data[i] = adapter->tx_stats_buffers[j].bytes;
2965                i++;
2966                data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2967                i++;
2968        }
2969
2970        for (j = 0; j < adapter->req_rx_queues; j++) {
2971                data[i] = adapter->rx_stats_buffers[j].packets;
2972                i++;
2973                data[i] = adapter->rx_stats_buffers[j].bytes;
2974                i++;
2975                data[i] = adapter->rx_stats_buffers[j].interrupts;
2976                i++;
2977        }
2978}
2979
2980static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2981{
2982        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2983
2984        return adapter->priv_flags;
2985}
2986
2987static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2988{
2989        struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2990        bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2991
2992        if (which_maxes)
2993                adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2994        else
2995                adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2996
2997        return 0;
2998}
2999
3000static const struct ethtool_ops ibmvnic_ethtool_ops = {
3001        .get_drvinfo            = ibmvnic_get_drvinfo,
3002        .get_msglevel           = ibmvnic_get_msglevel,
3003        .set_msglevel           = ibmvnic_set_msglevel,
3004        .get_link               = ibmvnic_get_link,
3005        .get_ringparam          = ibmvnic_get_ringparam,
3006        .set_ringparam          = ibmvnic_set_ringparam,
3007        .get_channels           = ibmvnic_get_channels,
3008        .set_channels           = ibmvnic_set_channels,
3009        .get_strings            = ibmvnic_get_strings,
3010        .get_sset_count         = ibmvnic_get_sset_count,
3011        .get_ethtool_stats      = ibmvnic_get_ethtool_stats,
3012        .get_link_ksettings     = ibmvnic_get_link_ksettings,
3013        .get_priv_flags         = ibmvnic_get_priv_flags,
3014        .set_priv_flags         = ibmvnic_set_priv_flags,
3015};
3016
3017/* Routines for managing CRQs/sCRQs  */
3018
3019static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3020                                   struct ibmvnic_sub_crq_queue *scrq)
3021{
3022        int rc;
3023
3024        if (!scrq) {
3025                netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
3026                return -EINVAL;
3027        }
3028
3029        if (scrq->irq) {
3030                free_irq(scrq->irq, scrq);
3031                irq_dispose_mapping(scrq->irq);
3032                scrq->irq = 0;
3033        }
3034
3035        if (scrq->msgs) {
3036                memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3037                atomic_set(&scrq->used, 0);
3038                scrq->cur = 0;
3039                scrq->ind_buf.index = 0;
3040        } else {
3041                netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3042                return -EINVAL;
3043        }
3044
3045        rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3046                           4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3047        return rc;
3048}
3049
3050static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3051{
3052        int i, rc;
3053
3054        if (!adapter->tx_scrq || !adapter->rx_scrq)
3055                return -EINVAL;
3056
3057        for (i = 0; i < adapter->req_tx_queues; i++) {
3058                netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
3059                rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3060                if (rc)
3061                        return rc;
3062        }
3063
3064        for (i = 0; i < adapter->req_rx_queues; i++) {
3065                netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
3066                rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3067                if (rc)
3068                        return rc;
3069        }
3070
3071        return rc;
3072}
3073
3074static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
3075                                  struct ibmvnic_sub_crq_queue *scrq,
3076                                  bool do_h_free)
3077{
3078        struct device *dev = &adapter->vdev->dev;
3079        long rc;
3080
3081        netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3082
3083        if (do_h_free) {
3084                /* Close the sub-crqs */
3085                do {
3086                        rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3087                                                adapter->vdev->unit_address,
3088                                                scrq->crq_num);
3089                } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3090
3091                if (rc) {
3092                        netdev_err(adapter->netdev,
3093                                   "Failed to release sub-CRQ %16lx, rc = %ld\n",
3094                                   scrq->crq_num, rc);
3095                }
3096        }
3097
3098        dma_free_coherent(dev,
3099                          IBMVNIC_IND_ARR_SZ,
3100                          scrq->ind_buf.indir_arr,
3101                          scrq->ind_buf.indir_dma);
3102
3103        dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3104                         DMA_BIDIRECTIONAL);
3105        free_pages((unsigned long)scrq->msgs, 2);
3106        kfree(scrq);
3107}
3108
3109static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3110                                                        *adapter)
3111{
3112        struct device *dev = &adapter->vdev->dev;
3113        struct ibmvnic_sub_crq_queue *scrq;
3114        int rc;
3115
3116        scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
3117        if (!scrq)
3118                return NULL;
3119
3120        scrq->msgs =
3121                (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
3122        if (!scrq->msgs) {
3123                dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3124                goto zero_page_failed;
3125        }
3126
3127        scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3128                                         DMA_BIDIRECTIONAL);
3129        if (dma_mapping_error(dev, scrq->msg_token)) {
3130                dev_warn(dev, "Couldn't map crq queue messages page\n");
3131                goto map_failed;
3132        }
3133
3134        rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3135                           4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3136
3137        if (rc == H_RESOURCE)
3138                rc = ibmvnic_reset_crq(adapter);
3139
3140        if (rc == H_CLOSED) {
3141                dev_warn(dev, "Partner adapter not ready, waiting.\n");
3142        } else if (rc) {
3143                dev_warn(dev, "Error %d registering sub-crq\n", rc);
3144                goto reg_failed;
3145        }
3146
3147        scrq->adapter = adapter;
3148        scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
3149        scrq->ind_buf.index = 0;
3150
3151        scrq->ind_buf.indir_arr =
3152                dma_alloc_coherent(dev,
3153                                   IBMVNIC_IND_ARR_SZ,
3154                                   &scrq->ind_buf.indir_dma,
3155                                   GFP_KERNEL);
3156
3157        if (!scrq->ind_buf.indir_arr)
3158                goto indir_failed;
3159
3160        spin_lock_init(&scrq->lock);
3161
3162        netdev_dbg(adapter->netdev,
3163                   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3164                   scrq->crq_num, scrq->hw_irq, scrq->irq);
3165
3166        return scrq;
3167
3168indir_failed:
3169        do {
3170                rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3171                                        adapter->vdev->unit_address,
3172                                        scrq->crq_num);
3173        } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
3174reg_failed:
3175        dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3176                         DMA_BIDIRECTIONAL);
3177map_failed:
3178        free_pages((unsigned long)scrq->msgs, 2);
3179zero_page_failed:
3180        kfree(scrq);
3181
3182        return NULL;
3183}
3184
3185static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
3186{
3187        int i;
3188
3189        if (adapter->tx_scrq) {
3190                for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
3191                        if (!adapter->tx_scrq[i])
3192                                continue;
3193
3194                        netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3195                                   i);
3196                        ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
3197                        if (adapter->tx_scrq[i]->irq) {
3198                                free_irq(adapter->tx_scrq[i]->irq,
3199                                         adapter->tx_scrq[i]);
3200                                irq_dispose_mapping(adapter->tx_scrq[i]->irq);
3201                                adapter->tx_scrq[i]->irq = 0;
3202                        }
3203
3204                        release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3205                                              do_h_free);
3206                }
3207
3208                kfree(adapter->tx_scrq);
3209                adapter->tx_scrq = NULL;
3210                adapter->num_active_tx_scrqs = 0;
3211        }
3212
3213        if (adapter->rx_scrq) {
3214                for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
3215                        if (!adapter->rx_scrq[i])
3216                                continue;
3217
3218                        netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3219                                   i);
3220                        if (adapter->rx_scrq[i]->irq) {
3221                                free_irq(adapter->rx_scrq[i]->irq,
3222                                         adapter->rx_scrq[i]);
3223                                irq_dispose_mapping(adapter->rx_scrq[i]->irq);
3224                                adapter->rx_scrq[i]->irq = 0;
3225                        }
3226
3227                        release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3228                                              do_h_free);
3229                }
3230
3231                kfree(adapter->rx_scrq);
3232                adapter->rx_scrq = NULL;
3233                adapter->num_active_rx_scrqs = 0;
3234        }
3235}
3236
3237static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3238                            struct ibmvnic_sub_crq_queue *scrq)
3239{
3240        struct device *dev = &adapter->vdev->dev;
3241        unsigned long rc;
3242
3243        rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3244                                H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3245        if (rc)
3246                dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3247                        scrq->hw_irq, rc);
3248        return rc;
3249}
3250
3251static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3252                           struct ibmvnic_sub_crq_queue *scrq)
3253{
3254        struct device *dev = &adapter->vdev->dev;
3255        unsigned long rc;
3256
3257        if (scrq->hw_irq > 0x100000000ULL) {
3258                dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3259                return 1;
3260        }
3261
3262        if (test_bit(0, &adapter->resetting) &&
3263            adapter->reset_reason == VNIC_RESET_MOBILITY) {
3264                u64 val = (0xff000000) | scrq->hw_irq;
3265
3266                rc = plpar_hcall_norets(H_EOI, val);
3267                /* H_EOI would fail with rc = H_FUNCTION when running
3268                 * in XIVE mode which is expected, but not an error.
3269                 */
3270                if (rc && rc != H_FUNCTION)
3271                        dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3272                                val, rc);
3273        }
3274
3275        rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3276                                H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3277        if (rc)
3278                dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3279                        scrq->hw_irq, rc);
3280        return rc;
3281}
3282
3283static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3284                               struct ibmvnic_sub_crq_queue *scrq)
3285{
3286        struct device *dev = &adapter->vdev->dev;
3287        struct ibmvnic_tx_pool *tx_pool;
3288        struct ibmvnic_tx_buff *txbuff;
3289        struct netdev_queue *txq;
3290        union sub_crq *next;
3291        int index;
3292        int i;
3293
3294restart_loop:
3295        while (pending_scrq(adapter, scrq)) {
3296                unsigned int pool = scrq->pool_index;
3297                int num_entries = 0;
3298                int total_bytes = 0;
3299                int num_packets = 0;
3300
3301                next = ibmvnic_next_scrq(adapter, scrq);
3302                for (i = 0; i < next->tx_comp.num_comps; i++) {
3303                        index = be32_to_cpu(next->tx_comp.correlators[i]);
3304                        if (index & IBMVNIC_TSO_POOL_MASK) {
3305                                tx_pool = &adapter->tso_pool[pool];
3306                                index &= ~IBMVNIC_TSO_POOL_MASK;
3307                        } else {
3308                                tx_pool = &adapter->tx_pool[pool];
3309                        }
3310
3311                        txbuff = &tx_pool->tx_buff[index];
3312                        num_packets++;
3313                        num_entries += txbuff->num_entries;
3314                        if (txbuff->skb) {
3315                                total_bytes += txbuff->skb->len;
3316                                if (next->tx_comp.rcs[i]) {
3317                                        dev_err(dev, "tx error %x\n",
3318                                                next->tx_comp.rcs[i]);
3319                                        dev_kfree_skb_irq(txbuff->skb);
3320                                } else {
3321                                        dev_consume_skb_irq(txbuff->skb);
3322                                }
3323                                txbuff->skb = NULL;
3324                        } else {
3325                                netdev_warn(adapter->netdev,
3326                                            "TX completion received with NULL socket buffer\n");
3327                        }
3328                        tx_pool->free_map[tx_pool->producer_index] = index;
3329                        tx_pool->producer_index =
3330                                (tx_pool->producer_index + 1) %
3331                                        tx_pool->num_buffers;
3332                }
3333                /* remove tx_comp scrq*/
3334                next->tx_comp.first = 0;
3335
3336                txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3337                netdev_tx_completed_queue(txq, num_packets, total_bytes);
3338
3339                if (atomic_sub_return(num_entries, &scrq->used) <=
3340                    (adapter->req_tx_entries_per_subcrq / 2) &&
3341                    __netif_subqueue_stopped(adapter->netdev,
3342                                             scrq->pool_index)) {
3343                        netif_wake_subqueue(adapter->netdev, scrq->pool_index);
3344                        netdev_dbg(adapter->netdev, "Started queue %d\n",
3345                                   scrq->pool_index);
3346                }
3347        }
3348
3349        enable_scrq_irq(adapter, scrq);
3350
3351        if (pending_scrq(adapter, scrq)) {
3352                disable_scrq_irq(adapter, scrq);
3353                goto restart_loop;
3354        }
3355
3356        return 0;
3357}
3358
3359static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3360{
3361        struct ibmvnic_sub_crq_queue *scrq = instance;
3362        struct ibmvnic_adapter *adapter = scrq->adapter;
3363
3364        disable_scrq_irq(adapter, scrq);
3365        ibmvnic_complete_tx(adapter, scrq);
3366
3367        return IRQ_HANDLED;
3368}
3369
3370static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3371{
3372        struct ibmvnic_sub_crq_queue *scrq = instance;
3373        struct ibmvnic_adapter *adapter = scrq->adapter;
3374
3375        /* When booting a kdump kernel we can hit pending interrupts
3376         * prior to completing driver initialization.
3377         */
3378        if (unlikely(adapter->state != VNIC_OPEN))
3379                return IRQ_NONE;
3380
3381        adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3382
3383        if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3384                disable_scrq_irq(adapter, scrq);
3385                __napi_schedule(&adapter->napi[scrq->scrq_num]);
3386        }
3387
3388        return IRQ_HANDLED;
3389}
3390
3391static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3392{
3393        struct device *dev = &adapter->vdev->dev;
3394        struct ibmvnic_sub_crq_queue *scrq;
3395        int i = 0, j = 0;
3396        int rc = 0;
3397
3398        for (i = 0; i < adapter->req_tx_queues; i++) {
3399                netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3400                           i);
3401                scrq = adapter->tx_scrq[i];
3402                scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3403
3404                if (!scrq->irq) {
3405                        rc = -EINVAL;
3406                        dev_err(dev, "Error mapping irq\n");
3407                        goto req_tx_irq_failed;
3408                }
3409
3410                snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3411                         adapter->vdev->unit_address, i);
3412                rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
3413                                 0, scrq->name, scrq);
3414
3415                if (rc) {
3416                        dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3417                                scrq->irq, rc);
3418                        irq_dispose_mapping(scrq->irq);
3419                        goto req_tx_irq_failed;
3420                }
3421        }
3422
3423        for (i = 0; i < adapter->req_rx_queues; i++) {
3424                netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3425                           i);
3426                scrq = adapter->rx_scrq[i];
3427                scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3428                if (!scrq->irq) {
3429                        rc = -EINVAL;
3430                        dev_err(dev, "Error mapping irq\n");
3431                        goto req_rx_irq_failed;
3432                }
3433                snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3434                         adapter->vdev->unit_address, i);
3435                rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
3436                                 0, scrq->name, scrq);
3437                if (rc) {
3438                        dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3439                                scrq->irq, rc);
3440                        irq_dispose_mapping(scrq->irq);
3441                        goto req_rx_irq_failed;
3442                }
3443        }
3444        return rc;
3445
3446req_rx_irq_failed:
3447        for (j = 0; j < i; j++) {
3448                free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3449                irq_dispose_mapping(adapter->rx_scrq[j]->irq);
3450        }
3451        i = adapter->req_tx_queues;
3452req_tx_irq_failed:
3453        for (j = 0; j < i; j++) {
3454                free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
3455                irq_dispose_mapping(adapter->tx_scrq[j]->irq);
3456        }
3457        release_sub_crqs(adapter, 1);
3458        return rc;
3459}
3460
3461static int init_sub_crqs(struct ibmvnic_adapter *adapter)
3462{
3463        struct device *dev = &adapter->vdev->dev;
3464        struct ibmvnic_sub_crq_queue **allqueues;
3465        int registered_queues = 0;
3466        int total_queues;
3467        int more = 0;
3468        int i;
3469
3470        total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3471
3472        allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
3473        if (!allqueues)
3474                return -1;
3475
3476        for (i = 0; i < total_queues; i++) {
3477                allqueues[i] = init_sub_crq_queue(adapter);
3478                if (!allqueues[i]) {
3479                        dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3480                        break;
3481                }
3482                registered_queues++;
3483        }
3484
3485        /* Make sure we were able to register the minimum number of queues */
3486        if (registered_queues <
3487            adapter->min_tx_queues + adapter->min_rx_queues) {
3488                dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
3489                goto tx_failed;
3490        }
3491
3492        /* Distribute the failed allocated queues*/
3493        for (i = 0; i < total_queues - registered_queues + more ; i++) {
3494                netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3495                switch (i % 3) {
3496                case 0:
3497                        if (adapter->req_rx_queues > adapter->min_rx_queues)
3498                                adapter->req_rx_queues--;
3499                        else
3500                                more++;
3501                        break;
3502                case 1:
3503                        if (adapter->req_tx_queues > adapter->min_tx_queues)
3504                                adapter->req_tx_queues--;
3505                        else
3506                                more++;
3507                        break;
3508                }
3509        }
3510
3511        adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3512                                   sizeof(*adapter->tx_scrq), GFP_KERNEL);
3513        if (!adapter->tx_scrq)
3514                goto tx_failed;
3515
3516        for (i = 0; i < adapter->req_tx_queues; i++) {
3517                adapter->tx_scrq[i] = allqueues[i];
3518                adapter->tx_scrq[i]->pool_index = i;
3519                adapter->num_active_tx_scrqs++;
3520        }
3521
3522        adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3523                                   sizeof(*adapter->rx_scrq), GFP_KERNEL);
3524        if (!adapter->rx_scrq)
3525                goto rx_failed;
3526
3527        for (i = 0; i < adapter->req_rx_queues; i++) {
3528                adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3529                adapter->rx_scrq[i]->scrq_num = i;
3530                adapter->num_active_rx_scrqs++;
3531        }
3532
3533        kfree(allqueues);
3534        return 0;
3535
3536rx_failed:
3537        kfree(adapter->tx_scrq);
3538        adapter->tx_scrq = NULL;
3539tx_failed:
3540        for (i = 0; i < registered_queues; i++)
3541                release_sub_crq_queue(adapter, allqueues[i], 1);
3542        kfree(allqueues);
3543        return -1;
3544}
3545
3546static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
3547{
3548        struct device *dev = &adapter->vdev->dev;
3549        union ibmvnic_crq crq;
3550        int max_entries;
3551
3552        if (!retry) {
3553                /* Sub-CRQ entries are 32 byte long */
3554                int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3555
3556                if (adapter->min_tx_entries_per_subcrq > entries_page ||
3557                    adapter->min_rx_add_entries_per_subcrq > entries_page) {
3558                        dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3559                        return;
3560                }
3561
3562                if (adapter->desired.mtu)
3563                        adapter->req_mtu = adapter->desired.mtu;
3564                else
3565                        adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
3566
3567                if (!adapter->desired.tx_entries)
3568                        adapter->desired.tx_entries =
3569                                        adapter->max_tx_entries_per_subcrq;
3570                if (!adapter->desired.rx_entries)
3571                        adapter->desired.rx_entries =
3572                                        adapter->max_rx_add_entries_per_subcrq;
3573
3574                max_entries = IBMVNIC_MAX_LTB_SIZE /
3575                              (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3576
3577                if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3578                        adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3579                        adapter->desired.tx_entries = max_entries;
3580                }
3581
3582                if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3583                        adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3584                        adapter->desired.rx_entries = max_entries;
3585                }
3586
3587                if (adapter->desired.tx_entries)
3588                        adapter->req_tx_entries_per_subcrq =
3589                                        adapter->desired.tx_entries;
3590                else
3591                        adapter->req_tx_entries_per_subcrq =
3592                                        adapter->max_tx_entries_per_subcrq;
3593
3594                if (adapter->desired.rx_entries)
3595                        adapter->req_rx_add_entries_per_subcrq =
3596                                        adapter->desired.rx_entries;
3597                else
3598                        adapter->req_rx_add_entries_per_subcrq =
3599                                        adapter->max_rx_add_entries_per_subcrq;
3600
3601                if (adapter->desired.tx_queues)
3602                        adapter->req_tx_queues =
3603                                        adapter->desired.tx_queues;
3604                else
3605                        adapter->req_tx_queues =
3606                                        adapter->opt_tx_comp_sub_queues;
3607
3608                if (adapter->desired.rx_queues)
3609                        adapter->req_rx_queues =
3610                                        adapter->desired.rx_queues;
3611                else
3612                        adapter->req_rx_queues =
3613                                        adapter->opt_rx_comp_queues;
3614
3615                adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3616        }
3617
3618        memset(&crq, 0, sizeof(crq));
3619        crq.request_capability.first = IBMVNIC_CRQ_CMD;
3620        crq.request_capability.cmd = REQUEST_CAPABILITY;
3621
3622        crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3623        crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3624        atomic_inc(&adapter->running_cap_crqs);
3625        ibmvnic_send_crq(adapter, &crq);
3626
3627        crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3628        crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3629        atomic_inc(&adapter->running_cap_crqs);
3630        ibmvnic_send_crq(adapter, &crq);
3631
3632        crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3633        crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3634        atomic_inc(&adapter->running_cap_crqs);
3635        ibmvnic_send_crq(adapter, &crq);
3636
3637        crq.request_capability.capability =
3638            cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3639        crq.request_capability.number =
3640            cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3641        atomic_inc(&adapter->running_cap_crqs);
3642        ibmvnic_send_crq(adapter, &crq);
3643
3644        crq.request_capability.capability =
3645            cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3646        crq.request_capability.number =
3647            cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3648        atomic_inc(&adapter->running_cap_crqs);
3649        ibmvnic_send_crq(adapter, &crq);
3650
3651        crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3652        crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3653        atomic_inc(&adapter->running_cap_crqs);
3654        ibmvnic_send_crq(adapter, &crq);
3655
3656        if (adapter->netdev->flags & IFF_PROMISC) {
3657                if (adapter->promisc_supported) {
3658                        crq.request_capability.capability =
3659                            cpu_to_be16(PROMISC_REQUESTED);
3660                        crq.request_capability.number = cpu_to_be64(1);
3661                        atomic_inc(&adapter->running_cap_crqs);
3662                        ibmvnic_send_crq(adapter, &crq);
3663                }
3664        } else {
3665                crq.request_capability.capability =
3666                    cpu_to_be16(PROMISC_REQUESTED);
3667                crq.request_capability.number = cpu_to_be64(0);
3668                atomic_inc(&adapter->running_cap_crqs);
3669                ibmvnic_send_crq(adapter, &crq);
3670        }
3671}
3672
3673static int pending_scrq(struct ibmvnic_adapter *adapter,
3674                        struct ibmvnic_sub_crq_queue *scrq)
3675{
3676        union sub_crq *entry = &scrq->msgs[scrq->cur];
3677        int rc;
3678
3679        rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3680
3681        /* Ensure that the SCRQ valid flag is loaded prior to loading the
3682         * contents of the SCRQ descriptor
3683         */
3684        dma_rmb();
3685
3686        return rc;
3687}
3688
3689static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3690                                        struct ibmvnic_sub_crq_queue *scrq)
3691{
3692        union sub_crq *entry;
3693        unsigned long flags;
3694
3695        spin_lock_irqsave(&scrq->lock, flags);
3696        entry = &scrq->msgs[scrq->cur];
3697        if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3698                if (++scrq->cur == scrq->size)
3699                        scrq->cur = 0;
3700        } else {
3701                entry = NULL;
3702        }
3703        spin_unlock_irqrestore(&scrq->lock, flags);
3704
3705        /* Ensure that the SCRQ valid flag is loaded prior to loading the
3706         * contents of the SCRQ descriptor
3707         */
3708        dma_rmb();
3709
3710        return entry;
3711}
3712
3713static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3714{
3715        struct ibmvnic_crq_queue *queue = &adapter->crq;
3716        union ibmvnic_crq *crq;
3717
3718        crq = &queue->msgs[queue->cur];
3719        if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3720                if (++queue->cur == queue->size)
3721                        queue->cur = 0;
3722        } else {
3723                crq = NULL;
3724        }
3725
3726        return crq;
3727}
3728
3729static void print_subcrq_error(struct device *dev, int rc, const char *func)
3730{
3731        switch (rc) {
3732        case H_PARAMETER:
3733                dev_warn_ratelimited(dev,
3734                                     "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3735                                     func, rc);
3736                break;
3737        case H_CLOSED:
3738                dev_warn_ratelimited(dev,
3739                                     "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3740                                     func, rc);
3741                break;
3742        default:
3743                dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3744                break;
3745        }
3746}
3747
3748static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3749                                u64 remote_handle, u64 ioba, u64 num_entries)
3750{
3751        unsigned int ua = adapter->vdev->unit_address;
3752        struct device *dev = &adapter->vdev->dev;
3753        int rc;
3754
3755        /* Make sure the hypervisor sees the complete request */
3756        dma_wmb();
3757        rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3758                                cpu_to_be64(remote_handle),
3759                                ioba, num_entries);
3760
3761        if (rc)
3762                print_subcrq_error(dev, rc, __func__);
3763
3764        return rc;
3765}
3766
3767static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3768                            union ibmvnic_crq *crq)
3769{
3770        unsigned int ua = adapter->vdev->unit_address;
3771        struct device *dev = &adapter->vdev->dev;
3772        u64 *u64_crq = (u64 *)crq;
3773        int rc;
3774
3775        netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3776                   (unsigned long)cpu_to_be64(u64_crq[0]),
3777                   (unsigned long)cpu_to_be64(u64_crq[1]));
3778
3779        if (!adapter->crq.active &&
3780            crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3781                dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3782                return -EINVAL;
3783        }
3784
3785        /* Make sure the hypervisor sees the complete request */
3786        dma_wmb();
3787
3788        rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3789                                cpu_to_be64(u64_crq[0]),
3790                                cpu_to_be64(u64_crq[1]));
3791
3792        if (rc) {
3793                if (rc == H_CLOSED) {
3794                        dev_warn(dev, "CRQ Queue closed\n");
3795                        /* do not reset, report the fail, wait for passive init from server */
3796                }
3797
3798                dev_warn(dev, "Send error (rc=%d)\n", rc);
3799        }
3800
3801        return rc;
3802}
3803
3804static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3805{
3806        struct device *dev = &adapter->vdev->dev;
3807        union ibmvnic_crq crq;
3808        int retries = 100;
3809        int rc;
3810
3811        memset(&crq, 0, sizeof(crq));
3812        crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3813        crq.generic.cmd = IBMVNIC_CRQ_INIT;
3814        netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3815
3816        do {
3817                rc = ibmvnic_send_crq(adapter, &crq);
3818                if (rc != H_CLOSED)
3819                        break;
3820                retries--;
3821                msleep(50);
3822
3823        } while (retries > 0);
3824
3825        if (rc) {
3826                dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3827                return rc;
3828        }
3829
3830        return 0;
3831}
3832
3833static int send_version_xchg(struct ibmvnic_adapter *adapter)
3834{
3835        union ibmvnic_crq crq;
3836
3837        memset(&crq, 0, sizeof(crq));
3838        crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3839        crq.version_exchange.cmd = VERSION_EXCHANGE;
3840        crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3841
3842        return ibmvnic_send_crq(adapter, &crq);
3843}
3844
3845struct vnic_login_client_data {
3846        u8      type;
3847        __be16  len;
3848        char    name[];
3849} __packed;
3850
3851static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3852{
3853        int len;
3854
3855        /* Calculate the amount of buffer space needed for the
3856         * vnic client data in the login buffer. There are four entries,
3857         * OS name, LPAR name, device name, and a null last entry.
3858         */
3859        len = 4 * sizeof(struct vnic_login_client_data);
3860        len += 6; /* "Linux" plus NULL */
3861        len += strlen(utsname()->nodename) + 1;
3862        len += strlen(adapter->netdev->name) + 1;
3863
3864        return len;
3865}
3866
3867static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3868                                 struct vnic_login_client_data *vlcd)
3869{
3870        const char *os_name = "Linux";
3871        int len;
3872
3873        /* Type 1 - LPAR OS */
3874        vlcd->type = 1;
3875        len = strlen(os_name) + 1;
3876        vlcd->len = cpu_to_be16(len);
3877