linux/drivers/s390/net/qeth_core_main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *    Copyright IBM Corp. 2007, 2009
   4 *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
   5 *               Frank Pavlic <fpavlic@de.ibm.com>,
   6 *               Thomas Spatzier <tspat@de.ibm.com>,
   7 *               Frank Blaschka <frank.blaschka@de.ibm.com>
   8 */
   9
  10#define KMSG_COMPONENT "qeth"
  11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12
  13#include <linux/compat.h>
  14#include <linux/module.h>
  15#include <linux/moduleparam.h>
  16#include <linux/string.h>
  17#include <linux/errno.h>
  18#include <linux/kernel.h>
  19#include <linux/log2.h>
  20#include <linux/io.h>
  21#include <linux/ip.h>
  22#include <linux/tcp.h>
  23#include <linux/mii.h>
  24#include <linux/mm.h>
  25#include <linux/kthread.h>
  26#include <linux/slab.h>
  27#include <linux/if_vlan.h>
  28#include <linux/netdevice.h>
  29#include <linux/netdev_features.h>
  30#include <linux/rcutree.h>
  31#include <linux/skbuff.h>
  32#include <linux/vmalloc.h>
  33
  34#include <net/iucv/af_iucv.h>
  35#include <net/dsfield.h>
  36#include <net/sock.h>
  37
  38#include <asm/ebcdic.h>
  39#include <asm/chpid.h>
  40#include <asm/sysinfo.h>
  41#include <asm/diag.h>
  42#include <asm/cio.h>
  43#include <asm/ccwdev.h>
  44#include <asm/cpcmd.h>
  45
  46#include "qeth_core.h"
  47
  48struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
  49        /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
  50        /*                   N  P  A    M  L  V                      H  */
  51        [QETH_DBF_SETUP] = {"qeth_setup",
  52                                8, 1,   8, 5, &debug_hex_ascii_view, NULL},
  53        [QETH_DBF_MSG]   = {"qeth_msg", 8, 1, 11 * sizeof(long), 3,
  54                            &debug_sprintf_view, NULL},
  55        [QETH_DBF_CTRL]  = {"qeth_control",
  56                8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
  57};
  58EXPORT_SYMBOL_GPL(qeth_dbf);
  59
  60struct kmem_cache *qeth_core_header_cache;
  61EXPORT_SYMBOL_GPL(qeth_core_header_cache);
  62static struct kmem_cache *qeth_qdio_outbuf_cache;
  63
  64static struct device *qeth_core_root_dev;
  65static struct dentry *qeth_debugfs_root;
  66static struct lock_class_key qdio_out_skb_queue_key;
  67
  68static void qeth_issue_next_read_cb(struct qeth_card *card,
  69                                    struct qeth_cmd_buffer *iob,
  70                                    unsigned int data_length);
  71static int qeth_qdio_establish(struct qeth_card *);
  72static void qeth_free_qdio_queues(struct qeth_card *card);
  73static void qeth_notify_skbs(struct qeth_qdio_out_q *queue,
  74                struct qeth_qdio_out_buffer *buf,
  75                enum iucv_tx_notify notification);
  76
  77static void qeth_close_dev_handler(struct work_struct *work)
  78{
  79        struct qeth_card *card;
  80
  81        card = container_of(work, struct qeth_card, close_dev_work);
  82        QETH_CARD_TEXT(card, 2, "cldevhdl");
  83        ccwgroup_set_offline(card->gdev);
  84}
  85
  86static const char *qeth_get_cardname(struct qeth_card *card)
  87{
  88        if (IS_VM_NIC(card)) {
  89                switch (card->info.type) {
  90                case QETH_CARD_TYPE_OSD:
  91                        return " Virtual NIC QDIO";
  92                case QETH_CARD_TYPE_IQD:
  93                        return " Virtual NIC Hiper";
  94                case QETH_CARD_TYPE_OSM:
  95                        return " Virtual NIC QDIO - OSM";
  96                case QETH_CARD_TYPE_OSX:
  97                        return " Virtual NIC QDIO - OSX";
  98                default:
  99                        return " unknown";
 100                }
 101        } else {
 102                switch (card->info.type) {
 103                case QETH_CARD_TYPE_OSD:
 104                        return " OSD Express";
 105                case QETH_CARD_TYPE_IQD:
 106                        return " HiperSockets";
 107                case QETH_CARD_TYPE_OSN:
 108                        return " OSN QDIO";
 109                case QETH_CARD_TYPE_OSM:
 110                        return " OSM QDIO";
 111                case QETH_CARD_TYPE_OSX:
 112                        return " OSX QDIO";
 113                default:
 114                        return " unknown";
 115                }
 116        }
 117        return " n/a";
 118}
 119
 120/* max length to be returned: 14 */
 121const char *qeth_get_cardname_short(struct qeth_card *card)
 122{
 123        if (IS_VM_NIC(card)) {
 124                switch (card->info.type) {
 125                case QETH_CARD_TYPE_OSD:
 126                        return "Virt.NIC QDIO";
 127                case QETH_CARD_TYPE_IQD:
 128                        return "Virt.NIC Hiper";
 129                case QETH_CARD_TYPE_OSM:
 130                        return "Virt.NIC OSM";
 131                case QETH_CARD_TYPE_OSX:
 132                        return "Virt.NIC OSX";
 133                default:
 134                        return "unknown";
 135                }
 136        } else {
 137                switch (card->info.type) {
 138                case QETH_CARD_TYPE_OSD:
 139                        switch (card->info.link_type) {
 140                        case QETH_LINK_TYPE_FAST_ETH:
 141                                return "OSD_100";
 142                        case QETH_LINK_TYPE_HSTR:
 143                                return "HSTR";
 144                        case QETH_LINK_TYPE_GBIT_ETH:
 145                                return "OSD_1000";
 146                        case QETH_LINK_TYPE_10GBIT_ETH:
 147                                return "OSD_10GIG";
 148                        case QETH_LINK_TYPE_25GBIT_ETH:
 149                                return "OSD_25GIG";
 150                        case QETH_LINK_TYPE_LANE_ETH100:
 151                                return "OSD_FE_LANE";
 152                        case QETH_LINK_TYPE_LANE_TR:
 153                                return "OSD_TR_LANE";
 154                        case QETH_LINK_TYPE_LANE_ETH1000:
 155                                return "OSD_GbE_LANE";
 156                        case QETH_LINK_TYPE_LANE:
 157                                return "OSD_ATM_LANE";
 158                        default:
 159                                return "OSD_Express";
 160                        }
 161                case QETH_CARD_TYPE_IQD:
 162                        return "HiperSockets";
 163                case QETH_CARD_TYPE_OSN:
 164                        return "OSN";
 165                case QETH_CARD_TYPE_OSM:
 166                        return "OSM_1000";
 167                case QETH_CARD_TYPE_OSX:
 168                        return "OSX_10GIG";
 169                default:
 170                        return "unknown";
 171                }
 172        }
 173        return "n/a";
 174}
 175
 176void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
 177                         int clear_start_mask)
 178{
 179        unsigned long flags;
 180
 181        spin_lock_irqsave(&card->thread_mask_lock, flags);
 182        card->thread_allowed_mask = threads;
 183        if (clear_start_mask)
 184                card->thread_start_mask &= threads;
 185        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 186        wake_up(&card->wait_q);
 187}
 188EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
 189
 190int qeth_threads_running(struct qeth_card *card, unsigned long threads)
 191{
 192        unsigned long flags;
 193        int rc = 0;
 194
 195        spin_lock_irqsave(&card->thread_mask_lock, flags);
 196        rc = (card->thread_running_mask & threads);
 197        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 198        return rc;
 199}
 200EXPORT_SYMBOL_GPL(qeth_threads_running);
 201
 202static void qeth_clear_working_pool_list(struct qeth_card *card)
 203{
 204        struct qeth_buffer_pool_entry *pool_entry, *tmp;
 205        struct qeth_qdio_q *queue = card->qdio.in_q;
 206        unsigned int i;
 207
 208        QETH_CARD_TEXT(card, 5, "clwrklst");
 209        list_for_each_entry_safe(pool_entry, tmp,
 210                                 &card->qdio.in_buf_pool.entry_list, list)
 211                list_del(&pool_entry->list);
 212
 213        for (i = 0; i < ARRAY_SIZE(queue->bufs); i++)
 214                queue->bufs[i].pool_entry = NULL;
 215}
 216
 217static void qeth_free_pool_entry(struct qeth_buffer_pool_entry *entry)
 218{
 219        unsigned int i;
 220
 221        for (i = 0; i < ARRAY_SIZE(entry->elements); i++) {
 222                if (entry->elements[i])
 223                        __free_page(entry->elements[i]);
 224        }
 225
 226        kfree(entry);
 227}
 228
 229static void qeth_free_buffer_pool(struct qeth_card *card)
 230{
 231        struct qeth_buffer_pool_entry *entry, *tmp;
 232
 233        list_for_each_entry_safe(entry, tmp, &card->qdio.init_pool.entry_list,
 234                                 init_list) {
 235                list_del(&entry->init_list);
 236                qeth_free_pool_entry(entry);
 237        }
 238}
 239
 240static struct qeth_buffer_pool_entry *qeth_alloc_pool_entry(unsigned int pages)
 241{
 242        struct qeth_buffer_pool_entry *entry;
 243        unsigned int i;
 244
 245        entry = kzalloc(sizeof(*entry), GFP_KERNEL);
 246        if (!entry)
 247                return NULL;
 248
 249        for (i = 0; i < pages; i++) {
 250                entry->elements[i] = __dev_alloc_page(GFP_KERNEL);
 251
 252                if (!entry->elements[i]) {
 253                        qeth_free_pool_entry(entry);
 254                        return NULL;
 255                }
 256        }
 257
 258        return entry;
 259}
 260
 261static int qeth_alloc_buffer_pool(struct qeth_card *card)
 262{
 263        unsigned int buf_elements = QETH_MAX_BUFFER_ELEMENTS(card);
 264        unsigned int i;
 265
 266        QETH_CARD_TEXT(card, 5, "alocpool");
 267        for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
 268                struct qeth_buffer_pool_entry *entry;
 269
 270                entry = qeth_alloc_pool_entry(buf_elements);
 271                if (!entry) {
 272                        qeth_free_buffer_pool(card);
 273                        return -ENOMEM;
 274                }
 275
 276                list_add(&entry->init_list, &card->qdio.init_pool.entry_list);
 277        }
 278        return 0;
 279}
 280
 281int qeth_resize_buffer_pool(struct qeth_card *card, unsigned int count)
 282{
 283        unsigned int buf_elements = QETH_MAX_BUFFER_ELEMENTS(card);
 284        struct qeth_qdio_buffer_pool *pool = &card->qdio.init_pool;
 285        struct qeth_buffer_pool_entry *entry, *tmp;
 286        int delta = count - pool->buf_count;
 287        LIST_HEAD(entries);
 288
 289        QETH_CARD_TEXT(card, 2, "realcbp");
 290
 291        /* Defer until queue is allocated: */
 292        if (!card->qdio.in_q)
 293                goto out;
 294
 295        /* Remove entries from the pool: */
 296        while (delta < 0) {
 297                entry = list_first_entry(&pool->entry_list,
 298                                         struct qeth_buffer_pool_entry,
 299                                         init_list);
 300                list_del(&entry->init_list);
 301                qeth_free_pool_entry(entry);
 302
 303                delta++;
 304        }
 305
 306        /* Allocate additional entries: */
 307        while (delta > 0) {
 308                entry = qeth_alloc_pool_entry(buf_elements);
 309                if (!entry) {
 310                        list_for_each_entry_safe(entry, tmp, &entries,
 311                                                 init_list) {
 312                                list_del(&entry->init_list);
 313                                qeth_free_pool_entry(entry);
 314                        }
 315
 316                        return -ENOMEM;
 317                }
 318
 319                list_add(&entry->init_list, &entries);
 320
 321                delta--;
 322        }
 323
 324        list_splice(&entries, &pool->entry_list);
 325
 326out:
 327        card->qdio.in_buf_pool.buf_count = count;
 328        pool->buf_count = count;
 329        return 0;
 330}
 331EXPORT_SYMBOL_GPL(qeth_resize_buffer_pool);
 332
 333static void qeth_free_qdio_queue(struct qeth_qdio_q *q)
 334{
 335        if (!q)
 336                return;
 337
 338        qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
 339        kfree(q);
 340}
 341
 342static struct qeth_qdio_q *qeth_alloc_qdio_queue(void)
 343{
 344        struct qeth_qdio_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
 345        int i;
 346
 347        if (!q)
 348                return NULL;
 349
 350        if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
 351                kfree(q);
 352                return NULL;
 353        }
 354
 355        for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
 356                q->bufs[i].buffer = q->qdio_bufs[i];
 357
 358        QETH_DBF_HEX(SETUP, 2, &q, sizeof(void *));
 359        return q;
 360}
 361
 362static int qeth_cq_init(struct qeth_card *card)
 363{
 364        int rc;
 365
 366        if (card->options.cq == QETH_CQ_ENABLED) {
 367                QETH_CARD_TEXT(card, 2, "cqinit");
 368                qdio_reset_buffers(card->qdio.c_q->qdio_bufs,
 369                                   QDIO_MAX_BUFFERS_PER_Q);
 370                card->qdio.c_q->next_buf_to_init = 127;
 371                rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
 372                             card->qdio.no_in_queues - 1, 0, 127, NULL);
 373                if (rc) {
 374                        QETH_CARD_TEXT_(card, 2, "1err%d", rc);
 375                        goto out;
 376                }
 377        }
 378        rc = 0;
 379out:
 380        return rc;
 381}
 382
 383static int qeth_alloc_cq(struct qeth_card *card)
 384{
 385        if (card->options.cq == QETH_CQ_ENABLED) {
 386                QETH_CARD_TEXT(card, 2, "cqon");
 387                card->qdio.c_q = qeth_alloc_qdio_queue();
 388                if (!card->qdio.c_q) {
 389                        dev_err(&card->gdev->dev, "Failed to create completion queue\n");
 390                        return -ENOMEM;
 391                }
 392
 393                card->qdio.no_in_queues = 2;
 394        } else {
 395                QETH_CARD_TEXT(card, 2, "nocq");
 396                card->qdio.c_q = NULL;
 397                card->qdio.no_in_queues = 1;
 398        }
 399        QETH_CARD_TEXT_(card, 2, "iqc%d", card->qdio.no_in_queues);
 400        return 0;
 401}
 402
 403static void qeth_free_cq(struct qeth_card *card)
 404{
 405        if (card->qdio.c_q) {
 406                --card->qdio.no_in_queues;
 407                qeth_free_qdio_queue(card->qdio.c_q);
 408                card->qdio.c_q = NULL;
 409        }
 410}
 411
 412static enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
 413                                                        int delayed)
 414{
 415        enum iucv_tx_notify n;
 416
 417        switch (sbalf15) {
 418        case 0:
 419                n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
 420                break;
 421        case 4:
 422        case 16:
 423        case 17:
 424        case 18:
 425                n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
 426                        TX_NOTIFY_UNREACHABLE;
 427                break;
 428        default:
 429                n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
 430                        TX_NOTIFY_GENERALERROR;
 431                break;
 432        }
 433
 434        return n;
 435}
 436
 437static void qeth_qdio_handle_aob(struct qeth_card *card,
 438                                 unsigned long phys_aob_addr)
 439{
 440        enum qeth_qdio_out_buffer_state new_state = QETH_QDIO_BUF_QAOB_OK;
 441        struct qaob *aob;
 442        struct qeth_qdio_out_buffer *buffer;
 443        enum iucv_tx_notify notification;
 444        struct qeth_qdio_out_q *queue;
 445        unsigned int i;
 446
 447        aob = (struct qaob *) phys_to_virt(phys_aob_addr);
 448        QETH_CARD_TEXT(card, 5, "haob");
 449        QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
 450        buffer = (struct qeth_qdio_out_buffer *) aob->user1;
 451        QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
 452
 453        if (aob->aorc) {
 454                QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
 455                new_state = QETH_QDIO_BUF_QAOB_ERROR;
 456        }
 457
 458        switch (atomic_xchg(&buffer->state, new_state)) {
 459        case QETH_QDIO_BUF_PRIMED:
 460                /* Faster than TX completion code, let it handle the async
 461                 * completion for us. It will also recycle the QAOB.
 462                 */
 463                break;
 464        case QETH_QDIO_BUF_PENDING:
 465                /* TX completion code is active and will handle the async
 466                 * completion for us. It will also recycle the QAOB.
 467                 */
 468                break;
 469        case QETH_QDIO_BUF_NEED_QAOB:
 470                /* TX completion code is already finished. */
 471                notification = qeth_compute_cq_notification(aob->aorc, 1);
 472                qeth_notify_skbs(buffer->q, buffer, notification);
 473
 474                /* Free dangling allocations. The attached skbs are handled by
 475                 * qeth_tx_complete_pending_bufs(), and so is the QAOB.
 476                 */
 477                for (i = 0;
 478                     i < aob->sb_count && i < QETH_MAX_BUFFER_ELEMENTS(card);
 479                     i++) {
 480                        void *data = phys_to_virt(aob->sba[i]);
 481
 482                        if (data && buffer->is_header[i])
 483                                kmem_cache_free(qeth_core_header_cache, data);
 484                        buffer->is_header[i] = 0;
 485                }
 486
 487                queue = buffer->q;
 488                atomic_set(&buffer->state, QETH_QDIO_BUF_EMPTY);
 489                napi_schedule(&queue->napi);
 490                break;
 491        default:
 492                WARN_ON_ONCE(1);
 493        }
 494}
 495
 496static void qeth_setup_ccw(struct ccw1 *ccw, u8 cmd_code, u8 flags, u32 len,
 497                           void *data)
 498{
 499        ccw->cmd_code = cmd_code;
 500        ccw->flags = flags | CCW_FLAG_SLI;
 501        ccw->count = len;
 502        ccw->cda = (__u32) __pa(data);
 503}
 504
 505static int __qeth_issue_next_read(struct qeth_card *card)
 506{
 507        struct qeth_cmd_buffer *iob = card->read_cmd;
 508        struct qeth_channel *channel = iob->channel;
 509        struct ccw1 *ccw = __ccw_from_cmd(iob);
 510        int rc;
 511
 512        QETH_CARD_TEXT(card, 5, "issnxrd");
 513        if (channel->state != CH_STATE_UP)
 514                return -EIO;
 515
 516        memset(iob->data, 0, iob->length);
 517        qeth_setup_ccw(ccw, CCW_CMD_READ, 0, iob->length, iob->data);
 518        iob->callback = qeth_issue_next_read_cb;
 519        /* keep the cmd alive after completion: */
 520        qeth_get_cmd(iob);
 521
 522        QETH_CARD_TEXT(card, 6, "noirqpnd");
 523        rc = ccw_device_start(channel->ccwdev, ccw, (addr_t) iob, 0, 0);
 524        if (!rc) {
 525                channel->active_cmd = iob;
 526        } else {
 527                QETH_DBF_MESSAGE(2, "error %i on device %x when starting next read ccw!\n",
 528                                 rc, CARD_DEVID(card));
 529                qeth_unlock_channel(card, channel);
 530                qeth_put_cmd(iob);
 531                card->read_or_write_problem = 1;
 532                qeth_schedule_recovery(card);
 533        }
 534        return rc;
 535}
 536
 537static int qeth_issue_next_read(struct qeth_card *card)
 538{
 539        int ret;
 540
 541        spin_lock_irq(get_ccwdev_lock(CARD_RDEV(card)));
 542        ret = __qeth_issue_next_read(card);
 543        spin_unlock_irq(get_ccwdev_lock(CARD_RDEV(card)));
 544
 545        return ret;
 546}
 547
 548static void qeth_enqueue_cmd(struct qeth_card *card,
 549                             struct qeth_cmd_buffer *iob)
 550{
 551        spin_lock_irq(&card->lock);
 552        list_add_tail(&iob->list_entry, &card->cmd_waiter_list);
 553        spin_unlock_irq(&card->lock);
 554}
 555
 556static void qeth_dequeue_cmd(struct qeth_card *card,
 557                             struct qeth_cmd_buffer *iob)
 558{
 559        spin_lock_irq(&card->lock);
 560        list_del(&iob->list_entry);
 561        spin_unlock_irq(&card->lock);
 562}
 563
 564void qeth_notify_cmd(struct qeth_cmd_buffer *iob, int reason)
 565{
 566        iob->rc = reason;
 567        complete(&iob->done);
 568}
 569EXPORT_SYMBOL_GPL(qeth_notify_cmd);
 570
 571static void qeth_flush_local_addrs4(struct qeth_card *card)
 572{
 573        struct qeth_local_addr *addr;
 574        struct hlist_node *tmp;
 575        unsigned int i;
 576
 577        spin_lock_irq(&card->local_addrs4_lock);
 578        hash_for_each_safe(card->local_addrs4, i, tmp, addr, hnode) {
 579                hash_del_rcu(&addr->hnode);
 580                kfree_rcu(addr, rcu);
 581        }
 582        spin_unlock_irq(&card->local_addrs4_lock);
 583}
 584
 585static void qeth_flush_local_addrs6(struct qeth_card *card)
 586{
 587        struct qeth_local_addr *addr;
 588        struct hlist_node *tmp;
 589        unsigned int i;
 590
 591        spin_lock_irq(&card->local_addrs6_lock);
 592        hash_for_each_safe(card->local_addrs6, i, tmp, addr, hnode) {
 593                hash_del_rcu(&addr->hnode);
 594                kfree_rcu(addr, rcu);
 595        }
 596        spin_unlock_irq(&card->local_addrs6_lock);
 597}
 598
 599static void qeth_flush_local_addrs(struct qeth_card *card)
 600{
 601        qeth_flush_local_addrs4(card);
 602        qeth_flush_local_addrs6(card);
 603}
 604
 605static void qeth_add_local_addrs4(struct qeth_card *card,
 606                                  struct qeth_ipacmd_local_addrs4 *cmd)
 607{
 608        unsigned int i;
 609
 610        if (cmd->addr_length !=
 611            sizeof_field(struct qeth_ipacmd_local_addr4, addr)) {
 612                dev_err_ratelimited(&card->gdev->dev,
 613                                    "Dropped IPv4 ADD LOCAL ADDR event with bad length %u\n",
 614                                    cmd->addr_length);
 615                return;
 616        }
 617
 618        spin_lock(&card->local_addrs4_lock);
 619        for (i = 0; i < cmd->count; i++) {
 620                unsigned int key = ipv4_addr_hash(cmd->addrs[i].addr);
 621                struct qeth_local_addr *addr;
 622                bool duplicate = false;
 623
 624                hash_for_each_possible(card->local_addrs4, addr, hnode, key) {
 625                        if (addr->addr.s6_addr32[3] == cmd->addrs[i].addr) {
 626                                duplicate = true;
 627                                break;
 628                        }
 629                }
 630
 631                if (duplicate)
 632                        continue;
 633
 634                addr = kmalloc(sizeof(*addr), GFP_ATOMIC);
 635                if (!addr) {
 636                        dev_err(&card->gdev->dev,
 637                                "Failed to allocate local addr object. Traffic to %pI4 might suffer.\n",
 638                                &cmd->addrs[i].addr);
 639                        continue;
 640                }
 641
 642                ipv6_addr_set(&addr->addr, 0, 0, 0, cmd->addrs[i].addr);
 643                hash_add_rcu(card->local_addrs4, &addr->hnode, key);
 644        }
 645        spin_unlock(&card->local_addrs4_lock);
 646}
 647
 648static void qeth_add_local_addrs6(struct qeth_card *card,
 649                                  struct qeth_ipacmd_local_addrs6 *cmd)
 650{
 651        unsigned int i;
 652
 653        if (cmd->addr_length !=
 654            sizeof_field(struct qeth_ipacmd_local_addr6, addr)) {
 655                dev_err_ratelimited(&card->gdev->dev,
 656                                    "Dropped IPv6 ADD LOCAL ADDR event with bad length %u\n",
 657                                    cmd->addr_length);
 658                return;
 659        }
 660
 661        spin_lock(&card->local_addrs6_lock);
 662        for (i = 0; i < cmd->count; i++) {
 663                u32 key = ipv6_addr_hash(&cmd->addrs[i].addr);
 664                struct qeth_local_addr *addr;
 665                bool duplicate = false;
 666
 667                hash_for_each_possible(card->local_addrs6, addr, hnode, key) {
 668                        if (ipv6_addr_equal(&addr->addr, &cmd->addrs[i].addr)) {
 669                                duplicate = true;
 670                                break;
 671                        }
 672                }
 673
 674                if (duplicate)
 675                        continue;
 676
 677                addr = kmalloc(sizeof(*addr), GFP_ATOMIC);
 678                if (!addr) {
 679                        dev_err(&card->gdev->dev,
 680                                "Failed to allocate local addr object. Traffic to %pI6c might suffer.\n",
 681                                &cmd->addrs[i].addr);
 682                        continue;
 683                }
 684
 685                addr->addr = cmd->addrs[i].addr;
 686                hash_add_rcu(card->local_addrs6, &addr->hnode, key);
 687        }
 688        spin_unlock(&card->local_addrs6_lock);
 689}
 690
 691static void qeth_del_local_addrs4(struct qeth_card *card,
 692                                  struct qeth_ipacmd_local_addrs4 *cmd)
 693{
 694        unsigned int i;
 695
 696        if (cmd->addr_length !=
 697            sizeof_field(struct qeth_ipacmd_local_addr4, addr)) {
 698                dev_err_ratelimited(&card->gdev->dev,
 699                                    "Dropped IPv4 DEL LOCAL ADDR event with bad length %u\n",
 700                                    cmd->addr_length);
 701                return;
 702        }
 703
 704        spin_lock(&card->local_addrs4_lock);
 705        for (i = 0; i < cmd->count; i++) {
 706                struct qeth_ipacmd_local_addr4 *addr = &cmd->addrs[i];
 707                unsigned int key = ipv4_addr_hash(addr->addr);
 708                struct qeth_local_addr *tmp;
 709
 710                hash_for_each_possible(card->local_addrs4, tmp, hnode, key) {
 711                        if (tmp->addr.s6_addr32[3] == addr->addr) {
 712                                hash_del_rcu(&tmp->hnode);
 713                                kfree_rcu(tmp, rcu);
 714                                break;
 715                        }
 716                }
 717        }
 718        spin_unlock(&card->local_addrs4_lock);
 719}
 720
 721static void qeth_del_local_addrs6(struct qeth_card *card,
 722                                  struct qeth_ipacmd_local_addrs6 *cmd)
 723{
 724        unsigned int i;
 725
 726        if (cmd->addr_length !=
 727            sizeof_field(struct qeth_ipacmd_local_addr6, addr)) {
 728                dev_err_ratelimited(&card->gdev->dev,
 729                                    "Dropped IPv6 DEL LOCAL ADDR event with bad length %u\n",
 730                                    cmd->addr_length);
 731                return;
 732        }
 733
 734        spin_lock(&card->local_addrs6_lock);
 735        for (i = 0; i < cmd->count; i++) {
 736                struct qeth_ipacmd_local_addr6 *addr = &cmd->addrs[i];
 737                u32 key = ipv6_addr_hash(&addr->addr);
 738                struct qeth_local_addr *tmp;
 739
 740                hash_for_each_possible(card->local_addrs6, tmp, hnode, key) {
 741                        if (ipv6_addr_equal(&tmp->addr, &addr->addr)) {
 742                                hash_del_rcu(&tmp->hnode);
 743                                kfree_rcu(tmp, rcu);
 744                                break;
 745                        }
 746                }
 747        }
 748        spin_unlock(&card->local_addrs6_lock);
 749}
 750
 751static bool qeth_next_hop_is_local_v4(struct qeth_card *card,
 752                                      struct sk_buff *skb)
 753{
 754        struct qeth_local_addr *tmp;
 755        bool is_local = false;
 756        unsigned int key;
 757        __be32 next_hop;
 758
 759        if (hash_empty(card->local_addrs4))
 760                return false;
 761
 762        rcu_read_lock();
 763        next_hop = qeth_next_hop_v4_rcu(skb,
 764                                        qeth_dst_check_rcu(skb, htons(ETH_P_IP)));
 765        key = ipv4_addr_hash(next_hop);
 766
 767        hash_for_each_possible_rcu(card->local_addrs4, tmp, hnode, key) {
 768                if (tmp->addr.s6_addr32[3] == next_hop) {
 769                        is_local = true;
 770                        break;
 771                }
 772        }
 773        rcu_read_unlock();
 774
 775        return is_local;
 776}
 777
 778static bool qeth_next_hop_is_local_v6(struct qeth_card *card,
 779                                      struct sk_buff *skb)
 780{
 781        struct qeth_local_addr *tmp;
 782        struct in6_addr *next_hop;
 783        bool is_local = false;
 784        u32 key;
 785
 786        if (hash_empty(card->local_addrs6))
 787                return false;
 788
 789        rcu_read_lock();
 790        next_hop = qeth_next_hop_v6_rcu(skb,
 791                                        qeth_dst_check_rcu(skb, htons(ETH_P_IPV6)));
 792        key = ipv6_addr_hash(next_hop);
 793
 794        hash_for_each_possible_rcu(card->local_addrs6, tmp, hnode, key) {
 795                if (ipv6_addr_equal(&tmp->addr, next_hop)) {
 796                        is_local = true;
 797                        break;
 798                }
 799        }
 800        rcu_read_unlock();
 801
 802        return is_local;
 803}
 804
 805static int qeth_debugfs_local_addr_show(struct seq_file *m, void *v)
 806{
 807        struct qeth_card *card = m->private;
 808        struct qeth_local_addr *tmp;
 809        unsigned int i;
 810
 811        rcu_read_lock();
 812        hash_for_each_rcu(card->local_addrs4, i, tmp, hnode)
 813                seq_printf(m, "%pI4\n", &tmp->addr.s6_addr32[3]);
 814        hash_for_each_rcu(card->local_addrs6, i, tmp, hnode)
 815                seq_printf(m, "%pI6c\n", &tmp->addr);
 816        rcu_read_unlock();
 817
 818        return 0;
 819}
 820
 821DEFINE_SHOW_ATTRIBUTE(qeth_debugfs_local_addr);
 822
 823static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
 824                struct qeth_card *card)
 825{
 826        const char *ipa_name;
 827        int com = cmd->hdr.command;
 828
 829        ipa_name = qeth_get_ipa_cmd_name(com);
 830
 831        if (rc)
 832                QETH_DBF_MESSAGE(2, "IPA: %s(%#x) for device %x returned %#x \"%s\"\n",
 833                                 ipa_name, com, CARD_DEVID(card), rc,
 834                                 qeth_get_ipa_msg(rc));
 835        else
 836                QETH_DBF_MESSAGE(5, "IPA: %s(%#x) for device %x succeeded\n",
 837                                 ipa_name, com, CARD_DEVID(card));
 838}
 839
 840static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
 841                                                struct qeth_ipa_cmd *cmd)
 842{
 843        QETH_CARD_TEXT(card, 5, "chkipad");
 844
 845        if (IS_IPA_REPLY(cmd)) {
 846                if (cmd->hdr.command != IPA_CMD_SETCCID &&
 847                    cmd->hdr.command != IPA_CMD_DELCCID &&
 848                    cmd->hdr.command != IPA_CMD_MODCCID &&
 849                    cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
 850                        qeth_issue_ipa_msg(cmd, cmd->hdr.return_code, card);
 851                return cmd;
 852        }
 853
 854        /* handle unsolicited event: */
 855        switch (cmd->hdr.command) {
 856        case IPA_CMD_STOPLAN:
 857                if (cmd->hdr.return_code == IPA_RC_VEPA_TO_VEB_TRANSITION) {
 858                        dev_err(&card->gdev->dev,
 859                                "Interface %s is down because the adjacent port is no longer in reflective relay mode\n",
 860                                netdev_name(card->dev));
 861                        schedule_work(&card->close_dev_work);
 862                } else {
 863                        dev_warn(&card->gdev->dev,
 864                                 "The link for interface %s on CHPID 0x%X failed\n",
 865                                 netdev_name(card->dev), card->info.chpid);
 866                        qeth_issue_ipa_msg(cmd, cmd->hdr.return_code, card);
 867                        netif_carrier_off(card->dev);
 868                }
 869                return NULL;
 870        case IPA_CMD_STARTLAN:
 871                dev_info(&card->gdev->dev,
 872                         "The link for %s on CHPID 0x%X has been restored\n",
 873                         netdev_name(card->dev), card->info.chpid);
 874                if (card->info.hwtrap)
 875                        card->info.hwtrap = 2;
 876                qeth_schedule_recovery(card);
 877                return NULL;
 878        case IPA_CMD_SETBRIDGEPORT_IQD:
 879        case IPA_CMD_SETBRIDGEPORT_OSA:
 880        case IPA_CMD_ADDRESS_CHANGE_NOTIF:
 881                if (card->discipline->control_event_handler(card, cmd))
 882                        return cmd;
 883                return NULL;
 884        case IPA_CMD_MODCCID:
 885                return cmd;
 886        case IPA_CMD_REGISTER_LOCAL_ADDR:
 887                if (cmd->hdr.prot_version == QETH_PROT_IPV4)
 888                        qeth_add_local_addrs4(card, &cmd->data.local_addrs4);
 889                else if (cmd->hdr.prot_version == QETH_PROT_IPV6)
 890                        qeth_add_local_addrs6(card, &cmd->data.local_addrs6);
 891
 892                QETH_CARD_TEXT(card, 3, "irla");
 893                return NULL;
 894        case IPA_CMD_UNREGISTER_LOCAL_ADDR:
 895                if (cmd->hdr.prot_version == QETH_PROT_IPV4)
 896                        qeth_del_local_addrs4(card, &cmd->data.local_addrs4);
 897                else if (cmd->hdr.prot_version == QETH_PROT_IPV6)
 898                        qeth_del_local_addrs6(card, &cmd->data.local_addrs6);
 899
 900                QETH_CARD_TEXT(card, 3, "urla");
 901                return NULL;
 902        default:
 903                QETH_DBF_MESSAGE(2, "Received data is IPA but not a reply!\n");
 904                return cmd;
 905        }
 906}
 907
 908static void qeth_clear_ipacmd_list(struct qeth_card *card)
 909{
 910        struct qeth_cmd_buffer *iob;
 911        unsigned long flags;
 912
 913        QETH_CARD_TEXT(card, 4, "clipalst");
 914
 915        spin_lock_irqsave(&card->lock, flags);
 916        list_for_each_entry(iob, &card->cmd_waiter_list, list_entry)
 917                qeth_notify_cmd(iob, -ECANCELED);
 918        spin_unlock_irqrestore(&card->lock, flags);
 919}
 920
 921static int qeth_check_idx_response(struct qeth_card *card,
 922        unsigned char *buffer)
 923{
 924        QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
 925        if ((buffer[2] & QETH_IDX_TERMINATE_MASK) == QETH_IDX_TERMINATE) {
 926                QETH_DBF_MESSAGE(2, "received an IDX TERMINATE with cause code %#04x\n",
 927                                 buffer[4]);
 928                QETH_CARD_TEXT(card, 2, "ckidxres");
 929                QETH_CARD_TEXT(card, 2, " idxterm");
 930                QETH_CARD_TEXT_(card, 2, "rc%x", buffer[4]);
 931                if (buffer[4] == QETH_IDX_TERM_BAD_TRANSPORT ||
 932                    buffer[4] == QETH_IDX_TERM_BAD_TRANSPORT_VM) {
 933                        dev_err(&card->gdev->dev,
 934                                "The device does not support the configured transport mode\n");
 935                        return -EPROTONOSUPPORT;
 936                }
 937                return -EIO;
 938        }
 939        return 0;
 940}
 941
 942void qeth_put_cmd(struct qeth_cmd_buffer *iob)
 943{
 944        if (refcount_dec_and_test(&iob->ref_count)) {
 945                kfree(iob->data);
 946                kfree(iob);
 947        }
 948}
 949EXPORT_SYMBOL_GPL(qeth_put_cmd);
 950
 951static void qeth_release_buffer_cb(struct qeth_card *card,
 952                                   struct qeth_cmd_buffer *iob,
 953                                   unsigned int data_length)
 954{
 955        qeth_put_cmd(iob);
 956}
 957
 958static void qeth_cancel_cmd(struct qeth_cmd_buffer *iob, int rc)
 959{
 960        qeth_notify_cmd(iob, rc);
 961        qeth_put_cmd(iob);
 962}
 963
 964struct qeth_cmd_buffer *qeth_alloc_cmd(struct qeth_channel *channel,
 965                                       unsigned int length, unsigned int ccws,
 966                                       long timeout)
 967{
 968        struct qeth_cmd_buffer *iob;
 969
 970        if (length > QETH_BUFSIZE)
 971                return NULL;
 972
 973        iob = kzalloc(sizeof(*iob), GFP_KERNEL);
 974        if (!iob)
 975                return NULL;
 976
 977        iob->data = kzalloc(ALIGN(length, 8) + ccws * sizeof(struct ccw1),
 978                            GFP_KERNEL | GFP_DMA);
 979        if (!iob->data) {
 980                kfree(iob);
 981                return NULL;
 982        }
 983
 984        init_completion(&iob->done);
 985        spin_lock_init(&iob->lock);
 986        refcount_set(&iob->ref_count, 1);
 987        iob->channel = channel;
 988        iob->timeout = timeout;
 989        iob->length = length;
 990        return iob;
 991}
 992EXPORT_SYMBOL_GPL(qeth_alloc_cmd);
 993
 994static void qeth_issue_next_read_cb(struct qeth_card *card,
 995                                    struct qeth_cmd_buffer *iob,
 996                                    unsigned int data_length)
 997{
 998        struct qeth_cmd_buffer *request = NULL;
 999        struct qeth_ipa_cmd *cmd = NULL;
1000        struct qeth_reply *reply = NULL;
1001        struct qeth_cmd_buffer *tmp;
1002        unsigned long flags;
1003        int rc = 0;
1004
1005        QETH_CARD_TEXT(card, 4, "sndctlcb");
1006        rc = qeth_check_idx_response(card, iob->data);
1007        switch (rc) {
1008        case 0:
1009                break;
1010        case -EIO:
1011                qeth_schedule_recovery(card);
1012                fallthrough;
1013        default:
1014                qeth_clear_ipacmd_list(card);
1015                goto err_idx;
1016        }
1017
1018        cmd = __ipa_reply(iob);
1019        if (cmd) {
1020                cmd = qeth_check_ipa_data(card, cmd);
1021                if (!cmd)
1022                        goto out;
1023                if (IS_OSN(card) && card->osn_info.assist_cb &&
1024                    cmd->hdr.command != IPA_CMD_STARTLAN) {
1025                        card->osn_info.assist_cb(card->dev, cmd);
1026                        goto out;
1027                }
1028        }
1029
1030        /* match against pending cmd requests */
1031        spin_lock_irqsave(&card->lock, flags);
1032        list_for_each_entry(tmp, &card->cmd_waiter_list, list_entry) {
1033                if (tmp->match && tmp->match(tmp, iob)) {
1034                        request = tmp;
1035                        /* take the object outside the lock */
1036                        qeth_get_cmd(request);
1037                        break;
1038                }
1039        }
1040        spin_unlock_irqrestore(&card->lock, flags);
1041
1042        if (!request)
1043                goto out;
1044
1045        reply = &request->reply;
1046        if (!reply->callback) {
1047                rc = 0;
1048                goto no_callback;
1049        }
1050
1051        spin_lock_irqsave(&request->lock, flags);
1052        if (request->rc)
1053                /* Bail out when the requestor has already left: */
1054                rc = request->rc;
1055        else
1056                rc = reply->callback(card, reply, cmd ? (unsigned long)cmd :
1057                                                        (unsigned long)iob);
1058        spin_unlock_irqrestore(&request->lock, flags);
1059
1060no_callback:
1061        if (rc <= 0)
1062                qeth_notify_cmd(request, rc);
1063        qeth_put_cmd(request);
1064out:
1065        memcpy(&card->seqno.pdu_hdr_ack,
1066                QETH_PDU_HEADER_SEQ_NO(iob->data),
1067                QETH_SEQ_NO_LENGTH);
1068        __qeth_issue_next_read(card);
1069err_idx:
1070        qeth_put_cmd(iob);
1071}
1072
1073static int qeth_set_thread_start_bit(struct qeth_card *card,
1074                unsigned long thread)
1075{
1076        unsigned long flags;
1077        int rc = 0;
1078
1079        spin_lock_irqsave(&card->thread_mask_lock, flags);
1080        if (!(card->thread_allowed_mask & thread))
1081                rc = -EPERM;
1082        else if (card->thread_start_mask & thread)
1083                rc = -EBUSY;
1084        else
1085                card->thread_start_mask |= thread;
1086        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1087
1088        return rc;
1089}
1090
1091static void qeth_clear_thread_start_bit(struct qeth_card *card,
1092                                        unsigned long thread)
1093{
1094        unsigned long flags;
1095
1096        spin_lock_irqsave(&card->thread_mask_lock, flags);
1097        card->thread_start_mask &= ~thread;
1098        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1099        wake_up(&card->wait_q);
1100}
1101
1102static void qeth_clear_thread_running_bit(struct qeth_card *card,
1103                                          unsigned long thread)
1104{
1105        unsigned long flags;
1106
1107        spin_lock_irqsave(&card->thread_mask_lock, flags);
1108        card->thread_running_mask &= ~thread;
1109        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1110        wake_up_all(&card->wait_q);
1111}
1112
1113static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
1114{
1115        unsigned long flags;
1116        int rc = 0;
1117
1118        spin_lock_irqsave(&card->thread_mask_lock, flags);
1119        if (card->thread_start_mask & thread) {
1120                if ((card->thread_allowed_mask & thread) &&
1121                    !(card->thread_running_mask & thread)) {
1122                        rc = 1;
1123                        card->thread_start_mask &= ~thread;
1124                        card->thread_running_mask |= thread;
1125                } else
1126                        rc = -EPERM;
1127        }
1128        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1129        return rc;
1130}
1131
1132static int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
1133{
1134        int rc = 0;
1135
1136        wait_event(card->wait_q,
1137                   (rc = __qeth_do_run_thread(card, thread)) >= 0);
1138        return rc;
1139}
1140
1141int qeth_schedule_recovery(struct qeth_card *card)
1142{
1143        int rc;
1144
1145        QETH_CARD_TEXT(card, 2, "startrec");
1146
1147        rc = qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD);
1148        if (!rc)
1149                schedule_work(&card->kernel_thread_starter);
1150
1151        return rc;
1152}
1153
1154static int qeth_get_problem(struct qeth_card *card, struct ccw_device *cdev,
1155                            struct irb *irb)
1156{
1157        int dstat, cstat;
1158        char *sense;
1159
1160        sense = (char *) irb->ecw;
1161        cstat = irb->scsw.cmd.cstat;
1162        dstat = irb->scsw.cmd.dstat;
1163
1164        if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1165                     SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1166                     SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
1167                QETH_CARD_TEXT(card, 2, "CGENCHK");
1168                dev_warn(&cdev->dev, "The qeth device driver "
1169                        "failed to recover an error on the device\n");
1170                QETH_DBF_MESSAGE(2, "check on channel %x with dstat=%#x, cstat=%#x\n",
1171                                 CCW_DEVID(cdev), dstat, cstat);
1172                print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
1173                                16, 1, irb, 64, 1);
1174                return -EIO;
1175        }
1176
1177        if (dstat & DEV_STAT_UNIT_CHECK) {
1178                if (sense[SENSE_RESETTING_EVENT_BYTE] &
1179                    SENSE_RESETTING_EVENT_FLAG) {
1180                        QETH_CARD_TEXT(card, 2, "REVIND");
1181                        return -EIO;
1182                }
1183                if (sense[SENSE_COMMAND_REJECT_BYTE] &
1184                    SENSE_COMMAND_REJECT_FLAG) {
1185                        QETH_CARD_TEXT(card, 2, "CMDREJi");
1186                        return -EIO;
1187                }
1188                if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
1189                        QETH_CARD_TEXT(card, 2, "AFFE");
1190                        return -EIO;
1191                }
1192                if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
1193                        QETH_CARD_TEXT(card, 2, "ZEROSEN");
1194                        return 0;
1195                }
1196                QETH_CARD_TEXT(card, 2, "DGENCHK");
1197                return -EIO;
1198        }
1199        return 0;
1200}
1201
1202static int qeth_check_irb_error(struct qeth_card *card, struct ccw_device *cdev,
1203                                struct irb *irb)
1204{
1205        if (!IS_ERR(irb))
1206                return 0;
1207
1208        switch (PTR_ERR(irb)) {
1209        case -EIO:
1210                QETH_DBF_MESSAGE(2, "i/o-error on channel %x\n",
1211                                 CCW_DEVID(cdev));
1212                QETH_CARD_TEXT(card, 2, "ckirberr");
1213                QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
1214                return -EIO;
1215        case -ETIMEDOUT:
1216                dev_warn(&cdev->dev, "A hardware operation timed out"
1217                        " on the device\n");
1218                QETH_CARD_TEXT(card, 2, "ckirberr");
1219                QETH_CARD_TEXT_(card, 2, "  rc%d", -ETIMEDOUT);
1220                return -ETIMEDOUT;
1221        default:
1222                QETH_DBF_MESSAGE(2, "unknown error %ld on channel %x\n",
1223                                 PTR_ERR(irb), CCW_DEVID(cdev));
1224                QETH_CARD_TEXT(card, 2, "ckirberr");
1225                QETH_CARD_TEXT(card, 2, "  rc???");
1226                return PTR_ERR(irb);
1227        }
1228}
1229
1230static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
1231                struct irb *irb)
1232{
1233        int rc;
1234        int cstat, dstat;
1235        struct qeth_cmd_buffer *iob = NULL;
1236        struct ccwgroup_device *gdev;
1237        struct qeth_channel *channel;
1238        struct qeth_card *card;
1239
1240        /* while we hold the ccwdev lock, this stays valid: */
1241        gdev = dev_get_drvdata(&cdev->dev);
1242        card = dev_get_drvdata(&gdev->dev);
1243
1244        QETH_CARD_TEXT(card, 5, "irq");
1245
1246        if (card->read.ccwdev == cdev) {
1247                channel = &card->read;
1248                QETH_CARD_TEXT(card, 5, "read");
1249        } else if (card->write.ccwdev == cdev) {
1250                channel = &card->write;
1251                QETH_CARD_TEXT(card, 5, "write");
1252        } else {
1253                channel = &card->data;
1254                QETH_CARD_TEXT(card, 5, "data");
1255        }
1256
1257        if (intparm == 0) {
1258                QETH_CARD_TEXT(card, 5, "irqunsol");
1259        } else if ((addr_t)intparm != (addr_t)channel->active_cmd) {
1260                QETH_CARD_TEXT(card, 5, "irqunexp");
1261
1262                dev_err(&cdev->dev,
1263                        "Received IRQ with intparm %lx, expected %px\n",
1264                        intparm, channel->active_cmd);
1265                if (channel->active_cmd)
1266                        qeth_cancel_cmd(channel->active_cmd, -EIO);
1267        } else {
1268                iob = (struct qeth_cmd_buffer *) (addr_t)intparm;
1269        }
1270
1271        channel->active_cmd = NULL;
1272        qeth_unlock_channel(card, channel);
1273
1274        rc = qeth_check_irb_error(card, cdev, irb);
1275        if (rc) {
1276                /* IO was terminated, free its resources. */
1277                if (iob)
1278                        qeth_cancel_cmd(iob, rc);
1279                return;
1280        }
1281
1282        if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC) {
1283                channel->state = CH_STATE_STOPPED;
1284                wake_up(&card->wait_q);
1285        }
1286
1287        if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
1288                channel->state = CH_STATE_HALTED;
1289                wake_up(&card->wait_q);
1290        }
1291
1292        if (iob && (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC |
1293                                          SCSW_FCTL_HALT_FUNC))) {
1294                qeth_cancel_cmd(iob, -ECANCELED);
1295                iob = NULL;
1296        }
1297
1298        cstat = irb->scsw.cmd.cstat;
1299        dstat = irb->scsw.cmd.dstat;
1300
1301        if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1302            (dstat & DEV_STAT_UNIT_CHECK) ||
1303            (cstat)) {
1304                if (irb->esw.esw0.erw.cons) {
1305                        dev_warn(&channel->ccwdev->dev,
1306                                "The qeth device driver failed to recover "
1307                                "an error on the device\n");
1308                        QETH_DBF_MESSAGE(2, "sense data available on channel %x: cstat %#X dstat %#X\n",
1309                                         CCW_DEVID(channel->ccwdev), cstat,
1310                                         dstat);
1311                        print_hex_dump(KERN_WARNING, "qeth: irb ",
1312                                DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1313                        print_hex_dump(KERN_WARNING, "qeth: sense data ",
1314                                DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1315                }
1316
1317                rc = qeth_get_problem(card, cdev, irb);
1318                if (rc) {
1319                        card->read_or_write_problem = 1;
1320                        if (iob)
1321                                qeth_cancel_cmd(iob, rc);
1322                        qeth_clear_ipacmd_list(card);
1323                        qeth_schedule_recovery(card);
1324                        return;
1325                }
1326        }
1327
1328        if (iob) {
1329                /* sanity check: */
1330                if (irb->scsw.cmd.count > iob->length) {
1331                        qeth_cancel_cmd(iob, -EIO);
1332                        return;
1333                }
1334                if (iob->callback)
1335                        iob->callback(card, iob,
1336                                      iob->length - irb->scsw.cmd.count);
1337        }
1338}
1339
1340static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1341                struct qeth_qdio_out_buffer *buf,
1342                enum iucv_tx_notify notification)
1343{
1344        struct sk_buff *skb;
1345
1346        skb_queue_walk(&buf->skb_list, skb) {
1347                struct sock *sk = skb->sk;
1348
1349                QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1350                QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1351                if (sk && sk->sk_family == PF_IUCV)
1352                        iucv_sk(sk)->sk_txnotify(sk, notification);
1353        }
1354}
1355
1356static void qeth_tx_complete_buf(struct qeth_qdio_out_buffer *buf, bool error,
1357                                 int budget)
1358{
1359        struct qeth_qdio_out_q *queue = buf->q;
1360        struct sk_buff *skb;
1361
1362        /* Empty buffer? */
1363        if (buf->next_element_to_fill == 0)
1364                return;
1365
1366        QETH_TXQ_STAT_INC(queue, bufs);
1367        QETH_TXQ_STAT_ADD(queue, buf_elements, buf->next_element_to_fill);
1368        if (error) {
1369                QETH_TXQ_STAT_ADD(queue, tx_errors, buf->frames);
1370        } else {
1371                QETH_TXQ_STAT_ADD(queue, tx_packets, buf->frames);
1372                QETH_TXQ_STAT_ADD(queue, tx_bytes, buf->bytes);
1373        }
1374
1375        while ((skb = __skb_dequeue(&buf->skb_list)) != NULL) {
1376                unsigned int bytes = qdisc_pkt_len(skb);
1377                bool is_tso = skb_is_gso(skb);
1378                unsigned int packets;
1379
1380                packets = is_tso ? skb_shinfo(skb)->gso_segs : 1;
1381                if (!error) {
1382                        if (skb->ip_summed == CHECKSUM_PARTIAL)
1383                                QETH_TXQ_STAT_ADD(queue, skbs_csum, packets);
1384                        if (skb_is_nonlinear(skb))
1385                                QETH_TXQ_STAT_INC(queue, skbs_sg);
1386                        if (is_tso) {
1387                                QETH_TXQ_STAT_INC(queue, skbs_tso);
1388                                QETH_TXQ_STAT_ADD(queue, tso_bytes, bytes);
1389                        }
1390                }
1391
1392                napi_consume_skb(skb, budget);
1393        }
1394}
1395
1396static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1397                                     struct qeth_qdio_out_buffer *buf,
1398                                     bool error, int budget)
1399{
1400        int i;
1401
1402        /* is PCI flag set on buffer? */
1403        if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1404                atomic_dec(&queue->set_pci_flags_count);
1405
1406        qeth_tx_complete_buf(buf, error, budget);
1407
1408        for (i = 0; i < queue->max_elements; ++i) {
1409                void *data = phys_to_virt(buf->buffer->element[i].addr);
1410
1411                if (data && buf->is_header[i])
1412                        kmem_cache_free(qeth_core_header_cache, data);
1413                buf->is_header[i] = 0;
1414        }
1415
1416        qeth_scrub_qdio_buffer(buf->buffer, queue->max_elements);
1417        buf->next_element_to_fill = 0;
1418        buf->frames = 0;
1419        buf->bytes = 0;
1420        atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
1421}
1422
1423static void qeth_free_out_buf(struct qeth_qdio_out_buffer *buf)
1424{
1425        if (buf->aob)
1426                qdio_release_aob(buf->aob);
1427        kmem_cache_free(qeth_qdio_outbuf_cache, buf);
1428}
1429
1430static void qeth_tx_complete_pending_bufs(struct qeth_card *card,
1431                                          struct qeth_qdio_out_q *queue,
1432                                          bool drain, int budget)
1433{
1434        struct qeth_qdio_out_buffer *buf, *tmp;
1435
1436        list_for_each_entry_safe(buf, tmp, &queue->pending_bufs, list_entry) {
1437                if (drain || atomic_read(&buf->state) == QETH_QDIO_BUF_EMPTY) {
1438                        QETH_CARD_TEXT(card, 5, "fp");
1439                        QETH_CARD_TEXT_(card, 5, "%lx", (long) buf);
1440
1441                        if (drain)
1442                                qeth_notify_skbs(queue, buf,
1443                                                 TX_NOTIFY_GENERALERROR);
1444                        qeth_tx_complete_buf(buf, drain, budget);
1445
1446                        list_del(&buf->list_entry);
1447                        qeth_free_out_buf(buf);
1448                }
1449        }
1450}
1451
1452static void qeth_drain_output_queue(struct qeth_qdio_out_q *q, bool free)
1453{
1454        int j;
1455
1456        qeth_tx_complete_pending_bufs(q->card, q, true, 0);
1457
1458        for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1459                if (!q->bufs[j])
1460                        continue;
1461
1462                qeth_clear_output_buffer(q, q->bufs[j], true, 0);
1463                if (free) {
1464                        qeth_free_out_buf(q->bufs[j]);
1465                        q->bufs[j] = NULL;
1466                }
1467        }
1468}
1469
1470static void qeth_drain_output_queues(struct qeth_card *card)
1471{
1472        int i;
1473
1474        QETH_CARD_TEXT(card, 2, "clearqdbf");
1475        /* clear outbound buffers to free skbs */
1476        for (i = 0; i < card->qdio.no_out_queues; ++i) {
1477                if (card->qdio.out_qs[i])
1478                        qeth_drain_output_queue(card->qdio.out_qs[i], false);
1479        }
1480}
1481
1482static void qeth_osa_set_output_queues(struct qeth_card *card, bool single)
1483{
1484        unsigned int max = single ? 1 : card->dev->num_tx_queues;
1485
1486        if (card->qdio.no_out_queues == max)
1487                return;
1488
1489        if (atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED)
1490                qeth_free_qdio_queues(card);
1491
1492        if (max == 1 && card->qdio.do_prio_queueing != QETH_PRIOQ_DEFAULT)
1493                dev_info(&card->gdev->dev, "Priority Queueing not supported\n");
1494
1495        card->qdio.no_out_queues = max;
1496}
1497
1498static int qeth_update_from_chp_desc(struct qeth_card *card)
1499{
1500        struct ccw_device *ccwdev;
1501        struct channel_path_desc_fmt0 *chp_dsc;
1502
1503        QETH_CARD_TEXT(card, 2, "chp_desc");
1504
1505        ccwdev = card->data.ccwdev;
1506        chp_dsc = ccw_device_get_chp_desc(ccwdev, 0);
1507        if (!chp_dsc)
1508                return -ENOMEM;
1509
1510        card->info.func_level = 0x4100 + chp_dsc->desc;
1511
1512        if (IS_OSD(card) || IS_OSX(card))
1513                /* CHPP field bit 6 == 1 -> single queue */
1514                qeth_osa_set_output_queues(card, chp_dsc->chpp & 0x02);
1515
1516        kfree(chp_dsc);
1517        QETH_CARD_TEXT_(card, 2, "nr:%x", card->qdio.no_out_queues);
1518        QETH_CARD_TEXT_(card, 2, "lvl:%02x", card->info.func_level);
1519        return 0;
1520}
1521
1522static void qeth_init_qdio_info(struct qeth_card *card)
1523{
1524        QETH_CARD_TEXT(card, 4, "intqdinf");
1525        atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1526        card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1527        card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1528
1529        /* inbound */
1530        card->qdio.no_in_queues = 1;
1531        card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1532        if (IS_IQD(card))
1533                card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1534        else
1535                card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1536        card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1537        INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1538        INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1539}
1540
1541static void qeth_set_initial_options(struct qeth_card *card)
1542{
1543        card->options.route4.type = NO_ROUTER;
1544        card->options.route6.type = NO_ROUTER;
1545        card->options.isolation = ISOLATION_MODE_NONE;
1546        card->options.cq = QETH_CQ_DISABLED;
1547        card->options.layer = QETH_DISCIPLINE_UNDETERMINED;
1548}
1549
1550static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1551{
1552        unsigned long flags;
1553        int rc = 0;
1554
1555        spin_lock_irqsave(&card->thread_mask_lock, flags);
1556        QETH_CARD_TEXT_(card, 4, "  %02x%02x%02x",
1557                        (u8) card->thread_start_mask,
1558                        (u8) card->thread_allowed_mask,
1559                        (u8) card->thread_running_mask);
1560        rc = (card->thread_start_mask & thread);
1561        spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1562        return rc;
1563}
1564
1565static int qeth_do_reset(void *data);
1566static void qeth_start_kernel_thread(struct work_struct *work)
1567{
1568        struct task_struct *ts;
1569        struct qeth_card *card = container_of(work, struct qeth_card,
1570                                        kernel_thread_starter);
1571        QETH_CARD_TEXT(card, 2, "strthrd");
1572
1573        if (card->read.state != CH_STATE_UP &&
1574            card->write.state != CH_STATE_UP)
1575                return;
1576        if (qeth_do_start_thread(card, QETH_RECOVER_THREAD)) {
1577                ts = kthread_run(qeth_do_reset, card, "qeth_recover");
1578                if (IS_ERR(ts)) {
1579                        qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1580                        qeth_clear_thread_running_bit(card,
1581                                QETH_RECOVER_THREAD);
1582                }
1583        }
1584}
1585
1586static void qeth_buffer_reclaim_work(struct work_struct *);
1587static void qeth_setup_card(struct qeth_card *card)
1588{
1589        QETH_CARD_TEXT(card, 2, "setupcrd");
1590
1591        card->info.type = CARD_RDEV(card)->id.driver_info;
1592        card->state = CARD_STATE_DOWN;
1593        spin_lock_init(&card->lock);
1594        spin_lock_init(&card->thread_mask_lock);
1595        mutex_init(&card->conf_mutex);
1596        mutex_init(&card->discipline_mutex);
1597        INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1598        INIT_LIST_HEAD(&card->cmd_waiter_list);
1599        init_waitqueue_head(&card->wait_q);
1600        qeth_set_initial_options(card);
1601        /* IP address takeover */
1602        INIT_LIST_HEAD(&card->ipato.entries);
1603        qeth_init_qdio_info(card);
1604        INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1605        INIT_WORK(&card->close_dev_work, qeth_close_dev_handler);
1606        hash_init(card->rx_mode_addrs);
1607        hash_init(card->local_addrs4);
1608        hash_init(card->local_addrs6);
1609        spin_lock_init(&card->local_addrs4_lock);
1610        spin_lock_init(&card->local_addrs6_lock);
1611}
1612
1613static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1614{
1615        struct qeth_card *card = container_of(slr, struct qeth_card,
1616                                        qeth_service_level);
1617        if (card->info.mcl_level[0])
1618                seq_printf(m, "qeth: %s firmware level %s\n",
1619                        CARD_BUS_ID(card), card->info.mcl_level);
1620}
1621
1622static struct qeth_card *qeth_alloc_card(struct ccwgroup_device *gdev)
1623{
1624        struct qeth_card *card;
1625
1626        QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1627        card = kzalloc(sizeof(*card), GFP_KERNEL);
1628        if (!card)
1629                goto out;
1630        QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1631
1632        card->gdev = gdev;
1633        dev_set_drvdata(&gdev->dev, card);
1634        CARD_RDEV(card) = gdev->cdev[0];
1635        CARD_WDEV(card) = gdev->cdev[1];
1636        CARD_DDEV(card) = gdev->cdev[2];
1637
1638        card->event_wq = alloc_ordered_workqueue("%s_event", 0,
1639                                                 dev_name(&gdev->dev));
1640        if (!card->event_wq)
1641                goto out_wq;
1642
1643        card->read_cmd = qeth_alloc_cmd(&card->read, QETH_BUFSIZE, 1, 0);
1644        if (!card->read_cmd)
1645                goto out_read_cmd;
1646
1647        card->debugfs = debugfs_create_dir(dev_name(&gdev->dev),
1648                                           qeth_debugfs_root);
1649        debugfs_create_file("local_addrs", 0400, card->debugfs, card,
1650                            &qeth_debugfs_local_addr_fops);
1651
1652        card->qeth_service_level.seq_print = qeth_core_sl_print;
1653        register_service_level(&card->qeth_service_level);
1654        return card;
1655
1656out_read_cmd:
1657        destroy_workqueue(card->event_wq);
1658out_wq:
1659        dev_set_drvdata(&gdev->dev, NULL);
1660        kfree(card);
1661out:
1662        return NULL;
1663}
1664
1665static int qeth_clear_channel(struct qeth_card *card,
1666                              struct qeth_channel *channel)
1667{
1668        int rc;
1669
1670        QETH_CARD_TEXT(card, 3, "clearch");
1671        spin_lock_irq(get_ccwdev_lock(channel->ccwdev));
1672        rc = ccw_device_clear(channel->ccwdev, (addr_t)channel->active_cmd);
1673        spin_unlock_irq(get_ccwdev_lock(channel->ccwdev));
1674
1675        if (rc)
1676                return rc;
1677        rc = wait_event_interruptible_timeout(card->wait_q,
1678                        channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1679        if (rc == -ERESTARTSYS)
1680                return rc;
1681        if (channel->state != CH_STATE_STOPPED)
1682                return -ETIME;
1683        channel->state = CH_STATE_DOWN;
1684        return 0;
1685}
1686
1687static int qeth_halt_channel(struct qeth_card *card,
1688                             struct qeth_channel *channel)
1689{
1690        int rc;
1691
1692        QETH_CARD_TEXT(card, 3, "haltch");
1693        spin_lock_irq(get_ccwdev_lock(channel->ccwdev));
1694        rc = ccw_device_halt(channel->ccwdev, (addr_t)channel->active_cmd);
1695        spin_unlock_irq(get_ccwdev_lock(channel->ccwdev));
1696
1697        if (rc)
1698                return rc;
1699        rc = wait_event_interruptible_timeout(card->wait_q,
1700                        channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1701        if (rc == -ERESTARTSYS)
1702                return rc;
1703        if (channel->state != CH_STATE_HALTED)
1704                return -ETIME;
1705        return 0;
1706}
1707
1708static int qeth_stop_channel(struct qeth_channel *channel)
1709{
1710        struct ccw_device *cdev = channel->ccwdev;
1711        int rc;
1712
1713        rc = ccw_device_set_offline(cdev);
1714
1715        spin_lock_irq(get_ccwdev_lock(cdev));
1716        if (channel->active_cmd) {
1717                dev_err(&cdev->dev, "Stopped channel while cmd %px was still active\n",
1718                        channel->active_cmd);
1719                channel->active_cmd = NULL;
1720        }
1721        cdev->handler = NULL;
1722        spin_unlock_irq(get_ccwdev_lock(cdev));
1723
1724        return rc;
1725}
1726
1727static int qeth_start_channel(struct qeth_channel *channel)
1728{
1729        struct ccw_device *cdev = channel->ccwdev;
1730        int rc;
1731
1732        channel->state = CH_STATE_DOWN;
1733        atomic_set(&channel->irq_pending, 0);
1734
1735        spin_lock_irq(get_ccwdev_lock(cdev));
1736        cdev->handler = qeth_irq;
1737        spin_unlock_irq(get_ccwdev_lock(cdev));
1738
1739        rc = ccw_device_set_online(cdev);
1740        if (rc)
1741                goto err;
1742
1743        return 0;
1744
1745err:
1746        spin_lock_irq(get_ccwdev_lock(cdev));
1747        cdev->handler = NULL;
1748        spin_unlock_irq(get_ccwdev_lock(cdev));
1749        return rc;
1750}
1751
1752static int qeth_halt_channels(struct qeth_card *card)
1753{
1754        int rc1 = 0, rc2 = 0, rc3 = 0;
1755
1756        QETH_CARD_TEXT(card, 3, "haltchs");
1757        rc1 = qeth_halt_channel(card, &card->read);
1758        rc2 = qeth_halt_channel(card, &card->write);
1759        rc3 = qeth_halt_channel(card, &card->data);
1760        if (rc1)
1761                return rc1;
1762        if (rc2)
1763                return rc2;
1764        return rc3;
1765}
1766
1767static int qeth_clear_channels(struct qeth_card *card)
1768{
1769        int rc1 = 0, rc2 = 0, rc3 = 0;
1770
1771        QETH_CARD_TEXT(card, 3, "clearchs");
1772        rc1 = qeth_clear_channel(card, &card->read);
1773        rc2 = qeth_clear_channel(card, &card->write);
1774        rc3 = qeth_clear_channel(card, &card->data);
1775        if (rc1)
1776                return rc1;
1777        if (rc2)
1778                return rc2;
1779        return rc3;
1780}
1781
1782static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1783{
1784        int rc = 0;
1785
1786        QETH_CARD_TEXT(card, 3, "clhacrd");
1787
1788        if (halt)
1789                rc = qeth_halt_channels(card);
1790        if (rc)
1791                return rc;
1792        return qeth_clear_channels(card);
1793}
1794
1795static int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1796{
1797        int rc = 0;
1798
1799        QETH_CARD_TEXT(card, 3, "qdioclr");
1800        switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1801                QETH_QDIO_CLEANING)) {
1802        case QETH_QDIO_ESTABLISHED:
1803                if (IS_IQD(card))
1804                        rc = qdio_shutdown(CARD_DDEV(card),
1805                                QDIO_FLAG_CLEANUP_USING_HALT);
1806                else
1807                        rc = qdio_shutdown(CARD_DDEV(card),
1808                                QDIO_FLAG_CLEANUP_USING_CLEAR);
1809                if (rc)
1810                        QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1811                atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1812                break;
1813        case QETH_QDIO_CLEANING:
1814                return rc;
1815        default:
1816                break;
1817        }
1818        rc = qeth_clear_halt_card(card, use_halt);
1819        if (rc)
1820                QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1821        return rc;
1822}
1823
1824static enum qeth_discipline_id qeth_vm_detect_layer(struct qeth_card *card)
1825{
1826        enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1827        struct diag26c_vnic_resp *response = NULL;
1828        struct diag26c_vnic_req *request = NULL;
1829        struct ccw_dev_id id;
1830        char userid[80];
1831        int rc = 0;
1832
1833        QETH_CARD_TEXT(card, 2, "vmlayer");
1834
1835        cpcmd("QUERY USERID", userid, sizeof(userid), &rc);
1836        if (rc)
1837                goto out;
1838
1839        request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
1840        response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
1841        if (!request || !response) {
1842                rc = -ENOMEM;
1843                goto out;
1844        }
1845
1846        ccw_device_get_id(CARD_RDEV(card), &id);
1847        request->resp_buf_len = sizeof(*response);
1848        request->resp_version = DIAG26C_VERSION6_VM65918;
1849        request->req_format = DIAG26C_VNIC_INFO;
1850        ASCEBC(userid, 8);
1851        memcpy(&request->sys_name, userid, 8);
1852        request->devno = id.devno;
1853
1854        QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1855        rc = diag26c(request, response, DIAG26C_PORT_VNIC);
1856        QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1857        if (rc)
1858                goto out;
1859        QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
1860
1861        if (request->resp_buf_len < sizeof(*response) ||
1862            response->version != request->resp_version) {
1863                rc = -EIO;
1864                goto out;
1865        }
1866
1867        if (response->protocol == VNIC_INFO_PROT_L2)
1868                disc = QETH_DISCIPLINE_LAYER2;
1869        else if (response->protocol == VNIC_INFO_PROT_L3)
1870                disc = QETH_DISCIPLINE_LAYER3;
1871
1872out:
1873        kfree(response);
1874        kfree(request);
1875        if (rc)
1876                QETH_CARD_TEXT_(card, 2, "err%x", rc);
1877        return disc;
1878}
1879
1880/* Determine whether the device requires a specific layer discipline */
1881static enum qeth_discipline_id qeth_enforce_discipline(struct qeth_card *card)
1882{
1883        enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1884
1885        if (IS_OSM(card) || IS_OSN(card))
1886                disc = QETH_DISCIPLINE_LAYER2;
1887        else if (IS_VM_NIC(card))
1888                disc = IS_IQD(card) ? QETH_DISCIPLINE_LAYER3 :
1889                                      qeth_vm_detect_layer(card);
1890
1891        switch (disc) {
1892        case QETH_DISCIPLINE_LAYER2:
1893                QETH_CARD_TEXT(card, 3, "force l2");
1894                break;
1895        case QETH_DISCIPLINE_LAYER3:
1896                QETH_CARD_TEXT(card, 3, "force l3");
1897                break;
1898        default:
1899                QETH_CARD_TEXT(card, 3, "force no");
1900        }
1901
1902        return disc;
1903}
1904
1905static void qeth_set_blkt_defaults(struct qeth_card *card)
1906{
1907        QETH_CARD_TEXT(card, 2, "cfgblkt");
1908
1909        if (card->info.use_v1_blkt) {
1910                card->info.blkt.time_total = 0;
1911                card->info.blkt.inter_packet = 0;
1912                card->info.blkt.inter_packet_jumbo = 0;
1913        } else {
1914                card->info.blkt.time_total = 250;
1915                card->info.blkt.inter_packet = 5;
1916                card->info.blkt.inter_packet_jumbo = 15;
1917        }
1918}
1919
1920static void qeth_idx_init(struct qeth_card *card)
1921{
1922        memset(&card->seqno, 0, sizeof(card->seqno));
1923
1924        card->token.issuer_rm_w = 0x00010103UL;
1925        card->token.cm_filter_w = 0x00010108UL;
1926        card->token.cm_connection_w = 0x0001010aUL;
1927        card->token.ulp_filter_w = 0x0001010bUL;
1928        card->token.ulp_connection_w = 0x0001010dUL;
1929
1930        switch (card->info.type) {
1931        case QETH_CARD_TYPE_IQD:
1932                card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1933                break;
1934        case QETH_CARD_TYPE_OSD:
1935        case QETH_CARD_TYPE_OSN:
1936                card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1937                break;
1938        default:
1939                break;
1940        }
1941}
1942
1943static void qeth_idx_finalize_cmd(struct qeth_card *card,
1944                                  struct qeth_cmd_buffer *iob)
1945{
1946        memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data), &card->seqno.trans_hdr,
1947               QETH_SEQ_NO_LENGTH);
1948        if (iob->channel == &card->write)
1949                card->seqno.trans_hdr++;
1950}
1951
1952static int qeth_peer_func_level(int level)
1953{
1954        if ((level & 0xff) == 8)
1955                return (level & 0xff) + 0x400;
1956        if (((level >> 8) & 3) == 1)
1957                return (level & 0xff) + 0x200;
1958        return level;
1959}
1960
1961static void qeth_mpc_finalize_cmd(struct qeth_card *card,
1962                                  struct qeth_cmd_buffer *iob)
1963{
1964        qeth_idx_finalize_cmd(card, iob);
1965
1966        memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1967               &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1968        card->seqno.pdu_hdr++;
1969        memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1970               &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1971
1972        iob->callback = qeth_release_buffer_cb;
1973}
1974
1975static bool qeth_mpc_match_reply(struct qeth_cmd_buffer *iob,
1976                                 struct qeth_cmd_buffer *reply)
1977{
1978        /* MPC cmds are issued strictly in sequence. */
1979        return !IS_IPA(reply->data);
1980}
1981
1982static struct qeth_cmd_buffer *qeth_mpc_alloc_cmd(struct qeth_card *card,
1983                                                  const void *data,
1984                                                  unsigned int data_length)
1985{
1986        struct qeth_cmd_buffer *iob;
1987
1988        iob = qeth_alloc_cmd(&card->write, data_length, 1, QETH_TIMEOUT);
1989        if (!iob)
1990                return NULL;
1991
1992        memcpy(iob->data, data, data_length);
1993        qeth_setup_ccw(__ccw_from_cmd(iob), CCW_CMD_WRITE, 0, data_length,
1994                       iob->data);
1995        iob->finalize = qeth_mpc_finalize_cmd;
1996        iob->match = qeth_mpc_match_reply;
1997        return iob;
1998}
1999
2000/**
2001 * qeth_send_control_data() -   send control command to the card
2002 * @card:                       qeth_card structure pointer
2003 * @iob:                        qeth_cmd_buffer pointer
2004 * @reply_cb:                   callback function pointer
2005 * @cb_card:                    pointer to the qeth_card structure
2006 * @cb_reply:                   pointer to the qeth_reply structure
2007 * @cb_cmd:                     pointer to the original iob for non-IPA
2008 *                              commands, or to the qeth_ipa_cmd structure
2009 *                              for the IPA commands.
2010 * @reply_param:                private pointer passed to the callback
2011 *
2012 * Callback function gets called one or more times, with cb_cmd
2013 * pointing to the response returned by the hardware. Callback
2014 * function must return
2015 *   > 0 if more reply blocks are expected,
2016 *     0 if the last or only reply block is received, and
2017 *   < 0 on error.
2018 * Callback function can get the value of the reply_param pointer from the
2019 * field 'param' of the structure qeth_reply.
2020 */
2021
2022static int qeth_send_control_data(struct qeth_card *card,
2023                                  struct qeth_cmd_buffer *iob,
2024                                  int (*reply_cb)(struct qeth_card *cb_card,
2025                                                  struct qeth_reply *cb_reply,
2026                                                  unsigned long cb_cmd),
2027                                  void *reply_param)
2028{
2029        struct qeth_channel *channel = iob->channel;
2030        struct qeth_reply *reply = &iob->reply;
2031        long timeout = iob->timeout;
2032        int rc;
2033
2034        QETH_CARD_TEXT(card, 2, "sendctl");
2035
2036        reply->callback = reply_cb;
2037        reply->param = reply_param;
2038
2039        timeout = wait_event_interruptible_timeout(card->wait_q,
2040                                                   qeth_trylock_channel(channel),
2041                                                   timeout);
2042        if (timeout <= 0) {
2043                qeth_put_cmd(iob);
2044                return (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
2045        }
2046
2047        if (iob->finalize)
2048                iob->finalize(card, iob);
2049        QETH_DBF_HEX(CTRL, 2, iob->data, min(iob->length, QETH_DBF_CTRL_LEN));
2050
2051        qeth_enqueue_cmd(card, iob);
2052
2053        /* This pairs with iob->callback, and keeps the iob alive after IO: */
2054        qeth_get_cmd(iob);
2055
2056        QETH_CARD_TEXT(card, 6, "noirqpnd");
2057        spin_lock_irq(get_ccwdev_lock(channel->ccwdev));
2058        rc = ccw_device_start_timeout(channel->ccwdev, __ccw_from_cmd(iob),
2059                                      (addr_t) iob, 0, 0, timeout);
2060        if (!rc)
2061                channel->active_cmd = iob;
2062        spin_unlock_irq(get_ccwdev_lock(channel->ccwdev));
2063        if (rc) {
2064                QETH_DBF_MESSAGE(2, "qeth_send_control_data on device %x: ccw_device_start rc = %i\n",
2065                                 CARD_DEVID(card), rc);
2066                QETH_CARD_TEXT_(card, 2, " err%d", rc);
2067                qeth_dequeue_cmd(card, iob);
2068                qeth_put_cmd(iob);
2069                qeth_unlock_channel(card, channel);
2070                goto out;
2071        }
2072
2073        timeout = wait_for_completion_interruptible_timeout(&iob->done,
2074                                                            timeout);
2075        if (timeout <= 0)
2076                rc = (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
2077
2078        qeth_dequeue_cmd(card, iob);
2079
2080        if (reply_cb) {
2081                /* Wait until the callback for a late reply has completed: */
2082                spin_lock_irq(&iob->lock);
2083                if (rc)
2084                        /* Zap any callback that's still pending: */
2085                        iob->rc = rc;
2086                spin_unlock_irq(&iob->lock);
2087        }
2088
2089        if (!rc)
2090                rc = iob->rc;
2091
2092out:
2093        qeth_put_cmd(iob);
2094        return rc;
2095}
2096
2097struct qeth_node_desc {
2098        struct node_descriptor nd1;
2099        struct node_descriptor nd2;
2100        struct node_descriptor nd3;
2101};
2102
2103static void qeth_read_conf_data_cb(struct qeth_card *card,
2104                                   struct qeth_cmd_buffer *iob,
2105                                   unsigned int data_length)
2106{
2107        struct qeth_node_desc *nd = (struct qeth_node_desc *) iob->data;
2108        int rc = 0;
2109        u8 *tag;
2110
2111        QETH_CARD_TEXT(card, 2, "cfgunit");
2112
2113        if (data_length < sizeof(*nd)) {
2114                rc = -EINVAL;
2115                goto out;
2116        }
2117
2118        card->info.is_vm_nic = nd->nd1.plant[0] == _ascebc['V'] &&
2119                               nd->nd1.plant[1] == _ascebc['M'];
2120        tag = (u8 *)&nd->nd1.tag;
2121        card->info.chpid = tag[0];
2122        card->info.unit_addr2 = tag[1];
2123
2124        tag = (u8 *)&nd->nd2.tag;
2125        card->info.cula = tag[1];
2126
2127        card->info.use_v1_blkt = nd->nd3.model[0] == 0xF0 &&
2128                                 nd->nd3.model[1] == 0xF0 &&
2129                                 nd->nd3.model[2] >= 0xF1 &&
2130                                 nd->nd3.model[2] <= 0xF4;
2131
2132out:
2133        qeth_notify_cmd(iob, rc);
2134        qeth_put_cmd(iob);
2135}
2136
2137static int qeth_read_conf_data(struct qeth_card *card)
2138{
2139        struct qeth_channel *channel = &card->data;
2140        struct qeth_cmd_buffer *iob;
2141        struct ciw *ciw;
2142
2143        /* scan for RCD command in extended SenseID data */
2144        ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
2145        if (!ciw || ciw->cmd == 0)
2146                return -EOPNOTSUPP;
2147        if (ciw->count < sizeof(struct qeth_node_desc))
2148                return -EINVAL;
2149
2150        iob = qeth_alloc_cmd(channel, ciw->count, 1, QETH_RCD_TIMEOUT);
2151        if (!iob)
2152                return -ENOMEM;
2153
2154        iob->callback = qeth_read_conf_data_cb;
2155        qeth_setup_ccw(__ccw_from_cmd(iob), ciw->cmd, 0, iob->length,
2156                       iob->data);
2157
2158        return qeth_send_control_data(card, iob, NULL, NULL);
2159}
2160
2161static int qeth_idx_check_activate_response(struct qeth_card *card,
2162                                            struct qeth_channel *channel,
2163                                            struct qeth_cmd_buffer *iob)
2164{
2165        int rc;
2166
2167        rc = qeth_check_idx_response(card, iob->data);
2168        if (rc)
2169                return rc;
2170
2171        if (QETH_IS_IDX_ACT_POS_REPLY(iob->data))
2172                return 0;
2173
2174        /* negative reply: */
2175        QETH_CARD_TEXT_(card, 2, "idxneg%c",
2176                        QETH_IDX_ACT_CAUSE_CODE(iob->data));
2177
2178        switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
2179        case QETH_IDX_ACT_ERR_EXCL:
2180                dev_err(&channel->ccwdev->dev,
2181                        "The adapter is used exclusively by another host\n");
2182                return -EBUSY;
2183        case QETH_IDX_ACT_ERR_AUTH:
2184        case QETH_IDX_ACT_ERR_AUTH_USER:
2185                dev_err(&channel->ccwdev->dev,
2186                        "Setting the device online failed because of insufficient authorization\n");
2187                return -EPERM;
2188        default:
2189                QETH_DBF_MESSAGE(2, "IDX_ACTIVATE on channel %x: negative reply\n",
2190                                 CCW_DEVID(channel->ccwdev));
2191                return -EIO;
2192        }
2193}
2194
2195static void qeth_idx_activate_read_channel_cb(struct qeth_card *card,
2196                                              struct qeth_cmd_buffer *iob,
2197                                              unsigned int data_length)
2198{
2199        struct qeth_channel *channel = iob->channel;
2200        u16 peer_level;
2201        int rc;
2202
2203        QETH_CARD_TEXT(card, 2, "idxrdcb");
2204
2205        rc = qeth_idx_check_activate_response(card, channel, iob);
2206        if (rc)
2207                goto out;
2208
2209        memcpy(&peer_level, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2210        if (peer_level != qeth_peer_func_level(card->info.func_level)) {
2211                QETH_DBF_MESSAGE(2, "IDX_ACTIVATE on channel %x: function level mismatch (sent: %#x, received: %#x)\n",
2212                                 CCW_DEVID(channel->ccwdev),
2213                                 card->info.func_level, peer_level);
2214                rc = -EINVAL;
2215                goto out;
2216        }
2217
2218        memcpy(&card->token.issuer_rm_r,
2219               QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
2220               QETH_MPC_TOKEN_LENGTH);
2221        memcpy(&card->info.mcl_level[0],
2222               QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
2223
2224out:
2225        qeth_notify_cmd(iob, rc);
2226        qeth_put_cmd(iob);
2227}
2228
2229static void qeth_idx_activate_write_channel_cb(struct qeth_card *card,
2230                                               struct qeth_cmd_buffer *iob,
2231                                               unsigned int data_length)
2232{
2233        struct qeth_channel *channel = iob->channel;
2234        u16 peer_level;
2235        int rc;
2236
2237        QETH_CARD_TEXT(card, 2, "idxwrcb");
2238
2239        rc = qeth_idx_check_activate_response(card, channel, iob);
2240        if (rc)
2241                goto out;
2242
2243        memcpy(&peer_level, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2244        if ((peer_level & ~0x0100) !=
2245            qeth_peer_func_level(card->info.func_level)) {
2246                QETH_DBF_MESSAGE(2, "IDX_ACTIVATE on channel %x: function level mismatch (sent: %#x, received: %#x)\n",
2247                                 CCW_DEVID(channel->ccwdev),
2248                                 card->info.func_level, peer_level);
2249                rc = -EINVAL;
2250        }
2251
2252out:
2253        qeth_notify_cmd(iob, rc);
2254        qeth_put_cmd(iob);
2255}
2256
2257static void qeth_idx_setup_activate_cmd(struct qeth_card *card,
2258                                        struct qeth_cmd_buffer *iob)
2259{
2260        u16 addr = (card->info.cula << 8) + card->info.unit_addr2;
2261        u8 port = ((u8)card->dev->dev_port) | 0x80;
2262        struct ccw1 *ccw = __ccw_from_cmd(iob);
2263
2264        qeth_setup_ccw(&ccw[0], CCW_CMD_WRITE, CCW_FLAG_CC, IDX_ACTIVATE_SIZE,
2265                       iob->data);
2266        qeth_setup_ccw(&ccw[1], CCW_CMD_READ, 0, iob->length, iob->data);
2267        iob->finalize = qeth_idx_finalize_cmd;
2268
2269        port |= QETH_IDX_ACT_INVAL_FRAME;
2270        memcpy(QETH_IDX_ACT_PNO(iob->data), &port, 1);
2271        memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
2272               &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
2273        memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
2274               &card->info.func_level, 2);
2275        memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &card->info.ddev_devno, 2);
2276        memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &addr, 2);
2277}
2278
2279static int qeth_idx_activate_read_channel(struct qeth_card *card)
2280{
2281        struct qeth_channel *channel = &card->read;
2282        struct qeth_cmd_buffer *iob;
2283        int rc;
2284
2285        QETH_CARD_TEXT(card, 2, "idxread");
2286
2287        iob = qeth_alloc_cmd(channel, QETH_BUFSIZE, 2, QETH_TIMEOUT);
2288        if (!iob)
2289                return -ENOMEM;
2290
2291        memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
2292        qeth_idx_setup_activate_cmd(card, iob);
2293        iob->callback = qeth_idx_activate_read_channel_cb;
2294
2295        rc = qeth_send_control_data(card, iob, NULL, NULL);
2296        if (rc)
2297                return rc;
2298
2299        channel->state = CH_STATE_UP;
2300        return 0;
2301}
2302
2303static int qeth_idx_activate_write_channel(struct qeth_card *card)
2304{
2305        struct qeth_channel *channel = &card->write;
2306        struct qeth_cmd_buffer *iob;
2307        int rc;
2308
2309        QETH_CARD_TEXT(card, 2, "idxwrite");
2310
2311        iob = qeth_alloc_cmd(channel, QETH_BUFSIZE, 2, QETH_TIMEOUT);
2312        if (!iob)
2313                return -ENOMEM;
2314
2315        memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
2316        qeth_idx_setup_activate_cmd(card, iob);
2317        iob->callback = qeth_idx_activate_write_channel_cb;
2318
2319        rc = qeth_send_control_data(card, iob, NULL, NULL);
2320        if (rc)
2321                return rc;
2322
2323        channel->state = CH_STATE_UP;
2324        return 0;
2325}
2326
2327static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2328                unsigned long data)
2329{
2330        struct qeth_cmd_buffer *iob;
2331
2332        QETH_CARD_TEXT(card, 2, "cmenblcb");
2333
2334        iob = (struct qeth_cmd_buffer *) data;
2335        memcpy(&card->token.cm_filter_r,
2336               QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2337               QETH_MPC_TOKEN_LENGTH);
2338        return 0;
2339}
2340
2341static int qeth_cm_enable(struct qeth_card *card)
2342{
2343        struct qeth_cmd_buffer *iob;
2344
2345        QETH_CARD_TEXT(card, 2, "cmenable");
2346
2347        iob = qeth_mpc_alloc_cmd(card, CM_ENABLE, CM_ENABLE_SIZE);
2348        if (!iob)
2349                return -ENOMEM;
2350
2351        memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2352               &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2353        memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2354               &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2355
2356        return qeth_send_control_data(card, iob, qeth_cm_enable_cb, NULL);
2357}
2358
2359static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2360                unsigned long data)
2361{
2362        struct qeth_cmd_buffer *iob;
2363
2364        QETH_CARD_TEXT(card, 2, "cmsetpcb");
2365
2366        iob = (struct qeth_cmd_buffer *) data;
2367        memcpy(&card->token.cm_connection_r,
2368               QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2369               QETH_MPC_TOKEN_LENGTH);
2370        return 0;
2371}
2372
2373static int qeth_cm_setup(struct qeth_card *card)
2374{
2375        struct qeth_cmd_buffer *iob;
2376
2377        QETH_CARD_TEXT(card, 2, "cmsetup");
2378
2379        iob = qeth_mpc_alloc_cmd(card, CM_SETUP, CM_SETUP_SIZE);
2380        if (!iob)
2381                return -ENOMEM;
2382
2383        memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2384               &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2385        memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2386               &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2387        memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2388               &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2389        return qeth_send_control_data(card, iob, qeth_cm_setup_cb, NULL);
2390}
2391
2392static bool qeth_is_supported_link_type(struct qeth_card *card, u8 link_type)
2393{
2394        if (link_type == QETH_LINK_TYPE_LANE_TR ||
2395            link_type == QETH_LINK_TYPE_HSTR) {
2396                dev_err(&card->gdev->dev, "Unsupported Token Ring device\n");
2397                return false;
2398        }
2399
2400        return true;
2401}
2402
2403static int qeth_update_max_mtu(struct qeth_card *card, unsigned int max_mtu)
2404{
2405        struct net_device *dev = card->dev;
2406        unsigned int new_mtu;
2407
2408        if (!max_mtu) {
2409                /* IQD needs accurate max MTU to set up its RX buffers: */
2410                if (IS_IQD(card))
2411                        return -EINVAL;
2412                /* tolerate quirky HW: */
2413                max_mtu = ETH_MAX_MTU;
2414        }
2415
2416        rtnl_lock();
2417        if (IS_IQD(card)) {
2418                /* move any device with default MTU to new max MTU: */
2419                new_mtu = (dev->mtu == dev->max_mtu) ? max_mtu : dev->mtu;
2420
2421                /* adjust RX buffer size to new max MTU: */
2422                card->qdio.in_buf_size = max_mtu + 2 * PAGE_SIZE;
2423                if (dev->max_mtu && dev->max_mtu != max_mtu)
2424                        qeth_free_qdio_queues(card);
2425        } else {
2426                if (dev->mtu)
2427                        new_mtu = dev->mtu;
2428                /* default MTUs for first setup: */
2429                else if (IS_LAYER2(card))
2430                        new_mtu = ETH_DATA_LEN;
2431                else
2432                        new_mtu = ETH_DATA_LEN - 8; /* allow for LLC + SNAP */
2433        }
2434
2435        dev->max_mtu = max_mtu;
2436        dev->mtu = min(new_mtu, max_mtu);
2437        rtnl_unlock();
2438        return 0;
2439}
2440
2441static int qeth_get_mtu_outof_framesize(int framesize)
2442{
2443        switch (framesize) {
2444        case 0x4000:
2445                return 8192;
2446        case 0x6000:
2447                return 16384;
2448        case 0xa000:
2449                return 32768;
2450        case 0xffff:
2451                return 57344;
2452        default:
2453                return 0;
2454        }
2455}
2456
2457static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2458                unsigned long data)
2459{
2460        __u16 mtu, framesize;
2461        __u16 len;
2462        struct qeth_cmd_buffer *iob;
2463        u8 link_type = 0;
2464
2465        QETH_CARD_TEXT(card, 2, "ulpenacb");
2466
2467        iob = (struct qeth_cmd_buffer *) data;
2468        memcpy(&card->token.ulp_filter_r,
2469               QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2470               QETH_MPC_TOKEN_LENGTH);
2471        if (IS_IQD(card)) {
2472                memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2473                mtu = qeth_get_mtu_outof_framesize(framesize);
2474        } else {
2475                mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data);
2476        }
2477        *(u16 *)reply->param = mtu;
2478
2479        memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2480        if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2481                memcpy(&link_type,
2482                       QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2483                if (!qeth_is_supported_link_type(card, link_type))
2484                        return -EPROTONOSUPPORT;
2485        }
2486
2487        card->info.link_type = link_type;
2488        QETH_CARD_TEXT_(card, 2, "link%d", card->info.link_type);
2489        return 0;
2490}
2491
2492static u8 qeth_mpc_select_prot_type(struct qeth_card *card)
2493{
2494        if (IS_OSN(card))
2495                return QETH_PROT_OSN2;
2496        return IS_LAYER2(card) ? QETH_PROT_LAYER2 : QETH_PROT_TCPIP;
2497}
2498
2499static int qeth_ulp_enable(struct qeth_card *card)
2500{
2501        u8 prot_type = qeth_mpc_select_prot_type(card);
2502        struct qeth_cmd_buffer *iob;
2503        u16 max_mtu;
2504        int rc;
2505
2506        QETH_CARD_TEXT(card, 2, "ulpenabl");
2507
2508        iob = qeth_mpc_alloc_cmd(card, ULP_ENABLE, ULP_ENABLE_SIZE);
2509        if (!iob)
2510                return -ENOMEM;
2511
2512        *(QETH_ULP_ENABLE_LINKNUM(iob->data)) = (u8) card->dev->dev_port;
2513        memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2514        memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2515               &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2516        memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2517               &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2518        rc = qeth_send_control_data(card, iob, qeth_ulp_enable_cb, &max_mtu);
2519        if (rc)
2520                return rc;
2521        return qeth_update_max_mtu(card, max_mtu);
2522}
2523
2524static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2525                unsigned long data)
2526{
2527        struct qeth_cmd_buffer *iob;
2528
2529        QETH_CARD_TEXT(card, 2, "ulpstpcb");
2530
2531        iob = (struct qeth_cmd_buffer *) data;
2532        memcpy(&card->token.ulp_connection_r,
2533               QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2534               QETH_MPC_TOKEN_LENGTH);
2535        if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2536                     3)) {
2537                QETH_CARD_TEXT(card, 2, "olmlimit");
2538                dev_err(&card->gdev->dev, "A connection could not be "
2539                        "established because of an OLM limit\n");
2540                return -EMLINK;
2541        }
2542        return 0;
2543}
2544
2545static int qeth_ulp_setup(struct qeth_card *card)
2546{
2547        __u16 temp;
2548        struct qeth_cmd_buffer *iob;
2549
2550        QETH_CARD_TEXT(card, 2, "ulpsetup");
2551
2552        iob = qeth_mpc_alloc_cmd(card, ULP_SETUP, ULP_SETUP_SIZE);
2553        if (!iob)
2554                return -ENOMEM;
2555
2556        memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2557               &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2558        memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2559               &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2560        memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2561               &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2562
2563        memcpy(QETH_ULP_SETUP_CUA(iob->data), &card->info.ddev_devno, 2);
2564        temp = (card->info.cula << 8) + card->info.unit_addr2;
2565        memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2566        return qeth_send_control_data(card, iob, qeth_ulp_setup_cb, NULL);
2567}
2568
2569static int qeth_alloc_out_buf(struct qeth_qdio_out_q *q, unsigned int bidx,
2570                              gfp_t gfp)
2571{
2572        struct qeth_qdio_out_buffer *newbuf;
2573
2574        newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, gfp);
2575        if (!newbuf)
2576                return -ENOMEM;
2577
2578        newbuf->buffer = q->qdio_bufs[bidx];
2579        skb_queue_head_init(&newbuf->skb_list);
2580        lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2581        newbuf->q = q;
2582        atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2583        q->bufs[bidx] = newbuf;
2584        return 0;
2585}
2586
2587static void qeth_free_output_queue(struct qeth_qdio_out_q *q)
2588{
2589        if (!q)
2590                return;
2591
2592        qeth_drain_output_queue(q, true);
2593        qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2594        kfree(q);
2595}
2596
2597static struct qeth_qdio_out_q *qeth_alloc_output_queue(void)
2598{
2599        struct qeth_qdio_out_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
2600        unsigned int i;
2601
2602        if (!q)
2603                return NULL;
2604
2605        if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q))
2606                goto err_qdio_bufs;
2607
2608        for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
2609                if (qeth_alloc_out_buf(q, i, GFP_KERNEL))
2610                        goto err_out_bufs;
2611        }
2612
2613        return q;
2614
2615err_out_bufs:
2616        while (i > 0)
2617                qeth_free_out_buf(q->bufs[--i]);
2618        qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2619err_qdio_bufs:
2620        kfree(q);
2621        return NULL;
2622}
2623
2624static void qeth_tx_completion_timer(struct timer_list *timer)
2625{
2626        struct qeth_qdio_out_q *queue = from_timer(queue, timer, timer);
2627
2628        napi_schedule(&queue->napi);
2629        QETH_TXQ_STAT_INC(queue, completion_timer);
2630}
2631
2632static int qeth_alloc_qdio_queues(struct qeth_card *card)
2633{
2634        unsigned int i;
2635
2636        QETH_CARD_TEXT(card, 2, "allcqdbf");
2637
2638        if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2639                QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2640                return 0;
2641
2642        QETH_CARD_TEXT(card, 2, "inq");
2643        card->qdio.in_q = qeth_alloc_qdio_queue();
2644        if (!card->qdio.in_q)
2645                goto out_nomem;
2646
2647        /* inbound buffer pool */
2648        if (qeth_alloc_buffer_pool(card))
2649                goto out_freeinq;
2650
2651        /* outbound */
2652        for (i = 0; i < card->qdio.no_out_queues; ++i) {
2653                struct qeth_qdio_out_q *queue;
2654
2655                queue = qeth_alloc_output_queue();
2656                if (!queue)
2657                        goto out_freeoutq;
2658                QETH_CARD_TEXT_(card, 2, "outq %i", i);
2659                QETH_CARD_HEX(card, 2, &queue, sizeof(void *));
2660                card->qdio.out_qs[i] = queue;
2661                queue->card = card;
2662                queue->queue_no = i;
2663                INIT_LIST_HEAD(&queue->pending_bufs);
2664                spin_lock_init(&queue->lock);
2665                timer_setup(&queue->timer, qeth_tx_completion_timer, 0);
2666                queue->coalesce_usecs = QETH_TX_COALESCE_USECS;
2667                queue->max_coalesced_frames = QETH_TX_MAX_COALESCED_FRAMES;
2668                queue->priority = QETH_QIB_PQUE_PRIO_DEFAULT;
2669        }
2670
2671        /* completion */
2672        if (qeth_alloc_cq(card))
2673                goto out_freeoutq;
2674
2675        return 0;
2676
2677out_freeoutq:
2678        while (i > 0) {
2679                qeth_free_output_queue(card->qdio.out_qs[--i]);
2680                card->qdio.out_qs[i] = NULL;
2681        }
2682        qeth_free_buffer_pool(card);
2683out_freeinq:
2684        qeth_free_qdio_queue(card->qdio.in_q);
2685        card->qdio.in_q = NULL;
2686out_nomem:
2687        atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2688        return -ENOMEM;
2689}
2690
2691static void qeth_free_qdio_queues(struct qeth_card *card)
2692{
2693        int i, j;
2694
2695        if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
2696                QETH_QDIO_UNINITIALIZED)
2697                return;
2698
2699        qeth_free_cq(card);
2700        for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2701                if (card->qdio.in_q->bufs[j].rx_skb)
2702                        dev_kfree_skb_any(card->qdio.in_q->bufs[j].rx_skb);
2703        }
2704        qeth_free_qdio_queue(card->qdio.in_q);
2705        card->qdio.in_q = NULL;
2706        /* inbound buffer pool */
2707        qeth_free_buffer_pool(card);
2708        /* free outbound qdio_qs */
2709        for (i = 0; i < card->qdio.no_out_queues; i++) {
2710                qeth_free_output_queue(card->qdio.out_qs[i]);
2711                card->qdio.out_qs[i] = NULL;
2712        }
2713}
2714
2715static void qeth_fill_qib_parms(struct qeth_card *card,
2716                                struct qeth_qib_parms *parms)
2717{
2718        struct qeth_qdio_out_q *queue;
2719        unsigned int i;
2720
2721        parms->pcit_magic[0] = 'P';
2722        parms->pcit_magic[1] = 'C';
2723        parms->pcit_magic[2] = 'I';
2724        parms->pcit_magic[3] = 'T';
2725        ASCEBC(parms->pcit_magic, sizeof(parms->pcit_magic));
2726        parms->pcit_a = QETH_PCI_THRESHOLD_A(card);
2727        parms->pcit_b = QETH_PCI_THRESHOLD_B(card);
2728        parms->pcit_c = QETH_PCI_TIMER_VALUE(card);
2729
2730        parms->blkt_magic[0] = 'B';
2731        parms->blkt_magic[1] = 'L';
2732        parms->blkt_magic[2] = 'K';
2733        parms->blkt_magic[3] = 'T';
2734        ASCEBC(parms->blkt_magic, sizeof(parms->blkt_magic));
2735        parms->blkt_total = card->info.blkt.time_total;
2736        parms->blkt_inter_packet = card->info.blkt.inter_packet;
2737        parms->blkt_inter_packet_jumbo = card->info.blkt.inter_packet_jumbo;
2738
2739        /* Prio-queueing implicitly uses the default priorities: */
2740        if (qeth_uses_tx_prio_queueing(card) || card->qdio.no_out_queues == 1)
2741                return;
2742
2743        parms->pque_magic[0] = 'P';
2744        parms->pque_magic[1] = 'Q';
2745        parms->pque_magic[2] = 'U';
2746        parms->pque_magic[3] = 'E';
2747        ASCEBC(parms->pque_magic, sizeof(parms->pque_magic));
2748        parms->pque_order = QETH_QIB_PQUE_ORDER_RR;
2749        parms->pque_units = QETH_QIB_PQUE_UNITS_SBAL;
2750
2751        qeth_for_each_output_queue(card, queue, i)
2752                parms->pque_priority[i] = queue->priority;
2753}
2754
2755static int qeth_qdio_activate(struct qeth_card *card)
2756{
2757        QETH_CARD_TEXT(card, 3, "qdioact");
2758        return qdio_activate(CARD_DDEV(card));
2759}
2760
2761static int qeth_dm_act(struct qeth_card *card)
2762{
2763        struct qeth_cmd_buffer *iob;
2764
2765        QETH_CARD_TEXT(card, 2, "dmact");
2766
2767        iob = qeth_mpc_alloc_cmd(card, DM_ACT, DM_ACT_SIZE);
2768        if (!iob)
2769                return -ENOMEM;
2770
2771        memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2772               &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2773        memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2774               &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2775        return qeth_send_control_data(card, iob, NULL, NULL);
2776}
2777
2778static int qeth_mpc_initialize(struct qeth_card *card)
2779{
2780        int rc;
2781
2782        QETH_CARD_TEXT(card, 2, "mpcinit");
2783
2784        rc = qeth_issue_next_read(card);
2785        if (rc) {
2786                QETH_CARD_TEXT_(card, 2, "1err%d", rc);
2787                return rc;
2788        }
2789        rc = qeth_cm_enable(card);
2790        if (rc) {
2791                QETH_CARD_TEXT_(card, 2, "2err%d", rc);
2792                return rc;
2793        }
2794        rc = qeth_cm_setup(card);
2795        if (rc) {
2796                QETH_CARD_TEXT_(card, 2, "3err%d", rc);
2797                return rc;
2798        }
2799        rc = qeth_ulp_enable(card);
2800        if (rc) {
2801                QETH_CARD_TEXT_(card, 2, "4err%d", rc);
2802                return rc;
2803        }
2804        rc = qeth_ulp_setup(card);
2805        if (rc) {
2806                QETH_CARD_TEXT_(card, 2, "5err%d", rc);
2807                return rc;
2808        }
2809        rc = qeth_alloc_qdio_queues(card);
2810        if (rc) {
2811                QETH_CARD_TEXT_(card, 2, "5err%d", rc);
2812                return rc;
2813        }
2814        rc = qeth_qdio_establish(card);
2815        if (rc) {
2816                QETH_CARD_TEXT_(card, 2, "6err%d", rc);
2817                qeth_free_qdio_queues(card);
2818                return rc;
2819        }
2820        rc = qeth_qdio_activate(card);
2821        if (rc) {
2822                QETH_CARD_TEXT_(card, 2, "7err%d", rc);
2823                return rc;
2824        }
2825        rc = qeth_dm_act(card);
2826        if (rc) {
2827                QETH_CARD_TEXT_(card, 2, "8err%d", rc);
2828                return rc;
2829        }
2830
2831        return 0;
2832}
2833
2834static void qeth_print_status_message(struct qeth_card *card)
2835{
2836        switch (card->info.type) {
2837        case QETH_CARD_TYPE_OSD:
2838        case QETH_CARD_TYPE_OSM:
2839        case QETH_CARD_TYPE_OSX:
2840                /* VM will use a non-zero first character
2841                 * to indicate a HiperSockets like reporting
2842                 * of the level OSA sets the first character to zero
2843                 * */
2844                if (!card->info.mcl_level[0]) {
2845                        sprintf(card->info.mcl_level, "%02x%02x",
2846                                card->info.mcl_level[2],
2847                                card->info.mcl_level[3]);
2848                        break;
2849                }
2850                fallthrough;
2851        case QETH_CARD_TYPE_IQD:
2852                if (IS_VM_NIC(card) || (card->info.mcl_level[0] & 0x80)) {
2853                        card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2854                                card->info.mcl_level[0]];
2855                        card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2856                                card->info.mcl_level[1]];
2857                        card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2858                                card->info.mcl_level[2]];
2859                        card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2860                                card->info.mcl_level[3]];
2861                        card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2862                }
2863                break;
2864        default:
2865                memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2866        }
2867        dev_info(&card->gdev->dev,
2868                 "Device is a%s card%s%s%s\nwith link type %s.\n",
2869                 qeth_get_cardname(card),
2870                 (card->info.mcl_level[0]) ? " (level: " : "",
2871                 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2872                 (card->info.mcl_level[0]) ? ")" : "",
2873                 qeth_get_cardname_short(card));
2874}
2875
2876static void qeth_initialize_working_pool_list(struct qeth_card *card)
2877{
2878        struct qeth_buffer_pool_entry *entry;
2879
2880        QETH_CARD_TEXT(card, 5, "inwrklst");
2881
2882        list_for_each_entry(entry,
2883                            &card->qdio.init_pool.entry_list, init_list) {
2884                qeth_put_buffer_pool_entry(card, entry);
2885        }
2886}
2887
2888static struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2889                                        struct qeth_card *card)
2890{
2891        struct qeth_buffer_pool_entry *entry;
2892        int i, free;
2893
2894        if (list_empty(&card->qdio.in_buf_pool.entry_list))
2895                return NULL;
2896
2897        list_for_each_entry(entry, &card->qdio.in_buf_pool.entry_list, list) {
2898                free = 1;
2899                for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2900                        if (page_count(entry->elements[i]) > 1) {
2901                                free = 0;
2902                                break;
2903                        }
2904                }
2905                if (free) {
2906                        list_del_init(&entry->list);
2907                        return entry;
2908                }
2909        }
2910
2911        /* no free buffer in pool so take first one and swap pages */
2912        entry = list_first_entry(&card->qdio.in_buf_pool.entry_list,
2913                                 struct qeth_buffer_pool_entry, list);
2914        for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2915                if (page_count(entry->elements[i]) > 1) {
2916                        struct page *page = dev_alloc_page();
2917
2918                        if (!page)
2919                                return NULL;
2920
2921                        __free_page(entry->elements[i]);
2922                        entry->elements[i] = page;
2923                        QETH_CARD_STAT_INC(card, rx_sg_alloc_page);
2924                }
2925        }
2926        list_del_init(&entry->list);
2927        return entry;
2928}
2929
2930static int qeth_init_input_buffer(struct qeth_card *card,
2931                struct qeth_qdio_buffer *buf)
2932{
2933        struct qeth_buffer_pool_entry *pool_entry = buf->pool_entry;
2934        int i;
2935
2936        if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2937                buf->rx_skb = netdev_alloc_skb(card->dev,
2938                                               ETH_HLEN +
2939                                               sizeof(struct ipv6hdr));
2940                if (!buf->rx_skb)
2941                        return -ENOMEM;
2942        }
2943
2944        if (!pool_entry) {
2945                pool_entry = qeth_find_free_buffer_pool_entry(card);
2946                if (!pool_entry)
2947                        return -ENOBUFS;
2948
2949                buf->pool_entry = pool_entry;
2950        }
2951
2952        /*
2953         * since the buffer is accessed only from the input_tasklet
2954         * there shouldn't be a need to synchronize; also, since we use
2955         * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2956         * buffers
2957         */
2958        for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2959                buf->buffer->element[i].length = PAGE_SIZE;
2960                buf->buffer->element[i].addr =
2961                        page_to_phys(pool_entry->elements[i]);
2962                if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2963                        buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2964                else
2965                        buf->buffer->element[i].eflags = 0;
2966                buf->buffer->element[i].sflags = 0;
2967        }
2968        return 0;
2969}
2970
2971static unsigned int qeth_tx_select_bulk_max(struct qeth_card *card,
2972                                            struct qeth_qdio_out_q *queue)
2973{
2974        if (!IS_IQD(card) ||
2975            qeth_iqd_is_mcast_queue(card, queue) ||
2976            card->options.cq == QETH_CQ_ENABLED ||
2977            qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd))
2978                return 1;
2979
2980        return card->ssqd.mmwc ? card->ssqd.mmwc : 1;
2981}
2982
2983static int qeth_init_qdio_queues(struct qeth_card *card)
2984{
2985        unsigned int rx_bufs = card->qdio.in_buf_pool.buf_count;
2986        unsigned int i;
2987        int rc;
2988
2989        QETH_CARD_TEXT(card, 2, "initqdqs");
2990
2991        /* inbound queue */
2992        qdio_reset_buffers(card->qdio.in_q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2993        memset(&card->rx, 0, sizeof(struct qeth_rx));
2994
2995        qeth_initialize_working_pool_list(card);
2996        /*give only as many buffers to hardware as we have buffer pool entries*/
2997        for (i = 0; i < rx_bufs; i++) {
2998                rc = qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2999                if (rc)
3000                        return rc;
3001        }
3002
3003        card->qdio.in_q->next_buf_to_init = QDIO_BUFNR(rx_bufs);
3004        rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0, rx_bufs,
3005                     NULL);
3006        if (rc) {
3007                QETH_CARD_TEXT_(card, 2, "1err%d", rc);
3008                return rc;
3009        }
3010
3011        /* completion */
3012        rc = qeth_cq_init(card);
3013        if (rc) {
3014                return rc;
3015        }
3016
3017        /* outbound queue */
3018        for (i = 0; i < card->qdio.no_out_queues; ++i) {
3019                struct qeth_qdio_out_q *queue = card->qdio.out_qs[i];
3020
3021                qdio_reset_buffers(queue->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
3022                queue->max_elements = QETH_MAX_BUFFER_ELEMENTS(card);
3023                queue->next_buf_to_fill = 0;
3024                queue->do_pack = 0;
3025                queue->prev_hdr = NULL;
3026                queue->coalesced_frames = 0;
3027                queue->bulk_start = 0;
3028                queue->bulk_count = 0;
3029                queue->bulk_max = qeth_tx_select_bulk_max(card, queue);
3030                atomic_set(&queue->used_buffers, 0);
3031                atomic_set(&queue->set_pci_flags_count, 0);
3032                netdev_tx_reset_queue(netdev_get_tx_queue(card->dev, i));
3033        }
3034        return 0;
3035}
3036
3037static void qeth_ipa_finalize_cmd(struct qeth_card *card,
3038                                  struct qeth_cmd_buffer *iob)
3039{
3040        qeth_mpc_finalize_cmd(card, iob);
3041
3042        /* override with IPA-specific values: */
3043        __ipa_cmd(iob)->hdr.seqno = card->seqno.ipa++;
3044}
3045
3046void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
3047                          u16 cmd_length,
3048                          bool (*match)(struct qeth_cmd_buffer *iob,
3049                                        struct qeth_cmd_buffer *reply))
3050{
3051        u8 prot_type = qeth_mpc_select_prot_type(card);
3052        u16 total_length = iob->length;
3053
3054        qeth_setup_ccw(__ccw_from_cmd(iob), CCW_CMD_WRITE, 0, total_length,
3055                       iob->data);
3056        iob->finalize = qeth_ipa_finalize_cmd;
3057        iob->match = match;
3058
3059        memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
3060        memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &total_length, 2);
3061        memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
3062        memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &cmd_length, 2);
3063        memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &cmd_length, 2);
3064        memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
3065               &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
3066        memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &cmd_length, 2);
3067}
3068EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
3069
3070static bool qeth_ipa_match_reply(struct qeth_cmd_buffer *iob,
3071                                 struct qeth_cmd_buffer *reply)
3072{
3073        struct qeth_ipa_cmd *ipa_reply = __ipa_reply(reply);
3074
3075        return ipa_reply && (__ipa_cmd(iob)->hdr.seqno == ipa_reply->hdr.seqno);
3076}
3077
3078struct qeth_cmd_buffer *qeth_ipa_alloc_cmd(struct qeth_card *card,
3079                                           enum qeth_ipa_cmds cmd_code,
3080                                           enum qeth_prot_versions prot,
3081                                           unsigned int data_length)
3082{
3083        struct qeth_cmd_buffer *iob;
3084        struct qeth_ipacmd_hdr *hdr;
3085
3086        data_length += offsetof(struct qeth_ipa_cmd, data);
3087        iob = qeth_alloc_cmd(&card->write, IPA_PDU_HEADER_SIZE + data_length, 1,
3088                             QETH_IPA_TIMEOUT);
3089        if (!iob)
3090                return NULL;
3091
3092        qeth_prepare_ipa_cmd(card, iob, data_length, qeth_ipa_match_reply);
3093
3094        hdr = &__ipa_cmd(iob)->hdr;
3095        hdr->command = cmd_code;
3096        hdr->initiator = IPA_CMD_INITIATOR_HOST;
3097        /* hdr->seqno is set by qeth_send_control_data() */
3098        hdr->adapter_type = QETH_LINK_TYPE_FAST_ETH;
3099        hdr->rel_adapter_no = (u8) card->dev->dev_port;
3100        hdr->prim_version_no = IS_LAYER2(card) ? 2 : 1;
3101        hdr->param_count = 1;
3102        hdr->prot_version = prot;
3103        return iob;
3104}
3105EXPORT_SYMBOL_GPL(qeth_ipa_alloc_cmd);
3106
3107static int qeth_send_ipa_cmd_cb(struct qeth_card *card,
3108                                struct qeth_reply *reply, unsigned long data)
3109{
3110        struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3111
3112        return (cmd->hdr.return_code) ? -EIO : 0;
3113}
3114
3115/**
3116 * qeth_send_ipa_cmd() - send an IPA command
3117 *
3118 * See qeth_send_control_data() for explanation of the arguments.
3119 */
3120
3121int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
3122                int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
3123                        unsigned long),
3124                void *reply_param)
3125{
3126        int rc;
3127
3128        QETH_CARD_TEXT(card, 4, "sendipa");
3129
3130        if (card->read_or_write_problem) {
3131                qeth_put_cmd(iob);
3132                return -EIO;
3133        }
3134
3135        if (reply_cb == NULL)
3136                reply_cb = qeth_send_ipa_cmd_cb;
3137        rc = qeth_send_control_data(card, iob, reply_cb, reply_param);
3138        if (rc == -ETIME) {
3139                qeth_clear_ipacmd_list(card);
3140                qeth_schedule_recovery(card);
3141        }
3142        return rc;
3143}
3144EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
3145
3146static int qeth_send_startlan_cb(struct qeth_card *card,
3147                                 struct qeth_reply *reply, unsigned long data)
3148{
3149        struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3150
3151        if (cmd->hdr.return_code == IPA_RC_LAN_OFFLINE)
3152                return -ENETDOWN;
3153
3154        return (cmd->hdr.return_code) ? -EIO : 0;
3155}
3156
3157static int qeth_send_startlan(struct qeth_card *card)
3158{
3159        struct qeth_cmd_buffer *iob;
3160
3161        QETH_CARD_TEXT(card, 2, "strtlan");
3162
3163        iob = qeth_ipa_alloc_cmd(card, IPA_CMD_STARTLAN, QETH_PROT_NONE, 0);
3164        if (!iob)
3165                return -ENOMEM;
3166        return qeth_send_ipa_cmd(card, iob, qeth_send_startlan_cb, NULL);
3167}
3168
3169static int qeth_setadpparms_inspect_rc(struct qeth_ipa_cmd *cmd)
3170{
3171        if (!cmd->hdr.return_code)
3172                cmd->hdr.return_code =
3173                        cmd->data.setadapterparms.hdr.return_code;
3174        return cmd->hdr.return_code;
3175}
3176
3177static int qeth_query_setadapterparms_cb(struct qeth_card *card,
3178                struct qeth_reply *reply, unsigned long data)
3179{
3180        struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3181        struct qeth_query_cmds_supp *query_cmd;
3182
3183        QETH_CARD_TEXT(card, 3, "quyadpcb");
3184        if (qeth_setadpparms_inspect_rc(cmd))
3185                return -EIO;
3186
3187        query_cmd = &cmd->data.setadapterparms.data.query_cmds_supp;
3188        if (query_cmd->lan_type & 0x7f) {
3189                if (!qeth_is_supported_link_type(card, query_cmd->lan_type))
3190                        return -EPROTONOSUPPORT;
3191
3192                card->info.link_type = query_cmd->lan_type;
3193                QETH_CARD_TEXT_(card, 2, "lnk %d", card->info.link_type);
3194        }
3195
3196        card->options.adp.supported = query_cmd->supported_cmds;
3197        return 0;
3198}
3199
3200static struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
3201                                                    enum qeth_ipa_setadp_cmd adp_cmd,
3202                                                    unsigned int data_length)
3203{
3204        struct qeth_ipacmd_setadpparms_hdr *hdr;
3205        struct qeth_cmd_buffer *iob;
3206
3207        iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETADAPTERPARMS, QETH_PROT_IPV4,
3208                                 data_length +
3209                                 offsetof(struct qeth_ipacmd_setadpparms,
3210                                          data));
3211        if (!iob)
3212                return NULL;
3213
3214        hdr = &__ipa_cmd(iob)->data.setadapterparms.hdr;
3215        hdr->cmdlength = sizeof(*hdr) + data_length;
3216        hdr->command_code = adp_cmd;
3217        hdr->used_total = 1;
3218        hdr->seq_no = 1;
3219        return iob;
3220}
3221
3222static int qeth_query_setadapterparms(struct qeth_card *card)
3223{
3224        int rc;
3225        struct qeth_cmd_buffer *iob;
3226
3227        QETH_CARD_TEXT(card, 3, "queryadp");
3228        iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
3229                                   SETADP_DATA_SIZEOF(query_cmds_supp));
3230        if (!iob)
3231                return -ENOMEM;
3232        rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
3233        return rc;
3234}
3235
3236static int qeth_query_ipassists_cb(struct qeth_card *card,
3237                struct qeth_reply *reply, unsigned long data)
3238{
3239        struct qeth_ipa_cmd *cmd;
3240
3241        QETH_CARD_TEXT(card, 2, "qipasscb");
3242
3243        cmd = (struct qeth_ipa_cmd *) data;
3244
3245        switch (cmd->hdr.return_code) {
3246        case IPA_RC_SUCCESS:
3247                break;
3248        case IPA_RC_NOTSUPP:
3249        case IPA_RC_L2_UNSUPPORTED_CMD:
3250                QETH_CARD_TEXT(card, 2, "ipaunsup");
3251                card->options.ipa4.supported |= IPA_SETADAPTERPARMS;
3252                card->options.ipa6.supported |= IPA_SETADAPTERPARMS;
3253                return -EOPNOTSUPP;
3254        default:
3255                QETH_DBF_MESSAGE(1, "IPA_CMD_QIPASSIST on device %x: Unhandled rc=%#x\n",
3256                                 CARD_DEVID(card), cmd->hdr.return_code);
3257                return -EIO;
3258        }
3259
3260        if (cmd->hdr.prot_version == QETH_PROT_IPV4)
3261                card->options.ipa4 = cmd->hdr.assists;
3262        else if (cmd->hdr.prot_version == QETH_PROT_IPV6)
3263                card->options.ipa6 = cmd->hdr.assists;
3264        else
3265                QETH_DBF_MESSAGE(1, "IPA_CMD_QIPASSIST on device %x: Flawed LIC detected\n",
3266                                 CARD_DEVID(card));
3267        return 0;
3268}
3269
3270static int qeth_query_ipassists(struct qeth_card *card,
3271                                enum qeth_prot_versions prot)
3272{
3273        int rc;
3274        struct qeth_cmd_buffer *iob;
3275
3276        QETH_CARD_TEXT_(card, 2, "qipassi%i", prot);
3277        iob = qeth_ipa_alloc_cmd(card, IPA_CMD_QIPASSIST, prot, 0);
3278        if (!iob)
3279                return -ENOMEM;
3280        rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
3281        return rc;
3282}
3283
3284static int qeth_query_switch_attributes_cb(struct qeth_card *card,
3285                                struct qeth_reply *reply, unsigned long data)
3286{
3287        struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3288        struct qeth_query_switch_attributes *attrs;
3289        struct qeth_switch_info *sw_info;
3290
3291        QETH_CARD_TEXT(card, 2, "qswiatcb");
3292        if (qeth_setadpparms_inspect_rc(cmd))
3293                return -EIO;
3294
3295        sw_info = (struct qeth_switch_info *)reply->param;
3296        attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
3297        sw_info->capabilities = attrs->capabilities;
3298        sw_info->settings = attrs->settings;
3299        QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
3300                        sw_info->settings);
3301        return 0;
3302}
3303
3304int qeth_query_switch_attributes(struct qeth_card *card,
3305                                 struct qeth_switch_info *sw_info)
3306{
3307        struct qeth_cmd_buffer *iob;
3308
3309        QETH_CARD_TEXT(card, 2, "qswiattr");
3310        if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
3311                return -EOPNOTSUPP;
3312        if (!netif_carrier_ok(card->dev))
3313                return -ENOMEDIUM;
3314        iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES, 0);
3315        if (!iob)
3316                return -ENOMEM;
3317        return qeth_send_ipa_cmd(card, iob,
3318                                qeth_query_switch_attributes_cb, sw_info);
3319}
3320
3321struct qeth_cmd_buffer *qeth_get_diag_cmd(struct qeth_card *card,
3322                                          enum qeth_diags_cmds sub_cmd,
3323                                          unsigned int data_length)
3324{
3325        struct qeth_ipacmd_diagass *cmd;
3326        struct qeth_cmd_buffer *iob;
3327
3328        iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SET_DIAG_ASS, QETH_PROT_NONE,
3329                                 DIAG_HDR_LEN + data_length);
3330        if (!iob)
3331                return NULL;
3332
3333        cmd = &__ipa_cmd(iob)->data.diagass;
3334        cmd->subcmd_len = DIAG_SUB_HDR_LEN + data_length;
3335        cmd->subcmd = sub_cmd;
3336        return iob;
3337}
3338EXPORT_SYMBOL_GPL(qeth_get_diag_cmd);
3339
3340static int qeth_query_setdiagass_cb(struct qeth_card *card,
3341                struct qeth_reply *reply, unsigned long data)
3342{
3343        struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3344        u16 rc = cmd->hdr.return_code;
3345
3346        if (rc) {
3347                QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3348                return -EIO;
3349        }
3350
3351        card->info.diagass_support = cmd->data.diagass.ext;
3352        return 0;
3353}
3354
3355static int qeth_query_setdiagass(struct qeth_card *card)
3356{
3357        struct qeth_cmd_buffer *iob;
3358
3359        QETH_CARD_TEXT(card, 2, "qdiagass");
3360        iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_QUERY, 0);
3361        if (!iob)
3362                return -ENOMEM;
3363        return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3364}
3365
3366static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3367{
3368        unsigned long info = get_zeroed_page(GFP_KERNEL);
3369        struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3370        struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3371        struct ccw_dev_id ccwid;
3372        int level;
3373
3374        tid->chpid = card->info.chpid;
3375        ccw_device_get_id(CARD_RDEV(card), &ccwid);
3376        tid->ssid = ccwid.ssid;
3377        tid->devno = ccwid.devno;
3378        if (!info)
3379                return;
3380        level = stsi(NULL, 0, 0, 0);
3381        if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3382                tid->lparnr = info222->lpar_number;
3383        if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3384                EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3385                memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3386        }
3387        free_page(info);
3388}
3389
3390static int qeth_hw_trap_cb(struct qeth_card *card,
3391                struct qeth_reply *reply, unsigned long data)
3392{
3393        struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3394        u16 rc = cmd->hdr.return_code;
3395
3396        if (rc) {
3397                QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3398                return -EIO;
3399        }
3400        return 0;
3401}
3402
3403int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3404{
3405        struct qeth_cmd_buffer *iob;
3406        struct qeth_ipa_cmd *cmd;
3407
3408        QETH_CARD_TEXT(card, 2, "diagtrap");
3409        iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRAP, 64);
3410        if (!iob)
3411                return -ENOMEM;
3412        cmd = __ipa_cmd(iob);
3413        cmd->data.diagass.type = 1;
3414        cmd->data.diagass.action = action;
3415        switch (action) {
3416        case QETH_DIAGS_TRAP_ARM:
3417                cmd->data.diagass.options = 0x0003;
3418                cmd->data.diagass.ext = 0x00010000 +
3419                        sizeof(struct qeth_trap_id);
3420                qeth_get_trap_id(card,
3421                        (struct qeth_trap_id *)cmd->data.diagass.cdata);
3422                break;
3423        case QETH_DIAGS_TRAP_DISARM:
3424                cmd->data.diagass.options = 0x0001;
3425                break;
3426        case QETH_DIAGS_TRAP_CAPTURE:
3427                break;
3428        }
3429        return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3430}
3431
3432static int qeth_check_qdio_errors(struct qeth_card *card,
3433                                  struct qdio_buffer *buf,
3434                                  unsigned int qdio_error,
3435                                  const char *dbftext)
3436{
3437        if (qdio_error) {
3438                QETH_CARD_TEXT(card, 2, dbftext);
3439                QETH_CARD_TEXT_(card, 2, " F15=%02X",
3440                               buf->element[15].sflags);
3441                QETH_CARD_TEXT_(card, 2, " F14=%02X",
3442                               buf->element[14].sflags);
3443                QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3444                if ((buf->element[15].sflags) == 0x12) {
3445                        QETH_CARD_STAT_INC(card, rx_fifo_errors);
3446                        return 0;
3447                } else
3448                        return 1;
3449        }
3450        return 0;
3451}
3452
3453static unsigned int qeth_rx_refill_queue(struct qeth_card *card,
3454                                         unsigned int count)
3455{
3456        struct qeth_qdio_q *queue = card->qdio.in_q;
3457        struct list_head *lh;
3458        int i;
3459        int rc;
3460        int newcount = 0;
3461
3462        /* only requeue at a certain threshold to avoid SIGAs */
3463        if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3464                for (i = queue->next_buf_to_init;
3465                     i < queue->next_buf_to_init + count; ++i) {
3466                        if (qeth_init_input_buffer(card,
3467                                &queue->bufs[QDIO_BUFNR(i)])) {
3468                                break;
3469                        } else {
3470                                newcount++;
3471                        }
3472                }
3473
3474                if (newcount < count) {
3475                        /* we are in memory shortage so we switch back to
3476                           traditional skb allocation and drop packages */
3477                        atomic_set(&card->force_alloc_skb, 3);
3478                        count = newcount;
3479                } else {
3480                        atomic_add_unless(&card->force_alloc_skb, -1, 0);
3481                }
3482
3483                if (!count) {
3484                        i = 0;
3485                        list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3486                                i++;
3487                        if (i == card->qdio.in_buf_pool.buf_count) {
3488                                QETH_CARD_TEXT(card, 2, "qsarbw");
3489                                schedule_delayed_work(
3490                                        &card->buffer_reclaim_work,
3491                                        QETH_RECLAIM_WORK_TIME);
3492                        }
3493                        return 0;
3494                }
3495
3496                rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
3497                             queue->next_buf_to_init, count, NULL);
3498                if (rc) {
3499                        QETH_CARD_TEXT(card, 2, "qinberr");
3500                }
3501                queue->next_buf_to_init = QDIO_BUFNR(queue->next_buf_to_init +
3502                                                     count);
3503                return count;
3504        }
3505
3506        return 0;
3507}
3508
3509static void qeth_buffer_reclaim_work(struct work_struct *work)
3510{
3511        struct qeth_card *card = container_of(to_delayed_work(work),
3512                                              struct qeth_card,
3513                                              buffer_reclaim_work);
3514
3515        local_bh_disable();
3516        napi_schedule(&card->napi);
3517        /* kick-start the NAPI softirq: */
3518        local_bh_enable();
3519}
3520
3521static void qeth_handle_send_error(struct qeth_card *card,
3522                struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3523{
3524        int sbalf15 = buffer->buffer->element[15].sflags;
3525
3526        QETH_CARD_TEXT(card, 6, "hdsnderr");
3527        qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3528
3529        if (!qdio_err)
3530                return;
3531
3532        if ((sbalf15 >= 15) && (sbalf15 <= 31))
3533                return;
3534
3535        QETH_CARD_TEXT(card, 1, "lnkfail");
3536        QETH_CARD_TEXT_(card, 1, "%04x %02x",
3537                       (u16)qdio_err, (u8)sbalf15);
3538}
3539
3540/**
3541 * qeth_prep_flush_pack_buffer - Prepares flushing of a packing buffer.
3542 * @queue: queue to check for packing buffer
3543 *
3544 * Returns number of buffers that were prepared for flush.
3545 */
3546static int qeth_prep_flush_pack_buffer(struct qeth_qdio_out_q *queue)
3547{
3548        struct qeth_qdio_out_buffer *buffer;
3549
3550        buffer = queue->bufs[queue->next_buf_to_fill];
3551        if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3552            (buffer->next_element_to_fill > 0)) {
3553                /* it's a packing buffer */
3554                atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3555                queue->next_buf_to_fill =
3556                        QDIO_BUFNR(queue->next_buf_to_fill + 1);
3557                return 1;
3558        }
3559        return 0;
3560}
3561
3562/*
3563 * Switched to packing state if the number of used buffers on a queue
3564 * reaches a certain limit.
3565 */
3566static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3567{
3568        if (!queue->do_pack) {
3569                if (atomic_read(&queue->used_buffers)
3570                    >= QETH_HIGH_WATERMARK_PACK){
3571                        /* switch non-PACKING -> PACKING */
3572                        QETH_CARD_TEXT(queue->card, 6, "np->pack");
3573                        QETH_TXQ_STAT_INC(queue, packing_mode_switch);
3574                        queue->do_pack = 1;
3575                }
3576        }
3577}
3578
3579/*
3580 * Switches from packing to non-packing mode. If there is a packing
3581 * buffer on the queue this buffer will be prepared to be flushed.
3582 * In that case 1 is returned to inform the caller. If no buffer
3583 * has to be flushed, zero is returned.
3584 */
3585static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3586{
3587        if (queue->do_pack) {
3588                if (atomic_read(&queue->used_buffers)
3589                    <= QETH_LOW_WATERMARK_PACK) {
3590                        /* switch PACKING -> non-PACKING */
3591                        QETH_CARD_TEXT(queue->card, 6, "pack->np");
3592                        QETH_TXQ_STAT_INC(queue, packing_mode_switch);
3593                        queue->do_pack = 0;
3594                        return qeth_prep_flush_pack_buffer(queue);
3595                }
3596        }
3597        return 0;
3598}
3599
3600static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3601                               int count)
3602{
3603        struct qeth_qdio_out_buffer *buf = queue->bufs[index];
3604        unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
3605        struct qeth_card *card = queue->card;
3606        struct qaob *aob = NULL;
3607        int rc;
3608        int i;
3609
3610        for (i = index; i < index + count; ++i) {
3611                unsigned int bidx = QDIO_BUFNR(i);
3612                struct sk_buff *skb;
3613
3614                buf = queue->bufs[bidx];
3615                buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3616                                SBAL_EFLAGS_LAST_ENTRY;
3617                queue->coalesced_frames += buf->frames;
3618
3619                if (IS_IQD(card)) {
3620                        skb_queue_walk(&buf->skb_list, skb)
3621                                skb_tx_timestamp(skb);
3622                }
3623        }
3624
3625        if (IS_IQD(card)) {
3626                if (card->options.cq == QETH_CQ_ENABLED &&
3627                    !qeth_iqd_is_mcast_queue(card, queue) &&
3628                    count == 1) {
3629                        if (!buf->aob)
3630                                buf->aob = qdio_allocate_aob();
3631                        if (buf->aob) {
3632                                aob = buf->aob;
3633                                aob->user1 = (u64) buf;
3634                        }
3635                }
3636        } else {
3637                if (!queue->do_pack) {
3638                        if ((atomic_read(&queue->used_buffers) >=
3639                                (QETH_HIGH_WATERMARK_PACK -
3640                                 QETH_WATERMARK_PACK_FUZZ)) &&
3641                            !atomic_read(&queue->set_pci_flags_count)) {
3642                                /* it's likely that we'll go to packing
3643                                 * mode soon */
3644                                atomic_inc(&queue->set_pci_flags_count);
3645                                buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3646                        }
3647                } else {
3648                        if (!atomic_read(&queue->set_pci_flags_count)) {
3649                                /*
3650                                 * there's no outstanding PCI any more, so we
3651                                 * have to request a PCI to be sure the the PCI
3652                                 * will wake at some time in the future then we
3653                                 * can flush packed buffers that might still be
3654                                 * hanging around, which can happen if no
3655                                 * further send was requested by the stack
3656                                 */
3657                                atomic_inc(&queue->set_pci_flags_count);
3658                                buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3659                        }
3660                }
3661
3662                if (atomic_read(&queue->set_pci_flags_count))
3663                        qdio_flags |= QDIO_FLAG_PCI_OUT;
3664        }
3665
3666        QETH_TXQ_STAT_INC(queue, doorbell);
3667        rc = do_QDIO(CARD_DDEV(card), qdio_flags, queue->queue_no, index, count,
3668                     aob);
3669
3670        switch (rc) {
3671        case 0:
3672        case -ENOBUFS:
3673                /* ignore temporary SIGA errors without busy condition */
3674
3675                /* Fake the TX completion interrupt: */
3676                if (IS_IQD(card)) {
3677                        unsigned int frames = READ_ONCE(queue->max_coalesced_frames);
3678                        unsigned int usecs = READ_ONCE(queue->coalesce_usecs);
3679
3680                        if (frames && queue->coalesced_frames >= frames) {
3681                                napi_schedule(&queue->napi);
3682                                queue->coalesced_frames = 0;
3683                                QETH_TXQ_STAT_INC(queue, coal_frames);
3684                        } else if (usecs) {
3685                                qeth_tx_arm_timer(queue, usecs);
3686                        }
3687                }
3688
3689                break;
3690        default:
3691                QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3692                QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3693                QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3694                QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3695                QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3696
3697                /* this must not happen under normal circumstances. if it
3698                 * happens something is really wrong -> recover */
3699                qeth_schedule_recovery(queue->card);
3700        }
3701}
3702
3703static void qeth_flush_queue(struct qeth_qdio_out_q *queue)
3704{
3705        qeth_flush_buffers(queue, queue->bulk_start, queue->bulk_count);
3706
3707        queue->bulk_start = QDIO_BUFNR(queue->bulk_start + queue->bulk_count);
3708        queue->prev_hdr = NULL;
3709        queue->bulk_count = 0;
3710}
3711
3712static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3713{
3714        /*
3715         * check if weed have to switch to non-packing mode or if
3716         * we have to get a pci flag out on the queue
3717         */
3718        if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3719            !atomic_read(&queue->set_pci_flags_count)) {
3720                unsigned int index, flush_cnt;
3721                bool q_was_packing;
3722
3723                spin_lock(&queue->lock);
3724
3725                index = queue->next_buf_to_fill;
3726                q_was_packing = queue->do_pack;
3727
3728                flush_cnt = qeth_switch_to_nonpacking_if_needed(queue);
3729                if (!flush_cnt && !atomic_read(&queue->set_pci_flags_count))
3730                        flush_cnt = qeth_prep_flush_pack_buffer(queue);
3731
3732                if (flush_cnt) {
3733                        qeth_flush_buffers(queue, index, flush_cnt);
3734                        if (q_was_packing)
3735                                QETH_TXQ_STAT_ADD(queue, bufs_pack, flush_cnt);
3736                }
3737
3738                spin_unlock(&queue->lock);
3739        }
3740}
3741
3742static void qeth_qdio_poll(struct ccw_device *cdev, unsigned long card_ptr)
3743{
3744        struct qeth_card *card = (struct qeth_card *)card_ptr;
3745
3746        napi_schedule_irqoff(&card->napi);
3747}
3748
3749int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3750{
3751        int rc;
3752
3753        if (card->options.cq ==  QETH_CQ_NOTAVAILABLE) {
3754                rc = -1;
3755                goto out;
3756        } else {
3757                if (card->options.cq == cq) {
3758                        rc = 0;
3759                        goto out;
3760                }
3761
3762                qeth_free_qdio_queues(card);
3763                card->options.cq = cq;
3764                rc = 0;
3765        }
3766out:
3767        return rc;
3768
3769}
3770EXPORT_SYMBOL_GPL(qeth_configure_cq);
3771
3772static void qeth_qdio_cq_handler(struct qeth_card *card, unsigned int qdio_err,
3773                                 unsigned int queue, int first_element,
3774                                 int count)
3775{
3776        struct qeth_qdio_q *cq = card->qdio.c_q;
3777        int i;
3778        int rc;
3779
3780        QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3781        QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3782        QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3783
3784        if (qdio_err) {
3785                netif_tx_stop_all_queues(card->dev);
3786                qeth_schedule_recovery(card);
3787                return;
3788        }
3789
3790        for (i = first_element; i < first_element + count; ++i) {
3791                struct qdio_buffer *buffer = cq->qdio_bufs[QDIO_BUFNR(i)];
3792                int e = 0;
3793
3794                while ((e < QDIO_MAX_ELEMENTS_PER_BUFFER) &&
3795                       buffer->element[e].addr) {
3796                        unsigned long phys_aob_addr = buffer->element[e].addr;
3797
3798                        qeth_qdio_handle_aob(card, phys_aob_addr);
3799                        ++e;
3800                }
3801                qeth_scrub_qdio_buffer(buffer, QDIO_MAX_ELEMENTS_PER_BUFFER);
3802        }
3803        rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, queue,
3804                     cq->next_buf_to_init, count, NULL);
3805        if (rc) {
3806                dev_warn(&card->gdev->dev,
3807                        "QDIO reported an error, rc=%i\n", rc);
3808                QETH_CARD_TEXT(card, 2, "qcqherr");
3809        }
3810
3811        cq->next_buf_to_init = QDIO_BUFNR(cq->next_buf_to_init + count);
3812}
3813
3814static void qeth_qdio_input_handler(struct ccw_device *ccwdev,
3815                                    unsigned int qdio_err, int queue,
3816                                    int first_elem, int count,
3817                                    unsigned long card_ptr)
3818{
3819        struct qeth_card *card = (struct qeth_card *)card_ptr;
3820
3821        QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3822        QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3823
3824        if (qdio_err)
3825                qeth_schedule_recovery(card);
3826}
3827
3828static void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3829                                     unsigned int qdio_error, int __queue,
3830                                     int first_element, int count,
3831                                     unsigned long card_ptr)
3832{
3833        struct qeth_card *card        = (struct qeth_card *) card_ptr;
3834        struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3835        struct net_device *dev = card->dev;
3836        struct netdev_queue *txq;
3837        int i;
3838
3839        QETH_CARD_TEXT(card, 6, "qdouhdl");
3840        if (qdio_error & QDIO_ERROR_FATAL) {
3841                QETH_CARD_TEXT(card, 2, "achkcond");
3842                netif_tx_stop_all_queues(dev);
3843                qeth_schedule_recovery(card);
3844                return;
3845        }
3846
3847        for (i = first_element; i < (first_element + count); ++i) {
3848                struct qeth_qdio_out_buffer *buf = queue->bufs[QDIO_BUFNR(i)];
3849
3850                qeth_handle_send_error(card, buf, qdio_error);
3851                qeth_clear_output_buffer(queue, buf, qdio_error, 0);
3852        }
3853
3854        atomic_sub(count, &queue->used_buffers);
3855        qeth_check_outbound_queue(queue);
3856
3857        txq = netdev_get_tx_queue(dev, __queue);
3858        /* xmit may have observed the full-condition, but not yet stopped the
3859         * txq. In which case the code below won't trigger. So before returning,
3860         * xmit will re-check the txq's fill level and wake it up if needed.
3861         */
3862        if (netif_tx_queue_stopped(txq) && !qeth_out_queue_is_full(queue))
3863                netif_tx_wake_queue(txq);
3864}
3865
3866/**
3867 * Note: Function assumes that we have 4 outbound queues.
3868 */
3869int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb)
3870{
3871        struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
3872        u8 tos;
3873
3874        switch (card->qdio.do_prio_queueing) {
3875        case QETH_PRIO_Q_ING_TOS:
3876        case QETH_PRIO_Q_ING_PREC:
3877                switch (vlan_get_protocol(skb)) {
3878                case htons(ETH_P_IP):
3879                        tos = ipv4_get_dsfield(ip_hdr(skb));
3880                        break;
3881                case htons(ETH_P_IPV6):
3882                        tos = ipv6_get_dsfield(ipv6_hdr(skb));
3883                        break;
3884                default:
3885                        return card->qdio.default_out_queue;
3886                }
3887                if (card->qdio.do_prio_queueing == QETH_PRIO_Q_ING_PREC)
3888                        return ~tos >> 6 & 3;
3889                if (tos & IPTOS_MINCOST)
3890                        return 3;
3891                if (tos & IPTOS_RELIABILITY)
3892                        return 2;
3893                if (tos & IPTOS_THROUGHPUT)
3894                        return 1;
3895                if (tos & IPTOS_LOWDELAY)
3896                        return 0;
3897                break;
3898        case QETH_PRIO_Q_ING_SKB:
3899                if (skb->