cm.c 61 KB

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