blob: 6aa53cd6947811ebd6f6257a2a6efd102689225d [file] [log] [blame]
Steve Wisecfdda9d2010-04-21 15:30:06 -07001/*
2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
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#include <linux/inetdevice.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41
42#include <net/neighbour.h>
43#include <net/netevent.h>
44#include <net/route.h>
45
46#include "iw_cxgb4.h"
47
48static char *states[] = {
49 "idle",
50 "listen",
51 "connecting",
52 "mpa_wait_req",
53 "mpa_req_sent",
54 "mpa_req_rcvd",
55 "mpa_rep_sent",
56 "fpdu_mode",
57 "aborting",
58 "closing",
59 "moribund",
60 "dead",
61 NULL,
62};
63
Steve Wiseb52fe092011-03-11 22:30:01 +000064static int dack_mode = 1;
Steve Wiseba6d3922010-06-23 15:46:49 +000065module_param(dack_mode, int, 0644);
Steve Wiseb52fe092011-03-11 22:30:01 +000066MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
Steve Wiseba6d3922010-06-23 15:46:49 +000067
Roland Dreierbe4c9ba2010-05-05 14:45:40 -070068int c4iw_max_read_depth = 8;
69module_param(c4iw_max_read_depth, int, 0644);
70MODULE_PARM_DESC(c4iw_max_read_depth, "Per-connection max ORD/IRD (default=8)");
71
Steve Wisecfdda9d2010-04-21 15:30:06 -070072static int enable_tcp_timestamps;
73module_param(enable_tcp_timestamps, int, 0644);
74MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
75
76static int enable_tcp_sack;
77module_param(enable_tcp_sack, int, 0644);
78MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
79
80static int enable_tcp_window_scaling = 1;
81module_param(enable_tcp_window_scaling, int, 0644);
82MODULE_PARM_DESC(enable_tcp_window_scaling,
83 "Enable tcp window scaling (default=1)");
84
85int c4iw_debug;
86module_param(c4iw_debug, int, 0644);
87MODULE_PARM_DESC(c4iw_debug, "Enable debug logging (default=0)");
88
89static int peer2peer;
90module_param(peer2peer, int, 0644);
91MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
92
93static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
94module_param(p2p_type, int, 0644);
95MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
96 "1=RDMA_READ 0=RDMA_WRITE (default 1)");
97
98static int ep_timeout_secs = 60;
99module_param(ep_timeout_secs, int, 0644);
100MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
101 "in seconds (default=60)");
102
103static int mpa_rev = 1;
104module_param(mpa_rev, int, 0644);
105MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
106 "1 is spec compliant. (default=1)");
107
108static int markers_enabled;
109module_param(markers_enabled, int, 0644);
110MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
111
112static int crc_enabled = 1;
113module_param(crc_enabled, int, 0644);
114MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
115
116static int rcv_win = 256 * 1024;
117module_param(rcv_win, int, 0644);
118MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
119
Steve Wise98ae68b2010-09-10 11:15:41 -0500120static int snd_win = 128 * 1024;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700121module_param(snd_win, int, 0644);
Steve Wise98ae68b2010-09-10 11:15:41 -0500122MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
Steve Wisecfdda9d2010-04-21 15:30:06 -0700123
Steve Wisecfdda9d2010-04-21 15:30:06 -0700124static struct workqueue_struct *workq;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700125
126static struct sk_buff_head rxq;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700127
128static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
129static void ep_timeout(unsigned long arg);
130static void connect_reply_upcall(struct c4iw_ep *ep, int status);
131
Roland Dreierbe4c9ba2010-05-05 14:45:40 -0700132static LIST_HEAD(timeout_list);
133static spinlock_t timeout_lock;
134
Steve Wisecfdda9d2010-04-21 15:30:06 -0700135static void start_ep_timer(struct c4iw_ep *ep)
136{
137 PDBG("%s ep %p\n", __func__, ep);
138 if (timer_pending(&ep->timer)) {
139 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
140 del_timer_sync(&ep->timer);
141 } else
142 c4iw_get_ep(&ep->com);
143 ep->timer.expires = jiffies + ep_timeout_secs * HZ;
144 ep->timer.data = (unsigned long)ep;
145 ep->timer.function = ep_timeout;
146 add_timer(&ep->timer);
147}
148
149static void stop_ep_timer(struct c4iw_ep *ep)
150{
151 PDBG("%s ep %p\n", __func__, ep);
152 if (!timer_pending(&ep->timer)) {
153 printk(KERN_ERR "%s timer stopped when its not running! "
154 "ep %p state %u\n", __func__, ep, ep->com.state);
155 WARN_ON(1);
156 return;
157 }
158 del_timer_sync(&ep->timer);
159 c4iw_put_ep(&ep->com);
160}
161
162static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
163 struct l2t_entry *l2e)
164{
165 int error = 0;
166
167 if (c4iw_fatal_error(rdev)) {
168 kfree_skb(skb);
169 PDBG("%s - device in error state - dropping\n", __func__);
170 return -EIO;
171 }
172 error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
173 if (error < 0)
174 kfree_skb(skb);
Steve Wise74594862010-09-10 11:14:58 -0500175 return error < 0 ? error : 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700176}
177
178int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
179{
180 int error = 0;
181
182 if (c4iw_fatal_error(rdev)) {
183 kfree_skb(skb);
184 PDBG("%s - device in error state - dropping\n", __func__);
185 return -EIO;
186 }
187 error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
188 if (error < 0)
189 kfree_skb(skb);
Steve Wise74594862010-09-10 11:14:58 -0500190 return error < 0 ? error : 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700191}
192
193static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
194{
195 struct cpl_tid_release *req;
196
197 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
198 if (!skb)
199 return;
200 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
201 INIT_TP_WR(req, hwtid);
202 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
203 set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
204 c4iw_ofld_send(rdev, skb);
205 return;
206}
207
208static void set_emss(struct c4iw_ep *ep, u16 opt)
209{
210 ep->emss = ep->com.dev->rdev.lldi.mtus[GET_TCPOPT_MSS(opt)] - 40;
211 ep->mss = ep->emss;
212 if (GET_TCPOPT_TSTAMP(opt))
213 ep->emss -= 12;
214 if (ep->emss < 128)
215 ep->emss = 128;
216 PDBG("%s mss_idx %u mss %u emss=%u\n", __func__, GET_TCPOPT_MSS(opt),
217 ep->mss, ep->emss);
218}
219
220static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
221{
Steve Wisecfdda9d2010-04-21 15:30:06 -0700222 enum c4iw_ep_state state;
223
Steve Wise2f5b48c2010-09-10 11:15:36 -0500224 mutex_lock(&epc->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700225 state = epc->state;
Steve Wise2f5b48c2010-09-10 11:15:36 -0500226 mutex_unlock(&epc->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700227 return state;
228}
229
230static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
231{
232 epc->state = new;
233}
234
235static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
236{
Steve Wise2f5b48c2010-09-10 11:15:36 -0500237 mutex_lock(&epc->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700238 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
239 __state_set(epc, new);
Steve Wise2f5b48c2010-09-10 11:15:36 -0500240 mutex_unlock(&epc->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700241 return;
242}
243
244static void *alloc_ep(int size, gfp_t gfp)
245{
246 struct c4iw_ep_common *epc;
247
248 epc = kzalloc(size, gfp);
249 if (epc) {
250 kref_init(&epc->kref);
Steve Wise2f5b48c2010-09-10 11:15:36 -0500251 mutex_init(&epc->mutex);
Steve Wiseaadc4df2010-09-10 11:15:25 -0500252 c4iw_init_wr_wait(&epc->wr_wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700253 }
254 PDBG("%s alloc ep %p\n", __func__, epc);
255 return epc;
256}
257
258void _c4iw_free_ep(struct kref *kref)
259{
260 struct c4iw_ep *ep;
261
262 ep = container_of(kref, struct c4iw_ep, com.kref);
263 PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
264 if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
265 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
266 dst_release(ep->dst);
267 cxgb4_l2t_release(ep->l2t);
268 }
269 kfree(ep);
270}
271
272static void release_ep_resources(struct c4iw_ep *ep)
273{
274 set_bit(RELEASE_RESOURCES, &ep->com.flags);
275 c4iw_put_ep(&ep->com);
276}
277
Steve Wisecfdda9d2010-04-21 15:30:06 -0700278static int status2errno(int status)
279{
280 switch (status) {
281 case CPL_ERR_NONE:
282 return 0;
283 case CPL_ERR_CONN_RESET:
284 return -ECONNRESET;
285 case CPL_ERR_ARP_MISS:
286 return -EHOSTUNREACH;
287 case CPL_ERR_CONN_TIMEDOUT:
288 return -ETIMEDOUT;
289 case CPL_ERR_TCAM_FULL:
290 return -ENOMEM;
291 case CPL_ERR_CONN_EXIST:
292 return -EADDRINUSE;
293 default:
294 return -EIO;
295 }
296}
297
298/*
299 * Try and reuse skbs already allocated...
300 */
301static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
302{
303 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
304 skb_trim(skb, 0);
305 skb_get(skb);
306 skb_reset_transport_header(skb);
307 } else {
308 skb = alloc_skb(len, gfp);
309 }
310 return skb;
311}
312
313static struct rtable *find_route(struct c4iw_dev *dev, __be32 local_ip,
314 __be32 peer_ip, __be16 local_port,
315 __be16 peer_port, u8 tos)
316{
317 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -0700318 struct flowi4 fl4;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700319
David S. Miller31e4543d2011-05-03 20:25:42 -0700320 rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
David S. Miller78fbfd82011-03-12 00:00:52 -0500321 peer_port, local_port, IPPROTO_TCP,
322 tos, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800323 if (IS_ERR(rt))
Steve Wisecfdda9d2010-04-21 15:30:06 -0700324 return NULL;
325 return rt;
326}
327
328static void arp_failure_discard(void *handle, struct sk_buff *skb)
329{
330 PDBG("%s c4iw_dev %p\n", __func__, handle);
331 kfree_skb(skb);
332}
333
334/*
335 * Handle an ARP failure for an active open.
336 */
337static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
338{
339 printk(KERN_ERR MOD "ARP failure duing connect\n");
340 kfree_skb(skb);
341}
342
343/*
344 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
345 * and send it along.
346 */
347static void abort_arp_failure(void *handle, struct sk_buff *skb)
348{
349 struct c4iw_rdev *rdev = handle;
350 struct cpl_abort_req *req = cplhdr(skb);
351
352 PDBG("%s rdev %p\n", __func__, rdev);
353 req->cmd = CPL_ABORT_NO_RST;
354 c4iw_ofld_send(rdev, skb);
355}
356
357static void send_flowc(struct c4iw_ep *ep, struct sk_buff *skb)
358{
359 unsigned int flowclen = 80;
360 struct fw_flowc_wr *flowc;
361 int i;
362
363 skb = get_skb(skb, flowclen, GFP_KERNEL);
364 flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);
365
366 flowc->op_to_nparams = cpu_to_be32(FW_WR_OP(FW_FLOWC_WR) |
367 FW_FLOWC_WR_NPARAMS(8));
368 flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16(DIV_ROUND_UP(flowclen,
369 16)) | FW_WR_FLOWID(ep->hwtid));
370
371 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
Steve Wise94788652011-01-21 17:00:34 +0000372 flowc->mnemval[0].val = cpu_to_be32(PCI_FUNC(ep->com.dev->rdev.lldi.pdev->devfn) << 8);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700373 flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
374 flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
375 flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
376 flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
377 flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
378 flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
379 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
380 flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
381 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
382 flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
383 flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
384 flowc->mnemval[6].val = cpu_to_be32(snd_win);
385 flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
386 flowc->mnemval[7].val = cpu_to_be32(ep->emss);
387 /* Pad WR to 16 byte boundary */
388 flowc->mnemval[8].mnemonic = 0;
389 flowc->mnemval[8].val = 0;
390 for (i = 0; i < 9; i++) {
391 flowc->mnemval[i].r4[0] = 0;
392 flowc->mnemval[i].r4[1] = 0;
393 flowc->mnemval[i].r4[2] = 0;
394 }
395
396 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
397 c4iw_ofld_send(&ep->com.dev->rdev, skb);
398}
399
400static int send_halfclose(struct c4iw_ep *ep, gfp_t gfp)
401{
402 struct cpl_close_con_req *req;
403 struct sk_buff *skb;
404 int wrlen = roundup(sizeof *req, 16);
405
406 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
407 skb = get_skb(NULL, wrlen, gfp);
408 if (!skb) {
409 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
410 return -ENOMEM;
411 }
412 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
413 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
414 req = (struct cpl_close_con_req *) skb_put(skb, wrlen);
415 memset(req, 0, wrlen);
416 INIT_TP_WR(req, ep->hwtid);
417 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ,
418 ep->hwtid));
419 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
420}
421
422static int send_abort(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
423{
424 struct cpl_abort_req *req;
425 int wrlen = roundup(sizeof *req, 16);
426
427 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
428 skb = get_skb(skb, wrlen, gfp);
429 if (!skb) {
430 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
431 __func__);
432 return -ENOMEM;
433 }
434 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
435 t4_set_arp_err_handler(skb, &ep->com.dev->rdev, abort_arp_failure);
436 req = (struct cpl_abort_req *) skb_put(skb, wrlen);
437 memset(req, 0, wrlen);
438 INIT_TP_WR(req, ep->hwtid);
439 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
440 req->cmd = CPL_ABORT_SEND_RST;
441 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
442}
443
444static int send_connect(struct c4iw_ep *ep)
445{
446 struct cpl_act_open_req *req;
447 struct sk_buff *skb;
448 u64 opt0;
449 u32 opt2;
450 unsigned int mtu_idx;
451 int wscale;
452 int wrlen = roundup(sizeof *req, 16);
453
454 PDBG("%s ep %p atid %u\n", __func__, ep, ep->atid);
455
456 skb = get_skb(NULL, wrlen, GFP_KERNEL);
457 if (!skb) {
458 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
459 __func__);
460 return -ENOMEM;
461 }
Steve Wised4f1a5c2010-07-23 19:12:32 +0000462 set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700463
464 cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
465 wscale = compute_wscale(rcv_win);
466 opt0 = KEEP_ALIVE(1) |
Steve Wiseba6d3922010-06-23 15:46:49 +0000467 DELACK(1) |
Steve Wisecfdda9d2010-04-21 15:30:06 -0700468 WND_SCALE(wscale) |
469 MSS_IDX(mtu_idx) |
470 L2T_IDX(ep->l2t->idx) |
471 TX_CHAN(ep->tx_chan) |
472 SMAC_SEL(ep->smac_idx) |
473 DSCP(ep->tos) |
Steve Wiseb48f3b92011-03-11 22:30:21 +0000474 ULP_MODE(ULP_MODE_TCPDDP) |
Steve Wisecfdda9d2010-04-21 15:30:06 -0700475 RCV_BUFSIZ(rcv_win>>10);
476 opt2 = RX_CHANNEL(0) |
477 RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
478 if (enable_tcp_timestamps)
479 opt2 |= TSTAMPS_EN(1);
480 if (enable_tcp_sack)
481 opt2 |= SACK_EN(1);
482 if (wscale && enable_tcp_window_scaling)
483 opt2 |= WND_SCALE_EN(1);
484 t4_set_arp_err_handler(skb, NULL, act_open_req_arp_failure);
485
486 req = (struct cpl_act_open_req *) skb_put(skb, wrlen);
487 INIT_TP_WR(req, 0);
488 OPCODE_TID(req) = cpu_to_be32(
489 MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ((ep->rss_qid<<14)|ep->atid)));
490 req->local_port = ep->com.local_addr.sin_port;
491 req->peer_port = ep->com.remote_addr.sin_port;
492 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
493 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
494 req->opt0 = cpu_to_be64(opt0);
495 req->params = 0;
496 req->opt2 = cpu_to_be32(opt2);
497 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
498}
499
500static void send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb)
501{
502 int mpalen, wrlen;
503 struct fw_ofld_tx_data_wr *req;
504 struct mpa_message *mpa;
505
506 PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
507
508 BUG_ON(skb_cloned(skb));
509
510 mpalen = sizeof(*mpa) + ep->plen;
511 wrlen = roundup(mpalen + sizeof *req, 16);
512 skb = get_skb(skb, wrlen, GFP_KERNEL);
513 if (!skb) {
514 connect_reply_upcall(ep, -ENOMEM);
515 return;
516 }
517 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
518
519 req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
520 memset(req, 0, wrlen);
521 req->op_to_immdlen = cpu_to_be32(
522 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
523 FW_WR_COMPL(1) |
524 FW_WR_IMMDLEN(mpalen));
525 req->flowid_len16 = cpu_to_be32(
526 FW_WR_FLOWID(ep->hwtid) |
527 FW_WR_LEN16(wrlen >> 4));
528 req->plen = cpu_to_be32(mpalen);
529 req->tunnel_to_proxy = cpu_to_be32(
530 FW_OFLD_TX_DATA_WR_FLUSH(1) |
531 FW_OFLD_TX_DATA_WR_SHOVE(1));
532
533 mpa = (struct mpa_message *)(req + 1);
534 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
535 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
536 (markers_enabled ? MPA_MARKERS : 0);
537 mpa->private_data_size = htons(ep->plen);
538 mpa->revision = mpa_rev;
539
540 if (ep->plen)
541 memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
542
543 /*
544 * Reference the mpa skb. This ensures the data area
545 * will remain in memory until the hw acks the tx.
546 * Function fw4_ack() will deref it.
547 */
548 skb_get(skb);
549 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
550 BUG_ON(ep->mpa_skb);
551 ep->mpa_skb = skb;
552 c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
553 start_ep_timer(ep);
554 state_set(&ep->com, MPA_REQ_SENT);
555 ep->mpa_attr.initiator = 1;
556 return;
557}
558
559static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
560{
561 int mpalen, wrlen;
562 struct fw_ofld_tx_data_wr *req;
563 struct mpa_message *mpa;
564 struct sk_buff *skb;
565
566 PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
567
568 mpalen = sizeof(*mpa) + plen;
569 wrlen = roundup(mpalen + sizeof *req, 16);
570
571 skb = get_skb(NULL, wrlen, GFP_KERNEL);
572 if (!skb) {
573 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
574 return -ENOMEM;
575 }
576 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
577
578 req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
579 memset(req, 0, wrlen);
580 req->op_to_immdlen = cpu_to_be32(
581 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
582 FW_WR_COMPL(1) |
583 FW_WR_IMMDLEN(mpalen));
584 req->flowid_len16 = cpu_to_be32(
585 FW_WR_FLOWID(ep->hwtid) |
586 FW_WR_LEN16(wrlen >> 4));
587 req->plen = cpu_to_be32(mpalen);
588 req->tunnel_to_proxy = cpu_to_be32(
589 FW_OFLD_TX_DATA_WR_FLUSH(1) |
590 FW_OFLD_TX_DATA_WR_SHOVE(1));
591
592 mpa = (struct mpa_message *)(req + 1);
593 memset(mpa, 0, sizeof(*mpa));
594 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
595 mpa->flags = MPA_REJECT;
596 mpa->revision = mpa_rev;
597 mpa->private_data_size = htons(plen);
598 if (plen)
599 memcpy(mpa->private_data, pdata, plen);
600
601 /*
602 * Reference the mpa skb again. This ensures the data area
603 * will remain in memory until the hw acks the tx.
604 * Function fw4_ack() will deref it.
605 */
606 skb_get(skb);
607 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
608 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
609 BUG_ON(ep->mpa_skb);
610 ep->mpa_skb = skb;
611 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
612}
613
614static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
615{
616 int mpalen, wrlen;
617 struct fw_ofld_tx_data_wr *req;
618 struct mpa_message *mpa;
619 struct sk_buff *skb;
620
621 PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
622
623 mpalen = sizeof(*mpa) + plen;
624 wrlen = roundup(mpalen + sizeof *req, 16);
625
626 skb = get_skb(NULL, wrlen, GFP_KERNEL);
627 if (!skb) {
628 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
629 return -ENOMEM;
630 }
631 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
632
633 req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
634 memset(req, 0, wrlen);
635 req->op_to_immdlen = cpu_to_be32(
636 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
637 FW_WR_COMPL(1) |
638 FW_WR_IMMDLEN(mpalen));
639 req->flowid_len16 = cpu_to_be32(
640 FW_WR_FLOWID(ep->hwtid) |
641 FW_WR_LEN16(wrlen >> 4));
642 req->plen = cpu_to_be32(mpalen);
643 req->tunnel_to_proxy = cpu_to_be32(
644 FW_OFLD_TX_DATA_WR_FLUSH(1) |
645 FW_OFLD_TX_DATA_WR_SHOVE(1));
646
647 mpa = (struct mpa_message *)(req + 1);
648 memset(mpa, 0, sizeof(*mpa));
649 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
650 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
651 (markers_enabled ? MPA_MARKERS : 0);
652 mpa->revision = mpa_rev;
653 mpa->private_data_size = htons(plen);
654 if (plen)
655 memcpy(mpa->private_data, pdata, plen);
656
657 /*
658 * Reference the mpa skb. This ensures the data area
659 * will remain in memory until the hw acks the tx.
660 * Function fw4_ack() will deref it.
661 */
662 skb_get(skb);
663 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
664 ep->mpa_skb = skb;
665 state_set(&ep->com, MPA_REP_SENT);
666 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
667}
668
669static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
670{
671 struct c4iw_ep *ep;
672 struct cpl_act_establish *req = cplhdr(skb);
673 unsigned int tid = GET_TID(req);
674 unsigned int atid = GET_TID_TID(ntohl(req->tos_atid));
675 struct tid_info *t = dev->rdev.lldi.tids;
676
677 ep = lookup_atid(t, atid);
678
679 PDBG("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
680 be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
681
682 dst_confirm(ep->dst);
683
684 /* setup the hwtid for this connection */
685 ep->hwtid = tid;
686 cxgb4_insert_tid(t, ep, tid);
687
688 ep->snd_seq = be32_to_cpu(req->snd_isn);
689 ep->rcv_seq = be32_to_cpu(req->rcv_isn);
690
691 set_emss(ep, ntohs(req->tcp_opt));
692
693 /* dealloc the atid */
694 cxgb4_free_atid(t, atid);
695
696 /* start MPA negotiation */
697 send_flowc(ep, NULL);
698 send_mpa_req(ep, skb);
699
700 return 0;
701}
702
703static void close_complete_upcall(struct c4iw_ep *ep)
704{
705 struct iw_cm_event event;
706
707 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
708 memset(&event, 0, sizeof(event));
709 event.event = IW_CM_EVENT_CLOSE;
710 if (ep->com.cm_id) {
711 PDBG("close complete delivered ep %p cm_id %p tid %u\n",
712 ep, ep->com.cm_id, ep->hwtid);
713 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
714 ep->com.cm_id->rem_ref(ep->com.cm_id);
715 ep->com.cm_id = NULL;
716 ep->com.qp = NULL;
717 }
718}
719
720static int abort_connection(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
721{
722 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
723 close_complete_upcall(ep);
724 state_set(&ep->com, ABORTING);
725 return send_abort(ep, skb, gfp);
726}
727
728static void peer_close_upcall(struct c4iw_ep *ep)
729{
730 struct iw_cm_event event;
731
732 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
733 memset(&event, 0, sizeof(event));
734 event.event = IW_CM_EVENT_DISCONNECT;
735 if (ep->com.cm_id) {
736 PDBG("peer close delivered ep %p cm_id %p tid %u\n",
737 ep, ep->com.cm_id, ep->hwtid);
738 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
739 }
740}
741
742static void peer_abort_upcall(struct c4iw_ep *ep)
743{
744 struct iw_cm_event event;
745
746 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
747 memset(&event, 0, sizeof(event));
748 event.event = IW_CM_EVENT_CLOSE;
749 event.status = -ECONNRESET;
750 if (ep->com.cm_id) {
751 PDBG("abort delivered ep %p cm_id %p tid %u\n", ep,
752 ep->com.cm_id, ep->hwtid);
753 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
754 ep->com.cm_id->rem_ref(ep->com.cm_id);
755 ep->com.cm_id = NULL;
756 ep->com.qp = NULL;
757 }
758}
759
760static void connect_reply_upcall(struct c4iw_ep *ep, int status)
761{
762 struct iw_cm_event event;
763
764 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid, status);
765 memset(&event, 0, sizeof(event));
766 event.event = IW_CM_EVENT_CONNECT_REPLY;
767 event.status = status;
768 event.local_addr = ep->com.local_addr;
769 event.remote_addr = ep->com.remote_addr;
770
771 if ((status == 0) || (status == -ECONNREFUSED)) {
772 event.private_data_len = ep->plen;
773 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
774 }
Roland Dreier85963e42010-07-19 13:13:09 -0700775
776 PDBG("%s ep %p tid %u status %d\n", __func__, ep,
777 ep->hwtid, status);
778 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
779
Steve Wisecfdda9d2010-04-21 15:30:06 -0700780 if (status < 0) {
781 ep->com.cm_id->rem_ref(ep->com.cm_id);
782 ep->com.cm_id = NULL;
783 ep->com.qp = NULL;
784 }
785}
786
787static void connect_request_upcall(struct c4iw_ep *ep)
788{
789 struct iw_cm_event event;
790
791 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
792 memset(&event, 0, sizeof(event));
793 event.event = IW_CM_EVENT_CONNECT_REQUEST;
794 event.local_addr = ep->com.local_addr;
795 event.remote_addr = ep->com.remote_addr;
796 event.private_data_len = ep->plen;
797 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
798 event.provider_data = ep;
799 if (state_read(&ep->parent_ep->com) != DEAD) {
800 c4iw_get_ep(&ep->com);
801 ep->parent_ep->com.cm_id->event_handler(
802 ep->parent_ep->com.cm_id,
803 &event);
804 }
805 c4iw_put_ep(&ep->parent_ep->com);
806 ep->parent_ep = NULL;
807}
808
809static void established_upcall(struct c4iw_ep *ep)
810{
811 struct iw_cm_event event;
812
813 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
814 memset(&event, 0, sizeof(event));
815 event.event = IW_CM_EVENT_ESTABLISHED;
816 if (ep->com.cm_id) {
817 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
818 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
819 }
820}
821
822static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
823{
824 struct cpl_rx_data_ack *req;
825 struct sk_buff *skb;
826 int wrlen = roundup(sizeof *req, 16);
827
828 PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
829 skb = get_skb(NULL, wrlen, GFP_KERNEL);
830 if (!skb) {
831 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
832 return 0;
833 }
834
835 req = (struct cpl_rx_data_ack *) skb_put(skb, wrlen);
836 memset(req, 0, wrlen);
837 INIT_TP_WR(req, ep->hwtid);
838 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
839 ep->hwtid));
Steve Wiseba6d3922010-06-23 15:46:49 +0000840 req->credit_dack = cpu_to_be32(credits | RX_FORCE_ACK(1) |
841 F_RX_DACK_CHANGE |
842 V_RX_DACK_MODE(dack_mode));
Steve Wised4f1a5c2010-07-23 19:12:32 +0000843 set_wr_txq(skb, CPL_PRIORITY_ACK, ep->ctrlq_idx);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700844 c4iw_ofld_send(&ep->com.dev->rdev, skb);
845 return credits;
846}
847
848static void process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
849{
850 struct mpa_message *mpa;
851 u16 plen;
852 struct c4iw_qp_attributes attrs;
853 enum c4iw_qp_attr_mask mask;
854 int err;
855
856 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
857
858 /*
859 * Stop mpa timer. If it expired, then the state has
860 * changed and we bail since ep_timeout already aborted
861 * the connection.
862 */
863 stop_ep_timer(ep);
864 if (state_read(&ep->com) != MPA_REQ_SENT)
865 return;
866
867 /*
868 * If we get more than the supported amount of private data
869 * then we must fail this connection.
870 */
871 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
872 err = -EINVAL;
873 goto err;
874 }
875
876 /*
877 * copy the new data into our accumulation buffer.
878 */
879 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
880 skb->len);
881 ep->mpa_pkt_len += skb->len;
882
883 /*
884 * if we don't even have the mpa message, then bail.
885 */
886 if (ep->mpa_pkt_len < sizeof(*mpa))
887 return;
888 mpa = (struct mpa_message *) ep->mpa_pkt;
889
890 /* Validate MPA header. */
891 if (mpa->revision != mpa_rev) {
892 err = -EPROTO;
893 goto err;
894 }
895 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
896 err = -EPROTO;
897 goto err;
898 }
899
900 plen = ntohs(mpa->private_data_size);
901
902 /*
903 * Fail if there's too much private data.
904 */
905 if (plen > MPA_MAX_PRIVATE_DATA) {
906 err = -EPROTO;
907 goto err;
908 }
909
910 /*
911 * If plen does not account for pkt size
912 */
913 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
914 err = -EPROTO;
915 goto err;
916 }
917
918 ep->plen = (u8) plen;
919
920 /*
921 * If we don't have all the pdata yet, then bail.
922 * We'll continue process when more data arrives.
923 */
924 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
925 return;
926
927 if (mpa->flags & MPA_REJECT) {
928 err = -ECONNREFUSED;
929 goto err;
930 }
931
932 /*
933 * If we get here we have accumulated the entire mpa
934 * start reply message including private data. And
935 * the MPA header is valid.
936 */
937 state_set(&ep->com, FPDU_MODE);
938 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
939 ep->mpa_attr.recv_marker_enabled = markers_enabled;
940 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
941 ep->mpa_attr.version = mpa_rev;
942 ep->mpa_attr.p2p_type = peer2peer ? p2p_type :
943 FW_RI_INIT_P2PTYPE_DISABLED;
944 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
945 "xmit_marker_enabled=%d, version=%d\n", __func__,
946 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
947 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
948
949 attrs.mpa_attr = ep->mpa_attr;
950 attrs.max_ird = ep->ird;
951 attrs.max_ord = ep->ord;
952 attrs.llp_stream_handle = ep;
953 attrs.next_state = C4IW_QP_STATE_RTS;
954
955 mask = C4IW_QP_ATTR_NEXT_STATE |
956 C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
957 C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
958
959 /* bind QP and TID with INIT_WR */
960 err = c4iw_modify_qp(ep->com.qp->rhp,
961 ep->com.qp, mask, &attrs, 1);
962 if (err)
963 goto err;
964 goto out;
965err:
Steve Wiseb21ef162010-06-10 19:02:55 +0000966 state_set(&ep->com, ABORTING);
967 send_abort(ep, skb, GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700968out:
969 connect_reply_upcall(ep, err);
970 return;
971}
972
973static void process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
974{
975 struct mpa_message *mpa;
976 u16 plen;
977
978 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
979
980 if (state_read(&ep->com) != MPA_REQ_WAIT)
981 return;
982
983 /*
984 * If we get more than the supported amount of private data
985 * then we must fail this connection.
986 */
987 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
988 stop_ep_timer(ep);
989 abort_connection(ep, skb, GFP_KERNEL);
990 return;
991 }
992
993 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
994
995 /*
996 * Copy the new data into our accumulation buffer.
997 */
998 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
999 skb->len);
1000 ep->mpa_pkt_len += skb->len;
1001
1002 /*
1003 * If we don't even have the mpa message, then bail.
1004 * We'll continue process when more data arrives.
1005 */
1006 if (ep->mpa_pkt_len < sizeof(*mpa))
1007 return;
1008
1009 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1010 stop_ep_timer(ep);
1011 mpa = (struct mpa_message *) ep->mpa_pkt;
1012
1013 /*
1014 * Validate MPA Header.
1015 */
1016 if (mpa->revision != mpa_rev) {
1017 abort_connection(ep, skb, GFP_KERNEL);
1018 return;
1019 }
1020
1021 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1022 abort_connection(ep, skb, GFP_KERNEL);
1023 return;
1024 }
1025
1026 plen = ntohs(mpa->private_data_size);
1027
1028 /*
1029 * Fail if there's too much private data.
1030 */
1031 if (plen > MPA_MAX_PRIVATE_DATA) {
1032 abort_connection(ep, skb, GFP_KERNEL);
1033 return;
1034 }
1035
1036 /*
1037 * If plen does not account for pkt size
1038 */
1039 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1040 abort_connection(ep, skb, GFP_KERNEL);
1041 return;
1042 }
1043 ep->plen = (u8) plen;
1044
1045 /*
1046 * If we don't have all the pdata yet, then bail.
1047 */
1048 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1049 return;
1050
1051 /*
1052 * If we get here we have accumulated the entire mpa
1053 * start reply message including private data.
1054 */
1055 ep->mpa_attr.initiator = 0;
1056 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1057 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1058 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1059 ep->mpa_attr.version = mpa_rev;
1060 ep->mpa_attr.p2p_type = peer2peer ? p2p_type :
1061 FW_RI_INIT_P2PTYPE_DISABLED;
1062 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1063 "xmit_marker_enabled=%d, version=%d p2p_type=%d\n", __func__,
1064 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1065 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1066 ep->mpa_attr.p2p_type);
1067
1068 state_set(&ep->com, MPA_REQ_RCVD);
1069
1070 /* drive upcall */
1071 connect_request_upcall(ep);
1072 return;
1073}
1074
1075static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
1076{
1077 struct c4iw_ep *ep;
1078 struct cpl_rx_data *hdr = cplhdr(skb);
1079 unsigned int dlen = ntohs(hdr->len);
1080 unsigned int tid = GET_TID(hdr);
1081 struct tid_info *t = dev->rdev.lldi.tids;
1082
1083 ep = lookup_tid(t, tid);
1084 PDBG("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
1085 skb_pull(skb, sizeof(*hdr));
1086 skb_trim(skb, dlen);
1087
1088 ep->rcv_seq += dlen;
1089 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1090
1091 /* update RX credits */
1092 update_rx_credits(ep, dlen);
1093
1094 switch (state_read(&ep->com)) {
1095 case MPA_REQ_SENT:
1096 process_mpa_reply(ep, skb);
1097 break;
1098 case MPA_REQ_WAIT:
1099 process_mpa_request(ep, skb);
1100 break;
1101 case MPA_REP_SENT:
1102 break;
1103 default:
1104 printk(KERN_ERR MOD "%s Unexpected streaming data."
1105 " ep %p state %d tid %u\n",
1106 __func__, ep, state_read(&ep->com), ep->hwtid);
1107
1108 /*
1109 * The ep will timeout and inform the ULP of the failure.
1110 * See ep_timeout().
1111 */
1112 break;
1113 }
1114 return 0;
1115}
1116
1117static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1118{
1119 struct c4iw_ep *ep;
1120 struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001121 int release = 0;
1122 unsigned int tid = GET_TID(rpl);
1123 struct tid_info *t = dev->rdev.lldi.tids;
1124
1125 ep = lookup_tid(t, tid);
1126 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1127 BUG_ON(!ep);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001128 mutex_lock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001129 switch (ep->com.state) {
1130 case ABORTING:
1131 __state_set(&ep->com, DEAD);
1132 release = 1;
1133 break;
1134 default:
1135 printk(KERN_ERR "%s ep %p state %d\n",
1136 __func__, ep, ep->com.state);
1137 break;
1138 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05001139 mutex_unlock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001140
1141 if (release)
1142 release_ep_resources(ep);
1143 return 0;
1144}
1145
1146/*
1147 * Return whether a failed active open has allocated a TID
1148 */
1149static inline int act_open_has_tid(int status)
1150{
1151 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1152 status != CPL_ERR_ARP_MISS;
1153}
1154
1155static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1156{
1157 struct c4iw_ep *ep;
1158 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1159 unsigned int atid = GET_TID_TID(GET_AOPEN_ATID(
1160 ntohl(rpl->atid_status)));
1161 struct tid_info *t = dev->rdev.lldi.tids;
1162 int status = GET_AOPEN_STATUS(ntohl(rpl->atid_status));
1163
1164 ep = lookup_atid(t, atid);
1165
1166 PDBG("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
1167 status, status2errno(status));
1168
1169 if (status == CPL_ERR_RTX_NEG_ADVICE) {
1170 printk(KERN_WARNING MOD "Connection problems for atid %u\n",
1171 atid);
1172 return 0;
1173 }
1174
1175 connect_reply_upcall(ep, status2errno(status));
1176 state_set(&ep->com, DEAD);
1177
1178 if (status && act_open_has_tid(status))
1179 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));
1180
1181 cxgb4_free_atid(t, atid);
1182 dst_release(ep->dst);
1183 cxgb4_l2t_release(ep->l2t);
1184 c4iw_put_ep(&ep->com);
1185
1186 return 0;
1187}
1188
1189static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1190{
1191 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1192 struct tid_info *t = dev->rdev.lldi.tids;
1193 unsigned int stid = GET_TID(rpl);
1194 struct c4iw_listen_ep *ep = lookup_stid(t, stid);
1195
1196 if (!ep) {
1197 printk(KERN_ERR MOD "stid %d lookup failure!\n", stid);
1198 return 0;
1199 }
1200 PDBG("%s ep %p status %d error %d\n", __func__, ep,
1201 rpl->status, status2errno(rpl->status));
Steve Wiseaadc4df2010-09-10 11:15:25 -05001202 ep->com.wr_wait.ret = status2errno(rpl->status);
1203 ep->com.wr_wait.done = 1;
1204 wake_up(&ep->com.wr_wait.wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001205
1206 return 0;
1207}
1208
1209static int listen_stop(struct c4iw_listen_ep *ep)
1210{
1211 struct sk_buff *skb;
1212 struct cpl_close_listsvr_req *req;
1213
1214 PDBG("%s ep %p\n", __func__, ep);
1215 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1216 if (!skb) {
1217 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1218 return -ENOMEM;
1219 }
1220 req = (struct cpl_close_listsvr_req *) skb_put(skb, sizeof(*req));
1221 INIT_TP_WR(req, 0);
1222 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ,
1223 ep->stid));
1224 req->reply_ctrl = cpu_to_be16(
1225 QUEUENO(ep->com.dev->rdev.lldi.rxq_ids[0]));
1226 set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
1227 return c4iw_ofld_send(&ep->com.dev->rdev, skb);
1228}
1229
1230static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1231{
1232 struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1233 struct tid_info *t = dev->rdev.lldi.tids;
1234 unsigned int stid = GET_TID(rpl);
1235 struct c4iw_listen_ep *ep = lookup_stid(t, stid);
1236
1237 PDBG("%s ep %p\n", __func__, ep);
Steve Wiseaadc4df2010-09-10 11:15:25 -05001238 ep->com.wr_wait.ret = status2errno(rpl->status);
1239 ep->com.wr_wait.done = 1;
1240 wake_up(&ep->com.wr_wait.wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001241 return 0;
1242}
1243
1244static void accept_cr(struct c4iw_ep *ep, __be32 peer_ip, struct sk_buff *skb,
1245 struct cpl_pass_accept_req *req)
1246{
1247 struct cpl_pass_accept_rpl *rpl;
1248 unsigned int mtu_idx;
1249 u64 opt0;
1250 u32 opt2;
1251 int wscale;
1252
1253 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1254 BUG_ON(skb_cloned(skb));
1255 skb_trim(skb, sizeof(*rpl));
1256 skb_get(skb);
1257 cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
1258 wscale = compute_wscale(rcv_win);
1259 opt0 = KEEP_ALIVE(1) |
Steve Wiseba6d3922010-06-23 15:46:49 +00001260 DELACK(1) |
Steve Wisecfdda9d2010-04-21 15:30:06 -07001261 WND_SCALE(wscale) |
1262 MSS_IDX(mtu_idx) |
1263 L2T_IDX(ep->l2t->idx) |
1264 TX_CHAN(ep->tx_chan) |
1265 SMAC_SEL(ep->smac_idx) |
1266 DSCP(ep->tos) |
Steve Wiseb48f3b92011-03-11 22:30:21 +00001267 ULP_MODE(ULP_MODE_TCPDDP) |
Steve Wisecfdda9d2010-04-21 15:30:06 -07001268 RCV_BUFSIZ(rcv_win>>10);
1269 opt2 = RX_CHANNEL(0) |
1270 RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
1271
1272 if (enable_tcp_timestamps && req->tcpopt.tstamp)
1273 opt2 |= TSTAMPS_EN(1);
1274 if (enable_tcp_sack && req->tcpopt.sack)
1275 opt2 |= SACK_EN(1);
1276 if (wscale && enable_tcp_window_scaling)
1277 opt2 |= WND_SCALE_EN(1);
1278
1279 rpl = cplhdr(skb);
1280 INIT_TP_WR(rpl, ep->hwtid);
1281 OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1282 ep->hwtid));
1283 rpl->opt0 = cpu_to_be64(opt0);
1284 rpl->opt2 = cpu_to_be32(opt2);
Steve Wised4f1a5c2010-07-23 19:12:32 +00001285 set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001286 c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1287
1288 return;
1289}
1290
1291static void reject_cr(struct c4iw_dev *dev, u32 hwtid, __be32 peer_ip,
1292 struct sk_buff *skb)
1293{
1294 PDBG("%s c4iw_dev %p tid %u peer_ip %x\n", __func__, dev, hwtid,
1295 peer_ip);
1296 BUG_ON(skb_cloned(skb));
1297 skb_trim(skb, sizeof(struct cpl_tid_release));
1298 skb_get(skb);
1299 release_tid(&dev->rdev, hwtid, skb);
1300 return;
1301}
1302
1303static void get_4tuple(struct cpl_pass_accept_req *req,
1304 __be32 *local_ip, __be32 *peer_ip,
1305 __be16 *local_port, __be16 *peer_port)
1306{
1307 int eth_len = G_ETH_HDR_LEN(be32_to_cpu(req->hdr_len));
1308 int ip_len = G_IP_HDR_LEN(be32_to_cpu(req->hdr_len));
1309 struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
1310 struct tcphdr *tcp = (struct tcphdr *)
1311 ((u8 *)(req + 1) + eth_len + ip_len);
1312
1313 PDBG("%s saddr 0x%x daddr 0x%x sport %u dport %u\n", __func__,
1314 ntohl(ip->saddr), ntohl(ip->daddr), ntohs(tcp->source),
1315 ntohs(tcp->dest));
1316
1317 *peer_ip = ip->saddr;
1318 *local_ip = ip->daddr;
1319 *peer_port = tcp->source;
1320 *local_port = tcp->dest;
1321
1322 return;
1323}
1324
1325static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
1326{
1327 struct c4iw_ep *child_ep, *parent_ep;
1328 struct cpl_pass_accept_req *req = cplhdr(skb);
1329 unsigned int stid = GET_POPEN_TID(ntohl(req->tos_stid));
1330 struct tid_info *t = dev->rdev.lldi.tids;
1331 unsigned int hwtid = GET_TID(req);
1332 struct dst_entry *dst;
1333 struct l2t_entry *l2t;
1334 struct rtable *rt;
1335 __be32 local_ip, peer_ip;
1336 __be16 local_port, peer_port;
1337 struct net_device *pdev;
1338 u32 tx_chan, smac_idx;
1339 u16 rss_qid;
1340 u32 mtu;
1341 int step;
Steve Wised4f1a5c2010-07-23 19:12:32 +00001342 int txq_idx, ctrlq_idx;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001343
1344 parent_ep = lookup_stid(t, stid);
1345 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1346
1347 get_4tuple(req, &local_ip, &peer_ip, &local_port, &peer_port);
1348
1349 if (state_read(&parent_ep->com) != LISTEN) {
1350 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1351 __func__);
1352 goto reject;
1353 }
1354
1355 /* Find output route */
1356 rt = find_route(dev, local_ip, peer_ip, local_port, peer_port,
1357 GET_POPEN_TOS(ntohl(req->tos_stid)));
1358 if (!rt) {
1359 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1360 __func__);
1361 goto reject;
1362 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001363 dst = &rt->dst;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001364 if (dst->neighbour->dev->flags & IFF_LOOPBACK) {
1365 pdev = ip_dev_find(&init_net, peer_ip);
1366 BUG_ON(!pdev);
1367 l2t = cxgb4_l2t_get(dev->rdev.lldi.l2t, dst->neighbour,
1368 pdev, 0);
1369 mtu = pdev->mtu;
1370 tx_chan = cxgb4_port_chan(pdev);
Steve Wise2c5934b2010-06-23 15:46:44 +00001371 smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001372 step = dev->rdev.lldi.ntxq / dev->rdev.lldi.nchan;
1373 txq_idx = cxgb4_port_idx(pdev) * step;
Steve Wised4f1a5c2010-07-23 19:12:32 +00001374 ctrlq_idx = cxgb4_port_idx(pdev);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001375 step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
1376 rss_qid = dev->rdev.lldi.rxq_ids[cxgb4_port_idx(pdev) * step];
1377 dev_put(pdev);
1378 } else {
1379 l2t = cxgb4_l2t_get(dev->rdev.lldi.l2t, dst->neighbour,
1380 dst->neighbour->dev, 0);
1381 mtu = dst_mtu(dst);
1382 tx_chan = cxgb4_port_chan(dst->neighbour->dev);
Steve Wise2c5934b2010-06-23 15:46:44 +00001383 smac_idx = (cxgb4_port_viid(dst->neighbour->dev) & 0x7F) << 1;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001384 step = dev->rdev.lldi.ntxq / dev->rdev.lldi.nchan;
1385 txq_idx = cxgb4_port_idx(dst->neighbour->dev) * step;
Steve Wised4f1a5c2010-07-23 19:12:32 +00001386 ctrlq_idx = cxgb4_port_idx(dst->neighbour->dev);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001387 step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
1388 rss_qid = dev->rdev.lldi.rxq_ids[
1389 cxgb4_port_idx(dst->neighbour->dev) * step];
1390 }
1391 if (!l2t) {
1392 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1393 __func__);
1394 dst_release(dst);
1395 goto reject;
1396 }
1397
1398 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1399 if (!child_ep) {
1400 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1401 __func__);
1402 cxgb4_l2t_release(l2t);
1403 dst_release(dst);
1404 goto reject;
1405 }
1406 state_set(&child_ep->com, CONNECTING);
1407 child_ep->com.dev = dev;
1408 child_ep->com.cm_id = NULL;
1409 child_ep->com.local_addr.sin_family = PF_INET;
1410 child_ep->com.local_addr.sin_port = local_port;
1411 child_ep->com.local_addr.sin_addr.s_addr = local_ip;
1412 child_ep->com.remote_addr.sin_family = PF_INET;
1413 child_ep->com.remote_addr.sin_port = peer_port;
1414 child_ep->com.remote_addr.sin_addr.s_addr = peer_ip;
1415 c4iw_get_ep(&parent_ep->com);
1416 child_ep->parent_ep = parent_ep;
1417 child_ep->tos = GET_POPEN_TOS(ntohl(req->tos_stid));
1418 child_ep->l2t = l2t;
1419 child_ep->dst = dst;
1420 child_ep->hwtid = hwtid;
1421 child_ep->tx_chan = tx_chan;
1422 child_ep->smac_idx = smac_idx;
1423 child_ep->rss_qid = rss_qid;
1424 child_ep->mtu = mtu;
1425 child_ep->txq_idx = txq_idx;
Steve Wised4f1a5c2010-07-23 19:12:32 +00001426 child_ep->ctrlq_idx = ctrlq_idx;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001427
1428 PDBG("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
1429 tx_chan, smac_idx, rss_qid);
1430
1431 init_timer(&child_ep->timer);
1432 cxgb4_insert_tid(t, child_ep, hwtid);
1433 accept_cr(child_ep, peer_ip, skb, req);
1434 goto out;
1435reject:
1436 reject_cr(dev, hwtid, peer_ip, skb);
1437out:
1438 return 0;
1439}
1440
1441static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
1442{
1443 struct c4iw_ep *ep;
1444 struct cpl_pass_establish *req = cplhdr(skb);
1445 struct tid_info *t = dev->rdev.lldi.tids;
1446 unsigned int tid = GET_TID(req);
1447
1448 ep = lookup_tid(t, tid);
1449 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1450 ep->snd_seq = be32_to_cpu(req->snd_isn);
1451 ep->rcv_seq = be32_to_cpu(req->rcv_isn);
1452
1453 set_emss(ep, ntohs(req->tcp_opt));
1454
1455 dst_confirm(ep->dst);
1456 state_set(&ep->com, MPA_REQ_WAIT);
1457 start_ep_timer(ep);
1458 send_flowc(ep, skb);
1459
1460 return 0;
1461}
1462
1463static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
1464{
1465 struct cpl_peer_close *hdr = cplhdr(skb);
1466 struct c4iw_ep *ep;
1467 struct c4iw_qp_attributes attrs;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001468 int disconnect = 1;
1469 int release = 0;
1470 int closing = 0;
1471 struct tid_info *t = dev->rdev.lldi.tids;
1472 unsigned int tid = GET_TID(hdr);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001473
1474 ep = lookup_tid(t, tid);
1475 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1476 dst_confirm(ep->dst);
1477
Steve Wise2f5b48c2010-09-10 11:15:36 -05001478 mutex_lock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001479 switch (ep->com.state) {
1480 case MPA_REQ_WAIT:
1481 __state_set(&ep->com, CLOSING);
1482 break;
1483 case MPA_REQ_SENT:
1484 __state_set(&ep->com, CLOSING);
1485 connect_reply_upcall(ep, -ECONNRESET);
1486 break;
1487 case MPA_REQ_RCVD:
1488
1489 /*
1490 * We're gonna mark this puppy DEAD, but keep
1491 * the reference on it until the ULP accepts or
1492 * rejects the CR. Also wake up anyone waiting
1493 * in rdma connection migration (see c4iw_accept_cr()).
1494 */
1495 __state_set(&ep->com, CLOSING);
Steve Wiseaadc4df2010-09-10 11:15:25 -05001496 ep->com.wr_wait.done = 1;
1497 ep->com.wr_wait.ret = -ECONNRESET;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001498 PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
Steve Wiseaadc4df2010-09-10 11:15:25 -05001499 wake_up(&ep->com.wr_wait.wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001500 break;
1501 case MPA_REP_SENT:
1502 __state_set(&ep->com, CLOSING);
Steve Wiseaadc4df2010-09-10 11:15:25 -05001503 ep->com.wr_wait.done = 1;
1504 ep->com.wr_wait.ret = -ECONNRESET;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001505 PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
Steve Wiseaadc4df2010-09-10 11:15:25 -05001506 wake_up(&ep->com.wr_wait.wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001507 break;
1508 case FPDU_MODE:
Steve Wiseca5a2202010-07-23 19:12:37 +00001509 start_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001510 __state_set(&ep->com, CLOSING);
1511 closing = 1;
1512 peer_close_upcall(ep);
1513 break;
1514 case ABORTING:
1515 disconnect = 0;
1516 break;
1517 case CLOSING:
1518 __state_set(&ep->com, MORIBUND);
1519 disconnect = 0;
1520 break;
1521 case MORIBUND:
Steve Wiseca5a2202010-07-23 19:12:37 +00001522 stop_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001523 if (ep->com.cm_id && ep->com.qp) {
1524 attrs.next_state = C4IW_QP_STATE_IDLE;
1525 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1526 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1527 }
1528 close_complete_upcall(ep);
1529 __state_set(&ep->com, DEAD);
1530 release = 1;
1531 disconnect = 0;
1532 break;
1533 case DEAD:
1534 disconnect = 0;
1535 break;
1536 default:
1537 BUG_ON(1);
1538 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05001539 mutex_unlock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001540 if (closing) {
1541 attrs.next_state = C4IW_QP_STATE_CLOSING;
1542 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1543 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1544 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001545 if (disconnect)
1546 c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1547 if (release)
1548 release_ep_resources(ep);
1549 return 0;
1550}
1551
1552/*
1553 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1554 */
1555static int is_neg_adv_abort(unsigned int status)
1556{
1557 return status == CPL_ERR_RTX_NEG_ADVICE ||
1558 status == CPL_ERR_PERSIST_NEG_ADVICE;
1559}
1560
1561static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
1562{
1563 struct cpl_abort_req_rss *req = cplhdr(skb);
1564 struct c4iw_ep *ep;
1565 struct cpl_abort_rpl *rpl;
1566 struct sk_buff *rpl_skb;
1567 struct c4iw_qp_attributes attrs;
1568 int ret;
1569 int release = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001570 struct tid_info *t = dev->rdev.lldi.tids;
1571 unsigned int tid = GET_TID(req);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001572
1573 ep = lookup_tid(t, tid);
1574 if (is_neg_adv_abort(req->status)) {
1575 PDBG("%s neg_adv_abort ep %p tid %u\n", __func__, ep,
1576 ep->hwtid);
1577 return 0;
1578 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001579 PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
1580 ep->com.state);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001581
1582 /*
1583 * Wake up any threads in rdma_init() or rdma_fini().
1584 */
1585 ep->com.wr_wait.done = 1;
1586 ep->com.wr_wait.ret = -ECONNRESET;
1587 wake_up(&ep->com.wr_wait.wait);
1588
1589 mutex_lock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001590 switch (ep->com.state) {
1591 case CONNECTING:
1592 break;
1593 case MPA_REQ_WAIT:
Steve Wiseca5a2202010-07-23 19:12:37 +00001594 stop_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001595 break;
1596 case MPA_REQ_SENT:
Steve Wiseca5a2202010-07-23 19:12:37 +00001597 stop_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001598 connect_reply_upcall(ep, -ECONNRESET);
1599 break;
1600 case MPA_REP_SENT:
Steve Wisecfdda9d2010-04-21 15:30:06 -07001601 break;
1602 case MPA_REQ_RCVD:
Steve Wisecfdda9d2010-04-21 15:30:06 -07001603 break;
1604 case MORIBUND:
1605 case CLOSING:
Steve Wiseca5a2202010-07-23 19:12:37 +00001606 stop_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001607 /*FALLTHROUGH*/
1608 case FPDU_MODE:
1609 if (ep->com.cm_id && ep->com.qp) {
1610 attrs.next_state = C4IW_QP_STATE_ERROR;
1611 ret = c4iw_modify_qp(ep->com.qp->rhp,
1612 ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
1613 &attrs, 1);
1614 if (ret)
1615 printk(KERN_ERR MOD
1616 "%s - qp <- error failed!\n",
1617 __func__);
1618 }
1619 peer_abort_upcall(ep);
1620 break;
1621 case ABORTING:
1622 break;
1623 case DEAD:
1624 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001625 mutex_unlock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001626 return 0;
1627 default:
1628 BUG_ON(1);
1629 break;
1630 }
1631 dst_confirm(ep->dst);
1632 if (ep->com.state != ABORTING) {
1633 __state_set(&ep->com, DEAD);
1634 release = 1;
1635 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05001636 mutex_unlock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001637
1638 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1639 if (!rpl_skb) {
1640 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
1641 __func__);
1642 release = 1;
1643 goto out;
1644 }
1645 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1646 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
1647 INIT_TP_WR(rpl, ep->hwtid);
1648 OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
1649 rpl->cmd = CPL_ABORT_NO_RST;
1650 c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
1651out:
Steve Wisecfdda9d2010-04-21 15:30:06 -07001652 if (release)
1653 release_ep_resources(ep);
1654 return 0;
1655}
1656
1657static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1658{
1659 struct c4iw_ep *ep;
1660 struct c4iw_qp_attributes attrs;
1661 struct cpl_close_con_rpl *rpl = cplhdr(skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001662 int release = 0;
1663 struct tid_info *t = dev->rdev.lldi.tids;
1664 unsigned int tid = GET_TID(rpl);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001665
1666 ep = lookup_tid(t, tid);
1667
1668 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1669 BUG_ON(!ep);
1670
1671 /* The cm_id may be null if we failed to connect */
Steve Wise2f5b48c2010-09-10 11:15:36 -05001672 mutex_lock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001673 switch (ep->com.state) {
1674 case CLOSING:
1675 __state_set(&ep->com, MORIBUND);
1676 break;
1677 case MORIBUND:
Steve Wiseca5a2202010-07-23 19:12:37 +00001678 stop_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001679 if ((ep->com.cm_id) && (ep->com.qp)) {
1680 attrs.next_state = C4IW_QP_STATE_IDLE;
1681 c4iw_modify_qp(ep->com.qp->rhp,
1682 ep->com.qp,
1683 C4IW_QP_ATTR_NEXT_STATE,
1684 &attrs, 1);
1685 }
1686 close_complete_upcall(ep);
1687 __state_set(&ep->com, DEAD);
1688 release = 1;
1689 break;
1690 case ABORTING:
1691 case DEAD:
1692 break;
1693 default:
1694 BUG_ON(1);
1695 break;
1696 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05001697 mutex_unlock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001698 if (release)
1699 release_ep_resources(ep);
1700 return 0;
1701}
1702
1703static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
1704{
Steve Wise0e42c1f2010-09-10 11:15:09 -05001705 struct cpl_rdma_terminate *rpl = cplhdr(skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001706 struct tid_info *t = dev->rdev.lldi.tids;
Steve Wise0e42c1f2010-09-10 11:15:09 -05001707 unsigned int tid = GET_TID(rpl);
1708 struct c4iw_ep *ep;
1709 struct c4iw_qp_attributes attrs;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001710
1711 ep = lookup_tid(t, tid);
Steve Wise0e42c1f2010-09-10 11:15:09 -05001712 BUG_ON(!ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001713
Steve Wise0e42c1f2010-09-10 11:15:09 -05001714 if (ep->com.qp) {
1715 printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
1716 ep->com.qp->wq.sq.qid);
1717 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1718 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1719 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1720 } else
1721 printk(KERN_WARNING MOD "TERM received tid %u no qp\n", tid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001722
Steve Wisecfdda9d2010-04-21 15:30:06 -07001723 return 0;
1724}
1725
1726/*
1727 * Upcall from the adapter indicating data has been transmitted.
1728 * For us its just the single MPA request or reply. We can now free
1729 * the skb holding the mpa message.
1730 */
1731static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
1732{
1733 struct c4iw_ep *ep;
1734 struct cpl_fw4_ack *hdr = cplhdr(skb);
1735 u8 credits = hdr->credits;
1736 unsigned int tid = GET_TID(hdr);
1737 struct tid_info *t = dev->rdev.lldi.tids;
1738
1739
1740 ep = lookup_tid(t, tid);
1741 PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
1742 if (credits == 0) {
Joe Perchesaa1ad262010-10-25 19:44:22 -07001743 PDBG("%s 0 credit ack ep %p tid %u state %u\n",
1744 __func__, ep, ep->hwtid, state_read(&ep->com));
Steve Wisecfdda9d2010-04-21 15:30:06 -07001745 return 0;
1746 }
1747
1748 dst_confirm(ep->dst);
1749 if (ep->mpa_skb) {
1750 PDBG("%s last streaming msg ack ep %p tid %u state %u "
1751 "initiator %u freeing skb\n", __func__, ep, ep->hwtid,
1752 state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
1753 kfree_skb(ep->mpa_skb);
1754 ep->mpa_skb = NULL;
1755 }
1756 return 0;
1757}
1758
Steve Wisecfdda9d2010-04-21 15:30:06 -07001759int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1760{
1761 int err;
1762 struct c4iw_ep *ep = to_ep(cm_id);
1763 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1764
1765 if (state_read(&ep->com) == DEAD) {
1766 c4iw_put_ep(&ep->com);
1767 return -ECONNRESET;
1768 }
1769 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1770 if (mpa_rev == 0)
1771 abort_connection(ep, NULL, GFP_KERNEL);
1772 else {
1773 err = send_mpa_reject(ep, pdata, pdata_len);
1774 err = c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1775 }
1776 c4iw_put_ep(&ep->com);
1777 return 0;
1778}
1779
1780int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1781{
1782 int err;
1783 struct c4iw_qp_attributes attrs;
1784 enum c4iw_qp_attr_mask mask;
1785 struct c4iw_ep *ep = to_ep(cm_id);
1786 struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
1787 struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
1788
1789 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1790 if (state_read(&ep->com) == DEAD) {
1791 err = -ECONNRESET;
1792 goto err;
1793 }
1794
1795 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1796 BUG_ON(!qp);
1797
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001798 if ((conn_param->ord > c4iw_max_read_depth) ||
1799 (conn_param->ird > c4iw_max_read_depth)) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001800 abort_connection(ep, NULL, GFP_KERNEL);
1801 err = -EINVAL;
1802 goto err;
1803 }
1804
1805 cm_id->add_ref(cm_id);
1806 ep->com.cm_id = cm_id;
1807 ep->com.qp = qp;
1808
1809 ep->ird = conn_param->ird;
1810 ep->ord = conn_param->ord;
1811
1812 if (peer2peer && ep->ird == 0)
1813 ep->ird = 1;
1814
1815 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
1816
1817 /* bind QP to EP and move to RTS */
1818 attrs.mpa_attr = ep->mpa_attr;
1819 attrs.max_ird = ep->ird;
1820 attrs.max_ord = ep->ord;
1821 attrs.llp_stream_handle = ep;
1822 attrs.next_state = C4IW_QP_STATE_RTS;
1823
1824 /* bind QP and TID with INIT_WR */
1825 mask = C4IW_QP_ATTR_NEXT_STATE |
1826 C4IW_QP_ATTR_LLP_STREAM_HANDLE |
1827 C4IW_QP_ATTR_MPA_ATTR |
1828 C4IW_QP_ATTR_MAX_IRD |
1829 C4IW_QP_ATTR_MAX_ORD;
1830
1831 err = c4iw_modify_qp(ep->com.qp->rhp,
1832 ep->com.qp, mask, &attrs, 1);
1833 if (err)
1834 goto err1;
1835 err = send_mpa_reply(ep, conn_param->private_data,
1836 conn_param->private_data_len);
1837 if (err)
1838 goto err1;
1839
1840 state_set(&ep->com, FPDU_MODE);
1841 established_upcall(ep);
1842 c4iw_put_ep(&ep->com);
1843 return 0;
1844err1:
1845 ep->com.cm_id = NULL;
1846 ep->com.qp = NULL;
1847 cm_id->rem_ref(cm_id);
1848err:
1849 c4iw_put_ep(&ep->com);
1850 return err;
1851}
1852
1853int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1854{
1855 int err = 0;
1856 struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
1857 struct c4iw_ep *ep;
1858 struct rtable *rt;
1859 struct net_device *pdev;
1860 int step;
1861
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001862 if ((conn_param->ord > c4iw_max_read_depth) ||
1863 (conn_param->ird > c4iw_max_read_depth)) {
1864 err = -EINVAL;
1865 goto out;
1866 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001867 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
1868 if (!ep) {
1869 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
1870 err = -ENOMEM;
1871 goto out;
1872 }
1873 init_timer(&ep->timer);
1874 ep->plen = conn_param->private_data_len;
1875 if (ep->plen)
1876 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
1877 conn_param->private_data, ep->plen);
1878 ep->ird = conn_param->ird;
1879 ep->ord = conn_param->ord;
1880
1881 if (peer2peer && ep->ord == 0)
1882 ep->ord = 1;
1883
1884 cm_id->add_ref(cm_id);
1885 ep->com.dev = dev;
1886 ep->com.cm_id = cm_id;
1887 ep->com.qp = get_qhp(dev, conn_param->qpn);
1888 BUG_ON(!ep->com.qp);
1889 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
1890 ep->com.qp, cm_id);
1891
1892 /*
1893 * Allocate an active TID to initiate a TCP connection.
1894 */
1895 ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
1896 if (ep->atid == -1) {
1897 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1898 err = -ENOMEM;
1899 goto fail2;
1900 }
1901
1902 PDBG("%s saddr 0x%x sport 0x%x raddr 0x%x rport 0x%x\n", __func__,
1903 ntohl(cm_id->local_addr.sin_addr.s_addr),
1904 ntohs(cm_id->local_addr.sin_port),
1905 ntohl(cm_id->remote_addr.sin_addr.s_addr),
1906 ntohs(cm_id->remote_addr.sin_port));
1907
1908 /* find a route */
1909 rt = find_route(dev,
1910 cm_id->local_addr.sin_addr.s_addr,
1911 cm_id->remote_addr.sin_addr.s_addr,
1912 cm_id->local_addr.sin_port,
1913 cm_id->remote_addr.sin_port, 0);
1914 if (!rt) {
1915 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1916 err = -EHOSTUNREACH;
1917 goto fail3;
1918 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001919 ep->dst = &rt->dst;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001920
1921 /* get a l2t entry */
1922 if (ep->dst->neighbour->dev->flags & IFF_LOOPBACK) {
1923 PDBG("%s LOOPBACK\n", __func__);
1924 pdev = ip_dev_find(&init_net,
1925 cm_id->remote_addr.sin_addr.s_addr);
1926 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
1927 ep->dst->neighbour,
1928 pdev, 0);
1929 ep->mtu = pdev->mtu;
1930 ep->tx_chan = cxgb4_port_chan(pdev);
Steve Wise2c5934b2010-06-23 15:46:44 +00001931 ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001932 step = ep->com.dev->rdev.lldi.ntxq /
1933 ep->com.dev->rdev.lldi.nchan;
1934 ep->txq_idx = cxgb4_port_idx(pdev) * step;
1935 step = ep->com.dev->rdev.lldi.nrxq /
1936 ep->com.dev->rdev.lldi.nchan;
Steve Wised4f1a5c2010-07-23 19:12:32 +00001937 ep->ctrlq_idx = cxgb4_port_idx(pdev);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001938 ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
1939 cxgb4_port_idx(pdev) * step];
1940 dev_put(pdev);
1941 } else {
1942 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
1943 ep->dst->neighbour,
1944 ep->dst->neighbour->dev, 0);
1945 ep->mtu = dst_mtu(ep->dst);
1946 ep->tx_chan = cxgb4_port_chan(ep->dst->neighbour->dev);
Steve Wise2c5934b2010-06-23 15:46:44 +00001947 ep->smac_idx = (cxgb4_port_viid(ep->dst->neighbour->dev) &
1948 0x7F) << 1;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001949 step = ep->com.dev->rdev.lldi.ntxq /
1950 ep->com.dev->rdev.lldi.nchan;
1951 ep->txq_idx = cxgb4_port_idx(ep->dst->neighbour->dev) * step;
Steve Wised4f1a5c2010-07-23 19:12:32 +00001952 ep->ctrlq_idx = cxgb4_port_idx(ep->dst->neighbour->dev);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001953 step = ep->com.dev->rdev.lldi.nrxq /
1954 ep->com.dev->rdev.lldi.nchan;
1955 ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
1956 cxgb4_port_idx(ep->dst->neighbour->dev) * step];
1957 }
1958 if (!ep->l2t) {
1959 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1960 err = -ENOMEM;
1961 goto fail4;
1962 }
1963
1964 PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
1965 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
1966 ep->l2t->idx);
1967
1968 state_set(&ep->com, CONNECTING);
1969 ep->tos = 0;
1970 ep->com.local_addr = cm_id->local_addr;
1971 ep->com.remote_addr = cm_id->remote_addr;
1972
1973 /* send connect request to rnic */
1974 err = send_connect(ep);
1975 if (!err)
1976 goto out;
1977
1978 cxgb4_l2t_release(ep->l2t);
1979fail4:
1980 dst_release(ep->dst);
1981fail3:
1982 cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
1983fail2:
1984 cm_id->rem_ref(cm_id);
1985 c4iw_put_ep(&ep->com);
1986out:
1987 return err;
1988}
1989
1990int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
1991{
1992 int err = 0;
1993 struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
1994 struct c4iw_listen_ep *ep;
1995
1996
1997 might_sleep();
1998
1999 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2000 if (!ep) {
2001 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2002 err = -ENOMEM;
2003 goto fail1;
2004 }
2005 PDBG("%s ep %p\n", __func__, ep);
2006 cm_id->add_ref(cm_id);
2007 ep->com.cm_id = cm_id;
2008 ep->com.dev = dev;
2009 ep->backlog = backlog;
2010 ep->com.local_addr = cm_id->local_addr;
2011
2012 /*
2013 * Allocate a server TID.
2014 */
2015 ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids, PF_INET, ep);
2016 if (ep->stid == -1) {
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002017 printk(KERN_ERR MOD "%s - cannot alloc stid.\n", __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002018 err = -ENOMEM;
2019 goto fail2;
2020 }
2021
2022 state_set(&ep->com, LISTEN);
Steve Wiseaadc4df2010-09-10 11:15:25 -05002023 c4iw_init_wr_wait(&ep->com.wr_wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002024 err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0], ep->stid,
2025 ep->com.local_addr.sin_addr.s_addr,
2026 ep->com.local_addr.sin_port,
2027 ep->com.dev->rdev.lldi.rxq_ids[0]);
2028 if (err)
2029 goto fail3;
2030
2031 /* wait for pass_open_rpl */
Steve Wiseaadc4df2010-09-10 11:15:25 -05002032 err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait, 0, 0,
2033 __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002034 if (!err) {
2035 cm_id->provider_data = ep;
2036 goto out;
2037 }
2038fail3:
2039 cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
2040fail2:
2041 cm_id->rem_ref(cm_id);
2042 c4iw_put_ep(&ep->com);
2043fail1:
2044out:
2045 return err;
2046}
2047
2048int c4iw_destroy_listen(struct iw_cm_id *cm_id)
2049{
2050 int err;
2051 struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
2052
2053 PDBG("%s ep %p\n", __func__, ep);
2054
2055 might_sleep();
2056 state_set(&ep->com, DEAD);
Steve Wiseaadc4df2010-09-10 11:15:25 -05002057 c4iw_init_wr_wait(&ep->com.wr_wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002058 err = listen_stop(ep);
2059 if (err)
2060 goto done;
Steve Wiseaadc4df2010-09-10 11:15:25 -05002061 err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait, 0, 0,
2062 __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002063 cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
2064done:
Steve Wisecfdda9d2010-04-21 15:30:06 -07002065 cm_id->rem_ref(cm_id);
2066 c4iw_put_ep(&ep->com);
2067 return err;
2068}
2069
2070int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2071{
2072 int ret = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002073 int close = 0;
2074 int fatal = 0;
2075 struct c4iw_rdev *rdev;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002076
Steve Wise2f5b48c2010-09-10 11:15:36 -05002077 mutex_lock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002078
2079 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2080 states[ep->com.state], abrupt);
2081
2082 rdev = &ep->com.dev->rdev;
2083 if (c4iw_fatal_error(rdev)) {
2084 fatal = 1;
2085 close_complete_upcall(ep);
2086 ep->com.state = DEAD;
2087 }
2088 switch (ep->com.state) {
2089 case MPA_REQ_WAIT:
2090 case MPA_REQ_SENT:
2091 case MPA_REQ_RCVD:
2092 case MPA_REP_SENT:
2093 case FPDU_MODE:
2094 close = 1;
2095 if (abrupt)
2096 ep->com.state = ABORTING;
2097 else {
2098 ep->com.state = CLOSING;
Steve Wiseca5a2202010-07-23 19:12:37 +00002099 start_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002100 }
2101 set_bit(CLOSE_SENT, &ep->com.flags);
2102 break;
2103 case CLOSING:
2104 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2105 close = 1;
2106 if (abrupt) {
Steve Wiseca5a2202010-07-23 19:12:37 +00002107 stop_ep_timer(ep);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002108 ep->com.state = ABORTING;
2109 } else
2110 ep->com.state = MORIBUND;
2111 }
2112 break;
2113 case MORIBUND:
2114 case ABORTING:
2115 case DEAD:
2116 PDBG("%s ignoring disconnect ep %p state %u\n",
2117 __func__, ep, ep->com.state);
2118 break;
2119 default:
2120 BUG();
2121 break;
2122 }
2123
Steve Wise2f5b48c2010-09-10 11:15:36 -05002124 mutex_unlock(&ep->com.mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002125 if (close) {
2126 if (abrupt)
2127 ret = abort_connection(ep, NULL, gfp);
2128 else
2129 ret = send_halfclose(ep, gfp);
2130 if (ret)
2131 fatal = 1;
2132 }
2133 if (fatal)
2134 release_ep_resources(ep);
2135 return ret;
2136}
2137
Steve Wise2f5b48c2010-09-10 11:15:36 -05002138static int async_event(struct c4iw_dev *dev, struct sk_buff *skb)
2139{
2140 struct cpl_fw6_msg *rpl = cplhdr(skb);
2141 c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
2142 return 0;
2143}
2144
Steve Wisecfdda9d2010-04-21 15:30:06 -07002145/*
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002146 * These are the real handlers that are called from a
2147 * work queue.
2148 */
2149static c4iw_handler_func work_handlers[NUM_CPL_CMDS] = {
2150 [CPL_ACT_ESTABLISH] = act_establish,
2151 [CPL_ACT_OPEN_RPL] = act_open_rpl,
2152 [CPL_RX_DATA] = rx_data,
2153 [CPL_ABORT_RPL_RSS] = abort_rpl,
2154 [CPL_ABORT_RPL] = abort_rpl,
2155 [CPL_PASS_OPEN_RPL] = pass_open_rpl,
2156 [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
2157 [CPL_PASS_ACCEPT_REQ] = pass_accept_req,
2158 [CPL_PASS_ESTABLISH] = pass_establish,
2159 [CPL_PEER_CLOSE] = peer_close,
2160 [CPL_ABORT_REQ_RSS] = peer_abort,
2161 [CPL_CLOSE_CON_RPL] = close_con_rpl,
2162 [CPL_RDMA_TERMINATE] = terminate,
Steve Wise2f5b48c2010-09-10 11:15:36 -05002163 [CPL_FW4_ACK] = fw4_ack,
2164 [CPL_FW6_MSG] = async_event
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002165};
2166
2167static void process_timeout(struct c4iw_ep *ep)
2168{
2169 struct c4iw_qp_attributes attrs;
2170 int abort = 1;
2171
Steve Wise2f5b48c2010-09-10 11:15:36 -05002172 mutex_lock(&ep->com.mutex);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002173 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
2174 ep->com.state);
2175 switch (ep->com.state) {
2176 case MPA_REQ_SENT:
2177 __state_set(&ep->com, ABORTING);
2178 connect_reply_upcall(ep, -ETIMEDOUT);
2179 break;
2180 case MPA_REQ_WAIT:
2181 __state_set(&ep->com, ABORTING);
2182 break;
2183 case CLOSING:
2184 case MORIBUND:
2185 if (ep->com.cm_id && ep->com.qp) {
2186 attrs.next_state = C4IW_QP_STATE_ERROR;
2187 c4iw_modify_qp(ep->com.qp->rhp,
2188 ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
2189 &attrs, 1);
2190 }
2191 __state_set(&ep->com, ABORTING);
2192 break;
2193 default:
2194 printk(KERN_ERR "%s unexpected state ep %p tid %u state %u\n",
2195 __func__, ep, ep->hwtid, ep->com.state);
2196 WARN_ON(1);
2197 abort = 0;
2198 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05002199 mutex_unlock(&ep->com.mutex);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002200 if (abort)
2201 abort_connection(ep, NULL, GFP_KERNEL);
2202 c4iw_put_ep(&ep->com);
2203}
2204
2205static void process_timedout_eps(void)
2206{
2207 struct c4iw_ep *ep;
2208
2209 spin_lock_irq(&timeout_lock);
2210 while (!list_empty(&timeout_list)) {
2211 struct list_head *tmp;
2212
2213 tmp = timeout_list.next;
2214 list_del(tmp);
2215 spin_unlock_irq(&timeout_lock);
2216 ep = list_entry(tmp, struct c4iw_ep, entry);
2217 process_timeout(ep);
2218 spin_lock_irq(&timeout_lock);
2219 }
2220 spin_unlock_irq(&timeout_lock);
2221}
2222
2223static void process_work(struct work_struct *work)
2224{
2225 struct sk_buff *skb = NULL;
2226 struct c4iw_dev *dev;
Dan Carpenterc1d73562010-05-31 14:00:53 +00002227 struct cpl_act_establish *rpl;
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002228 unsigned int opcode;
2229 int ret;
2230
2231 while ((skb = skb_dequeue(&rxq))) {
2232 rpl = cplhdr(skb);
2233 dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
2234 opcode = rpl->ot.opcode;
2235
2236 BUG_ON(!work_handlers[opcode]);
2237 ret = work_handlers[opcode](dev, skb);
2238 if (!ret)
2239 kfree_skb(skb);
2240 }
2241 process_timedout_eps();
2242}
2243
2244static DECLARE_WORK(skb_work, process_work);
2245
2246static void ep_timeout(unsigned long arg)
2247{
2248 struct c4iw_ep *ep = (struct c4iw_ep *)arg;
2249
2250 spin_lock(&timeout_lock);
2251 list_add_tail(&ep->entry, &timeout_list);
2252 spin_unlock(&timeout_lock);
2253 queue_work(workq, &skb_work);
2254}
2255
2256/*
Steve Wisecfdda9d2010-04-21 15:30:06 -07002257 * All the CM events are handled on a work queue to have a safe context.
2258 */
2259static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
2260{
2261
2262 /*
2263 * Save dev in the skb->cb area.
2264 */
2265 *((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
2266
2267 /*
2268 * Queue the skb and schedule the worker thread.
2269 */
2270 skb_queue_tail(&rxq, skb);
2271 queue_work(workq, &skb_work);
2272 return 0;
2273}
2274
2275static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2276{
2277 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2278
2279 if (rpl->status != CPL_ERR_NONE) {
2280 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2281 "for tid %u\n", rpl->status, GET_TID(rpl));
2282 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05002283 kfree_skb(skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002284 return 0;
2285}
2286
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002287static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
2288{
2289 struct cpl_fw6_msg *rpl = cplhdr(skb);
2290 struct c4iw_wr_wait *wr_waitp;
2291 int ret;
2292
2293 PDBG("%s type %u\n", __func__, rpl->type);
2294
2295 switch (rpl->type) {
2296 case 1:
2297 ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
Roland Dreierc8e081a2010-09-27 17:51:04 -07002298 wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002299 PDBG("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
2300 if (wr_waitp) {
Steve Wise2f5b48c2010-09-10 11:15:36 -05002301 if (ret)
2302 wr_waitp->ret = -ret;
2303 else
2304 wr_waitp->ret = 0;
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002305 wr_waitp->done = 1;
2306 wake_up(&wr_waitp->wait);
2307 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05002308 kfree_skb(skb);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002309 break;
2310 case 2:
Steve Wise2f5b48c2010-09-10 11:15:36 -05002311 sched(dev, skb);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002312 break;
2313 default:
2314 printk(KERN_ERR MOD "%s unexpected fw6 msg type %u\n", __func__,
2315 rpl->type);
Steve Wise2f5b48c2010-09-10 11:15:36 -05002316 kfree_skb(skb);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002317 break;
2318 }
2319 return 0;
2320}
2321
2322/*
2323 * Most upcalls from the T4 Core go to sched() to
2324 * schedule the processing on a work queue.
2325 */
2326c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
2327 [CPL_ACT_ESTABLISH] = sched,
2328 [CPL_ACT_OPEN_RPL] = sched,
2329 [CPL_RX_DATA] = sched,
2330 [CPL_ABORT_RPL_RSS] = sched,
2331 [CPL_ABORT_RPL] = sched,
2332 [CPL_PASS_OPEN_RPL] = sched,
2333 [CPL_CLOSE_LISTSRV_RPL] = sched,
2334 [CPL_PASS_ACCEPT_REQ] = sched,
2335 [CPL_PASS_ESTABLISH] = sched,
2336 [CPL_PEER_CLOSE] = sched,
2337 [CPL_CLOSE_CON_RPL] = sched,
2338 [CPL_ABORT_REQ_RSS] = sched,
2339 [CPL_RDMA_TERMINATE] = sched,
2340 [CPL_FW4_ACK] = sched,
2341 [CPL_SET_TCB_RPL] = set_tcb_rpl,
2342 [CPL_FW6_MSG] = fw6_msg
2343};
2344
Steve Wisecfdda9d2010-04-21 15:30:06 -07002345int __init c4iw_cm_init(void)
2346{
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002347 spin_lock_init(&timeout_lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002348 skb_queue_head_init(&rxq);
2349
2350 workq = create_singlethread_workqueue("iw_cxgb4");
2351 if (!workq)
2352 return -ENOMEM;
2353
Steve Wisecfdda9d2010-04-21 15:30:06 -07002354 return 0;
2355}
2356
2357void __exit c4iw_cm_term(void)
2358{
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002359 WARN_ON(!list_empty(&timeout_list));
Steve Wisecfdda9d2010-04-21 15:30:06 -07002360 flush_workqueue(workq);
2361 destroy_workqueue(workq);
2362}