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