nes_cm.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992
  1. /*
  2. * Copyright (c) 2006 - 2014 Intel Corporation. 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 <linux/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 <linux/highmem.h>
  53. #include <linux/slab.h>
  54. #include <net/arp.h>
  55. #include <net/neighbour.h>
  56. #include <net/route.h>
  57. #include <net/ip_fib.h>
  58. #include <net/secure_seq.h>
  59. #include <net/tcp.h>
  60. #include <linux/fcntl.h>
  61. #include "nes.h"
  62. u32 cm_packets_sent;
  63. u32 cm_packets_bounced;
  64. u32 cm_packets_dropped;
  65. u32 cm_packets_retrans;
  66. u32 cm_packets_created;
  67. u32 cm_packets_received;
  68. atomic_t cm_listens_created;
  69. atomic_t cm_listens_destroyed;
  70. u32 cm_backlog_drops;
  71. atomic_t cm_loopbacks;
  72. atomic_t cm_nodes_created;
  73. atomic_t cm_nodes_destroyed;
  74. atomic_t cm_accel_dropped_pkts;
  75. atomic_t cm_resets_recvd;
  76. static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
  77. static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
  78. static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
  79. static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
  80. static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
  81. static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
  82. static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
  83. static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
  84. static int mini_cm_dealloc_core(struct nes_cm_core *);
  85. static int mini_cm_get(struct nes_cm_core *);
  86. static int mini_cm_set(struct nes_cm_core *, u32, u32);
  87. static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
  88. static int add_ref_cm_node(struct nes_cm_node *);
  89. static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
  90. static int nes_cm_disconn_true(struct nes_qp *);
  91. static int nes_cm_post_event(struct nes_cm_event *event);
  92. static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
  93. static void nes_disconnect_worker(struct work_struct *work);
  94. static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
  95. static int send_mpa_reject(struct nes_cm_node *);
  96. static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
  97. static int send_reset(struct nes_cm_node *, struct sk_buff *);
  98. static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
  99. static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
  100. static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
  101. static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
  102. static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
  103. static void cleanup_retrans_entry(struct nes_cm_node *);
  104. static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
  105. static void free_retrans_entry(struct nes_cm_node *cm_node);
  106. static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
  107. /* CM event handler functions */
  108. static void cm_event_connected(struct nes_cm_event *);
  109. static void cm_event_connect_error(struct nes_cm_event *);
  110. static void cm_event_reset(struct nes_cm_event *);
  111. static void cm_event_mpa_req(struct nes_cm_event *);
  112. static void cm_event_mpa_reject(struct nes_cm_event *);
  113. static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
  114. /* MPA build functions */
  115. static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
  116. static void build_mpa_v2(struct nes_cm_node *, void *, u8);
  117. static void build_mpa_v1(struct nes_cm_node *, void *, u8);
  118. static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
  119. static void print_core(struct nes_cm_core *core);
  120. static void record_ird_ord(struct nes_cm_node *, u16, u16);
  121. /* External CM API Interface */
  122. /* instance of function pointers for client API */
  123. /* set address of this instance to cm_core->cm_ops at cm_core alloc */
  124. static const struct nes_cm_ops nes_cm_api = {
  125. .accelerated = mini_cm_accelerated,
  126. .listen = mini_cm_listen,
  127. .stop_listener = mini_cm_del_listen,
  128. .connect = mini_cm_connect,
  129. .close = mini_cm_close,
  130. .accept = mini_cm_accept,
  131. .reject = mini_cm_reject,
  132. .recv_pkt = mini_cm_recv_pkt,
  133. .destroy_cm_core = mini_cm_dealloc_core,
  134. .get = mini_cm_get,
  135. .set = mini_cm_set
  136. };
  137. static struct nes_cm_core *g_cm_core;
  138. atomic_t cm_connects;
  139. atomic_t cm_accepts;
  140. atomic_t cm_disconnects;
  141. atomic_t cm_closes;
  142. atomic_t cm_connecteds;
  143. atomic_t cm_connect_reqs;
  144. atomic_t cm_rejects;
  145. int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
  146. {
  147. return add_ref_cm_node(cm_node);
  148. }
  149. int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
  150. {
  151. return rem_ref_cm_node(cm_node->cm_core, cm_node);
  152. }
  153. /**
  154. * create_event
  155. */
  156. static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
  157. enum nes_cm_event_type type)
  158. {
  159. struct nes_cm_event *event;
  160. if (!cm_node->cm_id)
  161. return NULL;
  162. /* allocate an empty event */
  163. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  164. if (!event)
  165. return NULL;
  166. event->type = type;
  167. event->cm_node = cm_node;
  168. event->cm_info.rem_addr = cm_node->rem_addr;
  169. event->cm_info.loc_addr = cm_node->loc_addr;
  170. event->cm_info.rem_port = cm_node->rem_port;
  171. event->cm_info.loc_port = cm_node->loc_port;
  172. event->cm_info.cm_id = cm_node->cm_id;
  173. nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
  174. "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
  175. cm_node, event, type, event->cm_info.loc_addr,
  176. event->cm_info.loc_port, event->cm_info.rem_addr,
  177. event->cm_info.rem_port);
  178. nes_cm_post_event(event);
  179. return event;
  180. }
  181. /**
  182. * send_mpa_request
  183. */
  184. static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
  185. {
  186. u8 start_addr = 0;
  187. u8 *start_ptr = &start_addr;
  188. u8 **start_buff = &start_ptr;
  189. u16 buff_len = 0;
  190. if (!skb) {
  191. nes_debug(NES_DBG_CM, "skb set to NULL\n");
  192. return -1;
  193. }
  194. /* send an MPA Request frame */
  195. cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
  196. form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
  197. return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  198. }
  199. static int send_mpa_reject(struct nes_cm_node *cm_node)
  200. {
  201. struct sk_buff *skb = NULL;
  202. u8 start_addr = 0;
  203. u8 *start_ptr = &start_addr;
  204. u8 **start_buff = &start_ptr;
  205. u16 buff_len = 0;
  206. struct ietf_mpa_v1 *mpa_frame;
  207. skb = dev_alloc_skb(MAX_CM_BUFFER);
  208. if (!skb) {
  209. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  210. return -ENOMEM;
  211. }
  212. /* send an MPA reject frame */
  213. cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
  214. mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
  215. mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
  216. form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
  217. cm_node->state = NES_CM_STATE_FIN_WAIT1;
  218. return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  219. }
  220. /**
  221. * recv_mpa - process a received TCP pkt, we are expecting an
  222. * IETF MPA frame
  223. */
  224. static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
  225. u32 len)
  226. {
  227. struct ietf_mpa_v1 *mpa_frame;
  228. struct ietf_mpa_v2 *mpa_v2_frame;
  229. struct ietf_rtr_msg *rtr_msg;
  230. int mpa_hdr_len;
  231. int priv_data_len;
  232. *type = NES_MPA_REQUEST_ACCEPT;
  233. /* assume req frame is in tcp data payload */
  234. if (len < sizeof(struct ietf_mpa_v1)) {
  235. nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
  236. return -EINVAL;
  237. }
  238. /* points to the beginning of the frame, which could be MPA V1 or V2 */
  239. mpa_frame = (struct ietf_mpa_v1 *)buffer;
  240. mpa_hdr_len = sizeof(struct ietf_mpa_v1);
  241. priv_data_len = ntohs(mpa_frame->priv_data_len);
  242. /* make sure mpa private data len is less than 512 bytes */
  243. if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
  244. nes_debug(NES_DBG_CM, "The received Length of Private"
  245. " Data field exceeds 512 octets\n");
  246. return -EINVAL;
  247. }
  248. /*
  249. * make sure MPA receiver interoperate with the
  250. * received MPA version and MPA key information
  251. *
  252. */
  253. if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
  254. nes_debug(NES_DBG_CM, "The received mpa version"
  255. " is not supported\n");
  256. return -EINVAL;
  257. }
  258. /*
  259. * backwards compatibility only
  260. */
  261. if (mpa_frame->rev > cm_node->mpa_frame_rev) {
  262. nes_debug(NES_DBG_CM, "The received mpa version"
  263. " can not be interoperated\n");
  264. return -EINVAL;
  265. } else {
  266. cm_node->mpa_frame_rev = mpa_frame->rev;
  267. }
  268. if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
  269. if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
  270. nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
  271. return -EINVAL;
  272. }
  273. } else {
  274. if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
  275. nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
  276. return -EINVAL;
  277. }
  278. }
  279. if (priv_data_len + mpa_hdr_len != len) {
  280. nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
  281. " complete (%x + %x != %x)\n",
  282. priv_data_len, mpa_hdr_len, len);
  283. return -EINVAL;
  284. }
  285. /* make sure it does not exceed the max size */
  286. if (len > MAX_CM_BUFFER) {
  287. nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
  288. " (%x + %x != %x)\n",
  289. priv_data_len, mpa_hdr_len, len);
  290. return -EINVAL;
  291. }
  292. cm_node->mpa_frame_size = priv_data_len;
  293. switch (mpa_frame->rev) {
  294. case IETF_MPA_V2: {
  295. u16 ird_size;
  296. u16 ord_size;
  297. u16 rtr_ctrl_ird;
  298. u16 rtr_ctrl_ord;
  299. mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
  300. mpa_hdr_len += IETF_RTR_MSG_SIZE;
  301. cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
  302. rtr_msg = &mpa_v2_frame->rtr_msg;
  303. /* parse rtr message */
  304. rtr_ctrl_ird = ntohs(rtr_msg->ctrl_ird);
  305. rtr_ctrl_ord = ntohs(rtr_msg->ctrl_ord);
  306. ird_size = rtr_ctrl_ird & IETF_NO_IRD_ORD;
  307. ord_size = rtr_ctrl_ord & IETF_NO_IRD_ORD;
  308. if (!(rtr_ctrl_ird & IETF_PEER_TO_PEER)) {
  309. /* send reset */
  310. return -EINVAL;
  311. }
  312. if (ird_size == IETF_NO_IRD_ORD || ord_size == IETF_NO_IRD_ORD)
  313. cm_node->mpav2_ird_ord = IETF_NO_IRD_ORD;
  314. if (cm_node->mpav2_ird_ord != IETF_NO_IRD_ORD) {
  315. /* responder */
  316. if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
  317. /* we are still negotiating */
  318. if (ord_size > NES_MAX_IRD) {
  319. cm_node->ird_size = NES_MAX_IRD;
  320. } else {
  321. cm_node->ird_size = ord_size;
  322. if (ord_size == 0 &&
  323. (rtr_ctrl_ord & IETF_RDMA0_READ)) {
  324. cm_node->ird_size = 1;
  325. nes_debug(NES_DBG_CM,
  326. "%s: Remote peer doesn't support RDMA0_READ (ord=%u)\n",
  327. __func__, ord_size);
  328. }
  329. }
  330. if (ird_size > NES_MAX_ORD)
  331. cm_node->ord_size = NES_MAX_ORD;
  332. else
  333. cm_node->ord_size = ird_size;
  334. } else { /* initiator */
  335. if (ord_size > NES_MAX_IRD) {
  336. nes_debug(NES_DBG_CM,
  337. "%s: Unable to support the requested (ord =%u)\n",
  338. __func__, ord_size);
  339. return -EINVAL;
  340. }
  341. cm_node->ird_size = ord_size;
  342. if (ird_size > NES_MAX_ORD) {
  343. cm_node->ord_size = NES_MAX_ORD;
  344. } else {
  345. if (ird_size == 0 &&
  346. (rtr_ctrl_ord & IETF_RDMA0_READ)) {
  347. nes_debug(NES_DBG_CM,
  348. "%s: Remote peer doesn't support RDMA0_READ (ird=%u)\n",
  349. __func__, ird_size);
  350. return -EINVAL;
  351. } else {
  352. cm_node->ord_size = ird_size;
  353. }
  354. }
  355. }
  356. }
  357. if (rtr_ctrl_ord & IETF_RDMA0_READ) {
  358. cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
  359. } else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
  360. cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
  361. } else { /* Not supported RDMA0 operation */
  362. return -EINVAL;
  363. }
  364. break;
  365. }
  366. case IETF_MPA_V1:
  367. default:
  368. break;
  369. }
  370. /* copy entire MPA frame to our cm_node's frame */
  371. memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
  372. if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
  373. *type = NES_MPA_REQUEST_REJECT;
  374. return 0;
  375. }
  376. /**
  377. * form_cm_frame - get a free packet and build empty frame Use
  378. * node info to build.
  379. */
  380. static void form_cm_frame(struct sk_buff *skb,
  381. struct nes_cm_node *cm_node, void *options, u32 optionsize,
  382. void *data, u32 datasize, u8 flags)
  383. {
  384. struct tcphdr *tcph;
  385. struct iphdr *iph;
  386. struct ethhdr *ethh;
  387. u8 *buf;
  388. u16 packetsize = sizeof(*iph);
  389. packetsize += sizeof(*tcph);
  390. packetsize += optionsize + datasize;
  391. skb_trim(skb, 0);
  392. memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
  393. buf = skb_put(skb, packetsize + ETH_HLEN);
  394. ethh = (struct ethhdr *)buf;
  395. buf += ETH_HLEN;
  396. iph = (struct iphdr *)buf;
  397. buf += sizeof(*iph);
  398. tcph = (struct tcphdr *)buf;
  399. skb_reset_mac_header(skb);
  400. skb_set_network_header(skb, ETH_HLEN);
  401. skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
  402. buf += sizeof(*tcph);
  403. skb->ip_summed = CHECKSUM_PARTIAL;
  404. if (!(cm_node->netdev->features & NETIF_F_IP_CSUM))
  405. skb->ip_summed = CHECKSUM_NONE;
  406. skb->protocol = htons(0x800);
  407. skb->data_len = 0;
  408. skb->mac_len = ETH_HLEN;
  409. memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
  410. memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
  411. ethh->h_proto = htons(0x0800);
  412. iph->version = IPVERSION;
  413. iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
  414. iph->tos = 0;
  415. iph->tot_len = htons(packetsize);
  416. iph->id = htons(++cm_node->tcp_cntxt.loc_id);
  417. iph->frag_off = htons(0x4000);
  418. iph->ttl = 0x40;
  419. iph->protocol = 0x06; /* IPPROTO_TCP */
  420. iph->saddr = htonl(cm_node->loc_addr);
  421. iph->daddr = htonl(cm_node->rem_addr);
  422. tcph->source = htons(cm_node->loc_port);
  423. tcph->dest = htons(cm_node->rem_port);
  424. tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
  425. if (flags & SET_ACK) {
  426. cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
  427. tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
  428. tcph->ack = 1;
  429. } else {
  430. tcph->ack_seq = 0;
  431. }
  432. if (flags & SET_SYN) {
  433. cm_node->tcp_cntxt.loc_seq_num++;
  434. tcph->syn = 1;
  435. } else {
  436. cm_node->tcp_cntxt.loc_seq_num += datasize;
  437. }
  438. if (flags & SET_FIN) {
  439. cm_node->tcp_cntxt.loc_seq_num++;
  440. tcph->fin = 1;
  441. }
  442. if (flags & SET_RST)
  443. tcph->rst = 1;
  444. tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
  445. tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
  446. tcph->urg_ptr = 0;
  447. if (optionsize)
  448. memcpy(buf, options, optionsize);
  449. buf += optionsize;
  450. if (datasize)
  451. memcpy(buf, data, datasize);
  452. skb_shinfo(skb)->nr_frags = 0;
  453. cm_packets_created++;
  454. }
  455. /**
  456. * print_core - dump a cm core
  457. */
  458. static void print_core(struct nes_cm_core *core)
  459. {
  460. nes_debug(NES_DBG_CM, "---------------------------------------------\n");
  461. nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
  462. if (!core)
  463. return;
  464. nes_debug(NES_DBG_CM, "---------------------------------------------\n");
  465. nes_debug(NES_DBG_CM, "State : %u \n", core->state);
  466. nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
  467. nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
  468. nes_debug(NES_DBG_CM, "core : %p \n", core);
  469. nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
  470. }
  471. static void record_ird_ord(struct nes_cm_node *cm_node,
  472. u16 conn_ird, u16 conn_ord)
  473. {
  474. if (conn_ird > NES_MAX_IRD)
  475. conn_ird = NES_MAX_IRD;
  476. if (conn_ord > NES_MAX_ORD)
  477. conn_ord = NES_MAX_ORD;
  478. cm_node->ird_size = conn_ird;
  479. cm_node->ord_size = conn_ord;
  480. }
  481. /**
  482. * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
  483. */
  484. static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
  485. u16 *buff_len, u8 *pci_mem, u8 mpa_key)
  486. {
  487. int ret = 0;
  488. *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
  489. switch (cm_node->mpa_frame_rev) {
  490. case IETF_MPA_V1:
  491. *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
  492. *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
  493. build_mpa_v1(cm_node, *start_buff, mpa_key);
  494. break;
  495. case IETF_MPA_V2:
  496. *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
  497. build_mpa_v2(cm_node, *start_buff, mpa_key);
  498. break;
  499. default:
  500. ret = -EINVAL;
  501. }
  502. return ret;
  503. }
  504. /**
  505. * build_mpa_v2 - build a MPA V2 frame
  506. */
  507. static void build_mpa_v2(struct nes_cm_node *cm_node,
  508. void *start_addr, u8 mpa_key)
  509. {
  510. struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
  511. struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
  512. u16 ctrl_ird;
  513. u16 ctrl_ord;
  514. /* initialize the upper 5 bytes of the frame */
  515. build_mpa_v1(cm_node, start_addr, mpa_key);
  516. mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
  517. mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
  518. /* initialize RTR msg */
  519. if (cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
  520. ctrl_ird = IETF_NO_IRD_ORD;
  521. ctrl_ord = IETF_NO_IRD_ORD;
  522. } else {
  523. ctrl_ird = cm_node->ird_size & IETF_NO_IRD_ORD;
  524. ctrl_ord = cm_node->ord_size & IETF_NO_IRD_ORD;
  525. }
  526. ctrl_ird |= IETF_PEER_TO_PEER;
  527. switch (mpa_key) {
  528. case MPA_KEY_REQUEST:
  529. ctrl_ord |= IETF_RDMA0_WRITE;
  530. ctrl_ord |= IETF_RDMA0_READ;
  531. break;
  532. case MPA_KEY_REPLY:
  533. switch (cm_node->send_rdma0_op) {
  534. case SEND_RDMA_WRITE_ZERO:
  535. ctrl_ord |= IETF_RDMA0_WRITE;
  536. break;
  537. case SEND_RDMA_READ_ZERO:
  538. ctrl_ord |= IETF_RDMA0_READ;
  539. break;
  540. }
  541. }
  542. rtr_msg->ctrl_ird = htons(ctrl_ird);
  543. rtr_msg->ctrl_ord = htons(ctrl_ord);
  544. }
  545. /**
  546. * build_mpa_v1 - build a MPA V1 frame
  547. */
  548. static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
  549. {
  550. struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
  551. switch (mpa_key) {
  552. case MPA_KEY_REQUEST:
  553. memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
  554. break;
  555. case MPA_KEY_REPLY:
  556. memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
  557. break;
  558. }
  559. mpa_frame->flags = IETF_MPA_FLAGS_CRC;
  560. mpa_frame->rev = cm_node->mpa_frame_rev;
  561. mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
  562. }
  563. static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
  564. {
  565. u64 u64temp;
  566. struct nes_qp *nesqp = *nesqp_addr;
  567. struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
  568. u64temp = (unsigned long)nesqp->nesuqp_addr;
  569. u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
  570. set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
  571. wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
  572. wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
  573. switch (cm_node->send_rdma0_op) {
  574. case SEND_RDMA_WRITE_ZERO:
  575. nes_debug(NES_DBG_CM, "Sending first write.\n");
  576. wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
  577. cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
  578. wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
  579. wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
  580. wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
  581. break;
  582. case SEND_RDMA_READ_ZERO:
  583. default:
  584. if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
  585. WARN(1, "Unsupported RDMA0 len operation=%u\n",
  586. cm_node->send_rdma0_op);
  587. nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
  588. wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
  589. cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
  590. wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
  591. wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
  592. wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
  593. wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
  594. wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
  595. break;
  596. }
  597. if (nesqp->sq_kmapped) {
  598. nesqp->sq_kmapped = 0;
  599. kunmap(nesqp->page);
  600. }
  601. /*use the reserved spot on the WQ for the extra first WQE*/
  602. nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
  603. NES_QPCONTEXT_ORDIRD_WRPDU |
  604. NES_QPCONTEXT_ORDIRD_ALSMM));
  605. nesqp->skip_lsmm = 1;
  606. nesqp->hwqp.sq_tail = 0;
  607. }
  608. /**
  609. * schedule_nes_timer
  610. * note - cm_node needs to be protected before calling this. Encase in:
  611. * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
  612. */
  613. int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
  614. enum nes_timer_type type, int send_retrans,
  615. int close_when_complete)
  616. {
  617. unsigned long flags;
  618. struct nes_cm_core *cm_core = cm_node->cm_core;
  619. struct nes_timer_entry *new_send;
  620. int ret = 0;
  621. new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
  622. if (!new_send)
  623. return -ENOMEM;
  624. /* new_send->timetosend = currenttime */
  625. new_send->retrycount = NES_DEFAULT_RETRYS;
  626. new_send->retranscount = NES_DEFAULT_RETRANS;
  627. new_send->skb = skb;
  628. new_send->timetosend = jiffies;
  629. new_send->type = type;
  630. new_send->netdev = cm_node->netdev;
  631. new_send->send_retrans = send_retrans;
  632. new_send->close_when_complete = close_when_complete;
  633. if (type == NES_TIMER_TYPE_CLOSE) {
  634. new_send->timetosend += (HZ / 10);
  635. if (cm_node->recv_entry) {
  636. kfree(new_send);
  637. WARN_ON(1);
  638. return -EINVAL;
  639. }
  640. cm_node->recv_entry = new_send;
  641. }
  642. if (type == NES_TIMER_TYPE_SEND) {
  643. new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
  644. refcount_inc(&new_send->skb->users);
  645. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  646. cm_node->send_entry = new_send;
  647. add_ref_cm_node(cm_node);
  648. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  649. new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
  650. ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
  651. if (ret != NETDEV_TX_OK) {
  652. nes_debug(NES_DBG_CM, "Error sending packet %p "
  653. "(jiffies = %lu)\n", new_send, jiffies);
  654. new_send->timetosend = jiffies;
  655. ret = NETDEV_TX_OK;
  656. } else {
  657. cm_packets_sent++;
  658. if (!send_retrans) {
  659. cleanup_retrans_entry(cm_node);
  660. if (close_when_complete)
  661. rem_ref_cm_node(cm_core, cm_node);
  662. return ret;
  663. }
  664. }
  665. }
  666. if (!timer_pending(&cm_core->tcp_timer))
  667. mod_timer(&cm_core->tcp_timer, new_send->timetosend);
  668. return ret;
  669. }
  670. static void nes_retrans_expired(struct nes_cm_node *cm_node)
  671. {
  672. struct iw_cm_id *cm_id = cm_node->cm_id;
  673. enum nes_cm_node_state state = cm_node->state;
  674. cm_node->state = NES_CM_STATE_CLOSED;
  675. switch (state) {
  676. case NES_CM_STATE_SYN_RCVD:
  677. case NES_CM_STATE_CLOSING:
  678. rem_ref_cm_node(cm_node->cm_core, cm_node);
  679. break;
  680. case NES_CM_STATE_LAST_ACK:
  681. case NES_CM_STATE_FIN_WAIT1:
  682. if (cm_node->cm_id)
  683. cm_id->rem_ref(cm_id);
  684. send_reset(cm_node, NULL);
  685. break;
  686. default:
  687. add_ref_cm_node(cm_node);
  688. send_reset(cm_node, NULL);
  689. create_event(cm_node, NES_CM_EVENT_ABORTED);
  690. }
  691. }
  692. static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
  693. {
  694. struct nes_timer_entry *recv_entry = cm_node->recv_entry;
  695. struct iw_cm_id *cm_id = cm_node->cm_id;
  696. struct nes_qp *nesqp;
  697. unsigned long qplockflags;
  698. if (!recv_entry)
  699. return;
  700. nesqp = (struct nes_qp *)recv_entry->skb;
  701. if (nesqp) {
  702. spin_lock_irqsave(&nesqp->lock, qplockflags);
  703. if (nesqp->cm_id) {
  704. nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
  705. "refcount = %d: HIT A "
  706. "NES_TIMER_TYPE_CLOSE with something "
  707. "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
  708. atomic_read(&nesqp->refcount));
  709. nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
  710. nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
  711. nesqp->ibqp_state = IB_QPS_ERR;
  712. spin_unlock_irqrestore(&nesqp->lock, qplockflags);
  713. nes_cm_disconn(nesqp);
  714. } else {
  715. spin_unlock_irqrestore(&nesqp->lock, qplockflags);
  716. nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
  717. "refcount = %d: HIT A "
  718. "NES_TIMER_TYPE_CLOSE with nothing "
  719. "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
  720. atomic_read(&nesqp->refcount));
  721. }
  722. } else if (rem_node) {
  723. /* TIME_WAIT state */
  724. rem_ref_cm_node(cm_node->cm_core, cm_node);
  725. }
  726. if (cm_node->cm_id)
  727. cm_id->rem_ref(cm_id);
  728. kfree(recv_entry);
  729. cm_node->recv_entry = NULL;
  730. }
  731. /**
  732. * nes_cm_timer_tick
  733. */
  734. static void nes_cm_timer_tick(struct timer_list *unused)
  735. {
  736. unsigned long flags;
  737. unsigned long nexttimeout = jiffies + NES_LONG_TIME;
  738. struct nes_cm_node *cm_node;
  739. struct nes_timer_entry *send_entry, *recv_entry;
  740. struct list_head *list_core_temp;
  741. struct list_head *list_node;
  742. struct nes_cm_core *cm_core = g_cm_core;
  743. u32 settimer = 0;
  744. unsigned long timetosend;
  745. int ret = NETDEV_TX_OK;
  746. struct list_head timer_list;
  747. INIT_LIST_HEAD(&timer_list);
  748. spin_lock_irqsave(&cm_core->ht_lock, flags);
  749. list_for_each_safe(list_node, list_core_temp,
  750. &cm_core->connected_nodes) {
  751. cm_node = container_of(list_node, struct nes_cm_node, list);
  752. if ((cm_node->recv_entry) || (cm_node->send_entry)) {
  753. add_ref_cm_node(cm_node);
  754. list_add(&cm_node->timer_entry, &timer_list);
  755. }
  756. }
  757. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  758. list_for_each_safe(list_node, list_core_temp, &timer_list) {
  759. cm_node = container_of(list_node, struct nes_cm_node,
  760. timer_entry);
  761. recv_entry = cm_node->recv_entry;
  762. if (recv_entry) {
  763. if (time_after(recv_entry->timetosend, jiffies)) {
  764. if (nexttimeout > recv_entry->timetosend ||
  765. !settimer) {
  766. nexttimeout = recv_entry->timetosend;
  767. settimer = 1;
  768. }
  769. } else {
  770. handle_recv_entry(cm_node, 1);
  771. }
  772. }
  773. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  774. do {
  775. send_entry = cm_node->send_entry;
  776. if (!send_entry)
  777. break;
  778. if (time_after(send_entry->timetosend, jiffies)) {
  779. if (cm_node->state != NES_CM_STATE_TSA) {
  780. if ((nexttimeout >
  781. send_entry->timetosend) ||
  782. !settimer) {
  783. nexttimeout =
  784. send_entry->timetosend;
  785. settimer = 1;
  786. }
  787. } else {
  788. free_retrans_entry(cm_node);
  789. }
  790. break;
  791. }
  792. if ((cm_node->state == NES_CM_STATE_TSA) ||
  793. (cm_node->state == NES_CM_STATE_CLOSED)) {
  794. free_retrans_entry(cm_node);
  795. break;
  796. }
  797. if (!send_entry->retranscount ||
  798. !send_entry->retrycount) {
  799. cm_packets_dropped++;
  800. free_retrans_entry(cm_node);
  801. spin_unlock_irqrestore(
  802. &cm_node->retrans_list_lock, flags);
  803. nes_retrans_expired(cm_node);
  804. cm_node->state = NES_CM_STATE_CLOSED;
  805. spin_lock_irqsave(&cm_node->retrans_list_lock,
  806. flags);
  807. break;
  808. }
  809. refcount_inc(&send_entry->skb->users);
  810. cm_packets_retrans++;
  811. nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
  812. "for node %p, jiffies = %lu, time to send = "
  813. "%lu, retranscount = %u, send_entry->seq_num = "
  814. "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
  815. "0x%08X\n", send_entry, cm_node, jiffies,
  816. send_entry->timetosend,
  817. send_entry->retranscount,
  818. send_entry->seq_num,
  819. cm_node->tcp_cntxt.rem_ack_num);
  820. spin_unlock_irqrestore(&cm_node->retrans_list_lock,
  821. flags);
  822. ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
  823. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  824. if (ret != NETDEV_TX_OK) {
  825. nes_debug(NES_DBG_CM, "rexmit failed for "
  826. "node=%p\n", cm_node);
  827. cm_packets_bounced++;
  828. send_entry->retrycount--;
  829. nexttimeout = jiffies + NES_SHORT_TIME;
  830. settimer = 1;
  831. break;
  832. } else {
  833. cm_packets_sent++;
  834. }
  835. nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
  836. "%u, retry count = %u.\n",
  837. send_entry->retranscount,
  838. send_entry->retrycount);
  839. if (send_entry->send_retrans) {
  840. send_entry->retranscount--;
  841. timetosend = (NES_RETRY_TIMEOUT <<
  842. (NES_DEFAULT_RETRANS - send_entry->retranscount));
  843. send_entry->timetosend = jiffies +
  844. min(timetosend, NES_MAX_TIMEOUT);
  845. if (nexttimeout > send_entry->timetosend ||
  846. !settimer) {
  847. nexttimeout = send_entry->timetosend;
  848. settimer = 1;
  849. }
  850. } else {
  851. int close_when_complete;
  852. close_when_complete =
  853. send_entry->close_when_complete;
  854. nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
  855. cm_node, cm_node->state);
  856. free_retrans_entry(cm_node);
  857. if (close_when_complete)
  858. rem_ref_cm_node(cm_node->cm_core,
  859. cm_node);
  860. }
  861. } while (0);
  862. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  863. rem_ref_cm_node(cm_node->cm_core, cm_node);
  864. }
  865. if (settimer) {
  866. if (!timer_pending(&cm_core->tcp_timer))
  867. mod_timer(&cm_core->tcp_timer, nexttimeout);
  868. }
  869. }
  870. /**
  871. * send_syn
  872. */
  873. static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
  874. struct sk_buff *skb)
  875. {
  876. int ret;
  877. int flags = SET_SYN;
  878. char optionsbuffer[sizeof(struct option_mss) +
  879. sizeof(struct option_windowscale) + sizeof(struct option_base) +
  880. TCP_OPTIONS_PADDING];
  881. int optionssize = 0;
  882. /* Sending MSS option */
  883. union all_known_options *options;
  884. if (!cm_node)
  885. return -EINVAL;
  886. options = (union all_known_options *)&optionsbuffer[optionssize];
  887. options->as_mss.optionnum = OPTION_NUMBER_MSS;
  888. options->as_mss.length = sizeof(struct option_mss);
  889. options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
  890. optionssize += sizeof(struct option_mss);
  891. options = (union all_known_options *)&optionsbuffer[optionssize];
  892. options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
  893. options->as_windowscale.length = sizeof(struct option_windowscale);
  894. options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
  895. optionssize += sizeof(struct option_windowscale);
  896. if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
  897. options = (union all_known_options *)&optionsbuffer[optionssize];
  898. options->as_base.optionnum = OPTION_NUMBER_WRITE0;
  899. options->as_base.length = sizeof(struct option_base);
  900. optionssize += sizeof(struct option_base);
  901. /* we need the size to be a multiple of 4 */
  902. options = (union all_known_options *)&optionsbuffer[optionssize];
  903. options->as_end = 1;
  904. optionssize += 1;
  905. options = (union all_known_options *)&optionsbuffer[optionssize];
  906. options->as_end = 1;
  907. optionssize += 1;
  908. }
  909. options = (union all_known_options *)&optionsbuffer[optionssize];
  910. options->as_end = OPTION_NUMBER_END;
  911. optionssize += 1;
  912. if (!skb)
  913. skb = dev_alloc_skb(MAX_CM_BUFFER);
  914. if (!skb) {
  915. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  916. return -1;
  917. }
  918. if (sendack)
  919. flags |= SET_ACK;
  920. form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
  921. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  922. return ret;
  923. }
  924. /**
  925. * send_reset
  926. */
  927. static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
  928. {
  929. int ret;
  930. int flags = SET_RST | SET_ACK;
  931. if (!skb)
  932. skb = dev_alloc_skb(MAX_CM_BUFFER);
  933. if (!skb) {
  934. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  935. return -ENOMEM;
  936. }
  937. form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
  938. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
  939. return ret;
  940. }
  941. /**
  942. * send_ack
  943. */
  944. static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
  945. {
  946. int ret;
  947. if (!skb)
  948. skb = dev_alloc_skb(MAX_CM_BUFFER);
  949. if (!skb) {
  950. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  951. return -1;
  952. }
  953. form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
  954. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
  955. return ret;
  956. }
  957. /**
  958. * send_fin
  959. */
  960. static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
  961. {
  962. int ret;
  963. /* if we didn't get a frame get one */
  964. if (!skb)
  965. skb = dev_alloc_skb(MAX_CM_BUFFER);
  966. if (!skb) {
  967. nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
  968. return -1;
  969. }
  970. form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
  971. ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
  972. return ret;
  973. }
  974. /**
  975. * find_node - find a cm node that matches the reference cm node
  976. */
  977. static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
  978. u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
  979. {
  980. unsigned long flags;
  981. struct list_head *hte;
  982. struct nes_cm_node *cm_node;
  983. /* get a handle on the hte */
  984. hte = &cm_core->connected_nodes;
  985. /* walk list and find cm_node associated with this session ID */
  986. spin_lock_irqsave(&cm_core->ht_lock, flags);
  987. list_for_each_entry(cm_node, hte, list) {
  988. /* compare quad, return node handle if a match */
  989. nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
  990. cm_node->loc_addr, cm_node->loc_port,
  991. loc_addr, loc_port,
  992. cm_node->rem_addr, cm_node->rem_port,
  993. rem_addr, rem_port);
  994. if ((cm_node->loc_addr == loc_addr) &&
  995. (cm_node->loc_port == loc_port) &&
  996. (cm_node->rem_addr == rem_addr) &&
  997. (cm_node->rem_port == rem_port)) {
  998. add_ref_cm_node(cm_node);
  999. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  1000. return cm_node;
  1001. }
  1002. }
  1003. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  1004. /* no owner node */
  1005. return NULL;
  1006. }
  1007. /**
  1008. * find_listener - find a cm node listening on this addr-port pair
  1009. */
  1010. static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
  1011. nes_addr_t dst_addr, u16 dst_port,
  1012. enum nes_cm_listener_state listener_state)
  1013. {
  1014. unsigned long flags;
  1015. struct nes_cm_listener *listen_node;
  1016. nes_addr_t listen_addr;
  1017. u16 listen_port;
  1018. /* walk list and find cm_node associated with this session ID */
  1019. spin_lock_irqsave(&cm_core->listen_list_lock, flags);
  1020. list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
  1021. listen_addr = listen_node->loc_addr;
  1022. listen_port = listen_node->loc_port;
  1023. /* compare node pair, return node handle if a match */
  1024. if (((listen_addr == dst_addr) ||
  1025. listen_addr == 0x00000000) &&
  1026. (listen_port == dst_port) &&
  1027. (listener_state & listen_node->listener_state)) {
  1028. atomic_inc(&listen_node->ref_count);
  1029. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  1030. return listen_node;
  1031. }
  1032. }
  1033. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  1034. /* no listener */
  1035. return NULL;
  1036. }
  1037. /**
  1038. * add_hte_node - add a cm node to the hash table
  1039. */
  1040. static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
  1041. {
  1042. unsigned long flags;
  1043. struct list_head *hte;
  1044. if (!cm_node || !cm_core)
  1045. return -EINVAL;
  1046. nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
  1047. cm_node);
  1048. spin_lock_irqsave(&cm_core->ht_lock, flags);
  1049. /* get a handle on the hash table element (list head for this slot) */
  1050. hte = &cm_core->connected_nodes;
  1051. list_add_tail(&cm_node->list, hte);
  1052. atomic_inc(&cm_core->ht_node_cnt);
  1053. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  1054. return 0;
  1055. }
  1056. /**
  1057. * mini_cm_dec_refcnt_listen
  1058. */
  1059. static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
  1060. struct nes_cm_listener *listener, int free_hanging_nodes)
  1061. {
  1062. int ret = -EINVAL;
  1063. int err = 0;
  1064. unsigned long flags;
  1065. struct list_head *list_pos = NULL;
  1066. struct list_head *list_temp = NULL;
  1067. struct nes_cm_node *cm_node = NULL;
  1068. struct list_head reset_list;
  1069. nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
  1070. "refcnt=%d\n", listener, free_hanging_nodes,
  1071. atomic_read(&listener->ref_count));
  1072. /* free non-accelerated child nodes for this listener */
  1073. INIT_LIST_HEAD(&reset_list);
  1074. if (free_hanging_nodes) {
  1075. spin_lock_irqsave(&cm_core->ht_lock, flags);
  1076. list_for_each_safe(list_pos, list_temp,
  1077. &g_cm_core->connected_nodes) {
  1078. cm_node = container_of(list_pos, struct nes_cm_node,
  1079. list);
  1080. if ((cm_node->listener == listener) &&
  1081. (!cm_node->accelerated)) {
  1082. add_ref_cm_node(cm_node);
  1083. list_add(&cm_node->reset_entry, &reset_list);
  1084. }
  1085. }
  1086. spin_unlock_irqrestore(&cm_core->ht_lock, flags);
  1087. }
  1088. list_for_each_safe(list_pos, list_temp, &reset_list) {
  1089. cm_node = container_of(list_pos, struct nes_cm_node,
  1090. reset_entry);
  1091. {
  1092. struct nes_cm_node *loopback = cm_node->loopbackpartner;
  1093. enum nes_cm_node_state old_state;
  1094. if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
  1095. rem_ref_cm_node(cm_node->cm_core, cm_node);
  1096. } else {
  1097. if (!loopback) {
  1098. cleanup_retrans_entry(cm_node);
  1099. err = send_reset(cm_node, NULL);
  1100. if (err) {
  1101. cm_node->state =
  1102. NES_CM_STATE_CLOSED;
  1103. WARN_ON(1);
  1104. } else {
  1105. old_state = cm_node->state;
  1106. cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
  1107. if (old_state != NES_CM_STATE_MPAREQ_RCVD)
  1108. rem_ref_cm_node(
  1109. cm_node->cm_core,
  1110. cm_node);
  1111. }
  1112. } else {
  1113. struct nes_cm_event event;
  1114. event.cm_node = loopback;
  1115. event.cm_info.rem_addr =
  1116. loopback->rem_addr;
  1117. event.cm_info.loc_addr =
  1118. loopback->loc_addr;
  1119. event.cm_info.rem_port =
  1120. loopback->rem_port;
  1121. event.cm_info.loc_port =
  1122. loopback->loc_port;
  1123. event.cm_info.cm_id = loopback->cm_id;
  1124. add_ref_cm_node(loopback);
  1125. loopback->state = NES_CM_STATE_CLOSED;
  1126. cm_event_connect_error(&event);
  1127. cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
  1128. rem_ref_cm_node(cm_node->cm_core,
  1129. cm_node);
  1130. }
  1131. }
  1132. }
  1133. }
  1134. spin_lock_irqsave(&cm_core->listen_list_lock, flags);
  1135. if (!atomic_dec_return(&listener->ref_count)) {
  1136. list_del(&listener->list);
  1137. /* decrement our listen node count */
  1138. atomic_dec(&cm_core->listen_node_cnt);
  1139. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  1140. if (listener->nesvnic) {
  1141. nes_manage_apbvt(listener->nesvnic,
  1142. listener->loc_port,
  1143. PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn),
  1144. NES_MANAGE_APBVT_DEL);
  1145. nes_debug(NES_DBG_NLMSG,
  1146. "Delete APBVT loc_port = %04X\n",
  1147. listener->loc_port);
  1148. }
  1149. nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
  1150. kfree(listener);
  1151. listener = NULL;
  1152. ret = 0;
  1153. atomic_inc(&cm_listens_destroyed);
  1154. } else {
  1155. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  1156. }
  1157. if (listener) {
  1158. if (atomic_read(&listener->pend_accepts_cnt) > 0)
  1159. nes_debug(NES_DBG_CM, "destroying listener (%p)"
  1160. " with non-zero pending accepts=%u\n",
  1161. listener, atomic_read(&listener->pend_accepts_cnt));
  1162. }
  1163. return ret;
  1164. }
  1165. /**
  1166. * mini_cm_del_listen
  1167. */
  1168. static int mini_cm_del_listen(struct nes_cm_core *cm_core,
  1169. struct nes_cm_listener *listener)
  1170. {
  1171. listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
  1172. listener->cm_id = NULL; /* going to be destroyed pretty soon */
  1173. return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
  1174. }
  1175. /**
  1176. * mini_cm_accelerated
  1177. */
  1178. static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
  1179. struct nes_cm_node *cm_node)
  1180. {
  1181. cm_node->accelerated = true;
  1182. if (cm_node->accept_pend) {
  1183. BUG_ON(!cm_node->listener);
  1184. atomic_dec(&cm_node->listener->pend_accepts_cnt);
  1185. cm_node->accept_pend = 0;
  1186. BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
  1187. }
  1188. if (!timer_pending(&cm_core->tcp_timer))
  1189. mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
  1190. return 0;
  1191. }
  1192. /**
  1193. * nes_addr_resolve_neigh
  1194. */
  1195. static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
  1196. {
  1197. struct rtable *rt;
  1198. struct neighbour *neigh;
  1199. int rc = arpindex;
  1200. struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
  1201. __be32 dst_ipaddr = htonl(dst_ip);
  1202. rt = ip_route_output(&init_net, dst_ipaddr, nesvnic->local_ipaddr, 0, 0);
  1203. if (IS_ERR(rt)) {
  1204. printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
  1205. __func__, dst_ip);
  1206. return rc;
  1207. }
  1208. neigh = dst_neigh_lookup(&rt->dst, &dst_ipaddr);
  1209. rcu_read_lock();
  1210. if (neigh) {
  1211. if (neigh->nud_state & NUD_VALID) {
  1212. nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
  1213. " is %pM, Gateway is 0x%08X \n", dst_ip,
  1214. neigh->ha, ntohl(rt->rt_gateway));
  1215. if (arpindex >= 0) {
  1216. if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
  1217. /* Mac address same as in nes_arp_table */
  1218. goto out;
  1219. }
  1220. nes_manage_arp_cache(nesvnic->netdev,
  1221. nesadapter->arp_table[arpindex].mac_addr,
  1222. dst_ip, NES_ARP_DELETE);
  1223. }
  1224. nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
  1225. dst_ip, NES_ARP_ADD);
  1226. rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
  1227. NES_ARP_RESOLVE);
  1228. } else {
  1229. neigh_event_send(neigh, NULL);
  1230. }
  1231. }
  1232. out:
  1233. rcu_read_unlock();
  1234. if (neigh)
  1235. neigh_release(neigh);
  1236. ip_rt_put(rt);
  1237. return rc;
  1238. }
  1239. /**
  1240. * make_cm_node - create a new instance of a cm node
  1241. */
  1242. static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
  1243. struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
  1244. struct nes_cm_listener *listener)
  1245. {
  1246. struct nes_cm_node *cm_node;
  1247. int oldarpindex = 0;
  1248. int arpindex = 0;
  1249. struct nes_device *nesdev;
  1250. struct nes_adapter *nesadapter;
  1251. /* create an hte and cm_node for this instance */
  1252. cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
  1253. if (!cm_node)
  1254. return NULL;
  1255. /* set our node specific transport info */
  1256. if (listener) {
  1257. cm_node->loc_addr = listener->loc_addr;
  1258. cm_node->loc_port = listener->loc_port;
  1259. } else {
  1260. cm_node->loc_addr = cm_info->loc_addr;
  1261. cm_node->loc_port = cm_info->loc_port;
  1262. }
  1263. cm_node->rem_addr = cm_info->rem_addr;
  1264. cm_node->rem_port = cm_info->rem_port;
  1265. cm_node->mpa_frame_rev = mpa_version;
  1266. cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
  1267. cm_node->mpav2_ird_ord = 0;
  1268. cm_node->ird_size = 0;
  1269. cm_node->ord_size = 0;
  1270. nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
  1271. &cm_node->loc_addr, cm_node->loc_port,
  1272. &cm_node->rem_addr, cm_node->rem_port);
  1273. cm_node->listener = listener;
  1274. if (listener)
  1275. cm_node->tos = listener->tos;
  1276. cm_node->netdev = nesvnic->netdev;
  1277. cm_node->cm_id = cm_info->cm_id;
  1278. memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
  1279. nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
  1280. cm_node->cm_id);
  1281. spin_lock_init(&cm_node->retrans_list_lock);
  1282. cm_node->loopbackpartner = NULL;
  1283. atomic_set(&cm_node->ref_count, 1);
  1284. /* associate our parent CM core */
  1285. cm_node->cm_core = cm_core;
  1286. cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
  1287. cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
  1288. cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
  1289. NES_CM_DEFAULT_RCV_WND_SCALE;
  1290. cm_node->tcp_cntxt.loc_seq_num = secure_tcp_seq(htonl(cm_node->loc_addr),
  1291. htonl(cm_node->rem_addr),
  1292. htons(cm_node->loc_port),
  1293. htons(cm_node->rem_port));
  1294. cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
  1295. sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
  1296. cm_node->tcp_cntxt.rcv_nxt = 0;
  1297. /* get a unique session ID , add thread_id to an upcounter to handle race */
  1298. atomic_inc(&cm_core->node_cnt);
  1299. cm_node->conn_type = cm_info->conn_type;
  1300. cm_node->apbvt_set = 0;
  1301. cm_node->accept_pend = 0;
  1302. cm_node->nesvnic = nesvnic;
  1303. /* get some device handles, for arp lookup */
  1304. nesdev = nesvnic->nesdev;
  1305. nesadapter = nesdev->nesadapter;
  1306. cm_node->loopbackpartner = NULL;
  1307. /* get the mac addr for the remote node */
  1308. oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr,
  1309. NULL, NES_ARP_RESOLVE);
  1310. arpindex = nes_addr_resolve_neigh(nesvnic, cm_node->rem_addr,
  1311. oldarpindex);
  1312. if (arpindex < 0) {
  1313. kfree(cm_node);
  1314. return NULL;
  1315. }
  1316. /* copy the mac addr to node context */
  1317. memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
  1318. nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
  1319. cm_node->rem_mac);
  1320. add_hte_node(cm_core, cm_node);
  1321. atomic_inc(&cm_nodes_created);
  1322. return cm_node;
  1323. }
  1324. /**
  1325. * add_ref_cm_node - destroy an instance of a cm node
  1326. */
  1327. static int add_ref_cm_node(struct nes_cm_node *cm_node)
  1328. {
  1329. atomic_inc(&cm_node->ref_count);
  1330. return 0;
  1331. }
  1332. /**
  1333. * rem_ref_cm_node - destroy an instance of a cm node
  1334. */
  1335. static int rem_ref_cm_node(struct nes_cm_core *cm_core,
  1336. struct nes_cm_node *cm_node)
  1337. {
  1338. unsigned long flags;
  1339. struct nes_qp *nesqp;
  1340. if (!cm_node)
  1341. return -EINVAL;
  1342. spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
  1343. if (atomic_dec_return(&cm_node->ref_count)) {
  1344. spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
  1345. return 0;
  1346. }
  1347. list_del(&cm_node->list);
  1348. atomic_dec(&cm_core->ht_node_cnt);
  1349. spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
  1350. /* if the node is destroyed before connection was accelerated */
  1351. if (!cm_node->accelerated && cm_node->accept_pend) {
  1352. BUG_ON(!cm_node->listener);
  1353. atomic_dec(&cm_node->listener->pend_accepts_cnt);
  1354. BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
  1355. }
  1356. WARN_ON(cm_node->send_entry);
  1357. if (cm_node->recv_entry)
  1358. handle_recv_entry(cm_node, 0);
  1359. if (cm_node->listener) {
  1360. mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
  1361. } else {
  1362. if (cm_node->apbvt_set && cm_node->nesvnic) {
  1363. nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
  1364. PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
  1365. NES_MANAGE_APBVT_DEL);
  1366. }
  1367. nes_debug(NES_DBG_NLMSG, "Delete APBVT loc_port = %04X\n",
  1368. cm_node->loc_port);
  1369. }
  1370. atomic_dec(&cm_core->node_cnt);
  1371. atomic_inc(&cm_nodes_destroyed);
  1372. nesqp = cm_node->nesqp;
  1373. if (nesqp) {
  1374. nesqp->cm_node = NULL;
  1375. nes_rem_ref(&nesqp->ibqp);
  1376. cm_node->nesqp = NULL;
  1377. }
  1378. kfree(cm_node);
  1379. return 0;
  1380. }
  1381. /**
  1382. * process_options
  1383. */
  1384. static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
  1385. u32 optionsize, u32 syn_packet)
  1386. {
  1387. u32 tmp;
  1388. u32 offset = 0;
  1389. union all_known_options *all_options;
  1390. char got_mss_option = 0;
  1391. while (offset < optionsize) {
  1392. all_options = (union all_known_options *)(optionsloc + offset);
  1393. switch (all_options->as_base.optionnum) {
  1394. case OPTION_NUMBER_END:
  1395. offset = optionsize;
  1396. break;
  1397. case OPTION_NUMBER_NONE:
  1398. offset += 1;
  1399. continue;
  1400. case OPTION_NUMBER_MSS:
  1401. nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
  1402. "Size: %d\n", __func__,
  1403. all_options->as_mss.length, offset, optionsize);
  1404. got_mss_option = 1;
  1405. if (all_options->as_mss.length != 4) {
  1406. return 1;
  1407. } else {
  1408. tmp = ntohs(all_options->as_mss.mss);
  1409. if (tmp > 0 && tmp <
  1410. cm_node->tcp_cntxt.mss)
  1411. cm_node->tcp_cntxt.mss = tmp;
  1412. }
  1413. break;
  1414. case OPTION_NUMBER_WINDOW_SCALE:
  1415. cm_node->tcp_cntxt.snd_wscale =
  1416. all_options->as_windowscale.shiftcount;
  1417. break;
  1418. default:
  1419. nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
  1420. all_options->as_base.optionnum);
  1421. break;
  1422. }
  1423. offset += all_options->as_base.length;
  1424. }
  1425. if ((!got_mss_option) && (syn_packet))
  1426. cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
  1427. return 0;
  1428. }
  1429. static void drop_packet(struct sk_buff *skb)
  1430. {
  1431. atomic_inc(&cm_accel_dropped_pkts);
  1432. dev_kfree_skb_any(skb);
  1433. }
  1434. static void handle_fin_pkt(struct nes_cm_node *cm_node)
  1435. {
  1436. nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
  1437. "refcnt=%d\n", cm_node, cm_node->state,
  1438. atomic_read(&cm_node->ref_count));
  1439. switch (cm_node->state) {
  1440. case NES_CM_STATE_SYN_RCVD:
  1441. case NES_CM_STATE_SYN_SENT:
  1442. case NES_CM_STATE_ESTABLISHED:
  1443. case NES_CM_STATE_MPAREJ_RCVD:
  1444. cm_node->tcp_cntxt.rcv_nxt++;
  1445. cleanup_retrans_entry(cm_node);
  1446. cm_node->state = NES_CM_STATE_LAST_ACK;
  1447. send_fin(cm_node, NULL);
  1448. break;
  1449. case NES_CM_STATE_MPAREQ_SENT:
  1450. create_event(cm_node, NES_CM_EVENT_ABORTED);
  1451. cm_node->tcp_cntxt.rcv_nxt++;
  1452. cleanup_retrans_entry(cm_node);
  1453. cm_node->state = NES_CM_STATE_CLOSED;
  1454. add_ref_cm_node(cm_node);
  1455. send_reset(cm_node, NULL);
  1456. break;
  1457. case NES_CM_STATE_FIN_WAIT1:
  1458. cm_node->tcp_cntxt.rcv_nxt++;
  1459. cleanup_retrans_entry(cm_node);
  1460. cm_node->state = NES_CM_STATE_CLOSING;
  1461. send_ack(cm_node, NULL);
  1462. /* Wait for ACK as this is simultaneous close..
  1463. * After we receive ACK, do not send anything..
  1464. * Just rm the node.. Done.. */
  1465. break;
  1466. case NES_CM_STATE_FIN_WAIT2:
  1467. cm_node->tcp_cntxt.rcv_nxt++;
  1468. cleanup_retrans_entry(cm_node);
  1469. cm_node->state = NES_CM_STATE_TIME_WAIT;
  1470. send_ack(cm_node, NULL);
  1471. schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
  1472. break;
  1473. case NES_CM_STATE_TIME_WAIT:
  1474. cm_node->tcp_cntxt.rcv_nxt++;
  1475. cleanup_retrans_entry(cm_node);
  1476. cm_node->state = NES_CM_STATE_CLOSED;
  1477. rem_ref_cm_node(cm_node->cm_core, cm_node);
  1478. break;
  1479. case NES_CM_STATE_TSA:
  1480. default:
  1481. nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
  1482. cm_node, cm_node->state);
  1483. break;
  1484. }
  1485. }
  1486. static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1487. struct tcphdr *tcph)
  1488. {
  1489. int reset = 0; /* whether to send reset in case of err.. */
  1490. atomic_inc(&cm_resets_recvd);
  1491. nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
  1492. " refcnt=%d\n", cm_node, cm_node->state,
  1493. atomic_read(&cm_node->ref_count));
  1494. cleanup_retrans_entry(cm_node);
  1495. switch (cm_node->state) {
  1496. case NES_CM_STATE_SYN_SENT:
  1497. case NES_CM_STATE_MPAREQ_SENT:
  1498. nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
  1499. "listener=%p state=%d\n", __func__, __LINE__, cm_node,
  1500. cm_node->listener, cm_node->state);
  1501. switch (cm_node->mpa_frame_rev) {
  1502. case IETF_MPA_V2:
  1503. cm_node->mpa_frame_rev = IETF_MPA_V1;
  1504. /* send a syn and goto syn sent state */
  1505. cm_node->state = NES_CM_STATE_SYN_SENT;
  1506. if (send_syn(cm_node, 0, NULL)) {
  1507. active_open_err(cm_node, skb, reset);
  1508. }
  1509. break;
  1510. case IETF_MPA_V1:
  1511. default:
  1512. active_open_err(cm_node, skb, reset);
  1513. break;
  1514. }
  1515. break;
  1516. case NES_CM_STATE_MPAREQ_RCVD:
  1517. atomic_inc(&cm_node->passive_state);
  1518. dev_kfree_skb_any(skb);
  1519. break;
  1520. case NES_CM_STATE_ESTABLISHED:
  1521. case NES_CM_STATE_SYN_RCVD:
  1522. case NES_CM_STATE_LISTENING:
  1523. nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
  1524. passive_open_err(cm_node, skb, reset);
  1525. break;
  1526. case NES_CM_STATE_TSA:
  1527. active_open_err(cm_node, skb, reset);
  1528. break;
  1529. case NES_CM_STATE_CLOSED:
  1530. drop_packet(skb);
  1531. break;
  1532. case NES_CM_STATE_FIN_WAIT2:
  1533. case NES_CM_STATE_FIN_WAIT1:
  1534. case NES_CM_STATE_LAST_ACK:
  1535. cm_node->cm_id->rem_ref(cm_node->cm_id);
  1536. /* fall through */
  1537. case NES_CM_STATE_TIME_WAIT:
  1538. cm_node->state = NES_CM_STATE_CLOSED;
  1539. rem_ref_cm_node(cm_node->cm_core, cm_node);
  1540. drop_packet(skb);
  1541. break;
  1542. default:
  1543. drop_packet(skb);
  1544. break;
  1545. }
  1546. }
  1547. static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
  1548. {
  1549. int ret = 0;
  1550. int datasize = skb->len;
  1551. u8 *dataloc = skb->data;
  1552. enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
  1553. u32 res_type;
  1554. ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
  1555. if (ret) {
  1556. nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
  1557. if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
  1558. nes_debug(NES_DBG_CM, "%s[%u] create abort for "
  1559. "cm_node=%p listener=%p state=%d\n", __func__,
  1560. __LINE__, cm_node, cm_node->listener,
  1561. cm_node->state);
  1562. active_open_err(cm_node, skb, 1);
  1563. } else {
  1564. passive_open_err(cm_node, skb, 1);
  1565. }
  1566. return;
  1567. }
  1568. switch (cm_node->state) {
  1569. case NES_CM_STATE_ESTABLISHED:
  1570. if (res_type == NES_MPA_REQUEST_REJECT)
  1571. /*BIG problem as we are receiving the MPA.. So should
  1572. * not be REJECT.. This is Passive Open.. We can
  1573. * only receive it Reject for Active Open...*/
  1574. WARN_ON(1);
  1575. cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
  1576. type = NES_CM_EVENT_MPA_REQ;
  1577. atomic_set(&cm_node->passive_state,
  1578. NES_PASSIVE_STATE_INDICATED);
  1579. break;
  1580. case NES_CM_STATE_MPAREQ_SENT:
  1581. cleanup_retrans_entry(cm_node);
  1582. if (res_type == NES_MPA_REQUEST_REJECT) {
  1583. type = NES_CM_EVENT_MPA_REJECT;
  1584. cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
  1585. } else {
  1586. type = NES_CM_EVENT_CONNECTED;
  1587. cm_node->state = NES_CM_STATE_TSA;
  1588. }
  1589. send_ack(cm_node, NULL);
  1590. break;
  1591. default:
  1592. WARN_ON(1);
  1593. break;
  1594. }
  1595. dev_kfree_skb_any(skb);
  1596. create_event(cm_node, type);
  1597. }
  1598. static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
  1599. {
  1600. switch (cm_node->state) {
  1601. case NES_CM_STATE_SYN_SENT:
  1602. case NES_CM_STATE_MPAREQ_SENT:
  1603. nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
  1604. "listener=%p state=%d\n", __func__, __LINE__, cm_node,
  1605. cm_node->listener, cm_node->state);
  1606. active_open_err(cm_node, skb, 1);
  1607. break;
  1608. case NES_CM_STATE_ESTABLISHED:
  1609. case NES_CM_STATE_SYN_RCVD:
  1610. passive_open_err(cm_node, skb, 1);
  1611. break;
  1612. case NES_CM_STATE_TSA:
  1613. default:
  1614. drop_packet(skb);
  1615. }
  1616. }
  1617. static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
  1618. struct sk_buff *skb)
  1619. {
  1620. int err;
  1621. err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
  1622. if (err)
  1623. active_open_err(cm_node, skb, 1);
  1624. return err;
  1625. }
  1626. static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
  1627. struct sk_buff *skb)
  1628. {
  1629. int err = 0;
  1630. u32 seq;
  1631. u32 ack_seq;
  1632. u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
  1633. u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
  1634. u32 rcv_wnd;
  1635. seq = ntohl(tcph->seq);
  1636. ack_seq = ntohl(tcph->ack_seq);
  1637. rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
  1638. if (ack_seq != loc_seq_num)
  1639. err = 1;
  1640. else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
  1641. err = 1;
  1642. if (err) {
  1643. nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
  1644. "listener=%p state=%d\n", __func__, __LINE__, cm_node,
  1645. cm_node->listener, cm_node->state);
  1646. indicate_pkt_err(cm_node, skb);
  1647. nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
  1648. "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
  1649. rcv_wnd);
  1650. }
  1651. return err;
  1652. }
  1653. /*
  1654. * handle_syn_pkt() is for Passive node. The syn packet is received when a node
  1655. * is created with a listener or it may comein as rexmitted packet which in
  1656. * that case will be just dropped.
  1657. */
  1658. static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1659. struct tcphdr *tcph)
  1660. {
  1661. int ret;
  1662. u32 inc_sequence;
  1663. int optionsize;
  1664. optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
  1665. skb_trim(skb, 0);
  1666. inc_sequence = ntohl(tcph->seq);
  1667. switch (cm_node->state) {
  1668. case NES_CM_STATE_SYN_SENT:
  1669. case NES_CM_STATE_MPAREQ_SENT:
  1670. /* Rcvd syn on active open connection*/
  1671. active_open_err(cm_node, skb, 1);
  1672. break;
  1673. case NES_CM_STATE_LISTENING:
  1674. /* Passive OPEN */
  1675. if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
  1676. cm_node->listener->backlog) {
  1677. nes_debug(NES_DBG_CM, "drop syn due to backlog "
  1678. "pressure \n");
  1679. cm_backlog_drops++;
  1680. passive_open_err(cm_node, skb, 0);
  1681. break;
  1682. }
  1683. ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
  1684. 1);
  1685. if (ret) {
  1686. passive_open_err(cm_node, skb, 0);
  1687. /* drop pkt */
  1688. break;
  1689. }
  1690. cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
  1691. BUG_ON(cm_node->send_entry);
  1692. cm_node->accept_pend = 1;
  1693. atomic_inc(&cm_node->listener->pend_accepts_cnt);
  1694. cm_node->state = NES_CM_STATE_SYN_RCVD;
  1695. send_syn(cm_node, 1, skb);
  1696. break;
  1697. case NES_CM_STATE_CLOSED:
  1698. cleanup_retrans_entry(cm_node);
  1699. add_ref_cm_node(cm_node);
  1700. send_reset(cm_node, skb);
  1701. break;
  1702. case NES_CM_STATE_TSA:
  1703. case NES_CM_STATE_ESTABLISHED:
  1704. case NES_CM_STATE_FIN_WAIT1:
  1705. case NES_CM_STATE_FIN_WAIT2:
  1706. case NES_CM_STATE_MPAREQ_RCVD:
  1707. case NES_CM_STATE_LAST_ACK:
  1708. case NES_CM_STATE_CLOSING:
  1709. case NES_CM_STATE_UNKNOWN:
  1710. default:
  1711. drop_packet(skb);
  1712. break;
  1713. }
  1714. }
  1715. static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1716. struct tcphdr *tcph)
  1717. {
  1718. int ret;
  1719. u32 inc_sequence;
  1720. int optionsize;
  1721. optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
  1722. skb_trim(skb, 0);
  1723. inc_sequence = ntohl(tcph->seq);
  1724. switch (cm_node->state) {
  1725. case NES_CM_STATE_SYN_SENT:
  1726. cleanup_retrans_entry(cm_node);
  1727. /* active open */
  1728. if (check_syn(cm_node, tcph, skb))
  1729. return;
  1730. cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
  1731. /* setup options */
  1732. ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
  1733. if (ret) {
  1734. nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
  1735. cm_node);
  1736. break;
  1737. }
  1738. cleanup_retrans_entry(cm_node);
  1739. cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
  1740. send_mpa_request(cm_node, skb);
  1741. cm_node->state = NES_CM_STATE_MPAREQ_SENT;
  1742. break;
  1743. case NES_CM_STATE_MPAREQ_RCVD:
  1744. /* passive open, so should not be here */
  1745. passive_open_err(cm_node, skb, 1);
  1746. break;
  1747. case NES_CM_STATE_LISTENING:
  1748. cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
  1749. cleanup_retrans_entry(cm_node);
  1750. cm_node->state = NES_CM_STATE_CLOSED;
  1751. send_reset(cm_node, skb);
  1752. break;
  1753. case NES_CM_STATE_CLOSED:
  1754. cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
  1755. cleanup_retrans_entry(cm_node);
  1756. add_ref_cm_node(cm_node);
  1757. send_reset(cm_node, skb);
  1758. break;
  1759. case NES_CM_STATE_ESTABLISHED:
  1760. case NES_CM_STATE_FIN_WAIT1:
  1761. case NES_CM_STATE_FIN_WAIT2:
  1762. case NES_CM_STATE_LAST_ACK:
  1763. case NES_CM_STATE_TSA:
  1764. case NES_CM_STATE_CLOSING:
  1765. case NES_CM_STATE_UNKNOWN:
  1766. case NES_CM_STATE_MPAREQ_SENT:
  1767. default:
  1768. drop_packet(skb);
  1769. break;
  1770. }
  1771. }
  1772. static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1773. struct tcphdr *tcph)
  1774. {
  1775. int datasize = 0;
  1776. u32 inc_sequence;
  1777. int ret = 0;
  1778. int optionsize;
  1779. optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
  1780. if (check_seq(cm_node, tcph, skb))
  1781. return -EINVAL;
  1782. skb_pull(skb, tcph->doff << 2);
  1783. inc_sequence = ntohl(tcph->seq);
  1784. datasize = skb->len;
  1785. switch (cm_node->state) {
  1786. case NES_CM_STATE_SYN_RCVD:
  1787. /* Passive OPEN */
  1788. cleanup_retrans_entry(cm_node);
  1789. ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
  1790. if (ret)
  1791. break;
  1792. cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
  1793. cm_node->state = NES_CM_STATE_ESTABLISHED;
  1794. if (datasize) {
  1795. cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
  1796. handle_rcv_mpa(cm_node, skb);
  1797. } else { /* rcvd ACK only */
  1798. dev_kfree_skb_any(skb);
  1799. }
  1800. break;
  1801. case NES_CM_STATE_ESTABLISHED:
  1802. /* Passive OPEN */
  1803. cleanup_retrans_entry(cm_node);
  1804. if (datasize) {
  1805. cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
  1806. handle_rcv_mpa(cm_node, skb);
  1807. } else {
  1808. drop_packet(skb);
  1809. }
  1810. break;
  1811. case NES_CM_STATE_MPAREQ_SENT:
  1812. cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
  1813. if (datasize) {
  1814. cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
  1815. handle_rcv_mpa(cm_node, skb);
  1816. } else { /* Could be just an ack pkt.. */
  1817. dev_kfree_skb_any(skb);
  1818. }
  1819. break;
  1820. case NES_CM_STATE_LISTENING:
  1821. cleanup_retrans_entry(cm_node);
  1822. cm_node->state = NES_CM_STATE_CLOSED;
  1823. send_reset(cm_node, skb);
  1824. break;
  1825. case NES_CM_STATE_CLOSED:
  1826. cleanup_retrans_entry(cm_node);
  1827. add_ref_cm_node(cm_node);
  1828. send_reset(cm_node, skb);
  1829. break;
  1830. case NES_CM_STATE_LAST_ACK:
  1831. case NES_CM_STATE_CLOSING:
  1832. cleanup_retrans_entry(cm_node);
  1833. cm_node->state = NES_CM_STATE_CLOSED;
  1834. cm_node->cm_id->rem_ref(cm_node->cm_id);
  1835. rem_ref_cm_node(cm_node->cm_core, cm_node);
  1836. drop_packet(skb);
  1837. break;
  1838. case NES_CM_STATE_FIN_WAIT1:
  1839. cleanup_retrans_entry(cm_node);
  1840. drop_packet(skb);
  1841. cm_node->state = NES_CM_STATE_FIN_WAIT2;
  1842. break;
  1843. case NES_CM_STATE_SYN_SENT:
  1844. case NES_CM_STATE_FIN_WAIT2:
  1845. case NES_CM_STATE_TSA:
  1846. case NES_CM_STATE_MPAREQ_RCVD:
  1847. case NES_CM_STATE_UNKNOWN:
  1848. default:
  1849. cleanup_retrans_entry(cm_node);
  1850. drop_packet(skb);
  1851. break;
  1852. }
  1853. return ret;
  1854. }
  1855. static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
  1856. struct sk_buff *skb, int optionsize, int passive)
  1857. {
  1858. u8 *optionsloc = (u8 *)&tcph[1];
  1859. if (optionsize) {
  1860. if (process_options(cm_node, optionsloc, optionsize,
  1861. (u32)tcph->syn)) {
  1862. nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
  1863. __func__, cm_node);
  1864. if (passive)
  1865. passive_open_err(cm_node, skb, 1);
  1866. else
  1867. active_open_err(cm_node, skb, 1);
  1868. return 1;
  1869. }
  1870. }
  1871. cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
  1872. cm_node->tcp_cntxt.snd_wscale;
  1873. if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
  1874. cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
  1875. return 0;
  1876. }
  1877. /*
  1878. * active_open_err() will send reset() if flag set..
  1879. * It will also send ABORT event.
  1880. */
  1881. static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1882. int reset)
  1883. {
  1884. cleanup_retrans_entry(cm_node);
  1885. if (reset) {
  1886. nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
  1887. "state=%d\n", cm_node, cm_node->state);
  1888. add_ref_cm_node(cm_node);
  1889. send_reset(cm_node, skb);
  1890. } else {
  1891. dev_kfree_skb_any(skb);
  1892. }
  1893. cm_node->state = NES_CM_STATE_CLOSED;
  1894. create_event(cm_node, NES_CM_EVENT_ABORTED);
  1895. }
  1896. /*
  1897. * passive_open_err() will either do a reset() or will free up the skb and
  1898. * remove the cm_node.
  1899. */
  1900. static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1901. int reset)
  1902. {
  1903. cleanup_retrans_entry(cm_node);
  1904. cm_node->state = NES_CM_STATE_CLOSED;
  1905. if (reset) {
  1906. nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
  1907. "cm_node=%p state =%d\n", cm_node, cm_node->state);
  1908. send_reset(cm_node, skb);
  1909. } else {
  1910. dev_kfree_skb_any(skb);
  1911. rem_ref_cm_node(cm_node->cm_core, cm_node);
  1912. }
  1913. }
  1914. /*
  1915. * free_retrans_entry() routines assumes that the retrans_list_lock has
  1916. * been acquired before calling.
  1917. */
  1918. static void free_retrans_entry(struct nes_cm_node *cm_node)
  1919. {
  1920. struct nes_timer_entry *send_entry;
  1921. send_entry = cm_node->send_entry;
  1922. if (send_entry) {
  1923. cm_node->send_entry = NULL;
  1924. dev_kfree_skb_any(send_entry->skb);
  1925. kfree(send_entry);
  1926. rem_ref_cm_node(cm_node->cm_core, cm_node);
  1927. }
  1928. }
  1929. static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
  1930. {
  1931. unsigned long flags;
  1932. spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
  1933. free_retrans_entry(cm_node);
  1934. spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
  1935. }
  1936. /**
  1937. * process_packet
  1938. * Returns skb if to be freed, else it will return NULL if already used..
  1939. */
  1940. static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
  1941. struct nes_cm_core *cm_core)
  1942. {
  1943. enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
  1944. struct tcphdr *tcph = tcp_hdr(skb);
  1945. u32 fin_set = 0;
  1946. int ret = 0;
  1947. skb_pull(skb, ip_hdr(skb)->ihl << 2);
  1948. nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
  1949. "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
  1950. tcph->ack, tcph->rst, tcph->fin);
  1951. if (tcph->rst) {
  1952. pkt_type = NES_PKT_TYPE_RST;
  1953. } else if (tcph->syn) {
  1954. pkt_type = NES_PKT_TYPE_SYN;
  1955. if (tcph->ack)
  1956. pkt_type = NES_PKT_TYPE_SYNACK;
  1957. } else if (tcph->ack) {
  1958. pkt_type = NES_PKT_TYPE_ACK;
  1959. }
  1960. if (tcph->fin)
  1961. fin_set = 1;
  1962. switch (pkt_type) {
  1963. case NES_PKT_TYPE_SYN:
  1964. handle_syn_pkt(cm_node, skb, tcph);
  1965. break;
  1966. case NES_PKT_TYPE_SYNACK:
  1967. handle_synack_pkt(cm_node, skb, tcph);
  1968. break;
  1969. case NES_PKT_TYPE_ACK:
  1970. ret = handle_ack_pkt(cm_node, skb, tcph);
  1971. if (fin_set && !ret)
  1972. handle_fin_pkt(cm_node);
  1973. break;
  1974. case NES_PKT_TYPE_RST:
  1975. handle_rst_pkt(cm_node, skb, tcph);
  1976. break;
  1977. default:
  1978. if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
  1979. handle_fin_pkt(cm_node);
  1980. drop_packet(skb);
  1981. break;
  1982. }
  1983. }
  1984. /**
  1985. * mini_cm_listen - create a listen node with params
  1986. */
  1987. static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
  1988. struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
  1989. {
  1990. struct nes_cm_listener *listener;
  1991. unsigned long flags;
  1992. nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
  1993. cm_info->loc_addr, cm_info->loc_port);
  1994. /* cannot have multiple matching listeners */
  1995. listener = find_listener(cm_core, cm_info->loc_addr, cm_info->loc_port,
  1996. NES_CM_LISTENER_EITHER_STATE);
  1997. if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
  1998. /* find automatically incs ref count ??? */
  1999. atomic_dec(&listener->ref_count);
  2000. nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
  2001. return NULL;
  2002. }
  2003. if (!listener) {
  2004. /* create a CM listen node (1/2 node to compare incoming traffic to) */
  2005. listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
  2006. if (!listener)
  2007. return NULL;
  2008. listener->loc_addr = cm_info->loc_addr;
  2009. listener->loc_port = cm_info->loc_port;
  2010. listener->reused_node = 0;
  2011. atomic_set(&listener->ref_count, 1);
  2012. }
  2013. /* pasive case */
  2014. /* find already inc'ed the ref count */
  2015. else {
  2016. listener->reused_node = 1;
  2017. }
  2018. listener->cm_id = cm_info->cm_id;
  2019. atomic_set(&listener->pend_accepts_cnt, 0);
  2020. listener->cm_core = cm_core;
  2021. listener->nesvnic = nesvnic;
  2022. atomic_inc(&cm_core->node_cnt);
  2023. listener->conn_type = cm_info->conn_type;
  2024. listener->backlog = cm_info->backlog;
  2025. listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
  2026. if (!listener->reused_node) {
  2027. spin_lock_irqsave(&cm_core->listen_list_lock, flags);
  2028. list_add(&listener->list, &cm_core->listen_list.list);
  2029. spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
  2030. atomic_inc(&cm_core->listen_node_cnt);
  2031. }
  2032. nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
  2033. " listener = %p, backlog = %d, cm_id = %p.\n",
  2034. cm_info->loc_addr, cm_info->loc_port,
  2035. listener, listener->backlog, listener->cm_id);
  2036. return listener;
  2037. }
  2038. /**
  2039. * mini_cm_connect - make a connection node with params
  2040. */
  2041. static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
  2042. struct nes_vnic *nesvnic, u16 private_data_len,
  2043. void *private_data, struct nes_cm_info *cm_info)
  2044. {
  2045. int ret = 0;
  2046. struct nes_cm_node *cm_node;
  2047. struct nes_cm_listener *loopbackremotelistener;
  2048. struct nes_cm_node *loopbackremotenode;
  2049. struct nes_cm_info loopback_cm_info;
  2050. u8 *start_buff;
  2051. /* create a CM connection node */
  2052. cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
  2053. if (!cm_node)
  2054. return NULL;
  2055. /* set our node side to client (active) side */
  2056. cm_node->tcp_cntxt.client = 1;
  2057. cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
  2058. if (cm_info->loc_addr == cm_info->rem_addr) {
  2059. loopbackremotelistener = find_listener(cm_core,
  2060. cm_node->loc_addr, cm_node->rem_port,
  2061. NES_CM_LISTENER_ACTIVE_STATE);
  2062. if (loopbackremotelistener == NULL) {
  2063. create_event(cm_node, NES_CM_EVENT_ABORTED);
  2064. } else {
  2065. loopback_cm_info = *cm_info;
  2066. loopback_cm_info.loc_port = cm_info->rem_port;
  2067. loopback_cm_info.rem_port = cm_info->loc_port;
  2068. loopback_cm_info.loc_port =
  2069. cm_info->rem_port;
  2070. loopback_cm_info.rem_port =
  2071. cm_info->loc_port;
  2072. loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
  2073. loopbackremotenode = make_cm_node(cm_core, nesvnic,
  2074. &loopback_cm_info, loopbackremotelistener);
  2075. if (!loopbackremotenode) {
  2076. rem_ref_cm_node(cm_node->cm_core, cm_node);
  2077. return NULL;
  2078. }
  2079. atomic_inc(&cm_loopbacks);
  2080. loopbackremotenode->loopbackpartner = cm_node;
  2081. loopbackremotenode->tcp_cntxt.rcv_wscale =
  2082. NES_CM_DEFAULT_RCV_WND_SCALE;
  2083. cm_node->loopbackpartner = loopbackremotenode;
  2084. memcpy(loopbackremotenode->mpa_frame_buf, private_data,
  2085. private_data_len);
  2086. loopbackremotenode->mpa_frame_size = private_data_len;
  2087. /* we are done handling this state. */
  2088. /* set node to a TSA state */
  2089. cm_node->state = NES_CM_STATE_TSA;
  2090. cm_node->tcp_cntxt.rcv_nxt =
  2091. loopbackremotenode->tcp_cntxt.loc_seq_num;
  2092. loopbackremotenode->tcp_cntxt.rcv_nxt =
  2093. cm_node->tcp_cntxt.loc_seq_num;
  2094. cm_node->tcp_cntxt.max_snd_wnd =
  2095. loopbackremotenode->tcp_cntxt.rcv_wnd;
  2096. loopbackremotenode->tcp_cntxt.max_snd_wnd =
  2097. cm_node->tcp_cntxt.rcv_wnd;
  2098. cm_node->tcp_cntxt.snd_wnd =
  2099. loopbackremotenode->tcp_cntxt.rcv_wnd;
  2100. loopbackremotenode->tcp_cntxt.snd_wnd =
  2101. cm_node->tcp_cntxt.rcv_wnd;
  2102. cm_node->tcp_cntxt.snd_wscale =
  2103. loopbackremotenode->tcp_cntxt.rcv_wscale;
  2104. loopbackremotenode->tcp_cntxt.snd_wscale =
  2105. cm_node->tcp_cntxt.rcv_wscale;
  2106. loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
  2107. create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
  2108. }
  2109. return cm_node;
  2110. }
  2111. start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
  2112. cm_node->mpa_frame_size = private_data_len;
  2113. memcpy(start_buff, private_data, private_data_len);
  2114. /* send a syn and goto syn sent state */
  2115. cm_node->state = NES_CM_STATE_SYN_SENT;
  2116. ret = send_syn(cm_node, 0, NULL);
  2117. if (ret) {
  2118. /* error in sending the syn free up the cm_node struct */
  2119. nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
  2120. "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
  2121. cm_node->rem_addr, cm_node->rem_port, cm_node,
  2122. cm_node->cm_id);
  2123. rem_ref_cm_node(cm_node->cm_core, cm_node);
  2124. cm_node = NULL;
  2125. }
  2126. if (cm_node) {
  2127. nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
  2128. "port=0x%04x, cm_node=%p, cm_id = %p.\n",
  2129. cm_node->rem_addr, cm_node->rem_port, cm_node,
  2130. cm_node->cm_id);
  2131. }
  2132. return cm_node;
  2133. }
  2134. /**
  2135. * mini_cm_accept - accept a connection
  2136. * This function is never called
  2137. */
  2138. static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
  2139. {
  2140. return 0;
  2141. }
  2142. /**
  2143. * mini_cm_reject - reject and teardown a connection
  2144. */
  2145. static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
  2146. {
  2147. int ret = 0;
  2148. int err = 0;
  2149. int passive_state;
  2150. struct nes_cm_event event;
  2151. struct iw_cm_id *cm_id = cm_node->cm_id;
  2152. struct nes_cm_node *loopback = cm_node->loopbackpartner;
  2153. nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
  2154. __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
  2155. if (cm_node->tcp_cntxt.client)
  2156. return ret;
  2157. cleanup_retrans_entry(cm_node);
  2158. if (!loopback) {
  2159. passive_state = atomic_add_return(1, &cm_node->passive_state);
  2160. if (passive_state == NES_SEND_RESET_EVENT) {
  2161. cm_node->state = NES_CM_STATE_CLOSED;
  2162. rem_ref_cm_node(cm_core, cm_node);
  2163. } else {
  2164. if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
  2165. rem_ref_cm_node(cm_core, cm_node);
  2166. } else {
  2167. ret = send_mpa_reject(cm_node);
  2168. if (ret) {
  2169. cm_node->state = NES_CM_STATE_CLOSED;
  2170. err = send_reset(cm_node, NULL);
  2171. if (err)
  2172. WARN_ON(1);
  2173. } else {
  2174. cm_id->add_ref(cm_id);
  2175. }
  2176. }
  2177. }
  2178. } else {
  2179. cm_node->cm_id = NULL;
  2180. if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
  2181. rem_ref_cm_node(cm_core, cm_node);
  2182. rem_ref_cm_node(cm_core, loopback);
  2183. } else {
  2184. event.cm_node = loopback;
  2185. event.cm_info.rem_addr = loopback->rem_addr;
  2186. event.cm_info.loc_addr = loopback->loc_addr;
  2187. event.cm_info.rem_port = loopback->rem_port;
  2188. event.cm_info.loc_port = loopback->loc_port;
  2189. event.cm_info.cm_id = loopback->cm_id;
  2190. cm_event_mpa_reject(&event);
  2191. rem_ref_cm_node(cm_core, cm_node);
  2192. loopback->state = NES_CM_STATE_CLOSING;
  2193. cm_id = loopback->cm_id;
  2194. rem_ref_cm_node(cm_core, loopback);
  2195. cm_id->rem_ref(cm_id);
  2196. }
  2197. }
  2198. return ret;
  2199. }
  2200. /**
  2201. * mini_cm_close
  2202. */
  2203. static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
  2204. {
  2205. int ret = 0;
  2206. if (!cm_core || !cm_node)
  2207. return -EINVAL;
  2208. switch (cm_node->state) {
  2209. case NES_CM_STATE_SYN_RCVD:
  2210. case NES_CM_STATE_SYN_SENT:
  2211. case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
  2212. case NES_CM_STATE_ESTABLISHED:
  2213. case NES_CM_STATE_ACCEPTING:
  2214. case NES_CM_STATE_MPAREQ_SENT:
  2215. case NES_CM_STATE_MPAREQ_RCVD:
  2216. cleanup_retrans_entry(cm_node);
  2217. send_reset(cm_node, NULL);
  2218. break;
  2219. case NES_CM_STATE_CLOSE_WAIT:
  2220. cm_node->state = NES_CM_STATE_LAST_ACK;
  2221. send_fin(cm_node, NULL);
  2222. break;
  2223. case NES_CM_STATE_FIN_WAIT1:
  2224. case NES_CM_STATE_FIN_WAIT2:
  2225. case NES_CM_STATE_LAST_ACK:
  2226. case NES_CM_STATE_TIME_WAIT:
  2227. case NES_CM_STATE_CLOSING:
  2228. ret = -1;
  2229. break;
  2230. case NES_CM_STATE_LISTENING:
  2231. cleanup_retrans_entry(cm_node);
  2232. send_reset(cm_node, NULL);
  2233. break;
  2234. case NES_CM_STATE_MPAREJ_RCVD:
  2235. case NES_CM_STATE_UNKNOWN:
  2236. case NES_CM_STATE_INITED:
  2237. case NES_CM_STATE_CLOSED:
  2238. case NES_CM_STATE_LISTENER_DESTROYED:
  2239. ret = rem_ref_cm_node(cm_core, cm_node);
  2240. break;
  2241. case NES_CM_STATE_TSA:
  2242. if (cm_node->send_entry)
  2243. printk(KERN_ERR "ERROR Close got called from STATE_TSA "
  2244. "send_entry=%p\n", cm_node->send_entry);
  2245. ret = rem_ref_cm_node(cm_core, cm_node);
  2246. break;
  2247. }
  2248. return ret;
  2249. }
  2250. /**
  2251. * recv_pkt - recv an ETHERNET packet, and process it through CM
  2252. * node state machine
  2253. */
  2254. static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
  2255. struct nes_vnic *nesvnic, struct sk_buff *skb)
  2256. {
  2257. struct nes_cm_node *cm_node = NULL;
  2258. struct nes_cm_listener *listener = NULL;
  2259. struct iphdr *iph;
  2260. struct tcphdr *tcph;
  2261. struct nes_cm_info nfo;
  2262. int skb_handled = 1;
  2263. __be32 tmp_daddr, tmp_saddr;
  2264. if (!skb)
  2265. return 0;
  2266. if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
  2267. return 0;
  2268. iph = (struct iphdr *)skb->data;
  2269. tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
  2270. nfo.loc_addr = ntohl(iph->daddr);
  2271. nfo.loc_port = ntohs(tcph->dest);
  2272. nfo.rem_addr = ntohl(iph->saddr);
  2273. nfo.rem_port = ntohs(tcph->source);
  2274. tmp_daddr = cpu_to_be32(iph->daddr);
  2275. tmp_saddr = cpu_to_be32(iph->saddr);
  2276. nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
  2277. &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
  2278. do {
  2279. cm_node = find_node(cm_core,
  2280. nfo.rem_port, nfo.rem_addr,
  2281. nfo.loc_port, nfo.loc_addr);
  2282. if (!cm_node) {
  2283. /* Only type of packet accepted are for */
  2284. /* the PASSIVE open (syn only) */
  2285. if ((!tcph->syn) || (tcph->ack)) {
  2286. skb_handled = 0;
  2287. break;
  2288. }
  2289. listener = find_listener(cm_core, nfo.loc_addr,
  2290. nfo.loc_port,
  2291. NES_CM_LISTENER_ACTIVE_STATE);
  2292. if (!listener) {
  2293. nfo.cm_id = NULL;
  2294. nfo.conn_type = 0;
  2295. nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
  2296. skb_handled = 0;
  2297. break;
  2298. }
  2299. nfo.cm_id = listener->cm_id;
  2300. nfo.conn_type = listener->conn_type;
  2301. cm_node = make_cm_node(cm_core, nesvnic, &nfo,
  2302. listener);
  2303. if (!cm_node) {
  2304. nes_debug(NES_DBG_CM, "Unable to allocate "
  2305. "node\n");
  2306. cm_packets_dropped++;
  2307. atomic_dec(&listener->ref_count);
  2308. dev_kfree_skb_any(skb);
  2309. break;
  2310. }
  2311. if (!tcph->rst && !tcph->fin) {
  2312. cm_node->state = NES_CM_STATE_LISTENING;
  2313. } else {
  2314. cm_packets_dropped++;
  2315. rem_ref_cm_node(cm_core, cm_node);
  2316. dev_kfree_skb_any(skb);
  2317. break;
  2318. }
  2319. add_ref_cm_node(cm_node);
  2320. } else if (cm_node->state == NES_CM_STATE_TSA) {
  2321. if (cm_node->nesqp->pau_mode)
  2322. nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
  2323. else {
  2324. rem_ref_cm_node(cm_core, cm_node);
  2325. atomic_inc(&cm_accel_dropped_pkts);
  2326. dev_kfree_skb_any(skb);
  2327. }
  2328. break;
  2329. }
  2330. skb_reset_network_header(skb);
  2331. skb_set_transport_header(skb, sizeof(*tcph));
  2332. skb->len = ntohs(iph->tot_len);
  2333. process_packet(cm_node, skb, cm_core);
  2334. rem_ref_cm_node(cm_core, cm_node);
  2335. } while (0);
  2336. return skb_handled;
  2337. }
  2338. /**
  2339. * nes_cm_alloc_core - allocate a top level instance of a cm core
  2340. */
  2341. static struct nes_cm_core *nes_cm_alloc_core(void)
  2342. {
  2343. struct nes_cm_core *cm_core;
  2344. /* setup the CM core */
  2345. /* alloc top level core control structure */
  2346. cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
  2347. if (!cm_core)
  2348. return NULL;
  2349. INIT_LIST_HEAD(&cm_core->connected_nodes);
  2350. timer_setup(&cm_core->tcp_timer, nes_cm_timer_tick, 0);
  2351. cm_core->mtu = NES_CM_DEFAULT_MTU;
  2352. cm_core->state = NES_CM_STATE_INITED;
  2353. cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
  2354. atomic_set(&cm_core->events_posted, 0);
  2355. cm_core->api = &nes_cm_api;
  2356. spin_lock_init(&cm_core->ht_lock);
  2357. spin_lock_init(&cm_core->listen_list_lock);
  2358. INIT_LIST_HEAD(&cm_core->listen_list.list);
  2359. nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
  2360. nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
  2361. cm_core->event_wq = alloc_ordered_workqueue("nesewq", 0);
  2362. if (!cm_core->event_wq)
  2363. goto out_free_cmcore;
  2364. cm_core->post_event = nes_cm_post_event;
  2365. nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
  2366. cm_core->disconn_wq = alloc_ordered_workqueue("nesdwq", 0);
  2367. if (!cm_core->disconn_wq)
  2368. goto out_free_wq;
  2369. print_core(cm_core);
  2370. return cm_core;
  2371. out_free_wq:
  2372. destroy_workqueue(cm_core->event_wq);
  2373. out_free_cmcore:
  2374. kfree(cm_core);
  2375. return NULL;
  2376. }
  2377. /**
  2378. * mini_cm_dealloc_core - deallocate a top level instance of a cm core
  2379. */
  2380. static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
  2381. {
  2382. nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
  2383. if (!cm_core)
  2384. return -EINVAL;
  2385. barrier();
  2386. if (timer_pending(&cm_core->tcp_timer))
  2387. del_timer(&cm_core->tcp_timer);
  2388. destroy_workqueue(cm_core->event_wq);
  2389. destroy_workqueue(cm_core->disconn_wq);
  2390. nes_debug(NES_DBG_CM, "\n");
  2391. kfree(cm_core);
  2392. return 0;
  2393. }
  2394. /**
  2395. * mini_cm_get
  2396. */
  2397. static int mini_cm_get(struct nes_cm_core *cm_core)
  2398. {
  2399. return cm_core->state;
  2400. }
  2401. /**
  2402. * mini_cm_set
  2403. */
  2404. static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
  2405. {
  2406. int ret = 0;
  2407. switch (type) {
  2408. case NES_CM_SET_PKT_SIZE:
  2409. cm_core->mtu = value;
  2410. break;
  2411. case NES_CM_SET_FREE_PKT_Q_SIZE:
  2412. cm_core->free_tx_pkt_max = value;
  2413. break;
  2414. default:
  2415. /* unknown set option */
  2416. ret = -EINVAL;
  2417. }
  2418. return ret;
  2419. }
  2420. /**
  2421. * nes_cm_init_tsa_conn setup HW; MPA frames must be
  2422. * successfully exchanged when this is called
  2423. */
  2424. static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
  2425. {
  2426. int ret = 0;
  2427. if (!nesqp)
  2428. return -EINVAL;
  2429. nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
  2430. NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
  2431. NES_QPCONTEXT_MISC_DROS);
  2432. if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
  2433. nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
  2434. nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
  2435. nesqp->nesqp_context->misc2 |= cpu_to_le32(
  2436. cm_node->tos << NES_QPCONTEXT_MISC2_TOS_SHIFT);
  2437. nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
  2438. nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
  2439. (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
  2440. nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
  2441. (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
  2442. NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
  2443. nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
  2444. (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
  2445. NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
  2446. nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
  2447. nesqp->nesqp_context->ts_recent = 0;
  2448. nesqp->nesqp_context->ts_age = 0;
  2449. nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  2450. nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
  2451. nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
  2452. nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
  2453. cm_node->tcp_cntxt.rcv_wscale);
  2454. nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  2455. nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  2456. nesqp->nesqp_context->srtt = 0;
  2457. nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
  2458. nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
  2459. nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
  2460. nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
  2461. nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
  2462. nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
  2463. nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
  2464. " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
  2465. nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
  2466. le32_to_cpu(nesqp->nesqp_context->snd_nxt),
  2467. cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
  2468. le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
  2469. le32_to_cpu(nesqp->nesqp_context->misc));
  2470. nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
  2471. nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
  2472. nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
  2473. nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
  2474. cm_node->state = NES_CM_STATE_TSA;
  2475. return ret;
  2476. }
  2477. /**
  2478. * nes_cm_disconn
  2479. */
  2480. int nes_cm_disconn(struct nes_qp *nesqp)
  2481. {
  2482. struct disconn_work *work;
  2483. work = kzalloc(sizeof *work, GFP_ATOMIC);
  2484. if (!work)
  2485. return -ENOMEM; /* Timer will clean up */
  2486. nes_add_ref(&nesqp->ibqp);
  2487. work->nesqp = nesqp;
  2488. INIT_WORK(&work->work, nes_disconnect_worker);
  2489. queue_work(g_cm_core->disconn_wq, &work->work);
  2490. return 0;
  2491. }
  2492. /**
  2493. * nes_disconnect_worker
  2494. */
  2495. static void nes_disconnect_worker(struct work_struct *work)
  2496. {
  2497. struct disconn_work *dwork = container_of(work, struct disconn_work, work);
  2498. struct nes_qp *nesqp = dwork->nesqp;
  2499. kfree(dwork);
  2500. nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
  2501. nesqp->last_aeq, nesqp->hwqp.qp_id);
  2502. nes_cm_disconn_true(nesqp);
  2503. nes_rem_ref(&nesqp->ibqp);
  2504. }
  2505. /**
  2506. * nes_cm_disconn_true
  2507. */
  2508. static int nes_cm_disconn_true(struct nes_qp *nesqp)
  2509. {
  2510. unsigned long flags;
  2511. int ret = 0;
  2512. struct iw_cm_id *cm_id;
  2513. struct iw_cm_event cm_event;
  2514. struct nes_vnic *nesvnic;
  2515. u16 last_ae;
  2516. u8 original_hw_tcp_state;
  2517. u8 original_ibqp_state;
  2518. int disconn_status = 0;
  2519. int issue_disconn = 0;
  2520. int issue_close = 0;
  2521. int issue_flush = 0;
  2522. u32 flush_q = NES_CQP_FLUSH_RQ;
  2523. struct ib_event ibevent;
  2524. if (!nesqp) {
  2525. nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
  2526. return -1;
  2527. }
  2528. spin_lock_irqsave(&nesqp->lock, flags);
  2529. cm_id = nesqp->cm_id;
  2530. /* make sure we havent already closed this connection */
  2531. if (!cm_id) {
  2532. nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
  2533. nesqp->hwqp.qp_id);
  2534. spin_unlock_irqrestore(&nesqp->lock, flags);
  2535. return -1;
  2536. }
  2537. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2538. nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
  2539. original_hw_tcp_state = nesqp->hw_tcp_state;
  2540. original_ibqp_state = nesqp->ibqp_state;
  2541. last_ae = nesqp->last_aeq;
  2542. if (nesqp->term_flags) {
  2543. issue_disconn = 1;
  2544. issue_close = 1;
  2545. nesqp->cm_id = NULL;
  2546. del_timer(&nesqp->terminate_timer);
  2547. if (nesqp->flush_issued == 0) {
  2548. nesqp->flush_issued = 1;
  2549. issue_flush = 1;
  2550. }
  2551. } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
  2552. ((original_ibqp_state == IB_QPS_RTS) &&
  2553. (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
  2554. issue_disconn = 1;
  2555. if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
  2556. disconn_status = -ECONNRESET;
  2557. }
  2558. if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
  2559. (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
  2560. (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
  2561. (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
  2562. issue_close = 1;
  2563. nesqp->cm_id = NULL;
  2564. if (nesqp->flush_issued == 0) {
  2565. nesqp->flush_issued = 1;
  2566. issue_flush = 1;
  2567. }
  2568. }
  2569. spin_unlock_irqrestore(&nesqp->lock, flags);
  2570. if ((issue_flush) && (nesqp->destroyed == 0)) {
  2571. /* Flush the queue(s) */
  2572. if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
  2573. flush_q |= NES_CQP_FLUSH_SQ;
  2574. flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
  2575. if (nesqp->term_flags) {
  2576. ibevent.device = nesqp->ibqp.device;
  2577. ibevent.event = nesqp->terminate_eventtype;
  2578. ibevent.element.qp = &nesqp->ibqp;
  2579. if (nesqp->ibqp.event_handler)
  2580. nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
  2581. }
  2582. }
  2583. if ((cm_id) && (cm_id->event_handler)) {
  2584. if (issue_disconn) {
  2585. atomic_inc(&cm_disconnects);
  2586. cm_event.event = IW_CM_EVENT_DISCONNECT;
  2587. cm_event.status = disconn_status;
  2588. cm_event.local_addr = cm_id->m_local_addr;
  2589. cm_event.remote_addr = cm_id->m_remote_addr;
  2590. cm_event.private_data = NULL;
  2591. cm_event.private_data_len = 0;
  2592. nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
  2593. " for QP%u, SQ Head = %u, SQ Tail = %u. "
  2594. "cm_id = %p, refcount = %u.\n",
  2595. nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
  2596. nesqp->hwqp.sq_tail, cm_id,
  2597. atomic_read(&nesqp->refcount));
  2598. ret = cm_id->event_handler(cm_id, &cm_event);
  2599. if (ret)
  2600. nes_debug(NES_DBG_CM, "OFA CM event_handler "
  2601. "returned, ret=%d\n", ret);
  2602. }
  2603. if (issue_close) {
  2604. atomic_inc(&cm_closes);
  2605. nes_disconnect(nesqp, 1);
  2606. cm_id->provider_data = nesqp;
  2607. /* Send up the close complete event */
  2608. cm_event.event = IW_CM_EVENT_CLOSE;
  2609. cm_event.status = 0;
  2610. cm_event.provider_data = cm_id->provider_data;
  2611. cm_event.local_addr = cm_id->m_local_addr;
  2612. cm_event.remote_addr = cm_id->m_remote_addr;
  2613. cm_event.private_data = NULL;
  2614. cm_event.private_data_len = 0;
  2615. ret = cm_id->event_handler(cm_id, &cm_event);
  2616. if (ret)
  2617. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  2618. cm_id->rem_ref(cm_id);
  2619. }
  2620. }
  2621. return 0;
  2622. }
  2623. /**
  2624. * nes_disconnect
  2625. */
  2626. static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
  2627. {
  2628. int ret = 0;
  2629. struct nes_vnic *nesvnic;
  2630. struct nes_device *nesdev;
  2631. struct nes_ib_device *nesibdev;
  2632. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2633. if (!nesvnic)
  2634. return -EINVAL;
  2635. nesdev = nesvnic->nesdev;
  2636. nesibdev = nesvnic->nesibdev;
  2637. nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
  2638. netdev_refcnt_read(nesvnic->netdev));
  2639. if (nesqp->active_conn) {
  2640. /* indicate this connection is NOT active */
  2641. nesqp->active_conn = 0;
  2642. } else {
  2643. /* Need to free the Last Streaming Mode Message */
  2644. if (nesqp->ietf_frame) {
  2645. if (nesqp->lsmm_mr)
  2646. nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
  2647. pci_free_consistent(nesdev->pcidev,
  2648. nesqp->private_data_len + nesqp->ietf_frame_size,
  2649. nesqp->ietf_frame, nesqp->ietf_frame_pbase);
  2650. }
  2651. }
  2652. /* close the CM node down if it is still active */
  2653. if (nesqp->cm_node) {
  2654. nes_debug(NES_DBG_CM, "Call close API\n");
  2655. g_cm_core->api->close(g_cm_core, nesqp->cm_node);
  2656. }
  2657. return ret;
  2658. }
  2659. /**
  2660. * nes_accept
  2661. */
  2662. int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  2663. {
  2664. u64 u64temp;
  2665. struct ib_qp *ibqp;
  2666. struct nes_qp *nesqp;
  2667. struct nes_vnic *nesvnic;
  2668. struct nes_device *nesdev;
  2669. struct nes_cm_node *cm_node;
  2670. struct nes_adapter *adapter;
  2671. struct ib_qp_attr attr;
  2672. struct iw_cm_event cm_event;
  2673. struct nes_hw_qp_wqe *wqe;
  2674. struct nes_v4_quad nes_quad;
  2675. u32 crc_value;
  2676. int ret;
  2677. int passive_state;
  2678. struct ib_mr *ibmr = NULL;
  2679. struct nes_pd *nespd;
  2680. u64 tagged_offset;
  2681. u8 mpa_frame_offset = 0;
  2682. struct ietf_mpa_v2 *mpa_v2_frame;
  2683. u8 start_addr = 0;
  2684. u8 *start_ptr = &start_addr;
  2685. u8 **start_buff = &start_ptr;
  2686. u16 buff_len = 0;
  2687. struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
  2688. struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
  2689. ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
  2690. if (!ibqp)
  2691. return -EINVAL;
  2692. /* get all our handles */
  2693. nesqp = to_nesqp(ibqp);
  2694. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2695. nesdev = nesvnic->nesdev;
  2696. adapter = nesdev->nesadapter;
  2697. cm_node = (struct nes_cm_node *)cm_id->provider_data;
  2698. nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
  2699. "%s\n", cm_node, nesvnic, nesvnic->netdev,
  2700. nesvnic->netdev->name);
  2701. if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
  2702. if (cm_node->loopbackpartner)
  2703. rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
  2704. rem_ref_cm_node(cm_node->cm_core, cm_node);
  2705. return -EINVAL;
  2706. }
  2707. passive_state = atomic_add_return(1, &cm_node->passive_state);
  2708. if (passive_state == NES_SEND_RESET_EVENT) {
  2709. rem_ref_cm_node(cm_node->cm_core, cm_node);
  2710. return -ECONNRESET;
  2711. }
  2712. /* associate the node with the QP */
  2713. nesqp->cm_node = (void *)cm_node;
  2714. cm_node->nesqp = nesqp;
  2715. nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
  2716. nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
  2717. atomic_inc(&cm_accepts);
  2718. nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
  2719. netdev_refcnt_read(nesvnic->netdev));
  2720. nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
  2721. /* allocate the ietf frame and space for private data */
  2722. nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
  2723. nesqp->ietf_frame_size + conn_param->private_data_len,
  2724. &nesqp->ietf_frame_pbase);
  2725. if (!nesqp->ietf_frame) {
  2726. nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
  2727. return -ENOMEM;
  2728. }
  2729. mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
  2730. if (cm_node->mpa_frame_rev == IETF_MPA_V1)
  2731. mpa_frame_offset = 4;
  2732. if (cm_node->mpa_frame_rev == IETF_MPA_V1 ||
  2733. cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
  2734. record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
  2735. }
  2736. memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
  2737. conn_param->private_data_len);
  2738. cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
  2739. nesqp->private_data_len = conn_param->private_data_len;
  2740. /* setup our first outgoing iWarp send WQE (the IETF frame response) */
  2741. wqe = &nesqp->hwqp.sq_vbase[0];
  2742. if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
  2743. u64temp = (unsigned long)nesqp;
  2744. nespd = nesqp->nespd;
  2745. tagged_offset = (u64)(unsigned long)*start_buff;
  2746. ibmr = nes_reg_phys_mr(&nespd->ibpd,
  2747. nesqp->ietf_frame_pbase + mpa_frame_offset,
  2748. buff_len, IB_ACCESS_LOCAL_WRITE,
  2749. &tagged_offset);
  2750. if (IS_ERR(ibmr)) {
  2751. nes_debug(NES_DBG_CM, "Unable to register memory region"
  2752. "for lSMM for cm_node = %p \n",
  2753. cm_node);
  2754. pci_free_consistent(nesdev->pcidev,
  2755. nesqp->private_data_len + nesqp->ietf_frame_size,
  2756. nesqp->ietf_frame, nesqp->ietf_frame_pbase);
  2757. return PTR_ERR(ibmr);
  2758. }
  2759. ibmr->pd = &nespd->ibpd;
  2760. ibmr->device = nespd->ibpd.device;
  2761. nesqp->lsmm_mr = ibmr;
  2762. u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
  2763. set_wqe_64bit_value(wqe->wqe_words,
  2764. NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
  2765. u64temp);
  2766. wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
  2767. cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
  2768. NES_IWARP_SQ_WQE_WRPDU);
  2769. wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
  2770. cpu_to_le32(buff_len);
  2771. set_wqe_64bit_value(wqe->wqe_words,
  2772. NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
  2773. (u64)(unsigned long)(*start_buff));
  2774. wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
  2775. cpu_to_le32(buff_len);
  2776. wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
  2777. if (nesqp->sq_kmapped) {
  2778. nesqp->sq_kmapped = 0;
  2779. kunmap(nesqp->page);
  2780. }
  2781. nesqp->nesqp_context->ird_ord_sizes |=
  2782. cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
  2783. NES_QPCONTEXT_ORDIRD_WRPDU);
  2784. } else {
  2785. nesqp->nesqp_context->ird_ord_sizes |=
  2786. cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
  2787. }
  2788. nesqp->skip_lsmm = 1;
  2789. /* Cache the cm_id in the qp */
  2790. nesqp->cm_id = cm_id;
  2791. cm_node->cm_id = cm_id;
  2792. /* nesqp->cm_node = (void *)cm_id->provider_data; */
  2793. cm_id->provider_data = nesqp;
  2794. nesqp->active_conn = 0;
  2795. if (cm_node->state == NES_CM_STATE_TSA)
  2796. nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
  2797. cm_node);
  2798. nes_cm_init_tsa_conn(nesqp, cm_node);
  2799. nesqp->nesqp_context->tcpPorts[0] =
  2800. cpu_to_le16(cm_node->loc_port);
  2801. nesqp->nesqp_context->tcpPorts[1] =
  2802. cpu_to_le16(cm_node->rem_port);
  2803. nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->rem_addr);
  2804. nesqp->nesqp_context->misc2 |= cpu_to_le32(
  2805. (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
  2806. NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
  2807. nesqp->nesqp_context->arp_index_vlan |=
  2808. cpu_to_le32(nes_arp_table(nesdev,
  2809. le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
  2810. NES_ARP_RESOLVE) << 16);
  2811. nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
  2812. jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
  2813. nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
  2814. nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
  2815. ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
  2816. nesqp->nesqp_context->ird_ord_sizes |=
  2817. cpu_to_le32((u32)cm_node->ord_size);
  2818. memset(&nes_quad, 0, sizeof(nes_quad));
  2819. nes_quad.DstIpAdrIndex =
  2820. cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
  2821. nes_quad.SrcIpadr = htonl(cm_node->rem_addr);
  2822. nes_quad.TcpPorts[0] = htons(cm_node->rem_port);
  2823. nes_quad.TcpPorts[1] = htons(cm_node->loc_port);
  2824. /* Produce hash key */
  2825. crc_value = get_crc_value(&nes_quad);
  2826. nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
  2827. nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
  2828. nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
  2829. nesqp->hte_index &= adapter->hte_index_mask;
  2830. nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
  2831. cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
  2832. nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
  2833. "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
  2834. "private data length=%u.\n", nesqp->hwqp.qp_id,
  2835. ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
  2836. ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
  2837. le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
  2838. le32_to_cpu(nesqp->nesqp_context->snd_nxt),
  2839. buff_len);
  2840. /* notify OF layer that accept event was successful */
  2841. cm_id->add_ref(cm_id);
  2842. nes_add_ref(&nesqp->ibqp);
  2843. cm_event.event = IW_CM_EVENT_ESTABLISHED;
  2844. cm_event.status = 0;
  2845. cm_event.provider_data = (void *)nesqp;
  2846. cm_event.local_addr = cm_id->m_local_addr;
  2847. cm_event.remote_addr = cm_id->m_remote_addr;
  2848. cm_event.private_data = NULL;
  2849. cm_event.private_data_len = 0;
  2850. cm_event.ird = cm_node->ird_size;
  2851. cm_event.ord = cm_node->ord_size;
  2852. ret = cm_id->event_handler(cm_id, &cm_event);
  2853. attr.qp_state = IB_QPS_RTS;
  2854. nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
  2855. if (cm_node->loopbackpartner) {
  2856. cm_node->loopbackpartner->mpa_frame_size =
  2857. nesqp->private_data_len;
  2858. /* copy entire MPA frame to our cm_node's frame */
  2859. memcpy(cm_node->loopbackpartner->mpa_frame_buf,
  2860. conn_param->private_data, conn_param->private_data_len);
  2861. create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
  2862. }
  2863. if (ret)
  2864. printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
  2865. "ret=%d\n", __func__, __LINE__, ret);
  2866. return 0;
  2867. }
  2868. /**
  2869. * nes_reject
  2870. */
  2871. int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
  2872. {
  2873. struct nes_cm_node *cm_node;
  2874. struct nes_cm_node *loopback;
  2875. struct nes_cm_core *cm_core;
  2876. u8 *start_buff;
  2877. atomic_inc(&cm_rejects);
  2878. cm_node = (struct nes_cm_node *)cm_id->provider_data;
  2879. loopback = cm_node->loopbackpartner;
  2880. cm_core = cm_node->cm_core;
  2881. cm_node->cm_id = cm_id;
  2882. if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
  2883. return -EINVAL;
  2884. if (loopback) {
  2885. memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
  2886. loopback->mpa_frame.priv_data_len = pdata_len;
  2887. loopback->mpa_frame_size = pdata_len;
  2888. } else {
  2889. start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
  2890. cm_node->mpa_frame_size = pdata_len;
  2891. memcpy(start_buff, pdata, pdata_len);
  2892. }
  2893. return cm_core->api->reject(cm_core, cm_node);
  2894. }
  2895. /**
  2896. * nes_connect
  2897. * setup and launch cm connect node
  2898. */
  2899. int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
  2900. {
  2901. struct ib_qp *ibqp;
  2902. struct nes_qp *nesqp;
  2903. struct nes_vnic *nesvnic;
  2904. struct nes_device *nesdev;
  2905. struct nes_cm_node *cm_node;
  2906. struct nes_cm_info cm_info;
  2907. int apbvt_set = 0;
  2908. struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
  2909. struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
  2910. if (cm_id->remote_addr.ss_family != AF_INET)
  2911. return -ENOSYS;
  2912. ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
  2913. if (!ibqp)
  2914. return -EINVAL;
  2915. nesqp = to_nesqp(ibqp);
  2916. if (!nesqp)
  2917. return -EINVAL;
  2918. nesvnic = to_nesvnic(nesqp->ibqp.device);
  2919. if (!nesvnic)
  2920. return -EINVAL;
  2921. nesdev = nesvnic->nesdev;
  2922. if (!nesdev)
  2923. return -EINVAL;
  2924. if (!laddr->sin_port || !raddr->sin_port)
  2925. return -EINVAL;
  2926. nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
  2927. "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
  2928. ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
  2929. ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
  2930. ntohs(laddr->sin_port));
  2931. atomic_inc(&cm_connects);
  2932. nesqp->active_conn = 1;
  2933. /* cache the cm_id in the qp */
  2934. nesqp->cm_id = cm_id;
  2935. cm_id->provider_data = nesqp;
  2936. nesqp->private_data_len = conn_param->private_data_len;
  2937. nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
  2938. nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
  2939. conn_param->private_data_len);
  2940. /* set up the connection params for the node */
  2941. cm_info.loc_addr = ntohl(laddr->sin_addr.s_addr);
  2942. cm_info.loc_port = ntohs(laddr->sin_port);
  2943. cm_info.rem_addr = ntohl(raddr->sin_addr.s_addr);
  2944. cm_info.rem_port = ntohs(raddr->sin_port);
  2945. cm_info.cm_id = cm_id;
  2946. cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
  2947. if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
  2948. nes_manage_apbvt(nesvnic, cm_info.loc_port,
  2949. PCI_FUNC(nesdev->pcidev->devfn),
  2950. NES_MANAGE_APBVT_ADD);
  2951. apbvt_set = 1;
  2952. }
  2953. cm_id->add_ref(cm_id);
  2954. /* create a connect CM node connection */
  2955. cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
  2956. conn_param->private_data_len, (void *)conn_param->private_data,
  2957. &cm_info);
  2958. if (!cm_node) {
  2959. if (apbvt_set)
  2960. nes_manage_apbvt(nesvnic, cm_info.loc_port,
  2961. PCI_FUNC(nesdev->pcidev->devfn),
  2962. NES_MANAGE_APBVT_DEL);
  2963. nes_debug(NES_DBG_NLMSG, "Delete loc_port = %04X\n",
  2964. cm_info.loc_port);
  2965. cm_id->rem_ref(cm_id);
  2966. return -ENOMEM;
  2967. }
  2968. record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
  2969. if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
  2970. cm_node->ord_size == 0)
  2971. cm_node->ord_size = 1;
  2972. cm_node->apbvt_set = apbvt_set;
  2973. cm_node->tos = cm_id->tos;
  2974. nesqp->cm_node = cm_node;
  2975. cm_node->nesqp = nesqp;
  2976. nes_add_ref(&nesqp->ibqp);
  2977. return 0;
  2978. }
  2979. /**
  2980. * nes_create_listen
  2981. */
  2982. int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
  2983. {
  2984. struct nes_vnic *nesvnic;
  2985. struct nes_cm_listener *cm_node;
  2986. struct nes_cm_info cm_info;
  2987. int err;
  2988. struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
  2989. nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
  2990. cm_id, ntohs(laddr->sin_port));
  2991. if (cm_id->m_local_addr.ss_family != AF_INET)
  2992. return -ENOSYS;
  2993. nesvnic = to_nesvnic(cm_id->device);
  2994. if (!nesvnic)
  2995. return -EINVAL;
  2996. nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
  2997. nesvnic, nesvnic->netdev, nesvnic->netdev->name);
  2998. nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
  2999. nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
  3000. /* setup listen params in our api call struct */
  3001. cm_info.loc_addr = ntohl(nesvnic->local_ipaddr);
  3002. cm_info.loc_port = ntohs(laddr->sin_port);
  3003. cm_info.backlog = backlog;
  3004. cm_info.cm_id = cm_id;
  3005. cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
  3006. cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
  3007. if (!cm_node) {
  3008. printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
  3009. __func__, __LINE__);
  3010. return -ENOMEM;
  3011. }
  3012. cm_id->provider_data = cm_node;
  3013. cm_node->tos = cm_id->tos;
  3014. if (!cm_node->reused_node) {
  3015. err = nes_manage_apbvt(nesvnic, cm_node->loc_port,
  3016. PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
  3017. NES_MANAGE_APBVT_ADD);
  3018. if (err) {
  3019. printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
  3020. err);
  3021. g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
  3022. return err;
  3023. }
  3024. atomic_inc(&cm_listens_created);
  3025. }
  3026. cm_id->add_ref(cm_id);
  3027. cm_id->provider_data = (void *)cm_node;
  3028. return 0;
  3029. }
  3030. /**
  3031. * nes_destroy_listen
  3032. */
  3033. int nes_destroy_listen(struct iw_cm_id *cm_id)
  3034. {
  3035. if (cm_id->provider_data)
  3036. g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
  3037. else
  3038. nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
  3039. cm_id->rem_ref(cm_id);
  3040. return 0;
  3041. }
  3042. /**
  3043. * nes_cm_recv
  3044. */
  3045. int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
  3046. {
  3047. int rc = 0;
  3048. cm_packets_received++;
  3049. if ((g_cm_core) && (g_cm_core->api))
  3050. rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
  3051. else
  3052. nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
  3053. " cm is not setup properly.\n");
  3054. return rc;
  3055. }
  3056. /**
  3057. * nes_cm_start
  3058. * Start and init a cm core module
  3059. */
  3060. int nes_cm_start(void)
  3061. {
  3062. nes_debug(NES_DBG_CM, "\n");
  3063. /* create the primary CM core, pass this handle to subsequent core inits */
  3064. g_cm_core = nes_cm_alloc_core();
  3065. if (g_cm_core)
  3066. return 0;
  3067. else
  3068. return -ENOMEM;
  3069. }
  3070. /**
  3071. * nes_cm_stop
  3072. * stop and dealloc all cm core instances
  3073. */
  3074. int nes_cm_stop(void)
  3075. {
  3076. g_cm_core->api->destroy_cm_core(g_cm_core);
  3077. return 0;
  3078. }
  3079. /**
  3080. * cm_event_connected
  3081. * handle a connected event, setup QPs and HW
  3082. */
  3083. static void cm_event_connected(struct nes_cm_event *event)
  3084. {
  3085. struct nes_qp *nesqp;
  3086. struct nes_vnic *nesvnic;
  3087. struct nes_device *nesdev;
  3088. struct nes_cm_node *cm_node;
  3089. struct nes_adapter *nesadapter;
  3090. struct ib_qp_attr attr;
  3091. struct iw_cm_id *cm_id;
  3092. struct iw_cm_event cm_event;
  3093. struct nes_v4_quad nes_quad;
  3094. u32 crc_value;
  3095. int ret;
  3096. struct sockaddr_in *laddr;
  3097. struct sockaddr_in *raddr;
  3098. struct sockaddr_in *cm_event_laddr;
  3099. /* get all our handles */
  3100. cm_node = event->cm_node;
  3101. cm_id = cm_node->cm_id;
  3102. nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
  3103. nesqp = (struct nes_qp *)cm_id->provider_data;
  3104. nesvnic = to_nesvnic(nesqp->ibqp.device);
  3105. nesdev = nesvnic->nesdev;
  3106. nesadapter = nesdev->nesadapter;
  3107. laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
  3108. raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
  3109. cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
  3110. if (nesqp->destroyed)
  3111. return;
  3112. atomic_inc(&cm_connecteds);
  3113. nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
  3114. " local port 0x%04X. jiffies = %lu.\n",
  3115. nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
  3116. ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
  3117. nes_cm_init_tsa_conn(nesqp, cm_node);
  3118. /* set the QP tsa context */
  3119. nesqp->nesqp_context->tcpPorts[0] =
  3120. cpu_to_le16(cm_node->loc_port);
  3121. nesqp->nesqp_context->tcpPorts[1] =
  3122. cpu_to_le16(cm_node->rem_port);
  3123. nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->rem_addr);
  3124. nesqp->nesqp_context->misc2 |= cpu_to_le32(
  3125. (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
  3126. NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
  3127. nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
  3128. nes_arp_table(nesdev,
  3129. le32_to_cpu(nesqp->nesqp_context->ip0),
  3130. NULL, NES_ARP_RESOLVE) << 16);
  3131. nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
  3132. jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
  3133. nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
  3134. nesqp->nesqp_context->ird_ord_sizes |=
  3135. cpu_to_le32((u32)1 <<
  3136. NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
  3137. nesqp->nesqp_context->ird_ord_sizes |=
  3138. cpu_to_le32((u32)cm_node->ord_size);
  3139. /* Adjust tail for not having a LSMM */
  3140. /*nesqp->hwqp.sq_tail = 1;*/
  3141. build_rdma0_msg(cm_node, &nesqp);
  3142. nes_write32(nesdev->regs + NES_WQE_ALLOC,
  3143. (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
  3144. memset(&nes_quad, 0, sizeof(nes_quad));
  3145. nes_quad.DstIpAdrIndex =
  3146. cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
  3147. nes_quad.SrcIpadr = htonl(cm_node->rem_addr);
  3148. nes_quad.TcpPorts[0] = htons(cm_node->rem_port);
  3149. nes_quad.TcpPorts[1] = htons(cm_node->loc_port);
  3150. /* Produce hash key */
  3151. crc_value = get_crc_value(&nes_quad);
  3152. nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
  3153. nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
  3154. nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
  3155. nesqp->hte_index &= nesadapter->hte_index_mask;
  3156. nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
  3157. nesqp->ietf_frame = &cm_node->mpa_frame;
  3158. nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
  3159. cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
  3160. /* notify OF layer we successfully created the requested connection */
  3161. cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
  3162. cm_event.status = 0;
  3163. cm_event.provider_data = cm_id->provider_data;
  3164. cm_event_laddr->sin_family = AF_INET;
  3165. cm_event_laddr->sin_port = laddr->sin_port;
  3166. cm_event.remote_addr = cm_id->m_remote_addr;
  3167. cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
  3168. cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
  3169. cm_event.ird = cm_node->ird_size;
  3170. cm_event.ord = cm_node->ord_size;
  3171. cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
  3172. ret = cm_id->event_handler(cm_id, &cm_event);
  3173. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  3174. if (ret)
  3175. printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
  3176. "ret=%d\n", __func__, __LINE__, ret);
  3177. attr.qp_state = IB_QPS_RTS;
  3178. nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
  3179. nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
  3180. "%lu\n", nesqp->hwqp.qp_id, jiffies);
  3181. return;
  3182. }
  3183. /**
  3184. * cm_event_connect_error
  3185. */
  3186. static void cm_event_connect_error(struct nes_cm_event *event)
  3187. {
  3188. struct nes_qp *nesqp;
  3189. struct iw_cm_id *cm_id;
  3190. struct iw_cm_event cm_event;
  3191. /* struct nes_cm_info cm_info; */
  3192. int ret;
  3193. if (!event->cm_node)
  3194. return;
  3195. cm_id = event->cm_node->cm_id;
  3196. if (!cm_id)
  3197. return;
  3198. nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
  3199. nesqp = cm_id->provider_data;
  3200. if (!nesqp)
  3201. return;
  3202. /* notify OF layer about this connection error event */
  3203. /* cm_id->rem_ref(cm_id); */
  3204. nesqp->cm_id = NULL;
  3205. cm_id->provider_data = NULL;
  3206. cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
  3207. cm_event.status = -ECONNRESET;
  3208. cm_event.provider_data = cm_id->provider_data;
  3209. cm_event.local_addr = cm_id->m_local_addr;
  3210. cm_event.remote_addr = cm_id->m_remote_addr;
  3211. cm_event.private_data = NULL;
  3212. cm_event.private_data_len = 0;
  3213. #ifdef CONFIG_INFINIBAND_NES_DEBUG
  3214. {
  3215. struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
  3216. &cm_event.local_addr;
  3217. struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
  3218. &cm_event.remote_addr;
  3219. nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
  3220. cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
  3221. }
  3222. #endif
  3223. ret = cm_id->event_handler(cm_id, &cm_event);
  3224. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  3225. if (ret)
  3226. printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
  3227. "ret=%d\n", __func__, __LINE__, ret);
  3228. cm_id->rem_ref(cm_id);
  3229. rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
  3230. return;
  3231. }
  3232. /**
  3233. * cm_event_reset
  3234. */
  3235. static void cm_event_reset(struct nes_cm_event *event)
  3236. {
  3237. struct nes_qp *nesqp;
  3238. struct iw_cm_id *cm_id;
  3239. struct iw_cm_event cm_event;
  3240. /* struct nes_cm_info cm_info; */
  3241. int ret;
  3242. if (!event->cm_node)
  3243. return;
  3244. if (!event->cm_node->cm_id)
  3245. return;
  3246. cm_id = event->cm_node->cm_id;
  3247. nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
  3248. nesqp = cm_id->provider_data;
  3249. if (!nesqp)
  3250. return;
  3251. nesqp->cm_id = NULL;
  3252. /* cm_id->provider_data = NULL; */
  3253. cm_event.event = IW_CM_EVENT_DISCONNECT;
  3254. cm_event.status = -ECONNRESET;
  3255. cm_event.provider_data = cm_id->provider_data;
  3256. cm_event.local_addr = cm_id->m_local_addr;
  3257. cm_event.remote_addr = cm_id->m_remote_addr;
  3258. cm_event.private_data = NULL;
  3259. cm_event.private_data_len = 0;
  3260. cm_id->add_ref(cm_id);
  3261. ret = cm_id->event_handler(cm_id, &cm_event);
  3262. atomic_inc(&cm_closes);
  3263. cm_event.event = IW_CM_EVENT_CLOSE;
  3264. cm_event.status = 0;
  3265. cm_event.provider_data = cm_id->provider_data;
  3266. cm_event.local_addr = cm_id->m_local_addr;
  3267. cm_event.remote_addr = cm_id->m_remote_addr;
  3268. cm_event.private_data = NULL;
  3269. cm_event.private_data_len = 0;
  3270. nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
  3271. ret = cm_id->event_handler(cm_id, &cm_event);
  3272. nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
  3273. /* notify OF layer about this connection error event */
  3274. cm_id->rem_ref(cm_id);
  3275. return;
  3276. }
  3277. /**
  3278. * cm_event_mpa_req
  3279. */
  3280. static void cm_event_mpa_req(struct nes_cm_event *event)
  3281. {
  3282. struct iw_cm_id *cm_id;
  3283. struct iw_cm_event cm_event;
  3284. int ret;
  3285. struct nes_cm_node *cm_node;
  3286. struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
  3287. &cm_event.local_addr;
  3288. struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
  3289. &cm_event.remote_addr;
  3290. cm_node = event->cm_node;
  3291. if (!cm_node)
  3292. return;
  3293. cm_id = cm_node->cm_id;
  3294. atomic_inc(&cm_connect_reqs);
  3295. nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
  3296. cm_node, cm_id, jiffies);
  3297. cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
  3298. cm_event.status = 0;
  3299. cm_event.provider_data = (void *)cm_node;
  3300. cm_event_laddr->sin_family = AF_INET;
  3301. cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
  3302. cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
  3303. cm_event_raddr->sin_family = AF_INET;
  3304. cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
  3305. cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
  3306. cm_event.private_data = cm_node->mpa_frame_buf;
  3307. cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
  3308. if (cm_node->mpa_frame_rev == IETF_MPA_V1) {
  3309. cm_event.ird = NES_MAX_IRD;
  3310. cm_event.ord = NES_MAX_ORD;
  3311. } else {
  3312. cm_event.ird = cm_node->ird_size;
  3313. cm_event.ord = cm_node->ord_size;
  3314. }
  3315. ret = cm_id->event_handler(cm_id, &cm_event);
  3316. if (ret)
  3317. printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
  3318. __func__, __LINE__, ret);
  3319. return;
  3320. }
  3321. static void cm_event_mpa_reject(struct nes_cm_event *event)
  3322. {
  3323. struct iw_cm_id *cm_id;
  3324. struct iw_cm_event cm_event;
  3325. struct nes_cm_node *cm_node;
  3326. int ret;
  3327. struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
  3328. &cm_event.local_addr;
  3329. struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
  3330. &cm_event.remote_addr;
  3331. cm_node = event->cm_node;
  3332. if (!cm_node)
  3333. return;
  3334. cm_id = cm_node->cm_id;
  3335. atomic_inc(&cm_connect_reqs);
  3336. nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
  3337. cm_node, cm_id, jiffies);
  3338. cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
  3339. cm_event.status = -ECONNREFUSED;
  3340. cm_event.provider_data = cm_id->provider_data;
  3341. cm_event_laddr->sin_family = AF_INET;
  3342. cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
  3343. cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
  3344. cm_event_raddr->sin_family = AF_INET;
  3345. cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
  3346. cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
  3347. cm_event.private_data = cm_node->mpa_frame_buf;
  3348. cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
  3349. nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
  3350. "remove_addr=%08x\n",
  3351. cm_event_laddr->sin_addr.s_addr,
  3352. cm_event_raddr->sin_addr.s_addr);
  3353. ret = cm_id->event_handler(cm_id, &cm_event);
  3354. if (ret)
  3355. printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
  3356. __func__, __LINE__, ret);
  3357. return;
  3358. }
  3359. static void nes_cm_event_handler(struct work_struct *);
  3360. /**
  3361. * nes_cm_post_event
  3362. * post an event to the cm event handler
  3363. */
  3364. static int nes_cm_post_event(struct nes_cm_event *event)
  3365. {
  3366. atomic_inc(&event->cm_node->cm_core->events_posted);
  3367. add_ref_cm_node(event->cm_node);
  3368. event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
  3369. INIT_WORK(&event->event_work, nes_cm_event_handler);
  3370. nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
  3371. event->cm_node, event);
  3372. queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
  3373. nes_debug(NES_DBG_CM, "Exit\n");
  3374. return 0;
  3375. }
  3376. /**
  3377. * nes_cm_event_handler
  3378. * worker function to handle cm events
  3379. * will free instance of nes_cm_event
  3380. */
  3381. static void nes_cm_event_handler(struct work_struct *work)
  3382. {
  3383. struct nes_cm_event *event = container_of(work, struct nes_cm_event,
  3384. event_work);
  3385. struct nes_cm_core *cm_core;
  3386. if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
  3387. return;
  3388. cm_core = event->cm_node->cm_core;
  3389. nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
  3390. event, event->type, atomic_read(&cm_core->events_posted));
  3391. switch (event->type) {
  3392. case NES_CM_EVENT_MPA_REQ:
  3393. cm_event_mpa_req(event);
  3394. nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
  3395. event->cm_node);
  3396. break;
  3397. case NES_CM_EVENT_RESET:
  3398. nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
  3399. event->cm_node);
  3400. cm_event_reset(event);
  3401. break;
  3402. case NES_CM_EVENT_CONNECTED:
  3403. if ((!event->cm_node->cm_id) ||
  3404. (event->cm_node->state != NES_CM_STATE_TSA))
  3405. break;
  3406. cm_event_connected(event);
  3407. nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
  3408. break;
  3409. case NES_CM_EVENT_MPA_REJECT:
  3410. if ((!event->cm_node->cm_id) ||
  3411. (event->cm_node->state == NES_CM_STATE_TSA))
  3412. break;
  3413. cm_event_mpa_reject(event);
  3414. nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
  3415. break;
  3416. case NES_CM_EVENT_ABORTED:
  3417. if ((!event->cm_node->cm_id) ||
  3418. (event->cm_node->state == NES_CM_STATE_TSA))
  3419. break;
  3420. cm_event_connect_error(event);
  3421. nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
  3422. break;
  3423. case NES_CM_EVENT_DROPPED_PKT:
  3424. nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
  3425. break;
  3426. default:
  3427. nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
  3428. break;
  3429. }
  3430. atomic_dec(&cm_core->events_posted);
  3431. event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
  3432. rem_ref_cm_node(cm_core, event->cm_node);
  3433. kfree(event);
  3434. return;
  3435. }