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