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