cm.c 61 KB

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