nes_cm.c 107 KB

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