cxgb3i_offload.c 49 KB

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