GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / target / iscsi / cxgbit / cxgbit_cm.c
1 /*
2  * Copyright (c) 2016 Chelsio Communications, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/workqueue.h>
12 #include <linux/skbuff.h>
13 #include <linux/timer.h>
14 #include <linux/notifier.h>
15 #include <linux/inetdevice.h>
16 #include <linux/ip.h>
17 #include <linux/tcp.h>
18 #include <linux/if_vlan.h>
19
20 #include <net/neighbour.h>
21 #include <net/netevent.h>
22 #include <net/route.h>
23 #include <net/tcp.h>
24 #include <net/ip6_route.h>
25 #include <net/addrconf.h>
26
27 #include <libcxgb_cm.h>
28 #include "cxgbit.h"
29 #include "clip_tbl.h"
30
31 static void cxgbit_init_wr_wait(struct cxgbit_wr_wait *wr_waitp)
32 {
33         wr_waitp->ret = 0;
34         reinit_completion(&wr_waitp->completion);
35 }
36
37 static void
38 cxgbit_wake_up(struct cxgbit_wr_wait *wr_waitp, const char *func, u8 ret)
39 {
40         if (ret == CPL_ERR_NONE)
41                 wr_waitp->ret = 0;
42         else
43                 wr_waitp->ret = -EIO;
44
45         if (wr_waitp->ret)
46                 pr_err("%s: err:%u", func, ret);
47
48         complete(&wr_waitp->completion);
49 }
50
51 static int
52 cxgbit_wait_for_reply(struct cxgbit_device *cdev,
53                       struct cxgbit_wr_wait *wr_waitp, u32 tid, u32 timeout,
54                       const char *func)
55 {
56         int ret;
57
58         if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
59                 wr_waitp->ret = -EIO;
60                 goto out;
61         }
62
63         ret = wait_for_completion_timeout(&wr_waitp->completion, timeout * HZ);
64         if (!ret) {
65                 pr_info("%s - Device %s not responding tid %u\n",
66                         func, pci_name(cdev->lldi.pdev), tid);
67                 wr_waitp->ret = -ETIMEDOUT;
68         }
69 out:
70         if (wr_waitp->ret)
71                 pr_info("%s: FW reply %d tid %u\n",
72                         pci_name(cdev->lldi.pdev), wr_waitp->ret, tid);
73         return wr_waitp->ret;
74 }
75
76 static int cxgbit_np_hashfn(const struct cxgbit_np *cnp)
77 {
78         return ((unsigned long)cnp >> 10) & (NP_INFO_HASH_SIZE - 1);
79 }
80
81 static struct np_info *
82 cxgbit_np_hash_add(struct cxgbit_device *cdev, struct cxgbit_np *cnp,
83                    unsigned int stid)
84 {
85         struct np_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
86
87         if (p) {
88                 int bucket = cxgbit_np_hashfn(cnp);
89
90                 p->cnp = cnp;
91                 p->stid = stid;
92                 spin_lock(&cdev->np_lock);
93                 p->next = cdev->np_hash_tab[bucket];
94                 cdev->np_hash_tab[bucket] = p;
95                 spin_unlock(&cdev->np_lock);
96         }
97
98         return p;
99 }
100
101 static int
102 cxgbit_np_hash_find(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
103 {
104         int stid = -1, bucket = cxgbit_np_hashfn(cnp);
105         struct np_info *p;
106
107         spin_lock(&cdev->np_lock);
108         for (p = cdev->np_hash_tab[bucket]; p; p = p->next) {
109                 if (p->cnp == cnp) {
110                         stid = p->stid;
111                         break;
112                 }
113         }
114         spin_unlock(&cdev->np_lock);
115
116         return stid;
117 }
118
119 static int cxgbit_np_hash_del(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
120 {
121         int stid = -1, bucket = cxgbit_np_hashfn(cnp);
122         struct np_info *p, **prev = &cdev->np_hash_tab[bucket];
123
124         spin_lock(&cdev->np_lock);
125         for (p = *prev; p; prev = &p->next, p = p->next) {
126                 if (p->cnp == cnp) {
127                         stid = p->stid;
128                         *prev = p->next;
129                         kfree(p);
130                         break;
131                 }
132         }
133         spin_unlock(&cdev->np_lock);
134
135         return stid;
136 }
137
138 void _cxgbit_free_cnp(struct kref *kref)
139 {
140         struct cxgbit_np *cnp;
141
142         cnp = container_of(kref, struct cxgbit_np, kref);
143         kfree(cnp);
144 }
145
146 static int
147 cxgbit_create_server6(struct cxgbit_device *cdev, unsigned int stid,
148                       struct cxgbit_np *cnp)
149 {
150         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
151                                      &cnp->com.local_addr;
152         int addr_type;
153         int ret;
154
155         pr_debug("%s: dev = %s; stid = %u; sin6_port = %u\n",
156                  __func__, cdev->lldi.ports[0]->name, stid, sin6->sin6_port);
157
158         addr_type = ipv6_addr_type((const struct in6_addr *)
159                                    &sin6->sin6_addr);
160         if (addr_type != IPV6_ADDR_ANY) {
161                 ret = cxgb4_clip_get(cdev->lldi.ports[0],
162                                      (const u32 *)&sin6->sin6_addr.s6_addr, 1);
163                 if (ret) {
164                         pr_err("Unable to find clip table entry. laddr %pI6. Error:%d.\n",
165                                sin6->sin6_addr.s6_addr, ret);
166                         return -ENOMEM;
167                 }
168         }
169
170         cxgbit_get_cnp(cnp);
171         cxgbit_init_wr_wait(&cnp->com.wr_wait);
172
173         ret = cxgb4_create_server6(cdev->lldi.ports[0],
174                                    stid, &sin6->sin6_addr,
175                                    sin6->sin6_port,
176                                    cdev->lldi.rxq_ids[0]);
177         if (!ret)
178                 ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
179                                             0, 10, __func__);
180         else if (ret > 0)
181                 ret = net_xmit_errno(ret);
182         else
183                 cxgbit_put_cnp(cnp);
184
185         if (ret) {
186                 if (ret != -ETIMEDOUT)
187                         cxgb4_clip_release(cdev->lldi.ports[0],
188                                    (const u32 *)&sin6->sin6_addr.s6_addr, 1);
189
190                 pr_err("create server6 err %d stid %d laddr %pI6 lport %d\n",
191                        ret, stid, sin6->sin6_addr.s6_addr,
192                        ntohs(sin6->sin6_port));
193         }
194
195         return ret;
196 }
197
198 static int
199 cxgbit_create_server4(struct cxgbit_device *cdev, unsigned int stid,
200                       struct cxgbit_np *cnp)
201 {
202         struct sockaddr_in *sin = (struct sockaddr_in *)
203                                    &cnp->com.local_addr;
204         int ret;
205
206         pr_debug("%s: dev = %s; stid = %u; sin_port = %u\n",
207                  __func__, cdev->lldi.ports[0]->name, stid, sin->sin_port);
208
209         cxgbit_get_cnp(cnp);
210         cxgbit_init_wr_wait(&cnp->com.wr_wait);
211
212         ret = cxgb4_create_server(cdev->lldi.ports[0],
213                                   stid, sin->sin_addr.s_addr,
214                                   sin->sin_port, 0,
215                                   cdev->lldi.rxq_ids[0]);
216         if (!ret)
217                 ret = cxgbit_wait_for_reply(cdev,
218                                             &cnp->com.wr_wait,
219                                             0, 10, __func__);
220         else if (ret > 0)
221                 ret = net_xmit_errno(ret);
222         else
223                 cxgbit_put_cnp(cnp);
224
225         if (ret)
226                 pr_err("create server failed err %d stid %d laddr %pI4 lport %d\n",
227                        ret, stid, &sin->sin_addr, ntohs(sin->sin_port));
228         return ret;
229 }
230
231 struct cxgbit_device *cxgbit_find_device(struct net_device *ndev, u8 *port_id)
232 {
233         struct cxgbit_device *cdev;
234         u8 i;
235
236         list_for_each_entry(cdev, &cdev_list_head, list) {
237                 struct cxgb4_lld_info *lldi = &cdev->lldi;
238
239                 for (i = 0; i < lldi->nports; i++) {
240                         if (lldi->ports[i] == ndev) {
241                                 if (port_id)
242                                         *port_id = i;
243                                 return cdev;
244                         }
245                 }
246         }
247
248         return NULL;
249 }
250
251 static struct net_device *cxgbit_get_real_dev(struct net_device *ndev)
252 {
253         if (ndev->priv_flags & IFF_BONDING) {
254                 pr_err("Bond devices are not supported. Interface:%s\n",
255                        ndev->name);
256                 return NULL;
257         }
258
259         if (is_vlan_dev(ndev))
260                 return vlan_dev_real_dev(ndev);
261
262         return ndev;
263 }
264
265 static struct net_device *cxgbit_ipv4_netdev(__be32 saddr)
266 {
267         struct net_device *ndev;
268
269         ndev = __ip_dev_find(&init_net, saddr, false);
270         if (!ndev)
271                 return NULL;
272
273         return cxgbit_get_real_dev(ndev);
274 }
275
276 static struct net_device *cxgbit_ipv6_netdev(struct in6_addr *addr6)
277 {
278         struct net_device *ndev = NULL;
279         bool found = false;
280
281         if (IS_ENABLED(CONFIG_IPV6)) {
282                 for_each_netdev_rcu(&init_net, ndev)
283                         if (ipv6_chk_addr(&init_net, addr6, ndev, 1)) {
284                                 found = true;
285                                 break;
286                         }
287         }
288         if (!found)
289                 return NULL;
290         return cxgbit_get_real_dev(ndev);
291 }
292
293 static struct cxgbit_device *cxgbit_find_np_cdev(struct cxgbit_np *cnp)
294 {
295         struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
296         int ss_family = sockaddr->ss_family;
297         struct net_device *ndev = NULL;
298         struct cxgbit_device *cdev = NULL;
299
300         rcu_read_lock();
301         if (ss_family == AF_INET) {
302                 struct sockaddr_in *sin;
303
304                 sin = (struct sockaddr_in *)sockaddr;
305                 ndev = cxgbit_ipv4_netdev(sin->sin_addr.s_addr);
306         } else if (ss_family == AF_INET6) {
307                 struct sockaddr_in6 *sin6;
308
309                 sin6 = (struct sockaddr_in6 *)sockaddr;
310                 ndev = cxgbit_ipv6_netdev(&sin6->sin6_addr);
311         }
312         if (!ndev)
313                 goto out;
314
315         cdev = cxgbit_find_device(ndev, NULL);
316 out:
317         rcu_read_unlock();
318         return cdev;
319 }
320
321 static bool cxgbit_inaddr_any(struct cxgbit_np *cnp)
322 {
323         struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
324         int ss_family = sockaddr->ss_family;
325         int addr_type;
326
327         if (ss_family == AF_INET) {
328                 struct sockaddr_in *sin;
329
330                 sin = (struct sockaddr_in *)sockaddr;
331                 if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
332                         return true;
333         } else if (ss_family == AF_INET6) {
334                 struct sockaddr_in6 *sin6;
335
336                 sin6 = (struct sockaddr_in6 *)sockaddr;
337                 addr_type = ipv6_addr_type((const struct in6_addr *)
338                                 &sin6->sin6_addr);
339                 if (addr_type == IPV6_ADDR_ANY)
340                         return true;
341         }
342         return false;
343 }
344
345 static int
346 __cxgbit_setup_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
347 {
348         int stid, ret;
349         int ss_family = cnp->com.local_addr.ss_family;
350
351         if (!test_bit(CDEV_STATE_UP, &cdev->flags))
352                 return -EINVAL;
353
354         stid = cxgb4_alloc_stid(cdev->lldi.tids, ss_family, cnp);
355         if (stid < 0)
356                 return -EINVAL;
357
358         if (!cxgbit_np_hash_add(cdev, cnp, stid)) {
359                 cxgb4_free_stid(cdev->lldi.tids, stid, ss_family);
360                 return -EINVAL;
361         }
362
363         if (ss_family == AF_INET)
364                 ret = cxgbit_create_server4(cdev, stid, cnp);
365         else
366                 ret = cxgbit_create_server6(cdev, stid, cnp);
367
368         if (ret) {
369                 if (ret != -ETIMEDOUT)
370                         cxgb4_free_stid(cdev->lldi.tids, stid,
371                                         ss_family);
372                 cxgbit_np_hash_del(cdev, cnp);
373                 return ret;
374         }
375         return ret;
376 }
377
378 static int cxgbit_setup_cdev_np(struct cxgbit_np *cnp)
379 {
380         struct cxgbit_device *cdev;
381         int ret = -1;
382
383         mutex_lock(&cdev_list_lock);
384         cdev = cxgbit_find_np_cdev(cnp);
385         if (!cdev)
386                 goto out;
387
388         if (cxgbit_np_hash_find(cdev, cnp) >= 0)
389                 goto out;
390
391         if (__cxgbit_setup_cdev_np(cdev, cnp))
392                 goto out;
393
394         cnp->com.cdev = cdev;
395         ret = 0;
396 out:
397         mutex_unlock(&cdev_list_lock);
398         return ret;
399 }
400
401 static int cxgbit_setup_all_np(struct cxgbit_np *cnp)
402 {
403         struct cxgbit_device *cdev;
404         int ret;
405         u32 count = 0;
406
407         mutex_lock(&cdev_list_lock);
408         list_for_each_entry(cdev, &cdev_list_head, list) {
409                 if (cxgbit_np_hash_find(cdev, cnp) >= 0) {
410                         mutex_unlock(&cdev_list_lock);
411                         return -1;
412                 }
413         }
414
415         list_for_each_entry(cdev, &cdev_list_head, list) {
416                 ret = __cxgbit_setup_cdev_np(cdev, cnp);
417                 if (ret == -ETIMEDOUT)
418                         break;
419                 if (ret != 0)
420                         continue;
421                 count++;
422         }
423         mutex_unlock(&cdev_list_lock);
424
425         return count ? 0 : -1;
426 }
427
428 int cxgbit_setup_np(struct iscsi_np *np, struct sockaddr_storage *ksockaddr)
429 {
430         struct cxgbit_np *cnp;
431         int ret;
432
433         if ((ksockaddr->ss_family != AF_INET) &&
434             (ksockaddr->ss_family != AF_INET6))
435                 return -EINVAL;
436
437         cnp = kzalloc(sizeof(*cnp), GFP_KERNEL);
438         if (!cnp)
439                 return -ENOMEM;
440
441         init_waitqueue_head(&cnp->accept_wait);
442         init_completion(&cnp->com.wr_wait.completion);
443         init_completion(&cnp->accept_comp);
444         INIT_LIST_HEAD(&cnp->np_accept_list);
445         spin_lock_init(&cnp->np_accept_lock);
446         kref_init(&cnp->kref);
447         memcpy(&np->np_sockaddr, ksockaddr,
448                sizeof(struct sockaddr_storage));
449         memcpy(&cnp->com.local_addr, &np->np_sockaddr,
450                sizeof(cnp->com.local_addr));
451
452         cnp->np = np;
453         cnp->com.cdev = NULL;
454
455         if (cxgbit_inaddr_any(cnp))
456                 ret = cxgbit_setup_all_np(cnp);
457         else
458                 ret = cxgbit_setup_cdev_np(cnp);
459
460         if (ret) {
461                 cxgbit_put_cnp(cnp);
462                 return -EINVAL;
463         }
464
465         np->np_context = cnp;
466         cnp->com.state = CSK_STATE_LISTEN;
467         return 0;
468 }
469
470 static void
471 cxgbit_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
472                      struct cxgbit_sock *csk)
473 {
474         conn->login_family = np->np_sockaddr.ss_family;
475         conn->login_sockaddr = csk->com.remote_addr;
476         conn->local_sockaddr = csk->com.local_addr;
477 }
478
479 int cxgbit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
480 {
481         struct cxgbit_np *cnp = np->np_context;
482         struct cxgbit_sock *csk;
483         int ret = 0;
484
485 accept_wait:
486         ret = wait_for_completion_interruptible(&cnp->accept_comp);
487         if (ret)
488                 return -ENODEV;
489
490         spin_lock_bh(&np->np_thread_lock);
491         if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
492                 spin_unlock_bh(&np->np_thread_lock);
493                 /**
494                  * No point in stalling here when np_thread
495                  * is in state RESET/SHUTDOWN/EXIT - bail
496                  **/
497                 return -ENODEV;
498         }
499         spin_unlock_bh(&np->np_thread_lock);
500
501         spin_lock_bh(&cnp->np_accept_lock);
502         if (list_empty(&cnp->np_accept_list)) {
503                 spin_unlock_bh(&cnp->np_accept_lock);
504                 goto accept_wait;
505         }
506
507         csk = list_first_entry(&cnp->np_accept_list,
508                                struct cxgbit_sock,
509                                accept_node);
510
511         list_del_init(&csk->accept_node);
512         spin_unlock_bh(&cnp->np_accept_lock);
513         conn->context = csk;
514         csk->conn = conn;
515
516         cxgbit_set_conn_info(np, conn, csk);
517         return 0;
518 }
519
520 static int
521 __cxgbit_free_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
522 {
523         int stid, ret;
524         bool ipv6 = false;
525
526         stid = cxgbit_np_hash_del(cdev, cnp);
527         if (stid < 0)
528                 return -EINVAL;
529         if (!test_bit(CDEV_STATE_UP, &cdev->flags))
530                 return -EINVAL;
531
532         if (cnp->np->np_sockaddr.ss_family == AF_INET6)
533                 ipv6 = true;
534
535         cxgbit_get_cnp(cnp);
536         cxgbit_init_wr_wait(&cnp->com.wr_wait);
537         ret = cxgb4_remove_server(cdev->lldi.ports[0], stid,
538                                   cdev->lldi.rxq_ids[0], ipv6);
539
540         if (ret > 0)
541                 ret = net_xmit_errno(ret);
542
543         if (ret) {
544                 cxgbit_put_cnp(cnp);
545                 return ret;
546         }
547
548         ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
549                                     0, 10, __func__);
550         if (ret == -ETIMEDOUT)
551                 return ret;
552
553         if (ipv6 && cnp->com.cdev) {
554                 struct sockaddr_in6 *sin6;
555
556                 sin6 = (struct sockaddr_in6 *)&cnp->com.local_addr;
557                 cxgb4_clip_release(cdev->lldi.ports[0],
558                                    (const u32 *)&sin6->sin6_addr.s6_addr,
559                                    1);
560         }
561
562         cxgb4_free_stid(cdev->lldi.tids, stid,
563                         cnp->com.local_addr.ss_family);
564         return 0;
565 }
566
567 static void cxgbit_free_all_np(struct cxgbit_np *cnp)
568 {
569         struct cxgbit_device *cdev;
570         int ret;
571
572         mutex_lock(&cdev_list_lock);
573         list_for_each_entry(cdev, &cdev_list_head, list) {
574                 ret = __cxgbit_free_cdev_np(cdev, cnp);
575                 if (ret == -ETIMEDOUT)
576                         break;
577         }
578         mutex_unlock(&cdev_list_lock);
579 }
580
581 static void cxgbit_free_cdev_np(struct cxgbit_np *cnp)
582 {
583         struct cxgbit_device *cdev;
584         bool found = false;
585
586         mutex_lock(&cdev_list_lock);
587         list_for_each_entry(cdev, &cdev_list_head, list) {
588                 if (cdev == cnp->com.cdev) {
589                         found = true;
590                         break;
591                 }
592         }
593         if (!found)
594                 goto out;
595
596         __cxgbit_free_cdev_np(cdev, cnp);
597 out:
598         mutex_unlock(&cdev_list_lock);
599 }
600
601 void cxgbit_free_np(struct iscsi_np *np)
602 {
603         struct cxgbit_np *cnp = np->np_context;
604
605         cnp->com.state = CSK_STATE_DEAD;
606         if (cnp->com.cdev)
607                 cxgbit_free_cdev_np(cnp);
608         else
609                 cxgbit_free_all_np(cnp);
610
611         np->np_context = NULL;
612         cxgbit_put_cnp(cnp);
613 }
614
615 static void cxgbit_send_halfclose(struct cxgbit_sock *csk)
616 {
617         struct sk_buff *skb;
618         u32 len = roundup(sizeof(struct cpl_close_con_req), 16);
619
620         skb = alloc_skb(len, GFP_ATOMIC);
621         if (!skb)
622                 return;
623
624         cxgb_mk_close_con_req(skb, len, csk->tid, csk->txq_idx,
625                               NULL, NULL);
626
627         cxgbit_skcb_flags(skb) |= SKCBF_TX_FLAG_COMPL;
628         __skb_queue_tail(&csk->txq, skb);
629         cxgbit_push_tx_frames(csk);
630 }
631
632 static void cxgbit_arp_failure_discard(void *handle, struct sk_buff *skb)
633 {
634         struct cxgbit_sock *csk = handle;
635
636         pr_debug("%s cxgbit_device %p\n", __func__, handle);
637         kfree_skb(skb);
638         cxgbit_put_csk(csk);
639 }
640
641 static void cxgbit_abort_arp_failure(void *handle, struct sk_buff *skb)
642 {
643         struct cxgbit_device *cdev = handle;
644         struct cpl_abort_req *req = cplhdr(skb);
645
646         pr_debug("%s cdev %p\n", __func__, cdev);
647         req->cmd = CPL_ABORT_NO_RST;
648         cxgbit_ofld_send(cdev, skb);
649 }
650
651 static int cxgbit_send_abort_req(struct cxgbit_sock *csk)
652 {
653         struct sk_buff *skb;
654         u32 len = roundup(sizeof(struct cpl_abort_req), 16);
655
656         pr_debug("%s: csk %p tid %u; state %d\n",
657                  __func__, csk, csk->tid, csk->com.state);
658
659         __skb_queue_purge(&csk->txq);
660
661         if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
662                 cxgbit_send_tx_flowc_wr(csk);
663
664         skb = __skb_dequeue(&csk->skbq);
665         cxgb_mk_abort_req(skb, len, csk->tid, csk->txq_idx,
666                           csk->com.cdev, cxgbit_abort_arp_failure);
667
668         return cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
669 }
670
671 void cxgbit_free_conn(struct iscsi_conn *conn)
672 {
673         struct cxgbit_sock *csk = conn->context;
674         bool release = false;
675
676         pr_debug("%s: state %d\n",
677                  __func__, csk->com.state);
678
679         spin_lock_bh(&csk->lock);
680         switch (csk->com.state) {
681         case CSK_STATE_ESTABLISHED:
682                 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
683                         csk->com.state = CSK_STATE_CLOSING;
684                         cxgbit_send_halfclose(csk);
685                 } else {
686                         csk->com.state = CSK_STATE_ABORTING;
687                         cxgbit_send_abort_req(csk);
688                 }
689                 break;
690         case CSK_STATE_CLOSING:
691                 csk->com.state = CSK_STATE_MORIBUND;
692                 cxgbit_send_halfclose(csk);
693                 break;
694         case CSK_STATE_DEAD:
695                 release = true;
696                 break;
697         default:
698                 pr_err("%s: csk %p; state %d\n",
699                        __func__, csk, csk->com.state);
700         }
701         spin_unlock_bh(&csk->lock);
702
703         if (release)
704                 cxgbit_put_csk(csk);
705 }
706
707 static void cxgbit_set_emss(struct cxgbit_sock *csk, u16 opt)
708 {
709         csk->emss = csk->com.cdev->lldi.mtus[TCPOPT_MSS_G(opt)] -
710                         ((csk->com.remote_addr.ss_family == AF_INET) ?
711                         sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
712                         sizeof(struct tcphdr);
713         csk->mss = csk->emss;
714         if (TCPOPT_TSTAMP_G(opt))
715                 csk->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
716         if (csk->emss < 128)
717                 csk->emss = 128;
718         if (csk->emss & 7)
719                 pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
720                         TCPOPT_MSS_G(opt), csk->mss, csk->emss);
721         pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
722                  csk->mss, csk->emss);
723 }
724
725 static void cxgbit_free_skb(struct cxgbit_sock *csk)
726 {
727         struct sk_buff *skb;
728
729         __skb_queue_purge(&csk->txq);
730         __skb_queue_purge(&csk->rxq);
731         __skb_queue_purge(&csk->backlogq);
732         __skb_queue_purge(&csk->ppodq);
733         __skb_queue_purge(&csk->skbq);
734
735         while ((skb = cxgbit_sock_dequeue_wr(csk)))
736                 kfree_skb(skb);
737
738         __kfree_skb(csk->lro_hskb);
739 }
740
741 void _cxgbit_free_csk(struct kref *kref)
742 {
743         struct cxgbit_sock *csk;
744         struct cxgbit_device *cdev;
745
746         csk = container_of(kref, struct cxgbit_sock, kref);
747
748         pr_debug("%s csk %p state %d\n", __func__, csk, csk->com.state);
749
750         if (csk->com.local_addr.ss_family == AF_INET6) {
751                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
752                                              &csk->com.local_addr;
753                 cxgb4_clip_release(csk->com.cdev->lldi.ports[0],
754                                    (const u32 *)
755                                    &sin6->sin6_addr.s6_addr, 1);
756         }
757
758         cxgb4_remove_tid(csk->com.cdev->lldi.tids, 0, csk->tid,
759                          csk->com.local_addr.ss_family);
760         dst_release(csk->dst);
761         cxgb4_l2t_release(csk->l2t);
762
763         cdev = csk->com.cdev;
764         spin_lock_bh(&cdev->cskq.lock);
765         list_del(&csk->list);
766         spin_unlock_bh(&cdev->cskq.lock);
767
768         cxgbit_free_skb(csk);
769         cxgbit_put_cdev(cdev);
770
771         kfree(csk);
772 }
773
774 static void cxgbit_set_tcp_window(struct cxgbit_sock *csk, struct port_info *pi)
775 {
776         unsigned int linkspeed;
777         u8 scale;
778
779         linkspeed = pi->link_cfg.speed;
780         scale = linkspeed / SPEED_10000;
781
782 #define CXGBIT_10G_RCV_WIN (256 * 1024)
783         csk->rcv_win = CXGBIT_10G_RCV_WIN;
784         if (scale)
785                 csk->rcv_win *= scale;
786
787 #define CXGBIT_10G_SND_WIN (256 * 1024)
788         csk->snd_win = CXGBIT_10G_SND_WIN;
789         if (scale)
790                 csk->snd_win *= scale;
791
792         pr_debug("%s snd_win %d rcv_win %d\n",
793                  __func__, csk->snd_win, csk->rcv_win);
794 }
795
796 #ifdef CONFIG_CHELSIO_T4_DCB
797 static u8 cxgbit_get_iscsi_dcb_state(struct net_device *ndev)
798 {
799         return ndev->dcbnl_ops->getstate(ndev);
800 }
801
802 static int cxgbit_select_priority(int pri_mask)
803 {
804         if (!pri_mask)
805                 return 0;
806
807         return (ffs(pri_mask) - 1);
808 }
809
810 static u8 cxgbit_get_iscsi_dcb_priority(struct net_device *ndev, u16 local_port)
811 {
812         int ret;
813         u8 caps;
814
815         struct dcb_app iscsi_dcb_app = {
816                 .protocol = local_port
817         };
818
819         ret = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
820
821         if (ret)
822                 return 0;
823
824         if (caps & DCB_CAP_DCBX_VER_IEEE) {
825                 iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
826
827                 ret = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
828
829         } else if (caps & DCB_CAP_DCBX_VER_CEE) {
830                 iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
831
832                 ret = dcb_getapp(ndev, &iscsi_dcb_app);
833         }
834
835         pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret));
836
837         return cxgbit_select_priority(ret);
838 }
839 #endif
840
841 static int
842 cxgbit_offload_init(struct cxgbit_sock *csk, int iptype, __u8 *peer_ip,
843                     u16 local_port, struct dst_entry *dst,
844                     struct cxgbit_device *cdev)
845 {
846         struct neighbour *n;
847         int ret, step;
848         struct net_device *ndev;
849         u16 rxq_idx, port_id;
850 #ifdef CONFIG_CHELSIO_T4_DCB
851         u8 priority = 0;
852 #endif
853
854         n = dst_neigh_lookup(dst, peer_ip);
855         if (!n)
856                 return -ENODEV;
857
858         rcu_read_lock();
859         ret = -ENOMEM;
860         if (n->dev->flags & IFF_LOOPBACK) {
861                 if (iptype == 4)
862                         ndev = cxgbit_ipv4_netdev(*(__be32 *)peer_ip);
863                 else if (IS_ENABLED(CONFIG_IPV6))
864                         ndev = cxgbit_ipv6_netdev((struct in6_addr *)peer_ip);
865                 else
866                         ndev = NULL;
867
868                 if (!ndev) {
869                         ret = -ENODEV;
870                         goto out;
871                 }
872
873                 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t,
874                                          n, ndev, 0);
875                 if (!csk->l2t)
876                         goto out;
877                 csk->mtu = ndev->mtu;
878                 csk->tx_chan = cxgb4_port_chan(ndev);
879                 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
880                                                  cxgb4_port_viid(ndev));
881                 step = cdev->lldi.ntxq /
882                         cdev->lldi.nchan;
883                 csk->txq_idx = cxgb4_port_idx(ndev) * step;
884                 step = cdev->lldi.nrxq /
885                         cdev->lldi.nchan;
886                 csk->ctrlq_idx = cxgb4_port_idx(ndev);
887                 csk->rss_qid = cdev->lldi.rxq_ids[
888                                 cxgb4_port_idx(ndev) * step];
889                 csk->port_id = cxgb4_port_idx(ndev);
890                 cxgbit_set_tcp_window(csk,
891                                       (struct port_info *)netdev_priv(ndev));
892         } else {
893                 ndev = cxgbit_get_real_dev(n->dev);
894                 if (!ndev) {
895                         ret = -ENODEV;
896                         goto out;
897                 }
898
899 #ifdef CONFIG_CHELSIO_T4_DCB
900                 if (cxgbit_get_iscsi_dcb_state(ndev))
901                         priority = cxgbit_get_iscsi_dcb_priority(ndev,
902                                                                  local_port);
903
904                 csk->dcb_priority = priority;
905
906                 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, priority);
907 #else
908                 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, 0);
909 #endif
910                 if (!csk->l2t)
911                         goto out;
912                 port_id = cxgb4_port_idx(ndev);
913                 csk->mtu = dst_mtu(dst);
914                 csk->tx_chan = cxgb4_port_chan(ndev);
915                 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
916                                                  cxgb4_port_viid(ndev));
917                 step = cdev->lldi.ntxq /
918                         cdev->lldi.nports;
919                 csk->txq_idx = (port_id * step) +
920                                 (cdev->selectq[port_id][0]++ % step);
921                 csk->ctrlq_idx = cxgb4_port_idx(ndev);
922                 step = cdev->lldi.nrxq /
923                         cdev->lldi.nports;
924                 rxq_idx = (port_id * step) +
925                                 (cdev->selectq[port_id][1]++ % step);
926                 csk->rss_qid = cdev->lldi.rxq_ids[rxq_idx];
927                 csk->port_id = port_id;
928                 cxgbit_set_tcp_window(csk,
929                                       (struct port_info *)netdev_priv(ndev));
930         }
931         ret = 0;
932 out:
933         rcu_read_unlock();
934         neigh_release(n);
935         return ret;
936 }
937
938 int cxgbit_ofld_send(struct cxgbit_device *cdev, struct sk_buff *skb)
939 {
940         int ret = 0;
941
942         if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
943                 kfree_skb(skb);
944                 pr_err("%s - device not up - dropping\n", __func__);
945                 return -EIO;
946         }
947
948         ret = cxgb4_ofld_send(cdev->lldi.ports[0], skb);
949         if (ret < 0)
950                 kfree_skb(skb);
951         return ret < 0 ? ret : 0;
952 }
953
954 static void cxgbit_release_tid(struct cxgbit_device *cdev, u32 tid)
955 {
956         u32 len = roundup(sizeof(struct cpl_tid_release), 16);
957         struct sk_buff *skb;
958
959         skb = alloc_skb(len, GFP_ATOMIC);
960         if (!skb)
961                 return;
962
963         cxgb_mk_tid_release(skb, len, tid, 0);
964         cxgbit_ofld_send(cdev, skb);
965 }
966
967 int
968 cxgbit_l2t_send(struct cxgbit_device *cdev, struct sk_buff *skb,
969                 struct l2t_entry *l2e)
970 {
971         int ret = 0;
972
973         if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
974                 kfree_skb(skb);
975                 pr_err("%s - device not up - dropping\n", __func__);
976                 return -EIO;
977         }
978
979         ret = cxgb4_l2t_send(cdev->lldi.ports[0], skb, l2e);
980         if (ret < 0)
981                 kfree_skb(skb);
982         return ret < 0 ? ret : 0;
983 }
984
985 static void cxgbit_send_rx_credits(struct cxgbit_sock *csk, struct sk_buff *skb)
986 {
987         if (csk->com.state != CSK_STATE_ESTABLISHED) {
988                 __kfree_skb(skb);
989                 return;
990         }
991
992         cxgbit_ofld_send(csk->com.cdev, skb);
993 }
994
995 /*
996  * CPL connection rx data ack: host ->
997  * Send RX credits through an RX_DATA_ACK CPL message.
998  * Returns the number of credits sent.
999  */
1000 int cxgbit_rx_data_ack(struct cxgbit_sock *csk)
1001 {
1002         struct sk_buff *skb;
1003         u32 len = roundup(sizeof(struct cpl_rx_data_ack), 16);
1004         u32 credit_dack;
1005
1006         skb = alloc_skb(len, GFP_KERNEL);
1007         if (!skb)
1008                 return -1;
1009
1010         credit_dack = RX_DACK_CHANGE_F | RX_DACK_MODE_V(1) |
1011                       RX_CREDITS_V(csk->rx_credits);
1012
1013         cxgb_mk_rx_data_ack(skb, len, csk->tid, csk->ctrlq_idx,
1014                             credit_dack);
1015
1016         csk->rx_credits = 0;
1017
1018         spin_lock_bh(&csk->lock);
1019         if (csk->lock_owner) {
1020                 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_send_rx_credits;
1021                 __skb_queue_tail(&csk->backlogq, skb);
1022                 spin_unlock_bh(&csk->lock);
1023                 return 0;
1024         }
1025
1026         cxgbit_send_rx_credits(csk, skb);
1027         spin_unlock_bh(&csk->lock);
1028
1029         return 0;
1030 }
1031
1032 #define FLOWC_WR_NPARAMS_MIN    9
1033 #define FLOWC_WR_NPARAMS_MAX    11
1034 static int cxgbit_alloc_csk_skb(struct cxgbit_sock *csk)
1035 {
1036         struct sk_buff *skb;
1037         u32 len, flowclen;
1038         u8 i;
1039
1040         flowclen = offsetof(struct fw_flowc_wr,
1041                             mnemval[FLOWC_WR_NPARAMS_MAX]);
1042
1043         len = max_t(u32, sizeof(struct cpl_abort_req),
1044                     sizeof(struct cpl_abort_rpl));
1045
1046         len = max(len, flowclen);
1047         len = roundup(len, 16);
1048
1049         for (i = 0; i < 3; i++) {
1050                 skb = alloc_skb(len, GFP_ATOMIC);
1051                 if (!skb)
1052                         goto out;
1053                 __skb_queue_tail(&csk->skbq, skb);
1054         }
1055
1056         skb = alloc_skb(LRO_SKB_MIN_HEADROOM, GFP_ATOMIC);
1057         if (!skb)
1058                 goto out;
1059
1060         memset(skb->data, 0, LRO_SKB_MIN_HEADROOM);
1061         csk->lro_hskb = skb;
1062
1063         return 0;
1064 out:
1065         __skb_queue_purge(&csk->skbq);
1066         return -ENOMEM;
1067 }
1068
1069 static void
1070 cxgbit_pass_accept_rpl(struct cxgbit_sock *csk, struct cpl_pass_accept_req *req)
1071 {
1072         struct sk_buff *skb;
1073         const struct tcphdr *tcph;
1074         struct cpl_t5_pass_accept_rpl *rpl5;
1075         struct cxgb4_lld_info *lldi = &csk->com.cdev->lldi;
1076         unsigned int len = roundup(sizeof(*rpl5), 16);
1077         unsigned int mtu_idx;
1078         u64 opt0;
1079         u32 opt2, hlen;
1080         u32 wscale;
1081         u32 win;
1082
1083         pr_debug("%s csk %p tid %u\n", __func__, csk, csk->tid);
1084
1085         skb = alloc_skb(len, GFP_ATOMIC);
1086         if (!skb) {
1087                 cxgbit_put_csk(csk);
1088                 return;
1089         }
1090
1091         rpl5 = __skb_put_zero(skb, len);
1092
1093         INIT_TP_WR(rpl5, csk->tid);
1094         OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1095                                                      csk->tid));
1096         cxgb_best_mtu(csk->com.cdev->lldi.mtus, csk->mtu, &mtu_idx,
1097                       req->tcpopt.tstamp,
1098                       (csk->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1099         wscale = cxgb_compute_wscale(csk->rcv_win);
1100         /*
1101          * Specify the largest window that will fit in opt0. The
1102          * remainder will be specified in the rx_data_ack.
1103          */
1104         win = csk->rcv_win >> 10;
1105         if (win > RCV_BUFSIZ_M)
1106                 win = RCV_BUFSIZ_M;
1107         opt0 =  TCAM_BYPASS_F |
1108                 WND_SCALE_V(wscale) |
1109                 MSS_IDX_V(mtu_idx) |
1110                 L2T_IDX_V(csk->l2t->idx) |
1111                 TX_CHAN_V(csk->tx_chan) |
1112                 SMAC_SEL_V(csk->smac_idx) |
1113                 DSCP_V(csk->tos >> 2) |
1114                 ULP_MODE_V(ULP_MODE_ISCSI) |
1115                 RCV_BUFSIZ_V(win);
1116
1117         opt2 = RX_CHANNEL_V(0) |
1118                 RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
1119
1120         if (!is_t5(lldi->adapter_type))
1121                 opt2 |= RX_FC_DISABLE_F;
1122
1123         if (req->tcpopt.tstamp)
1124                 opt2 |= TSTAMPS_EN_F;
1125         if (req->tcpopt.sack)
1126                 opt2 |= SACK_EN_F;
1127         if (wscale)
1128                 opt2 |= WND_SCALE_EN_F;
1129
1130         hlen = ntohl(req->hdr_len);
1131
1132         if (is_t5(lldi->adapter_type))
1133                 tcph = (struct tcphdr *)((u8 *)(req + 1) +
1134                        ETH_HDR_LEN_G(hlen) + IP_HDR_LEN_G(hlen));
1135         else
1136                 tcph = (struct tcphdr *)((u8 *)(req + 1) +
1137                        T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
1138
1139         if (tcph->ece && tcph->cwr)
1140                 opt2 |= CCTRL_ECN_V(1);
1141
1142         opt2 |= RX_COALESCE_V(3);
1143         opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
1144
1145         opt2 |= T5_ISS_F;
1146         rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
1147
1148         opt2 |= T5_OPT_2_VALID_F;
1149
1150         rpl5->opt0 = cpu_to_be64(opt0);
1151         rpl5->opt2 = cpu_to_be32(opt2);
1152         set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->ctrlq_idx);
1153         t4_set_arp_err_handler(skb, csk, cxgbit_arp_failure_discard);
1154         cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
1155 }
1156
1157 static void
1158 cxgbit_pass_accept_req(struct cxgbit_device *cdev, struct sk_buff *skb)
1159 {
1160         struct cxgbit_sock *csk = NULL;
1161         struct cxgbit_np *cnp;
1162         struct cpl_pass_accept_req *req = cplhdr(skb);
1163         unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1164         struct tid_info *t = cdev->lldi.tids;
1165         unsigned int tid = GET_TID(req);
1166         u16 peer_mss = ntohs(req->tcpopt.mss);
1167         unsigned short hdrs;
1168
1169         struct dst_entry *dst;
1170         __u8 local_ip[16], peer_ip[16];
1171         __be16 local_port, peer_port;
1172         int ret;
1173         int iptype;
1174
1175         pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
1176                  __func__, cdev, stid, tid);
1177
1178         cnp = lookup_stid(t, stid);
1179         if (!cnp) {
1180                 pr_err("%s connect request on invalid stid %d\n",
1181                        __func__, stid);
1182                 goto rel_skb;
1183         }
1184
1185         if (cnp->com.state != CSK_STATE_LISTEN) {
1186                 pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
1187                        __func__);
1188                 goto reject;
1189         }
1190
1191         csk = lookup_tid(t, tid);
1192         if (csk) {
1193                 pr_err("%s csk not null tid %u\n",
1194                        __func__, tid);
1195                 goto rel_skb;
1196         }
1197
1198         cxgb_get_4tuple(req, cdev->lldi.adapter_type, &iptype, local_ip,
1199                         peer_ip, &local_port, &peer_port);
1200
1201         /* Find output route */
1202         if (iptype == 4)  {
1203                 pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
1204                          "lport %d rport %d peer_mss %d\n"
1205                          , __func__, cnp, tid,
1206                          local_ip, peer_ip, ntohs(local_port),
1207                          ntohs(peer_port), peer_mss);
1208                 dst = cxgb_find_route(&cdev->lldi, cxgbit_get_real_dev,
1209                                       *(__be32 *)local_ip,
1210                                       *(__be32 *)peer_ip,
1211                                       local_port, peer_port,
1212                                       PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
1213         } else {
1214                 pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
1215                          "lport %d rport %d peer_mss %d\n"
1216                          , __func__, cnp, tid,
1217                          local_ip, peer_ip, ntohs(local_port),
1218                          ntohs(peer_port), peer_mss);
1219                 dst = cxgb_find_route6(&cdev->lldi, cxgbit_get_real_dev,
1220                                        local_ip, peer_ip,
1221                                        local_port, peer_port,
1222                                        PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
1223                                        ((struct sockaddr_in6 *)
1224                                         &cnp->com.local_addr)->sin6_scope_id);
1225         }
1226         if (!dst) {
1227                 pr_err("%s - failed to find dst entry!\n",
1228                        __func__);
1229                 goto reject;
1230         }
1231
1232         csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
1233         if (!csk) {
1234                 dst_release(dst);
1235                 goto rel_skb;
1236         }
1237
1238         ret = cxgbit_offload_init(csk, iptype, peer_ip, ntohs(local_port),
1239                                   dst, cdev);
1240         if (ret) {
1241                 pr_err("%s - failed to allocate l2t entry!\n",
1242                        __func__);
1243                 dst_release(dst);
1244                 kfree(csk);
1245                 goto reject;
1246         }
1247
1248         kref_init(&csk->kref);
1249         init_completion(&csk->com.wr_wait.completion);
1250
1251         INIT_LIST_HEAD(&csk->accept_node);
1252
1253         hdrs = (iptype == 4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
1254                 sizeof(struct tcphdr) + (req->tcpopt.tstamp ? 12 : 0);
1255         if (peer_mss && csk->mtu > (peer_mss + hdrs))
1256                 csk->mtu = peer_mss + hdrs;
1257
1258         csk->com.state = CSK_STATE_CONNECTING;
1259         csk->com.cdev = cdev;
1260         csk->cnp = cnp;
1261         csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
1262         csk->dst = dst;
1263         csk->tid = tid;
1264         csk->wr_cred = cdev->lldi.wr_cred -
1265                         DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
1266         csk->wr_max_cred = csk->wr_cred;
1267         csk->wr_una_cred = 0;
1268
1269         if (iptype == 4) {
1270                 struct sockaddr_in *sin = (struct sockaddr_in *)
1271                                           &csk->com.local_addr;
1272                 sin->sin_family = AF_INET;
1273                 sin->sin_port = local_port;
1274                 sin->sin_addr.s_addr = *(__be32 *)local_ip;
1275
1276                 sin = (struct sockaddr_in *)&csk->com.remote_addr;
1277                 sin->sin_family = AF_INET;
1278                 sin->sin_port = peer_port;
1279                 sin->sin_addr.s_addr = *(__be32 *)peer_ip;
1280         } else {
1281                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
1282                                             &csk->com.local_addr;
1283
1284                 sin6->sin6_family = PF_INET6;
1285                 sin6->sin6_port = local_port;
1286                 memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
1287                 cxgb4_clip_get(cdev->lldi.ports[0],
1288                                (const u32 *)&sin6->sin6_addr.s6_addr,
1289                                1);
1290
1291                 sin6 = (struct sockaddr_in6 *)&csk->com.remote_addr;
1292                 sin6->sin6_family = PF_INET6;
1293                 sin6->sin6_port = peer_port;
1294                 memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
1295         }
1296
1297         skb_queue_head_init(&csk->rxq);
1298         skb_queue_head_init(&csk->txq);
1299         skb_queue_head_init(&csk->ppodq);
1300         skb_queue_head_init(&csk->backlogq);
1301         skb_queue_head_init(&csk->skbq);
1302         cxgbit_sock_reset_wr_list(csk);
1303         spin_lock_init(&csk->lock);
1304         init_waitqueue_head(&csk->waitq);
1305         init_waitqueue_head(&csk->ack_waitq);
1306         csk->lock_owner = false;
1307
1308         if (cxgbit_alloc_csk_skb(csk)) {
1309                 dst_release(dst);
1310                 kfree(csk);
1311                 goto rel_skb;
1312         }
1313
1314         cxgbit_get_cdev(cdev);
1315
1316         spin_lock(&cdev->cskq.lock);
1317         list_add_tail(&csk->list, &cdev->cskq.list);
1318         spin_unlock(&cdev->cskq.lock);
1319         cxgb4_insert_tid(t, csk, tid, csk->com.local_addr.ss_family);
1320         cxgbit_pass_accept_rpl(csk, req);
1321         goto rel_skb;
1322
1323 reject:
1324         cxgbit_release_tid(cdev, tid);
1325 rel_skb:
1326         __kfree_skb(skb);
1327 }
1328
1329 static u32
1330 cxgbit_tx_flowc_wr_credits(struct cxgbit_sock *csk, u32 *nparamsp,
1331                            u32 *flowclenp)
1332 {
1333         u32 nparams, flowclen16, flowclen;
1334
1335         nparams = FLOWC_WR_NPARAMS_MIN;
1336
1337         if (csk->snd_wscale)
1338                 nparams++;
1339
1340 #ifdef CONFIG_CHELSIO_T4_DCB
1341         nparams++;
1342 #endif
1343         flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
1344         flowclen16 = DIV_ROUND_UP(flowclen, 16);
1345         flowclen = flowclen16 * 16;
1346         /*
1347          * Return the number of 16-byte credits used by the flowc request.
1348          * Pass back the nparams and actual flowc length if requested.
1349          */
1350         if (nparamsp)
1351                 *nparamsp = nparams;
1352         if (flowclenp)
1353                 *flowclenp = flowclen;
1354         return flowclen16;
1355 }
1356
1357 u32 cxgbit_send_tx_flowc_wr(struct cxgbit_sock *csk)
1358 {
1359         struct cxgbit_device *cdev = csk->com.cdev;
1360         struct fw_flowc_wr *flowc;
1361         u32 nparams, flowclen16, flowclen;
1362         struct sk_buff *skb;
1363         u8 index;
1364
1365 #ifdef CONFIG_CHELSIO_T4_DCB
1366         u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
1367 #endif
1368
1369         flowclen16 = cxgbit_tx_flowc_wr_credits(csk, &nparams, &flowclen);
1370
1371         skb = __skb_dequeue(&csk->skbq);
1372         flowc = __skb_put_zero(skb, flowclen);
1373
1374         flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
1375                                            FW_FLOWC_WR_NPARAMS_V(nparams));
1376         flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
1377                                           FW_WR_FLOWID_V(csk->tid));
1378         flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
1379         flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
1380                                             (csk->com.cdev->lldi.pf));
1381         flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
1382         flowc->mnemval[1].val = cpu_to_be32(csk->tx_chan);
1383         flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
1384         flowc->mnemval[2].val = cpu_to_be32(csk->tx_chan);
1385         flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
1386         flowc->mnemval[3].val = cpu_to_be32(csk->rss_qid);
1387         flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
1388         flowc->mnemval[4].val = cpu_to_be32(csk->snd_nxt);
1389         flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
1390         flowc->mnemval[5].val = cpu_to_be32(csk->rcv_nxt);
1391         flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
1392         flowc->mnemval[6].val = cpu_to_be32(csk->snd_win);
1393         flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
1394         flowc->mnemval[7].val = cpu_to_be32(csk->emss);
1395
1396         flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
1397         if (test_bit(CDEV_ISO_ENABLE, &cdev->flags))
1398                 flowc->mnemval[8].val = cpu_to_be32(CXGBIT_MAX_ISO_PAYLOAD);
1399         else
1400                 flowc->mnemval[8].val = cpu_to_be32(16384);
1401
1402         index = 9;
1403
1404         if (csk->snd_wscale) {
1405                 flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
1406                 flowc->mnemval[index].val = cpu_to_be32(csk->snd_wscale);
1407                 index++;
1408         }
1409
1410 #ifdef CONFIG_CHELSIO_T4_DCB
1411         flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_DCBPRIO;
1412         if (vlan == VLAN_NONE) {
1413                 pr_warn("csk %u without VLAN Tag on DCB Link\n", csk->tid);
1414                 flowc->mnemval[index].val = cpu_to_be32(0);
1415         } else
1416                 flowc->mnemval[index].val = cpu_to_be32(
1417                                 (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT);
1418 #endif
1419
1420         pr_debug("%s: csk %p; tx_chan = %u; rss_qid = %u; snd_seq = %u;"
1421                  " rcv_seq = %u; snd_win = %u; emss = %u\n",
1422                  __func__, csk, csk->tx_chan, csk->rss_qid, csk->snd_nxt,
1423                  csk->rcv_nxt, csk->snd_win, csk->emss);
1424         set_wr_txq(skb, CPL_PRIORITY_DATA, csk->txq_idx);
1425         cxgbit_ofld_send(csk->com.cdev, skb);
1426         return flowclen16;
1427 }
1428
1429 int cxgbit_setup_conn_digest(struct cxgbit_sock *csk)
1430 {
1431         struct sk_buff *skb;
1432         struct cpl_set_tcb_field *req;
1433         u8 hcrc = csk->submode & CXGBIT_SUBMODE_HCRC;
1434         u8 dcrc = csk->submode & CXGBIT_SUBMODE_DCRC;
1435         unsigned int len = roundup(sizeof(*req), 16);
1436         int ret;
1437
1438         skb = alloc_skb(len, GFP_KERNEL);
1439         if (!skb)
1440                 return -ENOMEM;
1441
1442         /*  set up ulp submode */
1443         req = __skb_put_zero(skb, len);
1444
1445         INIT_TP_WR(req, csk->tid);
1446         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1447         req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1448         req->word_cookie = htons(0);
1449         req->mask = cpu_to_be64(0x3 << 4);
1450         req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
1451                                 (dcrc ? ULP_CRC_DATA : 0)) << 4);
1452         set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1453
1454         cxgbit_get_csk(csk);
1455         cxgbit_init_wr_wait(&csk->com.wr_wait);
1456
1457         cxgbit_ofld_send(csk->com.cdev, skb);
1458
1459         ret = cxgbit_wait_for_reply(csk->com.cdev,
1460                                     &csk->com.wr_wait,
1461                                     csk->tid, 5, __func__);
1462         if (ret)
1463                 return -1;
1464
1465         return 0;
1466 }
1467
1468 int cxgbit_setup_conn_pgidx(struct cxgbit_sock *csk, u32 pg_idx)
1469 {
1470         struct sk_buff *skb;
1471         struct cpl_set_tcb_field *req;
1472         unsigned int len = roundup(sizeof(*req), 16);
1473         int ret;
1474
1475         skb = alloc_skb(len, GFP_KERNEL);
1476         if (!skb)
1477                 return -ENOMEM;
1478
1479         req = __skb_put_zero(skb, len);
1480
1481         INIT_TP_WR(req, csk->tid);
1482         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1483         req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1484         req->word_cookie = htons(0);
1485         req->mask = cpu_to_be64(0x3 << 8);
1486         req->val = cpu_to_be64(pg_idx << 8);
1487         set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1488
1489         cxgbit_get_csk(csk);
1490         cxgbit_init_wr_wait(&csk->com.wr_wait);
1491
1492         cxgbit_ofld_send(csk->com.cdev, skb);
1493
1494         ret = cxgbit_wait_for_reply(csk->com.cdev,
1495                                     &csk->com.wr_wait,
1496                                     csk->tid, 5, __func__);
1497         if (ret)
1498                 return -1;
1499
1500         return 0;
1501 }
1502
1503 static void
1504 cxgbit_pass_open_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1505 {
1506         struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1507         struct tid_info *t = cdev->lldi.tids;
1508         unsigned int stid = GET_TID(rpl);
1509         struct cxgbit_np *cnp = lookup_stid(t, stid);
1510
1511         pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1512                  __func__, cnp, stid, rpl->status);
1513
1514         if (!cnp) {
1515                 pr_info("%s stid %d lookup failure\n", __func__, stid);
1516                 goto rel_skb;
1517         }
1518
1519         cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1520         cxgbit_put_cnp(cnp);
1521 rel_skb:
1522         __kfree_skb(skb);
1523 }
1524
1525 static void
1526 cxgbit_close_listsrv_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1527 {
1528         struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1529         struct tid_info *t = cdev->lldi.tids;
1530         unsigned int stid = GET_TID(rpl);
1531         struct cxgbit_np *cnp = lookup_stid(t, stid);
1532
1533         pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1534                  __func__, cnp, stid, rpl->status);
1535
1536         if (!cnp) {
1537                 pr_info("%s stid %d lookup failure\n", __func__, stid);
1538                 goto rel_skb;
1539         }
1540
1541         cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1542         cxgbit_put_cnp(cnp);
1543 rel_skb:
1544         __kfree_skb(skb);
1545 }
1546
1547 static void
1548 cxgbit_pass_establish(struct cxgbit_device *cdev, struct sk_buff *skb)
1549 {
1550         struct cpl_pass_establish *req = cplhdr(skb);
1551         struct tid_info *t = cdev->lldi.tids;
1552         unsigned int tid = GET_TID(req);
1553         struct cxgbit_sock *csk;
1554         struct cxgbit_np *cnp;
1555         u16 tcp_opt = be16_to_cpu(req->tcp_opt);
1556         u32 snd_isn = be32_to_cpu(req->snd_isn);
1557         u32 rcv_isn = be32_to_cpu(req->rcv_isn);
1558
1559         csk = lookup_tid(t, tid);
1560         if (unlikely(!csk)) {
1561                 pr_err("can't find connection for tid %u.\n", tid);
1562                 goto rel_skb;
1563         }
1564         cnp = csk->cnp;
1565
1566         pr_debug("%s: csk %p; tid %u; cnp %p\n",
1567                  __func__, csk, tid, cnp);
1568
1569         csk->write_seq = snd_isn;
1570         csk->snd_una = snd_isn;
1571         csk->snd_nxt = snd_isn;
1572
1573         csk->rcv_nxt = rcv_isn;
1574
1575         if (csk->rcv_win > (RCV_BUFSIZ_M << 10))
1576                 csk->rx_credits = (csk->rcv_win - (RCV_BUFSIZ_M << 10));
1577
1578         csk->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
1579         cxgbit_set_emss(csk, tcp_opt);
1580         dst_confirm(csk->dst);
1581         csk->com.state = CSK_STATE_ESTABLISHED;
1582         spin_lock_bh(&cnp->np_accept_lock);
1583         list_add_tail(&csk->accept_node, &cnp->np_accept_list);
1584         spin_unlock_bh(&cnp->np_accept_lock);
1585         complete(&cnp->accept_comp);
1586 rel_skb:
1587         __kfree_skb(skb);
1588 }
1589
1590 static void cxgbit_queue_rx_skb(struct cxgbit_sock *csk, struct sk_buff *skb)
1591 {
1592         cxgbit_skcb_flags(skb) = 0;
1593         spin_lock_bh(&csk->rxq.lock);
1594         __skb_queue_tail(&csk->rxq, skb);
1595         spin_unlock_bh(&csk->rxq.lock);
1596         wake_up(&csk->waitq);
1597 }
1598
1599 static void cxgbit_peer_close(struct cxgbit_sock *csk, struct sk_buff *skb)
1600 {
1601         pr_debug("%s: csk %p; tid %u; state %d\n",
1602                  __func__, csk, csk->tid, csk->com.state);
1603
1604         switch (csk->com.state) {
1605         case CSK_STATE_ESTABLISHED:
1606                 csk->com.state = CSK_STATE_CLOSING;
1607                 cxgbit_queue_rx_skb(csk, skb);
1608                 return;
1609         case CSK_STATE_CLOSING:
1610                 /* simultaneous close */
1611                 csk->com.state = CSK_STATE_MORIBUND;
1612                 break;
1613         case CSK_STATE_MORIBUND:
1614                 csk->com.state = CSK_STATE_DEAD;
1615                 cxgbit_put_csk(csk);
1616                 break;
1617         case CSK_STATE_ABORTING:
1618                 break;
1619         default:
1620                 pr_info("%s: cpl_peer_close in bad state %d\n",
1621                         __func__, csk->com.state);
1622         }
1623
1624         __kfree_skb(skb);
1625 }
1626
1627 static void cxgbit_close_con_rpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1628 {
1629         pr_debug("%s: csk %p; tid %u; state %d\n",
1630                  __func__, csk, csk->tid, csk->com.state);
1631
1632         switch (csk->com.state) {
1633         case CSK_STATE_CLOSING:
1634                 csk->com.state = CSK_STATE_MORIBUND;
1635                 break;
1636         case CSK_STATE_MORIBUND:
1637                 csk->com.state = CSK_STATE_DEAD;
1638                 cxgbit_put_csk(csk);
1639                 break;
1640         case CSK_STATE_ABORTING:
1641         case CSK_STATE_DEAD:
1642                 break;
1643         default:
1644                 pr_info("%s: cpl_close_con_rpl in bad state %d\n",
1645                         __func__, csk->com.state);
1646         }
1647
1648         __kfree_skb(skb);
1649 }
1650
1651 static void cxgbit_abort_req_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1652 {
1653         struct cpl_abort_req_rss *hdr = cplhdr(skb);
1654         unsigned int tid = GET_TID(hdr);
1655         struct sk_buff *rpl_skb;
1656         bool release = false;
1657         bool wakeup_thread = false;
1658         u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
1659
1660         pr_debug("%s: csk %p; tid %u; state %d\n",
1661                  __func__, csk, tid, csk->com.state);
1662
1663         if (cxgb_is_neg_adv(hdr->status)) {
1664                 pr_err("%s: got neg advise %d on tid %u\n",
1665                        __func__, hdr->status, tid);
1666                 goto rel_skb;
1667         }
1668
1669         switch (csk->com.state) {
1670         case CSK_STATE_CONNECTING:
1671         case CSK_STATE_MORIBUND:
1672                 csk->com.state = CSK_STATE_DEAD;
1673                 release = true;
1674                 break;
1675         case CSK_STATE_ESTABLISHED:
1676                 csk->com.state = CSK_STATE_DEAD;
1677                 wakeup_thread = true;
1678                 break;
1679         case CSK_STATE_CLOSING:
1680                 csk->com.state = CSK_STATE_DEAD;
1681                 if (!csk->conn)
1682                         release = true;
1683                 break;
1684         case CSK_STATE_ABORTING:
1685                 break;
1686         default:
1687                 pr_info("%s: cpl_abort_req_rss in bad state %d\n",
1688                         __func__, csk->com.state);
1689                 csk->com.state = CSK_STATE_DEAD;
1690         }
1691
1692         __skb_queue_purge(&csk->txq);
1693
1694         if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
1695                 cxgbit_send_tx_flowc_wr(csk);
1696
1697         rpl_skb = __skb_dequeue(&csk->skbq);
1698
1699         cxgb_mk_abort_rpl(rpl_skb, len, csk->tid, csk->txq_idx);
1700         cxgbit_ofld_send(csk->com.cdev, rpl_skb);
1701
1702         if (wakeup_thread) {
1703                 cxgbit_queue_rx_skb(csk, skb);
1704                 return;
1705         }
1706
1707         if (release)
1708                 cxgbit_put_csk(csk);
1709 rel_skb:
1710         __kfree_skb(skb);
1711 }
1712
1713 static void cxgbit_abort_rpl_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1714 {
1715         pr_debug("%s: csk %p; tid %u; state %d\n",
1716                  __func__, csk, csk->tid, csk->com.state);
1717
1718         switch (csk->com.state) {
1719         case CSK_STATE_ABORTING:
1720                 csk->com.state = CSK_STATE_DEAD;
1721                 cxgbit_put_csk(csk);
1722                 break;
1723         default:
1724                 pr_info("%s: cpl_abort_rpl_rss in state %d\n",
1725                         __func__, csk->com.state);
1726         }
1727
1728         __kfree_skb(skb);
1729 }
1730
1731 static bool cxgbit_credit_err(const struct cxgbit_sock *csk)
1732 {
1733         const struct sk_buff *skb = csk->wr_pending_head;
1734         u32 credit = 0;
1735
1736         if (unlikely(csk->wr_cred > csk->wr_max_cred)) {
1737                 pr_err("csk 0x%p, tid %u, credit %u > %u\n",
1738                        csk, csk->tid, csk->wr_cred, csk->wr_max_cred);
1739                 return true;
1740         }
1741
1742         while (skb) {
1743                 credit += (__force u32)skb->csum;
1744                 skb = cxgbit_skcb_tx_wr_next(skb);
1745         }
1746
1747         if (unlikely((csk->wr_cred + credit) != csk->wr_max_cred)) {
1748                 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1749                        csk, csk->tid, csk->wr_cred,
1750                        credit, csk->wr_max_cred);
1751
1752                 return true;
1753         }
1754
1755         return false;
1756 }
1757
1758 static void cxgbit_fw4_ack(struct cxgbit_sock *csk, struct sk_buff *skb)
1759 {
1760         struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)cplhdr(skb);
1761         u32 credits = rpl->credits;
1762         u32 snd_una = ntohl(rpl->snd_una);
1763
1764         csk->wr_cred += credits;
1765         if (csk->wr_una_cred > (csk->wr_max_cred - csk->wr_cred))
1766                 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1767
1768         while (credits) {
1769                 struct sk_buff *p = cxgbit_sock_peek_wr(csk);
1770                 u32 csum;
1771
1772                 if (unlikely(!p)) {
1773                         pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
1774                                csk, csk->tid, credits,
1775                                csk->wr_cred, csk->wr_una_cred);
1776                         break;
1777                 }
1778
1779                 csum = (__force u32)p->csum;
1780                 if (unlikely(credits < csum)) {
1781                         pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
1782                                 csk,  csk->tid,
1783                                 credits, csk->wr_cred, csk->wr_una_cred,
1784                                 csum);
1785                         p->csum = (__force __wsum)(csum - credits);
1786                         break;
1787                 }
1788
1789                 cxgbit_sock_dequeue_wr(csk);
1790                 credits -= csum;
1791                 kfree_skb(p);
1792         }
1793
1794         if (unlikely(cxgbit_credit_err(csk))) {
1795                 cxgbit_queue_rx_skb(csk, skb);
1796                 return;
1797         }
1798
1799         if (rpl->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
1800                 if (unlikely(before(snd_una, csk->snd_una))) {
1801                         pr_warn("csk 0x%p,%u, snd_una %u/%u.",
1802                                 csk, csk->tid, snd_una,
1803                                 csk->snd_una);
1804                         goto rel_skb;
1805                 }
1806
1807                 if (csk->snd_una != snd_una) {
1808                         csk->snd_una = snd_una;
1809                         dst_confirm(csk->dst);
1810                         wake_up(&csk->ack_waitq);
1811                 }
1812         }
1813
1814         if (skb_queue_len(&csk->txq))
1815                 cxgbit_push_tx_frames(csk);
1816
1817 rel_skb:
1818         __kfree_skb(skb);
1819 }
1820
1821 static void cxgbit_set_tcb_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1822 {
1823         struct cxgbit_sock *csk;
1824         struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
1825         unsigned int tid = GET_TID(rpl);
1826         struct cxgb4_lld_info *lldi = &cdev->lldi;
1827         struct tid_info *t = lldi->tids;
1828
1829         csk = lookup_tid(t, tid);
1830         if (unlikely(!csk)) {
1831                 pr_err("can't find connection for tid %u.\n", tid);
1832                 goto rel_skb;
1833         } else {
1834                 cxgbit_wake_up(&csk->com.wr_wait, __func__, rpl->status);
1835         }
1836
1837         cxgbit_put_csk(csk);
1838 rel_skb:
1839         __kfree_skb(skb);
1840 }
1841
1842 static void cxgbit_rx_data(struct cxgbit_device *cdev, struct sk_buff *skb)
1843 {
1844         struct cxgbit_sock *csk;
1845         struct cpl_rx_data *cpl = cplhdr(skb);
1846         unsigned int tid = GET_TID(cpl);
1847         struct cxgb4_lld_info *lldi = &cdev->lldi;
1848         struct tid_info *t = lldi->tids;
1849
1850         csk = lookup_tid(t, tid);
1851         if (unlikely(!csk)) {
1852                 pr_err("can't find conn. for tid %u.\n", tid);
1853                 goto rel_skb;
1854         }
1855
1856         cxgbit_queue_rx_skb(csk, skb);
1857         return;
1858 rel_skb:
1859         __kfree_skb(skb);
1860 }
1861
1862 static void
1863 __cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1864 {
1865         spin_lock(&csk->lock);
1866         if (csk->lock_owner) {
1867                 __skb_queue_tail(&csk->backlogq, skb);
1868                 spin_unlock(&csk->lock);
1869                 return;
1870         }
1871
1872         cxgbit_skcb_rx_backlog_fn(skb)(csk, skb);
1873         spin_unlock(&csk->lock);
1874 }
1875
1876 static void cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1877 {
1878         cxgbit_get_csk(csk);
1879         __cxgbit_process_rx_cpl(csk, skb);
1880         cxgbit_put_csk(csk);
1881 }
1882
1883 static void cxgbit_rx_cpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1884 {
1885         struct cxgbit_sock *csk;
1886         struct cpl_tx_data *cpl = cplhdr(skb);
1887         struct cxgb4_lld_info *lldi = &cdev->lldi;
1888         struct tid_info *t = lldi->tids;
1889         unsigned int tid = GET_TID(cpl);
1890         u8 opcode = cxgbit_skcb_rx_opcode(skb);
1891         bool ref = true;
1892
1893         switch (opcode) {
1894         case CPL_FW4_ACK:
1895                         cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_fw4_ack;
1896                         ref = false;
1897                         break;
1898         case CPL_PEER_CLOSE:
1899                         cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_peer_close;
1900                         break;
1901         case CPL_CLOSE_CON_RPL:
1902                         cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_close_con_rpl;
1903                         break;
1904         case CPL_ABORT_REQ_RSS:
1905                         cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_req_rss;
1906                         break;
1907         case CPL_ABORT_RPL_RSS:
1908                         cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_rpl_rss;
1909                         break;
1910         default:
1911                 goto rel_skb;
1912         }
1913
1914         csk = lookup_tid(t, tid);
1915         if (unlikely(!csk)) {
1916                 pr_err("can't find conn. for tid %u.\n", tid);
1917                 goto rel_skb;
1918         }
1919
1920         if (ref)
1921                 cxgbit_process_rx_cpl(csk, skb);
1922         else
1923                 __cxgbit_process_rx_cpl(csk, skb);
1924
1925         return;
1926 rel_skb:
1927         __kfree_skb(skb);
1928 }
1929
1930 cxgbit_cplhandler_func cxgbit_cplhandlers[NUM_CPL_CMDS] = {
1931         [CPL_PASS_OPEN_RPL]     = cxgbit_pass_open_rpl,
1932         [CPL_CLOSE_LISTSRV_RPL] = cxgbit_close_listsrv_rpl,
1933         [CPL_PASS_ACCEPT_REQ]   = cxgbit_pass_accept_req,
1934         [CPL_PASS_ESTABLISH]    = cxgbit_pass_establish,
1935         [CPL_SET_TCB_RPL]       = cxgbit_set_tcb_rpl,
1936         [CPL_RX_DATA]           = cxgbit_rx_data,
1937         [CPL_FW4_ACK]           = cxgbit_rx_cpl,
1938         [CPL_PEER_CLOSE]        = cxgbit_rx_cpl,
1939         [CPL_CLOSE_CON_RPL]     = cxgbit_rx_cpl,
1940         [CPL_ABORT_REQ_RSS]     = cxgbit_rx_cpl,
1941         [CPL_ABORT_RPL_RSS]     = cxgbit_rx_cpl,
1942 };