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