]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/cxgb3i/cxgb3i_offload.c
sata_mv: shorten register names
[linux-2.6-omap-h63xx.git] / drivers / scsi / cxgb3i / cxgb3i_offload.c
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  */
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
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
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
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
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
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
168 static inline void c3cn_hold(struct s3_conn *c3cn)
169 {
170         atomic_inc(&c3cn->refcnt);
171 }
172
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
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  */
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
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
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
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
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
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
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
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  */
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
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  */
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
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
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  */
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  */
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 };
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
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
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  */
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
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
525 static inline struct sk_buff *peek_wr(const struct s3_conn *c3cn)
526 {
527         return c3cn->wr_pending_head;
528 }
529
530 static inline void free_wr_skb(struct sk_buff *skb)
531 {
532         kfree_skb(skb);
533 }
534
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
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
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  */
591 static void arp_failure_discard(struct t3cdev *cdev, struct sk_buff *skb)
592 {
593         kfree_skb(skb);
594 }
595
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  */
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  */
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
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
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
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
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  */
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
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
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
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  */
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
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  */
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
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
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
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
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  */
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
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  */
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
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  */
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
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
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  */
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
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  */
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  */
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
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
1421  * @c3cn: the iscsi tcp connection
1422  */
1423 void cxgb3i_c3cn_release(struct s3_conn *c3cn)
1424 {
1425         c3cn_conn_debug("c3cn 0x%p, s %u, f 0x%lx.\n",
1426                         c3cn, c3cn->state, c3cn->flags);
1427         if (likely(c3cn->state != C3CN_STATE_CONNECTING))
1428                 c3cn_active_close(c3cn);
1429         else
1430                 c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
1431         c3cn_put(c3cn);
1432 }
1433
1434 static int is_cxgb3_dev(struct net_device *dev)
1435 {
1436         struct cxgb3i_sdev_data *cdata;
1437
1438         write_lock(&cdata_rwlock);
1439         list_for_each_entry(cdata, &cdata_list, list) {
1440                 struct adap_ports *ports = &cdata->ports;
1441                 int i;
1442
1443                 for (i = 0; i < ports->nports; i++)
1444                         if (dev == ports->lldevs[i]) {
1445                                 write_unlock(&cdata_rwlock);
1446                                 return 1;
1447                         }
1448         }
1449         write_unlock(&cdata_rwlock);
1450         return 0;
1451 }
1452
1453 /**
1454  * cxgb3_egress_dev - return the cxgb3 egress device
1455  * @root_dev: the root device anchoring the search
1456  * @c3cn: the connection used to determine egress port in bonding mode
1457  * @context: in bonding mode, indicates a connection set up or failover
1458  *
1459  * Return egress device or NULL if the egress device isn't one of our ports.
1460  */
1461 static struct net_device *cxgb3_egress_dev(struct net_device *root_dev,
1462                                            struct s3_conn *c3cn,
1463                                            int context)
1464 {
1465         while (root_dev) {
1466                 if (root_dev->priv_flags & IFF_802_1Q_VLAN)
1467                         root_dev = vlan_dev_real_dev(root_dev);
1468                 else if (is_cxgb3_dev(root_dev))
1469                         return root_dev;
1470                 else
1471                         return NULL;
1472         }
1473         return NULL;
1474 }
1475
1476 static struct rtable *find_route(__be32 saddr, __be32 daddr,
1477                                  __be16 sport, __be16 dport)
1478 {
1479         struct rtable *rt;
1480         struct flowi fl = {
1481                 .oif = 0,
1482                 .nl_u = {
1483                          .ip4_u = {
1484                                    .daddr = daddr,
1485                                    .saddr = saddr,
1486                                    .tos = 0 } },
1487                 .proto = IPPROTO_TCP,
1488                 .uli_u = {
1489                           .ports = {
1490                                     .sport = sport,
1491                                     .dport = dport } } };
1492
1493         if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
1494                 return NULL;
1495         return rt;
1496 }
1497
1498 /*
1499  * Assign offload parameters to some connection fields.
1500  */
1501 static void init_offload_conn(struct s3_conn *c3cn,
1502                               struct t3cdev *cdev,
1503                               struct dst_entry *dst)
1504 {
1505         BUG_ON(c3cn->cdev != cdev);
1506         c3cn->wr_max = c3cn->wr_avail = T3C_DATA(cdev)->max_wrs - 1;
1507         c3cn->wr_unacked = 0;
1508         c3cn->mss_idx = select_mss(c3cn, dst_mtu(dst));
1509
1510         reset_wr_list(c3cn);
1511 }
1512
1513 static int initiate_act_open(struct s3_conn *c3cn, struct net_device *dev)
1514 {
1515         struct cxgb3i_sdev_data *cdata = NDEV2CDATA(dev);
1516         struct t3cdev *cdev = cdata->cdev;
1517         struct dst_entry *dst = c3cn->dst_cache;
1518         struct sk_buff *skb;
1519
1520         c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
1521                         c3cn, c3cn->state, c3cn->flags);
1522         /*
1523          * Initialize connection data.  Note that the flags and ULP mode are
1524          * initialized higher up ...
1525          */
1526         c3cn->dev = dev;
1527         c3cn->cdev = cdev;
1528         c3cn->tid = cxgb3_alloc_atid(cdev, cdata->client, c3cn);
1529         if (c3cn->tid < 0)
1530                 goto out_err;
1531
1532         c3cn->qset = 0;
1533         c3cn->l2t = t3_l2t_get(cdev, dst->neighbour, dev);
1534         if (!c3cn->l2t)
1535                 goto free_tid;
1536
1537         skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_KERNEL);
1538         if (!skb)
1539                 goto free_l2t;
1540
1541         skb->sk = (struct sock *)c3cn;
1542         set_arp_failure_handler(skb, act_open_req_arp_failure);
1543
1544         c3cn_hold(c3cn);
1545
1546         init_offload_conn(c3cn, cdev, dst);
1547         c3cn->err = 0;
1548
1549         make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
1550         l2t_send(cdev, skb, c3cn->l2t);
1551         return 0;
1552
1553 free_l2t:
1554         l2t_release(L2DATA(cdev), c3cn->l2t);
1555 free_tid:
1556         s3_free_atid(cdev, c3cn->tid);
1557         c3cn->tid = 0;
1558 out_err:
1559         return -1;
1560 }
1561
1562
1563 /**
1564  * cxgb3i_c3cn_connect - initiates an iscsi tcp connection to a given address
1565  * @c3cn: the iscsi tcp connection
1566  * @usin: destination address
1567  *
1568  * return 0 if active open request is sent, < 0 otherwise.
1569  */
1570 int cxgb3i_c3cn_connect(struct s3_conn *c3cn, struct sockaddr_in *usin)
1571 {
1572         struct rtable *rt;
1573         struct net_device *dev;
1574         struct cxgb3i_sdev_data *cdata;
1575         struct t3cdev *cdev;
1576         __be32 sipv4;
1577         int err;
1578
1579         if (usin->sin_family != AF_INET)
1580                 return -EAFNOSUPPORT;
1581
1582         c3cn->daddr.sin_port = usin->sin_port;
1583         c3cn->daddr.sin_addr.s_addr = usin->sin_addr.s_addr;
1584
1585         rt = find_route(c3cn->saddr.sin_addr.s_addr,
1586                         c3cn->daddr.sin_addr.s_addr,
1587                         c3cn->saddr.sin_port,
1588                         c3cn->daddr.sin_port);
1589         if (rt == NULL) {
1590                 c3cn_conn_debug("NO route to 0x%x, port %u.\n",
1591                                 c3cn->daddr.sin_addr.s_addr,
1592                                 ntohs(c3cn->daddr.sin_port));
1593                 return -ENETUNREACH;
1594         }
1595
1596         if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
1597                 c3cn_conn_debug("multi-cast route to 0x%x, port %u.\n",
1598                                 c3cn->daddr.sin_addr.s_addr,
1599                                 ntohs(c3cn->daddr.sin_port));
1600                 ip_rt_put(rt);
1601                 return -ENETUNREACH;
1602         }
1603
1604         if (!c3cn->saddr.sin_addr.s_addr)
1605                 c3cn->saddr.sin_addr.s_addr = rt->rt_src;
1606
1607         /* now commit destination to connection */
1608         c3cn->dst_cache = &rt->u.dst;
1609
1610         /* try to establish an offloaded connection */
1611         dev = cxgb3_egress_dev(c3cn->dst_cache->dev, c3cn, 0);
1612         if (dev == NULL) {
1613                 c3cn_conn_debug("c3cn 0x%p, egress dev NULL.\n", c3cn);
1614                 return -ENETUNREACH;
1615         }
1616         cdata = NDEV2CDATA(dev);
1617         cdev = cdata->cdev;
1618
1619         /* get a source port if one hasn't been provided */
1620         err = c3cn_get_port(c3cn, cdata);
1621         if (err)
1622                 return err;
1623
1624         c3cn_conn_debug("c3cn 0x%p get port %u.\n",
1625                         c3cn, ntohs(c3cn->saddr.sin_port));
1626
1627         sipv4 = cxgb3i_get_private_ipv4addr(dev);
1628         if (!sipv4) {
1629                 c3cn_conn_debug("c3cn 0x%p, iscsi ip not configured.\n", c3cn);
1630                 sipv4 = c3cn->saddr.sin_addr.s_addr;
1631                 cxgb3i_set_private_ipv4addr(dev, sipv4);
1632         } else
1633                 c3cn->saddr.sin_addr.s_addr = sipv4;
1634
1635         c3cn_conn_debug("c3cn 0x%p, %u.%u.%u.%u,%u-%u.%u.%u.%u,%u SYN_SENT.\n",
1636                         c3cn, NIPQUAD(c3cn->saddr.sin_addr.s_addr),
1637                         ntohs(c3cn->saddr.sin_port),
1638                         NIPQUAD(c3cn->daddr.sin_addr.s_addr),
1639                         ntohs(c3cn->daddr.sin_port));
1640
1641         c3cn_set_state(c3cn, C3CN_STATE_CONNECTING);
1642         if (!initiate_act_open(c3cn, dev))
1643                 return 0;
1644
1645         /*
1646          * If we get here, we don't have an offload connection so simply
1647          * return a failure.
1648          */
1649         err = -ENOTSUPP;
1650
1651         /*
1652          * This trashes the connection and releases the local port,
1653          * if necessary.
1654          */
1655         c3cn_conn_debug("c3cn 0x%p -> CLOSED.\n", c3cn);
1656         c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
1657         ip_rt_put(rt);
1658         c3cn_put_port(c3cn);
1659         c3cn->daddr.sin_port = 0;
1660         return err;
1661 }
1662
1663 /**
1664  * cxgb3i_c3cn_rx_credits - ack received tcp data.
1665  * @c3cn: iscsi tcp connection
1666  * @copied: # of bytes processed
1667  *
1668  * Called after some received data has been read.  It returns RX credits
1669  * to the HW for the amount of data processed.
1670  */
1671 void cxgb3i_c3cn_rx_credits(struct s3_conn *c3cn, int copied)
1672 {
1673         struct t3cdev *cdev;
1674         int must_send;
1675         u32 credits, dack = 0;
1676
1677         if (c3cn->state != C3CN_STATE_ESTABLISHED)
1678                 return;
1679
1680         credits = c3cn->copied_seq - c3cn->rcv_wup;
1681         if (unlikely(!credits))
1682                 return;
1683
1684         cdev = c3cn->cdev;
1685
1686         if (unlikely(cxgb3_rx_credit_thres == 0))
1687                 return;
1688
1689         dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
1690
1691         /*
1692          * For coalescing to work effectively ensure the receive window has
1693          * at least 16KB left.
1694          */
1695         must_send = credits + 16384 >= cxgb3_rcv_win;
1696
1697         if (must_send || credits >= cxgb3_rx_credit_thres)
1698                 c3cn->rcv_wup += send_rx_credits(c3cn, credits, dack);
1699 }
1700
1701 /**
1702  * cxgb3i_c3cn_send_pdus - send the skbs containing iscsi pdus
1703  * @c3cn: iscsi tcp connection
1704  * @skb: skb contains the iscsi pdu
1705  *
1706  * Add a list of skbs to a connection send queue. The skbs must comply with
1707  * the max size limit of the device and have a headroom of at least
1708  * TX_HEADER_LEN bytes.
1709  * Return # of bytes queued.
1710  */
1711 int cxgb3i_c3cn_send_pdus(struct s3_conn *c3cn, struct sk_buff *skb)
1712 {
1713         struct sk_buff *next;
1714         int err, copied = 0;
1715
1716         spin_lock_bh(&c3cn->lock);
1717
1718         if (c3cn->state != C3CN_STATE_ESTABLISHED) {
1719                 c3cn_tx_debug("c3cn 0x%p, not in est. state %u.\n",
1720                               c3cn, c3cn->state);
1721                 err = -EAGAIN;
1722                 goto out_err;
1723         }
1724
1725         if (c3cn->err) {
1726                 c3cn_tx_debug("c3cn 0x%p, err %d.\n", c3cn, c3cn->err);
1727                 err = -EPIPE;
1728                 goto out_err;
1729         }
1730
1731         if (c3cn->write_seq - c3cn->snd_una >= cxgb3_snd_win) {
1732                 c3cn_tx_debug("c3cn 0x%p, snd %u - %u > %u.\n",
1733                                 c3cn, c3cn->write_seq, c3cn->snd_una,
1734                                 cxgb3_snd_win);
1735                 err = -EAGAIN;
1736                 goto out_err;
1737         }
1738
1739         while (skb) {
1740                 int frags = skb_shinfo(skb)->nr_frags +
1741                                 (skb->len != skb->data_len);
1742
1743                 if (unlikely(skb_headroom(skb) < TX_HEADER_LEN)) {
1744                         c3cn_tx_debug("c3cn 0x%p, skb head.\n", c3cn);
1745                         err = -EINVAL;
1746                         goto out_err;
1747                 }
1748
1749                 if (frags >= SKB_WR_LIST_SIZE) {
1750                         cxgb3i_log_error("c3cn 0x%p, tx frags %d, len %u,%u.\n",
1751                                          c3cn, skb_shinfo(skb)->nr_frags,
1752                                          skb->len, skb->data_len);
1753                         err = -EINVAL;
1754                         goto out_err;
1755                 }
1756
1757                 next = skb->next;
1758                 skb->next = NULL;
1759                 skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND | C3CB_FLAG_NEED_HDR);
1760                 copied += skb->len;
1761                 c3cn->write_seq += skb->len + ulp_extra_len(skb);
1762                 skb = next;
1763         }
1764 done:
1765         if (likely(skb_queue_len(&c3cn->write_queue)))
1766                 c3cn_push_tx_frames(c3cn, 1);
1767         spin_unlock_bh(&c3cn->lock);
1768         return copied;
1769
1770 out_err:
1771         if (copied == 0 && err == -EPIPE)
1772                 copied = c3cn->err ? c3cn->err : -EPIPE;
1773         goto done;
1774 }
1775
1776 static void sdev_data_cleanup(struct cxgb3i_sdev_data *cdata)
1777 {
1778         struct adap_ports *ports = &cdata->ports;
1779         int i;
1780
1781         for (i = 0; i < ports->nports; i++)
1782                 NDEV2CDATA(ports->lldevs[i]) = NULL;
1783         cxgb3i_free_big_mem(cdata);
1784 }
1785
1786 void cxgb3i_sdev_cleanup(void)
1787 {
1788         struct cxgb3i_sdev_data *cdata;
1789
1790         write_lock(&cdata_rwlock);
1791         list_for_each_entry(cdata, &cdata_list, list) {
1792                 list_del(&cdata->list);
1793                 sdev_data_cleanup(cdata);
1794         }
1795         write_unlock(&cdata_rwlock);
1796 }
1797
1798 int cxgb3i_sdev_init(cxgb3_cpl_handler_func *cpl_handlers)
1799 {
1800         cpl_handlers[CPL_ACT_ESTABLISH] = do_act_establish;
1801         cpl_handlers[CPL_ACT_OPEN_RPL] = do_act_open_rpl;
1802         cpl_handlers[CPL_PEER_CLOSE] = do_peer_close;
1803         cpl_handlers[CPL_ABORT_REQ_RSS] = do_abort_req;
1804         cpl_handlers[CPL_ABORT_RPL_RSS] = do_abort_rpl;
1805         cpl_handlers[CPL_CLOSE_CON_RPL] = do_close_con_rpl;
1806         cpl_handlers[CPL_TX_DMA_ACK] = do_wr_ack;
1807         cpl_handlers[CPL_ISCSI_HDR] = do_iscsi_hdr;
1808
1809         if (cxgb3_max_connect > CXGB3I_MAX_CONN)
1810                 cxgb3_max_connect = CXGB3I_MAX_CONN;
1811         return 0;
1812 }
1813
1814 /**
1815  * cxgb3i_sdev_add - allocate and initialize resources for each adapter found
1816  * @cdev:       t3cdev adapter
1817  * @client:     cxgb3 driver client
1818  */
1819 void cxgb3i_sdev_add(struct t3cdev *cdev, struct cxgb3_client *client)
1820 {
1821         struct cxgb3i_sdev_data *cdata;
1822         struct ofld_page_info rx_page_info;
1823         unsigned int wr_len;
1824         int mapsize = DIV_ROUND_UP(cxgb3_max_connect,
1825                                    8 * sizeof(unsigned long));
1826         int i;
1827
1828         cdata =  cxgb3i_alloc_big_mem(sizeof(*cdata) + mapsize, GFP_KERNEL);
1829         if (!cdata)
1830                 return;
1831
1832         if (cdev->ctl(cdev, GET_WR_LEN, &wr_len) < 0 ||
1833             cdev->ctl(cdev, GET_PORTS, &cdata->ports) < 0 ||
1834             cdev->ctl(cdev, GET_RX_PAGE_INFO, &rx_page_info) < 0)
1835                 goto free_cdata;
1836
1837         s3_init_wr_tab(wr_len);
1838
1839         INIT_LIST_HEAD(&cdata->list);
1840         cdata->cdev = cdev;
1841         cdata->client = client;
1842
1843         for (i = 0; i < cdata->ports.nports; i++)
1844                 NDEV2CDATA(cdata->ports.lldevs[i]) = cdata;
1845
1846         write_lock(&cdata_rwlock);
1847         list_add_tail(&cdata->list, &cdata_list);
1848         write_unlock(&cdata_rwlock);
1849
1850         return;
1851
1852 free_cdata:
1853         cxgb3i_free_big_mem(cdata);
1854 }
1855
1856 /**
1857  * cxgb3i_sdev_remove - free the allocated resources for the adapter
1858  * @cdev:       t3cdev adapter
1859  */
1860 void cxgb3i_sdev_remove(struct t3cdev *cdev)
1861 {
1862         struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
1863
1864         write_lock(&cdata_rwlock);
1865         list_del(&cdata->list);
1866         write_unlock(&cdata_rwlock);
1867
1868         sdev_data_cleanup(cdata);
1869 }