xref: /openbmc/linux/drivers/scsi/cxgbi/libcxgbi.c (revision fc8d0590)
19ba682f0Skxie@chelsio.com /*
29ba682f0Skxie@chelsio.com  * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
39ba682f0Skxie@chelsio.com  *
49ba682f0Skxie@chelsio.com  * Copyright (c) 2010 Chelsio Communications, Inc.
59ba682f0Skxie@chelsio.com  *
69ba682f0Skxie@chelsio.com  * This program is free software; you can redistribute it and/or modify
79ba682f0Skxie@chelsio.com  * it under the terms of the GNU General Public License as published by
89ba682f0Skxie@chelsio.com  * the Free Software Foundation.
99ba682f0Skxie@chelsio.com  *
109ba682f0Skxie@chelsio.com  * Written by: Karen Xie (kxie@chelsio.com)
119ba682f0Skxie@chelsio.com  * Written by: Rakesh Ranjan (rranjan@chelsio.com)
129ba682f0Skxie@chelsio.com  */
139ba682f0Skxie@chelsio.com 
149ba682f0Skxie@chelsio.com #define pr_fmt(fmt)	KBUILD_MODNAME ":%s: " fmt, __func__
159ba682f0Skxie@chelsio.com 
169ba682f0Skxie@chelsio.com #include <linux/skbuff.h>
179ba682f0Skxie@chelsio.com #include <linux/crypto.h>
189ba682f0Skxie@chelsio.com #include <linux/scatterlist.h>
199ba682f0Skxie@chelsio.com #include <linux/pci.h>
209ba682f0Skxie@chelsio.com #include <scsi/scsi.h>
219ba682f0Skxie@chelsio.com #include <scsi/scsi_cmnd.h>
229ba682f0Skxie@chelsio.com #include <scsi/scsi_host.h>
239ba682f0Skxie@chelsio.com #include <linux/if_vlan.h>
249ba682f0Skxie@chelsio.com #include <linux/inet.h>
259ba682f0Skxie@chelsio.com #include <net/dst.h>
269ba682f0Skxie@chelsio.com #include <net/route.h>
27fc8d0590SAnish Bhatt #include <net/ipv6.h>
28fc8d0590SAnish Bhatt #include <net/ip6_route.h>
29fc8d0590SAnish Bhatt #include <net/addrconf.h>
30fc8d0590SAnish Bhatt 
319ba682f0Skxie@chelsio.com #include <linux/inetdevice.h>	/* ip_dev_find */
32acf3368fSPaul Gortmaker #include <linux/module.h>
339ba682f0Skxie@chelsio.com #include <net/tcp.h>
349ba682f0Skxie@chelsio.com 
359ba682f0Skxie@chelsio.com static unsigned int dbg_level;
369ba682f0Skxie@chelsio.com 
379ba682f0Skxie@chelsio.com #include "libcxgbi.h"
389ba682f0Skxie@chelsio.com 
399ba682f0Skxie@chelsio.com #define DRV_MODULE_NAME		"libcxgbi"
409ba682f0Skxie@chelsio.com #define DRV_MODULE_DESC		"Chelsio iSCSI driver library"
419ba682f0Skxie@chelsio.com #define DRV_MODULE_VERSION	"0.9.0"
429ba682f0Skxie@chelsio.com #define DRV_MODULE_RELDATE	"Jun. 2010"
439ba682f0Skxie@chelsio.com 
449ba682f0Skxie@chelsio.com MODULE_AUTHOR("Chelsio Communications, Inc.");
459ba682f0Skxie@chelsio.com MODULE_DESCRIPTION(DRV_MODULE_DESC);
469ba682f0Skxie@chelsio.com MODULE_VERSION(DRV_MODULE_VERSION);
479ba682f0Skxie@chelsio.com MODULE_LICENSE("GPL");
489ba682f0Skxie@chelsio.com 
499ba682f0Skxie@chelsio.com module_param(dbg_level, uint, 0644);
509ba682f0Skxie@chelsio.com MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
519ba682f0Skxie@chelsio.com 
529ba682f0Skxie@chelsio.com 
539ba682f0Skxie@chelsio.com /*
549ba682f0Skxie@chelsio.com  * cxgbi device management
559ba682f0Skxie@chelsio.com  * maintains a list of the cxgbi devices
569ba682f0Skxie@chelsio.com  */
579ba682f0Skxie@chelsio.com static LIST_HEAD(cdev_list);
589ba682f0Skxie@chelsio.com static DEFINE_MUTEX(cdev_mutex);
599ba682f0Skxie@chelsio.com 
609ba682f0Skxie@chelsio.com int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
619ba682f0Skxie@chelsio.com 				unsigned int max_conn)
629ba682f0Skxie@chelsio.com {
639ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
649ba682f0Skxie@chelsio.com 
659ba682f0Skxie@chelsio.com 	pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
669ba682f0Skxie@chelsio.com 					     sizeof(struct cxgbi_sock *),
679ba682f0Skxie@chelsio.com 					     GFP_KERNEL);
689ba682f0Skxie@chelsio.com 	if (!pmap->port_csk) {
699ba682f0Skxie@chelsio.com 		pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
709ba682f0Skxie@chelsio.com 		return -ENOMEM;
719ba682f0Skxie@chelsio.com 	}
729ba682f0Skxie@chelsio.com 
739ba682f0Skxie@chelsio.com 	pmap->max_connect = max_conn;
749ba682f0Skxie@chelsio.com 	pmap->sport_base = base;
759ba682f0Skxie@chelsio.com 	spin_lock_init(&pmap->lock);
769ba682f0Skxie@chelsio.com 	return 0;
779ba682f0Skxie@chelsio.com }
789ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
799ba682f0Skxie@chelsio.com 
809ba682f0Skxie@chelsio.com void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
819ba682f0Skxie@chelsio.com {
829ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
839ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
849ba682f0Skxie@chelsio.com 	int i;
859ba682f0Skxie@chelsio.com 
869ba682f0Skxie@chelsio.com 	for (i = 0; i < pmap->max_connect; i++) {
879ba682f0Skxie@chelsio.com 		if (pmap->port_csk[i]) {
889ba682f0Skxie@chelsio.com 			csk = pmap->port_csk[i];
899ba682f0Skxie@chelsio.com 			pmap->port_csk[i] = NULL;
909ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_SOCK,
919ba682f0Skxie@chelsio.com 				"csk 0x%p, cdev 0x%p, offload down.\n",
929ba682f0Skxie@chelsio.com 				csk, cdev);
939ba682f0Skxie@chelsio.com 			spin_lock_bh(&csk->lock);
949ba682f0Skxie@chelsio.com 			cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
959ba682f0Skxie@chelsio.com 			cxgbi_sock_closed(csk);
969ba682f0Skxie@chelsio.com 			spin_unlock_bh(&csk->lock);
979ba682f0Skxie@chelsio.com 			cxgbi_sock_put(csk);
989ba682f0Skxie@chelsio.com 		}
999ba682f0Skxie@chelsio.com 	}
1009ba682f0Skxie@chelsio.com }
1019ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
1029ba682f0Skxie@chelsio.com 
1039ba682f0Skxie@chelsio.com static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
1049ba682f0Skxie@chelsio.com {
1059ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1069ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u.\n", cdev, cdev->nports);
1079ba682f0Skxie@chelsio.com 	cxgbi_hbas_remove(cdev);
1089ba682f0Skxie@chelsio.com 	cxgbi_device_portmap_cleanup(cdev);
1099ba682f0Skxie@chelsio.com 	if (cdev->dev_ddp_cleanup)
1109ba682f0Skxie@chelsio.com 		cdev->dev_ddp_cleanup(cdev);
1119ba682f0Skxie@chelsio.com 	else
1129ba682f0Skxie@chelsio.com 		cxgbi_ddp_cleanup(cdev);
1139ba682f0Skxie@chelsio.com 	if (cdev->ddp)
1149ba682f0Skxie@chelsio.com 		cxgbi_ddp_cleanup(cdev);
1159ba682f0Skxie@chelsio.com 	if (cdev->pmap.max_connect)
1169ba682f0Skxie@chelsio.com 		cxgbi_free_big_mem(cdev->pmap.port_csk);
1179ba682f0Skxie@chelsio.com 	kfree(cdev);
1189ba682f0Skxie@chelsio.com }
1199ba682f0Skxie@chelsio.com 
1209ba682f0Skxie@chelsio.com struct cxgbi_device *cxgbi_device_register(unsigned int extra,
1219ba682f0Skxie@chelsio.com 					   unsigned int nports)
1229ba682f0Skxie@chelsio.com {
1239ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev;
1249ba682f0Skxie@chelsio.com 
1259ba682f0Skxie@chelsio.com 	cdev = kzalloc(sizeof(*cdev) + extra + nports *
1269ba682f0Skxie@chelsio.com 			(sizeof(struct cxgbi_hba *) +
1279ba682f0Skxie@chelsio.com 			 sizeof(struct net_device *)),
1289ba682f0Skxie@chelsio.com 			GFP_KERNEL);
1299ba682f0Skxie@chelsio.com 	if (!cdev) {
1309ba682f0Skxie@chelsio.com 		pr_warn("nport %d, OOM.\n", nports);
1319ba682f0Skxie@chelsio.com 		return NULL;
1329ba682f0Skxie@chelsio.com 	}
1339ba682f0Skxie@chelsio.com 	cdev->ports = (struct net_device **)(cdev + 1);
1349ba682f0Skxie@chelsio.com 	cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
1359ba682f0Skxie@chelsio.com 						sizeof(struct net_device *));
1369ba682f0Skxie@chelsio.com 	if (extra)
1379ba682f0Skxie@chelsio.com 		cdev->dd_data = ((char *)cdev->hbas) +
1389ba682f0Skxie@chelsio.com 				nports * sizeof(struct cxgbi_hba *);
1399ba682f0Skxie@chelsio.com 	spin_lock_init(&cdev->pmap.lock);
1409ba682f0Skxie@chelsio.com 
1419ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1429ba682f0Skxie@chelsio.com 	list_add_tail(&cdev->list_head, &cdev_list);
1439ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1449ba682f0Skxie@chelsio.com 
1459ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1469ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u.\n", cdev, nports);
1479ba682f0Skxie@chelsio.com 	return cdev;
1489ba682f0Skxie@chelsio.com }
1499ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_register);
1509ba682f0Skxie@chelsio.com 
1519ba682f0Skxie@chelsio.com void cxgbi_device_unregister(struct cxgbi_device *cdev)
1529ba682f0Skxie@chelsio.com {
1539ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1549ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u,%s.\n",
1559ba682f0Skxie@chelsio.com 		cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
1569ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1579ba682f0Skxie@chelsio.com 	list_del(&cdev->list_head);
1589ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1599ba682f0Skxie@chelsio.com 	cxgbi_device_destroy(cdev);
1609ba682f0Skxie@chelsio.com }
1619ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
1629ba682f0Skxie@chelsio.com 
1639ba682f0Skxie@chelsio.com void cxgbi_device_unregister_all(unsigned int flag)
1649ba682f0Skxie@chelsio.com {
1659ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
1669ba682f0Skxie@chelsio.com 
1679ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1689ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
1699ba682f0Skxie@chelsio.com 		if ((cdev->flags & flag) == flag) {
1709ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_DEV,
1719ba682f0Skxie@chelsio.com 				"cdev 0x%p, p# %u,%s.\n",
1729ba682f0Skxie@chelsio.com 				cdev, cdev->nports, cdev->nports ?
1739ba682f0Skxie@chelsio.com 				 cdev->ports[0]->name : "");
1749ba682f0Skxie@chelsio.com 			list_del(&cdev->list_head);
1759ba682f0Skxie@chelsio.com 			cxgbi_device_destroy(cdev);
1769ba682f0Skxie@chelsio.com 		}
1779ba682f0Skxie@chelsio.com 	}
1789ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1799ba682f0Skxie@chelsio.com }
1809ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
1819ba682f0Skxie@chelsio.com 
1829ba682f0Skxie@chelsio.com struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
1839ba682f0Skxie@chelsio.com {
1849ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
1859ba682f0Skxie@chelsio.com 
1869ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1879ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
1889ba682f0Skxie@chelsio.com 		if (cdev->lldev == lldev) {
1899ba682f0Skxie@chelsio.com 			mutex_unlock(&cdev_mutex);
1909ba682f0Skxie@chelsio.com 			return cdev;
1919ba682f0Skxie@chelsio.com 		}
1929ba682f0Skxie@chelsio.com 	}
1939ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1949ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1959ba682f0Skxie@chelsio.com 		"lldev 0x%p, NO match found.\n", lldev);
1969ba682f0Skxie@chelsio.com 	return NULL;
1979ba682f0Skxie@chelsio.com }
1989ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
1999ba682f0Skxie@chelsio.com 
200fc8d0590SAnish Bhatt struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
2019ba682f0Skxie@chelsio.com 						 int *port)
2029ba682f0Skxie@chelsio.com {
2030b3d8947Skxie@chelsio.com 	struct net_device *vdev = NULL;
2049ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
2059ba682f0Skxie@chelsio.com 	int i;
2069ba682f0Skxie@chelsio.com 
2070b3d8947Skxie@chelsio.com 	if (ndev->priv_flags & IFF_802_1Q_VLAN) {
2080b3d8947Skxie@chelsio.com 		vdev = ndev;
2099ba682f0Skxie@chelsio.com 		ndev = vlan_dev_real_dev(ndev);
2100b3d8947Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
2110b3d8947Skxie@chelsio.com 			"vlan dev %s -> %s.\n", vdev->name, ndev->name);
2120b3d8947Skxie@chelsio.com 	}
2139ba682f0Skxie@chelsio.com 
2149ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
2159ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
2169ba682f0Skxie@chelsio.com 		for (i = 0; i < cdev->nports; i++) {
2179ba682f0Skxie@chelsio.com 			if (ndev == cdev->ports[i]) {
2180b3d8947Skxie@chelsio.com 				cdev->hbas[i]->vdev = vdev;
2199ba682f0Skxie@chelsio.com 				mutex_unlock(&cdev_mutex);
2209ba682f0Skxie@chelsio.com 				if (port)
2219ba682f0Skxie@chelsio.com 					*port = i;
2229ba682f0Skxie@chelsio.com 				return cdev;
2239ba682f0Skxie@chelsio.com 			}
2249ba682f0Skxie@chelsio.com 		}
2259ba682f0Skxie@chelsio.com 	}
2269ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
2279ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
2289ba682f0Skxie@chelsio.com 		"ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
2299ba682f0Skxie@chelsio.com 	return NULL;
2309ba682f0Skxie@chelsio.com }
231fc8d0590SAnish Bhatt EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev);
232fc8d0590SAnish Bhatt 
233fc8d0590SAnish Bhatt static struct cxgbi_device *cxgbi_device_find_by_mac(struct net_device *ndev,
234fc8d0590SAnish Bhatt 						     int *port)
235fc8d0590SAnish Bhatt {
236fc8d0590SAnish Bhatt 	struct net_device *vdev = NULL;
237fc8d0590SAnish Bhatt 	struct cxgbi_device *cdev, *tmp;
238fc8d0590SAnish Bhatt 	int i;
239fc8d0590SAnish Bhatt 
240fc8d0590SAnish Bhatt 	if (ndev->priv_flags & IFF_802_1Q_VLAN) {
241fc8d0590SAnish Bhatt 		vdev = ndev;
242fc8d0590SAnish Bhatt 		ndev = vlan_dev_real_dev(ndev);
243fc8d0590SAnish Bhatt 		pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
244fc8d0590SAnish Bhatt 	}
245fc8d0590SAnish Bhatt 
246fc8d0590SAnish Bhatt 	mutex_lock(&cdev_mutex);
247fc8d0590SAnish Bhatt 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
248fc8d0590SAnish Bhatt 		for (i = 0; i < cdev->nports; i++) {
249fc8d0590SAnish Bhatt 			if (!memcmp(ndev->dev_addr, cdev->ports[i]->dev_addr,
250fc8d0590SAnish Bhatt 				    MAX_ADDR_LEN)) {
251fc8d0590SAnish Bhatt 				cdev->hbas[i]->vdev = vdev;
252fc8d0590SAnish Bhatt 				mutex_unlock(&cdev_mutex);
253fc8d0590SAnish Bhatt 				if (port)
254fc8d0590SAnish Bhatt 					*port = i;
255fc8d0590SAnish Bhatt 				return cdev;
256fc8d0590SAnish Bhatt 			}
257fc8d0590SAnish Bhatt 		}
258fc8d0590SAnish Bhatt 	}
259fc8d0590SAnish Bhatt 	mutex_unlock(&cdev_mutex);
260fc8d0590SAnish Bhatt 	log_debug(1 << CXGBI_DBG_DEV,
261fc8d0590SAnish Bhatt 		  "ndev 0x%p, %s, NO match mac found.\n",
262fc8d0590SAnish Bhatt 		  ndev, ndev->name);
263fc8d0590SAnish Bhatt 	return NULL;
264fc8d0590SAnish Bhatt }
2659ba682f0Skxie@chelsio.com 
2669ba682f0Skxie@chelsio.com void cxgbi_hbas_remove(struct cxgbi_device *cdev)
2679ba682f0Skxie@chelsio.com {
2689ba682f0Skxie@chelsio.com 	int i;
2699ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
2709ba682f0Skxie@chelsio.com 
2719ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
2729ba682f0Skxie@chelsio.com 		"cdev 0x%p, p#%u.\n", cdev, cdev->nports);
2739ba682f0Skxie@chelsio.com 
2749ba682f0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
2759ba682f0Skxie@chelsio.com 		chba = cdev->hbas[i];
2769ba682f0Skxie@chelsio.com 		if (chba) {
2779ba682f0Skxie@chelsio.com 			cdev->hbas[i] = NULL;
2789ba682f0Skxie@chelsio.com 			iscsi_host_remove(chba->shost);
2799ba682f0Skxie@chelsio.com 			pci_dev_put(cdev->pdev);
2809ba682f0Skxie@chelsio.com 			iscsi_host_free(chba->shost);
2819ba682f0Skxie@chelsio.com 		}
2829ba682f0Skxie@chelsio.com 	}
2839ba682f0Skxie@chelsio.com }
2849ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
2859ba682f0Skxie@chelsio.com 
2869ba682f0Skxie@chelsio.com int cxgbi_hbas_add(struct cxgbi_device *cdev, unsigned int max_lun,
2879ba682f0Skxie@chelsio.com 		unsigned int max_id, struct scsi_host_template *sht,
2889ba682f0Skxie@chelsio.com 		struct scsi_transport_template *stt)
2899ba682f0Skxie@chelsio.com {
2909ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
2919ba682f0Skxie@chelsio.com 	struct Scsi_Host *shost;
2929ba682f0Skxie@chelsio.com 	int i, err;
2939ba682f0Skxie@chelsio.com 
2949ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
2959ba682f0Skxie@chelsio.com 
2969ba682f0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
2979ba682f0Skxie@chelsio.com 		shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
2989ba682f0Skxie@chelsio.com 		if (!shost) {
2999ba682f0Skxie@chelsio.com 			pr_info("0x%p, p%d, %s, host alloc failed.\n",
3009ba682f0Skxie@chelsio.com 				cdev, i, cdev->ports[i]->name);
3019ba682f0Skxie@chelsio.com 			err = -ENOMEM;
3029ba682f0Skxie@chelsio.com 			goto err_out;
3039ba682f0Skxie@chelsio.com 		}
3049ba682f0Skxie@chelsio.com 
3059ba682f0Skxie@chelsio.com 		shost->transportt = stt;
3069ba682f0Skxie@chelsio.com 		shost->max_lun = max_lun;
3079ba682f0Skxie@chelsio.com 		shost->max_id = max_id;
3089ba682f0Skxie@chelsio.com 		shost->max_channel = 0;
3099ba682f0Skxie@chelsio.com 		shost->max_cmd_len = 16;
3109ba682f0Skxie@chelsio.com 
3119ba682f0Skxie@chelsio.com 		chba = iscsi_host_priv(shost);
3129ba682f0Skxie@chelsio.com 		chba->cdev = cdev;
3139ba682f0Skxie@chelsio.com 		chba->ndev = cdev->ports[i];
3149ba682f0Skxie@chelsio.com 		chba->shost = shost;
3159ba682f0Skxie@chelsio.com 
3169ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
3179ba682f0Skxie@chelsio.com 			"cdev 0x%p, p#%d %s: chba 0x%p.\n",
3189ba682f0Skxie@chelsio.com 			cdev, i, cdev->ports[i]->name, chba);
3199ba682f0Skxie@chelsio.com 
3209ba682f0Skxie@chelsio.com 		pci_dev_get(cdev->pdev);
3219ba682f0Skxie@chelsio.com 		err = iscsi_host_add(shost, &cdev->pdev->dev);
3229ba682f0Skxie@chelsio.com 		if (err) {
3239ba682f0Skxie@chelsio.com 			pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
3249ba682f0Skxie@chelsio.com 				cdev, i, cdev->ports[i]->name);
3259ba682f0Skxie@chelsio.com 			pci_dev_put(cdev->pdev);
3269ba682f0Skxie@chelsio.com 			scsi_host_put(shost);
3279ba682f0Skxie@chelsio.com 			goto  err_out;
3289ba682f0Skxie@chelsio.com 		}
3299ba682f0Skxie@chelsio.com 
3309ba682f0Skxie@chelsio.com 		cdev->hbas[i] = chba;
3319ba682f0Skxie@chelsio.com 	}
3329ba682f0Skxie@chelsio.com 
3339ba682f0Skxie@chelsio.com 	return 0;
3349ba682f0Skxie@chelsio.com 
3359ba682f0Skxie@chelsio.com err_out:
3369ba682f0Skxie@chelsio.com 	cxgbi_hbas_remove(cdev);
3379ba682f0Skxie@chelsio.com 	return err;
3389ba682f0Skxie@chelsio.com }
3399ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
3409ba682f0Skxie@chelsio.com 
3419ba682f0Skxie@chelsio.com /*
3429ba682f0Skxie@chelsio.com  * iSCSI offload
3439ba682f0Skxie@chelsio.com  *
3449ba682f0Skxie@chelsio.com  * - source port management
3459ba682f0Skxie@chelsio.com  *   To find a free source port in the port allocation map we use a very simple
3469ba682f0Skxie@chelsio.com  *   rotor scheme to look for the next free port.
3479ba682f0Skxie@chelsio.com  *
3489ba682f0Skxie@chelsio.com  *   If a source port has been specified make sure that it doesn't collide with
3499ba682f0Skxie@chelsio.com  *   our normal source port allocation map.  If it's outside the range of our
3509ba682f0Skxie@chelsio.com  *   allocation/deallocation scheme just let them use it.
3519ba682f0Skxie@chelsio.com  *
3529ba682f0Skxie@chelsio.com  *   If the source port is outside our allocation range, the caller is
3539ba682f0Skxie@chelsio.com  *   responsible for keeping track of their port usage.
3549ba682f0Skxie@chelsio.com  */
3559ba682f0Skxie@chelsio.com static int sock_get_port(struct cxgbi_sock *csk)
3569ba682f0Skxie@chelsio.com {
3579ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
3589ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
3599ba682f0Skxie@chelsio.com 	unsigned int start;
3609ba682f0Skxie@chelsio.com 	int idx;
361fc8d0590SAnish Bhatt 	__be16 *port;
3629ba682f0Skxie@chelsio.com 
3639ba682f0Skxie@chelsio.com 	if (!pmap->max_connect) {
3649ba682f0Skxie@chelsio.com 		pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
3659ba682f0Skxie@chelsio.com 			   cdev, csk->port_id, cdev->ports[csk->port_id]->name);
3669ba682f0Skxie@chelsio.com 		return -EADDRNOTAVAIL;
3679ba682f0Skxie@chelsio.com 	}
3689ba682f0Skxie@chelsio.com 
369fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
370fc8d0590SAnish Bhatt 		port = &csk->saddr.sin_port;
371fc8d0590SAnish Bhatt 	else /* ipv6 */
372fc8d0590SAnish Bhatt 		port = &csk->saddr6.sin6_port;
373fc8d0590SAnish Bhatt 
374fc8d0590SAnish Bhatt 	if (*port) {
3759ba682f0Skxie@chelsio.com 		pr_err("source port NON-ZERO %u.\n",
376fc8d0590SAnish Bhatt 			ntohs(*port));
3779ba682f0Skxie@chelsio.com 		return -EADDRINUSE;
3789ba682f0Skxie@chelsio.com 	}
3799ba682f0Skxie@chelsio.com 
3809ba682f0Skxie@chelsio.com 	spin_lock_bh(&pmap->lock);
3819ba682f0Skxie@chelsio.com 	if (pmap->used >= pmap->max_connect) {
3829ba682f0Skxie@chelsio.com 		spin_unlock_bh(&pmap->lock);
3839ba682f0Skxie@chelsio.com 		pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
3849ba682f0Skxie@chelsio.com 			cdev, csk->port_id, cdev->ports[csk->port_id]->name);
3859ba682f0Skxie@chelsio.com 		return -EADDRNOTAVAIL;
3869ba682f0Skxie@chelsio.com 	}
3879ba682f0Skxie@chelsio.com 
3889ba682f0Skxie@chelsio.com 	start = idx = pmap->next;
3899ba682f0Skxie@chelsio.com 	do {
3909ba682f0Skxie@chelsio.com 		if (++idx >= pmap->max_connect)
3919ba682f0Skxie@chelsio.com 			idx = 0;
3929ba682f0Skxie@chelsio.com 		if (!pmap->port_csk[idx]) {
3939ba682f0Skxie@chelsio.com 			pmap->used++;
394fc8d0590SAnish Bhatt 			*port = htons(pmap->sport_base + idx);
3959ba682f0Skxie@chelsio.com 			pmap->next = idx;
3969ba682f0Skxie@chelsio.com 			pmap->port_csk[idx] = csk;
3979ba682f0Skxie@chelsio.com 			spin_unlock_bh(&pmap->lock);
3989ba682f0Skxie@chelsio.com 			cxgbi_sock_get(csk);
3999ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_SOCK,
4009ba682f0Skxie@chelsio.com 				"cdev 0x%p, p#%u %s, p %u, %u.\n",
4019ba682f0Skxie@chelsio.com 				cdev, csk->port_id,
4029ba682f0Skxie@chelsio.com 				cdev->ports[csk->port_id]->name,
4039ba682f0Skxie@chelsio.com 				pmap->sport_base + idx, pmap->next);
4049ba682f0Skxie@chelsio.com 			return 0;
4059ba682f0Skxie@chelsio.com 		}
4069ba682f0Skxie@chelsio.com 	} while (idx != start);
4079ba682f0Skxie@chelsio.com 	spin_unlock_bh(&pmap->lock);
4089ba682f0Skxie@chelsio.com 
4099ba682f0Skxie@chelsio.com 	/* should not happen */
4109ba682f0Skxie@chelsio.com 	pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
4119ba682f0Skxie@chelsio.com 		cdev, csk->port_id, cdev->ports[csk->port_id]->name,
4129ba682f0Skxie@chelsio.com 		pmap->next);
4139ba682f0Skxie@chelsio.com 	return -EADDRNOTAVAIL;
4149ba682f0Skxie@chelsio.com }
4159ba682f0Skxie@chelsio.com 
4169ba682f0Skxie@chelsio.com static void sock_put_port(struct cxgbi_sock *csk)
4179ba682f0Skxie@chelsio.com {
4189ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
4199ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
420fc8d0590SAnish Bhatt 	__be16 *port;
4219ba682f0Skxie@chelsio.com 
422fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
423fc8d0590SAnish Bhatt 		port = &csk->saddr.sin_port;
424fc8d0590SAnish Bhatt 	else /* ipv6 */
425fc8d0590SAnish Bhatt 		port = &csk->saddr6.sin6_port;
4269ba682f0Skxie@chelsio.com 
427fc8d0590SAnish Bhatt 	if (*port) {
428fc8d0590SAnish Bhatt 		int idx = ntohs(*port) - pmap->sport_base;
429fc8d0590SAnish Bhatt 
430fc8d0590SAnish Bhatt 		*port = 0;
4319ba682f0Skxie@chelsio.com 		if (idx < 0 || idx >= pmap->max_connect) {
4329ba682f0Skxie@chelsio.com 			pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
4339ba682f0Skxie@chelsio.com 				cdev, csk->port_id,
4349ba682f0Skxie@chelsio.com 				cdev->ports[csk->port_id]->name,
435fc8d0590SAnish Bhatt 				ntohs(*port));
4369ba682f0Skxie@chelsio.com 			return;
4379ba682f0Skxie@chelsio.com 		}
4389ba682f0Skxie@chelsio.com 
4399ba682f0Skxie@chelsio.com 		spin_lock_bh(&pmap->lock);
4409ba682f0Skxie@chelsio.com 		pmap->port_csk[idx] = NULL;
4419ba682f0Skxie@chelsio.com 		pmap->used--;
4429ba682f0Skxie@chelsio.com 		spin_unlock_bh(&pmap->lock);
4439ba682f0Skxie@chelsio.com 
4449ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_SOCK,
4459ba682f0Skxie@chelsio.com 			"cdev 0x%p, p#%u %s, release %u.\n",
4469ba682f0Skxie@chelsio.com 			cdev, csk->port_id, cdev->ports[csk->port_id]->name,
4479ba682f0Skxie@chelsio.com 			pmap->sport_base + idx);
4489ba682f0Skxie@chelsio.com 
4499ba682f0Skxie@chelsio.com 		cxgbi_sock_put(csk);
4509ba682f0Skxie@chelsio.com 	}
4519ba682f0Skxie@chelsio.com }
4529ba682f0Skxie@chelsio.com 
4539ba682f0Skxie@chelsio.com /*
4549ba682f0Skxie@chelsio.com  * iscsi tcp connection
4559ba682f0Skxie@chelsio.com  */
4569ba682f0Skxie@chelsio.com void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
4579ba682f0Skxie@chelsio.com {
4589ba682f0Skxie@chelsio.com 	if (csk->cpl_close) {
4599ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_close);
4609ba682f0Skxie@chelsio.com 		csk->cpl_close = NULL;
4619ba682f0Skxie@chelsio.com 	}
4629ba682f0Skxie@chelsio.com 	if (csk->cpl_abort_req) {
4639ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_abort_req);
4649ba682f0Skxie@chelsio.com 		csk->cpl_abort_req = NULL;
4659ba682f0Skxie@chelsio.com 	}
4669ba682f0Skxie@chelsio.com 	if (csk->cpl_abort_rpl) {
4679ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_abort_rpl);
4689ba682f0Skxie@chelsio.com 		csk->cpl_abort_rpl = NULL;
4699ba682f0Skxie@chelsio.com 	}
4709ba682f0Skxie@chelsio.com }
4719ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
4729ba682f0Skxie@chelsio.com 
4739ba682f0Skxie@chelsio.com static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
4749ba682f0Skxie@chelsio.com {
4759ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
4769ba682f0Skxie@chelsio.com 
4779ba682f0Skxie@chelsio.com 	if (!csk) {
4789ba682f0Skxie@chelsio.com 		pr_info("alloc csk %zu failed.\n", sizeof(*csk));
4799ba682f0Skxie@chelsio.com 		return NULL;
4809ba682f0Skxie@chelsio.com 	}
4819ba682f0Skxie@chelsio.com 
4829ba682f0Skxie@chelsio.com 	if (cdev->csk_alloc_cpls(csk) < 0) {
4839ba682f0Skxie@chelsio.com 		pr_info("csk 0x%p, alloc cpls failed.\n", csk);
4849ba682f0Skxie@chelsio.com 		kfree(csk);
4859ba682f0Skxie@chelsio.com 		return NULL;
4869ba682f0Skxie@chelsio.com 	}
4879ba682f0Skxie@chelsio.com 
4889ba682f0Skxie@chelsio.com 	spin_lock_init(&csk->lock);
4899ba682f0Skxie@chelsio.com 	kref_init(&csk->refcnt);
4909ba682f0Skxie@chelsio.com 	skb_queue_head_init(&csk->receive_queue);
4919ba682f0Skxie@chelsio.com 	skb_queue_head_init(&csk->write_queue);
4929ba682f0Skxie@chelsio.com 	setup_timer(&csk->retry_timer, NULL, (unsigned long)csk);
4939ba682f0Skxie@chelsio.com 	rwlock_init(&csk->callback_lock);
4949ba682f0Skxie@chelsio.com 	csk->cdev = cdev;
4959ba682f0Skxie@chelsio.com 	csk->flags = 0;
4969ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CLOSED);
4979ba682f0Skxie@chelsio.com 
4989ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
4999ba682f0Skxie@chelsio.com 
5009ba682f0Skxie@chelsio.com 	return csk;
5019ba682f0Skxie@chelsio.com }
5029ba682f0Skxie@chelsio.com 
503f4bfd99fSDavid S. Miller static struct rtable *find_route_ipv4(struct flowi4 *fl4,
504f4bfd99fSDavid S. Miller 				      __be32 saddr, __be32 daddr,
5059ba682f0Skxie@chelsio.com 				      __be16 sport, __be16 dport, u8 tos)
5069ba682f0Skxie@chelsio.com {
5079ba682f0Skxie@chelsio.com 	struct rtable *rt;
5089ba682f0Skxie@chelsio.com 
509f4bfd99fSDavid S. Miller 	rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
51078fbfd8aSDavid S. Miller 				   dport, sport, IPPROTO_TCP, tos, 0);
511b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
5129ba682f0Skxie@chelsio.com 		return NULL;
5139ba682f0Skxie@chelsio.com 
5149ba682f0Skxie@chelsio.com 	return rt;
5159ba682f0Skxie@chelsio.com }
5169ba682f0Skxie@chelsio.com 
5179ba682f0Skxie@chelsio.com static struct cxgbi_sock *cxgbi_check_route(struct sockaddr *dst_addr)
5189ba682f0Skxie@chelsio.com {
5199ba682f0Skxie@chelsio.com 	struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
5209ba682f0Skxie@chelsio.com 	struct dst_entry *dst;
5219ba682f0Skxie@chelsio.com 	struct net_device *ndev;
5229ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev;
5239ba682f0Skxie@chelsio.com 	struct rtable *rt = NULL;
524a58b61e5SDavid Miller 	struct neighbour *n;
525f4bfd99fSDavid S. Miller 	struct flowi4 fl4;
5269ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = NULL;
5279ba682f0Skxie@chelsio.com 	unsigned int mtu = 0;
5289ba682f0Skxie@chelsio.com 	int port = 0xFFFF;
5299ba682f0Skxie@chelsio.com 	int err = 0;
5309ba682f0Skxie@chelsio.com 
531f4bfd99fSDavid S. Miller 	rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0, daddr->sin_port, 0);
5329ba682f0Skxie@chelsio.com 	if (!rt) {
5339ba682f0Skxie@chelsio.com 		pr_info("no route to ipv4 0x%x, port %u.\n",
534fc8d0590SAnish Bhatt 			be32_to_cpu(daddr->sin_addr.s_addr),
535fc8d0590SAnish Bhatt 			be16_to_cpu(daddr->sin_port));
5369ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
5379ba682f0Skxie@chelsio.com 		goto err_out;
5389ba682f0Skxie@chelsio.com 	}
5399ba682f0Skxie@chelsio.com 	dst = &rt->dst;
5400b399d46SDavid S. Miller 	n = dst_neigh_lookup(dst, &daddr->sin_addr.s_addr);
541a58b61e5SDavid Miller 	if (!n) {
542a58b61e5SDavid Miller 		err = -ENODEV;
543a58b61e5SDavid Miller 		goto rel_rt;
544a58b61e5SDavid Miller 	}
545a58b61e5SDavid Miller 	ndev = n->dev;
5469ba682f0Skxie@chelsio.com 
5479ba682f0Skxie@chelsio.com 	if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
5489ba682f0Skxie@chelsio.com 		pr_info("multi-cast route %pI4, port %u, dev %s.\n",
5499ba682f0Skxie@chelsio.com 			&daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
5509ba682f0Skxie@chelsio.com 			ndev->name);
5519ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
5520b399d46SDavid S. Miller 		goto rel_neigh;
5539ba682f0Skxie@chelsio.com 	}
5549ba682f0Skxie@chelsio.com 
5559ba682f0Skxie@chelsio.com 	if (ndev->flags & IFF_LOOPBACK) {
5569ba682f0Skxie@chelsio.com 		ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
5579ba682f0Skxie@chelsio.com 		mtu = ndev->mtu;
5589ba682f0Skxie@chelsio.com 		pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
559a58b61e5SDavid Miller 			n->dev->name, ndev->name, mtu);
5609ba682f0Skxie@chelsio.com 	}
5619ba682f0Skxie@chelsio.com 
5629ba682f0Skxie@chelsio.com 	cdev = cxgbi_device_find_by_netdev(ndev, &port);
5639ba682f0Skxie@chelsio.com 	if (!cdev) {
5649ba682f0Skxie@chelsio.com 		pr_info("dst %pI4, %s, NOT cxgbi device.\n",
5659ba682f0Skxie@chelsio.com 			&daddr->sin_addr.s_addr, ndev->name);
5669ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
5670b399d46SDavid S. Miller 		goto rel_neigh;
5689ba682f0Skxie@chelsio.com 	}
5699ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK,
5709ba682f0Skxie@chelsio.com 		"route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
5719ba682f0Skxie@chelsio.com 		&daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
5729ba682f0Skxie@chelsio.com 			   port, ndev->name, cdev);
5739ba682f0Skxie@chelsio.com 
5749ba682f0Skxie@chelsio.com 	csk = cxgbi_sock_create(cdev);
5759ba682f0Skxie@chelsio.com 	if (!csk) {
5769ba682f0Skxie@chelsio.com 		err = -ENOMEM;
5770b399d46SDavid S. Miller 		goto rel_neigh;
5789ba682f0Skxie@chelsio.com 	}
5799ba682f0Skxie@chelsio.com 	csk->cdev = cdev;
5809ba682f0Skxie@chelsio.com 	csk->port_id = port;
5819ba682f0Skxie@chelsio.com 	csk->mtu = mtu;
5829ba682f0Skxie@chelsio.com 	csk->dst = dst;
583fc8d0590SAnish Bhatt 
584fc8d0590SAnish Bhatt 	csk->csk_family = AF_INET;
5859ba682f0Skxie@chelsio.com 	csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
5869ba682f0Skxie@chelsio.com 	csk->daddr.sin_port = daddr->sin_port;
587c71b9b66SMike Christie 	csk->daddr.sin_family = daddr->sin_family;
588fc8d0590SAnish Bhatt 	csk->saddr.sin_family = daddr->sin_family;
589f4bfd99fSDavid S. Miller 	csk->saddr.sin_addr.s_addr = fl4.saddr;
5900b399d46SDavid S. Miller 	neigh_release(n);
5919ba682f0Skxie@chelsio.com 
5929ba682f0Skxie@chelsio.com 	return csk;
5939ba682f0Skxie@chelsio.com 
5940b399d46SDavid S. Miller rel_neigh:
5950b399d46SDavid S. Miller 	neigh_release(n);
5960b399d46SDavid S. Miller 
5979ba682f0Skxie@chelsio.com rel_rt:
5989ba682f0Skxie@chelsio.com 	ip_rt_put(rt);
5999ba682f0Skxie@chelsio.com 	if (csk)
6009ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
6019ba682f0Skxie@chelsio.com err_out:
6029ba682f0Skxie@chelsio.com 	return ERR_PTR(err);
6039ba682f0Skxie@chelsio.com }
6049ba682f0Skxie@chelsio.com 
605fc8d0590SAnish Bhatt static struct rt6_info *find_route_ipv6(const struct in6_addr *saddr,
606fc8d0590SAnish Bhatt 					const struct in6_addr *daddr)
607fc8d0590SAnish Bhatt {
608fc8d0590SAnish Bhatt 	struct flowi6 fl;
609fc8d0590SAnish Bhatt 
610fc8d0590SAnish Bhatt 	if (saddr)
611fc8d0590SAnish Bhatt 		memcpy(&fl.saddr, saddr, sizeof(struct in6_addr));
612fc8d0590SAnish Bhatt 	if (daddr)
613fc8d0590SAnish Bhatt 		memcpy(&fl.daddr, daddr, sizeof(struct in6_addr));
614fc8d0590SAnish Bhatt 	return (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
615fc8d0590SAnish Bhatt }
616fc8d0590SAnish Bhatt 
617fc8d0590SAnish Bhatt static struct cxgbi_sock *cxgbi_check_route6(struct sockaddr *dst_addr)
618fc8d0590SAnish Bhatt {
619fc8d0590SAnish Bhatt 	struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)dst_addr;
620fc8d0590SAnish Bhatt 	struct dst_entry *dst;
621fc8d0590SAnish Bhatt 	struct net_device *ndev;
622fc8d0590SAnish Bhatt 	struct cxgbi_device *cdev;
623fc8d0590SAnish Bhatt 	struct rt6_info *rt = NULL;
624fc8d0590SAnish Bhatt 	struct neighbour *n;
625fc8d0590SAnish Bhatt 	struct in6_addr pref_saddr;
626fc8d0590SAnish Bhatt 	struct cxgbi_sock *csk = NULL;
627fc8d0590SAnish Bhatt 	unsigned int mtu = 0;
628fc8d0590SAnish Bhatt 	int port = 0xFFFF;
629fc8d0590SAnish Bhatt 	int err = 0;
630fc8d0590SAnish Bhatt 
631fc8d0590SAnish Bhatt 	rt = find_route_ipv6(NULL, &daddr6->sin6_addr);
632fc8d0590SAnish Bhatt 
633fc8d0590SAnish Bhatt 	if (!rt) {
634fc8d0590SAnish Bhatt 		pr_info("no route to ipv6 %pI6 port %u\n",
635fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
636fc8d0590SAnish Bhatt 			be16_to_cpu(daddr6->sin6_port));
637fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
638fc8d0590SAnish Bhatt 		goto err_out;
639fc8d0590SAnish Bhatt 	}
640fc8d0590SAnish Bhatt 
641fc8d0590SAnish Bhatt 	dst = &rt->dst;
642fc8d0590SAnish Bhatt 
643fc8d0590SAnish Bhatt 	n = dst_neigh_lookup(dst, &daddr6->sin6_addr);
644fc8d0590SAnish Bhatt 
645fc8d0590SAnish Bhatt 	if (!n) {
646fc8d0590SAnish Bhatt 		pr_info("%pI6, port %u, dst no neighbour.\n",
647fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
648fc8d0590SAnish Bhatt 			be16_to_cpu(daddr6->sin6_port));
649fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
650fc8d0590SAnish Bhatt 		goto rel_rt;
651fc8d0590SAnish Bhatt 	}
652fc8d0590SAnish Bhatt 	ndev = n->dev;
653fc8d0590SAnish Bhatt 
654fc8d0590SAnish Bhatt 	if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
655fc8d0590SAnish Bhatt 		pr_info("multi-cast route %pI6 port %u, dev %s.\n",
656fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
657fc8d0590SAnish Bhatt 			ntohs(daddr6->sin6_port), ndev->name);
658fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
659fc8d0590SAnish Bhatt 		goto rel_rt;
660fc8d0590SAnish Bhatt 	}
661fc8d0590SAnish Bhatt 
662fc8d0590SAnish Bhatt 	cdev = cxgbi_device_find_by_netdev(ndev, &port);
663fc8d0590SAnish Bhatt 	if (!cdev)
664fc8d0590SAnish Bhatt 		cdev = cxgbi_device_find_by_mac(ndev, &port);
665fc8d0590SAnish Bhatt 	if (!cdev) {
666fc8d0590SAnish Bhatt 		pr_info("dst %pI6 %s, NOT cxgbi device.\n",
667fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr, ndev->name);
668fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
669fc8d0590SAnish Bhatt 		goto rel_rt;
670fc8d0590SAnish Bhatt 	}
671fc8d0590SAnish Bhatt 	log_debug(1 << CXGBI_DBG_SOCK,
672fc8d0590SAnish Bhatt 		  "route to %pI6 :%u, ndev p#%d,%s, cdev 0x%p.\n",
673fc8d0590SAnish Bhatt 		  daddr6->sin6_addr.s6_addr, ntohs(daddr6->sin6_port), port,
674fc8d0590SAnish Bhatt 		  ndev->name, cdev);
675fc8d0590SAnish Bhatt 
676fc8d0590SAnish Bhatt 	csk = cxgbi_sock_create(cdev);
677fc8d0590SAnish Bhatt 	if (!csk) {
678fc8d0590SAnish Bhatt 		err = -ENOMEM;
679fc8d0590SAnish Bhatt 		goto rel_rt;
680fc8d0590SAnish Bhatt 	}
681fc8d0590SAnish Bhatt 	csk->cdev = cdev;
682fc8d0590SAnish Bhatt 	csk->port_id = port;
683fc8d0590SAnish Bhatt 	csk->mtu = mtu;
684fc8d0590SAnish Bhatt 	csk->dst = dst;
685fc8d0590SAnish Bhatt 
686fc8d0590SAnish Bhatt 	if (ipv6_addr_any(&rt->rt6i_prefsrc.addr)) {
687fc8d0590SAnish Bhatt 		struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
688fc8d0590SAnish Bhatt 
689fc8d0590SAnish Bhatt 		err = ipv6_dev_get_saddr(&init_net, idev ? idev->dev : NULL,
690fc8d0590SAnish Bhatt 					 &daddr6->sin6_addr, 0, &pref_saddr);
691fc8d0590SAnish Bhatt 		if (err) {
692fc8d0590SAnish Bhatt 			pr_info("failed to get source address to reach %pI6\n",
693fc8d0590SAnish Bhatt 				&daddr6->sin6_addr);
694fc8d0590SAnish Bhatt 			goto rel_rt;
695fc8d0590SAnish Bhatt 		}
696fc8d0590SAnish Bhatt 	} else {
697fc8d0590SAnish Bhatt 		pref_saddr = rt->rt6i_prefsrc.addr;
698fc8d0590SAnish Bhatt 	}
699fc8d0590SAnish Bhatt 
700fc8d0590SAnish Bhatt 	csk->csk_family = AF_INET6;
701fc8d0590SAnish Bhatt 	csk->daddr6.sin6_addr = daddr6->sin6_addr;
702fc8d0590SAnish Bhatt 	csk->daddr6.sin6_port = daddr6->sin6_port;
703fc8d0590SAnish Bhatt 	csk->daddr6.sin6_family = daddr6->sin6_family;
704fc8d0590SAnish Bhatt 	csk->saddr6.sin6_addr = pref_saddr;
705fc8d0590SAnish Bhatt 
706fc8d0590SAnish Bhatt 	neigh_release(n);
707fc8d0590SAnish Bhatt 	return csk;
708fc8d0590SAnish Bhatt 
709fc8d0590SAnish Bhatt rel_rt:
710fc8d0590SAnish Bhatt 	if (n)
711fc8d0590SAnish Bhatt 		neigh_release(n);
712fc8d0590SAnish Bhatt 
713fc8d0590SAnish Bhatt 	ip6_rt_put(rt);
714fc8d0590SAnish Bhatt 	if (csk)
715fc8d0590SAnish Bhatt 		cxgbi_sock_closed(csk);
716fc8d0590SAnish Bhatt err_out:
717fc8d0590SAnish Bhatt 	return ERR_PTR(err);
718fc8d0590SAnish Bhatt }
719fc8d0590SAnish Bhatt 
7209ba682f0Skxie@chelsio.com void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
7219ba682f0Skxie@chelsio.com 			unsigned int opt)
7229ba682f0Skxie@chelsio.com {
7239ba682f0Skxie@chelsio.com 	csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
7249ba682f0Skxie@chelsio.com 	dst_confirm(csk->dst);
7259ba682f0Skxie@chelsio.com 	smp_mb();
7269ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
7279ba682f0Skxie@chelsio.com }
7289ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_established);
7299ba682f0Skxie@chelsio.com 
7309ba682f0Skxie@chelsio.com static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
7319ba682f0Skxie@chelsio.com {
7329ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK,
7339ba682f0Skxie@chelsio.com 		"csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
7349ba682f0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->user_data);
7359ba682f0Skxie@chelsio.com 
7369ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED) {
737e3d2ad8cSkxie@chelsio.com 		read_lock_bh(&csk->callback_lock);
7389ba682f0Skxie@chelsio.com 		if (csk->user_data)
7399ba682f0Skxie@chelsio.com 			iscsi_conn_failure(csk->user_data,
7409ba682f0Skxie@chelsio.com 					ISCSI_ERR_CONN_FAILED);
741e3d2ad8cSkxie@chelsio.com 		read_unlock_bh(&csk->callback_lock);
7429ba682f0Skxie@chelsio.com 	}
7439ba682f0Skxie@chelsio.com }
7449ba682f0Skxie@chelsio.com 
7459ba682f0Skxie@chelsio.com void cxgbi_sock_closed(struct cxgbi_sock *csk)
7469ba682f0Skxie@chelsio.com {
7479ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
7489ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
7499ba682f0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
7509ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
7519ba682f0Skxie@chelsio.com 		return;
7529ba682f0Skxie@chelsio.com 	if (csk->saddr.sin_port)
7539ba682f0Skxie@chelsio.com 		sock_put_port(csk);
7549ba682f0Skxie@chelsio.com 	if (csk->dst)
7559ba682f0Skxie@chelsio.com 		dst_release(csk->dst);
7569ba682f0Skxie@chelsio.com 	csk->cdev->csk_release_offload_resources(csk);
7579ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CLOSED);
7589ba682f0Skxie@chelsio.com 	cxgbi_inform_iscsi_conn_closing(csk);
7599ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
7609ba682f0Skxie@chelsio.com }
7619ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
7629ba682f0Skxie@chelsio.com 
7639ba682f0Skxie@chelsio.com static void need_active_close(struct cxgbi_sock *csk)
7649ba682f0Skxie@chelsio.com {
7659ba682f0Skxie@chelsio.com 	int data_lost;
7669ba682f0Skxie@chelsio.com 	int close_req = 0;
7679ba682f0Skxie@chelsio.com 
7689ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
7699ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
7709ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
7719ba682f0Skxie@chelsio.com 	dst_confirm(csk->dst);
7729ba682f0Skxie@chelsio.com 	data_lost = skb_queue_len(&csk->receive_queue);
7739ba682f0Skxie@chelsio.com 	__skb_queue_purge(&csk->receive_queue);
7749ba682f0Skxie@chelsio.com 
7759ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN)
7769ba682f0Skxie@chelsio.com 		cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
7779ba682f0Skxie@chelsio.com 	else if (csk->state == CTP_ESTABLISHED) {
7789ba682f0Skxie@chelsio.com 		close_req = 1;
7799ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
7809ba682f0Skxie@chelsio.com 	} else if (csk->state == CTP_PASSIVE_CLOSE) {
7819ba682f0Skxie@chelsio.com 		close_req = 1;
7829ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
7839ba682f0Skxie@chelsio.com 	}
7849ba682f0Skxie@chelsio.com 
7859ba682f0Skxie@chelsio.com 	if (close_req) {
7869ba682f0Skxie@chelsio.com 		if (data_lost)
7879ba682f0Skxie@chelsio.com 			csk->cdev->csk_send_abort_req(csk);
7889ba682f0Skxie@chelsio.com 		else
7899ba682f0Skxie@chelsio.com 			csk->cdev->csk_send_close_req(csk);
7909ba682f0Skxie@chelsio.com 	}
7919ba682f0Skxie@chelsio.com 
7929ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
7939ba682f0Skxie@chelsio.com }
7949ba682f0Skxie@chelsio.com 
7959ba682f0Skxie@chelsio.com void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
7969ba682f0Skxie@chelsio.com {
7979ba682f0Skxie@chelsio.com 	pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
7989ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags,
7999ba682f0Skxie@chelsio.com 			&csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
8009ba682f0Skxie@chelsio.com 			&csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
8019ba682f0Skxie@chelsio.com 			errno);
8029ba682f0Skxie@chelsio.com 
8039ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CONNECTING);
8049ba682f0Skxie@chelsio.com 	csk->err = errno;
8059ba682f0Skxie@chelsio.com 	cxgbi_sock_closed(csk);
8069ba682f0Skxie@chelsio.com }
8079ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
8089ba682f0Skxie@chelsio.com 
8099ba682f0Skxie@chelsio.com void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
8109ba682f0Skxie@chelsio.com {
8119ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
8129ba682f0Skxie@chelsio.com 
8139ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8149ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8159ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
8169ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
8179ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN)
8189ba682f0Skxie@chelsio.com 		cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
8199ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
8209ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
8219ba682f0Skxie@chelsio.com 	__kfree_skb(skb);
8229ba682f0Skxie@chelsio.com }
8239ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
8249ba682f0Skxie@chelsio.com 
8259ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
8269ba682f0Skxie@chelsio.com {
8279ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
8289ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
8299ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
8309ba682f0Skxie@chelsio.com 		if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_RCVD))
8319ba682f0Skxie@chelsio.com 			cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
8329ba682f0Skxie@chelsio.com 		else {
8339ba682f0Skxie@chelsio.com 			cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_RCVD);
8349ba682f0Skxie@chelsio.com 			cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
8359ba682f0Skxie@chelsio.com 			if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
8369ba682f0Skxie@chelsio.com 				pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
8379ba682f0Skxie@chelsio.com 					csk, csk->state, csk->flags, csk->tid);
8389ba682f0Skxie@chelsio.com 			cxgbi_sock_closed(csk);
8399ba682f0Skxie@chelsio.com 		}
8409ba682f0Skxie@chelsio.com 	}
8419ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
8429ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
8439ba682f0Skxie@chelsio.com }
8449ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
8459ba682f0Skxie@chelsio.com 
8469ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
8479ba682f0Skxie@chelsio.com {
8489ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8499ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8509ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
8519ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
8529ba682f0Skxie@chelsio.com 
8539ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
8549ba682f0Skxie@chelsio.com 		goto done;
8559ba682f0Skxie@chelsio.com 
8569ba682f0Skxie@chelsio.com 	switch (csk->state) {
8579ba682f0Skxie@chelsio.com 	case CTP_ESTABLISHED:
8589ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
8599ba682f0Skxie@chelsio.com 		break;
8609ba682f0Skxie@chelsio.com 	case CTP_ACTIVE_CLOSE:
8619ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
8629ba682f0Skxie@chelsio.com 		break;
8639ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_1:
8649ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
8659ba682f0Skxie@chelsio.com 		break;
8669ba682f0Skxie@chelsio.com 	case CTP_ABORTING:
8679ba682f0Skxie@chelsio.com 		break;
8689ba682f0Skxie@chelsio.com 	default:
8699ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
8709ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
8719ba682f0Skxie@chelsio.com 	}
8729ba682f0Skxie@chelsio.com 	cxgbi_inform_iscsi_conn_closing(csk);
8739ba682f0Skxie@chelsio.com done:
8749ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
8759ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
8769ba682f0Skxie@chelsio.com }
8779ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
8789ba682f0Skxie@chelsio.com 
8799ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
8809ba682f0Skxie@chelsio.com {
8819ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8829ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8839ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
8849ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
8859ba682f0Skxie@chelsio.com 
8869ba682f0Skxie@chelsio.com 	csk->snd_una = snd_nxt - 1;
8879ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
8889ba682f0Skxie@chelsio.com 		goto done;
8899ba682f0Skxie@chelsio.com 
8909ba682f0Skxie@chelsio.com 	switch (csk->state) {
8919ba682f0Skxie@chelsio.com 	case CTP_ACTIVE_CLOSE:
8929ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
8939ba682f0Skxie@chelsio.com 		break;
8949ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_1:
8959ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_2:
8969ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
8979ba682f0Skxie@chelsio.com 		break;
8989ba682f0Skxie@chelsio.com 	case CTP_ABORTING:
8999ba682f0Skxie@chelsio.com 		break;
9009ba682f0Skxie@chelsio.com 	default:
9019ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
9029ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
9039ba682f0Skxie@chelsio.com 	}
9049ba682f0Skxie@chelsio.com done:
9059ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9069ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9079ba682f0Skxie@chelsio.com }
9089ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
9099ba682f0Skxie@chelsio.com 
9109ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
9119ba682f0Skxie@chelsio.com 			   unsigned int snd_una, int seq_chk)
9129ba682f0Skxie@chelsio.com {
9139ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
9149ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
9159ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, credits,
9169ba682f0Skxie@chelsio.com 			csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
9179ba682f0Skxie@chelsio.com 
9189ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9199ba682f0Skxie@chelsio.com 
9209ba682f0Skxie@chelsio.com 	csk->wr_cred += credits;
9219ba682f0Skxie@chelsio.com 	if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
9229ba682f0Skxie@chelsio.com 		csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
9239ba682f0Skxie@chelsio.com 
9249ba682f0Skxie@chelsio.com 	while (credits) {
9259ba682f0Skxie@chelsio.com 		struct sk_buff *p = cxgbi_sock_peek_wr(csk);
9269ba682f0Skxie@chelsio.com 
9279ba682f0Skxie@chelsio.com 		if (unlikely(!p)) {
9289ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
9299ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid, credits,
9309ba682f0Skxie@chelsio.com 				csk->wr_cred, csk->wr_una_cred);
9319ba682f0Skxie@chelsio.com 			break;
9329ba682f0Skxie@chelsio.com 		}
9339ba682f0Skxie@chelsio.com 
9349ba682f0Skxie@chelsio.com 		if (unlikely(credits < p->csum)) {
9359ba682f0Skxie@chelsio.com 			pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
9369ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid,
9379ba682f0Skxie@chelsio.com 				credits, csk->wr_cred, csk->wr_una_cred,
9389ba682f0Skxie@chelsio.com 				p->csum);
9399ba682f0Skxie@chelsio.com 			p->csum -= credits;
9409ba682f0Skxie@chelsio.com 			break;
9419ba682f0Skxie@chelsio.com 		} else {
9429ba682f0Skxie@chelsio.com 			cxgbi_sock_dequeue_wr(csk);
9439ba682f0Skxie@chelsio.com 			credits -= p->csum;
9449ba682f0Skxie@chelsio.com 			kfree_skb(p);
9459ba682f0Skxie@chelsio.com 		}
9469ba682f0Skxie@chelsio.com 	}
9479ba682f0Skxie@chelsio.com 
9489ba682f0Skxie@chelsio.com 	cxgbi_sock_check_wr_invariants(csk);
9499ba682f0Skxie@chelsio.com 
9509ba682f0Skxie@chelsio.com 	if (seq_chk) {
9519ba682f0Skxie@chelsio.com 		if (unlikely(before(snd_una, csk->snd_una))) {
9529ba682f0Skxie@chelsio.com 			pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
9539ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid, snd_una,
9549ba682f0Skxie@chelsio.com 				csk->snd_una);
9559ba682f0Skxie@chelsio.com 			goto done;
9569ba682f0Skxie@chelsio.com 		}
9579ba682f0Skxie@chelsio.com 
9589ba682f0Skxie@chelsio.com 		if (csk->snd_una != snd_una) {
9599ba682f0Skxie@chelsio.com 			csk->snd_una = snd_una;
9609ba682f0Skxie@chelsio.com 			dst_confirm(csk->dst);
9619ba682f0Skxie@chelsio.com 		}
9629ba682f0Skxie@chelsio.com 	}
9639ba682f0Skxie@chelsio.com 
9649ba682f0Skxie@chelsio.com 	if (skb_queue_len(&csk->write_queue)) {
9659ba682f0Skxie@chelsio.com 		if (csk->cdev->csk_push_tx_frames(csk, 0))
9669ba682f0Skxie@chelsio.com 			cxgbi_conn_tx_open(csk);
9679ba682f0Skxie@chelsio.com 	} else
9689ba682f0Skxie@chelsio.com 		cxgbi_conn_tx_open(csk);
9699ba682f0Skxie@chelsio.com done:
9709ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9719ba682f0Skxie@chelsio.com }
9729ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
9739ba682f0Skxie@chelsio.com 
9749ba682f0Skxie@chelsio.com static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
9759ba682f0Skxie@chelsio.com 					     unsigned short mtu)
9769ba682f0Skxie@chelsio.com {
9779ba682f0Skxie@chelsio.com 	int i = 0;
9789ba682f0Skxie@chelsio.com 
9799ba682f0Skxie@chelsio.com 	while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
9809ba682f0Skxie@chelsio.com 		++i;
9819ba682f0Skxie@chelsio.com 
9829ba682f0Skxie@chelsio.com 	return i;
9839ba682f0Skxie@chelsio.com }
9849ba682f0Skxie@chelsio.com 
9859ba682f0Skxie@chelsio.com unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
9869ba682f0Skxie@chelsio.com {
9879ba682f0Skxie@chelsio.com 	unsigned int idx;
9889ba682f0Skxie@chelsio.com 	struct dst_entry *dst = csk->dst;
9899ba682f0Skxie@chelsio.com 
9900dbaee3bSDavid S. Miller 	csk->advmss = dst_metric_advmss(dst);
9919ba682f0Skxie@chelsio.com 
9929ba682f0Skxie@chelsio.com 	if (csk->advmss > pmtu - 40)
9939ba682f0Skxie@chelsio.com 		csk->advmss = pmtu - 40;
9949ba682f0Skxie@chelsio.com 	if (csk->advmss < csk->cdev->mtus[0] - 40)
9959ba682f0Skxie@chelsio.com 		csk->advmss = csk->cdev->mtus[0] - 40;
9969ba682f0Skxie@chelsio.com 	idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
9979ba682f0Skxie@chelsio.com 
9989ba682f0Skxie@chelsio.com 	return idx;
9999ba682f0Skxie@chelsio.com }
10009ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
10019ba682f0Skxie@chelsio.com 
10029ba682f0Skxie@chelsio.com void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
10039ba682f0Skxie@chelsio.com {
10049ba682f0Skxie@chelsio.com 	cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
10059ba682f0Skxie@chelsio.com 	__skb_queue_tail(&csk->write_queue, skb);
10069ba682f0Skxie@chelsio.com }
10079ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
10089ba682f0Skxie@chelsio.com 
10099ba682f0Skxie@chelsio.com void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
10109ba682f0Skxie@chelsio.com {
10119ba682f0Skxie@chelsio.com 	struct sk_buff *skb;
10129ba682f0Skxie@chelsio.com 
10139ba682f0Skxie@chelsio.com 	while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
10149ba682f0Skxie@chelsio.com 		kfree_skb(skb);
10159ba682f0Skxie@chelsio.com }
10169ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
10179ba682f0Skxie@chelsio.com 
10189ba682f0Skxie@chelsio.com void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
10199ba682f0Skxie@chelsio.com {
10209ba682f0Skxie@chelsio.com 	int pending = cxgbi_sock_count_pending_wrs(csk);
10219ba682f0Skxie@chelsio.com 
10229ba682f0Skxie@chelsio.com 	if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
10239ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
10249ba682f0Skxie@chelsio.com 			csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
10259ba682f0Skxie@chelsio.com }
10269ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
10279ba682f0Skxie@chelsio.com 
10289ba682f0Skxie@chelsio.com static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
10299ba682f0Skxie@chelsio.com {
10309ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
10319ba682f0Skxie@chelsio.com 	struct sk_buff *next;
10329ba682f0Skxie@chelsio.com 	int err, copied = 0;
10339ba682f0Skxie@chelsio.com 
10349ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
10359ba682f0Skxie@chelsio.com 
10369ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED) {
10379ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
10389ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
10399ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
10409ba682f0Skxie@chelsio.com 		err = -EAGAIN;
10419ba682f0Skxie@chelsio.com 		goto out_err;
10429ba682f0Skxie@chelsio.com 	}
10439ba682f0Skxie@chelsio.com 
10449ba682f0Skxie@chelsio.com 	if (csk->err) {
10459ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
10469ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
10479ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, csk->err);
10489ba682f0Skxie@chelsio.com 		err = -EPIPE;
10499ba682f0Skxie@chelsio.com 		goto out_err;
10509ba682f0Skxie@chelsio.com 	}
10519ba682f0Skxie@chelsio.com 
10529ba682f0Skxie@chelsio.com 	if (csk->write_seq - csk->snd_una >= cdev->snd_win) {
10539ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
10549ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
10559ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, csk->write_seq,
10569ba682f0Skxie@chelsio.com 			csk->snd_una, cdev->snd_win);
10579ba682f0Skxie@chelsio.com 		err = -ENOBUFS;
10589ba682f0Skxie@chelsio.com 		goto out_err;
10599ba682f0Skxie@chelsio.com 	}
10609ba682f0Skxie@chelsio.com 
10619ba682f0Skxie@chelsio.com 	while (skb) {
10629ba682f0Skxie@chelsio.com 		int frags = skb_shinfo(skb)->nr_frags +
10639ba682f0Skxie@chelsio.com 				(skb->len != skb->data_len);
10649ba682f0Skxie@chelsio.com 
10659ba682f0Skxie@chelsio.com 		if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
10669ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p, skb head %u < %u.\n",
10679ba682f0Skxie@chelsio.com 				csk, skb_headroom(skb), cdev->skb_tx_rsvd);
10689ba682f0Skxie@chelsio.com 			err = -EINVAL;
10699ba682f0Skxie@chelsio.com 			goto out_err;
10709ba682f0Skxie@chelsio.com 		}
10719ba682f0Skxie@chelsio.com 
10729ba682f0Skxie@chelsio.com 		if (frags >= SKB_WR_LIST_SIZE) {
10739ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
10749ba682f0Skxie@chelsio.com 				csk, skb_shinfo(skb)->nr_frags, skb->len,
10759ba682f0Skxie@chelsio.com 				skb->data_len, (uint)(SKB_WR_LIST_SIZE));
10769ba682f0Skxie@chelsio.com 			err = -EINVAL;
10779ba682f0Skxie@chelsio.com 			goto out_err;
10789ba682f0Skxie@chelsio.com 		}
10799ba682f0Skxie@chelsio.com 
10809ba682f0Skxie@chelsio.com 		next = skb->next;
10819ba682f0Skxie@chelsio.com 		skb->next = NULL;
10829ba682f0Skxie@chelsio.com 		cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
10839ba682f0Skxie@chelsio.com 		cxgbi_sock_skb_entail(csk, skb);
10849ba682f0Skxie@chelsio.com 		copied += skb->len;
10859ba682f0Skxie@chelsio.com 		csk->write_seq += skb->len +
10869ba682f0Skxie@chelsio.com 				cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
10879ba682f0Skxie@chelsio.com 		skb = next;
10889ba682f0Skxie@chelsio.com 	}
10899ba682f0Skxie@chelsio.com done:
10909ba682f0Skxie@chelsio.com 	if (likely(skb_queue_len(&csk->write_queue)))
10919ba682f0Skxie@chelsio.com 		cdev->csk_push_tx_frames(csk, 1);
10929ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10939ba682f0Skxie@chelsio.com 	return copied;
10949ba682f0Skxie@chelsio.com 
10959ba682f0Skxie@chelsio.com out_err:
10969ba682f0Skxie@chelsio.com 	if (copied == 0 && err == -EPIPE)
10979ba682f0Skxie@chelsio.com 		copied = csk->err ? csk->err : -EPIPE;
10989ba682f0Skxie@chelsio.com 	else
10999ba682f0Skxie@chelsio.com 		copied = err;
11009ba682f0Skxie@chelsio.com 	goto done;
11019ba682f0Skxie@chelsio.com }
11029ba682f0Skxie@chelsio.com 
11039ba682f0Skxie@chelsio.com /*
11049ba682f0Skxie@chelsio.com  * Direct Data Placement -
11059ba682f0Skxie@chelsio.com  * Directly place the iSCSI Data-In or Data-Out PDU's payload into pre-posted
11069ba682f0Skxie@chelsio.com  * final destination host-memory buffers based on the Initiator Task Tag (ITT)
11079ba682f0Skxie@chelsio.com  * in Data-In or Target Task Tag (TTT) in Data-Out PDUs.
11089ba682f0Skxie@chelsio.com  * The host memory address is programmed into h/w in the format of pagepod
11099ba682f0Skxie@chelsio.com  * entries.
11109ba682f0Skxie@chelsio.com  * The location of the pagepod entry is encoded into ddp tag which is used as
11119ba682f0Skxie@chelsio.com  * the base for ITT/TTT.
11129ba682f0Skxie@chelsio.com  */
11139ba682f0Skxie@chelsio.com 
11149ba682f0Skxie@chelsio.com static unsigned char ddp_page_order[DDP_PGIDX_MAX] = {0, 1, 2, 4};
11159ba682f0Skxie@chelsio.com static unsigned char ddp_page_shift[DDP_PGIDX_MAX] = {12, 13, 14, 16};
11169ba682f0Skxie@chelsio.com static unsigned char page_idx = DDP_PGIDX_MAX;
11179ba682f0Skxie@chelsio.com 
11189ba682f0Skxie@chelsio.com static unsigned char sw_tag_idx_bits;
11199ba682f0Skxie@chelsio.com static unsigned char sw_tag_age_bits;
11209ba682f0Skxie@chelsio.com 
11219ba682f0Skxie@chelsio.com /*
11229ba682f0Skxie@chelsio.com  * Direct-Data Placement page size adjustment
11239ba682f0Skxie@chelsio.com  */
11249ba682f0Skxie@chelsio.com static int ddp_adjust_page_table(void)
11259ba682f0Skxie@chelsio.com {
11269ba682f0Skxie@chelsio.com 	int i;
11279ba682f0Skxie@chelsio.com 	unsigned int base_order, order;
11289ba682f0Skxie@chelsio.com 
11299ba682f0Skxie@chelsio.com 	if (PAGE_SIZE < (1UL << ddp_page_shift[0])) {
11309ba682f0Skxie@chelsio.com 		pr_info("PAGE_SIZE 0x%lx too small, min 0x%lx\n",
11319ba682f0Skxie@chelsio.com 			PAGE_SIZE, 1UL << ddp_page_shift[0]);
11329ba682f0Skxie@chelsio.com 		return -EINVAL;
11339ba682f0Skxie@chelsio.com 	}
11349ba682f0Skxie@chelsio.com 
11359ba682f0Skxie@chelsio.com 	base_order = get_order(1UL << ddp_page_shift[0]);
11369ba682f0Skxie@chelsio.com 	order = get_order(1UL << PAGE_SHIFT);
11379ba682f0Skxie@chelsio.com 
11389ba682f0Skxie@chelsio.com 	for (i = 0; i < DDP_PGIDX_MAX; i++) {
11399ba682f0Skxie@chelsio.com 		/* first is the kernel page size, then just doubling */
11409ba682f0Skxie@chelsio.com 		ddp_page_order[i] = order - base_order + i;
11419ba682f0Skxie@chelsio.com 		ddp_page_shift[i] = PAGE_SHIFT + i;
11429ba682f0Skxie@chelsio.com 	}
11439ba682f0Skxie@chelsio.com 	return 0;
11449ba682f0Skxie@chelsio.com }
11459ba682f0Skxie@chelsio.com 
11469ba682f0Skxie@chelsio.com static int ddp_find_page_index(unsigned long pgsz)
11479ba682f0Skxie@chelsio.com {
11489ba682f0Skxie@chelsio.com 	int i;
11499ba682f0Skxie@chelsio.com 
11509ba682f0Skxie@chelsio.com 	for (i = 0; i < DDP_PGIDX_MAX; i++) {
11519ba682f0Skxie@chelsio.com 		if (pgsz == (1UL << ddp_page_shift[i]))
11529ba682f0Skxie@chelsio.com 			return i;
11539ba682f0Skxie@chelsio.com 	}
11549ba682f0Skxie@chelsio.com 	pr_info("ddp page size %lu not supported.\n", pgsz);
11559ba682f0Skxie@chelsio.com 	return DDP_PGIDX_MAX;
11569ba682f0Skxie@chelsio.com }
11579ba682f0Skxie@chelsio.com 
11589ba682f0Skxie@chelsio.com static void ddp_setup_host_page_size(void)
11599ba682f0Skxie@chelsio.com {
11609ba682f0Skxie@chelsio.com 	if (page_idx == DDP_PGIDX_MAX) {
11619ba682f0Skxie@chelsio.com 		page_idx = ddp_find_page_index(PAGE_SIZE);
11629ba682f0Skxie@chelsio.com 
11639ba682f0Skxie@chelsio.com 		if (page_idx == DDP_PGIDX_MAX) {
11649ba682f0Skxie@chelsio.com 			pr_info("system PAGE %lu, update hw.\n", PAGE_SIZE);
11659ba682f0Skxie@chelsio.com 			if (ddp_adjust_page_table() < 0) {
11669ba682f0Skxie@chelsio.com 				pr_info("PAGE %lu, disable ddp.\n", PAGE_SIZE);
11679ba682f0Skxie@chelsio.com 				return;
11689ba682f0Skxie@chelsio.com 			}
11699ba682f0Skxie@chelsio.com 			page_idx = ddp_find_page_index(PAGE_SIZE);
11709ba682f0Skxie@chelsio.com 		}
11719ba682f0Skxie@chelsio.com 		pr_info("system PAGE %lu, ddp idx %u.\n", PAGE_SIZE, page_idx);
11729ba682f0Skxie@chelsio.com 	}
11739ba682f0Skxie@chelsio.com }
11749ba682f0Skxie@chelsio.com 
11759ba682f0Skxie@chelsio.com void cxgbi_ddp_page_size_factor(int *pgsz_factor)
11769ba682f0Skxie@chelsio.com {
11779ba682f0Skxie@chelsio.com 	int i;
11789ba682f0Skxie@chelsio.com 
11799ba682f0Skxie@chelsio.com 	for (i = 0; i < DDP_PGIDX_MAX; i++)
11809ba682f0Skxie@chelsio.com 		pgsz_factor[i] = ddp_page_order[i];
11819ba682f0Skxie@chelsio.com }
11829ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ddp_page_size_factor);
11839ba682f0Skxie@chelsio.com 
11849ba682f0Skxie@chelsio.com /*
11859ba682f0Skxie@chelsio.com  * DDP setup & teardown
11869ba682f0Skxie@chelsio.com  */
11879ba682f0Skxie@chelsio.com 
11889ba682f0Skxie@chelsio.com void cxgbi_ddp_ppod_set(struct cxgbi_pagepod *ppod,
11899ba682f0Skxie@chelsio.com 			struct cxgbi_pagepod_hdr *hdr,
11909ba682f0Skxie@chelsio.com 			struct cxgbi_gather_list *gl, unsigned int gidx)
11919ba682f0Skxie@chelsio.com {
11929ba682f0Skxie@chelsio.com 	int i;
11939ba682f0Skxie@chelsio.com 
11949ba682f0Skxie@chelsio.com 	memcpy(ppod, hdr, sizeof(*hdr));
11959ba682f0Skxie@chelsio.com 	for (i = 0; i < (PPOD_PAGES_MAX + 1); i++, gidx++) {
11969ba682f0Skxie@chelsio.com 		ppod->addr[i] = gidx < gl->nelem ?
11979ba682f0Skxie@chelsio.com 				cpu_to_be64(gl->phys_addr[gidx]) : 0ULL;
11989ba682f0Skxie@chelsio.com 	}
11999ba682f0Skxie@chelsio.com }
12009ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_set);
12019ba682f0Skxie@chelsio.com 
12029ba682f0Skxie@chelsio.com void cxgbi_ddp_ppod_clear(struct cxgbi_pagepod *ppod)
12039ba682f0Skxie@chelsio.com {
12049ba682f0Skxie@chelsio.com 	memset(ppod, 0, sizeof(*ppod));
12059ba682f0Skxie@chelsio.com }
12069ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_clear);
12079ba682f0Skxie@chelsio.com 
12089ba682f0Skxie@chelsio.com static inline int ddp_find_unused_entries(struct cxgbi_ddp_info *ddp,
12099ba682f0Skxie@chelsio.com 					unsigned int start, unsigned int max,
12109ba682f0Skxie@chelsio.com 					unsigned int count,
12119ba682f0Skxie@chelsio.com 					struct cxgbi_gather_list *gl)
12129ba682f0Skxie@chelsio.com {
12139ba682f0Skxie@chelsio.com 	unsigned int i, j, k;
12149ba682f0Skxie@chelsio.com 
12159ba682f0Skxie@chelsio.com 	/*  not enough entries */
12169ba682f0Skxie@chelsio.com 	if ((max - start) < count) {
12179ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
12189ba682f0Skxie@chelsio.com 			"NOT enough entries %u+%u < %u.\n", start, count, max);
12199ba682f0Skxie@chelsio.com 		return -EBUSY;
12209ba682f0Skxie@chelsio.com 	}
12219ba682f0Skxie@chelsio.com 
12229ba682f0Skxie@chelsio.com 	max -= count;
12239ba682f0Skxie@chelsio.com 	spin_lock(&ddp->map_lock);
12249ba682f0Skxie@chelsio.com 	for (i = start; i < max;) {
12259ba682f0Skxie@chelsio.com 		for (j = 0, k = i; j < count; j++, k++) {
12269ba682f0Skxie@chelsio.com 			if (ddp->gl_map[k])
12279ba682f0Skxie@chelsio.com 				break;
12289ba682f0Skxie@chelsio.com 		}
12299ba682f0Skxie@chelsio.com 		if (j == count) {
12309ba682f0Skxie@chelsio.com 			for (j = 0, k = i; j < count; j++, k++)
12319ba682f0Skxie@chelsio.com 				ddp->gl_map[k] = gl;
12329ba682f0Skxie@chelsio.com 			spin_unlock(&ddp->map_lock);
12339ba682f0Skxie@chelsio.com 			return i;
12349ba682f0Skxie@chelsio.com 		}
12359ba682f0Skxie@chelsio.com 		i += j + 1;
12369ba682f0Skxie@chelsio.com 	}
12379ba682f0Skxie@chelsio.com 	spin_unlock(&ddp->map_lock);
12389ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
12399ba682f0Skxie@chelsio.com 		"NO suitable entries %u available.\n", count);
12409ba682f0Skxie@chelsio.com 	return -EBUSY;
12419ba682f0Skxie@chelsio.com }
12429ba682f0Skxie@chelsio.com 
12439ba682f0Skxie@chelsio.com static inline void ddp_unmark_entries(struct cxgbi_ddp_info *ddp,
12449ba682f0Skxie@chelsio.com 						int start, int count)
12459ba682f0Skxie@chelsio.com {
12469ba682f0Skxie@chelsio.com 	spin_lock(&ddp->map_lock);
12479ba682f0Skxie@chelsio.com 	memset(&ddp->gl_map[start], 0,
12489ba682f0Skxie@chelsio.com 		count * sizeof(struct cxgbi_gather_list *));
12499ba682f0Skxie@chelsio.com 	spin_unlock(&ddp->map_lock);
12509ba682f0Skxie@chelsio.com }
12519ba682f0Skxie@chelsio.com 
12529ba682f0Skxie@chelsio.com static inline void ddp_gl_unmap(struct pci_dev *pdev,
12539ba682f0Skxie@chelsio.com 					struct cxgbi_gather_list *gl)
12549ba682f0Skxie@chelsio.com {
12559ba682f0Skxie@chelsio.com 	int i;
12569ba682f0Skxie@chelsio.com 
12579ba682f0Skxie@chelsio.com 	for (i = 0; i < gl->nelem; i++)
12589ba682f0Skxie@chelsio.com 		dma_unmap_page(&pdev->dev, gl->phys_addr[i], PAGE_SIZE,
12599ba682f0Skxie@chelsio.com 				PCI_DMA_FROMDEVICE);
12609ba682f0Skxie@chelsio.com }
12619ba682f0Skxie@chelsio.com 
12629ba682f0Skxie@chelsio.com static inline int ddp_gl_map(struct pci_dev *pdev,
12639ba682f0Skxie@chelsio.com 				    struct cxgbi_gather_list *gl)
12649ba682f0Skxie@chelsio.com {
12659ba682f0Skxie@chelsio.com 	int i;
12669ba682f0Skxie@chelsio.com 
12679ba682f0Skxie@chelsio.com 	for (i = 0; i < gl->nelem; i++) {
12689ba682f0Skxie@chelsio.com 		gl->phys_addr[i] = dma_map_page(&pdev->dev, gl->pages[i], 0,
12699ba682f0Skxie@chelsio.com 						PAGE_SIZE,
12709ba682f0Skxie@chelsio.com 						PCI_DMA_FROMDEVICE);
12719ba682f0Skxie@chelsio.com 		if (unlikely(dma_mapping_error(&pdev->dev, gl->phys_addr[i]))) {
12729ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_DDP,
12739ba682f0Skxie@chelsio.com 				"page %d 0x%p, 0x%p dma mapping err.\n",
12749ba682f0Skxie@chelsio.com 				i, gl->pages[i], pdev);
12759ba682f0Skxie@chelsio.com 			goto unmap;
12769ba682f0Skxie@chelsio.com 		}
12779ba682f0Skxie@chelsio.com 	}
12789ba682f0Skxie@chelsio.com 	return i;
12799ba682f0Skxie@chelsio.com unmap:
12809ba682f0Skxie@chelsio.com 	if (i) {
12819ba682f0Skxie@chelsio.com 		unsigned int nelem = gl->nelem;
12829ba682f0Skxie@chelsio.com 
12839ba682f0Skxie@chelsio.com 		gl->nelem = i;
12849ba682f0Skxie@chelsio.com 		ddp_gl_unmap(pdev, gl);
12859ba682f0Skxie@chelsio.com 		gl->nelem = nelem;
12869ba682f0Skxie@chelsio.com 	}
12879ba682f0Skxie@chelsio.com 	return -EINVAL;
12889ba682f0Skxie@chelsio.com }
12899ba682f0Skxie@chelsio.com 
12909ba682f0Skxie@chelsio.com static void ddp_release_gl(struct cxgbi_gather_list *gl,
12919ba682f0Skxie@chelsio.com 				  struct pci_dev *pdev)
12929ba682f0Skxie@chelsio.com {
12939ba682f0Skxie@chelsio.com 	ddp_gl_unmap(pdev, gl);
12949ba682f0Skxie@chelsio.com 	kfree(gl);
12959ba682f0Skxie@chelsio.com }
12969ba682f0Skxie@chelsio.com 
12979ba682f0Skxie@chelsio.com static struct cxgbi_gather_list *ddp_make_gl(unsigned int xferlen,
12989ba682f0Skxie@chelsio.com 						    struct scatterlist *sgl,
12999ba682f0Skxie@chelsio.com 						    unsigned int sgcnt,
13009ba682f0Skxie@chelsio.com 						    struct pci_dev *pdev,
13019ba682f0Skxie@chelsio.com 						    gfp_t gfp)
13029ba682f0Skxie@chelsio.com {
13039ba682f0Skxie@chelsio.com 	struct cxgbi_gather_list *gl;
13049ba682f0Skxie@chelsio.com 	struct scatterlist *sg = sgl;
13059ba682f0Skxie@chelsio.com 	struct page *sgpage = sg_page(sg);
13069ba682f0Skxie@chelsio.com 	unsigned int sglen = sg->length;
13079ba682f0Skxie@chelsio.com 	unsigned int sgoffset = sg->offset;
13089ba682f0Skxie@chelsio.com 	unsigned int npages = (xferlen + sgoffset + PAGE_SIZE - 1) >>
13099ba682f0Skxie@chelsio.com 				PAGE_SHIFT;
13109ba682f0Skxie@chelsio.com 	int i = 1, j = 0;
13119ba682f0Skxie@chelsio.com 
13129ba682f0Skxie@chelsio.com 	if (xferlen < DDP_THRESHOLD) {
13139ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
13149ba682f0Skxie@chelsio.com 			"xfer %u < threshold %u, no ddp.\n",
13159ba682f0Skxie@chelsio.com 			xferlen, DDP_THRESHOLD);
13169ba682f0Skxie@chelsio.com 		return NULL;
13179ba682f0Skxie@chelsio.com 	}
13189ba682f0Skxie@chelsio.com 
13199ba682f0Skxie@chelsio.com 	gl = kzalloc(sizeof(struct cxgbi_gather_list) +
13209ba682f0Skxie@chelsio.com 		     npages * (sizeof(dma_addr_t) +
13219ba682f0Skxie@chelsio.com 		     sizeof(struct page *)), gfp);
13229ba682f0Skxie@chelsio.com 	if (!gl) {
13239ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
13249ba682f0Skxie@chelsio.com 			"xfer %u, %u pages, OOM.\n", xferlen, npages);
13259ba682f0Skxie@chelsio.com 		return NULL;
13269ba682f0Skxie@chelsio.com 	}
13279ba682f0Skxie@chelsio.com 
13289ba682f0Skxie@chelsio.com 	 log_debug(1 << CXGBI_DBG_DDP,
13299ba682f0Skxie@chelsio.com 		"xfer %u, sgl %u, gl max %u.\n", xferlen, sgcnt, npages);
13309ba682f0Skxie@chelsio.com 
13319ba682f0Skxie@chelsio.com 	gl->pages = (struct page **)&gl->phys_addr[npages];
13329ba682f0Skxie@chelsio.com 	gl->nelem = npages;
13339ba682f0Skxie@chelsio.com 	gl->length = xferlen;
13349ba682f0Skxie@chelsio.com 	gl->offset = sgoffset;
13359ba682f0Skxie@chelsio.com 	gl->pages[0] = sgpage;
13369ba682f0Skxie@chelsio.com 
13379ba682f0Skxie@chelsio.com 	for (i = 1, sg = sg_next(sgl), j = 0; i < sgcnt;
13389ba682f0Skxie@chelsio.com 		i++, sg = sg_next(sg)) {
13399ba682f0Skxie@chelsio.com 		struct page *page = sg_page(sg);
13409ba682f0Skxie@chelsio.com 
13419ba682f0Skxie@chelsio.com 		if (sgpage == page && sg->offset == sgoffset + sglen)
13429ba682f0Skxie@chelsio.com 			sglen += sg->length;
13439ba682f0Skxie@chelsio.com 		else {
13449ba682f0Skxie@chelsio.com 			/*  make sure the sgl is fit for ddp:
13459ba682f0Skxie@chelsio.com 			 *  each has the same page size, and
13469ba682f0Skxie@chelsio.com 			 *  all of the middle pages are used completely
13479ba682f0Skxie@chelsio.com 			 */
13489ba682f0Skxie@chelsio.com 			if ((j && sgoffset) || ((i != sgcnt - 1) &&
13499ba682f0Skxie@chelsio.com 			    ((sglen + sgoffset) & ~PAGE_MASK))) {
13509ba682f0Skxie@chelsio.com 				log_debug(1 << CXGBI_DBG_DDP,
13519ba682f0Skxie@chelsio.com 					"page %d/%u, %u + %u.\n",
13529ba682f0Skxie@chelsio.com 					i, sgcnt, sgoffset, sglen);
13539ba682f0Skxie@chelsio.com 				goto error_out;
13549ba682f0Skxie@chelsio.com 			}
13559ba682f0Skxie@chelsio.com 
13569ba682f0Skxie@chelsio.com 			j++;
13579ba682f0Skxie@chelsio.com 			if (j == gl->nelem || sg->offset) {
13589ba682f0Skxie@chelsio.com 				log_debug(1 << CXGBI_DBG_DDP,
13599ba682f0Skxie@chelsio.com 					"page %d/%u, offset %u.\n",
13609ba682f0Skxie@chelsio.com 					j, gl->nelem, sg->offset);
13619ba682f0Skxie@chelsio.com 				goto error_out;
13629ba682f0Skxie@chelsio.com 			}
13639ba682f0Skxie@chelsio.com 			gl->pages[j] = page;
13649ba682f0Skxie@chelsio.com 			sglen = sg->length;
13659ba682f0Skxie@chelsio.com 			sgoffset = sg->offset;
13669ba682f0Skxie@chelsio.com 			sgpage = page;
13679ba682f0Skxie@chelsio.com 		}
13689ba682f0Skxie@chelsio.com 	}
13699ba682f0Skxie@chelsio.com 	gl->nelem = ++j;
13709ba682f0Skxie@chelsio.com 
13719ba682f0Skxie@chelsio.com 	if (ddp_gl_map(pdev, gl) < 0)
13729ba682f0Skxie@chelsio.com 		goto error_out;
13739ba682f0Skxie@chelsio.com 
13749ba682f0Skxie@chelsio.com 	return gl;
13759ba682f0Skxie@chelsio.com 
13769ba682f0Skxie@chelsio.com error_out:
13779ba682f0Skxie@chelsio.com 	kfree(gl);
13789ba682f0Skxie@chelsio.com 	return NULL;
13799ba682f0Skxie@chelsio.com }
13809ba682f0Skxie@chelsio.com 
13819ba682f0Skxie@chelsio.com static void ddp_tag_release(struct cxgbi_hba *chba, u32 tag)
13829ba682f0Skxie@chelsio.com {
13839ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = chba->cdev;
13849ba682f0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp = cdev->ddp;
13859ba682f0Skxie@chelsio.com 	u32 idx;
13869ba682f0Skxie@chelsio.com 
13879ba682f0Skxie@chelsio.com 	idx = (tag >> PPOD_IDX_SHIFT) & ddp->idx_mask;
13889ba682f0Skxie@chelsio.com 	if (idx < ddp->nppods) {
13899ba682f0Skxie@chelsio.com 		struct cxgbi_gather_list *gl = ddp->gl_map[idx];
13909ba682f0Skxie@chelsio.com 		unsigned int npods;
13919ba682f0Skxie@chelsio.com 
13929ba682f0Skxie@chelsio.com 		if (!gl || !gl->nelem) {
13939ba682f0Skxie@chelsio.com 			pr_warn("tag 0x%x, idx %u, gl 0x%p, %u.\n",
13949ba682f0Skxie@chelsio.com 				tag, idx, gl, gl ? gl->nelem : 0);
13959ba682f0Skxie@chelsio.com 			return;
13969ba682f0Skxie@chelsio.com 		}
13979ba682f0Skxie@chelsio.com 		npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
13989ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
13999ba682f0Skxie@chelsio.com 			"tag 0x%x, release idx %u, npods %u.\n",
14009ba682f0Skxie@chelsio.com 			tag, idx, npods);
14019ba682f0Skxie@chelsio.com 		cdev->csk_ddp_clear(chba, tag, idx, npods);
14029ba682f0Skxie@chelsio.com 		ddp_unmark_entries(ddp, idx, npods);
14039ba682f0Skxie@chelsio.com 		ddp_release_gl(gl, ddp->pdev);
14049ba682f0Skxie@chelsio.com 	} else
14059ba682f0Skxie@chelsio.com 		pr_warn("tag 0x%x, idx %u > max %u.\n", tag, idx, ddp->nppods);
14069ba682f0Skxie@chelsio.com }
14079ba682f0Skxie@chelsio.com 
14089ba682f0Skxie@chelsio.com static int ddp_tag_reserve(struct cxgbi_sock *csk, unsigned int tid,
14099ba682f0Skxie@chelsio.com 			   u32 sw_tag, u32 *tagp, struct cxgbi_gather_list *gl,
14109ba682f0Skxie@chelsio.com 			   gfp_t gfp)
14119ba682f0Skxie@chelsio.com {
14129ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
14139ba682f0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp = cdev->ddp;
14149ba682f0Skxie@chelsio.com 	struct cxgbi_tag_format *tformat = &cdev->tag_format;
14159ba682f0Skxie@chelsio.com 	struct cxgbi_pagepod_hdr hdr;
14169ba682f0Skxie@chelsio.com 	unsigned int npods;
14179ba682f0Skxie@chelsio.com 	int idx = -1;
14189ba682f0Skxie@chelsio.com 	int err = -ENOMEM;
14199ba682f0Skxie@chelsio.com 	u32 tag;
14209ba682f0Skxie@chelsio.com 
14219ba682f0Skxie@chelsio.com 	npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
14229ba682f0Skxie@chelsio.com 	if (ddp->idx_last == ddp->nppods)
14239ba682f0Skxie@chelsio.com 		idx = ddp_find_unused_entries(ddp, 0, ddp->nppods,
14249ba682f0Skxie@chelsio.com 							npods, gl);
14259ba682f0Skxie@chelsio.com 	else {
14269ba682f0Skxie@chelsio.com 		idx = ddp_find_unused_entries(ddp, ddp->idx_last + 1,
14279ba682f0Skxie@chelsio.com 							ddp->nppods, npods,
14289ba682f0Skxie@chelsio.com 							gl);
14299ba682f0Skxie@chelsio.com 		if (idx < 0 && ddp->idx_last >= npods) {
14309ba682f0Skxie@chelsio.com 			idx = ddp_find_unused_entries(ddp, 0,
14319ba682f0Skxie@chelsio.com 				min(ddp->idx_last + npods, ddp->nppods),
14329ba682f0Skxie@chelsio.com 							npods, gl);
14339ba682f0Skxie@chelsio.com 		}
14349ba682f0Skxie@chelsio.com 	}
14359ba682f0Skxie@chelsio.com 	if (idx < 0) {
14369ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
14379ba682f0Skxie@chelsio.com 			"xferlen %u, gl %u, npods %u NO DDP.\n",
14389ba682f0Skxie@chelsio.com 			gl->length, gl->nelem, npods);
14399ba682f0Skxie@chelsio.com 		return idx;
14409ba682f0Skxie@chelsio.com 	}
14419ba682f0Skxie@chelsio.com 
14429ba682f0Skxie@chelsio.com 	tag = cxgbi_ddp_tag_base(tformat, sw_tag);
14439ba682f0Skxie@chelsio.com 	tag |= idx << PPOD_IDX_SHIFT;
14449ba682f0Skxie@chelsio.com 
14459ba682f0Skxie@chelsio.com 	hdr.rsvd = 0;
14469ba682f0Skxie@chelsio.com 	hdr.vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
14479ba682f0Skxie@chelsio.com 	hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
14489ba682f0Skxie@chelsio.com 	hdr.max_offset = htonl(gl->length);
14499ba682f0Skxie@chelsio.com 	hdr.page_offset = htonl(gl->offset);
14509ba682f0Skxie@chelsio.com 
14519ba682f0Skxie@chelsio.com 	err = cdev->csk_ddp_set(csk, &hdr, idx, npods, gl);
1452b8ce8b59Skxie@chelsio.com 	if (err < 0)
14539ba682f0Skxie@chelsio.com 		goto unmark_entries;
14549ba682f0Skxie@chelsio.com 
14559ba682f0Skxie@chelsio.com 	ddp->idx_last = idx;
14569ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
14579ba682f0Skxie@chelsio.com 		"xfer %u, gl %u,%u, tid 0x%x, tag 0x%x->0x%x(%u,%u).\n",
14589ba682f0Skxie@chelsio.com 		gl->length, gl->nelem, gl->offset, tid, sw_tag, tag, idx,
14599ba682f0Skxie@chelsio.com 		npods);
14609ba682f0Skxie@chelsio.com 	*tagp = tag;
14619ba682f0Skxie@chelsio.com 	return 0;
14629ba682f0Skxie@chelsio.com 
14639ba682f0Skxie@chelsio.com unmark_entries:
14649ba682f0Skxie@chelsio.com 	ddp_unmark_entries(ddp, idx, npods);
14659ba682f0Skxie@chelsio.com 	return err;
14669ba682f0Skxie@chelsio.com }
14679ba682f0Skxie@chelsio.com 
14689ba682f0Skxie@chelsio.com int cxgbi_ddp_reserve(struct cxgbi_sock *csk, unsigned int *tagp,
14699ba682f0Skxie@chelsio.com 			unsigned int sw_tag, unsigned int xferlen,
14709ba682f0Skxie@chelsio.com 			struct scatterlist *sgl, unsigned int sgcnt, gfp_t gfp)
14719ba682f0Skxie@chelsio.com {
14729ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
14739ba682f0Skxie@chelsio.com 	struct cxgbi_tag_format *tformat = &cdev->tag_format;
14749ba682f0Skxie@chelsio.com 	struct cxgbi_gather_list *gl;
14759ba682f0Skxie@chelsio.com 	int err;
14769ba682f0Skxie@chelsio.com 
14779ba682f0Skxie@chelsio.com 	if (page_idx >= DDP_PGIDX_MAX || !cdev->ddp ||
14789ba682f0Skxie@chelsio.com 	    xferlen < DDP_THRESHOLD) {
14799ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
14809ba682f0Skxie@chelsio.com 			"pgidx %u, xfer %u, NO ddp.\n", page_idx, xferlen);
14819ba682f0Skxie@chelsio.com 		return -EINVAL;
14829ba682f0Skxie@chelsio.com 	}
14839ba682f0Skxie@chelsio.com 
14849ba682f0Skxie@chelsio.com 	if (!cxgbi_sw_tag_usable(tformat, sw_tag)) {
14859ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DDP,
14869ba682f0Skxie@chelsio.com 			"sw_tag 0x%x NOT usable.\n", sw_tag);
14879ba682f0Skxie@chelsio.com 		return -EINVAL;
14889ba682f0Skxie@chelsio.com 	}
14899ba682f0Skxie@chelsio.com 
14909ba682f0Skxie@chelsio.com 	gl = ddp_make_gl(xferlen, sgl, sgcnt, cdev->pdev, gfp);
14919ba682f0Skxie@chelsio.com 	if (!gl)
14929ba682f0Skxie@chelsio.com 		return -ENOMEM;
14939ba682f0Skxie@chelsio.com 
14949ba682f0Skxie@chelsio.com 	err = ddp_tag_reserve(csk, csk->tid, sw_tag, tagp, gl, gfp);
14959ba682f0Skxie@chelsio.com 	if (err < 0)
14969ba682f0Skxie@chelsio.com 		ddp_release_gl(gl, cdev->pdev);
14979ba682f0Skxie@chelsio.com 
14989ba682f0Skxie@chelsio.com 	return err;
14999ba682f0Skxie@chelsio.com }
15009ba682f0Skxie@chelsio.com 
15019ba682f0Skxie@chelsio.com static void ddp_destroy(struct kref *kref)
15029ba682f0Skxie@chelsio.com {
15039ba682f0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp = container_of(kref,
15049ba682f0Skxie@chelsio.com 						struct cxgbi_ddp_info,
15059ba682f0Skxie@chelsio.com 						refcnt);
15069ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = ddp->cdev;
15079ba682f0Skxie@chelsio.com 	int i = 0;
15089ba682f0Skxie@chelsio.com 
15099ba682f0Skxie@chelsio.com 	pr_info("kref 0, destroy ddp 0x%p, cdev 0x%p.\n", ddp, cdev);
15109ba682f0Skxie@chelsio.com 
15119ba682f0Skxie@chelsio.com 	while (i < ddp->nppods) {
15129ba682f0Skxie@chelsio.com 		struct cxgbi_gather_list *gl = ddp->gl_map[i];
15139ba682f0Skxie@chelsio.com 
15149ba682f0Skxie@chelsio.com 		if (gl) {
15159ba682f0Skxie@chelsio.com 			int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
15169ba682f0Skxie@chelsio.com 					>> PPOD_PAGES_SHIFT;
15179ba682f0Skxie@chelsio.com 			pr_info("cdev 0x%p, ddp %d + %d.\n", cdev, i, npods);
15189ba682f0Skxie@chelsio.com 			kfree(gl);
15199ba682f0Skxie@chelsio.com 			i += npods;
15209ba682f0Skxie@chelsio.com 		} else
15219ba682f0Skxie@chelsio.com 			i++;
15229ba682f0Skxie@chelsio.com 	}
15239ba682f0Skxie@chelsio.com 	cxgbi_free_big_mem(ddp);
15249ba682f0Skxie@chelsio.com }
15259ba682f0Skxie@chelsio.com 
15269ba682f0Skxie@chelsio.com int cxgbi_ddp_cleanup(struct cxgbi_device *cdev)
15279ba682f0Skxie@chelsio.com {
15289ba682f0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp = cdev->ddp;
15299ba682f0Skxie@chelsio.com 
15309ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
15319ba682f0Skxie@chelsio.com 		"cdev 0x%p, release ddp 0x%p.\n", cdev, ddp);
15329ba682f0Skxie@chelsio.com 	cdev->ddp = NULL;
15339ba682f0Skxie@chelsio.com 	if (ddp)
15349ba682f0Skxie@chelsio.com 		return kref_put(&ddp->refcnt, ddp_destroy);
15359ba682f0Skxie@chelsio.com 	return 0;
15369ba682f0Skxie@chelsio.com }
15379ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ddp_cleanup);
15389ba682f0Skxie@chelsio.com 
15399ba682f0Skxie@chelsio.com int cxgbi_ddp_init(struct cxgbi_device *cdev,
15409ba682f0Skxie@chelsio.com 		   unsigned int llimit, unsigned int ulimit,
15419ba682f0Skxie@chelsio.com 		   unsigned int max_txsz, unsigned int max_rxsz)
15429ba682f0Skxie@chelsio.com {
15439ba682f0Skxie@chelsio.com 	struct cxgbi_ddp_info *ddp;
15449ba682f0Skxie@chelsio.com 	unsigned int ppmax, bits;
15459ba682f0Skxie@chelsio.com 
15469ba682f0Skxie@chelsio.com 	ppmax = (ulimit - llimit + 1) >> PPOD_SIZE_SHIFT;
15479ba682f0Skxie@chelsio.com 	bits = __ilog2_u32(ppmax) + 1;
15489ba682f0Skxie@chelsio.com 	if (bits > PPOD_IDX_MAX_SIZE)
15499ba682f0Skxie@chelsio.com 		bits = PPOD_IDX_MAX_SIZE;
15509ba682f0Skxie@chelsio.com 	ppmax = (1 << (bits - 1)) - 1;
15519ba682f0Skxie@chelsio.com 
15529ba682f0Skxie@chelsio.com 	ddp = cxgbi_alloc_big_mem(sizeof(struct cxgbi_ddp_info) +
15539ba682f0Skxie@chelsio.com 				ppmax * (sizeof(struct cxgbi_gather_list *) +
15549ba682f0Skxie@chelsio.com 					 sizeof(struct sk_buff *)),
15559ba682f0Skxie@chelsio.com 				GFP_KERNEL);
15569ba682f0Skxie@chelsio.com 	if (!ddp) {
15579ba682f0Skxie@chelsio.com 		pr_warn("cdev 0x%p, ddp ppmax %u OOM.\n", cdev, ppmax);
15589ba682f0Skxie@chelsio.com 		return -ENOMEM;
15599ba682f0Skxie@chelsio.com 	}
15609ba682f0Skxie@chelsio.com 	ddp->gl_map = (struct cxgbi_gather_list **)(ddp + 1);
15619ba682f0Skxie@chelsio.com 	cdev->ddp = ddp;
15629ba682f0Skxie@chelsio.com 
15639ba682f0Skxie@chelsio.com 	spin_lock_init(&ddp->map_lock);
15649ba682f0Skxie@chelsio.com 	kref_init(&ddp->refcnt);
15659ba682f0Skxie@chelsio.com 
15669ba682f0Skxie@chelsio.com 	ddp->cdev = cdev;
15679ba682f0Skxie@chelsio.com 	ddp->pdev = cdev->pdev;
15689ba682f0Skxie@chelsio.com 	ddp->llimit = llimit;
15699ba682f0Skxie@chelsio.com 	ddp->ulimit = ulimit;
15709ba682f0Skxie@chelsio.com 	ddp->max_txsz = min_t(unsigned int, max_txsz, ULP2_MAX_PKT_SIZE);
15719ba682f0Skxie@chelsio.com 	ddp->max_rxsz = min_t(unsigned int, max_rxsz, ULP2_MAX_PKT_SIZE);
15729ba682f0Skxie@chelsio.com 	ddp->nppods = ppmax;
15739ba682f0Skxie@chelsio.com 	ddp->idx_last = ppmax;
15749ba682f0Skxie@chelsio.com 	ddp->idx_bits = bits;
15759ba682f0Skxie@chelsio.com 	ddp->idx_mask = (1 << bits) - 1;
15769ba682f0Skxie@chelsio.com 	ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
15779ba682f0Skxie@chelsio.com 
15789ba682f0Skxie@chelsio.com 	cdev->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
15799ba682f0Skxie@chelsio.com 	cdev->tag_format.rsvd_bits = ddp->idx_bits;
15809ba682f0Skxie@chelsio.com 	cdev->tag_format.rsvd_shift = PPOD_IDX_SHIFT;
15819ba682f0Skxie@chelsio.com 	cdev->tag_format.rsvd_mask = (1 << cdev->tag_format.rsvd_bits) - 1;
15829ba682f0Skxie@chelsio.com 
15839ba682f0Skxie@chelsio.com 	pr_info("%s tag format, sw %u, rsvd %u,%u, mask 0x%x.\n",
15849ba682f0Skxie@chelsio.com 		cdev->ports[0]->name, cdev->tag_format.sw_bits,
15859ba682f0Skxie@chelsio.com 		cdev->tag_format.rsvd_bits, cdev->tag_format.rsvd_shift,
15869ba682f0Skxie@chelsio.com 		cdev->tag_format.rsvd_mask);
15879ba682f0Skxie@chelsio.com 
15889ba682f0Skxie@chelsio.com 	cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
15899ba682f0Skxie@chelsio.com 				ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
15909ba682f0Skxie@chelsio.com 	cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
15919ba682f0Skxie@chelsio.com 				ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
15929ba682f0Skxie@chelsio.com 
15939ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
15949ba682f0Skxie@chelsio.com 		"%s max payload size: %u/%u, %u/%u.\n",
15959ba682f0Skxie@chelsio.com 		cdev->ports[0]->name, cdev->tx_max_size, ddp->max_txsz,
15969ba682f0Skxie@chelsio.com 		cdev->rx_max_size, ddp->max_rxsz);
15979ba682f0Skxie@chelsio.com 	return 0;
15989ba682f0Skxie@chelsio.com }
15999ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ddp_init);
16009ba682f0Skxie@chelsio.com 
16019ba682f0Skxie@chelsio.com /*
16029ba682f0Skxie@chelsio.com  * APIs interacting with open-iscsi libraries
16039ba682f0Skxie@chelsio.com  */
16049ba682f0Skxie@chelsio.com 
16059ba682f0Skxie@chelsio.com static unsigned char padding[4];
16069ba682f0Skxie@chelsio.com 
16079ba682f0Skxie@chelsio.com static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
16089ba682f0Skxie@chelsio.com {
16099ba682f0Skxie@chelsio.com 	struct scsi_cmnd *sc = task->sc;
16109ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
16119ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
16129ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = cconn->chba;
16139ba682f0Skxie@chelsio.com 	struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
16149ba682f0Skxie@chelsio.com 	u32 tag = ntohl((__force u32)hdr_itt);
16159ba682f0Skxie@chelsio.com 
16169ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
16179ba682f0Skxie@chelsio.com 		   "cdev 0x%p, release tag 0x%x.\n", chba->cdev, tag);
16189ba682f0Skxie@chelsio.com 	if (sc &&
16199ba682f0Skxie@chelsio.com 	    (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
16209ba682f0Skxie@chelsio.com 	    cxgbi_is_ddp_tag(tformat, tag))
16219ba682f0Skxie@chelsio.com 		ddp_tag_release(chba, tag);
16229ba682f0Skxie@chelsio.com }
16239ba682f0Skxie@chelsio.com 
16249ba682f0Skxie@chelsio.com static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
16259ba682f0Skxie@chelsio.com {
16269ba682f0Skxie@chelsio.com 	struct scsi_cmnd *sc = task->sc;
16279ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = task->conn;
16289ba682f0Skxie@chelsio.com 	struct iscsi_session *sess = conn->session;
16299ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
16309ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
16319ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = cconn->chba;
16329ba682f0Skxie@chelsio.com 	struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
16339ba682f0Skxie@chelsio.com 	u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
16349ba682f0Skxie@chelsio.com 	u32 tag = 0;
16359ba682f0Skxie@chelsio.com 	int err = -EINVAL;
16369ba682f0Skxie@chelsio.com 
16379ba682f0Skxie@chelsio.com 	if (sc &&
16389ba682f0Skxie@chelsio.com 	    (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)) {
16399ba682f0Skxie@chelsio.com 		err = cxgbi_ddp_reserve(cconn->cep->csk, &tag, sw_tag,
16409ba682f0Skxie@chelsio.com 					scsi_in(sc)->length,
16419ba682f0Skxie@chelsio.com 					scsi_in(sc)->table.sgl,
16429ba682f0Skxie@chelsio.com 					scsi_in(sc)->table.nents,
16439ba682f0Skxie@chelsio.com 					GFP_ATOMIC);
16449ba682f0Skxie@chelsio.com 		if (err < 0)
16459ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_DDP,
16469ba682f0Skxie@chelsio.com 				"csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
16479ba682f0Skxie@chelsio.com 				cconn->cep->csk, task, scsi_in(sc)->length,
16489ba682f0Skxie@chelsio.com 				scsi_in(sc)->table.nents);
16499ba682f0Skxie@chelsio.com 	}
16509ba682f0Skxie@chelsio.com 
16519ba682f0Skxie@chelsio.com 	if (err < 0)
16529ba682f0Skxie@chelsio.com 		tag = cxgbi_set_non_ddp_tag(tformat, sw_tag);
16539ba682f0Skxie@chelsio.com 	/*  the itt need to sent in big-endian order */
16549ba682f0Skxie@chelsio.com 	*hdr_itt = (__force itt_t)htonl(tag);
16559ba682f0Skxie@chelsio.com 
16569ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
16579ba682f0Skxie@chelsio.com 		"cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
16589ba682f0Skxie@chelsio.com 		chba->cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
16599ba682f0Skxie@chelsio.com 	return 0;
16609ba682f0Skxie@chelsio.com }
16619ba682f0Skxie@chelsio.com 
16629ba682f0Skxie@chelsio.com void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
16639ba682f0Skxie@chelsio.com {
16649ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
16659ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
16669ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
16679ba682f0Skxie@chelsio.com 	u32 tag = ntohl((__force u32) itt);
16689ba682f0Skxie@chelsio.com 	u32 sw_bits;
16699ba682f0Skxie@chelsio.com 
16709ba682f0Skxie@chelsio.com 	sw_bits = cxgbi_tag_nonrsvd_bits(&cdev->tag_format, tag);
16719ba682f0Skxie@chelsio.com 	if (idx)
16729ba682f0Skxie@chelsio.com 		*idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
16739ba682f0Skxie@chelsio.com 	if (age)
16749ba682f0Skxie@chelsio.com 		*age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
16759ba682f0Skxie@chelsio.com 
16769ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DDP,
16779ba682f0Skxie@chelsio.com 		"cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
16789ba682f0Skxie@chelsio.com 		cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
16799ba682f0Skxie@chelsio.com 		age ? *age : 0xFF);
16809ba682f0Skxie@chelsio.com }
16819ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
16829ba682f0Skxie@chelsio.com 
16839ba682f0Skxie@chelsio.com void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
16849ba682f0Skxie@chelsio.com {
16859ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = csk->user_data;
16869ba682f0Skxie@chelsio.com 
16879ba682f0Skxie@chelsio.com 	if (conn) {
16889ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_SOCK,
16899ba682f0Skxie@chelsio.com 			"csk 0x%p, cid %d.\n", csk, conn->id);
16909ba682f0Skxie@chelsio.com 		iscsi_conn_queue_work(conn);
16919ba682f0Skxie@chelsio.com 	}
16929ba682f0Skxie@chelsio.com }
16939ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
16949ba682f0Skxie@chelsio.com 
16959ba682f0Skxie@chelsio.com /*
16969ba682f0Skxie@chelsio.com  * pdu receive, interact with libiscsi_tcp
16979ba682f0Skxie@chelsio.com  */
16989ba682f0Skxie@chelsio.com static inline int read_pdu_skb(struct iscsi_conn *conn,
16999ba682f0Skxie@chelsio.com 			       struct sk_buff *skb,
17009ba682f0Skxie@chelsio.com 			       unsigned int offset,
17019ba682f0Skxie@chelsio.com 			       int offloaded)
17029ba682f0Skxie@chelsio.com {
17039ba682f0Skxie@chelsio.com 	int status = 0;
17049ba682f0Skxie@chelsio.com 	int bytes_read;
17059ba682f0Skxie@chelsio.com 
17069ba682f0Skxie@chelsio.com 	bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
17079ba682f0Skxie@chelsio.com 	switch (status) {
17089ba682f0Skxie@chelsio.com 	case ISCSI_TCP_CONN_ERR:
17099ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
17109ba682f0Skxie@chelsio.com 			  skb, offset, offloaded);
17119ba682f0Skxie@chelsio.com 		return -EIO;
17129ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SUSPENDED:
17139ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
17149ba682f0Skxie@chelsio.com 			"skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
17159ba682f0Skxie@chelsio.com 			skb, offset, offloaded, bytes_read);
17169ba682f0Skxie@chelsio.com 		/* no transfer - just have caller flush queue */
17179ba682f0Skxie@chelsio.com 		return bytes_read;
17189ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SKB_DONE:
17199ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
17209ba682f0Skxie@chelsio.com 			skb, offset, offloaded);
17219ba682f0Skxie@chelsio.com 		/*
17229ba682f0Skxie@chelsio.com 		 * pdus should always fit in the skb and we should get
17239ba682f0Skxie@chelsio.com 		 * segment done notifcation.
17249ba682f0Skxie@chelsio.com 		 */
17259ba682f0Skxie@chelsio.com 		iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
17269ba682f0Skxie@chelsio.com 		return -EFAULT;
17279ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SEGMENT_DONE:
17289ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
17299ba682f0Skxie@chelsio.com 			"skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
17309ba682f0Skxie@chelsio.com 			skb, offset, offloaded, bytes_read);
17319ba682f0Skxie@chelsio.com 		return bytes_read;
17329ba682f0Skxie@chelsio.com 	default:
17339ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
17349ba682f0Skxie@chelsio.com 			skb, offset, offloaded, status);
17359ba682f0Skxie@chelsio.com 		return -EINVAL;
17369ba682f0Skxie@chelsio.com 	}
17379ba682f0Skxie@chelsio.com }
17389ba682f0Skxie@chelsio.com 
17399ba682f0Skxie@chelsio.com static int skb_read_pdu_bhs(struct iscsi_conn *conn, struct sk_buff *skb)
17409ba682f0Skxie@chelsio.com {
17419ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
17429ba682f0Skxie@chelsio.com 
17439ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
17449ba682f0Skxie@chelsio.com 		"conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
17459ba682f0Skxie@chelsio.com 		conn, skb, skb->len, cxgbi_skcb_flags(skb));
17469ba682f0Skxie@chelsio.com 
17479ba682f0Skxie@chelsio.com 	if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
17489ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
17499ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
17509ba682f0Skxie@chelsio.com 		return -EIO;
17519ba682f0Skxie@chelsio.com 	}
17529ba682f0Skxie@chelsio.com 
17539ba682f0Skxie@chelsio.com 	if (conn->hdrdgst_en &&
17549ba682f0Skxie@chelsio.com 	    cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
17559ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
17569ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
17579ba682f0Skxie@chelsio.com 		return -EIO;
17589ba682f0Skxie@chelsio.com 	}
17599ba682f0Skxie@chelsio.com 
17609ba682f0Skxie@chelsio.com 	return read_pdu_skb(conn, skb, 0, 0);
17619ba682f0Skxie@chelsio.com }
17629ba682f0Skxie@chelsio.com 
17639ba682f0Skxie@chelsio.com static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
17649ba682f0Skxie@chelsio.com 			     struct sk_buff *skb, unsigned int offset)
17659ba682f0Skxie@chelsio.com {
17669ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
17679ba682f0Skxie@chelsio.com 	bool offloaded = 0;
17689ba682f0Skxie@chelsio.com 	int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
17699ba682f0Skxie@chelsio.com 
17709ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
17719ba682f0Skxie@chelsio.com 		"conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
17729ba682f0Skxie@chelsio.com 		conn, skb, skb->len, cxgbi_skcb_flags(skb));
17739ba682f0Skxie@chelsio.com 
17749ba682f0Skxie@chelsio.com 	if (conn->datadgst_en &&
17759ba682f0Skxie@chelsio.com 	    cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
17769ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
17779ba682f0Skxie@chelsio.com 			conn, lskb, cxgbi_skcb_flags(lskb));
17789ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
17799ba682f0Skxie@chelsio.com 		return -EIO;
17809ba682f0Skxie@chelsio.com 	}
17819ba682f0Skxie@chelsio.com 
17829ba682f0Skxie@chelsio.com 	if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
17839ba682f0Skxie@chelsio.com 		return 0;
17849ba682f0Skxie@chelsio.com 
17859ba682f0Skxie@chelsio.com 	/* coalesced, add header digest length */
17869ba682f0Skxie@chelsio.com 	if (lskb == skb && conn->hdrdgst_en)
17879ba682f0Skxie@chelsio.com 		offset += ISCSI_DIGEST_SIZE;
17889ba682f0Skxie@chelsio.com 
17899ba682f0Skxie@chelsio.com 	if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
17909ba682f0Skxie@chelsio.com 		offloaded = 1;
17919ba682f0Skxie@chelsio.com 
17929ba682f0Skxie@chelsio.com 	if (opcode == ISCSI_OP_SCSI_DATA_IN)
17939ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
17949ba682f0Skxie@chelsio.com 			"skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
17959ba682f0Skxie@chelsio.com 			skb, opcode, ntohl(tcp_conn->in.hdr->itt),
17969ba682f0Skxie@chelsio.com 			tcp_conn->in.datalen, offloaded ? "is" : "not");
17979ba682f0Skxie@chelsio.com 
17989ba682f0Skxie@chelsio.com 	return read_pdu_skb(conn, skb, offset, offloaded);
17999ba682f0Skxie@chelsio.com }
18009ba682f0Skxie@chelsio.com 
18019ba682f0Skxie@chelsio.com static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
18029ba682f0Skxie@chelsio.com {
18039ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
18049ba682f0Skxie@chelsio.com 	int must_send;
18059ba682f0Skxie@chelsio.com 	u32 credits;
18069ba682f0Skxie@chelsio.com 
18079ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
18089ba682f0Skxie@chelsio.com 		"csk 0x%p,%u,0x%lu,%u, seq %u, wup %u, thre %u, %u.\n",
18099ba682f0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
18109ba682f0Skxie@chelsio.com 		csk->rcv_wup, cdev->rx_credit_thres,
18119ba682f0Skxie@chelsio.com 		cdev->rcv_win);
18129ba682f0Skxie@chelsio.com 
18139ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED)
18149ba682f0Skxie@chelsio.com 		return;
18159ba682f0Skxie@chelsio.com 
18169ba682f0Skxie@chelsio.com 	credits = csk->copied_seq - csk->rcv_wup;
18179ba682f0Skxie@chelsio.com 	if (unlikely(!credits))
18189ba682f0Skxie@chelsio.com 		return;
18199ba682f0Skxie@chelsio.com 	if (unlikely(cdev->rx_credit_thres == 0))
18209ba682f0Skxie@chelsio.com 		return;
18219ba682f0Skxie@chelsio.com 
18229ba682f0Skxie@chelsio.com 	must_send = credits + 16384 >= cdev->rcv_win;
18239ba682f0Skxie@chelsio.com 	if (must_send || credits >= cdev->rx_credit_thres)
18249ba682f0Skxie@chelsio.com 		csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
18259ba682f0Skxie@chelsio.com }
18269ba682f0Skxie@chelsio.com 
18279ba682f0Skxie@chelsio.com void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
18289ba682f0Skxie@chelsio.com {
18299ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
18309ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = csk->user_data;
18319ba682f0Skxie@chelsio.com 	struct sk_buff *skb;
18329ba682f0Skxie@chelsio.com 	unsigned int read = 0;
18339ba682f0Skxie@chelsio.com 	int err = 0;
18349ba682f0Skxie@chelsio.com 
18359ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
18369ba682f0Skxie@chelsio.com 		"csk 0x%p, conn 0x%p.\n", csk, conn);
18379ba682f0Skxie@chelsio.com 
18389ba682f0Skxie@chelsio.com 	if (unlikely(!conn || conn->suspend_rx)) {
18399ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
18409ba682f0Skxie@chelsio.com 			"csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
18419ba682f0Skxie@chelsio.com 			csk, conn, conn ? conn->id : 0xFF,
18429ba682f0Skxie@chelsio.com 			conn ? conn->suspend_rx : 0xFF);
18439ba682f0Skxie@chelsio.com 		return;
18449ba682f0Skxie@chelsio.com 	}
18459ba682f0Skxie@chelsio.com 
18469ba682f0Skxie@chelsio.com 	while (!err) {
18479ba682f0Skxie@chelsio.com 		skb = skb_peek(&csk->receive_queue);
18489ba682f0Skxie@chelsio.com 		if (!skb ||
18499ba682f0Skxie@chelsio.com 		    !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
18509ba682f0Skxie@chelsio.com 			if (skb)
18519ba682f0Skxie@chelsio.com 				log_debug(1 << CXGBI_DBG_PDU_RX,
18529ba682f0Skxie@chelsio.com 					"skb 0x%p, NOT ready 0x%lx.\n",
18539ba682f0Skxie@chelsio.com 					skb, cxgbi_skcb_flags(skb));
18549ba682f0Skxie@chelsio.com 			break;
18559ba682f0Skxie@chelsio.com 		}
18569ba682f0Skxie@chelsio.com 		__skb_unlink(skb, &csk->receive_queue);
18579ba682f0Skxie@chelsio.com 
18589ba682f0Skxie@chelsio.com 		read += cxgbi_skcb_rx_pdulen(skb);
18599ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
18609ba682f0Skxie@chelsio.com 			"csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
18619ba682f0Skxie@chelsio.com 			csk, skb, skb->len, cxgbi_skcb_flags(skb),
18629ba682f0Skxie@chelsio.com 			cxgbi_skcb_rx_pdulen(skb));
18639ba682f0Skxie@chelsio.com 
18649ba682f0Skxie@chelsio.com 		if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
18659ba682f0Skxie@chelsio.com 			err = skb_read_pdu_bhs(conn, skb);
1866e3d2ad8cSkxie@chelsio.com 			if (err < 0) {
1867e3d2ad8cSkxie@chelsio.com 				pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1868e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1869e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1870e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1871e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
1872e3d2ad8cSkxie@chelsio.com 				goto skb_done;
1873e3d2ad8cSkxie@chelsio.com 			}
18749ba682f0Skxie@chelsio.com 			err = skb_read_pdu_data(conn, skb, skb,
18759ba682f0Skxie@chelsio.com 						err + cdev->skb_rx_extra);
1876e3d2ad8cSkxie@chelsio.com 			if (err < 0)
1877e3d2ad8cSkxie@chelsio.com 				pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1878e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1879e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1880e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1881e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
18829ba682f0Skxie@chelsio.com 		} else {
18839ba682f0Skxie@chelsio.com 			err = skb_read_pdu_bhs(conn, skb);
1884e3d2ad8cSkxie@chelsio.com 			if (err < 0) {
1885e3d2ad8cSkxie@chelsio.com 				pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1886e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1887e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1888e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1889e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
1890e3d2ad8cSkxie@chelsio.com 				goto skb_done;
1891e3d2ad8cSkxie@chelsio.com 			}
1892e3d2ad8cSkxie@chelsio.com 
18939ba682f0Skxie@chelsio.com 			if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
18949ba682f0Skxie@chelsio.com 				struct sk_buff *dskb;
18959ba682f0Skxie@chelsio.com 
18969ba682f0Skxie@chelsio.com 				dskb = skb_peek(&csk->receive_queue);
18979ba682f0Skxie@chelsio.com 				if (!dskb) {
1898e3d2ad8cSkxie@chelsio.com 					pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1899e3d2ad8cSkxie@chelsio.com 						" plen %u, NO data.\n",
1900e3d2ad8cSkxie@chelsio.com 						csk, skb, skb->len,
1901e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_flags(skb),
1902e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_rx_pdulen(skb));
1903e3d2ad8cSkxie@chelsio.com 					err = -EIO;
1904e3d2ad8cSkxie@chelsio.com 					goto skb_done;
19059ba682f0Skxie@chelsio.com 				}
19069ba682f0Skxie@chelsio.com 				__skb_unlink(dskb, &csk->receive_queue);
19079ba682f0Skxie@chelsio.com 
19089ba682f0Skxie@chelsio.com 				err = skb_read_pdu_data(conn, skb, dskb, 0);
1909e3d2ad8cSkxie@chelsio.com 				if (err < 0)
1910e3d2ad8cSkxie@chelsio.com 					pr_err("data, csk 0x%p, skb 0x%p,%u, "
1911e3d2ad8cSkxie@chelsio.com 						"f 0x%lx, plen %u, dskb 0x%p,"
1912e3d2ad8cSkxie@chelsio.com 						"%u.\n",
1913e3d2ad8cSkxie@chelsio.com 						csk, skb, skb->len,
1914e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_flags(skb),
1915e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_rx_pdulen(skb),
1916e3d2ad8cSkxie@chelsio.com 						dskb, dskb->len);
19179ba682f0Skxie@chelsio.com 				__kfree_skb(dskb);
19189ba682f0Skxie@chelsio.com 			} else
19199ba682f0Skxie@chelsio.com 				err = skb_read_pdu_data(conn, skb, skb, 0);
19209ba682f0Skxie@chelsio.com 		}
1921e3d2ad8cSkxie@chelsio.com skb_done:
1922e3d2ad8cSkxie@chelsio.com 		__kfree_skb(skb);
1923e3d2ad8cSkxie@chelsio.com 
19249ba682f0Skxie@chelsio.com 		if (err < 0)
19259ba682f0Skxie@chelsio.com 			break;
19269ba682f0Skxie@chelsio.com 	}
19279ba682f0Skxie@chelsio.com 
19289ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
19299ba682f0Skxie@chelsio.com 	if (read) {
19309ba682f0Skxie@chelsio.com 		csk->copied_seq += read;
19319ba682f0Skxie@chelsio.com 		csk_return_rx_credits(csk, read);
19329ba682f0Skxie@chelsio.com 		conn->rxdata_octets += read;
19339ba682f0Skxie@chelsio.com 	}
19349ba682f0Skxie@chelsio.com 
19359ba682f0Skxie@chelsio.com 	if (err < 0) {
1936e3d2ad8cSkxie@chelsio.com 		pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1937e3d2ad8cSkxie@chelsio.com 			csk, conn, err, read);
19389ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
19399ba682f0Skxie@chelsio.com 	}
19409ba682f0Skxie@chelsio.com }
19419ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
19429ba682f0Skxie@chelsio.com 
19439ba682f0Skxie@chelsio.com static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
19449ba682f0Skxie@chelsio.com 				unsigned int offset, unsigned int *off,
19459ba682f0Skxie@chelsio.com 				struct scatterlist **sgp)
19469ba682f0Skxie@chelsio.com {
19479ba682f0Skxie@chelsio.com 	int i;
19489ba682f0Skxie@chelsio.com 	struct scatterlist *sg;
19499ba682f0Skxie@chelsio.com 
19509ba682f0Skxie@chelsio.com 	for_each_sg(sgl, sg, sgcnt, i) {
19519ba682f0Skxie@chelsio.com 		if (offset < sg->length) {
19529ba682f0Skxie@chelsio.com 			*off = offset;
19539ba682f0Skxie@chelsio.com 			*sgp = sg;
19549ba682f0Skxie@chelsio.com 			return 0;
19559ba682f0Skxie@chelsio.com 		}
19569ba682f0Skxie@chelsio.com 		offset -= sg->length;
19579ba682f0Skxie@chelsio.com 	}
19589ba682f0Skxie@chelsio.com 	return -EFAULT;
19599ba682f0Skxie@chelsio.com }
19609ba682f0Skxie@chelsio.com 
19619ba682f0Skxie@chelsio.com static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
19626a39a16aSIan Campbell 				unsigned int dlen, struct page_frag *frags,
19639ba682f0Skxie@chelsio.com 				int frag_max)
19649ba682f0Skxie@chelsio.com {
19659ba682f0Skxie@chelsio.com 	unsigned int datalen = dlen;
19669ba682f0Skxie@chelsio.com 	unsigned int sglen = sg->length - sgoffset;
19679ba682f0Skxie@chelsio.com 	struct page *page = sg_page(sg);
19689ba682f0Skxie@chelsio.com 	int i;
19699ba682f0Skxie@chelsio.com 
19709ba682f0Skxie@chelsio.com 	i = 0;
19719ba682f0Skxie@chelsio.com 	do {
19729ba682f0Skxie@chelsio.com 		unsigned int copy;
19739ba682f0Skxie@chelsio.com 
19749ba682f0Skxie@chelsio.com 		if (!sglen) {
19759ba682f0Skxie@chelsio.com 			sg = sg_next(sg);
19769ba682f0Skxie@chelsio.com 			if (!sg) {
19779ba682f0Skxie@chelsio.com 				pr_warn("sg %d NULL, len %u/%u.\n",
19789ba682f0Skxie@chelsio.com 					i, datalen, dlen);
19799ba682f0Skxie@chelsio.com 				return -EINVAL;
19809ba682f0Skxie@chelsio.com 			}
19819ba682f0Skxie@chelsio.com 			sgoffset = 0;
19829ba682f0Skxie@chelsio.com 			sglen = sg->length;
19839ba682f0Skxie@chelsio.com 			page = sg_page(sg);
19849ba682f0Skxie@chelsio.com 
19859ba682f0Skxie@chelsio.com 		}
19869ba682f0Skxie@chelsio.com 		copy = min(datalen, sglen);
19879ba682f0Skxie@chelsio.com 		if (i && page == frags[i - 1].page &&
19889ba682f0Skxie@chelsio.com 		    sgoffset + sg->offset ==
19896a39a16aSIan Campbell 			frags[i - 1].offset + frags[i - 1].size) {
19906a39a16aSIan Campbell 			frags[i - 1].size += copy;
19919ba682f0Skxie@chelsio.com 		} else {
19929ba682f0Skxie@chelsio.com 			if (i >= frag_max) {
19939ba682f0Skxie@chelsio.com 				pr_warn("too many pages %u, dlen %u.\n",
19949ba682f0Skxie@chelsio.com 					frag_max, dlen);
19959ba682f0Skxie@chelsio.com 				return -EINVAL;
19969ba682f0Skxie@chelsio.com 			}
19979ba682f0Skxie@chelsio.com 
19989ba682f0Skxie@chelsio.com 			frags[i].page = page;
19996a39a16aSIan Campbell 			frags[i].offset = sg->offset + sgoffset;
20006a39a16aSIan Campbell 			frags[i].size = copy;
20019ba682f0Skxie@chelsio.com 			i++;
20029ba682f0Skxie@chelsio.com 		}
20039ba682f0Skxie@chelsio.com 		datalen -= copy;
20049ba682f0Skxie@chelsio.com 		sgoffset += copy;
20059ba682f0Skxie@chelsio.com 		sglen -= copy;
20069ba682f0Skxie@chelsio.com 	} while (datalen);
20079ba682f0Skxie@chelsio.com 
20089ba682f0Skxie@chelsio.com 	return i;
20099ba682f0Skxie@chelsio.com }
20109ba682f0Skxie@chelsio.com 
20119ba682f0Skxie@chelsio.com int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
20129ba682f0Skxie@chelsio.com {
20139ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
20149ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
20159ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
20169ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = task->conn;
20179ba682f0Skxie@chelsio.com 	struct iscsi_tcp_task *tcp_task = task->dd_data;
2018e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
20199ba682f0Skxie@chelsio.com 	struct scsi_cmnd *sc = task->sc;
20209ba682f0Skxie@chelsio.com 	int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
20219ba682f0Skxie@chelsio.com 
20229ba682f0Skxie@chelsio.com 	tcp_task->dd_data = tdata;
20239ba682f0Skxie@chelsio.com 	task->hdr = NULL;
20249ba682f0Skxie@chelsio.com 
20259ba682f0Skxie@chelsio.com 	if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
20269ba682f0Skxie@chelsio.com 	    (opcode == ISCSI_OP_SCSI_DATA_OUT ||
20279ba682f0Skxie@chelsio.com 	     (opcode == ISCSI_OP_SCSI_CMD &&
20289ba682f0Skxie@chelsio.com 	      (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
20299ba682f0Skxie@chelsio.com 		/* data could goes into skb head */
20309ba682f0Skxie@chelsio.com 		headroom += min_t(unsigned int,
20319ba682f0Skxie@chelsio.com 				SKB_MAX_HEAD(cdev->skb_tx_rsvd),
20329ba682f0Skxie@chelsio.com 				conn->max_xmit_dlength);
20339ba682f0Skxie@chelsio.com 
20349ba682f0Skxie@chelsio.com 	tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
20359ba682f0Skxie@chelsio.com 	if (!tdata->skb) {
203600c4a09bSThadeu Lima de Souza Cascardo 		struct cxgbi_sock *csk = cconn->cep->csk;
203700c4a09bSThadeu Lima de Souza Cascardo 		struct net_device *ndev = cdev->ports[csk->port_id];
203800c4a09bSThadeu Lima de Souza Cascardo 		ndev->stats.tx_dropped++;
20399ba682f0Skxie@chelsio.com 		return -ENOMEM;
20409ba682f0Skxie@chelsio.com 	}
20419ba682f0Skxie@chelsio.com 
20429ba682f0Skxie@chelsio.com 	skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
20439ba682f0Skxie@chelsio.com 	task->hdr = (struct iscsi_hdr *)tdata->skb->data;
20449ba682f0Skxie@chelsio.com 	task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
20459ba682f0Skxie@chelsio.com 
20469ba682f0Skxie@chelsio.com 	/* data_out uses scsi_cmd's itt */
20479ba682f0Skxie@chelsio.com 	if (opcode != ISCSI_OP_SCSI_DATA_OUT)
20489ba682f0Skxie@chelsio.com 		task_reserve_itt(task, &task->hdr->itt);
20499ba682f0Skxie@chelsio.com 
20509ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
20519ba682f0Skxie@chelsio.com 		"task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
20529ba682f0Skxie@chelsio.com 		task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
20539ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength, ntohl(task->hdr->itt));
20549ba682f0Skxie@chelsio.com 
20559ba682f0Skxie@chelsio.com 	return 0;
20569ba682f0Skxie@chelsio.com }
20579ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
20589ba682f0Skxie@chelsio.com 
20599ba682f0Skxie@chelsio.com static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
20609ba682f0Skxie@chelsio.com {
2061c343a01cSkxie@chelsio.com 	if (hcrc || dcrc) {
20629ba682f0Skxie@chelsio.com 		u8 submode = 0;
20639ba682f0Skxie@chelsio.com 
20649ba682f0Skxie@chelsio.com 		if (hcrc)
20659ba682f0Skxie@chelsio.com 			submode |= 1;
20669ba682f0Skxie@chelsio.com 		if (dcrc)
20679ba682f0Skxie@chelsio.com 			submode |= 2;
20689ba682f0Skxie@chelsio.com 		cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
2069c343a01cSkxie@chelsio.com 	} else
2070c343a01cSkxie@chelsio.com 		cxgbi_skcb_ulp_mode(skb) = 0;
20719ba682f0Skxie@chelsio.com }
20729ba682f0Skxie@chelsio.com 
20739ba682f0Skxie@chelsio.com int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
20749ba682f0Skxie@chelsio.com 			      unsigned int count)
20759ba682f0Skxie@chelsio.com {
20769ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = task->conn;
2077e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
20789ba682f0Skxie@chelsio.com 	struct sk_buff *skb = tdata->skb;
20799ba682f0Skxie@chelsio.com 	unsigned int datalen = count;
20809ba682f0Skxie@chelsio.com 	int i, padlen = iscsi_padding(count);
20819ba682f0Skxie@chelsio.com 	struct page *pg;
20829ba682f0Skxie@chelsio.com 
20839ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
20849ba682f0Skxie@chelsio.com 		"task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
20859ba682f0Skxie@chelsio.com 		task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
20869ba682f0Skxie@chelsio.com 		ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
20879ba682f0Skxie@chelsio.com 
20889ba682f0Skxie@chelsio.com 	skb_put(skb, task->hdr_len);
20899ba682f0Skxie@chelsio.com 	tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
20909ba682f0Skxie@chelsio.com 	if (!count)
20919ba682f0Skxie@chelsio.com 		return 0;
20929ba682f0Skxie@chelsio.com 
20939ba682f0Skxie@chelsio.com 	if (task->sc) {
20949ba682f0Skxie@chelsio.com 		struct scsi_data_buffer *sdb = scsi_out(task->sc);
20959ba682f0Skxie@chelsio.com 		struct scatterlist *sg = NULL;
20969ba682f0Skxie@chelsio.com 		int err;
20979ba682f0Skxie@chelsio.com 
20989ba682f0Skxie@chelsio.com 		tdata->offset = offset;
20999ba682f0Skxie@chelsio.com 		tdata->count = count;
21009ba682f0Skxie@chelsio.com 		err = sgl_seek_offset(
21019ba682f0Skxie@chelsio.com 					sdb->table.sgl, sdb->table.nents,
21029ba682f0Skxie@chelsio.com 					tdata->offset, &tdata->sgoffset, &sg);
21039ba682f0Skxie@chelsio.com 		if (err < 0) {
21049ba682f0Skxie@chelsio.com 			pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
21059ba682f0Skxie@chelsio.com 				sdb->table.nents, tdata->offset, sdb->length);
21069ba682f0Skxie@chelsio.com 			return err;
21079ba682f0Skxie@chelsio.com 		}
21089ba682f0Skxie@chelsio.com 		err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
21099ba682f0Skxie@chelsio.com 					tdata->frags, MAX_PDU_FRAGS);
21109ba682f0Skxie@chelsio.com 		if (err < 0) {
21119ba682f0Skxie@chelsio.com 			pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
21129ba682f0Skxie@chelsio.com 				sdb->table.nents, tdata->offset, tdata->count);
21139ba682f0Skxie@chelsio.com 			return err;
21149ba682f0Skxie@chelsio.com 		}
21159ba682f0Skxie@chelsio.com 		tdata->nr_frags = err;
21169ba682f0Skxie@chelsio.com 
21179ba682f0Skxie@chelsio.com 		if (tdata->nr_frags > MAX_SKB_FRAGS ||
21189ba682f0Skxie@chelsio.com 		    (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
21199ba682f0Skxie@chelsio.com 			char *dst = skb->data + task->hdr_len;
21206a39a16aSIan Campbell 			struct page_frag *frag = tdata->frags;
21219ba682f0Skxie@chelsio.com 
21229ba682f0Skxie@chelsio.com 			/* data fits in the skb's headroom */
21239ba682f0Skxie@chelsio.com 			for (i = 0; i < tdata->nr_frags; i++, frag++) {
212477dfce07SCong Wang 				char *src = kmap_atomic(frag->page);
21259ba682f0Skxie@chelsio.com 
21266a39a16aSIan Campbell 				memcpy(dst, src+frag->offset, frag->size);
21276a39a16aSIan Campbell 				dst += frag->size;
212877dfce07SCong Wang 				kunmap_atomic(src);
21299ba682f0Skxie@chelsio.com 			}
21309ba682f0Skxie@chelsio.com 			if (padlen) {
21319ba682f0Skxie@chelsio.com 				memset(dst, 0, padlen);
21329ba682f0Skxie@chelsio.com 				padlen = 0;
21339ba682f0Skxie@chelsio.com 			}
21349ba682f0Skxie@chelsio.com 			skb_put(skb, count + padlen);
21359ba682f0Skxie@chelsio.com 		} else {
21369ba682f0Skxie@chelsio.com 			/* data fit into frag_list */
21376a39a16aSIan Campbell 			for (i = 0; i < tdata->nr_frags; i++) {
21386a39a16aSIan Campbell 				__skb_fill_page_desc(skb, i,
21396a39a16aSIan Campbell 						tdata->frags[i].page,
21406a39a16aSIan Campbell 						tdata->frags[i].offset,
21416a39a16aSIan Campbell 						tdata->frags[i].size);
21426a39a16aSIan Campbell 				skb_frag_ref(skb, i);
21436a39a16aSIan Campbell 			}
21449ba682f0Skxie@chelsio.com 			skb_shinfo(skb)->nr_frags = tdata->nr_frags;
21459ba682f0Skxie@chelsio.com 			skb->len += count;
21469ba682f0Skxie@chelsio.com 			skb->data_len += count;
21479ba682f0Skxie@chelsio.com 			skb->truesize += count;
21489ba682f0Skxie@chelsio.com 		}
21499ba682f0Skxie@chelsio.com 
21509ba682f0Skxie@chelsio.com 	} else {
21519ba682f0Skxie@chelsio.com 		pg = virt_to_page(task->data);
21529ba682f0Skxie@chelsio.com 
21539ba682f0Skxie@chelsio.com 		get_page(pg);
21549ba682f0Skxie@chelsio.com 		skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
21559ba682f0Skxie@chelsio.com 					count);
21569ba682f0Skxie@chelsio.com 		skb->len += count;
21579ba682f0Skxie@chelsio.com 		skb->data_len += count;
21589ba682f0Skxie@chelsio.com 		skb->truesize += count;
21599ba682f0Skxie@chelsio.com 	}
21609ba682f0Skxie@chelsio.com 
21619ba682f0Skxie@chelsio.com 	if (padlen) {
21629ba682f0Skxie@chelsio.com 		i = skb_shinfo(skb)->nr_frags;
21639ba682f0Skxie@chelsio.com 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
21649ba682f0Skxie@chelsio.com 				virt_to_page(padding), offset_in_page(padding),
21659ba682f0Skxie@chelsio.com 				padlen);
21669ba682f0Skxie@chelsio.com 
21679ba682f0Skxie@chelsio.com 		skb->data_len += padlen;
21689ba682f0Skxie@chelsio.com 		skb->truesize += padlen;
21699ba682f0Skxie@chelsio.com 		skb->len += padlen;
21709ba682f0Skxie@chelsio.com 	}
21719ba682f0Skxie@chelsio.com 
21729ba682f0Skxie@chelsio.com 	return 0;
21739ba682f0Skxie@chelsio.com }
21749ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
21759ba682f0Skxie@chelsio.com 
21769ba682f0Skxie@chelsio.com int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
21779ba682f0Skxie@chelsio.com {
21789ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
21799ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
2180e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
21819ba682f0Skxie@chelsio.com 	struct sk_buff *skb = tdata->skb;
21829ba682f0Skxie@chelsio.com 	unsigned int datalen;
21839ba682f0Skxie@chelsio.com 	int err;
21849ba682f0Skxie@chelsio.com 
21859ba682f0Skxie@chelsio.com 	if (!skb) {
21869ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
21879ba682f0Skxie@chelsio.com 			"task 0x%p, skb NULL.\n", task);
21889ba682f0Skxie@chelsio.com 		return 0;
21899ba682f0Skxie@chelsio.com 	}
21909ba682f0Skxie@chelsio.com 
21919ba682f0Skxie@chelsio.com 	datalen = skb->data_len;
21929ba682f0Skxie@chelsio.com 	tdata->skb = NULL;
21939ba682f0Skxie@chelsio.com 	err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
21949ba682f0Skxie@chelsio.com 	if (err > 0) {
21959ba682f0Skxie@chelsio.com 		int pdulen = err;
21969ba682f0Skxie@chelsio.com 
21979ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
21989ba682f0Skxie@chelsio.com 			"task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
21999ba682f0Skxie@chelsio.com 			task, task->sc, skb, skb->len, skb->data_len, err);
22009ba682f0Skxie@chelsio.com 
22019ba682f0Skxie@chelsio.com 		if (task->conn->hdrdgst_en)
22029ba682f0Skxie@chelsio.com 			pdulen += ISCSI_DIGEST_SIZE;
22039ba682f0Skxie@chelsio.com 
22049ba682f0Skxie@chelsio.com 		if (datalen && task->conn->datadgst_en)
22059ba682f0Skxie@chelsio.com 			pdulen += ISCSI_DIGEST_SIZE;
22069ba682f0Skxie@chelsio.com 
22079ba682f0Skxie@chelsio.com 		task->conn->txdata_octets += pdulen;
22089ba682f0Skxie@chelsio.com 		return 0;
22099ba682f0Skxie@chelsio.com 	}
22109ba682f0Skxie@chelsio.com 
22119ba682f0Skxie@chelsio.com 	if (err == -EAGAIN || err == -ENOBUFS) {
22129ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
22139ba682f0Skxie@chelsio.com 			"task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
22149ba682f0Skxie@chelsio.com 			task, skb, skb->len, skb->data_len, err);
22159ba682f0Skxie@chelsio.com 		/* reset skb to send when we are called again */
22169ba682f0Skxie@chelsio.com 		tdata->skb = skb;
22179ba682f0Skxie@chelsio.com 		return err;
22189ba682f0Skxie@chelsio.com 	}
22199ba682f0Skxie@chelsio.com 
22209ba682f0Skxie@chelsio.com 	kfree_skb(skb);
22219ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
22229ba682f0Skxie@chelsio.com 		"itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
22239ba682f0Skxie@chelsio.com 		task->itt, skb, skb->len, skb->data_len, err);
22249ba682f0Skxie@chelsio.com 	iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
22259ba682f0Skxie@chelsio.com 	iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
22269ba682f0Skxie@chelsio.com 	return err;
22279ba682f0Skxie@chelsio.com }
22289ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
22299ba682f0Skxie@chelsio.com 
22309ba682f0Skxie@chelsio.com void cxgbi_cleanup_task(struct iscsi_task *task)
22319ba682f0Skxie@chelsio.com {
2232e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
22339ba682f0Skxie@chelsio.com 
22349ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
22359ba682f0Skxie@chelsio.com 		"task 0x%p, skb 0x%p, itt 0x%x.\n",
22369ba682f0Skxie@chelsio.com 		task, tdata->skb, task->hdr_itt);
22379ba682f0Skxie@chelsio.com 
22389ba682f0Skxie@chelsio.com 	/*  never reached the xmit task callout */
22399ba682f0Skxie@chelsio.com 	if (tdata->skb)
22409ba682f0Skxie@chelsio.com 		__kfree_skb(tdata->skb);
22419ba682f0Skxie@chelsio.com 	memset(tdata, 0, sizeof(*tdata));
22429ba682f0Skxie@chelsio.com 
22439ba682f0Skxie@chelsio.com 	task_release_itt(task, task->hdr_itt);
22449ba682f0Skxie@chelsio.com 	iscsi_tcp_cleanup_task(task);
22459ba682f0Skxie@chelsio.com }
22469ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
22479ba682f0Skxie@chelsio.com 
22489ba682f0Skxie@chelsio.com void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
22499ba682f0Skxie@chelsio.com 				struct iscsi_stats *stats)
22509ba682f0Skxie@chelsio.com {
22519ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
22529ba682f0Skxie@chelsio.com 
22539ba682f0Skxie@chelsio.com 	stats->txdata_octets = conn->txdata_octets;
22549ba682f0Skxie@chelsio.com 	stats->rxdata_octets = conn->rxdata_octets;
22559ba682f0Skxie@chelsio.com 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
22569ba682f0Skxie@chelsio.com 	stats->dataout_pdus = conn->dataout_pdus_cnt;
22579ba682f0Skxie@chelsio.com 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
22589ba682f0Skxie@chelsio.com 	stats->datain_pdus = conn->datain_pdus_cnt;
22599ba682f0Skxie@chelsio.com 	stats->r2t_pdus = conn->r2t_pdus_cnt;
22609ba682f0Skxie@chelsio.com 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
22619ba682f0Skxie@chelsio.com 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
22629ba682f0Skxie@chelsio.com 	stats->digest_err = 0;
22639ba682f0Skxie@chelsio.com 	stats->timeout_err = 0;
22649ba682f0Skxie@chelsio.com 	stats->custom_length = 1;
22659ba682f0Skxie@chelsio.com 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
22669ba682f0Skxie@chelsio.com 	stats->custom[0].value = conn->eh_abort_cnt;
22679ba682f0Skxie@chelsio.com }
22689ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
22699ba682f0Skxie@chelsio.com 
22709ba682f0Skxie@chelsio.com static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
22719ba682f0Skxie@chelsio.com {
22729ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
22739ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
22749ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
22759ba682f0Skxie@chelsio.com 	unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
22769ba682f0Skxie@chelsio.com 	unsigned int max_def = 512 * MAX_SKB_FRAGS;
22779ba682f0Skxie@chelsio.com 	unsigned int max = max(max_def, headroom);
22789ba682f0Skxie@chelsio.com 
22799ba682f0Skxie@chelsio.com 	max = min(cconn->chba->cdev->tx_max_size, max);
22809ba682f0Skxie@chelsio.com 	if (conn->max_xmit_dlength)
22819ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
22829ba682f0Skxie@chelsio.com 	else
22839ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength = max;
22849ba682f0Skxie@chelsio.com 	cxgbi_align_pdu_size(conn->max_xmit_dlength);
22859ba682f0Skxie@chelsio.com 
22869ba682f0Skxie@chelsio.com 	return 0;
22879ba682f0Skxie@chelsio.com }
22889ba682f0Skxie@chelsio.com 
22899ba682f0Skxie@chelsio.com static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
22909ba682f0Skxie@chelsio.com {
22919ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
22929ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
22939ba682f0Skxie@chelsio.com 	unsigned int max = cconn->chba->cdev->rx_max_size;
22949ba682f0Skxie@chelsio.com 
22959ba682f0Skxie@chelsio.com 	cxgbi_align_pdu_size(max);
22969ba682f0Skxie@chelsio.com 
22979ba682f0Skxie@chelsio.com 	if (conn->max_recv_dlength) {
22989ba682f0Skxie@chelsio.com 		if (conn->max_recv_dlength > max) {
22999ba682f0Skxie@chelsio.com 			pr_err("MaxRecvDataSegmentLength %u > %u.\n",
23009ba682f0Skxie@chelsio.com 				conn->max_recv_dlength, max);
23019ba682f0Skxie@chelsio.com 			return -EINVAL;
23029ba682f0Skxie@chelsio.com 		}
23039ba682f0Skxie@chelsio.com 		conn->max_recv_dlength = min(conn->max_recv_dlength, max);
23049ba682f0Skxie@chelsio.com 		cxgbi_align_pdu_size(conn->max_recv_dlength);
23059ba682f0Skxie@chelsio.com 	} else
23069ba682f0Skxie@chelsio.com 		conn->max_recv_dlength = max;
23079ba682f0Skxie@chelsio.com 
23089ba682f0Skxie@chelsio.com 	return 0;
23099ba682f0Skxie@chelsio.com }
23109ba682f0Skxie@chelsio.com 
23119ba682f0Skxie@chelsio.com int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
23129ba682f0Skxie@chelsio.com 			enum iscsi_param param, char *buf, int buflen)
23139ba682f0Skxie@chelsio.com {
23149ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
23159ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
23169ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
23179ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cconn->cep->csk;
23181304be5fSMike Christie 	int err;
23199ba682f0Skxie@chelsio.com 
23209ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
23219ba682f0Skxie@chelsio.com 		"cls_conn 0x%p, param %d, buf(%d) %s.\n",
23229ba682f0Skxie@chelsio.com 		cls_conn, param, buflen, buf);
23239ba682f0Skxie@chelsio.com 
23249ba682f0Skxie@chelsio.com 	switch (param) {
23259ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_HDRDGST_EN:
23269ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
23279ba682f0Skxie@chelsio.com 		if (!err && conn->hdrdgst_en)
23289ba682f0Skxie@chelsio.com 			err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
23299ba682f0Skxie@chelsio.com 							conn->hdrdgst_en,
23309ba682f0Skxie@chelsio.com 							conn->datadgst_en, 0);
23319ba682f0Skxie@chelsio.com 		break;
23329ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_DATADGST_EN:
23339ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
23349ba682f0Skxie@chelsio.com 		if (!err && conn->datadgst_en)
23359ba682f0Skxie@chelsio.com 			err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
23369ba682f0Skxie@chelsio.com 							conn->hdrdgst_en,
23379ba682f0Skxie@chelsio.com 							conn->datadgst_en, 0);
23389ba682f0Skxie@chelsio.com 		break;
23399ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_R2T:
23401304be5fSMike Christie 		return iscsi_tcp_set_max_r2t(conn, buf);
23419ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
23429ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
23439ba682f0Skxie@chelsio.com 		if (!err)
23449ba682f0Skxie@chelsio.com 			err = cxgbi_conn_max_recv_dlength(conn);
23459ba682f0Skxie@chelsio.com 		break;
23469ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
23479ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
23489ba682f0Skxie@chelsio.com 		if (!err)
23499ba682f0Skxie@chelsio.com 			err = cxgbi_conn_max_xmit_dlength(conn);
23509ba682f0Skxie@chelsio.com 		break;
23519ba682f0Skxie@chelsio.com 	default:
23529ba682f0Skxie@chelsio.com 		return iscsi_set_param(cls_conn, param, buf, buflen);
23539ba682f0Skxie@chelsio.com 	}
23549ba682f0Skxie@chelsio.com 	return err;
23559ba682f0Skxie@chelsio.com }
23569ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
23579ba682f0Skxie@chelsio.com 
2358fc8d0590SAnish Bhatt static inline int csk_print_port(struct cxgbi_sock *csk, char *buf)
2359fc8d0590SAnish Bhatt {
2360fc8d0590SAnish Bhatt 	int len;
2361fc8d0590SAnish Bhatt 
2362fc8d0590SAnish Bhatt 	cxgbi_sock_get(csk);
2363fc8d0590SAnish Bhatt 	len = sprintf(buf, "%hu\n", ntohs(csk->daddr.sin_port));
2364fc8d0590SAnish Bhatt 	cxgbi_sock_put(csk);
2365fc8d0590SAnish Bhatt 
2366fc8d0590SAnish Bhatt 	return len;
2367fc8d0590SAnish Bhatt }
2368fc8d0590SAnish Bhatt 
2369fc8d0590SAnish Bhatt static inline int csk_print_ip(struct cxgbi_sock *csk, char *buf)
2370fc8d0590SAnish Bhatt {
2371fc8d0590SAnish Bhatt 	int len;
2372fc8d0590SAnish Bhatt 
2373fc8d0590SAnish Bhatt 	cxgbi_sock_get(csk);
2374fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
2375fc8d0590SAnish Bhatt 		len = sprintf(buf, "%pI4",
2376fc8d0590SAnish Bhatt 			      &csk->daddr.sin_addr.s_addr);
2377fc8d0590SAnish Bhatt 	else
2378fc8d0590SAnish Bhatt 		len = sprintf(buf, "%pI6",
2379fc8d0590SAnish Bhatt 			      &csk->daddr6.sin6_addr);
2380fc8d0590SAnish Bhatt 
2381fc8d0590SAnish Bhatt 	cxgbi_sock_put(csk);
2382fc8d0590SAnish Bhatt 
2383fc8d0590SAnish Bhatt 	return len;
2384fc8d0590SAnish Bhatt }
2385fc8d0590SAnish Bhatt 
2386c71b9b66SMike Christie int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2387c71b9b66SMike Christie 		       char *buf)
23889ba682f0Skxie@chelsio.com {
2389c71b9b66SMike Christie 	struct cxgbi_endpoint *cep = ep->dd_data;
2390c71b9b66SMike Christie 	struct cxgbi_sock *csk;
23919ba682f0Skxie@chelsio.com 	int len;
23929ba682f0Skxie@chelsio.com 
23939ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
2394c71b9b66SMike Christie 		"cls_conn 0x%p, param %d.\n", ep, param);
23959ba682f0Skxie@chelsio.com 
23969ba682f0Skxie@chelsio.com 	switch (param) {
23979ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_CONN_PORT:
23989ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_CONN_ADDRESS:
2399c71b9b66SMike Christie 		if (!cep)
2400c71b9b66SMike Christie 			return -ENOTCONN;
2401c71b9b66SMike Christie 
2402c71b9b66SMike Christie 		csk = cep->csk;
2403c71b9b66SMike Christie 		if (!csk)
2404c71b9b66SMike Christie 			return -ENOTCONN;
2405c71b9b66SMike Christie 
2406c71b9b66SMike Christie 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2407c71b9b66SMike Christie 						 &csk->daddr, param, buf);
24089ba682f0Skxie@chelsio.com 	default:
2409c71b9b66SMike Christie 		return -ENOSYS;
24109ba682f0Skxie@chelsio.com 	}
24119ba682f0Skxie@chelsio.com 	return len;
24129ba682f0Skxie@chelsio.com }
2413c71b9b66SMike Christie EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
24149ba682f0Skxie@chelsio.com 
24159ba682f0Skxie@chelsio.com struct iscsi_cls_conn *
24169ba682f0Skxie@chelsio.com cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
24179ba682f0Skxie@chelsio.com {
24189ba682f0Skxie@chelsio.com 	struct iscsi_cls_conn *cls_conn;
24199ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn;
24209ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn;
24219ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn;
24229ba682f0Skxie@chelsio.com 
24239ba682f0Skxie@chelsio.com 	cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
24249ba682f0Skxie@chelsio.com 	if (!cls_conn)
24259ba682f0Skxie@chelsio.com 		return NULL;
24269ba682f0Skxie@chelsio.com 
24279ba682f0Skxie@chelsio.com 	conn = cls_conn->dd_data;
24289ba682f0Skxie@chelsio.com 	tcp_conn = conn->dd_data;
24299ba682f0Skxie@chelsio.com 	cconn = tcp_conn->dd_data;
24309ba682f0Skxie@chelsio.com 	cconn->iconn = conn;
24319ba682f0Skxie@chelsio.com 
24329ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24339ba682f0Skxie@chelsio.com 		"cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
24349ba682f0Skxie@chelsio.com 		cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
24359ba682f0Skxie@chelsio.com 
24369ba682f0Skxie@chelsio.com 	return cls_conn;
24379ba682f0Skxie@chelsio.com }
24389ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_create_conn);
24399ba682f0Skxie@chelsio.com 
24409ba682f0Skxie@chelsio.com int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
24419ba682f0Skxie@chelsio.com 				struct iscsi_cls_conn *cls_conn,
24429ba682f0Skxie@chelsio.com 				u64 transport_eph, int is_leading)
24439ba682f0Skxie@chelsio.com {
24449ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
24459ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
24469ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
24479ba682f0Skxie@chelsio.com 	struct iscsi_endpoint *ep;
24489ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
24499ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
24509ba682f0Skxie@chelsio.com 	int err;
24519ba682f0Skxie@chelsio.com 
24529ba682f0Skxie@chelsio.com 	ep = iscsi_lookup_endpoint(transport_eph);
24539ba682f0Skxie@chelsio.com 	if (!ep)
24549ba682f0Skxie@chelsio.com 		return -EINVAL;
24559ba682f0Skxie@chelsio.com 
24569ba682f0Skxie@chelsio.com 	/*  setup ddp pagesize */
24579ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
24589ba682f0Skxie@chelsio.com 	csk = cep->csk;
24599ba682f0Skxie@chelsio.com 	err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid, page_idx, 0);
24609ba682f0Skxie@chelsio.com 	if (err < 0)
24619ba682f0Skxie@chelsio.com 		return err;
24629ba682f0Skxie@chelsio.com 
24639ba682f0Skxie@chelsio.com 	err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
24649ba682f0Skxie@chelsio.com 	if (err)
24659ba682f0Skxie@chelsio.com 		return -EINVAL;
24669ba682f0Skxie@chelsio.com 
24679ba682f0Skxie@chelsio.com 	/*  calculate the tag idx bits needed for this conn based on cmds_max */
24689ba682f0Skxie@chelsio.com 	cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
24699ba682f0Skxie@chelsio.com 
2470e3d2ad8cSkxie@chelsio.com 	write_lock_bh(&csk->callback_lock);
24719ba682f0Skxie@chelsio.com 	csk->user_data = conn;
24729ba682f0Skxie@chelsio.com 	cconn->chba = cep->chba;
24739ba682f0Skxie@chelsio.com 	cconn->cep = cep;
24749ba682f0Skxie@chelsio.com 	cep->cconn = cconn;
2475e3d2ad8cSkxie@chelsio.com 	write_unlock_bh(&csk->callback_lock);
24769ba682f0Skxie@chelsio.com 
24779ba682f0Skxie@chelsio.com 	cxgbi_conn_max_xmit_dlength(conn);
24789ba682f0Skxie@chelsio.com 	cxgbi_conn_max_recv_dlength(conn);
24799ba682f0Skxie@chelsio.com 
24809ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24819ba682f0Skxie@chelsio.com 		"cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
24829ba682f0Skxie@chelsio.com 		cls_session, cls_conn, ep, cconn, csk);
24839ba682f0Skxie@chelsio.com 	/*  init recv engine */
24849ba682f0Skxie@chelsio.com 	iscsi_tcp_hdr_recv_prep(tcp_conn);
24859ba682f0Skxie@chelsio.com 
24869ba682f0Skxie@chelsio.com 	return 0;
24879ba682f0Skxie@chelsio.com }
24889ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
24899ba682f0Skxie@chelsio.com 
24909ba682f0Skxie@chelsio.com struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
24919ba682f0Skxie@chelsio.com 						u16 cmds_max, u16 qdepth,
24929ba682f0Skxie@chelsio.com 						u32 initial_cmdsn)
24939ba682f0Skxie@chelsio.com {
24949ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
24959ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
24969ba682f0Skxie@chelsio.com 	struct Scsi_Host *shost;
24979ba682f0Skxie@chelsio.com 	struct iscsi_cls_session *cls_session;
24989ba682f0Skxie@chelsio.com 	struct iscsi_session *session;
24999ba682f0Skxie@chelsio.com 
25009ba682f0Skxie@chelsio.com 	if (!ep) {
25019ba682f0Skxie@chelsio.com 		pr_err("missing endpoint.\n");
25029ba682f0Skxie@chelsio.com 		return NULL;
25039ba682f0Skxie@chelsio.com 	}
25049ba682f0Skxie@chelsio.com 
25059ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
25069ba682f0Skxie@chelsio.com 	chba = cep->chba;
25079ba682f0Skxie@chelsio.com 	shost = chba->shost;
25089ba682f0Skxie@chelsio.com 
25099ba682f0Skxie@chelsio.com 	BUG_ON(chba != iscsi_host_priv(shost));
25109ba682f0Skxie@chelsio.com 
25119ba682f0Skxie@chelsio.com 	cls_session = iscsi_session_setup(chba->cdev->itp, shost,
25129ba682f0Skxie@chelsio.com 					cmds_max, 0,
25139ba682f0Skxie@chelsio.com 					sizeof(struct iscsi_tcp_task) +
25149ba682f0Skxie@chelsio.com 					sizeof(struct cxgbi_task_data),
25159ba682f0Skxie@chelsio.com 					initial_cmdsn, ISCSI_MAX_TARGET);
25169ba682f0Skxie@chelsio.com 	if (!cls_session)
25179ba682f0Skxie@chelsio.com 		return NULL;
25189ba682f0Skxie@chelsio.com 
25199ba682f0Skxie@chelsio.com 	session = cls_session->dd_data;
25209ba682f0Skxie@chelsio.com 	if (iscsi_tcp_r2tpool_alloc(session))
25219ba682f0Skxie@chelsio.com 		goto remove_session;
25229ba682f0Skxie@chelsio.com 
25239ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
25249ba682f0Skxie@chelsio.com 		"ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
25259ba682f0Skxie@chelsio.com 	return cls_session;
25269ba682f0Skxie@chelsio.com 
25279ba682f0Skxie@chelsio.com remove_session:
25289ba682f0Skxie@chelsio.com 	iscsi_session_teardown(cls_session);
25299ba682f0Skxie@chelsio.com 	return NULL;
25309ba682f0Skxie@chelsio.com }
25319ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_create_session);
25329ba682f0Skxie@chelsio.com 
25339ba682f0Skxie@chelsio.com void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
25349ba682f0Skxie@chelsio.com {
25359ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
25369ba682f0Skxie@chelsio.com 		"cls sess 0x%p.\n", cls_session);
25379ba682f0Skxie@chelsio.com 
25389ba682f0Skxie@chelsio.com 	iscsi_tcp_r2tpool_free(cls_session->dd_data);
25399ba682f0Skxie@chelsio.com 	iscsi_session_teardown(cls_session);
25409ba682f0Skxie@chelsio.com }
25419ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
25429ba682f0Skxie@chelsio.com 
25439ba682f0Skxie@chelsio.com int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
25449ba682f0Skxie@chelsio.com 			char *buf, int buflen)
25459ba682f0Skxie@chelsio.com {
25469ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = iscsi_host_priv(shost);
25479ba682f0Skxie@chelsio.com 
25489ba682f0Skxie@chelsio.com 	if (!chba->ndev) {
25499ba682f0Skxie@chelsio.com 		shost_printk(KERN_ERR, shost, "Could not get host param. "
25509ba682f0Skxie@chelsio.com 				"netdev for host not set.\n");
25519ba682f0Skxie@chelsio.com 		return -ENODEV;
25529ba682f0Skxie@chelsio.com 	}
25539ba682f0Skxie@chelsio.com 
25549ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
25559ba682f0Skxie@chelsio.com 		"shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
25569ba682f0Skxie@chelsio.com 		shost, chba, chba->ndev->name, param, buflen, buf);
25579ba682f0Skxie@chelsio.com 
25589ba682f0Skxie@chelsio.com 	switch (param) {
25599ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_IPADDRESS:
25609ba682f0Skxie@chelsio.com 	{
25619ba682f0Skxie@chelsio.com 		__be32 addr = in_aton(buf);
25629ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
25639ba682f0Skxie@chelsio.com 			"hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
25649ba682f0Skxie@chelsio.com 		cxgbi_set_iscsi_ipv4(chba, addr);
25659ba682f0Skxie@chelsio.com 		return 0;
25669ba682f0Skxie@chelsio.com 	}
25679ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_HWADDRESS:
25689ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_NETDEV_NAME:
25699ba682f0Skxie@chelsio.com 		return 0;
25709ba682f0Skxie@chelsio.com 	default:
25719ba682f0Skxie@chelsio.com 		return iscsi_host_set_param(shost, param, buf, buflen);
25729ba682f0Skxie@chelsio.com 	}
25739ba682f0Skxie@chelsio.com }
25749ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
25759ba682f0Skxie@chelsio.com 
25769ba682f0Skxie@chelsio.com int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
25779ba682f0Skxie@chelsio.com 			char *buf)
25789ba682f0Skxie@chelsio.com {
25799ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = iscsi_host_priv(shost);
25809ba682f0Skxie@chelsio.com 	int len = 0;
25819ba682f0Skxie@chelsio.com 
25829ba682f0Skxie@chelsio.com 	if (!chba->ndev) {
25839ba682f0Skxie@chelsio.com 		shost_printk(KERN_ERR, shost, "Could not get host param. "
25849ba682f0Skxie@chelsio.com 				"netdev for host not set.\n");
25859ba682f0Skxie@chelsio.com 		return -ENODEV;
25869ba682f0Skxie@chelsio.com 	}
25879ba682f0Skxie@chelsio.com 
25889ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
25899ba682f0Skxie@chelsio.com 		"shost 0x%p, hba 0x%p,%s, param %d.\n",
25909ba682f0Skxie@chelsio.com 		shost, chba, chba->ndev->name, param);
25919ba682f0Skxie@chelsio.com 
25929ba682f0Skxie@chelsio.com 	switch (param) {
25939ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_HWADDRESS:
25949ba682f0Skxie@chelsio.com 		len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
25959ba682f0Skxie@chelsio.com 		break;
25969ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_NETDEV_NAME:
25979ba682f0Skxie@chelsio.com 		len = sprintf(buf, "%s\n", chba->ndev->name);
25989ba682f0Skxie@chelsio.com 		break;
25999ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_IPADDRESS:
26009ba682f0Skxie@chelsio.com 	{
26019ba682f0Skxie@chelsio.com 		__be32 addr;
26029ba682f0Skxie@chelsio.com 
26039ba682f0Skxie@chelsio.com 		addr = cxgbi_get_iscsi_ipv4(chba);
26049ba682f0Skxie@chelsio.com 		len = sprintf(buf, "%pI4", &addr);
26059ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
26069ba682f0Skxie@chelsio.com 			"hba %s, ipv4 %pI4.\n", chba->ndev->name, &addr);
26079ba682f0Skxie@chelsio.com 		break;
26089ba682f0Skxie@chelsio.com 	}
26099ba682f0Skxie@chelsio.com 	default:
26109ba682f0Skxie@chelsio.com 		return iscsi_host_get_param(shost, param, buf);
26119ba682f0Skxie@chelsio.com 	}
26129ba682f0Skxie@chelsio.com 
26139ba682f0Skxie@chelsio.com 	return len;
26149ba682f0Skxie@chelsio.com }
26159ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
26169ba682f0Skxie@chelsio.com 
26179ba682f0Skxie@chelsio.com struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
26189ba682f0Skxie@chelsio.com 					struct sockaddr *dst_addr,
26199ba682f0Skxie@chelsio.com 					int non_blocking)
26209ba682f0Skxie@chelsio.com {
26219ba682f0Skxie@chelsio.com 	struct iscsi_endpoint *ep;
26229ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
26239ba682f0Skxie@chelsio.com 	struct cxgbi_hba *hba = NULL;
26249ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
26259ba682f0Skxie@chelsio.com 	int err = -EINVAL;
26269ba682f0Skxie@chelsio.com 
26279ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
26289ba682f0Skxie@chelsio.com 		"shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
26299ba682f0Skxie@chelsio.com 		shost, non_blocking, dst_addr);
26309ba682f0Skxie@chelsio.com 
26319ba682f0Skxie@chelsio.com 	if (shost) {
26329ba682f0Skxie@chelsio.com 		hba = iscsi_host_priv(shost);
26339ba682f0Skxie@chelsio.com 		if (!hba) {
26349ba682f0Skxie@chelsio.com 			pr_info("shost 0x%p, priv NULL.\n", shost);
26359ba682f0Skxie@chelsio.com 			goto err_out;
26369ba682f0Skxie@chelsio.com 		}
26379ba682f0Skxie@chelsio.com 	}
26389ba682f0Skxie@chelsio.com 
2639fc8d0590SAnish Bhatt 	if (dst_addr->sa_family == AF_INET) {
26409ba682f0Skxie@chelsio.com 		csk = cxgbi_check_route(dst_addr);
2641fc8d0590SAnish Bhatt 	} else if (dst_addr->sa_family == AF_INET6) {
2642fc8d0590SAnish Bhatt 		csk = cxgbi_check_route6(dst_addr);
2643fc8d0590SAnish Bhatt 	} else {
2644fc8d0590SAnish Bhatt 		pr_info("address family 0x%x NOT supported.\n",
2645fc8d0590SAnish Bhatt 			dst_addr->sa_family);
2646fc8d0590SAnish Bhatt 		err = -EAFNOSUPPORT;
2647fc8d0590SAnish Bhatt 		return (struct iscsi_endpoint *)ERR_PTR(err);
2648fc8d0590SAnish Bhatt 	}
2649fc8d0590SAnish Bhatt 
26509ba682f0Skxie@chelsio.com 	if (IS_ERR(csk))
26519ba682f0Skxie@chelsio.com 		return (struct iscsi_endpoint *)csk;
26529ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
26539ba682f0Skxie@chelsio.com 
26549ba682f0Skxie@chelsio.com 	if (!hba)
26559ba682f0Skxie@chelsio.com 		hba = csk->cdev->hbas[csk->port_id];
26569ba682f0Skxie@chelsio.com 	else if (hba != csk->cdev->hbas[csk->port_id]) {
26579ba682f0Skxie@chelsio.com 		pr_info("Could not connect through requested host %u"
26589ba682f0Skxie@chelsio.com 			"hba 0x%p != 0x%p (%u).\n",
26599ba682f0Skxie@chelsio.com 			shost->host_no, hba,
26609ba682f0Skxie@chelsio.com 			csk->cdev->hbas[csk->port_id], csk->port_id);
26619ba682f0Skxie@chelsio.com 		err = -ENOSPC;
26629ba682f0Skxie@chelsio.com 		goto release_conn;
26639ba682f0Skxie@chelsio.com 	}
26649ba682f0Skxie@chelsio.com 
26659ba682f0Skxie@chelsio.com 	err = sock_get_port(csk);
26669ba682f0Skxie@chelsio.com 	if (err)
26679ba682f0Skxie@chelsio.com 		goto release_conn;
26689ba682f0Skxie@chelsio.com 
26699ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CONNECTING);
26709ba682f0Skxie@chelsio.com 	err = csk->cdev->csk_init_act_open(csk);
26719ba682f0Skxie@chelsio.com 	if (err)
26729ba682f0Skxie@chelsio.com 		goto release_conn;
26739ba682f0Skxie@chelsio.com 
26749ba682f0Skxie@chelsio.com 	if (cxgbi_sock_is_closing(csk)) {
26759ba682f0Skxie@chelsio.com 		err = -ENOSPC;
26769ba682f0Skxie@chelsio.com 		pr_info("csk 0x%p is closing.\n", csk);
26779ba682f0Skxie@chelsio.com 		goto release_conn;
26789ba682f0Skxie@chelsio.com 	}
26799ba682f0Skxie@chelsio.com 
26809ba682f0Skxie@chelsio.com 	ep = iscsi_create_endpoint(sizeof(*cep));
26819ba682f0Skxie@chelsio.com 	if (!ep) {
26829ba682f0Skxie@chelsio.com 		err = -ENOMEM;
26839ba682f0Skxie@chelsio.com 		pr_info("iscsi alloc ep, OOM.\n");
26849ba682f0Skxie@chelsio.com 		goto release_conn;
26859ba682f0Skxie@chelsio.com 	}
26869ba682f0Skxie@chelsio.com 
26879ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
26889ba682f0Skxie@chelsio.com 	cep->csk = csk;
26899ba682f0Skxie@chelsio.com 	cep->chba = hba;
26909ba682f0Skxie@chelsio.com 
26919ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
26929ba682f0Skxie@chelsio.com 		"ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
26939ba682f0Skxie@chelsio.com 		ep, cep, csk, hba, hba->ndev->name);
26949ba682f0Skxie@chelsio.com 	return ep;
26959ba682f0Skxie@chelsio.com 
26969ba682f0Skxie@chelsio.com release_conn:
26979ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
26989ba682f0Skxie@chelsio.com 	cxgbi_sock_closed(csk);
26999ba682f0Skxie@chelsio.com err_out:
27009ba682f0Skxie@chelsio.com 	return ERR_PTR(err);
27019ba682f0Skxie@chelsio.com }
27029ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
27039ba682f0Skxie@chelsio.com 
27049ba682f0Skxie@chelsio.com int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
27059ba682f0Skxie@chelsio.com {
27069ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep = ep->dd_data;
27079ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cep->csk;
27089ba682f0Skxie@chelsio.com 
27099ba682f0Skxie@chelsio.com 	if (!cxgbi_sock_is_established(csk))
27109ba682f0Skxie@chelsio.com 		return 0;
27119ba682f0Skxie@chelsio.com 	return 1;
27129ba682f0Skxie@chelsio.com }
27139ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
27149ba682f0Skxie@chelsio.com 
27159ba682f0Skxie@chelsio.com void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
27169ba682f0Skxie@chelsio.com {
27179ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep = ep->dd_data;
27189ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = cep->cconn;
27199ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cep->csk;
27209ba682f0Skxie@chelsio.com 
27219ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
27229ba682f0Skxie@chelsio.com 		"ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
27239ba682f0Skxie@chelsio.com 		ep, cep, cconn, csk, csk->state, csk->flags);
27249ba682f0Skxie@chelsio.com 
27259ba682f0Skxie@chelsio.com 	if (cconn && cconn->iconn) {
27269ba682f0Skxie@chelsio.com 		iscsi_suspend_tx(cconn->iconn);
27279ba682f0Skxie@chelsio.com 		write_lock_bh(&csk->callback_lock);
27289ba682f0Skxie@chelsio.com 		cep->csk->user_data = NULL;
27299ba682f0Skxie@chelsio.com 		cconn->cep = NULL;
27309ba682f0Skxie@chelsio.com 		write_unlock_bh(&csk->callback_lock);
27319ba682f0Skxie@chelsio.com 	}
27329ba682f0Skxie@chelsio.com 	iscsi_destroy_endpoint(ep);
27339ba682f0Skxie@chelsio.com 
27349ba682f0Skxie@chelsio.com 	if (likely(csk->state >= CTP_ESTABLISHED))
27359ba682f0Skxie@chelsio.com 		need_active_close(csk);
27369ba682f0Skxie@chelsio.com 	else
27379ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
27389ba682f0Skxie@chelsio.com 
27399ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
27409ba682f0Skxie@chelsio.com }
27419ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
27429ba682f0Skxie@chelsio.com 
27439ba682f0Skxie@chelsio.com int cxgbi_iscsi_init(struct iscsi_transport *itp,
27449ba682f0Skxie@chelsio.com 			struct scsi_transport_template **stt)
27459ba682f0Skxie@chelsio.com {
27469ba682f0Skxie@chelsio.com 	*stt = iscsi_register_transport(itp);
27479ba682f0Skxie@chelsio.com 	if (*stt == NULL) {
27489ba682f0Skxie@chelsio.com 		pr_err("unable to register %s transport 0x%p.\n",
27499ba682f0Skxie@chelsio.com 			itp->name, itp);
27509ba682f0Skxie@chelsio.com 		return -ENODEV;
27519ba682f0Skxie@chelsio.com 	}
27529ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
27539ba682f0Skxie@chelsio.com 		"%s, registered iscsi transport 0x%p.\n",
27549ba682f0Skxie@chelsio.com 		itp->name, stt);
27559ba682f0Skxie@chelsio.com 	return 0;
27569ba682f0Skxie@chelsio.com }
27579ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
27589ba682f0Skxie@chelsio.com 
27599ba682f0Skxie@chelsio.com void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
27609ba682f0Skxie@chelsio.com 			struct scsi_transport_template **stt)
27619ba682f0Skxie@chelsio.com {
27629ba682f0Skxie@chelsio.com 	if (*stt) {
27639ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
27649ba682f0Skxie@chelsio.com 			"de-register transport 0x%p, %s, stt 0x%p.\n",
27659ba682f0Skxie@chelsio.com 			itp, itp->name, *stt);
27669ba682f0Skxie@chelsio.com 		*stt = NULL;
27679ba682f0Skxie@chelsio.com 		iscsi_unregister_transport(itp);
27689ba682f0Skxie@chelsio.com 	}
27699ba682f0Skxie@chelsio.com }
27709ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
27719ba682f0Skxie@chelsio.com 
2772587a1f16SAl Viro umode_t cxgbi_attr_is_visible(int param_type, int param)
27733128c6c7SMike Christie {
27743128c6c7SMike Christie 	switch (param_type) {
2775f27fb2efSMike Christie 	case ISCSI_HOST_PARAM:
2776f27fb2efSMike Christie 		switch (param) {
2777f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_NETDEV_NAME:
2778f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_HWADDRESS:
2779f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_IPADDRESS:
2780f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
2781f27fb2efSMike Christie 			return S_IRUGO;
2782f27fb2efSMike Christie 		default:
2783f27fb2efSMike Christie 			return 0;
2784f27fb2efSMike Christie 		}
27853128c6c7SMike Christie 	case ISCSI_PARAM:
27863128c6c7SMike Christie 		switch (param) {
27873128c6c7SMike Christie 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
27883128c6c7SMike Christie 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
27893128c6c7SMike Christie 		case ISCSI_PARAM_HDRDGST_EN:
27903128c6c7SMike Christie 		case ISCSI_PARAM_DATADGST_EN:
27913128c6c7SMike Christie 		case ISCSI_PARAM_CONN_ADDRESS:
27923128c6c7SMike Christie 		case ISCSI_PARAM_CONN_PORT:
27933128c6c7SMike Christie 		case ISCSI_PARAM_EXP_STATSN:
27943128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
27953128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_PORT:
27963128c6c7SMike Christie 		case ISCSI_PARAM_PING_TMO:
27973128c6c7SMike Christie 		case ISCSI_PARAM_RECV_TMO:
27981d063c17SMike Christie 		case ISCSI_PARAM_INITIAL_R2T_EN:
27991d063c17SMike Christie 		case ISCSI_PARAM_MAX_R2T:
28001d063c17SMike Christie 		case ISCSI_PARAM_IMM_DATA_EN:
28011d063c17SMike Christie 		case ISCSI_PARAM_FIRST_BURST:
28021d063c17SMike Christie 		case ISCSI_PARAM_MAX_BURST:
28031d063c17SMike Christie 		case ISCSI_PARAM_PDU_INORDER_EN:
28041d063c17SMike Christie 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
28051d063c17SMike Christie 		case ISCSI_PARAM_ERL:
28061d063c17SMike Christie 		case ISCSI_PARAM_TARGET_NAME:
28071d063c17SMike Christie 		case ISCSI_PARAM_TPGT:
28081d063c17SMike Christie 		case ISCSI_PARAM_USERNAME:
28091d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD:
28101d063c17SMike Christie 		case ISCSI_PARAM_USERNAME_IN:
28111d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD_IN:
28121d063c17SMike Christie 		case ISCSI_PARAM_FAST_ABORT:
28131d063c17SMike Christie 		case ISCSI_PARAM_ABORT_TMO:
28141d063c17SMike Christie 		case ISCSI_PARAM_LU_RESET_TMO:
28151d063c17SMike Christie 		case ISCSI_PARAM_TGT_RESET_TMO:
28161d063c17SMike Christie 		case ISCSI_PARAM_IFACE_NAME:
28171d063c17SMike Christie 		case ISCSI_PARAM_INITIATOR_NAME:
28183128c6c7SMike Christie 			return S_IRUGO;
28193128c6c7SMike Christie 		default:
28203128c6c7SMike Christie 			return 0;
28213128c6c7SMike Christie 		}
28223128c6c7SMike Christie 	}
28233128c6c7SMike Christie 
28243128c6c7SMike Christie 	return 0;
28253128c6c7SMike Christie }
28263128c6c7SMike Christie EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
28273128c6c7SMike Christie 
28289ba682f0Skxie@chelsio.com static int __init libcxgbi_init_module(void)
28299ba682f0Skxie@chelsio.com {
28309ba682f0Skxie@chelsio.com 	sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
28319ba682f0Skxie@chelsio.com 	sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
28329ba682f0Skxie@chelsio.com 
28339ba682f0Skxie@chelsio.com 	pr_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
28349ba682f0Skxie@chelsio.com 		ISCSI_ITT_MASK, sw_tag_idx_bits,
28359ba682f0Skxie@chelsio.com 		ISCSI_AGE_MASK, sw_tag_age_bits);
28369ba682f0Skxie@chelsio.com 
28379ba682f0Skxie@chelsio.com 	ddp_setup_host_page_size();
28389ba682f0Skxie@chelsio.com 	return 0;
28399ba682f0Skxie@chelsio.com }
28409ba682f0Skxie@chelsio.com 
28419ba682f0Skxie@chelsio.com static void __exit libcxgbi_exit_module(void)
28429ba682f0Skxie@chelsio.com {
28439ba682f0Skxie@chelsio.com 	cxgbi_device_unregister_all(0xFF);
28449ba682f0Skxie@chelsio.com 	return;
28459ba682f0Skxie@chelsio.com }
28469ba682f0Skxie@chelsio.com 
28479ba682f0Skxie@chelsio.com module_init(libcxgbi_init_module);
28489ba682f0Skxie@chelsio.com module_exit(libcxgbi_exit_module);
2849