nes_cm.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117
  1. /*
  2. * Copyright (c) 2006 - 2008 NetEffect, 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. */
  33. #define TCPOPT_TIMESTAMP 8
  34. #include <asm/atomic.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/ip.h>
  37. #include <linux/tcp.h>
  38. #include <linux/init.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/notifier.h>
  42. #include <linux/net.h>
  43. #include <linux/types.h>
  44. #include <linux/timer.h>
  45. #include <linux/time.h>
  46. #include <linux/delay.h>
  47. #include <linux/etherdevice.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/random.h>
  50. #include <linux/list.h>
  51. #include <linux/threads.h>
  52. #include <net/neighbour.h>
  53. #include <net/route.h>
  54. #include <net/ip_fib.h>
  55. #include "nes.h"
  56. u32 cm_packets_sent;
  57. u32 cm_packets_bounced;
  58. u32 cm_packets_dropped;
  59. u32 cm_packets_retrans;
  60. u32 cm_packets_created;
  61. u32 cm_packets_received;
  62. u32 cm_listens_created;
  63. u32 cm_listens_destroyed;
  64. u32 cm_backlog_drops;
  65. atomic_t cm_loopbacks;
  66. atomic_t cm_nodes_created;
  67. atomic_t cm_nodes_destroyed;
  68. atomic_t cm_accel_dropped_pkts;
  69. atomic_t cm_resets_recvd;
  70. static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
  71. static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
  72. struct nes_vnic *, struct nes_cm_info *);
  73. static int add_ref_cm_node(struct nes_cm_node *);
  74. static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
  75. static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
  76. static struct sk_buff *form_cm_frame(struct sk_buff *, struct nes_cm_node *,
  77. void *, u32, void *, u32, u8);
  78. static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node);
  79. static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
  80. struct nes_vnic *,
  81. struct ietf_mpa_frame *,
  82. struct nes_cm_info *);
  83. static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
  84. struct nes_cm_node *);
  85. static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
  86. struct nes_cm_node *);
  87. static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
  88. static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
  89. struct sk_buff *);
  90. static int mini_cm_dealloc_core(struct nes_cm_core *);
  91. static int mini_cm_get(struct nes_cm_core *);
  92. static int mini_cm_set(struct nes_cm_core *, u32, u32);
  93. static int nes_cm_disconn_true(struct nes_qp *);
  94. static int nes_cm_post_event(struct nes_cm_event *event);
  95. static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
  96. static void nes_disconnect_worker(struct work_struct *work);
  97. static int send_ack(struct nes_cm_node *cm_node);
  98. static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
  99. /* External CM API Interface */
  100. /* instance of function pointers for client API */
  101. /* set address of this instance to cm_core->cm_ops at cm_core alloc */
  102. static struct nes_cm_ops nes_cm_api = {
  103. mini_cm_accelerated,
  104. mini_cm_listen,
  105. mini_cm_del_listen,
  106. mini_cm_connect,
  107. mini_cm_close,
  108. mini_cm_accept,
  109. mini_cm_reject,
  110. mini_cm_recv_pkt,
  111. mini_cm_dealloc_core,
  112. mini_cm_get,
  113. mini_cm_set
  114. };
  115. static struct nes_cm_core *g_cm_core;
  116. atomic_t cm_connects;
  117. atomic_t cm_accepts;
  118. atomic_t cm_disconnects;
  119. atomic_t cm_closes;
  120. atomic_t cm_connecteds;
  121. atomic_t cm_connect_reqs;
  122. atomic_t cm_rejects;
  123. /**
  124. * create_event
  125. */
  126. static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
  127. enum nes_cm_event_type type)
  128. {
  129. struct nes_cm_event *event;
  130. if (!cm_node->cm_id)
  131. return NULL;
  132. /* allocate an empty event */
  133. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  134. if (!event)
  135. return NULL;
  136. event->type = type;
  137. event->cm_node = cm_node;
  138. event->cm_info.rem_addr = cm_node->rem_addr;
  139. event->cm_info.loc_addr = cm_node->loc_addr;
  140. event->cm_info.rem_port = cm_node->rem_port;
  141. event->cm_info.loc_port = cm_node->loc_port;
  142. event->cm_info.cm_id = cm_node->cm_id;
  143. nes_debug(NES_DBG_CM, "Created event=%p, type=%u, dst_addr=%08x[%x],"
  144. " src_addr=%08x[%x]\n",
  145. event, type,
  146. event->cm_info.loc_addr, event->cm_info.loc_port,
  147. event->cm_info.rem_addr, event->cm_info.rem_port);
  148. nes_cm_post_event(event);
  149. return event;
  150. }
  151. /**
  152. * send_mpa_request
  153. */
  154. static int send_mpa_request(struct nes_cm_node *cm_node)
  155. {
  156. struct sk_buff *skb;
  157. int ret;
  158. skb = get_free_pkt(cm_node);
  159. if (!skb) {
  160. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  161. return -1;
  162. }
  163. /* send an MPA Request frame */
  164. form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
  165. cm_node->mpa_frame_size, SET_ACK);
  166. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  167. if (ret < 0) {
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. /**
  173. * recv_mpa - process a received TCP pkt, we are expecting an
  174. * IETF MPA frame
  175. */
  176. static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 len)
  177. {
  178. struct ietf_mpa_frame *mpa_frame;
  179. /* assume req frame is in tcp data payload */
  180. if (len < sizeof(struct ietf_mpa_frame)) {
  181. nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
  182. return -1;
  183. }
  184. mpa_frame = (struct ietf_mpa_frame *)buffer;
  185. cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
  186. if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
  187. nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
  188. " complete (%x + %x != %x)\n",
  189. cm_node->mpa_frame_size, (u32)sizeof(struct ietf_mpa_frame), len);
  190. return -1;
  191. }
  192. /* copy entire MPA frame to our cm_node's frame */
  193. memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
  194. cm_node->mpa_frame_size);
  195. return 0;
  196. }
  197. /**
  198. * handle_exception_pkt - process an exception packet.
  199. * We have been in a TSA state, and we have now received SW
  200. * TCP/IP traffic should be a FIN request or IP pkt with options
  201. */
  202. static int handle_exception_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb)
  203. {
  204. int ret = 0;
  205. struct tcphdr *tcph = tcp_hdr(skb);
  206. /* first check to see if this a FIN pkt */
  207. if (tcph->fin) {
  208. /* we need to ACK the FIN request */
  209. send_ack(cm_node);
  210. /* check which side we are (client/server) and set next state accordingly */
  211. if (cm_node->tcp_cntxt.client)
  212. cm_node->state = NES_CM_STATE_CLOSING;
  213. else {
  214. /* we are the server side */
  215. cm_node->state = NES_CM_STATE_CLOSE_WAIT;
  216. /* since this is a self contained CM we don't wait for */
  217. /* an APP to close us, just send final FIN immediately */
  218. ret = send_fin(cm_node, NULL);
  219. cm_node->state = NES_CM_STATE_LAST_ACK;
  220. }
  221. } else {
  222. ret = -EINVAL;
  223. }
  224. return ret;
  225. }
  226. /**
  227. * form_cm_frame - get a free packet and build empty frame Use
  228. * node info to build.
  229. */
  230. static struct sk_buff *form_cm_frame(struct sk_buff *skb, struct nes_cm_node *cm_node,
  231. void *options, u32 optionsize, void *data,
  232. u32 datasize, u8 flags)
  233. {
  234. struct tcphdr *tcph;
  235. struct iphdr *iph;
  236. struct ethhdr *ethh;
  237. u8 *buf;
  238. u16 packetsize = sizeof(*iph);
  239. packetsize += sizeof(*tcph);
  240. packetsize += optionsize + datasize;
  241. memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
  242. skb->len = 0;
  243. buf = skb_put(skb, packetsize + ETH_HLEN);
  244. ethh = (struct ethhdr *) buf;
  245. buf += ETH_HLEN;
  246. iph = (struct iphdr *)buf;
  247. buf += sizeof(*iph);
  248. tcph = (struct tcphdr *)buf;
  249. skb_reset_mac_header(skb);
  250. skb_set_network_header(skb, ETH_HLEN);
  251. skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
  252. buf += sizeof(*tcph);
  253. skb->ip_summed = CHECKSUM_PARTIAL;
  254. skb->protocol = htons(0x800);
  255. skb->data_len = 0;
  256. skb->mac_len = ETH_HLEN;
  257. memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
  258. memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
  259. ethh->h_proto = htons(0x0800);
  260. iph->version = IPVERSION;
  261. iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
  262. iph->tos = 0;
  263. iph->tot_len = htons(packetsize);
  264. iph->id = htons(++cm_node->tcp_cntxt.loc_id);
  265. iph->frag_off = htons(0x4000);
  266. iph->ttl = 0x40;
  267. iph->protocol = 0x06; /* IPPROTO_TCP */
  268. iph->saddr = htonl(cm_node->loc_addr);
  269. iph->daddr = htonl(cm_node->rem_addr);
  270. tcph->source = htons(cm_node->loc_port);
  271. tcph->dest = htons(cm_node->rem_port);
  272. tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
  273. if (flags & SET_ACK) {
  274. cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
  275. tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
  276. tcph->ack = 1;
  277. } else
  278. tcph->ack_seq = 0;
  279. if (flags & SET_SYN) {
  280. cm_node->tcp_cntxt.loc_seq_num++;
  281. tcph->syn = 1;
  282. } else
  283. cm_node->tcp_cntxt.loc_seq_num += datasize; /* data (no headers) */
  284. if (flags & SET_FIN)
  285. tcph->fin = 1;
  286. if (flags & SET_RST)
  287. tcph->rst = 1;
  288. tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
  289. tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
  290. tcph->urg_ptr = 0;
  291. if (optionsize)
  292. memcpy(buf, options, optionsize);
  293. buf += optionsize;
  294. if (datasize)
  295. memcpy(buf, data, datasize);
  296. skb_shinfo(skb)->nr_frags = 0;
  297. cm_packets_created++;
  298. return skb;
  299. }
  300. /**
  301. * print_core - dump a cm core
  302. */
  303. static void print_core(struct nes_cm_core *core)
  304. {
  305. nes_debug(NES_DBG_CM, "---------------------------------------------\n");
  306. nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
  307. if (!core)
  308. return;
  309. nes_debug(NES_DBG_CM, "---------------------------------------------\n");
  310. nes_debug(NES_DBG_CM, "Session ID : %u \n", atomic_read(&core->session_id));
  311. nes_debug(NES_DBG_CM, "State : %u \n", core->state);
  312. nes_debug(NES_DBG_CM, "Tx Free cnt : %u \n", skb_queue_len(&core->tx_free_list));
  313. nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
  314. nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
  315. nes_debug(NES_DBG_CM, "core : %p \n", core);
  316. nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
  317. }
  318. /**
  319. * schedule_nes_timer
  320. * note - cm_node needs to be protected before calling this. Encase in:
  321. * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
  322. */
  323. int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
  324. enum nes_timer_type type, int send_retrans,
  325. int close_when_complete)
  326. {
  327. unsigned long flags;
  328. struct nes_cm_core *cm_core;
  329. struct nes_timer_entry *new_send;
  330. int ret = 0;
  331. u32 was_timer_set;
  332. if (!cm_node)
  333. return -EINVAL;
  334. new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
  335. if (!new_send)
  336. return -1;
  337. /* new_send->timetosend = currenttime */
  338. new_send->retrycount = NES_DEFAULT_RETRYS;
  339. new_send->retranscount = NES_DEFAULT_RETRANS;
  340. new_send->skb = skb;
  341. new_send->timetosend = jiffies;
  342. new_send->type = type;
  343. new_send->netdev = cm_node->netdev;
  344. new_send->send_retrans = send_retrans;
  345. new_send->close_when_complete = close_when_complete;
  346. if (type == NES_TIMER_TYPE_CLOSE) {
  347. new_send->timetosend += (HZ/2); /* TODO: decide on the correct value here */
  348. spin_lock_irqsave(&cm_node->recv_list_lock, flags);
  349. list_add_tail(&new_send->list, &cm_node->recv_list);
  350. spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
  351. }
  352. if (type == NES_TIMER_TYPE_SEND) {
  353. new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
  354. atomic_inc(&new_send->skb->users);
  355. ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
  356. if (ret != NETDEV_TX_OK) {
  357. nes_debug(NES_DBG_CM, "Error sending packet %p (jiffies = %lu)\n",
  358. new_send, jiffies);
  359. atomic_dec(&new_send->skb->users);
  360. new_send->timetosend = jiffies;
  361. } else {
  362. cm_packets_sent++;
  363. if (!send_retrans) {
  364. if (close_when_complete)
  365. rem_ref_cm_node(cm_node->cm_core, cm_node);
  366. dev_kfree_skb_any(new_send->skb);
  367. kfree(new_send);
  368. return ret;
  369. }
  370. new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
  371. }
  372. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  373. list_add_tail(&new_send->list, &cm_node->retrans_list);
  374. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  375. }
  376. if (type == NES_TIMER_TYPE_RECV) {
  377. new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
  378. new_send->timetosend = jiffies;
  379. spin_lock_irqsave(&cm_node->recv_list_lock, flags);
  380. list_add_tail(&new_send->list, &cm_node->recv_list);
  381. spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
  382. }
  383. cm_core = cm_node->cm_core;
  384. was_timer_set = timer_pending(&cm_core->tcp_timer);
  385. if (!was_timer_set) {
  386. cm_core->tcp_timer.expires = new_send->timetosend;
  387. add_timer(&cm_core->tcp_timer);
  388. }
  389. return ret;
  390. }
  391. /**
  392. * nes_cm_timer_tick
  393. */
  394. static void nes_cm_timer_tick(unsigned long pass)
  395. {
  396. unsigned long flags, qplockflags;
  397. unsigned long nexttimeout = jiffies + NES_LONG_TIME;
  398. struct iw_cm_id *cm_id;
  399. struct nes_cm_node *cm_node;
  400. struct nes_timer_entry *send_entry, *recv_entry;
  401. struct list_head *list_core, *list_core_temp;
  402. struct list_head *list_node, *list_node_temp;
  403. struct nes_cm_core *cm_core = g_cm_core;
  404. struct nes_qp *nesqp;
  405. struct sk_buff *skb;
  406. u32 settimer = 0;
  407. int ret = NETDEV_TX_OK;
  408. int node_done;
  409. spin_lock_irqsave(&cm_core->ht_lock, flags);
  410. list_for_each_safe(list_node, list_core_temp, &cm_core->connected_nodes) {
  411. cm_node = container_of(list_node, struct nes_cm_node, list);
  412. add_ref_cm_node(cm_node);
  413. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  414. spin_lock_irqsave(&cm_node->recv_list_lock, flags);
  415. list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
  416. recv_entry = container_of(list_core, struct nes_timer_entry, list);
  417. if ((time_after(recv_entry->timetosend, jiffies)) &&
  418. (recv_entry->type == NES_TIMER_TYPE_CLOSE)) {
  419. if (nexttimeout > recv_entry->timetosend || !settimer) {
  420. nexttimeout = recv_entry->timetosend;
  421. settimer = 1;
  422. }
  423. continue;
  424. }
  425. list_del(&recv_entry->list);
  426. cm_id = cm_node->cm_id;
  427. spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
  428. if (recv_entry->type == NES_TIMER_TYPE_CLOSE) {
  429. nesqp = (struct nes_qp *)recv_entry->skb;
  430. spin_lock_irqsave(&nesqp->lock, qplockflags);
  431. if (nesqp->cm_id) {
  432. nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, refcount = %d: "
  433. "****** HIT A NES_TIMER_TYPE_CLOSE"
  434. " with something to do!!! ******\n",
  435. nesqp->hwqp.qp_id, cm_id,
  436. atomic_read(&nesqp->refcount));
  437. nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
  438. nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
  439. nesqp->ibqp_state = IB_QPS_ERR;
  440. spin_unlock_irqrestore(&nesqp->lock, qplockflags);
  441. nes_cm_disconn(nesqp);
  442. } else {
  443. spin_unlock_irqrestore(&nesqp->lock, qplockflags);
  444. nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, refcount = %d:"
  445. " ****** HIT A NES_TIMER_TYPE_CLOSE"
  446. " with nothing to do!!! ******\n",
  447. nesqp->hwqp.qp_id, cm_id,
  448. atomic_read(&nesqp->refcount));
  449. nes_rem_ref(&nesqp->ibqp);
  450. }
  451. if (cm_id)
  452. cm_id->rem_ref(cm_id);
  453. }
  454. kfree(recv_entry);
  455. spin_lock_irqsave(&cm_node->recv_list_lock, flags);
  456. }
  457. spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
  458. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  459. node_done = 0;
  460. list_for_each_safe(list_core, list_node_temp, &cm_node->retrans_list) {
  461. if (node_done) {
  462. break;
  463. }
  464. send_entry = container_of(list_core, struct nes_timer_entry, list);
  465. if (time_after(send_entry->timetosend, jiffies)) {
  466. if (cm_node->state != NES_CM_STATE_TSA) {
  467. if ((nexttimeout > send_entry->timetosend) || !settimer) {
  468. nexttimeout = send_entry->timetosend;
  469. settimer = 1;
  470. }
  471. node_done = 1;
  472. continue;
  473. } else {
  474. list_del(&send_entry->list);
  475. skb = send_entry->skb;
  476. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  477. dev_kfree_skb_any(skb);
  478. kfree(send_entry);
  479. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  480. continue;
  481. }
  482. }
  483. if (send_entry->type == NES_TIMER_NODE_CLEANUP) {
  484. list_del(&send_entry->list);
  485. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  486. kfree(send_entry);
  487. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  488. continue;
  489. }
  490. if ((send_entry->seq_num < cm_node->tcp_cntxt.rem_ack_num) ||
  491. (cm_node->state == NES_CM_STATE_TSA) ||
  492. (cm_node->state == NES_CM_STATE_CLOSED)) {
  493. skb = send_entry->skb;
  494. list_del(&send_entry->list);
  495. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  496. kfree(send_entry);
  497. dev_kfree_skb_any(skb);
  498. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  499. continue;
  500. }
  501. if (!send_entry->retranscount || !send_entry->retrycount) {
  502. cm_packets_dropped++;
  503. skb = send_entry->skb;
  504. list_del(&send_entry->list);
  505. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  506. dev_kfree_skb_any(skb);
  507. kfree(send_entry);
  508. if (cm_node->state == NES_CM_STATE_SYN_RCVD) {
  509. /* this node never even generated an indication up to the cm */
  510. rem_ref_cm_node(cm_core, cm_node);
  511. } else {
  512. cm_node->state = NES_CM_STATE_CLOSED;
  513. create_event(cm_node, NES_CM_EVENT_ABORTED);
  514. }
  515. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  516. continue;
  517. }
  518. /* this seems like the correct place, but leave send entry unprotected */
  519. // spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  520. atomic_inc(&send_entry->skb->users);
  521. cm_packets_retrans++;
  522. nes_debug(NES_DBG_CM, "Retransmitting send_entry %p for node %p,"
  523. " jiffies = %lu, time to send = %lu, retranscount = %u, "
  524. "send_entry->seq_num = 0x%08X, cm_node->tcp_cntxt.rem_ack_num = 0x%08X\n",
  525. send_entry, cm_node, jiffies, send_entry->timetosend, send_entry->retranscount,
  526. send_entry->seq_num, cm_node->tcp_cntxt.rem_ack_num);
  527. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  528. ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
  529. if (ret != NETDEV_TX_OK) {
  530. cm_packets_bounced++;
  531. atomic_dec(&send_entry->skb->users);
  532. send_entry->retrycount--;
  533. nexttimeout = jiffies + NES_SHORT_TIME;
  534. settimer = 1;
  535. node_done = 1;
  536. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  537. continue;
  538. } else {
  539. cm_packets_sent++;
  540. }
  541. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  542. list_del(&send_entry->list);
  543. nes_debug(NES_DBG_CM, "Packet Sent: retrans count = %u, retry count = %u.\n",
  544. send_entry->retranscount, send_entry->retrycount);
  545. if (send_entry->send_retrans) {
  546. send_entry->retranscount--;
  547. send_entry->timetosend = jiffies + NES_RETRY_TIMEOUT;
  548. if (nexttimeout > send_entry->timetosend || !settimer) {
  549. nexttimeout = send_entry->timetosend;
  550. settimer = 1;
  551. }
  552. list_add(&send_entry->list, &cm_node->retrans_list);
  553. continue;
  554. } else {
  555. int close_when_complete;
  556. skb = send_entry->skb;
  557. close_when_complete = send_entry->close_when_complete;
  558. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  559. if (close_when_complete) {
  560. BUG_ON(atomic_read(&cm_node->ref_count) == 1);
  561. rem_ref_cm_node(cm_core, cm_node);
  562. }
  563. dev_kfree_skb_any(skb);
  564. kfree(send_entry);
  565. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  566. continue;
  567. }
  568. }
  569. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  570. rem_ref_cm_node(cm_core, cm_node);
  571. spin_lock_irqsave(&cm_core->ht_lock, flags);
  572. if (ret != NETDEV_TX_OK)
  573. break;
  574. }
  575. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  576. if (settimer) {
  577. if (!timer_pending(&cm_core->tcp_timer)) {
  578. cm_core->tcp_timer.expires = nexttimeout;
  579. add_timer(&cm_core->tcp_timer);
  580. }
  581. }
  582. }
  583. /**
  584. * send_syn
  585. */
  586. static int send_syn(struct nes_cm_node *cm_node, u32 sendack)
  587. {
  588. int ret;
  589. int flags = SET_SYN;
  590. struct sk_buff *skb;
  591. char optionsbuffer[sizeof(struct option_mss) +
  592. sizeof(struct option_windowscale) +
  593. sizeof(struct option_base) + 1];
  594. int optionssize = 0;
  595. /* Sending MSS option */
  596. union all_known_options *options;
  597. if (!cm_node)
  598. return -EINVAL;
  599. options = (union all_known_options *)&optionsbuffer[optionssize];
  600. options->as_mss.optionnum = OPTION_NUMBER_MSS;
  601. options->as_mss.length = sizeof(struct option_mss);
  602. options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
  603. optionssize += sizeof(struct option_mss);
  604. options = (union all_known_options *)&optionsbuffer[optionssize];
  605. options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
  606. options->as_windowscale.length = sizeof(struct option_windowscale);
  607. options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
  608. optionssize += sizeof(struct option_windowscale);
  609. if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)
  610. ) {
  611. options = (union all_known_options *)&optionsbuffer[optionssize];
  612. options->as_base.optionnum = OPTION_NUMBER_WRITE0;
  613. options->as_base.length = sizeof(struct option_base);
  614. optionssize += sizeof(struct option_base);
  615. /* we need the size to be a multiple of 4 */
  616. options = (union all_known_options *)&optionsbuffer[optionssize];
  617. options->as_end = 1;
  618. optionssize += 1;
  619. options = (union all_known_options *)&optionsbuffer[optionssize];
  620. options->as_end = 1;
  621. optionssize += 1;
  622. }
  623. options = (union all_known_options *)&optionsbuffer[optionssize];
  624. options->as_end = OPTION_NUMBER_END;
  625. optionssize += 1;
  626. skb = get_free_pkt(cm_node);
  627. if (!skb) {
  628. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  629. return -1;
  630. }
  631. if (sendack)
  632. flags |= SET_ACK;
  633. form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
  634. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  635. return ret;
  636. }
  637. /**
  638. * send_reset
  639. */
  640. static int send_reset(struct nes_cm_node *cm_node)
  641. {
  642. int ret;
  643. struct sk_buff *skb = get_free_pkt(cm_node);
  644. int flags = SET_RST | SET_ACK;
  645. if (!skb) {
  646. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  647. return -1;
  648. }
  649. add_ref_cm_node(cm_node);
  650. form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
  651. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
  652. return ret;
  653. }
  654. /**
  655. * send_ack
  656. */
  657. static int send_ack(struct nes_cm_node *cm_node)
  658. {
  659. int ret;
  660. struct sk_buff *skb = get_free_pkt(cm_node);
  661. if (!skb) {
  662. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  663. return -1;
  664. }
  665. form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
  666. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
  667. return ret;
  668. }
  669. /**
  670. * send_fin
  671. */
  672. static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
  673. {
  674. int ret;
  675. /* if we didn't get a frame get one */
  676. if (!skb)
  677. skb = get_free_pkt(cm_node);
  678. if (!skb) {
  679. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  680. return -1;
  681. }
  682. form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
  683. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  684. return ret;
  685. }
  686. /**
  687. * get_free_pkt
  688. */
  689. static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node)
  690. {
  691. struct sk_buff *skb, *new_skb;
  692. /* check to see if we need to repopulate the free tx pkt queue */
  693. if (skb_queue_len(&cm_node->cm_core->tx_free_list) < NES_CM_FREE_PKT_LO_WATERMARK) {
  694. while (skb_queue_len(&cm_node->cm_core->tx_free_list) <
  695. cm_node->cm_core->free_tx_pkt_max) {
  696. /* replace the frame we took, we won't get it back */
  697. new_skb = dev_alloc_skb(cm_node->cm_core->mtu);
  698. BUG_ON(!new_skb);
  699. /* add a replacement frame to the free tx list head */
  700. skb_queue_head(&cm_node->cm_core->tx_free_list, new_skb);
  701. }
  702. }
  703. skb = skb_dequeue(&cm_node->cm_core->tx_free_list);
  704. return skb;
  705. }
  706. /**
  707. * make_hashkey - generate hash key from node tuple
  708. */
  709. static inline int make_hashkey(u16 loc_port, nes_addr_t loc_addr, u16 rem_port,
  710. nes_addr_t rem_addr)
  711. {
  712. u32 hashkey = 0;
  713. hashkey = loc_addr + rem_addr + loc_port + rem_port;
  714. hashkey = (hashkey % NES_CM_HASHTABLE_SIZE);
  715. return hashkey;
  716. }
  717. /**
  718. * find_node - find a cm node that matches the reference cm node
  719. */
  720. static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
  721. u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
  722. {
  723. unsigned long flags;
  724. u32 hashkey;
  725. struct list_head *list_pos;
  726. struct list_head *hte;
  727. struct nes_cm_node *cm_node;
  728. /* make a hash index key for this packet */
  729. hashkey = make_hashkey(loc_port, loc_addr, rem_port, rem_addr);
  730. /* get a handle on the hte */
  731. hte = &cm_core->connected_nodes;
  732. nes_debug(NES_DBG_CM, "Searching for an owner node:%x:%x from core %p->%p\n",
  733. loc_addr, loc_port, cm_core, hte);
  734. /* walk list and find cm_node associated with this session ID */
  735. spin_lock_irqsave(&cm_core->ht_lock, flags);
  736. list_for_each(list_pos, hte) {
  737. cm_node = container_of(list_pos, struct nes_cm_node, list);
  738. /* compare quad, return node handle if a match */
  739. nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
  740. cm_node->loc_addr, cm_node->loc_port,
  741. loc_addr, loc_port,
  742. cm_node->rem_addr, cm_node->rem_port,
  743. rem_addr, rem_port);
  744. if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
  745. (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
  746. add_ref_cm_node(cm_node);
  747. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  748. return cm_node;
  749. }
  750. }
  751. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  752. /* no owner node */
  753. return NULL;
  754. }
  755. /**
  756. * find_listener - find a cm node listening on this addr-port pair
  757. */
  758. static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
  759. nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
  760. {
  761. unsigned long flags;
  762. struct list_head *listen_list;
  763. struct nes_cm_listener *listen_node;
  764. /* walk list and find cm_node associated with this session ID */
  765. spin_lock_irqsave(&cm_core->listen_list_lock, flags);
  766. list_for_each(listen_list, &cm_core->listen_list.list) {
  767. listen_node = container_of(listen_list, struct nes_cm_listener, list);
  768. /* compare node pair, return node handle if a match */
  769. if (((listen_node->loc_addr == dst_addr) ||
  770. listen_node->loc_addr == 0x00000000) &&
  771. (listen_node->loc_port == dst_port) &&
  772. (listener_state & listen_node->listener_state)) {
  773. atomic_inc(&listen_node->ref_count);
  774. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  775. return listen_node;
  776. }
  777. }
  778. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  779. nes_debug(NES_DBG_CM, "Unable to find listener- %x:%x\n",
  780. dst_addr, dst_port);
  781. /* no listener */
  782. return NULL;
  783. }
  784. /**
  785. * add_hte_node - add a cm node to the hash table
  786. */
  787. static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
  788. {
  789. unsigned long flags;
  790. u32 hashkey;
  791. struct list_head *hte;
  792. if (!cm_node || !cm_core)
  793. return -EINVAL;
  794. nes_debug(NES_DBG_CM, "Adding Node to Active Connection HT\n");
  795. /* first, make an index into our hash table */
  796. hashkey = make_hashkey(cm_node->loc_port, cm_node->loc_addr,
  797. cm_node->rem_port, cm_node->rem_addr);
  798. cm_node->hashkey = hashkey;
  799. spin_lock_irqsave(&cm_core->ht_lock, flags);
  800. /* get a handle on the hash table element (list head for this slot) */
  801. hte = &cm_core->connected_nodes;
  802. list_add_tail(&cm_node->list, hte);
  803. atomic_inc(&cm_core->ht_node_cnt);
  804. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  805. return 0;
  806. }
  807. /**
  808. * mini_cm_dec_refcnt_listen
  809. */
  810. static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
  811. struct nes_cm_listener *listener, int free_hanging_nodes)
  812. {
  813. int ret = 1;
  814. unsigned long flags;
  815. spin_lock_irqsave(&cm_core->listen_list_lock, flags);
  816. if (!atomic_dec_return(&listener->ref_count)) {
  817. list_del(&listener->list);
  818. /* decrement our listen node count */
  819. atomic_dec(&cm_core->listen_node_cnt);
  820. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  821. if (listener->nesvnic) {
  822. nes_manage_apbvt(listener->nesvnic, listener->loc_port,
  823. PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
  824. }
  825. nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
  826. kfree(listener);
  827. listener = NULL;
  828. ret = 0;
  829. cm_listens_destroyed++;
  830. } else {
  831. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  832. }
  833. if (listener) {
  834. if (atomic_read(&listener->pend_accepts_cnt) > 0)
  835. nes_debug(NES_DBG_CM, "destroying listener (%p)"
  836. " with non-zero pending accepts=%u\n",
  837. listener, atomic_read(&listener->pend_accepts_cnt));
  838. }
  839. return ret;
  840. }
  841. /**
  842. * mini_cm_del_listen
  843. */
  844. static int mini_cm_del_listen(struct nes_cm_core *cm_core,
  845. struct nes_cm_listener *listener)
  846. {
  847. listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
  848. listener->cm_id = NULL; /* going to be destroyed pretty soon */
  849. return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
  850. }
  851. /**
  852. * mini_cm_accelerated
  853. */
  854. static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
  855. struct nes_cm_node *cm_node)
  856. {
  857. u32 was_timer_set;
  858. cm_node->accelerated = 1;
  859. if (cm_node->accept_pend) {
  860. BUG_ON(!cm_node->listener);
  861. atomic_dec(&cm_node->listener->pend_accepts_cnt);
  862. BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
  863. }
  864. was_timer_set = timer_pending(&cm_core->tcp_timer);
  865. if (!was_timer_set) {
  866. cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
  867. add_timer(&cm_core->tcp_timer);
  868. }
  869. return 0;
  870. }
  871. /**
  872. * nes_addr_send_arp
  873. */
  874. static void nes_addr_send_arp(u32 dst_ip)
  875. {
  876. struct rtable *rt;
  877. struct flowi fl;
  878. memset(&fl, 0, sizeof fl);
  879. fl.nl_u.ip4_u.daddr = htonl(dst_ip);
  880. if (ip_route_output_key(&init_net, &rt, &fl)) {
  881. printk("%s: ip_route_output_key failed for 0x%08X\n",
  882. __FUNCTION__, dst_ip);
  883. return;
  884. }
  885. neigh_event_send(rt->u.dst.neighbour, NULL);
  886. ip_rt_put(rt);
  887. }
  888. /**
  889. * make_cm_node - create a new instance of a cm node
  890. */
  891. static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
  892. struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
  893. struct nes_cm_listener *listener)
  894. {
  895. struct nes_cm_node *cm_node;
  896. struct timespec ts;
  897. int arpindex = 0;
  898. struct nes_device *nesdev;
  899. struct nes_adapter *nesadapter;
  900. /* create an hte and cm_node for this instance */
  901. cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
  902. if (!cm_node)
  903. return NULL;
  904. /* set our node specific transport info */
  905. cm_node->loc_addr = cm_info->loc_addr;
  906. cm_node->rem_addr = cm_info->rem_addr;
  907. cm_node->loc_port = cm_info->loc_port;
  908. cm_node->rem_port = cm_info->rem_port;
  909. cm_node->send_write0 = send_first;
  910. nes_debug(NES_DBG_CM, "Make node addresses : loc = %x:%x, rem = %x:%x\n",
  911. cm_node->loc_addr, cm_node->loc_port, cm_node->rem_addr, cm_node->rem_port);
  912. cm_node->listener = listener;
  913. cm_node->netdev = nesvnic->netdev;
  914. cm_node->cm_id = cm_info->cm_id;
  915. memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
  916. nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n",
  917. cm_node->listener, cm_node->cm_id);
  918. INIT_LIST_HEAD(&cm_node->retrans_list);
  919. spin_lock_init(&cm_node->retrans_list_lock);
  920. INIT_LIST_HEAD(&cm_node->recv_list);
  921. spin_lock_init(&cm_node->recv_list_lock);
  922. cm_node->loopbackpartner = NULL;
  923. atomic_set(&cm_node->ref_count, 1);
  924. /* associate our parent CM core */
  925. cm_node->cm_core = cm_core;
  926. cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
  927. cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
  928. cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
  929. NES_CM_DEFAULT_RCV_WND_SCALE;
  930. ts = current_kernel_time();
  931. cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
  932. cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
  933. sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
  934. cm_node->tcp_cntxt.rcv_nxt = 0;
  935. /* get a unique session ID , add thread_id to an upcounter to handle race */
  936. atomic_inc(&cm_core->node_cnt);
  937. atomic_inc(&cm_core->session_id);
  938. cm_node->session_id = (u32)(atomic_read(&cm_core->session_id) + current->tgid);
  939. cm_node->conn_type = cm_info->conn_type;
  940. cm_node->apbvt_set = 0;
  941. cm_node->accept_pend = 0;
  942. cm_node->nesvnic = nesvnic;
  943. /* get some device handles, for arp lookup */
  944. nesdev = nesvnic->nesdev;
  945. nesadapter = nesdev->nesadapter;
  946. cm_node->loopbackpartner = NULL;
  947. /* get the mac addr for the remote node */
  948. arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
  949. if (arpindex < 0) {
  950. kfree(cm_node);
  951. nes_addr_send_arp(cm_info->rem_addr);
  952. return NULL;
  953. }
  954. /* copy the mac addr to node context */
  955. memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
  956. nes_debug(NES_DBG_CM, "Remote mac addr from arp table:%02x,"
  957. " %02x, %02x, %02x, %02x, %02x\n",
  958. cm_node->rem_mac[0], cm_node->rem_mac[1],
  959. cm_node->rem_mac[2], cm_node->rem_mac[3],
  960. cm_node->rem_mac[4], cm_node->rem_mac[5]);
  961. add_hte_node(cm_core, cm_node);
  962. atomic_inc(&cm_nodes_created);
  963. return cm_node;
  964. }
  965. /**
  966. * add_ref_cm_node - destroy an instance of a cm node
  967. */
  968. static int add_ref_cm_node(struct nes_cm_node *cm_node)
  969. {
  970. atomic_inc(&cm_node->ref_count);
  971. return 0;
  972. }
  973. /**
  974. * rem_ref_cm_node - destroy an instance of a cm node
  975. */
  976. static int rem_ref_cm_node(struct nes_cm_core *cm_core,
  977. struct nes_cm_node *cm_node)
  978. {
  979. unsigned long flags, qplockflags;
  980. struct nes_timer_entry *send_entry;
  981. struct nes_timer_entry *recv_entry;
  982. struct iw_cm_id *cm_id;
  983. struct list_head *list_core, *list_node_temp;
  984. struct nes_qp *nesqp;
  985. if (!cm_node)
  986. return -EINVAL;
  987. spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
  988. if (atomic_dec_return(&cm_node->ref_count)) {
  989. spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
  990. return 0;
  991. }
  992. list_del(&cm_node->list);
  993. atomic_dec(&cm_core->ht_node_cnt);
  994. spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
  995. /* if the node is destroyed before connection was accelerated */
  996. if (!cm_node->accelerated && cm_node->accept_pend) {
  997. BUG_ON(!cm_node->listener);
  998. atomic_dec(&cm_node->listener->pend_accepts_cnt);
  999. BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
  1000. }
  1001. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  1002. list_for_each_safe(list_core, list_node_temp, &cm_node->retrans_list) {
  1003. send_entry = container_of(list_core, struct nes_timer_entry, list);
  1004. list_del(&send_entry->list);
  1005. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  1006. dev_kfree_skb_any(send_entry->skb);
  1007. kfree(send_entry);
  1008. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  1009. continue;
  1010. }
  1011. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  1012. spin_lock_irqsave(&cm_node->recv_list_lock, flags);
  1013. list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
  1014. recv_entry = container_of(list_core, struct nes_timer_entry, list);
  1015. list_del(&recv_entry->list);
  1016. cm_id = cm_node->cm_id;
  1017. spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
  1018. if (recv_entry->type == NES_TIMER_TYPE_CLOSE) {
  1019. nesqp = (struct nes_qp *)recv_entry->skb;
  1020. spin_lock_irqsave(&nesqp->lock, qplockflags);
  1021. if (nesqp->cm_id) {
  1022. nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: ****** HIT A NES_TIMER_TYPE_CLOSE"
  1023. " with something to do!!! ******\n",
  1024. nesqp->hwqp.qp_id, cm_id);
  1025. nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
  1026. nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
  1027. nesqp->ibqp_state = IB_QPS_ERR;
  1028. spin_unlock_irqrestore(&nesqp->lock, qplockflags);
  1029. nes_cm_disconn(nesqp);
  1030. } else {
  1031. spin_unlock_irqrestore(&nesqp->lock, qplockflags);
  1032. nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: ****** HIT A NES_TIMER_TYPE_CLOSE"
  1033. " with nothing to do!!! ******\n",
  1034. nesqp->hwqp.qp_id, cm_id);
  1035. nes_rem_ref(&nesqp->ibqp);
  1036. }
  1037. cm_id->rem_ref(cm_id);
  1038. } else if (recv_entry->type == NES_TIMER_TYPE_RECV) {
  1039. dev_kfree_skb_any(recv_entry->skb);
  1040. }
  1041. kfree(recv_entry);
  1042. spin_lock_irqsave(&cm_node->recv_list_lock, flags);
  1043. }
  1044. spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
  1045. if (cm_node->listener) {
  1046. mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
  1047. } else {
  1048. if (cm_node->apbvt_set && cm_node->nesvnic) {
  1049. nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
  1050. PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
  1051. NES_MANAGE_APBVT_DEL);
  1052. }
  1053. }
  1054. kfree(cm_node);
  1055. atomic_dec(&cm_core->node_cnt);
  1056. atomic_inc(&cm_nodes_destroyed);
  1057. return 0;
  1058. }
  1059. /**
  1060. * process_options
  1061. */
  1062. static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc, u32 optionsize, u32 syn_packet)
  1063. {
  1064. u32 tmp;
  1065. u32 offset = 0;
  1066. union all_known_options *all_options;
  1067. char got_mss_option = 0;
  1068. while (offset < optionsize) {
  1069. all_options = (union all_known_options *)(optionsloc + offset);
  1070. switch (all_options->as_base.optionnum) {
  1071. case OPTION_NUMBER_END:
  1072. offset = optionsize;
  1073. break;
  1074. case OPTION_NUMBER_NONE:
  1075. offset += 1;
  1076. continue;
  1077. case OPTION_NUMBER_MSS:
  1078. nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d Size: %d\n",
  1079. __FUNCTION__,
  1080. all_options->as_mss.length, offset, optionsize);
  1081. got_mss_option = 1;
  1082. if (all_options->as_mss.length != 4) {
  1083. return 1;
  1084. } else {
  1085. tmp = ntohs(all_options->as_mss.mss);
  1086. if (tmp > 0 && tmp < cm_node->tcp_cntxt.mss)
  1087. cm_node->tcp_cntxt.mss = tmp;
  1088. }
  1089. break;
  1090. case OPTION_NUMBER_WINDOW_SCALE:
  1091. cm_node->tcp_cntxt.snd_wscale = all_options->as_windowscale.shiftcount;
  1092. break;
  1093. case OPTION_NUMBER_WRITE0:
  1094. cm_node->send_write0 = 1;
  1095. break;
  1096. default:
  1097. nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
  1098. all_options->as_base.optionnum);
  1099. break;
  1100. }
  1101. offset += all_options->as_base.length;
  1102. }
  1103. if ((!got_mss_option) && (syn_packet))
  1104. cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
  1105. return 0;
  1106. }
  1107. /**
  1108. * process_packet
  1109. */
  1110. static int process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1111. struct nes_cm_core *cm_core)
  1112. {
  1113. int optionsize;
  1114. int datasize;
  1115. int ret = 0;
  1116. struct tcphdr *tcph = tcp_hdr(skb);
  1117. u32 inc_sequence;
  1118. if (cm_node->state == NES_CM_STATE_SYN_SENT && tcph->syn) {
  1119. inc_sequence = ntohl(tcph->seq);
  1120. cm_node->tcp_cntxt.rcv_nxt = inc_sequence;
  1121. }
  1122. if ((!tcph) || (cm_node->state == NES_CM_STATE_TSA)) {
  1123. BUG_ON(!tcph);
  1124. atomic_inc(&cm_accel_dropped_pkts);
  1125. return -1;
  1126. }
  1127. if (tcph->rst) {
  1128. atomic_inc(&cm_resets_recvd);
  1129. nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u. refcnt=%d\n",
  1130. cm_node, cm_node->state, atomic_read(&cm_node->ref_count));
  1131. switch (cm_node->state) {
  1132. case NES_CM_STATE_LISTENING:
  1133. rem_ref_cm_node(cm_core, cm_node);
  1134. break;
  1135. case NES_CM_STATE_TSA:
  1136. case NES_CM_STATE_CLOSED:
  1137. break;
  1138. case NES_CM_STATE_SYN_RCVD:
  1139. nes_debug(NES_DBG_CM, "Received a reset for local 0x%08X:%04X,"
  1140. " remote 0x%08X:%04X, node state = %u\n",
  1141. cm_node->loc_addr, cm_node->loc_port,
  1142. cm_node->rem_addr, cm_node->rem_port,
  1143. cm_node->state);
  1144. rem_ref_cm_node(cm_core, cm_node);
  1145. break;
  1146. case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
  1147. case NES_CM_STATE_ESTABLISHED:
  1148. case NES_CM_STATE_MPAREQ_SENT:
  1149. default:
  1150. nes_debug(NES_DBG_CM, "Received a reset for local 0x%08X:%04X,"
  1151. " remote 0x%08X:%04X, node state = %u refcnt=%d\n",
  1152. cm_node->loc_addr, cm_node->loc_port,
  1153. cm_node->rem_addr, cm_node->rem_port,
  1154. cm_node->state, atomic_read(&cm_node->ref_count));
  1155. // create event
  1156. cm_node->state = NES_CM_STATE_CLOSED;
  1157. create_event(cm_node, NES_CM_EVENT_ABORTED);
  1158. break;
  1159. }
  1160. return -1;
  1161. }
  1162. optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
  1163. skb_pull(skb, ip_hdr(skb)->ihl << 2);
  1164. skb_pull(skb, tcph->doff << 2);
  1165. datasize = skb->len;
  1166. inc_sequence = ntohl(tcph->seq);
  1167. nes_debug(NES_DBG_CM, "datasize = %u, sequence = 0x%08X, ack_seq = 0x%08X,"
  1168. " rcv_nxt = 0x%08X Flags: %s %s.\n",
  1169. datasize, inc_sequence, ntohl(tcph->ack_seq),
  1170. cm_node->tcp_cntxt.rcv_nxt, (tcph->syn ? "SYN":""),
  1171. (tcph->ack ? "ACK":""));
  1172. if (!tcph->syn && (inc_sequence != cm_node->tcp_cntxt.rcv_nxt)
  1173. ) {
  1174. nes_debug(NES_DBG_CM, "dropping packet, datasize = %u, sequence = 0x%08X,"
  1175. " ack_seq = 0x%08X, rcv_nxt = 0x%08X Flags: %s.\n",
  1176. datasize, inc_sequence, ntohl(tcph->ack_seq),
  1177. cm_node->tcp_cntxt.rcv_nxt, (tcph->ack ? "ACK":""));
  1178. if (cm_node->state == NES_CM_STATE_LISTENING) {
  1179. rem_ref_cm_node(cm_core, cm_node);
  1180. }
  1181. return -1;
  1182. }
  1183. cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
  1184. if (optionsize) {
  1185. u8 *optionsloc = (u8 *)&tcph[1];
  1186. if (process_options(cm_node, optionsloc, optionsize, (u32)tcph->syn)) {
  1187. nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n", __FUNCTION__, cm_node);
  1188. send_reset(cm_node);
  1189. if (cm_node->state != NES_CM_STATE_SYN_SENT)
  1190. rem_ref_cm_node(cm_core, cm_node);
  1191. return 0;
  1192. }
  1193. } else if (tcph->syn)
  1194. cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
  1195. cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
  1196. cm_node->tcp_cntxt.snd_wscale;
  1197. if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd) {
  1198. cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
  1199. }
  1200. if (tcph->ack) {
  1201. cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
  1202. switch (cm_node->state) {
  1203. case NES_CM_STATE_SYN_RCVD:
  1204. case NES_CM_STATE_SYN_SENT:
  1205. /* read and stash current sequence number */
  1206. if (cm_node->tcp_cntxt.rem_ack_num != cm_node->tcp_cntxt.loc_seq_num) {
  1207. nes_debug(NES_DBG_CM, "ERROR - cm_node->tcp_cntxt.rem_ack_num !="
  1208. " cm_node->tcp_cntxt.loc_seq_num\n");
  1209. send_reset(cm_node);
  1210. return 0;
  1211. }
  1212. if (cm_node->state == NES_CM_STATE_SYN_SENT)
  1213. cm_node->state = NES_CM_STATE_ONE_SIDE_ESTABLISHED;
  1214. else {
  1215. cm_node->state = NES_CM_STATE_ESTABLISHED;
  1216. }
  1217. break;
  1218. case NES_CM_STATE_LAST_ACK:
  1219. cm_node->state = NES_CM_STATE_CLOSED;
  1220. break;
  1221. case NES_CM_STATE_FIN_WAIT1:
  1222. cm_node->state = NES_CM_STATE_FIN_WAIT2;
  1223. break;
  1224. case NES_CM_STATE_CLOSING:
  1225. cm_node->state = NES_CM_STATE_TIME_WAIT;
  1226. /* need to schedule this to happen in 2MSL timeouts */
  1227. cm_node->state = NES_CM_STATE_CLOSED;
  1228. break;
  1229. case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
  1230. case NES_CM_STATE_ESTABLISHED:
  1231. case NES_CM_STATE_MPAREQ_SENT:
  1232. case NES_CM_STATE_CLOSE_WAIT:
  1233. case NES_CM_STATE_TIME_WAIT:
  1234. case NES_CM_STATE_CLOSED:
  1235. break;
  1236. case NES_CM_STATE_LISTENING:
  1237. nes_debug(NES_DBG_CM, "Received an ACK on a listening port (SYN %d)\n", tcph->syn);
  1238. cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
  1239. send_reset(cm_node);
  1240. /* send_reset bumps refcount, this should have been a new node */
  1241. rem_ref_cm_node(cm_core, cm_node);
  1242. return -1;
  1243. break;
  1244. case NES_CM_STATE_TSA:
  1245. nes_debug(NES_DBG_CM, "Received a packet with the ack bit set while in TSA state\n");
  1246. break;
  1247. case NES_CM_STATE_UNKNOWN:
  1248. case NES_CM_STATE_INITED:
  1249. case NES_CM_STATE_ACCEPTING:
  1250. case NES_CM_STATE_FIN_WAIT2:
  1251. default:
  1252. nes_debug(NES_DBG_CM, "Received ack from unknown state: %x\n",
  1253. cm_node->state);
  1254. send_reset(cm_node);
  1255. break;
  1256. }
  1257. }
  1258. if (tcph->syn) {
  1259. if (cm_node->state == NES_CM_STATE_LISTENING) {
  1260. /* do not exceed backlog */
  1261. atomic_inc(&cm_node->listener->pend_accepts_cnt);
  1262. if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
  1263. cm_node->listener->backlog) {
  1264. nes_debug(NES_DBG_CM, "drop syn due to backlog pressure \n");
  1265. cm_backlog_drops++;
  1266. atomic_dec(&cm_node->listener->pend_accepts_cnt);
  1267. rem_ref_cm_node(cm_core, cm_node);
  1268. return 0;
  1269. }
  1270. cm_node->accept_pend = 1;
  1271. }
  1272. if (datasize == 0)
  1273. cm_node->tcp_cntxt.rcv_nxt ++;
  1274. if (cm_node->state == NES_CM_STATE_LISTENING) {
  1275. cm_node->state = NES_CM_STATE_SYN_RCVD;
  1276. send_syn(cm_node, 1);
  1277. }
  1278. if (cm_node->state == NES_CM_STATE_ONE_SIDE_ESTABLISHED) {
  1279. cm_node->state = NES_CM_STATE_ESTABLISHED;
  1280. /* send final handshake ACK */
  1281. ret = send_ack(cm_node);
  1282. if (ret < 0)
  1283. return ret;
  1284. cm_node->state = NES_CM_STATE_MPAREQ_SENT;
  1285. ret = send_mpa_request(cm_node);
  1286. if (ret < 0)
  1287. return ret;
  1288. }
  1289. }
  1290. if (tcph->fin) {
  1291. cm_node->tcp_cntxt.rcv_nxt++;
  1292. switch (cm_node->state) {
  1293. case NES_CM_STATE_SYN_RCVD:
  1294. case NES_CM_STATE_SYN_SENT:
  1295. case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
  1296. case NES_CM_STATE_ESTABLISHED:
  1297. case NES_CM_STATE_ACCEPTING:
  1298. case NES_CM_STATE_MPAREQ_SENT:
  1299. cm_node->state = NES_CM_STATE_CLOSE_WAIT;
  1300. cm_node->state = NES_CM_STATE_LAST_ACK;
  1301. ret = send_fin(cm_node, NULL);
  1302. break;
  1303. case NES_CM_STATE_FIN_WAIT1:
  1304. cm_node->state = NES_CM_STATE_CLOSING;
  1305. ret = send_ack(cm_node);
  1306. break;
  1307. case NES_CM_STATE_FIN_WAIT2:
  1308. cm_node->state = NES_CM_STATE_TIME_WAIT;
  1309. cm_node->tcp_cntxt.loc_seq_num ++;
  1310. ret = send_ack(cm_node);
  1311. /* need to schedule this to happen in 2MSL timeouts */
  1312. cm_node->state = NES_CM_STATE_CLOSED;
  1313. break;
  1314. case NES_CM_STATE_CLOSE_WAIT:
  1315. case NES_CM_STATE_LAST_ACK:
  1316. case NES_CM_STATE_CLOSING:
  1317. case NES_CM_STATE_TSA:
  1318. default:
  1319. nes_debug(NES_DBG_CM, "Received a fin while in %x state\n",
  1320. cm_node->state);
  1321. ret = -EINVAL;
  1322. break;
  1323. }
  1324. }
  1325. if (datasize) {
  1326. u8 *dataloc = skb->data;
  1327. /* figure out what state we are in and handle transition to next state */
  1328. switch (cm_node->state) {
  1329. case NES_CM_STATE_LISTENING:
  1330. case NES_CM_STATE_SYN_RCVD:
  1331. case NES_CM_STATE_SYN_SENT:
  1332. case NES_CM_STATE_FIN_WAIT1:
  1333. case NES_CM_STATE_FIN_WAIT2:
  1334. case NES_CM_STATE_CLOSE_WAIT:
  1335. case NES_CM_STATE_LAST_ACK:
  1336. case NES_CM_STATE_CLOSING:
  1337. break;
  1338. case NES_CM_STATE_MPAREQ_SENT:
  1339. /* recv the mpa res frame, ret=frame len (incl priv data) */
  1340. ret = parse_mpa(cm_node, dataloc, datasize);
  1341. if (ret < 0)
  1342. break;
  1343. /* set the req frame payload len in skb */
  1344. /* we are done handling this state, set node to a TSA state */
  1345. cm_node->state = NES_CM_STATE_TSA;
  1346. send_ack(cm_node);
  1347. create_event(cm_node, NES_CM_EVENT_CONNECTED);
  1348. break;
  1349. case NES_CM_STATE_ESTABLISHED:
  1350. /* we are expecting an MPA req frame */
  1351. ret = parse_mpa(cm_node, dataloc, datasize);
  1352. if (ret < 0) {
  1353. break;
  1354. }
  1355. cm_node->state = NES_CM_STATE_TSA;
  1356. send_ack(cm_node);
  1357. /* we got a valid MPA request, create an event */
  1358. create_event(cm_node, NES_CM_EVENT_MPA_REQ);
  1359. break;
  1360. case NES_CM_STATE_TSA:
  1361. handle_exception_pkt(cm_node, skb);
  1362. break;
  1363. case NES_CM_STATE_UNKNOWN:
  1364. case NES_CM_STATE_INITED:
  1365. default:
  1366. ret = -1;
  1367. }
  1368. }
  1369. return ret;
  1370. }
  1371. /**
  1372. * mini_cm_listen - create a listen node with params
  1373. */
  1374. static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
  1375. struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
  1376. {
  1377. struct nes_cm_listener *listener;
  1378. unsigned long flags;
  1379. nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
  1380. cm_info->loc_addr, cm_info->loc_port);
  1381. /* cannot have multiple matching listeners */
  1382. listener = find_listener(cm_core, htonl(cm_info->loc_addr),
  1383. htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
  1384. if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
  1385. /* find automatically incs ref count ??? */
  1386. atomic_dec(&listener->ref_count);
  1387. nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
  1388. return NULL;
  1389. }
  1390. if (!listener) {
  1391. /* create a CM listen node (1/2 node to compare incoming traffic to) */
  1392. listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
  1393. if (!listener) {
  1394. nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
  1395. return NULL;
  1396. }
  1397. memset(listener, 0, sizeof(struct nes_cm_listener));
  1398. listener->loc_addr = htonl(cm_info->loc_addr);
  1399. listener->loc_port = htons(cm_info->loc_port);
  1400. listener->reused_node = 0;
  1401. atomic_set(&listener->ref_count, 1);
  1402. }
  1403. /* pasive case */
  1404. /* find already inc'ed the ref count */
  1405. else {
  1406. listener->reused_node = 1;
  1407. }
  1408. listener->cm_id = cm_info->cm_id;
  1409. atomic_set(&listener->pend_accepts_cnt, 0);
  1410. listener->cm_core = cm_core;
  1411. listener->nesvnic = nesvnic;
  1412. atomic_inc(&cm_core->node_cnt);
  1413. atomic_inc(&cm_core->session_id);
  1414. listener->session_id = (u32)(atomic_read(&cm_core->session_id) + current->tgid);
  1415. listener->conn_type = cm_info->conn_type;
  1416. listener->backlog = cm_info->backlog;
  1417. listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
  1418. if (!listener->reused_node) {
  1419. spin_lock_irqsave(&cm_core->listen_list_lock, flags);
  1420. list_add(&listener->list, &cm_core->listen_list.list);
  1421. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  1422. atomic_inc(&cm_core->listen_node_cnt);
  1423. }
  1424. nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
  1425. " listener = %p, backlog = %d, cm_id = %p.\n",
  1426. cm_info->loc_addr, cm_info->loc_port,
  1427. listener, listener->backlog, listener->cm_id);
  1428. return listener;
  1429. }
  1430. /**
  1431. * mini_cm_connect - make a connection node with params
  1432. */
  1433. static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
  1434. struct nes_vnic *nesvnic,
  1435. struct ietf_mpa_frame *mpa_frame,
  1436. struct nes_cm_info *cm_info)
  1437. {
  1438. int ret = 0;
  1439. struct nes_cm_node *cm_node;
  1440. struct nes_cm_listener *loopbackremotelistener;
  1441. struct nes_cm_node *loopbackremotenode;
  1442. struct nes_cm_info loopback_cm_info;
  1443. u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) +
  1444. ntohs(mpa_frame->priv_data_len);
  1445. cm_info->loc_addr = htonl(cm_info->loc_addr);
  1446. cm_info->rem_addr = htonl(cm_info->rem_addr);
  1447. cm_info->loc_port = htons(cm_info->loc_port);
  1448. cm_info->rem_port = htons(cm_info->rem_port);
  1449. /* create a CM connection node */
  1450. cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
  1451. if (!cm_node)
  1452. return NULL;
  1453. // set our node side to client (active) side
  1454. cm_node->tcp_cntxt.client = 1;
  1455. cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
  1456. if (cm_info->loc_addr == cm_info->rem_addr) {
  1457. loopbackremotelistener = find_listener(cm_core, cm_node->rem_addr,
  1458. cm_node->rem_port, NES_CM_LISTENER_ACTIVE_STATE);
  1459. if (loopbackremotelistener == NULL) {
  1460. create_event(cm_node, NES_CM_EVENT_ABORTED);
  1461. } else {
  1462. atomic_inc(&cm_loopbacks);
  1463. loopback_cm_info = *cm_info;
  1464. loopback_cm_info.loc_port = cm_info->rem_port;
  1465. loopback_cm_info.rem_port = cm_info->loc_port;
  1466. loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
  1467. loopbackremotenode = make_cm_node(cm_core, nesvnic, &loopback_cm_info,
  1468. loopbackremotelistener);
  1469. loopbackremotenode->loopbackpartner = cm_node;
  1470. loopbackremotenode->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
  1471. cm_node->loopbackpartner = loopbackremotenode;
  1472. memcpy(loopbackremotenode->mpa_frame_buf, &mpa_frame->priv_data,
  1473. mpa_frame_size);
  1474. loopbackremotenode->mpa_frame_size = mpa_frame_size -
  1475. sizeof(struct ietf_mpa_frame);
  1476. // we are done handling this state, set node to a TSA state
  1477. cm_node->state = NES_CM_STATE_TSA;
  1478. cm_node->tcp_cntxt.rcv_nxt = loopbackremotenode->tcp_cntxt.loc_seq_num;
  1479. loopbackremotenode->tcp_cntxt.rcv_nxt = cm_node->tcp_cntxt.loc_seq_num;
  1480. cm_node->tcp_cntxt.max_snd_wnd = loopbackremotenode->tcp_cntxt.rcv_wnd;
  1481. loopbackremotenode->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
  1482. cm_node->tcp_cntxt.snd_wnd = loopbackremotenode->tcp_cntxt.rcv_wnd;
  1483. loopbackremotenode->tcp_cntxt.snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
  1484. cm_node->tcp_cntxt.snd_wscale = loopbackremotenode->tcp_cntxt.rcv_wscale;
  1485. loopbackremotenode->tcp_cntxt.snd_wscale = cm_node->tcp_cntxt.rcv_wscale;
  1486. create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
  1487. }
  1488. return cm_node;
  1489. }
  1490. /* set our node side to client (active) side */
  1491. cm_node->tcp_cntxt.client = 1;
  1492. /* init our MPA frame ptr */
  1493. memcpy(&cm_node->mpa_frame, mpa_frame, mpa_frame_size);
  1494. cm_node->mpa_frame_size = mpa_frame_size;
  1495. /* send a syn and goto syn sent state */
  1496. cm_node->state = NES_CM_STATE_SYN_SENT;
  1497. ret = send_syn(cm_node, 0);
  1498. nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X, port=0x%04x,"
  1499. " cm_node=%p, cm_id = %p.\n",
  1500. cm_node->rem_addr, cm_node->rem_port, cm_node, cm_node->cm_id);
  1501. return cm_node;
  1502. }
  1503. /**
  1504. * mini_cm_accept - accept a connection
  1505. * This function is never called
  1506. */
  1507. static int mini_cm_accept(struct nes_cm_core *cm_core, struct ietf_mpa_frame *mpa_frame,
  1508. struct nes_cm_node *cm_node)
  1509. {
  1510. return 0;
  1511. }
  1512. /**
  1513. * mini_cm_reject - reject and teardown a connection
  1514. */
  1515. static int mini_cm_reject(struct nes_cm_core *cm_core,
  1516. struct ietf_mpa_frame *mpa_frame,
  1517. struct nes_cm_node *cm_node)
  1518. {
  1519. int ret = 0;
  1520. struct sk_buff *skb;
  1521. u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) +
  1522. ntohs(mpa_frame->priv_data_len);
  1523. skb = get_free_pkt(cm_node);
  1524. if (!skb) {
  1525. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  1526. return -1;
  1527. }
  1528. /* send an MPA Request frame */
  1529. form_cm_frame(skb, cm_node, NULL, 0, mpa_frame, mpa_frame_size, SET_ACK | SET_FIN);
  1530. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  1531. cm_node->state = NES_CM_STATE_CLOSED;
  1532. ret = send_fin(cm_node, NULL);
  1533. if (ret < 0) {
  1534. printk(KERN_INFO PFX "failed to send MPA Reply (reject)\n");
  1535. return ret;
  1536. }
  1537. return ret;
  1538. }
  1539. /**
  1540. * mini_cm_close
  1541. */
  1542. static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
  1543. {
  1544. int ret = 0;
  1545. if (!cm_core || !cm_node)
  1546. return -EINVAL;
  1547. switch (cm_node->state) {
  1548. /* if passed in node is null, create a reference key node for node search */
  1549. /* check if we found an owner node for this pkt */
  1550. case NES_CM_STATE_SYN_RCVD:
  1551. case NES_CM_STATE_SYN_SENT:
  1552. case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
  1553. case NES_CM_STATE_ESTABLISHED:
  1554. case NES_CM_STATE_ACCEPTING:
  1555. case NES_CM_STATE_MPAREQ_SENT:
  1556. cm_node->state = NES_CM_STATE_FIN_WAIT1;
  1557. send_fin(cm_node, NULL);
  1558. break;
  1559. case NES_CM_STATE_CLOSE_WAIT:
  1560. cm_node->state = NES_CM_STATE_LAST_ACK;
  1561. send_fin(cm_node, NULL);
  1562. break;
  1563. case NES_CM_STATE_FIN_WAIT1:
  1564. case NES_CM_STATE_FIN_WAIT2:
  1565. case NES_CM_STATE_LAST_ACK:
  1566. case NES_CM_STATE_TIME_WAIT:
  1567. case NES_CM_STATE_CLOSING:
  1568. ret = -1;
  1569. break;
  1570. case NES_CM_STATE_LISTENING:
  1571. case NES_CM_STATE_UNKNOWN:
  1572. case NES_CM_STATE_INITED:
  1573. case NES_CM_STATE_CLOSED:
  1574. case NES_CM_STATE_TSA:
  1575. ret = rem_ref_cm_node(cm_core, cm_node);
  1576. break;
  1577. }
  1578. cm_node->cm_id = NULL;
  1579. return ret;
  1580. }
  1581. /**
  1582. * recv_pkt - recv an ETHERNET packet, and process it through CM
  1583. * node state machine
  1584. */
  1585. static int mini_cm_recv_pkt(struct nes_cm_core *cm_core, struct nes_vnic *nesvnic,
  1586. struct sk_buff *skb)
  1587. {
  1588. struct nes_cm_node *cm_node = NULL;
  1589. struct nes_cm_listener *listener = NULL;
  1590. struct iphdr *iph;
  1591. struct tcphdr *tcph;
  1592. struct nes_cm_info nfo;
  1593. int ret = 0;
  1594. if (!skb || skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
  1595. ret = -EINVAL;
  1596. goto out;
  1597. }
  1598. iph = (struct iphdr *)skb->data;
  1599. tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
  1600. skb_reset_network_header(skb);
  1601. skb_set_transport_header(skb, sizeof(*tcph));
  1602. skb->len = ntohs(iph->tot_len);
  1603. nfo.loc_addr = ntohl(iph->daddr);
  1604. nfo.loc_port = ntohs(tcph->dest);
  1605. nfo.rem_addr = ntohl(iph->saddr);
  1606. nfo.rem_port = ntohs(tcph->source);
  1607. nes_debug(NES_DBG_CM, "Received packet: dest=0x%08X:0x%04X src=0x%08X:0x%04X\n",
  1608. iph->daddr, tcph->dest, iph->saddr, tcph->source);
  1609. /* note: this call is going to increment cm_node ref count */
  1610. cm_node = find_node(cm_core,
  1611. nfo.rem_port, nfo.rem_addr,
  1612. nfo.loc_port, nfo.loc_addr);
  1613. if (!cm_node) {
  1614. listener = find_listener(cm_core, nfo.loc_addr, nfo.loc_port,
  1615. NES_CM_LISTENER_ACTIVE_STATE);
  1616. if (listener) {
  1617. nfo.cm_id = listener->cm_id;
  1618. nfo.conn_type = listener->conn_type;
  1619. } else {
  1620. nfo.cm_id = NULL;
  1621. nfo.conn_type = 0;
  1622. }
  1623. cm_node = make_cm_node(cm_core, nesvnic, &nfo, listener);
  1624. if (!cm_node) {
  1625. nes_debug(NES_DBG_CM, "Unable to allocate node\n");
  1626. if (listener) {
  1627. nes_debug(NES_DBG_CM, "unable to allocate node and decrementing listener refcount\n");
  1628. atomic_dec(&listener->ref_count);
  1629. }
  1630. ret = -1;
  1631. goto out;
  1632. }
  1633. if (!listener) {
  1634. nes_debug(NES_DBG_CM, "Packet found for unknown port %x refcnt=%d\n",
  1635. nfo.loc_port, atomic_read(&cm_node->ref_count));
  1636. if (!tcph->rst) {
  1637. nes_debug(NES_DBG_CM, "Packet found for unknown port=%d"
  1638. " rem_port=%d refcnt=%d\n",
  1639. nfo.loc_port, nfo.rem_port, atomic_read(&cm_node->ref_count));
  1640. cm_node->tcp_cntxt.rcv_nxt = ntohl(tcph->seq);
  1641. cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
  1642. send_reset(cm_node);
  1643. }
  1644. rem_ref_cm_node(cm_core, cm_node);
  1645. ret = -1;
  1646. goto out;
  1647. }
  1648. add_ref_cm_node(cm_node);
  1649. cm_node->state = NES_CM_STATE_LISTENING;
  1650. }
  1651. nes_debug(NES_DBG_CM, "Processing Packet for node %p, data = (%p):\n",
  1652. cm_node, skb->data);
  1653. process_packet(cm_node, skb, cm_core);
  1654. rem_ref_cm_node(cm_core, cm_node);
  1655. out:
  1656. if (skb)
  1657. dev_kfree_skb_any(skb);
  1658. return ret;
  1659. }
  1660. /**
  1661. * nes_cm_alloc_core - allocate a top level instance of a cm core
  1662. */
  1663. static struct nes_cm_core *nes_cm_alloc_core(void)
  1664. {
  1665. int i;
  1666. struct nes_cm_core *cm_core;
  1667. struct sk_buff *skb = NULL;
  1668. /* setup the CM core */
  1669. /* alloc top level core control structure */
  1670. cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
  1671. if (!cm_core)
  1672. return NULL;
  1673. INIT_LIST_HEAD(&cm_core->connected_nodes);
  1674. init_timer(&cm_core->tcp_timer);
  1675. cm_core->tcp_timer.function = nes_cm_timer_tick;
  1676. cm_core->mtu = NES_CM_DEFAULT_MTU;
  1677. cm_core->state = NES_CM_STATE_INITED;
  1678. cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
  1679. atomic_set(&cm_core->session_id, 0);
  1680. atomic_set(&cm_core->events_posted, 0);
  1681. /* init the packet lists */
  1682. skb_queue_head_init(&cm_core->tx_free_list);
  1683. for (i = 0; i < NES_CM_DEFAULT_FRAME_CNT; i++) {
  1684. skb = dev_alloc_skb(cm_core->mtu);
  1685. if (!skb) {
  1686. kfree(cm_core);
  1687. return NULL;
  1688. }
  1689. /* add 'raw' skb to free frame list */
  1690. skb_queue_head(&cm_core->tx_free_list, skb);
  1691. }
  1692. cm_core->api = &nes_cm_api;
  1693. spin_lock_init(&cm_core->ht_lock);
  1694. spin_lock_init(&cm_core->listen_list_lock);
  1695. INIT_LIST_HEAD(&cm_core->listen_list.list);
  1696. nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
  1697. nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
  1698. cm_core->event_wq = create_singlethread_workqueue("nesewq");
  1699. cm_core->post_event = nes_cm_post_event;
  1700. nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
  1701. cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
  1702. print_core(cm_core);
  1703. return cm_core;
  1704. }
  1705. /**
  1706. * mini_cm_dealloc_core - deallocate a top level instance of a cm core
  1707. */
  1708. static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
  1709. {
  1710. nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
  1711. if (!cm_core)
  1712. return -EINVAL;
  1713. barrier();
  1714. if (timer_pending(&cm_core->tcp_timer)) {
  1715. del_timer(&cm_core->tcp_timer);
  1716. }
  1717. destroy_workqueue(cm_core->event_wq);
  1718. destroy_workqueue(cm_core->disconn_wq);
  1719. nes_debug(NES_DBG_CM, "\n");
  1720. kfree(cm_core);
  1721. return 0;
  1722. }
  1723. /**
  1724. * mini_cm_get
  1725. */
  1726. static int mini_cm_get(struct nes_cm_core *cm_core)
  1727. {
  1728. return cm_core->state;
  1729. }
  1730. /**
  1731. * mini_cm_set
  1732. */
  1733. static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
  1734. {
  1735. int ret = 0;
  1736. switch (type) {
  1737. case NES_CM_SET_PKT_SIZE:
  1738. cm_core->mtu = value;
  1739. break;
  1740. case NES_CM_SET_FREE_PKT_Q_SIZE:
  1741. cm_core->free_tx_pkt_max = value;
  1742. break;
  1743. default:
  1744. /* unknown set option */
  1745. ret = -EINVAL;
  1746. }
  1747. return ret;
  1748. }
  1749. /**
  1750. * nes_cm_init_tsa_conn setup HW; MPA frames must be
  1751. * successfully exchanged when this is called
  1752. */
  1753. static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
  1754. {
  1755. int ret = 0;
  1756. if (!nesqp)
  1757. return -EINVAL;
  1758. nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
  1759. NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
  1760. NES_QPCONTEXT_MISC_DROS);
  1761. if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
  1762. nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
  1763. nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
  1764. nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
  1765. nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
  1766. (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
  1767. nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
  1768. (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
  1769. NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
  1770. nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
  1771. (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
  1772. NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
  1773. nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
  1774. nesqp->nesqp_context->ts_recent = 0;
  1775. nesqp->nesqp_context->ts_age = 0;
  1776. nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  1777. nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
  1778. nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
  1779. nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
  1780. cm_node->tcp_cntxt.rcv_wscale);
  1781. nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  1782. nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  1783. nesqp->nesqp_context->srtt = 0;
  1784. nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
  1785. nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
  1786. nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
  1787. nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
  1788. nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  1789. nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
  1790. nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
  1791. " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
  1792. nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
  1793. le32_to_cpu(nesqp->nesqp_context->snd_nxt),
  1794. cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
  1795. le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
  1796. le32_to_cpu(nesqp->nesqp_context->misc));
  1797. nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
  1798. nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
  1799. nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
  1800. nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
  1801. cm_node->state = NES_CM_STATE_TSA;
  1802. return ret;
  1803. }
  1804. /**
  1805. * nes_cm_disconn
  1806. */
  1807. int nes_cm_disconn(struct nes_qp *nesqp)
  1808. {
  1809. unsigned long flags;
  1810. spin_lock_irqsave(&nesqp->lock, flags);
  1811. if (nesqp->disconn_pending == 0) {
  1812. nesqp->disconn_pending++;
  1813. spin_unlock_irqrestore(&nesqp->lock, flags);
  1814. /* nes_add_ref(&nesqp->ibqp); */
  1815. /* init our disconnect work element, to */
  1816. INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
  1817. queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
  1818. } else {
  1819. spin_unlock_irqrestore(&nesqp->lock, flags);
  1820. nes_rem_ref(&nesqp->ibqp);
  1821. }
  1822. return 0;
  1823. }
  1824. /**
  1825. * nes_disconnect_worker
  1826. */
  1827. static void nes_disconnect_worker(struct work_struct *work)
  1828. {
  1829. struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
  1830. nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
  1831. nesqp->last_aeq, nesqp->hwqp.qp_id);
  1832. nes_cm_disconn_true(nesqp);
  1833. }
  1834. /**
  1835. * nes_cm_disconn_true
  1836. */
  1837. static int nes_cm_disconn_true(struct nes_qp *nesqp)
  1838. {
  1839. unsigned long flags;
  1840. int ret = 0;
  1841. struct iw_cm_id *cm_id;
  1842. struct iw_cm_event cm_event;
  1843. struct nes_vnic *nesvnic;
  1844. u16 last_ae;
  1845. u8 original_hw_tcp_state;
  1846. u8 original_ibqp_state;
  1847. u8 issued_disconnect_reset = 0;
  1848. if (!nesqp) {
  1849. nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
  1850. return -1;
  1851. }
  1852. spin_lock_irqsave(&nesqp->lock, flags);
  1853. cm_id = nesqp->cm_id;
  1854. /* make sure we havent already closed this connection */
  1855. if (!cm_id) {
  1856. nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
  1857. nesqp->hwqp.qp_id);
  1858. spin_unlock_irqrestore(&nesqp->lock, flags);
  1859. nes_rem_ref(&nesqp->ibqp);
  1860. return -1;
  1861. }
  1862. nesvnic = to_nesvnic(nesqp->ibqp.device);
  1863. nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
  1864. original_hw_tcp_state = nesqp->hw_tcp_state;
  1865. original_ibqp_state = nesqp->ibqp_state;
  1866. last_ae = nesqp->last_aeq;
  1867. nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
  1868. if ((nesqp->cm_id) && (cm_id->event_handler)) {
  1869. if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
  1870. ((original_ibqp_state == IB_QPS_RTS) &&
  1871. (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
  1872. atomic_inc(&cm_disconnects);
  1873. cm_event.event = IW_CM_EVENT_DISCONNECT;
  1874. if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
  1875. issued_disconnect_reset = 1;
  1876. cm_event.status = IW_CM_EVENT_STATUS_RESET;
  1877. nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event (status reset) for "
  1878. " QP%u, cm_id = %p. \n",
  1879. nesqp->hwqp.qp_id, cm_id);
  1880. } else {
  1881. cm_event.status = IW_CM_EVENT_STATUS_OK;
  1882. }
  1883. cm_event.local_addr = cm_id->local_addr;
  1884. cm_event.remote_addr = cm_id->remote_addr;
  1885. cm_event.private_data = NULL;
  1886. cm_event.private_data_len = 0;
  1887. nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event for "
  1888. " QP%u, SQ Head = %u, SQ Tail = %u. cm_id = %p, refcount = %u.\n",
  1889. nesqp->hwqp.qp_id,
  1890. nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail, cm_id,
  1891. atomic_read(&nesqp->refcount));
  1892. spin_unlock_irqrestore(&nesqp->lock, flags);
  1893. ret = cm_id->event_handler(cm_id, &cm_event);
  1894. if (ret)
  1895. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  1896. spin_lock_irqsave(&nesqp->lock, flags);
  1897. }
  1898. nesqp->disconn_pending = 0;
  1899. /* There might have been another AE while the lock was released */
  1900. original_hw_tcp_state = nesqp->hw_tcp_state;
  1901. original_ibqp_state = nesqp->ibqp_state;
  1902. last_ae = nesqp->last_aeq;
  1903. if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
  1904. ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
  1905. (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
  1906. (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
  1907. (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
  1908. atomic_inc(&cm_closes);
  1909. nesqp->cm_id = NULL;
  1910. nesqp->in_disconnect = 0;
  1911. spin_unlock_irqrestore(&nesqp->lock, flags);
  1912. nes_disconnect(nesqp, 1);
  1913. cm_id->provider_data = nesqp;
  1914. /* Send up the close complete event */
  1915. cm_event.event = IW_CM_EVENT_CLOSE;
  1916. cm_event.status = IW_CM_EVENT_STATUS_OK;
  1917. cm_event.provider_data = cm_id->provider_data;
  1918. cm_event.local_addr = cm_id->local_addr;
  1919. cm_event.remote_addr = cm_id->remote_addr;
  1920. cm_event.private_data = NULL;
  1921. cm_event.private_data_len = 0;
  1922. ret = cm_id->event_handler(cm_id, &cm_event);
  1923. if (ret) {
  1924. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  1925. }
  1926. cm_id->rem_ref(cm_id);
  1927. spin_lock_irqsave(&nesqp->lock, flags);
  1928. if (nesqp->flush_issued == 0) {
  1929. nesqp->flush_issued = 1;
  1930. spin_unlock_irqrestore(&nesqp->lock, flags);
  1931. flush_wqes(nesvnic->nesdev, nesqp, NES_CQP_FLUSH_RQ, 1);
  1932. } else {
  1933. spin_unlock_irqrestore(&nesqp->lock, flags);
  1934. }
  1935. /* This reference is from either ModifyQP or the AE processing,
  1936. there is still a race here with modifyqp */
  1937. nes_rem_ref(&nesqp->ibqp);
  1938. } else {
  1939. cm_id = nesqp->cm_id;
  1940. spin_unlock_irqrestore(&nesqp->lock, flags);
  1941. /* check to see if the inbound reset beat the outbound reset */
  1942. if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
  1943. nes_debug(NES_DBG_CM, "QP%u: Decing refcount due to inbound reset"
  1944. " beating the outbound reset.\n",
  1945. nesqp->hwqp.qp_id);
  1946. nes_rem_ref(&nesqp->ibqp);
  1947. }
  1948. }
  1949. } else {
  1950. nesqp->disconn_pending = 0;
  1951. spin_unlock_irqrestore(&nesqp->lock, flags);
  1952. }
  1953. nes_rem_ref(&nesqp->ibqp);
  1954. return 0;
  1955. }
  1956. /**
  1957. * nes_disconnect
  1958. */
  1959. static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
  1960. {
  1961. int ret = 0;
  1962. struct nes_vnic *nesvnic;
  1963. struct nes_device *nesdev;
  1964. nesvnic = to_nesvnic(nesqp->ibqp.device);
  1965. if (!nesvnic)
  1966. return -EINVAL;
  1967. nesdev = nesvnic->nesdev;
  1968. nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
  1969. atomic_read(&nesvnic->netdev->refcnt));
  1970. if (nesqp->active_conn) {
  1971. /* indicate this connection is NOT active */
  1972. nesqp->active_conn = 0;
  1973. } else {
  1974. /* Need to free the Last Streaming Mode Message */
  1975. if (nesqp->ietf_frame) {
  1976. pci_free_consistent(nesdev->pcidev,
  1977. nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
  1978. nesqp->ietf_frame, nesqp->ietf_frame_pbase);
  1979. }
  1980. }
  1981. /* close the CM node down if it is still active */
  1982. if (nesqp->cm_node) {
  1983. nes_debug(NES_DBG_CM, "Call close API\n");
  1984. g_cm_core->api->close(g_cm_core, nesqp->cm_node);
  1985. nesqp->cm_node = NULL;
  1986. }
  1987. return ret;
  1988. }
  1989. /**
  1990. * nes_accept
  1991. */
  1992. int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  1993. {
  1994. u64 u64temp;
  1995. struct ib_qp *ibqp;
  1996. struct nes_qp *nesqp;
  1997. struct nes_vnic *nesvnic;
  1998. struct nes_device *nesdev;
  1999. struct nes_cm_node *cm_node;
  2000. struct nes_adapter *adapter;
  2001. struct ib_qp_attr attr;
  2002. struct iw_cm_event cm_event;
  2003. struct nes_hw_qp_wqe *wqe;
  2004. struct nes_v4_quad nes_quad;
  2005. u32 crc_value;
  2006. int ret;
  2007. ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
  2008. if (!ibqp)
  2009. return -EINVAL;
  2010. /* get all our handles */
  2011. nesqp = to_nesqp(ibqp);
  2012. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2013. nesdev = nesvnic->nesdev;
  2014. adapter = nesdev->nesadapter;
  2015. nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
  2016. nesvnic, nesvnic->netdev, nesvnic->netdev->name);
  2017. /* since this is from a listen, we were able to put node handle into cm_id */
  2018. cm_node = (struct nes_cm_node *)cm_id->provider_data;
  2019. /* associate the node with the QP */
  2020. nesqp->cm_node = (void *)cm_node;
  2021. nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu\n",
  2022. nesqp->hwqp.qp_id, cm_node, jiffies);
  2023. atomic_inc(&cm_accepts);
  2024. nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
  2025. atomic_read(&nesvnic->netdev->refcnt));
  2026. /* allocate the ietf frame and space for private data */
  2027. nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
  2028. sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
  2029. &nesqp->ietf_frame_pbase);
  2030. if (!nesqp->ietf_frame) {
  2031. nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
  2032. return -ENOMEM;
  2033. }
  2034. /* setup the MPA frame */
  2035. nesqp->private_data_len = conn_param->private_data_len;
  2036. memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
  2037. memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
  2038. conn_param->private_data_len);
  2039. nesqp->ietf_frame->priv_data_len = cpu_to_be16(conn_param->private_data_len);
  2040. nesqp->ietf_frame->rev = mpa_version;
  2041. nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
  2042. /* setup our first outgoing iWarp send WQE (the IETF frame response) */
  2043. wqe = &nesqp->hwqp.sq_vbase[0];
  2044. if (cm_id->remote_addr.sin_addr.s_addr != cm_id->local_addr.sin_addr.s_addr) {
  2045. u64temp = (unsigned long)nesqp;
  2046. u64temp |= NES_SW_CONTEXT_ALIGN>>1;
  2047. set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
  2048. u64temp);
  2049. wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
  2050. cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING | NES_IWARP_SQ_WQE_WRPDU);
  2051. wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
  2052. cpu_to_le32(conn_param->private_data_len + sizeof(struct ietf_mpa_frame));
  2053. wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
  2054. cpu_to_le32((u32)nesqp->ietf_frame_pbase);
  2055. wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
  2056. cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
  2057. wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
  2058. cpu_to_le32(conn_param->private_data_len + sizeof(struct ietf_mpa_frame));
  2059. wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
  2060. nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
  2061. NES_QPCONTEXT_ORDIRD_LSMM_PRESENT | NES_QPCONTEXT_ORDIRD_WRPDU);
  2062. } else {
  2063. nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
  2064. NES_QPCONTEXT_ORDIRD_WRPDU | NES_QPCONTEXT_ORDIRD_ALSMM));
  2065. }
  2066. nesqp->skip_lsmm = 1;
  2067. /* Cache the cm_id in the qp */
  2068. nesqp->cm_id = cm_id;
  2069. cm_node->cm_id = cm_id;
  2070. /* nesqp->cm_node = (void *)cm_id->provider_data; */
  2071. cm_id->provider_data = nesqp;
  2072. nesqp->active_conn = 0;
  2073. nes_cm_init_tsa_conn(nesqp, cm_node);
  2074. nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
  2075. nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
  2076. nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
  2077. nesqp->nesqp_context->misc2 |= cpu_to_le32(
  2078. (u32)PCI_FUNC(nesdev->pcidev->devfn) << NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
  2079. nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
  2080. nes_arp_table(nesdev, le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
  2081. NES_ARP_RESOLVE) << 16);
  2082. nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
  2083. jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
  2084. nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
  2085. nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
  2086. ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
  2087. nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
  2088. memset(&nes_quad, 0, sizeof(nes_quad));
  2089. nes_quad.DstIpAdrIndex = cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
  2090. nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
  2091. nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
  2092. nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
  2093. /* Produce hash key */
  2094. crc_value = get_crc_value(&nes_quad);
  2095. nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
  2096. nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
  2097. nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
  2098. nesqp->hte_index &= adapter->hte_index_mask;
  2099. nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
  2100. cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
  2101. nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = 0x%08X:0x%04X,"
  2102. " rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + private data length=%zu.\n",
  2103. nesqp->hwqp.qp_id,
  2104. ntohl(cm_id->remote_addr.sin_addr.s_addr),
  2105. ntohs(cm_id->remote_addr.sin_port),
  2106. ntohl(cm_id->local_addr.sin_addr.s_addr),
  2107. ntohs(cm_id->local_addr.sin_port),
  2108. le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
  2109. le32_to_cpu(nesqp->nesqp_context->snd_nxt),
  2110. conn_param->private_data_len+sizeof(struct ietf_mpa_frame));
  2111. attr.qp_state = IB_QPS_RTS;
  2112. nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
  2113. /* notify OF layer that accept event was successfull */
  2114. cm_id->add_ref(cm_id);
  2115. cm_event.event = IW_CM_EVENT_ESTABLISHED;
  2116. cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
  2117. cm_event.provider_data = (void *)nesqp;
  2118. cm_event.local_addr = cm_id->local_addr;
  2119. cm_event.remote_addr = cm_id->remote_addr;
  2120. cm_event.private_data = NULL;
  2121. cm_event.private_data_len = 0;
  2122. ret = cm_id->event_handler(cm_id, &cm_event);
  2123. if (cm_node->loopbackpartner) {
  2124. cm_node->loopbackpartner->mpa_frame_size = nesqp->private_data_len;
  2125. /* copy entire MPA frame to our cm_node's frame */
  2126. memcpy(cm_node->loopbackpartner->mpa_frame_buf, nesqp->ietf_frame->priv_data,
  2127. nesqp->private_data_len);
  2128. create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
  2129. }
  2130. if (ret)
  2131. printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
  2132. __FUNCTION__, __LINE__, ret);
  2133. return 0;
  2134. }
  2135. /**
  2136. * nes_reject
  2137. */
  2138. int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
  2139. {
  2140. struct nes_cm_node *cm_node;
  2141. struct nes_cm_core *cm_core;
  2142. atomic_inc(&cm_rejects);
  2143. cm_node = (struct nes_cm_node *) cm_id->provider_data;
  2144. cm_core = cm_node->cm_core;
  2145. cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
  2146. strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
  2147. memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
  2148. cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
  2149. cm_node->mpa_frame.rev = mpa_version;
  2150. cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
  2151. cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
  2152. return 0;
  2153. }
  2154. /**
  2155. * nes_connect
  2156. * setup and launch cm connect node
  2157. */
  2158. int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  2159. {
  2160. struct ib_qp *ibqp;
  2161. struct nes_qp *nesqp;
  2162. struct nes_vnic *nesvnic;
  2163. struct nes_device *nesdev;
  2164. struct nes_cm_node *cm_node;
  2165. struct nes_cm_info cm_info;
  2166. ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
  2167. if (!ibqp)
  2168. return -EINVAL;
  2169. nesqp = to_nesqp(ibqp);
  2170. if (!nesqp)
  2171. return -EINVAL;
  2172. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2173. if (!nesvnic)
  2174. return -EINVAL;
  2175. nesdev = nesvnic->nesdev;
  2176. if (!nesdev)
  2177. return -EINVAL;
  2178. atomic_inc(&cm_connects);
  2179. nesqp->ietf_frame = kzalloc(sizeof(struct ietf_mpa_frame) +
  2180. conn_param->private_data_len, GFP_KERNEL);
  2181. if (!nesqp->ietf_frame)
  2182. return -ENOMEM;
  2183. /* set qp as having an active connection */
  2184. nesqp->active_conn = 1;
  2185. nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = 0x%08X:0x%04X.\n",
  2186. nesqp->hwqp.qp_id,
  2187. ntohl(cm_id->remote_addr.sin_addr.s_addr),
  2188. ntohs(cm_id->remote_addr.sin_port),
  2189. ntohl(cm_id->local_addr.sin_addr.s_addr),
  2190. ntohs(cm_id->local_addr.sin_port));
  2191. /* cache the cm_id in the qp */
  2192. nesqp->cm_id = cm_id;
  2193. cm_id->provider_data = nesqp;
  2194. /* copy the private data */
  2195. if (conn_param->private_data_len) {
  2196. memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
  2197. conn_param->private_data_len);
  2198. }
  2199. nesqp->private_data_len = conn_param->private_data_len;
  2200. nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
  2201. nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
  2202. nes_debug(NES_DBG_CM, "mpa private data len =%u\n", conn_param->private_data_len);
  2203. strcpy(&nesqp->ietf_frame->key[0], IEFT_MPA_KEY_REQ);
  2204. nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
  2205. nesqp->ietf_frame->rev = IETF_MPA_VERSION;
  2206. nesqp->ietf_frame->priv_data_len = htons(conn_param->private_data_len);
  2207. if (cm_id->local_addr.sin_addr.s_addr != cm_id->remote_addr.sin_addr.s_addr)
  2208. nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
  2209. PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
  2210. /* set up the connection params for the node */
  2211. cm_info.loc_addr = (cm_id->local_addr.sin_addr.s_addr);
  2212. cm_info.loc_port = (cm_id->local_addr.sin_port);
  2213. cm_info.rem_addr = (cm_id->remote_addr.sin_addr.s_addr);
  2214. cm_info.rem_port = (cm_id->remote_addr.sin_port);
  2215. cm_info.cm_id = cm_id;
  2216. cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
  2217. cm_id->add_ref(cm_id);
  2218. nes_add_ref(&nesqp->ibqp);
  2219. /* create a connect CM node connection */
  2220. cm_node = g_cm_core->api->connect(g_cm_core, nesvnic, nesqp->ietf_frame, &cm_info);
  2221. if (!cm_node) {
  2222. if (cm_id->local_addr.sin_addr.s_addr != cm_id->remote_addr.sin_addr.s_addr)
  2223. nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
  2224. PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
  2225. nes_rem_ref(&nesqp->ibqp);
  2226. kfree(nesqp->ietf_frame);
  2227. nesqp->ietf_frame = NULL;
  2228. cm_id->rem_ref(cm_id);
  2229. return -ENOMEM;
  2230. }
  2231. cm_node->apbvt_set = 1;
  2232. nesqp->cm_node = cm_node;
  2233. return 0;
  2234. }
  2235. /**
  2236. * nes_create_listen
  2237. */
  2238. int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
  2239. {
  2240. struct nes_vnic *nesvnic;
  2241. struct nes_cm_listener *cm_node;
  2242. struct nes_cm_info cm_info;
  2243. struct nes_adapter *adapter;
  2244. int err;
  2245. nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
  2246. cm_id, ntohs(cm_id->local_addr.sin_port));
  2247. nesvnic = to_nesvnic(cm_id->device);
  2248. if (!nesvnic)
  2249. return -EINVAL;
  2250. adapter = nesvnic->nesdev->nesadapter;
  2251. nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
  2252. nesvnic, nesvnic->netdev, nesvnic->netdev->name);
  2253. nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
  2254. nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
  2255. /* setup listen params in our api call struct */
  2256. cm_info.loc_addr = nesvnic->local_ipaddr;
  2257. cm_info.loc_port = cm_id->local_addr.sin_port;
  2258. cm_info.backlog = backlog;
  2259. cm_info.cm_id = cm_id;
  2260. cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
  2261. cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
  2262. if (!cm_node) {
  2263. printk("%s[%u] Error returned from listen API call\n",
  2264. __FUNCTION__, __LINE__);
  2265. return -ENOMEM;
  2266. }
  2267. cm_id->provider_data = cm_node;
  2268. if (!cm_node->reused_node) {
  2269. err = nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
  2270. PCI_FUNC(nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
  2271. if (err) {
  2272. printk("nes_manage_apbvt call returned %d.\n", err);
  2273. g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
  2274. return err;
  2275. }
  2276. cm_listens_created++;
  2277. }
  2278. cm_id->add_ref(cm_id);
  2279. cm_id->provider_data = (void *)cm_node;
  2280. return 0;
  2281. }
  2282. /**
  2283. * nes_destroy_listen
  2284. */
  2285. int nes_destroy_listen(struct iw_cm_id *cm_id)
  2286. {
  2287. if (cm_id->provider_data)
  2288. g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
  2289. else
  2290. nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
  2291. cm_id->rem_ref(cm_id);
  2292. return 0;
  2293. }
  2294. /**
  2295. * nes_cm_recv
  2296. */
  2297. int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
  2298. {
  2299. cm_packets_received++;
  2300. if ((g_cm_core) && (g_cm_core->api)) {
  2301. g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
  2302. } else {
  2303. nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
  2304. " cm is not setup properly.\n");
  2305. }
  2306. return 0;
  2307. }
  2308. /**
  2309. * nes_cm_start
  2310. * Start and init a cm core module
  2311. */
  2312. int nes_cm_start(void)
  2313. {
  2314. nes_debug(NES_DBG_CM, "\n");
  2315. /* create the primary CM core, pass this handle to subsequent core inits */
  2316. g_cm_core = nes_cm_alloc_core();
  2317. if (g_cm_core) {
  2318. return 0;
  2319. } else {
  2320. return -ENOMEM;
  2321. }
  2322. }
  2323. /**
  2324. * nes_cm_stop
  2325. * stop and dealloc all cm core instances
  2326. */
  2327. int nes_cm_stop(void)
  2328. {
  2329. g_cm_core->api->destroy_cm_core(g_cm_core);
  2330. return 0;
  2331. }
  2332. /**
  2333. * cm_event_connected
  2334. * handle a connected event, setup QPs and HW
  2335. */
  2336. static void cm_event_connected(struct nes_cm_event *event)
  2337. {
  2338. u64 u64temp;
  2339. struct nes_qp *nesqp;
  2340. struct nes_vnic *nesvnic;
  2341. struct nes_device *nesdev;
  2342. struct nes_cm_node *cm_node;
  2343. struct nes_adapter *nesadapter;
  2344. struct ib_qp_attr attr;
  2345. struct iw_cm_id *cm_id;
  2346. struct iw_cm_event cm_event;
  2347. struct nes_hw_qp_wqe *wqe;
  2348. struct nes_v4_quad nes_quad;
  2349. u32 crc_value;
  2350. int ret;
  2351. /* get all our handles */
  2352. cm_node = event->cm_node;
  2353. cm_id = cm_node->cm_id;
  2354. nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
  2355. nesqp = (struct nes_qp *)cm_id->provider_data;
  2356. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2357. nesdev = nesvnic->nesdev;
  2358. nesadapter = nesdev->nesadapter;
  2359. if (nesqp->destroyed) {
  2360. return;
  2361. }
  2362. atomic_inc(&cm_connecteds);
  2363. nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
  2364. " local port 0x%04X. jiffies = %lu.\n",
  2365. nesqp->hwqp.qp_id,
  2366. ntohl(cm_id->remote_addr.sin_addr.s_addr),
  2367. ntohs(cm_id->remote_addr.sin_port),
  2368. ntohs(cm_id->local_addr.sin_port),
  2369. jiffies);
  2370. nes_cm_init_tsa_conn(nesqp, cm_node);
  2371. /* set the QP tsa context */
  2372. nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
  2373. nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
  2374. nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
  2375. nesqp->nesqp_context->misc2 |= cpu_to_le32(
  2376. (u32)PCI_FUNC(nesdev->pcidev->devfn) << NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
  2377. nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
  2378. nes_arp_table(nesdev, le32_to_cpu(nesqp->nesqp_context->ip0),
  2379. NULL, NES_ARP_RESOLVE) << 16);
  2380. nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
  2381. jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
  2382. nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
  2383. nesqp->nesqp_context->ird_ord_sizes |=
  2384. cpu_to_le32((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
  2385. /* Adjust tail for not having a LSMM */
  2386. nesqp->hwqp.sq_tail = 1;
  2387. #if defined(NES_SEND_FIRST_WRITE)
  2388. if (cm_node->send_write0) {
  2389. nes_debug(NES_DBG_CM, "Sending first write.\n");
  2390. wqe = &nesqp->hwqp.sq_vbase[0];
  2391. u64temp = (unsigned long)nesqp;
  2392. u64temp |= NES_SW_CONTEXT_ALIGN>>1;
  2393. set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
  2394. u64temp);
  2395. wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
  2396. wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
  2397. wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
  2398. wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
  2399. wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
  2400. wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
  2401. /* use the reserved spot on the WQ for the extra first WQE */
  2402. nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
  2403. NES_QPCONTEXT_ORDIRD_WRPDU | NES_QPCONTEXT_ORDIRD_ALSMM));
  2404. nesqp->skip_lsmm = 1;
  2405. nesqp->hwqp.sq_tail = 0;
  2406. nes_write32(nesdev->regs + NES_WQE_ALLOC,
  2407. (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
  2408. }
  2409. #endif
  2410. memset(&nes_quad, 0, sizeof(nes_quad));
  2411. nes_quad.DstIpAdrIndex = cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
  2412. nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
  2413. nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
  2414. nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
  2415. /* Produce hash key */
  2416. crc_value = get_crc_value(&nes_quad);
  2417. nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
  2418. nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
  2419. nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
  2420. nesqp->hte_index &= nesadapter->hte_index_mask;
  2421. nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
  2422. nesqp->ietf_frame = &cm_node->mpa_frame;
  2423. nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
  2424. cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
  2425. /* modify QP state to rts */
  2426. attr.qp_state = IB_QPS_RTS;
  2427. nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
  2428. /* notify OF layer we successfully created the requested connection */
  2429. cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
  2430. cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
  2431. cm_event.provider_data = cm_id->provider_data;
  2432. cm_event.local_addr.sin_family = AF_INET;
  2433. cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
  2434. cm_event.remote_addr = cm_id->remote_addr;
  2435. cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
  2436. cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
  2437. cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
  2438. ret = cm_id->event_handler(cm_id, &cm_event);
  2439. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  2440. if (ret)
  2441. printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
  2442. __FUNCTION__, __LINE__, ret);
  2443. nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = %lu\n",
  2444. nesqp->hwqp.qp_id, jiffies );
  2445. nes_rem_ref(&nesqp->ibqp);
  2446. return;
  2447. }
  2448. /**
  2449. * cm_event_connect_error
  2450. */
  2451. static void cm_event_connect_error(struct nes_cm_event *event)
  2452. {
  2453. struct nes_qp *nesqp;
  2454. struct iw_cm_id *cm_id;
  2455. struct iw_cm_event cm_event;
  2456. /* struct nes_cm_info cm_info; */
  2457. int ret;
  2458. if (!event->cm_node)
  2459. return;
  2460. cm_id = event->cm_node->cm_id;
  2461. if (!cm_id) {
  2462. return;
  2463. }
  2464. nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
  2465. nesqp = cm_id->provider_data;
  2466. if (!nesqp) {
  2467. return;
  2468. }
  2469. /* notify OF layer about this connection error event */
  2470. /* cm_id->rem_ref(cm_id); */
  2471. nesqp->cm_id = NULL;
  2472. cm_id->provider_data = NULL;
  2473. cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
  2474. cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
  2475. cm_event.provider_data = cm_id->provider_data;
  2476. cm_event.local_addr = cm_id->local_addr;
  2477. cm_event.remote_addr = cm_id->remote_addr;
  2478. cm_event.private_data = NULL;
  2479. cm_event.private_data_len = 0;
  2480. nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remove_addr=%08x\n",
  2481. cm_event.local_addr.sin_addr.s_addr, cm_event.remote_addr.sin_addr.s_addr);
  2482. ret = cm_id->event_handler(cm_id, &cm_event);
  2483. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  2484. if (ret)
  2485. printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
  2486. __FUNCTION__, __LINE__, ret);
  2487. nes_rem_ref(&nesqp->ibqp);
  2488. cm_id->rem_ref(cm_id);
  2489. return;
  2490. }
  2491. /**
  2492. * cm_event_reset
  2493. */
  2494. static void cm_event_reset(struct nes_cm_event *event)
  2495. {
  2496. struct nes_qp *nesqp;
  2497. struct iw_cm_id *cm_id;
  2498. struct iw_cm_event cm_event;
  2499. /* struct nes_cm_info cm_info; */
  2500. int ret;
  2501. if (!event->cm_node)
  2502. return;
  2503. if (!event->cm_node->cm_id)
  2504. return;
  2505. cm_id = event->cm_node->cm_id;
  2506. nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
  2507. nesqp = cm_id->provider_data;
  2508. nesqp->cm_id = NULL;
  2509. /* cm_id->provider_data = NULL; */
  2510. cm_event.event = IW_CM_EVENT_DISCONNECT;
  2511. cm_event.status = IW_CM_EVENT_STATUS_RESET;
  2512. cm_event.provider_data = cm_id->provider_data;
  2513. cm_event.local_addr = cm_id->local_addr;
  2514. cm_event.remote_addr = cm_id->remote_addr;
  2515. cm_event.private_data = NULL;
  2516. cm_event.private_data_len = 0;
  2517. ret = cm_id->event_handler(cm_id, &cm_event);
  2518. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  2519. /* notify OF layer about this connection error event */
  2520. cm_id->rem_ref(cm_id);
  2521. return;
  2522. }
  2523. /**
  2524. * cm_event_mpa_req
  2525. */
  2526. static void cm_event_mpa_req(struct nes_cm_event *event)
  2527. {
  2528. struct iw_cm_id *cm_id;
  2529. struct iw_cm_event cm_event;
  2530. int ret;
  2531. struct nes_cm_node *cm_node;
  2532. cm_node = event->cm_node;
  2533. if (!cm_node)
  2534. return;
  2535. cm_id = cm_node->cm_id;
  2536. atomic_inc(&cm_connect_reqs);
  2537. nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
  2538. cm_node, cm_id, jiffies);
  2539. cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
  2540. cm_event.status = IW_CM_EVENT_STATUS_OK;
  2541. cm_event.provider_data = (void *)cm_node;
  2542. cm_event.local_addr.sin_family = AF_INET;
  2543. cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
  2544. cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
  2545. cm_event.remote_addr.sin_family = AF_INET;
  2546. cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
  2547. cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
  2548. cm_event.private_data = cm_node->mpa_frame_buf;
  2549. cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
  2550. ret = cm_id->event_handler(cm_id, &cm_event);
  2551. if (ret)
  2552. printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
  2553. __FUNCTION__, __LINE__, ret);
  2554. return;
  2555. }
  2556. static void nes_cm_event_handler(struct work_struct *);
  2557. /**
  2558. * nes_cm_post_event
  2559. * post an event to the cm event handler
  2560. */
  2561. static int nes_cm_post_event(struct nes_cm_event *event)
  2562. {
  2563. atomic_inc(&event->cm_node->cm_core->events_posted);
  2564. add_ref_cm_node(event->cm_node);
  2565. event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
  2566. INIT_WORK(&event->event_work, nes_cm_event_handler);
  2567. nes_debug(NES_DBG_CM, "queue_work, event=%p\n", event);
  2568. queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
  2569. nes_debug(NES_DBG_CM, "Exit\n");
  2570. return 0;
  2571. }
  2572. /**
  2573. * nes_cm_event_handler
  2574. * worker function to handle cm events
  2575. * will free instance of nes_cm_event
  2576. */
  2577. static void nes_cm_event_handler(struct work_struct *work)
  2578. {
  2579. struct nes_cm_event *event = container_of(work, struct nes_cm_event, event_work);
  2580. struct nes_cm_core *cm_core;
  2581. if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core)) {
  2582. return;
  2583. }
  2584. cm_core = event->cm_node->cm_core;
  2585. nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
  2586. event, event->type, atomic_read(&cm_core->events_posted));
  2587. switch (event->type) {
  2588. case NES_CM_EVENT_MPA_REQ:
  2589. cm_event_mpa_req(event);
  2590. nes_debug(NES_DBG_CM, "CM Event: MPA REQUEST\n");
  2591. break;
  2592. case NES_CM_EVENT_RESET:
  2593. nes_debug(NES_DBG_CM, "CM Event: RESET\n");
  2594. cm_event_reset(event);
  2595. break;
  2596. case NES_CM_EVENT_CONNECTED:
  2597. if ((!event->cm_node->cm_id) ||
  2598. (event->cm_node->state != NES_CM_STATE_TSA)) {
  2599. break;
  2600. }
  2601. cm_event_connected(event);
  2602. nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
  2603. break;
  2604. case NES_CM_EVENT_ABORTED:
  2605. if ((!event->cm_node->cm_id) || (event->cm_node->state == NES_CM_STATE_TSA)) {
  2606. break;
  2607. }
  2608. cm_event_connect_error(event);
  2609. nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
  2610. break;
  2611. case NES_CM_EVENT_DROPPED_PKT:
  2612. nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
  2613. break;
  2614. default:
  2615. nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
  2616. break;
  2617. }
  2618. atomic_dec(&cm_core->events_posted);
  2619. event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
  2620. rem_ref_cm_node(cm_core, event->cm_node);
  2621. kfree(event);
  2622. return;
  2623. }