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