xref: /openbmc/linux/drivers/scsi/cxgbi/libcxgbi.c (revision 3948b059)
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 
cxgbi_decode_sw_tag(u32 sw_tag,int * idx,int * age)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 
cxgbi_device_portmap_create(struct cxgbi_device * cdev,unsigned int base,unsigned int max_conn)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 
80ee9108feSDenis Efremov 	pmap->port_csk = kvzalloc(array_size(max_conn,
81ee9108feSDenis Efremov 					     sizeof(struct cxgbi_sock *)),
82ee9108feSDenis Efremov 				  GFP_KERNEL | __GFP_NOWARN);
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 
cxgbi_device_portmap_cleanup(struct cxgbi_device * cdev)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 
cxgbi_device_destroy(struct cxgbi_device * cdev)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);
12471482fdeSVarun Prakash 	if (cdev->cdev2ppm)
1259d5c44b7SVarun Prakash 		cxgbi_ppm_release(cdev->cdev2ppm(cdev));
1269ba682f0Skxie@chelsio.com 	if (cdev->pmap.max_connect)
127ee9108feSDenis Efremov 		kvfree(cdev->pmap.port_csk);
1289ba682f0Skxie@chelsio.com 	kfree(cdev);
1299ba682f0Skxie@chelsio.com }
1309ba682f0Skxie@chelsio.com 
cxgbi_device_register(unsigned int extra,unsigned int nports)1319ba682f0Skxie@chelsio.com struct cxgbi_device *cxgbi_device_register(unsigned int extra,
1329ba682f0Skxie@chelsio.com 					   unsigned int nports)
1339ba682f0Skxie@chelsio.com {
1349ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev;
1359ba682f0Skxie@chelsio.com 
1369ba682f0Skxie@chelsio.com 	cdev = kzalloc(sizeof(*cdev) + extra + nports *
1379ba682f0Skxie@chelsio.com 			(sizeof(struct cxgbi_hba *) +
1389ba682f0Skxie@chelsio.com 			 sizeof(struct net_device *)),
1399ba682f0Skxie@chelsio.com 			GFP_KERNEL);
1409ba682f0Skxie@chelsio.com 	if (!cdev) {
1419ba682f0Skxie@chelsio.com 		pr_warn("nport %d, OOM.\n", nports);
1429ba682f0Skxie@chelsio.com 		return NULL;
1439ba682f0Skxie@chelsio.com 	}
1449ba682f0Skxie@chelsio.com 	cdev->ports = (struct net_device **)(cdev + 1);
1459ba682f0Skxie@chelsio.com 	cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
1469ba682f0Skxie@chelsio.com 						sizeof(struct net_device *));
1479ba682f0Skxie@chelsio.com 	if (extra)
1489ba682f0Skxie@chelsio.com 		cdev->dd_data = ((char *)cdev->hbas) +
1499ba682f0Skxie@chelsio.com 				nports * sizeof(struct cxgbi_hba *);
1509ba682f0Skxie@chelsio.com 	spin_lock_init(&cdev->pmap.lock);
1519ba682f0Skxie@chelsio.com 
1529ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1539ba682f0Skxie@chelsio.com 	list_add_tail(&cdev->list_head, &cdev_list);
1549ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1559ba682f0Skxie@chelsio.com 
156576b5863SAnish Bhatt 	spin_lock(&cdev_rcu_lock);
157576b5863SAnish Bhatt 	list_add_tail_rcu(&cdev->rcu_node, &cdev_rcu_list);
158576b5863SAnish Bhatt 	spin_unlock(&cdev_rcu_lock);
159576b5863SAnish Bhatt 
1609ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1619ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u.\n", cdev, nports);
1629ba682f0Skxie@chelsio.com 	return cdev;
1639ba682f0Skxie@chelsio.com }
1649ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_register);
1659ba682f0Skxie@chelsio.com 
cxgbi_device_unregister(struct cxgbi_device * cdev)1669ba682f0Skxie@chelsio.com void cxgbi_device_unregister(struct cxgbi_device *cdev)
1679ba682f0Skxie@chelsio.com {
1689ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
1699ba682f0Skxie@chelsio.com 		"cdev 0x%p, p# %u,%s.\n",
1709ba682f0Skxie@chelsio.com 		cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
171576b5863SAnish Bhatt 
1729ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1739ba682f0Skxie@chelsio.com 	list_del(&cdev->list_head);
1749ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
175576b5863SAnish Bhatt 
176576b5863SAnish Bhatt 	spin_lock(&cdev_rcu_lock);
177576b5863SAnish Bhatt 	list_del_rcu(&cdev->rcu_node);
178576b5863SAnish Bhatt 	spin_unlock(&cdev_rcu_lock);
179576b5863SAnish Bhatt 	synchronize_rcu();
180576b5863SAnish Bhatt 
1819ba682f0Skxie@chelsio.com 	cxgbi_device_destroy(cdev);
1829ba682f0Skxie@chelsio.com }
1839ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
1849ba682f0Skxie@chelsio.com 
cxgbi_device_unregister_all(unsigned int flag)1859ba682f0Skxie@chelsio.com void cxgbi_device_unregister_all(unsigned int flag)
1869ba682f0Skxie@chelsio.com {
1879ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
1889ba682f0Skxie@chelsio.com 
1899ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
1909ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
1919ba682f0Skxie@chelsio.com 		if ((cdev->flags & flag) == flag) {
192576b5863SAnish Bhatt 			mutex_unlock(&cdev_mutex);
193576b5863SAnish Bhatt 			cxgbi_device_unregister(cdev);
194576b5863SAnish Bhatt 			mutex_lock(&cdev_mutex);
1959ba682f0Skxie@chelsio.com 		}
1969ba682f0Skxie@chelsio.com 	}
1979ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
1989ba682f0Skxie@chelsio.com }
1999ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
2009ba682f0Skxie@chelsio.com 
cxgbi_device_find_by_lldev(void * lldev)2019ba682f0Skxie@chelsio.com struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
2029ba682f0Skxie@chelsio.com {
2039ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
2049ba682f0Skxie@chelsio.com 
2059ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
2069ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
2079ba682f0Skxie@chelsio.com 		if (cdev->lldev == lldev) {
2089ba682f0Skxie@chelsio.com 			mutex_unlock(&cdev_mutex);
2099ba682f0Skxie@chelsio.com 			return cdev;
2109ba682f0Skxie@chelsio.com 		}
2119ba682f0Skxie@chelsio.com 	}
2129ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
213576b5863SAnish Bhatt 
2149ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
2159ba682f0Skxie@chelsio.com 		"lldev 0x%p, NO match found.\n", lldev);
2169ba682f0Skxie@chelsio.com 	return NULL;
2179ba682f0Skxie@chelsio.com }
2189ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
2199ba682f0Skxie@chelsio.com 
cxgbi_device_find_by_netdev(struct net_device * ndev,int * port)220fc8d0590SAnish Bhatt struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
2219ba682f0Skxie@chelsio.com 						 int *port)
2229ba682f0Skxie@chelsio.com {
2230b3d8947Skxie@chelsio.com 	struct net_device *vdev = NULL;
2249ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev, *tmp;
2259ba682f0Skxie@chelsio.com 	int i;
2269ba682f0Skxie@chelsio.com 
227d0d7b10bSParav Pandit 	if (is_vlan_dev(ndev)) {
2280b3d8947Skxie@chelsio.com 		vdev = ndev;
2299ba682f0Skxie@chelsio.com 		ndev = vlan_dev_real_dev(ndev);
2300b3d8947Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
2310b3d8947Skxie@chelsio.com 			"vlan dev %s -> %s.\n", vdev->name, ndev->name);
2320b3d8947Skxie@chelsio.com 	}
2339ba682f0Skxie@chelsio.com 
2349ba682f0Skxie@chelsio.com 	mutex_lock(&cdev_mutex);
2359ba682f0Skxie@chelsio.com 	list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
2369ba682f0Skxie@chelsio.com 		for (i = 0; i < cdev->nports; i++) {
2379ba682f0Skxie@chelsio.com 			if (ndev == cdev->ports[i]) {
2380b3d8947Skxie@chelsio.com 				cdev->hbas[i]->vdev = vdev;
2399ba682f0Skxie@chelsio.com 				mutex_unlock(&cdev_mutex);
2409ba682f0Skxie@chelsio.com 				if (port)
2419ba682f0Skxie@chelsio.com 					*port = i;
2429ba682f0Skxie@chelsio.com 				return cdev;
2439ba682f0Skxie@chelsio.com 			}
2449ba682f0Skxie@chelsio.com 		}
2459ba682f0Skxie@chelsio.com 	}
2469ba682f0Skxie@chelsio.com 	mutex_unlock(&cdev_mutex);
2479ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
2489ba682f0Skxie@chelsio.com 		"ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
2499ba682f0Skxie@chelsio.com 	return NULL;
2509ba682f0Skxie@chelsio.com }
251fc8d0590SAnish Bhatt EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev);
252fc8d0590SAnish Bhatt 
cxgbi_device_find_by_netdev_rcu(struct net_device * ndev,int * port)253576b5863SAnish Bhatt struct cxgbi_device *cxgbi_device_find_by_netdev_rcu(struct net_device *ndev,
254576b5863SAnish Bhatt 						     int *port)
255576b5863SAnish Bhatt {
256576b5863SAnish Bhatt 	struct net_device *vdev = NULL;
257576b5863SAnish Bhatt 	struct cxgbi_device *cdev;
258576b5863SAnish Bhatt 	int i;
259576b5863SAnish Bhatt 
260d0d7b10bSParav Pandit 	if (is_vlan_dev(ndev)) {
261576b5863SAnish Bhatt 		vdev = ndev;
262576b5863SAnish Bhatt 		ndev = vlan_dev_real_dev(ndev);
263576b5863SAnish Bhatt 		pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
264576b5863SAnish Bhatt 	}
265576b5863SAnish Bhatt 
266576b5863SAnish Bhatt 	rcu_read_lock();
267576b5863SAnish Bhatt 	list_for_each_entry_rcu(cdev, &cdev_rcu_list, rcu_node) {
268576b5863SAnish Bhatt 		for (i = 0; i < cdev->nports; i++) {
269576b5863SAnish Bhatt 			if (ndev == cdev->ports[i]) {
270576b5863SAnish Bhatt 				cdev->hbas[i]->vdev = vdev;
271576b5863SAnish Bhatt 				rcu_read_unlock();
272576b5863SAnish Bhatt 				if (port)
273576b5863SAnish Bhatt 					*port = i;
274576b5863SAnish Bhatt 				return cdev;
275576b5863SAnish Bhatt 			}
276576b5863SAnish Bhatt 		}
277576b5863SAnish Bhatt 	}
278576b5863SAnish Bhatt 	rcu_read_unlock();
279576b5863SAnish Bhatt 
280576b5863SAnish Bhatt 	log_debug(1 << CXGBI_DBG_DEV,
281576b5863SAnish Bhatt 		  "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
282576b5863SAnish Bhatt 	return NULL;
283576b5863SAnish Bhatt }
284576b5863SAnish Bhatt EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev_rcu);
285576b5863SAnish Bhatt 
cxgbi_device_find_by_mac(struct net_device * ndev,int * port)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 }
3189ba682f0Skxie@chelsio.com 
cxgbi_hbas_remove(struct cxgbi_device * cdev)3199ba682f0Skxie@chelsio.com void cxgbi_hbas_remove(struct cxgbi_device *cdev)
3209ba682f0Skxie@chelsio.com {
3219ba682f0Skxie@chelsio.com 	int i;
3229ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
3239ba682f0Skxie@chelsio.com 
3249ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV,
3259ba682f0Skxie@chelsio.com 		"cdev 0x%p, p#%u.\n", cdev, cdev->nports);
3269ba682f0Skxie@chelsio.com 
3279ba682f0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
3289ba682f0Skxie@chelsio.com 		chba = cdev->hbas[i];
3299ba682f0Skxie@chelsio.com 		if (chba) {
3309ba682f0Skxie@chelsio.com 			cdev->hbas[i] = NULL;
33131500e90SMike Christie 			iscsi_host_remove(chba->shost, false);
3329ba682f0Skxie@chelsio.com 			pci_dev_put(cdev->pdev);
3339ba682f0Skxie@chelsio.com 			iscsi_host_free(chba->shost);
3349ba682f0Skxie@chelsio.com 		}
3359ba682f0Skxie@chelsio.com 	}
3369ba682f0Skxie@chelsio.com }
3379ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
3389ba682f0Skxie@chelsio.com 
cxgbi_hbas_add(struct cxgbi_device * cdev,u64 max_lun,unsigned int max_conns,const struct scsi_host_template * sht,struct scsi_transport_template * stt)3391abf635dSHannes Reinecke int cxgbi_hbas_add(struct cxgbi_device *cdev, u64 max_lun,
340bdd4aad7SMike Christie 		unsigned int max_conns, const struct scsi_host_template *sht,
3419ba682f0Skxie@chelsio.com 		struct scsi_transport_template *stt)
3429ba682f0Skxie@chelsio.com {
3439ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
3449ba682f0Skxie@chelsio.com 	struct Scsi_Host *shost;
3459ba682f0Skxie@chelsio.com 	int i, err;
3469ba682f0Skxie@chelsio.com 
3479ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
3489ba682f0Skxie@chelsio.com 
3499ba682f0Skxie@chelsio.com 	for (i = 0; i < cdev->nports; i++) {
3509ba682f0Skxie@chelsio.com 		shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
3519ba682f0Skxie@chelsio.com 		if (!shost) {
3529ba682f0Skxie@chelsio.com 			pr_info("0x%p, p%d, %s, host alloc failed.\n",
3539ba682f0Skxie@chelsio.com 				cdev, i, cdev->ports[i]->name);
3549ba682f0Skxie@chelsio.com 			err = -ENOMEM;
3559ba682f0Skxie@chelsio.com 			goto err_out;
3569ba682f0Skxie@chelsio.com 		}
3579ba682f0Skxie@chelsio.com 
3589ba682f0Skxie@chelsio.com 		shost->transportt = stt;
3599ba682f0Skxie@chelsio.com 		shost->max_lun = max_lun;
360bdd4aad7SMike Christie 		shost->max_id = max_conns - 1;
3619ba682f0Skxie@chelsio.com 		shost->max_channel = 0;
362e33c2482SVarun Prakash 		shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
3639ba682f0Skxie@chelsio.com 
3649ba682f0Skxie@chelsio.com 		chba = iscsi_host_priv(shost);
3659ba682f0Skxie@chelsio.com 		chba->cdev = cdev;
3669ba682f0Skxie@chelsio.com 		chba->ndev = cdev->ports[i];
3679ba682f0Skxie@chelsio.com 		chba->shost = shost;
3689ba682f0Skxie@chelsio.com 
369e33c2482SVarun Prakash 		shost->can_queue = sht->can_queue - ISCSI_MGMT_CMDS_MAX;
370e33c2482SVarun Prakash 
3719ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_DEV,
3729ba682f0Skxie@chelsio.com 			"cdev 0x%p, p#%d %s: chba 0x%p.\n",
3739ba682f0Skxie@chelsio.com 			cdev, i, cdev->ports[i]->name, chba);
3749ba682f0Skxie@chelsio.com 
3759ba682f0Skxie@chelsio.com 		pci_dev_get(cdev->pdev);
3769ba682f0Skxie@chelsio.com 		err = iscsi_host_add(shost, &cdev->pdev->dev);
3779ba682f0Skxie@chelsio.com 		if (err) {
3789ba682f0Skxie@chelsio.com 			pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
3799ba682f0Skxie@chelsio.com 				cdev, i, cdev->ports[i]->name);
3809ba682f0Skxie@chelsio.com 			pci_dev_put(cdev->pdev);
3819ba682f0Skxie@chelsio.com 			scsi_host_put(shost);
3829ba682f0Skxie@chelsio.com 			goto  err_out;
3839ba682f0Skxie@chelsio.com 		}
3849ba682f0Skxie@chelsio.com 
3859ba682f0Skxie@chelsio.com 		cdev->hbas[i] = chba;
3869ba682f0Skxie@chelsio.com 	}
3879ba682f0Skxie@chelsio.com 
3889ba682f0Skxie@chelsio.com 	return 0;
3899ba682f0Skxie@chelsio.com 
3909ba682f0Skxie@chelsio.com err_out:
3919ba682f0Skxie@chelsio.com 	cxgbi_hbas_remove(cdev);
3929ba682f0Skxie@chelsio.com 	return err;
3939ba682f0Skxie@chelsio.com }
3949ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
3959ba682f0Skxie@chelsio.com 
3969ba682f0Skxie@chelsio.com /*
3979ba682f0Skxie@chelsio.com  * iSCSI offload
3989ba682f0Skxie@chelsio.com  *
3999ba682f0Skxie@chelsio.com  * - source port management
4009ba682f0Skxie@chelsio.com  *   To find a free source port in the port allocation map we use a very simple
4019ba682f0Skxie@chelsio.com  *   rotor scheme to look for the next free port.
4029ba682f0Skxie@chelsio.com  *
4039ba682f0Skxie@chelsio.com  *   If a source port has been specified make sure that it doesn't collide with
4049ba682f0Skxie@chelsio.com  *   our normal source port allocation map.  If it's outside the range of our
4059ba682f0Skxie@chelsio.com  *   allocation/deallocation scheme just let them use it.
4069ba682f0Skxie@chelsio.com  *
4079ba682f0Skxie@chelsio.com  *   If the source port is outside our allocation range, the caller is
4089ba682f0Skxie@chelsio.com  *   responsible for keeping track of their port usage.
4099ba682f0Skxie@chelsio.com  */
410dd9ad67eSAnish Bhatt 
find_sock_on_port(struct cxgbi_device * cdev,unsigned char port_id)411dd9ad67eSAnish Bhatt static struct cxgbi_sock *find_sock_on_port(struct cxgbi_device *cdev,
412dd9ad67eSAnish Bhatt 					    unsigned char port_id)
413dd9ad67eSAnish Bhatt {
414dd9ad67eSAnish Bhatt 	struct cxgbi_ports_map *pmap = &cdev->pmap;
415dd9ad67eSAnish Bhatt 	unsigned int i;
416dd9ad67eSAnish Bhatt 	unsigned int used;
417dd9ad67eSAnish Bhatt 
418dd9ad67eSAnish Bhatt 	if (!pmap->max_connect || !pmap->used)
419dd9ad67eSAnish Bhatt 		return NULL;
420dd9ad67eSAnish Bhatt 
421dd9ad67eSAnish Bhatt 	spin_lock_bh(&pmap->lock);
422dd9ad67eSAnish Bhatt 	used = pmap->used;
423dd9ad67eSAnish Bhatt 	for (i = 0; used && i < pmap->max_connect; i++) {
424dd9ad67eSAnish Bhatt 		struct cxgbi_sock *csk = pmap->port_csk[i];
425dd9ad67eSAnish Bhatt 
426dd9ad67eSAnish Bhatt 		if (csk) {
427dd9ad67eSAnish Bhatt 			if (csk->port_id == port_id) {
428dd9ad67eSAnish Bhatt 				spin_unlock_bh(&pmap->lock);
429dd9ad67eSAnish Bhatt 				return csk;
430dd9ad67eSAnish Bhatt 			}
431dd9ad67eSAnish Bhatt 			used--;
432dd9ad67eSAnish Bhatt 		}
433dd9ad67eSAnish Bhatt 	}
434dd9ad67eSAnish Bhatt 	spin_unlock_bh(&pmap->lock);
435dd9ad67eSAnish Bhatt 
436dd9ad67eSAnish Bhatt 	return NULL;
437dd9ad67eSAnish Bhatt }
438dd9ad67eSAnish Bhatt 
sock_get_port(struct cxgbi_sock * csk)4399ba682f0Skxie@chelsio.com static int sock_get_port(struct cxgbi_sock *csk)
4409ba682f0Skxie@chelsio.com {
4419ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
4429ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
4439ba682f0Skxie@chelsio.com 	unsigned int start;
4449ba682f0Skxie@chelsio.com 	int idx;
445fc8d0590SAnish Bhatt 	__be16 *port;
4469ba682f0Skxie@chelsio.com 
4479ba682f0Skxie@chelsio.com 	if (!pmap->max_connect) {
4489ba682f0Skxie@chelsio.com 		pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
4499ba682f0Skxie@chelsio.com 			   cdev, csk->port_id, cdev->ports[csk->port_id]->name);
4509ba682f0Skxie@chelsio.com 		return -EADDRNOTAVAIL;
4519ba682f0Skxie@chelsio.com 	}
4529ba682f0Skxie@chelsio.com 
453fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
454fc8d0590SAnish Bhatt 		port = &csk->saddr.sin_port;
455fc8d0590SAnish Bhatt 	else /* ipv6 */
456fc8d0590SAnish Bhatt 		port = &csk->saddr6.sin6_port;
457fc8d0590SAnish Bhatt 
458fc8d0590SAnish Bhatt 	if (*port) {
4599ba682f0Skxie@chelsio.com 		pr_err("source port NON-ZERO %u.\n",
460fc8d0590SAnish Bhatt 			ntohs(*port));
4619ba682f0Skxie@chelsio.com 		return -EADDRINUSE;
4629ba682f0Skxie@chelsio.com 	}
4639ba682f0Skxie@chelsio.com 
4649ba682f0Skxie@chelsio.com 	spin_lock_bh(&pmap->lock);
4659ba682f0Skxie@chelsio.com 	if (pmap->used >= pmap->max_connect) {
4669ba682f0Skxie@chelsio.com 		spin_unlock_bh(&pmap->lock);
4679ba682f0Skxie@chelsio.com 		pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
4689ba682f0Skxie@chelsio.com 			cdev, csk->port_id, cdev->ports[csk->port_id]->name);
4699ba682f0Skxie@chelsio.com 		return -EADDRNOTAVAIL;
4709ba682f0Skxie@chelsio.com 	}
4719ba682f0Skxie@chelsio.com 
4729ba682f0Skxie@chelsio.com 	start = idx = pmap->next;
4739ba682f0Skxie@chelsio.com 	do {
4749ba682f0Skxie@chelsio.com 		if (++idx >= pmap->max_connect)
4759ba682f0Skxie@chelsio.com 			idx = 0;
4769ba682f0Skxie@chelsio.com 		if (!pmap->port_csk[idx]) {
4779ba682f0Skxie@chelsio.com 			pmap->used++;
478fc8d0590SAnish Bhatt 			*port = htons(pmap->sport_base + idx);
4799ba682f0Skxie@chelsio.com 			pmap->next = idx;
4809ba682f0Skxie@chelsio.com 			pmap->port_csk[idx] = csk;
4819ba682f0Skxie@chelsio.com 			spin_unlock_bh(&pmap->lock);
4829ba682f0Skxie@chelsio.com 			cxgbi_sock_get(csk);
4839ba682f0Skxie@chelsio.com 			log_debug(1 << CXGBI_DBG_SOCK,
4849ba682f0Skxie@chelsio.com 				"cdev 0x%p, p#%u %s, p %u, %u.\n",
4859ba682f0Skxie@chelsio.com 				cdev, csk->port_id,
4869ba682f0Skxie@chelsio.com 				cdev->ports[csk->port_id]->name,
4879ba682f0Skxie@chelsio.com 				pmap->sport_base + idx, pmap->next);
4889ba682f0Skxie@chelsio.com 			return 0;
4899ba682f0Skxie@chelsio.com 		}
4909ba682f0Skxie@chelsio.com 	} while (idx != start);
4919ba682f0Skxie@chelsio.com 	spin_unlock_bh(&pmap->lock);
4929ba682f0Skxie@chelsio.com 
4939ba682f0Skxie@chelsio.com 	/* should not happen */
4949ba682f0Skxie@chelsio.com 	pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
4959ba682f0Skxie@chelsio.com 		cdev, csk->port_id, cdev->ports[csk->port_id]->name,
4969ba682f0Skxie@chelsio.com 		pmap->next);
4979ba682f0Skxie@chelsio.com 	return -EADDRNOTAVAIL;
4989ba682f0Skxie@chelsio.com }
4999ba682f0Skxie@chelsio.com 
sock_put_port(struct cxgbi_sock * csk)5009ba682f0Skxie@chelsio.com static void sock_put_port(struct cxgbi_sock *csk)
5019ba682f0Skxie@chelsio.com {
5029ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
5039ba682f0Skxie@chelsio.com 	struct cxgbi_ports_map *pmap = &cdev->pmap;
504fc8d0590SAnish Bhatt 	__be16 *port;
5059ba682f0Skxie@chelsio.com 
506fc8d0590SAnish Bhatt 	if (csk->csk_family == AF_INET)
507fc8d0590SAnish Bhatt 		port = &csk->saddr.sin_port;
508fc8d0590SAnish Bhatt 	else /* ipv6 */
509fc8d0590SAnish Bhatt 		port = &csk->saddr6.sin6_port;
5109ba682f0Skxie@chelsio.com 
511fc8d0590SAnish Bhatt 	if (*port) {
512fc8d0590SAnish Bhatt 		int idx = ntohs(*port) - pmap->sport_base;
513fc8d0590SAnish Bhatt 
514fc8d0590SAnish Bhatt 		*port = 0;
5159ba682f0Skxie@chelsio.com 		if (idx < 0 || idx >= pmap->max_connect) {
5169ba682f0Skxie@chelsio.com 			pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
5179ba682f0Skxie@chelsio.com 				cdev, csk->port_id,
5189ba682f0Skxie@chelsio.com 				cdev->ports[csk->port_id]->name,
519fc8d0590SAnish Bhatt 				ntohs(*port));
5209ba682f0Skxie@chelsio.com 			return;
5219ba682f0Skxie@chelsio.com 		}
5229ba682f0Skxie@chelsio.com 
5239ba682f0Skxie@chelsio.com 		spin_lock_bh(&pmap->lock);
5249ba682f0Skxie@chelsio.com 		pmap->port_csk[idx] = NULL;
5259ba682f0Skxie@chelsio.com 		pmap->used--;
5269ba682f0Skxie@chelsio.com 		spin_unlock_bh(&pmap->lock);
5279ba682f0Skxie@chelsio.com 
5289ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_SOCK,
5299ba682f0Skxie@chelsio.com 			"cdev 0x%p, p#%u %s, release %u.\n",
5309ba682f0Skxie@chelsio.com 			cdev, csk->port_id, cdev->ports[csk->port_id]->name,
5319ba682f0Skxie@chelsio.com 			pmap->sport_base + idx);
5329ba682f0Skxie@chelsio.com 
5339ba682f0Skxie@chelsio.com 		cxgbi_sock_put(csk);
5349ba682f0Skxie@chelsio.com 	}
5359ba682f0Skxie@chelsio.com }
5369ba682f0Skxie@chelsio.com 
5379ba682f0Skxie@chelsio.com /*
5389ba682f0Skxie@chelsio.com  * iscsi tcp connection
5399ba682f0Skxie@chelsio.com  */
cxgbi_sock_free_cpl_skbs(struct cxgbi_sock * csk)5409ba682f0Skxie@chelsio.com void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
5419ba682f0Skxie@chelsio.com {
5429ba682f0Skxie@chelsio.com 	if (csk->cpl_close) {
5439ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_close);
5449ba682f0Skxie@chelsio.com 		csk->cpl_close = NULL;
5459ba682f0Skxie@chelsio.com 	}
5469ba682f0Skxie@chelsio.com 	if (csk->cpl_abort_req) {
5479ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_abort_req);
5489ba682f0Skxie@chelsio.com 		csk->cpl_abort_req = NULL;
5499ba682f0Skxie@chelsio.com 	}
5509ba682f0Skxie@chelsio.com 	if (csk->cpl_abort_rpl) {
5519ba682f0Skxie@chelsio.com 		kfree_skb(csk->cpl_abort_rpl);
5529ba682f0Skxie@chelsio.com 		csk->cpl_abort_rpl = NULL;
5539ba682f0Skxie@chelsio.com 	}
5549ba682f0Skxie@chelsio.com }
5559ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
5569ba682f0Skxie@chelsio.com 
cxgbi_sock_create(struct cxgbi_device * cdev)5579ba682f0Skxie@chelsio.com static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
5589ba682f0Skxie@chelsio.com {
5599ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
5609ba682f0Skxie@chelsio.com 
5619ba682f0Skxie@chelsio.com 	if (!csk) {
5629ba682f0Skxie@chelsio.com 		pr_info("alloc csk %zu failed.\n", sizeof(*csk));
5639ba682f0Skxie@chelsio.com 		return NULL;
5649ba682f0Skxie@chelsio.com 	}
5659ba682f0Skxie@chelsio.com 
5669ba682f0Skxie@chelsio.com 	if (cdev->csk_alloc_cpls(csk) < 0) {
5679ba682f0Skxie@chelsio.com 		pr_info("csk 0x%p, alloc cpls failed.\n", csk);
5689ba682f0Skxie@chelsio.com 		kfree(csk);
5699ba682f0Skxie@chelsio.com 		return NULL;
5709ba682f0Skxie@chelsio.com 	}
5719ba682f0Skxie@chelsio.com 
5729ba682f0Skxie@chelsio.com 	spin_lock_init(&csk->lock);
5739ba682f0Skxie@chelsio.com 	kref_init(&csk->refcnt);
5749ba682f0Skxie@chelsio.com 	skb_queue_head_init(&csk->receive_queue);
5759ba682f0Skxie@chelsio.com 	skb_queue_head_init(&csk->write_queue);
576cd07f958SKees Cook 	timer_setup(&csk->retry_timer, NULL, 0);
5779e8f1c79SVarun Prakash 	init_completion(&csk->cmpl);
5789ba682f0Skxie@chelsio.com 	rwlock_init(&csk->callback_lock);
5799ba682f0Skxie@chelsio.com 	csk->cdev = cdev;
5809ba682f0Skxie@chelsio.com 	csk->flags = 0;
5819ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CLOSED);
5829ba682f0Skxie@chelsio.com 
5839ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
5849ba682f0Skxie@chelsio.com 
5859ba682f0Skxie@chelsio.com 	return csk;
5869ba682f0Skxie@chelsio.com }
5879ba682f0Skxie@chelsio.com 
find_route_ipv4(struct flowi4 * fl4,__be32 saddr,__be32 daddr,__be16 sport,__be16 dport,u8 tos,int ifindex)588f4bfd99fSDavid S. Miller static struct rtable *find_route_ipv4(struct flowi4 *fl4,
589f4bfd99fSDavid S. Miller 				      __be32 saddr, __be32 daddr,
5904737c5a0SVarun Prakash 				      __be16 sport, __be16 dport, u8 tos,
5914737c5a0SVarun Prakash 				      int ifindex)
5929ba682f0Skxie@chelsio.com {
5939ba682f0Skxie@chelsio.com 	struct rtable *rt;
5949ba682f0Skxie@chelsio.com 
595f4bfd99fSDavid S. Miller 	rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
5964737c5a0SVarun Prakash 				   dport, sport, IPPROTO_TCP, tos, ifindex);
597b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
5989ba682f0Skxie@chelsio.com 		return NULL;
5999ba682f0Skxie@chelsio.com 
6009ba682f0Skxie@chelsio.com 	return rt;
6019ba682f0Skxie@chelsio.com }
6029ba682f0Skxie@chelsio.com 
6034737c5a0SVarun Prakash static struct cxgbi_sock *
cxgbi_check_route(struct sockaddr * dst_addr,int ifindex)6044737c5a0SVarun Prakash cxgbi_check_route(struct sockaddr *dst_addr, int ifindex)
6059ba682f0Skxie@chelsio.com {
6069ba682f0Skxie@chelsio.com 	struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
6079ba682f0Skxie@chelsio.com 	struct dst_entry *dst;
6089ba682f0Skxie@chelsio.com 	struct net_device *ndev;
6099ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev;
6109ba682f0Skxie@chelsio.com 	struct rtable *rt = NULL;
611a58b61e5SDavid Miller 	struct neighbour *n;
612f4bfd99fSDavid S. Miller 	struct flowi4 fl4;
6139ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = NULL;
6149ba682f0Skxie@chelsio.com 	unsigned int mtu = 0;
6159ba682f0Skxie@chelsio.com 	int port = 0xFFFF;
6169ba682f0Skxie@chelsio.com 	int err = 0;
6179ba682f0Skxie@chelsio.com 
6184737c5a0SVarun Prakash 	rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0,
6194737c5a0SVarun Prakash 			     daddr->sin_port, 0, ifindex);
6209ba682f0Skxie@chelsio.com 	if (!rt) {
6219ba682f0Skxie@chelsio.com 		pr_info("no route to ipv4 0x%x, port %u.\n",
622fc8d0590SAnish Bhatt 			be32_to_cpu(daddr->sin_addr.s_addr),
623fc8d0590SAnish Bhatt 			be16_to_cpu(daddr->sin_port));
6249ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
6259ba682f0Skxie@chelsio.com 		goto err_out;
6269ba682f0Skxie@chelsio.com 	}
6279ba682f0Skxie@chelsio.com 	dst = &rt->dst;
6280b399d46SDavid S. Miller 	n = dst_neigh_lookup(dst, &daddr->sin_addr.s_addr);
629a58b61e5SDavid Miller 	if (!n) {
630a58b61e5SDavid Miller 		err = -ENODEV;
631a58b61e5SDavid Miller 		goto rel_rt;
632a58b61e5SDavid Miller 	}
633a58b61e5SDavid Miller 	ndev = n->dev;
6349ba682f0Skxie@chelsio.com 
6359ba682f0Skxie@chelsio.com 	if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
6369ba682f0Skxie@chelsio.com 		pr_info("multi-cast route %pI4, port %u, dev %s.\n",
6379ba682f0Skxie@chelsio.com 			&daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
6389ba682f0Skxie@chelsio.com 			ndev->name);
6399ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
6400b399d46SDavid S. Miller 		goto rel_neigh;
6419ba682f0Skxie@chelsio.com 	}
6429ba682f0Skxie@chelsio.com 
6439ba682f0Skxie@chelsio.com 	if (ndev->flags & IFF_LOOPBACK) {
6449ba682f0Skxie@chelsio.com 		ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
645cc555759SVarun Prakash 		if (!ndev) {
646cc555759SVarun Prakash 			err = -ENETUNREACH;
647cc555759SVarun Prakash 			goto rel_neigh;
648cc555759SVarun Prakash 		}
6499ba682f0Skxie@chelsio.com 		mtu = ndev->mtu;
6509ba682f0Skxie@chelsio.com 		pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
651a58b61e5SDavid Miller 			n->dev->name, ndev->name, mtu);
6529ba682f0Skxie@chelsio.com 	}
6539ba682f0Skxie@chelsio.com 
654165ae50eSVarun Prakash 	if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
655165ae50eSVarun Prakash 		pr_info("%s interface not up.\n", ndev->name);
656165ae50eSVarun Prakash 		err = -ENETDOWN;
657165ae50eSVarun Prakash 		goto rel_neigh;
658165ae50eSVarun Prakash 	}
659165ae50eSVarun Prakash 
6609ba682f0Skxie@chelsio.com 	cdev = cxgbi_device_find_by_netdev(ndev, &port);
661ffcd686bSVarun Prakash 	if (!cdev)
662ffcd686bSVarun Prakash 		cdev = cxgbi_device_find_by_mac(ndev, &port);
6639ba682f0Skxie@chelsio.com 	if (!cdev) {
6649ba682f0Skxie@chelsio.com 		pr_info("dst %pI4, %s, NOT cxgbi device.\n",
6659ba682f0Skxie@chelsio.com 			&daddr->sin_addr.s_addr, ndev->name);
6669ba682f0Skxie@chelsio.com 		err = -ENETUNREACH;
6670b399d46SDavid S. Miller 		goto rel_neigh;
6689ba682f0Skxie@chelsio.com 	}
6699ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK,
6709ba682f0Skxie@chelsio.com 		"route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
6719ba682f0Skxie@chelsio.com 		&daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
6729ba682f0Skxie@chelsio.com 			   port, ndev->name, cdev);
6739ba682f0Skxie@chelsio.com 
6749ba682f0Skxie@chelsio.com 	csk = cxgbi_sock_create(cdev);
6759ba682f0Skxie@chelsio.com 	if (!csk) {
6769ba682f0Skxie@chelsio.com 		err = -ENOMEM;
6770b399d46SDavid S. Miller 		goto rel_neigh;
6789ba682f0Skxie@chelsio.com 	}
6799ba682f0Skxie@chelsio.com 	csk->cdev = cdev;
6809ba682f0Skxie@chelsio.com 	csk->port_id = port;
6819ba682f0Skxie@chelsio.com 	csk->mtu = mtu;
6829ba682f0Skxie@chelsio.com 	csk->dst = dst;
683fc8d0590SAnish Bhatt 
684fc8d0590SAnish Bhatt 	csk->csk_family = AF_INET;
6859ba682f0Skxie@chelsio.com 	csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
6869ba682f0Skxie@chelsio.com 	csk->daddr.sin_port = daddr->sin_port;
687c71b9b66SMike Christie 	csk->daddr.sin_family = daddr->sin_family;
688fc8d0590SAnish Bhatt 	csk->saddr.sin_family = daddr->sin_family;
689f4bfd99fSDavid S. Miller 	csk->saddr.sin_addr.s_addr = fl4.saddr;
6900b399d46SDavid S. Miller 	neigh_release(n);
6919ba682f0Skxie@chelsio.com 
6929ba682f0Skxie@chelsio.com 	return csk;
6939ba682f0Skxie@chelsio.com 
6940b399d46SDavid S. Miller rel_neigh:
6950b399d46SDavid S. Miller 	neigh_release(n);
6960b399d46SDavid S. Miller 
6979ba682f0Skxie@chelsio.com rel_rt:
6989ba682f0Skxie@chelsio.com 	ip_rt_put(rt);
6999ba682f0Skxie@chelsio.com err_out:
7009ba682f0Skxie@chelsio.com 	return ERR_PTR(err);
7019ba682f0Skxie@chelsio.com }
7029ba682f0Skxie@chelsio.com 
703e81fbf6cSAnish Bhatt #if IS_ENABLED(CONFIG_IPV6)
find_route_ipv6(const struct in6_addr * saddr,const struct in6_addr * daddr,int ifindex)704fc8d0590SAnish Bhatt static struct rt6_info *find_route_ipv6(const struct in6_addr *saddr,
7054737c5a0SVarun Prakash 					const struct in6_addr *daddr,
7064737c5a0SVarun Prakash 					int ifindex)
707fc8d0590SAnish Bhatt {
708fc8d0590SAnish Bhatt 	struct flowi6 fl;
709fc8d0590SAnish Bhatt 
7103d6d30d6SJiri Benc 	memset(&fl, 0, sizeof(fl));
7114737c5a0SVarun Prakash 	fl.flowi6_oif = ifindex;
712fc8d0590SAnish Bhatt 	if (saddr)
713fc8d0590SAnish Bhatt 		memcpy(&fl.saddr, saddr, sizeof(struct in6_addr));
714fc8d0590SAnish Bhatt 	if (daddr)
715fc8d0590SAnish Bhatt 		memcpy(&fl.daddr, daddr, sizeof(struct in6_addr));
716fc8d0590SAnish Bhatt 	return (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
717fc8d0590SAnish Bhatt }
718fc8d0590SAnish Bhatt 
7194737c5a0SVarun Prakash static struct cxgbi_sock *
cxgbi_check_route6(struct sockaddr * dst_addr,int ifindex)7204737c5a0SVarun Prakash cxgbi_check_route6(struct sockaddr *dst_addr, int ifindex)
721fc8d0590SAnish Bhatt {
722fc8d0590SAnish Bhatt 	struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)dst_addr;
723fc8d0590SAnish Bhatt 	struct dst_entry *dst;
724fc8d0590SAnish Bhatt 	struct net_device *ndev;
725fc8d0590SAnish Bhatt 	struct cxgbi_device *cdev;
726fc8d0590SAnish Bhatt 	struct rt6_info *rt = NULL;
727fc8d0590SAnish Bhatt 	struct neighbour *n;
728fc8d0590SAnish Bhatt 	struct in6_addr pref_saddr;
729fc8d0590SAnish Bhatt 	struct cxgbi_sock *csk = NULL;
730fc8d0590SAnish Bhatt 	unsigned int mtu = 0;
731fc8d0590SAnish Bhatt 	int port = 0xFFFF;
732fc8d0590SAnish Bhatt 	int err = 0;
733fc8d0590SAnish Bhatt 
7344737c5a0SVarun Prakash 	rt = find_route_ipv6(NULL, &daddr6->sin6_addr, ifindex);
735fc8d0590SAnish Bhatt 
736fc8d0590SAnish Bhatt 	if (!rt) {
737fc8d0590SAnish Bhatt 		pr_info("no route to ipv6 %pI6 port %u\n",
738fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
739fc8d0590SAnish Bhatt 			be16_to_cpu(daddr6->sin6_port));
740fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
741fc8d0590SAnish Bhatt 		goto err_out;
742fc8d0590SAnish Bhatt 	}
743fc8d0590SAnish Bhatt 
744fc8d0590SAnish Bhatt 	dst = &rt->dst;
745fc8d0590SAnish Bhatt 
746fc8d0590SAnish Bhatt 	n = dst_neigh_lookup(dst, &daddr6->sin6_addr);
747fc8d0590SAnish Bhatt 
748fc8d0590SAnish Bhatt 	if (!n) {
749fc8d0590SAnish Bhatt 		pr_info("%pI6, port %u, dst no neighbour.\n",
750fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
751fc8d0590SAnish Bhatt 			be16_to_cpu(daddr6->sin6_port));
752fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
753fc8d0590SAnish Bhatt 		goto rel_rt;
754fc8d0590SAnish Bhatt 	}
755fc8d0590SAnish Bhatt 	ndev = n->dev;
756fc8d0590SAnish Bhatt 
757165ae50eSVarun Prakash 	if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
758165ae50eSVarun Prakash 		pr_info("%s interface not up.\n", ndev->name);
759165ae50eSVarun Prakash 		err = -ENETDOWN;
760165ae50eSVarun Prakash 		goto rel_rt;
761165ae50eSVarun Prakash 	}
762165ae50eSVarun Prakash 
763fd0273d7SMartin KaFai Lau 	if (ipv6_addr_is_multicast(&daddr6->sin6_addr)) {
764fc8d0590SAnish Bhatt 		pr_info("multi-cast route %pI6 port %u, dev %s.\n",
765fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr,
766fc8d0590SAnish Bhatt 			ntohs(daddr6->sin6_port), ndev->name);
767fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
768fc8d0590SAnish Bhatt 		goto rel_rt;
769fc8d0590SAnish Bhatt 	}
770fc8d0590SAnish Bhatt 
771fc8d0590SAnish Bhatt 	cdev = cxgbi_device_find_by_netdev(ndev, &port);
772fc8d0590SAnish Bhatt 	if (!cdev)
773fc8d0590SAnish Bhatt 		cdev = cxgbi_device_find_by_mac(ndev, &port);
774fc8d0590SAnish Bhatt 	if (!cdev) {
775fc8d0590SAnish Bhatt 		pr_info("dst %pI6 %s, NOT cxgbi device.\n",
776fc8d0590SAnish Bhatt 			daddr6->sin6_addr.s6_addr, ndev->name);
777fc8d0590SAnish Bhatt 		err = -ENETUNREACH;
778fc8d0590SAnish Bhatt 		goto rel_rt;
779fc8d0590SAnish Bhatt 	}
780fc8d0590SAnish Bhatt 	log_debug(1 << CXGBI_DBG_SOCK,
781fc8d0590SAnish Bhatt 		  "route to %pI6 :%u, ndev p#%d,%s, cdev 0x%p.\n",
782fc8d0590SAnish Bhatt 		  daddr6->sin6_addr.s6_addr, ntohs(daddr6->sin6_port), port,
783fc8d0590SAnish Bhatt 		  ndev->name, cdev);
784fc8d0590SAnish Bhatt 
785fc8d0590SAnish Bhatt 	csk = cxgbi_sock_create(cdev);
786fc8d0590SAnish Bhatt 	if (!csk) {
787fc8d0590SAnish Bhatt 		err = -ENOMEM;
788fc8d0590SAnish Bhatt 		goto rel_rt;
789fc8d0590SAnish Bhatt 	}
790fc8d0590SAnish Bhatt 	csk->cdev = cdev;
791fc8d0590SAnish Bhatt 	csk->port_id = port;
792fc8d0590SAnish Bhatt 	csk->mtu = mtu;
793fc8d0590SAnish Bhatt 	csk->dst = dst;
794fc8d0590SAnish Bhatt 
79567edf21eSDavid Ahern 	rt6_get_prefsrc(rt, &pref_saddr);
79667edf21eSDavid Ahern 	if (ipv6_addr_any(&pref_saddr)) {
797fc8d0590SAnish Bhatt 		struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
798fc8d0590SAnish Bhatt 
799fc8d0590SAnish Bhatt 		err = ipv6_dev_get_saddr(&init_net, idev ? idev->dev : NULL,
800fc8d0590SAnish Bhatt 					 &daddr6->sin6_addr, 0, &pref_saddr);
801fc8d0590SAnish Bhatt 		if (err) {
802fc8d0590SAnish Bhatt 			pr_info("failed to get source address to reach %pI6\n",
803fc8d0590SAnish Bhatt 				&daddr6->sin6_addr);
804fc8d0590SAnish Bhatt 			goto rel_rt;
805fc8d0590SAnish Bhatt 		}
806fc8d0590SAnish Bhatt 	}
807fc8d0590SAnish Bhatt 
808fc8d0590SAnish Bhatt 	csk->csk_family = AF_INET6;
809fc8d0590SAnish Bhatt 	csk->daddr6.sin6_addr = daddr6->sin6_addr;
810fc8d0590SAnish Bhatt 	csk->daddr6.sin6_port = daddr6->sin6_port;
811fc8d0590SAnish Bhatt 	csk->daddr6.sin6_family = daddr6->sin6_family;
812dd9ad67eSAnish Bhatt 	csk->saddr6.sin6_family = daddr6->sin6_family;
813fc8d0590SAnish Bhatt 	csk->saddr6.sin6_addr = pref_saddr;
814fc8d0590SAnish Bhatt 
815fc8d0590SAnish Bhatt 	neigh_release(n);
816fc8d0590SAnish Bhatt 	return csk;
817fc8d0590SAnish Bhatt 
818fc8d0590SAnish Bhatt rel_rt:
819fc8d0590SAnish Bhatt 	if (n)
820fc8d0590SAnish Bhatt 		neigh_release(n);
821fc8d0590SAnish Bhatt 
822fc8d0590SAnish Bhatt 	ip6_rt_put(rt);
823fc8d0590SAnish Bhatt 	if (csk)
824fc8d0590SAnish Bhatt 		cxgbi_sock_closed(csk);
825fc8d0590SAnish Bhatt err_out:
826fc8d0590SAnish Bhatt 	return ERR_PTR(err);
827fc8d0590SAnish Bhatt }
828e81fbf6cSAnish Bhatt #endif /* IS_ENABLED(CONFIG_IPV6) */
829fc8d0590SAnish Bhatt 
cxgbi_sock_established(struct cxgbi_sock * csk,unsigned int snd_isn,unsigned int opt)8309ba682f0Skxie@chelsio.com void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
8319ba682f0Skxie@chelsio.com 			unsigned int opt)
8329ba682f0Skxie@chelsio.com {
8339ba682f0Skxie@chelsio.com 	csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
8349ba682f0Skxie@chelsio.com 	dst_confirm(csk->dst);
8359ba682f0Skxie@chelsio.com 	smp_mb();
8369ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
8379ba682f0Skxie@chelsio.com }
8389ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_established);
8399ba682f0Skxie@chelsio.com 
cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock * csk)8409ba682f0Skxie@chelsio.com static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
8419ba682f0Skxie@chelsio.com {
8429ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK,
8439ba682f0Skxie@chelsio.com 		"csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
8449ba682f0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->user_data);
8459ba682f0Skxie@chelsio.com 
8469ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED) {
847e3d2ad8cSkxie@chelsio.com 		read_lock_bh(&csk->callback_lock);
8489ba682f0Skxie@chelsio.com 		if (csk->user_data)
8499ba682f0Skxie@chelsio.com 			iscsi_conn_failure(csk->user_data,
850ee7255adSAnish Bhatt 					ISCSI_ERR_TCP_CONN_CLOSE);
851e3d2ad8cSkxie@chelsio.com 		read_unlock_bh(&csk->callback_lock);
8529ba682f0Skxie@chelsio.com 	}
8539ba682f0Skxie@chelsio.com }
8549ba682f0Skxie@chelsio.com 
cxgbi_sock_closed(struct cxgbi_sock * csk)8559ba682f0Skxie@chelsio.com void cxgbi_sock_closed(struct cxgbi_sock *csk)
8569ba682f0Skxie@chelsio.com {
8579ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8589ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8599ba682f0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
8609ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
8619ba682f0Skxie@chelsio.com 		return;
8629ba682f0Skxie@chelsio.com 	if (csk->saddr.sin_port)
8639ba682f0Skxie@chelsio.com 		sock_put_port(csk);
8649ba682f0Skxie@chelsio.com 	if (csk->dst)
8659ba682f0Skxie@chelsio.com 		dst_release(csk->dst);
8669ba682f0Skxie@chelsio.com 	csk->cdev->csk_release_offload_resources(csk);
8679ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CLOSED);
8689ba682f0Skxie@chelsio.com 	cxgbi_inform_iscsi_conn_closing(csk);
8699ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
8709ba682f0Skxie@chelsio.com }
8719ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
8729ba682f0Skxie@chelsio.com 
need_active_close(struct cxgbi_sock * csk)8739ba682f0Skxie@chelsio.com static void need_active_close(struct cxgbi_sock *csk)
8749ba682f0Skxie@chelsio.com {
8759ba682f0Skxie@chelsio.com 	int data_lost;
8769ba682f0Skxie@chelsio.com 	int close_req = 0;
8779ba682f0Skxie@chelsio.com 
8789ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
8799ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
8809ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
881e0f8e8cfSVarun Prakash 	if (csk->dst)
8829ba682f0Skxie@chelsio.com 		dst_confirm(csk->dst);
8839ba682f0Skxie@chelsio.com 	data_lost = skb_queue_len(&csk->receive_queue);
8849ba682f0Skxie@chelsio.com 	__skb_queue_purge(&csk->receive_queue);
8859ba682f0Skxie@chelsio.com 
8869ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN)
8879ba682f0Skxie@chelsio.com 		cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
8889ba682f0Skxie@chelsio.com 	else if (csk->state == CTP_ESTABLISHED) {
8899ba682f0Skxie@chelsio.com 		close_req = 1;
8909ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
8919ba682f0Skxie@chelsio.com 	} else if (csk->state == CTP_PASSIVE_CLOSE) {
8929ba682f0Skxie@chelsio.com 		close_req = 1;
8939ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
8949ba682f0Skxie@chelsio.com 	}
8959ba682f0Skxie@chelsio.com 
8969ba682f0Skxie@chelsio.com 	if (close_req) {
897e0f8e8cfSVarun Prakash 		if (!cxgbi_sock_flag(csk, CTPF_LOGOUT_RSP_RCVD) ||
898e0f8e8cfSVarun Prakash 		    data_lost)
8999ba682f0Skxie@chelsio.com 			csk->cdev->csk_send_abort_req(csk);
9009ba682f0Skxie@chelsio.com 		else
9019ba682f0Skxie@chelsio.com 			csk->cdev->csk_send_close_req(csk);
9029ba682f0Skxie@chelsio.com 	}
9039ba682f0Skxie@chelsio.com 
9049ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9059ba682f0Skxie@chelsio.com }
9069ba682f0Skxie@chelsio.com 
cxgbi_sock_fail_act_open(struct cxgbi_sock * csk,int errno)9079ba682f0Skxie@chelsio.com void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
9089ba682f0Skxie@chelsio.com {
9099ba682f0Skxie@chelsio.com 	pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
9109ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags,
9119ba682f0Skxie@chelsio.com 			&csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
9129ba682f0Skxie@chelsio.com 			&csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
9139ba682f0Skxie@chelsio.com 			errno);
9149ba682f0Skxie@chelsio.com 
9159ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CONNECTING);
9169ba682f0Skxie@chelsio.com 	csk->err = errno;
9179ba682f0Skxie@chelsio.com 	cxgbi_sock_closed(csk);
9189ba682f0Skxie@chelsio.com }
9199ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
9209ba682f0Skxie@chelsio.com 
cxgbi_sock_act_open_req_arp_failure(void * handle,struct sk_buff * skb)9219ba682f0Skxie@chelsio.com void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
9229ba682f0Skxie@chelsio.com {
9239ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
9241fe1fdb0SVarun Prakash 	struct module *owner = csk->cdev->owner;
9259ba682f0Skxie@chelsio.com 
9269ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
9279ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
9289ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9299ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9309ba682f0Skxie@chelsio.com 	if (csk->state == CTP_ACTIVE_OPEN)
9319ba682f0Skxie@chelsio.com 		cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
9329ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9339ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9349ba682f0Skxie@chelsio.com 	__kfree_skb(skb);
9351fe1fdb0SVarun Prakash 
9361fe1fdb0SVarun Prakash 	module_put(owner);
9379ba682f0Skxie@chelsio.com }
9389ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
9399ba682f0Skxie@chelsio.com 
cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock * csk)9409ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
9419ba682f0Skxie@chelsio.com {
9429ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9439ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9447b07bf24SAnish Bhatt 
9459ba682f0Skxie@chelsio.com 	cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
9467b07bf24SAnish Bhatt 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
9479ba682f0Skxie@chelsio.com 		cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
9489ba682f0Skxie@chelsio.com 		if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
9499ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
9509ba682f0Skxie@chelsio.com 			       csk, csk->state, csk->flags, csk->tid);
9519ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
9529ba682f0Skxie@chelsio.com 	}
9537b07bf24SAnish Bhatt 
9549ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9559ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9569ba682f0Skxie@chelsio.com }
9579ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
9589ba682f0Skxie@chelsio.com 
cxgbi_sock_rcv_peer_close(struct cxgbi_sock * csk)9599ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
9609ba682f0Skxie@chelsio.com {
9619ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
9629ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
9639ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9649ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9659ba682f0Skxie@chelsio.com 
9669ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
9679ba682f0Skxie@chelsio.com 		goto done;
9689ba682f0Skxie@chelsio.com 
9699ba682f0Skxie@chelsio.com 	switch (csk->state) {
9709ba682f0Skxie@chelsio.com 	case CTP_ESTABLISHED:
9719ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
9729ba682f0Skxie@chelsio.com 		break;
9739ba682f0Skxie@chelsio.com 	case CTP_ACTIVE_CLOSE:
9749ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
9759ba682f0Skxie@chelsio.com 		break;
9769ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_1:
9779ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
9789ba682f0Skxie@chelsio.com 		break;
9799ba682f0Skxie@chelsio.com 	case CTP_ABORTING:
9809ba682f0Skxie@chelsio.com 		break;
9819ba682f0Skxie@chelsio.com 	default:
9829ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
9839ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
9849ba682f0Skxie@chelsio.com 	}
9859ba682f0Skxie@chelsio.com 	cxgbi_inform_iscsi_conn_closing(csk);
9869ba682f0Skxie@chelsio.com done:
9879ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
9889ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
9899ba682f0Skxie@chelsio.com }
9909ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
9919ba682f0Skxie@chelsio.com 
cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock * csk,u32 snd_nxt)9929ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
9939ba682f0Skxie@chelsio.com {
9949ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
9959ba682f0Skxie@chelsio.com 		csk, (csk)->state, (csk)->flags, (csk)->tid);
9969ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
9979ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
9989ba682f0Skxie@chelsio.com 
9999ba682f0Skxie@chelsio.com 	csk->snd_una = snd_nxt - 1;
10009ba682f0Skxie@chelsio.com 	if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
10019ba682f0Skxie@chelsio.com 		goto done;
10029ba682f0Skxie@chelsio.com 
10039ba682f0Skxie@chelsio.com 	switch (csk->state) {
10049ba682f0Skxie@chelsio.com 	case CTP_ACTIVE_CLOSE:
10059ba682f0Skxie@chelsio.com 		cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
10069ba682f0Skxie@chelsio.com 		break;
10079ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_1:
10089ba682f0Skxie@chelsio.com 	case CTP_CLOSE_WAIT_2:
10099ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
10109ba682f0Skxie@chelsio.com 		break;
10119ba682f0Skxie@chelsio.com 	case CTP_ABORTING:
10129ba682f0Skxie@chelsio.com 		break;
10139ba682f0Skxie@chelsio.com 	default:
10149ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
10159ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid);
10169ba682f0Skxie@chelsio.com 	}
10179ba682f0Skxie@chelsio.com done:
10189ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10199ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
10209ba682f0Skxie@chelsio.com }
10219ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
10229ba682f0Skxie@chelsio.com 
cxgbi_sock_rcv_wr_ack(struct cxgbi_sock * csk,unsigned int credits,unsigned int snd_una,int seq_chk)10239ba682f0Skxie@chelsio.com void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
10249ba682f0Skxie@chelsio.com 			   unsigned int snd_una, int seq_chk)
10259ba682f0Skxie@chelsio.com {
10269ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
10279ba682f0Skxie@chelsio.com 			"csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
10289ba682f0Skxie@chelsio.com 			csk, csk->state, csk->flags, csk->tid, credits,
10299ba682f0Skxie@chelsio.com 			csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
10309ba682f0Skxie@chelsio.com 
10319ba682f0Skxie@chelsio.com 	spin_lock_bh(&csk->lock);
10329ba682f0Skxie@chelsio.com 
10339ba682f0Skxie@chelsio.com 	csk->wr_cred += credits;
10349ba682f0Skxie@chelsio.com 	if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
10359ba682f0Skxie@chelsio.com 		csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
10369ba682f0Skxie@chelsio.com 
10379ba682f0Skxie@chelsio.com 	while (credits) {
10389ba682f0Skxie@chelsio.com 		struct sk_buff *p = cxgbi_sock_peek_wr(csk);
10399ba682f0Skxie@chelsio.com 
10409ba682f0Skxie@chelsio.com 		if (unlikely(!p)) {
10419ba682f0Skxie@chelsio.com 			pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
10429ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid, credits,
10439ba682f0Skxie@chelsio.com 				csk->wr_cred, csk->wr_una_cred);
10449ba682f0Skxie@chelsio.com 			break;
10459ba682f0Skxie@chelsio.com 		}
10469ba682f0Skxie@chelsio.com 
10479ba682f0Skxie@chelsio.com 		if (unlikely(credits < p->csum)) {
10489ba682f0Skxie@chelsio.com 			pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
10499ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid,
10509ba682f0Skxie@chelsio.com 				credits, csk->wr_cred, csk->wr_una_cred,
10519ba682f0Skxie@chelsio.com 				p->csum);
10529ba682f0Skxie@chelsio.com 			p->csum -= credits;
10539ba682f0Skxie@chelsio.com 			break;
10549ba682f0Skxie@chelsio.com 		} else {
10559ba682f0Skxie@chelsio.com 			cxgbi_sock_dequeue_wr(csk);
10569ba682f0Skxie@chelsio.com 			credits -= p->csum;
10579ba682f0Skxie@chelsio.com 			kfree_skb(p);
10589ba682f0Skxie@chelsio.com 		}
10599ba682f0Skxie@chelsio.com 	}
10609ba682f0Skxie@chelsio.com 
10619ba682f0Skxie@chelsio.com 	cxgbi_sock_check_wr_invariants(csk);
10629ba682f0Skxie@chelsio.com 
10639ba682f0Skxie@chelsio.com 	if (seq_chk) {
10649ba682f0Skxie@chelsio.com 		if (unlikely(before(snd_una, csk->snd_una))) {
10659ba682f0Skxie@chelsio.com 			pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
10669ba682f0Skxie@chelsio.com 				csk, csk->state, csk->flags, csk->tid, snd_una,
10679ba682f0Skxie@chelsio.com 				csk->snd_una);
10689ba682f0Skxie@chelsio.com 			goto done;
10699ba682f0Skxie@chelsio.com 		}
10709ba682f0Skxie@chelsio.com 
10719ba682f0Skxie@chelsio.com 		if (csk->snd_una != snd_una) {
10729ba682f0Skxie@chelsio.com 			csk->snd_una = snd_una;
10739ba682f0Skxie@chelsio.com 			dst_confirm(csk->dst);
10749ba682f0Skxie@chelsio.com 		}
10759ba682f0Skxie@chelsio.com 	}
10769ba682f0Skxie@chelsio.com 
10779ba682f0Skxie@chelsio.com 	if (skb_queue_len(&csk->write_queue)) {
10789ba682f0Skxie@chelsio.com 		if (csk->cdev->csk_push_tx_frames(csk, 0))
10799ba682f0Skxie@chelsio.com 			cxgbi_conn_tx_open(csk);
10809ba682f0Skxie@chelsio.com 	} else
10819ba682f0Skxie@chelsio.com 		cxgbi_conn_tx_open(csk);
10829ba682f0Skxie@chelsio.com done:
10839ba682f0Skxie@chelsio.com 	spin_unlock_bh(&csk->lock);
10849ba682f0Skxie@chelsio.com }
10859ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
10869ba682f0Skxie@chelsio.com 
cxgbi_sock_find_best_mtu(struct cxgbi_sock * csk,unsigned short mtu)10879ba682f0Skxie@chelsio.com static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
10889ba682f0Skxie@chelsio.com 					     unsigned short mtu)
10899ba682f0Skxie@chelsio.com {
10909ba682f0Skxie@chelsio.com 	int i = 0;
10919ba682f0Skxie@chelsio.com 
10929ba682f0Skxie@chelsio.com 	while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
10939ba682f0Skxie@chelsio.com 		++i;
10949ba682f0Skxie@chelsio.com 
10959ba682f0Skxie@chelsio.com 	return i;
10969ba682f0Skxie@chelsio.com }
10979ba682f0Skxie@chelsio.com 
cxgbi_sock_select_mss(struct cxgbi_sock * csk,unsigned int pmtu)10989ba682f0Skxie@chelsio.com unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
10999ba682f0Skxie@chelsio.com {
11009ba682f0Skxie@chelsio.com 	unsigned int idx;
11019ba682f0Skxie@chelsio.com 	struct dst_entry *dst = csk->dst;
11029ba682f0Skxie@chelsio.com 
11030dbaee3bSDavid S. Miller 	csk->advmss = dst_metric_advmss(dst);
11049ba682f0Skxie@chelsio.com 
11059ba682f0Skxie@chelsio.com 	if (csk->advmss > pmtu - 40)
11069ba682f0Skxie@chelsio.com 		csk->advmss = pmtu - 40;
11079ba682f0Skxie@chelsio.com 	if (csk->advmss < csk->cdev->mtus[0] - 40)
11089ba682f0Skxie@chelsio.com 		csk->advmss = csk->cdev->mtus[0] - 40;
11099ba682f0Skxie@chelsio.com 	idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
11109ba682f0Skxie@chelsio.com 
11119ba682f0Skxie@chelsio.com 	return idx;
11129ba682f0Skxie@chelsio.com }
11139ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
11149ba682f0Skxie@chelsio.com 
cxgbi_sock_skb_entail(struct cxgbi_sock * csk,struct sk_buff * skb)11159ba682f0Skxie@chelsio.com void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
11169ba682f0Skxie@chelsio.com {
11179ba682f0Skxie@chelsio.com 	cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
11189ba682f0Skxie@chelsio.com 	__skb_queue_tail(&csk->write_queue, skb);
11199ba682f0Skxie@chelsio.com }
11209ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
11219ba682f0Skxie@chelsio.com 
cxgbi_sock_purge_wr_queue(struct cxgbi_sock * csk)11229ba682f0Skxie@chelsio.com void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
11239ba682f0Skxie@chelsio.com {
11249ba682f0Skxie@chelsio.com 	struct sk_buff *skb;
11259ba682f0Skxie@chelsio.com 
11269ba682f0Skxie@chelsio.com 	while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
11279ba682f0Skxie@chelsio.com 		kfree_skb(skb);
11289ba682f0Skxie@chelsio.com }
11299ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
11309ba682f0Skxie@chelsio.com 
cxgbi_sock_check_wr_invariants(const struct cxgbi_sock * csk)11319ba682f0Skxie@chelsio.com void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
11329ba682f0Skxie@chelsio.com {
11339ba682f0Skxie@chelsio.com 	int pending = cxgbi_sock_count_pending_wrs(csk);
11349ba682f0Skxie@chelsio.com 
11359ba682f0Skxie@chelsio.com 	if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
11369ba682f0Skxie@chelsio.com 		pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
11379ba682f0Skxie@chelsio.com 			csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
11389ba682f0Skxie@chelsio.com }
11399ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
11409ba682f0Skxie@chelsio.com 
114171f7a00bSVarun Prakash static inline void
scmd_get_params(struct scsi_cmnd * sc,struct scatterlist ** sgl,unsigned int * sgcnt,unsigned int * dlen,unsigned int prot)114271f7a00bSVarun Prakash scmd_get_params(struct scsi_cmnd *sc, struct scatterlist **sgl,
114371f7a00bSVarun Prakash 		unsigned int *sgcnt, unsigned int *dlen,
114471f7a00bSVarun Prakash 		unsigned int prot)
114571f7a00bSVarun Prakash {
1146ae3d56d8SChristoph Hellwig 	struct scsi_data_buffer *sdb = prot ? scsi_prot(sc) : &sc->sdb;
114771f7a00bSVarun Prakash 
114871f7a00bSVarun Prakash 	*sgl = sdb->table.sgl;
114971f7a00bSVarun Prakash 	*sgcnt = sdb->table.nents;
115071f7a00bSVarun Prakash 	*dlen = sdb->length;
115171f7a00bSVarun Prakash 	/* Caution: for protection sdb, sdb->length is invalid */
115271f7a00bSVarun Prakash }
115371f7a00bSVarun Prakash 
cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod * ppod,struct cxgbi_task_tag_info * ttinfo,struct scatterlist ** sg_pp,unsigned int * sg_off)115471f7a00bSVarun Prakash void cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod *ppod,
115571f7a00bSVarun Prakash 			    struct cxgbi_task_tag_info *ttinfo,
115671f7a00bSVarun Prakash 			    struct scatterlist **sg_pp, unsigned int *sg_off)
115771f7a00bSVarun Prakash {
115871f7a00bSVarun Prakash 	struct scatterlist *sg = sg_pp ? *sg_pp : NULL;
115971f7a00bSVarun Prakash 	unsigned int offset = sg_off ? *sg_off : 0;
116071f7a00bSVarun Prakash 	dma_addr_t addr = 0UL;
116171f7a00bSVarun Prakash 	unsigned int len = 0;
116271f7a00bSVarun Prakash 	int i;
116371f7a00bSVarun Prakash 
116471f7a00bSVarun Prakash 	memcpy(ppod, &ttinfo->hdr, sizeof(struct cxgbi_pagepod_hdr));
116571f7a00bSVarun Prakash 
116671f7a00bSVarun Prakash 	if (sg) {
116771f7a00bSVarun Prakash 		addr = sg_dma_address(sg);
116871f7a00bSVarun Prakash 		len = sg_dma_len(sg);
116971f7a00bSVarun Prakash 	}
117071f7a00bSVarun Prakash 
117171f7a00bSVarun Prakash 	for (i = 0; i < PPOD_PAGES_MAX; i++) {
117271f7a00bSVarun Prakash 		if (sg) {
117371f7a00bSVarun Prakash 			ppod->addr[i] = cpu_to_be64(addr + offset);
117471f7a00bSVarun Prakash 			offset += PAGE_SIZE;
117571f7a00bSVarun Prakash 			if (offset == (len + sg->offset)) {
117671f7a00bSVarun Prakash 				offset = 0;
117771f7a00bSVarun Prakash 				sg = sg_next(sg);
117871f7a00bSVarun Prakash 				if (sg) {
117971f7a00bSVarun Prakash 					addr = sg_dma_address(sg);
118071f7a00bSVarun Prakash 					len = sg_dma_len(sg);
118171f7a00bSVarun Prakash 				}
118271f7a00bSVarun Prakash 			}
118371f7a00bSVarun Prakash 		} else {
118471f7a00bSVarun Prakash 			ppod->addr[i] = 0ULL;
118571f7a00bSVarun Prakash 		}
118671f7a00bSVarun Prakash 	}
118771f7a00bSVarun Prakash 
118871f7a00bSVarun Prakash 	/*
118971f7a00bSVarun Prakash 	 * the fifth address needs to be repeated in the next ppod, so do
119071f7a00bSVarun Prakash 	 * not move sg
119171f7a00bSVarun Prakash 	 */
119271f7a00bSVarun Prakash 	if (sg_pp) {
119371f7a00bSVarun Prakash 		*sg_pp = sg;
119471f7a00bSVarun Prakash 		*sg_off = offset;
119571f7a00bSVarun Prakash 	}
119671f7a00bSVarun Prakash 
119771f7a00bSVarun Prakash 	if (offset == len) {
119871f7a00bSVarun Prakash 		offset = 0;
119971f7a00bSVarun Prakash 		sg = sg_next(sg);
120071f7a00bSVarun Prakash 		if (sg) {
120171f7a00bSVarun Prakash 			addr = sg_dma_address(sg);
120271f7a00bSVarun Prakash 			len = sg_dma_len(sg);
120371f7a00bSVarun Prakash 		}
120471f7a00bSVarun Prakash 	}
120571f7a00bSVarun Prakash 	ppod->addr[i] = sg ? cpu_to_be64(addr + offset) : 0ULL;
120671f7a00bSVarun Prakash }
120771f7a00bSVarun Prakash EXPORT_SYMBOL_GPL(cxgbi_ddp_set_one_ppod);
120871f7a00bSVarun Prakash 
12099ba682f0Skxie@chelsio.com /*
12109ba682f0Skxie@chelsio.com  * APIs interacting with open-iscsi libraries
12119ba682f0Skxie@chelsio.com  */
12129ba682f0Skxie@chelsio.com 
cxgbi_ddp_ppm_setup(void ** ppm_pp,struct cxgbi_device * cdev,struct cxgbi_tag_format * tformat,unsigned int iscsi_size,unsigned int llimit,unsigned int start,unsigned int rsvd_factor,unsigned int edram_start,unsigned int edram_size)1213a248384eSVarun Prakash int cxgbi_ddp_ppm_setup(void **ppm_pp, struct cxgbi_device *cdev,
1214a248384eSVarun Prakash 			struct cxgbi_tag_format *tformat,
1215a248384eSVarun Prakash 			unsigned int iscsi_size, unsigned int llimit,
1216a248384eSVarun Prakash 			unsigned int start, unsigned int rsvd_factor,
1217a248384eSVarun Prakash 			unsigned int edram_start, unsigned int edram_size)
121871f7a00bSVarun Prakash {
121971f7a00bSVarun Prakash 	int err = cxgbi_ppm_init(ppm_pp, cdev->ports[0], cdev->pdev,
1220a248384eSVarun Prakash 				cdev->lldev, tformat, iscsi_size, llimit, start,
1221a248384eSVarun Prakash 				rsvd_factor, edram_start, edram_size);
122271f7a00bSVarun Prakash 
122371f7a00bSVarun Prakash 	if (err >= 0) {
122471f7a00bSVarun Prakash 		struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
122571f7a00bSVarun Prakash 
122671f7a00bSVarun Prakash 		if (ppm->ppmax < 1024 ||
122771f7a00bSVarun Prakash 		    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX)
122871f7a00bSVarun Prakash 			cdev->flags |= CXGBI_FLAG_DDP_OFF;
122971f7a00bSVarun Prakash 		err = 0;
123071f7a00bSVarun Prakash 	} else {
123171f7a00bSVarun Prakash 		cdev->flags |= CXGBI_FLAG_DDP_OFF;
123271f7a00bSVarun Prakash 	}
1233a248384eSVarun Prakash 
1234a248384eSVarun Prakash 	return err;
123571f7a00bSVarun Prakash }
123671f7a00bSVarun Prakash EXPORT_SYMBOL_GPL(cxgbi_ddp_ppm_setup);
123771f7a00bSVarun Prakash 
cxgbi_ddp_sgl_check(struct scatterlist * sgl,int nents)123871f7a00bSVarun Prakash static int cxgbi_ddp_sgl_check(struct scatterlist *sgl, int nents)
123971f7a00bSVarun Prakash {
124071f7a00bSVarun Prakash 	int i;
124171f7a00bSVarun Prakash 	int last_sgidx = nents - 1;
124271f7a00bSVarun Prakash 	struct scatterlist *sg = sgl;
124371f7a00bSVarun Prakash 
124471f7a00bSVarun Prakash 	for (i = 0; i < nents; i++, sg = sg_next(sg)) {
124571f7a00bSVarun Prakash 		unsigned int len = sg->length + sg->offset;
124671f7a00bSVarun Prakash 
124771f7a00bSVarun Prakash 		if ((sg->offset & 0x3) || (i && sg->offset) ||
124871f7a00bSVarun Prakash 		    ((i != last_sgidx) && len != PAGE_SIZE)) {
124971f7a00bSVarun Prakash 			log_debug(1 << CXGBI_DBG_DDP,
125071f7a00bSVarun Prakash 				  "sg %u/%u, %u,%u, not aligned.\n",
125171f7a00bSVarun Prakash 				  i, nents, sg->offset, sg->length);
125271f7a00bSVarun Prakash 			goto err_out;
125371f7a00bSVarun Prakash 		}
125471f7a00bSVarun Prakash 	}
125571f7a00bSVarun Prakash 	return 0;
125671f7a00bSVarun Prakash err_out:
125771f7a00bSVarun Prakash 	return -EINVAL;
125871f7a00bSVarun Prakash }
125971f7a00bSVarun Prakash 
cxgbi_ddp_reserve(struct cxgbi_conn * cconn,struct cxgbi_task_data * tdata,u32 sw_tag,unsigned int xferlen)126071f7a00bSVarun Prakash static int cxgbi_ddp_reserve(struct cxgbi_conn *cconn,
126171f7a00bSVarun Prakash 			     struct cxgbi_task_data *tdata, u32 sw_tag,
126271f7a00bSVarun Prakash 			     unsigned int xferlen)
126371f7a00bSVarun Prakash {
126471f7a00bSVarun Prakash 	struct cxgbi_sock *csk = cconn->cep->csk;
126571f7a00bSVarun Prakash 	struct cxgbi_device *cdev = csk->cdev;
126671f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
126771f7a00bSVarun Prakash 	struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
126871f7a00bSVarun Prakash 	struct scatterlist *sgl = ttinfo->sgl;
126971f7a00bSVarun Prakash 	unsigned int sgcnt = ttinfo->nents;
127071f7a00bSVarun Prakash 	unsigned int sg_offset = sgl->offset;
127171f7a00bSVarun Prakash 	int err;
127271f7a00bSVarun Prakash 
127371f7a00bSVarun Prakash 	if (cdev->flags & CXGBI_FLAG_DDP_OFF) {
127471f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_DDP,
127571f7a00bSVarun Prakash 			  "cdev 0x%p DDP off.\n", cdev);
127671f7a00bSVarun Prakash 		return -EINVAL;
127771f7a00bSVarun Prakash 	}
127871f7a00bSVarun Prakash 
127971f7a00bSVarun Prakash 	if (!ppm || xferlen < DDP_THRESHOLD || !sgcnt ||
128071f7a00bSVarun Prakash 	    ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX) {
128171f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_DDP,
128271f7a00bSVarun Prakash 			  "ppm 0x%p, pgidx %u, xfer %u, sgcnt %u, NO ddp.\n",
128371f7a00bSVarun Prakash 			  ppm, ppm ? ppm->tformat.pgsz_idx_dflt : DDP_PGIDX_MAX,
128471f7a00bSVarun Prakash 			  xferlen, ttinfo->nents);
128571f7a00bSVarun Prakash 		return -EINVAL;
128671f7a00bSVarun Prakash 	}
128771f7a00bSVarun Prakash 
128871f7a00bSVarun Prakash 	/* make sure the buffer is suitable for ddp */
128971f7a00bSVarun Prakash 	if (cxgbi_ddp_sgl_check(sgl, sgcnt) < 0)
129071f7a00bSVarun Prakash 		return -EINVAL;
129171f7a00bSVarun Prakash 
129271f7a00bSVarun Prakash 	ttinfo->nr_pages = (xferlen + sgl->offset + (1 << PAGE_SHIFT) - 1) >>
129371f7a00bSVarun Prakash 			    PAGE_SHIFT;
129471f7a00bSVarun Prakash 
129571f7a00bSVarun Prakash 	/*
129671f7a00bSVarun Prakash 	 * the ddp tag will be used for the itt in the outgoing pdu,
129771f7a00bSVarun Prakash 	 * the itt genrated by libiscsi is saved in the ppm and can be
129871f7a00bSVarun Prakash 	 * retrieved via the ddp tag
129971f7a00bSVarun Prakash 	 */
130071f7a00bSVarun Prakash 	err = cxgbi_ppm_ppods_reserve(ppm, ttinfo->nr_pages, 0, &ttinfo->idx,
130171f7a00bSVarun Prakash 				      &ttinfo->tag, (unsigned long)sw_tag);
130271f7a00bSVarun Prakash 	if (err < 0) {
130371f7a00bSVarun Prakash 		cconn->ddp_full++;
130471f7a00bSVarun Prakash 		return err;
130571f7a00bSVarun Prakash 	}
130671f7a00bSVarun Prakash 	ttinfo->npods = err;
130771f7a00bSVarun Prakash 
130871f7a00bSVarun Prakash 	 /* setup dma from scsi command sgl */
130971f7a00bSVarun Prakash 	sgl->offset = 0;
131071f7a00bSVarun Prakash 	err = dma_map_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
131171f7a00bSVarun Prakash 	sgl->offset = sg_offset;
131271f7a00bSVarun Prakash 	if (err == 0) {
131371f7a00bSVarun Prakash 		pr_info("%s: 0x%x, xfer %u, sgl %u dma mapping err.\n",
131471f7a00bSVarun Prakash 			__func__, sw_tag, xferlen, sgcnt);
131571f7a00bSVarun Prakash 		goto rel_ppods;
131671f7a00bSVarun Prakash 	}
131771f7a00bSVarun Prakash 	if (err != ttinfo->nr_pages) {
131871f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_DDP,
131971f7a00bSVarun Prakash 			  "%s: sw tag 0x%x, xfer %u, sgl %u, dma count %d.\n",
132071f7a00bSVarun Prakash 			  __func__, sw_tag, xferlen, sgcnt, err);
132171f7a00bSVarun Prakash 	}
132271f7a00bSVarun Prakash 
132371f7a00bSVarun Prakash 	ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_MAPPED;
132471f7a00bSVarun Prakash 	ttinfo->cid = csk->port_id;
132571f7a00bSVarun Prakash 
132671f7a00bSVarun Prakash 	cxgbi_ppm_make_ppod_hdr(ppm, ttinfo->tag, csk->tid, sgl->offset,
132771f7a00bSVarun Prakash 				xferlen, &ttinfo->hdr);
132871f7a00bSVarun Prakash 
132971f7a00bSVarun Prakash 	if (cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ) {
133071f7a00bSVarun Prakash 		/* write ppod from xmit_pdu (of iscsi_scsi_command pdu) */
133171f7a00bSVarun Prakash 		ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_VALID;
133271f7a00bSVarun Prakash 	} else {
133371f7a00bSVarun Prakash 		/* write ppod from control queue now */
133471f7a00bSVarun Prakash 		err = cdev->csk_ddp_set_map(ppm, csk, ttinfo);
133571f7a00bSVarun Prakash 		if (err < 0)
133671f7a00bSVarun Prakash 			goto rel_ppods;
133771f7a00bSVarun Prakash 	}
133871f7a00bSVarun Prakash 
133971f7a00bSVarun Prakash 	return 0;
134071f7a00bSVarun Prakash 
134171f7a00bSVarun Prakash rel_ppods:
134271f7a00bSVarun Prakash 	cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
134371f7a00bSVarun Prakash 
134471f7a00bSVarun Prakash 	if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_MAPPED) {
134571f7a00bSVarun Prakash 		ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_MAPPED;
134671f7a00bSVarun Prakash 		dma_unmap_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
134771f7a00bSVarun Prakash 	}
134871f7a00bSVarun Prakash 	return -EINVAL;
134971f7a00bSVarun Prakash }
135071f7a00bSVarun Prakash 
task_release_itt(struct iscsi_task * task,itt_t hdr_itt)13519ba682f0Skxie@chelsio.com static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
13529ba682f0Skxie@chelsio.com {
135371f7a00bSVarun Prakash 	struct scsi_cmnd *sc = task->sc;
135471f7a00bSVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
135571f7a00bSVarun Prakash 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
135671f7a00bSVarun Prakash 	struct cxgbi_device *cdev = cconn->chba->cdev;
135771f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
135871f7a00bSVarun Prakash 	u32 tag = ntohl((__force u32)hdr_itt);
135971f7a00bSVarun Prakash 
136071f7a00bSVarun Prakash 	log_debug(1 << CXGBI_DBG_DDP,
136171f7a00bSVarun Prakash 		  "cdev 0x%p, task 0x%p, release tag 0x%x.\n",
136271f7a00bSVarun Prakash 		  cdev, task, tag);
1363ae3d56d8SChristoph Hellwig 	if (sc && sc->sc_data_direction == DMA_FROM_DEVICE &&
136471f7a00bSVarun Prakash 	    cxgbi_ppm_is_ddp_tag(ppm, tag)) {
136571f7a00bSVarun Prakash 		struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
136671f7a00bSVarun Prakash 		struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
136771f7a00bSVarun Prakash 
136871f7a00bSVarun Prakash 		if (!(cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ))
136971f7a00bSVarun Prakash 			cdev->csk_ddp_clear_map(cdev, ppm, ttinfo);
137071f7a00bSVarun Prakash 		cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
137171f7a00bSVarun Prakash 		dma_unmap_sg(&ppm->pdev->dev, ttinfo->sgl, ttinfo->nents,
137271f7a00bSVarun Prakash 			     DMA_FROM_DEVICE);
137371f7a00bSVarun Prakash 	}
137471f7a00bSVarun Prakash }
137571f7a00bSVarun Prakash 
cxgbi_build_sw_tag(u32 idx,u32 age)137671f7a00bSVarun Prakash static inline u32 cxgbi_build_sw_tag(u32 idx, u32 age)
137771f7a00bSVarun Prakash {
137871f7a00bSVarun Prakash 	/* assume idx and age both are < 0x7FFF (32767) */
137971f7a00bSVarun Prakash 	return (idx << 16) | age;
13809ba682f0Skxie@chelsio.com }
13819ba682f0Skxie@chelsio.com 
task_reserve_itt(struct iscsi_task * task,itt_t * hdr_itt)13829ba682f0Skxie@chelsio.com static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
13839ba682f0Skxie@chelsio.com {
138471f7a00bSVarun Prakash 	struct scsi_cmnd *sc = task->sc;
138571f7a00bSVarun Prakash 	struct iscsi_conn *conn = task->conn;
138671f7a00bSVarun Prakash 	struct iscsi_session *sess = conn->session;
138771f7a00bSVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
138871f7a00bSVarun Prakash 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
138971f7a00bSVarun Prakash 	struct cxgbi_device *cdev = cconn->chba->cdev;
139071f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
139171f7a00bSVarun Prakash 	u32 sw_tag = cxgbi_build_sw_tag(task->itt, sess->age);
139271f7a00bSVarun Prakash 	u32 tag = 0;
139371f7a00bSVarun Prakash 	int err = -EINVAL;
139471f7a00bSVarun Prakash 
1395ae3d56d8SChristoph Hellwig 	if (sc && sc->sc_data_direction == DMA_FROM_DEVICE) {
139671f7a00bSVarun Prakash 		struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
139771f7a00bSVarun Prakash 		struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
139871f7a00bSVarun Prakash 
139971f7a00bSVarun Prakash 		scmd_get_params(sc, &ttinfo->sgl, &ttinfo->nents,
140071f7a00bSVarun Prakash 				&tdata->dlen, 0);
140171f7a00bSVarun Prakash 		err = cxgbi_ddp_reserve(cconn, tdata, sw_tag, tdata->dlen);
140271f7a00bSVarun Prakash 		if (!err)
140371f7a00bSVarun Prakash 			tag = ttinfo->tag;
140471f7a00bSVarun Prakash 		else
140571f7a00bSVarun Prakash 			 log_debug(1 << CXGBI_DBG_DDP,
140671f7a00bSVarun Prakash 				   "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
140771f7a00bSVarun Prakash 				   cconn->cep->csk, task, tdata->dlen,
140871f7a00bSVarun Prakash 				   ttinfo->nents);
140971f7a00bSVarun Prakash 	}
141071f7a00bSVarun Prakash 
141171f7a00bSVarun Prakash 	if (err < 0) {
141271f7a00bSVarun Prakash 		err = cxgbi_ppm_make_non_ddp_tag(ppm, sw_tag, &tag);
141371f7a00bSVarun Prakash 		if (err < 0)
141471f7a00bSVarun Prakash 			return err;
141571f7a00bSVarun Prakash 	}
141671f7a00bSVarun Prakash 	/*  the itt need to sent in big-endian order */
141771f7a00bSVarun Prakash 	*hdr_itt = (__force itt_t)htonl(tag);
141871f7a00bSVarun Prakash 
141971f7a00bSVarun Prakash 	log_debug(1 << CXGBI_DBG_DDP,
142071f7a00bSVarun Prakash 		  "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
142171f7a00bSVarun Prakash 		  cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
14229ba682f0Skxie@chelsio.com 	return 0;
14239ba682f0Skxie@chelsio.com }
14249ba682f0Skxie@chelsio.com 
cxgbi_parse_pdu_itt(struct iscsi_conn * conn,itt_t itt,int * idx,int * age)14259ba682f0Skxie@chelsio.com void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
14269ba682f0Skxie@chelsio.com {
142771f7a00bSVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
142871f7a00bSVarun Prakash 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
142971f7a00bSVarun Prakash 	struct cxgbi_device *cdev = cconn->chba->cdev;
143071f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
143171f7a00bSVarun Prakash 	u32 tag = ntohl((__force u32)itt);
143271f7a00bSVarun Prakash 	u32 sw_bits;
143371f7a00bSVarun Prakash 
143471f7a00bSVarun Prakash 	if (ppm) {
143571f7a00bSVarun Prakash 		if (cxgbi_ppm_is_ddp_tag(ppm, tag))
143671f7a00bSVarun Prakash 			sw_bits = cxgbi_ppm_get_tag_caller_data(ppm, tag);
143771f7a00bSVarun Prakash 		else
143871f7a00bSVarun Prakash 			sw_bits = cxgbi_ppm_decode_non_ddp_tag(ppm, tag);
143971f7a00bSVarun Prakash 	} else {
144071f7a00bSVarun Prakash 		sw_bits = tag;
144171f7a00bSVarun Prakash 	}
144271f7a00bSVarun Prakash 
144371f7a00bSVarun Prakash 	cxgbi_decode_sw_tag(sw_bits, idx, age);
144471f7a00bSVarun Prakash 	log_debug(1 << CXGBI_DBG_DDP,
144571f7a00bSVarun Prakash 		  "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
144671f7a00bSVarun Prakash 		  cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
144771f7a00bSVarun Prakash 		  age ? *age : 0xFF);
14489ba682f0Skxie@chelsio.com }
14499ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
14509ba682f0Skxie@chelsio.com 
cxgbi_conn_tx_open(struct cxgbi_sock * csk)14519ba682f0Skxie@chelsio.com void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
14529ba682f0Skxie@chelsio.com {
14539ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = csk->user_data;
14549ba682f0Skxie@chelsio.com 
14559ba682f0Skxie@chelsio.com 	if (conn) {
14569ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_SOCK,
14579ba682f0Skxie@chelsio.com 			"csk 0x%p, cid %d.\n", csk, conn->id);
14584b9f8ce4SMike Christie 		iscsi_conn_queue_xmit(conn);
14599ba682f0Skxie@chelsio.com 	}
14609ba682f0Skxie@chelsio.com }
14619ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
14629ba682f0Skxie@chelsio.com 
14639ba682f0Skxie@chelsio.com /*
14649ba682f0Skxie@chelsio.com  * pdu receive, interact with libiscsi_tcp
14659ba682f0Skxie@chelsio.com  */
read_pdu_skb(struct iscsi_conn * conn,struct sk_buff * skb,unsigned int offset,int offloaded)14669ba682f0Skxie@chelsio.com static inline int read_pdu_skb(struct iscsi_conn *conn,
14679ba682f0Skxie@chelsio.com 			       struct sk_buff *skb,
14689ba682f0Skxie@chelsio.com 			       unsigned int offset,
14699ba682f0Skxie@chelsio.com 			       int offloaded)
14709ba682f0Skxie@chelsio.com {
14719ba682f0Skxie@chelsio.com 	int status = 0;
14729ba682f0Skxie@chelsio.com 	int bytes_read;
14739ba682f0Skxie@chelsio.com 
14749ba682f0Skxie@chelsio.com 	bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
14759ba682f0Skxie@chelsio.com 	switch (status) {
14769ba682f0Skxie@chelsio.com 	case ISCSI_TCP_CONN_ERR:
14779ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
14789ba682f0Skxie@chelsio.com 			  skb, offset, offloaded);
14799ba682f0Skxie@chelsio.com 		return -EIO;
14809ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SUSPENDED:
14819ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
14829ba682f0Skxie@chelsio.com 			"skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
14839ba682f0Skxie@chelsio.com 			skb, offset, offloaded, bytes_read);
14849ba682f0Skxie@chelsio.com 		/* no transfer - just have caller flush queue */
14859ba682f0Skxie@chelsio.com 		return bytes_read;
14869ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SKB_DONE:
14879ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
14889ba682f0Skxie@chelsio.com 			skb, offset, offloaded);
14899ba682f0Skxie@chelsio.com 		/*
14909ba682f0Skxie@chelsio.com 		 * pdus should always fit in the skb and we should get
14919ba682f0Skxie@chelsio.com 		 * segment done notifcation.
14929ba682f0Skxie@chelsio.com 		 */
14939ba682f0Skxie@chelsio.com 		iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
14949ba682f0Skxie@chelsio.com 		return -EFAULT;
14959ba682f0Skxie@chelsio.com 	case ISCSI_TCP_SEGMENT_DONE:
14969ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
14979ba682f0Skxie@chelsio.com 			"skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
14989ba682f0Skxie@chelsio.com 			skb, offset, offloaded, bytes_read);
14999ba682f0Skxie@chelsio.com 		return bytes_read;
15009ba682f0Skxie@chelsio.com 	default:
15019ba682f0Skxie@chelsio.com 		pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
15029ba682f0Skxie@chelsio.com 			skb, offset, offloaded, status);
15039ba682f0Skxie@chelsio.com 		return -EINVAL;
15049ba682f0Skxie@chelsio.com 	}
15059ba682f0Skxie@chelsio.com }
15069ba682f0Skxie@chelsio.com 
1507e0f8e8cfSVarun Prakash static int
skb_read_pdu_bhs(struct cxgbi_sock * csk,struct iscsi_conn * conn,struct sk_buff * skb)1508e0f8e8cfSVarun Prakash skb_read_pdu_bhs(struct cxgbi_sock *csk, struct iscsi_conn *conn,
1509e0f8e8cfSVarun Prakash 		 struct sk_buff *skb)
15109ba682f0Skxie@chelsio.com {
15119ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1512e0f8e8cfSVarun Prakash 	int err;
15139ba682f0Skxie@chelsio.com 
15149ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
15159ba682f0Skxie@chelsio.com 		"conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
15169ba682f0Skxie@chelsio.com 		conn, skb, skb->len, cxgbi_skcb_flags(skb));
15179ba682f0Skxie@chelsio.com 
15189ba682f0Skxie@chelsio.com 	if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
15199ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
15209ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
15219ba682f0Skxie@chelsio.com 		return -EIO;
15229ba682f0Skxie@chelsio.com 	}
15239ba682f0Skxie@chelsio.com 
15249ba682f0Skxie@chelsio.com 	if (conn->hdrdgst_en &&
15259ba682f0Skxie@chelsio.com 	    cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
15269ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
15279ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
15289ba682f0Skxie@chelsio.com 		return -EIO;
15299ba682f0Skxie@chelsio.com 	}
15309ba682f0Skxie@chelsio.com 
153144830d8fSVarun Prakash 	if (cxgbi_skcb_test_flag(skb, SKCBF_RX_ISCSI_COMPL) &&
153244830d8fSVarun Prakash 	    cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA_DDPD)) {
153344830d8fSVarun Prakash 		/* If completion flag is set and data is directly
153444830d8fSVarun Prakash 		 * placed in to the host memory then update
153544830d8fSVarun Prakash 		 * task->exp_datasn to the datasn in completion
153644830d8fSVarun Prakash 		 * iSCSI hdr as T6 adapter generates completion only
153744830d8fSVarun Prakash 		 * for the last pdu of a sequence.
153844830d8fSVarun Prakash 		 */
153944830d8fSVarun Prakash 		itt_t itt = ((struct iscsi_data *)skb->data)->itt;
154044830d8fSVarun Prakash 		struct iscsi_task *task = iscsi_itt_to_ctask(conn, itt);
154144830d8fSVarun Prakash 		u32 data_sn = be32_to_cpu(((struct iscsi_data *)
154244830d8fSVarun Prakash 							skb->data)->datasn);
154344830d8fSVarun Prakash 		if (task && task->sc) {
154444830d8fSVarun Prakash 			struct iscsi_tcp_task *tcp_task = task->dd_data;
154544830d8fSVarun Prakash 
154644830d8fSVarun Prakash 			tcp_task->exp_datasn = data_sn;
154744830d8fSVarun Prakash 		}
154844830d8fSVarun Prakash 	}
154944830d8fSVarun Prakash 
1550e0f8e8cfSVarun Prakash 	err = read_pdu_skb(conn, skb, 0, 0);
1551e0f8e8cfSVarun Prakash 	if (likely(err >= 0)) {
1552e0f8e8cfSVarun Prakash 		struct iscsi_hdr *hdr = (struct iscsi_hdr *)skb->data;
1553e0f8e8cfSVarun Prakash 		u8 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
1554e0f8e8cfSVarun Prakash 
1555e0f8e8cfSVarun Prakash 		if (unlikely(opcode == ISCSI_OP_LOGOUT_RSP))
1556e0f8e8cfSVarun Prakash 			cxgbi_sock_set_flag(csk, CTPF_LOGOUT_RSP_RCVD);
1557e0f8e8cfSVarun Prakash 	}
1558e0f8e8cfSVarun Prakash 
1559e0f8e8cfSVarun Prakash 	return err;
15609ba682f0Skxie@chelsio.com }
15619ba682f0Skxie@chelsio.com 
skb_read_pdu_data(struct iscsi_conn * conn,struct sk_buff * lskb,struct sk_buff * skb,unsigned int offset)15629ba682f0Skxie@chelsio.com static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
15639ba682f0Skxie@chelsio.com 			     struct sk_buff *skb, unsigned int offset)
15649ba682f0Skxie@chelsio.com {
15659ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
15669ba682f0Skxie@chelsio.com 	bool offloaded = 0;
15679ba682f0Skxie@chelsio.com 	int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
15689ba682f0Skxie@chelsio.com 
15699ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
15709ba682f0Skxie@chelsio.com 		"conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
15719ba682f0Skxie@chelsio.com 		conn, skb, skb->len, cxgbi_skcb_flags(skb));
15729ba682f0Skxie@chelsio.com 
15739ba682f0Skxie@chelsio.com 	if (conn->datadgst_en &&
15749ba682f0Skxie@chelsio.com 	    cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
15759ba682f0Skxie@chelsio.com 		pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
15769ba682f0Skxie@chelsio.com 			conn, lskb, cxgbi_skcb_flags(lskb));
15779ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
15789ba682f0Skxie@chelsio.com 		return -EIO;
15799ba682f0Skxie@chelsio.com 	}
15809ba682f0Skxie@chelsio.com 
15819ba682f0Skxie@chelsio.com 	if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
15829ba682f0Skxie@chelsio.com 		return 0;
15839ba682f0Skxie@chelsio.com 
15849ba682f0Skxie@chelsio.com 	/* coalesced, add header digest length */
15859ba682f0Skxie@chelsio.com 	if (lskb == skb && conn->hdrdgst_en)
15869ba682f0Skxie@chelsio.com 		offset += ISCSI_DIGEST_SIZE;
15879ba682f0Skxie@chelsio.com 
15889ba682f0Skxie@chelsio.com 	if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
15899ba682f0Skxie@chelsio.com 		offloaded = 1;
15909ba682f0Skxie@chelsio.com 
15919ba682f0Skxie@chelsio.com 	if (opcode == ISCSI_OP_SCSI_DATA_IN)
15929ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
15939ba682f0Skxie@chelsio.com 			"skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
15949ba682f0Skxie@chelsio.com 			skb, opcode, ntohl(tcp_conn->in.hdr->itt),
15959ba682f0Skxie@chelsio.com 			tcp_conn->in.datalen, offloaded ? "is" : "not");
15969ba682f0Skxie@chelsio.com 
15979ba682f0Skxie@chelsio.com 	return read_pdu_skb(conn, skb, offset, offloaded);
15989ba682f0Skxie@chelsio.com }
15999ba682f0Skxie@chelsio.com 
csk_return_rx_credits(struct cxgbi_sock * csk,int copied)16009ba682f0Skxie@chelsio.com static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
16019ba682f0Skxie@chelsio.com {
16029ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
16039ba682f0Skxie@chelsio.com 	int must_send;
16049ba682f0Skxie@chelsio.com 	u32 credits;
16059ba682f0Skxie@chelsio.com 
16069ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
1607fc385045SHans Wennborg 		"csk 0x%p,%u,0x%lx,%u, seq %u, wup %u, thre %u, %u.\n",
16089ba682f0Skxie@chelsio.com 		csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
16099ba682f0Skxie@chelsio.com 		csk->rcv_wup, cdev->rx_credit_thres,
161081daf10cSKaren Xie 		csk->rcv_win);
16119ba682f0Skxie@chelsio.com 
1612586be7cbSVarun Prakash 	if (!cdev->rx_credit_thres)
1613586be7cbSVarun Prakash 		return;
1614586be7cbSVarun Prakash 
16159ba682f0Skxie@chelsio.com 	if (csk->state != CTP_ESTABLISHED)
16169ba682f0Skxie@chelsio.com 		return;
16179ba682f0Skxie@chelsio.com 
16189ba682f0Skxie@chelsio.com 	credits = csk->copied_seq - csk->rcv_wup;
16199ba682f0Skxie@chelsio.com 	if (unlikely(!credits))
16209ba682f0Skxie@chelsio.com 		return;
162181daf10cSKaren Xie 	must_send = credits + 16384 >= csk->rcv_win;
16229ba682f0Skxie@chelsio.com 	if (must_send || credits >= cdev->rx_credit_thres)
16239ba682f0Skxie@chelsio.com 		csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
16249ba682f0Skxie@chelsio.com }
16259ba682f0Skxie@chelsio.com 
cxgbi_conn_pdu_ready(struct cxgbi_sock * csk)16269ba682f0Skxie@chelsio.com void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
16279ba682f0Skxie@chelsio.com {
16289ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = csk->cdev;
16299ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = csk->user_data;
16309ba682f0Skxie@chelsio.com 	struct sk_buff *skb;
16319ba682f0Skxie@chelsio.com 	unsigned int read = 0;
16329ba682f0Skxie@chelsio.com 	int err = 0;
16339ba682f0Skxie@chelsio.com 
16349ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX,
16359ba682f0Skxie@chelsio.com 		"csk 0x%p, conn 0x%p.\n", csk, conn);
16369ba682f0Skxie@chelsio.com 
16375bd85625SMike Christie 	if (unlikely(!conn || test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))) {
16389ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
16395bd85625SMike Christie 			"csk 0x%p, conn 0x%p, id %d, conn flags 0x%lx!\n",
16409ba682f0Skxie@chelsio.com 			csk, conn, conn ? conn->id : 0xFF,
16415bd85625SMike Christie 			conn ? conn->flags : 0xFF);
16429ba682f0Skxie@chelsio.com 		return;
16439ba682f0Skxie@chelsio.com 	}
16449ba682f0Skxie@chelsio.com 
16459ba682f0Skxie@chelsio.com 	while (!err) {
16469ba682f0Skxie@chelsio.com 		skb = skb_peek(&csk->receive_queue);
16479ba682f0Skxie@chelsio.com 		if (!skb ||
16489ba682f0Skxie@chelsio.com 		    !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
16499ba682f0Skxie@chelsio.com 			if (skb)
16509ba682f0Skxie@chelsio.com 				log_debug(1 << CXGBI_DBG_PDU_RX,
16519ba682f0Skxie@chelsio.com 					"skb 0x%p, NOT ready 0x%lx.\n",
16529ba682f0Skxie@chelsio.com 					skb, cxgbi_skcb_flags(skb));
16539ba682f0Skxie@chelsio.com 			break;
16549ba682f0Skxie@chelsio.com 		}
16559ba682f0Skxie@chelsio.com 		__skb_unlink(skb, &csk->receive_queue);
16569ba682f0Skxie@chelsio.com 
16579ba682f0Skxie@chelsio.com 		read += cxgbi_skcb_rx_pdulen(skb);
16589ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_RX,
16599ba682f0Skxie@chelsio.com 			"csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
16609ba682f0Skxie@chelsio.com 			csk, skb, skb->len, cxgbi_skcb_flags(skb),
16619ba682f0Skxie@chelsio.com 			cxgbi_skcb_rx_pdulen(skb));
16629ba682f0Skxie@chelsio.com 
16639ba682f0Skxie@chelsio.com 		if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1664e0f8e8cfSVarun Prakash 			err = skb_read_pdu_bhs(csk, conn, skb);
1665e3d2ad8cSkxie@chelsio.com 			if (err < 0) {
1666e3d2ad8cSkxie@chelsio.com 				pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1667e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1668e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1669e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1670e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
1671e3d2ad8cSkxie@chelsio.com 				goto skb_done;
1672e3d2ad8cSkxie@chelsio.com 			}
16739ba682f0Skxie@chelsio.com 			err = skb_read_pdu_data(conn, skb, skb,
16749ba682f0Skxie@chelsio.com 						err + cdev->skb_rx_extra);
1675e3d2ad8cSkxie@chelsio.com 			if (err < 0)
1676e3d2ad8cSkxie@chelsio.com 				pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1677e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1678e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1679e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1680e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
16819ba682f0Skxie@chelsio.com 		} else {
1682e0f8e8cfSVarun Prakash 			err = skb_read_pdu_bhs(csk, conn, skb);
1683e3d2ad8cSkxie@chelsio.com 			if (err < 0) {
1684e3d2ad8cSkxie@chelsio.com 				pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1685e3d2ad8cSkxie@chelsio.com 					"f 0x%lx, plen %u.\n",
1686e3d2ad8cSkxie@chelsio.com 					csk, skb, skb->len,
1687e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_flags(skb),
1688e3d2ad8cSkxie@chelsio.com 					cxgbi_skcb_rx_pdulen(skb));
1689e3d2ad8cSkxie@chelsio.com 				goto skb_done;
1690e3d2ad8cSkxie@chelsio.com 			}
1691e3d2ad8cSkxie@chelsio.com 
16929ba682f0Skxie@chelsio.com 			if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
16939ba682f0Skxie@chelsio.com 				struct sk_buff *dskb;
16949ba682f0Skxie@chelsio.com 
16959ba682f0Skxie@chelsio.com 				dskb = skb_peek(&csk->receive_queue);
16969ba682f0Skxie@chelsio.com 				if (!dskb) {
1697e3d2ad8cSkxie@chelsio.com 					pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1698e3d2ad8cSkxie@chelsio.com 						" plen %u, NO data.\n",
1699e3d2ad8cSkxie@chelsio.com 						csk, skb, skb->len,
1700e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_flags(skb),
1701e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_rx_pdulen(skb));
1702e3d2ad8cSkxie@chelsio.com 					err = -EIO;
1703e3d2ad8cSkxie@chelsio.com 					goto skb_done;
17049ba682f0Skxie@chelsio.com 				}
17059ba682f0Skxie@chelsio.com 				__skb_unlink(dskb, &csk->receive_queue);
17069ba682f0Skxie@chelsio.com 
17079ba682f0Skxie@chelsio.com 				err = skb_read_pdu_data(conn, skb, dskb, 0);
1708e3d2ad8cSkxie@chelsio.com 				if (err < 0)
1709e3d2ad8cSkxie@chelsio.com 					pr_err("data, csk 0x%p, skb 0x%p,%u, "
1710e3d2ad8cSkxie@chelsio.com 						"f 0x%lx, plen %u, dskb 0x%p,"
1711e3d2ad8cSkxie@chelsio.com 						"%u.\n",
1712e3d2ad8cSkxie@chelsio.com 						csk, skb, skb->len,
1713e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_flags(skb),
1714e3d2ad8cSkxie@chelsio.com 						cxgbi_skcb_rx_pdulen(skb),
1715e3d2ad8cSkxie@chelsio.com 						dskb, dskb->len);
17169ba682f0Skxie@chelsio.com 				__kfree_skb(dskb);
17179ba682f0Skxie@chelsio.com 			} else
17189ba682f0Skxie@chelsio.com 				err = skb_read_pdu_data(conn, skb, skb, 0);
17199ba682f0Skxie@chelsio.com 		}
1720e3d2ad8cSkxie@chelsio.com skb_done:
1721e3d2ad8cSkxie@chelsio.com 		__kfree_skb(skb);
1722e3d2ad8cSkxie@chelsio.com 
17239ba682f0Skxie@chelsio.com 		if (err < 0)
17249ba682f0Skxie@chelsio.com 			break;
17259ba682f0Skxie@chelsio.com 	}
17269ba682f0Skxie@chelsio.com 
17279ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
17289ba682f0Skxie@chelsio.com 	if (read) {
17299ba682f0Skxie@chelsio.com 		csk->copied_seq += read;
17309ba682f0Skxie@chelsio.com 		csk_return_rx_credits(csk, read);
17319ba682f0Skxie@chelsio.com 		conn->rxdata_octets += read;
17329ba682f0Skxie@chelsio.com 	}
17339ba682f0Skxie@chelsio.com 
17349ba682f0Skxie@chelsio.com 	if (err < 0) {
1735e3d2ad8cSkxie@chelsio.com 		pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1736e3d2ad8cSkxie@chelsio.com 			csk, conn, err, read);
17379ba682f0Skxie@chelsio.com 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
17389ba682f0Skxie@chelsio.com 	}
17399ba682f0Skxie@chelsio.com }
17409ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
17419ba682f0Skxie@chelsio.com 
sgl_seek_offset(struct scatterlist * sgl,unsigned int sgcnt,unsigned int offset,unsigned int * off,struct scatterlist ** sgp)17429ba682f0Skxie@chelsio.com static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
17439ba682f0Skxie@chelsio.com 				unsigned int offset, unsigned int *off,
17449ba682f0Skxie@chelsio.com 				struct scatterlist **sgp)
17459ba682f0Skxie@chelsio.com {
17469ba682f0Skxie@chelsio.com 	int i;
17479ba682f0Skxie@chelsio.com 	struct scatterlist *sg;
17489ba682f0Skxie@chelsio.com 
17499ba682f0Skxie@chelsio.com 	for_each_sg(sgl, sg, sgcnt, i) {
17509ba682f0Skxie@chelsio.com 		if (offset < sg->length) {
17519ba682f0Skxie@chelsio.com 			*off = offset;
17529ba682f0Skxie@chelsio.com 			*sgp = sg;
17539ba682f0Skxie@chelsio.com 			return 0;
17549ba682f0Skxie@chelsio.com 		}
17559ba682f0Skxie@chelsio.com 		offset -= sg->length;
17569ba682f0Skxie@chelsio.com 	}
17579ba682f0Skxie@chelsio.com 	return -EFAULT;
17589ba682f0Skxie@chelsio.com }
17599ba682f0Skxie@chelsio.com 
1760e33c2482SVarun Prakash static int
sgl_read_to_frags(struct scatterlist * sg,unsigned int sgoffset,unsigned int dlen,struct page_frag * frags,int frag_max,u32 * dlimit)1761e33c2482SVarun Prakash sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
17626a39a16aSIan Campbell 		  unsigned int dlen, struct page_frag *frags,
1763e33c2482SVarun Prakash 		  int frag_max, u32 *dlimit)
17649ba682f0Skxie@chelsio.com {
17659ba682f0Skxie@chelsio.com 	unsigned int datalen = dlen;
17669ba682f0Skxie@chelsio.com 	unsigned int sglen = sg->length - sgoffset;
17679ba682f0Skxie@chelsio.com 	struct page *page = sg_page(sg);
17689ba682f0Skxie@chelsio.com 	int i;
17699ba682f0Skxie@chelsio.com 
17709ba682f0Skxie@chelsio.com 	i = 0;
17719ba682f0Skxie@chelsio.com 	do {
17729ba682f0Skxie@chelsio.com 		unsigned int copy;
17739ba682f0Skxie@chelsio.com 
17749ba682f0Skxie@chelsio.com 		if (!sglen) {
17759ba682f0Skxie@chelsio.com 			sg = sg_next(sg);
17769ba682f0Skxie@chelsio.com 			if (!sg) {
17779ba682f0Skxie@chelsio.com 				pr_warn("sg %d NULL, len %u/%u.\n",
17789ba682f0Skxie@chelsio.com 					i, datalen, dlen);
17799ba682f0Skxie@chelsio.com 				return -EINVAL;
17809ba682f0Skxie@chelsio.com 			}
17819ba682f0Skxie@chelsio.com 			sgoffset = 0;
17829ba682f0Skxie@chelsio.com 			sglen = sg->length;
17839ba682f0Skxie@chelsio.com 			page = sg_page(sg);
17849ba682f0Skxie@chelsio.com 
17859ba682f0Skxie@chelsio.com 		}
17869ba682f0Skxie@chelsio.com 		copy = min(datalen, sglen);
17879ba682f0Skxie@chelsio.com 		if (i && page == frags[i - 1].page &&
17889ba682f0Skxie@chelsio.com 		    sgoffset + sg->offset ==
17896a39a16aSIan Campbell 			frags[i - 1].offset + frags[i - 1].size) {
17906a39a16aSIan Campbell 			frags[i - 1].size += copy;
17919ba682f0Skxie@chelsio.com 		} else {
17929ba682f0Skxie@chelsio.com 			if (i >= frag_max) {
17939ba682f0Skxie@chelsio.com 				pr_warn("too many pages %u, dlen %u.\n",
17949ba682f0Skxie@chelsio.com 					frag_max, dlen);
1795e33c2482SVarun Prakash 				*dlimit = dlen - datalen;
17969ba682f0Skxie@chelsio.com 				return -EINVAL;
17979ba682f0Skxie@chelsio.com 			}
17989ba682f0Skxie@chelsio.com 
17999ba682f0Skxie@chelsio.com 			frags[i].page = page;
18006a39a16aSIan Campbell 			frags[i].offset = sg->offset + sgoffset;
18016a39a16aSIan Campbell 			frags[i].size = copy;
18029ba682f0Skxie@chelsio.com 			i++;
18039ba682f0Skxie@chelsio.com 		}
18049ba682f0Skxie@chelsio.com 		datalen -= copy;
18059ba682f0Skxie@chelsio.com 		sgoffset += copy;
18069ba682f0Skxie@chelsio.com 		sglen -= copy;
18079ba682f0Skxie@chelsio.com 	} while (datalen);
18089ba682f0Skxie@chelsio.com 
18099ba682f0Skxie@chelsio.com 	return i;
18109ba682f0Skxie@chelsio.com }
18119ba682f0Skxie@chelsio.com 
cxgbi_task_data_sgl_check(struct iscsi_task * task)1812e33c2482SVarun Prakash static void cxgbi_task_data_sgl_check(struct iscsi_task *task)
18139ba682f0Skxie@chelsio.com {
1814e33c2482SVarun Prakash 	struct scsi_cmnd *sc = task->sc;
1815e33c2482SVarun Prakash 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1816e33c2482SVarun Prakash 	struct scatterlist *sg, *sgl = NULL;
1817e33c2482SVarun Prakash 	u32 sgcnt = 0;
1818e33c2482SVarun Prakash 	int i;
1819e33c2482SVarun Prakash 
1820e33c2482SVarun Prakash 	tdata->flags = CXGBI_TASK_SGL_CHECKED;
1821e33c2482SVarun Prakash 	if (!sc)
1822e33c2482SVarun Prakash 		return;
1823e33c2482SVarun Prakash 
1824e33c2482SVarun Prakash 	scmd_get_params(sc, &sgl, &sgcnt, &tdata->dlen, 0);
1825e33c2482SVarun Prakash 	if (!sgl || !sgcnt) {
1826e33c2482SVarun Prakash 		tdata->flags |= CXGBI_TASK_SGL_COPY;
1827e33c2482SVarun Prakash 		return;
1828e33c2482SVarun Prakash 	}
1829e33c2482SVarun Prakash 
1830e33c2482SVarun Prakash 	for_each_sg(sgl, sg, sgcnt, i) {
1831e33c2482SVarun Prakash 		if (page_count(sg_page(sg)) < 1) {
1832e33c2482SVarun Prakash 			tdata->flags |= CXGBI_TASK_SGL_COPY;
1833e33c2482SVarun Prakash 			return;
1834e33c2482SVarun Prakash 		}
1835e33c2482SVarun Prakash 	}
1836e33c2482SVarun Prakash }
1837e33c2482SVarun Prakash 
1838e33c2482SVarun Prakash static int
cxgbi_task_data_sgl_read(struct iscsi_task * task,u32 offset,u32 count,u32 * dlimit)1839e33c2482SVarun Prakash cxgbi_task_data_sgl_read(struct iscsi_task *task, u32 offset, u32 count,
1840e33c2482SVarun Prakash 			 u32 *dlimit)
1841e33c2482SVarun Prakash {
1842e33c2482SVarun Prakash 	struct scsi_cmnd *sc = task->sc;
1843e33c2482SVarun Prakash 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1844e33c2482SVarun Prakash 	struct scatterlist *sgl = NULL;
1845e33c2482SVarun Prakash 	struct scatterlist *sg;
1846e33c2482SVarun Prakash 	u32 dlen = 0;
1847e33c2482SVarun Prakash 	u32 sgcnt;
1848e33c2482SVarun Prakash 	int err;
1849e33c2482SVarun Prakash 
1850e33c2482SVarun Prakash 	if (!sc)
1851e33c2482SVarun Prakash 		return 0;
1852e33c2482SVarun Prakash 
1853e33c2482SVarun Prakash 	scmd_get_params(sc, &sgl, &sgcnt, &dlen, 0);
1854e33c2482SVarun Prakash 	if (!sgl || !sgcnt)
1855e33c2482SVarun Prakash 		return 0;
1856e33c2482SVarun Prakash 
1857e33c2482SVarun Prakash 	err = sgl_seek_offset(sgl, sgcnt, offset, &tdata->sgoffset, &sg);
1858e33c2482SVarun Prakash 	if (err < 0) {
1859e33c2482SVarun Prakash 		pr_warn("tpdu max, sgl %u, bad offset %u/%u.\n",
1860e33c2482SVarun Prakash 			sgcnt, offset, tdata->dlen);
1861e33c2482SVarun Prakash 		return err;
1862e33c2482SVarun Prakash 	}
1863e33c2482SVarun Prakash 	err = sgl_read_to_frags(sg, tdata->sgoffset, count,
1864e33c2482SVarun Prakash 				tdata->frags, MAX_SKB_FRAGS, dlimit);
1865e33c2482SVarun Prakash 	if (err < 0) {
1866e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_ISCSI,
1867e33c2482SVarun Prakash 			  "sgl max limit, sgl %u, offset %u, %u/%u, dlimit %u.\n",
1868e33c2482SVarun Prakash 			  sgcnt, offset, count, tdata->dlen, *dlimit);
1869e33c2482SVarun Prakash 		return err;
1870e33c2482SVarun Prakash 	}
1871e33c2482SVarun Prakash 	tdata->offset = offset;
1872e33c2482SVarun Prakash 	tdata->count = count;
1873e33c2482SVarun Prakash 	tdata->nr_frags = err;
1874e33c2482SVarun Prakash 	tdata->total_count = count;
1875e33c2482SVarun Prakash 	tdata->total_offset = offset;
1876e33c2482SVarun Prakash 
1877e33c2482SVarun Prakash 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1878e33c2482SVarun Prakash 		  "%s: offset %u, count %u,\n"
1879e33c2482SVarun Prakash 		  "err %u, total_count %u, total_offset %u\n",
1880e33c2482SVarun Prakash 		  __func__, offset, count, err,  tdata->total_count, tdata->total_offset);
1881e33c2482SVarun Prakash 
1882e33c2482SVarun Prakash 	return 0;
1883e33c2482SVarun Prakash }
1884e33c2482SVarun Prakash 
cxgbi_conn_alloc_pdu(struct iscsi_task * task,u8 op)1885e33c2482SVarun Prakash int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 op)
1886e33c2482SVarun Prakash {
1887e33c2482SVarun Prakash 	struct iscsi_conn *conn = task->conn;
1888e33c2482SVarun Prakash 	struct iscsi_session *session = task->conn->session;
1889e33c2482SVarun Prakash 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
18909ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
18919ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
1892ba8ca097SVarun Prakash 	struct cxgbi_sock *csk = cconn->cep ? cconn->cep->csk : NULL;
18939ba682f0Skxie@chelsio.com 	struct iscsi_tcp_task *tcp_task = task->dd_data;
1894e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
18959ba682f0Skxie@chelsio.com 	struct scsi_cmnd *sc = task->sc;
1896e33c2482SVarun Prakash 	u32 headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1897e33c2482SVarun Prakash 	u32 max_txdata_len = conn->max_xmit_dlength;
1898e33c2482SVarun Prakash 	u32 iso_tx_rsvd = 0, local_iso_info = 0;
1899e33c2482SVarun Prakash 	u32 last_tdata_offset, last_tdata_count;
1900e33c2482SVarun Prakash 	int err = 0;
1901e33c2482SVarun Prakash 
190261965bf6SVarun Prakash 	if (!tcp_task) {
1903e33c2482SVarun Prakash 		pr_err("task 0x%p, tcp_task 0x%p, tdata 0x%p.\n",
1904e33c2482SVarun Prakash 		       task, tcp_task, tdata);
1905e33c2482SVarun Prakash 		return -ENOMEM;
1906e33c2482SVarun Prakash 	}
1907e33c2482SVarun Prakash 	if (!csk) {
1908e33c2482SVarun Prakash 		pr_err("task 0x%p, csk gone.\n", task);
1909e33c2482SVarun Prakash 		return -EPIPE;
1910e33c2482SVarun Prakash 	}
1911e33c2482SVarun Prakash 
1912e33c2482SVarun Prakash 	op &= ISCSI_OPCODE_MASK;
19139ba682f0Skxie@chelsio.com 
19149ba682f0Skxie@chelsio.com 	tcp_task->dd_data = tdata;
19159ba682f0Skxie@chelsio.com 	task->hdr = NULL;
19169ba682f0Skxie@chelsio.com 
1917e33c2482SVarun Prakash 	last_tdata_count = tdata->count;
1918e33c2482SVarun Prakash 	last_tdata_offset = tdata->offset;
19199ba682f0Skxie@chelsio.com 
1920e33c2482SVarun Prakash 	if ((op == ISCSI_OP_SCSI_DATA_OUT) ||
1921e33c2482SVarun Prakash 	    ((op == ISCSI_OP_SCSI_CMD) &&
1922e33c2482SVarun Prakash 	     (sc->sc_data_direction == DMA_TO_DEVICE))) {
1923e33c2482SVarun Prakash 		u32 remaining_data_tosend, dlimit = 0;
1924e33c2482SVarun Prakash 		u32 max_pdu_size, max_num_pdu, num_pdu;
1925e33c2482SVarun Prakash 		u32 count;
1926e33c2482SVarun Prakash 
1927e33c2482SVarun Prakash 		/* Preserve conn->max_xmit_dlength because it can get updated to
1928e33c2482SVarun Prakash 		 * ISO data size.
1929e33c2482SVarun Prakash 		 */
1930e33c2482SVarun Prakash 		if (task->state == ISCSI_TASK_PENDING)
1931e33c2482SVarun Prakash 			tdata->max_xmit_dlength = conn->max_xmit_dlength;
1932e33c2482SVarun Prakash 
1933e33c2482SVarun Prakash 		if (!tdata->offset)
1934e33c2482SVarun Prakash 			cxgbi_task_data_sgl_check(task);
1935e33c2482SVarun Prakash 
1936e33c2482SVarun Prakash 		remaining_data_tosend =
1937e33c2482SVarun Prakash 			tdata->dlen - tdata->offset - tdata->count;
1938e33c2482SVarun Prakash 
1939e33c2482SVarun Prakash recalculate_sgl:
1940e33c2482SVarun Prakash 		max_txdata_len = tdata->max_xmit_dlength;
1941e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1942e33c2482SVarun Prakash 			  "tdata->dlen %u, remaining to send %u "
1943e33c2482SVarun Prakash 			  "conn->max_xmit_dlength %u, "
1944e33c2482SVarun Prakash 			  "tdata->max_xmit_dlength %u\n",
1945e33c2482SVarun Prakash 			  tdata->dlen, remaining_data_tosend,
1946e33c2482SVarun Prakash 			  conn->max_xmit_dlength, tdata->max_xmit_dlength);
1947e33c2482SVarun Prakash 
1948e33c2482SVarun Prakash 		if (cdev->skb_iso_txhdr && !csk->disable_iso &&
1949e33c2482SVarun Prakash 		    (remaining_data_tosend > tdata->max_xmit_dlength) &&
1950e33c2482SVarun Prakash 		    !(remaining_data_tosend % 4)) {
1951e33c2482SVarun Prakash 			u32 max_iso_data;
1952e33c2482SVarun Prakash 
1953e33c2482SVarun Prakash 			if ((op == ISCSI_OP_SCSI_CMD) &&
1954e33c2482SVarun Prakash 			    session->initial_r2t_en)
1955e33c2482SVarun Prakash 				goto no_iso;
1956e33c2482SVarun Prakash 
1957e33c2482SVarun Prakash 			max_pdu_size = tdata->max_xmit_dlength +
1958e33c2482SVarun Prakash 				       ISCSI_PDU_NONPAYLOAD_LEN;
1959e33c2482SVarun Prakash 			max_iso_data = rounddown(CXGBI_MAX_ISO_DATA_IN_SKB,
1960e33c2482SVarun Prakash 						 csk->advmss);
1961e33c2482SVarun Prakash 			max_num_pdu = max_iso_data / max_pdu_size;
1962e33c2482SVarun Prakash 
1963e33c2482SVarun Prakash 			num_pdu = (remaining_data_tosend +
1964e33c2482SVarun Prakash 				   tdata->max_xmit_dlength - 1) /
1965e33c2482SVarun Prakash 				  tdata->max_xmit_dlength;
1966e33c2482SVarun Prakash 
1967e33c2482SVarun Prakash 			if (num_pdu > max_num_pdu)
1968e33c2482SVarun Prakash 				num_pdu = max_num_pdu;
1969e33c2482SVarun Prakash 
1970e33c2482SVarun Prakash 			conn->max_xmit_dlength = tdata->max_xmit_dlength * num_pdu;
1971e33c2482SVarun Prakash 			max_txdata_len = conn->max_xmit_dlength;
1972e33c2482SVarun Prakash 			iso_tx_rsvd = cdev->skb_iso_txhdr;
1973e33c2482SVarun Prakash 			local_iso_info = sizeof(struct cxgbi_iso_info);
1974e33c2482SVarun Prakash 
1975e33c2482SVarun Prakash 			log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1976e33c2482SVarun Prakash 				  "max_pdu_size %u, max_num_pdu %u, "
1977e33c2482SVarun Prakash 				  "max_txdata %u, num_pdu %u\n",
1978e33c2482SVarun Prakash 				  max_pdu_size, max_num_pdu,
1979e33c2482SVarun Prakash 				  max_txdata_len, num_pdu);
1980e33c2482SVarun Prakash 		}
1981e33c2482SVarun Prakash no_iso:
1982e33c2482SVarun Prakash 		count  = min_t(u32, max_txdata_len, remaining_data_tosend);
1983e33c2482SVarun Prakash 		err = cxgbi_task_data_sgl_read(task,
1984e33c2482SVarun Prakash 					       tdata->offset + tdata->count,
1985e33c2482SVarun Prakash 					       count, &dlimit);
1986e33c2482SVarun Prakash 		if (unlikely(err < 0)) {
1987e33c2482SVarun Prakash 			log_debug(1 << CXGBI_DBG_ISCSI,
1988e33c2482SVarun Prakash 				  "task 0x%p, tcp_task 0x%p, tdata 0x%p, "
1989e33c2482SVarun Prakash 				  "sgl err %d, count %u, dlimit %u\n",
1990e33c2482SVarun Prakash 				  task, tcp_task, tdata, err, count, dlimit);
1991e33c2482SVarun Prakash 			if (dlimit) {
1992e33c2482SVarun Prakash 				remaining_data_tosend =
1993e33c2482SVarun Prakash 					rounddown(dlimit,
1994e33c2482SVarun Prakash 						  tdata->max_xmit_dlength);
1995e33c2482SVarun Prakash 				if (!remaining_data_tosend)
1996e33c2482SVarun Prakash 					remaining_data_tosend = dlimit;
1997e33c2482SVarun Prakash 
1998e33c2482SVarun Prakash 				dlimit = 0;
1999e33c2482SVarun Prakash 
2000e33c2482SVarun Prakash 				conn->max_xmit_dlength = remaining_data_tosend;
2001e33c2482SVarun Prakash 				goto recalculate_sgl;
20029ba682f0Skxie@chelsio.com 			}
20039ba682f0Skxie@chelsio.com 
2004e33c2482SVarun Prakash 			pr_err("task 0x%p, tcp_task 0x%p, tdata 0x%p, "
2005e33c2482SVarun Prakash 				"sgl err %d\n",
2006e33c2482SVarun Prakash 				task, tcp_task, tdata, err);
2007e33c2482SVarun Prakash 			goto ret_err;
2008e33c2482SVarun Prakash 		}
2009e33c2482SVarun Prakash 
2010e33c2482SVarun Prakash 		if ((tdata->flags & CXGBI_TASK_SGL_COPY) ||
2011e33c2482SVarun Prakash 		    (tdata->nr_frags > MAX_SKB_FRAGS))
2012e33c2482SVarun Prakash 			headroom += conn->max_xmit_dlength;
2013e33c2482SVarun Prakash 	}
2014e33c2482SVarun Prakash 
2015e33c2482SVarun Prakash 	tdata->skb = alloc_skb(local_iso_info + cdev->skb_tx_rsvd +
2016e33c2482SVarun Prakash 			       iso_tx_rsvd + headroom, GFP_ATOMIC);
2017e33c2482SVarun Prakash 	if (!tdata->skb) {
2018e33c2482SVarun Prakash 		tdata->count = last_tdata_count;
2019e33c2482SVarun Prakash 		tdata->offset = last_tdata_offset;
2020e33c2482SVarun Prakash 		err = -ENOMEM;
2021e33c2482SVarun Prakash 		goto ret_err;
2022e33c2482SVarun Prakash 	}
2023e33c2482SVarun Prakash 
2024e33c2482SVarun Prakash 	skb_reserve(tdata->skb, local_iso_info + cdev->skb_tx_rsvd +
2025e33c2482SVarun Prakash 		    iso_tx_rsvd);
202622698483SVarun Prakash 
202722698483SVarun Prakash 	if (task->sc) {
20289ba682f0Skxie@chelsio.com 		task->hdr = (struct iscsi_hdr *)tdata->skb->data;
202922698483SVarun Prakash 	} else {
2030dd04b0f3SVarun Prakash 		task->hdr = kzalloc(SKB_TX_ISCSI_PDU_HEADER_MAX, GFP_ATOMIC);
203122698483SVarun Prakash 		if (!task->hdr) {
203222698483SVarun Prakash 			__kfree_skb(tdata->skb);
203322698483SVarun Prakash 			tdata->skb = NULL;
203422698483SVarun Prakash 			return -ENOMEM;
203522698483SVarun Prakash 		}
203622698483SVarun Prakash 	}
2037e33c2482SVarun Prakash 
2038e33c2482SVarun Prakash 	task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX;
2039e33c2482SVarun Prakash 
2040e33c2482SVarun Prakash 	if (iso_tx_rsvd)
2041e33c2482SVarun Prakash 		cxgbi_skcb_set_flag(tdata->skb, SKCBF_TX_ISO);
20429ba682f0Skxie@chelsio.com 
20439ba682f0Skxie@chelsio.com 	/* data_out uses scsi_cmd's itt */
2044e33c2482SVarun Prakash 	if (op != ISCSI_OP_SCSI_DATA_OUT)
20459ba682f0Skxie@chelsio.com 		task_reserve_itt(task, &task->hdr->itt);
20469ba682f0Skxie@chelsio.com 
20479ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
20489ba682f0Skxie@chelsio.com 		  "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
2049e33c2482SVarun Prakash 		  task, op, tdata->skb, cdev->skb_tx_rsvd, headroom,
2050e33c2482SVarun Prakash 		  conn->max_xmit_dlength, be32_to_cpu(task->hdr->itt));
20519ba682f0Skxie@chelsio.com 
20529ba682f0Skxie@chelsio.com 	return 0;
2053e33c2482SVarun Prakash 
2054e33c2482SVarun Prakash ret_err:
2055e33c2482SVarun Prakash 	conn->max_xmit_dlength = tdata->max_xmit_dlength;
2056e33c2482SVarun Prakash 	return err;
20579ba682f0Skxie@chelsio.com }
20589ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
20599ba682f0Skxie@chelsio.com 
2060e33c2482SVarun Prakash static int
cxgbi_prep_iso_info(struct iscsi_task * task,struct sk_buff * skb,u32 count)2061e33c2482SVarun Prakash cxgbi_prep_iso_info(struct iscsi_task *task, struct sk_buff *skb,
2062e33c2482SVarun Prakash 		    u32 count)
2063e33c2482SVarun Prakash {
2064e33c2482SVarun Prakash 	struct cxgbi_iso_info *iso_info = (struct cxgbi_iso_info *)skb->head;
2065e33c2482SVarun Prakash 	struct iscsi_r2t_info *r2t;
2066e33c2482SVarun Prakash 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2067e33c2482SVarun Prakash 	struct iscsi_conn *conn = task->conn;
2068e33c2482SVarun Prakash 	struct iscsi_session *session = conn->session;
2069e33c2482SVarun Prakash 	struct iscsi_tcp_task *tcp_task = task->dd_data;
2070e33c2482SVarun Prakash 	u32 burst_size = 0, r2t_dlength = 0, dlength;
2071e33c2482SVarun Prakash 	u32 max_pdu_len = tdata->max_xmit_dlength;
2072e33c2482SVarun Prakash 	u32 segment_offset = 0;
2073e33c2482SVarun Prakash 	u32 num_pdu;
2074e33c2482SVarun Prakash 
2075e33c2482SVarun Prakash 	if (unlikely(!cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO)))
2076e33c2482SVarun Prakash 		return 0;
2077e33c2482SVarun Prakash 
2078e33c2482SVarun Prakash 	memset(iso_info, 0, sizeof(struct cxgbi_iso_info));
2079e33c2482SVarun Prakash 
2080e33c2482SVarun Prakash 	if (task->hdr->opcode == ISCSI_OP_SCSI_CMD && session->imm_data_en) {
2081e33c2482SVarun Prakash 		iso_info->flags |= CXGBI_ISO_INFO_IMM_ENABLE;
2082e33c2482SVarun Prakash 		burst_size = count;
2083e33c2482SVarun Prakash 	}
2084e33c2482SVarun Prakash 
2085e33c2482SVarun Prakash 	dlength = ntoh24(task->hdr->dlength);
2086e33c2482SVarun Prakash 	dlength = min(dlength, max_pdu_len);
2087e33c2482SVarun Prakash 	hton24(task->hdr->dlength, dlength);
2088e33c2482SVarun Prakash 
2089e33c2482SVarun Prakash 	num_pdu = (count + max_pdu_len - 1) / max_pdu_len;
2090e33c2482SVarun Prakash 
2091e33c2482SVarun Prakash 	if (iscsi_task_has_unsol_data(task))
2092e33c2482SVarun Prakash 		r2t = &task->unsol_r2t;
2093e33c2482SVarun Prakash 	else
2094e33c2482SVarun Prakash 		r2t = tcp_task->r2t;
2095e33c2482SVarun Prakash 
2096e33c2482SVarun Prakash 	if (r2t) {
2097e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2098e33c2482SVarun Prakash 			  "count %u, tdata->count %u, num_pdu %u,"
2099e33c2482SVarun Prakash 			  "task->hdr_len %u, r2t->data_length %u, r2t->sent %u\n",
2100e33c2482SVarun Prakash 			  count, tdata->count, num_pdu, task->hdr_len,
2101e33c2482SVarun Prakash 			  r2t->data_length, r2t->sent);
2102e33c2482SVarun Prakash 
2103e33c2482SVarun Prakash 		r2t_dlength = r2t->data_length - r2t->sent;
2104e33c2482SVarun Prakash 		segment_offset = r2t->sent;
2105e33c2482SVarun Prakash 		r2t->datasn += num_pdu - 1;
2106e33c2482SVarun Prakash 	}
2107e33c2482SVarun Prakash 
2108e33c2482SVarun Prakash 	if (!r2t || !r2t->sent)
2109e33c2482SVarun Prakash 		iso_info->flags |= CXGBI_ISO_INFO_FSLICE;
2110e33c2482SVarun Prakash 
2111e33c2482SVarun Prakash 	if (task->hdr->flags & ISCSI_FLAG_CMD_FINAL)
2112e33c2482SVarun Prakash 		iso_info->flags |= CXGBI_ISO_INFO_LSLICE;
2113e33c2482SVarun Prakash 
2114e33c2482SVarun Prakash 	task->hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2115e33c2482SVarun Prakash 
2116e33c2482SVarun Prakash 	iso_info->op = task->hdr->opcode;
2117e33c2482SVarun Prakash 	iso_info->ahs = task->hdr->hlength;
2118e33c2482SVarun Prakash 	iso_info->num_pdu = num_pdu;
2119e33c2482SVarun Prakash 	iso_info->mpdu = max_pdu_len;
2120e33c2482SVarun Prakash 	iso_info->burst_size = (burst_size + r2t_dlength) >> 2;
2121e33c2482SVarun Prakash 	iso_info->len = count + task->hdr_len;
2122e33c2482SVarun Prakash 	iso_info->segment_offset = segment_offset;
2123e33c2482SVarun Prakash 
2124e33c2482SVarun Prakash 	cxgbi_skcb_tx_iscsi_hdrlen(skb) = task->hdr_len;
2125e33c2482SVarun Prakash 	return 0;
2126e33c2482SVarun Prakash }
2127e33c2482SVarun Prakash 
tx_skb_setmode(struct sk_buff * skb,int hcrc,int dcrc)21289ba682f0Skxie@chelsio.com static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
21299ba682f0Skxie@chelsio.com {
2130c343a01cSkxie@chelsio.com 	if (hcrc || dcrc) {
21319ba682f0Skxie@chelsio.com 		u8 submode = 0;
21329ba682f0Skxie@chelsio.com 
21339ba682f0Skxie@chelsio.com 		if (hcrc)
21349ba682f0Skxie@chelsio.com 			submode |= 1;
21359ba682f0Skxie@chelsio.com 		if (dcrc)
21369ba682f0Skxie@chelsio.com 			submode |= 2;
2137e33c2482SVarun Prakash 		cxgbi_skcb_tx_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
2138c343a01cSkxie@chelsio.com 	} else
2139e33c2482SVarun Prakash 		cxgbi_skcb_tx_ulp_mode(skb) = 0;
21409ba682f0Skxie@chelsio.com }
21419ba682f0Skxie@chelsio.com 
2142e33c2482SVarun Prakash static struct page *rsvd_page;
2143e33c2482SVarun Prakash 
cxgbi_conn_init_pdu(struct iscsi_task * task,unsigned int offset,unsigned int count)21449ba682f0Skxie@chelsio.com int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
21459ba682f0Skxie@chelsio.com 			      unsigned int count)
21469ba682f0Skxie@chelsio.com {
21479ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = task->conn;
2148e33c2482SVarun Prakash 	struct iscsi_tcp_task *tcp_task = task->dd_data;
2149e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2150b92a4a9fSColin Ian King 	struct sk_buff *skb;
2151e33c2482SVarun Prakash 	struct scsi_cmnd *sc = task->sc;
2152e33c2482SVarun Prakash 	u32 expected_count, expected_offset;
2153e33c2482SVarun Prakash 	u32 datalen = count, dlimit = 0;
2154e33c2482SVarun Prakash 	u32 i, padlen = iscsi_padding(count);
21559ba682f0Skxie@chelsio.com 	struct page *pg;
2156e33c2482SVarun Prakash 	int err;
2157e33c2482SVarun Prakash 
215861965bf6SVarun Prakash 	if (!tcp_task || (tcp_task->dd_data != tdata)) {
2159e33c2482SVarun Prakash 		pr_err("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
2160e33c2482SVarun Prakash 		       task, task->sc, tcp_task,
2161e33c2482SVarun Prakash 		       tcp_task ? tcp_task->dd_data : NULL, tdata);
2162e33c2482SVarun Prakash 		return -EINVAL;
2163e33c2482SVarun Prakash 	}
2164b92a4a9fSColin Ian King 	skb = tdata->skb;
21659ba682f0Skxie@chelsio.com 
21669ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
21679ba682f0Skxie@chelsio.com 		  "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
21689ba682f0Skxie@chelsio.com 		  task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
2169e33c2482SVarun Prakash 		  be32_to_cpu(task->cmdsn), be32_to_cpu(task->hdr->itt), offset, count);
21709ba682f0Skxie@chelsio.com 
21719ba682f0Skxie@chelsio.com 	skb_put(skb, task->hdr_len);
21729ba682f0Skxie@chelsio.com 	tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
2173e33c2482SVarun Prakash 	if (!count) {
21749ba682f0Skxie@chelsio.com 		tdata->count = count;
2175e33c2482SVarun Prakash 		tdata->offset = offset;
2176e33c2482SVarun Prakash 		tdata->nr_frags = 0;
2177e33c2482SVarun Prakash 		tdata->total_offset = 0;
2178e33c2482SVarun Prakash 		tdata->total_count = 0;
2179e33c2482SVarun Prakash 		if (tdata->max_xmit_dlength)
2180e33c2482SVarun Prakash 			conn->max_xmit_dlength = tdata->max_xmit_dlength;
2181e33c2482SVarun Prakash 		cxgbi_skcb_clear_flag(skb, SKCBF_TX_ISO);
2182e33c2482SVarun Prakash 		return 0;
21839ba682f0Skxie@chelsio.com 	}
21849ba682f0Skxie@chelsio.com 
2185e33c2482SVarun Prakash 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
218630f259b4SDan Carpenter 		  "data->total_count %u, tdata->total_offset %u\n",
2187e33c2482SVarun Prakash 		  tdata->total_count, tdata->total_offset);
2188e33c2482SVarun Prakash 
2189e33c2482SVarun Prakash 	expected_count = tdata->total_count;
2190e33c2482SVarun Prakash 	expected_offset = tdata->total_offset;
2191e33c2482SVarun Prakash 
2192e33c2482SVarun Prakash 	if ((count != expected_count) ||
2193e33c2482SVarun Prakash 	    (offset != expected_offset)) {
2194e33c2482SVarun Prakash 		err = cxgbi_task_data_sgl_read(task, offset, count, &dlimit);
2195e33c2482SVarun Prakash 		if (err < 0) {
2196e33c2482SVarun Prakash 			pr_err("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p "
2197e33c2482SVarun Prakash 			       "dlimit %u, sgl err %d.\n", task, task->sc,
2198e33c2482SVarun Prakash 			       tcp_task, tcp_task ? tcp_task->dd_data : NULL,
2199e33c2482SVarun Prakash 			       tdata, dlimit, err);
2200e33c2482SVarun Prakash 			return err;
2201e33c2482SVarun Prakash 		}
2202e33c2482SVarun Prakash 	}
2203e33c2482SVarun Prakash 
2204e33c2482SVarun Prakash 	/* Restore original value of conn->max_xmit_dlength because
2205e33c2482SVarun Prakash 	 * it can get updated to ISO data size.
2206e33c2482SVarun Prakash 	 */
2207e33c2482SVarun Prakash 	conn->max_xmit_dlength = tdata->max_xmit_dlength;
2208e33c2482SVarun Prakash 
2209e33c2482SVarun Prakash 	if (sc) {
22106a39a16aSIan Campbell 		struct page_frag *frag = tdata->frags;
22119ba682f0Skxie@chelsio.com 
2212e33c2482SVarun Prakash 		if ((tdata->flags & CXGBI_TASK_SGL_COPY) ||
2213e33c2482SVarun Prakash 		    (tdata->nr_frags > MAX_SKB_FRAGS) ||
2214e33c2482SVarun Prakash 		    (padlen && (tdata->nr_frags ==
2215e33c2482SVarun Prakash 					MAX_SKB_FRAGS))) {
2216e33c2482SVarun Prakash 			char *dst = skb->data + task->hdr_len;
2217e33c2482SVarun Prakash 
22189ba682f0Skxie@chelsio.com 			/* data fits in the skb's headroom */
22199ba682f0Skxie@chelsio.com 			for (i = 0; i < tdata->nr_frags; i++, frag++) {
222077dfce07SCong Wang 				char *src = kmap_atomic(frag->page);
22219ba682f0Skxie@chelsio.com 
22226a39a16aSIan Campbell 				memcpy(dst, src + frag->offset, frag->size);
22236a39a16aSIan Campbell 				dst += frag->size;
222477dfce07SCong Wang 				kunmap_atomic(src);
22259ba682f0Skxie@chelsio.com 			}
2226e33c2482SVarun Prakash 
22279ba682f0Skxie@chelsio.com 			if (padlen) {
22289ba682f0Skxie@chelsio.com 				memset(dst, 0, padlen);
22299ba682f0Skxie@chelsio.com 				padlen = 0;
22309ba682f0Skxie@chelsio.com 			}
22319ba682f0Skxie@chelsio.com 			skb_put(skb, count + padlen);
22329ba682f0Skxie@chelsio.com 		} else {
2233e33c2482SVarun Prakash 			for (i = 0; i < tdata->nr_frags; i++, frag++) {
2234e33c2482SVarun Prakash 				get_page(frag->page);
2235e33c2482SVarun Prakash 				skb_fill_page_desc(skb, i, frag->page,
2236e33c2482SVarun Prakash 						   frag->offset, frag->size);
22376a39a16aSIan Campbell 			}
2238e33c2482SVarun Prakash 
22399ba682f0Skxie@chelsio.com 			skb->len += count;
22409ba682f0Skxie@chelsio.com 			skb->data_len += count;
22419ba682f0Skxie@chelsio.com 			skb->truesize += count;
22429ba682f0Skxie@chelsio.com 		}
22439ba682f0Skxie@chelsio.com 	} else {
2244e33c2482SVarun Prakash 		pg = virt_to_head_page(task->data);
22459ba682f0Skxie@chelsio.com 		get_page(pg);
2246e33c2482SVarun Prakash 		skb_fill_page_desc(skb, 0, pg,
2247e33c2482SVarun Prakash 				   task->data - (char *)page_address(pg),
22489ba682f0Skxie@chelsio.com 				   count);
22499ba682f0Skxie@chelsio.com 		skb->len += count;
22509ba682f0Skxie@chelsio.com 		skb->data_len += count;
22519ba682f0Skxie@chelsio.com 		skb->truesize += count;
22529ba682f0Skxie@chelsio.com 	}
22539ba682f0Skxie@chelsio.com 
22549ba682f0Skxie@chelsio.com 	if (padlen) {
2255e33c2482SVarun Prakash 		get_page(rsvd_page);
22569ba682f0Skxie@chelsio.com 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2257e33c2482SVarun Prakash 				   rsvd_page, 0, padlen);
22589ba682f0Skxie@chelsio.com 
22599ba682f0Skxie@chelsio.com 		skb->data_len += padlen;
22609ba682f0Skxie@chelsio.com 		skb->truesize += padlen;
22619ba682f0Skxie@chelsio.com 		skb->len += padlen;
22629ba682f0Skxie@chelsio.com 	}
22639ba682f0Skxie@chelsio.com 
2264e33c2482SVarun Prakash 	if (likely(count > tdata->max_xmit_dlength))
2265e33c2482SVarun Prakash 		cxgbi_prep_iso_info(task, skb, count);
2266e33c2482SVarun Prakash 	else
2267e33c2482SVarun Prakash 		cxgbi_skcb_clear_flag(skb, SKCBF_TX_ISO);
2268e33c2482SVarun Prakash 
22699ba682f0Skxie@chelsio.com 	return 0;
22709ba682f0Skxie@chelsio.com }
22719ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
22729ba682f0Skxie@chelsio.com 
cxgbi_sock_tx_queue_up(struct cxgbi_sock * csk,struct sk_buff * skb)2273e33c2482SVarun Prakash static int cxgbi_sock_tx_queue_up(struct cxgbi_sock *csk, struct sk_buff *skb)
2274e33c2482SVarun Prakash {
2275e33c2482SVarun Prakash 	struct cxgbi_device *cdev = csk->cdev;
2276e33c2482SVarun Prakash 	struct cxgbi_iso_info *iso_cpl;
2277e33c2482SVarun Prakash 	u32 frags = skb_shinfo(skb)->nr_frags;
2278e33c2482SVarun Prakash 	u32 extra_len, num_pdu, hdr_len;
2279e33c2482SVarun Prakash 	u32 iso_tx_rsvd = 0;
2280e33c2482SVarun Prakash 
2281e33c2482SVarun Prakash 	if (csk->state != CTP_ESTABLISHED) {
2282e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_PDU_TX,
2283e33c2482SVarun Prakash 			  "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
2284e33c2482SVarun Prakash 			  csk, csk->state, csk->flags, csk->tid);
2285e33c2482SVarun Prakash 		return -EPIPE;
2286e33c2482SVarun Prakash 	}
2287e33c2482SVarun Prakash 
2288e33c2482SVarun Prakash 	if (csk->err) {
2289e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_PDU_TX,
2290e33c2482SVarun Prakash 			  "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
2291e33c2482SVarun Prakash 			  csk, csk->state, csk->flags, csk->tid, csk->err);
2292e33c2482SVarun Prakash 		return -EPIPE;
2293e33c2482SVarun Prakash 	}
2294e33c2482SVarun Prakash 
2295e33c2482SVarun Prakash 	if ((cdev->flags & CXGBI_FLAG_DEV_T3) &&
2296e33c2482SVarun Prakash 	    before((csk->snd_win + csk->snd_una), csk->write_seq)) {
2297e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_PDU_TX,
2298e33c2482SVarun Prakash 			  "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
2299e33c2482SVarun Prakash 			  csk, csk->state, csk->flags, csk->tid, csk->write_seq,
2300e33c2482SVarun Prakash 			  csk->snd_una, csk->snd_win);
2301e33c2482SVarun Prakash 		return -ENOBUFS;
2302e33c2482SVarun Prakash 	}
2303e33c2482SVarun Prakash 
2304e33c2482SVarun Prakash 	if (cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO))
2305e33c2482SVarun Prakash 		iso_tx_rsvd = cdev->skb_iso_txhdr;
2306e33c2482SVarun Prakash 
2307e33c2482SVarun Prakash 	if (unlikely(skb_headroom(skb) < (cdev->skb_tx_rsvd + iso_tx_rsvd))) {
2308e33c2482SVarun Prakash 		pr_err("csk 0x%p, skb head %u < %u.\n",
2309e33c2482SVarun Prakash 		       csk, skb_headroom(skb), cdev->skb_tx_rsvd);
2310e33c2482SVarun Prakash 		return -EINVAL;
2311e33c2482SVarun Prakash 	}
2312e33c2482SVarun Prakash 
2313e33c2482SVarun Prakash 	if (skb->len != skb->data_len)
2314e33c2482SVarun Prakash 		frags++;
2315e33c2482SVarun Prakash 
2316e33c2482SVarun Prakash 	if (frags >= SKB_WR_LIST_SIZE) {
2317*3948b059SEric Dumazet 		pr_err("csk 0x%p, frags %u, %u,%u >%u.\n",
2318e33c2482SVarun Prakash 		       csk, skb_shinfo(skb)->nr_frags, skb->len,
2319*3948b059SEric Dumazet 		       skb->data_len, (unsigned int)SKB_WR_LIST_SIZE);
2320e33c2482SVarun Prakash 		return -EINVAL;
2321e33c2482SVarun Prakash 	}
2322e33c2482SVarun Prakash 
2323e33c2482SVarun Prakash 	cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
2324e33c2482SVarun Prakash 	skb_reset_transport_header(skb);
2325e33c2482SVarun Prakash 	cxgbi_sock_skb_entail(csk, skb);
2326e33c2482SVarun Prakash 
2327e33c2482SVarun Prakash 	extra_len = cxgbi_ulp_extra_len(cxgbi_skcb_tx_ulp_mode(skb));
2328e33c2482SVarun Prakash 
2329e33c2482SVarun Prakash 	if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_ISO))) {
2330e33c2482SVarun Prakash 		iso_cpl = (struct cxgbi_iso_info *)skb->head;
2331e33c2482SVarun Prakash 		num_pdu = iso_cpl->num_pdu;
2332e33c2482SVarun Prakash 		hdr_len = cxgbi_skcb_tx_iscsi_hdrlen(skb);
2333e33c2482SVarun Prakash 		extra_len = (cxgbi_ulp_extra_len(cxgbi_skcb_tx_ulp_mode(skb)) *
2334e33c2482SVarun Prakash 			     num_pdu) +	(hdr_len * (num_pdu - 1));
2335e33c2482SVarun Prakash 	}
2336e33c2482SVarun Prakash 
2337e33c2482SVarun Prakash 	csk->write_seq += (skb->len + extra_len);
2338e33c2482SVarun Prakash 
2339e33c2482SVarun Prakash 	return 0;
2340e33c2482SVarun Prakash }
2341e33c2482SVarun Prakash 
cxgbi_sock_send_skb(struct cxgbi_sock * csk,struct sk_buff * skb)2342e33c2482SVarun Prakash static int cxgbi_sock_send_skb(struct cxgbi_sock *csk, struct sk_buff *skb)
2343e33c2482SVarun Prakash {
2344e33c2482SVarun Prakash 	struct cxgbi_device *cdev = csk->cdev;
2345e33c2482SVarun Prakash 	int len = skb->len;
2346e33c2482SVarun Prakash 	int err;
2347e33c2482SVarun Prakash 
2348e33c2482SVarun Prakash 	spin_lock_bh(&csk->lock);
2349e33c2482SVarun Prakash 	err = cxgbi_sock_tx_queue_up(csk, skb);
2350e33c2482SVarun Prakash 	if (err < 0) {
2351e33c2482SVarun Prakash 		spin_unlock_bh(&csk->lock);
2352e33c2482SVarun Prakash 		return err;
2353e33c2482SVarun Prakash 	}
2354e33c2482SVarun Prakash 
2355e33c2482SVarun Prakash 	if (likely(skb_queue_len(&csk->write_queue)))
2356e33c2482SVarun Prakash 		cdev->csk_push_tx_frames(csk, 0);
2357e33c2482SVarun Prakash 	spin_unlock_bh(&csk->lock);
2358e33c2482SVarun Prakash 	return len;
2359e33c2482SVarun Prakash }
2360e33c2482SVarun Prakash 
cxgbi_conn_xmit_pdu(struct iscsi_task * task)23619ba682f0Skxie@chelsio.com int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
23629ba682f0Skxie@chelsio.com {
23639ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
23649ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
2365e33c2482SVarun Prakash 	struct iscsi_tcp_task *tcp_task = task->dd_data;
2366e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
236771f7a00bSVarun Prakash 	struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
2368b92a4a9fSColin Ian King 	struct sk_buff *skb;
236971f7a00bSVarun Prakash 	struct cxgbi_sock *csk = NULL;
2370e33c2482SVarun Prakash 	u32 pdulen = 0;
2371e33c2482SVarun Prakash 	u32 datalen;
23729ba682f0Skxie@chelsio.com 	int err;
23739ba682f0Skxie@chelsio.com 
237461965bf6SVarun Prakash 	if (!tcp_task || (tcp_task->dd_data != tdata)) {
2375e33c2482SVarun Prakash 		pr_err("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
2376e33c2482SVarun Prakash 		       task, task->sc, tcp_task,
2377e33c2482SVarun Prakash 		       tcp_task ? tcp_task->dd_data : NULL, tdata);
2378e33c2482SVarun Prakash 		return -EINVAL;
2379e33c2482SVarun Prakash 	}
2380e33c2482SVarun Prakash 
2381b92a4a9fSColin Ian King 	skb = tdata->skb;
238222698483SVarun Prakash 	if (!skb) {
23839ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2384e33c2482SVarun Prakash 			  "task 0x%p, skb NULL.\n", task);
23859ba682f0Skxie@chelsio.com 		return 0;
23869ba682f0Skxie@chelsio.com 	}
23879ba682f0Skxie@chelsio.com 
238871f7a00bSVarun Prakash 	if (cconn && cconn->cep)
238971f7a00bSVarun Prakash 		csk = cconn->cep->csk;
2390e33c2482SVarun Prakash 
239171f7a00bSVarun Prakash 	if (!csk) {
239271f7a00bSVarun Prakash 		log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
239371f7a00bSVarun Prakash 			  "task 0x%p, csk gone.\n", task);
239471f7a00bSVarun Prakash 		return -EPIPE;
239571f7a00bSVarun Prakash 	}
239671f7a00bSVarun Prakash 
239722698483SVarun Prakash 	tdata->skb = NULL;
23989ba682f0Skxie@chelsio.com 	datalen = skb->data_len;
239971f7a00bSVarun Prakash 
240071f7a00bSVarun Prakash 	/* write ppod first if using ofldq to write ppod */
240171f7a00bSVarun Prakash 	if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_VALID) {
240271f7a00bSVarun Prakash 		struct cxgbi_ppm *ppm = csk->cdev->cdev2ppm(csk->cdev);
240371f7a00bSVarun Prakash 
240471f7a00bSVarun Prakash 		ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_VALID;
240571f7a00bSVarun Prakash 		if (csk->cdev->csk_ddp_set_map(ppm, csk, ttinfo) < 0)
240671f7a00bSVarun Prakash 			pr_err("task 0x%p, ppod writing using ofldq failed.\n",
240771f7a00bSVarun Prakash 			       task);
240871f7a00bSVarun Prakash 			/* continue. Let fl get the data */
240971f7a00bSVarun Prakash 	}
241071f7a00bSVarun Prakash 
241122698483SVarun Prakash 	if (!task->sc)
241222698483SVarun Prakash 		memcpy(skb->data, task->hdr, SKB_TX_ISCSI_PDU_HEADER_MAX);
241322698483SVarun Prakash 
2414e33c2482SVarun Prakash 	err = cxgbi_sock_send_skb(csk, skb);
24159ba682f0Skxie@chelsio.com 	if (err > 0) {
2416e33c2482SVarun Prakash 		pdulen += err;
24179ba682f0Skxie@chelsio.com 
2418e33c2482SVarun Prakash 		log_debug(1 << CXGBI_DBG_PDU_TX, "task 0x%p,0x%p, rv %d.\n",
2419e33c2482SVarun Prakash 			  task, task->sc, err);
24209ba682f0Skxie@chelsio.com 
24219ba682f0Skxie@chelsio.com 		if (task->conn->hdrdgst_en)
24229ba682f0Skxie@chelsio.com 			pdulen += ISCSI_DIGEST_SIZE;
24239ba682f0Skxie@chelsio.com 
24249ba682f0Skxie@chelsio.com 		if (datalen && task->conn->datadgst_en)
24259ba682f0Skxie@chelsio.com 			pdulen += ISCSI_DIGEST_SIZE;
24269ba682f0Skxie@chelsio.com 
24279ba682f0Skxie@chelsio.com 		task->conn->txdata_octets += pdulen;
2428e33c2482SVarun Prakash 
2429e33c2482SVarun Prakash 		if (unlikely(cxgbi_is_iso_config(csk) && cxgbi_is_iso_disabled(csk))) {
2430e33c2482SVarun Prakash 			if (time_after(jiffies, csk->prev_iso_ts + HZ)) {
2431e33c2482SVarun Prakash 				csk->disable_iso = false;
2432e33c2482SVarun Prakash 				csk->prev_iso_ts = 0;
2433e33c2482SVarun Prakash 				log_debug(1 << CXGBI_DBG_PDU_TX,
2434e33c2482SVarun Prakash 					  "enable iso: csk 0x%p\n", csk);
2435e33c2482SVarun Prakash 			}
2436e33c2482SVarun Prakash 		}
2437e33c2482SVarun Prakash 
24389ba682f0Skxie@chelsio.com 		return 0;
24399ba682f0Skxie@chelsio.com 	}
24409ba682f0Skxie@chelsio.com 
24419ba682f0Skxie@chelsio.com 	if (err == -EAGAIN || err == -ENOBUFS) {
24429ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_PDU_TX,
24439ba682f0Skxie@chelsio.com 			  "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
24449ba682f0Skxie@chelsio.com 			  task, skb, skb->len, skb->data_len, err);
24459ba682f0Skxie@chelsio.com 		/* reset skb to send when we are called again */
244622698483SVarun Prakash 		tdata->skb = skb;
2447e33c2482SVarun Prakash 
2448e33c2482SVarun Prakash 		if (cxgbi_is_iso_config(csk) && !cxgbi_is_iso_disabled(csk) &&
2449e33c2482SVarun Prakash 		    (csk->no_tx_credits++ >= 2)) {
2450e33c2482SVarun Prakash 			csk->disable_iso = true;
2451e33c2482SVarun Prakash 			csk->prev_iso_ts = jiffies;
2452e33c2482SVarun Prakash 			log_debug(1 << CXGBI_DBG_PDU_TX,
2453e33c2482SVarun Prakash 				  "disable iso:csk 0x%p, ts:%lu\n",
2454e33c2482SVarun Prakash 				  csk, csk->prev_iso_ts);
2455e33c2482SVarun Prakash 		}
2456e33c2482SVarun Prakash 
24579ba682f0Skxie@chelsio.com 		return err;
24589ba682f0Skxie@chelsio.com 	}
24599ba682f0Skxie@chelsio.com 
24609ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
24619ba682f0Skxie@chelsio.com 		  "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
24629ba682f0Skxie@chelsio.com 		  task->itt, skb, skb->len, skb->data_len, err);
246338660389SDan Carpenter 	__kfree_skb(skb);
24649ba682f0Skxie@chelsio.com 	iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
24659ba682f0Skxie@chelsio.com 	iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
24669ba682f0Skxie@chelsio.com 	return err;
24679ba682f0Skxie@chelsio.com }
24689ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
24699ba682f0Skxie@chelsio.com 
cxgbi_cleanup_task(struct iscsi_task * task)24709ba682f0Skxie@chelsio.com void cxgbi_cleanup_task(struct iscsi_task *task)
24719ba682f0Skxie@chelsio.com {
247271f7a00bSVarun Prakash 	struct iscsi_tcp_task *tcp_task = task->dd_data;
2473e3d2ad8cSkxie@chelsio.com 	struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
24749ba682f0Skxie@chelsio.com 
247561965bf6SVarun Prakash 	if (!tcp_task || (tcp_task->dd_data != tdata)) {
247650292710SVarun Prakash 		pr_info("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
247750292710SVarun Prakash 			task, task->sc, tcp_task,
247850292710SVarun Prakash 			tcp_task ? tcp_task->dd_data : NULL, tdata);
247950292710SVarun Prakash 		return;
248050292710SVarun Prakash 	}
248150292710SVarun Prakash 
24829ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
24839ba682f0Skxie@chelsio.com 		"task 0x%p, skb 0x%p, itt 0x%x.\n",
24849ba682f0Skxie@chelsio.com 		task, tdata->skb, task->hdr_itt);
24859ba682f0Skxie@chelsio.com 
248671f7a00bSVarun Prakash 	tcp_task->dd_data = NULL;
248722698483SVarun Prakash 
248822698483SVarun Prakash 	if (!task->sc)
248922698483SVarun Prakash 		kfree(task->hdr);
249022698483SVarun Prakash 	task->hdr = NULL;
249122698483SVarun Prakash 
24929ba682f0Skxie@chelsio.com 	/*  never reached the xmit task callout */
249375b61250SVarun Prakash 	if (tdata->skb) {
249422698483SVarun Prakash 		__kfree_skb(tdata->skb);
249575b61250SVarun Prakash 		tdata->skb = NULL;
249675b61250SVarun Prakash 	}
24979ba682f0Skxie@chelsio.com 
24989ba682f0Skxie@chelsio.com 	task_release_itt(task, task->hdr_itt);
249969e2d1e6SVarun Prakash 	memset(tdata, 0, sizeof(*tdata));
250069e2d1e6SVarun Prakash 
25019ba682f0Skxie@chelsio.com 	iscsi_tcp_cleanup_task(task);
25029ba682f0Skxie@chelsio.com }
25039ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
25049ba682f0Skxie@chelsio.com 
cxgbi_get_conn_stats(struct iscsi_cls_conn * cls_conn,struct iscsi_stats * stats)25059ba682f0Skxie@chelsio.com void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
25069ba682f0Skxie@chelsio.com 				struct iscsi_stats *stats)
25079ba682f0Skxie@chelsio.com {
25089ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
25099ba682f0Skxie@chelsio.com 
25109ba682f0Skxie@chelsio.com 	stats->txdata_octets = conn->txdata_octets;
25119ba682f0Skxie@chelsio.com 	stats->rxdata_octets = conn->rxdata_octets;
25129ba682f0Skxie@chelsio.com 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
25139ba682f0Skxie@chelsio.com 	stats->dataout_pdus = conn->dataout_pdus_cnt;
25149ba682f0Skxie@chelsio.com 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
25159ba682f0Skxie@chelsio.com 	stats->datain_pdus = conn->datain_pdus_cnt;
25169ba682f0Skxie@chelsio.com 	stats->r2t_pdus = conn->r2t_pdus_cnt;
25179ba682f0Skxie@chelsio.com 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
25189ba682f0Skxie@chelsio.com 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
25199ba682f0Skxie@chelsio.com 	stats->digest_err = 0;
25209ba682f0Skxie@chelsio.com 	stats->timeout_err = 0;
25219ba682f0Skxie@chelsio.com 	stats->custom_length = 1;
25229ba682f0Skxie@chelsio.com 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
25239ba682f0Skxie@chelsio.com 	stats->custom[0].value = conn->eh_abort_cnt;
25249ba682f0Skxie@chelsio.com }
25259ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
25269ba682f0Skxie@chelsio.com 
cxgbi_conn_max_xmit_dlength(struct iscsi_conn * conn)25279ba682f0Skxie@chelsio.com static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
25289ba682f0Skxie@chelsio.com {
25299ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
25309ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
25319ba682f0Skxie@chelsio.com 	struct cxgbi_device *cdev = cconn->chba->cdev;
25329ba682f0Skxie@chelsio.com 	unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
25339ba682f0Skxie@chelsio.com 	unsigned int max_def = 512 * MAX_SKB_FRAGS;
25349ba682f0Skxie@chelsio.com 	unsigned int max = max(max_def, headroom);
25359ba682f0Skxie@chelsio.com 
25369ba682f0Skxie@chelsio.com 	max = min(cconn->chba->cdev->tx_max_size, max);
25379ba682f0Skxie@chelsio.com 	if (conn->max_xmit_dlength)
25389ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
25399ba682f0Skxie@chelsio.com 	else
25409ba682f0Skxie@chelsio.com 		conn->max_xmit_dlength = max;
25419ba682f0Skxie@chelsio.com 	cxgbi_align_pdu_size(conn->max_xmit_dlength);
25429ba682f0Skxie@chelsio.com 
25439ba682f0Skxie@chelsio.com 	return 0;
25449ba682f0Skxie@chelsio.com }
25459ba682f0Skxie@chelsio.com 
cxgbi_conn_max_recv_dlength(struct iscsi_conn * conn)25469ba682f0Skxie@chelsio.com static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
25479ba682f0Skxie@chelsio.com {
25489ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
25499ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
25509ba682f0Skxie@chelsio.com 	unsigned int max = cconn->chba->cdev->rx_max_size;
25519ba682f0Skxie@chelsio.com 
25529ba682f0Skxie@chelsio.com 	cxgbi_align_pdu_size(max);
25539ba682f0Skxie@chelsio.com 
25549ba682f0Skxie@chelsio.com 	if (conn->max_recv_dlength) {
25559ba682f0Skxie@chelsio.com 		if (conn->max_recv_dlength > max) {
25569ba682f0Skxie@chelsio.com 			pr_err("MaxRecvDataSegmentLength %u > %u.\n",
25579ba682f0Skxie@chelsio.com 				conn->max_recv_dlength, max);
25589ba682f0Skxie@chelsio.com 			return -EINVAL;
25599ba682f0Skxie@chelsio.com 		}
25609ba682f0Skxie@chelsio.com 		conn->max_recv_dlength = min(conn->max_recv_dlength, max);
25619ba682f0Skxie@chelsio.com 		cxgbi_align_pdu_size(conn->max_recv_dlength);
25629ba682f0Skxie@chelsio.com 	} else
25639ba682f0Skxie@chelsio.com 		conn->max_recv_dlength = max;
25649ba682f0Skxie@chelsio.com 
25659ba682f0Skxie@chelsio.com 	return 0;
25669ba682f0Skxie@chelsio.com }
25679ba682f0Skxie@chelsio.com 
cxgbi_set_conn_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf,int buflen)25689ba682f0Skxie@chelsio.com int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
25699ba682f0Skxie@chelsio.com 			enum iscsi_param param, char *buf, int buflen)
25709ba682f0Skxie@chelsio.com {
25719ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
25729ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
25739ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
25749ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cconn->cep->csk;
25751304be5fSMike Christie 	int err;
25769ba682f0Skxie@chelsio.com 
25779ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
25789ba682f0Skxie@chelsio.com 		"cls_conn 0x%p, param %d, buf(%d) %s.\n",
25799ba682f0Skxie@chelsio.com 		cls_conn, param, buflen, buf);
25809ba682f0Skxie@chelsio.com 
25819ba682f0Skxie@chelsio.com 	switch (param) {
25829ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_HDRDGST_EN:
25839ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
25849ba682f0Skxie@chelsio.com 		if (!err && conn->hdrdgst_en)
25859ba682f0Skxie@chelsio.com 			err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
25869ba682f0Skxie@chelsio.com 							conn->hdrdgst_en,
25879e8f1c79SVarun Prakash 							conn->datadgst_en);
25889ba682f0Skxie@chelsio.com 		break;
25899ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_DATADGST_EN:
25909ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
25919ba682f0Skxie@chelsio.com 		if (!err && conn->datadgst_en)
25929ba682f0Skxie@chelsio.com 			err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
25939ba682f0Skxie@chelsio.com 							conn->hdrdgst_en,
25949e8f1c79SVarun Prakash 							conn->datadgst_en);
25959ba682f0Skxie@chelsio.com 		break;
25969ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_R2T:
25971304be5fSMike Christie 		return iscsi_tcp_set_max_r2t(conn, buf);
25989ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
25999ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
26009ba682f0Skxie@chelsio.com 		if (!err)
26019ba682f0Skxie@chelsio.com 			err = cxgbi_conn_max_recv_dlength(conn);
26029ba682f0Skxie@chelsio.com 		break;
26039ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
26049ba682f0Skxie@chelsio.com 		err = iscsi_set_param(cls_conn, param, buf, buflen);
26059ba682f0Skxie@chelsio.com 		if (!err)
26069ba682f0Skxie@chelsio.com 			err = cxgbi_conn_max_xmit_dlength(conn);
26079ba682f0Skxie@chelsio.com 		break;
26089ba682f0Skxie@chelsio.com 	default:
26099ba682f0Skxie@chelsio.com 		return iscsi_set_param(cls_conn, param, buf, buflen);
26109ba682f0Skxie@chelsio.com 	}
26119ba682f0Skxie@chelsio.com 	return err;
26129ba682f0Skxie@chelsio.com }
26139ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
26149ba682f0Skxie@chelsio.com 
cxgbi_get_ep_param(struct iscsi_endpoint * ep,enum iscsi_param param,char * buf)2615c71b9b66SMike Christie int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2616c71b9b66SMike Christie 		       char *buf)
26179ba682f0Skxie@chelsio.com {
2618c71b9b66SMike Christie 	struct cxgbi_endpoint *cep = ep->dd_data;
2619c71b9b66SMike Christie 	struct cxgbi_sock *csk;
26209ba682f0Skxie@chelsio.com 
26219ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
2622c71b9b66SMike Christie 		"cls_conn 0x%p, param %d.\n", ep, param);
26239ba682f0Skxie@chelsio.com 
26249ba682f0Skxie@chelsio.com 	switch (param) {
26259ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_CONN_PORT:
26269ba682f0Skxie@chelsio.com 	case ISCSI_PARAM_CONN_ADDRESS:
2627c71b9b66SMike Christie 		if (!cep)
2628c71b9b66SMike Christie 			return -ENOTCONN;
2629c71b9b66SMike Christie 
2630c71b9b66SMike Christie 		csk = cep->csk;
2631c71b9b66SMike Christie 		if (!csk)
2632c71b9b66SMike Christie 			return -ENOTCONN;
2633c71b9b66SMike Christie 
2634c71b9b66SMike Christie 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2635c71b9b66SMike Christie 						 &csk->daddr, param, buf);
26369ba682f0Skxie@chelsio.com 	default:
263783785733SColin Ian King 		break;
26389ba682f0Skxie@chelsio.com 	}
263983785733SColin Ian King 	return -ENOSYS;
26409ba682f0Skxie@chelsio.com }
2641c71b9b66SMike Christie EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
26429ba682f0Skxie@chelsio.com 
26439ba682f0Skxie@chelsio.com struct iscsi_cls_conn *
cxgbi_create_conn(struct iscsi_cls_session * cls_session,u32 cid)26449ba682f0Skxie@chelsio.com cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
26459ba682f0Skxie@chelsio.com {
26469ba682f0Skxie@chelsio.com 	struct iscsi_cls_conn *cls_conn;
26479ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn;
26489ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn;
26499ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn;
26509ba682f0Skxie@chelsio.com 
26519ba682f0Skxie@chelsio.com 	cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
26529ba682f0Skxie@chelsio.com 	if (!cls_conn)
26539ba682f0Skxie@chelsio.com 		return NULL;
26549ba682f0Skxie@chelsio.com 
26559ba682f0Skxie@chelsio.com 	conn = cls_conn->dd_data;
26569ba682f0Skxie@chelsio.com 	tcp_conn = conn->dd_data;
26579ba682f0Skxie@chelsio.com 	cconn = tcp_conn->dd_data;
26589ba682f0Skxie@chelsio.com 	cconn->iconn = conn;
26599ba682f0Skxie@chelsio.com 
26609ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
26619ba682f0Skxie@chelsio.com 		"cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
26629ba682f0Skxie@chelsio.com 		cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
26639ba682f0Skxie@chelsio.com 
26649ba682f0Skxie@chelsio.com 	return cls_conn;
26659ba682f0Skxie@chelsio.com }
26669ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_create_conn);
26679ba682f0Skxie@chelsio.com 
cxgbi_bind_conn(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,u64 transport_eph,int is_leading)26689ba682f0Skxie@chelsio.com int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
26699ba682f0Skxie@chelsio.com 				struct iscsi_cls_conn *cls_conn,
26709ba682f0Skxie@chelsio.com 				u64 transport_eph, int is_leading)
26719ba682f0Skxie@chelsio.com {
26729ba682f0Skxie@chelsio.com 	struct iscsi_conn *conn = cls_conn->dd_data;
26739ba682f0Skxie@chelsio.com 	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
26749ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = tcp_conn->dd_data;
267571f7a00bSVarun Prakash 	struct cxgbi_ppm *ppm;
26769ba682f0Skxie@chelsio.com 	struct iscsi_endpoint *ep;
26779ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
26789ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
26799ba682f0Skxie@chelsio.com 	int err;
26809ba682f0Skxie@chelsio.com 
26819ba682f0Skxie@chelsio.com 	ep = iscsi_lookup_endpoint(transport_eph);
26829ba682f0Skxie@chelsio.com 	if (!ep)
26839ba682f0Skxie@chelsio.com 		return -EINVAL;
26849ba682f0Skxie@chelsio.com 
26859ba682f0Skxie@chelsio.com 	/*  setup ddp pagesize */
26869ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
26879ba682f0Skxie@chelsio.com 	csk = cep->csk;
26889ba682f0Skxie@chelsio.com 
268971f7a00bSVarun Prakash 	ppm = csk->cdev->cdev2ppm(csk->cdev);
269071f7a00bSVarun Prakash 	err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid,
26919e8f1c79SVarun Prakash 					     ppm->tformat.pgsz_idx_dflt);
269271f7a00bSVarun Prakash 	if (err < 0)
26939e5fe170SMike Christie 		goto put_ep;
269471f7a00bSVarun Prakash 
26959ba682f0Skxie@chelsio.com 	err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
26969e5fe170SMike Christie 	if (err) {
26979e5fe170SMike Christie 		err = -EINVAL;
26989e5fe170SMike Christie 		goto put_ep;
26999e5fe170SMike Christie 	}
27009ba682f0Skxie@chelsio.com 
27019ba682f0Skxie@chelsio.com 	/*  calculate the tag idx bits needed for this conn based on cmds_max */
27029ba682f0Skxie@chelsio.com 	cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
27039ba682f0Skxie@chelsio.com 
2704e3d2ad8cSkxie@chelsio.com 	write_lock_bh(&csk->callback_lock);
27059ba682f0Skxie@chelsio.com 	csk->user_data = conn;
27069ba682f0Skxie@chelsio.com 	cconn->chba = cep->chba;
27079ba682f0Skxie@chelsio.com 	cconn->cep = cep;
27089ba682f0Skxie@chelsio.com 	cep->cconn = cconn;
2709e3d2ad8cSkxie@chelsio.com 	write_unlock_bh(&csk->callback_lock);
27109ba682f0Skxie@chelsio.com 
27119ba682f0Skxie@chelsio.com 	cxgbi_conn_max_xmit_dlength(conn);
27129ba682f0Skxie@chelsio.com 	cxgbi_conn_max_recv_dlength(conn);
27139ba682f0Skxie@chelsio.com 
27149ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
27159ba682f0Skxie@chelsio.com 		"cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
27169ba682f0Skxie@chelsio.com 		cls_session, cls_conn, ep, cconn, csk);
27179ba682f0Skxie@chelsio.com 	/*  init recv engine */
27189ba682f0Skxie@chelsio.com 	iscsi_tcp_hdr_recv_prep(tcp_conn);
27199ba682f0Skxie@chelsio.com 
27209e5fe170SMike Christie put_ep:
27219e5fe170SMike Christie 	iscsi_put_endpoint(ep);
27229e5fe170SMike Christie 	return err;
27239ba682f0Skxie@chelsio.com }
27249ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
27259ba682f0Skxie@chelsio.com 
cxgbi_create_session(struct iscsi_endpoint * ep,u16 cmds_max,u16 qdepth,u32 initial_cmdsn)27269ba682f0Skxie@chelsio.com struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
27279ba682f0Skxie@chelsio.com 						u16 cmds_max, u16 qdepth,
27289ba682f0Skxie@chelsio.com 						u32 initial_cmdsn)
27299ba682f0Skxie@chelsio.com {
27309ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
27319ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba;
27329ba682f0Skxie@chelsio.com 	struct Scsi_Host *shost;
27339ba682f0Skxie@chelsio.com 	struct iscsi_cls_session *cls_session;
27349ba682f0Skxie@chelsio.com 	struct iscsi_session *session;
27359ba682f0Skxie@chelsio.com 
27369ba682f0Skxie@chelsio.com 	if (!ep) {
27379ba682f0Skxie@chelsio.com 		pr_err("missing endpoint.\n");
27389ba682f0Skxie@chelsio.com 		return NULL;
27399ba682f0Skxie@chelsio.com 	}
27409ba682f0Skxie@chelsio.com 
27419ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
27429ba682f0Skxie@chelsio.com 	chba = cep->chba;
27439ba682f0Skxie@chelsio.com 	shost = chba->shost;
27449ba682f0Skxie@chelsio.com 
27459ba682f0Skxie@chelsio.com 	BUG_ON(chba != iscsi_host_priv(shost));
27469ba682f0Skxie@chelsio.com 
27479ba682f0Skxie@chelsio.com 	cls_session = iscsi_session_setup(chba->cdev->itp, shost,
27489ba682f0Skxie@chelsio.com 					cmds_max, 0,
27499ba682f0Skxie@chelsio.com 					sizeof(struct iscsi_tcp_task) +
27509ba682f0Skxie@chelsio.com 					sizeof(struct cxgbi_task_data),
27519ba682f0Skxie@chelsio.com 					initial_cmdsn, ISCSI_MAX_TARGET);
27529ba682f0Skxie@chelsio.com 	if (!cls_session)
27539ba682f0Skxie@chelsio.com 		return NULL;
27549ba682f0Skxie@chelsio.com 
27559ba682f0Skxie@chelsio.com 	session = cls_session->dd_data;
27569ba682f0Skxie@chelsio.com 	if (iscsi_tcp_r2tpool_alloc(session))
27579ba682f0Skxie@chelsio.com 		goto remove_session;
27589ba682f0Skxie@chelsio.com 
27599ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
27609ba682f0Skxie@chelsio.com 		"ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
27619ba682f0Skxie@chelsio.com 	return cls_session;
27629ba682f0Skxie@chelsio.com 
27639ba682f0Skxie@chelsio.com remove_session:
27649ba682f0Skxie@chelsio.com 	iscsi_session_teardown(cls_session);
27659ba682f0Skxie@chelsio.com 	return NULL;
27669ba682f0Skxie@chelsio.com }
27679ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_create_session);
27689ba682f0Skxie@chelsio.com 
cxgbi_destroy_session(struct iscsi_cls_session * cls_session)27699ba682f0Skxie@chelsio.com void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
27709ba682f0Skxie@chelsio.com {
27719ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
27729ba682f0Skxie@chelsio.com 		"cls sess 0x%p.\n", cls_session);
27739ba682f0Skxie@chelsio.com 
27749ba682f0Skxie@chelsio.com 	iscsi_tcp_r2tpool_free(cls_session->dd_data);
27759ba682f0Skxie@chelsio.com 	iscsi_session_teardown(cls_session);
27769ba682f0Skxie@chelsio.com }
27779ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
27789ba682f0Skxie@chelsio.com 
cxgbi_set_host_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf,int buflen)27799ba682f0Skxie@chelsio.com int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
27809ba682f0Skxie@chelsio.com 			char *buf, int buflen)
27819ba682f0Skxie@chelsio.com {
27829ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = iscsi_host_priv(shost);
27839ba682f0Skxie@chelsio.com 
27849ba682f0Skxie@chelsio.com 	if (!chba->ndev) {
27859ba682f0Skxie@chelsio.com 		shost_printk(KERN_ERR, shost, "Could not get host param. "
27869ba682f0Skxie@chelsio.com 				"netdev for host not set.\n");
27879ba682f0Skxie@chelsio.com 		return -ENODEV;
27889ba682f0Skxie@chelsio.com 	}
27899ba682f0Skxie@chelsio.com 
27909ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
27919ba682f0Skxie@chelsio.com 		"shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
27929ba682f0Skxie@chelsio.com 		shost, chba, chba->ndev->name, param, buflen, buf);
27939ba682f0Skxie@chelsio.com 
27949ba682f0Skxie@chelsio.com 	switch (param) {
27959ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_IPADDRESS:
27969ba682f0Skxie@chelsio.com 	{
27979ba682f0Skxie@chelsio.com 		__be32 addr = in_aton(buf);
27989ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
27999ba682f0Skxie@chelsio.com 			"hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
28009ba682f0Skxie@chelsio.com 		cxgbi_set_iscsi_ipv4(chba, addr);
28019ba682f0Skxie@chelsio.com 		return 0;
28029ba682f0Skxie@chelsio.com 	}
28039ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_HWADDRESS:
28049ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_NETDEV_NAME:
28059ba682f0Skxie@chelsio.com 		return 0;
28069ba682f0Skxie@chelsio.com 	default:
28079ba682f0Skxie@chelsio.com 		return iscsi_host_set_param(shost, param, buf, buflen);
28089ba682f0Skxie@chelsio.com 	}
28099ba682f0Skxie@chelsio.com }
28109ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
28119ba682f0Skxie@chelsio.com 
cxgbi_get_host_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf)28129ba682f0Skxie@chelsio.com int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
28139ba682f0Skxie@chelsio.com 			char *buf)
28149ba682f0Skxie@chelsio.com {
28159ba682f0Skxie@chelsio.com 	struct cxgbi_hba *chba = iscsi_host_priv(shost);
28169ba682f0Skxie@chelsio.com 	int len = 0;
28179ba682f0Skxie@chelsio.com 
28189ba682f0Skxie@chelsio.com 	if (!chba->ndev) {
28199ba682f0Skxie@chelsio.com 		shost_printk(KERN_ERR, shost, "Could not get host param. "
28209ba682f0Skxie@chelsio.com 				"netdev for host not set.\n");
28219ba682f0Skxie@chelsio.com 		return -ENODEV;
28229ba682f0Skxie@chelsio.com 	}
28239ba682f0Skxie@chelsio.com 
28249ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
28259ba682f0Skxie@chelsio.com 		"shost 0x%p, hba 0x%p,%s, param %d.\n",
28269ba682f0Skxie@chelsio.com 		shost, chba, chba->ndev->name, param);
28279ba682f0Skxie@chelsio.com 
28289ba682f0Skxie@chelsio.com 	switch (param) {
28299ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_HWADDRESS:
28309ba682f0Skxie@chelsio.com 		len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
28319ba682f0Skxie@chelsio.com 		break;
28329ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_NETDEV_NAME:
28339ba682f0Skxie@chelsio.com 		len = sprintf(buf, "%s\n", chba->ndev->name);
28349ba682f0Skxie@chelsio.com 		break;
28359ba682f0Skxie@chelsio.com 	case ISCSI_HOST_PARAM_IPADDRESS:
28369ba682f0Skxie@chelsio.com 	{
2837dd9ad67eSAnish Bhatt 		struct cxgbi_sock *csk = find_sock_on_port(chba->cdev,
2838dd9ad67eSAnish Bhatt 							   chba->port_id);
2839dd9ad67eSAnish Bhatt 		if (csk) {
2840dd9ad67eSAnish Bhatt 			len = sprintf(buf, "%pIS",
2841dd9ad67eSAnish Bhatt 				      (struct sockaddr *)&csk->saddr);
2842dd9ad67eSAnish Bhatt 		}
28439ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
2844dd9ad67eSAnish Bhatt 			  "hba %s, addr %s.\n", chba->ndev->name, buf);
28459ba682f0Skxie@chelsio.com 		break;
28469ba682f0Skxie@chelsio.com 	}
28479ba682f0Skxie@chelsio.com 	default:
28489ba682f0Skxie@chelsio.com 		return iscsi_host_get_param(shost, param, buf);
28499ba682f0Skxie@chelsio.com 	}
28509ba682f0Skxie@chelsio.com 
28519ba682f0Skxie@chelsio.com 	return len;
28529ba682f0Skxie@chelsio.com }
28539ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
28549ba682f0Skxie@chelsio.com 
cxgbi_ep_connect(struct Scsi_Host * shost,struct sockaddr * dst_addr,int non_blocking)28559ba682f0Skxie@chelsio.com struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
28569ba682f0Skxie@chelsio.com 					struct sockaddr *dst_addr,
28579ba682f0Skxie@chelsio.com 					int non_blocking)
28589ba682f0Skxie@chelsio.com {
28599ba682f0Skxie@chelsio.com 	struct iscsi_endpoint *ep;
28609ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep;
28619ba682f0Skxie@chelsio.com 	struct cxgbi_hba *hba = NULL;
28629ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk;
28634737c5a0SVarun Prakash 	int ifindex = 0;
28649ba682f0Skxie@chelsio.com 	int err = -EINVAL;
28659ba682f0Skxie@chelsio.com 
28669ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
28679ba682f0Skxie@chelsio.com 		"shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
28689ba682f0Skxie@chelsio.com 		shost, non_blocking, dst_addr);
28699ba682f0Skxie@chelsio.com 
28709ba682f0Skxie@chelsio.com 	if (shost) {
28719ba682f0Skxie@chelsio.com 		hba = iscsi_host_priv(shost);
28729ba682f0Skxie@chelsio.com 		if (!hba) {
28739ba682f0Skxie@chelsio.com 			pr_info("shost 0x%p, priv NULL.\n", shost);
28749ba682f0Skxie@chelsio.com 			goto err_out;
28759ba682f0Skxie@chelsio.com 		}
28769ba682f0Skxie@chelsio.com 	}
28779ba682f0Skxie@chelsio.com 
287874dea0beSVarun Prakash check_route:
2879fc8d0590SAnish Bhatt 	if (dst_addr->sa_family == AF_INET) {
28804737c5a0SVarun Prakash 		csk = cxgbi_check_route(dst_addr, ifindex);
2881e81fbf6cSAnish Bhatt #if IS_ENABLED(CONFIG_IPV6)
2882fc8d0590SAnish Bhatt 	} else if (dst_addr->sa_family == AF_INET6) {
28834737c5a0SVarun Prakash 		csk = cxgbi_check_route6(dst_addr, ifindex);
2884e81fbf6cSAnish Bhatt #endif
2885fc8d0590SAnish Bhatt 	} else {
2886fc8d0590SAnish Bhatt 		pr_info("address family 0x%x NOT supported.\n",
2887fc8d0590SAnish Bhatt 			dst_addr->sa_family);
2888fc8d0590SAnish Bhatt 		err = -EAFNOSUPPORT;
2889fc8d0590SAnish Bhatt 		return (struct iscsi_endpoint *)ERR_PTR(err);
2890fc8d0590SAnish Bhatt 	}
2891fc8d0590SAnish Bhatt 
28929ba682f0Skxie@chelsio.com 	if (IS_ERR(csk))
28939ba682f0Skxie@chelsio.com 		return (struct iscsi_endpoint *)csk;
28949ba682f0Skxie@chelsio.com 	cxgbi_sock_get(csk);
28959ba682f0Skxie@chelsio.com 
28969ba682f0Skxie@chelsio.com 	if (!hba)
28979ba682f0Skxie@chelsio.com 		hba = csk->cdev->hbas[csk->port_id];
28989ba682f0Skxie@chelsio.com 	else if (hba != csk->cdev->hbas[csk->port_id]) {
289974dea0beSVarun Prakash 		if (ifindex != hba->ndev->ifindex) {
290074dea0beSVarun Prakash 			cxgbi_sock_put(csk);
290174dea0beSVarun Prakash 			cxgbi_sock_closed(csk);
290274dea0beSVarun Prakash 			ifindex = hba->ndev->ifindex;
290374dea0beSVarun Prakash 			goto check_route;
290474dea0beSVarun Prakash 		}
290574dea0beSVarun Prakash 
29069ba682f0Skxie@chelsio.com 		pr_info("Could not connect through requested host %u"
29079ba682f0Skxie@chelsio.com 			"hba 0x%p != 0x%p (%u).\n",
29089ba682f0Skxie@chelsio.com 			shost->host_no, hba,
29099ba682f0Skxie@chelsio.com 			csk->cdev->hbas[csk->port_id], csk->port_id);
29109ba682f0Skxie@chelsio.com 		err = -ENOSPC;
29119ba682f0Skxie@chelsio.com 		goto release_conn;
29129ba682f0Skxie@chelsio.com 	}
29139ba682f0Skxie@chelsio.com 
29149ba682f0Skxie@chelsio.com 	err = sock_get_port(csk);
29159ba682f0Skxie@chelsio.com 	if (err)
29169ba682f0Skxie@chelsio.com 		goto release_conn;
29179ba682f0Skxie@chelsio.com 
29189ba682f0Skxie@chelsio.com 	cxgbi_sock_set_state(csk, CTP_CONNECTING);
29199ba682f0Skxie@chelsio.com 	err = csk->cdev->csk_init_act_open(csk);
29209ba682f0Skxie@chelsio.com 	if (err)
29219ba682f0Skxie@chelsio.com 		goto release_conn;
29229ba682f0Skxie@chelsio.com 
29239ba682f0Skxie@chelsio.com 	if (cxgbi_sock_is_closing(csk)) {
29249ba682f0Skxie@chelsio.com 		err = -ENOSPC;
29259ba682f0Skxie@chelsio.com 		pr_info("csk 0x%p is closing.\n", csk);
29269ba682f0Skxie@chelsio.com 		goto release_conn;
29279ba682f0Skxie@chelsio.com 	}
29289ba682f0Skxie@chelsio.com 
29299ba682f0Skxie@chelsio.com 	ep = iscsi_create_endpoint(sizeof(*cep));
29309ba682f0Skxie@chelsio.com 	if (!ep) {
29319ba682f0Skxie@chelsio.com 		err = -ENOMEM;
29329ba682f0Skxie@chelsio.com 		pr_info("iscsi alloc ep, OOM.\n");
29339ba682f0Skxie@chelsio.com 		goto release_conn;
29349ba682f0Skxie@chelsio.com 	}
29359ba682f0Skxie@chelsio.com 
29369ba682f0Skxie@chelsio.com 	cep = ep->dd_data;
29379ba682f0Skxie@chelsio.com 	cep->csk = csk;
29389ba682f0Skxie@chelsio.com 	cep->chba = hba;
29399ba682f0Skxie@chelsio.com 
29409ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
29419ba682f0Skxie@chelsio.com 		"ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
29429ba682f0Skxie@chelsio.com 		ep, cep, csk, hba, hba->ndev->name);
29439ba682f0Skxie@chelsio.com 	return ep;
29449ba682f0Skxie@chelsio.com 
29459ba682f0Skxie@chelsio.com release_conn:
29469ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
29479ba682f0Skxie@chelsio.com 	cxgbi_sock_closed(csk);
29489ba682f0Skxie@chelsio.com err_out:
29499ba682f0Skxie@chelsio.com 	return ERR_PTR(err);
29509ba682f0Skxie@chelsio.com }
29519ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
29529ba682f0Skxie@chelsio.com 
cxgbi_ep_poll(struct iscsi_endpoint * ep,int timeout_ms)29539ba682f0Skxie@chelsio.com int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
29549ba682f0Skxie@chelsio.com {
29559ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep = ep->dd_data;
29569ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cep->csk;
29579ba682f0Skxie@chelsio.com 
29589ba682f0Skxie@chelsio.com 	if (!cxgbi_sock_is_established(csk))
29599ba682f0Skxie@chelsio.com 		return 0;
29609ba682f0Skxie@chelsio.com 	return 1;
29619ba682f0Skxie@chelsio.com }
29629ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
29639ba682f0Skxie@chelsio.com 
cxgbi_ep_disconnect(struct iscsi_endpoint * ep)29649ba682f0Skxie@chelsio.com void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
29659ba682f0Skxie@chelsio.com {
29669ba682f0Skxie@chelsio.com 	struct cxgbi_endpoint *cep = ep->dd_data;
29679ba682f0Skxie@chelsio.com 	struct cxgbi_conn *cconn = cep->cconn;
29689ba682f0Skxie@chelsio.com 	struct cxgbi_sock *csk = cep->csk;
29699ba682f0Skxie@chelsio.com 
29709ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
29719ba682f0Skxie@chelsio.com 		"ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
29729ba682f0Skxie@chelsio.com 		ep, cep, cconn, csk, csk->state, csk->flags);
29739ba682f0Skxie@chelsio.com 
29749ba682f0Skxie@chelsio.com 	if (cconn && cconn->iconn) {
29759ba682f0Skxie@chelsio.com 		write_lock_bh(&csk->callback_lock);
29769ba682f0Skxie@chelsio.com 		cep->csk->user_data = NULL;
29779ba682f0Skxie@chelsio.com 		cconn->cep = NULL;
29789ba682f0Skxie@chelsio.com 		write_unlock_bh(&csk->callback_lock);
29799ba682f0Skxie@chelsio.com 	}
29809ba682f0Skxie@chelsio.com 	iscsi_destroy_endpoint(ep);
29819ba682f0Skxie@chelsio.com 
29829ba682f0Skxie@chelsio.com 	if (likely(csk->state >= CTP_ESTABLISHED))
29839ba682f0Skxie@chelsio.com 		need_active_close(csk);
29849ba682f0Skxie@chelsio.com 	else
29859ba682f0Skxie@chelsio.com 		cxgbi_sock_closed(csk);
29869ba682f0Skxie@chelsio.com 
29879ba682f0Skxie@chelsio.com 	cxgbi_sock_put(csk);
29889ba682f0Skxie@chelsio.com }
29899ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
29909ba682f0Skxie@chelsio.com 
cxgbi_iscsi_init(struct iscsi_transport * itp,struct scsi_transport_template ** stt)29919ba682f0Skxie@chelsio.com int cxgbi_iscsi_init(struct iscsi_transport *itp,
29929ba682f0Skxie@chelsio.com 			struct scsi_transport_template **stt)
29939ba682f0Skxie@chelsio.com {
29949ba682f0Skxie@chelsio.com 	*stt = iscsi_register_transport(itp);
29959ba682f0Skxie@chelsio.com 	if (*stt == NULL) {
29969ba682f0Skxie@chelsio.com 		pr_err("unable to register %s transport 0x%p.\n",
29979ba682f0Skxie@chelsio.com 			itp->name, itp);
29989ba682f0Skxie@chelsio.com 		return -ENODEV;
29999ba682f0Skxie@chelsio.com 	}
30009ba682f0Skxie@chelsio.com 	log_debug(1 << CXGBI_DBG_ISCSI,
30019ba682f0Skxie@chelsio.com 		"%s, registered iscsi transport 0x%p.\n",
30029ba682f0Skxie@chelsio.com 		itp->name, stt);
30039ba682f0Skxie@chelsio.com 	return 0;
30049ba682f0Skxie@chelsio.com }
30059ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
30069ba682f0Skxie@chelsio.com 
cxgbi_iscsi_cleanup(struct iscsi_transport * itp,struct scsi_transport_template ** stt)30079ba682f0Skxie@chelsio.com void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
30089ba682f0Skxie@chelsio.com 			struct scsi_transport_template **stt)
30099ba682f0Skxie@chelsio.com {
30109ba682f0Skxie@chelsio.com 	if (*stt) {
30119ba682f0Skxie@chelsio.com 		log_debug(1 << CXGBI_DBG_ISCSI,
30129ba682f0Skxie@chelsio.com 			"de-register transport 0x%p, %s, stt 0x%p.\n",
30139ba682f0Skxie@chelsio.com 			itp, itp->name, *stt);
30149ba682f0Skxie@chelsio.com 		*stt = NULL;
30159ba682f0Skxie@chelsio.com 		iscsi_unregister_transport(itp);
30169ba682f0Skxie@chelsio.com 	}
30179ba682f0Skxie@chelsio.com }
30189ba682f0Skxie@chelsio.com EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
30199ba682f0Skxie@chelsio.com 
cxgbi_attr_is_visible(int param_type,int param)3020587a1f16SAl Viro umode_t cxgbi_attr_is_visible(int param_type, int param)
30213128c6c7SMike Christie {
30223128c6c7SMike Christie 	switch (param_type) {
3023f27fb2efSMike Christie 	case ISCSI_HOST_PARAM:
3024f27fb2efSMike Christie 		switch (param) {
3025f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_NETDEV_NAME:
3026f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_HWADDRESS:
3027f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_IPADDRESS:
3028f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
3029f27fb2efSMike Christie 			return S_IRUGO;
3030f27fb2efSMike Christie 		default:
3031f27fb2efSMike Christie 			return 0;
3032f27fb2efSMike Christie 		}
30333128c6c7SMike Christie 	case ISCSI_PARAM:
30343128c6c7SMike Christie 		switch (param) {
30353128c6c7SMike Christie 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
30363128c6c7SMike Christie 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
30373128c6c7SMike Christie 		case ISCSI_PARAM_HDRDGST_EN:
30383128c6c7SMike Christie 		case ISCSI_PARAM_DATADGST_EN:
30393128c6c7SMike Christie 		case ISCSI_PARAM_CONN_ADDRESS:
30403128c6c7SMike Christie 		case ISCSI_PARAM_CONN_PORT:
30413128c6c7SMike Christie 		case ISCSI_PARAM_EXP_STATSN:
30423128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
30433128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_PORT:
30443128c6c7SMike Christie 		case ISCSI_PARAM_PING_TMO:
30453128c6c7SMike Christie 		case ISCSI_PARAM_RECV_TMO:
30461d063c17SMike Christie 		case ISCSI_PARAM_INITIAL_R2T_EN:
30471d063c17SMike Christie 		case ISCSI_PARAM_MAX_R2T:
30481d063c17SMike Christie 		case ISCSI_PARAM_IMM_DATA_EN:
30491d063c17SMike Christie 		case ISCSI_PARAM_FIRST_BURST:
30501d063c17SMike Christie 		case ISCSI_PARAM_MAX_BURST:
30511d063c17SMike Christie 		case ISCSI_PARAM_PDU_INORDER_EN:
30521d063c17SMike Christie 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
30531d063c17SMike Christie 		case ISCSI_PARAM_ERL:
30541d063c17SMike Christie 		case ISCSI_PARAM_TARGET_NAME:
30551d063c17SMike Christie 		case ISCSI_PARAM_TPGT:
30561d063c17SMike Christie 		case ISCSI_PARAM_USERNAME:
30571d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD:
30581d063c17SMike Christie 		case ISCSI_PARAM_USERNAME_IN:
30591d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD_IN:
30601d063c17SMike Christie 		case ISCSI_PARAM_FAST_ABORT:
30611d063c17SMike Christie 		case ISCSI_PARAM_ABORT_TMO:
30621d063c17SMike Christie 		case ISCSI_PARAM_LU_RESET_TMO:
30631d063c17SMike Christie 		case ISCSI_PARAM_TGT_RESET_TMO:
30641d063c17SMike Christie 		case ISCSI_PARAM_IFACE_NAME:
30651d063c17SMike Christie 		case ISCSI_PARAM_INITIATOR_NAME:
30663128c6c7SMike Christie 			return S_IRUGO;
30673128c6c7SMike Christie 		default:
30683128c6c7SMike Christie 			return 0;
30693128c6c7SMike Christie 		}
30703128c6c7SMike Christie 	}
30713128c6c7SMike Christie 
30723128c6c7SMike Christie 	return 0;
30733128c6c7SMike Christie }
30743128c6c7SMike Christie EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
30753128c6c7SMike Christie 
libcxgbi_init_module(void)30769ba682f0Skxie@chelsio.com static int __init libcxgbi_init_module(void)
30779ba682f0Skxie@chelsio.com {
30780ea5bf3dSKaren Xie 	pr_info("%s", version);
307975b61250SVarun Prakash 
3080c593642cSPankaj Bharadiya 	BUILD_BUG_ON(sizeof_field(struct sk_buff, cb) <
308175b61250SVarun Prakash 		     sizeof(struct cxgbi_skb_cb));
3082e33c2482SVarun Prakash 	rsvd_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3083e33c2482SVarun Prakash 	if (!rsvd_page)
3084e33c2482SVarun Prakash 		return -ENOMEM;
3085e33c2482SVarun Prakash 
30869ba682f0Skxie@chelsio.com 	return 0;
30879ba682f0Skxie@chelsio.com }
30889ba682f0Skxie@chelsio.com 
libcxgbi_exit_module(void)30899ba682f0Skxie@chelsio.com static void __exit libcxgbi_exit_module(void)
30909ba682f0Skxie@chelsio.com {
30919ba682f0Skxie@chelsio.com 	cxgbi_device_unregister_all(0xFF);
3092e33c2482SVarun Prakash 	put_page(rsvd_page);
30939ba682f0Skxie@chelsio.com 	return;
30949ba682f0Skxie@chelsio.com }
30959ba682f0Skxie@chelsio.com 
30969ba682f0Skxie@chelsio.com module_init(libcxgbi_init_module);
30979ba682f0Skxie@chelsio.com module_exit(libcxgbi_exit_module);
3098