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