iwch_cm.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /*
  2. * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/list.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/timer.h>
  37. #include <linux/notifier.h>
  38. #include <linux/inetdevice.h>
  39. #include <net/neighbour.h>
  40. #include <net/netevent.h>
  41. #include <net/route.h>
  42. #include "tcb.h"
  43. #include "cxgb3_offload.h"
  44. #include "iwch.h"
  45. #include "iwch_provider.h"
  46. #include "iwch_cm.h"
  47. static char *states[] = {
  48. "idle",
  49. "listen",
  50. "connecting",
  51. "mpa_wait_req",
  52. "mpa_req_sent",
  53. "mpa_req_rcvd",
  54. "mpa_rep_sent",
  55. "fpdu_mode",
  56. "aborting",
  57. "closing",
  58. "moribund",
  59. "dead",
  60. NULL,
  61. };
  62. static int ep_timeout_secs = 10;
  63. module_param(ep_timeout_secs, int, 0644);
  64. MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
  65. "in seconds (default=10)");
  66. static int mpa_rev = 1;
  67. module_param(mpa_rev, int, 0644);
  68. MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
  69. "1 is spec compliant. (default=1)");
  70. static int markers_enabled = 0;
  71. module_param(markers_enabled, int, 0644);
  72. MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
  73. static int crc_enabled = 1;
  74. module_param(crc_enabled, int, 0644);
  75. MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
  76. static int rcv_win = 256 * 1024;
  77. module_param(rcv_win, int, 0644);
  78. MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256)");
  79. static int snd_win = 32 * 1024;
  80. module_param(snd_win, int, 0644);
  81. MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=32KB)");
  82. static unsigned int nocong = 0;
  83. module_param(nocong, uint, 0644);
  84. MODULE_PARM_DESC(nocong, "Turn off congestion control (default=0)");
  85. static unsigned int cong_flavor = 1;
  86. module_param(cong_flavor, uint, 0644);
  87. MODULE_PARM_DESC(cong_flavor, "TCP Congestion control flavor (default=1)");
  88. static void process_work(struct work_struct *work);
  89. static struct workqueue_struct *workq;
  90. static DECLARE_WORK(skb_work, process_work);
  91. static struct sk_buff_head rxq;
  92. static cxgb3_cpl_handler_func work_handlers[NUM_CPL_CMDS];
  93. static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
  94. static void ep_timeout(unsigned long arg);
  95. static void connect_reply_upcall(struct iwch_ep *ep, int status);
  96. static void start_ep_timer(struct iwch_ep *ep)
  97. {
  98. PDBG("%s ep %p\n", __func__, ep);
  99. if (timer_pending(&ep->timer)) {
  100. PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
  101. del_timer_sync(&ep->timer);
  102. } else
  103. get_ep(&ep->com);
  104. ep->timer.expires = jiffies + ep_timeout_secs * HZ;
  105. ep->timer.data = (unsigned long)ep;
  106. ep->timer.function = ep_timeout;
  107. add_timer(&ep->timer);
  108. }
  109. static void stop_ep_timer(struct iwch_ep *ep)
  110. {
  111. PDBG("%s ep %p\n", __func__, ep);
  112. del_timer_sync(&ep->timer);
  113. put_ep(&ep->com);
  114. }
  115. static void release_tid(struct t3cdev *tdev, u32 hwtid, struct sk_buff *skb)
  116. {
  117. struct cpl_tid_release *req;
  118. skb = get_skb(skb, sizeof *req, GFP_KERNEL);
  119. if (!skb)
  120. return;
  121. req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
  122. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  123. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
  124. skb->priority = CPL_PRIORITY_SETUP;
  125. cxgb3_ofld_send(tdev, skb);
  126. return;
  127. }
  128. int iwch_quiesce_tid(struct iwch_ep *ep)
  129. {
  130. struct cpl_set_tcb_field *req;
  131. struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
  132. if (!skb)
  133. return -ENOMEM;
  134. req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
  135. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  136. req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
  137. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
  138. req->reply = 0;
  139. req->cpu_idx = 0;
  140. req->word = htons(W_TCB_RX_QUIESCE);
  141. req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
  142. req->val = cpu_to_be64(1 << S_TCB_RX_QUIESCE);
  143. skb->priority = CPL_PRIORITY_DATA;
  144. cxgb3_ofld_send(ep->com.tdev, skb);
  145. return 0;
  146. }
  147. int iwch_resume_tid(struct iwch_ep *ep)
  148. {
  149. struct cpl_set_tcb_field *req;
  150. struct sk_buff *skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
  151. if (!skb)
  152. return -ENOMEM;
  153. req = (struct cpl_set_tcb_field *) skb_put(skb, sizeof(*req));
  154. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  155. req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
  156. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, ep->hwtid));
  157. req->reply = 0;
  158. req->cpu_idx = 0;
  159. req->word = htons(W_TCB_RX_QUIESCE);
  160. req->mask = cpu_to_be64(1ULL << S_TCB_RX_QUIESCE);
  161. req->val = 0;
  162. skb->priority = CPL_PRIORITY_DATA;
  163. cxgb3_ofld_send(ep->com.tdev, skb);
  164. return 0;
  165. }
  166. static void set_emss(struct iwch_ep *ep, u16 opt)
  167. {
  168. PDBG("%s ep %p opt %u\n", __func__, ep, opt);
  169. ep->emss = T3C_DATA(ep->com.tdev)->mtus[G_TCPOPT_MSS(opt)] - 40;
  170. if (G_TCPOPT_TSTAMP(opt))
  171. ep->emss -= 12;
  172. if (ep->emss < 128)
  173. ep->emss = 128;
  174. PDBG("emss=%d\n", ep->emss);
  175. }
  176. static enum iwch_ep_state state_read(struct iwch_ep_common *epc)
  177. {
  178. unsigned long flags;
  179. enum iwch_ep_state state;
  180. spin_lock_irqsave(&epc->lock, flags);
  181. state = epc->state;
  182. spin_unlock_irqrestore(&epc->lock, flags);
  183. return state;
  184. }
  185. static void __state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
  186. {
  187. epc->state = new;
  188. }
  189. static void state_set(struct iwch_ep_common *epc, enum iwch_ep_state new)
  190. {
  191. unsigned long flags;
  192. spin_lock_irqsave(&epc->lock, flags);
  193. PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
  194. __state_set(epc, new);
  195. spin_unlock_irqrestore(&epc->lock, flags);
  196. return;
  197. }
  198. static void *alloc_ep(int size, gfp_t gfp)
  199. {
  200. struct iwch_ep_common *epc;
  201. epc = kzalloc(size, gfp);
  202. if (epc) {
  203. kref_init(&epc->kref);
  204. spin_lock_init(&epc->lock);
  205. init_waitqueue_head(&epc->waitq);
  206. }
  207. PDBG("%s alloc ep %p\n", __func__, epc);
  208. return epc;
  209. }
  210. void __free_ep(struct kref *kref)
  211. {
  212. struct iwch_ep_common *epc;
  213. epc = container_of(kref, struct iwch_ep_common, kref);
  214. PDBG("%s ep %p state %s\n", __func__, epc, states[state_read(epc)]);
  215. kfree(epc);
  216. }
  217. static void release_ep_resources(struct iwch_ep *ep)
  218. {
  219. PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
  220. cxgb3_remove_tid(ep->com.tdev, (void *)ep, ep->hwtid);
  221. dst_release(ep->dst);
  222. l2t_release(L2DATA(ep->com.tdev), ep->l2t);
  223. put_ep(&ep->com);
  224. }
  225. static void process_work(struct work_struct *work)
  226. {
  227. struct sk_buff *skb = NULL;
  228. void *ep;
  229. struct t3cdev *tdev;
  230. int ret;
  231. while ((skb = skb_dequeue(&rxq))) {
  232. ep = *((void **) (skb->cb));
  233. tdev = *((struct t3cdev **) (skb->cb + sizeof(void *)));
  234. ret = work_handlers[G_OPCODE(ntohl((__force __be32)skb->csum))](tdev, skb, ep);
  235. if (ret & CPL_RET_BUF_DONE)
  236. kfree_skb(skb);
  237. /*
  238. * ep was referenced in sched(), and is freed here.
  239. */
  240. put_ep((struct iwch_ep_common *)ep);
  241. }
  242. }
  243. static int status2errno(int status)
  244. {
  245. switch (status) {
  246. case CPL_ERR_NONE:
  247. return 0;
  248. case CPL_ERR_CONN_RESET:
  249. return -ECONNRESET;
  250. case CPL_ERR_ARP_MISS:
  251. return -EHOSTUNREACH;
  252. case CPL_ERR_CONN_TIMEDOUT:
  253. return -ETIMEDOUT;
  254. case CPL_ERR_TCAM_FULL:
  255. return -ENOMEM;
  256. case CPL_ERR_CONN_EXIST:
  257. return -EADDRINUSE;
  258. default:
  259. return -EIO;
  260. }
  261. }
  262. /*
  263. * Try and reuse skbs already allocated...
  264. */
  265. static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
  266. {
  267. if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
  268. skb_trim(skb, 0);
  269. skb_get(skb);
  270. } else {
  271. skb = alloc_skb(len, gfp);
  272. }
  273. return skb;
  274. }
  275. static struct rtable *find_route(struct t3cdev *dev, __be32 local_ip,
  276. __be32 peer_ip, __be16 local_port,
  277. __be16 peer_port, u8 tos)
  278. {
  279. struct rtable *rt;
  280. struct flowi fl = {
  281. .oif = 0,
  282. .nl_u = {
  283. .ip4_u = {
  284. .daddr = peer_ip,
  285. .saddr = local_ip,
  286. .tos = tos}
  287. },
  288. .proto = IPPROTO_TCP,
  289. .uli_u = {
  290. .ports = {
  291. .sport = local_port,
  292. .dport = peer_port}
  293. }
  294. };
  295. if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
  296. return NULL;
  297. return rt;
  298. }
  299. static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
  300. {
  301. int i = 0;
  302. while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
  303. ++i;
  304. return i;
  305. }
  306. static void arp_failure_discard(struct t3cdev *dev, struct sk_buff *skb)
  307. {
  308. PDBG("%s t3cdev %p\n", __func__, dev);
  309. kfree_skb(skb);
  310. }
  311. /*
  312. * Handle an ARP failure for an active open.
  313. */
  314. static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
  315. {
  316. printk(KERN_ERR MOD "ARP failure duing connect\n");
  317. kfree_skb(skb);
  318. }
  319. /*
  320. * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
  321. * and send it along.
  322. */
  323. static void abort_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
  324. {
  325. struct cpl_abort_req *req = cplhdr(skb);
  326. PDBG("%s t3cdev %p\n", __func__, dev);
  327. req->cmd = CPL_ABORT_NO_RST;
  328. cxgb3_ofld_send(dev, skb);
  329. }
  330. static int send_halfclose(struct iwch_ep *ep, gfp_t gfp)
  331. {
  332. struct cpl_close_con_req *req;
  333. struct sk_buff *skb;
  334. PDBG("%s ep %p\n", __func__, ep);
  335. skb = get_skb(NULL, sizeof(*req), gfp);
  336. if (!skb) {
  337. printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
  338. return -ENOMEM;
  339. }
  340. skb->priority = CPL_PRIORITY_DATA;
  341. set_arp_failure_handler(skb, arp_failure_discard);
  342. req = (struct cpl_close_con_req *) skb_put(skb, sizeof(*req));
  343. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
  344. req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
  345. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, ep->hwtid));
  346. l2t_send(ep->com.tdev, skb, ep->l2t);
  347. return 0;
  348. }
  349. static int send_abort(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
  350. {
  351. struct cpl_abort_req *req;
  352. PDBG("%s ep %p\n", __func__, ep);
  353. skb = get_skb(skb, sizeof(*req), gfp);
  354. if (!skb) {
  355. printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
  356. __func__);
  357. return -ENOMEM;
  358. }
  359. skb->priority = CPL_PRIORITY_DATA;
  360. set_arp_failure_handler(skb, abort_arp_failure);
  361. req = (struct cpl_abort_req *) skb_put(skb, sizeof(*req));
  362. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
  363. req->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
  364. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
  365. req->cmd = CPL_ABORT_SEND_RST;
  366. l2t_send(ep->com.tdev, skb, ep->l2t);
  367. return 0;
  368. }
  369. static int send_connect(struct iwch_ep *ep)
  370. {
  371. struct cpl_act_open_req *req;
  372. struct sk_buff *skb;
  373. u32 opt0h, opt0l, opt2;
  374. unsigned int mtu_idx;
  375. int wscale;
  376. PDBG("%s ep %p\n", __func__, ep);
  377. skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
  378. if (!skb) {
  379. printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
  380. __func__);
  381. return -ENOMEM;
  382. }
  383. mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
  384. wscale = compute_wscale(rcv_win);
  385. opt0h = V_NAGLE(0) |
  386. V_NO_CONG(nocong) |
  387. V_KEEP_ALIVE(1) |
  388. F_TCAM_BYPASS |
  389. V_WND_SCALE(wscale) |
  390. V_MSS_IDX(mtu_idx) |
  391. V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
  392. opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
  393. opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
  394. skb->priority = CPL_PRIORITY_SETUP;
  395. set_arp_failure_handler(skb, act_open_req_arp_failure);
  396. req = (struct cpl_act_open_req *) skb_put(skb, sizeof(*req));
  397. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  398. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ep->atid));
  399. req->local_port = ep->com.local_addr.sin_port;
  400. req->peer_port = ep->com.remote_addr.sin_port;
  401. req->local_ip = ep->com.local_addr.sin_addr.s_addr;
  402. req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
  403. req->opt0h = htonl(opt0h);
  404. req->opt0l = htonl(opt0l);
  405. req->params = 0;
  406. req->opt2 = htonl(opt2);
  407. l2t_send(ep->com.tdev, skb, ep->l2t);
  408. return 0;
  409. }
  410. static void send_mpa_req(struct iwch_ep *ep, struct sk_buff *skb)
  411. {
  412. int mpalen;
  413. struct tx_data_wr *req;
  414. struct mpa_message *mpa;
  415. int len;
  416. PDBG("%s ep %p pd_len %d\n", __func__, ep, ep->plen);
  417. BUG_ON(skb_cloned(skb));
  418. mpalen = sizeof(*mpa) + ep->plen;
  419. if (skb->data + mpalen + sizeof(*req) > skb_end_pointer(skb)) {
  420. kfree_skb(skb);
  421. skb=alloc_skb(mpalen + sizeof(*req), GFP_KERNEL);
  422. if (!skb) {
  423. connect_reply_upcall(ep, -ENOMEM);
  424. return;
  425. }
  426. }
  427. skb_trim(skb, 0);
  428. skb_reserve(skb, sizeof(*req));
  429. skb_put(skb, mpalen);
  430. skb->priority = CPL_PRIORITY_DATA;
  431. mpa = (struct mpa_message *) skb->data;
  432. memset(mpa, 0, sizeof(*mpa));
  433. memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
  434. mpa->flags = (crc_enabled ? MPA_CRC : 0) |
  435. (markers_enabled ? MPA_MARKERS : 0);
  436. mpa->private_data_size = htons(ep->plen);
  437. mpa->revision = mpa_rev;
  438. if (ep->plen)
  439. memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
  440. /*
  441. * Reference the mpa skb. This ensures the data area
  442. * will remain in memory until the hw acks the tx.
  443. * Function tx_ack() will deref it.
  444. */
  445. skb_get(skb);
  446. set_arp_failure_handler(skb, arp_failure_discard);
  447. skb_reset_transport_header(skb);
  448. len = skb->len;
  449. req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
  450. req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
  451. req->wr_lo = htonl(V_WR_TID(ep->hwtid));
  452. req->len = htonl(len);
  453. req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
  454. V_TX_SNDBUF(snd_win>>15));
  455. req->flags = htonl(F_TX_INIT);
  456. req->sndseq = htonl(ep->snd_seq);
  457. BUG_ON(ep->mpa_skb);
  458. ep->mpa_skb = skb;
  459. l2t_send(ep->com.tdev, skb, ep->l2t);
  460. start_ep_timer(ep);
  461. state_set(&ep->com, MPA_REQ_SENT);
  462. return;
  463. }
  464. static int send_mpa_reject(struct iwch_ep *ep, const void *pdata, u8 plen)
  465. {
  466. int mpalen;
  467. struct tx_data_wr *req;
  468. struct mpa_message *mpa;
  469. struct sk_buff *skb;
  470. PDBG("%s ep %p plen %d\n", __func__, ep, plen);
  471. mpalen = sizeof(*mpa) + plen;
  472. skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
  473. if (!skb) {
  474. printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
  475. return -ENOMEM;
  476. }
  477. skb_reserve(skb, sizeof(*req));
  478. mpa = (struct mpa_message *) skb_put(skb, mpalen);
  479. memset(mpa, 0, sizeof(*mpa));
  480. memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
  481. mpa->flags = MPA_REJECT;
  482. mpa->revision = mpa_rev;
  483. mpa->private_data_size = htons(plen);
  484. if (plen)
  485. memcpy(mpa->private_data, pdata, plen);
  486. /*
  487. * Reference the mpa skb again. This ensures the data area
  488. * will remain in memory until the hw acks the tx.
  489. * Function tx_ack() will deref it.
  490. */
  491. skb_get(skb);
  492. skb->priority = CPL_PRIORITY_DATA;
  493. set_arp_failure_handler(skb, arp_failure_discard);
  494. skb_reset_transport_header(skb);
  495. req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
  496. req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
  497. req->wr_lo = htonl(V_WR_TID(ep->hwtid));
  498. req->len = htonl(mpalen);
  499. req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
  500. V_TX_SNDBUF(snd_win>>15));
  501. req->flags = htonl(F_TX_INIT);
  502. req->sndseq = htonl(ep->snd_seq);
  503. BUG_ON(ep->mpa_skb);
  504. ep->mpa_skb = skb;
  505. l2t_send(ep->com.tdev, skb, ep->l2t);
  506. return 0;
  507. }
  508. static int send_mpa_reply(struct iwch_ep *ep, const void *pdata, u8 plen)
  509. {
  510. int mpalen;
  511. struct tx_data_wr *req;
  512. struct mpa_message *mpa;
  513. int len;
  514. struct sk_buff *skb;
  515. PDBG("%s ep %p plen %d\n", __func__, ep, plen);
  516. mpalen = sizeof(*mpa) + plen;
  517. skb = get_skb(NULL, mpalen + sizeof(*req), GFP_KERNEL);
  518. if (!skb) {
  519. printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
  520. return -ENOMEM;
  521. }
  522. skb->priority = CPL_PRIORITY_DATA;
  523. skb_reserve(skb, sizeof(*req));
  524. mpa = (struct mpa_message *) skb_put(skb, mpalen);
  525. memset(mpa, 0, sizeof(*mpa));
  526. memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
  527. mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
  528. (markers_enabled ? MPA_MARKERS : 0);
  529. mpa->revision = mpa_rev;
  530. mpa->private_data_size = htons(plen);
  531. if (plen)
  532. memcpy(mpa->private_data, pdata, plen);
  533. /*
  534. * Reference the mpa skb. This ensures the data area
  535. * will remain in memory until the hw acks the tx.
  536. * Function tx_ack() will deref it.
  537. */
  538. skb_get(skb);
  539. set_arp_failure_handler(skb, arp_failure_discard);
  540. skb_reset_transport_header(skb);
  541. len = skb->len;
  542. req = (struct tx_data_wr *) skb_push(skb, sizeof(*req));
  543. req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
  544. req->wr_lo = htonl(V_WR_TID(ep->hwtid));
  545. req->len = htonl(len);
  546. req->param = htonl(V_TX_PORT(ep->l2t->smt_idx) |
  547. V_TX_SNDBUF(snd_win>>15));
  548. req->flags = htonl(F_TX_INIT);
  549. req->sndseq = htonl(ep->snd_seq);
  550. ep->mpa_skb = skb;
  551. state_set(&ep->com, MPA_REP_SENT);
  552. l2t_send(ep->com.tdev, skb, ep->l2t);
  553. return 0;
  554. }
  555. static int act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  556. {
  557. struct iwch_ep *ep = ctx;
  558. struct cpl_act_establish *req = cplhdr(skb);
  559. unsigned int tid = GET_TID(req);
  560. PDBG("%s ep %p tid %d\n", __func__, ep, tid);
  561. dst_confirm(ep->dst);
  562. /* setup the hwtid for this connection */
  563. ep->hwtid = tid;
  564. cxgb3_insert_tid(ep->com.tdev, &t3c_client, ep, tid);
  565. ep->snd_seq = ntohl(req->snd_isn);
  566. ep->rcv_seq = ntohl(req->rcv_isn);
  567. set_emss(ep, ntohs(req->tcp_opt));
  568. /* dealloc the atid */
  569. cxgb3_free_atid(ep->com.tdev, ep->atid);
  570. /* start MPA negotiation */
  571. send_mpa_req(ep, skb);
  572. return 0;
  573. }
  574. static void abort_connection(struct iwch_ep *ep, struct sk_buff *skb, gfp_t gfp)
  575. {
  576. PDBG("%s ep %p\n", __FILE__, ep);
  577. state_set(&ep->com, ABORTING);
  578. send_abort(ep, skb, gfp);
  579. }
  580. static void close_complete_upcall(struct iwch_ep *ep)
  581. {
  582. struct iw_cm_event event;
  583. PDBG("%s ep %p\n", __func__, ep);
  584. memset(&event, 0, sizeof(event));
  585. event.event = IW_CM_EVENT_CLOSE;
  586. if (ep->com.cm_id) {
  587. PDBG("close complete delivered ep %p cm_id %p tid %d\n",
  588. ep, ep->com.cm_id, ep->hwtid);
  589. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  590. ep->com.cm_id->rem_ref(ep->com.cm_id);
  591. ep->com.cm_id = NULL;
  592. ep->com.qp = NULL;
  593. }
  594. }
  595. static void peer_close_upcall(struct iwch_ep *ep)
  596. {
  597. struct iw_cm_event event;
  598. PDBG("%s ep %p\n", __func__, ep);
  599. memset(&event, 0, sizeof(event));
  600. event.event = IW_CM_EVENT_DISCONNECT;
  601. if (ep->com.cm_id) {
  602. PDBG("peer close delivered ep %p cm_id %p tid %d\n",
  603. ep, ep->com.cm_id, ep->hwtid);
  604. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  605. }
  606. }
  607. static void peer_abort_upcall(struct iwch_ep *ep)
  608. {
  609. struct iw_cm_event event;
  610. PDBG("%s ep %p\n", __func__, ep);
  611. memset(&event, 0, sizeof(event));
  612. event.event = IW_CM_EVENT_CLOSE;
  613. event.status = -ECONNRESET;
  614. if (ep->com.cm_id) {
  615. PDBG("abort delivered ep %p cm_id %p tid %d\n", ep,
  616. ep->com.cm_id, ep->hwtid);
  617. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  618. ep->com.cm_id->rem_ref(ep->com.cm_id);
  619. ep->com.cm_id = NULL;
  620. ep->com.qp = NULL;
  621. }
  622. }
  623. static void connect_reply_upcall(struct iwch_ep *ep, int status)
  624. {
  625. struct iw_cm_event event;
  626. PDBG("%s ep %p status %d\n", __func__, ep, status);
  627. memset(&event, 0, sizeof(event));
  628. event.event = IW_CM_EVENT_CONNECT_REPLY;
  629. event.status = status;
  630. event.local_addr = ep->com.local_addr;
  631. event.remote_addr = ep->com.remote_addr;
  632. if ((status == 0) || (status == -ECONNREFUSED)) {
  633. event.private_data_len = ep->plen;
  634. event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
  635. }
  636. if (ep->com.cm_id) {
  637. PDBG("%s ep %p tid %d status %d\n", __func__, ep,
  638. ep->hwtid, status);
  639. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  640. }
  641. if (status < 0) {
  642. ep->com.cm_id->rem_ref(ep->com.cm_id);
  643. ep->com.cm_id = NULL;
  644. ep->com.qp = NULL;
  645. }
  646. }
  647. static void connect_request_upcall(struct iwch_ep *ep)
  648. {
  649. struct iw_cm_event event;
  650. PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
  651. memset(&event, 0, sizeof(event));
  652. event.event = IW_CM_EVENT_CONNECT_REQUEST;
  653. event.local_addr = ep->com.local_addr;
  654. event.remote_addr = ep->com.remote_addr;
  655. event.private_data_len = ep->plen;
  656. event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
  657. event.provider_data = ep;
  658. if (state_read(&ep->parent_ep->com) != DEAD)
  659. ep->parent_ep->com.cm_id->event_handler(
  660. ep->parent_ep->com.cm_id,
  661. &event);
  662. put_ep(&ep->parent_ep->com);
  663. ep->parent_ep = NULL;
  664. }
  665. static void established_upcall(struct iwch_ep *ep)
  666. {
  667. struct iw_cm_event event;
  668. PDBG("%s ep %p\n", __func__, ep);
  669. memset(&event, 0, sizeof(event));
  670. event.event = IW_CM_EVENT_ESTABLISHED;
  671. if (ep->com.cm_id) {
  672. PDBG("%s ep %p tid %d\n", __func__, ep, ep->hwtid);
  673. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  674. }
  675. }
  676. static int update_rx_credits(struct iwch_ep *ep, u32 credits)
  677. {
  678. struct cpl_rx_data_ack *req;
  679. struct sk_buff *skb;
  680. PDBG("%s ep %p credits %u\n", __func__, ep, credits);
  681. skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
  682. if (!skb) {
  683. printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
  684. return 0;
  685. }
  686. req = (struct cpl_rx_data_ack *) skb_put(skb, sizeof(*req));
  687. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  688. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, ep->hwtid));
  689. req->credit_dack = htonl(V_RX_CREDITS(credits) | V_RX_FORCE_ACK(1));
  690. skb->priority = CPL_PRIORITY_ACK;
  691. cxgb3_ofld_send(ep->com.tdev, skb);
  692. return credits;
  693. }
  694. static void process_mpa_reply(struct iwch_ep *ep, struct sk_buff *skb)
  695. {
  696. struct mpa_message *mpa;
  697. u16 plen;
  698. struct iwch_qp_attributes attrs;
  699. enum iwch_qp_attr_mask mask;
  700. int err;
  701. PDBG("%s ep %p\n", __func__, ep);
  702. /*
  703. * Stop mpa timer. If it expired, then the state has
  704. * changed and we bail since ep_timeout already aborted
  705. * the connection.
  706. */
  707. stop_ep_timer(ep);
  708. if (state_read(&ep->com) != MPA_REQ_SENT)
  709. return;
  710. /*
  711. * If we get more than the supported amount of private data
  712. * then we must fail this connection.
  713. */
  714. if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
  715. err = -EINVAL;
  716. goto err;
  717. }
  718. /*
  719. * copy the new data into our accumulation buffer.
  720. */
  721. skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
  722. skb->len);
  723. ep->mpa_pkt_len += skb->len;
  724. /*
  725. * if we don't even have the mpa message, then bail.
  726. */
  727. if (ep->mpa_pkt_len < sizeof(*mpa))
  728. return;
  729. mpa = (struct mpa_message *) ep->mpa_pkt;
  730. /* Validate MPA header. */
  731. if (mpa->revision != mpa_rev) {
  732. err = -EPROTO;
  733. goto err;
  734. }
  735. if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
  736. err = -EPROTO;
  737. goto err;
  738. }
  739. plen = ntohs(mpa->private_data_size);
  740. /*
  741. * Fail if there's too much private data.
  742. */
  743. if (plen > MPA_MAX_PRIVATE_DATA) {
  744. err = -EPROTO;
  745. goto err;
  746. }
  747. /*
  748. * If plen does not account for pkt size
  749. */
  750. if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
  751. err = -EPROTO;
  752. goto err;
  753. }
  754. ep->plen = (u8) plen;
  755. /*
  756. * If we don't have all the pdata yet, then bail.
  757. * We'll continue process when more data arrives.
  758. */
  759. if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
  760. return;
  761. if (mpa->flags & MPA_REJECT) {
  762. err = -ECONNREFUSED;
  763. goto err;
  764. }
  765. /*
  766. * If we get here we have accumulated the entire mpa
  767. * start reply message including private data. And
  768. * the MPA header is valid.
  769. */
  770. state_set(&ep->com, FPDU_MODE);
  771. ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
  772. ep->mpa_attr.recv_marker_enabled = markers_enabled;
  773. ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
  774. ep->mpa_attr.version = mpa_rev;
  775. PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
  776. "xmit_marker_enabled=%d, version=%d\n", __func__,
  777. ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
  778. ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
  779. attrs.mpa_attr = ep->mpa_attr;
  780. attrs.max_ird = ep->ird;
  781. attrs.max_ord = ep->ord;
  782. attrs.llp_stream_handle = ep;
  783. attrs.next_state = IWCH_QP_STATE_RTS;
  784. mask = IWCH_QP_ATTR_NEXT_STATE |
  785. IWCH_QP_ATTR_LLP_STREAM_HANDLE | IWCH_QP_ATTR_MPA_ATTR |
  786. IWCH_QP_ATTR_MAX_IRD | IWCH_QP_ATTR_MAX_ORD;
  787. /* bind QP and TID with INIT_WR */
  788. err = iwch_modify_qp(ep->com.qp->rhp,
  789. ep->com.qp, mask, &attrs, 1);
  790. if (!err)
  791. goto out;
  792. err:
  793. abort_connection(ep, skb, GFP_KERNEL);
  794. out:
  795. connect_reply_upcall(ep, err);
  796. return;
  797. }
  798. static void process_mpa_request(struct iwch_ep *ep, struct sk_buff *skb)
  799. {
  800. struct mpa_message *mpa;
  801. u16 plen;
  802. PDBG("%s ep %p\n", __func__, ep);
  803. /*
  804. * Stop mpa timer. If it expired, then the state has
  805. * changed and we bail since ep_timeout already aborted
  806. * the connection.
  807. */
  808. stop_ep_timer(ep);
  809. if (state_read(&ep->com) != MPA_REQ_WAIT)
  810. return;
  811. /*
  812. * If we get more than the supported amount of private data
  813. * then we must fail this connection.
  814. */
  815. if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
  816. abort_connection(ep, skb, GFP_KERNEL);
  817. return;
  818. }
  819. PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
  820. /*
  821. * Copy the new data into our accumulation buffer.
  822. */
  823. skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
  824. skb->len);
  825. ep->mpa_pkt_len += skb->len;
  826. /*
  827. * If we don't even have the mpa message, then bail.
  828. * We'll continue process when more data arrives.
  829. */
  830. if (ep->mpa_pkt_len < sizeof(*mpa))
  831. return;
  832. PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
  833. mpa = (struct mpa_message *) ep->mpa_pkt;
  834. /*
  835. * Validate MPA Header.
  836. */
  837. if (mpa->revision != mpa_rev) {
  838. abort_connection(ep, skb, GFP_KERNEL);
  839. return;
  840. }
  841. if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
  842. abort_connection(ep, skb, GFP_KERNEL);
  843. return;
  844. }
  845. plen = ntohs(mpa->private_data_size);
  846. /*
  847. * Fail if there's too much private data.
  848. */
  849. if (plen > MPA_MAX_PRIVATE_DATA) {
  850. abort_connection(ep, skb, GFP_KERNEL);
  851. return;
  852. }
  853. /*
  854. * If plen does not account for pkt size
  855. */
  856. if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
  857. abort_connection(ep, skb, GFP_KERNEL);
  858. return;
  859. }
  860. ep->plen = (u8) plen;
  861. /*
  862. * If we don't have all the pdata yet, then bail.
  863. */
  864. if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
  865. return;
  866. /*
  867. * If we get here we have accumulated the entire mpa
  868. * start reply message including private data.
  869. */
  870. ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
  871. ep->mpa_attr.recv_marker_enabled = markers_enabled;
  872. ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
  873. ep->mpa_attr.version = mpa_rev;
  874. PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
  875. "xmit_marker_enabled=%d, version=%d\n", __func__,
  876. ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
  877. ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
  878. state_set(&ep->com, MPA_REQ_RCVD);
  879. /* drive upcall */
  880. connect_request_upcall(ep);
  881. return;
  882. }
  883. static int rx_data(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  884. {
  885. struct iwch_ep *ep = ctx;
  886. struct cpl_rx_data *hdr = cplhdr(skb);
  887. unsigned int dlen = ntohs(hdr->len);
  888. PDBG("%s ep %p dlen %u\n", __func__, ep, dlen);
  889. skb_pull(skb, sizeof(*hdr));
  890. skb_trim(skb, dlen);
  891. ep->rcv_seq += dlen;
  892. BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
  893. switch (state_read(&ep->com)) {
  894. case MPA_REQ_SENT:
  895. process_mpa_reply(ep, skb);
  896. break;
  897. case MPA_REQ_WAIT:
  898. process_mpa_request(ep, skb);
  899. break;
  900. case MPA_REP_SENT:
  901. break;
  902. default:
  903. printk(KERN_ERR MOD "%s Unexpected streaming data."
  904. " ep %p state %d tid %d\n",
  905. __func__, ep, state_read(&ep->com), ep->hwtid);
  906. /*
  907. * The ep will timeout and inform the ULP of the failure.
  908. * See ep_timeout().
  909. */
  910. break;
  911. }
  912. /* update RX credits */
  913. update_rx_credits(ep, dlen);
  914. return CPL_RET_BUF_DONE;
  915. }
  916. /*
  917. * Upcall from the adapter indicating data has been transmitted.
  918. * For us its just the single MPA request or reply. We can now free
  919. * the skb holding the mpa message.
  920. */
  921. static int tx_ack(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  922. {
  923. struct iwch_ep *ep = ctx;
  924. struct cpl_wr_ack *hdr = cplhdr(skb);
  925. unsigned int credits = ntohs(hdr->credits);
  926. PDBG("%s ep %p credits %u\n", __func__, ep, credits);
  927. if (credits == 0)
  928. return CPL_RET_BUF_DONE;
  929. BUG_ON(credits != 1);
  930. BUG_ON(ep->mpa_skb == NULL);
  931. kfree_skb(ep->mpa_skb);
  932. ep->mpa_skb = NULL;
  933. dst_confirm(ep->dst);
  934. if (state_read(&ep->com) == MPA_REP_SENT) {
  935. ep->com.rpl_done = 1;
  936. PDBG("waking up ep %p\n", ep);
  937. wake_up(&ep->com.waitq);
  938. }
  939. return CPL_RET_BUF_DONE;
  940. }
  941. static int abort_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  942. {
  943. struct iwch_ep *ep = ctx;
  944. PDBG("%s ep %p\n", __func__, ep);
  945. /*
  946. * We get 2 abort replies from the HW. The first one must
  947. * be ignored except for scribbling that we need one more.
  948. */
  949. if (!(ep->flags & ABORT_REQ_IN_PROGRESS)) {
  950. ep->flags |= ABORT_REQ_IN_PROGRESS;
  951. return CPL_RET_BUF_DONE;
  952. }
  953. close_complete_upcall(ep);
  954. state_set(&ep->com, DEAD);
  955. release_ep_resources(ep);
  956. return CPL_RET_BUF_DONE;
  957. }
  958. /*
  959. * Return whether a failed active open has allocated a TID
  960. */
  961. static inline int act_open_has_tid(int status)
  962. {
  963. return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
  964. status != CPL_ERR_ARP_MISS;
  965. }
  966. static int act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  967. {
  968. struct iwch_ep *ep = ctx;
  969. struct cpl_act_open_rpl *rpl = cplhdr(skb);
  970. PDBG("%s ep %p status %u errno %d\n", __func__, ep, rpl->status,
  971. status2errno(rpl->status));
  972. connect_reply_upcall(ep, status2errno(rpl->status));
  973. state_set(&ep->com, DEAD);
  974. if (ep->com.tdev->type != T3A && act_open_has_tid(rpl->status))
  975. release_tid(ep->com.tdev, GET_TID(rpl), NULL);
  976. cxgb3_free_atid(ep->com.tdev, ep->atid);
  977. dst_release(ep->dst);
  978. l2t_release(L2DATA(ep->com.tdev), ep->l2t);
  979. put_ep(&ep->com);
  980. return CPL_RET_BUF_DONE;
  981. }
  982. static int listen_start(struct iwch_listen_ep *ep)
  983. {
  984. struct sk_buff *skb;
  985. struct cpl_pass_open_req *req;
  986. PDBG("%s ep %p\n", __func__, ep);
  987. skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
  988. if (!skb) {
  989. printk(KERN_ERR MOD "t3c_listen_start failed to alloc skb!\n");
  990. return -ENOMEM;
  991. }
  992. req = (struct cpl_pass_open_req *) skb_put(skb, sizeof(*req));
  993. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  994. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, ep->stid));
  995. req->local_port = ep->com.local_addr.sin_port;
  996. req->local_ip = ep->com.local_addr.sin_addr.s_addr;
  997. req->peer_port = 0;
  998. req->peer_ip = 0;
  999. req->peer_netmask = 0;
  1000. req->opt0h = htonl(F_DELACK | F_TCAM_BYPASS);
  1001. req->opt0l = htonl(V_RCV_BUFSIZ(rcv_win>>10));
  1002. req->opt1 = htonl(V_CONN_POLICY(CPL_CONN_POLICY_ASK));
  1003. skb->priority = 1;
  1004. cxgb3_ofld_send(ep->com.tdev, skb);
  1005. return 0;
  1006. }
  1007. static int pass_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1008. {
  1009. struct iwch_listen_ep *ep = ctx;
  1010. struct cpl_pass_open_rpl *rpl = cplhdr(skb);
  1011. PDBG("%s ep %p status %d error %d\n", __func__, ep,
  1012. rpl->status, status2errno(rpl->status));
  1013. ep->com.rpl_err = status2errno(rpl->status);
  1014. ep->com.rpl_done = 1;
  1015. wake_up(&ep->com.waitq);
  1016. return CPL_RET_BUF_DONE;
  1017. }
  1018. static int listen_stop(struct iwch_listen_ep *ep)
  1019. {
  1020. struct sk_buff *skb;
  1021. struct cpl_close_listserv_req *req;
  1022. PDBG("%s ep %p\n", __func__, ep);
  1023. skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
  1024. if (!skb) {
  1025. printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
  1026. return -ENOMEM;
  1027. }
  1028. req = (struct cpl_close_listserv_req *) skb_put(skb, sizeof(*req));
  1029. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  1030. req->cpu_idx = 0;
  1031. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, ep->stid));
  1032. skb->priority = 1;
  1033. cxgb3_ofld_send(ep->com.tdev, skb);
  1034. return 0;
  1035. }
  1036. static int close_listsrv_rpl(struct t3cdev *tdev, struct sk_buff *skb,
  1037. void *ctx)
  1038. {
  1039. struct iwch_listen_ep *ep = ctx;
  1040. struct cpl_close_listserv_rpl *rpl = cplhdr(skb);
  1041. PDBG("%s ep %p\n", __func__, ep);
  1042. ep->com.rpl_err = status2errno(rpl->status);
  1043. ep->com.rpl_done = 1;
  1044. wake_up(&ep->com.waitq);
  1045. return CPL_RET_BUF_DONE;
  1046. }
  1047. static void accept_cr(struct iwch_ep *ep, __be32 peer_ip, struct sk_buff *skb)
  1048. {
  1049. struct cpl_pass_accept_rpl *rpl;
  1050. unsigned int mtu_idx;
  1051. u32 opt0h, opt0l, opt2;
  1052. int wscale;
  1053. PDBG("%s ep %p\n", __func__, ep);
  1054. BUG_ON(skb_cloned(skb));
  1055. skb_trim(skb, sizeof(*rpl));
  1056. skb_get(skb);
  1057. mtu_idx = find_best_mtu(T3C_DATA(ep->com.tdev), dst_mtu(ep->dst));
  1058. wscale = compute_wscale(rcv_win);
  1059. opt0h = V_NAGLE(0) |
  1060. V_NO_CONG(nocong) |
  1061. V_KEEP_ALIVE(1) |
  1062. F_TCAM_BYPASS |
  1063. V_WND_SCALE(wscale) |
  1064. V_MSS_IDX(mtu_idx) |
  1065. V_L2T_IDX(ep->l2t->idx) | V_TX_CHANNEL(ep->l2t->smt_idx);
  1066. opt0l = V_TOS((ep->tos >> 2) & M_TOS) | V_RCV_BUFSIZ(rcv_win>>10);
  1067. opt2 = V_FLAVORS_VALID(1) | V_CONG_CONTROL_FLAVOR(cong_flavor);
  1068. rpl = cplhdr(skb);
  1069. rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  1070. OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL, ep->hwtid));
  1071. rpl->peer_ip = peer_ip;
  1072. rpl->opt0h = htonl(opt0h);
  1073. rpl->opt0l_status = htonl(opt0l | CPL_PASS_OPEN_ACCEPT);
  1074. rpl->opt2 = htonl(opt2);
  1075. rpl->rsvd = rpl->opt2; /* workaround for HW bug */
  1076. skb->priority = CPL_PRIORITY_SETUP;
  1077. l2t_send(ep->com.tdev, skb, ep->l2t);
  1078. return;
  1079. }
  1080. static void reject_cr(struct t3cdev *tdev, u32 hwtid, __be32 peer_ip,
  1081. struct sk_buff *skb)
  1082. {
  1083. PDBG("%s t3cdev %p tid %u peer_ip %x\n", __func__, tdev, hwtid,
  1084. peer_ip);
  1085. BUG_ON(skb_cloned(skb));
  1086. skb_trim(skb, sizeof(struct cpl_tid_release));
  1087. skb_get(skb);
  1088. if (tdev->type != T3A)
  1089. release_tid(tdev, hwtid, skb);
  1090. else {
  1091. struct cpl_pass_accept_rpl *rpl;
  1092. rpl = cplhdr(skb);
  1093. skb->priority = CPL_PRIORITY_SETUP;
  1094. rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  1095. OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
  1096. hwtid));
  1097. rpl->peer_ip = peer_ip;
  1098. rpl->opt0h = htonl(F_TCAM_BYPASS);
  1099. rpl->opt0l_status = htonl(CPL_PASS_OPEN_REJECT);
  1100. rpl->opt2 = 0;
  1101. rpl->rsvd = rpl->opt2;
  1102. cxgb3_ofld_send(tdev, skb);
  1103. }
  1104. }
  1105. static int pass_accept_req(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1106. {
  1107. struct iwch_ep *child_ep, *parent_ep = ctx;
  1108. struct cpl_pass_accept_req *req = cplhdr(skb);
  1109. unsigned int hwtid = GET_TID(req);
  1110. struct dst_entry *dst;
  1111. struct l2t_entry *l2t;
  1112. struct rtable *rt;
  1113. struct iff_mac tim;
  1114. PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
  1115. if (state_read(&parent_ep->com) != LISTEN) {
  1116. printk(KERN_ERR "%s - listening ep not in LISTEN\n",
  1117. __func__);
  1118. goto reject;
  1119. }
  1120. /*
  1121. * Find the netdev for this connection request.
  1122. */
  1123. tim.mac_addr = req->dst_mac;
  1124. tim.vlan_tag = ntohs(req->vlan_tag);
  1125. if (tdev->ctl(tdev, GET_IFF_FROM_MAC, &tim) < 0 || !tim.dev) {
  1126. printk(KERN_ERR
  1127. "%s bad dst mac %02x %02x %02x %02x %02x %02x\n",
  1128. __func__,
  1129. req->dst_mac[0],
  1130. req->dst_mac[1],
  1131. req->dst_mac[2],
  1132. req->dst_mac[3],
  1133. req->dst_mac[4],
  1134. req->dst_mac[5]);
  1135. goto reject;
  1136. }
  1137. /* Find output route */
  1138. rt = find_route(tdev,
  1139. req->local_ip,
  1140. req->peer_ip,
  1141. req->local_port,
  1142. req->peer_port, G_PASS_OPEN_TOS(ntohl(req->tos_tid)));
  1143. if (!rt) {
  1144. printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
  1145. __func__);
  1146. goto reject;
  1147. }
  1148. dst = &rt->u.dst;
  1149. l2t = t3_l2t_get(tdev, dst->neighbour, dst->neighbour->dev);
  1150. if (!l2t) {
  1151. printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
  1152. __func__);
  1153. dst_release(dst);
  1154. goto reject;
  1155. }
  1156. child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
  1157. if (!child_ep) {
  1158. printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
  1159. __func__);
  1160. l2t_release(L2DATA(tdev), l2t);
  1161. dst_release(dst);
  1162. goto reject;
  1163. }
  1164. state_set(&child_ep->com, CONNECTING);
  1165. child_ep->com.tdev = tdev;
  1166. child_ep->com.cm_id = NULL;
  1167. child_ep->com.local_addr.sin_family = PF_INET;
  1168. child_ep->com.local_addr.sin_port = req->local_port;
  1169. child_ep->com.local_addr.sin_addr.s_addr = req->local_ip;
  1170. child_ep->com.remote_addr.sin_family = PF_INET;
  1171. child_ep->com.remote_addr.sin_port = req->peer_port;
  1172. child_ep->com.remote_addr.sin_addr.s_addr = req->peer_ip;
  1173. get_ep(&parent_ep->com);
  1174. child_ep->parent_ep = parent_ep;
  1175. child_ep->tos = G_PASS_OPEN_TOS(ntohl(req->tos_tid));
  1176. child_ep->l2t = l2t;
  1177. child_ep->dst = dst;
  1178. child_ep->hwtid = hwtid;
  1179. init_timer(&child_ep->timer);
  1180. cxgb3_insert_tid(tdev, &t3c_client, child_ep, hwtid);
  1181. accept_cr(child_ep, req->peer_ip, skb);
  1182. goto out;
  1183. reject:
  1184. reject_cr(tdev, hwtid, req->peer_ip, skb);
  1185. out:
  1186. return CPL_RET_BUF_DONE;
  1187. }
  1188. static int pass_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1189. {
  1190. struct iwch_ep *ep = ctx;
  1191. struct cpl_pass_establish *req = cplhdr(skb);
  1192. PDBG("%s ep %p\n", __func__, ep);
  1193. ep->snd_seq = ntohl(req->snd_isn);
  1194. ep->rcv_seq = ntohl(req->rcv_isn);
  1195. set_emss(ep, ntohs(req->tcp_opt));
  1196. dst_confirm(ep->dst);
  1197. state_set(&ep->com, MPA_REQ_WAIT);
  1198. start_ep_timer(ep);
  1199. return CPL_RET_BUF_DONE;
  1200. }
  1201. static int peer_close(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1202. {
  1203. struct iwch_ep *ep = ctx;
  1204. struct iwch_qp_attributes attrs;
  1205. unsigned long flags;
  1206. int disconnect = 1;
  1207. int release = 0;
  1208. PDBG("%s ep %p\n", __func__, ep);
  1209. dst_confirm(ep->dst);
  1210. spin_lock_irqsave(&ep->com.lock, flags);
  1211. switch (ep->com.state) {
  1212. case MPA_REQ_WAIT:
  1213. __state_set(&ep->com, CLOSING);
  1214. break;
  1215. case MPA_REQ_SENT:
  1216. __state_set(&ep->com, CLOSING);
  1217. connect_reply_upcall(ep, -ECONNRESET);
  1218. break;
  1219. case MPA_REQ_RCVD:
  1220. /*
  1221. * We're gonna mark this puppy DEAD, but keep
  1222. * the reference on it until the ULP accepts or
  1223. * rejects the CR.
  1224. */
  1225. __state_set(&ep->com, CLOSING);
  1226. get_ep(&ep->com);
  1227. break;
  1228. case MPA_REP_SENT:
  1229. __state_set(&ep->com, CLOSING);
  1230. ep->com.rpl_done = 1;
  1231. ep->com.rpl_err = -ECONNRESET;
  1232. PDBG("waking up ep %p\n", ep);
  1233. wake_up(&ep->com.waitq);
  1234. break;
  1235. case FPDU_MODE:
  1236. start_ep_timer(ep);
  1237. __state_set(&ep->com, CLOSING);
  1238. attrs.next_state = IWCH_QP_STATE_CLOSING;
  1239. iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
  1240. IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
  1241. peer_close_upcall(ep);
  1242. break;
  1243. case ABORTING:
  1244. disconnect = 0;
  1245. break;
  1246. case CLOSING:
  1247. __state_set(&ep->com, MORIBUND);
  1248. disconnect = 0;
  1249. break;
  1250. case MORIBUND:
  1251. stop_ep_timer(ep);
  1252. if (ep->com.cm_id && ep->com.qp) {
  1253. attrs.next_state = IWCH_QP_STATE_IDLE;
  1254. iwch_modify_qp(ep->com.qp->rhp, ep->com.qp,
  1255. IWCH_QP_ATTR_NEXT_STATE, &attrs, 1);
  1256. }
  1257. close_complete_upcall(ep);
  1258. __state_set(&ep->com, DEAD);
  1259. release = 1;
  1260. disconnect = 0;
  1261. break;
  1262. case DEAD:
  1263. disconnect = 0;
  1264. break;
  1265. default:
  1266. BUG_ON(1);
  1267. }
  1268. spin_unlock_irqrestore(&ep->com.lock, flags);
  1269. if (disconnect)
  1270. iwch_ep_disconnect(ep, 0, GFP_KERNEL);
  1271. if (release)
  1272. release_ep_resources(ep);
  1273. return CPL_RET_BUF_DONE;
  1274. }
  1275. /*
  1276. * Returns whether an ABORT_REQ_RSS message is a negative advice.
  1277. */
  1278. static int is_neg_adv_abort(unsigned int status)
  1279. {
  1280. return status == CPL_ERR_RTX_NEG_ADVICE ||
  1281. status == CPL_ERR_PERSIST_NEG_ADVICE;
  1282. }
  1283. static int peer_abort(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1284. {
  1285. struct cpl_abort_req_rss *req = cplhdr(skb);
  1286. struct iwch_ep *ep = ctx;
  1287. struct cpl_abort_rpl *rpl;
  1288. struct sk_buff *rpl_skb;
  1289. struct iwch_qp_attributes attrs;
  1290. int ret;
  1291. int state;
  1292. if (is_neg_adv_abort(req->status)) {
  1293. PDBG("%s neg_adv_abort ep %p tid %d\n", __func__, ep,
  1294. ep->hwtid);
  1295. t3_l2t_send_event(ep->com.tdev, ep->l2t);
  1296. return CPL_RET_BUF_DONE;
  1297. }
  1298. /*
  1299. * We get 2 peer aborts from the HW. The first one must
  1300. * be ignored except for scribbling that we need one more.
  1301. */
  1302. if (!(ep->flags & PEER_ABORT_IN_PROGRESS)) {
  1303. ep->flags |= PEER_ABORT_IN_PROGRESS;
  1304. return CPL_RET_BUF_DONE;
  1305. }
  1306. state = state_read(&ep->com);
  1307. PDBG("%s ep %p state %u\n", __func__, ep, state);
  1308. switch (state) {
  1309. case CONNECTING:
  1310. break;
  1311. case MPA_REQ_WAIT:
  1312. stop_ep_timer(ep);
  1313. break;
  1314. case MPA_REQ_SENT:
  1315. stop_ep_timer(ep);
  1316. connect_reply_upcall(ep, -ECONNRESET);
  1317. break;
  1318. case MPA_REP_SENT:
  1319. ep->com.rpl_done = 1;
  1320. ep->com.rpl_err = -ECONNRESET;
  1321. PDBG("waking up ep %p\n", ep);
  1322. wake_up(&ep->com.waitq);
  1323. break;
  1324. case MPA_REQ_RCVD:
  1325. /*
  1326. * We're gonna mark this puppy DEAD, but keep
  1327. * the reference on it until the ULP accepts or
  1328. * rejects the CR.
  1329. */
  1330. get_ep(&ep->com);
  1331. break;
  1332. case MORIBUND:
  1333. case CLOSING:
  1334. stop_ep_timer(ep);
  1335. /*FALLTHROUGH*/
  1336. case FPDU_MODE:
  1337. if (ep->com.cm_id && ep->com.qp) {
  1338. attrs.next_state = IWCH_QP_STATE_ERROR;
  1339. ret = iwch_modify_qp(ep->com.qp->rhp,
  1340. ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
  1341. &attrs, 1);
  1342. if (ret)
  1343. printk(KERN_ERR MOD
  1344. "%s - qp <- error failed!\n",
  1345. __func__);
  1346. }
  1347. peer_abort_upcall(ep);
  1348. break;
  1349. case ABORTING:
  1350. break;
  1351. case DEAD:
  1352. PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
  1353. return CPL_RET_BUF_DONE;
  1354. default:
  1355. BUG_ON(1);
  1356. break;
  1357. }
  1358. dst_confirm(ep->dst);
  1359. rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
  1360. if (!rpl_skb) {
  1361. printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
  1362. __func__);
  1363. dst_release(ep->dst);
  1364. l2t_release(L2DATA(ep->com.tdev), ep->l2t);
  1365. put_ep(&ep->com);
  1366. return CPL_RET_BUF_DONE;
  1367. }
  1368. rpl_skb->priority = CPL_PRIORITY_DATA;
  1369. rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
  1370. rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
  1371. rpl->wr.wr_lo = htonl(V_WR_TID(ep->hwtid));
  1372. OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
  1373. rpl->cmd = CPL_ABORT_NO_RST;
  1374. cxgb3_ofld_send(ep->com.tdev, rpl_skb);
  1375. if (state != ABORTING) {
  1376. state_set(&ep->com, DEAD);
  1377. release_ep_resources(ep);
  1378. }
  1379. return CPL_RET_BUF_DONE;
  1380. }
  1381. static int close_con_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1382. {
  1383. struct iwch_ep *ep = ctx;
  1384. struct iwch_qp_attributes attrs;
  1385. unsigned long flags;
  1386. int release = 0;
  1387. PDBG("%s ep %p\n", __func__, ep);
  1388. BUG_ON(!ep);
  1389. /* The cm_id may be null if we failed to connect */
  1390. spin_lock_irqsave(&ep->com.lock, flags);
  1391. switch (ep->com.state) {
  1392. case CLOSING:
  1393. __state_set(&ep->com, MORIBUND);
  1394. break;
  1395. case MORIBUND:
  1396. stop_ep_timer(ep);
  1397. if ((ep->com.cm_id) && (ep->com.qp)) {
  1398. attrs.next_state = IWCH_QP_STATE_IDLE;
  1399. iwch_modify_qp(ep->com.qp->rhp,
  1400. ep->com.qp,
  1401. IWCH_QP_ATTR_NEXT_STATE,
  1402. &attrs, 1);
  1403. }
  1404. close_complete_upcall(ep);
  1405. __state_set(&ep->com, DEAD);
  1406. release = 1;
  1407. break;
  1408. case ABORTING:
  1409. break;
  1410. case DEAD:
  1411. default:
  1412. BUG_ON(1);
  1413. break;
  1414. }
  1415. spin_unlock_irqrestore(&ep->com.lock, flags);
  1416. if (release)
  1417. release_ep_resources(ep);
  1418. return CPL_RET_BUF_DONE;
  1419. }
  1420. /*
  1421. * T3A does 3 things when a TERM is received:
  1422. * 1) send up a CPL_RDMA_TERMINATE message with the TERM packet
  1423. * 2) generate an async event on the QP with the TERMINATE opcode
  1424. * 3) post a TERMINATE opcde cqe into the associated CQ.
  1425. *
  1426. * For (1), we save the message in the qp for later consumer consumption.
  1427. * For (2), we move the QP into TERMINATE, post a QP event and disconnect.
  1428. * For (3), we toss the CQE in cxio_poll_cq().
  1429. *
  1430. * terminate() handles case (1)...
  1431. */
  1432. static int terminate(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1433. {
  1434. struct iwch_ep *ep = ctx;
  1435. PDBG("%s ep %p\n", __func__, ep);
  1436. skb_pull(skb, sizeof(struct cpl_rdma_terminate));
  1437. PDBG("%s saving %d bytes of term msg\n", __func__, skb->len);
  1438. skb_copy_from_linear_data(skb, ep->com.qp->attr.terminate_buffer,
  1439. skb->len);
  1440. ep->com.qp->attr.terminate_msg_len = skb->len;
  1441. ep->com.qp->attr.is_terminate_local = 0;
  1442. return CPL_RET_BUF_DONE;
  1443. }
  1444. static int ec_status(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1445. {
  1446. struct cpl_rdma_ec_status *rep = cplhdr(skb);
  1447. struct iwch_ep *ep = ctx;
  1448. PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid,
  1449. rep->status);
  1450. if (rep->status) {
  1451. struct iwch_qp_attributes attrs;
  1452. printk(KERN_ERR MOD "%s BAD CLOSE - Aborting tid %u\n",
  1453. __func__, ep->hwtid);
  1454. stop_ep_timer(ep);
  1455. attrs.next_state = IWCH_QP_STATE_ERROR;
  1456. iwch_modify_qp(ep->com.qp->rhp,
  1457. ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
  1458. &attrs, 1);
  1459. abort_connection(ep, NULL, GFP_KERNEL);
  1460. }
  1461. return CPL_RET_BUF_DONE;
  1462. }
  1463. static void ep_timeout(unsigned long arg)
  1464. {
  1465. struct iwch_ep *ep = (struct iwch_ep *)arg;
  1466. struct iwch_qp_attributes attrs;
  1467. unsigned long flags;
  1468. spin_lock_irqsave(&ep->com.lock, flags);
  1469. PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
  1470. ep->com.state);
  1471. switch (ep->com.state) {
  1472. case MPA_REQ_SENT:
  1473. connect_reply_upcall(ep, -ETIMEDOUT);
  1474. break;
  1475. case MPA_REQ_WAIT:
  1476. break;
  1477. case CLOSING:
  1478. case MORIBUND:
  1479. if (ep->com.cm_id && ep->com.qp) {
  1480. attrs.next_state = IWCH_QP_STATE_ERROR;
  1481. iwch_modify_qp(ep->com.qp->rhp,
  1482. ep->com.qp, IWCH_QP_ATTR_NEXT_STATE,
  1483. &attrs, 1);
  1484. }
  1485. break;
  1486. default:
  1487. BUG();
  1488. }
  1489. __state_set(&ep->com, CLOSING);
  1490. spin_unlock_irqrestore(&ep->com.lock, flags);
  1491. abort_connection(ep, NULL, GFP_ATOMIC);
  1492. put_ep(&ep->com);
  1493. }
  1494. int iwch_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
  1495. {
  1496. int err;
  1497. struct iwch_ep *ep = to_ep(cm_id);
  1498. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1499. if (state_read(&ep->com) == DEAD) {
  1500. put_ep(&ep->com);
  1501. return -ECONNRESET;
  1502. }
  1503. BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
  1504. if (mpa_rev == 0)
  1505. abort_connection(ep, NULL, GFP_KERNEL);
  1506. else {
  1507. err = send_mpa_reject(ep, pdata, pdata_len);
  1508. err = iwch_ep_disconnect(ep, 0, GFP_KERNEL);
  1509. }
  1510. return 0;
  1511. }
  1512. int iwch_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  1513. {
  1514. int err;
  1515. struct iwch_qp_attributes attrs;
  1516. enum iwch_qp_attr_mask mask;
  1517. struct iwch_ep *ep = to_ep(cm_id);
  1518. struct iwch_dev *h = to_iwch_dev(cm_id->device);
  1519. struct iwch_qp *qp = get_qhp(h, conn_param->qpn);
  1520. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1521. if (state_read(&ep->com) == DEAD)
  1522. return -ECONNRESET;
  1523. BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
  1524. BUG_ON(!qp);
  1525. if ((conn_param->ord > qp->rhp->attr.max_rdma_read_qp_depth) ||
  1526. (conn_param->ird > qp->rhp->attr.max_rdma_reads_per_qp)) {
  1527. abort_connection(ep, NULL, GFP_KERNEL);
  1528. return -EINVAL;
  1529. }
  1530. cm_id->add_ref(cm_id);
  1531. ep->com.cm_id = cm_id;
  1532. ep->com.qp = qp;
  1533. ep->com.rpl_done = 0;
  1534. ep->com.rpl_err = 0;
  1535. ep->ird = conn_param->ird;
  1536. ep->ord = conn_param->ord;
  1537. PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
  1538. get_ep(&ep->com);
  1539. /* bind QP to EP and move to RTS */
  1540. attrs.mpa_attr = ep->mpa_attr;
  1541. attrs.max_ird = ep->ird;
  1542. attrs.max_ord = ep->ord;
  1543. attrs.llp_stream_handle = ep;
  1544. attrs.next_state = IWCH_QP_STATE_RTS;
  1545. /* bind QP and TID with INIT_WR */
  1546. mask = IWCH_QP_ATTR_NEXT_STATE |
  1547. IWCH_QP_ATTR_LLP_STREAM_HANDLE |
  1548. IWCH_QP_ATTR_MPA_ATTR |
  1549. IWCH_QP_ATTR_MAX_IRD |
  1550. IWCH_QP_ATTR_MAX_ORD;
  1551. err = iwch_modify_qp(ep->com.qp->rhp,
  1552. ep->com.qp, mask, &attrs, 1);
  1553. if (err)
  1554. goto err;
  1555. err = send_mpa_reply(ep, conn_param->private_data,
  1556. conn_param->private_data_len);
  1557. if (err)
  1558. goto err;
  1559. /* wait for wr_ack */
  1560. wait_event(ep->com.waitq, ep->com.rpl_done);
  1561. err = ep->com.rpl_err;
  1562. if (err)
  1563. goto err;
  1564. state_set(&ep->com, FPDU_MODE);
  1565. established_upcall(ep);
  1566. put_ep(&ep->com);
  1567. return 0;
  1568. err:
  1569. ep->com.cm_id = NULL;
  1570. ep->com.qp = NULL;
  1571. cm_id->rem_ref(cm_id);
  1572. put_ep(&ep->com);
  1573. return err;
  1574. }
  1575. static int is_loopback_dst(struct iw_cm_id *cm_id)
  1576. {
  1577. struct net_device *dev;
  1578. dev = ip_dev_find(&init_net, cm_id->remote_addr.sin_addr.s_addr);
  1579. if (!dev)
  1580. return 0;
  1581. dev_put(dev);
  1582. return 1;
  1583. }
  1584. int iwch_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  1585. {
  1586. int err = 0;
  1587. struct iwch_dev *h = to_iwch_dev(cm_id->device);
  1588. struct iwch_ep *ep;
  1589. struct rtable *rt;
  1590. if (is_loopback_dst(cm_id)) {
  1591. err = -ENOSYS;
  1592. goto out;
  1593. }
  1594. ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
  1595. if (!ep) {
  1596. printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
  1597. err = -ENOMEM;
  1598. goto out;
  1599. }
  1600. init_timer(&ep->timer);
  1601. ep->plen = conn_param->private_data_len;
  1602. if (ep->plen)
  1603. memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
  1604. conn_param->private_data, ep->plen);
  1605. ep->ird = conn_param->ird;
  1606. ep->ord = conn_param->ord;
  1607. ep->com.tdev = h->rdev.t3cdev_p;
  1608. cm_id->add_ref(cm_id);
  1609. ep->com.cm_id = cm_id;
  1610. ep->com.qp = get_qhp(h, conn_param->qpn);
  1611. BUG_ON(!ep->com.qp);
  1612. PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
  1613. ep->com.qp, cm_id);
  1614. /*
  1615. * Allocate an active TID to initiate a TCP connection.
  1616. */
  1617. ep->atid = cxgb3_alloc_atid(h->rdev.t3cdev_p, &t3c_client, ep);
  1618. if (ep->atid == -1) {
  1619. printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
  1620. err = -ENOMEM;
  1621. goto fail2;
  1622. }
  1623. /* find a route */
  1624. rt = find_route(h->rdev.t3cdev_p,
  1625. cm_id->local_addr.sin_addr.s_addr,
  1626. cm_id->remote_addr.sin_addr.s_addr,
  1627. cm_id->local_addr.sin_port,
  1628. cm_id->remote_addr.sin_port, IPTOS_LOWDELAY);
  1629. if (!rt) {
  1630. printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
  1631. err = -EHOSTUNREACH;
  1632. goto fail3;
  1633. }
  1634. ep->dst = &rt->u.dst;
  1635. /* get a l2t entry */
  1636. ep->l2t = t3_l2t_get(ep->com.tdev, ep->dst->neighbour,
  1637. ep->dst->neighbour->dev);
  1638. if (!ep->l2t) {
  1639. printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
  1640. err = -ENOMEM;
  1641. goto fail4;
  1642. }
  1643. state_set(&ep->com, CONNECTING);
  1644. ep->tos = IPTOS_LOWDELAY;
  1645. ep->com.local_addr = cm_id->local_addr;
  1646. ep->com.remote_addr = cm_id->remote_addr;
  1647. /* send connect request to rnic */
  1648. err = send_connect(ep);
  1649. if (!err)
  1650. goto out;
  1651. l2t_release(L2DATA(h->rdev.t3cdev_p), ep->l2t);
  1652. fail4:
  1653. dst_release(ep->dst);
  1654. fail3:
  1655. cxgb3_free_atid(ep->com.tdev, ep->atid);
  1656. fail2:
  1657. put_ep(&ep->com);
  1658. out:
  1659. return err;
  1660. }
  1661. int iwch_create_listen(struct iw_cm_id *cm_id, int backlog)
  1662. {
  1663. int err = 0;
  1664. struct iwch_dev *h = to_iwch_dev(cm_id->device);
  1665. struct iwch_listen_ep *ep;
  1666. might_sleep();
  1667. ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
  1668. if (!ep) {
  1669. printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
  1670. err = -ENOMEM;
  1671. goto fail1;
  1672. }
  1673. PDBG("%s ep %p\n", __func__, ep);
  1674. ep->com.tdev = h->rdev.t3cdev_p;
  1675. cm_id->add_ref(cm_id);
  1676. ep->com.cm_id = cm_id;
  1677. ep->backlog = backlog;
  1678. ep->com.local_addr = cm_id->local_addr;
  1679. /*
  1680. * Allocate a server TID.
  1681. */
  1682. ep->stid = cxgb3_alloc_stid(h->rdev.t3cdev_p, &t3c_client, ep);
  1683. if (ep->stid == -1) {
  1684. printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
  1685. err = -ENOMEM;
  1686. goto fail2;
  1687. }
  1688. state_set(&ep->com, LISTEN);
  1689. err = listen_start(ep);
  1690. if (err)
  1691. goto fail3;
  1692. /* wait for pass_open_rpl */
  1693. wait_event(ep->com.waitq, ep->com.rpl_done);
  1694. err = ep->com.rpl_err;
  1695. if (!err) {
  1696. cm_id->provider_data = ep;
  1697. goto out;
  1698. }
  1699. fail3:
  1700. cxgb3_free_stid(ep->com.tdev, ep->stid);
  1701. fail2:
  1702. cm_id->rem_ref(cm_id);
  1703. put_ep(&ep->com);
  1704. fail1:
  1705. out:
  1706. return err;
  1707. }
  1708. int iwch_destroy_listen(struct iw_cm_id *cm_id)
  1709. {
  1710. int err;
  1711. struct iwch_listen_ep *ep = to_listen_ep(cm_id);
  1712. PDBG("%s ep %p\n", __func__, ep);
  1713. might_sleep();
  1714. state_set(&ep->com, DEAD);
  1715. ep->com.rpl_done = 0;
  1716. ep->com.rpl_err = 0;
  1717. err = listen_stop(ep);
  1718. wait_event(ep->com.waitq, ep->com.rpl_done);
  1719. cxgb3_free_stid(ep->com.tdev, ep->stid);
  1720. err = ep->com.rpl_err;
  1721. cm_id->rem_ref(cm_id);
  1722. put_ep(&ep->com);
  1723. return err;
  1724. }
  1725. int iwch_ep_disconnect(struct iwch_ep *ep, int abrupt, gfp_t gfp)
  1726. {
  1727. int ret=0;
  1728. unsigned long flags;
  1729. int close = 0;
  1730. spin_lock_irqsave(&ep->com.lock, flags);
  1731. PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
  1732. states[ep->com.state], abrupt);
  1733. if (ep->com.state == DEAD) {
  1734. PDBG("%s already dead ep %p\n", __func__, ep);
  1735. goto out;
  1736. }
  1737. if (abrupt) {
  1738. if (ep->com.state != ABORTING) {
  1739. ep->com.state = ABORTING;
  1740. close = 1;
  1741. }
  1742. goto out;
  1743. }
  1744. switch (ep->com.state) {
  1745. case MPA_REQ_WAIT:
  1746. case MPA_REQ_SENT:
  1747. case MPA_REQ_RCVD:
  1748. case MPA_REP_SENT:
  1749. case FPDU_MODE:
  1750. start_ep_timer(ep);
  1751. ep->com.state = CLOSING;
  1752. close = 1;
  1753. break;
  1754. case CLOSING:
  1755. ep->com.state = MORIBUND;
  1756. close = 1;
  1757. break;
  1758. case MORIBUND:
  1759. break;
  1760. default:
  1761. BUG();
  1762. break;
  1763. }
  1764. out:
  1765. spin_unlock_irqrestore(&ep->com.lock, flags);
  1766. if (close) {
  1767. if (abrupt)
  1768. ret = send_abort(ep, NULL, gfp);
  1769. else
  1770. ret = send_halfclose(ep, gfp);
  1771. }
  1772. return ret;
  1773. }
  1774. int iwch_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
  1775. struct l2t_entry *l2t)
  1776. {
  1777. struct iwch_ep *ep = ctx;
  1778. if (ep->dst != old)
  1779. return 0;
  1780. PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
  1781. l2t);
  1782. dst_hold(new);
  1783. l2t_release(L2DATA(ep->com.tdev), ep->l2t);
  1784. ep->l2t = l2t;
  1785. dst_release(old);
  1786. ep->dst = new;
  1787. return 1;
  1788. }
  1789. /*
  1790. * All the CM events are handled on a work queue to have a safe context.
  1791. */
  1792. static int sched(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1793. {
  1794. struct iwch_ep_common *epc = ctx;
  1795. get_ep(epc);
  1796. /*
  1797. * Save ctx and tdev in the skb->cb area.
  1798. */
  1799. *((void **) skb->cb) = ctx;
  1800. *((struct t3cdev **) (skb->cb + sizeof(void *))) = tdev;
  1801. /*
  1802. * Queue the skb and schedule the worker thread.
  1803. */
  1804. skb_queue_tail(&rxq, skb);
  1805. queue_work(workq, &skb_work);
  1806. return 0;
  1807. }
  1808. static int set_tcb_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  1809. {
  1810. struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
  1811. if (rpl->status != CPL_ERR_NONE) {
  1812. printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
  1813. "for tid %u\n", rpl->status, GET_TID(rpl));
  1814. }
  1815. return CPL_RET_BUF_DONE;
  1816. }
  1817. int __init iwch_cm_init(void)
  1818. {
  1819. skb_queue_head_init(&rxq);
  1820. workq = create_singlethread_workqueue("iw_cxgb3");
  1821. if (!workq)
  1822. return -ENOMEM;
  1823. /*
  1824. * All upcalls from the T3 Core go to sched() to
  1825. * schedule the processing on a work queue.
  1826. */
  1827. t3c_handlers[CPL_ACT_ESTABLISH] = sched;
  1828. t3c_handlers[CPL_ACT_OPEN_RPL] = sched;
  1829. t3c_handlers[CPL_RX_DATA] = sched;
  1830. t3c_handlers[CPL_TX_DMA_ACK] = sched;
  1831. t3c_handlers[CPL_ABORT_RPL_RSS] = sched;
  1832. t3c_handlers[CPL_ABORT_RPL] = sched;
  1833. t3c_handlers[CPL_PASS_OPEN_RPL] = sched;
  1834. t3c_handlers[CPL_CLOSE_LISTSRV_RPL] = sched;
  1835. t3c_handlers[CPL_PASS_ACCEPT_REQ] = sched;
  1836. t3c_handlers[CPL_PASS_ESTABLISH] = sched;
  1837. t3c_handlers[CPL_PEER_CLOSE] = sched;
  1838. t3c_handlers[CPL_CLOSE_CON_RPL] = sched;
  1839. t3c_handlers[CPL_ABORT_REQ_RSS] = sched;
  1840. t3c_handlers[CPL_RDMA_TERMINATE] = sched;
  1841. t3c_handlers[CPL_RDMA_EC_STATUS] = sched;
  1842. t3c_handlers[CPL_SET_TCB_RPL] = set_tcb_rpl;
  1843. /*
  1844. * These are the real handlers that are called from a
  1845. * work queue.
  1846. */
  1847. work_handlers[CPL_ACT_ESTABLISH] = act_establish;
  1848. work_handlers[CPL_ACT_OPEN_RPL] = act_open_rpl;
  1849. work_handlers[CPL_RX_DATA] = rx_data;
  1850. work_handlers[CPL_TX_DMA_ACK] = tx_ack;
  1851. work_handlers[CPL_ABORT_RPL_RSS] = abort_rpl;
  1852. work_handlers[CPL_ABORT_RPL] = abort_rpl;
  1853. work_handlers[CPL_PASS_OPEN_RPL] = pass_open_rpl;
  1854. work_handlers[CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl;
  1855. work_handlers[CPL_PASS_ACCEPT_REQ] = pass_accept_req;
  1856. work_handlers[CPL_PASS_ESTABLISH] = pass_establish;
  1857. work_handlers[CPL_PEER_CLOSE] = peer_close;
  1858. work_handlers[CPL_ABORT_REQ_RSS] = peer_abort;
  1859. work_handlers[CPL_CLOSE_CON_RPL] = close_con_rpl;
  1860. work_handlers[CPL_RDMA_TERMINATE] = terminate;
  1861. work_handlers[CPL_RDMA_EC_STATUS] = ec_status;
  1862. return 0;
  1863. }
  1864. void __exit iwch_cm_term(void)
  1865. {
  1866. flush_workqueue(workq);
  1867. destroy_workqueue(workq);
  1868. }