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