cxgb3i_offload.c 50 KB

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