]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/infiniband/hw/cxgb3/iwch_cm.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[net-next-2.6.git] / drivers / infiniband / hw / cxgb3 / iwch_cm.c
CommitLineData
b038ced7
SW
1/*
2 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
b038ced7
SW
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include <linux/module.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <linux/skbuff.h>
36#include <linux/timer.h>
37#include <linux/notifier.h>
8704e9a8 38#include <linux/inetdevice.h>
b038ced7
SW
39
40#include <net/neighbour.h>
41#include <net/netevent.h>
42#include <net/route.h>
43
44#include "tcb.h"
45#include "cxgb3_offload.h"
46#include "iwch.h"
47#include "iwch_provider.h"
48#include "iwch_cm.h"
49
50static char *states[] = {
51 "idle",
52 "listen",
53 "connecting",
54 "mpa_wait_req",
55 "mpa_req_sent",
56 "mpa_req_rcvd",
57 "mpa_rep_sent",
58 "fpdu_mode",
59 "aborting",
60 "closing",
61 "moribund",
62 "dead",
63 NULL,
64};
65
f8b0dfd1
SW
66int peer2peer = 0;
67module_param(peer2peer, int, 0644);
68MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
69
77a8d574 70static int ep_timeout_secs = 60;
e54664c0 71module_param(ep_timeout_secs, int, 0644);
b038ced7 72MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
77a8d574 73 "in seconds (default=60)");
b038ced7
SW
74
75static int mpa_rev = 1;
e54664c0 76module_param(mpa_rev, int, 0644);
b038ced7
SW
77MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
78 "1 is spec compliant. (default=1)");
79
80static int markers_enabled = 0;
e54664c0 81module_param(markers_enabled, int, 0644);
b038ced7
SW
82MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
83
84static int crc_enabled = 1;
e54664c0 85module_param(crc_enabled, int, 0644);
b038ced7
SW
86MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
87
88static int rcv_win = 256 * 1024;
e54664c0 89module_param(rcv_win, int, 0644);
b038ced7
SW
90MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
91
92static int snd_win = 32 * 1024;
e54664c0 93module_param(snd_win, int, 0644);
b038ced7
SW
94MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
95
96static unsigned int nocong = 0;
e54664c0 97module_param(nocong, uint, 0644);
b038ced7
SW
98MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
99
100static unsigned int cong_flavor = 1;
e54664c0 101module_param(cong_flavor, uint, 0644);
b038ced7
SW
102MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
103
104static void process_work(struct work_struct *work);
105static struct workqueue_struct *workq;
106static DECLARE_WORK(skb_work, process_work);
107
108static struct sk_buff_head rxq;
109static cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS];
110
111static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
112static void ep_timeout(unsigned long arg);
113static void connect_reply_upcall(struct iwch_ep *ep, int status);
114
115static void start_ep_timer(struct iwch_ep *ep)
116{
33718363 117 PDBG("%s ep %p\n", __func__, ep);
b038ced7 118 if (timer_pending(&ep->timer)) {
33718363 119 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
b038ced7
SW
120 del_timer_sync(&ep->timer);
121 } else
122 get_ep(&ep->com);
123 ep->timer.expires = jiffies + ep_timeout_secs * HZ;
124 ep->timer.data = (unsigned long)ep;
125 ep->timer.function = ep_timeout;
126 add_timer(&ep->timer);
127}
128
129static void stop_ep_timer(struct iwch_ep *ep)
130{
33718363 131 PDBG("%s ep %p\n", __func__, ep);
989a1780
SW
132 if (!timer_pending(&ep->timer)) {
133 printk(KERN_ERR "%s timer stopped when its not running! ep %p state %u\n",
134 __func__, ep, ep->com.state);
135 WARN_ON(1);
136 return;
137 }
b038ced7
SW
138 del_timer_sync(&ep->timer);
139 put_ep(&ep->com);
140}
141
04b5d028
SW
142int iwch_l2t_send(struct t3cdev *tdev, struct sk_buff *skb, struct l2t_entry *l2e)
143{
144 int error = 0;
145 struct cxio_rdev *rdev;
146
147 rdev = (struct cxio_rdev *)tdev->ulp;
148 if (cxio_fatal_error(rdev)) {
149 kfree_skb(skb);
150 return -EIO;
151 }
152 error = l2t_send(tdev, skb, l2e);
153 if (error)
154 kfree_skb(skb);
155 return error;
156}
157
158int iwch_cxgb3_ofld_send(struct t3cdev *tdev, struct sk_buff *skb)
159{
160 int error = 0;
161 struct cxio_rdev *rdev;
162
163 rdev = (struct cxio_rdev *)tdev->ulp;
164 if (cxio_fatal_error(rdev)) {
165 kfree_skb(skb);
166 return -EIO;
167 }
168 error = cxgb3_ofld_send(tdev, skb);
169 if (error)
170 kfree_skb(skb);
171 return error;
172}
173
b038ced7
SW
174static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
175{
176 struct cpl_tid_release *req;
177
178 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
179 if (!skb)
180 return;
181 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
182 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
183 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
184 skb->priority = CPL_PRIORITY_SETUP;
04b5d028 185 iwch_cxgb3_ofld_send(tdev, skb);
b038ced7
SW
186 return;
187}
188
189int iwch_quiesce_tid(struct iwch_ep *ep)
190{
191 struct cpl_set_tcb_field *req;
192 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
193
194 if (!skb)
195 return -ENOMEM;
196 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
197 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
198 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
199 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
200 req->reply = 0;
201 req->cpu_idx = 0;
202 req->word = htons(W_TCB_RX_QUIESCE);
203 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
204 req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
205
206 skb->priority = CPL_PRIORITY_DATA;
04b5d028 207 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
b038ced7
SW
208}
209
210int iwch_resume_tid(struct iwch_ep *ep)
211{
212 struct cpl_set_tcb_field *req;
213 struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
214
215 if (!skb)
216 return -ENOMEM;
217 req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
218 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
219 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
220 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
221 req->reply = 0;
222 req->cpu_idx = 0;
223 req->word = htons(W_TCB_RX_QUIESCE);
224 req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
225 req->val = 0;
226
227 skb->priority = CPL_PRIORITY_DATA;
04b5d028 228 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
b038ced7
SW
229}
230
231static void set_emss(struct iwch_ep *ep, u16 opt)
232{
33718363 233 PDBG("%s ep %p opt %u\n", __func__, ep, opt);
b038ced7
SW
234 ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
235 if (G_TCPOPT_TSTAMP(opt))
236 ep->emss -= 12;
237 if (ep->emss < 128)
238 ep->emss = 128;
239 PDBG("emss=%d\n", ep->emss);
240}
241
242static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
243{
244 unsigned long flags;
245 enum iwch_ep_state state;
246
247 spin_lock_irqsave(&epc->lock, flags);
248 state = epc->state;
249 spin_unlock_irqrestore(&epc->lock, flags);
250 return state;
251}
252
2b540355 253static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
b038ced7
SW
254{
255 epc->state = new;
256}
257
258static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
259{
260 unsigned long flags;
261
262 spin_lock_irqsave(&epc->lock, flags);
33718363 263 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
b038ced7
SW
264 __state_set(epc, new);
265 spin_unlock_irqrestore(&epc->lock, flags);
266 return;
267}
268
269static void *alloc_ep(int size, gfp_t gfp)
270{
271 struct iwch_ep_common *epc;
272
dd00cc48 273 epc = kzalloc(size, gfp);
b038ced7 274 if (epc) {
b038ced7
SW
275 kref_init(&epc->kref);
276 spin_lock_init(&epc->lock);
277 init_waitqueue_head(&epc->waitq);
278 }
33718363 279 PDBG("%s alloc ep %p\n", __func__, epc);
b038ced7
SW
280 return epc;
281}
282
283void __free_ep(struct kref *kref)
284{
874d8df5
SW
285 struct iwch_ep *ep;
286 ep = container_of(container_of(kref, struct iwch_ep_common, kref),
287 struct iwch_ep, com);
288 PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
289 if (ep->com.flags & RELEASE_RESOURCES) {
290 cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
291 dst_release(ep->dst);
292 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
293 }
294 kfree(ep);
b038ced7
SW
295}
296
297static void release_ep_resources(struct iwch_ep *ep)
298{
33718363 299 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
874d8df5 300 ep->com.flags |= RELEASE_RESOURCES;
b038ced7
SW
301 put_ep(&ep->com);
302}
303
304static void process_work(struct work_struct *work)
305{
306 struct sk_buff *skb = NULL;
307 void *ep;
308 struct t3cdev *tdev;
309 int ret;
310
311 while ((skb = skb_dequeue(&rxq))) {
312 ep = *((void **) (skb->cb));
313 tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
314 ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
315 if (ret & CPL_RET_BUF_DONE)
316 kfree_skb(skb);
317
318 /*
319 * ep was referenced in sched(), and is freed here.
320 */
321 put_ep((struct iwch_ep_common *)ep);
322 }
323}
324
325static int status2errno(int status)
326{
327 switch (status) {
328 case CPL_ERR_NONE:
329 return 0;
330 case CPL_ERR_CONN_RESET:
331 return -ECONNRESET;
332 case CPL_ERR_ARP_MISS:
333 return -EHOSTUNREACH;
334 case CPL_ERR_CONN_TIMEDOUT:
335 return -ETIMEDOUT;
336 case CPL_ERR_TCAM_FULL:
337 return -ENOMEM;
338 case CPL_ERR_CONN_EXIST:
339 return -EADDRINUSE;
340 default:
341 return -EIO;
342 }
343}
344
345/*
346 * Try and reuse skbs already allocated...
347 */
348static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
349{
1f6a849b 350 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
b038ced7
SW
351 skb_trim(skb, 0);
352 skb_get(skb);
353 } else {
354 skb = alloc_skb(len, gfp);
355 }
356 return skb;
357}
358
359static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
360 __be32 peer_ip, __be16 local_port,
361 __be16 peer_port, u8 tos)
362{
363 struct rtable *rt;
364 struct flowi fl = {
365 .oif = 0,
366 .nl_u = {
367 .ip4_u = {
368 .daddr = peer_ip,
369 .saddr = local_ip,
370 .tos = tos}
371 },
372 .proto = IPPROTO_TCP,
373 .uli_u = {
374 .ports = {
375 .sport = local_port,
376 .dport = peer_port}
377 }
378 };
379
f1b050bf 380 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
b038ced7
SW
381 return NULL;
382 return rt;
383}
384
385static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
386{
387 int i = 0;
388
389 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
390 ++i;
391 return i;
392}
393
394static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
395{
33718363 396 PDBG("%s t3cdev %p\n", __func__, dev);
b038ced7
SW
397 kfree_skb(skb);
398}
399
400/*
401 * Handle an ARP failure for an active open.
402 */
403static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
404{
405 printk(KERN_ERR MOD "ARP failure duing connect\n");
406 kfree_skb(skb);
407}
408
409/*
410 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
411 * and send it along.
412 */
413static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
414{
415 struct cpl_abort_req *req = cplhdr(skb);
416
33718363 417 PDBG("%s t3cdev %p\n", __func__, dev);
b038ced7 418 req->cmd = CPL_ABORT_NO_RST;
04b5d028 419 iwch_cxgb3_ofld_send(dev, skb);
b038ced7
SW
420}
421
422static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
423{
424 struct cpl_close_con_req *req;
425 struct sk_buff *skb;
426
33718363 427 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
428 skb = get_skb(NULL, sizeof(*req), gfp);
429 if (!skb) {
33718363 430 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
b038ced7
SW
431 return -ENOMEM;
432 }
433 skb->priority = CPL_PRIORITY_DATA;
434 set_arp_failure_handler(skb, arp_failure_discard);
435 req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
436 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
437 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
438 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
04b5d028 439 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
440}
441
442static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
443{
444 struct cpl_abort_req *req;
445
33718363 446 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
447 skb = get_skb(skb, sizeof(*req), gfp);
448 if (!skb) {
449 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
33718363 450 __func__);
b038ced7
SW
451 return -ENOMEM;
452 }
453 skb->priority = CPL_PRIORITY_DATA;
454 set_arp_failure_handler(skb, abort_arp_failure);
455 req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
456 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
457 req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
458 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
459 req->cmd = CPL_ABORT_SEND_RST;
04b5d028 460 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
461}
462
463static int send_connect(struct iwch_ep *ep)
464{
465 struct cpl_act_open_req *req;
466 struct sk_buff *skb;
467 u32 opt0h, opt0l, opt2;
468 unsigned int mtu_idx;
469 int wscale;
470
33718363 471 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
472
473 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
474 if (!skb) {
475 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
33718363 476 __func__);
b038ced7
SW
477 return -ENOMEM;
478 }
479 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
480 wscale = compute_wscale(rcv_win);
481 opt0h = V_NAGLE(0) |
482 V_NO_CONG(nocong) |
483 V_KEEP_ALIVE(1) |
484 F_TCAM_BYPASS |
485 V_WND_SCALE(wscale) |
486 V_MSS_IDX(mtu_idx) |
487 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
488 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
489 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
490 skb->priority = CPL_PRIORITY_SETUP;
491 set_arp_failure_handler(skb, act_open_req_arp_failure);
492
493 req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
494 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
495 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
496 req->local_port = ep->com.local_addr.sin_port;
497 req->peer_port = ep->com.remote_addr.sin_port;
498 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
499 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
500 req->opt0h = htonl(opt0h);
501 req->opt0l = htonl(opt0l);
502 req->params = 0;
503 req->opt2 = htonl(opt2);
04b5d028 504 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
505}
506
507static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
508{
509 int mpalen;
510 struct tx_data_wr *req;
511 struct mpa_message *mpa;
512 int len;
513
33718363 514 PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
b038ced7
SW
515
516 BUG_ON(skb_cloned(skb));
517
518 mpalen = sizeof(*mpa) + ep->plen;
4305b541 519 if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
b038ced7
SW
520 kfree_skb(skb);
521 skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
522 if (!skb) {
523 connect_reply_upcall(ep, -ENOMEM);
524 return;
525 }
526 }
527 skb_trim(skb, 0);
528 skb_reserve(skb, sizeof(*req));
529 skb_put(skb, mpalen);
530 skb->priority = CPL_PRIORITY_DATA;
531 mpa = (struct mpa_message *) skb->data;
532 memset(mpa, 0, sizeof(*mpa));
533 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
534 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
535 (markers_enabled ? MPA_MARKERS : 0);
536 mpa->private_data_size = htons(ep->plen);
537 mpa->revision = mpa_rev;
538
539 if (ep->plen)
540 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
541
542 /*
543 * Reference the mpa skb. This ensures the data area
544 * will remain in memory until the hw acks the tx.
545 * Function tx_ack() will deref it.
546 */
547 skb_get(skb);
548 set_arp_failure_handler(skb, arp_failure_discard);
badff6d0 549 skb_reset_transport_header(skb);
b038ced7
SW
550 len = skb->len;
551 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
f8b0dfd1 552 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
b038ced7
SW
553 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
554 req->len = htonl(len);
555 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
556 V_TX_SNDBUF(snd_win>>15));
de3d3530 557 req->flags = htonl(F_TX_INIT);
b038ced7
SW
558 req->sndseq = htonl(ep->snd_seq);
559 BUG_ON(ep->mpa_skb);
560 ep->mpa_skb = skb;
04b5d028 561 iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
562 start_ep_timer(ep);
563 state_set(&ep->com, MPA_REQ_SENT);
564 return;
565}
566
567static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
568{
569 int mpalen;
570 struct tx_data_wr *req;
571 struct mpa_message *mpa;
572 struct sk_buff *skb;
573
33718363 574 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
b038ced7
SW
575
576 mpalen = sizeof(*mpa) + plen;
577
578 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
579 if (!skb) {
33718363 580 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
b038ced7
SW
581 return -ENOMEM;
582 }
583 skb_reserve(skb, sizeof(*req));
584 mpa = (struct mpa_message *) skb_put(skb, mpalen);
585 memset(mpa, 0, sizeof(*mpa));
586 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
587 mpa->flags = MPA_REJECT;
588 mpa->revision = mpa_rev;
589 mpa->private_data_size = htons(plen);
590 if (plen)
591 memcpy(mpa->private_data, pdata, plen);
592
593 /*
594 * Reference the mpa skb again. This ensures the data area
595 * will remain in memory until the hw acks the tx.
596 * Function tx_ack() will deref it.
597 */
598 skb_get(skb);
599 skb->priority = CPL_PRIORITY_DATA;
600 set_arp_failure_handler(skb, arp_failure_discard);
badff6d0 601 skb_reset_transport_header(skb);
b038ced7 602 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
f8b0dfd1 603 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
b038ced7
SW
604 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
605 req->len = htonl(mpalen);
606 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
607 V_TX_SNDBUF(snd_win>>15));
de3d3530 608 req->flags = htonl(F_TX_INIT);
b038ced7
SW
609 req->sndseq = htonl(ep->snd_seq);
610 BUG_ON(ep->mpa_skb);
611 ep->mpa_skb = skb;
04b5d028 612 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
613}
614
615static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
616{
617 int mpalen;
618 struct tx_data_wr *req;
619 struct mpa_message *mpa;
620 int len;
621 struct sk_buff *skb;
622
33718363 623 PDBG("%s ep %p plen %d\n", __func__, ep, plen);
b038ced7
SW
624
625 mpalen = sizeof(*mpa) + plen;
626
627 skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
628 if (!skb) {
33718363 629 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
b038ced7
SW
630 return -ENOMEM;
631 }
632 skb->priority = CPL_PRIORITY_DATA;
633 skb_reserve(skb, sizeof(*req));
634 mpa = (struct mpa_message *) skb_put(skb, mpalen);
635 memset(mpa, 0, sizeof(*mpa));
636 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
637 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
638 (markers_enabled ? MPA_MARKERS : 0);
639 mpa->revision = mpa_rev;
640 mpa->private_data_size = htons(plen);
641 if (plen)
642 memcpy(mpa->private_data, pdata, plen);
643
644 /*
645 * Reference the mpa skb. This ensures the data area
646 * will remain in memory until the hw acks the tx.
647 * Function tx_ack() will deref it.
648 */
649 skb_get(skb);
650 set_arp_failure_handler(skb, arp_failure_discard);
badff6d0 651 skb_reset_transport_header(skb);
b038ced7
SW
652 len = skb->len;
653 req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
f8b0dfd1 654 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA)|F_WR_COMPL);
b038ced7
SW
655 req->wr_lo = htonl(V_WR_TID(ep->hwtid));
656 req->len = htonl(len);
657 req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
658 V_TX_SNDBUF(snd_win>>15));
de3d3530 659 req->flags = htonl(F_TX_INIT);
b038ced7
SW
660 req->sndseq = htonl(ep->snd_seq);
661 ep->mpa_skb = skb;
662 state_set(&ep->com, MPA_REP_SENT);
04b5d028 663 return iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
664}
665
666static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
667{
668 struct iwch_ep *ep = ctx;
669 struct cpl_act_establish *req = cplhdr(skb);
670 unsigned int tid = GET_TID(req);
671
33718363 672 PDBG("%s ep %p tid %d\n", __func__, ep, tid);
b038ced7
SW
673
674 dst_confirm(ep->dst);
675
676 /* setup the hwtid for this connection */
677 ep->hwtid = tid;
678 cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
679
680 ep->snd_seq = ntohl(req->snd_isn);
de3d3530 681 ep->rcv_seq = ntohl(req->rcv_isn);
b038ced7
SW
682
683 set_emss(ep, ntohs(req->tcp_opt));
684
685 /* dealloc the atid */
686 cxgb3_free_atid(ep->com.tdev, ep->atid);
687
688 /* start MPA negotiation */
689 send_mpa_req(ep, skb);
690
691 return 0;
692}
693
694static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
695{
696 PDBG("%s ep %p\n", __FILE__, ep);
697 state_set(&ep->com, ABORTING);
698 send_abort(ep, skb, gfp);
699}
700
701static void close_complete_upcall(struct iwch_ep *ep)
702{
703 struct iw_cm_event event;
704
33718363 705 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
706 memset(&event, 0, sizeof(event));
707 event.event = IW_CM_EVENT_CLOSE;
708 if (ep->com.cm_id) {
709 PDBG("close complete delivered ep %p cm_id %p tid %d\n",
710 ep, ep->com.cm_id, ep->hwtid);
711 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
712 ep->com.cm_id->rem_ref(ep->com.cm_id);
713 ep->com.cm_id = NULL;
714 ep->com.qp = NULL;
715 }
716}
717
718static void peer_close_upcall(struct iwch_ep *ep)
719{
720 struct iw_cm_event event;
721
33718363 722 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
723 memset(&event, 0, sizeof(event));
724 event.event = IW_CM_EVENT_DISCONNECT;
725 if (ep->com.cm_id) {
726 PDBG("peer close delivered ep %p cm_id %p tid %d\n",
727 ep, ep->com.cm_id, ep->hwtid);
728 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
729 }
730}
731
732static void peer_abort_upcall(struct iwch_ep *ep)
733{
734 struct iw_cm_event event;
735
33718363 736 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
737 memset(&event, 0, sizeof(event));
738 event.event = IW_CM_EVENT_CLOSE;
739 event.status = -ECONNRESET;
740 if (ep->com.cm_id) {
741 PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
742 ep->com.cm_id, ep->hwtid);
743 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
744 ep->com.cm_id->rem_ref(ep->com.cm_id);
745 ep->com.cm_id = NULL;
746 ep->com.qp = NULL;
747 }
748}
749
750static void connect_reply_upcall(struct iwch_ep *ep, int status)
751{
752 struct iw_cm_event event;
753
33718363 754 PDBG("%s ep %p status %d\n", __func__, ep, status);
b038ced7
SW
755 memset(&event, 0, sizeof(event));
756 event.event = IW_CM_EVENT_CONNECT_REPLY;
757 event.status = status;
758 event.local_addr = ep->com.local_addr;
759 event.remote_addr = ep->com.remote_addr;
760
761 if ((status == 0) || (status == -ECONNREFUSED)) {
762 event.private_data_len = ep->plen;
763 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
764 }
765 if (ep->com.cm_id) {
33718363 766 PDBG("%s ep %p tid %d status %d\n", __func__, ep,
b038ced7
SW
767 ep->hwtid, status);
768 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
769 }
770 if (status < 0) {
771 ep->com.cm_id->rem_ref(ep->com.cm_id);
772 ep->com.cm_id = NULL;
773 ep->com.qp = NULL;
774 }
775}
776
777static void connect_request_upcall(struct iwch_ep *ep)
778{
779 struct iw_cm_event event;
780
33718363 781 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
b038ced7
SW
782 memset(&event, 0, sizeof(event));
783 event.event = IW_CM_EVENT_CONNECT_REQUEST;
784 event.local_addr = ep->com.local_addr;
785 event.remote_addr = ep->com.remote_addr;
786 event.private_data_len = ep->plen;
787 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
788 event.provider_data = ep;
789 if (state_read(&ep->parent_ep->com) != DEAD)
790 ep->parent_ep->com.cm_id->event_handler(
791 ep->parent_ep->com.cm_id,
792 &event);
793 put_ep(&ep->parent_ep->com);
794 ep->parent_ep = NULL;
795}
796
797static void established_upcall(struct iwch_ep *ep)
798{
799 struct iw_cm_event event;
800
33718363 801 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
802 memset(&event, 0, sizeof(event));
803 event.event = IW_CM_EVENT_ESTABLISHED;
804 if (ep->com.cm_id) {
33718363 805 PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
b038ced7
SW
806 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
807 }
808}
809
810static int update_rx_credits(struct iwch_ep *ep, u32 credits)
811{
812 struct cpl_rx_data_ack *req;
813 struct sk_buff *skb;
814
33718363 815 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
b038ced7
SW
816 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
817 if (!skb) {
818 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
819 return 0;
820 }
821
822 req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
823 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
824 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
825 req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
826 skb->priority = CPL_PRIORITY_ACK;
04b5d028 827 iwch_cxgb3_ofld_send(ep->com.tdev, skb);
b038ced7
SW
828 return credits;
829}
830
831static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
832{
833 struct mpa_message *mpa;
834 u16 plen;
835 struct iwch_qp_attributes attrs;
836 enum iwch_qp_attr_mask mask;
837 int err;
838
33718363 839 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
840
841 /*
842 * Stop mpa timer. If it expired, then the state has
843 * changed and we bail since ep_timeout already aborted
844 * the connection.
845 */
846 stop_ep_timer(ep);
847 if (state_read(&ep->com) != MPA_REQ_SENT)
848 return;
849
850 /*
851 * If we get more than the supported amount of private data
852 * then we must fail this connection.
853 */
854 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
855 err = -EINVAL;
856 goto err;
857 }
858
859 /*
860 * copy the new data into our accumulation buffer.
861 */
d626f62b
ACM
862 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
863 skb->len);
b038ced7
SW
864 ep->mpa_pkt_len += skb->len;
865
866 /*
867 * if we don't even have the mpa message, then bail.
868 */
869 if (ep->mpa_pkt_len < sizeof(*mpa))
870 return;
871 mpa = (struct mpa_message *) ep->mpa_pkt;
872
873 /* Validate MPA header. */
874 if (mpa->revision != mpa_rev) {
875 err = -EPROTO;
876 goto err;
877 }
878 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
879 err = -EPROTO;
880 goto err;
881 }
882
883 plen = ntohs(mpa->private_data_size);
884
885 /*
886 * Fail if there's too much private data.
887 */
888 if (plen > MPA_MAX_PRIVATE_DATA) {
889 err = -EPROTO;
890 goto err;
891 }
892
893 /*
894 * If plen does not account for pkt size
895 */
896 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
897 err = -EPROTO;
898 goto err;
899 }
900
901 ep->plen = (u8) plen;
902
903 /*
904 * If we don't have all the pdata yet, then bail.
905 * We'll continue process when more data arrives.
906 */
907 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
908 return;
909
910 if (mpa->flags & MPA_REJECT) {
911 err = -ECONNREFUSED;
912 goto err;
913 }
914
915 /*
916 * If we get here we have accumulated the entire mpa
917 * start reply message including private data. And
918 * the MPA header is valid.
919 */
920 state_set(&ep->com, FPDU_MODE);
f8b0dfd1 921 ep->mpa_attr.initiator = 1;
b038ced7
SW
922 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
923 ep->mpa_attr.recv_marker_enabled = markers_enabled;
924 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
925 ep->mpa_attr.version = mpa_rev;
926 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
33718363 927 "xmit_marker_enabled=%d, version=%d\n", __func__,
b038ced7
SW
928 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
929 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
930
931 attrs.mpa_attr = ep->mpa_attr;
932 attrs.max_ird = ep->ird;
933 attrs.max_ord = ep->ord;
934 attrs.llp_stream_handle = ep;
935 attrs.next_state = IWCH_QP_STATE_RTS;
936
937 mask = IWCH_QP_ATTR_NEXT_STATE |
938 IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
939 IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
940
941 /* bind QP and TID with INIT_WR */
942 err = iwch_modify_qp(ep->com.qp->rhp,
943 ep->com.qp, mask, &attrs, 1);
f8b0dfd1
SW
944 if (err)
945 goto err;
946
947 if (peer2peer && iwch_rqes_posted(ep->com.qp) == 0) {
948 iwch_post_zb_read(ep->com.qp);
949 }
950
951 goto out;
b038ced7
SW
952err:
953 abort_connection(ep, skb, GFP_KERNEL);
954out:
955 connect_reply_upcall(ep, err);
956 return;
957}
958
959static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
960{
961 struct mpa_message *mpa;
962 u16 plen;
963
33718363 964 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
965
966 /*
967 * Stop mpa timer. If it expired, then the state has
968 * changed and we bail since ep_timeout already aborted
969 * the connection.
970 */
971 stop_ep_timer(ep);
972 if (state_read(&ep->com) != MPA_REQ_WAIT)
973 return;
974
975 /*
976 * If we get more than the supported amount of private data
977 * then we must fail this connection.
978 */
979 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
980 abort_connection(ep, skb, GFP_KERNEL);
981 return;
982 }
983
33718363 984 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
b038ced7
SW
985
986 /*
987 * Copy the new data into our accumulation buffer.
988 */
d626f62b
ACM
989 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
990 skb->len);
b038ced7
SW
991 ep->mpa_pkt_len += skb->len;
992
993 /*
994 * If we don't even have the mpa message, then bail.
995 * We'll continue process when more data arrives.
996 */
997 if (ep->mpa_pkt_len < sizeof(*mpa))
998 return;
33718363 999 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
b038ced7
SW
1000 mpa = (struct mpa_message *) ep->mpa_pkt;
1001
1002 /*
1003 * Validate MPA Header.
1004 */
1005 if (mpa->revision != mpa_rev) {
1006 abort_connection(ep, skb, GFP_KERNEL);
1007 return;
1008 }
1009
1010 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1011 abort_connection(ep, skb, GFP_KERNEL);
1012 return;
1013 }
1014
1015 plen = ntohs(mpa->private_data_size);
1016
1017 /*
1018 * Fail if there's too much private data.
1019 */
1020 if (plen > MPA_MAX_PRIVATE_DATA) {
1021 abort_connection(ep, skb, GFP_KERNEL);
1022 return;
1023 }
1024
1025 /*
1026 * If plen does not account for pkt size
1027 */
1028 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1029 abort_connection(ep, skb, GFP_KERNEL);
1030 return;
1031 }
1032 ep->plen = (u8) plen;
1033
1034 /*
1035 * If we don't have all the pdata yet, then bail.
1036 */
1037 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1038 return;
1039
1040 /*
1041 * If we get here we have accumulated the entire mpa
1042 * start reply message including private data.
1043 */
f8b0dfd1 1044 ep->mpa_attr.initiator = 0;
b038ced7
SW
1045 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1046 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1047 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1048 ep->mpa_attr.version = mpa_rev;
1049 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
33718363 1050 "xmit_marker_enabled=%d, version=%d\n", __func__,
b038ced7
SW
1051 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1052 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1053
1054 state_set(&ep->com, MPA_REQ_RCVD);
1055
1056 /* drive upcall */
1057 connect_request_upcall(ep);
1058 return;
1059}
1060
1061static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1062{
1063 struct iwch_ep *ep = ctx;
1064 struct cpl_rx_data *hdr = cplhdr(skb);
1065 unsigned int dlen = ntohs(hdr->len);
1066
33718363 1067 PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
b038ced7
SW
1068
1069 skb_pull(skb, sizeof(*hdr));
1070 skb_trim(skb, dlen);
1071
de3d3530
SW
1072 ep->rcv_seq += dlen;
1073 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1074
b038ced7
SW
1075 switch (state_read(&ep->com)) {
1076 case MPA_REQ_SENT:
1077 process_mpa_reply(ep, skb);
1078 break;
1079 case MPA_REQ_WAIT:
1080 process_mpa_request(ep, skb);
1081 break;
1082 case MPA_REP_SENT:
1083 break;
1084 default:
1085 printk(KERN_ERR MOD "%s Unexpected streaming data."
1086 " ep %p state %d tid %d\n",
33718363 1087 __func__, ep, state_read(&ep->com), ep->hwtid);
b038ced7
SW
1088
1089 /*
1090 * The ep will timeout and inform the ULP of the failure.
1091 * See ep_timeout().
1092 */
1093 break;
1094 }
1095
1096 /* update RX credits */
1097 update_rx_credits(ep, dlen);
1098
1099 return CPL_RET_BUF_DONE;
1100}
1101
1102/*
1103 * Upcall from the adapter indicating data has been transmitted.
1104 * For us its just the single MPA request or reply. We can now free
1105 * the skb holding the mpa message.
1106 */
1107static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1108{
1109 struct iwch_ep *ep = ctx;
1110 struct cpl_wr_ack *hdr = cplhdr(skb);
1111 unsigned int credits = ntohs(hdr->credits);
b038ced7 1112
33718363 1113 PDBG("%s ep %p credits %u\n", __func__, ep, credits);
b038ced7 1114
f8b0dfd1
SW
1115 if (credits == 0) {
1116 PDBG(KERN_ERR "%s 0 credit ack ep %p state %u\n",
1117 __func__, ep, state_read(&ep->com));
b038ced7 1118 return CPL_RET_BUF_DONE;
f8b0dfd1
SW
1119 }
1120
b038ced7 1121 BUG_ON(credits != 1);
b038ced7 1122 dst_confirm(ep->dst);
f8b0dfd1
SW
1123 if (!ep->mpa_skb) {
1124 PDBG("%s rdma_init wr_ack ep %p state %u\n",
1125 __func__, ep, state_read(&ep->com));
1126 if (ep->mpa_attr.initiator) {
1127 PDBG("%s initiator ep %p state %u\n",
1128 __func__, ep, state_read(&ep->com));
1129 if (peer2peer)
1130 iwch_post_zb_read(ep->com.qp);
1131 } else {
1132 PDBG("%s responder ep %p state %u\n",
1133 __func__, ep, state_read(&ep->com));
1134 ep->com.rpl_done = 1;
1135 wake_up(&ep->com.waitq);
1136 }
1137 } else {
1138 PDBG("%s lsm ack ep %p state %u freeing skb\n",
1139 __func__, ep, state_read(&ep->com));
1140 kfree_skb(ep->mpa_skb);
1141 ep->mpa_skb = NULL;
b038ced7
SW
1142 }
1143 return CPL_RET_BUF_DONE;
1144}
1145
1146static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1147{
1148 struct iwch_ep *ep = ctx;
989a1780
SW
1149 unsigned long flags;
1150 int release = 0;
b038ced7 1151
33718363 1152 PDBG("%s ep %p\n", __func__, ep);
989a1780 1153 BUG_ON(!ep);
b038ced7 1154
aff9e39d
SW
1155 /*
1156 * We get 2 abort replies from the HW. The first one must
1157 * be ignored except for scribbling that we need one more.
1158 */
874d8df5
SW
1159 if (!(ep->com.flags & ABORT_REQ_IN_PROGRESS)) {
1160 ep->com.flags |= ABORT_REQ_IN_PROGRESS;
aff9e39d
SW
1161 return CPL_RET_BUF_DONE;
1162 }
1163
989a1780
SW
1164 spin_lock_irqsave(&ep->com.lock, flags);
1165 switch (ep->com.state) {
1166 case ABORTING:
1167 close_complete_upcall(ep);
1168 __state_set(&ep->com, DEAD);
1169 release = 1;
1170 break;
1171 default:
1172 printk(KERN_ERR "%s ep %p state %d\n",
1173 __func__, ep, ep->com.state);
1174 break;
1175 }
1176 spin_unlock_irqrestore(&ep->com.lock, flags);
1177
1178 if (release)
1179 release_ep_resources(ep);
b038ced7
SW
1180 return CPL_RET_BUF_DONE;
1181}
1182
96d0e493
SW
1183/*
1184 * Return whether a failed active open has allocated a TID
1185 */
1186static inline int act_open_has_tid(int status)
1187{
1188 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1189 status != CPL_ERR_ARP_MISS;
1190}
1191
b038ced7
SW
1192static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1193{
1194 struct iwch_ep *ep = ctx;
1195 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1196
33718363 1197 PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
b038ced7
SW
1198 status2errno(rpl->status));
1199 connect_reply_upcall(ep, status2errno(rpl->status));
1200 state_set(&ep->com, DEAD);
8176d297 1201 if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
b038ced7
SW
1202 release_tid(ep->com.tdev, GET_TID(rpl), NULL);
1203 cxgb3_free_atid(ep->com.tdev, ep->atid);
1204 dst_release(ep->dst);
1205 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
1206 put_ep(&ep->com);
1207 return CPL_RET_BUF_DONE;
1208}
1209
1210static int listen_start(struct iwch_listen_ep *ep)
1211{
1212 struct sk_buff *skb;
1213 struct cpl_pass_open_req *req;
1214
33718363 1215 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1216 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1217 if (!skb) {
1218 printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
1219 return -ENOMEM;
1220 }
1221
1222 req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
1223 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1224 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
1225 req->local_port = ep->com.local_addr.sin_port;
1226 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
1227 req->peer_port = 0;
1228 req->peer_ip = 0;
1229 req->peer_netmask = 0;
1230 req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
1231 req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
1232 req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
1233
1234 skb->priority = 1;
04b5d028 1235 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
b038ced7
SW
1236}
1237
1238static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1239{
1240 struct iwch_listen_ep *ep = ctx;
1241 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1242
33718363 1243 PDBG("%s ep %p status %d error %d\n", __func__, ep,
b038ced7
SW
1244 rpl->status, status2errno(rpl->status));
1245 ep->com.rpl_err = status2errno(rpl->status);
1246 ep->com.rpl_done = 1;
1247 wake_up(&ep->com.waitq);
1248
1249 return CPL_RET_BUF_DONE;
1250}
1251
1252static int listen_stop(struct iwch_listen_ep *ep)
1253{
1254 struct sk_buff *skb;
1255 struct cpl_close_listserv_req *req;
1256
33718363 1257 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1258 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1259 if (!skb) {
33718363 1260 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
b038ced7
SW
1261 return -ENOMEM;
1262 }
1263 req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
1264 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
60be4b59 1265 req->cpu_idx = 0;
b038ced7
SW
1266 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
1267 skb->priority = 1;
04b5d028 1268 return iwch_cxgb3_ofld_send(ep->com.tdev, skb);
b038ced7
SW
1269}
1270
1271static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
1272 void *ctx)
1273{
1274 struct iwch_listen_ep *ep = ctx;
1275 struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
1276
33718363 1277 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1278 ep->com.rpl_err = status2errno(rpl->status);
1279 ep->com.rpl_done = 1;
1280 wake_up(&ep->com.waitq);
1281 return CPL_RET_BUF_DONE;
1282}
1283
1284static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
1285{
1286 struct cpl_pass_accept_rpl *rpl;
1287 unsigned int mtu_idx;
1288 u32 opt0h, opt0l, opt2;
1289 int wscale;
1290
33718363 1291 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1292 BUG_ON(skb_cloned(skb));
1293 skb_trim(skb, sizeof(*rpl));
1294 skb_get(skb);
1295 mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
1296 wscale = compute_wscale(rcv_win);
1297 opt0h = V_NAGLE(0) |
1298 V_NO_CONG(nocong) |
1299 V_KEEP_ALIVE(1) |
1300 F_TCAM_BYPASS |
1301 V_WND_SCALE(wscale) |
1302 V_MSS_IDX(mtu_idx) |
1303 V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
1304 opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
1305 opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
1306
1307 rpl = cplhdr(skb);
1308 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1309 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
1310 rpl->peer_ip = peer_ip;
1311 rpl->opt0h = htonl(opt0h);
1312 rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
1313 rpl->opt2 = htonl(opt2);
1314 rpl->rsvd = rpl->opt2; /* workaround for HW bug */
1315 skb->priority = CPL_PRIORITY_SETUP;
04b5d028 1316 iwch_l2t_send(ep->com.tdev, skb, ep->l2t);
b038ced7
SW
1317
1318 return;
1319}
1320
1321static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
1322 struct sk_buff *skb)
1323{
33718363 1324 PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
b038ced7
SW
1325 peer_ip);
1326 BUG_ON(skb_cloned(skb));
1327 skb_trim(skb, sizeof(struct cpl_tid_release));
1328 skb_get(skb);
1329
8176d297 1330 if (tdev->type != T3A)
b038ced7
SW
1331 release_tid(tdev, hwtid, skb);
1332 else {
1333 struct cpl_pass_accept_rpl *rpl;
1334
1335 rpl = cplhdr(skb);
1336 skb->priority = CPL_PRIORITY_SETUP;
1337 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1338 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1339 hwtid));
1340 rpl->peer_ip = peer_ip;
1341 rpl->opt0h = htonl(F_TCAM_BYPASS);
1342 rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
1343 rpl->opt2 = 0;
1344 rpl->rsvd = rpl->opt2;
04b5d028 1345 iwch_cxgb3_ofld_send(tdev, skb);
b038ced7
SW
1346 }
1347}
1348
1349static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1350{
1351 struct iwch_ep *child_ep, *parent_ep = ctx;
1352 struct cpl_pass_accept_req *req = cplhdr(skb);
1353 unsigned int hwtid = GET_TID(req);
1354 struct dst_entry *dst;
1355 struct l2t_entry *l2t;
1356 struct rtable *rt;
1357 struct iff_mac tim;
1358
33718363 1359 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
b038ced7
SW
1360
1361 if (state_read(&parent_ep->com) != LISTEN) {
1362 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
33718363 1363 __func__);
b038ced7
SW
1364 goto reject;
1365 }
1366
1367 /*
1368 * Find the netdev for this connection request.
1369 */
1370 tim.mac_addr = req->dst_mac;
1371 tim.vlan_tag = ntohs(req->vlan_tag);
1372 if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
1373 printk(KERN_ERR
1374 "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
33718363 1375 __func__,
b038ced7
SW
1376 req->dst_mac[0],
1377 req->dst_mac[1],
1378 req->dst_mac[2],
1379 req->dst_mac[3],
1380 req->dst_mac[4],
1381 req->dst_mac[5]);
1382 goto reject;
1383 }
1384
1385 /* Find output route */
1386 rt = find_route(tdev,
1387 req->local_ip,
1388 req->peer_ip,
1389 req->local_port,
1390 req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
1391 if (!rt) {
1392 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
33718363 1393 __func__);
b038ced7
SW
1394 goto reject;
1395 }
1396 dst = &rt->u.dst;
1397 l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
1398 if (!l2t) {
1399 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
33718363 1400 __func__);
b038ced7
SW
1401 dst_release(dst);
1402 goto reject;
1403 }
1404 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1405 if (!child_ep) {
1406 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
33718363 1407 __func__);
b038ced7
SW
1408 l2t_release(L2DATA(tdev), l2t);
1409 dst_release(dst);
1410 goto reject;
1411 }
1412 state_set(&child_ep->com, CONNECTING);
1413 child_ep->com.tdev = tdev;
1414 child_ep->com.cm_id = NULL;
1415 child_ep->com.local_addr.sin_family = PF_INET;
1416 child_ep->com.local_addr.sin_port = req->local_port;
1417 child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
1418 child_ep->com.remote_addr.sin_family = PF_INET;
1419 child_ep->com.remote_addr.sin_port = req->peer_port;
1420 child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
1421 get_ep(&parent_ep->com);
1422 child_ep->parent_ep = parent_ep;
1423 child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
1424 child_ep->l2t = l2t;
1425 child_ep->dst = dst;
1426 child_ep->hwtid = hwtid;
1427 init_timer(&child_ep->timer);
1428 cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
1429 accept_cr(child_ep, req->peer_ip, skb);
1430 goto out;
1431reject:
1432 reject_cr(tdev, hwtid, req->peer_ip, skb);
1433out:
1434 return CPL_RET_BUF_DONE;
1435}
1436
1437static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1438{
1439 struct iwch_ep *ep = ctx;
1440 struct cpl_pass_establish *req = cplhdr(skb);
1441
33718363 1442 PDBG("%s ep %p\n", __func__, ep);
b038ced7 1443 ep->snd_seq = ntohl(req->snd_isn);
de3d3530 1444 ep->rcv_seq = ntohl(req->rcv_isn);
b038ced7
SW
1445
1446 set_emss(ep, ntohs(req->tcp_opt));
1447
1448 dst_confirm(ep->dst);
1449 state_set(&ep->com, MPA_REQ_WAIT);
1450 start_ep_timer(ep);
1451
1452 return CPL_RET_BUF_DONE;
1453}
1454
1455static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1456{
1457 struct iwch_ep *ep = ctx;
1458 struct iwch_qp_attributes attrs;
1459 unsigned long flags;
1460 int disconnect = 1;
1461 int release = 0;
1462
33718363 1463 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1464 dst_confirm(ep->dst);
1465
1466 spin_lock_irqsave(&ep->com.lock, flags);
1467 switch (ep->com.state) {
1468 case MPA_REQ_WAIT:
1469 __state_set(&ep->com, CLOSING);
1470 break;
1471 case MPA_REQ_SENT:
1472 __state_set(&ep->com, CLOSING);
1473 connect_reply_upcall(ep, -ECONNRESET);
1474 break;
1475 case MPA_REQ_RCVD:
1476
1477 /*
1478 * We're gonna mark this puppy DEAD, but keep
1479 * the reference on it until the ULP accepts or
1480 * rejects the CR.
1481 */
1482 __state_set(&ep->com, CLOSING);
1483 get_ep(&ep->com);
1484 break;
1485 case MPA_REP_SENT:
1486 __state_set(&ep->com, CLOSING);
1487 ep->com.rpl_done = 1;
1488 ep->com.rpl_err = -ECONNRESET;
1489 PDBG("waking up ep %p\n", ep);
1490 wake_up(&ep->com.waitq);
1491 break;
1492 case FPDU_MODE:
42e31753 1493 start_ep_timer(ep);
b038ced7
SW
1494 __state_set(&ep->com, CLOSING);
1495 attrs.next_state = IWCH_QP_STATE_CLOSING;
1496 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1497 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1498 peer_close_upcall(ep);
1499 break;
1500 case ABORTING:
1501 disconnect = 0;
1502 break;
1503 case CLOSING:
b038ced7
SW
1504 __state_set(&ep->com, MORIBUND);
1505 disconnect = 0;
1506 break;
1507 case MORIBUND:
1508 stop_ep_timer(ep);
1509 if (ep->com.cm_id && ep->com.qp) {
1510 attrs.next_state = IWCH_QP_STATE_IDLE;
1511 iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
1512 IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
1513 }
1514 close_complete_upcall(ep);
1515 __state_set(&ep->com, DEAD);
1516 release = 1;
1517 disconnect = 0;
1518 break;
1519 case DEAD:
1520 disconnect = 0;
1521 break;
1522 default:
1523 BUG_ON(1);
1524 }
1525 spin_unlock_irqrestore(&ep->com.lock, flags);
1526 if (disconnect)
1527 iwch_ep_disconnect(ep, 0, GFP_KERNEL);
1528 if (release)
1529 release_ep_resources(ep);
1530 return CPL_RET_BUF_DONE;
1531}
1532
1533/*
1534 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1535 */
2b540355 1536static int is_neg_adv_abort(unsigned int status)
b038ced7
SW
1537{
1538 return status == CPL_ERR_RTX_NEG_ADVICE ||
1539 status == CPL_ERR_PERSIST_NEG_ADVICE;
1540}
1541
1542static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1543{
1544 struct cpl_abort_req_rss *req = cplhdr(skb);
1545 struct iwch_ep *ep = ctx;
1546 struct cpl_abort_rpl *rpl;
1547 struct sk_buff *rpl_skb;
1548 struct iwch_qp_attributes attrs;
1549 int ret;
989a1780
SW
1550 int release = 0;
1551 unsigned long flags;
b038ced7 1552
1580367e 1553 if (is_neg_adv_abort(req->status)) {
33718363 1554 PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
1580367e
SW
1555 ep->hwtid);
1556 t3_l2t_send_event(ep->com.tdev, ep->l2t);
1557 return CPL_RET_BUF_DONE;
1558 }
1559
aff9e39d
SW
1560 /*
1561 * We get 2 peer aborts from the HW. The first one must
1562 * be ignored except for scribbling that we need one more.
1563 */
874d8df5
SW
1564 if (!(ep->com.flags & PEER_ABORT_IN_PROGRESS)) {
1565 ep->com.flags |= PEER_ABORT_IN_PROGRESS;
aff9e39d
SW
1566 return CPL_RET_BUF_DONE;
1567 }
1568
989a1780
SW
1569 spin_lock_irqsave(&ep->com.lock, flags);
1570 PDBG("%s ep %p state %u\n", __func__, ep, ep->com.state);
1571 switch (ep->com.state) {
b038ced7
SW
1572 case CONNECTING:
1573 break;
1574 case MPA_REQ_WAIT:
adf376b3 1575 stop_ep_timer(ep);
b038ced7
SW
1576 break;
1577 case MPA_REQ_SENT:
adf376b3 1578 stop_ep_timer(ep);
b038ced7
SW
1579 connect_reply_upcall(ep, -ECONNRESET);
1580 break;
1581 case MPA_REP_SENT:
1582 ep->com.rpl_done = 1;
1583 ep->com.rpl_err = -ECONNRESET;
1584 PDBG("waking up ep %p\n", ep);
1585 wake_up(&ep->com.waitq);
1586 break;
1587 case MPA_REQ_RCVD:
1588
1589 /*
1590 * We're gonna mark this puppy DEAD, but keep
1591 * the reference on it until the ULP accepts or
1592 * rejects the CR.
1593 */
1594 get_ep(&ep->com);
1595 break;
1596 case MORIBUND:
42e31753 1597 case CLOSING:
b038ced7 1598 stop_ep_timer(ep);
42e31753 1599 /*FALLTHROUGH*/
b038ced7 1600 case FPDU_MODE:
b038ced7
SW
1601 if (ep->com.cm_id && ep->com.qp) {
1602 attrs.next_state = IWCH_QP_STATE_ERROR;
1603 ret = iwch_modify_qp(ep->com.qp->rhp,
1604 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1605 &attrs, 1);
1606 if (ret)
1607 printk(KERN_ERR MOD
1608 "%s - qp <- error failed!\n",
33718363 1609 __func__);
b038ced7
SW
1610 }
1611 peer_abort_upcall(ep);
1612 break;
1613 case ABORTING:
1614 break;
1615 case DEAD:
33718363 1616 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
989a1780 1617 spin_unlock_irqrestore(&ep->com.lock, flags);
b038ced7
SW
1618 return CPL_RET_BUF_DONE;
1619 default:
1620 BUG_ON(1);
1621 break;
1622 }
1623 dst_confirm(ep->dst);
989a1780
SW
1624 if (ep->com.state != ABORTING) {
1625 __state_set(&ep->com, DEAD);
1626 release = 1;
1627 }
1628 spin_unlock_irqrestore(&ep->com.lock, flags);
b038ced7
SW
1629
1630 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1631 if (!rpl_skb) {
1632 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
33718363 1633 __func__);
989a1780
SW
1634 release = 1;
1635 goto out;
b038ced7
SW
1636 }
1637 rpl_skb->priority = CPL_PRIORITY_DATA;
1638 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1639 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
1640 rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
1641 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1642 rpl->cmd = CPL_ABORT_NO_RST;
04b5d028 1643 iwch_cxgb3_ofld_send(ep->com.tdev, rpl_skb);
989a1780
SW
1644out:
1645 if (release)
b038ced7 1646 release_ep_resources(ep);
b038ced7
SW
1647 return CPL_RET_BUF_DONE;
1648}
1649
1650static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1651{
1652 struct iwch_ep *ep = ctx;
1653 struct iwch_qp_attributes attrs;
1654 unsigned long flags;
1655 int release = 0;
1656
33718363 1657 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1658 BUG_ON(!ep);
1659
1660 /* The cm_id may be null if we failed to connect */
1661 spin_lock_irqsave(&ep->com.lock, flags);
1662 switch (ep->com.state) {
1663 case CLOSING:
b038ced7
SW
1664 __state_set(&ep->com, MORIBUND);
1665 break;
1666 case MORIBUND:
1667 stop_ep_timer(ep);
1668 if ((ep->com.cm_id) && (ep->com.qp)) {
1669 attrs.next_state = IWCH_QP_STATE_IDLE;
1670 iwch_modify_qp(ep->com.qp->rhp,
1671 ep->com.qp,
1672 IWCH_QP_ATTR_NEXT_STATE,
1673 &attrs, 1);
1674 }
1675 close_complete_upcall(ep);
1676 __state_set(&ep->com, DEAD);
1677 release = 1;
1678 break;
42e31753 1679 case ABORTING:
b038ced7 1680 case DEAD:
c4d49776 1681 break;
b038ced7
SW
1682 default:
1683 BUG_ON(1);
1684 break;
1685 }
1686 spin_unlock_irqrestore(&ep->com.lock, flags);
1687 if (release)
1688 release_ep_resources(ep);
1689 return CPL_RET_BUF_DONE;
1690}
1691
1692/*
1693 * T3A does 3 things when a TERM is received:
1694 * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
1695 * 2) generate an async event on the QP with the TERMINATE opcode
1696 * 3) post a TERMINATE opcde cqe into the associated CQ.
1697 *
1698 * For (1), we save the message in the qp for later consumer consumption.
1699 * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
1700 * For (3), we toss the CQE in cxio_poll_cq().
1701 *
1702 * terminate() handles case (1)...
1703 */
1704static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1705{
1706 struct iwch_ep *ep = ctx;
1707
42fb61f0
SW
1708 if (state_read(&ep->com) != FPDU_MODE)
1709 return CPL_RET_BUF_DONE;
1710
33718363 1711 PDBG("%s ep %p\n", __func__, ep);
b038ced7 1712 skb_pull(skb, sizeof(struct cpl_rdma_terminate));
33718363 1713 PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
d626f62b
ACM
1714 skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
1715 skb->len);
b038ced7
SW
1716 ep->com.qp->attr.terminate_msg_len = skb->len;
1717 ep->com.qp->attr.is_terminate_local = 0;
1718 return CPL_RET_BUF_DONE;
1719}
1720
1721static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
1722{
1723 struct cpl_rdma_ec_status *rep = cplhdr(skb);
1724 struct iwch_ep *ep = ctx;
1725
33718363 1726 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
b038ced7
SW
1727 rep->status);
1728 if (rep->status) {
1729 struct iwch_qp_attributes attrs;
1730
1731 printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
33718363 1732 __func__, ep->hwtid);
2f236735 1733 stop_ep_timer(ep);
b038ced7
SW
1734 attrs.next_state = IWCH_QP_STATE_ERROR;
1735 iwch_modify_qp(ep->com.qp->rhp,
1736 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1737 &attrs, 1);
1738 abort_connection(ep, NULL, GFP_KERNEL);
1739 }
1740 return CPL_RET_BUF_DONE;
1741}
1742
1743static void ep_timeout(unsigned long arg)
1744{
1745 struct iwch_ep *ep = (struct iwch_ep *)arg;
1746 struct iwch_qp_attributes attrs;
1747 unsigned long flags;
989a1780 1748 int abort = 1;
b038ced7
SW
1749
1750 spin_lock_irqsave(&ep->com.lock, flags);
33718363 1751 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
b038ced7
SW
1752 ep->com.state);
1753 switch (ep->com.state) {
1754 case MPA_REQ_SENT:
989a1780 1755 __state_set(&ep->com, ABORTING);
b038ced7
SW
1756 connect_reply_upcall(ep, -ETIMEDOUT);
1757 break;
1758 case MPA_REQ_WAIT:
989a1780 1759 __state_set(&ep->com, ABORTING);
b038ced7 1760 break;
42e31753 1761 case CLOSING:
b038ced7
SW
1762 case MORIBUND:
1763 if (ep->com.cm_id && ep->com.qp) {
1764 attrs.next_state = IWCH_QP_STATE_ERROR;
1765 iwch_modify_qp(ep->com.qp->rhp,
1766 ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
1767 &attrs, 1);
1768 }
989a1780 1769 __state_set(&ep->com, ABORTING);
b038ced7
SW
1770 break;
1771 default:
989a1780
SW
1772 printk(KERN_ERR "%s unexpected state ep %p state %u\n",
1773 __func__, ep, ep->com.state);
1774 WARN_ON(1);
1775 abort = 0;
b038ced7 1776 }
b038ced7 1777 spin_unlock_irqrestore(&ep->com.lock, flags);
989a1780
SW
1778 if (abort)
1779 abort_connection(ep, NULL, GFP_ATOMIC);
b038ced7
SW
1780 put_ep(&ep->com);
1781}
1782
1783int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1784{
1785 int err;
1786 struct iwch_ep *ep = to_ep(cm_id);
33718363 1787 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
b038ced7
SW
1788
1789 if (state_read(&ep->com) == DEAD) {
1790 put_ep(&ep->com);
1791 return -ECONNRESET;
1792 }
1793 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
b038ced7
SW
1794 if (mpa_rev == 0)
1795 abort_connection(ep, NULL, GFP_KERNEL);
1796 else {
1797 err = send_mpa_reject(ep, pdata, pdata_len);
7d526e6b 1798 err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
b038ced7
SW
1799 }
1800 return 0;
1801}
1802
1803int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1804{
1805 int err;
1806 struct iwch_qp_attributes attrs;
1807 enum iwch_qp_attr_mask mask;
1808 struct iwch_ep *ep = to_ep(cm_id);
1809 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1810 struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
1811
33718363 1812 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
de3d3530 1813 if (state_read(&ep->com) == DEAD)
b038ced7 1814 return -ECONNRESET;
b038ced7
SW
1815
1816 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1817 BUG_ON(!qp);
1818
1819 if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
1820 (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
1821 abort_connection(ep, NULL, GFP_KERNEL);
1822 return -EINVAL;
1823 }
1824
1825 cm_id->add_ref(cm_id);
1826 ep->com.cm_id = cm_id;
1827 ep->com.qp = qp;
1828
1829 ep->com.rpl_done = 0;
1830 ep->com.rpl_err = 0;
1831 ep->ird = conn_param->ird;
1832 ep->ord = conn_param->ord;
33718363 1833 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
de3d3530 1834
b038ced7 1835 get_ep(&ep->com);
b038ced7
SW
1836
1837 /* bind QP to EP and move to RTS */
1838 attrs.mpa_attr = ep->mpa_attr;
1f71f503 1839 attrs.max_ird = ep->ird;
b038ced7
SW
1840 attrs.max_ord = ep->ord;
1841 attrs.llp_stream_handle = ep;
1842 attrs.next_state = IWCH_QP_STATE_RTS;
1843
1844 /* bind QP and TID with INIT_WR */
1845 mask = IWCH_QP_ATTR_NEXT_STATE |
1846 IWCH_QP_ATTR_LLP_STREAM_HANDLE |
1847 IWCH_QP_ATTR_MPA_ATTR |
1848 IWCH_QP_ATTR_MAX_IRD |
1849 IWCH_QP_ATTR_MAX_ORD;
1850
1851 err = iwch_modify_qp(ep->com.qp->rhp,
1852 ep->com.qp, mask, &attrs, 1);
de3d3530
SW
1853 if (err)
1854 goto err;
b038ced7 1855
f8b0dfd1
SW
1856 /* if needed, wait for wr_ack */
1857 if (iwch_rqes_posted(qp)) {
1858 wait_event(ep->com.waitq, ep->com.rpl_done);
1859 err = ep->com.rpl_err;
1860 if (err)
1861 goto err;
1862 }
1863
de3d3530
SW
1864 err = send_mpa_reply(ep, conn_param->private_data,
1865 conn_param->private_data_len);
1866 if (err)
1867 goto err;
1868
de3d3530
SW
1869
1870 state_set(&ep->com, FPDU_MODE);
1871 established_upcall(ep);
1872 put_ep(&ep->com);
1873 return 0;
1874err:
1875 ep->com.cm_id = NULL;
1876 ep->com.qp = NULL;
1877 cm_id->rem_ref(cm_id);
b038ced7
SW
1878 put_ep(&ep->com);
1879 return err;
1880}
1881
8704e9a8
SW
1882static int is_loopback_dst(struct iw_cm_id *cm_id)
1883{
1884 struct net_device *dev;
1885
1886 dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
1887 if (!dev)
1888 return 0;
1889 dev_put(dev);
1890 return 1;
1891}
1892
b038ced7
SW
1893int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1894{
1895 int err = 0;
1896 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1897 struct iwch_ep *ep;
1898 struct rtable *rt;
1899
8704e9a8
SW
1900 if (is_loopback_dst(cm_id)) {
1901 err = -ENOSYS;
1902 goto out;
1903 }
1904
b038ced7
SW
1905 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1906 if (!ep) {
33718363 1907 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
b038ced7
SW
1908 err = -ENOMEM;
1909 goto out;
1910 }
1911 init_timer(&ep->timer);
1912 ep->plen = conn_param->private_data_len;
1913 if (ep->plen)
1914 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1915 conn_param->private_data, ep->plen);
1916 ep->ird = conn_param->ird;
1917 ep->ord = conn_param->ord;
1918 ep->com.tdev = h->rdev.t3cdev_p;
1919
1920 cm_id->add_ref(cm_id);
1921 ep->com.cm_id = cm_id;
1922 ep->com.qp = get_qhp(h, conn_param->qpn);
1923 BUG_ON(!ep->com.qp);
33718363 1924 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
b038ced7
SW
1925 ep->com.qp, cm_id);
1926
1927 /*
1928 * Allocate an active TID to initiate a TCP connection.
1929 */
1930 ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
1931 if (ep->atid == -1) {
33718363 1932 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
b038ced7
SW
1933 err = -ENOMEM;
1934 goto fail2;
1935 }
1936
1937 /* find a route */
1938 rt = find_route(h->rdev.t3cdev_p,
1939 cm_id->local_addr.sin_addr.s_addr,
1940 cm_id->remote_addr.sin_addr.s_addr,
1941 cm_id->local_addr.sin_port,
1942 cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
1943 if (!rt) {
33718363 1944 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
b038ced7
SW
1945 err = -EHOSTUNREACH;
1946 goto fail3;
1947 }
1948 ep->dst = &rt->u.dst;
1949
1950 /* get a l2t entry */
1951 ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
1952 ep->dst->neighbour->dev);
1953 if (!ep->l2t) {
33718363 1954 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
b038ced7
SW
1955 err = -ENOMEM;
1956 goto fail4;
1957 }
1958
1959 state_set(&ep->com, CONNECTING);
1960 ep->tos = IPTOS_LOWDELAY;
1961 ep->com.local_addr = cm_id->local_addr;
1962 ep->com.remote_addr = cm_id->remote_addr;
1963
1964 /* send connect request to rnic */
1965 err = send_connect(ep);
1966 if (!err)
1967 goto out;
1968
1969 l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
1970fail4:
1971 dst_release(ep->dst);
1972fail3:
1973 cxgb3_free_atid(ep->com.tdev, ep->atid);
1974fail2:
dc35fac9 1975 cm_id->rem_ref(cm_id);
b038ced7
SW
1976 put_ep(&ep->com);
1977out:
1978 return err;
1979}
1980
1981int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
1982{
1983 int err = 0;
1984 struct iwch_dev *h = to_iwch_dev(cm_id->device);
1985 struct iwch_listen_ep *ep;
1986
1987
1988 might_sleep();
1989
1990 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1991 if (!ep) {
33718363 1992 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
b038ced7
SW
1993 err = -ENOMEM;
1994 goto fail1;
1995 }
33718363 1996 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
1997 ep->com.tdev = h->rdev.t3cdev_p;
1998 cm_id->add_ref(cm_id);
1999 ep->com.cm_id = cm_id;
2000 ep->backlog = backlog;
2001 ep->com.local_addr = cm_id->local_addr;
2002
2003 /*
2004 * Allocate a server TID.
2005 */
2006 ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
2007 if (ep->stid == -1) {
33718363 2008 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
b038ced7
SW
2009 err = -ENOMEM;
2010 goto fail2;
2011 }
2012
2013 state_set(&ep->com, LISTEN);
2014 err = listen_start(ep);
2015 if (err)
2016 goto fail3;
2017
2018 /* wait for pass_open_rpl */
2019 wait_event(ep->com.waitq, ep->com.rpl_done);
2020 err = ep->com.rpl_err;
2021 if (!err) {
2022 cm_id->provider_data = ep;
2023 goto out;
2024 }
2025fail3:
2026 cxgb3_free_stid(ep->com.tdev, ep->stid);
2027fail2:
1b07db70 2028 cm_id->rem_ref(cm_id);
b038ced7
SW
2029 put_ep(&ep->com);
2030fail1:
2031out:
2032 return err;
2033}
2034
2035int iwch_destroy_listen(struct iw_cm_id *cm_id)
2036{
2037 int err;
2038 struct iwch_listen_ep *ep = to_listen_ep(cm_id);
2039
33718363 2040 PDBG("%s ep %p\n", __func__, ep);
b038ced7
SW
2041
2042 might_sleep();
2043 state_set(&ep->com, DEAD);
2044 ep->com.rpl_done = 0;
2045 ep->com.rpl_err = 0;
2046 err = listen_stop(ep);
04b5d028
SW
2047 if (err)
2048 goto done;
b038ced7
SW
2049 wait_event(ep->com.waitq, ep->com.rpl_done);
2050 cxgb3_free_stid(ep->com.tdev, ep->stid);
04b5d028 2051done:
b038ced7
SW
2052 err = ep->com.rpl_err;
2053 cm_id->rem_ref(cm_id);
2054 put_ep(&ep->com);
2055 return err;
2056}
2057
2058int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
2059{
2060 int ret=0;
2061 unsigned long flags;
2062 int close = 0;
04b5d028
SW
2063 int fatal = 0;
2064 struct t3cdev *tdev;
2065 struct cxio_rdev *rdev;
b038ced7
SW
2066
2067 spin_lock_irqsave(&ep->com.lock, flags);
2068
33718363 2069 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
b038ced7
SW
2070 states[ep->com.state], abrupt);
2071
04b5d028
SW
2072 tdev = (struct t3cdev *)ep->com.tdev;
2073 rdev = (struct cxio_rdev *)tdev->ulp;
2074 if (cxio_fatal_error(rdev)) {
2075 fatal = 1;
2076 close_complete_upcall(ep);
2077 ep->com.state = DEAD;
2078 }
b038ced7
SW
2079 switch (ep->com.state) {
2080 case MPA_REQ_WAIT:
2081 case MPA_REQ_SENT:
2082 case MPA_REQ_RCVD:
2083 case MPA_REP_SENT:
2084 case FPDU_MODE:
b038ced7 2085 close = 1;
989a1780
SW
2086 if (abrupt)
2087 ep->com.state = ABORTING;
2088 else {
2089 ep->com.state = CLOSING;
2090 start_ep_timer(ep);
2091 }
b038ced7
SW
2092 break;
2093 case CLOSING:
b038ced7 2094 close = 1;
989a1780
SW
2095 if (abrupt) {
2096 stop_ep_timer(ep);
2097 ep->com.state = ABORTING;
2098 } else
2099 ep->com.state = MORIBUND;
b038ced7
SW
2100 break;
2101 case MORIBUND:
989a1780
SW
2102 case ABORTING:
2103 case DEAD:
2104 PDBG("%s ignoring disconnect ep %p state %u\n",
2105 __func__, ep, ep->com.state);
b038ced7
SW
2106 break;
2107 default:
2108 BUG();
2109 break;
2110 }
989a1780 2111
b038ced7
SW
2112 spin_unlock_irqrestore(&ep->com.lock, flags);
2113 if (close) {
2114 if (abrupt)
2115 ret = send_abort(ep, NULL, gfp);
2116 else
2117 ret = send_halfclose(ep, gfp);
04b5d028
SW
2118 if (ret)
2119 fatal = 1;
b038ced7 2120 }
04b5d028
SW
2121 if (fatal)
2122 release_ep_resources(ep);
b038ced7
SW
2123 return ret;
2124}
2125
2126int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2127 struct l2t_entry *l2t)
2128{
2129 struct iwch_ep *ep = ctx;
2130
2131 if (ep->dst != old)
2132 return 0;
2133
33718363 2134 PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
b038ced7
SW
2135 l2t);
2136 dst_hold(new);
2137 l2t_release(L2DATA(ep->com.tdev), ep->l2t);
2138 ep->l2t = l2t;
2139 dst_release(old);
2140 ep->dst = new;
2141 return 1;
2142}
2143
2144/*
2145 * All the CM events are handled on a work queue to have a safe context.
2146 */
2147static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2148{
2149 struct iwch_ep_common *epc = ctx;
2150
2151 get_ep(epc);
2152
2153 /*
2154 * Save ctx and tdev in the skb->cb area.
2155 */
2156 *((void **) skb->cb) = ctx;
2157 *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
2158
2159 /*
2160 * Queue the skb and schedule the worker thread.
2161 */
2162 skb_queue_tail(&rxq, skb);
2163 queue_work(workq, &skb_work);
2164 return 0;
2165}
2166
1ca19770
SW
2167static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
2168{
2169 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2170
2171 if (rpl->status != CPL_ERR_NONE) {
2172 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2173 "for tid %u\n", rpl->status, GET_TID(rpl));
2174 }
2175 return CPL_RET_BUF_DONE;
2176}
2177
b038ced7
SW
2178int __init iwch_cm_init(void)
2179{
2180 skb_queue_head_init(&rxq);
2181
2182 workq = create_singlethread_workqueue("iw_cxgb3");
2183 if (!workq)
2184 return -ENOMEM;
2185
2186 /*
2187 * All upcalls from the T3 Core go to sched() to
2188 * schedule the processing on a work queue.
2189 */
2190 t3c_handlers[CPL_ACT_ESTABLISH] = sched;
2191 t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
2192 t3c_handlers[CPL_RX_DATA] = sched;
2193 t3c_handlers[CPL_TX_DMA_ACK] = sched;
2194 t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
2195 t3c_handlers[CPL_ABORT_RPL] = sched;
2196 t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
2197 t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
2198 t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
2199 t3c_handlers[CPL_PASS_ESTABLISH] = sched;
2200 t3c_handlers[CPL_PEER_CLOSE] = sched;
2201 t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
2202 t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
2203 t3c_handlers[CPL_RDMA_TERMINATE] = sched;
2204 t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
1ca19770 2205 t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
b038ced7
SW
2206
2207 /*
2208 * These are the real handlers that are called from a
2209 * work queue.
2210 */
2211 work_handlers[CPL_ACT_ESTABLISH] = act_establish;
2212 work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
2213 work_handlers[CPL_RX_DATA] = rx_data;
2214 work_handlers[CPL_TX_DMA_ACK] = tx_ack;
2215 work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
2216 work_handlers[CPL_ABORT_RPL] = abort_rpl;
2217 work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
2218 work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
2219 work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
2220 work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
2221 work_handlers[CPL_PEER_CLOSE] = peer_close;
2222 work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
2223 work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
2224 work_handlers[CPL_RDMA_TERMINATE] = terminate;
2225 work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
2226 return 0;
2227}
2228
2229void __exit iwch_cm_term(void)
2230{
2231 flush_workqueue(workq);
2232 destroy_workqueue(workq);
2233}