cm.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  1. /*
  2. * Copyright (c) 2009-2010 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 <linux/ip.h>
  40. #include <linux/tcp.h>
  41. #include <net/neighbour.h>
  42. #include <net/netevent.h>
  43. #include <net/route.h>
  44. #include "iw_cxgb4.h"
  45. static char *states[] = {
  46. "idle",
  47. "listen",
  48. "connecting",
  49. "mpa_wait_req",
  50. "mpa_req_sent",
  51. "mpa_req_rcvd",
  52. "mpa_rep_sent",
  53. "fpdu_mode",
  54. "aborting",
  55. "closing",
  56. "moribund",
  57. "dead",
  58. NULL,
  59. };
  60. static int dack_mode = 1;
  61. module_param(dack_mode, int, 0644);
  62. MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
  63. int c4iw_max_read_depth = 8;
  64. module_param(c4iw_max_read_depth, int, 0644);
  65. MODULE_PARM_DESC(c4iw_max_read_depth, "Per-connection max ORD/IRD (default=8)");
  66. static int enable_tcp_timestamps;
  67. module_param(enable_tcp_timestamps, int, 0644);
  68. MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
  69. static int enable_tcp_sack;
  70. module_param(enable_tcp_sack, int, 0644);
  71. MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
  72. static int enable_tcp_window_scaling = 1;
  73. module_param(enable_tcp_window_scaling, int, 0644);
  74. MODULE_PARM_DESC(enable_tcp_window_scaling,
  75. "Enable tcp window scaling (default=1)");
  76. int c4iw_debug;
  77. module_param(c4iw_debug, int, 0644);
  78. MODULE_PARM_DESC(c4iw_debug, "Enable debug logging (default=0)");
  79. static int peer2peer;
  80. module_param(peer2peer, int, 0644);
  81. MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
  82. static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
  83. module_param(p2p_type, int, 0644);
  84. MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
  85. "1=RDMA_READ 0=RDMA_WRITE (default 1)");
  86. static int ep_timeout_secs = 60;
  87. module_param(ep_timeout_secs, int, 0644);
  88. MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
  89. "in seconds (default=60)");
  90. static int mpa_rev = 1;
  91. module_param(mpa_rev, int, 0644);
  92. MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
  93. "1 is spec compliant. (default=1)");
  94. static int markers_enabled;
  95. module_param(markers_enabled, int, 0644);
  96. MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
  97. static int crc_enabled = 1;
  98. module_param(crc_enabled, int, 0644);
  99. MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
  100. static int rcv_win = 256 * 1024;
  101. module_param(rcv_win, int, 0644);
  102. MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
  103. static int snd_win = 128 * 1024;
  104. module_param(snd_win, int, 0644);
  105. MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
  106. static struct workqueue_struct *workq;
  107. static struct sk_buff_head rxq;
  108. static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
  109. static void ep_timeout(unsigned long arg);
  110. static void connect_reply_upcall(struct c4iw_ep *ep, int status);
  111. static LIST_HEAD(timeout_list);
  112. static spinlock_t timeout_lock;
  113. static void start_ep_timer(struct c4iw_ep *ep)
  114. {
  115. PDBG("%s ep %p\n", __func__, ep);
  116. if (timer_pending(&ep->timer)) {
  117. PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
  118. del_timer_sync(&ep->timer);
  119. } else
  120. c4iw_get_ep(&ep->com);
  121. ep->timer.expires = jiffies + ep_timeout_secs * HZ;
  122. ep->timer.data = (unsigned long)ep;
  123. ep->timer.function = ep_timeout;
  124. add_timer(&ep->timer);
  125. }
  126. static void stop_ep_timer(struct c4iw_ep *ep)
  127. {
  128. PDBG("%s ep %p\n", __func__, ep);
  129. if (!timer_pending(&ep->timer)) {
  130. printk(KERN_ERR "%s timer stopped when its not running! "
  131. "ep %p state %u\n", __func__, ep, ep->com.state);
  132. WARN_ON(1);
  133. return;
  134. }
  135. del_timer_sync(&ep->timer);
  136. c4iw_put_ep(&ep->com);
  137. }
  138. static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
  139. struct l2t_entry *l2e)
  140. {
  141. int error = 0;
  142. if (c4iw_fatal_error(rdev)) {
  143. kfree_skb(skb);
  144. PDBG("%s - device in error state - dropping\n", __func__);
  145. return -EIO;
  146. }
  147. error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
  148. if (error < 0)
  149. kfree_skb(skb);
  150. return error < 0 ? error : 0;
  151. }
  152. int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
  153. {
  154. int error = 0;
  155. if (c4iw_fatal_error(rdev)) {
  156. kfree_skb(skb);
  157. PDBG("%s - device in error state - dropping\n", __func__);
  158. return -EIO;
  159. }
  160. error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
  161. if (error < 0)
  162. kfree_skb(skb);
  163. return error < 0 ? error : 0;
  164. }
  165. static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
  166. {
  167. struct cpl_tid_release *req;
  168. skb = get_skb(skb, sizeof *req, GFP_KERNEL);
  169. if (!skb)
  170. return;
  171. req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
  172. INIT_TP_WR(req, hwtid);
  173. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
  174. set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
  175. c4iw_ofld_send(rdev, skb);
  176. return;
  177. }
  178. static void set_emss(struct c4iw_ep *ep, u16 opt)
  179. {
  180. ep->emss = ep->com.dev->rdev.lldi.mtus[GET_TCPOPT_MSS(opt)] - 40;
  181. ep->mss = ep->emss;
  182. if (GET_TCPOPT_TSTAMP(opt))
  183. ep->emss -= 12;
  184. if (ep->emss < 128)
  185. ep->emss = 128;
  186. PDBG("%s mss_idx %u mss %u emss=%u\n", __func__, GET_TCPOPT_MSS(opt),
  187. ep->mss, ep->emss);
  188. }
  189. static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
  190. {
  191. enum c4iw_ep_state state;
  192. mutex_lock(&epc->mutex);
  193. state = epc->state;
  194. mutex_unlock(&epc->mutex);
  195. return state;
  196. }
  197. static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
  198. {
  199. epc->state = new;
  200. }
  201. static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
  202. {
  203. mutex_lock(&epc->mutex);
  204. PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
  205. __state_set(epc, new);
  206. mutex_unlock(&epc->mutex);
  207. return;
  208. }
  209. static void *alloc_ep(int size, gfp_t gfp)
  210. {
  211. struct c4iw_ep_common *epc;
  212. epc = kzalloc(size, gfp);
  213. if (epc) {
  214. kref_init(&epc->kref);
  215. mutex_init(&epc->mutex);
  216. c4iw_init_wr_wait(&epc->wr_wait);
  217. }
  218. PDBG("%s alloc ep %p\n", __func__, epc);
  219. return epc;
  220. }
  221. void _c4iw_free_ep(struct kref *kref)
  222. {
  223. struct c4iw_ep *ep;
  224. ep = container_of(kref, struct c4iw_ep, com.kref);
  225. PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
  226. if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
  227. cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
  228. dst_release(ep->dst);
  229. cxgb4_l2t_release(ep->l2t);
  230. }
  231. kfree(ep);
  232. }
  233. static void release_ep_resources(struct c4iw_ep *ep)
  234. {
  235. set_bit(RELEASE_RESOURCES, &ep->com.flags);
  236. c4iw_put_ep(&ep->com);
  237. }
  238. static int status2errno(int status)
  239. {
  240. switch (status) {
  241. case CPL_ERR_NONE:
  242. return 0;
  243. case CPL_ERR_CONN_RESET:
  244. return -ECONNRESET;
  245. case CPL_ERR_ARP_MISS:
  246. return -EHOSTUNREACH;
  247. case CPL_ERR_CONN_TIMEDOUT:
  248. return -ETIMEDOUT;
  249. case CPL_ERR_TCAM_FULL:
  250. return -ENOMEM;
  251. case CPL_ERR_CONN_EXIST:
  252. return -EADDRINUSE;
  253. default:
  254. return -EIO;
  255. }
  256. }
  257. /*
  258. * Try and reuse skbs already allocated...
  259. */
  260. static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
  261. {
  262. if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
  263. skb_trim(skb, 0);
  264. skb_get(skb);
  265. skb_reset_transport_header(skb);
  266. } else {
  267. skb = alloc_skb(len, gfp);
  268. }
  269. return skb;
  270. }
  271. static struct rtable *find_route(struct c4iw_dev *dev, __be32 local_ip,
  272. __be32 peer_ip, __be16 local_port,
  273. __be16 peer_port, u8 tos)
  274. {
  275. struct rtable *rt;
  276. struct flowi4 fl4;
  277. rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
  278. peer_port, local_port, IPPROTO_TCP,
  279. tos, 0);
  280. if (IS_ERR(rt))
  281. return NULL;
  282. return rt;
  283. }
  284. static void arp_failure_discard(void *handle, struct sk_buff *skb)
  285. {
  286. PDBG("%s c4iw_dev %p\n", __func__, handle);
  287. kfree_skb(skb);
  288. }
  289. /*
  290. * Handle an ARP failure for an active open.
  291. */
  292. static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
  293. {
  294. printk(KERN_ERR MOD "ARP failure duing connect\n");
  295. kfree_skb(skb);
  296. }
  297. /*
  298. * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
  299. * and send it along.
  300. */
  301. static void abort_arp_failure(void *handle, struct sk_buff *skb)
  302. {
  303. struct c4iw_rdev *rdev = handle;
  304. struct cpl_abort_req *req = cplhdr(skb);
  305. PDBG("%s rdev %p\n", __func__, rdev);
  306. req->cmd = CPL_ABORT_NO_RST;
  307. c4iw_ofld_send(rdev, skb);
  308. }
  309. static void send_flowc(struct c4iw_ep *ep, struct sk_buff *skb)
  310. {
  311. unsigned int flowclen = 80;
  312. struct fw_flowc_wr *flowc;
  313. int i;
  314. skb = get_skb(skb, flowclen, GFP_KERNEL);
  315. flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);
  316. flowc->op_to_nparams = cpu_to_be32(FW_WR_OP(FW_FLOWC_WR) |
  317. FW_FLOWC_WR_NPARAMS(8));
  318. flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16(DIV_ROUND_UP(flowclen,
  319. 16)) | FW_WR_FLOWID(ep->hwtid));
  320. flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
  321. flowc->mnemval[0].val = cpu_to_be32(PCI_FUNC(ep->com.dev->rdev.lldi.pdev->devfn) << 8);
  322. flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
  323. flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
  324. flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
  325. flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
  326. flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
  327. flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
  328. flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
  329. flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
  330. flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
  331. flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
  332. flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
  333. flowc->mnemval[6].val = cpu_to_be32(snd_win);
  334. flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
  335. flowc->mnemval[7].val = cpu_to_be32(ep->emss);
  336. /* Pad WR to 16 byte boundary */
  337. flowc->mnemval[8].mnemonic = 0;
  338. flowc->mnemval[8].val = 0;
  339. for (i = 0; i < 9; i++) {
  340. flowc->mnemval[i].r4[0] = 0;
  341. flowc->mnemval[i].r4[1] = 0;
  342. flowc->mnemval[i].r4[2] = 0;
  343. }
  344. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  345. c4iw_ofld_send(&ep->com.dev->rdev, skb);
  346. }
  347. static int send_halfclose(struct c4iw_ep *ep, gfp_t gfp)
  348. {
  349. struct cpl_close_con_req *req;
  350. struct sk_buff *skb;
  351. int wrlen = roundup(sizeof *req, 16);
  352. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  353. skb = get_skb(NULL, wrlen, gfp);
  354. if (!skb) {
  355. printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
  356. return -ENOMEM;
  357. }
  358. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  359. t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
  360. req = (struct cpl_close_con_req *) skb_put(skb, wrlen);
  361. memset(req, 0, wrlen);
  362. INIT_TP_WR(req, ep->hwtid);
  363. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ,
  364. ep->hwtid));
  365. return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  366. }
  367. static int send_abort(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
  368. {
  369. struct cpl_abort_req *req;
  370. int wrlen = roundup(sizeof *req, 16);
  371. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  372. skb = get_skb(skb, wrlen, gfp);
  373. if (!skb) {
  374. printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
  375. __func__);
  376. return -ENOMEM;
  377. }
  378. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  379. t4_set_arp_err_handler(skb, &ep->com.dev->rdev, abort_arp_failure);
  380. req = (struct cpl_abort_req *) skb_put(skb, wrlen);
  381. memset(req, 0, wrlen);
  382. INIT_TP_WR(req, ep->hwtid);
  383. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
  384. req->cmd = CPL_ABORT_SEND_RST;
  385. return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  386. }
  387. static int send_connect(struct c4iw_ep *ep)
  388. {
  389. struct cpl_act_open_req *req;
  390. struct sk_buff *skb;
  391. u64 opt0;
  392. u32 opt2;
  393. unsigned int mtu_idx;
  394. int wscale;
  395. int wrlen = roundup(sizeof *req, 16);
  396. PDBG("%s ep %p atid %u\n", __func__, ep, ep->atid);
  397. skb = get_skb(NULL, wrlen, GFP_KERNEL);
  398. if (!skb) {
  399. printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
  400. __func__);
  401. return -ENOMEM;
  402. }
  403. set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
  404. cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
  405. wscale = compute_wscale(rcv_win);
  406. opt0 = KEEP_ALIVE(1) |
  407. DELACK(1) |
  408. WND_SCALE(wscale) |
  409. MSS_IDX(mtu_idx) |
  410. L2T_IDX(ep->l2t->idx) |
  411. TX_CHAN(ep->tx_chan) |
  412. SMAC_SEL(ep->smac_idx) |
  413. DSCP(ep->tos) |
  414. ULP_MODE(ULP_MODE_TCPDDP) |
  415. RCV_BUFSIZ(rcv_win>>10);
  416. opt2 = RX_CHANNEL(0) |
  417. RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
  418. if (enable_tcp_timestamps)
  419. opt2 |= TSTAMPS_EN(1);
  420. if (enable_tcp_sack)
  421. opt2 |= SACK_EN(1);
  422. if (wscale && enable_tcp_window_scaling)
  423. opt2 |= WND_SCALE_EN(1);
  424. t4_set_arp_err_handler(skb, NULL, act_open_req_arp_failure);
  425. req = (struct cpl_act_open_req *) skb_put(skb, wrlen);
  426. INIT_TP_WR(req, 0);
  427. OPCODE_TID(req) = cpu_to_be32(
  428. MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ((ep->rss_qid<<14)|ep->atid)));
  429. req->local_port = ep->com.local_addr.sin_port;
  430. req->peer_port = ep->com.remote_addr.sin_port;
  431. req->local_ip = ep->com.local_addr.sin_addr.s_addr;
  432. req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
  433. req->opt0 = cpu_to_be64(opt0);
  434. req->params = 0;
  435. req->opt2 = cpu_to_be32(opt2);
  436. return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  437. }
  438. static void send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb)
  439. {
  440. int mpalen, wrlen;
  441. struct fw_ofld_tx_data_wr *req;
  442. struct mpa_message *mpa;
  443. PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
  444. BUG_ON(skb_cloned(skb));
  445. mpalen = sizeof(*mpa) + ep->plen;
  446. wrlen = roundup(mpalen + sizeof *req, 16);
  447. skb = get_skb(skb, wrlen, GFP_KERNEL);
  448. if (!skb) {
  449. connect_reply_upcall(ep, -ENOMEM);
  450. return;
  451. }
  452. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  453. req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
  454. memset(req, 0, wrlen);
  455. req->op_to_immdlen = cpu_to_be32(
  456. FW_WR_OP(FW_OFLD_TX_DATA_WR) |
  457. FW_WR_COMPL(1) |
  458. FW_WR_IMMDLEN(mpalen));
  459. req->flowid_len16 = cpu_to_be32(
  460. FW_WR_FLOWID(ep->hwtid) |
  461. FW_WR_LEN16(wrlen >> 4));
  462. req->plen = cpu_to_be32(mpalen);
  463. req->tunnel_to_proxy = cpu_to_be32(
  464. FW_OFLD_TX_DATA_WR_FLUSH(1) |
  465. FW_OFLD_TX_DATA_WR_SHOVE(1));
  466. mpa = (struct mpa_message *)(req + 1);
  467. memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
  468. mpa->flags = (crc_enabled ? MPA_CRC : 0) |
  469. (markers_enabled ? MPA_MARKERS : 0);
  470. mpa->private_data_size = htons(ep->plen);
  471. mpa->revision = mpa_rev;
  472. if (ep->plen)
  473. memcpy(mpa->private_data, ep->mpa_pkt + sizeof(*mpa), ep->plen);
  474. /*
  475. * Reference the mpa skb. This ensures the data area
  476. * will remain in memory until the hw acks the tx.
  477. * Function fw4_ack() will deref it.
  478. */
  479. skb_get(skb);
  480. t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
  481. BUG_ON(ep->mpa_skb);
  482. ep->mpa_skb = skb;
  483. c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  484. start_ep_timer(ep);
  485. state_set(&ep->com, MPA_REQ_SENT);
  486. ep->mpa_attr.initiator = 1;
  487. return;
  488. }
  489. static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
  490. {
  491. int mpalen, wrlen;
  492. struct fw_ofld_tx_data_wr *req;
  493. struct mpa_message *mpa;
  494. struct sk_buff *skb;
  495. PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
  496. mpalen = sizeof(*mpa) + plen;
  497. wrlen = roundup(mpalen + sizeof *req, 16);
  498. skb = get_skb(NULL, wrlen, GFP_KERNEL);
  499. if (!skb) {
  500. printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
  501. return -ENOMEM;
  502. }
  503. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  504. req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
  505. memset(req, 0, wrlen);
  506. req->op_to_immdlen = cpu_to_be32(
  507. FW_WR_OP(FW_OFLD_TX_DATA_WR) |
  508. FW_WR_COMPL(1) |
  509. FW_WR_IMMDLEN(mpalen));
  510. req->flowid_len16 = cpu_to_be32(
  511. FW_WR_FLOWID(ep->hwtid) |
  512. FW_WR_LEN16(wrlen >> 4));
  513. req->plen = cpu_to_be32(mpalen);
  514. req->tunnel_to_proxy = cpu_to_be32(
  515. FW_OFLD_TX_DATA_WR_FLUSH(1) |
  516. FW_OFLD_TX_DATA_WR_SHOVE(1));
  517. mpa = (struct mpa_message *)(req + 1);
  518. memset(mpa, 0, sizeof(*mpa));
  519. memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
  520. mpa->flags = MPA_REJECT;
  521. mpa->revision = mpa_rev;
  522. mpa->private_data_size = htons(plen);
  523. if (plen)
  524. memcpy(mpa->private_data, pdata, plen);
  525. /*
  526. * Reference the mpa skb again. This ensures the data area
  527. * will remain in memory until the hw acks the tx.
  528. * Function fw4_ack() will deref it.
  529. */
  530. skb_get(skb);
  531. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  532. t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
  533. BUG_ON(ep->mpa_skb);
  534. ep->mpa_skb = skb;
  535. return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  536. }
  537. static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
  538. {
  539. int mpalen, wrlen;
  540. struct fw_ofld_tx_data_wr *req;
  541. struct mpa_message *mpa;
  542. struct sk_buff *skb;
  543. PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
  544. mpalen = sizeof(*mpa) + plen;
  545. wrlen = roundup(mpalen + sizeof *req, 16);
  546. skb = get_skb(NULL, wrlen, GFP_KERNEL);
  547. if (!skb) {
  548. printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
  549. return -ENOMEM;
  550. }
  551. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  552. req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
  553. memset(req, 0, wrlen);
  554. req->op_to_immdlen = cpu_to_be32(
  555. FW_WR_OP(FW_OFLD_TX_DATA_WR) |
  556. FW_WR_COMPL(1) |
  557. FW_WR_IMMDLEN(mpalen));
  558. req->flowid_len16 = cpu_to_be32(
  559. FW_WR_FLOWID(ep->hwtid) |
  560. FW_WR_LEN16(wrlen >> 4));
  561. req->plen = cpu_to_be32(mpalen);
  562. req->tunnel_to_proxy = cpu_to_be32(
  563. FW_OFLD_TX_DATA_WR_FLUSH(1) |
  564. FW_OFLD_TX_DATA_WR_SHOVE(1));
  565. mpa = (struct mpa_message *)(req + 1);
  566. memset(mpa, 0, sizeof(*mpa));
  567. memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
  568. mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
  569. (markers_enabled ? MPA_MARKERS : 0);
  570. mpa->revision = mpa_rev;
  571. mpa->private_data_size = htons(plen);
  572. if (plen)
  573. memcpy(mpa->private_data, pdata, plen);
  574. /*
  575. * Reference the mpa skb. This ensures the data area
  576. * will remain in memory until the hw acks the tx.
  577. * Function fw4_ack() will deref it.
  578. */
  579. skb_get(skb);
  580. t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
  581. ep->mpa_skb = skb;
  582. state_set(&ep->com, MPA_REP_SENT);
  583. return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  584. }
  585. static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
  586. {
  587. struct c4iw_ep *ep;
  588. struct cpl_act_establish *req = cplhdr(skb);
  589. unsigned int tid = GET_TID(req);
  590. unsigned int atid = GET_TID_TID(ntohl(req->tos_atid));
  591. struct tid_info *t = dev->rdev.lldi.tids;
  592. ep = lookup_atid(t, atid);
  593. PDBG("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
  594. be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
  595. dst_confirm(ep->dst);
  596. /* setup the hwtid for this connection */
  597. ep->hwtid = tid;
  598. cxgb4_insert_tid(t, ep, tid);
  599. ep->snd_seq = be32_to_cpu(req->snd_isn);
  600. ep->rcv_seq = be32_to_cpu(req->rcv_isn);
  601. set_emss(ep, ntohs(req->tcp_opt));
  602. /* dealloc the atid */
  603. cxgb4_free_atid(t, atid);
  604. /* start MPA negotiation */
  605. send_flowc(ep, NULL);
  606. send_mpa_req(ep, skb);
  607. return 0;
  608. }
  609. static void close_complete_upcall(struct c4iw_ep *ep)
  610. {
  611. struct iw_cm_event event;
  612. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  613. memset(&event, 0, sizeof(event));
  614. event.event = IW_CM_EVENT_CLOSE;
  615. if (ep->com.cm_id) {
  616. PDBG("close complete delivered ep %p cm_id %p tid %u\n",
  617. ep, ep->com.cm_id, ep->hwtid);
  618. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  619. ep->com.cm_id->rem_ref(ep->com.cm_id);
  620. ep->com.cm_id = NULL;
  621. ep->com.qp = NULL;
  622. }
  623. }
  624. static int abort_connection(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
  625. {
  626. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  627. close_complete_upcall(ep);
  628. state_set(&ep->com, ABORTING);
  629. return send_abort(ep, skb, gfp);
  630. }
  631. static void peer_close_upcall(struct c4iw_ep *ep)
  632. {
  633. struct iw_cm_event event;
  634. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  635. memset(&event, 0, sizeof(event));
  636. event.event = IW_CM_EVENT_DISCONNECT;
  637. if (ep->com.cm_id) {
  638. PDBG("peer close delivered ep %p cm_id %p tid %u\n",
  639. ep, ep->com.cm_id, ep->hwtid);
  640. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  641. }
  642. }
  643. static void peer_abort_upcall(struct c4iw_ep *ep)
  644. {
  645. struct iw_cm_event event;
  646. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  647. memset(&event, 0, sizeof(event));
  648. event.event = IW_CM_EVENT_CLOSE;
  649. event.status = -ECONNRESET;
  650. if (ep->com.cm_id) {
  651. PDBG("abort delivered ep %p cm_id %p tid %u\n", ep,
  652. ep->com.cm_id, ep->hwtid);
  653. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  654. ep->com.cm_id->rem_ref(ep->com.cm_id);
  655. ep->com.cm_id = NULL;
  656. ep->com.qp = NULL;
  657. }
  658. }
  659. static void connect_reply_upcall(struct c4iw_ep *ep, int status)
  660. {
  661. struct iw_cm_event event;
  662. PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid, status);
  663. memset(&event, 0, sizeof(event));
  664. event.event = IW_CM_EVENT_CONNECT_REPLY;
  665. event.status = status;
  666. event.local_addr = ep->com.local_addr;
  667. event.remote_addr = ep->com.remote_addr;
  668. if ((status == 0) || (status == -ECONNREFUSED)) {
  669. event.private_data_len = ep->plen;
  670. event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
  671. }
  672. PDBG("%s ep %p tid %u status %d\n", __func__, ep,
  673. ep->hwtid, status);
  674. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  675. if (status < 0) {
  676. ep->com.cm_id->rem_ref(ep->com.cm_id);
  677. ep->com.cm_id = NULL;
  678. ep->com.qp = NULL;
  679. }
  680. }
  681. static void connect_request_upcall(struct c4iw_ep *ep)
  682. {
  683. struct iw_cm_event event;
  684. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  685. memset(&event, 0, sizeof(event));
  686. event.event = IW_CM_EVENT_CONNECT_REQUEST;
  687. event.local_addr = ep->com.local_addr;
  688. event.remote_addr = ep->com.remote_addr;
  689. event.private_data_len = ep->plen;
  690. event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
  691. event.provider_data = ep;
  692. if (state_read(&ep->parent_ep->com) != DEAD) {
  693. c4iw_get_ep(&ep->com);
  694. ep->parent_ep->com.cm_id->event_handler(
  695. ep->parent_ep->com.cm_id,
  696. &event);
  697. }
  698. c4iw_put_ep(&ep->parent_ep->com);
  699. ep->parent_ep = NULL;
  700. }
  701. static void established_upcall(struct c4iw_ep *ep)
  702. {
  703. struct iw_cm_event event;
  704. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  705. memset(&event, 0, sizeof(event));
  706. event.event = IW_CM_EVENT_ESTABLISHED;
  707. if (ep->com.cm_id) {
  708. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  709. ep->com.cm_id->event_handler(ep->com.cm_id, &event);
  710. }
  711. }
  712. static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
  713. {
  714. struct cpl_rx_data_ack *req;
  715. struct sk_buff *skb;
  716. int wrlen = roundup(sizeof *req, 16);
  717. PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
  718. skb = get_skb(NULL, wrlen, GFP_KERNEL);
  719. if (!skb) {
  720. printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
  721. return 0;
  722. }
  723. req = (struct cpl_rx_data_ack *) skb_put(skb, wrlen);
  724. memset(req, 0, wrlen);
  725. INIT_TP_WR(req, ep->hwtid);
  726. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
  727. ep->hwtid));
  728. req->credit_dack = cpu_to_be32(credits | RX_FORCE_ACK(1) |
  729. F_RX_DACK_CHANGE |
  730. V_RX_DACK_MODE(dack_mode));
  731. set_wr_txq(skb, CPL_PRIORITY_ACK, ep->ctrlq_idx);
  732. c4iw_ofld_send(&ep->com.dev->rdev, skb);
  733. return credits;
  734. }
  735. static void process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
  736. {
  737. struct mpa_message *mpa;
  738. u16 plen;
  739. struct c4iw_qp_attributes attrs;
  740. enum c4iw_qp_attr_mask mask;
  741. int err;
  742. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  743. /*
  744. * Stop mpa timer. If it expired, then the state has
  745. * changed and we bail since ep_timeout already aborted
  746. * the connection.
  747. */
  748. stop_ep_timer(ep);
  749. if (state_read(&ep->com) != MPA_REQ_SENT)
  750. return;
  751. /*
  752. * If we get more than the supported amount of private data
  753. * then we must fail this connection.
  754. */
  755. if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
  756. err = -EINVAL;
  757. goto err;
  758. }
  759. /*
  760. * copy the new data into our accumulation buffer.
  761. */
  762. skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
  763. skb->len);
  764. ep->mpa_pkt_len += skb->len;
  765. /*
  766. * if we don't even have the mpa message, then bail.
  767. */
  768. if (ep->mpa_pkt_len < sizeof(*mpa))
  769. return;
  770. mpa = (struct mpa_message *) ep->mpa_pkt;
  771. /* Validate MPA header. */
  772. if (mpa->revision != mpa_rev) {
  773. err = -EPROTO;
  774. goto err;
  775. }
  776. if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
  777. err = -EPROTO;
  778. goto err;
  779. }
  780. plen = ntohs(mpa->private_data_size);
  781. /*
  782. * Fail if there's too much private data.
  783. */
  784. if (plen > MPA_MAX_PRIVATE_DATA) {
  785. err = -EPROTO;
  786. goto err;
  787. }
  788. /*
  789. * If plen does not account for pkt size
  790. */
  791. if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
  792. err = -EPROTO;
  793. goto err;
  794. }
  795. ep->plen = (u8) plen;
  796. /*
  797. * If we don't have all the pdata yet, then bail.
  798. * We'll continue process when more data arrives.
  799. */
  800. if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
  801. return;
  802. if (mpa->flags & MPA_REJECT) {
  803. err = -ECONNREFUSED;
  804. goto err;
  805. }
  806. /*
  807. * If we get here we have accumulated the entire mpa
  808. * start reply message including private data. And
  809. * the MPA header is valid.
  810. */
  811. state_set(&ep->com, FPDU_MODE);
  812. ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
  813. ep->mpa_attr.recv_marker_enabled = markers_enabled;
  814. ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
  815. ep->mpa_attr.version = mpa_rev;
  816. ep->mpa_attr.p2p_type = peer2peer ? p2p_type :
  817. FW_RI_INIT_P2PTYPE_DISABLED;
  818. PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
  819. "xmit_marker_enabled=%d, version=%d\n", __func__,
  820. ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
  821. ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
  822. attrs.mpa_attr = ep->mpa_attr;
  823. attrs.max_ird = ep->ird;
  824. attrs.max_ord = ep->ord;
  825. attrs.llp_stream_handle = ep;
  826. attrs.next_state = C4IW_QP_STATE_RTS;
  827. mask = C4IW_QP_ATTR_NEXT_STATE |
  828. C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
  829. C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
  830. /* bind QP and TID with INIT_WR */
  831. err = c4iw_modify_qp(ep->com.qp->rhp,
  832. ep->com.qp, mask, &attrs, 1);
  833. if (err)
  834. goto err;
  835. goto out;
  836. err:
  837. state_set(&ep->com, ABORTING);
  838. send_abort(ep, skb, GFP_KERNEL);
  839. out:
  840. connect_reply_upcall(ep, err);
  841. return;
  842. }
  843. static void process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
  844. {
  845. struct mpa_message *mpa;
  846. u16 plen;
  847. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  848. if (state_read(&ep->com) != MPA_REQ_WAIT)
  849. return;
  850. /*
  851. * If we get more than the supported amount of private data
  852. * then we must fail this connection.
  853. */
  854. if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
  855. stop_ep_timer(ep);
  856. abort_connection(ep, skb, GFP_KERNEL);
  857. return;
  858. }
  859. PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
  860. /*
  861. * Copy the new data into our accumulation buffer.
  862. */
  863. skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
  864. skb->len);
  865. ep->mpa_pkt_len += skb->len;
  866. /*
  867. * If we don't even have the mpa message, then bail.
  868. * We'll continue process when more data arrives.
  869. */
  870. if (ep->mpa_pkt_len < sizeof(*mpa))
  871. return;
  872. PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
  873. stop_ep_timer(ep);
  874. mpa = (struct mpa_message *) ep->mpa_pkt;
  875. /*
  876. * Validate MPA Header.
  877. */
  878. if (mpa->revision != mpa_rev) {
  879. abort_connection(ep, skb, GFP_KERNEL);
  880. return;
  881. }
  882. if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
  883. abort_connection(ep, skb, GFP_KERNEL);
  884. return;
  885. }
  886. plen = ntohs(mpa->private_data_size);
  887. /*
  888. * Fail if there's too much private data.
  889. */
  890. if (plen > MPA_MAX_PRIVATE_DATA) {
  891. abort_connection(ep, skb, GFP_KERNEL);
  892. return;
  893. }
  894. /*
  895. * If plen does not account for pkt size
  896. */
  897. if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
  898. abort_connection(ep, skb, GFP_KERNEL);
  899. return;
  900. }
  901. ep->plen = (u8) plen;
  902. /*
  903. * If we don't have all the pdata yet, then bail.
  904. */
  905. if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
  906. return;
  907. /*
  908. * If we get here we have accumulated the entire mpa
  909. * start reply message including private data.
  910. */
  911. ep->mpa_attr.initiator = 0;
  912. ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
  913. ep->mpa_attr.recv_marker_enabled = markers_enabled;
  914. ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
  915. ep->mpa_attr.version = mpa_rev;
  916. ep->mpa_attr.p2p_type = peer2peer ? p2p_type :
  917. FW_RI_INIT_P2PTYPE_DISABLED;
  918. PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
  919. "xmit_marker_enabled=%d, version=%d p2p_type=%d\n", __func__,
  920. ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
  921. ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
  922. ep->mpa_attr.p2p_type);
  923. state_set(&ep->com, MPA_REQ_RCVD);
  924. /* drive upcall */
  925. connect_request_upcall(ep);
  926. return;
  927. }
  928. static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
  929. {
  930. struct c4iw_ep *ep;
  931. struct cpl_rx_data *hdr = cplhdr(skb);
  932. unsigned int dlen = ntohs(hdr->len);
  933. unsigned int tid = GET_TID(hdr);
  934. struct tid_info *t = dev->rdev.lldi.tids;
  935. ep = lookup_tid(t, tid);
  936. PDBG("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
  937. skb_pull(skb, sizeof(*hdr));
  938. skb_trim(skb, dlen);
  939. ep->rcv_seq += dlen;
  940. BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
  941. /* update RX credits */
  942. update_rx_credits(ep, dlen);
  943. switch (state_read(&ep->com)) {
  944. case MPA_REQ_SENT:
  945. process_mpa_reply(ep, skb);
  946. break;
  947. case MPA_REQ_WAIT:
  948. process_mpa_request(ep, skb);
  949. break;
  950. case MPA_REP_SENT:
  951. break;
  952. default:
  953. printk(KERN_ERR MOD "%s Unexpected streaming data."
  954. " ep %p state %d tid %u\n",
  955. __func__, ep, state_read(&ep->com), ep->hwtid);
  956. /*
  957. * The ep will timeout and inform the ULP of the failure.
  958. * See ep_timeout().
  959. */
  960. break;
  961. }
  962. return 0;
  963. }
  964. static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
  965. {
  966. struct c4iw_ep *ep;
  967. struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
  968. int release = 0;
  969. unsigned int tid = GET_TID(rpl);
  970. struct tid_info *t = dev->rdev.lldi.tids;
  971. ep = lookup_tid(t, tid);
  972. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  973. BUG_ON(!ep);
  974. mutex_lock(&ep->com.mutex);
  975. switch (ep->com.state) {
  976. case ABORTING:
  977. __state_set(&ep->com, DEAD);
  978. release = 1;
  979. break;
  980. default:
  981. printk(KERN_ERR "%s ep %p state %d\n",
  982. __func__, ep, ep->com.state);
  983. break;
  984. }
  985. mutex_unlock(&ep->com.mutex);
  986. if (release)
  987. release_ep_resources(ep);
  988. return 0;
  989. }
  990. /*
  991. * Return whether a failed active open has allocated a TID
  992. */
  993. static inline int act_open_has_tid(int status)
  994. {
  995. return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
  996. status != CPL_ERR_ARP_MISS;
  997. }
  998. static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
  999. {
  1000. struct c4iw_ep *ep;
  1001. struct cpl_act_open_rpl *rpl = cplhdr(skb);
  1002. unsigned int atid = GET_TID_TID(GET_AOPEN_ATID(
  1003. ntohl(rpl->atid_status)));
  1004. struct tid_info *t = dev->rdev.lldi.tids;
  1005. int status = GET_AOPEN_STATUS(ntohl(rpl->atid_status));
  1006. ep = lookup_atid(t, atid);
  1007. PDBG("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
  1008. status, status2errno(status));
  1009. if (status == CPL_ERR_RTX_NEG_ADVICE) {
  1010. printk(KERN_WARNING MOD "Connection problems for atid %u\n",
  1011. atid);
  1012. return 0;
  1013. }
  1014. connect_reply_upcall(ep, status2errno(status));
  1015. state_set(&ep->com, DEAD);
  1016. if (status && act_open_has_tid(status))
  1017. cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));
  1018. cxgb4_free_atid(t, atid);
  1019. dst_release(ep->dst);
  1020. cxgb4_l2t_release(ep->l2t);
  1021. c4iw_put_ep(&ep->com);
  1022. return 0;
  1023. }
  1024. static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
  1025. {
  1026. struct cpl_pass_open_rpl *rpl = cplhdr(skb);
  1027. struct tid_info *t = dev->rdev.lldi.tids;
  1028. unsigned int stid = GET_TID(rpl);
  1029. struct c4iw_listen_ep *ep = lookup_stid(t, stid);
  1030. if (!ep) {
  1031. printk(KERN_ERR MOD "stid %d lookup failure!\n", stid);
  1032. return 0;
  1033. }
  1034. PDBG("%s ep %p status %d error %d\n", __func__, ep,
  1035. rpl->status, status2errno(rpl->status));
  1036. c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
  1037. return 0;
  1038. }
  1039. static int listen_stop(struct c4iw_listen_ep *ep)
  1040. {
  1041. struct sk_buff *skb;
  1042. struct cpl_close_listsvr_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_listsvr_req *) skb_put(skb, sizeof(*req));
  1050. INIT_TP_WR(req, 0);
  1051. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ,
  1052. ep->stid));
  1053. req->reply_ctrl = cpu_to_be16(
  1054. QUEUENO(ep->com.dev->rdev.lldi.rxq_ids[0]));
  1055. set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
  1056. return c4iw_ofld_send(&ep->com.dev->rdev, skb);
  1057. }
  1058. static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
  1059. {
  1060. struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
  1061. struct tid_info *t = dev->rdev.lldi.tids;
  1062. unsigned int stid = GET_TID(rpl);
  1063. struct c4iw_listen_ep *ep = lookup_stid(t, stid);
  1064. PDBG("%s ep %p\n", __func__, ep);
  1065. c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
  1066. return 0;
  1067. }
  1068. static void accept_cr(struct c4iw_ep *ep, __be32 peer_ip, struct sk_buff *skb,
  1069. struct cpl_pass_accept_req *req)
  1070. {
  1071. struct cpl_pass_accept_rpl *rpl;
  1072. unsigned int mtu_idx;
  1073. u64 opt0;
  1074. u32 opt2;
  1075. int wscale;
  1076. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1077. BUG_ON(skb_cloned(skb));
  1078. skb_trim(skb, sizeof(*rpl));
  1079. skb_get(skb);
  1080. cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
  1081. wscale = compute_wscale(rcv_win);
  1082. opt0 = KEEP_ALIVE(1) |
  1083. DELACK(1) |
  1084. WND_SCALE(wscale) |
  1085. MSS_IDX(mtu_idx) |
  1086. L2T_IDX(ep->l2t->idx) |
  1087. TX_CHAN(ep->tx_chan) |
  1088. SMAC_SEL(ep->smac_idx) |
  1089. DSCP(ep->tos) |
  1090. ULP_MODE(ULP_MODE_TCPDDP) |
  1091. RCV_BUFSIZ(rcv_win>>10);
  1092. opt2 = RX_CHANNEL(0) |
  1093. RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
  1094. if (enable_tcp_timestamps && req->tcpopt.tstamp)
  1095. opt2 |= TSTAMPS_EN(1);
  1096. if (enable_tcp_sack && req->tcpopt.sack)
  1097. opt2 |= SACK_EN(1);
  1098. if (wscale && enable_tcp_window_scaling)
  1099. opt2 |= WND_SCALE_EN(1);
  1100. rpl = cplhdr(skb);
  1101. INIT_TP_WR(rpl, ep->hwtid);
  1102. OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
  1103. ep->hwtid));
  1104. rpl->opt0 = cpu_to_be64(opt0);
  1105. rpl->opt2 = cpu_to_be32(opt2);
  1106. set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
  1107. c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
  1108. return;
  1109. }
  1110. static void reject_cr(struct c4iw_dev *dev, u32 hwtid, __be32 peer_ip,
  1111. struct sk_buff *skb)
  1112. {
  1113. PDBG("%s c4iw_dev %p tid %u peer_ip %x\n", __func__, dev, hwtid,
  1114. peer_ip);
  1115. BUG_ON(skb_cloned(skb));
  1116. skb_trim(skb, sizeof(struct cpl_tid_release));
  1117. skb_get(skb);
  1118. release_tid(&dev->rdev, hwtid, skb);
  1119. return;
  1120. }
  1121. static void get_4tuple(struct cpl_pass_accept_req *req,
  1122. __be32 *local_ip, __be32 *peer_ip,
  1123. __be16 *local_port, __be16 *peer_port)
  1124. {
  1125. int eth_len = G_ETH_HDR_LEN(be32_to_cpu(req->hdr_len));
  1126. int ip_len = G_IP_HDR_LEN(be32_to_cpu(req->hdr_len));
  1127. struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
  1128. struct tcphdr *tcp = (struct tcphdr *)
  1129. ((u8 *)(req + 1) + eth_len + ip_len);
  1130. PDBG("%s saddr 0x%x daddr 0x%x sport %u dport %u\n", __func__,
  1131. ntohl(ip->saddr), ntohl(ip->daddr), ntohs(tcp->source),
  1132. ntohs(tcp->dest));
  1133. *peer_ip = ip->saddr;
  1134. *local_ip = ip->daddr;
  1135. *peer_port = tcp->source;
  1136. *local_port = tcp->dest;
  1137. return;
  1138. }
  1139. static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
  1140. {
  1141. struct c4iw_ep *child_ep, *parent_ep;
  1142. struct cpl_pass_accept_req *req = cplhdr(skb);
  1143. unsigned int stid = GET_POPEN_TID(ntohl(req->tos_stid));
  1144. struct tid_info *t = dev->rdev.lldi.tids;
  1145. unsigned int hwtid = GET_TID(req);
  1146. struct dst_entry *dst;
  1147. struct l2t_entry *l2t;
  1148. struct rtable *rt;
  1149. __be32 local_ip, peer_ip;
  1150. __be16 local_port, peer_port;
  1151. struct net_device *pdev;
  1152. u32 tx_chan, smac_idx;
  1153. u16 rss_qid;
  1154. u32 mtu;
  1155. int step;
  1156. int txq_idx, ctrlq_idx;
  1157. parent_ep = lookup_stid(t, stid);
  1158. PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
  1159. get_4tuple(req, &local_ip, &peer_ip, &local_port, &peer_port);
  1160. if (state_read(&parent_ep->com) != LISTEN) {
  1161. printk(KERN_ERR "%s - listening ep not in LISTEN\n",
  1162. __func__);
  1163. goto reject;
  1164. }
  1165. /* Find output route */
  1166. rt = find_route(dev, local_ip, peer_ip, local_port, peer_port,
  1167. GET_POPEN_TOS(ntohl(req->tos_stid)));
  1168. if (!rt) {
  1169. printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
  1170. __func__);
  1171. goto reject;
  1172. }
  1173. dst = &rt->dst;
  1174. if (dst->neighbour->dev->flags & IFF_LOOPBACK) {
  1175. pdev = ip_dev_find(&init_net, peer_ip);
  1176. BUG_ON(!pdev);
  1177. l2t = cxgb4_l2t_get(dev->rdev.lldi.l2t, dst->neighbour,
  1178. pdev, 0);
  1179. mtu = pdev->mtu;
  1180. tx_chan = cxgb4_port_chan(pdev);
  1181. smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
  1182. step = dev->rdev.lldi.ntxq / dev->rdev.lldi.nchan;
  1183. txq_idx = cxgb4_port_idx(pdev) * step;
  1184. ctrlq_idx = cxgb4_port_idx(pdev);
  1185. step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
  1186. rss_qid = dev->rdev.lldi.rxq_ids[cxgb4_port_idx(pdev) * step];
  1187. dev_put(pdev);
  1188. } else {
  1189. l2t = cxgb4_l2t_get(dev->rdev.lldi.l2t, dst->neighbour,
  1190. dst->neighbour->dev, 0);
  1191. mtu = dst_mtu(dst);
  1192. tx_chan = cxgb4_port_chan(dst->neighbour->dev);
  1193. smac_idx = (cxgb4_port_viid(dst->neighbour->dev) & 0x7F) << 1;
  1194. step = dev->rdev.lldi.ntxq / dev->rdev.lldi.nchan;
  1195. txq_idx = cxgb4_port_idx(dst->neighbour->dev) * step;
  1196. ctrlq_idx = cxgb4_port_idx(dst->neighbour->dev);
  1197. step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
  1198. rss_qid = dev->rdev.lldi.rxq_ids[
  1199. cxgb4_port_idx(dst->neighbour->dev) * step];
  1200. }
  1201. if (!l2t) {
  1202. printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
  1203. __func__);
  1204. dst_release(dst);
  1205. goto reject;
  1206. }
  1207. child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
  1208. if (!child_ep) {
  1209. printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
  1210. __func__);
  1211. cxgb4_l2t_release(l2t);
  1212. dst_release(dst);
  1213. goto reject;
  1214. }
  1215. state_set(&child_ep->com, CONNECTING);
  1216. child_ep->com.dev = dev;
  1217. child_ep->com.cm_id = NULL;
  1218. child_ep->com.local_addr.sin_family = PF_INET;
  1219. child_ep->com.local_addr.sin_port = local_port;
  1220. child_ep->com.local_addr.sin_addr.s_addr = local_ip;
  1221. child_ep->com.remote_addr.sin_family = PF_INET;
  1222. child_ep->com.remote_addr.sin_port = peer_port;
  1223. child_ep->com.remote_addr.sin_addr.s_addr = peer_ip;
  1224. c4iw_get_ep(&parent_ep->com);
  1225. child_ep->parent_ep = parent_ep;
  1226. child_ep->tos = GET_POPEN_TOS(ntohl(req->tos_stid));
  1227. child_ep->l2t = l2t;
  1228. child_ep->dst = dst;
  1229. child_ep->hwtid = hwtid;
  1230. child_ep->tx_chan = tx_chan;
  1231. child_ep->smac_idx = smac_idx;
  1232. child_ep->rss_qid = rss_qid;
  1233. child_ep->mtu = mtu;
  1234. child_ep->txq_idx = txq_idx;
  1235. child_ep->ctrlq_idx = ctrlq_idx;
  1236. PDBG("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
  1237. tx_chan, smac_idx, rss_qid);
  1238. init_timer(&child_ep->timer);
  1239. cxgb4_insert_tid(t, child_ep, hwtid);
  1240. accept_cr(child_ep, peer_ip, skb, req);
  1241. goto out;
  1242. reject:
  1243. reject_cr(dev, hwtid, peer_ip, skb);
  1244. out:
  1245. return 0;
  1246. }
  1247. static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
  1248. {
  1249. struct c4iw_ep *ep;
  1250. struct cpl_pass_establish *req = cplhdr(skb);
  1251. struct tid_info *t = dev->rdev.lldi.tids;
  1252. unsigned int tid = GET_TID(req);
  1253. ep = lookup_tid(t, tid);
  1254. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1255. ep->snd_seq = be32_to_cpu(req->snd_isn);
  1256. ep->rcv_seq = be32_to_cpu(req->rcv_isn);
  1257. set_emss(ep, ntohs(req->tcp_opt));
  1258. dst_confirm(ep->dst);
  1259. state_set(&ep->com, MPA_REQ_WAIT);
  1260. start_ep_timer(ep);
  1261. send_flowc(ep, skb);
  1262. return 0;
  1263. }
  1264. static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
  1265. {
  1266. struct cpl_peer_close *hdr = cplhdr(skb);
  1267. struct c4iw_ep *ep;
  1268. struct c4iw_qp_attributes attrs;
  1269. int disconnect = 1;
  1270. int release = 0;
  1271. int abort = 0;
  1272. struct tid_info *t = dev->rdev.lldi.tids;
  1273. unsigned int tid = GET_TID(hdr);
  1274. ep = lookup_tid(t, tid);
  1275. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1276. dst_confirm(ep->dst);
  1277. mutex_lock(&ep->com.mutex);
  1278. switch (ep->com.state) {
  1279. case MPA_REQ_WAIT:
  1280. __state_set(&ep->com, CLOSING);
  1281. break;
  1282. case MPA_REQ_SENT:
  1283. __state_set(&ep->com, CLOSING);
  1284. connect_reply_upcall(ep, -ECONNRESET);
  1285. break;
  1286. case MPA_REQ_RCVD:
  1287. /*
  1288. * We're gonna mark this puppy DEAD, but keep
  1289. * the reference on it until the ULP accepts or
  1290. * rejects the CR. Also wake up anyone waiting
  1291. * in rdma connection migration (see c4iw_accept_cr()).
  1292. */
  1293. __state_set(&ep->com, CLOSING);
  1294. PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
  1295. c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
  1296. break;
  1297. case MPA_REP_SENT:
  1298. __state_set(&ep->com, CLOSING);
  1299. PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
  1300. c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
  1301. break;
  1302. case FPDU_MODE:
  1303. start_ep_timer(ep);
  1304. __state_set(&ep->com, CLOSING);
  1305. attrs.next_state = C4IW_QP_STATE_CLOSING;
  1306. abort = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
  1307. C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  1308. peer_close_upcall(ep);
  1309. disconnect = 1;
  1310. break;
  1311. case ABORTING:
  1312. disconnect = 0;
  1313. break;
  1314. case CLOSING:
  1315. __state_set(&ep->com, MORIBUND);
  1316. disconnect = 0;
  1317. break;
  1318. case MORIBUND:
  1319. stop_ep_timer(ep);
  1320. if (ep->com.cm_id && ep->com.qp) {
  1321. attrs.next_state = C4IW_QP_STATE_IDLE;
  1322. c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
  1323. C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  1324. }
  1325. close_complete_upcall(ep);
  1326. __state_set(&ep->com, DEAD);
  1327. release = 1;
  1328. disconnect = 0;
  1329. break;
  1330. case DEAD:
  1331. disconnect = 0;
  1332. break;
  1333. default:
  1334. BUG_ON(1);
  1335. }
  1336. mutex_unlock(&ep->com.mutex);
  1337. if (disconnect)
  1338. c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
  1339. if (release)
  1340. release_ep_resources(ep);
  1341. return 0;
  1342. }
  1343. /*
  1344. * Returns whether an ABORT_REQ_RSS message is a negative advice.
  1345. */
  1346. static int is_neg_adv_abort(unsigned int status)
  1347. {
  1348. return status == CPL_ERR_RTX_NEG_ADVICE ||
  1349. status == CPL_ERR_PERSIST_NEG_ADVICE;
  1350. }
  1351. static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
  1352. {
  1353. struct cpl_abort_req_rss *req = cplhdr(skb);
  1354. struct c4iw_ep *ep;
  1355. struct cpl_abort_rpl *rpl;
  1356. struct sk_buff *rpl_skb;
  1357. struct c4iw_qp_attributes attrs;
  1358. int ret;
  1359. int release = 0;
  1360. struct tid_info *t = dev->rdev.lldi.tids;
  1361. unsigned int tid = GET_TID(req);
  1362. ep = lookup_tid(t, tid);
  1363. if (is_neg_adv_abort(req->status)) {
  1364. PDBG("%s neg_adv_abort ep %p tid %u\n", __func__, ep,
  1365. ep->hwtid);
  1366. return 0;
  1367. }
  1368. PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
  1369. ep->com.state);
  1370. /*
  1371. * Wake up any threads in rdma_init() or rdma_fini().
  1372. */
  1373. c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
  1374. mutex_lock(&ep->com.mutex);
  1375. switch (ep->com.state) {
  1376. case CONNECTING:
  1377. break;
  1378. case MPA_REQ_WAIT:
  1379. stop_ep_timer(ep);
  1380. break;
  1381. case MPA_REQ_SENT:
  1382. stop_ep_timer(ep);
  1383. connect_reply_upcall(ep, -ECONNRESET);
  1384. break;
  1385. case MPA_REP_SENT:
  1386. break;
  1387. case MPA_REQ_RCVD:
  1388. break;
  1389. case MORIBUND:
  1390. case CLOSING:
  1391. stop_ep_timer(ep);
  1392. /*FALLTHROUGH*/
  1393. case FPDU_MODE:
  1394. if (ep->com.cm_id && ep->com.qp) {
  1395. attrs.next_state = C4IW_QP_STATE_ERROR;
  1396. ret = c4iw_modify_qp(ep->com.qp->rhp,
  1397. ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
  1398. &attrs, 1);
  1399. if (ret)
  1400. printk(KERN_ERR MOD
  1401. "%s - qp <- error failed!\n",
  1402. __func__);
  1403. }
  1404. peer_abort_upcall(ep);
  1405. break;
  1406. case ABORTING:
  1407. break;
  1408. case DEAD:
  1409. PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
  1410. mutex_unlock(&ep->com.mutex);
  1411. return 0;
  1412. default:
  1413. BUG_ON(1);
  1414. break;
  1415. }
  1416. dst_confirm(ep->dst);
  1417. if (ep->com.state != ABORTING) {
  1418. __state_set(&ep->com, DEAD);
  1419. release = 1;
  1420. }
  1421. mutex_unlock(&ep->com.mutex);
  1422. rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
  1423. if (!rpl_skb) {
  1424. printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
  1425. __func__);
  1426. release = 1;
  1427. goto out;
  1428. }
  1429. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  1430. rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
  1431. INIT_TP_WR(rpl, ep->hwtid);
  1432. OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
  1433. rpl->cmd = CPL_ABORT_NO_RST;
  1434. c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
  1435. out:
  1436. if (release)
  1437. release_ep_resources(ep);
  1438. return 0;
  1439. }
  1440. static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
  1441. {
  1442. struct c4iw_ep *ep;
  1443. struct c4iw_qp_attributes attrs;
  1444. struct cpl_close_con_rpl *rpl = cplhdr(skb);
  1445. int release = 0;
  1446. struct tid_info *t = dev->rdev.lldi.tids;
  1447. unsigned int tid = GET_TID(rpl);
  1448. ep = lookup_tid(t, tid);
  1449. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1450. BUG_ON(!ep);
  1451. /* The cm_id may be null if we failed to connect */
  1452. mutex_lock(&ep->com.mutex);
  1453. switch (ep->com.state) {
  1454. case CLOSING:
  1455. __state_set(&ep->com, MORIBUND);
  1456. break;
  1457. case MORIBUND:
  1458. stop_ep_timer(ep);
  1459. if ((ep->com.cm_id) && (ep->com.qp)) {
  1460. attrs.next_state = C4IW_QP_STATE_IDLE;
  1461. c4iw_modify_qp(ep->com.qp->rhp,
  1462. ep->com.qp,
  1463. C4IW_QP_ATTR_NEXT_STATE,
  1464. &attrs, 1);
  1465. }
  1466. close_complete_upcall(ep);
  1467. __state_set(&ep->com, DEAD);
  1468. release = 1;
  1469. break;
  1470. case ABORTING:
  1471. case DEAD:
  1472. break;
  1473. default:
  1474. BUG_ON(1);
  1475. break;
  1476. }
  1477. mutex_unlock(&ep->com.mutex);
  1478. if (release)
  1479. release_ep_resources(ep);
  1480. return 0;
  1481. }
  1482. static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
  1483. {
  1484. struct cpl_rdma_terminate *rpl = cplhdr(skb);
  1485. struct tid_info *t = dev->rdev.lldi.tids;
  1486. unsigned int tid = GET_TID(rpl);
  1487. struct c4iw_ep *ep;
  1488. struct c4iw_qp_attributes attrs;
  1489. ep = lookup_tid(t, tid);
  1490. BUG_ON(!ep);
  1491. if (ep && ep->com.qp) {
  1492. printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
  1493. ep->com.qp->wq.sq.qid);
  1494. attrs.next_state = C4IW_QP_STATE_TERMINATE;
  1495. c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
  1496. C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  1497. } else
  1498. printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
  1499. return 0;
  1500. }
  1501. /*
  1502. * Upcall from the adapter indicating data has been transmitted.
  1503. * For us its just the single MPA request or reply. We can now free
  1504. * the skb holding the mpa message.
  1505. */
  1506. static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
  1507. {
  1508. struct c4iw_ep *ep;
  1509. struct cpl_fw4_ack *hdr = cplhdr(skb);
  1510. u8 credits = hdr->credits;
  1511. unsigned int tid = GET_TID(hdr);
  1512. struct tid_info *t = dev->rdev.lldi.tids;
  1513. ep = lookup_tid(t, tid);
  1514. PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
  1515. if (credits == 0) {
  1516. PDBG("%s 0 credit ack ep %p tid %u state %u\n",
  1517. __func__, ep, ep->hwtid, state_read(&ep->com));
  1518. return 0;
  1519. }
  1520. dst_confirm(ep->dst);
  1521. if (ep->mpa_skb) {
  1522. PDBG("%s last streaming msg ack ep %p tid %u state %u "
  1523. "initiator %u freeing skb\n", __func__, ep, ep->hwtid,
  1524. state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
  1525. kfree_skb(ep->mpa_skb);
  1526. ep->mpa_skb = NULL;
  1527. }
  1528. return 0;
  1529. }
  1530. int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
  1531. {
  1532. int err;
  1533. struct c4iw_ep *ep = to_ep(cm_id);
  1534. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1535. if (state_read(&ep->com) == DEAD) {
  1536. c4iw_put_ep(&ep->com);
  1537. return -ECONNRESET;
  1538. }
  1539. BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
  1540. if (mpa_rev == 0)
  1541. abort_connection(ep, NULL, GFP_KERNEL);
  1542. else {
  1543. err = send_mpa_reject(ep, pdata, pdata_len);
  1544. err = c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
  1545. }
  1546. c4iw_put_ep(&ep->com);
  1547. return 0;
  1548. }
  1549. int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  1550. {
  1551. int err;
  1552. struct c4iw_qp_attributes attrs;
  1553. enum c4iw_qp_attr_mask mask;
  1554. struct c4iw_ep *ep = to_ep(cm_id);
  1555. struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
  1556. struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
  1557. PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
  1558. if (state_read(&ep->com) == DEAD) {
  1559. err = -ECONNRESET;
  1560. goto err;
  1561. }
  1562. BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
  1563. BUG_ON(!qp);
  1564. if ((conn_param->ord > c4iw_max_read_depth) ||
  1565. (conn_param->ird > c4iw_max_read_depth)) {
  1566. abort_connection(ep, NULL, GFP_KERNEL);
  1567. err = -EINVAL;
  1568. goto err;
  1569. }
  1570. cm_id->add_ref(cm_id);
  1571. ep->com.cm_id = cm_id;
  1572. ep->com.qp = qp;
  1573. ep->ird = conn_param->ird;
  1574. ep->ord = conn_param->ord;
  1575. if (peer2peer && ep->ird == 0)
  1576. ep->ird = 1;
  1577. PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
  1578. /* bind QP to EP and move to RTS */
  1579. attrs.mpa_attr = ep->mpa_attr;
  1580. attrs.max_ird = ep->ird;
  1581. attrs.max_ord = ep->ord;
  1582. attrs.llp_stream_handle = ep;
  1583. attrs.next_state = C4IW_QP_STATE_RTS;
  1584. /* bind QP and TID with INIT_WR */
  1585. mask = C4IW_QP_ATTR_NEXT_STATE |
  1586. C4IW_QP_ATTR_LLP_STREAM_HANDLE |
  1587. C4IW_QP_ATTR_MPA_ATTR |
  1588. C4IW_QP_ATTR_MAX_IRD |
  1589. C4IW_QP_ATTR_MAX_ORD;
  1590. err = c4iw_modify_qp(ep->com.qp->rhp,
  1591. ep->com.qp, mask, &attrs, 1);
  1592. if (err)
  1593. goto err1;
  1594. err = send_mpa_reply(ep, conn_param->private_data,
  1595. conn_param->private_data_len);
  1596. if (err)
  1597. goto err1;
  1598. state_set(&ep->com, FPDU_MODE);
  1599. established_upcall(ep);
  1600. c4iw_put_ep(&ep->com);
  1601. return 0;
  1602. err1:
  1603. ep->com.cm_id = NULL;
  1604. ep->com.qp = NULL;
  1605. cm_id->rem_ref(cm_id);
  1606. err:
  1607. c4iw_put_ep(&ep->com);
  1608. return err;
  1609. }
  1610. int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  1611. {
  1612. int err = 0;
  1613. struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
  1614. struct c4iw_ep *ep;
  1615. struct rtable *rt;
  1616. struct net_device *pdev;
  1617. int step;
  1618. if ((conn_param->ord > c4iw_max_read_depth) ||
  1619. (conn_param->ird > c4iw_max_read_depth)) {
  1620. err = -EINVAL;
  1621. goto out;
  1622. }
  1623. ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
  1624. if (!ep) {
  1625. printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
  1626. err = -ENOMEM;
  1627. goto out;
  1628. }
  1629. init_timer(&ep->timer);
  1630. ep->plen = conn_param->private_data_len;
  1631. if (ep->plen)
  1632. memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
  1633. conn_param->private_data, ep->plen);
  1634. ep->ird = conn_param->ird;
  1635. ep->ord = conn_param->ord;
  1636. if (peer2peer && ep->ord == 0)
  1637. ep->ord = 1;
  1638. cm_id->add_ref(cm_id);
  1639. ep->com.dev = dev;
  1640. ep->com.cm_id = cm_id;
  1641. ep->com.qp = get_qhp(dev, conn_param->qpn);
  1642. BUG_ON(!ep->com.qp);
  1643. PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
  1644. ep->com.qp, cm_id);
  1645. /*
  1646. * Allocate an active TID to initiate a TCP connection.
  1647. */
  1648. ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
  1649. if (ep->atid == -1) {
  1650. printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
  1651. err = -ENOMEM;
  1652. goto fail2;
  1653. }
  1654. PDBG("%s saddr 0x%x sport 0x%x raddr 0x%x rport 0x%x\n", __func__,
  1655. ntohl(cm_id->local_addr.sin_addr.s_addr),
  1656. ntohs(cm_id->local_addr.sin_port),
  1657. ntohl(cm_id->remote_addr.sin_addr.s_addr),
  1658. ntohs(cm_id->remote_addr.sin_port));
  1659. /* find a route */
  1660. rt = find_route(dev,
  1661. cm_id->local_addr.sin_addr.s_addr,
  1662. cm_id->remote_addr.sin_addr.s_addr,
  1663. cm_id->local_addr.sin_port,
  1664. cm_id->remote_addr.sin_port, 0);
  1665. if (!rt) {
  1666. printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
  1667. err = -EHOSTUNREACH;
  1668. goto fail3;
  1669. }
  1670. ep->dst = &rt->dst;
  1671. /* get a l2t entry */
  1672. if (ep->dst->neighbour->dev->flags & IFF_LOOPBACK) {
  1673. PDBG("%s LOOPBACK\n", __func__);
  1674. pdev = ip_dev_find(&init_net,
  1675. cm_id->remote_addr.sin_addr.s_addr);
  1676. ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
  1677. ep->dst->neighbour,
  1678. pdev, 0);
  1679. ep->mtu = pdev->mtu;
  1680. ep->tx_chan = cxgb4_port_chan(pdev);
  1681. ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
  1682. step = ep->com.dev->rdev.lldi.ntxq /
  1683. ep->com.dev->rdev.lldi.nchan;
  1684. ep->txq_idx = cxgb4_port_idx(pdev) * step;
  1685. step = ep->com.dev->rdev.lldi.nrxq /
  1686. ep->com.dev->rdev.lldi.nchan;
  1687. ep->ctrlq_idx = cxgb4_port_idx(pdev);
  1688. ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
  1689. cxgb4_port_idx(pdev) * step];
  1690. dev_put(pdev);
  1691. } else {
  1692. ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
  1693. ep->dst->neighbour,
  1694. ep->dst->neighbour->dev, 0);
  1695. ep->mtu = dst_mtu(ep->dst);
  1696. ep->tx_chan = cxgb4_port_chan(ep->dst->neighbour->dev);
  1697. ep->smac_idx = (cxgb4_port_viid(ep->dst->neighbour->dev) &
  1698. 0x7F) << 1;
  1699. step = ep->com.dev->rdev.lldi.ntxq /
  1700. ep->com.dev->rdev.lldi.nchan;
  1701. ep->txq_idx = cxgb4_port_idx(ep->dst->neighbour->dev) * step;
  1702. ep->ctrlq_idx = cxgb4_port_idx(ep->dst->neighbour->dev);
  1703. step = ep->com.dev->rdev.lldi.nrxq /
  1704. ep->com.dev->rdev.lldi.nchan;
  1705. ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
  1706. cxgb4_port_idx(ep->dst->neighbour->dev) * step];
  1707. }
  1708. if (!ep->l2t) {
  1709. printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
  1710. err = -ENOMEM;
  1711. goto fail4;
  1712. }
  1713. PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
  1714. __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
  1715. ep->l2t->idx);
  1716. state_set(&ep->com, CONNECTING);
  1717. ep->tos = 0;
  1718. ep->com.local_addr = cm_id->local_addr;
  1719. ep->com.remote_addr = cm_id->remote_addr;
  1720. /* send connect request to rnic */
  1721. err = send_connect(ep);
  1722. if (!err)
  1723. goto out;
  1724. cxgb4_l2t_release(ep->l2t);
  1725. fail4:
  1726. dst_release(ep->dst);
  1727. fail3:
  1728. cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
  1729. fail2:
  1730. cm_id->rem_ref(cm_id);
  1731. c4iw_put_ep(&ep->com);
  1732. out:
  1733. return err;
  1734. }
  1735. int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
  1736. {
  1737. int err = 0;
  1738. struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
  1739. struct c4iw_listen_ep *ep;
  1740. might_sleep();
  1741. ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
  1742. if (!ep) {
  1743. printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
  1744. err = -ENOMEM;
  1745. goto fail1;
  1746. }
  1747. PDBG("%s ep %p\n", __func__, ep);
  1748. cm_id->add_ref(cm_id);
  1749. ep->com.cm_id = cm_id;
  1750. ep->com.dev = dev;
  1751. ep->backlog = backlog;
  1752. ep->com.local_addr = cm_id->local_addr;
  1753. /*
  1754. * Allocate a server TID.
  1755. */
  1756. ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids, PF_INET, ep);
  1757. if (ep->stid == -1) {
  1758. printk(KERN_ERR MOD "%s - cannot alloc stid.\n", __func__);
  1759. err = -ENOMEM;
  1760. goto fail2;
  1761. }
  1762. state_set(&ep->com, LISTEN);
  1763. c4iw_init_wr_wait(&ep->com.wr_wait);
  1764. err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0], ep->stid,
  1765. ep->com.local_addr.sin_addr.s_addr,
  1766. ep->com.local_addr.sin_port,
  1767. ep->com.dev->rdev.lldi.rxq_ids[0]);
  1768. if (err)
  1769. goto fail3;
  1770. /* wait for pass_open_rpl */
  1771. err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait, 0, 0,
  1772. __func__);
  1773. if (!err) {
  1774. cm_id->provider_data = ep;
  1775. goto out;
  1776. }
  1777. fail3:
  1778. cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
  1779. fail2:
  1780. cm_id->rem_ref(cm_id);
  1781. c4iw_put_ep(&ep->com);
  1782. fail1:
  1783. out:
  1784. return err;
  1785. }
  1786. int c4iw_destroy_listen(struct iw_cm_id *cm_id)
  1787. {
  1788. int err;
  1789. struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
  1790. PDBG("%s ep %p\n", __func__, ep);
  1791. might_sleep();
  1792. state_set(&ep->com, DEAD);
  1793. c4iw_init_wr_wait(&ep->com.wr_wait);
  1794. err = listen_stop(ep);
  1795. if (err)
  1796. goto done;
  1797. err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait, 0, 0,
  1798. __func__);
  1799. cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
  1800. done:
  1801. cm_id->rem_ref(cm_id);
  1802. c4iw_put_ep(&ep->com);
  1803. return err;
  1804. }
  1805. int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
  1806. {
  1807. int ret = 0;
  1808. int close = 0;
  1809. int fatal = 0;
  1810. struct c4iw_rdev *rdev;
  1811. mutex_lock(&ep->com.mutex);
  1812. PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
  1813. states[ep->com.state], abrupt);
  1814. rdev = &ep->com.dev->rdev;
  1815. if (c4iw_fatal_error(rdev)) {
  1816. fatal = 1;
  1817. close_complete_upcall(ep);
  1818. ep->com.state = DEAD;
  1819. }
  1820. switch (ep->com.state) {
  1821. case MPA_REQ_WAIT:
  1822. case MPA_REQ_SENT:
  1823. case MPA_REQ_RCVD:
  1824. case MPA_REP_SENT:
  1825. case FPDU_MODE:
  1826. close = 1;
  1827. if (abrupt)
  1828. ep->com.state = ABORTING;
  1829. else {
  1830. ep->com.state = CLOSING;
  1831. start_ep_timer(ep);
  1832. }
  1833. set_bit(CLOSE_SENT, &ep->com.flags);
  1834. break;
  1835. case CLOSING:
  1836. if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
  1837. close = 1;
  1838. if (abrupt) {
  1839. stop_ep_timer(ep);
  1840. ep->com.state = ABORTING;
  1841. } else
  1842. ep->com.state = MORIBUND;
  1843. }
  1844. break;
  1845. case MORIBUND:
  1846. case ABORTING:
  1847. case DEAD:
  1848. PDBG("%s ignoring disconnect ep %p state %u\n",
  1849. __func__, ep, ep->com.state);
  1850. break;
  1851. default:
  1852. BUG();
  1853. break;
  1854. }
  1855. mutex_unlock(&ep->com.mutex);
  1856. if (close) {
  1857. if (abrupt)
  1858. ret = abort_connection(ep, NULL, gfp);
  1859. else
  1860. ret = send_halfclose(ep, gfp);
  1861. if (ret)
  1862. fatal = 1;
  1863. }
  1864. if (fatal)
  1865. release_ep_resources(ep);
  1866. return ret;
  1867. }
  1868. static int async_event(struct c4iw_dev *dev, struct sk_buff *skb)
  1869. {
  1870. struct cpl_fw6_msg *rpl = cplhdr(skb);
  1871. c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
  1872. return 0;
  1873. }
  1874. /*
  1875. * These are the real handlers that are called from a
  1876. * work queue.
  1877. */
  1878. static c4iw_handler_func work_handlers[NUM_CPL_CMDS] = {
  1879. [CPL_ACT_ESTABLISH] = act_establish,
  1880. [CPL_ACT_OPEN_RPL] = act_open_rpl,
  1881. [CPL_RX_DATA] = rx_data,
  1882. [CPL_ABORT_RPL_RSS] = abort_rpl,
  1883. [CPL_ABORT_RPL] = abort_rpl,
  1884. [CPL_PASS_OPEN_RPL] = pass_open_rpl,
  1885. [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
  1886. [CPL_PASS_ACCEPT_REQ] = pass_accept_req,
  1887. [CPL_PASS_ESTABLISH] = pass_establish,
  1888. [CPL_PEER_CLOSE] = peer_close,
  1889. [CPL_ABORT_REQ_RSS] = peer_abort,
  1890. [CPL_CLOSE_CON_RPL] = close_con_rpl,
  1891. [CPL_RDMA_TERMINATE] = terminate,
  1892. [CPL_FW4_ACK] = fw4_ack,
  1893. [CPL_FW6_MSG] = async_event
  1894. };
  1895. static void process_timeout(struct c4iw_ep *ep)
  1896. {
  1897. struct c4iw_qp_attributes attrs;
  1898. int abort = 1;
  1899. mutex_lock(&ep->com.mutex);
  1900. PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
  1901. ep->com.state);
  1902. switch (ep->com.state) {
  1903. case MPA_REQ_SENT:
  1904. __state_set(&ep->com, ABORTING);
  1905. connect_reply_upcall(ep, -ETIMEDOUT);
  1906. break;
  1907. case MPA_REQ_WAIT:
  1908. __state_set(&ep->com, ABORTING);
  1909. break;
  1910. case CLOSING:
  1911. case MORIBUND:
  1912. if (ep->com.cm_id && ep->com.qp) {
  1913. attrs.next_state = C4IW_QP_STATE_ERROR;
  1914. c4iw_modify_qp(ep->com.qp->rhp,
  1915. ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
  1916. &attrs, 1);
  1917. }
  1918. __state_set(&ep->com, ABORTING);
  1919. break;
  1920. default:
  1921. printk(KERN_ERR "%s unexpected state ep %p tid %u state %u\n",
  1922. __func__, ep, ep->hwtid, ep->com.state);
  1923. WARN_ON(1);
  1924. abort = 0;
  1925. }
  1926. mutex_unlock(&ep->com.mutex);
  1927. if (abort)
  1928. abort_connection(ep, NULL, GFP_KERNEL);
  1929. c4iw_put_ep(&ep->com);
  1930. }
  1931. static void process_timedout_eps(void)
  1932. {
  1933. struct c4iw_ep *ep;
  1934. spin_lock_irq(&timeout_lock);
  1935. while (!list_empty(&timeout_list)) {
  1936. struct list_head *tmp;
  1937. tmp = timeout_list.next;
  1938. list_del(tmp);
  1939. spin_unlock_irq(&timeout_lock);
  1940. ep = list_entry(tmp, struct c4iw_ep, entry);
  1941. process_timeout(ep);
  1942. spin_lock_irq(&timeout_lock);
  1943. }
  1944. spin_unlock_irq(&timeout_lock);
  1945. }
  1946. static void process_work(struct work_struct *work)
  1947. {
  1948. struct sk_buff *skb = NULL;
  1949. struct c4iw_dev *dev;
  1950. struct cpl_act_establish *rpl;
  1951. unsigned int opcode;
  1952. int ret;
  1953. while ((skb = skb_dequeue(&rxq))) {
  1954. rpl = cplhdr(skb);
  1955. dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
  1956. opcode = rpl->ot.opcode;
  1957. BUG_ON(!work_handlers[opcode]);
  1958. ret = work_handlers[opcode](dev, skb);
  1959. if (!ret)
  1960. kfree_skb(skb);
  1961. }
  1962. process_timedout_eps();
  1963. }
  1964. static DECLARE_WORK(skb_work, process_work);
  1965. static void ep_timeout(unsigned long arg)
  1966. {
  1967. struct c4iw_ep *ep = (struct c4iw_ep *)arg;
  1968. spin_lock(&timeout_lock);
  1969. list_add_tail(&ep->entry, &timeout_list);
  1970. spin_unlock(&timeout_lock);
  1971. queue_work(workq, &skb_work);
  1972. }
  1973. /*
  1974. * All the CM events are handled on a work queue to have a safe context.
  1975. */
  1976. static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
  1977. {
  1978. /*
  1979. * Save dev in the skb->cb area.
  1980. */
  1981. *((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
  1982. /*
  1983. * Queue the skb and schedule the worker thread.
  1984. */
  1985. skb_queue_tail(&rxq, skb);
  1986. queue_work(workq, &skb_work);
  1987. return 0;
  1988. }
  1989. static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
  1990. {
  1991. struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
  1992. if (rpl->status != CPL_ERR_NONE) {
  1993. printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
  1994. "for tid %u\n", rpl->status, GET_TID(rpl));
  1995. }
  1996. kfree_skb(skb);
  1997. return 0;
  1998. }
  1999. static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
  2000. {
  2001. struct cpl_fw6_msg *rpl = cplhdr(skb);
  2002. struct c4iw_wr_wait *wr_waitp;
  2003. int ret;
  2004. PDBG("%s type %u\n", __func__, rpl->type);
  2005. switch (rpl->type) {
  2006. case 1:
  2007. ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
  2008. wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
  2009. PDBG("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
  2010. if (wr_waitp)
  2011. c4iw_wake_up(wr_waitp, ret ? -ret : 0);
  2012. kfree_skb(skb);
  2013. break;
  2014. case 2:
  2015. sched(dev, skb);
  2016. break;
  2017. default:
  2018. printk(KERN_ERR MOD "%s unexpected fw6 msg type %u\n", __func__,
  2019. rpl->type);
  2020. kfree_skb(skb);
  2021. break;
  2022. }
  2023. return 0;
  2024. }
  2025. /*
  2026. * Most upcalls from the T4 Core go to sched() to
  2027. * schedule the processing on a work queue.
  2028. */
  2029. c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
  2030. [CPL_ACT_ESTABLISH] = sched,
  2031. [CPL_ACT_OPEN_RPL] = sched,
  2032. [CPL_RX_DATA] = sched,
  2033. [CPL_ABORT_RPL_RSS] = sched,
  2034. [CPL_ABORT_RPL] = sched,
  2035. [CPL_PASS_OPEN_RPL] = sched,
  2036. [CPL_CLOSE_LISTSRV_RPL] = sched,
  2037. [CPL_PASS_ACCEPT_REQ] = sched,
  2038. [CPL_PASS_ESTABLISH] = sched,
  2039. [CPL_PEER_CLOSE] = sched,
  2040. [CPL_CLOSE_CON_RPL] = sched,
  2041. [CPL_ABORT_REQ_RSS] = sched,
  2042. [CPL_RDMA_TERMINATE] = sched,
  2043. [CPL_FW4_ACK] = sched,
  2044. [CPL_SET_TCB_RPL] = set_tcb_rpl,
  2045. [CPL_FW6_MSG] = fw6_msg
  2046. };
  2047. int __init c4iw_cm_init(void)
  2048. {
  2049. spin_lock_init(&timeout_lock);
  2050. skb_queue_head_init(&rxq);
  2051. workq = create_singlethread_workqueue("iw_cxgb4");
  2052. if (!workq)
  2053. return -ENOMEM;
  2054. return 0;
  2055. }
  2056. void __exit c4iw_cm_term(void)
  2057. {
  2058. WARN_ON(!list_empty(&timeout_list));
  2059. flush_workqueue(workq);
  2060. destroy_workqueue(workq);
  2061. }