iwch_cm.c 53 KB

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