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