cxgb3i_offload.c 48 KB

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