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