cxgb3i_offload.c 49 KB

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