iwch_cm.c 55 KB

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