2 * cxgb3i_offload.c: Chelsio S3xx iscsi offloaded tcp connection management
4 * Copyright (C) 2003-2008 Chelsio Communications. All rights reserved.
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.
11 * Written by: Dimitris Michailidis (dm@chelsio.com)
12 * Karen Xie (kxie@chelsio.com)
15 #include <linux/if_vlan.h>
16 #include <linux/version.h>
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"
25 #ifdef __DEBUG_C3CN_CONN__
26 #define c3cn_conn_debug cxgb3i_log_info
28 #define c3cn_conn_debug(fmt...)
31 #ifdef __DEBUG_C3CN_TX__
32 #define c3cn_tx_debug cxgb3i_log_debug
34 #define c3cn_tx_debug(fmt...)
37 #ifdef __DEBUG_C3CN_RX__
38 #define c3cn_rx_debug cxgb3i_log_debug
40 #define c3cn_rx_debug(fmt...)
44 * module parameters releated to offloaded iscsi connection
46 static int cxgb3_rcv_win = 256 * 1024;
47 module_param(cxgb3_rcv_win, int, 0644);
48 MODULE_PARM_DESC(cxgb3_rcv_win, "TCP receive window in bytes (default=256KB)");
50 static int cxgb3_snd_win = 64 * 1024;
51 module_param(cxgb3_snd_win, int, 0644);
52 MODULE_PARM_DESC(cxgb3_snd_win, "TCP send window in bytes (default=64KB)");
54 static int cxgb3_rx_credit_thres = 10 * 1024;
55 module_param(cxgb3_rx_credit_thres, int, 0644);
56 MODULE_PARM_DESC(rx_credit_thres,
57 "RX credits return threshold in bytes (default=10KB)");
59 static unsigned int cxgb3_max_connect = 8 * 1024;
60 module_param(cxgb3_max_connect, uint, 0644);
61 MODULE_PARM_DESC(cxgb3_max_connect, "Max. # of connections (default=8092)");
63 static unsigned int cxgb3_sport_base = 20000;
64 module_param(cxgb3_sport_base, uint, 0644);
65 MODULE_PARM_DESC(cxgb3_sport_base, "starting port number (default=20000)");
68 * cxgb3i tcp connection data(per adapter) list
70 static LIST_HEAD(cdata_list);
71 static DEFINE_RWLOCK(cdata_rwlock);
73 static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion);
74 static void c3cn_release_offload_resources(struct s3_conn *c3cn);
77 * iscsi source port management
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.
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.
86 * If the source port is outside our allocation range, the caller is
87 * responsible for keeping track of their port usage.
89 static int c3cn_get_port(struct s3_conn *c3cn, struct cxgb3i_sdev_data *cdata)
97 if (c3cn->saddr.sin_port != 0) {
98 idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
99 if (idx < 0 || idx >= cxgb3_max_connect)
101 if (!test_and_set_bit(idx, cdata->sport_map))
105 /* the sport_map_next may not be accurate but that is okay, sport_map
107 start = idx = cdata->sport_map_next;
109 if (++idx >= cxgb3_max_connect)
111 if (!(test_and_set_bit(idx, cdata->sport_map))) {
112 c3cn->saddr.sin_port = htons(cxgb3_sport_base + idx);
113 cdata->sport_map_next = idx;
114 c3cn_conn_debug("%s reserve port %u.\n",
116 cxgb3_sport_base + idx);
119 } while (idx != start);
122 return -EADDRNOTAVAIL;
125 static void c3cn_put_port(struct s3_conn *c3cn)
127 struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(c3cn->cdev);
129 if (c3cn->saddr.sin_port) {
130 int idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
132 c3cn->saddr.sin_port = 0;
133 if (idx < 0 || idx >= cxgb3_max_connect)
135 clear_bit(idx, cdata->sport_map);
136 c3cn_conn_debug("%s, release port %u.\n",
137 cdata->cdev->name, cxgb3_sport_base + idx);
141 static inline void c3cn_set_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
143 __set_bit(flag, &c3cn->flags);
144 c3cn_conn_debug("c3cn 0x%p, set %d, s %u, f 0x%lx.\n",
145 c3cn, flag, c3cn->state, c3cn->flags);
148 static inline void c3cn_clear_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
150 __clear_bit(flag, &c3cn->flags);
151 c3cn_conn_debug("c3cn 0x%p, clear %d, s %u, f 0x%lx.\n",
152 c3cn, flag, c3cn->state, c3cn->flags);
155 static inline int c3cn_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
159 return test_bit(flag, &c3cn->flags);
162 static void c3cn_set_state(struct s3_conn *c3cn, int state)
164 c3cn_conn_debug("c3cn 0x%p state -> %u.\n", c3cn, state);
168 static inline void c3cn_hold(struct s3_conn *c3cn)
170 atomic_inc(&c3cn->refcnt);
173 static inline void c3cn_put(struct s3_conn *c3cn)
175 if (atomic_dec_and_test(&c3cn->refcnt)) {
176 c3cn_conn_debug("free c3cn 0x%p, s %u, f 0x%lx.\n",
177 c3cn, c3cn->state, c3cn->flags);
182 static void c3cn_closed(struct s3_conn *c3cn)
184 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
185 c3cn, c3cn->state, c3cn->flags);
188 c3cn_release_offload_resources(c3cn);
189 c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
190 cxgb3i_conn_closing(c3cn);
194 * CPL (Chelsio Protocol Language) defines a message passing interface between
195 * the host driver and T3 asic.
196 * The section below implments CPLs that related to iscsi tcp connection
197 * open/close/abort and data send/receive.
201 * CPL connection active open request: host ->
203 static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
207 while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
212 static unsigned int select_mss(struct s3_conn *c3cn, unsigned int pmtu)
215 struct dst_entry *dst = c3cn->dst_cache;
216 struct t3cdev *cdev = c3cn->cdev;
217 const struct t3c_data *td = T3C_DATA(cdev);
218 u16 advmss = dst_metric(dst, RTAX_ADVMSS);
220 if (advmss > pmtu - 40)
222 if (advmss < td->mtus[0] - 40)
223 advmss = td->mtus[0] - 40;
224 idx = find_best_mtu(td, advmss + 40);
228 static inline int compute_wscale(int win)
231 while (wscale < 14 && (65535<<wscale) < win)
236 static inline unsigned int calc_opt0h(struct s3_conn *c3cn)
238 int wscale = compute_wscale(cxgb3_rcv_win);
239 return V_KEEP_ALIVE(1) |
241 V_WND_SCALE(wscale) |
242 V_MSS_IDX(c3cn->mss_idx);
245 static inline unsigned int calc_opt0l(struct s3_conn *c3cn)
247 return V_ULP_MODE(ULP_MODE_ISCSI) |
248 V_RCV_BUFSIZ(cxgb3_rcv_win>>10);
251 static void make_act_open_req(struct s3_conn *c3cn, struct sk_buff *skb,
252 unsigned int atid, const struct l2t_entry *e)
254 struct cpl_act_open_req *req;
256 c3cn_conn_debug("c3cn 0x%p, atid 0x%x.\n", c3cn, atid);
258 skb->priority = CPL_PRIORITY_SETUP;
259 req = (struct cpl_act_open_req *)__skb_put(skb, sizeof(*req));
260 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
261 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, atid));
262 req->local_port = c3cn->saddr.sin_port;
263 req->peer_port = c3cn->daddr.sin_port;
264 req->local_ip = c3cn->saddr.sin_addr.s_addr;
265 req->peer_ip = c3cn->daddr.sin_addr.s_addr;
266 req->opt0h = htonl(calc_opt0h(c3cn) | V_L2T_IDX(e->idx) |
267 V_TX_CHANNEL(e->smt_idx));
268 req->opt0l = htonl(calc_opt0l(c3cn));
272 static void fail_act_open(struct s3_conn *c3cn, int errno)
274 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
275 c3cn, c3cn->state, c3cn->flags);
280 static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
282 struct s3_conn *c3cn = (struct s3_conn *)skb->sk;
284 c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
287 spin_lock_bh(&c3cn->lock);
288 if (c3cn->state == C3CN_STATE_CONNECTING)
289 fail_act_open(c3cn, EHOSTUNREACH);
290 spin_unlock_bh(&c3cn->lock);
296 * CPL connection close request: host ->
298 * Close a connection by sending a CPL_CLOSE_CON_REQ message and queue it to
299 * the write queue (i.e., after any unsent txt data).
301 static void skb_entail(struct s3_conn *c3cn, struct sk_buff *skb,
304 CXGB3_SKB_CB(skb)->seq = c3cn->write_seq;
305 CXGB3_SKB_CB(skb)->flags = flags;
306 __skb_queue_tail(&c3cn->write_queue, skb);
309 static void send_close_req(struct s3_conn *c3cn)
311 struct sk_buff *skb = c3cn->cpl_close;
312 struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
313 unsigned int tid = c3cn->tid;
315 c3cn_conn_debug("c3cn 0x%p, state 0x%x, flag 0x%lx.\n",
316 c3cn, c3cn->state, c3cn->flags);
318 c3cn->cpl_close = NULL;
320 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
321 req->wr.wr_lo = htonl(V_WR_TID(tid));
322 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
323 req->rsvd = htonl(c3cn->write_seq);
325 skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND);
326 if (c3cn->state != C3CN_STATE_CONNECTING)
327 c3cn_push_tx_frames(c3cn, 1);
331 * CPL connection abort request: host ->
333 * Send an ABORT_REQ message. Makes sure we do not send multiple ABORT_REQs
334 * for the same connection and also that we do not try to send a message
335 * after the connection has closed.
337 static void abort_arp_failure(struct t3cdev *cdev, struct sk_buff *skb)
339 struct cpl_abort_req *req = cplhdr(skb);
341 c3cn_conn_debug("tdev 0x%p.\n", cdev);
343 req->cmd = CPL_ABORT_NO_RST;
344 cxgb3_ofld_send(cdev, skb);
347 static inline void c3cn_purge_write_queue(struct s3_conn *c3cn)
351 while ((skb = __skb_dequeue(&c3cn->write_queue)))
355 static void send_abort_req(struct s3_conn *c3cn)
357 struct sk_buff *skb = c3cn->cpl_abort_req;
358 struct cpl_abort_req *req;
359 unsigned int tid = c3cn->tid;
361 if (unlikely(c3cn->state == C3CN_STATE_ABORTING) || !skb ||
365 c3cn_set_state(c3cn, C3CN_STATE_ABORTING);
367 c3cn_conn_debug("c3cn 0x%p, flag ABORT_RPL + ABORT_SHUT.\n", c3cn);
369 c3cn_set_flag(c3cn, C3CN_ABORT_RPL_PENDING);
371 /* Purge the send queue so we don't send anything after an abort. */
372 c3cn_purge_write_queue(c3cn);
374 c3cn->cpl_abort_req = NULL;
375 req = (struct cpl_abort_req *)skb->head;
377 skb->priority = CPL_PRIORITY_DATA;
378 set_arp_failure_handler(skb, abort_arp_failure);
380 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
381 req->wr.wr_lo = htonl(V_WR_TID(tid));
382 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, tid));
383 req->rsvd0 = htonl(c3cn->snd_nxt);
384 req->rsvd1 = !c3cn_flag(c3cn, C3CN_TX_DATA_SENT);
385 req->cmd = CPL_ABORT_SEND_RST;
387 l2t_send(c3cn->cdev, skb, c3cn->l2t);
391 * CPL connection abort reply: host ->
393 * Send an ABORT_RPL message in response of the ABORT_REQ received.
395 static void send_abort_rpl(struct s3_conn *c3cn, int rst_status)
397 struct sk_buff *skb = c3cn->cpl_abort_rpl;
398 struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
400 c3cn->cpl_abort_rpl = NULL;
402 skb->priority = CPL_PRIORITY_DATA;
403 rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
404 rpl->wr.wr_lo = htonl(V_WR_TID(c3cn->tid));
405 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, c3cn->tid));
406 rpl->cmd = rst_status;
408 cxgb3_ofld_send(c3cn->cdev, skb);
412 * CPL connection rx data ack: host ->
413 * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
416 static u32 send_rx_credits(struct s3_conn *c3cn, u32 credits, u32 dack)
419 struct cpl_rx_data_ack *req;
421 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
425 req = (struct cpl_rx_data_ack *)__skb_put(skb, sizeof(*req));
426 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
427 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, c3cn->tid));
428 req->credit_dack = htonl(dack | V_RX_CREDITS(credits));
429 skb->priority = CPL_PRIORITY_ACK;
430 cxgb3_ofld_send(c3cn->cdev, skb);
435 * CPL connection tx data: host ->
437 * Send iscsi PDU via TX_DATA CPL message. Returns the number of
439 * Each TX_DATA consumes work request credit (wrs), so we need to keep track of
440 * how many we've used so far and how many are pending (i.e., yet ack'ed by T3).
444 * For ULP connections HW may inserts digest bytes into the pdu. Those digest
445 * bytes are not sent by the host but are part of the TCP payload and therefore
446 * consume TCP sequence space.
448 static const unsigned int cxgb3_ulp_extra_len[] = { 0, 4, 4, 8 };
449 static inline unsigned int ulp_extra_len(const struct sk_buff *skb)
451 return cxgb3_ulp_extra_len[skb_ulp_mode(skb) & 3];
454 static unsigned int wrlen __read_mostly;
457 * The number of WRs needed for an skb depends on the number of fragments
458 * in the skb and whether it has any payload in its main body. This maps the
459 * length of the gather list represented by an skb into the # of necessary WRs.
461 * The max. length of an skb is controlled by the max pdu size which is ~16K.
462 * Also, assume the min. fragment length is the sector size (512), then add
463 * extra fragment counts for iscsi bhs and payload padding.
465 #define SKB_WR_LIST_SIZE (16384/512 + 3)
466 static unsigned int skb_wrs[SKB_WR_LIST_SIZE] __read_mostly;
468 static void s3_init_wr_tab(unsigned int wr_len)
472 if (skb_wrs[1]) /* already initialized */
475 for (i = 1; i < SKB_WR_LIST_SIZE; i++) {
476 int sgl_len = (3 * i) / 2 + (i & 1);
479 skb_wrs[i] = (sgl_len <= wr_len
480 ? 1 : 1 + (sgl_len - 2) / (wr_len - 1));
486 static inline void reset_wr_list(struct s3_conn *c3cn)
488 c3cn->wr_pending_head = NULL;
492 * Add a WR to a connections's list of pending WRs. This is a singly-linked
493 * list of sk_buffs operating as a FIFO. The head is kept in wr_pending_head
494 * and the tail in wr_pending_tail.
496 static inline void enqueue_wr(struct s3_conn *c3cn,
499 skb_wr_data(skb) = NULL;
502 * We want to take an extra reference since both us and the driver
503 * need to free the packet before it's really freed. We know there's
504 * just one user currently so we use atomic_set rather than skb_get
505 * to avoid the atomic op.
507 atomic_set(&skb->users, 2);
509 if (!c3cn->wr_pending_head)
510 c3cn->wr_pending_head = skb;
512 skb_wr_data(skb) = skb;
513 c3cn->wr_pending_tail = skb;
516 static inline struct sk_buff *peek_wr(const struct s3_conn *c3cn)
518 return c3cn->wr_pending_head;
521 static inline void free_wr_skb(struct sk_buff *skb)
526 static inline struct sk_buff *dequeue_wr(struct s3_conn *c3cn)
528 struct sk_buff *skb = c3cn->wr_pending_head;
531 /* Don't bother clearing the tail */
532 c3cn->wr_pending_head = skb_wr_data(skb);
533 skb_wr_data(skb) = NULL;
538 static void purge_wr_queue(struct s3_conn *c3cn)
541 while ((skb = dequeue_wr(c3cn)) != NULL)
545 static inline void make_tx_data_wr(struct s3_conn *c3cn, struct sk_buff *skb,
548 struct tx_data_wr *req;
550 skb_reset_transport_header(skb);
551 req = (struct tx_data_wr *)__skb_push(skb, sizeof(*req));
552 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
553 req->wr_lo = htonl(V_WR_TID(c3cn->tid));
554 req->sndseq = htonl(c3cn->snd_nxt);
555 /* len includes the length of any HW ULP additions */
556 req->len = htonl(len);
557 req->param = htonl(V_TX_PORT(c3cn->l2t->smt_idx));
558 /* V_TX_ULP_SUBMODE sets both the mode and submode */
559 req->flags = htonl(V_TX_ULP_SUBMODE(skb_ulp_mode(skb)) |
560 V_TX_SHOVE((skb_peek(&c3cn->write_queue) ? 0 : 1)));
562 if (!c3cn_flag(c3cn, C3CN_TX_DATA_SENT)) {
563 req->flags |= htonl(V_TX_ACK_PAGES(2) | F_TX_INIT |
564 V_TX_CPU_IDX(c3cn->qset));
565 /* Sendbuffer is in units of 32KB. */
566 req->param |= htonl(V_TX_SNDBUF(cxgb3_snd_win >> 15));
567 c3cn_set_flag(c3cn, C3CN_TX_DATA_SENT);
572 * c3cn_push_tx_frames -- start transmit
573 * @c3cn: the offloaded connection
574 * @req_completion: request wr_ack or not
576 * Prepends TX_DATA_WR or CPL_CLOSE_CON_REQ headers to buffers waiting in a
577 * connection's send queue and sends them on to T3. Must be called with the
578 * connection's lock held. Returns the amount of send buffer space that was
579 * freed as a result of sending queued data to T3.
581 static void arp_failure_discard(struct t3cdev *cdev, struct sk_buff *skb)
586 static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion)
591 struct cxgb3i_sdev_data *cdata;
593 if (unlikely(c3cn->state == C3CN_STATE_CONNECTING ||
594 c3cn->state == C3CN_STATE_CLOSE_WAIT_1 ||
595 c3cn->state == C3CN_STATE_ABORTING)) {
596 c3cn_tx_debug("c3cn 0x%p, in closing state %u.\n",
602 cdata = CXGB3_SDEV_DATA(cdev);
604 while (c3cn->wr_avail
605 && (skb = skb_peek(&c3cn->write_queue)) != NULL) {
606 int len = skb->len; /* length before skb_push */
607 int frags = skb_shinfo(skb)->nr_frags + (len != skb->data_len);
608 int wrs_needed = skb_wrs[frags];
610 if (wrs_needed > 1 && len + sizeof(struct tx_data_wr) <= wrlen)
613 WARN_ON(frags >= SKB_WR_LIST_SIZE || wrs_needed < 1);
615 if (c3cn->wr_avail < wrs_needed) {
616 c3cn_tx_debug("c3cn 0x%p, skb len %u/%u, frag %u, "
618 c3cn, skb->len, skb->datalen, frags,
619 wrs_needed, c3cn->wr_avail);
623 __skb_unlink(skb, &c3cn->write_queue);
624 skb->priority = CPL_PRIORITY_DATA;
625 skb->csum = wrs_needed; /* remember this until the WR_ACK */
626 c3cn->wr_avail -= wrs_needed;
627 c3cn->wr_unacked += wrs_needed;
628 enqueue_wr(c3cn, skb);
630 if (likely(CXGB3_SKB_CB(skb)->flags & C3CB_FLAG_NEED_HDR)) {
631 len += ulp_extra_len(skb);
632 make_tx_data_wr(c3cn, skb, len);
633 c3cn->snd_nxt += len;
635 && c3cn->wr_unacked == wrs_needed)
636 || (CXGB3_SKB_CB(skb)->flags & C3CB_FLAG_COMPL)
637 || c3cn->wr_unacked >= c3cn->wr_max / 2) {
638 struct work_request_hdr *wr = cplhdr(skb);
640 wr->wr_hi |= htonl(F_WR_COMPL);
641 c3cn->wr_unacked = 0;
643 CXGB3_SKB_CB(skb)->flags &= ~C3CB_FLAG_NEED_HDR;
646 total_size += skb->truesize;
647 set_arp_failure_handler(skb, arp_failure_discard);
648 l2t_send(cdev, skb, c3cn->l2t);
654 * process_cpl_msg: -> host
655 * Top-level CPL message processing used by most CPL messages that
656 * pertain to connections.
658 static inline void process_cpl_msg(void (*fn)(struct s3_conn *,
660 struct s3_conn *c3cn,
663 spin_lock_bh(&c3cn->lock);
665 spin_unlock_bh(&c3cn->lock);
669 * process_cpl_msg_ref: -> host
670 * Similar to process_cpl_msg() but takes an extra connection reference around
671 * the call to the handler. Should be used if the handler may drop a
672 * connection reference.
674 static inline void process_cpl_msg_ref(void (*fn) (struct s3_conn *,
676 struct s3_conn *c3cn,
680 process_cpl_msg(fn, c3cn, skb);
685 * Process a CPL_ACT_ESTABLISH message: -> host
686 * Updates connection state from an active establish CPL message. Runs with
687 * the connection lock held.
690 static inline void s3_free_atid(struct t3cdev *cdev, unsigned int tid)
692 struct s3_conn *c3cn = cxgb3_free_atid(cdev, tid);
697 static void c3cn_established(struct s3_conn *c3cn, u32 snd_isn,
700 c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
702 c3cn->write_seq = c3cn->snd_nxt = c3cn->snd_una = snd_isn;
705 * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
708 if (cxgb3_rcv_win > (M_RCV_BUFSIZ << 10))
709 c3cn->rcv_wup -= cxgb3_rcv_win - (M_RCV_BUFSIZ << 10);
711 dst_confirm(c3cn->dst_cache);
715 c3cn_set_state(c3cn, C3CN_STATE_ESTABLISHED);
718 static void process_act_establish(struct s3_conn *c3cn, struct sk_buff *skb)
720 struct cpl_act_establish *req = cplhdr(skb);
721 u32 rcv_isn = ntohl(req->rcv_isn); /* real RCV_ISN + 1 */
723 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
724 c3cn, c3cn->state, c3cn->flags);
726 if (unlikely(c3cn->state != C3CN_STATE_CONNECTING))
727 cxgb3i_log_error("TID %u expected SYN_SENT, got EST., s %u\n",
728 c3cn->tid, c3cn->state);
730 c3cn->copied_seq = c3cn->rcv_wup = c3cn->rcv_nxt = rcv_isn;
731 c3cn_established(c3cn, ntohl(req->snd_isn), ntohs(req->tcp_opt));
735 if (unlikely(c3cn_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED)))
736 /* upper layer has requested closing */
737 send_abort_req(c3cn);
738 else if (c3cn_push_tx_frames(c3cn, 1))
739 cxgb3i_conn_tx_open(c3cn);
742 static int do_act_establish(struct t3cdev *cdev, struct sk_buff *skb,
745 struct cpl_act_establish *req = cplhdr(skb);
746 unsigned int tid = GET_TID(req);
747 unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
748 struct s3_conn *c3cn = ctx;
749 struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
751 c3cn_conn_debug("rcv, tid 0x%x, c3cn 0x%p, s %u, f 0x%lx.\n",
752 tid, c3cn, c3cn->state, c3cn->flags);
756 cxgb3_insert_tid(cdata->cdev, cdata->client, c3cn, tid);
757 s3_free_atid(cdev, atid);
759 c3cn->qset = G_QNUM(ntohl(skb->csum));
761 process_cpl_msg(process_act_establish, c3cn, skb);
766 * Process a CPL_ACT_OPEN_RPL message: -> host
767 * Handle active open failures.
769 static int act_open_rpl_status_to_errno(int status)
772 case CPL_ERR_CONN_RESET:
774 case CPL_ERR_ARP_MISS:
776 case CPL_ERR_CONN_TIMEDOUT:
778 case CPL_ERR_TCAM_FULL:
780 case CPL_ERR_CONN_EXIST:
781 cxgb3i_log_error("ACTIVE_OPEN_RPL: 4-tuple in use\n");
788 static void act_open_retry_timer(unsigned long data)
791 struct s3_conn *c3cn = (struct s3_conn *)data;
793 c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
795 spin_lock_bh(&c3cn->lock);
796 skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_ATOMIC);
798 fail_act_open(c3cn, ENOMEM);
800 skb->sk = (struct sock *)c3cn;
801 set_arp_failure_handler(skb, act_open_req_arp_failure);
802 make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
803 l2t_send(c3cn->cdev, skb, c3cn->l2t);
805 spin_unlock_bh(&c3cn->lock);
809 static void process_act_open_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
811 struct cpl_act_open_rpl *rpl = cplhdr(skb);
813 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
814 c3cn, c3cn->state, c3cn->flags);
816 if (rpl->status == CPL_ERR_CONN_EXIST &&
817 c3cn->retry_timer.function != act_open_retry_timer) {
818 c3cn->retry_timer.function = act_open_retry_timer;
819 if (!mod_timer(&c3cn->retry_timer, jiffies + HZ / 2))
822 fail_act_open(c3cn, act_open_rpl_status_to_errno(rpl->status));
826 static int do_act_open_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
828 struct s3_conn *c3cn = ctx;
829 struct cpl_act_open_rpl *rpl = cplhdr(skb);
831 c3cn_conn_debug("rcv, status 0x%x, c3cn 0x%p, s %u, f 0x%lx.\n",
832 rpl->status, c3cn, c3cn->state, c3cn->flags);
834 if (rpl->status != CPL_ERR_TCAM_FULL &&
835 rpl->status != CPL_ERR_CONN_EXIST &&
836 rpl->status != CPL_ERR_ARP_MISS)
837 cxgb3_queue_tid_release(cdev, GET_TID(rpl));
839 process_cpl_msg_ref(process_act_open_rpl, c3cn, skb);
844 * Process PEER_CLOSE CPL messages: -> host
847 static void process_peer_close(struct s3_conn *c3cn, struct sk_buff *skb)
849 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
850 c3cn, c3cn->state, c3cn->flags);
852 if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING))
855 switch (c3cn->state) {
856 case C3CN_STATE_ESTABLISHED:
857 c3cn_set_state(c3cn, C3CN_STATE_PASSIVE_CLOSE);
859 case C3CN_STATE_ACTIVE_CLOSE:
860 c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_2);
862 case C3CN_STATE_CLOSE_WAIT_1:
865 case C3CN_STATE_ABORTING:
868 cxgb3i_log_error("%s: peer close, TID %u in bad state %u\n",
869 c3cn->cdev->name, c3cn->tid, c3cn->state);
872 cxgb3i_conn_closing(c3cn);
877 static int do_peer_close(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
879 struct s3_conn *c3cn = ctx;
881 c3cn_conn_debug("rcv, c3cn 0x%p, s %u, f 0x%lx.\n",
882 c3cn, c3cn->state, c3cn->flags);
883 process_cpl_msg_ref(process_peer_close, c3cn, skb);
888 * Process CLOSE_CONN_RPL CPL message: -> host
889 * Process a peer ACK to our FIN.
891 static void process_close_con_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
893 struct cpl_close_con_rpl *rpl = cplhdr(skb);
895 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
896 c3cn, c3cn->state, c3cn->flags);
898 c3cn->snd_una = ntohl(rpl->snd_nxt) - 1; /* exclude FIN */
900 if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING))
903 switch (c3cn->state) {
904 case C3CN_STATE_ACTIVE_CLOSE:
905 c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_1);
907 case C3CN_STATE_CLOSE_WAIT_1:
908 case C3CN_STATE_CLOSE_WAIT_2:
911 case C3CN_STATE_ABORTING:
914 cxgb3i_log_error("%s: close_rpl, TID %u in bad state %u\n",
915 c3cn->cdev->name, c3cn->tid, c3cn->state);
922 static int do_close_con_rpl(struct t3cdev *cdev, struct sk_buff *skb,
925 struct s3_conn *c3cn = ctx;
927 c3cn_conn_debug("rcv, c3cn 0x%p, s %u, f 0x%lx.\n",
928 c3cn, c3cn->state, c3cn->flags);
930 process_cpl_msg_ref(process_close_con_rpl, c3cn, skb);
935 * Process ABORT_REQ_RSS CPL message: -> host
936 * Process abort requests. If we are waiting for an ABORT_RPL we ignore this
937 * request except that we need to reply to it.
940 static int abort_status_to_errno(struct s3_conn *c3cn, int abort_reason,
943 switch (abort_reason) {
944 case CPL_ERR_BAD_SYN: /* fall through */
945 case CPL_ERR_CONN_RESET:
946 return c3cn->state > C3CN_STATE_ESTABLISHED ?
948 case CPL_ERR_XMIT_TIMEDOUT:
949 case CPL_ERR_PERSIST_TIMEDOUT:
950 case CPL_ERR_FINWAIT2_TIMEDOUT:
951 case CPL_ERR_KEEPALIVE_TIMEDOUT:
958 static void process_abort_req(struct s3_conn *c3cn, struct sk_buff *skb)
960 int rst_status = CPL_ABORT_NO_RST;
961 const struct cpl_abort_req_rss *req = cplhdr(skb);
963 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
964 c3cn, c3cn->state, c3cn->flags);
966 if (!c3cn_flag(c3cn, C3CN_ABORT_REQ_RCVD)) {
967 c3cn_set_flag(c3cn, C3CN_ABORT_REQ_RCVD);
968 c3cn_set_state(c3cn, C3CN_STATE_ABORTING);
973 c3cn_clear_flag(c3cn, C3CN_ABORT_REQ_RCVD);
974 send_abort_rpl(c3cn, rst_status);
976 if (!c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING)) {
978 abort_status_to_errno(c3cn, req->status, &rst_status);
983 static int do_abort_req(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
985 const struct cpl_abort_req_rss *req = cplhdr(skb);
986 struct s3_conn *c3cn = ctx;
988 c3cn_conn_debug("rcv, c3cn 0x%p, s 0x%x, f 0x%lx.\n",
989 c3cn, c3cn->state, c3cn->flags);
991 if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
992 req->status == CPL_ERR_PERSIST_NEG_ADVICE) {
997 process_cpl_msg_ref(process_abort_req, c3cn, skb);
1002 * Process ABORT_RPL_RSS CPL message: -> host
1003 * Process abort replies. We only process these messages if we anticipate
1004 * them as the coordination between SW and HW in this area is somewhat lacking
1005 * and sometimes we get ABORT_RPLs after we are done with the connection that
1006 * originated the ABORT_REQ.
1008 static void process_abort_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
1010 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1011 c3cn, c3cn->state, c3cn->flags);
1013 if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING)) {
1014 if (!c3cn_flag(c3cn, C3CN_ABORT_RPL_RCVD))
1015 c3cn_set_flag(c3cn, C3CN_ABORT_RPL_RCVD);
1017 c3cn_clear_flag(c3cn, C3CN_ABORT_RPL_RCVD);
1018 c3cn_clear_flag(c3cn, C3CN_ABORT_RPL_PENDING);
1019 if (c3cn_flag(c3cn, C3CN_ABORT_REQ_RCVD))
1020 cxgb3i_log_error("%s tid %u, ABORT_RPL_RSS\n",
1021 c3cn->cdev->name, c3cn->tid);
1028 static int do_abort_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
1030 struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1031 struct s3_conn *c3cn = ctx;
1033 c3cn_conn_debug("rcv, status 0x%x, c3cn 0x%p, s %u, 0x%lx.\n",
1034 rpl->status, c3cn, c3cn ? c3cn->state : 0,
1035 c3cn ? c3cn->flags : 0UL);
1038 * Ignore replies to post-close aborts indicating that the abort was
1039 * requested too late. These connections are terminated when we get
1040 * PEER_CLOSE or CLOSE_CON_RPL and by the time the abort_rpl_rss
1041 * arrives the TID is either no longer used or it has been recycled.
1043 if (rpl->status == CPL_ERR_ABORT_FAILED)
1047 * Sometimes we've already closed the connection, e.g., a post-close
1048 * abort races with ABORT_REQ_RSS, the latter frees the connection
1049 * expecting the ABORT_REQ will fail with CPL_ERR_ABORT_FAILED,
1050 * but FW turns the ABORT_REQ into a regular one and so we get
1051 * ABORT_RPL_RSS with status 0 and no connection.
1056 process_cpl_msg_ref(process_abort_rpl, c3cn, skb);
1065 * Process RX_ISCSI_HDR CPL message: -> host
1066 * Handle received PDUs, the payload could be DDP'ed. If not, the payload
1067 * follow after the bhs.
1069 static void process_rx_iscsi_hdr(struct s3_conn *c3cn, struct sk_buff *skb)
1071 struct cpl_iscsi_hdr *hdr_cpl = cplhdr(skb);
1072 struct cpl_iscsi_hdr_norss data_cpl;
1073 struct cpl_rx_data_ddp_norss ddp_cpl;
1074 unsigned int hdr_len, data_len, status;
1078 if (unlikely(c3cn->state >= C3CN_STATE_PASSIVE_CLOSE)) {
1079 if (c3cn->state != C3CN_STATE_ABORTING)
1080 send_abort_req(c3cn);
1085 CXGB3_SKB_CB(skb)->seq = ntohl(hdr_cpl->seq);
1086 CXGB3_SKB_CB(skb)->flags = 0;
1088 skb_reset_transport_header(skb);
1089 __skb_pull(skb, sizeof(struct cpl_iscsi_hdr));
1091 len = hdr_len = ntohs(hdr_cpl->len);
1092 /* msg coalesce is off or not enough data received */
1093 if (skb->len <= hdr_len) {
1094 cxgb3i_log_error("%s: TID %u, ISCSI_HDR, skb len %u < %u.\n",
1095 c3cn->cdev->name, c3cn->tid,
1100 err = skb_copy_bits(skb, skb->len - sizeof(ddp_cpl), &ddp_cpl,
1105 skb_ulp_mode(skb) = ULP2_FLAG_DATA_READY;
1106 skb_ulp_pdulen(skb) = ntohs(ddp_cpl.len);
1107 skb_ulp_ddigest(skb) = ntohl(ddp_cpl.ulp_crc);
1108 status = ntohl(ddp_cpl.ddp_status);
1110 c3cn_rx_debug("rx skb 0x%p, len %u, pdulen %u, ddp status 0x%x.\n",
1111 skb, skb->len, skb_ulp_pdulen(skb), status);
1113 if (status & (1 << RX_DDP_STATUS_HCRC_SHIFT))
1114 skb_ulp_mode(skb) |= ULP2_FLAG_HCRC_ERROR;
1115 if (status & (1 << RX_DDP_STATUS_DCRC_SHIFT))
1116 skb_ulp_mode(skb) |= ULP2_FLAG_DCRC_ERROR;
1117 if (status & (1 << RX_DDP_STATUS_PAD_SHIFT))
1118 skb_ulp_mode(skb) |= ULP2_FLAG_PAD_ERROR;
1120 if (skb->len > (hdr_len + sizeof(ddp_cpl))) {
1121 err = skb_copy_bits(skb, hdr_len, &data_cpl, sizeof(data_cpl));
1124 data_len = ntohs(data_cpl.len);
1125 len += sizeof(data_cpl) + data_len;
1126 } else if (status & (1 << RX_DDP_STATUS_DDP_SHIFT))
1127 skb_ulp_mode(skb) |= ULP2_FLAG_DATA_DDPED;
1129 c3cn->rcv_nxt = ntohl(ddp_cpl.seq) + skb_ulp_pdulen(skb);
1130 __pskb_trim(skb, len);
1131 __skb_queue_tail(&c3cn->receive_queue, skb);
1132 cxgb3i_conn_pdu_ready(c3cn);
1137 send_abort_req(c3cn);
1141 static int do_iscsi_hdr(struct t3cdev *t3dev, struct sk_buff *skb, void *ctx)
1143 struct s3_conn *c3cn = ctx;
1145 process_cpl_msg(process_rx_iscsi_hdr, c3cn, skb);
1150 * Process TX_DATA_ACK CPL messages: -> host
1151 * Process an acknowledgment of WR completion. Advance snd_una and send the
1152 * next batch of work requests from the write queue.
1154 static void process_wr_ack(struct s3_conn *c3cn, struct sk_buff *skb)
1156 struct cpl_wr_ack *hdr = cplhdr(skb);
1157 unsigned int credits = ntohs(hdr->credits);
1158 u32 snd_una = ntohl(hdr->snd_una);
1160 c3cn->wr_avail += credits;
1161 if (c3cn->wr_unacked > c3cn->wr_max - c3cn->wr_avail)
1162 c3cn->wr_unacked = c3cn->wr_max - c3cn->wr_avail;
1165 struct sk_buff *p = peek_wr(c3cn);
1168 cxgb3i_log_error("%u WR_ACK credits for TID %u with "
1169 "nothing pending, state %u\n",
1170 credits, c3cn->tid, c3cn->state);
1173 if (unlikely(credits < p->csum)) {
1183 if (unlikely(before(snd_una, c3cn->snd_una)))
1186 if (c3cn->snd_una != snd_una) {
1187 c3cn->snd_una = snd_una;
1188 dst_confirm(c3cn->dst_cache);
1191 if (skb_queue_len(&c3cn->write_queue) && c3cn_push_tx_frames(c3cn, 0))
1192 cxgb3i_conn_tx_open(c3cn);
1197 static int do_wr_ack(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
1199 struct s3_conn *c3cn = ctx;
1201 process_cpl_msg(process_wr_ack, c3cn, skb);
1206 * for each connection, pre-allocate skbs needed for close/abort requests. So
1207 * that we can service the request right away.
1209 static void c3cn_free_cpl_skbs(struct s3_conn *c3cn)
1211 if (c3cn->cpl_close)
1212 kfree_skb(c3cn->cpl_close);
1213 if (c3cn->cpl_abort_req)
1214 kfree_skb(c3cn->cpl_abort_req);
1215 if (c3cn->cpl_abort_rpl)
1216 kfree_skb(c3cn->cpl_abort_rpl);
1219 static int c3cn_alloc_cpl_skbs(struct s3_conn *c3cn)
1221 c3cn->cpl_close = alloc_skb(sizeof(struct cpl_close_con_req),
1223 if (!c3cn->cpl_close)
1225 skb_put(c3cn->cpl_close, sizeof(struct cpl_close_con_req));
1227 c3cn->cpl_abort_req = alloc_skb(sizeof(struct cpl_abort_req),
1229 if (!c3cn->cpl_abort_req)
1231 skb_put(c3cn->cpl_abort_req, sizeof(struct cpl_abort_req));
1233 c3cn->cpl_abort_rpl = alloc_skb(sizeof(struct cpl_abort_rpl),
1235 if (!c3cn->cpl_abort_rpl)
1237 skb_put(c3cn->cpl_abort_rpl, sizeof(struct cpl_abort_rpl));
1242 c3cn_free_cpl_skbs(c3cn);
1247 * c3cn_release_offload_resources - release offload resource
1248 * @c3cn: the offloaded iscsi tcp connection.
1249 * Release resources held by an offload connection (TID, L2T entry, etc.)
1251 static void c3cn_release_offload_resources(struct s3_conn *c3cn)
1253 struct t3cdev *cdev = c3cn->cdev;
1254 unsigned int tid = c3cn->tid;
1261 c3cn_free_cpl_skbs(c3cn);
1263 if (c3cn->wr_avail != c3cn->wr_max) {
1264 purge_wr_queue(c3cn);
1265 reset_wr_list(c3cn);
1269 l2t_release(L2DATA(cdev), c3cn->l2t);
1273 if (c3cn->state == C3CN_STATE_CONNECTING) /* we have ATID */
1274 s3_free_atid(cdev, tid);
1275 else { /* we have TID */
1276 cxgb3_remove_tid(cdev, (void *)c3cn, tid);
1284 * cxgb3i_c3cn_create - allocate and initialize an s3_conn structure
1285 * returns the s3_conn structure allocated.
1287 struct s3_conn *cxgb3i_c3cn_create(void)
1289 struct s3_conn *c3cn;
1291 c3cn = kzalloc(sizeof(*c3cn), GFP_KERNEL);
1295 /* pre-allocate close/abort cpl, so we don't need to wait for memory
1296 when close/abort is requested. */
1297 if (c3cn_alloc_cpl_skbs(c3cn) < 0)
1300 c3cn_conn_debug("alloc c3cn 0x%p.\n", c3cn);
1303 spin_lock_init(&c3cn->lock);
1304 atomic_set(&c3cn->refcnt, 1);
1305 skb_queue_head_init(&c3cn->receive_queue);
1306 skb_queue_head_init(&c3cn->write_queue);
1307 setup_timer(&c3cn->retry_timer, NULL, (unsigned long)c3cn);
1308 rwlock_init(&c3cn->callback_lock);
1317 static void c3cn_active_close(struct s3_conn *c3cn)
1322 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1323 c3cn, c3cn->state, c3cn->flags);
1325 dst_confirm(c3cn->dst_cache);
1328 spin_lock_bh(&c3cn->lock);
1330 data_lost = skb_queue_len(&c3cn->receive_queue);
1331 __skb_queue_purge(&c3cn->receive_queue);
1333 switch (c3cn->state) {
1334 case C3CN_STATE_CLOSED:
1335 case C3CN_STATE_ACTIVE_CLOSE:
1336 case C3CN_STATE_CLOSE_WAIT_1:
1337 case C3CN_STATE_CLOSE_WAIT_2:
1338 case C3CN_STATE_ABORTING:
1339 /* nothing need to be done */
1341 case C3CN_STATE_CONNECTING:
1342 /* defer until cpl_act_open_rpl or cpl_act_establish */
1343 c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
1345 case C3CN_STATE_ESTABLISHED:
1347 c3cn_set_state(c3cn, C3CN_STATE_ACTIVE_CLOSE);
1349 case C3CN_STATE_PASSIVE_CLOSE:
1351 c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_2);
1357 /* Unread data was tossed, zap the connection. */
1358 send_abort_req(c3cn);
1360 send_close_req(c3cn);
1363 spin_unlock_bh(&c3cn->lock);
1368 * cxgb3i_c3cn_release - close and release an iscsi tcp connection and any
1370 * @c3cn: the iscsi tcp connection
1372 void cxgb3i_c3cn_release(struct s3_conn *c3cn)
1374 c3cn_conn_debug("c3cn 0x%p, s %u, f 0x%lx.\n",
1375 c3cn, c3cn->state, c3cn->flags);
1376 if (likely(c3cn->state != C3CN_STATE_CONNECTING))
1377 c3cn_active_close(c3cn);
1379 c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
1383 static int is_cxgb3_dev(struct net_device *dev)
1385 struct cxgb3i_sdev_data *cdata;
1387 write_lock(&cdata_rwlock);
1388 list_for_each_entry(cdata, &cdata_list, list) {
1389 struct adap_ports *ports = &cdata->ports;
1392 for (i = 0; i < ports->nports; i++)
1393 if (dev == ports->lldevs[i]) {
1394 write_unlock(&cdata_rwlock);
1398 write_unlock(&cdata_rwlock);
1403 * cxgb3_egress_dev - return the cxgb3 egress device
1404 * @root_dev: the root device anchoring the search
1405 * @c3cn: the connection used to determine egress port in bonding mode
1406 * @context: in bonding mode, indicates a connection set up or failover
1408 * Return egress device or NULL if the egress device isn't one of our ports.
1410 static struct net_device *cxgb3_egress_dev(struct net_device *root_dev,
1411 struct s3_conn *c3cn,
1415 if (root_dev->priv_flags & IFF_802_1Q_VLAN)
1416 root_dev = vlan_dev_real_dev(root_dev);
1417 else if (is_cxgb3_dev(root_dev))
1425 static struct rtable *find_route(__be32 saddr, __be32 daddr,
1426 __be16 sport, __be16 dport)
1436 .proto = IPPROTO_TCP,
1440 .dport = dport } } };
1442 if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
1448 * Assign offload parameters to some connection fields.
1450 static void init_offload_conn(struct s3_conn *c3cn,
1451 struct t3cdev *cdev,
1452 struct dst_entry *dst)
1454 BUG_ON(c3cn->cdev != cdev);
1455 c3cn->wr_max = c3cn->wr_avail = T3C_DATA(cdev)->max_wrs;
1456 c3cn->wr_unacked = 0;
1457 c3cn->mss_idx = select_mss(c3cn, dst_mtu(dst));
1459 reset_wr_list(c3cn);
1462 static int initiate_act_open(struct s3_conn *c3cn, struct net_device *dev)
1464 struct cxgb3i_sdev_data *cdata = NDEV2CDATA(dev);
1465 struct t3cdev *cdev = cdata->cdev;
1466 struct dst_entry *dst = c3cn->dst_cache;
1467 struct sk_buff *skb;
1469 c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1470 c3cn, c3cn->state, c3cn->flags);
1472 * Initialize connection data. Note that the flags and ULP mode are
1473 * initialized higher up ...
1477 c3cn->tid = cxgb3_alloc_atid(cdev, cdata->client, c3cn);
1482 c3cn->l2t = t3_l2t_get(cdev, dst->neighbour, dev);
1486 skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_KERNEL);
1490 skb->sk = (struct sock *)c3cn;
1491 set_arp_failure_handler(skb, act_open_req_arp_failure);
1495 init_offload_conn(c3cn, cdev, dst);
1498 make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
1499 l2t_send(cdev, skb, c3cn->l2t);
1503 l2t_release(L2DATA(cdev), c3cn->l2t);
1505 s3_free_atid(cdev, c3cn->tid);
1513 * cxgb3i_c3cn_connect - initiates an iscsi tcp connection to a given address
1514 * @c3cn: the iscsi tcp connection
1515 * @usin: destination address
1517 * return 0 if active open request is sent, < 0 otherwise.
1519 int cxgb3i_c3cn_connect(struct s3_conn *c3cn, struct sockaddr_in *usin)
1522 struct net_device *dev;
1523 struct cxgb3i_sdev_data *cdata;
1524 struct t3cdev *cdev;
1528 if (usin->sin_family != AF_INET)
1529 return -EAFNOSUPPORT;
1531 c3cn->daddr.sin_port = usin->sin_port;
1532 c3cn->daddr.sin_addr.s_addr = usin->sin_addr.s_addr;
1534 rt = find_route(c3cn->saddr.sin_addr.s_addr,
1535 c3cn->daddr.sin_addr.s_addr,
1536 c3cn->saddr.sin_port,
1537 c3cn->daddr.sin_port);
1539 c3cn_conn_debug("NO route to 0x%x, port %u.\n",
1540 c3cn->daddr.sin_addr.s_addr,
1541 ntohs(c3cn->daddr.sin_port));
1542 return -ENETUNREACH;
1545 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
1546 c3cn_conn_debug("multi-cast route to 0x%x, port %u.\n",
1547 c3cn->daddr.sin_addr.s_addr,
1548 ntohs(c3cn->daddr.sin_port));
1550 return -ENETUNREACH;
1553 if (!c3cn->saddr.sin_addr.s_addr)
1554 c3cn->saddr.sin_addr.s_addr = rt->rt_src;
1556 /* now commit destination to connection */
1557 c3cn->dst_cache = &rt->u.dst;
1559 /* try to establish an offloaded connection */
1560 dev = cxgb3_egress_dev(c3cn->dst_cache->dev, c3cn, 0);
1562 c3cn_conn_debug("c3cn 0x%p, egress dev NULL.\n", c3cn);
1563 return -ENETUNREACH;
1565 cdata = NDEV2CDATA(dev);
1568 /* get a source port if one hasn't been provided */
1569 err = c3cn_get_port(c3cn, cdata);
1573 c3cn_conn_debug("c3cn 0x%p get port %u.\n",
1574 c3cn, ntohs(c3cn->saddr.sin_port));
1576 sipv4 = cxgb3i_get_private_ipv4addr(dev);
1578 c3cn_conn_debug("c3cn 0x%p, iscsi ip not configured.\n", c3cn);
1579 sipv4 = c3cn->saddr.sin_addr.s_addr;
1580 cxgb3i_set_private_ipv4addr(dev, sipv4);
1582 c3cn->saddr.sin_addr.s_addr = sipv4;
1584 c3cn_conn_debug("c3cn 0x%p, %u.%u.%u.%u,%u-%u.%u.%u.%u,%u SYN_SENT.\n",
1585 c3cn, NIPQUAD(c3cn->saddr.sin_addr.s_addr),
1586 ntohs(c3cn->saddr.sin_port),
1587 NIPQUAD(c3cn->daddr.sin_addr.s_addr),
1588 ntohs(c3cn->daddr.sin_port));
1590 c3cn_set_state(c3cn, C3CN_STATE_CONNECTING);
1591 if (!initiate_act_open(c3cn, dev))
1595 * If we get here, we don't have an offload connection so simply
1601 * This trashes the connection and releases the local port,
1604 c3cn_conn_debug("c3cn 0x%p -> CLOSED.\n", c3cn);
1605 c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
1607 c3cn_put_port(c3cn);
1608 c3cn->daddr.sin_port = 0;
1613 * cxgb3i_c3cn_rx_credits - ack received tcp data.
1614 * @c3cn: iscsi tcp connection
1615 * @copied: # of bytes processed
1617 * Called after some received data has been read. It returns RX credits
1618 * to the HW for the amount of data processed.
1620 void cxgb3i_c3cn_rx_credits(struct s3_conn *c3cn, int copied)
1622 struct t3cdev *cdev;
1624 u32 credits, dack = 0;
1626 if (c3cn->state != C3CN_STATE_ESTABLISHED)
1629 credits = c3cn->copied_seq - c3cn->rcv_wup;
1630 if (unlikely(!credits))
1635 if (unlikely(cxgb3_rx_credit_thres == 0))
1638 dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
1641 * For coalescing to work effectively ensure the receive window has
1642 * at least 16KB left.
1644 must_send = credits + 16384 >= cxgb3_rcv_win;
1646 if (must_send || credits >= cxgb3_rx_credit_thres)
1647 c3cn->rcv_wup += send_rx_credits(c3cn, credits, dack);
1651 * cxgb3i_c3cn_send_pdus - send the skbs containing iscsi pdus
1652 * @c3cn: iscsi tcp connection
1653 * @skb: skb contains the iscsi pdu
1655 * Add a list of skbs to a connection send queue. The skbs must comply with
1656 * the max size limit of the device and have a headroom of at least
1657 * TX_HEADER_LEN bytes.
1658 * Return # of bytes queued.
1660 int cxgb3i_c3cn_send_pdus(struct s3_conn *c3cn, struct sk_buff *skb)
1662 struct sk_buff *next;
1663 int err, copied = 0;
1665 spin_lock_bh(&c3cn->lock);
1667 if (c3cn->state != C3CN_STATE_ESTABLISHED) {
1668 c3cn_tx_debug("c3cn 0x%p, not in est. state %u.\n",
1676 c3cn_tx_debug("c3cn 0x%p, err %d.\n", c3cn, c3cn->err);
1681 int frags = skb_shinfo(skb)->nr_frags +
1682 (skb->len != skb->data_len);
1684 if (unlikely(skb_headroom(skb) < TX_HEADER_LEN)) {
1685 c3cn_tx_debug("c3cn 0x%p, skb head.\n", c3cn);
1690 if (frags >= SKB_WR_LIST_SIZE) {
1691 cxgb3i_log_error("c3cn 0x%p, tx frags %d, len %u,%u.\n",
1692 c3cn, skb_shinfo(skb)->nr_frags,
1693 skb->len, skb->data_len);
1700 skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND | C3CB_FLAG_NEED_HDR);
1702 c3cn->write_seq += skb->len + ulp_extra_len(skb);
1706 if (likely(skb_queue_len(&c3cn->write_queue)))
1707 c3cn_push_tx_frames(c3cn, 1);
1708 spin_unlock_bh(&c3cn->lock);
1712 if (copied == 0 && err == -EPIPE)
1713 copied = c3cn->err ? c3cn->err : -EPIPE;
1717 static void sdev_data_cleanup(struct cxgb3i_sdev_data *cdata)
1719 struct adap_ports *ports = &cdata->ports;
1722 for (i = 0; i < ports->nports; i++)
1723 NDEV2CDATA(ports->lldevs[i]) = NULL;
1724 cxgb3i_free_big_mem(cdata);
1727 void cxgb3i_sdev_cleanup(void)
1729 struct cxgb3i_sdev_data *cdata;
1731 write_lock(&cdata_rwlock);
1732 list_for_each_entry(cdata, &cdata_list, list) {
1733 list_del(&cdata->list);
1734 sdev_data_cleanup(cdata);
1736 write_unlock(&cdata_rwlock);
1739 int cxgb3i_sdev_init(cxgb3_cpl_handler_func *cpl_handlers)
1741 cpl_handlers[CPL_ACT_ESTABLISH] = do_act_establish;
1742 cpl_handlers[CPL_ACT_OPEN_RPL] = do_act_open_rpl;
1743 cpl_handlers[CPL_PEER_CLOSE] = do_peer_close;
1744 cpl_handlers[CPL_ABORT_REQ_RSS] = do_abort_req;
1745 cpl_handlers[CPL_ABORT_RPL_RSS] = do_abort_rpl;
1746 cpl_handlers[CPL_CLOSE_CON_RPL] = do_close_con_rpl;
1747 cpl_handlers[CPL_TX_DMA_ACK] = do_wr_ack;
1748 cpl_handlers[CPL_ISCSI_HDR] = do_iscsi_hdr;
1750 if (cxgb3_max_connect > CXGB3I_MAX_CONN)
1751 cxgb3_max_connect = CXGB3I_MAX_CONN;
1756 * cxgb3i_sdev_add - allocate and initialize resources for each adapter found
1757 * @cdev: t3cdev adapter
1758 * @client: cxgb3 driver client
1760 void cxgb3i_sdev_add(struct t3cdev *cdev, struct cxgb3_client *client)
1762 struct cxgb3i_sdev_data *cdata;
1763 struct ofld_page_info rx_page_info;
1764 unsigned int wr_len;
1765 int mapsize = DIV_ROUND_UP(cxgb3_max_connect,
1766 8 * sizeof(unsigned long));
1769 cdata = cxgb3i_alloc_big_mem(sizeof(*cdata) + mapsize, GFP_KERNEL);
1773 if (cdev->ctl(cdev, GET_WR_LEN, &wr_len) < 0 ||
1774 cdev->ctl(cdev, GET_PORTS, &cdata->ports) < 0 ||
1775 cdev->ctl(cdev, GET_RX_PAGE_INFO, &rx_page_info) < 0)
1778 s3_init_wr_tab(wr_len);
1780 INIT_LIST_HEAD(&cdata->list);
1782 cdata->client = client;
1784 for (i = 0; i < cdata->ports.nports; i++)
1785 NDEV2CDATA(cdata->ports.lldevs[i]) = cdata;
1787 write_lock(&cdata_rwlock);
1788 list_add_tail(&cdata->list, &cdata_list);
1789 write_unlock(&cdata_rwlock);
1794 cxgb3i_free_big_mem(cdata);
1798 * cxgb3i_sdev_remove - free the allocated resources for the adapter
1799 * @cdev: t3cdev adapter
1801 void cxgb3i_sdev_remove(struct t3cdev *cdev)
1803 struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
1805 write_lock(&cdata_rwlock);
1806 list_del(&cdata->list);
1807 write_unlock(&cdata_rwlock);
1809 sdev_data_cleanup(cdata);