cm.c 61 KB

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