blob: c1d5be4adf9c6cdfe7fa530c93821f22f091ba97 [file] [log] [blame]
Karen Xiec3673462008-12-09 14:15:32 -08001/*
2 * cxgb3i_offload.c: Chelsio S3xx iscsi offloaded tcp connection management
3 *
4 * Copyright (C) 2003-2008 Chelsio Communications. All rights reserved.
5 *
6 * This program is distributed in the hope that it will be useful, but WITHOUT
7 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8 * FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file included in this
9 * release for licensing terms and conditions.
10 *
11 * Written by: Dimitris Michailidis (dm@chelsio.com)
12 * Karen Xie (kxie@chelsio.com)
13 */
14
15#include <linux/if_vlan.h>
16#include <linux/version.h>
17
18#include "cxgb3_defs.h"
19#include "cxgb3_ctl_defs.h"
20#include "firmware_exports.h"
21#include "cxgb3i_offload.h"
22#include "cxgb3i_pdu.h"
23#include "cxgb3i_ddp.h"
24
25#ifdef __DEBUG_C3CN_CONN__
Karen Xie1648b112009-02-13 21:38:44 -080026#define c3cn_conn_debug cxgb3i_log_debug
Karen Xiec3673462008-12-09 14:15:32 -080027#else
28#define c3cn_conn_debug(fmt...)
29#endif
30
31#ifdef __DEBUG_C3CN_TX__
Karen Xie1648b112009-02-13 21:38:44 -080032#define c3cn_tx_debug cxgb3i_log_debug
Karen Xiec3673462008-12-09 14:15:32 -080033#else
34#define c3cn_tx_debug(fmt...)
35#endif
36
37#ifdef __DEBUG_C3CN_RX__
Karen Xie1648b112009-02-13 21:38:44 -080038#define c3cn_rx_debug cxgb3i_log_debug
Karen Xiec3673462008-12-09 14:15:32 -080039#else
40#define c3cn_rx_debug(fmt...)
41#endif
42
43/*
44 * module parameters releated to offloaded iscsi connection
45 */
46static int cxgb3_rcv_win = 256 * 1024;
47module_param(cxgb3_rcv_win, int, 0644);
48MODULE_PARM_DESC(cxgb3_rcv_win, "TCP receive window in bytes (default=256KB)");
49
Karen Xie1648b112009-02-13 21:38:44 -080050static int cxgb3_snd_win = 128 * 1024;
Karen Xiec3673462008-12-09 14:15:32 -080051module_param(cxgb3_snd_win, int, 0644);
Karen Xie1648b112009-02-13 21:38:44 -080052MODULE_PARM_DESC(cxgb3_snd_win, "TCP send window in bytes (default=128KB)");
Karen Xiec3673462008-12-09 14:15:32 -080053
54static int cxgb3_rx_credit_thres = 10 * 1024;
55module_param(cxgb3_rx_credit_thres, int, 0644);
56MODULE_PARM_DESC(rx_credit_thres,
57 "RX credits return threshold in bytes (default=10KB)");
58
59static unsigned int cxgb3_max_connect = 8 * 1024;
60module_param(cxgb3_max_connect, uint, 0644);
61MODULE_PARM_DESC(cxgb3_max_connect, "Max. # of connections (default=8092)");
62
63static unsigned int cxgb3_sport_base = 20000;
64module_param(cxgb3_sport_base, uint, 0644);
65MODULE_PARM_DESC(cxgb3_sport_base, "starting port number (default=20000)");
66
67/*
68 * cxgb3i tcp connection data(per adapter) list
69 */
70static LIST_HEAD(cdata_list);
71static DEFINE_RWLOCK(cdata_rwlock);
72
73static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion);
74static void c3cn_release_offload_resources(struct s3_conn *c3cn);
75
76/*
77 * iscsi source port management
78 *
79 * Find a free source port in the port allocation map. We use a very simple
80 * rotor scheme to look for the next free port.
81 *
82 * If a source port has been specified make sure that it doesn't collide with
83 * our normal source port allocation map. If it's outside the range of our
84 * allocation/deallocation scheme just let them use it.
85 *
86 * If the source port is outside our allocation range, the caller is
87 * responsible for keeping track of their port usage.
88 */
89static int c3cn_get_port(struct s3_conn *c3cn, struct cxgb3i_sdev_data *cdata)
90{
91 unsigned int start;
92 int idx;
93
94 if (!cdata)
95 goto error_out;
96
Karen Xie2a90030f2009-04-01 13:11:26 -050097 if (c3cn->saddr.sin_port) {
98 cxgb3i_log_error("connect, sin_port NON-ZERO %u.\n",
99 c3cn->saddr.sin_port);
100 return -EADDRINUSE;
Karen Xiec3673462008-12-09 14:15:32 -0800101 }
102
Karen Xie2a90030f2009-04-01 13:11:26 -0500103 spin_lock_bh(&cdata->lock);
104 start = idx = cdata->sport_next;
Karen Xiec3673462008-12-09 14:15:32 -0800105 do {
106 if (++idx >= cxgb3_max_connect)
107 idx = 0;
Karen Xie2a90030f2009-04-01 13:11:26 -0500108 if (!cdata->sport_conn[idx]) {
Karen Xiec3673462008-12-09 14:15:32 -0800109 c3cn->saddr.sin_port = htons(cxgb3_sport_base + idx);
Karen Xie2a90030f2009-04-01 13:11:26 -0500110 cdata->sport_next = idx;
111 cdata->sport_conn[idx] = c3cn;
112 spin_unlock_bh(&cdata->lock);
113
Karen Xiec3673462008-12-09 14:15:32 -0800114 c3cn_conn_debug("%s reserve port %u.\n",
115 cdata->cdev->name,
116 cxgb3_sport_base + idx);
117 return 0;
118 }
119 } while (idx != start);
Karen Xie2a90030f2009-04-01 13:11:26 -0500120 spin_unlock_bh(&cdata->lock);
Karen Xiec3673462008-12-09 14:15:32 -0800121
122error_out:
123 return -EADDRNOTAVAIL;
124}
125
126static void c3cn_put_port(struct s3_conn *c3cn)
127{
Karen Xie2a90030f2009-04-01 13:11:26 -0500128 if (!c3cn->cdev)
129 return;
Karen Xiec3673462008-12-09 14:15:32 -0800130
131 if (c3cn->saddr.sin_port) {
Karen Xie2a90030f2009-04-01 13:11:26 -0500132 struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(c3cn->cdev);
Karen Xiec3673462008-12-09 14:15:32 -0800133 int idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
134
135 c3cn->saddr.sin_port = 0;
136 if (idx < 0 || idx >= cxgb3_max_connect)
137 return;
Karen Xie2a90030f2009-04-01 13:11:26 -0500138 spin_lock_bh(&cdata->lock);
139 cdata->sport_conn[idx] = NULL;
140 spin_unlock_bh(&cdata->lock);
Karen Xiec3673462008-12-09 14:15:32 -0800141 c3cn_conn_debug("%s, release port %u.\n",
142 cdata->cdev->name, cxgb3_sport_base + idx);
143 }
144}
145
146static inline void c3cn_set_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
147{
148 __set_bit(flag, &c3cn->flags);
149 c3cn_conn_debug("c3cn 0x%p, set %d, s %u, f 0x%lx.\n",
150 c3cn, flag, c3cn->state, c3cn->flags);
151}
152
153static inline void c3cn_clear_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
154{
155 __clear_bit(flag, &c3cn->flags);
156 c3cn_conn_debug("c3cn 0x%p, clear %d, s %u, f 0x%lx.\n",
157 c3cn, flag, c3cn->state, c3cn->flags);
158}
159
160static inline int c3cn_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
161{
162 if (c3cn == NULL)
163 return 0;
164 return test_bit(flag, &c3cn->flags);
165}
166
167static void c3cn_set_state(struct s3_conn *c3cn, int state)
168{
169 c3cn_conn_debug("c3cn 0x%p state -> %u.\n", c3cn, state);
170 c3cn->state = state;
171}
172
173static inline void c3cn_hold(struct s3_conn *c3cn)
174{
175 atomic_inc(&c3cn->refcnt);
176}
177
178static inline void c3cn_put(struct s3_conn *c3cn)
179{
180 if (atomic_dec_and_test(&c3cn->refcnt)) {
181 c3cn_conn_debug("free c3cn 0x%p, s %u, f 0x%lx.\n",
182 c3cn, c3cn->state, c3cn->flags);
183 kfree(c3cn);
184 }
185}
186
187static void c3cn_closed(struct s3_conn *c3cn)
188{
189 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
190 c3cn, c3cn->state, c3cn->flags);
191
192 c3cn_put_port(c3cn);
193 c3cn_release_offload_resources(c3cn);
194 c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
195 cxgb3i_conn_closing(c3cn);
196}
197
198/*
199 * CPL (Chelsio Protocol Language) defines a message passing interface between
200 * the host driver and T3 asic.
201 * The section below implments CPLs that related to iscsi tcp connection
202 * open/close/abort and data send/receive.
203 */
204
205/*
206 * CPL connection active open request: host ->
207 */
208static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
209{
210 int i = 0;
211
212 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
213 ++i;
214 return i;
215}
216
217static unsigned int select_mss(struct s3_conn *c3cn, unsigned int pmtu)
218{
219 unsigned int idx;
220 struct dst_entry *dst = c3cn->dst_cache;
221 struct t3cdev *cdev = c3cn->cdev;
222 const struct t3c_data *td = T3C_DATA(cdev);
223 u16 advmss = dst_metric(dst, RTAX_ADVMSS);
224
225 if (advmss > pmtu - 40)
226 advmss = pmtu - 40;
227 if (advmss < td->mtus[0] - 40)
228 advmss = td->mtus[0] - 40;
229 idx = find_best_mtu(td, advmss + 40);
230 return idx;
231}
232
233static inline int compute_wscale(int win)
234{
235 int wscale = 0;
236 while (wscale < 14 && (65535<<wscale) < win)
237 wscale++;
238 return wscale;
239}
240
241static inline unsigned int calc_opt0h(struct s3_conn *c3cn)
242{
243 int wscale = compute_wscale(cxgb3_rcv_win);
244 return V_KEEP_ALIVE(1) |
245 F_TCAM_BYPASS |
246 V_WND_SCALE(wscale) |
247 V_MSS_IDX(c3cn->mss_idx);
248}
249
250static inline unsigned int calc_opt0l(struct s3_conn *c3cn)
251{
252 return V_ULP_MODE(ULP_MODE_ISCSI) |
253 V_RCV_BUFSIZ(cxgb3_rcv_win>>10);
254}
255
256static void make_act_open_req(struct s3_conn *c3cn, struct sk_buff *skb,
257 unsigned int atid, const struct l2t_entry *e)
258{
259 struct cpl_act_open_req *req;
260
261 c3cn_conn_debug("c3cn 0x%p, atid 0x%x.\n", c3cn, atid);
262
263 skb->priority = CPL_PRIORITY_SETUP;
264 req = (struct cpl_act_open_req *)__skb_put(skb, sizeof(*req));
265 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
266 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, atid));
267 req->local_port = c3cn->saddr.sin_port;
268 req->peer_port = c3cn->daddr.sin_port;
269 req->local_ip = c3cn->saddr.sin_addr.s_addr;
270 req->peer_ip = c3cn->daddr.sin_addr.s_addr;
271 req->opt0h = htonl(calc_opt0h(c3cn) | V_L2T_IDX(e->idx) |
272 V_TX_CHANNEL(e->smt_idx));
273 req->opt0l = htonl(calc_opt0l(c3cn));
274 req->params = 0;
275}
276
277static void fail_act_open(struct s3_conn *c3cn, int errno)
278{
279 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
280 c3cn, c3cn->state, c3cn->flags);
281 c3cn->err = errno;
282 c3cn_closed(c3cn);
283}
284
285static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
286{
287 struct s3_conn *c3cn = (struct s3_conn *)skb->sk;
288
289 c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
290
291 c3cn_hold(c3cn);
292 spin_lock_bh(&c3cn->lock);
293 if (c3cn->state == C3CN_STATE_CONNECTING)
294 fail_act_open(c3cn, EHOSTUNREACH);
295 spin_unlock_bh(&c3cn->lock);
296 c3cn_put(c3cn);
297 __kfree_skb(skb);
298}
299
300/*
301 * CPL connection close request: host ->
302 *
303 * Close a connection by sending a CPL_CLOSE_CON_REQ message and queue it to
304 * the write queue (i.e., after any unsent txt data).
305 */
306static void skb_entail(struct s3_conn *c3cn, struct sk_buff *skb,
307 int flags)
308{
Karen Xie1648b112009-02-13 21:38:44 -0800309 skb_tcp_seq(skb) = c3cn->write_seq;
310 skb_flags(skb) = flags;
Karen Xiec3673462008-12-09 14:15:32 -0800311 __skb_queue_tail(&c3cn->write_queue, skb);
312}
313
314static void send_close_req(struct s3_conn *c3cn)
315{
316 struct sk_buff *skb = c3cn->cpl_close;
317 struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
318 unsigned int tid = c3cn->tid;
319
320 c3cn_conn_debug("c3cn 0x%p, state 0x%x, flag 0x%lx.\n",
321 c3cn, c3cn->state, c3cn->flags);
322
323 c3cn->cpl_close = NULL;
324
325 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
326 req->wr.wr_lo = htonl(V_WR_TID(tid));
327 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
328 req->rsvd = htonl(c3cn->write_seq);
329
330 skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND);
331 if (c3cn->state != C3CN_STATE_CONNECTING)
332 c3cn_push_tx_frames(c3cn, 1);
333}
334
335/*
336 * CPL connection abort request: host ->
337 *
338 * Send an ABORT_REQ message. Makes sure we do not send multiple ABORT_REQs
339 * for the same connection and also that we do not try to send a message
340 * after the connection has closed.
341 */
342static void abort_arp_failure(struct t3cdev *cdev, struct sk_buff *skb)
343{
344 struct cpl_abort_req *req = cplhdr(skb);
345
346 c3cn_conn_debug("tdev 0x%p.\n", cdev);
347
348 req->cmd = CPL_ABORT_NO_RST;
349 cxgb3_ofld_send(cdev, skb);
350}
351
352static inline void c3cn_purge_write_queue(struct s3_conn *c3cn)
353{
354 struct sk_buff *skb;
355
356 while ((skb = __skb_dequeue(&c3cn->write_queue)))
357 __kfree_skb(skb);
358}
359
360static void send_abort_req(struct s3_conn *c3cn)
361{
362 struct sk_buff *skb = c3cn->cpl_abort_req;
363 struct cpl_abort_req *req;
364 unsigned int tid = c3cn->tid;
365
366 if (unlikely(c3cn->state == C3CN_STATE_ABORTING) || !skb ||
367 !c3cn->cdev)
368 return;
369
370 c3cn_set_state(c3cn, C3CN_STATE_ABORTING);
371
372 c3cn_conn_debug("c3cn 0x%p, flag ABORT_RPL + ABORT_SHUT.\n", c3cn);
373
374 c3cn_set_flag(c3cn, C3CN_ABORT_RPL_PENDING);
375
376 /* Purge the send queue so we don't send anything after an abort. */
377 c3cn_purge_write_queue(c3cn);
378
379 c3cn->cpl_abort_req = NULL;
380 req = (struct cpl_abort_req *)skb->head;
381
382 skb->priority = CPL_PRIORITY_DATA;
383 set_arp_failure_handler(skb, abort_arp_failure);
384
385 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
386 req->wr.wr_lo = htonl(V_WR_TID(tid));
387 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, tid));
388 req->rsvd0 = htonl(c3cn->snd_nxt);
389 req->rsvd1 = !c3cn_flag(c3cn, C3CN_TX_DATA_SENT);
390 req->cmd = CPL_ABORT_SEND_RST;
391
392 l2t_send(c3cn->cdev, skb, c3cn->l2t);
393}
394
395/*
396 * CPL connection abort reply: host ->
397 *
398 * Send an ABORT_RPL message in response of the ABORT_REQ received.
399 */
400static void send_abort_rpl(struct s3_conn *c3cn, int rst_status)
401{
402 struct sk_buff *skb = c3cn->cpl_abort_rpl;
403 struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
404
405 c3cn->cpl_abort_rpl = NULL;
406
407 skb->priority = CPL_PRIORITY_DATA;
408 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
409 rpl->wr.wr_lo = htonl(V_WR_TID(c3cn->tid));
410 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, c3cn->tid));
411 rpl->cmd = rst_status;
412
413 cxgb3_ofld_send(c3cn->cdev, skb);
414}
415
416/*
417 * CPL connection rx data ack: host ->
418 * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
419 * credits sent.
420 */
421static u32 send_rx_credits(struct s3_conn *c3cn, u32 credits, u32 dack)
422{
423 struct sk_buff *skb;
424 struct cpl_rx_data_ack *req;
425
426 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
427 if (!skb)
428 return 0;
429
430 req = (struct cpl_rx_data_ack *)__skb_put(skb, sizeof(*req));
431 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
432 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, c3cn->tid));
433 req->credit_dack = htonl(dack | V_RX_CREDITS(credits));
434 skb->priority = CPL_PRIORITY_ACK;
435 cxgb3_ofld_send(c3cn->cdev, skb);
436 return credits;
437}
438
439/*
440 * CPL connection tx data: host ->
441 *
442 * Send iscsi PDU via TX_DATA CPL message. Returns the number of
443 * credits sent.
444 * Each TX_DATA consumes work request credit (wrs), so we need to keep track of
445 * how many we've used so far and how many are pending (i.e., yet ack'ed by T3).
446 */
447
448/*
449 * For ULP connections HW may inserts digest bytes into the pdu. Those digest
450 * bytes are not sent by the host but are part of the TCP payload and therefore
451 * consume TCP sequence space.
452 */
453static const unsigned int cxgb3_ulp_extra_len[] = { 0, 4, 4, 8 };
454static inline unsigned int ulp_extra_len(const struct sk_buff *skb)
455{
456 return cxgb3_ulp_extra_len[skb_ulp_mode(skb) & 3];
457}
458
459static unsigned int wrlen __read_mostly;
460
461/*
462 * The number of WRs needed for an skb depends on the number of fragments
463 * in the skb and whether it has any payload in its main body. This maps the
464 * length of the gather list represented by an skb into the # of necessary WRs.
Karen Xie1648b112009-02-13 21:38:44 -0800465 * The extra two fragments are for iscsi bhs and payload padding.
Karen Xiec3673462008-12-09 14:15:32 -0800466 */
Karen Xie1648b112009-02-13 21:38:44 -0800467#define SKB_WR_LIST_SIZE (MAX_SKB_FRAGS + 2)
Karen Xiec3673462008-12-09 14:15:32 -0800468static unsigned int skb_wrs[SKB_WR_LIST_SIZE] __read_mostly;
469
470static void s3_init_wr_tab(unsigned int wr_len)
471{
472 int i;
473
474 if (skb_wrs[1]) /* already initialized */
475 return;
476
477 for (i = 1; i < SKB_WR_LIST_SIZE; i++) {
478 int sgl_len = (3 * i) / 2 + (i & 1);
479
480 sgl_len += 3;
481 skb_wrs[i] = (sgl_len <= wr_len
482 ? 1 : 1 + (sgl_len - 2) / (wr_len - 1));
483 }
484
485 wrlen = wr_len * 8;
486}
487
488static inline void reset_wr_list(struct s3_conn *c3cn)
489{
Karen Xie1648b112009-02-13 21:38:44 -0800490 c3cn->wr_pending_head = c3cn->wr_pending_tail = NULL;
Karen Xiec3673462008-12-09 14:15:32 -0800491}
492
493/*
494 * Add a WR to a connections's list of pending WRs. This is a singly-linked
495 * list of sk_buffs operating as a FIFO. The head is kept in wr_pending_head
496 * and the tail in wr_pending_tail.
497 */
498static inline void enqueue_wr(struct s3_conn *c3cn,
499 struct sk_buff *skb)
500{
Karen Xie1648b112009-02-13 21:38:44 -0800501 skb_tx_wr_next(skb) = NULL;
Karen Xiec3673462008-12-09 14:15:32 -0800502
503 /*
504 * We want to take an extra reference since both us and the driver
505 * need to free the packet before it's really freed. We know there's
506 * just one user currently so we use atomic_set rather than skb_get
507 * to avoid the atomic op.
508 */
509 atomic_set(&skb->users, 2);
510
511 if (!c3cn->wr_pending_head)
512 c3cn->wr_pending_head = skb;
513 else
Karen Xie1648b112009-02-13 21:38:44 -0800514 skb_tx_wr_next(c3cn->wr_pending_tail) = skb;
Karen Xiec3673462008-12-09 14:15:32 -0800515 c3cn->wr_pending_tail = skb;
516}
517
Karen Xie1648b112009-02-13 21:38:44 -0800518static int count_pending_wrs(struct s3_conn *c3cn)
519{
520 int n = 0;
521 const struct sk_buff *skb = c3cn->wr_pending_head;
522
523 while (skb) {
524 n += skb->csum;
525 skb = skb_tx_wr_next(skb);
526 }
527 return n;
528}
529
Karen Xiec3673462008-12-09 14:15:32 -0800530static inline struct sk_buff *peek_wr(const struct s3_conn *c3cn)
531{
532 return c3cn->wr_pending_head;
533}
534
535static inline void free_wr_skb(struct sk_buff *skb)
536{
537 kfree_skb(skb);
538}
539
540static inline struct sk_buff *dequeue_wr(struct s3_conn *c3cn)
541{
542 struct sk_buff *skb = c3cn->wr_pending_head;
543
544 if (likely(skb)) {
545 /* Don't bother clearing the tail */
Karen Xie1648b112009-02-13 21:38:44 -0800546 c3cn->wr_pending_head = skb_tx_wr_next(skb);
547 skb_tx_wr_next(skb) = NULL;
Karen Xiec3673462008-12-09 14:15:32 -0800548 }
549 return skb;
550}
551
552static void purge_wr_queue(struct s3_conn *c3cn)
553{
554 struct sk_buff *skb;
555 while ((skb = dequeue_wr(c3cn)) != NULL)
556 free_wr_skb(skb);
557}
558
559static inline void make_tx_data_wr(struct s3_conn *c3cn, struct sk_buff *skb,
Karen Xie1648b112009-02-13 21:38:44 -0800560 int len, int req_completion)
Karen Xiec3673462008-12-09 14:15:32 -0800561{
562 struct tx_data_wr *req;
563
564 skb_reset_transport_header(skb);
565 req = (struct tx_data_wr *)__skb_push(skb, sizeof(*req));
Karen Xie1648b112009-02-13 21:38:44 -0800566 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA) |
567 (req_completion ? F_WR_COMPL : 0));
Karen Xiec3673462008-12-09 14:15:32 -0800568 req->wr_lo = htonl(V_WR_TID(c3cn->tid));
569 req->sndseq = htonl(c3cn->snd_nxt);
570 /* len includes the length of any HW ULP additions */
571 req->len = htonl(len);
572 req->param = htonl(V_TX_PORT(c3cn->l2t->smt_idx));
573 /* V_TX_ULP_SUBMODE sets both the mode and submode */
574 req->flags = htonl(V_TX_ULP_SUBMODE(skb_ulp_mode(skb)) |
575 V_TX_SHOVE((skb_peek(&c3cn->write_queue) ? 0 : 1)));
576
577 if (!c3cn_flag(c3cn, C3CN_TX_DATA_SENT)) {
578 req->flags |= htonl(V_TX_ACK_PAGES(2) | F_TX_INIT |
579 V_TX_CPU_IDX(c3cn->qset));
580 /* Sendbuffer is in units of 32KB. */
581 req->param |= htonl(V_TX_SNDBUF(cxgb3_snd_win >> 15));
582 c3cn_set_flag(c3cn, C3CN_TX_DATA_SENT);
583 }
584}
585
586/**
587 * c3cn_push_tx_frames -- start transmit
588 * @c3cn: the offloaded connection
589 * @req_completion: request wr_ack or not
590 *
591 * Prepends TX_DATA_WR or CPL_CLOSE_CON_REQ headers to buffers waiting in a
592 * connection's send queue and sends them on to T3. Must be called with the
593 * connection's lock held. Returns the amount of send buffer space that was
594 * freed as a result of sending queued data to T3.
595 */
596static void arp_failure_discard(struct t3cdev *cdev, struct sk_buff *skb)
597{
598 kfree_skb(skb);
599}
600
601static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion)
602{
603 int total_size = 0;
604 struct sk_buff *skb;
605 struct t3cdev *cdev;
606 struct cxgb3i_sdev_data *cdata;
607
608 if (unlikely(c3cn->state == C3CN_STATE_CONNECTING ||
609 c3cn->state == C3CN_STATE_CLOSE_WAIT_1 ||
Karen Xie1648b112009-02-13 21:38:44 -0800610 c3cn->state >= C3CN_STATE_ABORTING)) {
Karen Xiec3673462008-12-09 14:15:32 -0800611 c3cn_tx_debug("c3cn 0x%p, in closing state %u.\n",
612 c3cn, c3cn->state);
613 return 0;
614 }
615
616 cdev = c3cn->cdev;
617 cdata = CXGB3_SDEV_DATA(cdev);
618
619 while (c3cn->wr_avail
620 && (skb = skb_peek(&c3cn->write_queue)) != NULL) {
621 int len = skb->len; /* length before skb_push */
622 int frags = skb_shinfo(skb)->nr_frags + (len != skb->data_len);
623 int wrs_needed = skb_wrs[frags];
624
625 if (wrs_needed > 1 && len + sizeof(struct tx_data_wr) <= wrlen)
626 wrs_needed = 1;
627
628 WARN_ON(frags >= SKB_WR_LIST_SIZE || wrs_needed < 1);
629
630 if (c3cn->wr_avail < wrs_needed) {
631 c3cn_tx_debug("c3cn 0x%p, skb len %u/%u, frag %u, "
632 "wr %d < %u.\n",
Karen Xie1648b112009-02-13 21:38:44 -0800633 c3cn, skb->len, skb->data_len, frags,
Karen Xiec3673462008-12-09 14:15:32 -0800634 wrs_needed, c3cn->wr_avail);
635 break;
636 }
637
638 __skb_unlink(skb, &c3cn->write_queue);
639 skb->priority = CPL_PRIORITY_DATA;
640 skb->csum = wrs_needed; /* remember this until the WR_ACK */
641 c3cn->wr_avail -= wrs_needed;
642 c3cn->wr_unacked += wrs_needed;
643 enqueue_wr(c3cn, skb);
644
Karen Xie1648b112009-02-13 21:38:44 -0800645 c3cn_tx_debug("c3cn 0x%p, enqueue, skb len %u/%u, frag %u, "
646 "wr %d, left %u, unack %u.\n",
647 c3cn, skb->len, skb->data_len, frags,
648 wrs_needed, c3cn->wr_avail, c3cn->wr_unacked);
Karen Xiec3673462008-12-09 14:15:32 -0800649
Karen Xie1648b112009-02-13 21:38:44 -0800650
651 if (likely(skb_flags(skb) & C3CB_FLAG_NEED_HDR)) {
652 if ((req_completion &&
653 c3cn->wr_unacked == wrs_needed) ||
654 (skb_flags(skb) & C3CB_FLAG_COMPL) ||
655 c3cn->wr_unacked >= c3cn->wr_max / 2) {
656 req_completion = 1;
Karen Xiec3673462008-12-09 14:15:32 -0800657 c3cn->wr_unacked = 0;
658 }
Karen Xie1648b112009-02-13 21:38:44 -0800659 len += ulp_extra_len(skb);
660 make_tx_data_wr(c3cn, skb, len, req_completion);
661 c3cn->snd_nxt += len;
662 skb_flags(skb) &= ~C3CB_FLAG_NEED_HDR;
Karen Xiec3673462008-12-09 14:15:32 -0800663 }
664
665 total_size += skb->truesize;
666 set_arp_failure_handler(skb, arp_failure_discard);
667 l2t_send(cdev, skb, c3cn->l2t);
668 }
669 return total_size;
670}
671
672/*
673 * process_cpl_msg: -> host
674 * Top-level CPL message processing used by most CPL messages that
675 * pertain to connections.
676 */
677static inline void process_cpl_msg(void (*fn)(struct s3_conn *,
678 struct sk_buff *),
679 struct s3_conn *c3cn,
680 struct sk_buff *skb)
681{
682 spin_lock_bh(&c3cn->lock);
683 fn(c3cn, skb);
684 spin_unlock_bh(&c3cn->lock);
685}
686
687/*
688 * process_cpl_msg_ref: -> host
689 * Similar to process_cpl_msg() but takes an extra connection reference around
690 * the call to the handler. Should be used if the handler may drop a
691 * connection reference.
692 */
693static inline void process_cpl_msg_ref(void (*fn) (struct s3_conn *,
694 struct sk_buff *),
695 struct s3_conn *c3cn,
696 struct sk_buff *skb)
697{
698 c3cn_hold(c3cn);
699 process_cpl_msg(fn, c3cn, skb);
700 c3cn_put(c3cn);
701}
702
703/*
704 * Process a CPL_ACT_ESTABLISH message: -> host
705 * Updates connection state from an active establish CPL message. Runs with
706 * the connection lock held.
707 */
708
709static inline void s3_free_atid(struct t3cdev *cdev, unsigned int tid)
710{
711 struct s3_conn *c3cn = cxgb3_free_atid(cdev, tid);
712 if (c3cn)
713 c3cn_put(c3cn);
714}
715
716static void c3cn_established(struct s3_conn *c3cn, u32 snd_isn,
717 unsigned int opt)
718{
719 c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
720
721 c3cn->write_seq = c3cn->snd_nxt = c3cn->snd_una = snd_isn;
722
723 /*
724 * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
725 * pass through opt0.
726 */
727 if (cxgb3_rcv_win > (M_RCV_BUFSIZ << 10))
728 c3cn->rcv_wup -= cxgb3_rcv_win - (M_RCV_BUFSIZ << 10);
729
730 dst_confirm(c3cn->dst_cache);
731
732 smp_mb();
733
734 c3cn_set_state(c3cn, C3CN_STATE_ESTABLISHED);
735}
736
737static void process_act_establish(struct s3_conn *c3cn, struct sk_buff *skb)
738{
739 struct cpl_act_establish *req = cplhdr(skb);
740 u32 rcv_isn = ntohl(req->rcv_isn); /* real RCV_ISN + 1 */
741
742 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
743 c3cn, c3cn->state, c3cn->flags);
744
745 if (unlikely(c3cn->state != C3CN_STATE_CONNECTING))
746 cxgb3i_log_error("TID %u expected SYN_SENT, got EST., s %u\n",
747 c3cn->tid, c3cn->state);
748
749 c3cn->copied_seq = c3cn->rcv_wup = c3cn->rcv_nxt = rcv_isn;
750 c3cn_established(c3cn, ntohl(req->snd_isn), ntohs(req->tcp_opt));
751
752 __kfree_skb(skb);
753
754 if (unlikely(c3cn_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED)))
755 /* upper layer has requested closing */
756 send_abort_req(c3cn);
Karen Xie1648b112009-02-13 21:38:44 -0800757 else {
758 if (skb_queue_len(&c3cn->write_queue))
759 c3cn_push_tx_frames(c3cn, 1);
Karen Xiec3673462008-12-09 14:15:32 -0800760 cxgb3i_conn_tx_open(c3cn);
Karen Xie1648b112009-02-13 21:38:44 -0800761 }
Karen Xiec3673462008-12-09 14:15:32 -0800762}
763
764static int do_act_establish(struct t3cdev *cdev, struct sk_buff *skb,
765 void *ctx)
766{
767 struct cpl_act_establish *req = cplhdr(skb);
768 unsigned int tid = GET_TID(req);
769 unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
770 struct s3_conn *c3cn = ctx;
771 struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
772
773 c3cn_conn_debug("rcv, tid 0x%x, c3cn 0x%p, s %u, f 0x%lx.\n",
774 tid, c3cn, c3cn->state, c3cn->flags);
775
776 c3cn->tid = tid;
777 c3cn_hold(c3cn);
778 cxgb3_insert_tid(cdata->cdev, cdata->client, c3cn, tid);
779 s3_free_atid(cdev, atid);
780
781 c3cn->qset = G_QNUM(ntohl(skb->csum));
782
783 process_cpl_msg(process_act_establish, c3cn, skb);
784 return 0;
785}
786
787/*
788 * Process a CPL_ACT_OPEN_RPL message: -> host
789 * Handle active open failures.
790 */
791static int act_open_rpl_status_to_errno(int status)
792{
793 switch (status) {
794 case CPL_ERR_CONN_RESET:
795 return ECONNREFUSED;
796 case CPL_ERR_ARP_MISS:
797 return EHOSTUNREACH;
798 case CPL_ERR_CONN_TIMEDOUT:
799 return ETIMEDOUT;
800 case CPL_ERR_TCAM_FULL:
801 return ENOMEM;
802 case CPL_ERR_CONN_EXIST:
803 cxgb3i_log_error("ACTIVE_OPEN_RPL: 4-tuple in use\n");
804 return EADDRINUSE;
805 default:
806 return EIO;
807 }
808}
809
810static void act_open_retry_timer(unsigned long data)
811{
812 struct sk_buff *skb;
813 struct s3_conn *c3cn = (struct s3_conn *)data;
814
815 c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
816
817 spin_lock_bh(&c3cn->lock);
818 skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_ATOMIC);
819 if (!skb)
820 fail_act_open(c3cn, ENOMEM);
821 else {
822 skb->sk = (struct sock *)c3cn;
823 set_arp_failure_handler(skb, act_open_req_arp_failure);
824 make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
825 l2t_send(c3cn->cdev, skb, c3cn->l2t);
826 }
827 spin_unlock_bh(&c3cn->lock);
828 c3cn_put(c3cn);
829}
830
831static void process_act_open_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
832{
833 struct cpl_act_open_rpl *rpl = cplhdr(skb);
834
835 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
836 c3cn, c3cn->state, c3cn->flags);
837
838 if (rpl->status == CPL_ERR_CONN_EXIST &&
839 c3cn->retry_timer.function != act_open_retry_timer) {
840 c3cn->retry_timer.function = act_open_retry_timer;
841 if (!mod_timer(&c3cn->retry_timer, jiffies + HZ / 2))
842 c3cn_hold(c3cn);
843 } else
844 fail_act_open(c3cn, act_open_rpl_status_to_errno(rpl->status));
845 __kfree_skb(skb);
846}
847
848static int do_act_open_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
849{
850 struct s3_conn *c3cn = ctx;
851 struct cpl_act_open_rpl *rpl = cplhdr(skb);
852
853 c3cn_conn_debug("rcv, status 0x%x, c3cn 0x%p, s %u, f 0x%lx.\n",
854 rpl->status, c3cn, c3cn->state, c3cn->flags);
855
856 if (rpl->status != CPL_ERR_TCAM_FULL &&
857 rpl->status != CPL_ERR_CONN_EXIST &&
858 rpl->status != CPL_ERR_ARP_MISS)
859 cxgb3_queue_tid_release(cdev, GET_TID(rpl));
860
861 process_cpl_msg_ref(process_act_open_rpl, c3cn, skb);
862 return 0;
863}
864
865/*
866 * Process PEER_CLOSE CPL messages: -> host
867 * Handle peer FIN.
868 */
869static void process_peer_close(struct s3_conn *c3cn, struct sk_buff *skb)
870{
871 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
872 c3cn, c3cn->state, c3cn->flags);
873
874 if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING))
875 goto out;
876
877 switch (c3cn->state) {
878 case C3CN_STATE_ESTABLISHED:
879 c3cn_set_state(c3cn, C3CN_STATE_PASSIVE_CLOSE);
880 break;
881 case C3CN_STATE_ACTIVE_CLOSE:
882 c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_2);
883 break;
884 case C3CN_STATE_CLOSE_WAIT_1:
885 c3cn_closed(c3cn);
886 break;
887 case C3CN_STATE_ABORTING:
888 break;
889 default:
890 cxgb3i_log_error("%s: peer close, TID %u in bad state %u\n",
891 c3cn->cdev->name, c3cn->tid, c3cn->state);
892 }
893
894 cxgb3i_conn_closing(c3cn);
895out:
896 __kfree_skb(skb);
897}
898
899static int do_peer_close(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
900{
901 struct s3_conn *c3cn = ctx;
902
903 c3cn_conn_debug("rcv, c3cn 0x%p, s %u, f 0x%lx.\n",
904 c3cn, c3cn->state, c3cn->flags);
905 process_cpl_msg_ref(process_peer_close, c3cn, skb);
906 return 0;
907}
908
909/*
910 * Process CLOSE_CONN_RPL CPL message: -> host
911 * Process a peer ACK to our FIN.
912 */
913static void process_close_con_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
914{
915 struct cpl_close_con_rpl *rpl = cplhdr(skb);
916
917 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
918 c3cn, c3cn->state, c3cn->flags);
919
920 c3cn->snd_una = ntohl(rpl->snd_nxt) - 1; /* exclude FIN */
921
922 if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING))
923 goto out;
924
925 switch (c3cn->state) {
926 case C3CN_STATE_ACTIVE_CLOSE:
927 c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_1);
928 break;
929 case C3CN_STATE_CLOSE_WAIT_1:
930 case C3CN_STATE_CLOSE_WAIT_2:
931 c3cn_closed(c3cn);
932 break;
933 case C3CN_STATE_ABORTING:
934 break;
935 default:
936 cxgb3i_log_error("%s: close_rpl, TID %u in bad state %u\n",
937 c3cn->cdev->name, c3cn->tid, c3cn->state);
938 }
939
940out:
941 kfree_skb(skb);
942}
943
944static int do_close_con_rpl(struct t3cdev *cdev, struct sk_buff *skb,
945 void *ctx)
946{
947 struct s3_conn *c3cn = ctx;
948
949 c3cn_conn_debug("rcv, c3cn 0x%p, s %u, f 0x%lx.\n",
950 c3cn, c3cn->state, c3cn->flags);
951
952 process_cpl_msg_ref(process_close_con_rpl, c3cn, skb);
953 return 0;
954}
955
956/*
957 * Process ABORT_REQ_RSS CPL message: -> host
958 * Process abort requests. If we are waiting for an ABORT_RPL we ignore this
959 * request except that we need to reply to it.
960 */
961
962static int abort_status_to_errno(struct s3_conn *c3cn, int abort_reason,
963 int *need_rst)
964{
965 switch (abort_reason) {
966 case CPL_ERR_BAD_SYN: /* fall through */
967 case CPL_ERR_CONN_RESET:
968 return c3cn->state > C3CN_STATE_ESTABLISHED ?
969 EPIPE : ECONNRESET;
970 case CPL_ERR_XMIT_TIMEDOUT:
971 case CPL_ERR_PERSIST_TIMEDOUT:
972 case CPL_ERR_FINWAIT2_TIMEDOUT:
973 case CPL_ERR_KEEPALIVE_TIMEDOUT:
974 return ETIMEDOUT;
975 default:
976 return EIO;
977 }
978}
979
980static void process_abort_req(struct s3_conn *c3cn, struct sk_buff *skb)
981{
982 int rst_status = CPL_ABORT_NO_RST;
983 const struct cpl_abort_req_rss *req = cplhdr(skb);
984
985 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
986 c3cn, c3cn->state, c3cn->flags);
987
988 if (!c3cn_flag(c3cn, C3CN_ABORT_REQ_RCVD)) {
989 c3cn_set_flag(c3cn, C3CN_ABORT_REQ_RCVD);
990 c3cn_set_state(c3cn, C3CN_STATE_ABORTING);
991 __kfree_skb(skb);
992 return;
993 }
994
995 c3cn_clear_flag(c3cn, C3CN_ABORT_REQ_RCVD);
996 send_abort_rpl(c3cn, rst_status);
997
998 if (!c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING)) {
999 c3cn->err =
1000 abort_status_to_errno(c3cn, req->status, &rst_status);
1001 c3cn_closed(c3cn);
1002 }
1003}
1004
1005static int do_abort_req(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
1006{
1007 const struct cpl_abort_req_rss *req = cplhdr(skb);
1008 struct s3_conn *c3cn = ctx;
1009
1010 c3cn_conn_debug("rcv, c3cn 0x%p, s 0x%x, f 0x%lx.\n",
1011 c3cn, c3cn->state, c3cn->flags);
1012
1013 if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
1014 req->status == CPL_ERR_PERSIST_NEG_ADVICE) {
1015 __kfree_skb(skb);
1016 return 0;
1017 }
1018
1019 process_cpl_msg_ref(process_abort_req, c3cn, skb);
1020 return 0;
1021}
1022
1023/*
1024 * Process ABORT_RPL_RSS CPL message: -> host
1025 * Process abort replies. We only process these messages if we anticipate
1026 * them as the coordination between SW and HW in this area is somewhat lacking
1027 * and sometimes we get ABORT_RPLs after we are done with the connection that
1028 * originated the ABORT_REQ.
1029 */
1030static void process_abort_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
1031{
1032 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1033 c3cn, c3cn->state, c3cn->flags);
1034
1035 if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING)) {
1036 if (!c3cn_flag(c3cn, C3CN_ABORT_RPL_RCVD))
1037 c3cn_set_flag(c3cn, C3CN_ABORT_RPL_RCVD);
1038 else {
1039 c3cn_clear_flag(c3cn, C3CN_ABORT_RPL_RCVD);
1040 c3cn_clear_flag(c3cn, C3CN_ABORT_RPL_PENDING);
1041 if (c3cn_flag(c3cn, C3CN_ABORT_REQ_RCVD))
1042 cxgb3i_log_error("%s tid %u, ABORT_RPL_RSS\n",
1043 c3cn->cdev->name, c3cn->tid);
1044 c3cn_closed(c3cn);
1045 }
1046 }
1047 __kfree_skb(skb);
1048}
1049
1050static int do_abort_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
1051{
1052 struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1053 struct s3_conn *c3cn = ctx;
1054
1055 c3cn_conn_debug("rcv, status 0x%x, c3cn 0x%p, s %u, 0x%lx.\n",
1056 rpl->status, c3cn, c3cn ? c3cn->state : 0,
1057 c3cn ? c3cn->flags : 0UL);
1058
1059 /*
1060 * Ignore replies to post-close aborts indicating that the abort was
1061 * requested too late. These connections are terminated when we get
1062 * PEER_CLOSE or CLOSE_CON_RPL and by the time the abort_rpl_rss
1063 * arrives the TID is either no longer used or it has been recycled.
1064 */
1065 if (rpl->status == CPL_ERR_ABORT_FAILED)
1066 goto discard;
1067
1068 /*
1069 * Sometimes we've already closed the connection, e.g., a post-close
1070 * abort races with ABORT_REQ_RSS, the latter frees the connection
1071 * expecting the ABORT_REQ will fail with CPL_ERR_ABORT_FAILED,
1072 * but FW turns the ABORT_REQ into a regular one and so we get
1073 * ABORT_RPL_RSS with status 0 and no connection.
1074 */
1075 if (!c3cn)
1076 goto discard;
1077
1078 process_cpl_msg_ref(process_abort_rpl, c3cn, skb);
1079 return 0;
1080
1081discard:
1082 __kfree_skb(skb);
1083 return 0;
1084}
1085
1086/*
1087 * Process RX_ISCSI_HDR CPL message: -> host
1088 * Handle received PDUs, the payload could be DDP'ed. If not, the payload
1089 * follow after the bhs.
1090 */
1091static void process_rx_iscsi_hdr(struct s3_conn *c3cn, struct sk_buff *skb)
1092{
1093 struct cpl_iscsi_hdr *hdr_cpl = cplhdr(skb);
1094 struct cpl_iscsi_hdr_norss data_cpl;
1095 struct cpl_rx_data_ddp_norss ddp_cpl;
1096 unsigned int hdr_len, data_len, status;
1097 unsigned int len;
1098 int err;
1099
1100 if (unlikely(c3cn->state >= C3CN_STATE_PASSIVE_CLOSE)) {
1101 if (c3cn->state != C3CN_STATE_ABORTING)
1102 send_abort_req(c3cn);
1103 __kfree_skb(skb);
1104 return;
1105 }
1106
Karen Xie1648b112009-02-13 21:38:44 -08001107 skb_tcp_seq(skb) = ntohl(hdr_cpl->seq);
1108 skb_flags(skb) = 0;
Karen Xiec3673462008-12-09 14:15:32 -08001109
1110 skb_reset_transport_header(skb);
1111 __skb_pull(skb, sizeof(struct cpl_iscsi_hdr));
1112
1113 len = hdr_len = ntohs(hdr_cpl->len);
1114 /* msg coalesce is off or not enough data received */
1115 if (skb->len <= hdr_len) {
1116 cxgb3i_log_error("%s: TID %u, ISCSI_HDR, skb len %u < %u.\n",
1117 c3cn->cdev->name, c3cn->tid,
1118 skb->len, hdr_len);
1119 goto abort_conn;
1120 }
1121
1122 err = skb_copy_bits(skb, skb->len - sizeof(ddp_cpl), &ddp_cpl,
1123 sizeof(ddp_cpl));
1124 if (err < 0)
1125 goto abort_conn;
1126
1127 skb_ulp_mode(skb) = ULP2_FLAG_DATA_READY;
Karen Xie1648b112009-02-13 21:38:44 -08001128 skb_rx_pdulen(skb) = ntohs(ddp_cpl.len);
1129 skb_rx_ddigest(skb) = ntohl(ddp_cpl.ulp_crc);
Karen Xiec3673462008-12-09 14:15:32 -08001130 status = ntohl(ddp_cpl.ddp_status);
1131
1132 c3cn_rx_debug("rx skb 0x%p, len %u, pdulen %u, ddp status 0x%x.\n",
Karen Xie1648b112009-02-13 21:38:44 -08001133 skb, skb->len, skb_rx_pdulen(skb), status);
Karen Xiec3673462008-12-09 14:15:32 -08001134
1135 if (status & (1 << RX_DDP_STATUS_HCRC_SHIFT))
1136 skb_ulp_mode(skb) |= ULP2_FLAG_HCRC_ERROR;
1137 if (status & (1 << RX_DDP_STATUS_DCRC_SHIFT))
1138 skb_ulp_mode(skb) |= ULP2_FLAG_DCRC_ERROR;
1139 if (status & (1 << RX_DDP_STATUS_PAD_SHIFT))
1140 skb_ulp_mode(skb) |= ULP2_FLAG_PAD_ERROR;
1141
1142 if (skb->len > (hdr_len + sizeof(ddp_cpl))) {
1143 err = skb_copy_bits(skb, hdr_len, &data_cpl, sizeof(data_cpl));
1144 if (err < 0)
1145 goto abort_conn;
1146 data_len = ntohs(data_cpl.len);
1147 len += sizeof(data_cpl) + data_len;
1148 } else if (status & (1 << RX_DDP_STATUS_DDP_SHIFT))
1149 skb_ulp_mode(skb) |= ULP2_FLAG_DATA_DDPED;
1150
Karen Xie1648b112009-02-13 21:38:44 -08001151 c3cn->rcv_nxt = ntohl(ddp_cpl.seq) + skb_rx_pdulen(skb);
Karen Xiec3673462008-12-09 14:15:32 -08001152 __pskb_trim(skb, len);
1153 __skb_queue_tail(&c3cn->receive_queue, skb);
1154 cxgb3i_conn_pdu_ready(c3cn);
1155
1156 return;
1157
1158abort_conn:
1159 send_abort_req(c3cn);
1160 __kfree_skb(skb);
1161}
1162
1163static int do_iscsi_hdr(struct t3cdev *t3dev, struct sk_buff *skb, void *ctx)
1164{
1165 struct s3_conn *c3cn = ctx;
1166
1167 process_cpl_msg(process_rx_iscsi_hdr, c3cn, skb);
1168 return 0;
1169}
1170
1171/*
1172 * Process TX_DATA_ACK CPL messages: -> host
1173 * Process an acknowledgment of WR completion. Advance snd_una and send the
1174 * next batch of work requests from the write queue.
1175 */
Karen Xie1648b112009-02-13 21:38:44 -08001176static void check_wr_invariants(struct s3_conn *c3cn)
1177{
1178 int pending = count_pending_wrs(c3cn);
1179
1180 if (unlikely(c3cn->wr_avail + pending != c3cn->wr_max))
1181 cxgb3i_log_error("TID %u: credit imbalance: avail %u, "
1182 "pending %u, total should be %u\n",
1183 c3cn->tid, c3cn->wr_avail, pending,
1184 c3cn->wr_max);
1185}
1186
Karen Xiec3673462008-12-09 14:15:32 -08001187static void process_wr_ack(struct s3_conn *c3cn, struct sk_buff *skb)
1188{
1189 struct cpl_wr_ack *hdr = cplhdr(skb);
1190 unsigned int credits = ntohs(hdr->credits);
1191 u32 snd_una = ntohl(hdr->snd_una);
1192
Karen Xie1648b112009-02-13 21:38:44 -08001193 c3cn_tx_debug("%u WR credits, avail %u, unack %u, TID %u, state %u.\n",
1194 credits, c3cn->wr_avail, c3cn->wr_unacked,
1195 c3cn->tid, c3cn->state);
1196
Karen Xiec3673462008-12-09 14:15:32 -08001197 c3cn->wr_avail += credits;
1198 if (c3cn->wr_unacked > c3cn->wr_max - c3cn->wr_avail)
1199 c3cn->wr_unacked = c3cn->wr_max - c3cn->wr_avail;
1200
1201 while (credits) {
1202 struct sk_buff *p = peek_wr(c3cn);
1203
1204 if (unlikely(!p)) {
1205 cxgb3i_log_error("%u WR_ACK credits for TID %u with "
1206 "nothing pending, state %u\n",
1207 credits, c3cn->tid, c3cn->state);
1208 break;
1209 }
1210 if (unlikely(credits < p->csum)) {
Karen Xie1648b112009-02-13 21:38:44 -08001211 struct tx_data_wr *w = cplhdr(p);
1212 cxgb3i_log_error("TID %u got %u WR credits need %u, "
1213 "len %u, main body %u, frags %u, "
1214 "seq # %u, ACK una %u, ACK nxt %u, "
1215 "WR_AVAIL %u, WRs pending %u\n",
1216 c3cn->tid, credits, p->csum, p->len,
1217 p->len - p->data_len,
1218 skb_shinfo(p)->nr_frags,
1219 ntohl(w->sndseq), snd_una,
1220 ntohl(hdr->snd_nxt), c3cn->wr_avail,
1221 count_pending_wrs(c3cn) - credits);
Karen Xiec3673462008-12-09 14:15:32 -08001222 p->csum -= credits;
1223 break;
1224 } else {
1225 dequeue_wr(c3cn);
1226 credits -= p->csum;
1227 free_wr_skb(p);
1228 }
1229 }
1230
Karen Xie1648b112009-02-13 21:38:44 -08001231 check_wr_invariants(c3cn);
1232
1233 if (unlikely(before(snd_una, c3cn->snd_una))) {
1234 cxgb3i_log_error("TID %u, unexpected sequence # %u in WR_ACK "
1235 "snd_una %u\n",
1236 c3cn->tid, snd_una, c3cn->snd_una);
Karen Xiec3673462008-12-09 14:15:32 -08001237 goto out_free;
Karen Xie1648b112009-02-13 21:38:44 -08001238 }
Karen Xiec3673462008-12-09 14:15:32 -08001239
1240 if (c3cn->snd_una != snd_una) {
1241 c3cn->snd_una = snd_una;
1242 dst_confirm(c3cn->dst_cache);
1243 }
1244
Karen Xie1648b112009-02-13 21:38:44 -08001245 if (skb_queue_len(&c3cn->write_queue)) {
1246 if (c3cn_push_tx_frames(c3cn, 0))
1247 cxgb3i_conn_tx_open(c3cn);
1248 } else
Karen Xiec3673462008-12-09 14:15:32 -08001249 cxgb3i_conn_tx_open(c3cn);
1250out_free:
1251 __kfree_skb(skb);
1252}
1253
1254static int do_wr_ack(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
1255{
1256 struct s3_conn *c3cn = ctx;
1257
1258 process_cpl_msg(process_wr_ack, c3cn, skb);
1259 return 0;
1260}
1261
1262/*
1263 * for each connection, pre-allocate skbs needed for close/abort requests. So
1264 * that we can service the request right away.
1265 */
1266static void c3cn_free_cpl_skbs(struct s3_conn *c3cn)
1267{
1268 if (c3cn->cpl_close)
1269 kfree_skb(c3cn->cpl_close);
1270 if (c3cn->cpl_abort_req)
1271 kfree_skb(c3cn->cpl_abort_req);
1272 if (c3cn->cpl_abort_rpl)
1273 kfree_skb(c3cn->cpl_abort_rpl);
1274}
1275
1276static int c3cn_alloc_cpl_skbs(struct s3_conn *c3cn)
1277{
1278 c3cn->cpl_close = alloc_skb(sizeof(struct cpl_close_con_req),
1279 GFP_KERNEL);
1280 if (!c3cn->cpl_close)
1281 return -ENOMEM;
1282 skb_put(c3cn->cpl_close, sizeof(struct cpl_close_con_req));
1283
1284 c3cn->cpl_abort_req = alloc_skb(sizeof(struct cpl_abort_req),
1285 GFP_KERNEL);
1286 if (!c3cn->cpl_abort_req)
1287 goto free_cpl_skbs;
1288 skb_put(c3cn->cpl_abort_req, sizeof(struct cpl_abort_req));
1289
1290 c3cn->cpl_abort_rpl = alloc_skb(sizeof(struct cpl_abort_rpl),
1291 GFP_KERNEL);
1292 if (!c3cn->cpl_abort_rpl)
1293 goto free_cpl_skbs;
1294 skb_put(c3cn->cpl_abort_rpl, sizeof(struct cpl_abort_rpl));
1295
1296 return 0;
1297
1298free_cpl_skbs:
1299 c3cn_free_cpl_skbs(c3cn);
1300 return -ENOMEM;
1301}
1302
1303/**
1304 * c3cn_release_offload_resources - release offload resource
1305 * @c3cn: the offloaded iscsi tcp connection.
1306 * Release resources held by an offload connection (TID, L2T entry, etc.)
1307 */
1308static void c3cn_release_offload_resources(struct s3_conn *c3cn)
1309{
1310 struct t3cdev *cdev = c3cn->cdev;
1311 unsigned int tid = c3cn->tid;
1312
Karen Xiec3673462008-12-09 14:15:32 -08001313 c3cn->qset = 0;
Karen Xiec3673462008-12-09 14:15:32 -08001314 c3cn_free_cpl_skbs(c3cn);
1315
1316 if (c3cn->wr_avail != c3cn->wr_max) {
1317 purge_wr_queue(c3cn);
1318 reset_wr_list(c3cn);
1319 }
1320
Karen Xie2a90030f2009-04-01 13:11:26 -05001321 if (cdev) {
1322 if (c3cn->l2t) {
1323 l2t_release(L2DATA(cdev), c3cn->l2t);
1324 c3cn->l2t = NULL;
1325 }
1326 if (c3cn->state == C3CN_STATE_CONNECTING)
1327 /* we have ATID */
1328 s3_free_atid(cdev, tid);
1329 else {
1330 /* we have TID */
1331 cxgb3_remove_tid(cdev, (void *)c3cn, tid);
1332 c3cn_put(c3cn);
1333 }
Karen Xiec3673462008-12-09 14:15:32 -08001334 }
1335
Karen Xie2a90030f2009-04-01 13:11:26 -05001336 c3cn->dst_cache = NULL;
Karen Xiec3673462008-12-09 14:15:32 -08001337 c3cn->cdev = NULL;
1338}
1339
1340/**
1341 * cxgb3i_c3cn_create - allocate and initialize an s3_conn structure
1342 * returns the s3_conn structure allocated.
1343 */
1344struct s3_conn *cxgb3i_c3cn_create(void)
1345{
1346 struct s3_conn *c3cn;
1347
1348 c3cn = kzalloc(sizeof(*c3cn), GFP_KERNEL);
1349 if (!c3cn)
1350 return NULL;
1351
1352 /* pre-allocate close/abort cpl, so we don't need to wait for memory
1353 when close/abort is requested. */
1354 if (c3cn_alloc_cpl_skbs(c3cn) < 0)
1355 goto free_c3cn;
1356
1357 c3cn_conn_debug("alloc c3cn 0x%p.\n", c3cn);
1358
1359 c3cn->flags = 0;
1360 spin_lock_init(&c3cn->lock);
1361 atomic_set(&c3cn->refcnt, 1);
1362 skb_queue_head_init(&c3cn->receive_queue);
1363 skb_queue_head_init(&c3cn->write_queue);
1364 setup_timer(&c3cn->retry_timer, NULL, (unsigned long)c3cn);
1365 rwlock_init(&c3cn->callback_lock);
1366
1367 return c3cn;
1368
1369free_c3cn:
1370 kfree(c3cn);
1371 return NULL;
1372}
1373
1374static void c3cn_active_close(struct s3_conn *c3cn)
1375{
1376 int data_lost;
1377 int close_req = 0;
1378
1379 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1380 c3cn, c3cn->state, c3cn->flags);
1381
1382 dst_confirm(c3cn->dst_cache);
1383
1384 c3cn_hold(c3cn);
1385 spin_lock_bh(&c3cn->lock);
1386
1387 data_lost = skb_queue_len(&c3cn->receive_queue);
1388 __skb_queue_purge(&c3cn->receive_queue);
1389
1390 switch (c3cn->state) {
1391 case C3CN_STATE_CLOSED:
1392 case C3CN_STATE_ACTIVE_CLOSE:
1393 case C3CN_STATE_CLOSE_WAIT_1:
1394 case C3CN_STATE_CLOSE_WAIT_2:
1395 case C3CN_STATE_ABORTING:
1396 /* nothing need to be done */
1397 break;
1398 case C3CN_STATE_CONNECTING:
1399 /* defer until cpl_act_open_rpl or cpl_act_establish */
1400 c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
1401 break;
1402 case C3CN_STATE_ESTABLISHED:
1403 close_req = 1;
1404 c3cn_set_state(c3cn, C3CN_STATE_ACTIVE_CLOSE);
1405 break;
1406 case C3CN_STATE_PASSIVE_CLOSE:
1407 close_req = 1;
1408 c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_2);
1409 break;
1410 }
1411
1412 if (close_req) {
1413 if (data_lost)
1414 /* Unread data was tossed, zap the connection. */
1415 send_abort_req(c3cn);
1416 else
1417 send_close_req(c3cn);
1418 }
1419
1420 spin_unlock_bh(&c3cn->lock);
1421 c3cn_put(c3cn);
1422}
1423
1424/**
Karen Xie2a90030f2009-04-01 13:11:26 -05001425 * cxgb3i_c3cn_release - close and release an iscsi tcp connection and any
1426 * resource held
Karen Xiec3673462008-12-09 14:15:32 -08001427 * @c3cn: the iscsi tcp connection
1428 */
1429void cxgb3i_c3cn_release(struct s3_conn *c3cn)
1430{
1431 c3cn_conn_debug("c3cn 0x%p, s %u, f 0x%lx.\n",
1432 c3cn, c3cn->state, c3cn->flags);
Karen Xie2a90030f2009-04-01 13:11:26 -05001433 if (unlikely(c3cn->state == C3CN_STATE_CONNECTING))
Karen Xiec3673462008-12-09 14:15:32 -08001434 c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
Karen Xie2a90030f2009-04-01 13:11:26 -05001435 else if (likely(c3cn->state != C3CN_STATE_CLOSED))
1436 c3cn_active_close(c3cn);
Karen Xiec3673462008-12-09 14:15:32 -08001437 c3cn_put(c3cn);
1438}
1439
1440static int is_cxgb3_dev(struct net_device *dev)
1441{
1442 struct cxgb3i_sdev_data *cdata;
1443
1444 write_lock(&cdata_rwlock);
1445 list_for_each_entry(cdata, &cdata_list, list) {
1446 struct adap_ports *ports = &cdata->ports;
1447 int i;
1448
1449 for (i = 0; i < ports->nports; i++)
1450 if (dev == ports->lldevs[i]) {
1451 write_unlock(&cdata_rwlock);
1452 return 1;
1453 }
1454 }
1455 write_unlock(&cdata_rwlock);
1456 return 0;
1457}
1458
1459/**
1460 * cxgb3_egress_dev - return the cxgb3 egress device
1461 * @root_dev: the root device anchoring the search
1462 * @c3cn: the connection used to determine egress port in bonding mode
1463 * @context: in bonding mode, indicates a connection set up or failover
1464 *
1465 * Return egress device or NULL if the egress device isn't one of our ports.
1466 */
1467static struct net_device *cxgb3_egress_dev(struct net_device *root_dev,
1468 struct s3_conn *c3cn,
1469 int context)
1470{
1471 while (root_dev) {
1472 if (root_dev->priv_flags & IFF_802_1Q_VLAN)
1473 root_dev = vlan_dev_real_dev(root_dev);
1474 else if (is_cxgb3_dev(root_dev))
1475 return root_dev;
1476 else
1477 return NULL;
1478 }
1479 return NULL;
1480}
1481
Mike Christie10eb0f02009-05-13 17:57:38 -05001482static struct rtable *find_route(struct net_device *dev,
1483 __be32 saddr, __be32 daddr,
Karen Xiec3673462008-12-09 14:15:32 -08001484 __be16 sport, __be16 dport)
1485{
1486 struct rtable *rt;
1487 struct flowi fl = {
Mike Christie10eb0f02009-05-13 17:57:38 -05001488 .oif = dev ? dev->ifindex : 0,
Karen Xiec3673462008-12-09 14:15:32 -08001489 .nl_u = {
1490 .ip4_u = {
1491 .daddr = daddr,
1492 .saddr = saddr,
1493 .tos = 0 } },
1494 .proto = IPPROTO_TCP,
1495 .uli_u = {
1496 .ports = {
1497 .sport = sport,
1498 .dport = dport } } };
1499
1500 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
1501 return NULL;
1502 return rt;
1503}
1504
1505/*
1506 * Assign offload parameters to some connection fields.
1507 */
1508static void init_offload_conn(struct s3_conn *c3cn,
1509 struct t3cdev *cdev,
1510 struct dst_entry *dst)
1511{
1512 BUG_ON(c3cn->cdev != cdev);
Karen Xie1648b112009-02-13 21:38:44 -08001513 c3cn->wr_max = c3cn->wr_avail = T3C_DATA(cdev)->max_wrs - 1;
Karen Xiec3673462008-12-09 14:15:32 -08001514 c3cn->wr_unacked = 0;
1515 c3cn->mss_idx = select_mss(c3cn, dst_mtu(dst));
1516
1517 reset_wr_list(c3cn);
1518}
1519
1520static int initiate_act_open(struct s3_conn *c3cn, struct net_device *dev)
1521{
1522 struct cxgb3i_sdev_data *cdata = NDEV2CDATA(dev);
1523 struct t3cdev *cdev = cdata->cdev;
1524 struct dst_entry *dst = c3cn->dst_cache;
1525 struct sk_buff *skb;
1526
1527 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1528 c3cn, c3cn->state, c3cn->flags);
1529 /*
1530 * Initialize connection data. Note that the flags and ULP mode are
1531 * initialized higher up ...
1532 */
1533 c3cn->dev = dev;
1534 c3cn->cdev = cdev;
1535 c3cn->tid = cxgb3_alloc_atid(cdev, cdata->client, c3cn);
1536 if (c3cn->tid < 0)
1537 goto out_err;
1538
1539 c3cn->qset = 0;
1540 c3cn->l2t = t3_l2t_get(cdev, dst->neighbour, dev);
1541 if (!c3cn->l2t)
1542 goto free_tid;
1543
1544 skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_KERNEL);
1545 if (!skb)
1546 goto free_l2t;
1547
1548 skb->sk = (struct sock *)c3cn;
1549 set_arp_failure_handler(skb, act_open_req_arp_failure);
1550
1551 c3cn_hold(c3cn);
1552
1553 init_offload_conn(c3cn, cdev, dst);
1554 c3cn->err = 0;
1555
1556 make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
1557 l2t_send(cdev, skb, c3cn->l2t);
1558 return 0;
1559
1560free_l2t:
1561 l2t_release(L2DATA(cdev), c3cn->l2t);
1562free_tid:
1563 s3_free_atid(cdev, c3cn->tid);
1564 c3cn->tid = 0;
1565out_err:
1566 return -1;
1567}
1568
1569
1570/**
1571 * cxgb3i_c3cn_connect - initiates an iscsi tcp connection to a given address
1572 * @c3cn: the iscsi tcp connection
1573 * @usin: destination address
1574 *
1575 * return 0 if active open request is sent, < 0 otherwise.
1576 */
Mike Christie10eb0f02009-05-13 17:57:38 -05001577int cxgb3i_c3cn_connect(struct net_device *dev, struct s3_conn *c3cn,
1578 struct sockaddr_in *usin)
Karen Xiec3673462008-12-09 14:15:32 -08001579{
1580 struct rtable *rt;
Karen Xiec3673462008-12-09 14:15:32 -08001581 struct cxgb3i_sdev_data *cdata;
1582 struct t3cdev *cdev;
1583 __be32 sipv4;
1584 int err;
1585
Mike Christie10eb0f02009-05-13 17:57:38 -05001586 c3cn_conn_debug("c3cn 0x%p, dev 0x%p.\n", c3cn, dev);
1587
Karen Xiec3673462008-12-09 14:15:32 -08001588 if (usin->sin_family != AF_INET)
1589 return -EAFNOSUPPORT;
1590
1591 c3cn->daddr.sin_port = usin->sin_port;
1592 c3cn->daddr.sin_addr.s_addr = usin->sin_addr.s_addr;
1593
Mike Christie10eb0f02009-05-13 17:57:38 -05001594 rt = find_route(dev, c3cn->saddr.sin_addr.s_addr,
Karen Xiec3673462008-12-09 14:15:32 -08001595 c3cn->daddr.sin_addr.s_addr,
1596 c3cn->saddr.sin_port,
1597 c3cn->daddr.sin_port);
1598 if (rt == NULL) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001599 c3cn_conn_debug("NO route to 0x%x, port %u, dev %s.\n",
Karen Xiec3673462008-12-09 14:15:32 -08001600 c3cn->daddr.sin_addr.s_addr,
Mike Christie10eb0f02009-05-13 17:57:38 -05001601 ntohs(c3cn->daddr.sin_port),
1602 dev ? dev->name : "any");
Karen Xiec3673462008-12-09 14:15:32 -08001603 return -ENETUNREACH;
1604 }
1605
1606 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001607 c3cn_conn_debug("multi-cast route to 0x%x, port %u, dev %s.\n",
Karen Xiec3673462008-12-09 14:15:32 -08001608 c3cn->daddr.sin_addr.s_addr,
Mike Christie10eb0f02009-05-13 17:57:38 -05001609 ntohs(c3cn->daddr.sin_port),
1610 dev ? dev->name : "any");
Karen Xiec3673462008-12-09 14:15:32 -08001611 ip_rt_put(rt);
1612 return -ENETUNREACH;
1613 }
1614
1615 if (!c3cn->saddr.sin_addr.s_addr)
1616 c3cn->saddr.sin_addr.s_addr = rt->rt_src;
1617
1618 /* now commit destination to connection */
1619 c3cn->dst_cache = &rt->u.dst;
1620
1621 /* try to establish an offloaded connection */
1622 dev = cxgb3_egress_dev(c3cn->dst_cache->dev, c3cn, 0);
1623 if (dev == NULL) {
1624 c3cn_conn_debug("c3cn 0x%p, egress dev NULL.\n", c3cn);
1625 return -ENETUNREACH;
1626 }
1627 cdata = NDEV2CDATA(dev);
1628 cdev = cdata->cdev;
1629
1630 /* get a source port if one hasn't been provided */
1631 err = c3cn_get_port(c3cn, cdata);
1632 if (err)
1633 return err;
1634
1635 c3cn_conn_debug("c3cn 0x%p get port %u.\n",
1636 c3cn, ntohs(c3cn->saddr.sin_port));
1637
1638 sipv4 = cxgb3i_get_private_ipv4addr(dev);
1639 if (!sipv4) {
1640 c3cn_conn_debug("c3cn 0x%p, iscsi ip not configured.\n", c3cn);
1641 sipv4 = c3cn->saddr.sin_addr.s_addr;
1642 cxgb3i_set_private_ipv4addr(dev, sipv4);
1643 } else
1644 c3cn->saddr.sin_addr.s_addr = sipv4;
1645
1646 c3cn_conn_debug("c3cn 0x%p, %u.%u.%u.%u,%u-%u.%u.%u.%u,%u SYN_SENT.\n",
1647 c3cn, NIPQUAD(c3cn->saddr.sin_addr.s_addr),
1648 ntohs(c3cn->saddr.sin_port),
1649 NIPQUAD(c3cn->daddr.sin_addr.s_addr),
1650 ntohs(c3cn->daddr.sin_port));
1651
1652 c3cn_set_state(c3cn, C3CN_STATE_CONNECTING);
1653 if (!initiate_act_open(c3cn, dev))
1654 return 0;
1655
1656 /*
1657 * If we get here, we don't have an offload connection so simply
1658 * return a failure.
1659 */
1660 err = -ENOTSUPP;
1661
1662 /*
1663 * This trashes the connection and releases the local port,
1664 * if necessary.
1665 */
1666 c3cn_conn_debug("c3cn 0x%p -> CLOSED.\n", c3cn);
1667 c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
1668 ip_rt_put(rt);
1669 c3cn_put_port(c3cn);
Karen Xiec3673462008-12-09 14:15:32 -08001670 return err;
1671}
1672
1673/**
1674 * cxgb3i_c3cn_rx_credits - ack received tcp data.
1675 * @c3cn: iscsi tcp connection
1676 * @copied: # of bytes processed
1677 *
1678 * Called after some received data has been read. It returns RX credits
1679 * to the HW for the amount of data processed.
1680 */
1681void cxgb3i_c3cn_rx_credits(struct s3_conn *c3cn, int copied)
1682{
1683 struct t3cdev *cdev;
1684 int must_send;
1685 u32 credits, dack = 0;
1686
1687 if (c3cn->state != C3CN_STATE_ESTABLISHED)
1688 return;
1689
1690 credits = c3cn->copied_seq - c3cn->rcv_wup;
1691 if (unlikely(!credits))
1692 return;
1693
1694 cdev = c3cn->cdev;
1695
1696 if (unlikely(cxgb3_rx_credit_thres == 0))
1697 return;
1698
1699 dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
1700
1701 /*
1702 * For coalescing to work effectively ensure the receive window has
1703 * at least 16KB left.
1704 */
1705 must_send = credits + 16384 >= cxgb3_rcv_win;
1706
1707 if (must_send || credits >= cxgb3_rx_credit_thres)
1708 c3cn->rcv_wup += send_rx_credits(c3cn, credits, dack);
1709}
1710
1711/**
1712 * cxgb3i_c3cn_send_pdus - send the skbs containing iscsi pdus
1713 * @c3cn: iscsi tcp connection
1714 * @skb: skb contains the iscsi pdu
1715 *
1716 * Add a list of skbs to a connection send queue. The skbs must comply with
1717 * the max size limit of the device and have a headroom of at least
1718 * TX_HEADER_LEN bytes.
1719 * Return # of bytes queued.
1720 */
1721int cxgb3i_c3cn_send_pdus(struct s3_conn *c3cn, struct sk_buff *skb)
1722{
1723 struct sk_buff *next;
1724 int err, copied = 0;
1725
1726 spin_lock_bh(&c3cn->lock);
1727
1728 if (c3cn->state != C3CN_STATE_ESTABLISHED) {
1729 c3cn_tx_debug("c3cn 0x%p, not in est. state %u.\n",
1730 c3cn, c3cn->state);
1731 err = -EAGAIN;
1732 goto out_err;
1733 }
1734
Karen Xiec3673462008-12-09 14:15:32 -08001735 if (c3cn->err) {
1736 c3cn_tx_debug("c3cn 0x%p, err %d.\n", c3cn, c3cn->err);
Karen Xie1648b112009-02-13 21:38:44 -08001737 err = -EPIPE;
1738 goto out_err;
1739 }
1740
1741 if (c3cn->write_seq - c3cn->snd_una >= cxgb3_snd_win) {
1742 c3cn_tx_debug("c3cn 0x%p, snd %u - %u > %u.\n",
1743 c3cn, c3cn->write_seq, c3cn->snd_una,
1744 cxgb3_snd_win);
Mike Christie13931092009-04-21 15:32:34 -05001745 err = -ENOBUFS;
Karen Xiec3673462008-12-09 14:15:32 -08001746 goto out_err;
1747 }
1748
1749 while (skb) {
1750 int frags = skb_shinfo(skb)->nr_frags +
1751 (skb->len != skb->data_len);
1752
1753 if (unlikely(skb_headroom(skb) < TX_HEADER_LEN)) {
1754 c3cn_tx_debug("c3cn 0x%p, skb head.\n", c3cn);
1755 err = -EINVAL;
1756 goto out_err;
1757 }
1758
1759 if (frags >= SKB_WR_LIST_SIZE) {
1760 cxgb3i_log_error("c3cn 0x%p, tx frags %d, len %u,%u.\n",
1761 c3cn, skb_shinfo(skb)->nr_frags,
1762 skb->len, skb->data_len);
1763 err = -EINVAL;
1764 goto out_err;
1765 }
1766
1767 next = skb->next;
1768 skb->next = NULL;
1769 skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND | C3CB_FLAG_NEED_HDR);
1770 copied += skb->len;
1771 c3cn->write_seq += skb->len + ulp_extra_len(skb);
1772 skb = next;
1773 }
1774done:
1775 if (likely(skb_queue_len(&c3cn->write_queue)))
1776 c3cn_push_tx_frames(c3cn, 1);
1777 spin_unlock_bh(&c3cn->lock);
1778 return copied;
1779
1780out_err:
1781 if (copied == 0 && err == -EPIPE)
1782 copied = c3cn->err ? c3cn->err : -EPIPE;
Mike Christie13931092009-04-21 15:32:34 -05001783 else
1784 copied = err;
Karen Xiec3673462008-12-09 14:15:32 -08001785 goto done;
1786}
1787
1788static void sdev_data_cleanup(struct cxgb3i_sdev_data *cdata)
1789{
1790 struct adap_ports *ports = &cdata->ports;
Karen Xie2a90030f2009-04-01 13:11:26 -05001791 struct s3_conn *c3cn;
Karen Xiec3673462008-12-09 14:15:32 -08001792 int i;
1793
Karen Xie2a90030f2009-04-01 13:11:26 -05001794 for (i = 0; i < cxgb3_max_connect; i++) {
1795 if (cdata->sport_conn[i]) {
1796 c3cn = cdata->sport_conn[i];
1797 cdata->sport_conn[i] = NULL;
1798
1799 spin_lock_bh(&c3cn->lock);
1800 c3cn->cdev = NULL;
1801 c3cn_set_flag(c3cn, C3CN_OFFLOAD_DOWN);
1802 c3cn_closed(c3cn);
1803 spin_unlock_bh(&c3cn->lock);
1804 }
1805 }
1806
Karen Xiec3673462008-12-09 14:15:32 -08001807 for (i = 0; i < ports->nports; i++)
1808 NDEV2CDATA(ports->lldevs[i]) = NULL;
Karen Xie2a90030f2009-04-01 13:11:26 -05001809
Karen Xiec3673462008-12-09 14:15:32 -08001810 cxgb3i_free_big_mem(cdata);
1811}
1812
1813void cxgb3i_sdev_cleanup(void)
1814{
1815 struct cxgb3i_sdev_data *cdata;
1816
1817 write_lock(&cdata_rwlock);
1818 list_for_each_entry(cdata, &cdata_list, list) {
1819 list_del(&cdata->list);
1820 sdev_data_cleanup(cdata);
1821 }
1822 write_unlock(&cdata_rwlock);
1823}
1824
1825int cxgb3i_sdev_init(cxgb3_cpl_handler_func *cpl_handlers)
1826{
1827 cpl_handlers[CPL_ACT_ESTABLISH] = do_act_establish;
1828 cpl_handlers[CPL_ACT_OPEN_RPL] = do_act_open_rpl;
1829 cpl_handlers[CPL_PEER_CLOSE] = do_peer_close;
1830 cpl_handlers[CPL_ABORT_REQ_RSS] = do_abort_req;
1831 cpl_handlers[CPL_ABORT_RPL_RSS] = do_abort_rpl;
1832 cpl_handlers[CPL_CLOSE_CON_RPL] = do_close_con_rpl;
1833 cpl_handlers[CPL_TX_DMA_ACK] = do_wr_ack;
1834 cpl_handlers[CPL_ISCSI_HDR] = do_iscsi_hdr;
1835
1836 if (cxgb3_max_connect > CXGB3I_MAX_CONN)
1837 cxgb3_max_connect = CXGB3I_MAX_CONN;
1838 return 0;
1839}
1840
1841/**
1842 * cxgb3i_sdev_add - allocate and initialize resources for each adapter found
1843 * @cdev: t3cdev adapter
1844 * @client: cxgb3 driver client
1845 */
1846void cxgb3i_sdev_add(struct t3cdev *cdev, struct cxgb3_client *client)
1847{
1848 struct cxgb3i_sdev_data *cdata;
1849 struct ofld_page_info rx_page_info;
1850 unsigned int wr_len;
Karen Xie2a90030f2009-04-01 13:11:26 -05001851 int mapsize = cxgb3_max_connect * sizeof(struct s3_conn *);
Karen Xiec3673462008-12-09 14:15:32 -08001852 int i;
1853
1854 cdata = cxgb3i_alloc_big_mem(sizeof(*cdata) + mapsize, GFP_KERNEL);
Karen Xie2a90030f2009-04-01 13:11:26 -05001855 if (!cdata) {
1856 cxgb3i_log_warn("t3dev 0x%p, offload up, OOM %d.\n",
1857 cdev, mapsize);
Karen Xiec3673462008-12-09 14:15:32 -08001858 return;
Karen Xie2a90030f2009-04-01 13:11:26 -05001859 }
Karen Xiec3673462008-12-09 14:15:32 -08001860
1861 if (cdev->ctl(cdev, GET_WR_LEN, &wr_len) < 0 ||
1862 cdev->ctl(cdev, GET_PORTS, &cdata->ports) < 0 ||
Karen Xie2a90030f2009-04-01 13:11:26 -05001863 cdev->ctl(cdev, GET_RX_PAGE_INFO, &rx_page_info) < 0) {
1864 cxgb3i_log_warn("t3dev 0x%p, offload up, ioctl failed.\n",
1865 cdev);
Karen Xiec3673462008-12-09 14:15:32 -08001866 goto free_cdata;
Karen Xie2a90030f2009-04-01 13:11:26 -05001867 }
Karen Xiec3673462008-12-09 14:15:32 -08001868
1869 s3_init_wr_tab(wr_len);
1870
Karen Xie2a90030f2009-04-01 13:11:26 -05001871 spin_lock_init(&cdata->lock);
Karen Xiec3673462008-12-09 14:15:32 -08001872 INIT_LIST_HEAD(&cdata->list);
1873 cdata->cdev = cdev;
1874 cdata->client = client;
1875
1876 for (i = 0; i < cdata->ports.nports; i++)
1877 NDEV2CDATA(cdata->ports.lldevs[i]) = cdata;
1878
1879 write_lock(&cdata_rwlock);
1880 list_add_tail(&cdata->list, &cdata_list);
1881 write_unlock(&cdata_rwlock);
1882
Karen Xie2a90030f2009-04-01 13:11:26 -05001883 cxgb3i_log_info("t3dev 0x%p, offload up, added.\n", cdev);
Karen Xiec3673462008-12-09 14:15:32 -08001884 return;
1885
1886free_cdata:
1887 cxgb3i_free_big_mem(cdata);
1888}
1889
1890/**
1891 * cxgb3i_sdev_remove - free the allocated resources for the adapter
1892 * @cdev: t3cdev adapter
1893 */
1894void cxgb3i_sdev_remove(struct t3cdev *cdev)
1895{
1896 struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
1897
Karen Xie2a90030f2009-04-01 13:11:26 -05001898 cxgb3i_log_info("t3dev 0x%p, offload down, remove.\n", cdev);
1899
Karen Xiec3673462008-12-09 14:15:32 -08001900 write_lock(&cdata_rwlock);
1901 list_del(&cdata->list);
1902 write_unlock(&cdata_rwlock);
1903
1904 sdev_data_cleanup(cdata);
1905}