linux/drivers/scsi/cxgbi/libcxgbi.c
<<
>>
Prefs
   1/*
   2 * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
   3 *
   4 * Copyright (c) 2010-2015 Chelsio Communications, Inc.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation.
   9 *
  10 * Written by: Karen Xie (kxie@chelsio.com)
  11 * Written by: Rakesh Ranjan (rranjan@chelsio.com)
  12 */
  13
  14#define pr_fmt(fmt)     KBUILD_MODNAME ":%s: " fmt, __func__
  15
  16#include <linux/skbuff.h>
  17#include <linux/crypto.h>
  18#include <linux/scatterlist.h>
  19#include <linux/pci.h>
  20#include <scsi/scsi.h>
  21#include <scsi/scsi_cmnd.h>
  22#include <scsi/scsi_host.h>
  23#include <linux/if_vlan.h>
  24#include <linux/inet.h>
  25#include <net/dst.h>
  26#include <net/route.h>
  27#include <net/ipv6.h>
  28#include <net/ip6_route.h>
  29#include <net/addrconf.h>
  30
  31#include <linux/inetdevice.h>   /* ip_dev_find */
  32#include <linux/module.h>
  33#include <net/tcp.h>
  34
  35static unsigned int dbg_level;
  36
  37#include "libcxgbi.h"
  38
  39#define DRV_MODULE_NAME         "libcxgbi"
  40#define DRV_MODULE_DESC         "Chelsio iSCSI driver library"
  41#define DRV_MODULE_VERSION      "0.9.1-ko"
  42#define DRV_MODULE_RELDATE      "Apr. 2015"
  43
  44static char version[] =
  45        DRV_MODULE_DESC " " DRV_MODULE_NAME
  46        " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  47
  48MODULE_AUTHOR("Chelsio Communications, Inc.");
  49MODULE_DESCRIPTION(DRV_MODULE_DESC);
  50MODULE_VERSION(DRV_MODULE_VERSION);
  51MODULE_LICENSE("GPL");
  52
  53module_param(dbg_level, uint, 0644);
  54MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
  55
  56
  57/*
  58 * cxgbi device management
  59 * maintains a list of the cxgbi devices
  60 */
  61static LIST_HEAD(cdev_list);
  62static DEFINE_MUTEX(cdev_mutex);
  63
  64static LIST_HEAD(cdev_rcu_list);
  65static DEFINE_SPINLOCK(cdev_rcu_lock);
  66
  67static inline void cxgbi_decode_sw_tag(u32 sw_tag, int *idx, int *age)
  68{
  69        if (age)
  70                *age = sw_tag & 0x7FFF;
  71        if (idx)
  72                *idx = (sw_tag >> 16) & 0x7FFF;
  73}
  74
  75int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
  76                                unsigned int max_conn)
  77{
  78        struct cxgbi_ports_map *pmap = &cdev->pmap;
  79
  80        pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
  81                                             sizeof(struct cxgbi_sock *),
  82                                             GFP_KERNEL);
  83        if (!pmap->port_csk) {
  84                pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
  85                return -ENOMEM;
  86        }
  87
  88        pmap->max_connect = max_conn;
  89        pmap->sport_base = base;
  90        spin_lock_init(&pmap->lock);
  91        return 0;
  92}
  93EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
  94
  95void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
  96{
  97        struct cxgbi_ports_map *pmap = &cdev->pmap;
  98        struct cxgbi_sock *csk;
  99        int i;
 100
 101        for (i = 0; i < pmap->max_connect; i++) {
 102                if (pmap->port_csk[i]) {
 103                        csk = pmap->port_csk[i];
 104                        pmap->port_csk[i] = NULL;
 105                        log_debug(1 << CXGBI_DBG_SOCK,
 106                                "csk 0x%p, cdev 0x%p, offload down.\n",
 107                                csk, cdev);
 108                        spin_lock_bh(&csk->lock);
 109                        cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
 110                        cxgbi_sock_closed(csk);
 111                        spin_unlock_bh(&csk->lock);
 112                        cxgbi_sock_put(csk);
 113                }
 114        }
 115}
 116EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
 117
 118static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
 119{
 120        log_debug(1 << CXGBI_DBG_DEV,
 121                "cdev 0x%p, p# %u.\n", cdev, cdev->nports);
 122        cxgbi_hbas_remove(cdev);
 123        cxgbi_device_portmap_cleanup(cdev);
 124        cxgbi_ppm_release(cdev->cdev2ppm(cdev));
 125        if (cdev->pmap.max_connect)
 126                cxgbi_free_big_mem(cdev->pmap.port_csk);
 127        kfree(cdev);
 128}
 129
 130struct cxgbi_device *cxgbi_device_register(unsigned int extra,
 131                                           unsigned int nports)
 132{
 133        struct cxgbi_device *cdev;
 134
 135        cdev = kzalloc(sizeof(*cdev) + extra + nports *
 136                        (sizeof(struct cxgbi_hba *) +
 137                         sizeof(struct net_device *)),
 138                        GFP_KERNEL);
 139        if (!cdev) {
 140                pr_warn("nport %d, OOM.\n", nports);
 141                return NULL;
 142        }
 143        cdev->ports = (struct net_device **)(cdev + 1);
 144        cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
 145                                                sizeof(struct net_device *));
 146        if (extra)
 147                cdev->dd_data = ((char *)cdev->hbas) +
 148                                nports * sizeof(struct cxgbi_hba *);
 149        spin_lock_init(&cdev->pmap.lock);
 150
 151        mutex_lock(&cdev_mutex);
 152        list_add_tail(&cdev->list_head, &cdev_list);
 153        mutex_unlock(&cdev_mutex);
 154
 155        spin_lock(&cdev_rcu_lock);
 156        list_add_tail_rcu(&cdev->rcu_node, &cdev_rcu_list);
 157        spin_unlock(&cdev_rcu_lock);
 158
 159        log_debug(1 << CXGBI_DBG_DEV,
 160                "cdev 0x%p, p# %u.\n", cdev, nports);
 161        return cdev;
 162}
 163EXPORT_SYMBOL_GPL(cxgbi_device_register);
 164
 165void cxgbi_device_unregister(struct cxgbi_device *cdev)
 166{
 167        log_debug(1 << CXGBI_DBG_DEV,
 168                "cdev 0x%p, p# %u,%s.\n",
 169                cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
 170
 171        mutex_lock(&cdev_mutex);
 172        list_del(&cdev->list_head);
 173        mutex_unlock(&cdev_mutex);
 174
 175        spin_lock(&cdev_rcu_lock);
 176        list_del_rcu(&cdev->rcu_node);
 177        spin_unlock(&cdev_rcu_lock);
 178        synchronize_rcu();
 179
 180        cxgbi_device_destroy(cdev);
 181}
 182EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
 183
 184void cxgbi_device_unregister_all(unsigned int flag)
 185{
 186        struct cxgbi_device *cdev, *tmp;
 187
 188        mutex_lock(&cdev_mutex);
 189        list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
 190                if ((cdev->flags & flag) == flag) {
 191                        mutex_unlock(&cdev_mutex);
 192                        cxgbi_device_unregister(cdev);
 193                        mutex_lock(&cdev_mutex);
 194                }
 195        }
 196        mutex_unlock(&cdev_mutex);
 197}
 198EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
 199
 200struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
 201{
 202        struct cxgbi_device *cdev, *tmp;
 203
 204        mutex_lock(&cdev_mutex);
 205        list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
 206                if (cdev->lldev == lldev) {
 207                        mutex_unlock(&cdev_mutex);
 208                        return cdev;
 209                }
 210        }
 211        mutex_unlock(&cdev_mutex);
 212
 213        log_debug(1 << CXGBI_DBG_DEV,
 214                "lldev 0x%p, NO match found.\n", lldev);
 215        return NULL;
 216}
 217EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
 218
 219struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
 220                                                 int *port)
 221{
 222        struct net_device *vdev = NULL;
 223        struct cxgbi_device *cdev, *tmp;
 224        int i;
 225
 226        if (is_vlan_dev(ndev)) {
 227                vdev = ndev;
 228                ndev = vlan_dev_real_dev(ndev);
 229                log_debug(1 << CXGBI_DBG_DEV,
 230                        "vlan dev %s -> %s.\n", vdev->name, ndev->name);
 231        }
 232
 233        mutex_lock(&cdev_mutex);
 234        list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
 235                for (i = 0; i < cdev->nports; i++) {
 236                        if (ndev == cdev->ports[i]) {
 237                                cdev->hbas[i]->vdev = vdev;
 238                                mutex_unlock(&cdev_mutex);
 239                                if (port)
 240                                        *port = i;
 241                                return cdev;
 242                        }
 243                }
 244        }
 245        mutex_unlock(&cdev_mutex);
 246        log_debug(1 << CXGBI_DBG_DEV,
 247                "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
 248        return NULL;
 249}
 250EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev);
 251
 252struct cxgbi_device *cxgbi_device_find_by_netdev_rcu(struct net_device *ndev,
 253                                                     int *port)
 254{
 255        struct net_device *vdev = NULL;
 256        struct cxgbi_device *cdev;
 257        int i;
 258
 259        if (is_vlan_dev(ndev)) {
 260                vdev = ndev;
 261                ndev = vlan_dev_real_dev(ndev);
 262                pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
 263        }
 264
 265        rcu_read_lock();
 266        list_for_each_entry_rcu(cdev, &cdev_rcu_list, rcu_node) {
 267                for (i = 0; i < cdev->nports; i++) {
 268                        if (ndev == cdev->ports[i]) {
 269                                cdev->hbas[i]->vdev = vdev;
 270                                rcu_read_unlock();
 271                                if (port)
 272                                        *port = i;
 273                                return cdev;
 274                        }
 275                }
 276        }
 277        rcu_read_unlock();
 278
 279        log_debug(1 << CXGBI_DBG_DEV,
 280                  "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
 281        return NULL;
 282}
 283EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev_rcu);
 284
 285#if IS_ENABLED(CONFIG_IPV6)
 286static struct cxgbi_device *cxgbi_device_find_by_mac(struct net_device *ndev,
 287                                                     int *port)
 288{
 289        struct net_device *vdev = NULL;
 290        struct cxgbi_device *cdev, *tmp;
 291        int i;
 292
 293        if (is_vlan_dev(ndev)) {
 294                vdev = ndev;
 295                ndev = vlan_dev_real_dev(ndev);
 296                pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
 297        }
 298
 299        mutex_lock(&cdev_mutex);
 300        list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
 301                for (i = 0; i < cdev->nports; i++) {
 302                        if (!memcmp(ndev->dev_addr, cdev->ports[i]->dev_addr,
 303                                    MAX_ADDR_LEN)) {
 304                                cdev->hbas[i]->vdev = vdev;
 305                                mutex_unlock(&cdev_mutex);
 306                                if (port)
 307                                        *port = i;
 308                                return cdev;
 309                        }
 310                }
 311        }
 312        mutex_unlock(&cdev_mutex);
 313        log_debug(1 << CXGBI_DBG_DEV,
 314                  "ndev 0x%p, %s, NO match mac found.\n",
 315                  ndev, ndev->name);
 316        return NULL;
 317}
 318#endif
 319
 320void cxgbi_hbas_remove(struct cxgbi_device *cdev)
 321{
 322        int i;
 323        struct cxgbi_hba *chba;
 324
 325        log_debug(1 << CXGBI_DBG_DEV,
 326                "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
 327
 328        for (i = 0; i < cdev->nports; i++) {
 329                chba = cdev->hbas[i];
 330                if (chba) {
 331                        cdev->hbas[i] = NULL;
 332                        iscsi_host_remove(chba->shost);
 333                        pci_dev_put(cdev->pdev);
 334                        iscsi_host_free(chba->shost);
 335                }
 336        }
 337}
 338EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
 339
 340int cxgbi_hbas_add(struct cxgbi_device *cdev, u64 max_lun,
 341                unsigned int max_id, struct scsi_host_template *sht,
 342                struct scsi_transport_template *stt)
 343{
 344        struct cxgbi_hba *chba;
 345        struct Scsi_Host *shost;
 346        int i, err;
 347
 348        log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
 349
 350        for (i = 0; i < cdev->nports; i++) {
 351                shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
 352                if (!shost) {
 353                        pr_info("0x%p, p%d, %s, host alloc failed.\n",
 354                                cdev, i, cdev->ports[i]->name);
 355                        err = -ENOMEM;
 356                        goto err_out;
 357                }
 358
 359                shost->transportt = stt;
 360                shost->max_lun = max_lun;
 361                shost->max_id = max_id;
 362                shost->max_channel = 0;
 363                shost->max_cmd_len = 16;
 364
 365                chba = iscsi_host_priv(shost);
 366                chba->cdev = cdev;
 367                chba->ndev = cdev->ports[i];
 368                chba->shost = shost;
 369
 370                log_debug(1 << CXGBI_DBG_DEV,
 371                        "cdev 0x%p, p#%d %s: chba 0x%p.\n",
 372                        cdev, i, cdev->ports[i]->name, chba);
 373
 374                pci_dev_get(cdev->pdev);
 375                err = iscsi_host_add(shost, &cdev->pdev->dev);
 376                if (err) {
 377                        pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
 378                                cdev, i, cdev->ports[i]->name);
 379                        pci_dev_put(cdev->pdev);
 380                        scsi_host_put(shost);
 381                        goto  err_out;
 382                }
 383
 384                cdev->hbas[i] = chba;
 385        }
 386
 387        return 0;
 388
 389err_out:
 390        cxgbi_hbas_remove(cdev);
 391        return err;
 392}
 393EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
 394
 395/*
 396 * iSCSI offload
 397 *
 398 * - source port management
 399 *   To find a free source port in the port allocation map we use a very simple
 400 *   rotor scheme to look for the next free port.
 401 *
 402 *   If a source port has been specified make sure that it doesn't collide with
 403 *   our normal source port allocation map.  If it's outside the range of our
 404 *   allocation/deallocation scheme just let them use it.
 405 *
 406 *   If the source port is outside our allocation range, the caller is
 407 *   responsible for keeping track of their port usage.
 408 */
 409
 410static struct cxgbi_sock *find_sock_on_port(struct cxgbi_device *cdev,
 411                                            unsigned char port_id)
 412{
 413        struct cxgbi_ports_map *pmap = &cdev->pmap;
 414        unsigned int i;
 415        unsigned int used;
 416
 417        if (!pmap->max_connect || !pmap->used)
 418                return NULL;
 419
 420        spin_lock_bh(&pmap->lock);
 421        used = pmap->used;
 422        for (i = 0; used && i < pmap->max_connect; i++) {
 423                struct cxgbi_sock *csk = pmap->port_csk[i];
 424
 425                if (csk) {
 426                        if (csk->port_id == port_id) {
 427                                spin_unlock_bh(&pmap->lock);
 428                                return csk;
 429                        }
 430                        used--;
 431                }
 432        }
 433        spin_unlock_bh(&pmap->lock);
 434
 435        return NULL;
 436}
 437
 438static int sock_get_port(struct cxgbi_sock *csk)
 439{
 440        struct cxgbi_device *cdev = csk->cdev;
 441        struct cxgbi_ports_map *pmap = &cdev->pmap;
 442        unsigned int start;
 443        int idx;
 444        __be16 *port;
 445
 446        if (!pmap->max_connect) {
 447                pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
 448                           cdev, csk->port_id, cdev->ports[csk->port_id]->name);
 449                return -EADDRNOTAVAIL;
 450        }
 451
 452        if (csk->csk_family == AF_INET)
 453                port = &csk->saddr.sin_port;
 454        else /* ipv6 */
 455                port = &csk->saddr6.sin6_port;
 456
 457        if (*port) {
 458                pr_err("source port NON-ZERO %u.\n",
 459                        ntohs(*port));
 460                return -EADDRINUSE;
 461        }
 462
 463        spin_lock_bh(&pmap->lock);
 464        if (pmap->used >= pmap->max_connect) {
 465                spin_unlock_bh(&pmap->lock);
 466                pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
 467                        cdev, csk->port_id, cdev->ports[csk->port_id]->name);
 468                return -EADDRNOTAVAIL;
 469        }
 470
 471        start = idx = pmap->next;
 472        do {
 473                if (++idx >= pmap->max_connect)
 474                        idx = 0;
 475                if (!pmap->port_csk[idx]) {
 476                        pmap->used++;
 477                        *port = htons(pmap->sport_base + idx);
 478                        pmap->next = idx;
 479                        pmap->port_csk[idx] = csk;
 480                        spin_unlock_bh(&pmap->lock);
 481                        cxgbi_sock_get(csk);
 482                        log_debug(1 << CXGBI_DBG_SOCK,
 483                                "cdev 0x%p, p#%u %s, p %u, %u.\n",
 484                                cdev, csk->port_id,
 485                                cdev->ports[csk->port_id]->name,
 486                                pmap->sport_base + idx, pmap->next);
 487                        return 0;
 488                }
 489        } while (idx != start);
 490        spin_unlock_bh(&pmap->lock);
 491
 492        /* should not happen */
 493        pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
 494                cdev, csk->port_id, cdev->ports[csk->port_id]->name,
 495                pmap->next);
 496        return -EADDRNOTAVAIL;
 497}
 498
 499static void sock_put_port(struct cxgbi_sock *csk)
 500{
 501        struct cxgbi_device *cdev = csk->cdev;
 502        struct cxgbi_ports_map *pmap = &cdev->pmap;
 503        __be16 *port;
 504
 505        if (csk->csk_family == AF_INET)
 506                port = &csk->saddr.sin_port;
 507        else /* ipv6 */
 508                port = &csk->saddr6.sin6_port;
 509
 510        if (*port) {
 511                int idx = ntohs(*port) - pmap->sport_base;
 512
 513                *port = 0;
 514                if (idx < 0 || idx >= pmap->max_connect) {
 515                        pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
 516                                cdev, csk->port_id,
 517                                cdev->ports[csk->port_id]->name,
 518                                ntohs(*port));
 519                        return;
 520                }
 521
 522                spin_lock_bh(&pmap->lock);
 523                pmap->port_csk[idx] = NULL;
 524                pmap->used--;
 525                spin_unlock_bh(&pmap->lock);
 526
 527                log_debug(1 << CXGBI_DBG_SOCK,
 528                        "cdev 0x%p, p#%u %s, release %u.\n",
 529                        cdev, csk->port_id, cdev->ports[csk->port_id]->name,
 530                        pmap->sport_base + idx);
 531
 532                cxgbi_sock_put(csk);
 533        }
 534}
 535
 536/*
 537 * iscsi tcp connection
 538 */
 539void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
 540{
 541        if (csk->cpl_close) {
 542                kfree_skb(csk->cpl_close);
 543                csk->cpl_close = NULL;
 544        }
 545        if (csk->cpl_abort_req) {
 546                kfree_skb(csk->cpl_abort_req);
 547                csk->cpl_abort_req = NULL;
 548        }
 549        if (csk->cpl_abort_rpl) {
 550                kfree_skb(csk->cpl_abort_rpl);
 551                csk->cpl_abort_rpl = NULL;
 552        }
 553}
 554EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
 555
 556static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
 557{
 558        struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
 559
 560        if (!csk) {
 561                pr_info("alloc csk %zu failed.\n", sizeof(*csk));
 562                return NULL;
 563        }
 564
 565        if (cdev->csk_alloc_cpls(csk) < 0) {
 566                pr_info("csk 0x%p, alloc cpls failed.\n", csk);
 567                kfree(csk);
 568                return NULL;
 569        }
 570
 571        spin_lock_init(&csk->lock);
 572        kref_init(&csk->refcnt);
 573        skb_queue_head_init(&csk->receive_queue);
 574        skb_queue_head_init(&csk->write_queue);
 575        setup_timer(&csk->retry_timer, NULL, (unsigned long)csk);
 576        rwlock_init(&csk->callback_lock);
 577        csk->cdev = cdev;
 578        csk->flags = 0;
 579        cxgbi_sock_set_state(csk, CTP_CLOSED);
 580
 581        log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
 582
 583        return csk;
 584}
 585
 586static struct rtable *find_route_ipv4(struct flowi4 *fl4,
 587                                      __be32 saddr, __be32 daddr,
 588                                      __be16 sport, __be16 dport, u8 tos,
 589                                      int ifindex)
 590{
 591        struct rtable *rt;
 592
 593        rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
 594                                   dport, sport, IPPROTO_TCP, tos, ifindex);
 595        if (IS_ERR(rt))
 596                return NULL;
 597
 598        return rt;
 599}
 600
 601static struct cxgbi_sock *
 602cxgbi_check_route(struct sockaddr *dst_addr, int ifindex)
 603{
 604        struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
 605        struct dst_entry *dst;
 606        struct net_device *ndev;
 607        struct cxgbi_device *cdev;
 608        struct rtable *rt = NULL;
 609        struct neighbour *n;
 610        struct flowi4 fl4;
 611        struct cxgbi_sock *csk = NULL;
 612        unsigned int mtu = 0;
 613        int port = 0xFFFF;
 614        int err = 0;
 615
 616        rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0,
 617                             daddr->sin_port, 0, ifindex);
 618        if (!rt) {
 619                pr_info("no route to ipv4 0x%x, port %u.\n",
 620                        be32_to_cpu(daddr->sin_addr.s_addr),
 621                        be16_to_cpu(daddr->sin_port));
 622                err = -ENETUNREACH;
 623                goto err_out;
 624        }
 625        dst = &rt->dst;
 626        n = dst_neigh_lookup(dst, &daddr->sin_addr.s_addr);
 627        if (!n) {
 628                err = -ENODEV;
 629                goto rel_rt;
 630        }
 631        ndev = n->dev;
 632
 633        if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
 634                pr_info("multi-cast route %pI4, port %u, dev %s.\n",
 635                        &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
 636                        ndev->name);
 637                err = -ENETUNREACH;
 638                goto rel_neigh;
 639        }
 640
 641        if (ndev->flags & IFF_LOOPBACK) {
 642                ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
 643                mtu = ndev->mtu;
 644                pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
 645                        n->dev->name, ndev->name, mtu);
 646        }
 647
 648        if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
 649                pr_info("%s interface not up.\n", ndev->name);
 650                err = -ENETDOWN;
 651                goto rel_neigh;
 652        }
 653
 654        cdev = cxgbi_device_find_by_netdev(ndev, &port);
 655        if (!cdev) {
 656                pr_info("dst %pI4, %s, NOT cxgbi device.\n",
 657                        &daddr->sin_addr.s_addr, ndev->name);
 658                err = -ENETUNREACH;
 659                goto rel_neigh;
 660        }
 661        log_debug(1 << CXGBI_DBG_SOCK,
 662                "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
 663                &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
 664                           port, ndev->name, cdev);
 665
 666        csk = cxgbi_sock_create(cdev);
 667        if (!csk) {
 668                err = -ENOMEM;
 669                goto rel_neigh;
 670        }
 671        csk->cdev = cdev;
 672        csk->port_id = port;
 673        csk->mtu = mtu;
 674        csk->dst = dst;
 675
 676        csk->csk_family = AF_INET;
 677        csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
 678        csk->daddr.sin_port = daddr->sin_port;
 679        csk->daddr.sin_family = daddr->sin_family;
 680        csk->saddr.sin_family = daddr->sin_family;
 681        csk->saddr.sin_addr.s_addr = fl4.saddr;
 682        neigh_release(n);
 683
 684        return csk;
 685
 686rel_neigh:
 687        neigh_release(n);
 688
 689rel_rt:
 690        ip_rt_put(rt);
 691        if (csk)
 692                cxgbi_sock_closed(csk);
 693err_out:
 694        return ERR_PTR(err);
 695}
 696
 697#if IS_ENABLED(CONFIG_IPV6)
 698static struct rt6_info *find_route_ipv6(const struct in6_addr *saddr,
 699                                        const struct in6_addr *daddr,
 700                                        int ifindex)
 701{
 702        struct flowi6 fl;
 703
 704        memset(&fl, 0, sizeof(fl));
 705        fl.flowi6_oif = ifindex;
 706        if (saddr)
 707                memcpy(&fl.saddr, saddr, sizeof(struct in6_addr));
 708        if (daddr)
 709                memcpy(&fl.daddr, daddr, sizeof(struct in6_addr));
 710        return (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
 711}
 712
 713static struct cxgbi_sock *
 714cxgbi_check_route6(struct sockaddr *dst_addr, int ifindex)
 715{
 716        struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)dst_addr;
 717        struct dst_entry *dst;
 718        struct net_device *ndev;
 719        struct cxgbi_device *cdev;
 720        struct rt6_info *rt = NULL;
 721        struct neighbour *n;
 722        struct in6_addr pref_saddr;
 723        struct cxgbi_sock *csk = NULL;
 724        unsigned int mtu = 0;
 725        int port = 0xFFFF;
 726        int err = 0;
 727
 728        rt = find_route_ipv6(NULL, &daddr6->sin6_addr, ifindex);
 729
 730        if (!rt) {
 731                pr_info("no route to ipv6 %pI6 port %u\n",
 732                        daddr6->sin6_addr.s6_addr,
 733                        be16_to_cpu(daddr6->sin6_port));
 734                err = -ENETUNREACH;
 735                goto err_out;
 736        }
 737
 738        dst = &rt->dst;
 739
 740        n = dst_neigh_lookup(dst, &daddr6->sin6_addr);
 741
 742        if (!n) {
 743                pr_info("%pI6, port %u, dst no neighbour.\n",
 744                        daddr6->sin6_addr.s6_addr,
 745                        be16_to_cpu(daddr6->sin6_port));
 746                err = -ENETUNREACH;
 747                goto rel_rt;
 748        }
 749        ndev = n->dev;
 750
 751        if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
 752                pr_info("%s interface not up.\n", ndev->name);
 753                err = -ENETDOWN;
 754                goto rel_rt;
 755        }
 756
 757        if (ipv6_addr_is_multicast(&daddr6->sin6_addr)) {
 758                pr_info("multi-cast route %pI6 port %u, dev %s.\n",
 759                        daddr6->sin6_addr.s6_addr,
 760                        ntohs(daddr6->sin6_port), ndev->name);
 761                err = -ENETUNREACH;
 762                goto rel_rt;
 763        }
 764
 765        cdev = cxgbi_device_find_by_netdev(ndev, &port);
 766        if (!cdev)
 767                cdev = cxgbi_device_find_by_mac(ndev, &port);
 768        if (!cdev) {
 769                pr_info("dst %pI6 %s, NOT cxgbi device.\n",
 770                        daddr6->sin6_addr.s6_addr, ndev->name);
 771                err = -ENETUNREACH;
 772                goto rel_rt;
 773        }
 774        log_debug(1 << CXGBI_DBG_SOCK,
 775                  "route to %pI6 :%u, ndev p#%d,%s, cdev 0x%p.\n",
 776                  daddr6->sin6_addr.s6_addr, ntohs(daddr6->sin6_port), port,
 777                  ndev->name, cdev);
 778
 779        csk = cxgbi_sock_create(cdev);
 780        if (!csk) {
 781                err = -ENOMEM;
 782                goto rel_rt;
 783        }
 784        csk->cdev = cdev;
 785        csk->port_id = port;
 786        csk->mtu = mtu;
 787        csk->dst = dst;
 788
 789        if (ipv6_addr_any(&rt->rt6i_prefsrc.addr)) {
 790                struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
 791
 792                err = ipv6_dev_get_saddr(&init_net, idev ? idev->dev : NULL,
 793                                         &daddr6->sin6_addr, 0, &pref_saddr);
 794                if (err) {
 795                        pr_info("failed to get source address to reach %pI6\n",
 796                                &daddr6->sin6_addr);
 797                        goto rel_rt;
 798                }
 799        } else {
 800                pref_saddr = rt->rt6i_prefsrc.addr;
 801        }
 802
 803        csk->csk_family = AF_INET6;
 804        csk->daddr6.sin6_addr = daddr6->sin6_addr;
 805        csk->daddr6.sin6_port = daddr6->sin6_port;
 806        csk->daddr6.sin6_family = daddr6->sin6_family;
 807        csk->saddr6.sin6_family = daddr6->sin6_family;
 808        csk->saddr6.sin6_addr = pref_saddr;
 809
 810        neigh_release(n);
 811        return csk;
 812
 813rel_rt:
 814        if (n)
 815                neigh_release(n);
 816
 817        ip6_rt_put(rt);
 818        if (csk)
 819                cxgbi_sock_closed(csk);
 820err_out:
 821        return ERR_PTR(err);
 822}
 823#endif /* IS_ENABLED(CONFIG_IPV6) */
 824
 825void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
 826                        unsigned int opt)
 827{
 828        csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
 829        dst_confirm(csk->dst);
 830        smp_mb();
 831        cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
 832}
 833EXPORT_SYMBOL_GPL(cxgbi_sock_established);
 834
 835static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
 836{
 837        log_debug(1 << CXGBI_DBG_SOCK,
 838                "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
 839                csk, csk->state, csk->flags, csk->user_data);
 840
 841        if (csk->state != CTP_ESTABLISHED) {
 842                read_lock_bh(&csk->callback_lock);
 843                if (csk->user_data)
 844                        iscsi_conn_failure(csk->user_data,
 845                                        ISCSI_ERR_TCP_CONN_CLOSE);
 846                read_unlock_bh(&csk->callback_lock);
 847        }
 848}
 849
 850void cxgbi_sock_closed(struct cxgbi_sock *csk)
 851{
 852        log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
 853                csk, (csk)->state, (csk)->flags, (csk)->tid);
 854        cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
 855        if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
 856                return;
 857        if (csk->saddr.sin_port)
 858                sock_put_port(csk);
 859        if (csk->dst)
 860                dst_release(csk->dst);
 861        csk->cdev->csk_release_offload_resources(csk);
 862        cxgbi_sock_set_state(csk, CTP_CLOSED);
 863        cxgbi_inform_iscsi_conn_closing(csk);
 864        cxgbi_sock_put(csk);
 865}
 866EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
 867
 868static void need_active_close(struct cxgbi_sock *csk)
 869{
 870        int data_lost;
 871        int close_req = 0;
 872
 873        log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
 874                csk, (csk)->state, (csk)->flags, (csk)->tid);
 875        spin_lock_bh(&csk->lock);
 876        if (csk->dst)
 877                dst_confirm(csk->dst);
 878        data_lost = skb_queue_len(&csk->receive_queue);
 879        __skb_queue_purge(&csk->receive_queue);
 880
 881        if (csk->state == CTP_ACTIVE_OPEN)
 882                cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
 883        else if (csk->state == CTP_ESTABLISHED) {
 884                close_req = 1;
 885                cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
 886        } else if (csk->state == CTP_PASSIVE_CLOSE) {
 887                close_req = 1;
 888                cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
 889        }
 890
 891        if (close_req) {
 892                if (!cxgbi_sock_flag(csk, CTPF_LOGOUT_RSP_RCVD) ||
 893                    data_lost)
 894                        csk->cdev->csk_send_abort_req(csk);
 895                else
 896                        csk->cdev->csk_send_close_req(csk);
 897        }
 898
 899        spin_unlock_bh(&csk->lock);
 900}
 901
 902void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
 903{
 904        pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
 905                        csk, csk->state, csk->flags,
 906                        &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
 907                        &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
 908                        errno);
 909
 910        cxgbi_sock_set_state(csk, CTP_CONNECTING);
 911        csk->err = errno;
 912        cxgbi_sock_closed(csk);
 913}
 914EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
 915
 916void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
 917{
 918        struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
 919        struct module *owner = csk->cdev->owner;
 920
 921        log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
 922                csk, (csk)->state, (csk)->flags, (csk)->tid);
 923        cxgbi_sock_get(csk);
 924        spin_lock_bh(&csk->lock);
 925        if (csk->state == CTP_ACTIVE_OPEN)
 926                cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
 927        spin_unlock_bh(&csk->lock);
 928        cxgbi_sock_put(csk);
 929        __kfree_skb(skb);
 930
 931        module_put(owner);
 932}
 933EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
 934
 935void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
 936{
 937        cxgbi_sock_get(csk);
 938        spin_lock_bh(&csk->lock);
 939
 940        cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
 941        if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
 942                cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
 943                if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
 944                        pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
 945                               csk, csk->state, csk->flags, csk->tid);
 946                cxgbi_sock_closed(csk);
 947        }
 948
 949        spin_unlock_bh(&csk->lock);
 950        cxgbi_sock_put(csk);
 951}
 952EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
 953
 954void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
 955{
 956        log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
 957                csk, (csk)->state, (csk)->flags, (csk)->tid);
 958        cxgbi_sock_get(csk);
 959        spin_lock_bh(&csk->lock);
 960
 961        if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
 962                goto done;
 963
 964        switch (csk->state) {
 965        case CTP_ESTABLISHED:
 966                cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
 967                break;
 968        case CTP_ACTIVE_CLOSE:
 969                cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
 970                break;
 971        case CTP_CLOSE_WAIT_1:
 972                cxgbi_sock_closed(csk);
 973                break;
 974        case CTP_ABORTING:
 975                break;
 976        default:
 977                pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
 978                        csk, csk->state, csk->flags, csk->tid);
 979        }
 980        cxgbi_inform_iscsi_conn_closing(csk);
 981done:
 982        spin_unlock_bh(&csk->lock);
 983        cxgbi_sock_put(csk);
 984}
 985EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
 986
 987void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
 988{
 989        log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
 990                csk, (csk)->state, (csk)->flags, (csk)->tid);
 991        cxgbi_sock_get(csk);
 992        spin_lock_bh(&csk->lock);
 993
 994        csk->snd_una = snd_nxt - 1;
 995        if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
 996                goto done;
 997
 998        switch (csk->state) {
 999        case CTP_ACTIVE_CLOSE:
1000                cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
1001                break;
1002        case CTP_CLOSE_WAIT_1:
1003        case CTP_CLOSE_WAIT_2:
1004                cxgbi_sock_closed(csk);
1005                break;
1006        case CTP_ABORTING:
1007                break;
1008        default:
1009                pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
1010                        csk, csk->state, csk->flags, csk->tid);
1011        }
1012done:
1013        spin_unlock_bh(&csk->lock);
1014        cxgbi_sock_put(csk);
1015}
1016EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
1017
1018void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
1019                           unsigned int snd_una, int seq_chk)
1020{
1021        log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1022                        "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
1023                        csk, csk->state, csk->flags, csk->tid, credits,
1024                        csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
1025
1026        spin_lock_bh(&csk->lock);
1027
1028        csk->wr_cred += credits;
1029        if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
1030                csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1031
1032        while (credits) {
1033                struct sk_buff *p = cxgbi_sock_peek_wr(csk);
1034
1035                if (unlikely(!p)) {
1036                        pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
1037                                csk, csk->state, csk->flags, csk->tid, credits,
1038                                csk->wr_cred, csk->wr_una_cred);
1039                        break;
1040                }
1041
1042                if (unlikely(credits < p->csum)) {
1043                        pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
1044                                csk, csk->state, csk->flags, csk->tid,
1045                                credits, csk->wr_cred, csk->wr_una_cred,
1046                                p->csum);
1047                        p->csum -= credits;
1048                        break;
1049                } else {
1050                        cxgbi_sock_dequeue_wr(csk);
1051                        credits -= p->csum;
1052                        kfree_skb(p);
1053                }
1054        }
1055
1056        cxgbi_sock_check_wr_invariants(csk);
1057
1058        if (seq_chk) {
1059                if (unlikely(before(snd_una, csk->snd_una))) {
1060                        pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
1061                                csk, csk->state, csk->flags, csk->tid, snd_una,
1062                                csk->snd_una);
1063                        goto done;
1064                }
1065
1066                if (csk->snd_una != snd_una) {
1067                        csk->snd_una = snd_una;
1068                        dst_confirm(csk->dst);
1069                }
1070        }
1071
1072        if (skb_queue_len(&csk->write_queue)) {
1073                if (csk->cdev->csk_push_tx_frames(csk, 0))
1074                        cxgbi_conn_tx_open(csk);
1075        } else
1076                cxgbi_conn_tx_open(csk);
1077done:
1078        spin_unlock_bh(&csk->lock);
1079}
1080EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
1081
1082static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
1083                                             unsigned short mtu)
1084{
1085        int i = 0;
1086
1087        while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
1088                ++i;
1089
1090        return i;
1091}
1092
1093unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
1094{
1095        unsigned int idx;
1096        struct dst_entry *dst = csk->dst;
1097
1098        csk->advmss = dst_metric_advmss(dst);
1099
1100        if (csk->advmss > pmtu - 40)
1101                csk->advmss = pmtu - 40;
1102        if (csk->advmss < csk->cdev->mtus[0] - 40)
1103                csk->advmss = csk->cdev->mtus[0] - 40;
1104        idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
1105
1106        return idx;
1107}
1108EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
1109
1110void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
1111{
1112        cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
1113        __skb_queue_tail(&csk->write_queue, skb);
1114}
1115EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
1116
1117void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
1118{
1119        struct sk_buff *skb;
1120
1121        while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
1122                kfree_skb(skb);
1123}
1124EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
1125
1126void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
1127{
1128        int pending = cxgbi_sock_count_pending_wrs(csk);
1129
1130        if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
1131                pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1132                        csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
1133}
1134EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
1135
1136static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
1137{
1138        struct cxgbi_device *cdev = csk->cdev;
1139        struct sk_buff *next;
1140        int err, copied = 0;
1141
1142        spin_lock_bh(&csk->lock);
1143
1144        if (csk->state != CTP_ESTABLISHED) {
1145                log_debug(1 << CXGBI_DBG_PDU_TX,
1146                        "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
1147                        csk, csk->state, csk->flags, csk->tid);
1148                err = -EAGAIN;
1149                goto out_err;
1150        }
1151
1152        if (csk->err) {
1153                log_debug(1 << CXGBI_DBG_PDU_TX,
1154                        "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
1155                        csk, csk->state, csk->flags, csk->tid, csk->err);
1156                err = -EPIPE;
1157                goto out_err;
1158        }
1159
1160        if (csk->write_seq - csk->snd_una >= csk->snd_win) {
1161                log_debug(1 << CXGBI_DBG_PDU_TX,
1162                        "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
1163                        csk, csk->state, csk->flags, csk->tid, csk->write_seq,
1164                        csk->snd_una, csk->snd_win);
1165                err = -ENOBUFS;
1166                goto out_err;
1167        }
1168
1169        while (skb) {
1170                int frags = skb_shinfo(skb)->nr_frags +
1171                                (skb->len != skb->data_len);
1172
1173                if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
1174                        pr_err("csk 0x%p, skb head %u < %u.\n",
1175                                csk, skb_headroom(skb), cdev->skb_tx_rsvd);
1176                        err = -EINVAL;
1177                        goto out_err;
1178                }
1179
1180                if (frags >= SKB_WR_LIST_SIZE) {
1181                        pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
1182                                csk, skb_shinfo(skb)->nr_frags, skb->len,
1183                                skb->data_len, (uint)(SKB_WR_LIST_SIZE));
1184                        err = -EINVAL;
1185                        goto out_err;
1186                }
1187
1188                next = skb->next;
1189                skb->next = NULL;
1190                cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
1191                cxgbi_sock_skb_entail(csk, skb);
1192                copied += skb->len;
1193                csk->write_seq += skb->len +
1194                                cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
1195                skb = next;
1196        }
1197
1198        if (likely(skb_queue_len(&csk->write_queue)))
1199                cdev->csk_push_tx_frames(csk, 1);
1200done:
1201        spin_unlock_bh(&csk->lock);
1202        return copied;
1203
1204out_err:
1205        if (copied == 0 && err == -EPIPE)
1206                copied = csk->err ? csk->err : -EPIPE;
1207        else
1208                copied = err;
1209        goto done;
1210}
1211
1212static inline void
1213scmd_get_params(struct scsi_cmnd *sc, struct scatterlist **sgl,
1214                unsigned int *sgcnt, unsigned int *dlen,
1215                unsigned int prot)
1216{
1217        struct scsi_data_buffer *sdb = prot ? scsi_prot(sc) : scsi_out(sc);
1218
1219        *sgl = sdb->table.sgl;
1220        *sgcnt = sdb->table.nents;
1221        *dlen = sdb->length;
1222        /* Caution: for protection sdb, sdb->length is invalid */
1223}
1224
1225void cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod *ppod,
1226                            struct cxgbi_task_tag_info *ttinfo,
1227                            struct scatterlist **sg_pp, unsigned int *sg_off)
1228{
1229        struct scatterlist *sg = sg_pp ? *sg_pp : NULL;
1230        unsigned int offset = sg_off ? *sg_off : 0;
1231        dma_addr_t addr = 0UL;
1232        unsigned int len = 0;
1233        int i;
1234
1235        memcpy(ppod, &ttinfo->hdr, sizeof(struct cxgbi_pagepod_hdr));
1236
1237        if (sg) {
1238                addr = sg_dma_address(sg);
1239                len = sg_dma_len(sg);
1240        }
1241
1242        for (i = 0; i < PPOD_PAGES_MAX; i++) {
1243                if (sg) {
1244                        ppod->addr[i] = cpu_to_be64(addr + offset);
1245                        offset += PAGE_SIZE;
1246                        if (offset == (len + sg->offset)) {
1247                                offset = 0;
1248                                sg = sg_next(sg);
1249                                if (sg) {
1250                                        addr = sg_dma_address(sg);
1251                                        len = sg_dma_len(sg);
1252                                }
1253                        }
1254                } else {
1255                        ppod->addr[i] = 0ULL;
1256                }
1257        }
1258
1259        /*
1260         * the fifth address needs to be repeated in the next ppod, so do
1261         * not move sg
1262         */
1263        if (sg_pp) {
1264                *sg_pp = sg;
1265                *sg_off = offset;
1266        }
1267
1268        if (offset == len) {
1269                offset = 0;
1270                sg = sg_next(sg);
1271                if (sg) {
1272                        addr = sg_dma_address(sg);
1273                        len = sg_dma_len(sg);
1274                }
1275        }
1276        ppod->addr[i] = sg ? cpu_to_be64(addr + offset) : 0ULL;
1277}
1278EXPORT_SYMBOL_GPL(cxgbi_ddp_set_one_ppod);
1279
1280/*
1281 * APIs interacting with open-iscsi libraries
1282 */
1283
1284static unsigned char padding[4];
1285
1286void cxgbi_ddp_ppm_setup(void **ppm_pp, struct cxgbi_device *cdev,
1287                         struct cxgbi_tag_format *tformat, unsigned int ppmax,
1288                         unsigned int llimit, unsigned int start,
1289                         unsigned int rsvd_factor)
1290{
1291        int err = cxgbi_ppm_init(ppm_pp, cdev->ports[0], cdev->pdev,
1292                                cdev->lldev, tformat, ppmax, llimit, start,
1293                                rsvd_factor);
1294
1295        if (err >= 0) {
1296                struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
1297
1298                if (ppm->ppmax < 1024 ||
1299                    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX)
1300                        cdev->flags |= CXGBI_FLAG_DDP_OFF;
1301                err = 0;
1302        } else {
1303                cdev->flags |= CXGBI_FLAG_DDP_OFF;
1304        }
1305}
1306EXPORT_SYMBOL_GPL(cxgbi_ddp_ppm_setup);
1307
1308static int cxgbi_ddp_sgl_check(struct scatterlist *sgl, int nents)
1309{
1310        int i;
1311        int last_sgidx = nents - 1;
1312        struct scatterlist *sg = sgl;
1313
1314        for (i = 0; i < nents; i++, sg = sg_next(sg)) {
1315                unsigned int len = sg->length + sg->offset;
1316
1317                if ((sg->offset & 0x3) || (i && sg->offset) ||
1318                    ((i != last_sgidx) && len != PAGE_SIZE)) {
1319                        log_debug(1 << CXGBI_DBG_DDP,
1320                                  "sg %u/%u, %u,%u, not aligned.\n",
1321                                  i, nents, sg->offset, sg->length);
1322                        goto err_out;
1323                }
1324        }
1325        return 0;
1326err_out:
1327        return -EINVAL;
1328}
1329
1330static int cxgbi_ddp_reserve(struct cxgbi_conn *cconn,
1331                             struct cxgbi_task_data *tdata, u32 sw_tag,
1332                             unsigned int xferlen)
1333{
1334        struct cxgbi_sock *csk = cconn->cep->csk;
1335        struct cxgbi_device *cdev = csk->cdev;
1336        struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1337        struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1338        struct scatterlist *sgl = ttinfo->sgl;
1339        unsigned int sgcnt = ttinfo->nents;
1340        unsigned int sg_offset = sgl->offset;
1341        int err;
1342
1343        if (cdev->flags & CXGBI_FLAG_DDP_OFF) {
1344                log_debug(1 << CXGBI_DBG_DDP,
1345                          "cdev 0x%p DDP off.\n", cdev);
1346                return -EINVAL;
1347        }
1348
1349        if (!ppm || xferlen < DDP_THRESHOLD || !sgcnt ||
1350            ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX) {
1351                log_debug(1 << CXGBI_DBG_DDP,
1352                          "ppm 0x%p, pgidx %u, xfer %u, sgcnt %u, NO ddp.\n",
1353                          ppm, ppm ? ppm->tformat.pgsz_idx_dflt : DDP_PGIDX_MAX,
1354                          xferlen, ttinfo->nents);
1355                return -EINVAL;
1356        }
1357
1358        /* make sure the buffer is suitable for ddp */
1359        if (cxgbi_ddp_sgl_check(sgl, sgcnt) < 0)
1360                return -EINVAL;
1361
1362        ttinfo->nr_pages = (xferlen + sgl->offset + (1 << PAGE_SHIFT) - 1) >>
1363                            PAGE_SHIFT;
1364
1365        /*
1366         * the ddp tag will be used for the itt in the outgoing pdu,
1367         * the itt genrated by libiscsi is saved in the ppm and can be
1368         * retrieved via the ddp tag
1369         */
1370        err = cxgbi_ppm_ppods_reserve(ppm, ttinfo->nr_pages, 0, &ttinfo->idx,
1371                                      &ttinfo->tag, (unsigned long)sw_tag);
1372        if (err < 0) {
1373                cconn->ddp_full++;
1374                return err;
1375        }
1376        ttinfo->npods = err;
1377
1378         /* setup dma from scsi command sgl */
1379        sgl->offset = 0;
1380        err = dma_map_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
1381        sgl->offset = sg_offset;
1382        if (err == 0) {
1383                pr_info("%s: 0x%x, xfer %u, sgl %u dma mapping err.\n",
1384                        __func__, sw_tag, xferlen, sgcnt);
1385                goto rel_ppods;
1386        }
1387        if (err != ttinfo->nr_pages) {
1388                log_debug(1 << CXGBI_DBG_DDP,
1389                          "%s: sw tag 0x%x, xfer %u, sgl %u, dma count %d.\n",
1390                          __func__, sw_tag, xferlen, sgcnt, err);
1391        }
1392
1393        ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_MAPPED;
1394        ttinfo->cid = csk->port_id;
1395
1396        cxgbi_ppm_make_ppod_hdr(ppm, ttinfo->tag, csk->tid, sgl->offset,
1397                                xferlen, &ttinfo->hdr);
1398
1399        if (cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ) {
1400                /* write ppod from xmit_pdu (of iscsi_scsi_command pdu) */
1401                ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_VALID;
1402        } else {
1403                /* write ppod from control queue now */
1404                err = cdev->csk_ddp_set_map(ppm, csk, ttinfo);
1405                if (err < 0)
1406                        goto rel_ppods;
1407        }
1408
1409        return 0;
1410
1411rel_ppods:
1412        cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
1413
1414        if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_MAPPED) {
1415                ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_MAPPED;
1416                dma_unmap_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
1417        }
1418        return -EINVAL;
1419}
1420
1421static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1422{
1423        struct scsi_cmnd *sc = task->sc;
1424        struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1425        struct cxgbi_conn *cconn = tcp_conn->dd_data;
1426        struct cxgbi_device *cdev = cconn->chba->cdev;
1427        struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1428        u32 tag = ntohl((__force u32)hdr_itt);
1429
1430        log_debug(1 << CXGBI_DBG_DDP,
1431                  "cdev 0x%p, task 0x%p, release tag 0x%x.\n",
1432                  cdev, task, tag);
1433        if (sc &&
1434            (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
1435            cxgbi_ppm_is_ddp_tag(ppm, tag)) {
1436                struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1437                struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1438
1439                if (!(cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ))
1440                        cdev->csk_ddp_clear_map(cdev, ppm, ttinfo);
1441                cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
1442                dma_unmap_sg(&ppm->pdev->dev, ttinfo->sgl, ttinfo->nents,
1443                             DMA_FROM_DEVICE);
1444        }
1445}
1446
1447static inline u32 cxgbi_build_sw_tag(u32 idx, u32 age)
1448{
1449        /* assume idx and age both are < 0x7FFF (32767) */
1450        return (idx << 16) | age;
1451}
1452
1453static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1454{
1455        struct scsi_cmnd *sc = task->sc;
1456        struct iscsi_conn *conn = task->conn;
1457        struct iscsi_session *sess = conn->session;
1458        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1459        struct cxgbi_conn *cconn = tcp_conn->dd_data;
1460        struct cxgbi_device *cdev = cconn->chba->cdev;
1461        struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1462        u32 sw_tag = cxgbi_build_sw_tag(task->itt, sess->age);
1463        u32 tag = 0;
1464        int err = -EINVAL;
1465
1466        if (sc &&
1467            (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)
1468        ) {
1469                struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1470                struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1471
1472                scmd_get_params(sc, &ttinfo->sgl, &ttinfo->nents,
1473                                &tdata->dlen, 0);
1474                err = cxgbi_ddp_reserve(cconn, tdata, sw_tag, tdata->dlen);
1475                if (!err)
1476                        tag = ttinfo->tag;
1477                else
1478                         log_debug(1 << CXGBI_DBG_DDP,
1479                                   "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
1480                                   cconn->cep->csk, task, tdata->dlen,
1481                                   ttinfo->nents);
1482        }
1483
1484        if (err < 0) {
1485                err = cxgbi_ppm_make_non_ddp_tag(ppm, sw_tag, &tag);
1486                if (err < 0)
1487                        return err;
1488        }
1489        /*  the itt need to sent in big-endian order */
1490        *hdr_itt = (__force itt_t)htonl(tag);
1491
1492        log_debug(1 << CXGBI_DBG_DDP,
1493                  "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1494                  cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
1495        return 0;
1496}
1497
1498void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1499{
1500        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1501        struct cxgbi_conn *cconn = tcp_conn->dd_data;
1502        struct cxgbi_device *cdev = cconn->chba->cdev;
1503        struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1504        u32 tag = ntohl((__force u32)itt);
1505        u32 sw_bits;
1506
1507        if (ppm) {
1508                if (cxgbi_ppm_is_ddp_tag(ppm, tag))
1509                        sw_bits = cxgbi_ppm_get_tag_caller_data(ppm, tag);
1510                else
1511                        sw_bits = cxgbi_ppm_decode_non_ddp_tag(ppm, tag);
1512        } else {
1513                sw_bits = tag;
1514        }
1515
1516        cxgbi_decode_sw_tag(sw_bits, idx, age);
1517        log_debug(1 << CXGBI_DBG_DDP,
1518                  "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1519                  cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1520                  age ? *age : 0xFF);
1521}
1522EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1523
1524void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1525{
1526        struct iscsi_conn *conn = csk->user_data;
1527
1528        if (conn) {
1529                log_debug(1 << CXGBI_DBG_SOCK,
1530                        "csk 0x%p, cid %d.\n", csk, conn->id);
1531                iscsi_conn_queue_work(conn);
1532        }
1533}
1534EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1535
1536/*
1537 * pdu receive, interact with libiscsi_tcp
1538 */
1539static inline int read_pdu_skb(struct iscsi_conn *conn,
1540                               struct sk_buff *skb,
1541                               unsigned int offset,
1542                               int offloaded)
1543{
1544        int status = 0;
1545        int bytes_read;
1546
1547        bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1548        switch (status) {
1549        case ISCSI_TCP_CONN_ERR:
1550                pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1551                          skb, offset, offloaded);
1552                return -EIO;
1553        case ISCSI_TCP_SUSPENDED:
1554                log_debug(1 << CXGBI_DBG_PDU_RX,
1555                        "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1556                        skb, offset, offloaded, bytes_read);
1557                /* no transfer - just have caller flush queue */
1558                return bytes_read;
1559        case ISCSI_TCP_SKB_DONE:
1560                pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1561                        skb, offset, offloaded);
1562                /*
1563                 * pdus should always fit in the skb and we should get
1564                 * segment done notifcation.
1565                 */
1566                iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1567                return -EFAULT;
1568        case ISCSI_TCP_SEGMENT_DONE:
1569                log_debug(1 << CXGBI_DBG_PDU_RX,
1570                        "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1571                        skb, offset, offloaded, bytes_read);
1572                return bytes_read;
1573        default:
1574                pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1575                        skb, offset, offloaded, status);
1576                return -EINVAL;
1577        }
1578}
1579
1580static int
1581skb_read_pdu_bhs(struct cxgbi_sock *csk, struct iscsi_conn *conn,
1582                 struct sk_buff *skb)
1583{
1584        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1585        int err;
1586
1587        log_debug(1 << CXGBI_DBG_PDU_RX,
1588                "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1589                conn, skb, skb->len, cxgbi_skcb_flags(skb));
1590
1591        if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1592                pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1593                iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1594                return -EIO;
1595        }
1596
1597        if (conn->hdrdgst_en &&
1598            cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1599                pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1600                iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1601                return -EIO;
1602        }
1603
1604        if (cxgbi_skcb_test_flag(skb, SKCBF_RX_ISCSI_COMPL) &&
1605            cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA_DDPD)) {
1606                /* If completion flag is set and data is directly
1607                 * placed in to the host memory then update
1608                 * task->exp_datasn to the datasn in completion
1609                 * iSCSI hdr as T6 adapter generates completion only
1610                 * for the last pdu of a sequence.
1611                 */
1612                itt_t itt = ((struct iscsi_data *)skb->data)->itt;
1613                struct iscsi_task *task = iscsi_itt_to_ctask(conn, itt);
1614                u32 data_sn = be32_to_cpu(((struct iscsi_data *)
1615                                                        skb->data)->datasn);
1616                if (task && task->sc) {
1617                        struct iscsi_tcp_task *tcp_task = task->dd_data;
1618
1619                        tcp_task->exp_datasn = data_sn;
1620                }
1621        }
1622
1623        err = read_pdu_skb(conn, skb, 0, 0);
1624        if (likely(err >= 0)) {
1625                struct iscsi_hdr *hdr = (struct iscsi_hdr *)skb->data;
1626                u8 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
1627
1628                if (unlikely(opcode == ISCSI_OP_LOGOUT_RSP))
1629                        cxgbi_sock_set_flag(csk, CTPF_LOGOUT_RSP_RCVD);
1630        }
1631
1632        return err;
1633}
1634
1635static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1636                             struct sk_buff *skb, unsigned int offset)
1637{
1638        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1639        bool offloaded = 0;
1640        int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1641
1642        log_debug(1 << CXGBI_DBG_PDU_RX,
1643                "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1644                conn, skb, skb->len, cxgbi_skcb_flags(skb));
1645
1646        if (conn->datadgst_en &&
1647            cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1648                pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1649                        conn, lskb, cxgbi_skcb_flags(lskb));
1650                iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1651                return -EIO;
1652        }
1653
1654        if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1655                return 0;
1656
1657        /* coalesced, add header digest length */
1658        if (lskb == skb && conn->hdrdgst_en)
1659                offset += ISCSI_DIGEST_SIZE;
1660
1661        if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1662                offloaded = 1;
1663
1664        if (opcode == ISCSI_OP_SCSI_DATA_IN)
1665                log_debug(1 << CXGBI_DBG_PDU_RX,
1666                        "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1667                        skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1668                        tcp_conn->in.datalen, offloaded ? "is" : "not");
1669
1670        return read_pdu_skb(conn, skb, offset, offloaded);
1671}
1672
1673static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1674{
1675        struct cxgbi_device *cdev = csk->cdev;
1676        int must_send;
1677        u32 credits;
1678
1679        log_debug(1 << CXGBI_DBG_PDU_RX,
1680                "csk 0x%p,%u,0x%lx,%u, seq %u, wup %u, thre %u, %u.\n",
1681                csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1682                csk->rcv_wup, cdev->rx_credit_thres,
1683                csk->rcv_win);
1684
1685        if (!cdev->rx_credit_thres)
1686                return;
1687
1688        if (csk->state != CTP_ESTABLISHED)
1689                return;
1690
1691        credits = csk->copied_seq - csk->rcv_wup;
1692        if (unlikely(!credits))
1693                return;
1694        must_send = credits + 16384 >= csk->rcv_win;
1695        if (must_send || credits >= cdev->rx_credit_thres)
1696                csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1697}
1698
1699void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1700{
1701        struct cxgbi_device *cdev = csk->cdev;
1702        struct iscsi_conn *conn = csk->user_data;
1703        struct sk_buff *skb;
1704        unsigned int read = 0;
1705        int err = 0;
1706
1707        log_debug(1 << CXGBI_DBG_PDU_RX,
1708                "csk 0x%p, conn 0x%p.\n", csk, conn);
1709
1710        if (unlikely(!conn || conn->suspend_rx)) {
1711                log_debug(1 << CXGBI_DBG_PDU_RX,
1712                        "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1713                        csk, conn, conn ? conn->id : 0xFF,
1714                        conn ? conn->suspend_rx : 0xFF);
1715                return;
1716        }
1717
1718        while (!err) {
1719                skb = skb_peek(&csk->receive_queue);
1720                if (!skb ||
1721                    !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1722                        if (skb)
1723                                log_debug(1 << CXGBI_DBG_PDU_RX,
1724                                        "skb 0x%p, NOT ready 0x%lx.\n",
1725                                        skb, cxgbi_skcb_flags(skb));
1726                        break;
1727                }
1728                __skb_unlink(skb, &csk->receive_queue);
1729
1730                read += cxgbi_skcb_rx_pdulen(skb);
1731                log_debug(1 << CXGBI_DBG_PDU_RX,
1732                        "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1733                        csk, skb, skb->len, cxgbi_skcb_flags(skb),
1734                        cxgbi_skcb_rx_pdulen(skb));
1735
1736                if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1737                        err = skb_read_pdu_bhs(csk, conn, skb);
1738                        if (err < 0) {
1739                                pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1740                                        "f 0x%lx, plen %u.\n",
1741                                        csk, skb, skb->len,
1742                                        cxgbi_skcb_flags(skb),
1743                                        cxgbi_skcb_rx_pdulen(skb));
1744                                goto skb_done;
1745                        }
1746                        err = skb_read_pdu_data(conn, skb, skb,
1747                                                err + cdev->skb_rx_extra);
1748                        if (err < 0)
1749                                pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1750                                        "f 0x%lx, plen %u.\n",
1751                                        csk, skb, skb->len,
1752                                        cxgbi_skcb_flags(skb),
1753                                        cxgbi_skcb_rx_pdulen(skb));
1754                } else {
1755                        err = skb_read_pdu_bhs(csk, conn, skb);
1756                        if (err < 0) {
1757                                pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1758                                        "f 0x%lx, plen %u.\n",
1759                                        csk, skb, skb->len,
1760                                        cxgbi_skcb_flags(skb),
1761                                        cxgbi_skcb_rx_pdulen(skb));
1762                                goto skb_done;
1763                        }
1764
1765                        if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1766                                struct sk_buff *dskb;
1767
1768                                dskb = skb_peek(&csk->receive_queue);
1769                                if (!dskb) {
1770                                        pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1771                                                " plen %u, NO data.\n",
1772                                                csk, skb, skb->len,
1773                                                cxgbi_skcb_flags(skb),
1774                                                cxgbi_skcb_rx_pdulen(skb));
1775                                        err = -EIO;
1776                                        goto skb_done;
1777                                }
1778                                __skb_unlink(dskb, &csk->receive_queue);
1779
1780                                err = skb_read_pdu_data(conn, skb, dskb, 0);
1781                                if (err < 0)
1782                                        pr_err("data, csk 0x%p, skb 0x%p,%u, "
1783                                                "f 0x%lx, plen %u, dskb 0x%p,"
1784                                                "%u.\n",
1785                                                csk, skb, skb->len,
1786                                                cxgbi_skcb_flags(skb),
1787                                                cxgbi_skcb_rx_pdulen(skb),
1788                                                dskb, dskb->len);
1789                                __kfree_skb(dskb);
1790                        } else
1791                                err = skb_read_pdu_data(conn, skb, skb, 0);
1792                }
1793skb_done:
1794                __kfree_skb(skb);
1795
1796                if (err < 0)
1797                        break;
1798        }
1799
1800        log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1801        if (read) {
1802                csk->copied_seq += read;
1803                csk_return_rx_credits(csk, read);
1804                conn->rxdata_octets += read;
1805        }
1806
1807        if (err < 0) {
1808                pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1809                        csk, conn, err, read);
1810                iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1811        }
1812}
1813EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1814
1815static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1816                                unsigned int offset, unsigned int *off,
1817                                struct scatterlist **sgp)
1818{
1819        int i;
1820        struct scatterlist *sg;
1821
1822        for_each_sg(sgl, sg, sgcnt, i) {
1823                if (offset < sg->length) {
1824                        *off = offset;
1825                        *sgp = sg;
1826                        return 0;
1827                }
1828                offset -= sg->length;
1829        }
1830        return -EFAULT;
1831}
1832
1833static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
1834                                unsigned int dlen, struct page_frag *frags,
1835                                int frag_max)
1836{
1837        unsigned int datalen = dlen;
1838        unsigned int sglen = sg->length - sgoffset;
1839        struct page *page = sg_page(sg);
1840        int i;
1841
1842        i = 0;
1843        do {
1844                unsigned int copy;
1845
1846                if (!sglen) {
1847                        sg = sg_next(sg);
1848                        if (!sg) {
1849                                pr_warn("sg %d NULL, len %u/%u.\n",
1850                                        i, datalen, dlen);
1851                                return -EINVAL;
1852                        }
1853                        sgoffset = 0;
1854                        sglen = sg->length;
1855                        page = sg_page(sg);
1856
1857                }
1858                copy = min(datalen, sglen);
1859                if (i && page == frags[i - 1].page &&
1860                    sgoffset + sg->offset ==
1861                        frags[i - 1].offset + frags[i - 1].size) {
1862                        frags[i - 1].size += copy;
1863                } else {
1864                        if (i >= frag_max) {
1865                                pr_warn("too many pages %u, dlen %u.\n",
1866                                        frag_max, dlen);
1867                                return -EINVAL;
1868                        }
1869
1870                        frags[i].page = page;
1871                        frags[i].offset = sg->offset + sgoffset;
1872                        frags[i].size = copy;
1873                        i++;
1874                }
1875                datalen -= copy;
1876                sgoffset += copy;
1877                sglen -= copy;
1878        } while (datalen);
1879
1880        return i;
1881}
1882
1883int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1884{
1885        struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1886        struct cxgbi_conn *cconn = tcp_conn->dd_data;
1887        struct cxgbi_device *cdev = cconn->chba->cdev;
1888        struct iscsi_conn *conn = task->conn;
1889        struct iscsi_tcp_task *tcp_task = task->dd_data;
1890        struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1891        struct scsi_cmnd *sc = task->sc;
1892        int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1893
1894        tcp_task->dd_data = tdata;
1895        task->hdr = NULL;
1896
1897        if (tdata->skb) {
1898                kfree_skb(tdata->skb);
1899                tdata->skb = NULL;
1900        }
1901
1902        if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1903            (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1904             (opcode == ISCSI_OP_SCSI_CMD &&
1905              (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
1906                /* data could goes into skb head */
1907                headroom += min_t(unsigned int,
1908                                SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1909                                conn->max_xmit_dlength);
1910
1911        tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1912        if (!tdata->skb) {
1913                struct cxgbi_sock *csk = cconn->cep->csk;
1914                struct net_device *ndev = cdev->ports[csk->port_id];
1915                ndev->stats.tx_dropped++;
1916                return -ENOMEM;
1917        }
1918
1919        skb_get(tdata->skb);
1920        skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1921        task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1922        task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1923
1924        /* data_out uses scsi_cmd's itt */
1925        if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1926                task_reserve_itt(task, &task->hdr->itt);
1927
1928        log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1929                "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1930                task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1931                conn->max_xmit_dlength, ntohl(task->hdr->itt));
1932
1933        return 0;
1934}
1935EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1936
1937static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1938{
1939        if (hcrc || dcrc) {
1940                u8 submode = 0;
1941
1942                if (hcrc)
1943                        submode |= 1;
1944                if (dcrc)
1945                        submode |= 2;
1946                cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1947        } else
1948                cxgbi_skcb_ulp_mode(skb) = 0;
1949}
1950
1951int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1952                              unsigned int count)
1953{
1954        struct iscsi_conn *conn = task->conn;
1955        struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1956        struct sk_buff *skb = tdata->skb;
1957        unsigned int datalen = count;
1958        int i, padlen = iscsi_padding(count);
1959        struct page *pg;
1960
1961        log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1962                "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1963                task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1964                ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1965
1966        skb_put(skb, task->hdr_len);
1967        tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1968        if (!count)
1969                return 0;
1970
1971        if (task->sc) {
1972                struct scsi_data_buffer *sdb = scsi_out(task->sc);
1973                struct scatterlist *sg = NULL;
1974                int err;
1975
1976                tdata->offset = offset;
1977                tdata->count = count;
1978                err = sgl_seek_offset(
1979                                        sdb->table.sgl, sdb->table.nents,
1980                                        tdata->offset, &tdata->sgoffset, &sg);
1981                if (err < 0) {
1982                        pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1983                                sdb->table.nents, tdata->offset, sdb->length);
1984                        return err;
1985                }
1986                err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1987                                        tdata->frags, MAX_PDU_FRAGS);
1988                if (err < 0) {
1989                        pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1990                                sdb->table.nents, tdata->offset, tdata->count);
1991                        return err;
1992                }
1993                tdata->nr_frags = err;
1994
1995                if (tdata->nr_frags > MAX_SKB_FRAGS ||
1996                    (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
1997                        char *dst = skb->data + task->hdr_len;
1998                        struct page_frag *frag = tdata->frags;
1999
2000                        /* data fits in the skb's headroom */
2001                        for (i = 0; i < tdata->nr_frags; i++, frag++) {
2002                                char *src = kmap_atomic(frag->page);
2003
2004                                memcpy(dst, src+frag->offset, frag->size);
2005                                dst += frag->size;
2006                                kunmap_atomic(src);
2007                        }
2008                        if (padlen) {
2009                                memset(dst, 0, padlen);
2010                                padlen = 0;
2011                        }
2012                        skb_put(skb, count + padlen);
2013                } else {
2014                        /* data fit into frag_list */
2015                        for (i = 0; i < tdata->nr_frags; i++) {
2016                                __skb_fill_page_desc(skb, i,
2017                                                tdata->frags[i].page,
2018                                                tdata->frags[i].offset,
2019                                                tdata->frags[i].size);
2020                                skb_frag_ref(skb, i);
2021                        }
2022                        skb_shinfo(skb)->nr_frags = tdata->nr_frags;
2023                        skb->len += count;
2024                        skb->data_len += count;
2025                        skb->truesize += count;
2026                }
2027
2028        } else {
2029                pg = virt_to_page(task->data);
2030
2031                get_page(pg);
2032                skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
2033                                        count);
2034                skb->len += count;
2035                skb->data_len += count;
2036                skb->truesize += count;
2037        }
2038
2039        if (padlen) {
2040                i = skb_shinfo(skb)->nr_frags;
2041                skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2042                                virt_to_page(padding), offset_in_page(padding),
2043                                padlen);
2044
2045                skb->data_len += padlen;
2046                skb->truesize += padlen;
2047                skb->len += padlen;
2048        }
2049
2050        return 0;
2051}
2052EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2053
2054int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2055{
2056        struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2057        struct cxgbi_conn *cconn = tcp_conn->dd_data;
2058        struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2059        struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
2060        struct sk_buff *skb = tdata->skb;
2061        struct cxgbi_sock *csk = NULL;
2062        unsigned int datalen;
2063        int err;
2064
2065        if (!skb || cxgbi_skcb_test_flag(skb, SKCBF_TX_DONE)) {
2066                log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2067                        "task 0x%p, skb 0x%p\n", task, skb);
2068                return 0;
2069        }
2070
2071        if (cconn && cconn->cep)
2072                csk = cconn->cep->csk;
2073        if (!csk) {
2074                log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2075                          "task 0x%p, csk gone.\n", task);
2076                return -EPIPE;
2077        }
2078
2079        datalen = skb->data_len;
2080
2081        /* write ppod first if using ofldq to write ppod */
2082        if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_VALID) {
2083                struct cxgbi_ppm *ppm = csk->cdev->cdev2ppm(csk->cdev);
2084
2085                ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_VALID;
2086                if (csk->cdev->csk_ddp_set_map(ppm, csk, ttinfo) < 0)
2087                        pr_err("task 0x%p, ppod writing using ofldq failed.\n",
2088                               task);
2089                        /* continue. Let fl get the data */
2090        }
2091
2092        err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2093        if (err > 0) {
2094                int pdulen = err;
2095
2096                log_debug(1 << CXGBI_DBG_PDU_TX,
2097                        "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2098                        task, task->sc, skb, skb->len, skb->data_len, err);
2099
2100                if (task->conn->hdrdgst_en)
2101                        pdulen += ISCSI_DIGEST_SIZE;
2102
2103                if (datalen && task->conn->datadgst_en)
2104                        pdulen += ISCSI_DIGEST_SIZE;
2105
2106                task->conn->txdata_octets += pdulen;
2107                cxgbi_skcb_set_flag(skb, SKCBF_TX_DONE);
2108                return 0;
2109        }
2110
2111        if (err == -EAGAIN || err == -ENOBUFS) {
2112                log_debug(1 << CXGBI_DBG_PDU_TX,
2113                        "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2114                        task, skb, skb->len, skb->data_len, err);
2115                /* reset skb to send when we are called again */
2116                return err;
2117        }
2118
2119        log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2120                "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2121                task->itt, skb, skb->len, skb->data_len, err);
2122
2123        __kfree_skb(tdata->skb);
2124        tdata->skb = NULL;
2125
2126        iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2127        iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2128        return err;
2129}
2130EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2131
2132void cxgbi_cleanup_task(struct iscsi_task *task)
2133{
2134        struct iscsi_tcp_task *tcp_task = task->dd_data;
2135        struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2136
2137        if (!tcp_task || !tdata || (tcp_task->dd_data != tdata)) {
2138                pr_info("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
2139                        task, task->sc, tcp_task,
2140                        tcp_task ? tcp_task->dd_data : NULL, tdata);
2141                return;
2142        }
2143
2144        log_debug(1 << CXGBI_DBG_ISCSI,
2145                "task 0x%p, skb 0x%p, itt 0x%x.\n",
2146                task, tdata->skb, task->hdr_itt);
2147
2148        tcp_task->dd_data = NULL;
2149        /*  never reached the xmit task callout */
2150        if (tdata->skb) {
2151                kfree_skb(tdata->skb);
2152                tdata->skb = NULL;
2153        }
2154
2155        task_release_itt(task, task->hdr_itt);
2156        memset(tdata, 0, sizeof(*tdata));
2157
2158        iscsi_tcp_cleanup_task(task);
2159}
2160EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2161
2162void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2163                                struct iscsi_stats *stats)
2164{
2165        struct iscsi_conn *conn = cls_conn->dd_data;
2166
2167        stats->txdata_octets = conn->txdata_octets;
2168        stats->rxdata_octets = conn->rxdata_octets;
2169        stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2170        stats->dataout_pdus = conn->dataout_pdus_cnt;
2171        stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2172        stats->datain_pdus = conn->datain_pdus_cnt;
2173        stats->r2t_pdus = conn->r2t_pdus_cnt;
2174        stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2175        stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2176        stats->digest_err = 0;
2177        stats->timeout_err = 0;
2178        stats->custom_length = 1;
2179        strcpy(stats->custom[0].desc, "eh_abort_cnt");
2180        stats->custom[0].value = conn->eh_abort_cnt;
2181}
2182EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2183
2184static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2185{
2186        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2187        struct cxgbi_conn *cconn = tcp_conn->dd_data;
2188        struct cxgbi_device *cdev = cconn->chba->cdev;
2189        unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2190        unsigned int max_def = 512 * MAX_SKB_FRAGS;
2191        unsigned int max = max(max_def, headroom);
2192
2193        max = min(cconn->chba->cdev->tx_max_size, max);
2194        if (conn->max_xmit_dlength)
2195                conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2196        else
2197                conn->max_xmit_dlength = max;
2198        cxgbi_align_pdu_size(conn->max_xmit_dlength);
2199
2200        return 0;
2201}
2202
2203static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2204{
2205        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2206        struct cxgbi_conn *cconn = tcp_conn->dd_data;
2207        unsigned int max = cconn->chba->cdev->rx_max_size;
2208
2209        cxgbi_align_pdu_size(max);
2210
2211        if (conn->max_recv_dlength) {
2212                if (conn->max_recv_dlength > max) {
2213                        pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2214                                conn->max_recv_dlength, max);
2215                        return -EINVAL;
2216                }
2217                conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2218                cxgbi_align_pdu_size(conn->max_recv_dlength);
2219        } else
2220                conn->max_recv_dlength = max;
2221
2222        return 0;
2223}
2224
2225int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2226                        enum iscsi_param param, char *buf, int buflen)
2227{
2228        struct iscsi_conn *conn = cls_conn->dd_data;
2229        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2230        struct cxgbi_conn *cconn = tcp_conn->dd_data;
2231        struct cxgbi_sock *csk = cconn->cep->csk;
2232        int err;
2233
2234        log_debug(1 << CXGBI_DBG_ISCSI,
2235                "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2236                cls_conn, param, buflen, buf);
2237
2238        switch (param) {
2239        case ISCSI_PARAM_HDRDGST_EN:
2240                err = iscsi_set_param(cls_conn, param, buf, buflen);
2241                if (!err && conn->hdrdgst_en)
2242                        err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2243                                                        conn->hdrdgst_en,
2244                                                        conn->datadgst_en, 0);
2245                break;
2246        case ISCSI_PARAM_DATADGST_EN:
2247                err = iscsi_set_param(cls_conn, param, buf, buflen);
2248                if (!err && conn->datadgst_en)
2249                        err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2250                                                        conn->hdrdgst_en,
2251                                                        conn->datadgst_en, 0);
2252                break;
2253        case ISCSI_PARAM_MAX_R2T:
2254                return iscsi_tcp_set_max_r2t(conn, buf);
2255        case ISCSI_PARAM_MAX_RECV_DLENGTH:
2256                err = iscsi_set_param(cls_conn, param, buf, buflen);
2257                if (!err)
2258                        err = cxgbi_conn_max_recv_dlength(conn);
2259                break;
2260        case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2261                err = iscsi_set_param(cls_conn, param, buf, buflen);
2262                if (!err)
2263                        err = cxgbi_conn_max_xmit_dlength(conn);
2264                break;
2265        default:
2266                return iscsi_set_param(cls_conn, param, buf, buflen);
2267        }
2268        return err;
2269}
2270EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2271
2272static inline int csk_print_port(struct cxgbi_sock *csk, char *buf)
2273{
2274        int len;
2275
2276        cxgbi_sock_get(csk);
2277        len = sprintf(buf, "%hu\n", ntohs(csk->daddr.sin_port));
2278        cxgbi_sock_put(csk);
2279
2280        return len;
2281}
2282
2283static inline int csk_print_ip(struct cxgbi_sock *csk, char *buf)
2284{
2285        int len;
2286
2287        cxgbi_sock_get(csk);
2288        if (csk->csk_family == AF_INET)
2289                len = sprintf(buf, "%pI4",
2290                              &csk->daddr.sin_addr.s_addr);
2291        else
2292                len = sprintf(buf, "%pI6",
2293                              &csk->daddr6.sin6_addr);
2294
2295        cxgbi_sock_put(csk);
2296
2297        return len;
2298}
2299
2300int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2301                       char *buf)
2302{
2303        struct cxgbi_endpoint *cep = ep->dd_data;
2304        struct cxgbi_sock *csk;
2305        int len;
2306
2307        log_debug(1 << CXGBI_DBG_ISCSI,
2308                "cls_conn 0x%p, param %d.\n", ep, param);
2309
2310        switch (param) {
2311        case ISCSI_PARAM_CONN_PORT:
2312        case ISCSI_PARAM_CONN_ADDRESS:
2313                if (!cep)
2314                        return -ENOTCONN;
2315
2316                csk = cep->csk;
2317                if (!csk)
2318                        return -ENOTCONN;
2319
2320                return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2321                                                 &csk->daddr, param, buf);
2322        default:
2323                return -ENOSYS;
2324        }
2325        return len;
2326}
2327EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
2328
2329struct iscsi_cls_conn *
2330cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2331{
2332        struct iscsi_cls_conn *cls_conn;
2333        struct iscsi_conn *conn;
2334        struct iscsi_tcp_conn *tcp_conn;
2335        struct cxgbi_conn *cconn;
2336
2337        cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2338        if (!cls_conn)
2339                return NULL;
2340
2341        conn = cls_conn->dd_data;
2342        tcp_conn = conn->dd_data;
2343        cconn = tcp_conn->dd_data;
2344        cconn->iconn = conn;
2345
2346        log_debug(1 << CXGBI_DBG_ISCSI,
2347                "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2348                cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2349
2350        return cls_conn;
2351}
2352EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2353
2354int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2355                                struct iscsi_cls_conn *cls_conn,
2356                                u64 transport_eph, int is_leading)
2357{
2358        struct iscsi_conn *conn = cls_conn->dd_data;
2359        struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2360        struct cxgbi_conn *cconn = tcp_conn->dd_data;
2361        struct cxgbi_ppm *ppm;
2362        struct iscsi_endpoint *ep;
2363        struct cxgbi_endpoint *cep;
2364        struct cxgbi_sock *csk;
2365        int err;
2366
2367        ep = iscsi_lookup_endpoint(transport_eph);
2368        if (!ep)
2369                return -EINVAL;
2370
2371        /*  setup ddp pagesize */
2372        cep = ep->dd_data;
2373        csk = cep->csk;
2374
2375        ppm = csk->cdev->cdev2ppm(csk->cdev);
2376        err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid,
2377                                             ppm->tformat.pgsz_idx_dflt, 0);
2378        if (err < 0)
2379                return err;
2380
2381        err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2382        if (err)
2383                return -EINVAL;
2384
2385        /*  calculate the tag idx bits needed for this conn based on cmds_max */
2386        cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2387
2388        write_lock_bh(&csk->callback_lock);
2389        csk->user_data = conn;
2390        cconn->chba = cep->chba;
2391        cconn->cep = cep;
2392        cep->cconn = cconn;
2393        write_unlock_bh(&csk->callback_lock);
2394
2395        cxgbi_conn_max_xmit_dlength(conn);
2396        cxgbi_conn_max_recv_dlength(conn);
2397
2398        log_debug(1 << CXGBI_DBG_ISCSI,
2399                "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2400                cls_session, cls_conn, ep, cconn, csk);
2401        /*  init recv engine */
2402        iscsi_tcp_hdr_recv_prep(tcp_conn);
2403
2404        return 0;
2405}
2406EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2407
2408struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2409                                                u16 cmds_max, u16 qdepth,
2410                                                u32 initial_cmdsn)
2411{
2412        struct cxgbi_endpoint *cep;
2413        struct cxgbi_hba *chba;
2414        struct Scsi_Host *shost;
2415        struct iscsi_cls_session *cls_session;
2416        struct iscsi_session *session;
2417
2418        if (!ep) {
2419                pr_err("missing endpoint.\n");
2420                return NULL;
2421        }
2422
2423        cep = ep->dd_data;
2424        chba = cep->chba;
2425        shost = chba->shost;
2426
2427        BUG_ON(chba != iscsi_host_priv(shost));
2428
2429        cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2430                                        cmds_max, 0,
2431                                        sizeof(struct iscsi_tcp_task) +
2432                                        sizeof(struct cxgbi_task_data),
2433                                        initial_cmdsn, ISCSI_MAX_TARGET);
2434        if (!cls_session)
2435                return NULL;
2436
2437        session = cls_session->dd_data;
2438        if (iscsi_tcp_r2tpool_alloc(session))
2439                goto remove_session;
2440
2441        log_debug(1 << CXGBI_DBG_ISCSI,
2442                "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2443        return cls_session;
2444
2445remove_session:
2446        iscsi_session_teardown(cls_session);
2447        return NULL;
2448}
2449EXPORT_SYMBOL_GPL(cxgbi_create_session);
2450
2451void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2452{
2453        log_debug(1 << CXGBI_DBG_ISCSI,
2454                "cls sess 0x%p.\n", cls_session);
2455
2456        iscsi_tcp_r2tpool_free(cls_session->dd_data);
2457        iscsi_session_teardown(cls_session);
2458}
2459EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2460
2461int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2462                        char *buf, int buflen)
2463{
2464        struct cxgbi_hba *chba = iscsi_host_priv(shost);
2465
2466        if (!chba->ndev) {
2467                shost_printk(KERN_ERR, shost, "Could not get host param. "
2468                                "netdev for host not set.\n");
2469                return -ENODEV;
2470        }
2471
2472        log_debug(1 << CXGBI_DBG_ISCSI,
2473                "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2474                shost, chba, chba->ndev->name, param, buflen, buf);
2475
2476        switch (param) {
2477        case ISCSI_HOST_PARAM_IPADDRESS:
2478        {
2479                __be32 addr = in_aton(buf);
2480                log_debug(1 << CXGBI_DBG_ISCSI,
2481                        "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2482                cxgbi_set_iscsi_ipv4(chba, addr);
2483                return 0;
2484        }
2485        case ISCSI_HOST_PARAM_HWADDRESS:
2486        case ISCSI_HOST_PARAM_NETDEV_NAME:
2487                return 0;
2488        default:
2489                return iscsi_host_set_param(shost, param, buf, buflen);
2490        }
2491}
2492EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2493
2494int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2495                        char *buf)
2496{
2497        struct cxgbi_hba *chba = iscsi_host_priv(shost);
2498        int len = 0;
2499
2500        if (!chba->ndev) {
2501                shost_printk(KERN_ERR, shost, "Could not get host param. "
2502                                "netdev for host not set.\n");
2503                return -ENODEV;
2504        }
2505
2506        log_debug(1 << CXGBI_DBG_ISCSI,
2507                "shost 0x%p, hba 0x%p,%s, param %d.\n",
2508                shost, chba, chba->ndev->name, param);
2509
2510        switch (param) {
2511        case ISCSI_HOST_PARAM_HWADDRESS:
2512                len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2513                break;
2514        case ISCSI_HOST_PARAM_NETDEV_NAME:
2515                len = sprintf(buf, "%s\n", chba->ndev->name);
2516                break;
2517        case ISCSI_HOST_PARAM_IPADDRESS:
2518        {
2519                struct cxgbi_sock *csk = find_sock_on_port(chba->cdev,
2520                                                           chba->port_id);
2521                if (csk) {
2522                        len = sprintf(buf, "%pIS",
2523                                      (struct sockaddr *)&csk->saddr);
2524                }
2525                log_debug(1 << CXGBI_DBG_ISCSI,
2526                          "hba %s, addr %s.\n", chba->ndev->name, buf);
2527                break;
2528        }
2529        default:
2530                return iscsi_host_get_param(shost, param, buf);
2531        }
2532
2533        return len;
2534}
2535EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2536
2537struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2538                                        struct sockaddr *dst_addr,
2539                                        int non_blocking)
2540{
2541        struct iscsi_endpoint *ep;
2542        struct cxgbi_endpoint *cep;
2543        struct cxgbi_hba *hba = NULL;
2544        struct cxgbi_sock *csk;
2545        int ifindex = 0;
2546        int err = -EINVAL;
2547
2548        log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2549                "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2550                shost, non_blocking, dst_addr);
2551
2552        if (shost) {
2553                hba = iscsi_host_priv(shost);
2554                if (!hba) {
2555                        pr_info("shost 0x%p, priv NULL.\n", shost);
2556                        goto err_out;
2557                }
2558
2559                ifindex = hba->ndev->ifindex;
2560        }
2561
2562        if (dst_addr->sa_family == AF_INET) {
2563                csk = cxgbi_check_route(dst_addr, ifindex);
2564#if IS_ENABLED(CONFIG_IPV6)
2565        } else if (dst_addr->sa_family == AF_INET6) {
2566                csk = cxgbi_check_route6(dst_addr, ifindex);
2567#endif
2568        } else {
2569                pr_info("address family 0x%x NOT supported.\n",
2570                        dst_addr->sa_family);
2571                err = -EAFNOSUPPORT;
2572                return (struct iscsi_endpoint *)ERR_PTR(err);
2573        }
2574
2575        if (IS_ERR(csk))
2576                return (struct iscsi_endpoint *)csk;
2577        cxgbi_sock_get(csk);
2578
2579        if (!hba)
2580                hba = csk->cdev->hbas[csk->port_id];
2581        else if (hba != csk->cdev->hbas[csk->port_id]) {
2582                pr_info("Could not connect through requested host %u"
2583                        "hba 0x%p != 0x%p (%u).\n",
2584                        shost->host_no, hba,
2585                        csk->cdev->hbas[csk->port_id], csk->port_id);
2586                err = -ENOSPC;
2587                goto release_conn;
2588        }
2589
2590        err = sock_get_port(csk);
2591        if (err)
2592                goto release_conn;
2593
2594        cxgbi_sock_set_state(csk, CTP_CONNECTING);
2595        err = csk->cdev->csk_init_act_open(csk);
2596        if (err)
2597                goto release_conn;
2598
2599        if (cxgbi_sock_is_closing(csk)) {
2600                err = -ENOSPC;
2601                pr_info("csk 0x%p is closing.\n", csk);
2602                goto release_conn;
2603        }
2604
2605        ep = iscsi_create_endpoint(sizeof(*cep));
2606        if (!ep) {
2607                err = -ENOMEM;
2608                pr_info("iscsi alloc ep, OOM.\n");
2609                goto release_conn;
2610        }
2611
2612        cep = ep->dd_data;
2613        cep->csk = csk;
2614        cep->chba = hba;
2615
2616        log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2617                "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2618                ep, cep, csk, hba, hba->ndev->name);
2619        return ep;
2620
2621release_conn:
2622        cxgbi_sock_put(csk);
2623        cxgbi_sock_closed(csk);
2624err_out:
2625        return ERR_PTR(err);
2626}
2627EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2628
2629int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2630{
2631        struct cxgbi_endpoint *cep = ep->dd_data;
2632        struct cxgbi_sock *csk = cep->csk;
2633
2634        if (!cxgbi_sock_is_established(csk))
2635                return 0;
2636        return 1;
2637}
2638EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2639
2640void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2641{
2642        struct cxgbi_endpoint *cep = ep->dd_data;
2643        struct cxgbi_conn *cconn = cep->cconn;
2644        struct cxgbi_sock *csk = cep->csk;
2645
2646        log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2647                "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2648                ep, cep, cconn, csk, csk->state, csk->flags);
2649
2650        if (cconn && cconn->iconn) {
2651                iscsi_suspend_tx(cconn->iconn);
2652                write_lock_bh(&csk->callback_lock);
2653                cep->csk->user_data = NULL;
2654                cconn->cep = NULL;
2655                write_unlock_bh(&csk->callback_lock);
2656        }
2657        iscsi_destroy_endpoint(ep);
2658
2659        if (likely(csk->state >= CTP_ESTABLISHED))
2660                need_active_close(csk);
2661        else
2662                cxgbi_sock_closed(csk);
2663
2664        cxgbi_sock_put(csk);
2665}
2666EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2667
2668int cxgbi_iscsi_init(struct iscsi_transport *itp,
2669                        struct scsi_transport_template **stt)
2670{
2671        *stt = iscsi_register_transport(itp);
2672        if (*stt == NULL) {
2673                pr_err("unable to register %s transport 0x%p.\n",
2674                        itp->name, itp);
2675                return -ENODEV;
2676        }
2677        log_debug(1 << CXGBI_DBG_ISCSI,
2678                "%s, registered iscsi transport 0x%p.\n",
2679                itp->name, stt);
2680        return 0;
2681}
2682EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2683
2684void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2685                        struct scsi_transport_template **stt)
2686{
2687        if (*stt) {
2688                log_debug(1 << CXGBI_DBG_ISCSI,
2689                        "de-register transport 0x%p, %s, stt 0x%p.\n",
2690                        itp, itp->name, *stt);
2691                *stt = NULL;
2692                iscsi_unregister_transport(itp);
2693        }
2694}
2695EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2696
2697umode_t cxgbi_attr_is_visible(int param_type, int param)
2698{
2699        switch (param_type) {
2700        case ISCSI_HOST_PARAM:
2701                switch (param) {
2702                case ISCSI_HOST_PARAM_NETDEV_NAME:
2703                case ISCSI_HOST_PARAM_HWADDRESS:
2704                case ISCSI_HOST_PARAM_IPADDRESS:
2705                case ISCSI_HOST_PARAM_INITIATOR_NAME:
2706                        return S_IRUGO;
2707                default:
2708                        return 0;
2709                }
2710        case ISCSI_PARAM:
2711                switch (param) {
2712                case ISCSI_PARAM_MAX_RECV_DLENGTH:
2713                case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2714                case ISCSI_PARAM_HDRDGST_EN:
2715                case ISCSI_PARAM_DATADGST_EN:
2716                case ISCSI_PARAM_CONN_ADDRESS:
2717                case ISCSI_PARAM_CONN_PORT:
2718                case ISCSI_PARAM_EXP_STATSN:
2719                case ISCSI_PARAM_PERSISTENT_ADDRESS:
2720                case ISCSI_PARAM_PERSISTENT_PORT:
2721                case ISCSI_PARAM_PING_TMO:
2722                case ISCSI_PARAM_RECV_TMO:
2723                case ISCSI_PARAM_INITIAL_R2T_EN:
2724                case ISCSI_PARAM_MAX_R2T:
2725                case ISCSI_PARAM_IMM_DATA_EN:
2726                case ISCSI_PARAM_FIRST_BURST:
2727                case ISCSI_PARAM_MAX_BURST:
2728                case ISCSI_PARAM_PDU_INORDER_EN:
2729                case ISCSI_PARAM_DATASEQ_INORDER_EN:
2730                case ISCSI_PARAM_ERL:
2731                case ISCSI_PARAM_TARGET_NAME:
2732                case ISCSI_PARAM_TPGT:
2733                case ISCSI_PARAM_USERNAME:
2734                case ISCSI_PARAM_PASSWORD:
2735                case ISCSI_PARAM_USERNAME_IN:
2736                case ISCSI_PARAM_PASSWORD_IN:
2737                case ISCSI_PARAM_FAST_ABORT:
2738                case ISCSI_PARAM_ABORT_TMO:
2739                case ISCSI_PARAM_LU_RESET_TMO:
2740                case ISCSI_PARAM_TGT_RESET_TMO:
2741                case ISCSI_PARAM_IFACE_NAME:
2742                case ISCSI_PARAM_INITIATOR_NAME:
2743                        return S_IRUGO;
2744                default:
2745                        return 0;
2746                }
2747        }
2748
2749        return 0;
2750}
2751EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
2752
2753static int __init libcxgbi_init_module(void)
2754{
2755        pr_info("%s", version);
2756
2757        BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, cb) <
2758                     sizeof(struct cxgbi_skb_cb));
2759        return 0;
2760}
2761
2762static void __exit libcxgbi_exit_module(void)
2763{
2764        cxgbi_device_unregister_all(0xFF);
2765        return;
2766}
2767
2768module_init(libcxgbi_init_module);
2769module_exit(libcxgbi_exit_module);
2770