xref: /openbmc/linux/drivers/scsi/cxgbi/libcxgbi.c (revision dd04b0f3b60a0144dcddf13b62392488d91bf8f6)
19ba682f0Skxie@chelsio.com /*
29ba682f0Skxie@chelsio.com  * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
39ba682f0Skxie@chelsio.com  *
41149a5edSKaren Xie  * Copyright (c) 2010-2015 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"
410ea5bf3dSKaren Xie #define DRV_MODULE_VERSION	"0.9.1-ko"
420ea5bf3dSKaren Xie #define DRV_MODULE_RELDATE	"Apr. 2015"
430ea5bf3dSKaren Xie 
440ea5bf3dSKaren Xie static char version[] =
450ea5bf3dSKaren Xie 	DRV_MODULE_DESC " " DRV_MODULE_NAME
460ea5bf3dSKaren Xie 	" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
479ba682f0Skxie@chelsio.com 
489ba682f0Skxie@chelsio.com MODULE_AUTHOR("Chelsio Communications, Inc.");
499ba682f0Skxie@chelsio.com MODULE_DESCRIPTION(DRV_MODULE_DESC);
509ba682f0Skxie@chelsio.com MODULE_VERSION(DRV_MODULE_VERSION);
519ba682f0Skxie@chelsio.com MODULE_LICENSE("GPL");
529ba682f0Skxie@chelsio.com 
539ba682f0Skxie@chelsio.com module_param(dbg_level, uint, 0644);
549ba682f0Skxie@chelsio.com MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
559ba682f0Skxie@chelsio.com 
569ba682f0Skxie@chelsio.com 
579ba682f0Skxie@chelsio.com /*
589ba682f0Skxie@chelsio.com  * cxgbi device management
599ba682f0Skxie@chelsio.com  * maintains a list of the cxgbi devices
609ba682f0Skxie@chelsio.com  */
619ba682f0Skxie@chelsio.com static LIST_HEAD(cdev_list);
629ba682f0Skxie@chelsio.com static DEFINE_MUTEX(cdev_mutex);
639ba682f0Skxie@chelsio.com 
64576b5863SAnish Bhatt static LIST_HEAD(cdev_rcu_list);
65576b5863SAnish Bhatt static DEFINE_SPINLOCK(cdev_rcu_lock);
66576b5863SAnish Bhatt 
6771f7a00bSVarun Prakash static inline void cxgbi_decode_sw_tag(u32 sw_tag, int *idx, int *age)
6871f7a00bSVarun Prakash {
6971f7a00bSVarun Prakash 	if (age)
7071f7a00bSVarun Prakash 		*age = sw_tag & 0x7FFF;
7171f7a00bSVarun Prakash 	if (idx)
7271f7a00bSVarun Prakash 		*idx = (sw_tag >> 16) & 0x7FFF;
7371f7a00bSVarun Prakash }
7471f7a00bSVarun Prakash 
759ba682f0Skxie@chelsio.com int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
769ba682f0Skxie@chelsio.com 				unsigned int max_conn)
779ba682f0Skxie@chelsio.com {
789ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
799ba682f0Skxie@chelsio.com 
809ba682f0Skxie@chelsio.com 	pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
819ba682f0Skxie@chelsio.com 					     sizeof(struct cxgbi_sock *),
829ba682f0Skxie@chelsio.com 					     GFP_KERNEL);
839ba682f0Skxie@chelsio.com 	if (!pmap->port_csk) {
849ba682f0Skxie@chelsio.com 		pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
859ba682f0Skxie@chelsio.com 		return -ENOMEM;
869ba682f0Skxie@chelsio.com 	}
879ba682f0Skxie@chelsio.com 
889ba682f0Skxie@chelsio.com 	pmap->max_connect = max_conn;
899ba682f0Skxie@chelsio.com 	pmap->sport_base = base;
909ba682f0Skxie@chelsio.com 	spin_lock_init(&pmap->lock);
919ba682f0Skxie@chelsio.com 	return 0;
929ba682f0Skxie@chelsio.com }
939ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
949ba682f0Skxie@chelsio.com 
959ba682f0Skxie@chelsio.com void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
969ba682f0Skxie@chelsio.com {
979ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
989ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
999ba682f0Skxie@chelsio.com 	int i;
1009ba682f0Skxie@chelsio.com 
1019ba682f0Skxie@chelsio.com 	for (i = 0; i < pmap->max_connect; i++) {
1029ba682f0Skxie@chelsio.com 		if (pmap->port_csk[i]) {
1039ba682f0Skxie@chelsio.com 			csk = pmap->port_csk[i];
1049ba682f0Skxie@chelsio.com 			pmap->port_csk[i] = NULL;
1059ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_SOCK,
1069ba682f0Skxie@chelsio.com 				"csk 0x%p, cdev 0x%p, offload down.\n",
1079ba682f0Skxie@chelsio.com 				csk, cdev);
1089ba682f0Skxie@chelsio.com 			spin_lock_bh(&csk->lock);
1099ba682f0Skxie@chelsio.com 			cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
1109ba682f0Skxie@chelsio.com 			cxgbi_sock_closed(csk);
1119ba682f0Skxie@chelsio.com 			spin_unlock_bh(&csk->lock);
1129ba682f0Skxie@chelsio.com 			cxgbi_sock_put(csk);
1139ba682f0Skxie@chelsio.com 		}
1149ba682f0Skxie@chelsio.com 	}
1159ba682f0Skxie@chelsio.com }
1169ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
1179ba682f0Skxie@chelsio.com 
1189ba682f0Skxie@chelsio.com static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
1199ba682f0Skxie@chelsio.com {
1209ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1219ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u.\n", cdev, cdev->nports);
1229ba682f0Skxie@chelsio.com 	cxgbi_hbas_remove(cdev);
1239ba682f0Skxie@chelsio.com 	cxgbi_device_portmap_cleanup(cdev);
1249d5c44b7SVarun Prakash 	cxgbi_ppm_release(cdev->cdev2ppm(cdev));
1259ba682f0Skxie@chelsio.com 	if (cdev->pmap.max_connect)
1269ba682f0Skxie@chelsio.com 		cxgbi_free_big_mem(cdev->pmap.port_csk);
1279ba682f0Skxie@chelsio.com 	kfree(cdev);
1289ba682f0Skxie@chelsio.com }
1299ba682f0Skxie@chelsio.com 
1309ba682f0Skxie@chelsio.com struct cxgbi_device *cxgbi_device_register(unsigned int extra,
1319ba682f0Skxie@chelsio.com 					   unsigned int nports)
1329ba682f0Skxie@chelsio.com {
1339ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev;
1349ba682f0Skxie@chelsio.com 
1359ba682f0Skxie@chelsio.com 	cdev = kzalloc(sizeof(*cdev) + extra + nports *
1369ba682f0Skxie@chelsio.com 			(sizeof(struct cxgbi_hba *) +
1379ba682f0Skxie@chelsio.com 			 sizeof(struct net_device *)),
1389ba682f0Skxie@chelsio.com 			GFP_KERNEL);
1399ba682f0Skxie@chelsio.com 	if (!cdev) {
1409ba682f0Skxie@chelsio.com 		pr_warn("nport %d, OOM.\n", nports);
1419ba682f0Skxie@chelsio.com 		return NULL;
1429ba682f0Skxie@chelsio.com 	}
1439ba682f0Skxie@chelsio.com 	cdev->ports = (struct net_device **)(cdev + 1);
1449ba682f0Skxie@chelsio.com 	cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
1459ba682f0Skxie@chelsio.com 						sizeof(struct net_device *));
1469ba682f0Skxie@chelsio.com 	if (extra)
1479ba682f0Skxie@chelsio.com 		cdev->dd_data = ((char *)cdev->hbas) +
1489ba682f0Skxie@chelsio.com 				nports * sizeof(struct cxgbi_hba *);
1499ba682f0Skxie@chelsio.com 	spin_lock_init(&cdev->pmap.lock);
1509ba682f0Skxie@chelsio.com 
1519ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1529ba682f0Skxie@chelsio.com 	list_add_tail(&cdev->list_head, &cdev_list);
1539ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1549ba682f0Skxie@chelsio.com 
155576b5863SAnish Bhatt 	spin_lock(&cdev_rcu_lock);
156576b5863SAnish Bhatt 	list_add_tail_rcu(&cdev->rcu_node, &cdev_rcu_list);
157576b5863SAnish Bhatt 	spin_unlock(&cdev_rcu_lock);
158576b5863SAnish Bhatt 
1599ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1609ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u.\n", cdev, nports);
1619ba682f0Skxie@chelsio.com 	return cdev;
1629ba682f0Skxie@chelsio.com }
1639ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_register);
1649ba682f0Skxie@chelsio.com 
1659ba682f0Skxie@chelsio.com void cxgbi_device_unregister(struct cxgbi_device *cdev)
1669ba682f0Skxie@chelsio.com {
1679ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1689ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u,%s.\n",
1699ba682f0Skxie@chelsio.com 		cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
170576b5863SAnish Bhatt 
1719ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1729ba682f0Skxie@chelsio.com 	list_del(&cdev->list_head);
1739ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
174576b5863SAnish Bhatt 
175576b5863SAnish Bhatt 	spin_lock(&cdev_rcu_lock);
176576b5863SAnish Bhatt 	list_del_rcu(&cdev->rcu_node);
177576b5863SAnish Bhatt 	spin_unlock(&cdev_rcu_lock);
178576b5863SAnish Bhatt 	synchronize_rcu();
179576b5863SAnish Bhatt 
1809ba682f0Skxie@chelsio.com 	cxgbi_device_destroy(cdev);
1819ba682f0Skxie@chelsio.com }
1829ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
1839ba682f0Skxie@chelsio.com 
1849ba682f0Skxie@chelsio.com void cxgbi_device_unregister_all(unsigned int flag)
1859ba682f0Skxie@chelsio.com {
1869ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
1879ba682f0Skxie@chelsio.com 
1889ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1899ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
1909ba682f0Skxie@chelsio.com 		if ((cdev->flags & flag) == flag) {
191576b5863SAnish Bhatt 			mutex_unlock(&cdev_mutex);
192576b5863SAnish Bhatt 			cxgbi_device_unregister(cdev);
193576b5863SAnish Bhatt 			mutex_lock(&cdev_mutex);
1949ba682f0Skxie@chelsio.com 		}
1959ba682f0Skxie@chelsio.com 	}
1969ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1979ba682f0Skxie@chelsio.com }
1989ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
1999ba682f0Skxie@chelsio.com 
2009ba682f0Skxie@chelsio.com struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
2019ba682f0Skxie@chelsio.com {
2029ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
2039ba682f0Skxie@chelsio.com 
2049ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
2059ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
2069ba682f0Skxie@chelsio.com 		if (cdev->lldev == lldev) {
2079ba682f0Skxie@chelsio.com 			mutex_unlock(&cdev_mutex);
2089ba682f0Skxie@chelsio.com 			return cdev;
2099ba682f0Skxie@chelsio.com 		}
2109ba682f0Skxie@chelsio.com 	}
2119ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
212576b5863SAnish Bhatt 
2139ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
2149ba682f0Skxie@chelsio.com 		"lldev 0x%p, NO match found.\n", lldev);
2159ba682f0Skxie@chelsio.com 	return NULL;
2169ba682f0Skxie@chelsio.com }
2179ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
2189ba682f0Skxie@chelsio.com 
219fc8d0590SAnish Bhatt struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
2209ba682f0Skxie@chelsio.com 						 int *port)
2219ba682f0Skxie@chelsio.com {
2220b3d8947Skxie@chelsio.com 	struct net_device *vdev = NULL;
2239ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
2249ba682f0Skxie@chelsio.com 	int i;
2259ba682f0Skxie@chelsio.com 
226d0d7b10bSParav Pandit 	if (is_vlan_dev(ndev)) {
2270b3d8947Skxie@chelsio.com 		vdev = ndev;
2289ba682f0Skxie@chelsio.com 		ndev = vlan_dev_real_dev(ndev);
2290b3d8947Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
2300b3d8947Skxie@chelsio.com 			"vlan dev %s -> %s.\n", vdev->name, ndev->name);
2310b3d8947Skxie@chelsio.com 	}
2329ba682f0Skxie@chelsio.com 
2339ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
2349ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
2359ba682f0Skxie@chelsio.com 		for (i = 0; i < cdev->nports; i++) {
2369ba682f0Skxie@chelsio.com 			if (ndev == cdev->ports[i]) {
2370b3d8947Skxie@chelsio.com 				cdev->hbas[i]->vdev = vdev;
2389ba682f0Skxie@chelsio.com 				mutex_unlock(&cdev_mutex);
2399ba682f0Skxie@chelsio.com 				if (port)
2409ba682f0Skxie@chelsio.com 					*port = i;
2419ba682f0Skxie@chelsio.com 				return cdev;
2429ba682f0Skxie@chelsio.com 			}
2439ba682f0Skxie@chelsio.com 		}
2449ba682f0Skxie@chelsio.com 	}
2459ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
2469ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
2479ba682f0Skxie@chelsio.com 		"ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
2489ba682f0Skxie@chelsio.com 	return NULL;
2499ba682f0Skxie@chelsio.com }
250fc8d0590SAnish Bhatt EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev);
251fc8d0590SAnish Bhatt 
252576b5863SAnish Bhatt struct cxgbi_device *cxgbi_device_find_by_netdev_rcu(struct net_device *ndev,
253576b5863SAnish Bhatt 						     int *port)
254576b5863SAnish Bhatt {
255576b5863SAnish Bhatt 	struct net_device *vdev = NULL;
256576b5863SAnish Bhatt 	struct cxgbi_device *cdev;
257576b5863SAnish Bhatt 	int i;
258576b5863SAnish Bhatt 
259d0d7b10bSParav Pandit 	if (is_vlan_dev(ndev)) {
260576b5863SAnish Bhatt 		vdev = ndev;
261576b5863SAnish Bhatt 		ndev = vlan_dev_real_dev(ndev);
262576b5863SAnish Bhatt 		pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
263576b5863SAnish Bhatt 	}
264576b5863SAnish Bhatt 
265576b5863SAnish Bhatt 	rcu_read_lock();
266576b5863SAnish Bhatt 	list_for_each_entry_rcu(cdev, &cdev_rcu_list, rcu_node) {
267576b5863SAnish Bhatt 		for (i = 0; i < cdev->nports; i++) {
268576b5863SAnish Bhatt 			if (ndev == cdev->ports[i]) {
269576b5863SAnish Bhatt 				cdev->hbas[i]->vdev = vdev;
270576b5863SAnish Bhatt 				rcu_read_unlock();
271576b5863SAnish Bhatt 				if (port)
272576b5863SAnish Bhatt 					*port = i;
273576b5863SAnish Bhatt 				return cdev;
274576b5863SAnish Bhatt 			}
275576b5863SAnish Bhatt 		}
276576b5863SAnish Bhatt 	}
277576b5863SAnish Bhatt 	rcu_read_unlock();
278576b5863SAnish Bhatt 
279576b5863SAnish Bhatt 	log_debug(1 << CXGBI_DBG_DEV,
280576b5863SAnish Bhatt 		  "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
281576b5863SAnish Bhatt 	return NULL;
282576b5863SAnish Bhatt }
283576b5863SAnish Bhatt EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev_rcu);
284576b5863SAnish Bhatt 
285f42bb57cSAnish Bhatt #if IS_ENABLED(CONFIG_IPV6)
286fc8d0590SAnish Bhatt static struct cxgbi_device *cxgbi_device_find_by_mac(struct net_device *ndev,
287fc8d0590SAnish Bhatt 						     int *port)
288fc8d0590SAnish Bhatt {
289fc8d0590SAnish Bhatt 	struct net_device *vdev = NULL;
290fc8d0590SAnish Bhatt 	struct cxgbi_device *cdev, *tmp;
291fc8d0590SAnish Bhatt 	int i;
292fc8d0590SAnish Bhatt 
293d0d7b10bSParav Pandit 	if (is_vlan_dev(ndev)) {
294fc8d0590SAnish Bhatt 		vdev = ndev;
295fc8d0590SAnish Bhatt 		ndev = vlan_dev_real_dev(ndev);
296fc8d0590SAnish Bhatt 		pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
297fc8d0590SAnish Bhatt 	}
298fc8d0590SAnish Bhatt 
299fc8d0590SAnish Bhatt 	mutex_lock(&cdev_mutex);
300fc8d0590SAnish Bhatt 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
301fc8d0590SAnish Bhatt 		for (i = 0; i < cdev->nports; i++) {
302fc8d0590SAnish Bhatt 			if (!memcmp(ndev->dev_addr, cdev->ports[i]->dev_addr,
303fc8d0590SAnish Bhatt 				    MAX_ADDR_LEN)) {
304fc8d0590SAnish Bhatt 				cdev->hbas[i]->vdev = vdev;
305fc8d0590SAnish Bhatt 				mutex_unlock(&cdev_mutex);
306fc8d0590SAnish Bhatt 				if (port)
307fc8d0590SAnish Bhatt 					*port = i;
308fc8d0590SAnish Bhatt 				return cdev;
309fc8d0590SAnish Bhatt 			}
310fc8d0590SAnish Bhatt 		}
311fc8d0590SAnish Bhatt 	}
312fc8d0590SAnish Bhatt 	mutex_unlock(&cdev_mutex);
313fc8d0590SAnish Bhatt 	log_debug(1 << CXGBI_DBG_DEV,
314fc8d0590SAnish Bhatt 		  "ndev 0x%p, %s, NO match mac found.\n",
315fc8d0590SAnish Bhatt 		  ndev, ndev->name);
316fc8d0590SAnish Bhatt 	return NULL;
317fc8d0590SAnish Bhatt }
318f42bb57cSAnish Bhatt #endif
3199ba682f0Skxie@chelsio.com 
3209ba682f0Skxie@chelsio.com void cxgbi_hbas_remove(struct cxgbi_device *cdev)
3219ba682f0Skxie@chelsio.com {
3229ba682f0Skxie@chelsio.com 	int i;
3239ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
3249ba682f0Skxie@chelsio.com 
3259ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
3269ba682f0Skxie@chelsio.com 		"cdev 0x%p, p#%u.\n", cdev, cdev->nports);
3279ba682f0Skxie@chelsio.com 
3289ba682f0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
3299ba682f0Skxie@chelsio.com 		chba = cdev->hbas[i];
3309ba682f0Skxie@chelsio.com 		if (chba) {
3319ba682f0Skxie@chelsio.com 			cdev->hbas[i] = NULL;
3329ba682f0Skxie@chelsio.com 			iscsi_host_remove(chba->shost);
3339ba682f0Skxie@chelsio.com 			pci_dev_put(cdev->pdev);
3349ba682f0Skxie@chelsio.com 			iscsi_host_free(chba->shost);
3359ba682f0Skxie@chelsio.com 		}
3369ba682f0Skxie@chelsio.com 	}
3379ba682f0Skxie@chelsio.com }
3389ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
3399ba682f0Skxie@chelsio.com 
3401abf635dSHannes Reinecke int cxgbi_hbas_add(struct cxgbi_device *cdev, u64 max_lun,
3419ba682f0Skxie@chelsio.com 		unsigned int max_id, struct scsi_host_template *sht,
3429ba682f0Skxie@chelsio.com 		struct scsi_transport_template *stt)
3439ba682f0Skxie@chelsio.com {
3449ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
3459ba682f0Skxie@chelsio.com 	struct Scsi_Host *shost;
3469ba682f0Skxie@chelsio.com 	int i, err;
3479ba682f0Skxie@chelsio.com 
3489ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
3499ba682f0Skxie@chelsio.com 
3509ba682f0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
3519ba682f0Skxie@chelsio.com 		shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
3529ba682f0Skxie@chelsio.com 		if (!shost) {
3539ba682f0Skxie@chelsio.com 			pr_info("0x%p, p%d, %s, host alloc failed.\n",
3549ba682f0Skxie@chelsio.com 				cdev, i, cdev->ports[i]->name);
3559ba682f0Skxie@chelsio.com 			err = -ENOMEM;
3569ba682f0Skxie@chelsio.com 			goto err_out;
3579ba682f0Skxie@chelsio.com 		}
3589ba682f0Skxie@chelsio.com 
3599ba682f0Skxie@chelsio.com 		shost->transportt = stt;
3609ba682f0Skxie@chelsio.com 		shost->max_lun = max_lun;
3619ba682f0Skxie@chelsio.com 		shost->max_id = max_id;
3629ba682f0Skxie@chelsio.com 		shost->max_channel = 0;
3639ba682f0Skxie@chelsio.com 		shost->max_cmd_len = 16;
3649ba682f0Skxie@chelsio.com 
3659ba682f0Skxie@chelsio.com 		chba = iscsi_host_priv(shost);
3669ba682f0Skxie@chelsio.com 		chba->cdev = cdev;
3679ba682f0Skxie@chelsio.com 		chba->ndev = cdev->ports[i];
3689ba682f0Skxie@chelsio.com 		chba->shost = shost;
3699ba682f0Skxie@chelsio.com 
3709ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
3719ba682f0Skxie@chelsio.com 			"cdev 0x%p, p#%d %s: chba 0x%p.\n",
3729ba682f0Skxie@chelsio.com 			cdev, i, cdev->ports[i]->name, chba);
3739ba682f0Skxie@chelsio.com 
3749ba682f0Skxie@chelsio.com 		pci_dev_get(cdev->pdev);
3759ba682f0Skxie@chelsio.com 		err = iscsi_host_add(shost, &cdev->pdev->dev);
3769ba682f0Skxie@chelsio.com 		if (err) {
3779ba682f0Skxie@chelsio.com 			pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
3789ba682f0Skxie@chelsio.com 				cdev, i, cdev->ports[i]->name);
3799ba682f0Skxie@chelsio.com 			pci_dev_put(cdev->pdev);
3809ba682f0Skxie@chelsio.com 			scsi_host_put(shost);
3819ba682f0Skxie@chelsio.com 			goto  err_out;
3829ba682f0Skxie@chelsio.com 		}
3839ba682f0Skxie@chelsio.com 
3849ba682f0Skxie@chelsio.com 		cdev->hbas[i] = chba;
3859ba682f0Skxie@chelsio.com 	}
3869ba682f0Skxie@chelsio.com 
3879ba682f0Skxie@chelsio.com 	return 0;
3889ba682f0Skxie@chelsio.com 
3899ba682f0Skxie@chelsio.com err_out:
3909ba682f0Skxie@chelsio.com 	cxgbi_hbas_remove(cdev);
3919ba682f0Skxie@chelsio.com 	return err;
3929ba682f0Skxie@chelsio.com }
3939ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
3949ba682f0Skxie@chelsio.com 
3959ba682f0Skxie@chelsio.com /*
3969ba682f0Skxie@chelsio.com  * iSCSI offload
3979ba682f0Skxie@chelsio.com  *
3989ba682f0Skxie@chelsio.com  * - source port management
3999ba682f0Skxie@chelsio.com  *   To find a free source port in the port allocation map we use a very simple
4009ba682f0Skxie@chelsio.com  *   rotor scheme to look for the next free port.
4019ba682f0Skxie@chelsio.com  *
4029ba682f0Skxie@chelsio.com  *   If a source port has been specified make sure that it doesn't collide with
4039ba682f0Skxie@chelsio.com  *   our normal source port allocation map.  If it's outside the range of our
4049ba682f0Skxie@chelsio.com  *   allocation/deallocation scheme just let them use it.
4059ba682f0Skxie@chelsio.com  *
4069ba682f0Skxie@chelsio.com  *   If the source port is outside our allocation range, the caller is
4079ba682f0Skxie@chelsio.com  *   responsible for keeping track of their port usage.
4089ba682f0Skxie@chelsio.com  */
409dd9ad67eSAnish Bhatt 
410dd9ad67eSAnish Bhatt static struct cxgbi_sock *find_sock_on_port(struct cxgbi_device *cdev,
411dd9ad67eSAnish Bhatt 					    unsigned char port_id)
412dd9ad67eSAnish Bhatt {
413dd9ad67eSAnish Bhatt 	struct cxgbi_ports_map *pmap = &cdev->pmap;
414dd9ad67eSAnish Bhatt 	unsigned int i;
415dd9ad67eSAnish Bhatt 	unsigned int used;
416dd9ad67eSAnish Bhatt 
417dd9ad67eSAnish Bhatt 	if (!pmap->max_connect || !pmap->used)
418dd9ad67eSAnish Bhatt 		return NULL;
419dd9ad67eSAnish Bhatt 
420dd9ad67eSAnish Bhatt 	spin_lock_bh(&pmap->lock);
421dd9ad67eSAnish Bhatt 	used = pmap->used;
422dd9ad67eSAnish Bhatt 	for (i = 0; used && i < pmap->max_connect; i++) {
423dd9ad67eSAnish Bhatt 		struct cxgbi_sock *csk = pmap->port_csk[i];
424dd9ad67eSAnish Bhatt 
425dd9ad67eSAnish Bhatt 		if (csk) {
426dd9ad67eSAnish Bhatt 			if (csk->port_id == port_id) {
427dd9ad67eSAnish Bhatt 				spin_unlock_bh(&pmap->lock);
428dd9ad67eSAnish Bhatt 				return csk;
429dd9ad67eSAnish Bhatt 			}
430dd9ad67eSAnish Bhatt 			used--;
431dd9ad67eSAnish Bhatt 		}
432dd9ad67eSAnish Bhatt 	}
433dd9ad67eSAnish Bhatt 	spin_unlock_bh(&pmap->lock);
434dd9ad67eSAnish Bhatt 
435dd9ad67eSAnish Bhatt 	return NULL;
436dd9ad67eSAnish Bhatt }
437dd9ad67eSAnish Bhatt 
4389ba682f0Skxie@chelsio.com static int sock_get_port(struct cxgbi_sock *csk)
4399ba682f0Skxie@chelsio.com {
4409ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
4419ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
4429ba682f0Skxie@chelsio.com 	unsigned int start;
4439ba682f0Skxie@chelsio.com 	int idx;
444fc8d0590SAnish Bhatt 	__be16 *port;
4459ba682f0Skxie@chelsio.com 
4469ba682f0Skxie@chelsio.com 	if (!pmap->max_connect) {
4479ba682f0Skxie@chelsio.com 		pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
4489ba682f0Skxie@chelsio.com 			   cdev, csk->port_id, cdev->ports[csk->port_id]->name);
4499ba682f0Skxie@chelsio.com 		return -EADDRNOTAVAIL;
4509ba682f0Skxie@chelsio.com 	}
4519ba682f0Skxie@chelsio.com 
452fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
453fc8d0590SAnish Bhatt 		port = &csk->saddr.sin_port;
454fc8d0590SAnish Bhatt 	else /* ipv6 */
455fc8d0590SAnish Bhatt 		port = &csk->saddr6.sin6_port;
456fc8d0590SAnish Bhatt 
457fc8d0590SAnish Bhatt 	if (*port) {
4589ba682f0Skxie@chelsio.com 		pr_err("source port NON-ZERO %u.\n",
459fc8d0590SAnish Bhatt 			ntohs(*port));
4609ba682f0Skxie@chelsio.com 		return -EADDRINUSE;
4619ba682f0Skxie@chelsio.com 	}
4629ba682f0Skxie@chelsio.com 
4639ba682f0Skxie@chelsio.com 	spin_lock_bh(&pmap->lock);
4649ba682f0Skxie@chelsio.com 	if (pmap->used >= pmap->max_connect) {
4659ba682f0Skxie@chelsio.com 		spin_unlock_bh(&pmap->lock);
4669ba682f0Skxie@chelsio.com 		pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
4679ba682f0Skxie@chelsio.com 			cdev, csk->port_id, cdev->ports[csk->port_id]->name);
4689ba682f0Skxie@chelsio.com 		return -EADDRNOTAVAIL;
4699ba682f0Skxie@chelsio.com 	}
4709ba682f0Skxie@chelsio.com 
4719ba682f0Skxie@chelsio.com 	start = idx = pmap->next;
4729ba682f0Skxie@chelsio.com 	do {
4739ba682f0Skxie@chelsio.com 		if (++idx >= pmap->max_connect)
4749ba682f0Skxie@chelsio.com 			idx = 0;
4759ba682f0Skxie@chelsio.com 		if (!pmap->port_csk[idx]) {
4769ba682f0Skxie@chelsio.com 			pmap->used++;
477fc8d0590SAnish Bhatt 			*port = htons(pmap->sport_base + idx);
4789ba682f0Skxie@chelsio.com 			pmap->next = idx;
4799ba682f0Skxie@chelsio.com 			pmap->port_csk[idx] = csk;
4809ba682f0Skxie@chelsio.com 			spin_unlock_bh(&pmap->lock);
4819ba682f0Skxie@chelsio.com 			cxgbi_sock_get(csk);
4829ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_SOCK,
4839ba682f0Skxie@chelsio.com 				"cdev 0x%p, p#%u %s, p %u, %u.\n",
4849ba682f0Skxie@chelsio.com 				cdev, csk->port_id,
4859ba682f0Skxie@chelsio.com 				cdev->ports[csk->port_id]->name,
4869ba682f0Skxie@chelsio.com 				pmap->sport_base + idx, pmap->next);
4879ba682f0Skxie@chelsio.com 			return 0;
4889ba682f0Skxie@chelsio.com 		}
4899ba682f0Skxie@chelsio.com 	} while (idx != start);
4909ba682f0Skxie@chelsio.com 	spin_unlock_bh(&pmap->lock);
4919ba682f0Skxie@chelsio.com 
4929ba682f0Skxie@chelsio.com 	/* should not happen */
4939ba682f0Skxie@chelsio.com 	pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
4949ba682f0Skxie@chelsio.com 		cdev, csk->port_id, cdev->ports[csk->port_id]->name,
4959ba682f0Skxie@chelsio.com 		pmap->next);
4969ba682f0Skxie@chelsio.com 	return -EADDRNOTAVAIL;
4979ba682f0Skxie@chelsio.com }
4989ba682f0Skxie@chelsio.com 
4999ba682f0Skxie@chelsio.com static void sock_put_port(struct cxgbi_sock *csk)
5009ba682f0Skxie@chelsio.com {
5019ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
5029ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
503fc8d0590SAnish Bhatt 	__be16 *port;
5049ba682f0Skxie@chelsio.com 
505fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
506fc8d0590SAnish Bhatt 		port = &csk->saddr.sin_port;
507fc8d0590SAnish Bhatt 	else /* ipv6 */
508fc8d0590SAnish Bhatt 		port = &csk->saddr6.sin6_port;
5099ba682f0Skxie@chelsio.com 
510fc8d0590SAnish Bhatt 	if (*port) {
511fc8d0590SAnish Bhatt 		int idx = ntohs(*port) - pmap->sport_base;
512fc8d0590SAnish Bhatt 
513fc8d0590SAnish Bhatt 		*port = 0;
5149ba682f0Skxie@chelsio.com 		if (idx < 0 || idx >= pmap->max_connect) {
5159ba682f0Skxie@chelsio.com 			pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
5169ba682f0Skxie@chelsio.com 				cdev, csk->port_id,
5179ba682f0Skxie@chelsio.com 				cdev->ports[csk->port_id]->name,
518fc8d0590SAnish Bhatt 				ntohs(*port));
5199ba682f0Skxie@chelsio.com 			return;
5209ba682f0Skxie@chelsio.com 		}
5219ba682f0Skxie@chelsio.com 
5229ba682f0Skxie@chelsio.com 		spin_lock_bh(&pmap->lock);
5239ba682f0Skxie@chelsio.com 		pmap->port_csk[idx] = NULL;
5249ba682f0Skxie@chelsio.com 		pmap->used--;
5259ba682f0Skxie@chelsio.com 		spin_unlock_bh(&pmap->lock);
5269ba682f0Skxie@chelsio.com 
5279ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_SOCK,
5289ba682f0Skxie@chelsio.com 			"cdev 0x%p, p#%u %s, release %u.\n",
5299ba682f0Skxie@chelsio.com 			cdev, csk->port_id, cdev->ports[csk->port_id]->name,
5309ba682f0Skxie@chelsio.com 			pmap->sport_base + idx);
5319ba682f0Skxie@chelsio.com 
5329ba682f0Skxie@chelsio.com 		cxgbi_sock_put(csk);
5339ba682f0Skxie@chelsio.com 	}
5349ba682f0Skxie@chelsio.com }
5359ba682f0Skxie@chelsio.com 
5369ba682f0Skxie@chelsio.com /*
5379ba682f0Skxie@chelsio.com  * iscsi tcp connection
5389ba682f0Skxie@chelsio.com  */
5399ba682f0Skxie@chelsio.com void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
5409ba682f0Skxie@chelsio.com {
5419ba682f0Skxie@chelsio.com 	if (csk->cpl_close) {
5429ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_close);
5439ba682f0Skxie@chelsio.com 		csk->cpl_close = NULL;
5449ba682f0Skxie@chelsio.com 	}
5459ba682f0Skxie@chelsio.com 	if (csk->cpl_abort_req) {
5469ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_abort_req);
5479ba682f0Skxie@chelsio.com 		csk->cpl_abort_req = NULL;
5489ba682f0Skxie@chelsio.com 	}
5499ba682f0Skxie@chelsio.com 	if (csk->cpl_abort_rpl) {
5509ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_abort_rpl);
5519ba682f0Skxie@chelsio.com 		csk->cpl_abort_rpl = NULL;
5529ba682f0Skxie@chelsio.com 	}
5539ba682f0Skxie@chelsio.com }
5549ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
5559ba682f0Skxie@chelsio.com 
5569ba682f0Skxie@chelsio.com static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
5579ba682f0Skxie@chelsio.com {
5589ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
5599ba682f0Skxie@chelsio.com 
5609ba682f0Skxie@chelsio.com 	if (!csk) {
5619ba682f0Skxie@chelsio.com 		pr_info("alloc csk %zu failed.\n", sizeof(*csk));
5629ba682f0Skxie@chelsio.com 		return NULL;
5639ba682f0Skxie@chelsio.com 	}
5649ba682f0Skxie@chelsio.com 
5659ba682f0Skxie@chelsio.com 	if (cdev->csk_alloc_cpls(csk) < 0) {
5669ba682f0Skxie@chelsio.com 		pr_info("csk 0x%p, alloc cpls failed.\n", csk);
5679ba682f0Skxie@chelsio.com 		kfree(csk);
5689ba682f0Skxie@chelsio.com 		return NULL;
5699ba682f0Skxie@chelsio.com 	}
5709ba682f0Skxie@chelsio.com 
5719ba682f0Skxie@chelsio.com 	spin_lock_init(&csk->lock);
5729ba682f0Skxie@chelsio.com 	kref_init(&csk->refcnt);
5739ba682f0Skxie@chelsio.com 	skb_queue_head_init(&csk->receive_queue);
5749ba682f0Skxie@chelsio.com 	skb_queue_head_init(&csk->write_queue);
575cd07f958SKees Cook 	timer_setup(&csk->retry_timer, NULL, 0);
5769ba682f0Skxie@chelsio.com 	rwlock_init(&csk->callback_lock);
5779ba682f0Skxie@chelsio.com 	csk->cdev = cdev;
5789ba682f0Skxie@chelsio.com 	csk->flags = 0;
5799ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CLOSED);
5809ba682f0Skxie@chelsio.com 
5819ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
5829ba682f0Skxie@chelsio.com 
5839ba682f0Skxie@chelsio.com 	return csk;
5849ba682f0Skxie@chelsio.com }
5859ba682f0Skxie@chelsio.com 
586f4bfd99fSDavid S. Miller static struct rtable *find_route_ipv4(struct flowi4 *fl4,
587f4bfd99fSDavid S. Miller 				      __be32 saddr, __be32 daddr,
5884737c5a0SVarun Prakash 				      __be16 sport, __be16 dport, u8 tos,
5894737c5a0SVarun Prakash 				      int ifindex)
5909ba682f0Skxie@chelsio.com {
5919ba682f0Skxie@chelsio.com 	struct rtable *rt;
5929ba682f0Skxie@chelsio.com 
593f4bfd99fSDavid S. Miller 	rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
5944737c5a0SVarun Prakash 				   dport, sport, IPPROTO_TCP, tos, ifindex);
595b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
5969ba682f0Skxie@chelsio.com 		return NULL;
5979ba682f0Skxie@chelsio.com 
5989ba682f0Skxie@chelsio.com 	return rt;
5999ba682f0Skxie@chelsio.com }
6009ba682f0Skxie@chelsio.com 
6014737c5a0SVarun Prakash static struct cxgbi_sock *
6024737c5a0SVarun Prakash cxgbi_check_route(struct sockaddr *dst_addr, int ifindex)
6039ba682f0Skxie@chelsio.com {
6049ba682f0Skxie@chelsio.com 	struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
6059ba682f0Skxie@chelsio.com 	struct dst_entry *dst;
6069ba682f0Skxie@chelsio.com 	struct net_device *ndev;
6079ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev;
6089ba682f0Skxie@chelsio.com 	struct rtable *rt = NULL;
609a58b61e5SDavid Miller 	struct neighbour *n;
610f4bfd99fSDavid S. Miller 	struct flowi4 fl4;
6119ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = NULL;
6129ba682f0Skxie@chelsio.com 	unsigned int mtu = 0;
6139ba682f0Skxie@chelsio.com 	int port = 0xFFFF;
6149ba682f0Skxie@chelsio.com 	int err = 0;
6159ba682f0Skxie@chelsio.com 
6164737c5a0SVarun Prakash 	rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0,
6174737c5a0SVarun Prakash 			     daddr->sin_port, 0, ifindex);
6189ba682f0Skxie@chelsio.com 	if (!rt) {
6199ba682f0Skxie@chelsio.com 		pr_info("no route to ipv4 0x%x, port %u.\n",
620fc8d0590SAnish Bhatt 			be32_to_cpu(daddr->sin_addr.s_addr),
621fc8d0590SAnish Bhatt 			be16_to_cpu(daddr->sin_port));
6229ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
6239ba682f0Skxie@chelsio.com 		goto err_out;
6249ba682f0Skxie@chelsio.com 	}
6259ba682f0Skxie@chelsio.com 	dst = &rt->dst;
6260b399d46SDavid S. Miller 	n = dst_neigh_lookup(dst, &daddr->sin_addr.s_addr);
627a58b61e5SDavid Miller 	if (!n) {
628a58b61e5SDavid Miller 		err = -ENODEV;
629a58b61e5SDavid Miller 		goto rel_rt;
630a58b61e5SDavid Miller 	}
631a58b61e5SDavid Miller 	ndev = n->dev;
6329ba682f0Skxie@chelsio.com 
6339ba682f0Skxie@chelsio.com 	if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
6349ba682f0Skxie@chelsio.com 		pr_info("multi-cast route %pI4, port %u, dev %s.\n",
6359ba682f0Skxie@chelsio.com 			&daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
6369ba682f0Skxie@chelsio.com 			ndev->name);
6379ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
6380b399d46SDavid S. Miller 		goto rel_neigh;
6399ba682f0Skxie@chelsio.com 	}
6409ba682f0Skxie@chelsio.com 
6419ba682f0Skxie@chelsio.com 	if (ndev->flags & IFF_LOOPBACK) {
6429ba682f0Skxie@chelsio.com 		ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
6439ba682f0Skxie@chelsio.com 		mtu = ndev->mtu;
6449ba682f0Skxie@chelsio.com 		pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
645a58b61e5SDavid Miller 			n->dev->name, ndev->name, mtu);
6469ba682f0Skxie@chelsio.com 	}
6479ba682f0Skxie@chelsio.com 
648165ae50eSVarun Prakash 	if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
649165ae50eSVarun Prakash 		pr_info("%s interface not up.\n", ndev->name);
650165ae50eSVarun Prakash 		err = -ENETDOWN;
651165ae50eSVarun Prakash 		goto rel_neigh;
652165ae50eSVarun Prakash 	}
653165ae50eSVarun Prakash 
6549ba682f0Skxie@chelsio.com 	cdev = cxgbi_device_find_by_netdev(ndev, &port);
6559ba682f0Skxie@chelsio.com 	if (!cdev) {
6569ba682f0Skxie@chelsio.com 		pr_info("dst %pI4, %s, NOT cxgbi device.\n",
6579ba682f0Skxie@chelsio.com 			&daddr->sin_addr.s_addr, ndev->name);
6589ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
6590b399d46SDavid S. Miller 		goto rel_neigh;
6609ba682f0Skxie@chelsio.com 	}
6619ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK,
6629ba682f0Skxie@chelsio.com 		"route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
6639ba682f0Skxie@chelsio.com 		&daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
6649ba682f0Skxie@chelsio.com 			   port, ndev->name, cdev);
6659ba682f0Skxie@chelsio.com 
6669ba682f0Skxie@chelsio.com 	csk = cxgbi_sock_create(cdev);
6679ba682f0Skxie@chelsio.com 	if (!csk) {
6689ba682f0Skxie@chelsio.com 		err = -ENOMEM;
6690b399d46SDavid S. Miller 		goto rel_neigh;
6709ba682f0Skxie@chelsio.com 	}
6719ba682f0Skxie@chelsio.com 	csk->cdev = cdev;
6729ba682f0Skxie@chelsio.com 	csk->port_id = port;
6739ba682f0Skxie@chelsio.com 	csk->mtu = mtu;
6749ba682f0Skxie@chelsio.com 	csk->dst = dst;
675fc8d0590SAnish Bhatt 
676fc8d0590SAnish Bhatt 	csk->csk_family = AF_INET;
6779ba682f0Skxie@chelsio.com 	csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
6789ba682f0Skxie@chelsio.com 	csk->daddr.sin_port = daddr->sin_port;
679c71b9b66SMike Christie 	csk->daddr.sin_family = daddr->sin_family;
680fc8d0590SAnish Bhatt 	csk->saddr.sin_family = daddr->sin_family;
681f4bfd99fSDavid S. Miller 	csk->saddr.sin_addr.s_addr = fl4.saddr;
6820b399d46SDavid S. Miller 	neigh_release(n);
6839ba682f0Skxie@chelsio.com 
6849ba682f0Skxie@chelsio.com 	return csk;
6859ba682f0Skxie@chelsio.com 
6860b399d46SDavid S. Miller rel_neigh:
6870b399d46SDavid S. Miller 	neigh_release(n);
6880b399d46SDavid S. Miller 
6899ba682f0Skxie@chelsio.com rel_rt:
6909ba682f0Skxie@chelsio.com 	ip_rt_put(rt);
6919ba682f0Skxie@chelsio.com err_out:
6929ba682f0Skxie@chelsio.com 	return ERR_PTR(err);
6939ba682f0Skxie@chelsio.com }
6949ba682f0Skxie@chelsio.com 
695e81fbf6cSAnish Bhatt #if IS_ENABLED(CONFIG_IPV6)
696fc8d0590SAnish Bhatt static struct rt6_info *find_route_ipv6(const struct in6_addr *saddr,
6974737c5a0SVarun Prakash 					const struct in6_addr *daddr,
6984737c5a0SVarun Prakash 					int ifindex)
699fc8d0590SAnish Bhatt {
700fc8d0590SAnish Bhatt 	struct flowi6 fl;
701fc8d0590SAnish Bhatt 
7023d6d30d6SJiri Benc 	memset(&fl, 0, sizeof(fl));
7034737c5a0SVarun Prakash 	fl.flowi6_oif = ifindex;
704fc8d0590SAnish Bhatt 	if (saddr)
705fc8d0590SAnish Bhatt 		memcpy(&fl.saddr, saddr, sizeof(struct in6_addr));
706fc8d0590SAnish Bhatt 	if (daddr)
707fc8d0590SAnish Bhatt 		memcpy(&fl.daddr, daddr, sizeof(struct in6_addr));
708fc8d0590SAnish Bhatt 	return (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
709fc8d0590SAnish Bhatt }
710fc8d0590SAnish Bhatt 
7114737c5a0SVarun Prakash static struct cxgbi_sock *
7124737c5a0SVarun Prakash cxgbi_check_route6(struct sockaddr *dst_addr, int ifindex)
713fc8d0590SAnish Bhatt {
714fc8d0590SAnish Bhatt 	struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)dst_addr;
715fc8d0590SAnish Bhatt 	struct dst_entry *dst;
716fc8d0590SAnish Bhatt 	struct net_device *ndev;
717fc8d0590SAnish Bhatt 	struct cxgbi_device *cdev;
718fc8d0590SAnish Bhatt 	struct rt6_info *rt = NULL;
719fc8d0590SAnish Bhatt 	struct neighbour *n;
720fc8d0590SAnish Bhatt 	struct in6_addr pref_saddr;
721fc8d0590SAnish Bhatt 	struct cxgbi_sock *csk = NULL;
722fc8d0590SAnish Bhatt 	unsigned int mtu = 0;
723fc8d0590SAnish Bhatt 	int port = 0xFFFF;
724fc8d0590SAnish Bhatt 	int err = 0;
725fc8d0590SAnish Bhatt 
7264737c5a0SVarun Prakash 	rt = find_route_ipv6(NULL, &daddr6->sin6_addr, ifindex);
727fc8d0590SAnish Bhatt 
728fc8d0590SAnish Bhatt 	if (!rt) {
729fc8d0590SAnish Bhatt 		pr_info("no route to ipv6 %pI6 port %u\n",
730fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
731fc8d0590SAnish Bhatt 			be16_to_cpu(daddr6->sin6_port));
732fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
733fc8d0590SAnish Bhatt 		goto err_out;
734fc8d0590SAnish Bhatt 	}
735fc8d0590SAnish Bhatt 
736fc8d0590SAnish Bhatt 	dst = &rt->dst;
737fc8d0590SAnish Bhatt 
738fc8d0590SAnish Bhatt 	n = dst_neigh_lookup(dst, &daddr6->sin6_addr);
739fc8d0590SAnish Bhatt 
740fc8d0590SAnish Bhatt 	if (!n) {
741fc8d0590SAnish Bhatt 		pr_info("%pI6, port %u, dst no neighbour.\n",
742fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
743fc8d0590SAnish Bhatt 			be16_to_cpu(daddr6->sin6_port));
744fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
745fc8d0590SAnish Bhatt 		goto rel_rt;
746fc8d0590SAnish Bhatt 	}
747fc8d0590SAnish Bhatt 	ndev = n->dev;
748fc8d0590SAnish Bhatt 
749165ae50eSVarun Prakash 	if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
750165ae50eSVarun Prakash 		pr_info("%s interface not up.\n", ndev->name);
751165ae50eSVarun Prakash 		err = -ENETDOWN;
752165ae50eSVarun Prakash 		goto rel_rt;
753165ae50eSVarun Prakash 	}
754165ae50eSVarun Prakash 
755fd0273d7SMartin KaFai Lau 	if (ipv6_addr_is_multicast(&daddr6->sin6_addr)) {
756fc8d0590SAnish Bhatt 		pr_info("multi-cast route %pI6 port %u, dev %s.\n",
757fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
758fc8d0590SAnish Bhatt 			ntohs(daddr6->sin6_port), ndev->name);
759fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
760fc8d0590SAnish Bhatt 		goto rel_rt;
761fc8d0590SAnish Bhatt 	}
762fc8d0590SAnish Bhatt 
763fc8d0590SAnish Bhatt 	cdev = cxgbi_device_find_by_netdev(ndev, &port);
764fc8d0590SAnish Bhatt 	if (!cdev)
765fc8d0590SAnish Bhatt 		cdev = cxgbi_device_find_by_mac(ndev, &port);
766fc8d0590SAnish Bhatt 	if (!cdev) {
767fc8d0590SAnish Bhatt 		pr_info("dst %pI6 %s, NOT cxgbi device.\n",
768fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr, ndev->name);
769fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
770fc8d0590SAnish Bhatt 		goto rel_rt;
771fc8d0590SAnish Bhatt 	}
772fc8d0590SAnish Bhatt 	log_debug(1 << CXGBI_DBG_SOCK,
773fc8d0590SAnish Bhatt 		  "route to %pI6 :%u, ndev p#%d,%s, cdev 0x%p.\n",
774fc8d0590SAnish Bhatt 		  daddr6->sin6_addr.s6_addr, ntohs(daddr6->sin6_port), port,
775fc8d0590SAnish Bhatt 		  ndev->name, cdev);
776fc8d0590SAnish Bhatt 
777fc8d0590SAnish Bhatt 	csk = cxgbi_sock_create(cdev);
778fc8d0590SAnish Bhatt 	if (!csk) {
779fc8d0590SAnish Bhatt 		err = -ENOMEM;
780fc8d0590SAnish Bhatt 		goto rel_rt;
781fc8d0590SAnish Bhatt 	}
782fc8d0590SAnish Bhatt 	csk->cdev = cdev;
783fc8d0590SAnish Bhatt 	csk->port_id = port;
784fc8d0590SAnish Bhatt 	csk->mtu = mtu;
785fc8d0590SAnish Bhatt 	csk->dst = dst;
786fc8d0590SAnish Bhatt 
787fc8d0590SAnish Bhatt 	if (ipv6_addr_any(&rt->rt6i_prefsrc.addr)) {
788fc8d0590SAnish Bhatt 		struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
789fc8d0590SAnish Bhatt 
790fc8d0590SAnish Bhatt 		err = ipv6_dev_get_saddr(&init_net, idev ? idev->dev : NULL,
791fc8d0590SAnish Bhatt 					 &daddr6->sin6_addr, 0, &pref_saddr);
792fc8d0590SAnish Bhatt 		if (err) {
793fc8d0590SAnish Bhatt 			pr_info("failed to get source address to reach %pI6\n",
794fc8d0590SAnish Bhatt 				&daddr6->sin6_addr);
795fc8d0590SAnish Bhatt 			goto rel_rt;
796fc8d0590SAnish Bhatt 		}
797fc8d0590SAnish Bhatt 	} else {
798fc8d0590SAnish Bhatt 		pref_saddr = rt->rt6i_prefsrc.addr;
799fc8d0590SAnish Bhatt 	}
800fc8d0590SAnish Bhatt 
801fc8d0590SAnish Bhatt 	csk->csk_family = AF_INET6;
802fc8d0590SAnish Bhatt 	csk->daddr6.sin6_addr = daddr6->sin6_addr;
803fc8d0590SAnish Bhatt 	csk->daddr6.sin6_port = daddr6->sin6_port;
804fc8d0590SAnish Bhatt 	csk->daddr6.sin6_family = daddr6->sin6_family;
805dd9ad67eSAnish Bhatt 	csk->saddr6.sin6_family = daddr6->sin6_family;
806fc8d0590SAnish Bhatt 	csk->saddr6.sin6_addr = pref_saddr;
807fc8d0590SAnish Bhatt 
808fc8d0590SAnish Bhatt 	neigh_release(n);
809fc8d0590SAnish Bhatt 	return csk;
810fc8d0590SAnish Bhatt 
811fc8d0590SAnish Bhatt rel_rt:
812fc8d0590SAnish Bhatt 	if (n)
813fc8d0590SAnish Bhatt 		neigh_release(n);
814fc8d0590SAnish Bhatt 
815fc8d0590SAnish Bhatt 	ip6_rt_put(rt);
816fc8d0590SAnish Bhatt 	if (csk)
817fc8d0590SAnish Bhatt 		cxgbi_sock_closed(csk);
818fc8d0590SAnish Bhatt err_out:
819fc8d0590SAnish Bhatt 	return ERR_PTR(err);
820fc8d0590SAnish Bhatt }
821e81fbf6cSAnish Bhatt #endif /* IS_ENABLED(CONFIG_IPV6) */
822fc8d0590SAnish Bhatt 
8239ba682f0Skxie@chelsio.com void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
8249ba682f0Skxie@chelsio.com 			unsigned int opt)
8259ba682f0Skxie@chelsio.com {
8269ba682f0Skxie@chelsio.com 	csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
8279ba682f0Skxie@chelsio.com 	dst_confirm(csk->dst);
8289ba682f0Skxie@chelsio.com 	smp_mb();
8299ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
8309ba682f0Skxie@chelsio.com }
8319ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_established);
8329ba682f0Skxie@chelsio.com 
8339ba682f0Skxie@chelsio.com static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
8349ba682f0Skxie@chelsio.com {
8359ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK,
8369ba682f0Skxie@chelsio.com 		"csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
8379ba682f0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->user_data);
8389ba682f0Skxie@chelsio.com 
8399ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED) {
840e3d2ad8cSkxie@chelsio.com 		read_lock_bh(&csk->callback_lock);
8419ba682f0Skxie@chelsio.com 		if (csk->user_data)
8429ba682f0Skxie@chelsio.com 			iscsi_conn_failure(csk->user_data,
843ee7255adSAnish Bhatt 					ISCSI_ERR_TCP_CONN_CLOSE);
844e3d2ad8cSkxie@chelsio.com 		read_unlock_bh(&csk->callback_lock);
8459ba682f0Skxie@chelsio.com 	}
8469ba682f0Skxie@chelsio.com }
8479ba682f0Skxie@chelsio.com 
8489ba682f0Skxie@chelsio.com void cxgbi_sock_closed(struct cxgbi_sock *csk)
8499ba682f0Skxie@chelsio.com {
8509ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8519ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8529ba682f0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
8539ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
8549ba682f0Skxie@chelsio.com 		return;
8559ba682f0Skxie@chelsio.com 	if (csk->saddr.sin_port)
8569ba682f0Skxie@chelsio.com 		sock_put_port(csk);
8579ba682f0Skxie@chelsio.com 	if (csk->dst)
8589ba682f0Skxie@chelsio.com 		dst_release(csk->dst);
8599ba682f0Skxie@chelsio.com 	csk->cdev->csk_release_offload_resources(csk);
8609ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CLOSED);
8619ba682f0Skxie@chelsio.com 	cxgbi_inform_iscsi_conn_closing(csk);
8629ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
8639ba682f0Skxie@chelsio.com }
8649ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
8659ba682f0Skxie@chelsio.com 
8669ba682f0Skxie@chelsio.com static void need_active_close(struct cxgbi_sock *csk)
8679ba682f0Skxie@chelsio.com {
8689ba682f0Skxie@chelsio.com 	int data_lost;
8699ba682f0Skxie@chelsio.com 	int close_req = 0;
8709ba682f0Skxie@chelsio.com 
8719ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8729ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8739ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
874e0f8e8cfSVarun Prakash 	if (csk->dst)
8759ba682f0Skxie@chelsio.com 		dst_confirm(csk->dst);
8769ba682f0Skxie@chelsio.com 	data_lost = skb_queue_len(&csk->receive_queue);
8779ba682f0Skxie@chelsio.com 	__skb_queue_purge(&csk->receive_queue);
8789ba682f0Skxie@chelsio.com 
8799ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN)
8809ba682f0Skxie@chelsio.com 		cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
8819ba682f0Skxie@chelsio.com 	else if (csk->state == CTP_ESTABLISHED) {
8829ba682f0Skxie@chelsio.com 		close_req = 1;
8839ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
8849ba682f0Skxie@chelsio.com 	} else if (csk->state == CTP_PASSIVE_CLOSE) {
8859ba682f0Skxie@chelsio.com 		close_req = 1;
8869ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
8879ba682f0Skxie@chelsio.com 	}
8889ba682f0Skxie@chelsio.com 
8899ba682f0Skxie@chelsio.com 	if (close_req) {
890e0f8e8cfSVarun Prakash 		if (!cxgbi_sock_flag(csk, CTPF_LOGOUT_RSP_RCVD) ||
891e0f8e8cfSVarun Prakash 		    data_lost)
8929ba682f0Skxie@chelsio.com 			csk->cdev->csk_send_abort_req(csk);
8939ba682f0Skxie@chelsio.com 		else
8949ba682f0Skxie@chelsio.com 			csk->cdev->csk_send_close_req(csk);
8959ba682f0Skxie@chelsio.com 	}
8969ba682f0Skxie@chelsio.com 
8979ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
8989ba682f0Skxie@chelsio.com }
8999ba682f0Skxie@chelsio.com 
9009ba682f0Skxie@chelsio.com void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
9019ba682f0Skxie@chelsio.com {
9029ba682f0Skxie@chelsio.com 	pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
9039ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags,
9049ba682f0Skxie@chelsio.com 			&csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
9059ba682f0Skxie@chelsio.com 			&csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
9069ba682f0Skxie@chelsio.com 			errno);
9079ba682f0Skxie@chelsio.com 
9089ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CONNECTING);
9099ba682f0Skxie@chelsio.com 	csk->err = errno;
9109ba682f0Skxie@chelsio.com 	cxgbi_sock_closed(csk);
9119ba682f0Skxie@chelsio.com }
9129ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
9139ba682f0Skxie@chelsio.com 
9149ba682f0Skxie@chelsio.com void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
9159ba682f0Skxie@chelsio.com {
9169ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
9171fe1fdb0SVarun Prakash 	struct module *owner = csk->cdev->owner;
9189ba682f0Skxie@chelsio.com 
9199ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
9209ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
9219ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9229ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9239ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN)
9249ba682f0Skxie@chelsio.com 		cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
9259ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9269ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9279ba682f0Skxie@chelsio.com 	__kfree_skb(skb);
9281fe1fdb0SVarun Prakash 
9291fe1fdb0SVarun Prakash 	module_put(owner);
9309ba682f0Skxie@chelsio.com }
9319ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
9329ba682f0Skxie@chelsio.com 
9339ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
9349ba682f0Skxie@chelsio.com {
9359ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9369ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9377b07bf24SAnish Bhatt 
9389ba682f0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
9397b07bf24SAnish Bhatt 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
9409ba682f0Skxie@chelsio.com 		cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
9419ba682f0Skxie@chelsio.com 		if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
9429ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
9439ba682f0Skxie@chelsio.com 			       csk, csk->state, csk->flags, csk->tid);
9449ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
9459ba682f0Skxie@chelsio.com 	}
9467b07bf24SAnish Bhatt 
9479ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9489ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9499ba682f0Skxie@chelsio.com }
9509ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
9519ba682f0Skxie@chelsio.com 
9529ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
9539ba682f0Skxie@chelsio.com {
9549ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
9559ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
9569ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9579ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9589ba682f0Skxie@chelsio.com 
9599ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
9609ba682f0Skxie@chelsio.com 		goto done;
9619ba682f0Skxie@chelsio.com 
9629ba682f0Skxie@chelsio.com 	switch (csk->state) {
9639ba682f0Skxie@chelsio.com 	case CTP_ESTABLISHED:
9649ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
9659ba682f0Skxie@chelsio.com 		break;
9669ba682f0Skxie@chelsio.com 	case CTP_ACTIVE_CLOSE:
9679ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
9689ba682f0Skxie@chelsio.com 		break;
9699ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_1:
9709ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
9719ba682f0Skxie@chelsio.com 		break;
9729ba682f0Skxie@chelsio.com 	case CTP_ABORTING:
9739ba682f0Skxie@chelsio.com 		break;
9749ba682f0Skxie@chelsio.com 	default:
9759ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
9769ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
9779ba682f0Skxie@chelsio.com 	}
9789ba682f0Skxie@chelsio.com 	cxgbi_inform_iscsi_conn_closing(csk);
9799ba682f0Skxie@chelsio.com done:
9809ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9819ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9829ba682f0Skxie@chelsio.com }
9839ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
9849ba682f0Skxie@chelsio.com 
9859ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
9869ba682f0Skxie@chelsio.com {
9879ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
9889ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
9899ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9909ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9919ba682f0Skxie@chelsio.com 
9929ba682f0Skxie@chelsio.com 	csk->snd_una = snd_nxt - 1;
9939ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
9949ba682f0Skxie@chelsio.com 		goto done;
9959ba682f0Skxie@chelsio.com 
9969ba682f0Skxie@chelsio.com 	switch (csk->state) {
9979ba682f0Skxie@chelsio.com 	case CTP_ACTIVE_CLOSE:
9989ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
9999ba682f0Skxie@chelsio.com 		break;
10009ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_1:
10019ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_2:
10029ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
10039ba682f0Skxie@chelsio.com 		break;
10049ba682f0Skxie@chelsio.com 	case CTP_ABORTING:
10059ba682f0Skxie@chelsio.com 		break;
10069ba682f0Skxie@chelsio.com 	default:
10079ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
10089ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
10099ba682f0Skxie@chelsio.com 	}
10109ba682f0Skxie@chelsio.com done:
10119ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10129ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
10139ba682f0Skxie@chelsio.com }
10149ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
10159ba682f0Skxie@chelsio.com 
10169ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
10179ba682f0Skxie@chelsio.com 			   unsigned int snd_una, int seq_chk)
10189ba682f0Skxie@chelsio.com {
10199ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
10209ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
10219ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, credits,
10229ba682f0Skxie@chelsio.com 			csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
10239ba682f0Skxie@chelsio.com 
10249ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
10259ba682f0Skxie@chelsio.com 
10269ba682f0Skxie@chelsio.com 	csk->wr_cred += credits;
10279ba682f0Skxie@chelsio.com 	if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
10289ba682f0Skxie@chelsio.com 		csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
10299ba682f0Skxie@chelsio.com 
10309ba682f0Skxie@chelsio.com 	while (credits) {
10319ba682f0Skxie@chelsio.com 		struct sk_buff *p = cxgbi_sock_peek_wr(csk);
10329ba682f0Skxie@chelsio.com 
10339ba682f0Skxie@chelsio.com 		if (unlikely(!p)) {
10349ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
10359ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid, credits,
10369ba682f0Skxie@chelsio.com 				csk->wr_cred, csk->wr_una_cred);
10379ba682f0Skxie@chelsio.com 			break;
10389ba682f0Skxie@chelsio.com 		}
10399ba682f0Skxie@chelsio.com 
10409ba682f0Skxie@chelsio.com 		if (unlikely(credits < p->csum)) {
10419ba682f0Skxie@chelsio.com 			pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
10429ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid,
10439ba682f0Skxie@chelsio.com 				credits, csk->wr_cred, csk->wr_una_cred,
10449ba682f0Skxie@chelsio.com 				p->csum);
10459ba682f0Skxie@chelsio.com 			p->csum -= credits;
10469ba682f0Skxie@chelsio.com 			break;
10479ba682f0Skxie@chelsio.com 		} else {
10489ba682f0Skxie@chelsio.com 			cxgbi_sock_dequeue_wr(csk);
10499ba682f0Skxie@chelsio.com 			credits -= p->csum;
10509ba682f0Skxie@chelsio.com 			kfree_skb(p);
10519ba682f0Skxie@chelsio.com 		}
10529ba682f0Skxie@chelsio.com 	}
10539ba682f0Skxie@chelsio.com 
10549ba682f0Skxie@chelsio.com 	cxgbi_sock_check_wr_invariants(csk);
10559ba682f0Skxie@chelsio.com 
10569ba682f0Skxie@chelsio.com 	if (seq_chk) {
10579ba682f0Skxie@chelsio.com 		if (unlikely(before(snd_una, csk->snd_una))) {
10589ba682f0Skxie@chelsio.com 			pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
10599ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid, snd_una,
10609ba682f0Skxie@chelsio.com 				csk->snd_una);
10619ba682f0Skxie@chelsio.com 			goto done;
10629ba682f0Skxie@chelsio.com 		}
10639ba682f0Skxie@chelsio.com 
10649ba682f0Skxie@chelsio.com 		if (csk->snd_una != snd_una) {
10659ba682f0Skxie@chelsio.com 			csk->snd_una = snd_una;
10669ba682f0Skxie@chelsio.com 			dst_confirm(csk->dst);
10679ba682f0Skxie@chelsio.com 		}
10689ba682f0Skxie@chelsio.com 	}
10699ba682f0Skxie@chelsio.com 
10709ba682f0Skxie@chelsio.com 	if (skb_queue_len(&csk->write_queue)) {
10719ba682f0Skxie@chelsio.com 		if (csk->cdev->csk_push_tx_frames(csk, 0))
10729ba682f0Skxie@chelsio.com 			cxgbi_conn_tx_open(csk);
10739ba682f0Skxie@chelsio.com 	} else
10749ba682f0Skxie@chelsio.com 		cxgbi_conn_tx_open(csk);
10759ba682f0Skxie@chelsio.com done:
10769ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10779ba682f0Skxie@chelsio.com }
10789ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
10799ba682f0Skxie@chelsio.com 
10809ba682f0Skxie@chelsio.com static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
10819ba682f0Skxie@chelsio.com 					     unsigned short mtu)
10829ba682f0Skxie@chelsio.com {
10839ba682f0Skxie@chelsio.com 	int i = 0;
10849ba682f0Skxie@chelsio.com 
10859ba682f0Skxie@chelsio.com 	while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
10869ba682f0Skxie@chelsio.com 		++i;
10879ba682f0Skxie@chelsio.com 
10889ba682f0Skxie@chelsio.com 	return i;
10899ba682f0Skxie@chelsio.com }
10909ba682f0Skxie@chelsio.com 
10919ba682f0Skxie@chelsio.com unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
10929ba682f0Skxie@chelsio.com {
10939ba682f0Skxie@chelsio.com 	unsigned int idx;
10949ba682f0Skxie@chelsio.com 	struct dst_entry *dst = csk->dst;
10959ba682f0Skxie@chelsio.com 
10960dbaee3bSDavid S. Miller 	csk->advmss = dst_metric_advmss(dst);
10979ba682f0Skxie@chelsio.com 
10989ba682f0Skxie@chelsio.com 	if (csk->advmss > pmtu - 40)
10999ba682f0Skxie@chelsio.com 		csk->advmss = pmtu - 40;
11009ba682f0Skxie@chelsio.com 	if (csk->advmss < csk->cdev->mtus[0] - 40)
11019ba682f0Skxie@chelsio.com 		csk->advmss = csk->cdev->mtus[0] - 40;
11029ba682f0Skxie@chelsio.com 	idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
11039ba682f0Skxie@chelsio.com 
11049ba682f0Skxie@chelsio.com 	return idx;
11059ba682f0Skxie@chelsio.com }
11069ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
11079ba682f0Skxie@chelsio.com 
11089ba682f0Skxie@chelsio.com void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
11099ba682f0Skxie@chelsio.com {
11109ba682f0Skxie@chelsio.com 	cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
11119ba682f0Skxie@chelsio.com 	__skb_queue_tail(&csk->write_queue, skb);
11129ba682f0Skxie@chelsio.com }
11139ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
11149ba682f0Skxie@chelsio.com 
11159ba682f0Skxie@chelsio.com void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
11169ba682f0Skxie@chelsio.com {
11179ba682f0Skxie@chelsio.com 	struct sk_buff *skb;
11189ba682f0Skxie@chelsio.com 
11199ba682f0Skxie@chelsio.com 	while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
11209ba682f0Skxie@chelsio.com 		kfree_skb(skb);
11219ba682f0Skxie@chelsio.com }
11229ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
11239ba682f0Skxie@chelsio.com 
11249ba682f0Skxie@chelsio.com void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
11259ba682f0Skxie@chelsio.com {
11269ba682f0Skxie@chelsio.com 	int pending = cxgbi_sock_count_pending_wrs(csk);
11279ba682f0Skxie@chelsio.com 
11289ba682f0Skxie@chelsio.com 	if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
11299ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
11309ba682f0Skxie@chelsio.com 			csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
11319ba682f0Skxie@chelsio.com }
11329ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
11339ba682f0Skxie@chelsio.com 
11349ba682f0Skxie@chelsio.com static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
11359ba682f0Skxie@chelsio.com {
11369ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
11379ba682f0Skxie@chelsio.com 	struct sk_buff *next;
11389ba682f0Skxie@chelsio.com 	int err, copied = 0;
11399ba682f0Skxie@chelsio.com 
11409ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
11419ba682f0Skxie@chelsio.com 
11429ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED) {
11439ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
11449ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
11459ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
11469ba682f0Skxie@chelsio.com 		err = -EAGAIN;
11479ba682f0Skxie@chelsio.com 		goto out_err;
11489ba682f0Skxie@chelsio.com 	}
11499ba682f0Skxie@chelsio.com 
11509ba682f0Skxie@chelsio.com 	if (csk->err) {
11519ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
11529ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
11539ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, csk->err);
11549ba682f0Skxie@chelsio.com 		err = -EPIPE;
11559ba682f0Skxie@chelsio.com 		goto out_err;
11569ba682f0Skxie@chelsio.com 	}
11579ba682f0Skxie@chelsio.com 
115881daf10cSKaren Xie 	if (csk->write_seq - csk->snd_una >= csk->snd_win) {
11599ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
11609ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
11619ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, csk->write_seq,
116281daf10cSKaren Xie 			csk->snd_una, csk->snd_win);
11639ba682f0Skxie@chelsio.com 		err = -ENOBUFS;
11649ba682f0Skxie@chelsio.com 		goto out_err;
11659ba682f0Skxie@chelsio.com 	}
11669ba682f0Skxie@chelsio.com 
11679ba682f0Skxie@chelsio.com 	while (skb) {
11689ba682f0Skxie@chelsio.com 		int frags = skb_shinfo(skb)->nr_frags +
11699ba682f0Skxie@chelsio.com 				(skb->len != skb->data_len);
11709ba682f0Skxie@chelsio.com 
11719ba682f0Skxie@chelsio.com 		if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
11729ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p, skb head %u < %u.\n",
11739ba682f0Skxie@chelsio.com 				csk, skb_headroom(skb), cdev->skb_tx_rsvd);
11749ba682f0Skxie@chelsio.com 			err = -EINVAL;
11759ba682f0Skxie@chelsio.com 			goto out_err;
11769ba682f0Skxie@chelsio.com 		}
11779ba682f0Skxie@chelsio.com 
11789ba682f0Skxie@chelsio.com 		if (frags >= SKB_WR_LIST_SIZE) {
11799ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
11809ba682f0Skxie@chelsio.com 				csk, skb_shinfo(skb)->nr_frags, skb->len,
11819ba682f0Skxie@chelsio.com 				skb->data_len, (uint)(SKB_WR_LIST_SIZE));
11829ba682f0Skxie@chelsio.com 			err = -EINVAL;
11839ba682f0Skxie@chelsio.com 			goto out_err;
11849ba682f0Skxie@chelsio.com 		}
11859ba682f0Skxie@chelsio.com 
11869ba682f0Skxie@chelsio.com 		next = skb->next;
11879ba682f0Skxie@chelsio.com 		skb->next = NULL;
11889ba682f0Skxie@chelsio.com 		cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
11899ba682f0Skxie@chelsio.com 		cxgbi_sock_skb_entail(csk, skb);
11909ba682f0Skxie@chelsio.com 		copied += skb->len;
11919ba682f0Skxie@chelsio.com 		csk->write_seq += skb->len +
11929ba682f0Skxie@chelsio.com 				cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
11939ba682f0Skxie@chelsio.com 		skb = next;
11949ba682f0Skxie@chelsio.com 	}
1195e0f8e8cfSVarun Prakash 
11969ba682f0Skxie@chelsio.com 	if (likely(skb_queue_len(&csk->write_queue)))
11979ba682f0Skxie@chelsio.com 		cdev->csk_push_tx_frames(csk, 1);
1198e0f8e8cfSVarun Prakash done:
11999ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
12009ba682f0Skxie@chelsio.com 	return copied;
12019ba682f0Skxie@chelsio.com 
12029ba682f0Skxie@chelsio.com out_err:
12039ba682f0Skxie@chelsio.com 	if (copied == 0 && err == -EPIPE)
12049ba682f0Skxie@chelsio.com 		copied = csk->err ? csk->err : -EPIPE;
12059ba682f0Skxie@chelsio.com 	else
12069ba682f0Skxie@chelsio.com 		copied = err;
12079ba682f0Skxie@chelsio.com 	goto done;
12089ba682f0Skxie@chelsio.com }
12099ba682f0Skxie@chelsio.com 
121071f7a00bSVarun Prakash static inline void
121171f7a00bSVarun Prakash scmd_get_params(struct scsi_cmnd *sc, struct scatterlist **sgl,
121271f7a00bSVarun Prakash 		unsigned int *sgcnt, unsigned int *dlen,
121371f7a00bSVarun Prakash 		unsigned int prot)
121471f7a00bSVarun Prakash {
121571f7a00bSVarun Prakash 	struct scsi_data_buffer *sdb = prot ? scsi_prot(sc) : scsi_out(sc);
121671f7a00bSVarun Prakash 
121771f7a00bSVarun Prakash 	*sgl = sdb->table.sgl;
121871f7a00bSVarun Prakash 	*sgcnt = sdb->table.nents;
121971f7a00bSVarun Prakash 	*dlen = sdb->length;
122071f7a00bSVarun Prakash 	/* Caution: for protection sdb, sdb->length is invalid */
122171f7a00bSVarun Prakash }
122271f7a00bSVarun Prakash 
122371f7a00bSVarun Prakash void cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod *ppod,
122471f7a00bSVarun Prakash 			    struct cxgbi_task_tag_info *ttinfo,
122571f7a00bSVarun Prakash 			    struct scatterlist **sg_pp, unsigned int *sg_off)
122671f7a00bSVarun Prakash {
122771f7a00bSVarun Prakash 	struct scatterlist *sg = sg_pp ? *sg_pp : NULL;
122871f7a00bSVarun Prakash 	unsigned int offset = sg_off ? *sg_off : 0;
122971f7a00bSVarun Prakash 	dma_addr_t addr = 0UL;
123071f7a00bSVarun Prakash 	unsigned int len = 0;
123171f7a00bSVarun Prakash 	int i;
123271f7a00bSVarun Prakash 
123371f7a00bSVarun Prakash 	memcpy(ppod, &ttinfo->hdr, sizeof(struct cxgbi_pagepod_hdr));
123471f7a00bSVarun Prakash 
123571f7a00bSVarun Prakash 	if (sg) {
123671f7a00bSVarun Prakash 		addr = sg_dma_address(sg);
123771f7a00bSVarun Prakash 		len = sg_dma_len(sg);
123871f7a00bSVarun Prakash 	}
123971f7a00bSVarun Prakash 
124071f7a00bSVarun Prakash 	for (i = 0; i < PPOD_PAGES_MAX; i++) {
124171f7a00bSVarun Prakash 		if (sg) {
124271f7a00bSVarun Prakash 			ppod->addr[i] = cpu_to_be64(addr + offset);
124371f7a00bSVarun Prakash 			offset += PAGE_SIZE;
124471f7a00bSVarun Prakash 			if (offset == (len + sg->offset)) {
124571f7a00bSVarun Prakash 				offset = 0;
124671f7a00bSVarun Prakash 				sg = sg_next(sg);
124771f7a00bSVarun Prakash 				if (sg) {
124871f7a00bSVarun Prakash 					addr = sg_dma_address(sg);
124971f7a00bSVarun Prakash 					len = sg_dma_len(sg);
125071f7a00bSVarun Prakash 				}
125171f7a00bSVarun Prakash 			}
125271f7a00bSVarun Prakash 		} else {
125371f7a00bSVarun Prakash 			ppod->addr[i] = 0ULL;
125471f7a00bSVarun Prakash 		}
125571f7a00bSVarun Prakash 	}
125671f7a00bSVarun Prakash 
125771f7a00bSVarun Prakash 	/*
125871f7a00bSVarun Prakash 	 * the fifth address needs to be repeated in the next ppod, so do
125971f7a00bSVarun Prakash 	 * not move sg
126071f7a00bSVarun Prakash 	 */
126171f7a00bSVarun Prakash 	if (sg_pp) {
126271f7a00bSVarun Prakash 		*sg_pp = sg;
126371f7a00bSVarun Prakash 		*sg_off = offset;
126471f7a00bSVarun Prakash 	}
126571f7a00bSVarun Prakash 
126671f7a00bSVarun Prakash 	if (offset == len) {
126771f7a00bSVarun Prakash 		offset = 0;
126871f7a00bSVarun Prakash 		sg = sg_next(sg);
126971f7a00bSVarun Prakash 		if (sg) {
127071f7a00bSVarun Prakash 			addr = sg_dma_address(sg);
127171f7a00bSVarun Prakash 			len = sg_dma_len(sg);
127271f7a00bSVarun Prakash 		}
127371f7a00bSVarun Prakash 	}
127471f7a00bSVarun Prakash 	ppod->addr[i] = sg ? cpu_to_be64(addr + offset) : 0ULL;
127571f7a00bSVarun Prakash }
127671f7a00bSVarun Prakash EXPORT_SYMBOL_GPL(cxgbi_ddp_set_one_ppod);
127771f7a00bSVarun Prakash 
12789ba682f0Skxie@chelsio.com /*
12799ba682f0Skxie@chelsio.com  * APIs interacting with open-iscsi libraries
12809ba682f0Skxie@chelsio.com  */
12819ba682f0Skxie@chelsio.com 
12829ba682f0Skxie@chelsio.com static unsigned char padding[4];
12839ba682f0Skxie@chelsio.com 
128471f7a00bSVarun Prakash void cxgbi_ddp_ppm_setup(void **ppm_pp, struct cxgbi_device *cdev,
128571f7a00bSVarun Prakash 			 struct cxgbi_tag_format *tformat, unsigned int ppmax,
128671f7a00bSVarun Prakash 			 unsigned int llimit, unsigned int start,
128771f7a00bSVarun Prakash 			 unsigned int rsvd_factor)
128871f7a00bSVarun Prakash {
128971f7a00bSVarun Prakash 	int err = cxgbi_ppm_init(ppm_pp, cdev->ports[0], cdev->pdev,
129071f7a00bSVarun Prakash 				cdev->lldev, tformat, ppmax, llimit, start,
129171f7a00bSVarun Prakash 				rsvd_factor);
129271f7a00bSVarun Prakash 
129371f7a00bSVarun Prakash 	if (err >= 0) {
129471f7a00bSVarun Prakash 		struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
129571f7a00bSVarun Prakash 
129671f7a00bSVarun Prakash 		if (ppm->ppmax < 1024 ||
129771f7a00bSVarun Prakash 		    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX)
129871f7a00bSVarun Prakash 			cdev->flags |= CXGBI_FLAG_DDP_OFF;
129971f7a00bSVarun Prakash 		err = 0;
130071f7a00bSVarun Prakash 	} else {
130171f7a00bSVarun Prakash 		cdev->flags |= CXGBI_FLAG_DDP_OFF;
130271f7a00bSVarun Prakash 	}
130371f7a00bSVarun Prakash }
130471f7a00bSVarun Prakash EXPORT_SYMBOL_GPL(cxgbi_ddp_ppm_setup);
130571f7a00bSVarun Prakash 
130671f7a00bSVarun Prakash static int cxgbi_ddp_sgl_check(struct scatterlist *sgl, int nents)
130771f7a00bSVarun Prakash {
130871f7a00bSVarun Prakash 	int i;
130971f7a00bSVarun Prakash 	int last_sgidx = nents - 1;
131071f7a00bSVarun Prakash 	struct scatterlist *sg = sgl;
131171f7a00bSVarun Prakash 
131271f7a00bSVarun Prakash 	for (i = 0; i < nents; i++, sg = sg_next(sg)) {
131371f7a00bSVarun Prakash 		unsigned int len = sg->length + sg->offset;
131471f7a00bSVarun Prakash 
131571f7a00bSVarun Prakash 		if ((sg->offset & 0x3) || (i && sg->offset) ||
131671f7a00bSVarun Prakash 		    ((i != last_sgidx) && len != PAGE_SIZE)) {
131771f7a00bSVarun Prakash 			log_debug(1 << CXGBI_DBG_DDP,
131871f7a00bSVarun Prakash 				  "sg %u/%u, %u,%u, not aligned.\n",
131971f7a00bSVarun Prakash 				  i, nents, sg->offset, sg->length);
132071f7a00bSVarun Prakash 			goto err_out;
132171f7a00bSVarun Prakash 		}
132271f7a00bSVarun Prakash 	}
132371f7a00bSVarun Prakash 	return 0;
132471f7a00bSVarun Prakash err_out:
132571f7a00bSVarun Prakash 	return -EINVAL;
132671f7a00bSVarun Prakash }
132771f7a00bSVarun Prakash 
132871f7a00bSVarun Prakash static int cxgbi_ddp_reserve(struct cxgbi_conn *cconn,
132971f7a00bSVarun Prakash 			     struct cxgbi_task_data *tdata, u32 sw_tag,
133071f7a00bSVarun Prakash 			     unsigned int xferlen)
133171f7a00bSVarun Prakash {
133271f7a00bSVarun Prakash 	struct cxgbi_sock *csk = cconn->cep->csk;
133371f7a00bSVarun Prakash 	struct cxgbi_device *cdev = csk->cdev;
133471f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
133571f7a00bSVarun Prakash 	struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
133671f7a00bSVarun Prakash 	struct scatterlist *sgl = ttinfo->sgl;
133771f7a00bSVarun Prakash 	unsigned int sgcnt = ttinfo->nents;
133871f7a00bSVarun Prakash 	unsigned int sg_offset = sgl->offset;
133971f7a00bSVarun Prakash 	int err;
134071f7a00bSVarun Prakash 
134171f7a00bSVarun Prakash 	if (cdev->flags & CXGBI_FLAG_DDP_OFF) {
134271f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_DDP,
134371f7a00bSVarun Prakash 			  "cdev 0x%p DDP off.\n", cdev);
134471f7a00bSVarun Prakash 		return -EINVAL;
134571f7a00bSVarun Prakash 	}
134671f7a00bSVarun Prakash 
134771f7a00bSVarun Prakash 	if (!ppm || xferlen < DDP_THRESHOLD || !sgcnt ||
134871f7a00bSVarun Prakash 	    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX) {
134971f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_DDP,
135071f7a00bSVarun Prakash 			  "ppm 0x%p, pgidx %u, xfer %u, sgcnt %u, NO ddp.\n",
135171f7a00bSVarun Prakash 			  ppm, ppm ? ppm->tformat.pgsz_idx_dflt : DDP_PGIDX_MAX,
135271f7a00bSVarun Prakash 			  xferlen, ttinfo->nents);
135371f7a00bSVarun Prakash 		return -EINVAL;
135471f7a00bSVarun Prakash 	}
135571f7a00bSVarun Prakash 
135671f7a00bSVarun Prakash 	/* make sure the buffer is suitable for ddp */
135771f7a00bSVarun Prakash 	if (cxgbi_ddp_sgl_check(sgl, sgcnt) < 0)
135871f7a00bSVarun Prakash 		return -EINVAL;
135971f7a00bSVarun Prakash 
136071f7a00bSVarun Prakash 	ttinfo->nr_pages = (xferlen + sgl->offset + (1 << PAGE_SHIFT) - 1) >>
136171f7a00bSVarun Prakash 			    PAGE_SHIFT;
136271f7a00bSVarun Prakash 
136371f7a00bSVarun Prakash 	/*
136471f7a00bSVarun Prakash 	 * the ddp tag will be used for the itt in the outgoing pdu,
136571f7a00bSVarun Prakash 	 * the itt genrated by libiscsi is saved in the ppm and can be
136671f7a00bSVarun Prakash 	 * retrieved via the ddp tag
136771f7a00bSVarun Prakash 	 */
136871f7a00bSVarun Prakash 	err = cxgbi_ppm_ppods_reserve(ppm, ttinfo->nr_pages, 0, &ttinfo->idx,
136971f7a00bSVarun Prakash 				      &ttinfo->tag, (unsigned long)sw_tag);
137071f7a00bSVarun Prakash 	if (err < 0) {
137171f7a00bSVarun Prakash 		cconn->ddp_full++;
137271f7a00bSVarun Prakash 		return err;
137371f7a00bSVarun Prakash 	}
137471f7a00bSVarun Prakash 	ttinfo->npods = err;
137571f7a00bSVarun Prakash 
137671f7a00bSVarun Prakash 	 /* setup dma from scsi command sgl */
137771f7a00bSVarun Prakash 	sgl->offset = 0;
137871f7a00bSVarun Prakash 	err = dma_map_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
137971f7a00bSVarun Prakash 	sgl->offset = sg_offset;
138071f7a00bSVarun Prakash 	if (err == 0) {
138171f7a00bSVarun Prakash 		pr_info("%s: 0x%x, xfer %u, sgl %u dma mapping err.\n",
138271f7a00bSVarun Prakash 			__func__, sw_tag, xferlen, sgcnt);
138371f7a00bSVarun Prakash 		goto rel_ppods;
138471f7a00bSVarun Prakash 	}
138571f7a00bSVarun Prakash 	if (err != ttinfo->nr_pages) {
138671f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_DDP,
138771f7a00bSVarun Prakash 			  "%s: sw tag 0x%x, xfer %u, sgl %u, dma count %d.\n",
138871f7a00bSVarun Prakash 			  __func__, sw_tag, xferlen, sgcnt, err);
138971f7a00bSVarun Prakash 	}
139071f7a00bSVarun Prakash 
139171f7a00bSVarun Prakash 	ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_MAPPED;
139271f7a00bSVarun Prakash 	ttinfo->cid = csk->port_id;
139371f7a00bSVarun Prakash 
139471f7a00bSVarun Prakash 	cxgbi_ppm_make_ppod_hdr(ppm, ttinfo->tag, csk->tid, sgl->offset,
139571f7a00bSVarun Prakash 				xferlen, &ttinfo->hdr);
139671f7a00bSVarun Prakash 
139771f7a00bSVarun Prakash 	if (cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ) {
139871f7a00bSVarun Prakash 		/* write ppod from xmit_pdu (of iscsi_scsi_command pdu) */
139971f7a00bSVarun Prakash 		ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_VALID;
140071f7a00bSVarun Prakash 	} else {
140171f7a00bSVarun Prakash 		/* write ppod from control queue now */
140271f7a00bSVarun Prakash 		err = cdev->csk_ddp_set_map(ppm, csk, ttinfo);
140371f7a00bSVarun Prakash 		if (err < 0)
140471f7a00bSVarun Prakash 			goto rel_ppods;
140571f7a00bSVarun Prakash 	}
140671f7a00bSVarun Prakash 
140771f7a00bSVarun Prakash 	return 0;
140871f7a00bSVarun Prakash 
140971f7a00bSVarun Prakash rel_ppods:
141071f7a00bSVarun Prakash 	cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
141171f7a00bSVarun Prakash 
141271f7a00bSVarun Prakash 	if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_MAPPED) {
141371f7a00bSVarun Prakash 		ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_MAPPED;
141471f7a00bSVarun Prakash 		dma_unmap_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
141571f7a00bSVarun Prakash 	}
141671f7a00bSVarun Prakash 	return -EINVAL;
141771f7a00bSVarun Prakash }
141871f7a00bSVarun Prakash 
14199ba682f0Skxie@chelsio.com static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
14209ba682f0Skxie@chelsio.com {
142171f7a00bSVarun Prakash 	struct scsi_cmnd *sc = task->sc;
142271f7a00bSVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
142371f7a00bSVarun Prakash 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
142471f7a00bSVarun Prakash 	struct cxgbi_device *cdev = cconn->chba->cdev;
142571f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
142671f7a00bSVarun Prakash 	u32 tag = ntohl((__force u32)hdr_itt);
142771f7a00bSVarun Prakash 
142871f7a00bSVarun Prakash 	log_debug(1 << CXGBI_DBG_DDP,
142971f7a00bSVarun Prakash 		  "cdev 0x%p, task 0x%p, release tag 0x%x.\n",
143071f7a00bSVarun Prakash 		  cdev, task, tag);
143171f7a00bSVarun Prakash 	if (sc &&
143271f7a00bSVarun Prakash 	    (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
143371f7a00bSVarun Prakash 	    cxgbi_ppm_is_ddp_tag(ppm, tag)) {
143471f7a00bSVarun Prakash 		struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
143571f7a00bSVarun Prakash 		struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
143671f7a00bSVarun Prakash 
143771f7a00bSVarun Prakash 		if (!(cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ))
143871f7a00bSVarun Prakash 			cdev->csk_ddp_clear_map(cdev, ppm, ttinfo);
143971f7a00bSVarun Prakash 		cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
144071f7a00bSVarun Prakash 		dma_unmap_sg(&ppm->pdev->dev, ttinfo->sgl, ttinfo->nents,
144171f7a00bSVarun Prakash 			     DMA_FROM_DEVICE);
144271f7a00bSVarun Prakash 	}
144371f7a00bSVarun Prakash }
144471f7a00bSVarun Prakash 
144571f7a00bSVarun Prakash static inline u32 cxgbi_build_sw_tag(u32 idx, u32 age)
144671f7a00bSVarun Prakash {
144771f7a00bSVarun Prakash 	/* assume idx and age both are < 0x7FFF (32767) */
144871f7a00bSVarun Prakash 	return (idx << 16) | age;
14499ba682f0Skxie@chelsio.com }
14509ba682f0Skxie@chelsio.com 
14519ba682f0Skxie@chelsio.com static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
14529ba682f0Skxie@chelsio.com {
145371f7a00bSVarun Prakash 	struct scsi_cmnd *sc = task->sc;
145471f7a00bSVarun Prakash 	struct iscsi_conn *conn = task->conn;
145571f7a00bSVarun Prakash 	struct iscsi_session *sess = conn->session;
145671f7a00bSVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
145771f7a00bSVarun Prakash 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
145871f7a00bSVarun Prakash 	struct cxgbi_device *cdev = cconn->chba->cdev;
145971f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
146071f7a00bSVarun Prakash 	u32 sw_tag = cxgbi_build_sw_tag(task->itt, sess->age);
146171f7a00bSVarun Prakash 	u32 tag = 0;
146271f7a00bSVarun Prakash 	int err = -EINVAL;
146371f7a00bSVarun Prakash 
146471f7a00bSVarun Prakash 	if (sc &&
146571f7a00bSVarun Prakash 	    (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)
146671f7a00bSVarun Prakash 	) {
146771f7a00bSVarun Prakash 		struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
146871f7a00bSVarun Prakash 		struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
146971f7a00bSVarun Prakash 
147071f7a00bSVarun Prakash 		scmd_get_params(sc, &ttinfo->sgl, &ttinfo->nents,
147171f7a00bSVarun Prakash 				&tdata->dlen, 0);
147271f7a00bSVarun Prakash 		err = cxgbi_ddp_reserve(cconn, tdata, sw_tag, tdata->dlen);
147371f7a00bSVarun Prakash 		if (!err)
147471f7a00bSVarun Prakash 			tag = ttinfo->tag;
147571f7a00bSVarun Prakash 		else
147671f7a00bSVarun Prakash 			 log_debug(1 << CXGBI_DBG_DDP,
147771f7a00bSVarun Prakash 				   "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
147871f7a00bSVarun Prakash 				   cconn->cep->csk, task, tdata->dlen,
147971f7a00bSVarun Prakash 				   ttinfo->nents);
148071f7a00bSVarun Prakash 	}
148171f7a00bSVarun Prakash 
148271f7a00bSVarun Prakash 	if (err < 0) {
148371f7a00bSVarun Prakash 		err = cxgbi_ppm_make_non_ddp_tag(ppm, sw_tag, &tag);
148471f7a00bSVarun Prakash 		if (err < 0)
148571f7a00bSVarun Prakash 			return err;
148671f7a00bSVarun Prakash 	}
148771f7a00bSVarun Prakash 	/*  the itt need to sent in big-endian order */
148871f7a00bSVarun Prakash 	*hdr_itt = (__force itt_t)htonl(tag);
148971f7a00bSVarun Prakash 
149071f7a00bSVarun Prakash 	log_debug(1 << CXGBI_DBG_DDP,
149171f7a00bSVarun Prakash 		  "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
149271f7a00bSVarun Prakash 		  cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
14939ba682f0Skxie@chelsio.com 	return 0;
14949ba682f0Skxie@chelsio.com }
14959ba682f0Skxie@chelsio.com 
14969ba682f0Skxie@chelsio.com void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
14979ba682f0Skxie@chelsio.com {
149871f7a00bSVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
149971f7a00bSVarun Prakash 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
150071f7a00bSVarun Prakash 	struct cxgbi_device *cdev = cconn->chba->cdev;
150171f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
150271f7a00bSVarun Prakash 	u32 tag = ntohl((__force u32)itt);
150371f7a00bSVarun Prakash 	u32 sw_bits;
150471f7a00bSVarun Prakash 
150571f7a00bSVarun Prakash 	if (ppm) {
150671f7a00bSVarun Prakash 		if (cxgbi_ppm_is_ddp_tag(ppm, tag))
150771f7a00bSVarun Prakash 			sw_bits = cxgbi_ppm_get_tag_caller_data(ppm, tag);
150871f7a00bSVarun Prakash 		else
150971f7a00bSVarun Prakash 			sw_bits = cxgbi_ppm_decode_non_ddp_tag(ppm, tag);
151071f7a00bSVarun Prakash 	} else {
151171f7a00bSVarun Prakash 		sw_bits = tag;
151271f7a00bSVarun Prakash 	}
151371f7a00bSVarun Prakash 
151471f7a00bSVarun Prakash 	cxgbi_decode_sw_tag(sw_bits, idx, age);
151571f7a00bSVarun Prakash 	log_debug(1 << CXGBI_DBG_DDP,
151671f7a00bSVarun Prakash 		  "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
151771f7a00bSVarun Prakash 		  cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
151871f7a00bSVarun Prakash 		  age ? *age : 0xFF);
15199ba682f0Skxie@chelsio.com }
15209ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
15219ba682f0Skxie@chelsio.com 
15229ba682f0Skxie@chelsio.com void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
15239ba682f0Skxie@chelsio.com {
15249ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = csk->user_data;
15259ba682f0Skxie@chelsio.com 
15269ba682f0Skxie@chelsio.com 	if (conn) {
15279ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_SOCK,
15289ba682f0Skxie@chelsio.com 			"csk 0x%p, cid %d.\n", csk, conn->id);
15299ba682f0Skxie@chelsio.com 		iscsi_conn_queue_work(conn);
15309ba682f0Skxie@chelsio.com 	}
15319ba682f0Skxie@chelsio.com }
15329ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
15339ba682f0Skxie@chelsio.com 
15349ba682f0Skxie@chelsio.com /*
15359ba682f0Skxie@chelsio.com  * pdu receive, interact with libiscsi_tcp
15369ba682f0Skxie@chelsio.com  */
15379ba682f0Skxie@chelsio.com static inline int read_pdu_skb(struct iscsi_conn *conn,
15389ba682f0Skxie@chelsio.com 			       struct sk_buff *skb,
15399ba682f0Skxie@chelsio.com 			       unsigned int offset,
15409ba682f0Skxie@chelsio.com 			       int offloaded)
15419ba682f0Skxie@chelsio.com {
15429ba682f0Skxie@chelsio.com 	int status = 0;
15439ba682f0Skxie@chelsio.com 	int bytes_read;
15449ba682f0Skxie@chelsio.com 
15459ba682f0Skxie@chelsio.com 	bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
15469ba682f0Skxie@chelsio.com 	switch (status) {
15479ba682f0Skxie@chelsio.com 	case ISCSI_TCP_CONN_ERR:
15489ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
15499ba682f0Skxie@chelsio.com 			  skb, offset, offloaded);
15509ba682f0Skxie@chelsio.com 		return -EIO;
15519ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SUSPENDED:
15529ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
15539ba682f0Skxie@chelsio.com 			"skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
15549ba682f0Skxie@chelsio.com 			skb, offset, offloaded, bytes_read);
15559ba682f0Skxie@chelsio.com 		/* no transfer - just have caller flush queue */
15569ba682f0Skxie@chelsio.com 		return bytes_read;
15579ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SKB_DONE:
15589ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
15599ba682f0Skxie@chelsio.com 			skb, offset, offloaded);
15609ba682f0Skxie@chelsio.com 		/*
15619ba682f0Skxie@chelsio.com 		 * pdus should always fit in the skb and we should get
15629ba682f0Skxie@chelsio.com 		 * segment done notifcation.
15639ba682f0Skxie@chelsio.com 		 */
15649ba682f0Skxie@chelsio.com 		iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
15659ba682f0Skxie@chelsio.com 		return -EFAULT;
15669ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SEGMENT_DONE:
15679ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
15689ba682f0Skxie@chelsio.com 			"skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
15699ba682f0Skxie@chelsio.com 			skb, offset, offloaded, bytes_read);
15709ba682f0Skxie@chelsio.com 		return bytes_read;
15719ba682f0Skxie@chelsio.com 	default:
15729ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
15739ba682f0Skxie@chelsio.com 			skb, offset, offloaded, status);
15749ba682f0Skxie@chelsio.com 		return -EINVAL;
15759ba682f0Skxie@chelsio.com 	}
15769ba682f0Skxie@chelsio.com }
15779ba682f0Skxie@chelsio.com 
1578e0f8e8cfSVarun Prakash static int
1579e0f8e8cfSVarun Prakash skb_read_pdu_bhs(struct cxgbi_sock *csk, struct iscsi_conn *conn,
1580e0f8e8cfSVarun Prakash 		 struct sk_buff *skb)
15819ba682f0Skxie@chelsio.com {
15829ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1583e0f8e8cfSVarun Prakash 	int err;
15849ba682f0Skxie@chelsio.com 
15859ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
15869ba682f0Skxie@chelsio.com 		"conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
15879ba682f0Skxie@chelsio.com 		conn, skb, skb->len, cxgbi_skcb_flags(skb));
15889ba682f0Skxie@chelsio.com 
15899ba682f0Skxie@chelsio.com 	if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
15909ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
15919ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
15929ba682f0Skxie@chelsio.com 		return -EIO;
15939ba682f0Skxie@chelsio.com 	}
15949ba682f0Skxie@chelsio.com 
15959ba682f0Skxie@chelsio.com 	if (conn->hdrdgst_en &&
15969ba682f0Skxie@chelsio.com 	    cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
15979ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
15989ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
15999ba682f0Skxie@chelsio.com 		return -EIO;
16009ba682f0Skxie@chelsio.com 	}
16019ba682f0Skxie@chelsio.com 
160244830d8fSVarun Prakash 	if (cxgbi_skcb_test_flag(skb, SKCBF_RX_ISCSI_COMPL) &&
160344830d8fSVarun Prakash 	    cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA_DDPD)) {
160444830d8fSVarun Prakash 		/* If completion flag is set and data is directly
160544830d8fSVarun Prakash 		 * placed in to the host memory then update
160644830d8fSVarun Prakash 		 * task->exp_datasn to the datasn in completion
160744830d8fSVarun Prakash 		 * iSCSI hdr as T6 adapter generates completion only
160844830d8fSVarun Prakash 		 * for the last pdu of a sequence.
160944830d8fSVarun Prakash 		 */
161044830d8fSVarun Prakash 		itt_t itt = ((struct iscsi_data *)skb->data)->itt;
161144830d8fSVarun Prakash 		struct iscsi_task *task = iscsi_itt_to_ctask(conn, itt);
161244830d8fSVarun Prakash 		u32 data_sn = be32_to_cpu(((struct iscsi_data *)
161344830d8fSVarun Prakash 							skb->data)->datasn);
161444830d8fSVarun Prakash 		if (task && task->sc) {
161544830d8fSVarun Prakash 			struct iscsi_tcp_task *tcp_task = task->dd_data;
161644830d8fSVarun Prakash 
161744830d8fSVarun Prakash 			tcp_task->exp_datasn = data_sn;
161844830d8fSVarun Prakash 		}
161944830d8fSVarun Prakash 	}
162044830d8fSVarun Prakash 
1621e0f8e8cfSVarun Prakash 	err = read_pdu_skb(conn, skb, 0, 0);
1622e0f8e8cfSVarun Prakash 	if (likely(err >= 0)) {
1623e0f8e8cfSVarun Prakash 		struct iscsi_hdr *hdr = (struct iscsi_hdr *)skb->data;
1624e0f8e8cfSVarun Prakash 		u8 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
1625e0f8e8cfSVarun Prakash 
1626e0f8e8cfSVarun Prakash 		if (unlikely(opcode == ISCSI_OP_LOGOUT_RSP))
1627e0f8e8cfSVarun Prakash 			cxgbi_sock_set_flag(csk, CTPF_LOGOUT_RSP_RCVD);
1628e0f8e8cfSVarun Prakash 	}
1629e0f8e8cfSVarun Prakash 
1630e0f8e8cfSVarun Prakash 	return err;
16319ba682f0Skxie@chelsio.com }
16329ba682f0Skxie@chelsio.com 
16339ba682f0Skxie@chelsio.com static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
16349ba682f0Skxie@chelsio.com 			     struct sk_buff *skb, unsigned int offset)
16359ba682f0Skxie@chelsio.com {
16369ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
16379ba682f0Skxie@chelsio.com 	bool offloaded = 0;
16389ba682f0Skxie@chelsio.com 	int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
16399ba682f0Skxie@chelsio.com 
16409ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
16419ba682f0Skxie@chelsio.com 		"conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
16429ba682f0Skxie@chelsio.com 		conn, skb, skb->len, cxgbi_skcb_flags(skb));
16439ba682f0Skxie@chelsio.com 
16449ba682f0Skxie@chelsio.com 	if (conn->datadgst_en &&
16459ba682f0Skxie@chelsio.com 	    cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
16469ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
16479ba682f0Skxie@chelsio.com 			conn, lskb, cxgbi_skcb_flags(lskb));
16489ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
16499ba682f0Skxie@chelsio.com 		return -EIO;
16509ba682f0Skxie@chelsio.com 	}
16519ba682f0Skxie@chelsio.com 
16529ba682f0Skxie@chelsio.com 	if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
16539ba682f0Skxie@chelsio.com 		return 0;
16549ba682f0Skxie@chelsio.com 
16559ba682f0Skxie@chelsio.com 	/* coalesced, add header digest length */
16569ba682f0Skxie@chelsio.com 	if (lskb == skb && conn->hdrdgst_en)
16579ba682f0Skxie@chelsio.com 		offset += ISCSI_DIGEST_SIZE;
16589ba682f0Skxie@chelsio.com 
16599ba682f0Skxie@chelsio.com 	if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
16609ba682f0Skxie@chelsio.com 		offloaded = 1;
16619ba682f0Skxie@chelsio.com 
16629ba682f0Skxie@chelsio.com 	if (opcode == ISCSI_OP_SCSI_DATA_IN)
16639ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
16649ba682f0Skxie@chelsio.com 			"skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
16659ba682f0Skxie@chelsio.com 			skb, opcode, ntohl(tcp_conn->in.hdr->itt),
16669ba682f0Skxie@chelsio.com 			tcp_conn->in.datalen, offloaded ? "is" : "not");
16679ba682f0Skxie@chelsio.com 
16689ba682f0Skxie@chelsio.com 	return read_pdu_skb(conn, skb, offset, offloaded);
16699ba682f0Skxie@chelsio.com }
16709ba682f0Skxie@chelsio.com 
16719ba682f0Skxie@chelsio.com static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
16729ba682f0Skxie@chelsio.com {
16739ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
16749ba682f0Skxie@chelsio.com 	int must_send;
16759ba682f0Skxie@chelsio.com 	u32 credits;
16769ba682f0Skxie@chelsio.com 
16779ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
1678fc385045SHans Wennborg 		"csk 0x%p,%u,0x%lx,%u, seq %u, wup %u, thre %u, %u.\n",
16799ba682f0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
16809ba682f0Skxie@chelsio.com 		csk->rcv_wup, cdev->rx_credit_thres,
168181daf10cSKaren Xie 		csk->rcv_win);
16829ba682f0Skxie@chelsio.com 
1683586be7cbSVarun Prakash 	if (!cdev->rx_credit_thres)
1684586be7cbSVarun Prakash 		return;
1685586be7cbSVarun Prakash 
16869ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED)
16879ba682f0Skxie@chelsio.com 		return;
16889ba682f0Skxie@chelsio.com 
16899ba682f0Skxie@chelsio.com 	credits = csk->copied_seq - csk->rcv_wup;
16909ba682f0Skxie@chelsio.com 	if (unlikely(!credits))
16919ba682f0Skxie@chelsio.com 		return;
169281daf10cSKaren Xie 	must_send = credits + 16384 >= csk->rcv_win;
16939ba682f0Skxie@chelsio.com 	if (must_send || credits >= cdev->rx_credit_thres)
16949ba682f0Skxie@chelsio.com 		csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
16959ba682f0Skxie@chelsio.com }
16969ba682f0Skxie@chelsio.com 
16979ba682f0Skxie@chelsio.com void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
16989ba682f0Skxie@chelsio.com {
16999ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
17009ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = csk->user_data;
17019ba682f0Skxie@chelsio.com 	struct sk_buff *skb;
17029ba682f0Skxie@chelsio.com 	unsigned int read = 0;
17039ba682f0Skxie@chelsio.com 	int err = 0;
17049ba682f0Skxie@chelsio.com 
17059ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
17069ba682f0Skxie@chelsio.com 		"csk 0x%p, conn 0x%p.\n", csk, conn);
17079ba682f0Skxie@chelsio.com 
17089ba682f0Skxie@chelsio.com 	if (unlikely(!conn || conn->suspend_rx)) {
17099ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
17109ba682f0Skxie@chelsio.com 			"csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
17119ba682f0Skxie@chelsio.com 			csk, conn, conn ? conn->id : 0xFF,
17129ba682f0Skxie@chelsio.com 			conn ? conn->suspend_rx : 0xFF);
17139ba682f0Skxie@chelsio.com 		return;
17149ba682f0Skxie@chelsio.com 	}
17159ba682f0Skxie@chelsio.com 
17169ba682f0Skxie@chelsio.com 	while (!err) {
17179ba682f0Skxie@chelsio.com 		skb = skb_peek(&csk->receive_queue);
17189ba682f0Skxie@chelsio.com 		if (!skb ||
17199ba682f0Skxie@chelsio.com 		    !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
17209ba682f0Skxie@chelsio.com 			if (skb)
17219ba682f0Skxie@chelsio.com 				log_debug(1 << CXGBI_DBG_PDU_RX,
17229ba682f0Skxie@chelsio.com 					"skb 0x%p, NOT ready 0x%lx.\n",
17239ba682f0Skxie@chelsio.com 					skb, cxgbi_skcb_flags(skb));
17249ba682f0Skxie@chelsio.com 			break;
17259ba682f0Skxie@chelsio.com 		}
17269ba682f0Skxie@chelsio.com 		__skb_unlink(skb, &csk->receive_queue);
17279ba682f0Skxie@chelsio.com 
17289ba682f0Skxie@chelsio.com 		read += cxgbi_skcb_rx_pdulen(skb);
17299ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
17309ba682f0Skxie@chelsio.com 			"csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
17319ba682f0Skxie@chelsio.com 			csk, skb, skb->len, cxgbi_skcb_flags(skb),
17329ba682f0Skxie@chelsio.com 			cxgbi_skcb_rx_pdulen(skb));
17339ba682f0Skxie@chelsio.com 
17349ba682f0Skxie@chelsio.com 		if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1735e0f8e8cfSVarun Prakash 			err = skb_read_pdu_bhs(csk, conn, skb);
1736e3d2ad8cSkxie@chelsio.com 			if (err < 0) {
1737e3d2ad8cSkxie@chelsio.com 				pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1738e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1739e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1740e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1741e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
1742e3d2ad8cSkxie@chelsio.com 				goto skb_done;
1743e3d2ad8cSkxie@chelsio.com 			}
17449ba682f0Skxie@chelsio.com 			err = skb_read_pdu_data(conn, skb, skb,
17459ba682f0Skxie@chelsio.com 						err + cdev->skb_rx_extra);
1746e3d2ad8cSkxie@chelsio.com 			if (err < 0)
1747e3d2ad8cSkxie@chelsio.com 				pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1748e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1749e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1750e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1751e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
17529ba682f0Skxie@chelsio.com 		} else {
1753e0f8e8cfSVarun Prakash 			err = skb_read_pdu_bhs(csk, conn, skb);
1754e3d2ad8cSkxie@chelsio.com 			if (err < 0) {
1755e3d2ad8cSkxie@chelsio.com 				pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1756e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1757e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1758e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1759e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
1760e3d2ad8cSkxie@chelsio.com 				goto skb_done;
1761e3d2ad8cSkxie@chelsio.com 			}
1762e3d2ad8cSkxie@chelsio.com 
17639ba682f0Skxie@chelsio.com 			if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
17649ba682f0Skxie@chelsio.com 				struct sk_buff *dskb;
17659ba682f0Skxie@chelsio.com 
17669ba682f0Skxie@chelsio.com 				dskb = skb_peek(&csk->receive_queue);
17679ba682f0Skxie@chelsio.com 				if (!dskb) {
1768e3d2ad8cSkxie@chelsio.com 					pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1769e3d2ad8cSkxie@chelsio.com 						" plen %u, NO data.\n",
1770e3d2ad8cSkxie@chelsio.com 						csk, skb, skb->len,
1771e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_flags(skb),
1772e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_rx_pdulen(skb));
1773e3d2ad8cSkxie@chelsio.com 					err = -EIO;
1774e3d2ad8cSkxie@chelsio.com 					goto skb_done;
17759ba682f0Skxie@chelsio.com 				}
17769ba682f0Skxie@chelsio.com 				__skb_unlink(dskb, &csk->receive_queue);
17779ba682f0Skxie@chelsio.com 
17789ba682f0Skxie@chelsio.com 				err = skb_read_pdu_data(conn, skb, dskb, 0);
1779e3d2ad8cSkxie@chelsio.com 				if (err < 0)
1780e3d2ad8cSkxie@chelsio.com 					pr_err("data, csk 0x%p, skb 0x%p,%u, "
1781e3d2ad8cSkxie@chelsio.com 						"f 0x%lx, plen %u, dskb 0x%p,"
1782e3d2ad8cSkxie@chelsio.com 						"%u.\n",
1783e3d2ad8cSkxie@chelsio.com 						csk, skb, skb->len,
1784e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_flags(skb),
1785e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_rx_pdulen(skb),
1786e3d2ad8cSkxie@chelsio.com 						dskb, dskb->len);
17879ba682f0Skxie@chelsio.com 				__kfree_skb(dskb);
17889ba682f0Skxie@chelsio.com 			} else
17899ba682f0Skxie@chelsio.com 				err = skb_read_pdu_data(conn, skb, skb, 0);
17909ba682f0Skxie@chelsio.com 		}
1791e3d2ad8cSkxie@chelsio.com skb_done:
1792e3d2ad8cSkxie@chelsio.com 		__kfree_skb(skb);
1793e3d2ad8cSkxie@chelsio.com 
17949ba682f0Skxie@chelsio.com 		if (err < 0)
17959ba682f0Skxie@chelsio.com 			break;
17969ba682f0Skxie@chelsio.com 	}
17979ba682f0Skxie@chelsio.com 
17989ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
17999ba682f0Skxie@chelsio.com 	if (read) {
18009ba682f0Skxie@chelsio.com 		csk->copied_seq += read;
18019ba682f0Skxie@chelsio.com 		csk_return_rx_credits(csk, read);
18029ba682f0Skxie@chelsio.com 		conn->rxdata_octets += read;
18039ba682f0Skxie@chelsio.com 	}
18049ba682f0Skxie@chelsio.com 
18059ba682f0Skxie@chelsio.com 	if (err < 0) {
1806e3d2ad8cSkxie@chelsio.com 		pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1807e3d2ad8cSkxie@chelsio.com 			csk, conn, err, read);
18089ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
18099ba682f0Skxie@chelsio.com 	}
18109ba682f0Skxie@chelsio.com }
18119ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
18129ba682f0Skxie@chelsio.com 
18139ba682f0Skxie@chelsio.com static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
18149ba682f0Skxie@chelsio.com 				unsigned int offset, unsigned int *off,
18159ba682f0Skxie@chelsio.com 				struct scatterlist **sgp)
18169ba682f0Skxie@chelsio.com {
18179ba682f0Skxie@chelsio.com 	int i;
18189ba682f0Skxie@chelsio.com 	struct scatterlist *sg;
18199ba682f0Skxie@chelsio.com 
18209ba682f0Skxie@chelsio.com 	for_each_sg(sgl, sg, sgcnt, i) {
18219ba682f0Skxie@chelsio.com 		if (offset < sg->length) {
18229ba682f0Skxie@chelsio.com 			*off = offset;
18239ba682f0Skxie@chelsio.com 			*sgp = sg;
18249ba682f0Skxie@chelsio.com 			return 0;
18259ba682f0Skxie@chelsio.com 		}
18269ba682f0Skxie@chelsio.com 		offset -= sg->length;
18279ba682f0Skxie@chelsio.com 	}
18289ba682f0Skxie@chelsio.com 	return -EFAULT;
18299ba682f0Skxie@chelsio.com }
18309ba682f0Skxie@chelsio.com 
18319ba682f0Skxie@chelsio.com static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
18326a39a16aSIan Campbell 				unsigned int dlen, struct page_frag *frags,
18339ba682f0Skxie@chelsio.com 				int frag_max)
18349ba682f0Skxie@chelsio.com {
18359ba682f0Skxie@chelsio.com 	unsigned int datalen = dlen;
18369ba682f0Skxie@chelsio.com 	unsigned int sglen = sg->length - sgoffset;
18379ba682f0Skxie@chelsio.com 	struct page *page = sg_page(sg);
18389ba682f0Skxie@chelsio.com 	int i;
18399ba682f0Skxie@chelsio.com 
18409ba682f0Skxie@chelsio.com 	i = 0;
18419ba682f0Skxie@chelsio.com 	do {
18429ba682f0Skxie@chelsio.com 		unsigned int copy;
18439ba682f0Skxie@chelsio.com 
18449ba682f0Skxie@chelsio.com 		if (!sglen) {
18459ba682f0Skxie@chelsio.com 			sg = sg_next(sg);
18469ba682f0Skxie@chelsio.com 			if (!sg) {
18479ba682f0Skxie@chelsio.com 				pr_warn("sg %d NULL, len %u/%u.\n",
18489ba682f0Skxie@chelsio.com 					i, datalen, dlen);
18499ba682f0Skxie@chelsio.com 				return -EINVAL;
18509ba682f0Skxie@chelsio.com 			}
18519ba682f0Skxie@chelsio.com 			sgoffset = 0;
18529ba682f0Skxie@chelsio.com 			sglen = sg->length;
18539ba682f0Skxie@chelsio.com 			page = sg_page(sg);
18549ba682f0Skxie@chelsio.com 
18559ba682f0Skxie@chelsio.com 		}
18569ba682f0Skxie@chelsio.com 		copy = min(datalen, sglen);
18579ba682f0Skxie@chelsio.com 		if (i && page == frags[i - 1].page &&
18589ba682f0Skxie@chelsio.com 		    sgoffset + sg->offset ==
18596a39a16aSIan Campbell 			frags[i - 1].offset + frags[i - 1].size) {
18606a39a16aSIan Campbell 			frags[i - 1].size += copy;
18619ba682f0Skxie@chelsio.com 		} else {
18629ba682f0Skxie@chelsio.com 			if (i >= frag_max) {
18639ba682f0Skxie@chelsio.com 				pr_warn("too many pages %u, dlen %u.\n",
18649ba682f0Skxie@chelsio.com 					frag_max, dlen);
18659ba682f0Skxie@chelsio.com 				return -EINVAL;
18669ba682f0Skxie@chelsio.com 			}
18679ba682f0Skxie@chelsio.com 
18689ba682f0Skxie@chelsio.com 			frags[i].page = page;
18696a39a16aSIan Campbell 			frags[i].offset = sg->offset + sgoffset;
18706a39a16aSIan Campbell 			frags[i].size = copy;
18719ba682f0Skxie@chelsio.com 			i++;
18729ba682f0Skxie@chelsio.com 		}
18739ba682f0Skxie@chelsio.com 		datalen -= copy;
18749ba682f0Skxie@chelsio.com 		sgoffset += copy;
18759ba682f0Skxie@chelsio.com 		sglen -= copy;
18769ba682f0Skxie@chelsio.com 	} while (datalen);
18779ba682f0Skxie@chelsio.com 
18789ba682f0Skxie@chelsio.com 	return i;
18799ba682f0Skxie@chelsio.com }
18809ba682f0Skxie@chelsio.com 
18819ba682f0Skxie@chelsio.com int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
18829ba682f0Skxie@chelsio.com {
18839ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
18849ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
18859ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
18869ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = task->conn;
18879ba682f0Skxie@chelsio.com 	struct iscsi_tcp_task *tcp_task = task->dd_data;
1888e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
18899ba682f0Skxie@chelsio.com 	struct scsi_cmnd *sc = task->sc;
189022698483SVarun Prakash 	struct cxgbi_sock *csk = cconn->cep->csk;
189122698483SVarun Prakash 	struct net_device *ndev = cdev->ports[csk->port_id];
18929ba682f0Skxie@chelsio.com 	int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
18939ba682f0Skxie@chelsio.com 
18949ba682f0Skxie@chelsio.com 	tcp_task->dd_data = tdata;
18959ba682f0Skxie@chelsio.com 	task->hdr = NULL;
18969ba682f0Skxie@chelsio.com 
18979ba682f0Skxie@chelsio.com 	if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
18989ba682f0Skxie@chelsio.com 	    (opcode == ISCSI_OP_SCSI_DATA_OUT ||
18999ba682f0Skxie@chelsio.com 	     (opcode == ISCSI_OP_SCSI_CMD &&
19009ba682f0Skxie@chelsio.com 	      (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
19019ba682f0Skxie@chelsio.com 		/* data could goes into skb head */
19029ba682f0Skxie@chelsio.com 		headroom += min_t(unsigned int,
19039ba682f0Skxie@chelsio.com 				SKB_MAX_HEAD(cdev->skb_tx_rsvd),
19049ba682f0Skxie@chelsio.com 				conn->max_xmit_dlength);
19059ba682f0Skxie@chelsio.com 
19069ba682f0Skxie@chelsio.com 	tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
19079ba682f0Skxie@chelsio.com 	if (!tdata->skb) {
190800c4a09bSThadeu Lima de Souza Cascardo 		ndev->stats.tx_dropped++;
19099ba682f0Skxie@chelsio.com 		return -ENOMEM;
19109ba682f0Skxie@chelsio.com 	}
19119ba682f0Skxie@chelsio.com 
19129ba682f0Skxie@chelsio.com 	skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
191322698483SVarun Prakash 
191422698483SVarun Prakash 	if (task->sc) {
19159ba682f0Skxie@chelsio.com 		task->hdr = (struct iscsi_hdr *)tdata->skb->data;
191622698483SVarun Prakash 	} else {
1917*dd04b0f3SVarun Prakash 		task->hdr = kzalloc(SKB_TX_ISCSI_PDU_HEADER_MAX, GFP_ATOMIC);
191822698483SVarun Prakash 		if (!task->hdr) {
191922698483SVarun Prakash 			__kfree_skb(tdata->skb);
192022698483SVarun Prakash 			tdata->skb = NULL;
192122698483SVarun Prakash 			ndev->stats.tx_dropped++;
192222698483SVarun Prakash 			return -ENOMEM;
192322698483SVarun Prakash 		}
192422698483SVarun Prakash 	}
19259ba682f0Skxie@chelsio.com 	task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
19269ba682f0Skxie@chelsio.com 
19279ba682f0Skxie@chelsio.com 	/* data_out uses scsi_cmd's itt */
19289ba682f0Skxie@chelsio.com 	if (opcode != ISCSI_OP_SCSI_DATA_OUT)
19299ba682f0Skxie@chelsio.com 		task_reserve_itt(task, &task->hdr->itt);
19309ba682f0Skxie@chelsio.com 
19319ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
19329ba682f0Skxie@chelsio.com 		"task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
19339ba682f0Skxie@chelsio.com 		task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
19349ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength, ntohl(task->hdr->itt));
19359ba682f0Skxie@chelsio.com 
19369ba682f0Skxie@chelsio.com 	return 0;
19379ba682f0Skxie@chelsio.com }
19389ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
19399ba682f0Skxie@chelsio.com 
19409ba682f0Skxie@chelsio.com static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
19419ba682f0Skxie@chelsio.com {
1942c343a01cSkxie@chelsio.com 	if (hcrc || dcrc) {
19439ba682f0Skxie@chelsio.com 		u8 submode = 0;
19449ba682f0Skxie@chelsio.com 
19459ba682f0Skxie@chelsio.com 		if (hcrc)
19469ba682f0Skxie@chelsio.com 			submode |= 1;
19479ba682f0Skxie@chelsio.com 		if (dcrc)
19489ba682f0Skxie@chelsio.com 			submode |= 2;
19499ba682f0Skxie@chelsio.com 		cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1950c343a01cSkxie@chelsio.com 	} else
1951c343a01cSkxie@chelsio.com 		cxgbi_skcb_ulp_mode(skb) = 0;
19529ba682f0Skxie@chelsio.com }
19539ba682f0Skxie@chelsio.com 
19549ba682f0Skxie@chelsio.com int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
19559ba682f0Skxie@chelsio.com 			      unsigned int count)
19569ba682f0Skxie@chelsio.com {
19579ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = task->conn;
1958e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
19599ba682f0Skxie@chelsio.com 	struct sk_buff *skb = tdata->skb;
19609ba682f0Skxie@chelsio.com 	unsigned int datalen = count;
19619ba682f0Skxie@chelsio.com 	int i, padlen = iscsi_padding(count);
19629ba682f0Skxie@chelsio.com 	struct page *pg;
19639ba682f0Skxie@chelsio.com 
19649ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
19659ba682f0Skxie@chelsio.com 		"task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
19669ba682f0Skxie@chelsio.com 		task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
19679ba682f0Skxie@chelsio.com 		ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
19689ba682f0Skxie@chelsio.com 
19699ba682f0Skxie@chelsio.com 	skb_put(skb, task->hdr_len);
19709ba682f0Skxie@chelsio.com 	tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
19719ba682f0Skxie@chelsio.com 	if (!count)
19729ba682f0Skxie@chelsio.com 		return 0;
19739ba682f0Skxie@chelsio.com 
19749ba682f0Skxie@chelsio.com 	if (task->sc) {
19759ba682f0Skxie@chelsio.com 		struct scsi_data_buffer *sdb = scsi_out(task->sc);
19769ba682f0Skxie@chelsio.com 		struct scatterlist *sg = NULL;
19779ba682f0Skxie@chelsio.com 		int err;
19789ba682f0Skxie@chelsio.com 
19799ba682f0Skxie@chelsio.com 		tdata->offset = offset;
19809ba682f0Skxie@chelsio.com 		tdata->count = count;
19819ba682f0Skxie@chelsio.com 		err = sgl_seek_offset(
19829ba682f0Skxie@chelsio.com 					sdb->table.sgl, sdb->table.nents,
19839ba682f0Skxie@chelsio.com 					tdata->offset, &tdata->sgoffset, &sg);
19849ba682f0Skxie@chelsio.com 		if (err < 0) {
19859ba682f0Skxie@chelsio.com 			pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
19869ba682f0Skxie@chelsio.com 				sdb->table.nents, tdata->offset, sdb->length);
19879ba682f0Skxie@chelsio.com 			return err;
19889ba682f0Skxie@chelsio.com 		}
19899ba682f0Skxie@chelsio.com 		err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
19909ba682f0Skxie@chelsio.com 					tdata->frags, MAX_PDU_FRAGS);
19919ba682f0Skxie@chelsio.com 		if (err < 0) {
19929ba682f0Skxie@chelsio.com 			pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
19939ba682f0Skxie@chelsio.com 				sdb->table.nents, tdata->offset, tdata->count);
19949ba682f0Skxie@chelsio.com 			return err;
19959ba682f0Skxie@chelsio.com 		}
19969ba682f0Skxie@chelsio.com 		tdata->nr_frags = err;
19979ba682f0Skxie@chelsio.com 
19989ba682f0Skxie@chelsio.com 		if (tdata->nr_frags > MAX_SKB_FRAGS ||
19999ba682f0Skxie@chelsio.com 		    (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
20009ba682f0Skxie@chelsio.com 			char *dst = skb->data + task->hdr_len;
20016a39a16aSIan Campbell 			struct page_frag *frag = tdata->frags;
20029ba682f0Skxie@chelsio.com 
20039ba682f0Skxie@chelsio.com 			/* data fits in the skb's headroom */
20049ba682f0Skxie@chelsio.com 			for (i = 0; i < tdata->nr_frags; i++, frag++) {
200577dfce07SCong Wang 				char *src = kmap_atomic(frag->page);
20069ba682f0Skxie@chelsio.com 
20076a39a16aSIan Campbell 				memcpy(dst, src+frag->offset, frag->size);
20086a39a16aSIan Campbell 				dst += frag->size;
200977dfce07SCong Wang 				kunmap_atomic(src);
20109ba682f0Skxie@chelsio.com 			}
20119ba682f0Skxie@chelsio.com 			if (padlen) {
20129ba682f0Skxie@chelsio.com 				memset(dst, 0, padlen);
20139ba682f0Skxie@chelsio.com 				padlen = 0;
20149ba682f0Skxie@chelsio.com 			}
20159ba682f0Skxie@chelsio.com 			skb_put(skb, count + padlen);
20169ba682f0Skxie@chelsio.com 		} else {
20179ba682f0Skxie@chelsio.com 			/* data fit into frag_list */
20186a39a16aSIan Campbell 			for (i = 0; i < tdata->nr_frags; i++) {
20196a39a16aSIan Campbell 				__skb_fill_page_desc(skb, i,
20206a39a16aSIan Campbell 						tdata->frags[i].page,
20216a39a16aSIan Campbell 						tdata->frags[i].offset,
20226a39a16aSIan Campbell 						tdata->frags[i].size);
20236a39a16aSIan Campbell 				skb_frag_ref(skb, i);
20246a39a16aSIan Campbell 			}
20259ba682f0Skxie@chelsio.com 			skb_shinfo(skb)->nr_frags = tdata->nr_frags;
20269ba682f0Skxie@chelsio.com 			skb->len += count;
20279ba682f0Skxie@chelsio.com 			skb->data_len += count;
20289ba682f0Skxie@chelsio.com 			skb->truesize += count;
20299ba682f0Skxie@chelsio.com 		}
20309ba682f0Skxie@chelsio.com 
20319ba682f0Skxie@chelsio.com 	} else {
20329ba682f0Skxie@chelsio.com 		pg = virt_to_page(task->data);
20339ba682f0Skxie@chelsio.com 
20349ba682f0Skxie@chelsio.com 		get_page(pg);
20359ba682f0Skxie@chelsio.com 		skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
20369ba682f0Skxie@chelsio.com 					count);
20379ba682f0Skxie@chelsio.com 		skb->len += count;
20389ba682f0Skxie@chelsio.com 		skb->data_len += count;
20399ba682f0Skxie@chelsio.com 		skb->truesize += count;
20409ba682f0Skxie@chelsio.com 	}
20419ba682f0Skxie@chelsio.com 
20429ba682f0Skxie@chelsio.com 	if (padlen) {
20439ba682f0Skxie@chelsio.com 		i = skb_shinfo(skb)->nr_frags;
20449ba682f0Skxie@chelsio.com 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
20459ba682f0Skxie@chelsio.com 				virt_to_page(padding), offset_in_page(padding),
20469ba682f0Skxie@chelsio.com 				padlen);
20479ba682f0Skxie@chelsio.com 
20489ba682f0Skxie@chelsio.com 		skb->data_len += padlen;
20499ba682f0Skxie@chelsio.com 		skb->truesize += padlen;
20509ba682f0Skxie@chelsio.com 		skb->len += padlen;
20519ba682f0Skxie@chelsio.com 	}
20529ba682f0Skxie@chelsio.com 
20539ba682f0Skxie@chelsio.com 	return 0;
20549ba682f0Skxie@chelsio.com }
20559ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
20569ba682f0Skxie@chelsio.com 
20579ba682f0Skxie@chelsio.com int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
20589ba682f0Skxie@chelsio.com {
20599ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
20609ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
2061e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
206271f7a00bSVarun Prakash 	struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
20639ba682f0Skxie@chelsio.com 	struct sk_buff *skb = tdata->skb;
206471f7a00bSVarun Prakash 	struct cxgbi_sock *csk = NULL;
20659ba682f0Skxie@chelsio.com 	unsigned int datalen;
20669ba682f0Skxie@chelsio.com 	int err;
20679ba682f0Skxie@chelsio.com 
206822698483SVarun Prakash 	if (!skb) {
20699ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
207022698483SVarun Prakash 			"task 0x%p\n", task);
20719ba682f0Skxie@chelsio.com 		return 0;
20729ba682f0Skxie@chelsio.com 	}
20739ba682f0Skxie@chelsio.com 
207471f7a00bSVarun Prakash 	if (cconn && cconn->cep)
207571f7a00bSVarun Prakash 		csk = cconn->cep->csk;
207671f7a00bSVarun Prakash 	if (!csk) {
207771f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
207871f7a00bSVarun Prakash 			  "task 0x%p, csk gone.\n", task);
207971f7a00bSVarun Prakash 		return -EPIPE;
208071f7a00bSVarun Prakash 	}
208171f7a00bSVarun Prakash 
208222698483SVarun Prakash 	tdata->skb = NULL;
20839ba682f0Skxie@chelsio.com 	datalen = skb->data_len;
208471f7a00bSVarun Prakash 
208571f7a00bSVarun Prakash 	/* write ppod first if using ofldq to write ppod */
208671f7a00bSVarun Prakash 	if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_VALID) {
208771f7a00bSVarun Prakash 		struct cxgbi_ppm *ppm = csk->cdev->cdev2ppm(csk->cdev);
208871f7a00bSVarun Prakash 
208971f7a00bSVarun Prakash 		ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_VALID;
209071f7a00bSVarun Prakash 		if (csk->cdev->csk_ddp_set_map(ppm, csk, ttinfo) < 0)
209171f7a00bSVarun Prakash 			pr_err("task 0x%p, ppod writing using ofldq failed.\n",
209271f7a00bSVarun Prakash 			       task);
209371f7a00bSVarun Prakash 			/* continue. Let fl get the data */
209471f7a00bSVarun Prakash 	}
209571f7a00bSVarun Prakash 
209622698483SVarun Prakash 	if (!task->sc)
209722698483SVarun Prakash 		memcpy(skb->data, task->hdr, SKB_TX_ISCSI_PDU_HEADER_MAX);
209822698483SVarun Prakash 
20999ba682f0Skxie@chelsio.com 	err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
21009ba682f0Skxie@chelsio.com 	if (err > 0) {
21019ba682f0Skxie@chelsio.com 		int pdulen = err;
21029ba682f0Skxie@chelsio.com 
21039ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
21049ba682f0Skxie@chelsio.com 			"task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
21059ba682f0Skxie@chelsio.com 			task, task->sc, skb, skb->len, skb->data_len, err);
21069ba682f0Skxie@chelsio.com 
21079ba682f0Skxie@chelsio.com 		if (task->conn->hdrdgst_en)
21089ba682f0Skxie@chelsio.com 			pdulen += ISCSI_DIGEST_SIZE;
21099ba682f0Skxie@chelsio.com 
21109ba682f0Skxie@chelsio.com 		if (datalen && task->conn->datadgst_en)
21119ba682f0Skxie@chelsio.com 			pdulen += ISCSI_DIGEST_SIZE;
21129ba682f0Skxie@chelsio.com 
21139ba682f0Skxie@chelsio.com 		task->conn->txdata_octets += pdulen;
21149ba682f0Skxie@chelsio.com 		return 0;
21159ba682f0Skxie@chelsio.com 	}
21169ba682f0Skxie@chelsio.com 
21179ba682f0Skxie@chelsio.com 	if (err == -EAGAIN || err == -ENOBUFS) {
21189ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
21199ba682f0Skxie@chelsio.com 			"task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
21209ba682f0Skxie@chelsio.com 			task, skb, skb->len, skb->data_len, err);
21219ba682f0Skxie@chelsio.com 		/* reset skb to send when we are called again */
212222698483SVarun Prakash 		tdata->skb = skb;
21239ba682f0Skxie@chelsio.com 		return err;
21249ba682f0Skxie@chelsio.com 	}
21259ba682f0Skxie@chelsio.com 
21269ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
21279ba682f0Skxie@chelsio.com 		"itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
21289ba682f0Skxie@chelsio.com 		task->itt, skb, skb->len, skb->data_len, err);
2129ed481a33SKaren Xie 
213022698483SVarun Prakash 	__kfree_skb(skb);
2131ed481a33SKaren Xie 
21329ba682f0Skxie@chelsio.com 	iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
21339ba682f0Skxie@chelsio.com 	iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
21349ba682f0Skxie@chelsio.com 	return err;
21359ba682f0Skxie@chelsio.com }
21369ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
21379ba682f0Skxie@chelsio.com 
21389ba682f0Skxie@chelsio.com void cxgbi_cleanup_task(struct iscsi_task *task)
21399ba682f0Skxie@chelsio.com {
214071f7a00bSVarun Prakash 	struct iscsi_tcp_task *tcp_task = task->dd_data;
2141e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
21429ba682f0Skxie@chelsio.com 
214350292710SVarun Prakash 	if (!tcp_task || !tdata || (tcp_task->dd_data != tdata)) {
214450292710SVarun Prakash 		pr_info("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
214550292710SVarun Prakash 			task, task->sc, tcp_task,
214650292710SVarun Prakash 			tcp_task ? tcp_task->dd_data : NULL, tdata);
214750292710SVarun Prakash 		return;
214850292710SVarun Prakash 	}
214950292710SVarun Prakash 
21509ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
21519ba682f0Skxie@chelsio.com 		"task 0x%p, skb 0x%p, itt 0x%x.\n",
21529ba682f0Skxie@chelsio.com 		task, tdata->skb, task->hdr_itt);
21539ba682f0Skxie@chelsio.com 
215471f7a00bSVarun Prakash 	tcp_task->dd_data = NULL;
215522698483SVarun Prakash 
215622698483SVarun Prakash 	if (!task->sc)
215722698483SVarun Prakash 		kfree(task->hdr);
215822698483SVarun Prakash 	task->hdr = NULL;
215922698483SVarun Prakash 
21609ba682f0Skxie@chelsio.com 	/*  never reached the xmit task callout */
216175b61250SVarun Prakash 	if (tdata->skb) {
216222698483SVarun Prakash 		__kfree_skb(tdata->skb);
216375b61250SVarun Prakash 		tdata->skb = NULL;
216475b61250SVarun Prakash 	}
21659ba682f0Skxie@chelsio.com 
21669ba682f0Skxie@chelsio.com 	task_release_itt(task, task->hdr_itt);
216769e2d1e6SVarun Prakash 	memset(tdata, 0, sizeof(*tdata));
216869e2d1e6SVarun Prakash 
21699ba682f0Skxie@chelsio.com 	iscsi_tcp_cleanup_task(task);
21709ba682f0Skxie@chelsio.com }
21719ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
21729ba682f0Skxie@chelsio.com 
21739ba682f0Skxie@chelsio.com void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
21749ba682f0Skxie@chelsio.com 				struct iscsi_stats *stats)
21759ba682f0Skxie@chelsio.com {
21769ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
21779ba682f0Skxie@chelsio.com 
21789ba682f0Skxie@chelsio.com 	stats->txdata_octets = conn->txdata_octets;
21799ba682f0Skxie@chelsio.com 	stats->rxdata_octets = conn->rxdata_octets;
21809ba682f0Skxie@chelsio.com 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
21819ba682f0Skxie@chelsio.com 	stats->dataout_pdus = conn->dataout_pdus_cnt;
21829ba682f0Skxie@chelsio.com 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
21839ba682f0Skxie@chelsio.com 	stats->datain_pdus = conn->datain_pdus_cnt;
21849ba682f0Skxie@chelsio.com 	stats->r2t_pdus = conn->r2t_pdus_cnt;
21859ba682f0Skxie@chelsio.com 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
21869ba682f0Skxie@chelsio.com 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
21879ba682f0Skxie@chelsio.com 	stats->digest_err = 0;
21889ba682f0Skxie@chelsio.com 	stats->timeout_err = 0;
21899ba682f0Skxie@chelsio.com 	stats->custom_length = 1;
21909ba682f0Skxie@chelsio.com 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
21919ba682f0Skxie@chelsio.com 	stats->custom[0].value = conn->eh_abort_cnt;
21929ba682f0Skxie@chelsio.com }
21939ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
21949ba682f0Skxie@chelsio.com 
21959ba682f0Skxie@chelsio.com static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
21969ba682f0Skxie@chelsio.com {
21979ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
21989ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
21999ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
22009ba682f0Skxie@chelsio.com 	unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
22019ba682f0Skxie@chelsio.com 	unsigned int max_def = 512 * MAX_SKB_FRAGS;
22029ba682f0Skxie@chelsio.com 	unsigned int max = max(max_def, headroom);
22039ba682f0Skxie@chelsio.com 
22049ba682f0Skxie@chelsio.com 	max = min(cconn->chba->cdev->tx_max_size, max);
22059ba682f0Skxie@chelsio.com 	if (conn->max_xmit_dlength)
22069ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
22079ba682f0Skxie@chelsio.com 	else
22089ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength = max;
22099ba682f0Skxie@chelsio.com 	cxgbi_align_pdu_size(conn->max_xmit_dlength);
22109ba682f0Skxie@chelsio.com 
22119ba682f0Skxie@chelsio.com 	return 0;
22129ba682f0Skxie@chelsio.com }
22139ba682f0Skxie@chelsio.com 
22149ba682f0Skxie@chelsio.com static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
22159ba682f0Skxie@chelsio.com {
22169ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
22179ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
22189ba682f0Skxie@chelsio.com 	unsigned int max = cconn->chba->cdev->rx_max_size;
22199ba682f0Skxie@chelsio.com 
22209ba682f0Skxie@chelsio.com 	cxgbi_align_pdu_size(max);
22219ba682f0Skxie@chelsio.com 
22229ba682f0Skxie@chelsio.com 	if (conn->max_recv_dlength) {
22239ba682f0Skxie@chelsio.com 		if (conn->max_recv_dlength > max) {
22249ba682f0Skxie@chelsio.com 			pr_err("MaxRecvDataSegmentLength %u > %u.\n",
22259ba682f0Skxie@chelsio.com 				conn->max_recv_dlength, max);
22269ba682f0Skxie@chelsio.com 			return -EINVAL;
22279ba682f0Skxie@chelsio.com 		}
22289ba682f0Skxie@chelsio.com 		conn->max_recv_dlength = min(conn->max_recv_dlength, max);
22299ba682f0Skxie@chelsio.com 		cxgbi_align_pdu_size(conn->max_recv_dlength);
22309ba682f0Skxie@chelsio.com 	} else
22319ba682f0Skxie@chelsio.com 		conn->max_recv_dlength = max;
22329ba682f0Skxie@chelsio.com 
22339ba682f0Skxie@chelsio.com 	return 0;
22349ba682f0Skxie@chelsio.com }
22359ba682f0Skxie@chelsio.com 
22369ba682f0Skxie@chelsio.com int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
22379ba682f0Skxie@chelsio.com 			enum iscsi_param param, char *buf, int buflen)
22389ba682f0Skxie@chelsio.com {
22399ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
22409ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
22419ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
22429ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cconn->cep->csk;
22431304be5fSMike Christie 	int err;
22449ba682f0Skxie@chelsio.com 
22459ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
22469ba682f0Skxie@chelsio.com 		"cls_conn 0x%p, param %d, buf(%d) %s.\n",
22479ba682f0Skxie@chelsio.com 		cls_conn, param, buflen, buf);
22489ba682f0Skxie@chelsio.com 
22499ba682f0Skxie@chelsio.com 	switch (param) {
22509ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_HDRDGST_EN:
22519ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
22529ba682f0Skxie@chelsio.com 		if (!err && conn->hdrdgst_en)
22539ba682f0Skxie@chelsio.com 			err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
22549ba682f0Skxie@chelsio.com 							conn->hdrdgst_en,
22559ba682f0Skxie@chelsio.com 							conn->datadgst_en, 0);
22569ba682f0Skxie@chelsio.com 		break;
22579ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_DATADGST_EN:
22589ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
22599ba682f0Skxie@chelsio.com 		if (!err && conn->datadgst_en)
22609ba682f0Skxie@chelsio.com 			err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
22619ba682f0Skxie@chelsio.com 							conn->hdrdgst_en,
22629ba682f0Skxie@chelsio.com 							conn->datadgst_en, 0);
22639ba682f0Skxie@chelsio.com 		break;
22649ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_R2T:
22651304be5fSMike Christie 		return iscsi_tcp_set_max_r2t(conn, buf);
22669ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
22679ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
22689ba682f0Skxie@chelsio.com 		if (!err)
22699ba682f0Skxie@chelsio.com 			err = cxgbi_conn_max_recv_dlength(conn);
22709ba682f0Skxie@chelsio.com 		break;
22719ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
22729ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
22739ba682f0Skxie@chelsio.com 		if (!err)
22749ba682f0Skxie@chelsio.com 			err = cxgbi_conn_max_xmit_dlength(conn);
22759ba682f0Skxie@chelsio.com 		break;
22769ba682f0Skxie@chelsio.com 	default:
22779ba682f0Skxie@chelsio.com 		return iscsi_set_param(cls_conn, param, buf, buflen);
22789ba682f0Skxie@chelsio.com 	}
22799ba682f0Skxie@chelsio.com 	return err;
22809ba682f0Skxie@chelsio.com }
22819ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
22829ba682f0Skxie@chelsio.com 
2283fc8d0590SAnish Bhatt static inline int csk_print_port(struct cxgbi_sock *csk, char *buf)
2284fc8d0590SAnish Bhatt {
2285fc8d0590SAnish Bhatt 	int len;
2286fc8d0590SAnish Bhatt 
2287fc8d0590SAnish Bhatt 	cxgbi_sock_get(csk);
2288fc8d0590SAnish Bhatt 	len = sprintf(buf, "%hu\n", ntohs(csk->daddr.sin_port));
2289fc8d0590SAnish Bhatt 	cxgbi_sock_put(csk);
2290fc8d0590SAnish Bhatt 
2291fc8d0590SAnish Bhatt 	return len;
2292fc8d0590SAnish Bhatt }
2293fc8d0590SAnish Bhatt 
2294fc8d0590SAnish Bhatt static inline int csk_print_ip(struct cxgbi_sock *csk, char *buf)
2295fc8d0590SAnish Bhatt {
2296fc8d0590SAnish Bhatt 	int len;
2297fc8d0590SAnish Bhatt 
2298fc8d0590SAnish Bhatt 	cxgbi_sock_get(csk);
2299fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
2300fc8d0590SAnish Bhatt 		len = sprintf(buf, "%pI4",
2301fc8d0590SAnish Bhatt 			      &csk->daddr.sin_addr.s_addr);
2302fc8d0590SAnish Bhatt 	else
2303fc8d0590SAnish Bhatt 		len = sprintf(buf, "%pI6",
2304fc8d0590SAnish Bhatt 			      &csk->daddr6.sin6_addr);
2305fc8d0590SAnish Bhatt 
2306fc8d0590SAnish Bhatt 	cxgbi_sock_put(csk);
2307fc8d0590SAnish Bhatt 
2308fc8d0590SAnish Bhatt 	return len;
2309fc8d0590SAnish Bhatt }
2310fc8d0590SAnish Bhatt 
2311c71b9b66SMike Christie int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2312c71b9b66SMike Christie 		       char *buf)
23139ba682f0Skxie@chelsio.com {
2314c71b9b66SMike Christie 	struct cxgbi_endpoint *cep = ep->dd_data;
2315c71b9b66SMike Christie 	struct cxgbi_sock *csk;
23169ba682f0Skxie@chelsio.com 	int len;
23179ba682f0Skxie@chelsio.com 
23189ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
2319c71b9b66SMike Christie 		"cls_conn 0x%p, param %d.\n", ep, param);
23209ba682f0Skxie@chelsio.com 
23219ba682f0Skxie@chelsio.com 	switch (param) {
23229ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_CONN_PORT:
23239ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_CONN_ADDRESS:
2324c71b9b66SMike Christie 		if (!cep)
2325c71b9b66SMike Christie 			return -ENOTCONN;
2326c71b9b66SMike Christie 
2327c71b9b66SMike Christie 		csk = cep->csk;
2328c71b9b66SMike Christie 		if (!csk)
2329c71b9b66SMike Christie 			return -ENOTCONN;
2330c71b9b66SMike Christie 
2331c71b9b66SMike Christie 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2332c71b9b66SMike Christie 						 &csk->daddr, param, buf);
23339ba682f0Skxie@chelsio.com 	default:
2334c71b9b66SMike Christie 		return -ENOSYS;
23359ba682f0Skxie@chelsio.com 	}
23369ba682f0Skxie@chelsio.com 	return len;
23379ba682f0Skxie@chelsio.com }
2338c71b9b66SMike Christie EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
23399ba682f0Skxie@chelsio.com 
23409ba682f0Skxie@chelsio.com struct iscsi_cls_conn *
23419ba682f0Skxie@chelsio.com cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
23429ba682f0Skxie@chelsio.com {
23439ba682f0Skxie@chelsio.com 	struct iscsi_cls_conn *cls_conn;
23449ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn;
23459ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn;
23469ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn;
23479ba682f0Skxie@chelsio.com 
23489ba682f0Skxie@chelsio.com 	cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
23499ba682f0Skxie@chelsio.com 	if (!cls_conn)
23509ba682f0Skxie@chelsio.com 		return NULL;
23519ba682f0Skxie@chelsio.com 
23529ba682f0Skxie@chelsio.com 	conn = cls_conn->dd_data;
23539ba682f0Skxie@chelsio.com 	tcp_conn = conn->dd_data;
23549ba682f0Skxie@chelsio.com 	cconn = tcp_conn->dd_data;
23559ba682f0Skxie@chelsio.com 	cconn->iconn = conn;
23569ba682f0Skxie@chelsio.com 
23579ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
23589ba682f0Skxie@chelsio.com 		"cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
23599ba682f0Skxie@chelsio.com 		cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
23609ba682f0Skxie@chelsio.com 
23619ba682f0Skxie@chelsio.com 	return cls_conn;
23629ba682f0Skxie@chelsio.com }
23639ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_create_conn);
23649ba682f0Skxie@chelsio.com 
23659ba682f0Skxie@chelsio.com int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
23669ba682f0Skxie@chelsio.com 				struct iscsi_cls_conn *cls_conn,
23679ba682f0Skxie@chelsio.com 				u64 transport_eph, int is_leading)
23689ba682f0Skxie@chelsio.com {
23699ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
23709ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
23719ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
237271f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm;
23739ba682f0Skxie@chelsio.com 	struct iscsi_endpoint *ep;
23749ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
23759ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
23769ba682f0Skxie@chelsio.com 	int err;
23779ba682f0Skxie@chelsio.com 
23789ba682f0Skxie@chelsio.com 	ep = iscsi_lookup_endpoint(transport_eph);
23799ba682f0Skxie@chelsio.com 	if (!ep)
23809ba682f0Skxie@chelsio.com 		return -EINVAL;
23819ba682f0Skxie@chelsio.com 
23829ba682f0Skxie@chelsio.com 	/*  setup ddp pagesize */
23839ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
23849ba682f0Skxie@chelsio.com 	csk = cep->csk;
23859ba682f0Skxie@chelsio.com 
238671f7a00bSVarun Prakash 	ppm = csk->cdev->cdev2ppm(csk->cdev);
238771f7a00bSVarun Prakash 	err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid,
238871f7a00bSVarun Prakash 					     ppm->tformat.pgsz_idx_dflt, 0);
238971f7a00bSVarun Prakash 	if (err < 0)
239071f7a00bSVarun Prakash 		return err;
239171f7a00bSVarun Prakash 
23929ba682f0Skxie@chelsio.com 	err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
23939ba682f0Skxie@chelsio.com 	if (err)
23949ba682f0Skxie@chelsio.com 		return -EINVAL;
23959ba682f0Skxie@chelsio.com 
23969ba682f0Skxie@chelsio.com 	/*  calculate the tag idx bits needed for this conn based on cmds_max */
23979ba682f0Skxie@chelsio.com 	cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
23989ba682f0Skxie@chelsio.com 
2399e3d2ad8cSkxie@chelsio.com 	write_lock_bh(&csk->callback_lock);
24009ba682f0Skxie@chelsio.com 	csk->user_data = conn;
24019ba682f0Skxie@chelsio.com 	cconn->chba = cep->chba;
24029ba682f0Skxie@chelsio.com 	cconn->cep = cep;
24039ba682f0Skxie@chelsio.com 	cep->cconn = cconn;
2404e3d2ad8cSkxie@chelsio.com 	write_unlock_bh(&csk->callback_lock);
24059ba682f0Skxie@chelsio.com 
24069ba682f0Skxie@chelsio.com 	cxgbi_conn_max_xmit_dlength(conn);
24079ba682f0Skxie@chelsio.com 	cxgbi_conn_max_recv_dlength(conn);
24089ba682f0Skxie@chelsio.com 
24099ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24109ba682f0Skxie@chelsio.com 		"cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
24119ba682f0Skxie@chelsio.com 		cls_session, cls_conn, ep, cconn, csk);
24129ba682f0Skxie@chelsio.com 	/*  init recv engine */
24139ba682f0Skxie@chelsio.com 	iscsi_tcp_hdr_recv_prep(tcp_conn);
24149ba682f0Skxie@chelsio.com 
24159ba682f0Skxie@chelsio.com 	return 0;
24169ba682f0Skxie@chelsio.com }
24179ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
24189ba682f0Skxie@chelsio.com 
24199ba682f0Skxie@chelsio.com struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
24209ba682f0Skxie@chelsio.com 						u16 cmds_max, u16 qdepth,
24219ba682f0Skxie@chelsio.com 						u32 initial_cmdsn)
24229ba682f0Skxie@chelsio.com {
24239ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
24249ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
24259ba682f0Skxie@chelsio.com 	struct Scsi_Host *shost;
24269ba682f0Skxie@chelsio.com 	struct iscsi_cls_session *cls_session;
24279ba682f0Skxie@chelsio.com 	struct iscsi_session *session;
24289ba682f0Skxie@chelsio.com 
24299ba682f0Skxie@chelsio.com 	if (!ep) {
24309ba682f0Skxie@chelsio.com 		pr_err("missing endpoint.\n");
24319ba682f0Skxie@chelsio.com 		return NULL;
24329ba682f0Skxie@chelsio.com 	}
24339ba682f0Skxie@chelsio.com 
24349ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
24359ba682f0Skxie@chelsio.com 	chba = cep->chba;
24369ba682f0Skxie@chelsio.com 	shost = chba->shost;
24379ba682f0Skxie@chelsio.com 
24389ba682f0Skxie@chelsio.com 	BUG_ON(chba != iscsi_host_priv(shost));
24399ba682f0Skxie@chelsio.com 
24409ba682f0Skxie@chelsio.com 	cls_session = iscsi_session_setup(chba->cdev->itp, shost,
24419ba682f0Skxie@chelsio.com 					cmds_max, 0,
24429ba682f0Skxie@chelsio.com 					sizeof(struct iscsi_tcp_task) +
24439ba682f0Skxie@chelsio.com 					sizeof(struct cxgbi_task_data),
24449ba682f0Skxie@chelsio.com 					initial_cmdsn, ISCSI_MAX_TARGET);
24459ba682f0Skxie@chelsio.com 	if (!cls_session)
24469ba682f0Skxie@chelsio.com 		return NULL;
24479ba682f0Skxie@chelsio.com 
24489ba682f0Skxie@chelsio.com 	session = cls_session->dd_data;
24499ba682f0Skxie@chelsio.com 	if (iscsi_tcp_r2tpool_alloc(session))
24509ba682f0Skxie@chelsio.com 		goto remove_session;
24519ba682f0Skxie@chelsio.com 
24529ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24539ba682f0Skxie@chelsio.com 		"ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
24549ba682f0Skxie@chelsio.com 	return cls_session;
24559ba682f0Skxie@chelsio.com 
24569ba682f0Skxie@chelsio.com remove_session:
24579ba682f0Skxie@chelsio.com 	iscsi_session_teardown(cls_session);
24589ba682f0Skxie@chelsio.com 	return NULL;
24599ba682f0Skxie@chelsio.com }
24609ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_create_session);
24619ba682f0Skxie@chelsio.com 
24629ba682f0Skxie@chelsio.com void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
24639ba682f0Skxie@chelsio.com {
24649ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24659ba682f0Skxie@chelsio.com 		"cls sess 0x%p.\n", cls_session);
24669ba682f0Skxie@chelsio.com 
24679ba682f0Skxie@chelsio.com 	iscsi_tcp_r2tpool_free(cls_session->dd_data);
24689ba682f0Skxie@chelsio.com 	iscsi_session_teardown(cls_session);
24699ba682f0Skxie@chelsio.com }
24709ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
24719ba682f0Skxie@chelsio.com 
24729ba682f0Skxie@chelsio.com int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
24739ba682f0Skxie@chelsio.com 			char *buf, int buflen)
24749ba682f0Skxie@chelsio.com {
24759ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = iscsi_host_priv(shost);
24769ba682f0Skxie@chelsio.com 
24779ba682f0Skxie@chelsio.com 	if (!chba->ndev) {
24789ba682f0Skxie@chelsio.com 		shost_printk(KERN_ERR, shost, "Could not get host param. "
24799ba682f0Skxie@chelsio.com 				"netdev for host not set.\n");
24809ba682f0Skxie@chelsio.com 		return -ENODEV;
24819ba682f0Skxie@chelsio.com 	}
24829ba682f0Skxie@chelsio.com 
24839ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24849ba682f0Skxie@chelsio.com 		"shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
24859ba682f0Skxie@chelsio.com 		shost, chba, chba->ndev->name, param, buflen, buf);
24869ba682f0Skxie@chelsio.com 
24879ba682f0Skxie@chelsio.com 	switch (param) {
24889ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_IPADDRESS:
24899ba682f0Skxie@chelsio.com 	{
24909ba682f0Skxie@chelsio.com 		__be32 addr = in_aton(buf);
24919ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
24929ba682f0Skxie@chelsio.com 			"hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
24939ba682f0Skxie@chelsio.com 		cxgbi_set_iscsi_ipv4(chba, addr);
24949ba682f0Skxie@chelsio.com 		return 0;
24959ba682f0Skxie@chelsio.com 	}
24969ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_HWADDRESS:
24979ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_NETDEV_NAME:
24989ba682f0Skxie@chelsio.com 		return 0;
24999ba682f0Skxie@chelsio.com 	default:
25009ba682f0Skxie@chelsio.com 		return iscsi_host_set_param(shost, param, buf, buflen);
25019ba682f0Skxie@chelsio.com 	}
25029ba682f0Skxie@chelsio.com }
25039ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
25049ba682f0Skxie@chelsio.com 
25059ba682f0Skxie@chelsio.com int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
25069ba682f0Skxie@chelsio.com 			char *buf)
25079ba682f0Skxie@chelsio.com {
25089ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = iscsi_host_priv(shost);
25099ba682f0Skxie@chelsio.com 	int len = 0;
25109ba682f0Skxie@chelsio.com 
25119ba682f0Skxie@chelsio.com 	if (!chba->ndev) {
25129ba682f0Skxie@chelsio.com 		shost_printk(KERN_ERR, shost, "Could not get host param. "
25139ba682f0Skxie@chelsio.com 				"netdev for host not set.\n");
25149ba682f0Skxie@chelsio.com 		return -ENODEV;
25159ba682f0Skxie@chelsio.com 	}
25169ba682f0Skxie@chelsio.com 
25179ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
25189ba682f0Skxie@chelsio.com 		"shost 0x%p, hba 0x%p,%s, param %d.\n",
25199ba682f0Skxie@chelsio.com 		shost, chba, chba->ndev->name, param);
25209ba682f0Skxie@chelsio.com 
25219ba682f0Skxie@chelsio.com 	switch (param) {
25229ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_HWADDRESS:
25239ba682f0Skxie@chelsio.com 		len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
25249ba682f0Skxie@chelsio.com 		break;
25259ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_NETDEV_NAME:
25269ba682f0Skxie@chelsio.com 		len = sprintf(buf, "%s\n", chba->ndev->name);
25279ba682f0Skxie@chelsio.com 		break;
25289ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_IPADDRESS:
25299ba682f0Skxie@chelsio.com 	{
2530dd9ad67eSAnish Bhatt 		struct cxgbi_sock *csk = find_sock_on_port(chba->cdev,
2531dd9ad67eSAnish Bhatt 							   chba->port_id);
2532dd9ad67eSAnish Bhatt 		if (csk) {
2533dd9ad67eSAnish Bhatt 			len = sprintf(buf, "%pIS",
2534dd9ad67eSAnish Bhatt 				      (struct sockaddr *)&csk->saddr);
2535dd9ad67eSAnish Bhatt 		}
25369ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
2537dd9ad67eSAnish Bhatt 			  "hba %s, addr %s.\n", chba->ndev->name, buf);
25389ba682f0Skxie@chelsio.com 		break;
25399ba682f0Skxie@chelsio.com 	}
25409ba682f0Skxie@chelsio.com 	default:
25419ba682f0Skxie@chelsio.com 		return iscsi_host_get_param(shost, param, buf);
25429ba682f0Skxie@chelsio.com 	}
25439ba682f0Skxie@chelsio.com 
25449ba682f0Skxie@chelsio.com 	return len;
25459ba682f0Skxie@chelsio.com }
25469ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
25479ba682f0Skxie@chelsio.com 
25489ba682f0Skxie@chelsio.com struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
25499ba682f0Skxie@chelsio.com 					struct sockaddr *dst_addr,
25509ba682f0Skxie@chelsio.com 					int non_blocking)
25519ba682f0Skxie@chelsio.com {
25529ba682f0Skxie@chelsio.com 	struct iscsi_endpoint *ep;
25539ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
25549ba682f0Skxie@chelsio.com 	struct cxgbi_hba *hba = NULL;
25559ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
25564737c5a0SVarun Prakash 	int ifindex = 0;
25579ba682f0Skxie@chelsio.com 	int err = -EINVAL;
25589ba682f0Skxie@chelsio.com 
25599ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
25609ba682f0Skxie@chelsio.com 		"shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
25619ba682f0Skxie@chelsio.com 		shost, non_blocking, dst_addr);
25629ba682f0Skxie@chelsio.com 
25639ba682f0Skxie@chelsio.com 	if (shost) {
25649ba682f0Skxie@chelsio.com 		hba = iscsi_host_priv(shost);
25659ba682f0Skxie@chelsio.com 		if (!hba) {
25669ba682f0Skxie@chelsio.com 			pr_info("shost 0x%p, priv NULL.\n", shost);
25679ba682f0Skxie@chelsio.com 			goto err_out;
25689ba682f0Skxie@chelsio.com 		}
25694737c5a0SVarun Prakash 
25704c2a0215SVarun Prakash 		rtnl_lock();
25714c2a0215SVarun Prakash 		if (!vlan_uses_dev(hba->ndev))
25724737c5a0SVarun Prakash 			ifindex = hba->ndev->ifindex;
25734c2a0215SVarun Prakash 		rtnl_unlock();
25749ba682f0Skxie@chelsio.com 	}
25759ba682f0Skxie@chelsio.com 
2576fc8d0590SAnish Bhatt 	if (dst_addr->sa_family == AF_INET) {
25774737c5a0SVarun Prakash 		csk = cxgbi_check_route(dst_addr, ifindex);
2578e81fbf6cSAnish Bhatt #if IS_ENABLED(CONFIG_IPV6)
2579fc8d0590SAnish Bhatt 	} else if (dst_addr->sa_family == AF_INET6) {
25804737c5a0SVarun Prakash 		csk = cxgbi_check_route6(dst_addr, ifindex);
2581e81fbf6cSAnish Bhatt #endif
2582fc8d0590SAnish Bhatt 	} else {
2583fc8d0590SAnish Bhatt 		pr_info("address family 0x%x NOT supported.\n",
2584fc8d0590SAnish Bhatt 			dst_addr->sa_family);
2585fc8d0590SAnish Bhatt 		err = -EAFNOSUPPORT;
2586fc8d0590SAnish Bhatt 		return (struct iscsi_endpoint *)ERR_PTR(err);
2587fc8d0590SAnish Bhatt 	}
2588fc8d0590SAnish Bhatt 
25899ba682f0Skxie@chelsio.com 	if (IS_ERR(csk))
25909ba682f0Skxie@chelsio.com 		return (struct iscsi_endpoint *)csk;
25919ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
25929ba682f0Skxie@chelsio.com 
25939ba682f0Skxie@chelsio.com 	if (!hba)
25949ba682f0Skxie@chelsio.com 		hba = csk->cdev->hbas[csk->port_id];
25959ba682f0Skxie@chelsio.com 	else if (hba != csk->cdev->hbas[csk->port_id]) {
25969ba682f0Skxie@chelsio.com 		pr_info("Could not connect through requested host %u"
25979ba682f0Skxie@chelsio.com 			"hba 0x%p != 0x%p (%u).\n",
25989ba682f0Skxie@chelsio.com 			shost->host_no, hba,
25999ba682f0Skxie@chelsio.com 			csk->cdev->hbas[csk->port_id], csk->port_id);
26009ba682f0Skxie@chelsio.com 		err = -ENOSPC;
26019ba682f0Skxie@chelsio.com 		goto release_conn;
26029ba682f0Skxie@chelsio.com 	}
26039ba682f0Skxie@chelsio.com 
26049ba682f0Skxie@chelsio.com 	err = sock_get_port(csk);
26059ba682f0Skxie@chelsio.com 	if (err)
26069ba682f0Skxie@chelsio.com 		goto release_conn;
26079ba682f0Skxie@chelsio.com 
26089ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CONNECTING);
26099ba682f0Skxie@chelsio.com 	err = csk->cdev->csk_init_act_open(csk);
26109ba682f0Skxie@chelsio.com 	if (err)
26119ba682f0Skxie@chelsio.com 		goto release_conn;
26129ba682f0Skxie@chelsio.com 
26139ba682f0Skxie@chelsio.com 	if (cxgbi_sock_is_closing(csk)) {
26149ba682f0Skxie@chelsio.com 		err = -ENOSPC;
26159ba682f0Skxie@chelsio.com 		pr_info("csk 0x%p is closing.\n", csk);
26169ba682f0Skxie@chelsio.com 		goto release_conn;
26179ba682f0Skxie@chelsio.com 	}
26189ba682f0Skxie@chelsio.com 
26199ba682f0Skxie@chelsio.com 	ep = iscsi_create_endpoint(sizeof(*cep));
26209ba682f0Skxie@chelsio.com 	if (!ep) {
26219ba682f0Skxie@chelsio.com 		err = -ENOMEM;
26229ba682f0Skxie@chelsio.com 		pr_info("iscsi alloc ep, OOM.\n");
26239ba682f0Skxie@chelsio.com 		goto release_conn;
26249ba682f0Skxie@chelsio.com 	}
26259ba682f0Skxie@chelsio.com 
26269ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
26279ba682f0Skxie@chelsio.com 	cep->csk = csk;
26289ba682f0Skxie@chelsio.com 	cep->chba = hba;
26299ba682f0Skxie@chelsio.com 
26309ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
26319ba682f0Skxie@chelsio.com 		"ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
26329ba682f0Skxie@chelsio.com 		ep, cep, csk, hba, hba->ndev->name);
26339ba682f0Skxie@chelsio.com 	return ep;
26349ba682f0Skxie@chelsio.com 
26359ba682f0Skxie@chelsio.com release_conn:
26369ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
26379ba682f0Skxie@chelsio.com 	cxgbi_sock_closed(csk);
26389ba682f0Skxie@chelsio.com err_out:
26399ba682f0Skxie@chelsio.com 	return ERR_PTR(err);
26409ba682f0Skxie@chelsio.com }
26419ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
26429ba682f0Skxie@chelsio.com 
26439ba682f0Skxie@chelsio.com int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
26449ba682f0Skxie@chelsio.com {
26459ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep = ep->dd_data;
26469ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cep->csk;
26479ba682f0Skxie@chelsio.com 
26489ba682f0Skxie@chelsio.com 	if (!cxgbi_sock_is_established(csk))
26499ba682f0Skxie@chelsio.com 		return 0;
26509ba682f0Skxie@chelsio.com 	return 1;
26519ba682f0Skxie@chelsio.com }
26529ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
26539ba682f0Skxie@chelsio.com 
26549ba682f0Skxie@chelsio.com void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
26559ba682f0Skxie@chelsio.com {
26569ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep = ep->dd_data;
26579ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = cep->cconn;
26589ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cep->csk;
26599ba682f0Skxie@chelsio.com 
26609ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
26619ba682f0Skxie@chelsio.com 		"ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
26629ba682f0Skxie@chelsio.com 		ep, cep, cconn, csk, csk->state, csk->flags);
26639ba682f0Skxie@chelsio.com 
26649ba682f0Skxie@chelsio.com 	if (cconn && cconn->iconn) {
26659ba682f0Skxie@chelsio.com 		iscsi_suspend_tx(cconn->iconn);
26669ba682f0Skxie@chelsio.com 		write_lock_bh(&csk->callback_lock);
26679ba682f0Skxie@chelsio.com 		cep->csk->user_data = NULL;
26689ba682f0Skxie@chelsio.com 		cconn->cep = NULL;
26699ba682f0Skxie@chelsio.com 		write_unlock_bh(&csk->callback_lock);
26709ba682f0Skxie@chelsio.com 	}
26719ba682f0Skxie@chelsio.com 	iscsi_destroy_endpoint(ep);
26729ba682f0Skxie@chelsio.com 
26739ba682f0Skxie@chelsio.com 	if (likely(csk->state >= CTP_ESTABLISHED))
26749ba682f0Skxie@chelsio.com 		need_active_close(csk);
26759ba682f0Skxie@chelsio.com 	else
26769ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
26779ba682f0Skxie@chelsio.com 
26789ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
26799ba682f0Skxie@chelsio.com }
26809ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
26819ba682f0Skxie@chelsio.com 
26829ba682f0Skxie@chelsio.com int cxgbi_iscsi_init(struct iscsi_transport *itp,
26839ba682f0Skxie@chelsio.com 			struct scsi_transport_template **stt)
26849ba682f0Skxie@chelsio.com {
26859ba682f0Skxie@chelsio.com 	*stt = iscsi_register_transport(itp);
26869ba682f0Skxie@chelsio.com 	if (*stt == NULL) {
26879ba682f0Skxie@chelsio.com 		pr_err("unable to register %s transport 0x%p.\n",
26889ba682f0Skxie@chelsio.com 			itp->name, itp);
26899ba682f0Skxie@chelsio.com 		return -ENODEV;
26909ba682f0Skxie@chelsio.com 	}
26919ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
26929ba682f0Skxie@chelsio.com 		"%s, registered iscsi transport 0x%p.\n",
26939ba682f0Skxie@chelsio.com 		itp->name, stt);
26949ba682f0Skxie@chelsio.com 	return 0;
26959ba682f0Skxie@chelsio.com }
26969ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
26979ba682f0Skxie@chelsio.com 
26989ba682f0Skxie@chelsio.com void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
26999ba682f0Skxie@chelsio.com 			struct scsi_transport_template **stt)
27009ba682f0Skxie@chelsio.com {
27019ba682f0Skxie@chelsio.com 	if (*stt) {
27029ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
27039ba682f0Skxie@chelsio.com 			"de-register transport 0x%p, %s, stt 0x%p.\n",
27049ba682f0Skxie@chelsio.com 			itp, itp->name, *stt);
27059ba682f0Skxie@chelsio.com 		*stt = NULL;
27069ba682f0Skxie@chelsio.com 		iscsi_unregister_transport(itp);
27079ba682f0Skxie@chelsio.com 	}
27089ba682f0Skxie@chelsio.com }
27099ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
27109ba682f0Skxie@chelsio.com 
2711587a1f16SAl Viro umode_t cxgbi_attr_is_visible(int param_type, int param)
27123128c6c7SMike Christie {
27133128c6c7SMike Christie 	switch (param_type) {
2714f27fb2efSMike Christie 	case ISCSI_HOST_PARAM:
2715f27fb2efSMike Christie 		switch (param) {
2716f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_NETDEV_NAME:
2717f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_HWADDRESS:
2718f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_IPADDRESS:
2719f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
2720f27fb2efSMike Christie 			return S_IRUGO;
2721f27fb2efSMike Christie 		default:
2722f27fb2efSMike Christie 			return 0;
2723f27fb2efSMike Christie 		}
27243128c6c7SMike Christie 	case ISCSI_PARAM:
27253128c6c7SMike Christie 		switch (param) {
27263128c6c7SMike Christie 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
27273128c6c7SMike Christie 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
27283128c6c7SMike Christie 		case ISCSI_PARAM_HDRDGST_EN:
27293128c6c7SMike Christie 		case ISCSI_PARAM_DATADGST_EN:
27303128c6c7SMike Christie 		case ISCSI_PARAM_CONN_ADDRESS:
27313128c6c7SMike Christie 		case ISCSI_PARAM_CONN_PORT:
27323128c6c7SMike Christie 		case ISCSI_PARAM_EXP_STATSN:
27333128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
27343128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_PORT:
27353128c6c7SMike Christie 		case ISCSI_PARAM_PING_TMO:
27363128c6c7SMike Christie 		case ISCSI_PARAM_RECV_TMO:
27371d063c17SMike Christie 		case ISCSI_PARAM_INITIAL_R2T_EN:
27381d063c17SMike Christie 		case ISCSI_PARAM_MAX_R2T:
27391d063c17SMike Christie 		case ISCSI_PARAM_IMM_DATA_EN:
27401d063c17SMike Christie 		case ISCSI_PARAM_FIRST_BURST:
27411d063c17SMike Christie 		case ISCSI_PARAM_MAX_BURST:
27421d063c17SMike Christie 		case ISCSI_PARAM_PDU_INORDER_EN:
27431d063c17SMike Christie 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
27441d063c17SMike Christie 		case ISCSI_PARAM_ERL:
27451d063c17SMike Christie 		case ISCSI_PARAM_TARGET_NAME:
27461d063c17SMike Christie 		case ISCSI_PARAM_TPGT:
27471d063c17SMike Christie 		case ISCSI_PARAM_USERNAME:
27481d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD:
27491d063c17SMike Christie 		case ISCSI_PARAM_USERNAME_IN:
27501d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD_IN:
27511d063c17SMike Christie 		case ISCSI_PARAM_FAST_ABORT:
27521d063c17SMike Christie 		case ISCSI_PARAM_ABORT_TMO:
27531d063c17SMike Christie 		case ISCSI_PARAM_LU_RESET_TMO:
27541d063c17SMike Christie 		case ISCSI_PARAM_TGT_RESET_TMO:
27551d063c17SMike Christie 		case ISCSI_PARAM_IFACE_NAME:
27561d063c17SMike Christie 		case ISCSI_PARAM_INITIATOR_NAME:
27573128c6c7SMike Christie 			return S_IRUGO;
27583128c6c7SMike Christie 		default:
27593128c6c7SMike Christie 			return 0;
27603128c6c7SMike Christie 		}
27613128c6c7SMike Christie 	}
27623128c6c7SMike Christie 
27633128c6c7SMike Christie 	return 0;
27643128c6c7SMike Christie }
27653128c6c7SMike Christie EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
27663128c6c7SMike Christie 
27679ba682f0Skxie@chelsio.com static int __init libcxgbi_init_module(void)
27689ba682f0Skxie@chelsio.com {
27690ea5bf3dSKaren Xie 	pr_info("%s", version);
277075b61250SVarun Prakash 
277175b61250SVarun Prakash 	BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, cb) <
277275b61250SVarun Prakash 		     sizeof(struct cxgbi_skb_cb));
27739ba682f0Skxie@chelsio.com 	return 0;
27749ba682f0Skxie@chelsio.com }
27759ba682f0Skxie@chelsio.com 
27769ba682f0Skxie@chelsio.com static void __exit libcxgbi_exit_module(void)
27779ba682f0Skxie@chelsio.com {
27789ba682f0Skxie@chelsio.com 	cxgbi_device_unregister_all(0xFF);
27799ba682f0Skxie@chelsio.com 	return;
27809ba682f0Skxie@chelsio.com }
27819ba682f0Skxie@chelsio.com 
27829ba682f0Skxie@chelsio.com module_init(libcxgbi_init_module);
27839ba682f0Skxie@chelsio.com module_exit(libcxgbi_exit_module);
2784