b2acdef3dcb728d2cbdfa0c6cf9cac37f4011cff
[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, 0))
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         tag = cxgbi_ddp_tag_base(tformat, sw_tag);
1281         tag |= idx << PPOD_IDX_SHIFT;
1282
1283         hdr.rsvd = 0;
1284         hdr.vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
1285         hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
1286         hdr.max_offset = htonl(gl->length);
1287         hdr.page_offset = htonl(gl->offset);
1288
1289         err = cdev->csk_ddp_set(csk, &hdr, idx, npods, gl);
1290         if (err < 0)
1291                 goto unmark_entries;
1292
1293         ddp->idx_last = idx;
1294         log_debug(1 << CXGBI_DBG_DDP,
1295                 "xfer %u, gl %u,%u, tid 0x%x, tag 0x%x->0x%x(%u,%u).\n",
1296                 gl->length, gl->nelem, gl->offset, tid, sw_tag, tag, idx,
1297                 npods);
1298         *tagp = tag;
1299         return 0;
1300
1301 unmark_entries:
1302         ddp_unmark_entries(ddp, idx, npods);
1303         return err;
1304 }
1305
1306 int cxgbi_ddp_reserve(struct cxgbi_sock *csk, unsigned int *tagp,
1307                         unsigned int sw_tag, unsigned int xferlen,
1308                         struct scatterlist *sgl, unsigned int sgcnt, gfp_t gfp)
1309 {
1310         struct cxgbi_device *cdev = csk->cdev;
1311         struct cxgbi_tag_format *tformat = &cdev->tag_format;
1312         struct cxgbi_gather_list *gl;
1313         int err;
1314
1315         if (page_idx >= DDP_PGIDX_MAX || !cdev->ddp ||
1316             xferlen < DDP_THRESHOLD) {
1317                 log_debug(1 << CXGBI_DBG_DDP,
1318                         "pgidx %u, xfer %u, NO ddp.\n", page_idx, xferlen);
1319                 return -EINVAL;
1320         }
1321
1322         if (!cxgbi_sw_tag_usable(tformat, sw_tag)) {
1323                 log_debug(1 << CXGBI_DBG_DDP,
1324                         "sw_tag 0x%x NOT usable.\n", sw_tag);
1325                 return -EINVAL;
1326         }
1327
1328         gl = ddp_make_gl(xferlen, sgl, sgcnt, cdev->pdev, gfp);
1329         if (!gl)
1330                 return -ENOMEM;
1331
1332         err = ddp_tag_reserve(csk, csk->tid, sw_tag, tagp, gl, gfp);
1333         if (err < 0)
1334                 ddp_release_gl(gl, cdev->pdev);
1335
1336         return err;
1337 }
1338
1339 static void ddp_destroy(struct kref *kref)
1340 {
1341         struct cxgbi_ddp_info *ddp = container_of(kref,
1342                                                 struct cxgbi_ddp_info,
1343                                                 refcnt);
1344         struct cxgbi_device *cdev = ddp->cdev;
1345         int i = 0;
1346
1347         pr_info("kref 0, destroy ddp 0x%p, cdev 0x%p.\n", ddp, cdev);
1348
1349         while (i < ddp->nppods) {
1350                 struct cxgbi_gather_list *gl = ddp->gl_map[i];
1351
1352                 if (gl) {
1353                         int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
1354                                         >> PPOD_PAGES_SHIFT;
1355                         pr_info("cdev 0x%p, ddp %d + %d.\n", cdev, i, npods);
1356                         kfree(gl);
1357                         i += npods;
1358                 } else
1359                         i++;
1360         }
1361         cxgbi_free_big_mem(ddp);
1362 }
1363
1364 int cxgbi_ddp_cleanup(struct cxgbi_device *cdev)
1365 {
1366         struct cxgbi_ddp_info *ddp = cdev->ddp;
1367
1368         log_debug(1 << CXGBI_DBG_DDP,
1369                 "cdev 0x%p, release ddp 0x%p.\n", cdev, ddp);
1370         cdev->ddp = NULL;
1371         if (ddp)
1372                 return kref_put(&ddp->refcnt, ddp_destroy);
1373         return 0;
1374 }
1375 EXPORT_SYMBOL_GPL(cxgbi_ddp_cleanup);
1376
1377 int cxgbi_ddp_init(struct cxgbi_device *cdev,
1378                    unsigned int llimit, unsigned int ulimit,
1379                    unsigned int max_txsz, unsigned int max_rxsz)
1380 {
1381         struct cxgbi_ddp_info *ddp;
1382         unsigned int ppmax, bits;
1383
1384         ppmax = (ulimit - llimit + 1) >> PPOD_SIZE_SHIFT;
1385         bits = __ilog2_u32(ppmax) + 1;
1386         if (bits > PPOD_IDX_MAX_SIZE)
1387                 bits = PPOD_IDX_MAX_SIZE;
1388         ppmax = (1 << (bits - 1)) - 1;
1389
1390         ddp = cxgbi_alloc_big_mem(sizeof(struct cxgbi_ddp_info) +
1391                                 ppmax * (sizeof(struct cxgbi_gather_list *) +
1392                                          sizeof(struct sk_buff *)),
1393                                 GFP_KERNEL);
1394         if (!ddp) {
1395                 pr_warn("cdev 0x%p, ddp ppmax %u OOM.\n", cdev, ppmax);
1396                 return -ENOMEM;
1397         }
1398         ddp->gl_map = (struct cxgbi_gather_list **)(ddp + 1);
1399         cdev->ddp = ddp;
1400
1401         spin_lock_init(&ddp->map_lock);
1402         kref_init(&ddp->refcnt);
1403
1404         ddp->cdev = cdev;
1405         ddp->pdev = cdev->pdev;
1406         ddp->llimit = llimit;
1407         ddp->ulimit = ulimit;
1408         ddp->max_txsz = min_t(unsigned int, max_txsz, ULP2_MAX_PKT_SIZE);
1409         ddp->max_rxsz = min_t(unsigned int, max_rxsz, ULP2_MAX_PKT_SIZE);
1410         ddp->nppods = ppmax;
1411         ddp->idx_last = ppmax;
1412         ddp->idx_bits = bits;
1413         ddp->idx_mask = (1 << bits) - 1;
1414         ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
1415
1416         cdev->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
1417         cdev->tag_format.rsvd_bits = ddp->idx_bits;
1418         cdev->tag_format.rsvd_shift = PPOD_IDX_SHIFT;
1419         cdev->tag_format.rsvd_mask = (1 << cdev->tag_format.rsvd_bits) - 1;
1420
1421         pr_info("%s tag format, sw %u, rsvd %u,%u, mask 0x%x.\n",
1422                 cdev->ports[0]->name, cdev->tag_format.sw_bits,
1423                 cdev->tag_format.rsvd_bits, cdev->tag_format.rsvd_shift,
1424                 cdev->tag_format.rsvd_mask);
1425
1426         cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1427                                 ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
1428         cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1429                                 ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
1430
1431         log_debug(1 << CXGBI_DBG_DDP,
1432                 "%s max payload size: %u/%u, %u/%u.\n",
1433                 cdev->ports[0]->name, cdev->tx_max_size, ddp->max_txsz,
1434                 cdev->rx_max_size, ddp->max_rxsz);
1435         return 0;
1436 }
1437 EXPORT_SYMBOL_GPL(cxgbi_ddp_init);
1438
1439 /*
1440  * APIs interacting with open-iscsi libraries
1441  */
1442
1443 static unsigned char padding[4];
1444
1445 static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1446 {
1447         struct scsi_cmnd *sc = task->sc;
1448         struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1449         struct cxgbi_conn *cconn = tcp_conn->dd_data;
1450         struct cxgbi_hba *chba = cconn->chba;
1451         struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1452         u32 tag = ntohl((__force u32)hdr_itt);
1453
1454         log_debug(1 << CXGBI_DBG_DDP,
1455                    "cdev 0x%p, release tag 0x%x.\n", chba->cdev, tag);
1456         if (sc &&
1457             (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
1458             cxgbi_is_ddp_tag(tformat, tag))
1459                 ddp_tag_release(chba, tag);
1460 }
1461
1462 static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1463 {
1464         struct scsi_cmnd *sc = task->sc;
1465         struct iscsi_conn *conn = task->conn;
1466         struct iscsi_session *sess = conn->session;
1467         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1468         struct cxgbi_conn *cconn = tcp_conn->dd_data;
1469         struct cxgbi_hba *chba = cconn->chba;
1470         struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1471         u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
1472         u32 tag = 0;
1473         int err = -EINVAL;
1474
1475         if (sc &&
1476             (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)) {
1477                 err = cxgbi_ddp_reserve(cconn->cep->csk, &tag, sw_tag,
1478                                         scsi_in(sc)->length,
1479                                         scsi_in(sc)->table.sgl,
1480                                         scsi_in(sc)->table.nents,
1481                                         GFP_ATOMIC);
1482                 if (err < 0)
1483                         log_debug(1 << CXGBI_DBG_DDP,
1484                                 "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
1485                                 cconn->cep->csk, task, scsi_in(sc)->length,
1486                                 scsi_in(sc)->table.nents);
1487         }
1488
1489         if (err < 0)
1490                 tag = cxgbi_set_non_ddp_tag(tformat, sw_tag);
1491         /*  the itt need to sent in big-endian order */
1492         *hdr_itt = (__force itt_t)htonl(tag);
1493
1494         log_debug(1 << CXGBI_DBG_DDP,
1495                 "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1496                 chba->cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
1497         return 0;
1498 }
1499
1500 void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1501 {
1502         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1503         struct cxgbi_conn *cconn = tcp_conn->dd_data;
1504         struct cxgbi_device *cdev = cconn->chba->cdev;
1505         u32 tag = ntohl((__force u32) itt);
1506         u32 sw_bits;
1507
1508         sw_bits = cxgbi_tag_nonrsvd_bits(&cdev->tag_format, tag);
1509         if (idx)
1510                 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
1511         if (age)
1512                 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
1513
1514         log_debug(1 << CXGBI_DBG_DDP,
1515                 "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1516                 cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1517                 age ? *age : 0xFF);
1518 }
1519 EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1520
1521 void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1522 {
1523         struct iscsi_conn *conn = csk->user_data;
1524
1525         if (conn) {
1526                 log_debug(1 << CXGBI_DBG_SOCK,
1527                         "csk 0x%p, cid %d.\n", csk, conn->id);
1528                 iscsi_conn_queue_work(conn);
1529         }
1530 }
1531 EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1532
1533 /*
1534  * pdu receive, interact with libiscsi_tcp
1535  */
1536 static inline int read_pdu_skb(struct iscsi_conn *conn,
1537                                struct sk_buff *skb,
1538                                unsigned int offset,
1539                                int offloaded)
1540 {
1541         int status = 0;
1542         int bytes_read;
1543
1544         bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1545         switch (status) {
1546         case ISCSI_TCP_CONN_ERR:
1547                 pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1548                           skb, offset, offloaded);
1549                 return -EIO;
1550         case ISCSI_TCP_SUSPENDED:
1551                 log_debug(1 << CXGBI_DBG_PDU_RX,
1552                         "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1553                         skb, offset, offloaded, bytes_read);
1554                 /* no transfer - just have caller flush queue */
1555                 return bytes_read;
1556         case ISCSI_TCP_SKB_DONE:
1557                 pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1558                         skb, offset, offloaded);
1559                 /*
1560                  * pdus should always fit in the skb and we should get
1561                  * segment done notifcation.
1562                  */
1563                 iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1564                 return -EFAULT;
1565         case ISCSI_TCP_SEGMENT_DONE:
1566                 log_debug(1 << CXGBI_DBG_PDU_RX,
1567                         "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1568                         skb, offset, offloaded, bytes_read);
1569                 return bytes_read;
1570         default:
1571                 pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1572                         skb, offset, offloaded, status);
1573                 return -EINVAL;
1574         }
1575 }
1576
1577 static int skb_read_pdu_bhs(struct iscsi_conn *conn, struct sk_buff *skb)
1578 {
1579         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1580
1581         log_debug(1 << CXGBI_DBG_PDU_RX,
1582                 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1583                 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1584
1585         if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1586                 pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1587                 iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1588                 return -EIO;
1589         }
1590
1591         if (conn->hdrdgst_en &&
1592             cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1593                 pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1594                 iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1595                 return -EIO;
1596         }
1597
1598         return read_pdu_skb(conn, skb, 0, 0);
1599 }
1600
1601 static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1602                              struct sk_buff *skb, unsigned int offset)
1603 {
1604         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1605         bool offloaded = 0;
1606         int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1607
1608         log_debug(1 << CXGBI_DBG_PDU_RX,
1609                 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1610                 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1611
1612         if (conn->datadgst_en &&
1613             cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1614                 pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1615                         conn, lskb, cxgbi_skcb_flags(lskb));
1616                 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1617                 return -EIO;
1618         }
1619
1620         if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1621                 return 0;
1622
1623         /* coalesced, add header digest length */
1624         if (lskb == skb && conn->hdrdgst_en)
1625                 offset += ISCSI_DIGEST_SIZE;
1626
1627         if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1628                 offloaded = 1;
1629
1630         if (opcode == ISCSI_OP_SCSI_DATA_IN)
1631                 log_debug(1 << CXGBI_DBG_PDU_RX,
1632                         "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1633                         skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1634                         tcp_conn->in.datalen, offloaded ? "is" : "not");
1635
1636         return read_pdu_skb(conn, skb, offset, offloaded);
1637 }
1638
1639 static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1640 {
1641         struct cxgbi_device *cdev = csk->cdev;
1642         int must_send;
1643         u32 credits;
1644
1645         log_debug(1 << CXGBI_DBG_PDU_RX,
1646                 "csk 0x%p,%u,0x%lu,%u, seq %u, wup %u, thre %u, %u.\n",
1647                 csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1648                 csk->rcv_wup, cdev->rx_credit_thres,
1649                 cdev->rcv_win);
1650
1651         if (csk->state != CTP_ESTABLISHED)
1652                 return;
1653
1654         credits = csk->copied_seq - csk->rcv_wup;
1655         if (unlikely(!credits))
1656                 return;
1657         if (unlikely(cdev->rx_credit_thres == 0))
1658                 return;
1659
1660         must_send = credits + 16384 >= cdev->rcv_win;
1661         if (must_send || credits >= cdev->rx_credit_thres)
1662                 csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1663 }
1664
1665 void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1666 {
1667         struct cxgbi_device *cdev = csk->cdev;
1668         struct iscsi_conn *conn = csk->user_data;
1669         struct sk_buff *skb;
1670         unsigned int read = 0;
1671         int err = 0;
1672
1673         log_debug(1 << CXGBI_DBG_PDU_RX,
1674                 "csk 0x%p, conn 0x%p.\n", csk, conn);
1675
1676         if (unlikely(!conn || conn->suspend_rx)) {
1677                 log_debug(1 << CXGBI_DBG_PDU_RX,
1678                         "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1679                         csk, conn, conn ? conn->id : 0xFF,
1680                         conn ? conn->suspend_rx : 0xFF);
1681                 return;
1682         }
1683
1684         while (!err) {
1685                 skb = skb_peek(&csk->receive_queue);
1686                 if (!skb ||
1687                     !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1688                         if (skb)
1689                                 log_debug(1 << CXGBI_DBG_PDU_RX,
1690                                         "skb 0x%p, NOT ready 0x%lx.\n",
1691                                         skb, cxgbi_skcb_flags(skb));
1692                         break;
1693                 }
1694                 __skb_unlink(skb, &csk->receive_queue);
1695
1696                 read += cxgbi_skcb_rx_pdulen(skb);
1697                 log_debug(1 << CXGBI_DBG_PDU_RX,
1698                         "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1699                         csk, skb, skb->len, cxgbi_skcb_flags(skb),
1700                         cxgbi_skcb_rx_pdulen(skb));
1701
1702                 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1703                         err = skb_read_pdu_bhs(conn, skb);
1704                         if (err < 0) {
1705                                 pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1706                                         "f 0x%lx, plen %u.\n",
1707                                         csk, skb, skb->len,
1708                                         cxgbi_skcb_flags(skb),
1709                                         cxgbi_skcb_rx_pdulen(skb));
1710                                 goto skb_done;
1711                         }
1712                         err = skb_read_pdu_data(conn, skb, skb,
1713                                                 err + cdev->skb_rx_extra);
1714                         if (err < 0)
1715                                 pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1716                                         "f 0x%lx, plen %u.\n",
1717                                         csk, skb, skb->len,
1718                                         cxgbi_skcb_flags(skb),
1719                                         cxgbi_skcb_rx_pdulen(skb));
1720                 } else {
1721                         err = skb_read_pdu_bhs(conn, skb);
1722                         if (err < 0) {
1723                                 pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1724                                         "f 0x%lx, plen %u.\n",
1725                                         csk, skb, skb->len,
1726                                         cxgbi_skcb_flags(skb),
1727                                         cxgbi_skcb_rx_pdulen(skb));
1728                                 goto skb_done;
1729                         }
1730
1731                         if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1732                                 struct sk_buff *dskb;
1733
1734                                 dskb = skb_peek(&csk->receive_queue);
1735                                 if (!dskb) {
1736                                         pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1737                                                 " plen %u, NO data.\n",
1738                                                 csk, skb, skb->len,
1739                                                 cxgbi_skcb_flags(skb),
1740                                                 cxgbi_skcb_rx_pdulen(skb));
1741                                         err = -EIO;
1742                                         goto skb_done;
1743                                 }
1744                                 __skb_unlink(dskb, &csk->receive_queue);
1745
1746                                 err = skb_read_pdu_data(conn, skb, dskb, 0);
1747                                 if (err < 0)
1748                                         pr_err("data, csk 0x%p, skb 0x%p,%u, "
1749                                                 "f 0x%lx, plen %u, dskb 0x%p,"
1750                                                 "%u.\n",
1751                                                 csk, skb, skb->len,
1752                                                 cxgbi_skcb_flags(skb),
1753                                                 cxgbi_skcb_rx_pdulen(skb),
1754                                                 dskb, dskb->len);
1755                                 __kfree_skb(dskb);
1756                         } else
1757                                 err = skb_read_pdu_data(conn, skb, skb, 0);
1758                 }
1759 skb_done:
1760                 __kfree_skb(skb);
1761
1762                 if (err < 0)
1763                         break;
1764         }
1765
1766         log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1767         if (read) {
1768                 csk->copied_seq += read;
1769                 csk_return_rx_credits(csk, read);
1770                 conn->rxdata_octets += read;
1771         }
1772
1773         if (err < 0) {
1774                 pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1775                         csk, conn, err, read);
1776                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1777         }
1778 }
1779 EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1780
1781 static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1782                                 unsigned int offset, unsigned int *off,
1783                                 struct scatterlist **sgp)
1784 {
1785         int i;
1786         struct scatterlist *sg;
1787
1788         for_each_sg(sgl, sg, sgcnt, i) {
1789                 if (offset < sg->length) {
1790                         *off = offset;
1791                         *sgp = sg;
1792                         return 0;
1793                 }
1794                 offset -= sg->length;
1795         }
1796         return -EFAULT;
1797 }
1798
1799 static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
1800                                 unsigned int dlen, skb_frag_t *frags,
1801                                 int frag_max)
1802 {
1803         unsigned int datalen = dlen;
1804         unsigned int sglen = sg->length - sgoffset;
1805         struct page *page = sg_page(sg);
1806         int i;
1807
1808         i = 0;
1809         do {
1810                 unsigned int copy;
1811
1812                 if (!sglen) {
1813                         sg = sg_next(sg);
1814                         if (!sg) {
1815                                 pr_warn("sg %d NULL, len %u/%u.\n",
1816                                         i, datalen, dlen);
1817                                 return -EINVAL;
1818                         }
1819                         sgoffset = 0;
1820                         sglen = sg->length;
1821                         page = sg_page(sg);
1822
1823                 }
1824                 copy = min(datalen, sglen);
1825                 if (i && page == frags[i - 1].page &&
1826                     sgoffset + sg->offset ==
1827                         frags[i - 1].page_offset + frags[i - 1].size) {
1828                         frags[i - 1].size += copy;
1829                 } else {
1830                         if (i >= frag_max) {
1831                                 pr_warn("too many pages %u, dlen %u.\n",
1832                                         frag_max, dlen);
1833                                 return -EINVAL;
1834                         }
1835
1836                         frags[i].page = page;
1837                         frags[i].page_offset = sg->offset + sgoffset;
1838                         frags[i].size = copy;
1839                         i++;
1840                 }
1841                 datalen -= copy;
1842                 sgoffset += copy;
1843                 sglen -= copy;
1844         } while (datalen);
1845
1846         return i;
1847 }
1848
1849 int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1850 {
1851         struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1852         struct cxgbi_conn *cconn = tcp_conn->dd_data;
1853         struct cxgbi_device *cdev = cconn->chba->cdev;
1854         struct iscsi_conn *conn = task->conn;
1855         struct iscsi_tcp_task *tcp_task = task->dd_data;
1856         struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1857         struct scsi_cmnd *sc = task->sc;
1858         int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1859
1860         tcp_task->dd_data = tdata;
1861         task->hdr = NULL;
1862
1863         if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1864             (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1865              (opcode == ISCSI_OP_SCSI_CMD &&
1866               (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
1867                 /* data could goes into skb head */
1868                 headroom += min_t(unsigned int,
1869                                 SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1870                                 conn->max_xmit_dlength);
1871
1872         tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1873         if (!tdata->skb) {
1874                 pr_warn("alloc skb %u+%u, opcode 0x%x failed.\n",
1875                         cdev->skb_tx_rsvd, headroom, opcode);
1876                 return -ENOMEM;
1877         }
1878
1879         skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1880         task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1881         task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1882
1883         /* data_out uses scsi_cmd's itt */
1884         if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1885                 task_reserve_itt(task, &task->hdr->itt);
1886
1887         log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1888                 "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1889                 task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1890                 conn->max_xmit_dlength, ntohl(task->hdr->itt));
1891
1892         return 0;
1893 }
1894 EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1895
1896 static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1897 {
1898         if (hcrc || dcrc) {
1899                 u8 submode = 0;
1900
1901                 if (hcrc)
1902                         submode |= 1;
1903                 if (dcrc)
1904                         submode |= 2;
1905                 cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1906         } else
1907                 cxgbi_skcb_ulp_mode(skb) = 0;
1908 }
1909
1910 int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1911                               unsigned int count)
1912 {
1913         struct iscsi_conn *conn = task->conn;
1914         struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1915         struct sk_buff *skb = tdata->skb;
1916         unsigned int datalen = count;
1917         int i, padlen = iscsi_padding(count);
1918         struct page *pg;
1919
1920         log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1921                 "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1922                 task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1923                 ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1924
1925         skb_put(skb, task->hdr_len);
1926         tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1927         if (!count)
1928                 return 0;
1929
1930         if (task->sc) {
1931                 struct scsi_data_buffer *sdb = scsi_out(task->sc);
1932                 struct scatterlist *sg = NULL;
1933                 int err;
1934
1935                 tdata->offset = offset;
1936                 tdata->count = count;
1937                 err = sgl_seek_offset(
1938                                         sdb->table.sgl, sdb->table.nents,
1939                                         tdata->offset, &tdata->sgoffset, &sg);
1940                 if (err < 0) {
1941                         pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1942                                 sdb->table.nents, tdata->offset, sdb->length);
1943                         return err;
1944                 }
1945                 err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1946                                         tdata->frags, MAX_PDU_FRAGS);
1947                 if (err < 0) {
1948                         pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1949                                 sdb->table.nents, tdata->offset, tdata->count);
1950                         return err;
1951                 }
1952                 tdata->nr_frags = err;
1953
1954                 if (tdata->nr_frags > MAX_SKB_FRAGS ||
1955                     (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
1956                         char *dst = skb->data + task->hdr_len;
1957                         skb_frag_t *frag = tdata->frags;
1958
1959                         /* data fits in the skb's headroom */
1960                         for (i = 0; i < tdata->nr_frags; i++, frag++) {
1961                                 char *src = kmap_atomic(frag->page,
1962                                                         KM_SOFTIRQ0);
1963
1964                                 memcpy(dst, src+frag->page_offset, frag->size);
1965                                 dst += frag->size;
1966                                 kunmap_atomic(src, KM_SOFTIRQ0);
1967                         }
1968                         if (padlen) {
1969                                 memset(dst, 0, padlen);
1970                                 padlen = 0;
1971                         }
1972                         skb_put(skb, count + padlen);
1973                 } else {
1974                         /* data fit into frag_list */
1975                         for (i = 0; i < tdata->nr_frags; i++)
1976                                 get_page(tdata->frags[i].page);
1977
1978                         memcpy(skb_shinfo(skb)->frags, tdata->frags,
1979                                 sizeof(skb_frag_t) * tdata->nr_frags);
1980                         skb_shinfo(skb)->nr_frags = tdata->nr_frags;
1981                         skb->len += count;
1982                         skb->data_len += count;
1983                         skb->truesize += count;
1984                 }
1985
1986         } else {
1987                 pg = virt_to_page(task->data);
1988
1989                 get_page(pg);
1990                 skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
1991                                         count);
1992                 skb->len += count;
1993                 skb->data_len += count;
1994                 skb->truesize += count;
1995         }
1996
1997         if (padlen) {
1998                 i = skb_shinfo(skb)->nr_frags;
1999                 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2000                                 virt_to_page(padding), offset_in_page(padding),
2001                                 padlen);
2002
2003                 skb->data_len += padlen;
2004                 skb->truesize += padlen;
2005                 skb->len += padlen;
2006         }
2007
2008         return 0;
2009 }
2010 EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2011
2012 int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2013 {
2014         struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2015         struct cxgbi_conn *cconn = tcp_conn->dd_data;
2016         struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2017         struct sk_buff *skb = tdata->skb;
2018         unsigned int datalen;
2019         int err;
2020
2021         if (!skb) {
2022                 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2023                         "task 0x%p, skb NULL.\n", task);
2024                 return 0;
2025         }
2026
2027         datalen = skb->data_len;
2028         tdata->skb = NULL;
2029         err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2030         if (err > 0) {
2031                 int pdulen = err;
2032
2033                 log_debug(1 << CXGBI_DBG_PDU_TX,
2034                         "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2035                         task, task->sc, skb, skb->len, skb->data_len, err);
2036
2037                 if (task->conn->hdrdgst_en)
2038                         pdulen += ISCSI_DIGEST_SIZE;
2039
2040                 if (datalen && task->conn->datadgst_en)
2041                         pdulen += ISCSI_DIGEST_SIZE;
2042
2043                 task->conn->txdata_octets += pdulen;
2044                 return 0;
2045         }
2046
2047         if (err == -EAGAIN || err == -ENOBUFS) {
2048                 log_debug(1 << CXGBI_DBG_PDU_TX,
2049                         "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2050                         task, skb, skb->len, skb->data_len, err);
2051                 /* reset skb to send when we are called again */
2052                 tdata->skb = skb;
2053                 return err;
2054         }
2055
2056         kfree_skb(skb);
2057         log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2058                 "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2059                 task->itt, skb, skb->len, skb->data_len, err);
2060         iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2061         iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2062         return err;
2063 }
2064 EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2065
2066 void cxgbi_cleanup_task(struct iscsi_task *task)
2067 {
2068         struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2069
2070         log_debug(1 << CXGBI_DBG_ISCSI,
2071                 "task 0x%p, skb 0x%p, itt 0x%x.\n",
2072                 task, tdata->skb, task->hdr_itt);
2073
2074         /*  never reached the xmit task callout */
2075         if (tdata->skb)
2076                 __kfree_skb(tdata->skb);
2077         memset(tdata, 0, sizeof(*tdata));
2078
2079         task_release_itt(task, task->hdr_itt);
2080         iscsi_tcp_cleanup_task(task);
2081 }
2082 EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2083
2084 void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2085                                 struct iscsi_stats *stats)
2086 {
2087         struct iscsi_conn *conn = cls_conn->dd_data;
2088
2089         stats->txdata_octets = conn->txdata_octets;
2090         stats->rxdata_octets = conn->rxdata_octets;
2091         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2092         stats->dataout_pdus = conn->dataout_pdus_cnt;
2093         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2094         stats->datain_pdus = conn->datain_pdus_cnt;
2095         stats->r2t_pdus = conn->r2t_pdus_cnt;
2096         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2097         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2098         stats->digest_err = 0;
2099         stats->timeout_err = 0;
2100         stats->custom_length = 1;
2101         strcpy(stats->custom[0].desc, "eh_abort_cnt");
2102         stats->custom[0].value = conn->eh_abort_cnt;
2103 }
2104 EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2105
2106 static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2107 {
2108         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2109         struct cxgbi_conn *cconn = tcp_conn->dd_data;
2110         struct cxgbi_device *cdev = cconn->chba->cdev;
2111         unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2112         unsigned int max_def = 512 * MAX_SKB_FRAGS;
2113         unsigned int max = max(max_def, headroom);
2114
2115         max = min(cconn->chba->cdev->tx_max_size, max);
2116         if (conn->max_xmit_dlength)
2117                 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2118         else
2119                 conn->max_xmit_dlength = max;
2120         cxgbi_align_pdu_size(conn->max_xmit_dlength);
2121
2122         return 0;
2123 }
2124
2125 static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2126 {
2127         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2128         struct cxgbi_conn *cconn = tcp_conn->dd_data;
2129         unsigned int max = cconn->chba->cdev->rx_max_size;
2130
2131         cxgbi_align_pdu_size(max);
2132
2133         if (conn->max_recv_dlength) {
2134                 if (conn->max_recv_dlength > max) {
2135                         pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2136                                 conn->max_recv_dlength, max);
2137                         return -EINVAL;
2138                 }
2139                 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2140                 cxgbi_align_pdu_size(conn->max_recv_dlength);
2141         } else
2142                 conn->max_recv_dlength = max;
2143
2144         return 0;
2145 }
2146
2147 int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2148                         enum iscsi_param param, char *buf, int buflen)
2149 {
2150         struct iscsi_conn *conn = cls_conn->dd_data;
2151         struct iscsi_session *session = conn->session;
2152         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2153         struct cxgbi_conn *cconn = tcp_conn->dd_data;
2154         struct cxgbi_sock *csk = cconn->cep->csk;
2155         int value, err = 0;
2156
2157         log_debug(1 << CXGBI_DBG_ISCSI,
2158                 "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2159                 cls_conn, param, buflen, buf);
2160
2161         switch (param) {
2162         case ISCSI_PARAM_HDRDGST_EN:
2163                 err = iscsi_set_param(cls_conn, param, buf, buflen);
2164                 if (!err && conn->hdrdgst_en)
2165                         err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2166                                                         conn->hdrdgst_en,
2167                                                         conn->datadgst_en, 0);
2168                 break;
2169         case ISCSI_PARAM_DATADGST_EN:
2170                 err = iscsi_set_param(cls_conn, param, buf, buflen);
2171                 if (!err && conn->datadgst_en)
2172                         err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2173                                                         conn->hdrdgst_en,
2174                                                         conn->datadgst_en, 0);
2175                 break;
2176         case ISCSI_PARAM_MAX_R2T:
2177                 sscanf(buf, "%d", &value);
2178                 if (value <= 0 || !is_power_of_2(value))
2179                         return -EINVAL;
2180                 if (session->max_r2t == value)
2181                         break;
2182                 iscsi_tcp_r2tpool_free(session);
2183                 err = iscsi_set_param(cls_conn, param, buf, buflen);
2184                 if (!err && iscsi_tcp_r2tpool_alloc(session))
2185                         return -ENOMEM;
2186         case ISCSI_PARAM_MAX_RECV_DLENGTH:
2187                 err = iscsi_set_param(cls_conn, param, buf, buflen);
2188                 if (!err)
2189                         err = cxgbi_conn_max_recv_dlength(conn);
2190                 break;
2191         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2192                 err = iscsi_set_param(cls_conn, param, buf, buflen);
2193                 if (!err)
2194                         err = cxgbi_conn_max_xmit_dlength(conn);
2195                 break;
2196         default:
2197                 return iscsi_set_param(cls_conn, param, buf, buflen);
2198         }
2199         return err;
2200 }
2201 EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2202
2203 int cxgbi_get_conn_param(struct iscsi_cls_conn *cls_conn,
2204                         enum iscsi_param param, char *buf)
2205 {
2206         struct iscsi_conn *iconn = cls_conn->dd_data;
2207         int len;
2208
2209         log_debug(1 << CXGBI_DBG_ISCSI,
2210                 "cls_conn 0x%p, param %d.\n", cls_conn, param);
2211
2212         switch (param) {
2213         case ISCSI_PARAM_CONN_PORT:
2214                 spin_lock_bh(&iconn->session->lock);
2215                 len = sprintf(buf, "%hu\n", iconn->portal_port);
2216                 spin_unlock_bh(&iconn->session->lock);
2217                 break;
2218         case ISCSI_PARAM_CONN_ADDRESS:
2219                 spin_lock_bh(&iconn->session->lock);
2220                 len = sprintf(buf, "%s\n", iconn->portal_address);
2221                 spin_unlock_bh(&iconn->session->lock);
2222                 break;
2223         default:
2224                 return iscsi_conn_get_param(cls_conn, param, buf);
2225         }
2226         return len;
2227 }
2228 EXPORT_SYMBOL_GPL(cxgbi_get_conn_param);
2229
2230 struct iscsi_cls_conn *
2231 cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2232 {
2233         struct iscsi_cls_conn *cls_conn;
2234         struct iscsi_conn *conn;
2235         struct iscsi_tcp_conn *tcp_conn;
2236         struct cxgbi_conn *cconn;
2237
2238         cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2239         if (!cls_conn)
2240                 return NULL;
2241
2242         conn = cls_conn->dd_data;
2243         tcp_conn = conn->dd_data;
2244         cconn = tcp_conn->dd_data;
2245         cconn->iconn = conn;
2246
2247         log_debug(1 << CXGBI_DBG_ISCSI,
2248                 "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2249                 cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2250
2251         return cls_conn;
2252 }
2253 EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2254
2255 int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2256                                 struct iscsi_cls_conn *cls_conn,
2257                                 u64 transport_eph, int is_leading)
2258 {
2259         struct iscsi_conn *conn = cls_conn->dd_data;
2260         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2261         struct cxgbi_conn *cconn = tcp_conn->dd_data;
2262         struct iscsi_endpoint *ep;
2263         struct cxgbi_endpoint *cep;
2264         struct cxgbi_sock *csk;
2265         int err;
2266
2267         ep = iscsi_lookup_endpoint(transport_eph);
2268         if (!ep)
2269                 return -EINVAL;
2270
2271         /*  setup ddp pagesize */
2272         cep = ep->dd_data;
2273         csk = cep->csk;
2274         err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid, page_idx, 0);
2275         if (err < 0)
2276                 return err;
2277
2278         err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2279         if (err)
2280                 return -EINVAL;
2281
2282         /*  calculate the tag idx bits needed for this conn based on cmds_max */
2283         cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2284
2285         write_lock_bh(&csk->callback_lock);
2286         csk->user_data = conn;
2287         cconn->chba = cep->chba;
2288         cconn->cep = cep;
2289         cep->cconn = cconn;
2290         write_unlock_bh(&csk->callback_lock);
2291
2292         cxgbi_conn_max_xmit_dlength(conn);
2293         cxgbi_conn_max_recv_dlength(conn);
2294
2295         spin_lock_bh(&conn->session->lock);
2296         sprintf(conn->portal_address, "%pI4", &csk->daddr.sin_addr.s_addr);
2297         conn->portal_port = ntohs(csk->daddr.sin_port);
2298         spin_unlock_bh(&conn->session->lock);
2299
2300         log_debug(1 << CXGBI_DBG_ISCSI,
2301                 "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2302                 cls_session, cls_conn, ep, cconn, csk);
2303         /*  init recv engine */
2304         iscsi_tcp_hdr_recv_prep(tcp_conn);
2305
2306         return 0;
2307 }
2308 EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2309
2310 struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2311                                                 u16 cmds_max, u16 qdepth,
2312                                                 u32 initial_cmdsn)
2313 {
2314         struct cxgbi_endpoint *cep;
2315         struct cxgbi_hba *chba;
2316         struct Scsi_Host *shost;
2317         struct iscsi_cls_session *cls_session;
2318         struct iscsi_session *session;
2319
2320         if (!ep) {
2321                 pr_err("missing endpoint.\n");
2322                 return NULL;
2323         }
2324
2325         cep = ep->dd_data;
2326         chba = cep->chba;
2327         shost = chba->shost;
2328
2329         BUG_ON(chba != iscsi_host_priv(shost));
2330
2331         cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2332                                         cmds_max, 0,
2333                                         sizeof(struct iscsi_tcp_task) +
2334                                         sizeof(struct cxgbi_task_data),
2335                                         initial_cmdsn, ISCSI_MAX_TARGET);
2336         if (!cls_session)
2337                 return NULL;
2338
2339         session = cls_session->dd_data;
2340         if (iscsi_tcp_r2tpool_alloc(session))
2341                 goto remove_session;
2342
2343         log_debug(1 << CXGBI_DBG_ISCSI,
2344                 "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2345         return cls_session;
2346
2347 remove_session:
2348         iscsi_session_teardown(cls_session);
2349         return NULL;
2350 }
2351 EXPORT_SYMBOL_GPL(cxgbi_create_session);
2352
2353 void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2354 {
2355         log_debug(1 << CXGBI_DBG_ISCSI,
2356                 "cls sess 0x%p.\n", cls_session);
2357
2358         iscsi_tcp_r2tpool_free(cls_session->dd_data);
2359         iscsi_session_teardown(cls_session);
2360 }
2361 EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2362
2363 int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2364                         char *buf, int buflen)
2365 {
2366         struct cxgbi_hba *chba = iscsi_host_priv(shost);
2367
2368         if (!chba->ndev) {
2369                 shost_printk(KERN_ERR, shost, "Could not get host param. "
2370                                 "netdev for host not set.\n");
2371                 return -ENODEV;
2372         }
2373
2374         log_debug(1 << CXGBI_DBG_ISCSI,
2375                 "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2376                 shost, chba, chba->ndev->name, param, buflen, buf);
2377
2378         switch (param) {
2379         case ISCSI_HOST_PARAM_IPADDRESS:
2380         {
2381                 __be32 addr = in_aton(buf);
2382                 log_debug(1 << CXGBI_DBG_ISCSI,
2383                         "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2384                 cxgbi_set_iscsi_ipv4(chba, addr);
2385                 return 0;
2386         }
2387         case ISCSI_HOST_PARAM_HWADDRESS:
2388         case ISCSI_HOST_PARAM_NETDEV_NAME:
2389                 return 0;
2390         default:
2391                 return iscsi_host_set_param(shost, param, buf, buflen);
2392         }
2393 }
2394 EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2395
2396 int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2397                         char *buf)
2398 {
2399         struct cxgbi_hba *chba = iscsi_host_priv(shost);
2400         int len = 0;
2401
2402         if (!chba->ndev) {
2403                 shost_printk(KERN_ERR, shost, "Could not get host param. "
2404                                 "netdev for host not set.\n");
2405                 return -ENODEV;
2406         }
2407
2408         log_debug(1 << CXGBI_DBG_ISCSI,
2409                 "shost 0x%p, hba 0x%p,%s, param %d.\n",
2410                 shost, chba, chba->ndev->name, param);
2411
2412         switch (param) {
2413         case ISCSI_HOST_PARAM_HWADDRESS:
2414                 len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2415                 break;
2416         case ISCSI_HOST_PARAM_NETDEV_NAME:
2417                 len = sprintf(buf, "%s\n", chba->ndev->name);
2418                 break;
2419         case ISCSI_HOST_PARAM_IPADDRESS:
2420         {
2421                 __be32 addr;
2422
2423                 addr = cxgbi_get_iscsi_ipv4(chba);
2424                 len = sprintf(buf, "%pI4", &addr);
2425                 log_debug(1 << CXGBI_DBG_ISCSI,
2426                         "hba %s, ipv4 %pI4.\n", chba->ndev->name, &addr);
2427                 break;
2428         }
2429         default:
2430                 return iscsi_host_get_param(shost, param, buf);
2431         }
2432
2433         return len;
2434 }
2435 EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2436
2437 struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2438                                         struct sockaddr *dst_addr,
2439                                         int non_blocking)
2440 {
2441         struct iscsi_endpoint *ep;
2442         struct cxgbi_endpoint *cep;
2443         struct cxgbi_hba *hba = NULL;
2444         struct cxgbi_sock *csk;
2445         int err = -EINVAL;
2446
2447         log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2448                 "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2449                 shost, non_blocking, dst_addr);
2450
2451         if (shost) {
2452                 hba = iscsi_host_priv(shost);
2453                 if (!hba) {
2454                         pr_info("shost 0x%p, priv NULL.\n", shost);
2455                         goto err_out;
2456                 }
2457         }
2458
2459         csk = cxgbi_check_route(dst_addr);
2460         if (IS_ERR(csk))
2461                 return (struct iscsi_endpoint *)csk;
2462         cxgbi_sock_get(csk);
2463
2464         if (!hba)
2465                 hba = csk->cdev->hbas[csk->port_id];
2466         else if (hba != csk->cdev->hbas[csk->port_id]) {
2467                 pr_info("Could not connect through requested host %u"
2468                         "hba 0x%p != 0x%p (%u).\n",
2469                         shost->host_no, hba,
2470                         csk->cdev->hbas[csk->port_id], csk->port_id);
2471                 err = -ENOSPC;
2472                 goto release_conn;
2473         }
2474
2475         err = sock_get_port(csk);
2476         if (err)
2477                 goto release_conn;
2478
2479         cxgbi_sock_set_state(csk, CTP_CONNECTING);
2480         err = csk->cdev->csk_init_act_open(csk);
2481         if (err)
2482                 goto release_conn;
2483
2484         if (cxgbi_sock_is_closing(csk)) {
2485                 err = -ENOSPC;
2486                 pr_info("csk 0x%p is closing.\n", csk);
2487                 goto release_conn;
2488         }
2489
2490         ep = iscsi_create_endpoint(sizeof(*cep));
2491         if (!ep) {
2492                 err = -ENOMEM;
2493                 pr_info("iscsi alloc ep, OOM.\n");
2494                 goto release_conn;
2495         }
2496
2497         cep = ep->dd_data;
2498         cep->csk = csk;
2499         cep->chba = hba;
2500
2501         log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2502                 "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2503                 ep, cep, csk, hba, hba->ndev->name);
2504         return ep;
2505
2506 release_conn:
2507         cxgbi_sock_put(csk);
2508         cxgbi_sock_closed(csk);
2509 err_out:
2510         return ERR_PTR(err);
2511 }
2512 EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2513
2514 int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2515 {
2516         struct cxgbi_endpoint *cep = ep->dd_data;
2517         struct cxgbi_sock *csk = cep->csk;
2518
2519         if (!cxgbi_sock_is_established(csk))
2520                 return 0;
2521         return 1;
2522 }
2523 EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2524
2525 void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2526 {
2527         struct cxgbi_endpoint *cep = ep->dd_data;
2528         struct cxgbi_conn *cconn = cep->cconn;
2529         struct cxgbi_sock *csk = cep->csk;
2530
2531         log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2532                 "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2533                 ep, cep, cconn, csk, csk->state, csk->flags);
2534
2535         if (cconn && cconn->iconn) {
2536                 iscsi_suspend_tx(cconn->iconn);
2537                 write_lock_bh(&csk->callback_lock);
2538                 cep->csk->user_data = NULL;
2539                 cconn->cep = NULL;
2540                 write_unlock_bh(&csk->callback_lock);
2541         }
2542         iscsi_destroy_endpoint(ep);
2543
2544         if (likely(csk->state >= CTP_ESTABLISHED))
2545                 need_active_close(csk);
2546         else
2547                 cxgbi_sock_closed(csk);
2548
2549         cxgbi_sock_put(csk);
2550 }
2551 EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2552
2553 int cxgbi_iscsi_init(struct iscsi_transport *itp,
2554                         struct scsi_transport_template **stt)
2555 {
2556         *stt = iscsi_register_transport(itp);
2557         if (*stt == NULL) {
2558                 pr_err("unable to register %s transport 0x%p.\n",
2559                         itp->name, itp);
2560                 return -ENODEV;
2561         }
2562         log_debug(1 << CXGBI_DBG_ISCSI,
2563                 "%s, registered iscsi transport 0x%p.\n",
2564                 itp->name, stt);
2565         return 0;
2566 }
2567 EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2568
2569 void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2570                         struct scsi_transport_template **stt)
2571 {
2572         if (*stt) {
2573                 log_debug(1 << CXGBI_DBG_ISCSI,
2574                         "de-register transport 0x%p, %s, stt 0x%p.\n",
2575                         itp, itp->name, *stt);
2576                 *stt = NULL;
2577                 iscsi_unregister_transport(itp);
2578         }
2579 }
2580 EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2581
2582 static int __init libcxgbi_init_module(void)
2583 {
2584         sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
2585         sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
2586
2587         pr_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
2588                 ISCSI_ITT_MASK, sw_tag_idx_bits,
2589                 ISCSI_AGE_MASK, sw_tag_age_bits);
2590
2591         ddp_setup_host_page_size();
2592         return 0;
2593 }
2594
2595 static void __exit libcxgbi_exit_module(void)
2596 {
2597         cxgbi_device_unregister_all(0xFF);
2598         return;
2599 }
2600
2601 module_init(libcxgbi_init_module);
2602 module_exit(libcxgbi_exit_module);