cxgb3i_offload.c 50 KB

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