cm.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876
  1. /*
  2. * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  4. * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
  5. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/completion.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/device.h>
  38. #include <linux/err.h>
  39. #include <linux/idr.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/random.h>
  42. #include <linux/rbtree.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/slab.h>
  45. #include <linux/sysfs.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/kdev_t.h>
  48. #include <rdma/ib_cache.h>
  49. #include <rdma/ib_cm.h>
  50. #include "cm_msgs.h"
  51. MODULE_AUTHOR("Sean Hefty");
  52. MODULE_DESCRIPTION("InfiniBand CM");
  53. MODULE_LICENSE("Dual BSD/GPL");
  54. static void cm_add_one(struct ib_device *device);
  55. static void cm_remove_one(struct ib_device *device);
  56. static struct ib_client cm_client = {
  57. .name = "cm",
  58. .add = cm_add_one,
  59. .remove = cm_remove_one
  60. };
  61. static struct ib_cm {
  62. spinlock_t lock;
  63. struct list_head device_list;
  64. rwlock_t device_lock;
  65. struct rb_root listen_service_table;
  66. u64 listen_service_id;
  67. /* struct rb_root peer_service_table; todo: fix peer to peer */
  68. struct rb_root remote_qp_table;
  69. struct rb_root remote_id_table;
  70. struct rb_root remote_sidr_table;
  71. struct idr local_id_table;
  72. __be32 random_id_operand;
  73. struct list_head timewait_list;
  74. struct workqueue_struct *wq;
  75. } cm;
  76. /* Counter indexes ordered by attribute ID */
  77. enum {
  78. CM_REQ_COUNTER,
  79. CM_MRA_COUNTER,
  80. CM_REJ_COUNTER,
  81. CM_REP_COUNTER,
  82. CM_RTU_COUNTER,
  83. CM_DREQ_COUNTER,
  84. CM_DREP_COUNTER,
  85. CM_SIDR_REQ_COUNTER,
  86. CM_SIDR_REP_COUNTER,
  87. CM_LAP_COUNTER,
  88. CM_APR_COUNTER,
  89. CM_ATTR_COUNT,
  90. CM_ATTR_ID_OFFSET = 0x0010,
  91. };
  92. enum {
  93. CM_XMIT,
  94. CM_XMIT_RETRIES,
  95. CM_RECV,
  96. CM_RECV_DUPLICATES,
  97. CM_COUNTER_GROUPS
  98. };
  99. static char const counter_group_names[CM_COUNTER_GROUPS]
  100. [sizeof("cm_rx_duplicates")] = {
  101. "cm_tx_msgs", "cm_tx_retries",
  102. "cm_rx_msgs", "cm_rx_duplicates"
  103. };
  104. struct cm_counter_group {
  105. struct kobject obj;
  106. atomic_long_t counter[CM_ATTR_COUNT];
  107. };
  108. struct cm_counter_attribute {
  109. struct attribute attr;
  110. int index;
  111. };
  112. #define CM_COUNTER_ATTR(_name, _index) \
  113. struct cm_counter_attribute cm_##_name##_counter_attr = { \
  114. .attr = { .name = __stringify(_name), .mode = 0444 }, \
  115. .index = _index \
  116. }
  117. static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
  118. static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
  119. static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
  120. static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
  121. static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
  122. static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
  123. static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
  124. static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
  125. static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
  126. static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
  127. static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
  128. static struct attribute *cm_counter_default_attrs[] = {
  129. &cm_req_counter_attr.attr,
  130. &cm_mra_counter_attr.attr,
  131. &cm_rej_counter_attr.attr,
  132. &cm_rep_counter_attr.attr,
  133. &cm_rtu_counter_attr.attr,
  134. &cm_dreq_counter_attr.attr,
  135. &cm_drep_counter_attr.attr,
  136. &cm_sidr_req_counter_attr.attr,
  137. &cm_sidr_rep_counter_attr.attr,
  138. &cm_lap_counter_attr.attr,
  139. &cm_apr_counter_attr.attr,
  140. NULL
  141. };
  142. struct cm_port {
  143. struct cm_device *cm_dev;
  144. struct ib_mad_agent *mad_agent;
  145. struct kobject port_obj;
  146. u8 port_num;
  147. struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
  148. };
  149. struct cm_device {
  150. struct list_head list;
  151. struct ib_device *ib_device;
  152. struct device *device;
  153. u8 ack_delay;
  154. struct cm_port *port[0];
  155. };
  156. struct cm_av {
  157. struct cm_port *port;
  158. union ib_gid dgid;
  159. struct ib_ah_attr ah_attr;
  160. u16 pkey_index;
  161. u8 timeout;
  162. };
  163. struct cm_work {
  164. struct delayed_work work;
  165. struct list_head list;
  166. struct cm_port *port;
  167. struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
  168. __be32 local_id; /* Established / timewait */
  169. __be32 remote_id;
  170. struct ib_cm_event cm_event;
  171. struct ib_sa_path_rec path[0];
  172. };
  173. struct cm_timewait_info {
  174. struct cm_work work; /* Must be first. */
  175. struct list_head list;
  176. struct rb_node remote_qp_node;
  177. struct rb_node remote_id_node;
  178. __be64 remote_ca_guid;
  179. __be32 remote_qpn;
  180. u8 inserted_remote_qp;
  181. u8 inserted_remote_id;
  182. };
  183. struct cm_id_private {
  184. struct ib_cm_id id;
  185. struct rb_node service_node;
  186. struct rb_node sidr_id_node;
  187. spinlock_t lock; /* Do not acquire inside cm.lock */
  188. struct completion comp;
  189. atomic_t refcount;
  190. struct ib_mad_send_buf *msg;
  191. struct cm_timewait_info *timewait_info;
  192. /* todo: use alternate port on send failure */
  193. struct cm_av av;
  194. struct cm_av alt_av;
  195. struct ib_cm_compare_data *compare_data;
  196. void *private_data;
  197. __be64 tid;
  198. __be32 local_qpn;
  199. __be32 remote_qpn;
  200. enum ib_qp_type qp_type;
  201. __be32 sq_psn;
  202. __be32 rq_psn;
  203. int timeout_ms;
  204. enum ib_mtu path_mtu;
  205. __be16 pkey;
  206. u8 private_data_len;
  207. u8 max_cm_retries;
  208. u8 peer_to_peer;
  209. u8 responder_resources;
  210. u8 initiator_depth;
  211. u8 retry_count;
  212. u8 rnr_retry_count;
  213. u8 service_timeout;
  214. u8 target_ack_delay;
  215. struct list_head work_list;
  216. atomic_t work_count;
  217. };
  218. static void cm_work_handler(struct work_struct *work);
  219. static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
  220. {
  221. if (atomic_dec_and_test(&cm_id_priv->refcount))
  222. complete(&cm_id_priv->comp);
  223. }
  224. static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
  225. struct ib_mad_send_buf **msg)
  226. {
  227. struct ib_mad_agent *mad_agent;
  228. struct ib_mad_send_buf *m;
  229. struct ib_ah *ah;
  230. mad_agent = cm_id_priv->av.port->mad_agent;
  231. ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
  232. if (IS_ERR(ah))
  233. return PTR_ERR(ah);
  234. m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
  235. cm_id_priv->av.pkey_index,
  236. 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  237. GFP_ATOMIC);
  238. if (IS_ERR(m)) {
  239. ib_destroy_ah(ah);
  240. return PTR_ERR(m);
  241. }
  242. /* Timeout set by caller if response is expected. */
  243. m->ah = ah;
  244. m->retries = cm_id_priv->max_cm_retries;
  245. atomic_inc(&cm_id_priv->refcount);
  246. m->context[0] = cm_id_priv;
  247. *msg = m;
  248. return 0;
  249. }
  250. static int cm_alloc_response_msg(struct cm_port *port,
  251. struct ib_mad_recv_wc *mad_recv_wc,
  252. struct ib_mad_send_buf **msg)
  253. {
  254. struct ib_mad_send_buf *m;
  255. struct ib_ah *ah;
  256. ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
  257. mad_recv_wc->recv_buf.grh, port->port_num);
  258. if (IS_ERR(ah))
  259. return PTR_ERR(ah);
  260. m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
  261. 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  262. GFP_ATOMIC);
  263. if (IS_ERR(m)) {
  264. ib_destroy_ah(ah);
  265. return PTR_ERR(m);
  266. }
  267. m->ah = ah;
  268. *msg = m;
  269. return 0;
  270. }
  271. static void cm_free_msg(struct ib_mad_send_buf *msg)
  272. {
  273. ib_destroy_ah(msg->ah);
  274. if (msg->context[0])
  275. cm_deref_id(msg->context[0]);
  276. ib_free_send_mad(msg);
  277. }
  278. static void * cm_copy_private_data(const void *private_data,
  279. u8 private_data_len)
  280. {
  281. void *data;
  282. if (!private_data || !private_data_len)
  283. return NULL;
  284. data = kmemdup(private_data, private_data_len, GFP_KERNEL);
  285. if (!data)
  286. return ERR_PTR(-ENOMEM);
  287. return data;
  288. }
  289. static void cm_set_private_data(struct cm_id_private *cm_id_priv,
  290. void *private_data, u8 private_data_len)
  291. {
  292. if (cm_id_priv->private_data && cm_id_priv->private_data_len)
  293. kfree(cm_id_priv->private_data);
  294. cm_id_priv->private_data = private_data;
  295. cm_id_priv->private_data_len = private_data_len;
  296. }
  297. static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
  298. struct ib_grh *grh, struct cm_av *av)
  299. {
  300. av->port = port;
  301. av->pkey_index = wc->pkey_index;
  302. ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
  303. grh, &av->ah_attr);
  304. }
  305. static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
  306. {
  307. struct cm_device *cm_dev;
  308. struct cm_port *port = NULL;
  309. unsigned long flags;
  310. int ret;
  311. u8 p;
  312. read_lock_irqsave(&cm.device_lock, flags);
  313. list_for_each_entry(cm_dev, &cm.device_list, list) {
  314. if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
  315. &p, NULL)) {
  316. port = cm_dev->port[p-1];
  317. break;
  318. }
  319. }
  320. read_unlock_irqrestore(&cm.device_lock, flags);
  321. if (!port)
  322. return -EINVAL;
  323. ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
  324. be16_to_cpu(path->pkey), &av->pkey_index);
  325. if (ret)
  326. return ret;
  327. av->port = port;
  328. ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
  329. &av->ah_attr);
  330. av->timeout = path->packet_life_time + 1;
  331. return 0;
  332. }
  333. static int cm_alloc_id(struct cm_id_private *cm_id_priv)
  334. {
  335. unsigned long flags;
  336. int ret, id;
  337. static int next_id;
  338. do {
  339. spin_lock_irqsave(&cm.lock, flags);
  340. ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
  341. next_id, &id);
  342. if (!ret)
  343. next_id = ((unsigned) id + 1) & MAX_ID_MASK;
  344. spin_unlock_irqrestore(&cm.lock, flags);
  345. } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
  346. cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
  347. return ret;
  348. }
  349. static void cm_free_id(__be32 local_id)
  350. {
  351. spin_lock_irq(&cm.lock);
  352. idr_remove(&cm.local_id_table,
  353. (__force int) (local_id ^ cm.random_id_operand));
  354. spin_unlock_irq(&cm.lock);
  355. }
  356. static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
  357. {
  358. struct cm_id_private *cm_id_priv;
  359. cm_id_priv = idr_find(&cm.local_id_table,
  360. (__force int) (local_id ^ cm.random_id_operand));
  361. if (cm_id_priv) {
  362. if (cm_id_priv->id.remote_id == remote_id)
  363. atomic_inc(&cm_id_priv->refcount);
  364. else
  365. cm_id_priv = NULL;
  366. }
  367. return cm_id_priv;
  368. }
  369. static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
  370. {
  371. struct cm_id_private *cm_id_priv;
  372. spin_lock_irq(&cm.lock);
  373. cm_id_priv = cm_get_id(local_id, remote_id);
  374. spin_unlock_irq(&cm.lock);
  375. return cm_id_priv;
  376. }
  377. static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
  378. {
  379. int i;
  380. for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
  381. ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
  382. ((unsigned long *) mask)[i];
  383. }
  384. static int cm_compare_data(struct ib_cm_compare_data *src_data,
  385. struct ib_cm_compare_data *dst_data)
  386. {
  387. u8 src[IB_CM_COMPARE_SIZE];
  388. u8 dst[IB_CM_COMPARE_SIZE];
  389. if (!src_data || !dst_data)
  390. return 0;
  391. cm_mask_copy(src, src_data->data, dst_data->mask);
  392. cm_mask_copy(dst, dst_data->data, src_data->mask);
  393. return memcmp(src, dst, IB_CM_COMPARE_SIZE);
  394. }
  395. static int cm_compare_private_data(u8 *private_data,
  396. struct ib_cm_compare_data *dst_data)
  397. {
  398. u8 src[IB_CM_COMPARE_SIZE];
  399. if (!dst_data)
  400. return 0;
  401. cm_mask_copy(src, private_data, dst_data->mask);
  402. return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
  403. }
  404. /*
  405. * Trivial helpers to strip endian annotation and compare; the
  406. * endianness doesn't actually matter since we just need a stable
  407. * order for the RB tree.
  408. */
  409. static int be32_lt(__be32 a, __be32 b)
  410. {
  411. return (__force u32) a < (__force u32) b;
  412. }
  413. static int be32_gt(__be32 a, __be32 b)
  414. {
  415. return (__force u32) a > (__force u32) b;
  416. }
  417. static int be64_lt(__be64 a, __be64 b)
  418. {
  419. return (__force u64) a < (__force u64) b;
  420. }
  421. static int be64_gt(__be64 a, __be64 b)
  422. {
  423. return (__force u64) a > (__force u64) b;
  424. }
  425. static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
  426. {
  427. struct rb_node **link = &cm.listen_service_table.rb_node;
  428. struct rb_node *parent = NULL;
  429. struct cm_id_private *cur_cm_id_priv;
  430. __be64 service_id = cm_id_priv->id.service_id;
  431. __be64 service_mask = cm_id_priv->id.service_mask;
  432. int data_cmp;
  433. while (*link) {
  434. parent = *link;
  435. cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
  436. service_node);
  437. data_cmp = cm_compare_data(cm_id_priv->compare_data,
  438. cur_cm_id_priv->compare_data);
  439. if ((cur_cm_id_priv->id.service_mask & service_id) ==
  440. (service_mask & cur_cm_id_priv->id.service_id) &&
  441. (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
  442. !data_cmp)
  443. return cur_cm_id_priv;
  444. if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
  445. link = &(*link)->rb_left;
  446. else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
  447. link = &(*link)->rb_right;
  448. else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
  449. link = &(*link)->rb_left;
  450. else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
  451. link = &(*link)->rb_right;
  452. else if (data_cmp < 0)
  453. link = &(*link)->rb_left;
  454. else
  455. link = &(*link)->rb_right;
  456. }
  457. rb_link_node(&cm_id_priv->service_node, parent, link);
  458. rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
  459. return NULL;
  460. }
  461. static struct cm_id_private * cm_find_listen(struct ib_device *device,
  462. __be64 service_id,
  463. u8 *private_data)
  464. {
  465. struct rb_node *node = cm.listen_service_table.rb_node;
  466. struct cm_id_private *cm_id_priv;
  467. int data_cmp;
  468. while (node) {
  469. cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
  470. data_cmp = cm_compare_private_data(private_data,
  471. cm_id_priv->compare_data);
  472. if ((cm_id_priv->id.service_mask & service_id) ==
  473. cm_id_priv->id.service_id &&
  474. (cm_id_priv->id.device == device) && !data_cmp)
  475. return cm_id_priv;
  476. if (device < cm_id_priv->id.device)
  477. node = node->rb_left;
  478. else if (device > cm_id_priv->id.device)
  479. node = node->rb_right;
  480. else if (be64_lt(service_id, cm_id_priv->id.service_id))
  481. node = node->rb_left;
  482. else if (be64_gt(service_id, cm_id_priv->id.service_id))
  483. node = node->rb_right;
  484. else if (data_cmp < 0)
  485. node = node->rb_left;
  486. else
  487. node = node->rb_right;
  488. }
  489. return NULL;
  490. }
  491. static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
  492. *timewait_info)
  493. {
  494. struct rb_node **link = &cm.remote_id_table.rb_node;
  495. struct rb_node *parent = NULL;
  496. struct cm_timewait_info *cur_timewait_info;
  497. __be64 remote_ca_guid = timewait_info->remote_ca_guid;
  498. __be32 remote_id = timewait_info->work.remote_id;
  499. while (*link) {
  500. parent = *link;
  501. cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
  502. remote_id_node);
  503. if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
  504. link = &(*link)->rb_left;
  505. else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
  506. link = &(*link)->rb_right;
  507. else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  508. link = &(*link)->rb_left;
  509. else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  510. link = &(*link)->rb_right;
  511. else
  512. return cur_timewait_info;
  513. }
  514. timewait_info->inserted_remote_id = 1;
  515. rb_link_node(&timewait_info->remote_id_node, parent, link);
  516. rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
  517. return NULL;
  518. }
  519. static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
  520. __be32 remote_id)
  521. {
  522. struct rb_node *node = cm.remote_id_table.rb_node;
  523. struct cm_timewait_info *timewait_info;
  524. while (node) {
  525. timewait_info = rb_entry(node, struct cm_timewait_info,
  526. remote_id_node);
  527. if (be32_lt(remote_id, timewait_info->work.remote_id))
  528. node = node->rb_left;
  529. else if (be32_gt(remote_id, timewait_info->work.remote_id))
  530. node = node->rb_right;
  531. else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
  532. node = node->rb_left;
  533. else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
  534. node = node->rb_right;
  535. else
  536. return timewait_info;
  537. }
  538. return NULL;
  539. }
  540. static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
  541. *timewait_info)
  542. {
  543. struct rb_node **link = &cm.remote_qp_table.rb_node;
  544. struct rb_node *parent = NULL;
  545. struct cm_timewait_info *cur_timewait_info;
  546. __be64 remote_ca_guid = timewait_info->remote_ca_guid;
  547. __be32 remote_qpn = timewait_info->remote_qpn;
  548. while (*link) {
  549. parent = *link;
  550. cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
  551. remote_qp_node);
  552. if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
  553. link = &(*link)->rb_left;
  554. else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
  555. link = &(*link)->rb_right;
  556. else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  557. link = &(*link)->rb_left;
  558. else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
  559. link = &(*link)->rb_right;
  560. else
  561. return cur_timewait_info;
  562. }
  563. timewait_info->inserted_remote_qp = 1;
  564. rb_link_node(&timewait_info->remote_qp_node, parent, link);
  565. rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
  566. return NULL;
  567. }
  568. static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
  569. *cm_id_priv)
  570. {
  571. struct rb_node **link = &cm.remote_sidr_table.rb_node;
  572. struct rb_node *parent = NULL;
  573. struct cm_id_private *cur_cm_id_priv;
  574. union ib_gid *port_gid = &cm_id_priv->av.dgid;
  575. __be32 remote_id = cm_id_priv->id.remote_id;
  576. while (*link) {
  577. parent = *link;
  578. cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
  579. sidr_id_node);
  580. if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
  581. link = &(*link)->rb_left;
  582. else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
  583. link = &(*link)->rb_right;
  584. else {
  585. int cmp;
  586. cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
  587. sizeof *port_gid);
  588. if (cmp < 0)
  589. link = &(*link)->rb_left;
  590. else if (cmp > 0)
  591. link = &(*link)->rb_right;
  592. else
  593. return cur_cm_id_priv;
  594. }
  595. }
  596. rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
  597. rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  598. return NULL;
  599. }
  600. static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
  601. enum ib_cm_sidr_status status)
  602. {
  603. struct ib_cm_sidr_rep_param param;
  604. memset(&param, 0, sizeof param);
  605. param.status = status;
  606. ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
  607. }
  608. struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
  609. ib_cm_handler cm_handler,
  610. void *context)
  611. {
  612. struct cm_id_private *cm_id_priv;
  613. int ret;
  614. cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
  615. if (!cm_id_priv)
  616. return ERR_PTR(-ENOMEM);
  617. cm_id_priv->id.state = IB_CM_IDLE;
  618. cm_id_priv->id.device = device;
  619. cm_id_priv->id.cm_handler = cm_handler;
  620. cm_id_priv->id.context = context;
  621. cm_id_priv->id.remote_cm_qpn = 1;
  622. ret = cm_alloc_id(cm_id_priv);
  623. if (ret)
  624. goto error;
  625. spin_lock_init(&cm_id_priv->lock);
  626. init_completion(&cm_id_priv->comp);
  627. INIT_LIST_HEAD(&cm_id_priv->work_list);
  628. atomic_set(&cm_id_priv->work_count, -1);
  629. atomic_set(&cm_id_priv->refcount, 1);
  630. return &cm_id_priv->id;
  631. error:
  632. kfree(cm_id_priv);
  633. return ERR_PTR(-ENOMEM);
  634. }
  635. EXPORT_SYMBOL(ib_create_cm_id);
  636. static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
  637. {
  638. struct cm_work *work;
  639. if (list_empty(&cm_id_priv->work_list))
  640. return NULL;
  641. work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
  642. list_del(&work->list);
  643. return work;
  644. }
  645. static void cm_free_work(struct cm_work *work)
  646. {
  647. if (work->mad_recv_wc)
  648. ib_free_recv_mad(work->mad_recv_wc);
  649. kfree(work);
  650. }
  651. static inline int cm_convert_to_ms(int iba_time)
  652. {
  653. /* approximate conversion to ms from 4.096us x 2^iba_time */
  654. return 1 << max(iba_time - 8, 0);
  655. }
  656. /*
  657. * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
  658. * Because of how ack_timeout is stored, adding one doubles the timeout.
  659. * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
  660. * increment it (round up) only if the other is within 50%.
  661. */
  662. static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
  663. {
  664. int ack_timeout = packet_life_time + 1;
  665. if (ack_timeout >= ca_ack_delay)
  666. ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
  667. else
  668. ack_timeout = ca_ack_delay +
  669. (ack_timeout >= (ca_ack_delay - 1));
  670. return min(31, ack_timeout);
  671. }
  672. static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
  673. {
  674. if (timewait_info->inserted_remote_id) {
  675. rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
  676. timewait_info->inserted_remote_id = 0;
  677. }
  678. if (timewait_info->inserted_remote_qp) {
  679. rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
  680. timewait_info->inserted_remote_qp = 0;
  681. }
  682. }
  683. static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
  684. {
  685. struct cm_timewait_info *timewait_info;
  686. timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
  687. if (!timewait_info)
  688. return ERR_PTR(-ENOMEM);
  689. timewait_info->work.local_id = local_id;
  690. INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
  691. timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
  692. return timewait_info;
  693. }
  694. static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
  695. {
  696. int wait_time;
  697. unsigned long flags;
  698. spin_lock_irqsave(&cm.lock, flags);
  699. cm_cleanup_timewait(cm_id_priv->timewait_info);
  700. list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
  701. spin_unlock_irqrestore(&cm.lock, flags);
  702. /*
  703. * The cm_id could be destroyed by the user before we exit timewait.
  704. * To protect against this, we search for the cm_id after exiting
  705. * timewait before notifying the user that we've exited timewait.
  706. */
  707. cm_id_priv->id.state = IB_CM_TIMEWAIT;
  708. wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
  709. queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
  710. msecs_to_jiffies(wait_time));
  711. cm_id_priv->timewait_info = NULL;
  712. }
  713. static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
  714. {
  715. unsigned long flags;
  716. cm_id_priv->id.state = IB_CM_IDLE;
  717. if (cm_id_priv->timewait_info) {
  718. spin_lock_irqsave(&cm.lock, flags);
  719. cm_cleanup_timewait(cm_id_priv->timewait_info);
  720. spin_unlock_irqrestore(&cm.lock, flags);
  721. kfree(cm_id_priv->timewait_info);
  722. cm_id_priv->timewait_info = NULL;
  723. }
  724. }
  725. static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
  726. {
  727. struct cm_id_private *cm_id_priv;
  728. struct cm_work *work;
  729. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  730. retest:
  731. spin_lock_irq(&cm_id_priv->lock);
  732. switch (cm_id->state) {
  733. case IB_CM_LISTEN:
  734. cm_id->state = IB_CM_IDLE;
  735. spin_unlock_irq(&cm_id_priv->lock);
  736. spin_lock_irq(&cm.lock);
  737. rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
  738. spin_unlock_irq(&cm.lock);
  739. break;
  740. case IB_CM_SIDR_REQ_SENT:
  741. cm_id->state = IB_CM_IDLE;
  742. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  743. spin_unlock_irq(&cm_id_priv->lock);
  744. break;
  745. case IB_CM_SIDR_REQ_RCVD:
  746. spin_unlock_irq(&cm_id_priv->lock);
  747. cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
  748. break;
  749. case IB_CM_REQ_SENT:
  750. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  751. spin_unlock_irq(&cm_id_priv->lock);
  752. ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
  753. &cm_id_priv->id.device->node_guid,
  754. sizeof cm_id_priv->id.device->node_guid,
  755. NULL, 0);
  756. break;
  757. case IB_CM_REQ_RCVD:
  758. if (err == -ENOMEM) {
  759. /* Do not reject to allow future retries. */
  760. cm_reset_to_idle(cm_id_priv);
  761. spin_unlock_irq(&cm_id_priv->lock);
  762. } else {
  763. spin_unlock_irq(&cm_id_priv->lock);
  764. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
  765. NULL, 0, NULL, 0);
  766. }
  767. break;
  768. case IB_CM_MRA_REQ_RCVD:
  769. case IB_CM_REP_SENT:
  770. case IB_CM_MRA_REP_RCVD:
  771. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  772. /* Fall through */
  773. case IB_CM_MRA_REQ_SENT:
  774. case IB_CM_REP_RCVD:
  775. case IB_CM_MRA_REP_SENT:
  776. spin_unlock_irq(&cm_id_priv->lock);
  777. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
  778. NULL, 0, NULL, 0);
  779. break;
  780. case IB_CM_ESTABLISHED:
  781. spin_unlock_irq(&cm_id_priv->lock);
  782. ib_send_cm_dreq(cm_id, NULL, 0);
  783. goto retest;
  784. case IB_CM_DREQ_SENT:
  785. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  786. cm_enter_timewait(cm_id_priv);
  787. spin_unlock_irq(&cm_id_priv->lock);
  788. break;
  789. case IB_CM_DREQ_RCVD:
  790. spin_unlock_irq(&cm_id_priv->lock);
  791. ib_send_cm_drep(cm_id, NULL, 0);
  792. break;
  793. default:
  794. spin_unlock_irq(&cm_id_priv->lock);
  795. break;
  796. }
  797. cm_free_id(cm_id->local_id);
  798. cm_deref_id(cm_id_priv);
  799. wait_for_completion(&cm_id_priv->comp);
  800. while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
  801. cm_free_work(work);
  802. kfree(cm_id_priv->compare_data);
  803. kfree(cm_id_priv->private_data);
  804. kfree(cm_id_priv);
  805. }
  806. void ib_destroy_cm_id(struct ib_cm_id *cm_id)
  807. {
  808. cm_destroy_id(cm_id, 0);
  809. }
  810. EXPORT_SYMBOL(ib_destroy_cm_id);
  811. int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
  812. struct ib_cm_compare_data *compare_data)
  813. {
  814. struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
  815. unsigned long flags;
  816. int ret = 0;
  817. service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
  818. service_id &= service_mask;
  819. if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
  820. (service_id != IB_CM_ASSIGN_SERVICE_ID))
  821. return -EINVAL;
  822. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  823. if (cm_id->state != IB_CM_IDLE)
  824. return -EINVAL;
  825. if (compare_data) {
  826. cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
  827. GFP_KERNEL);
  828. if (!cm_id_priv->compare_data)
  829. return -ENOMEM;
  830. cm_mask_copy(cm_id_priv->compare_data->data,
  831. compare_data->data, compare_data->mask);
  832. memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
  833. IB_CM_COMPARE_SIZE);
  834. }
  835. cm_id->state = IB_CM_LISTEN;
  836. spin_lock_irqsave(&cm.lock, flags);
  837. if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
  838. cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
  839. cm_id->service_mask = ~cpu_to_be64(0);
  840. } else {
  841. cm_id->service_id = service_id;
  842. cm_id->service_mask = service_mask;
  843. }
  844. cur_cm_id_priv = cm_insert_listen(cm_id_priv);
  845. spin_unlock_irqrestore(&cm.lock, flags);
  846. if (cur_cm_id_priv) {
  847. cm_id->state = IB_CM_IDLE;
  848. kfree(cm_id_priv->compare_data);
  849. cm_id_priv->compare_data = NULL;
  850. ret = -EBUSY;
  851. }
  852. return ret;
  853. }
  854. EXPORT_SYMBOL(ib_cm_listen);
  855. static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
  856. enum cm_msg_sequence msg_seq)
  857. {
  858. u64 hi_tid, low_tid;
  859. hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
  860. low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
  861. (msg_seq << 30));
  862. return cpu_to_be64(hi_tid | low_tid);
  863. }
  864. static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
  865. __be16 attr_id, __be64 tid)
  866. {
  867. hdr->base_version = IB_MGMT_BASE_VERSION;
  868. hdr->mgmt_class = IB_MGMT_CLASS_CM;
  869. hdr->class_version = IB_CM_CLASS_VERSION;
  870. hdr->method = IB_MGMT_METHOD_SEND;
  871. hdr->attr_id = attr_id;
  872. hdr->tid = tid;
  873. }
  874. static void cm_format_req(struct cm_req_msg *req_msg,
  875. struct cm_id_private *cm_id_priv,
  876. struct ib_cm_req_param *param)
  877. {
  878. struct ib_sa_path_rec *pri_path = param->primary_path;
  879. struct ib_sa_path_rec *alt_path = param->alternate_path;
  880. cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
  881. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
  882. req_msg->local_comm_id = cm_id_priv->id.local_id;
  883. req_msg->service_id = param->service_id;
  884. req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
  885. cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
  886. cm_req_set_resp_res(req_msg, param->responder_resources);
  887. cm_req_set_init_depth(req_msg, param->initiator_depth);
  888. cm_req_set_remote_resp_timeout(req_msg,
  889. param->remote_cm_response_timeout);
  890. cm_req_set_qp_type(req_msg, param->qp_type);
  891. cm_req_set_flow_ctrl(req_msg, param->flow_control);
  892. cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
  893. cm_req_set_local_resp_timeout(req_msg,
  894. param->local_cm_response_timeout);
  895. cm_req_set_retry_count(req_msg, param->retry_count);
  896. req_msg->pkey = param->primary_path->pkey;
  897. cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
  898. cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
  899. cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
  900. cm_req_set_srq(req_msg, param->srq);
  901. if (pri_path->hop_limit <= 1) {
  902. req_msg->primary_local_lid = pri_path->slid;
  903. req_msg->primary_remote_lid = pri_path->dlid;
  904. } else {
  905. /* Work-around until there's a way to obtain remote LID info */
  906. req_msg->primary_local_lid = IB_LID_PERMISSIVE;
  907. req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
  908. }
  909. req_msg->primary_local_gid = pri_path->sgid;
  910. req_msg->primary_remote_gid = pri_path->dgid;
  911. cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
  912. cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
  913. req_msg->primary_traffic_class = pri_path->traffic_class;
  914. req_msg->primary_hop_limit = pri_path->hop_limit;
  915. cm_req_set_primary_sl(req_msg, pri_path->sl);
  916. cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
  917. cm_req_set_primary_local_ack_timeout(req_msg,
  918. cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
  919. pri_path->packet_life_time));
  920. if (alt_path) {
  921. if (alt_path->hop_limit <= 1) {
  922. req_msg->alt_local_lid = alt_path->slid;
  923. req_msg->alt_remote_lid = alt_path->dlid;
  924. } else {
  925. req_msg->alt_local_lid = IB_LID_PERMISSIVE;
  926. req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
  927. }
  928. req_msg->alt_local_gid = alt_path->sgid;
  929. req_msg->alt_remote_gid = alt_path->dgid;
  930. cm_req_set_alt_flow_label(req_msg,
  931. alt_path->flow_label);
  932. cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
  933. req_msg->alt_traffic_class = alt_path->traffic_class;
  934. req_msg->alt_hop_limit = alt_path->hop_limit;
  935. cm_req_set_alt_sl(req_msg, alt_path->sl);
  936. cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
  937. cm_req_set_alt_local_ack_timeout(req_msg,
  938. cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
  939. alt_path->packet_life_time));
  940. }
  941. if (param->private_data && param->private_data_len)
  942. memcpy(req_msg->private_data, param->private_data,
  943. param->private_data_len);
  944. }
  945. static int cm_validate_req_param(struct ib_cm_req_param *param)
  946. {
  947. /* peer-to-peer not supported */
  948. if (param->peer_to_peer)
  949. return -EINVAL;
  950. if (!param->primary_path)
  951. return -EINVAL;
  952. if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
  953. return -EINVAL;
  954. if (param->private_data &&
  955. param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
  956. return -EINVAL;
  957. if (param->alternate_path &&
  958. (param->alternate_path->pkey != param->primary_path->pkey ||
  959. param->alternate_path->mtu != param->primary_path->mtu))
  960. return -EINVAL;
  961. return 0;
  962. }
  963. int ib_send_cm_req(struct ib_cm_id *cm_id,
  964. struct ib_cm_req_param *param)
  965. {
  966. struct cm_id_private *cm_id_priv;
  967. struct cm_req_msg *req_msg;
  968. unsigned long flags;
  969. int ret;
  970. ret = cm_validate_req_param(param);
  971. if (ret)
  972. return ret;
  973. /* Verify that we're not in timewait. */
  974. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  975. spin_lock_irqsave(&cm_id_priv->lock, flags);
  976. if (cm_id->state != IB_CM_IDLE) {
  977. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  978. ret = -EINVAL;
  979. goto out;
  980. }
  981. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  982. cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
  983. id.local_id);
  984. if (IS_ERR(cm_id_priv->timewait_info)) {
  985. ret = PTR_ERR(cm_id_priv->timewait_info);
  986. goto out;
  987. }
  988. ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
  989. if (ret)
  990. goto error1;
  991. if (param->alternate_path) {
  992. ret = cm_init_av_by_path(param->alternate_path,
  993. &cm_id_priv->alt_av);
  994. if (ret)
  995. goto error1;
  996. }
  997. cm_id->service_id = param->service_id;
  998. cm_id->service_mask = ~cpu_to_be64(0);
  999. cm_id_priv->timeout_ms = cm_convert_to_ms(
  1000. param->primary_path->packet_life_time) * 2 +
  1001. cm_convert_to_ms(
  1002. param->remote_cm_response_timeout);
  1003. cm_id_priv->max_cm_retries = param->max_cm_retries;
  1004. cm_id_priv->initiator_depth = param->initiator_depth;
  1005. cm_id_priv->responder_resources = param->responder_resources;
  1006. cm_id_priv->retry_count = param->retry_count;
  1007. cm_id_priv->path_mtu = param->primary_path->mtu;
  1008. cm_id_priv->pkey = param->primary_path->pkey;
  1009. cm_id_priv->qp_type = param->qp_type;
  1010. ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
  1011. if (ret)
  1012. goto error1;
  1013. req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
  1014. cm_format_req(req_msg, cm_id_priv, param);
  1015. cm_id_priv->tid = req_msg->hdr.tid;
  1016. cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
  1017. cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
  1018. cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
  1019. cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
  1020. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1021. ret = ib_post_send_mad(cm_id_priv->msg, NULL);
  1022. if (ret) {
  1023. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1024. goto error2;
  1025. }
  1026. BUG_ON(cm_id->state != IB_CM_IDLE);
  1027. cm_id->state = IB_CM_REQ_SENT;
  1028. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1029. return 0;
  1030. error2: cm_free_msg(cm_id_priv->msg);
  1031. error1: kfree(cm_id_priv->timewait_info);
  1032. out: return ret;
  1033. }
  1034. EXPORT_SYMBOL(ib_send_cm_req);
  1035. static int cm_issue_rej(struct cm_port *port,
  1036. struct ib_mad_recv_wc *mad_recv_wc,
  1037. enum ib_cm_rej_reason reason,
  1038. enum cm_msg_response msg_rejected,
  1039. void *ari, u8 ari_length)
  1040. {
  1041. struct ib_mad_send_buf *msg = NULL;
  1042. struct cm_rej_msg *rej_msg, *rcv_msg;
  1043. int ret;
  1044. ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
  1045. if (ret)
  1046. return ret;
  1047. /* We just need common CM header information. Cast to any message. */
  1048. rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
  1049. rej_msg = (struct cm_rej_msg *) msg->mad;
  1050. cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
  1051. rej_msg->remote_comm_id = rcv_msg->local_comm_id;
  1052. rej_msg->local_comm_id = rcv_msg->remote_comm_id;
  1053. cm_rej_set_msg_rejected(rej_msg, msg_rejected);
  1054. rej_msg->reason = cpu_to_be16(reason);
  1055. if (ari && ari_length) {
  1056. cm_rej_set_reject_info_len(rej_msg, ari_length);
  1057. memcpy(rej_msg->ari, ari, ari_length);
  1058. }
  1059. ret = ib_post_send_mad(msg, NULL);
  1060. if (ret)
  1061. cm_free_msg(msg);
  1062. return ret;
  1063. }
  1064. static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
  1065. __be32 local_qpn, __be32 remote_qpn)
  1066. {
  1067. return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
  1068. ((local_ca_guid == remote_ca_guid) &&
  1069. (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
  1070. }
  1071. static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
  1072. struct ib_sa_path_rec *primary_path,
  1073. struct ib_sa_path_rec *alt_path)
  1074. {
  1075. memset(primary_path, 0, sizeof *primary_path);
  1076. primary_path->dgid = req_msg->primary_local_gid;
  1077. primary_path->sgid = req_msg->primary_remote_gid;
  1078. primary_path->dlid = req_msg->primary_local_lid;
  1079. primary_path->slid = req_msg->primary_remote_lid;
  1080. primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
  1081. primary_path->hop_limit = req_msg->primary_hop_limit;
  1082. primary_path->traffic_class = req_msg->primary_traffic_class;
  1083. primary_path->reversible = 1;
  1084. primary_path->pkey = req_msg->pkey;
  1085. primary_path->sl = cm_req_get_primary_sl(req_msg);
  1086. primary_path->mtu_selector = IB_SA_EQ;
  1087. primary_path->mtu = cm_req_get_path_mtu(req_msg);
  1088. primary_path->rate_selector = IB_SA_EQ;
  1089. primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
  1090. primary_path->packet_life_time_selector = IB_SA_EQ;
  1091. primary_path->packet_life_time =
  1092. cm_req_get_primary_local_ack_timeout(req_msg);
  1093. primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
  1094. if (req_msg->alt_local_lid) {
  1095. memset(alt_path, 0, sizeof *alt_path);
  1096. alt_path->dgid = req_msg->alt_local_gid;
  1097. alt_path->sgid = req_msg->alt_remote_gid;
  1098. alt_path->dlid = req_msg->alt_local_lid;
  1099. alt_path->slid = req_msg->alt_remote_lid;
  1100. alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
  1101. alt_path->hop_limit = req_msg->alt_hop_limit;
  1102. alt_path->traffic_class = req_msg->alt_traffic_class;
  1103. alt_path->reversible = 1;
  1104. alt_path->pkey = req_msg->pkey;
  1105. alt_path->sl = cm_req_get_alt_sl(req_msg);
  1106. alt_path->mtu_selector = IB_SA_EQ;
  1107. alt_path->mtu = cm_req_get_path_mtu(req_msg);
  1108. alt_path->rate_selector = IB_SA_EQ;
  1109. alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
  1110. alt_path->packet_life_time_selector = IB_SA_EQ;
  1111. alt_path->packet_life_time =
  1112. cm_req_get_alt_local_ack_timeout(req_msg);
  1113. alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
  1114. }
  1115. }
  1116. static void cm_format_req_event(struct cm_work *work,
  1117. struct cm_id_private *cm_id_priv,
  1118. struct ib_cm_id *listen_id)
  1119. {
  1120. struct cm_req_msg *req_msg;
  1121. struct ib_cm_req_event_param *param;
  1122. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1123. param = &work->cm_event.param.req_rcvd;
  1124. param->listen_id = listen_id;
  1125. param->port = cm_id_priv->av.port->port_num;
  1126. param->primary_path = &work->path[0];
  1127. if (req_msg->alt_local_lid)
  1128. param->alternate_path = &work->path[1];
  1129. else
  1130. param->alternate_path = NULL;
  1131. param->remote_ca_guid = req_msg->local_ca_guid;
  1132. param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
  1133. param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
  1134. param->qp_type = cm_req_get_qp_type(req_msg);
  1135. param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
  1136. param->responder_resources = cm_req_get_init_depth(req_msg);
  1137. param->initiator_depth = cm_req_get_resp_res(req_msg);
  1138. param->local_cm_response_timeout =
  1139. cm_req_get_remote_resp_timeout(req_msg);
  1140. param->flow_control = cm_req_get_flow_ctrl(req_msg);
  1141. param->remote_cm_response_timeout =
  1142. cm_req_get_local_resp_timeout(req_msg);
  1143. param->retry_count = cm_req_get_retry_count(req_msg);
  1144. param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
  1145. param->srq = cm_req_get_srq(req_msg);
  1146. work->cm_event.private_data = &req_msg->private_data;
  1147. }
  1148. static void cm_process_work(struct cm_id_private *cm_id_priv,
  1149. struct cm_work *work)
  1150. {
  1151. int ret;
  1152. /* We will typically only have the current event to report. */
  1153. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
  1154. cm_free_work(work);
  1155. while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
  1156. spin_lock_irq(&cm_id_priv->lock);
  1157. work = cm_dequeue_work(cm_id_priv);
  1158. spin_unlock_irq(&cm_id_priv->lock);
  1159. BUG_ON(!work);
  1160. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
  1161. &work->cm_event);
  1162. cm_free_work(work);
  1163. }
  1164. cm_deref_id(cm_id_priv);
  1165. if (ret)
  1166. cm_destroy_id(&cm_id_priv->id, ret);
  1167. }
  1168. static void cm_format_mra(struct cm_mra_msg *mra_msg,
  1169. struct cm_id_private *cm_id_priv,
  1170. enum cm_msg_response msg_mraed, u8 service_timeout,
  1171. const void *private_data, u8 private_data_len)
  1172. {
  1173. cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
  1174. cm_mra_set_msg_mraed(mra_msg, msg_mraed);
  1175. mra_msg->local_comm_id = cm_id_priv->id.local_id;
  1176. mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1177. cm_mra_set_service_timeout(mra_msg, service_timeout);
  1178. if (private_data && private_data_len)
  1179. memcpy(mra_msg->private_data, private_data, private_data_len);
  1180. }
  1181. static void cm_format_rej(struct cm_rej_msg *rej_msg,
  1182. struct cm_id_private *cm_id_priv,
  1183. enum ib_cm_rej_reason reason,
  1184. void *ari,
  1185. u8 ari_length,
  1186. const void *private_data,
  1187. u8 private_data_len)
  1188. {
  1189. cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
  1190. rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1191. switch(cm_id_priv->id.state) {
  1192. case IB_CM_REQ_RCVD:
  1193. rej_msg->local_comm_id = 0;
  1194. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
  1195. break;
  1196. case IB_CM_MRA_REQ_SENT:
  1197. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1198. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
  1199. break;
  1200. case IB_CM_REP_RCVD:
  1201. case IB_CM_MRA_REP_SENT:
  1202. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1203. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
  1204. break;
  1205. default:
  1206. rej_msg->local_comm_id = cm_id_priv->id.local_id;
  1207. cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
  1208. break;
  1209. }
  1210. rej_msg->reason = cpu_to_be16(reason);
  1211. if (ari && ari_length) {
  1212. cm_rej_set_reject_info_len(rej_msg, ari_length);
  1213. memcpy(rej_msg->ari, ari, ari_length);
  1214. }
  1215. if (private_data && private_data_len)
  1216. memcpy(rej_msg->private_data, private_data, private_data_len);
  1217. }
  1218. static void cm_dup_req_handler(struct cm_work *work,
  1219. struct cm_id_private *cm_id_priv)
  1220. {
  1221. struct ib_mad_send_buf *msg = NULL;
  1222. int ret;
  1223. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1224. counter[CM_REQ_COUNTER]);
  1225. /* Quick state check to discard duplicate REQs. */
  1226. if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
  1227. return;
  1228. ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
  1229. if (ret)
  1230. return;
  1231. spin_lock_irq(&cm_id_priv->lock);
  1232. switch (cm_id_priv->id.state) {
  1233. case IB_CM_MRA_REQ_SENT:
  1234. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1235. CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
  1236. cm_id_priv->private_data,
  1237. cm_id_priv->private_data_len);
  1238. break;
  1239. case IB_CM_TIMEWAIT:
  1240. cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
  1241. IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
  1242. break;
  1243. default:
  1244. goto unlock;
  1245. }
  1246. spin_unlock_irq(&cm_id_priv->lock);
  1247. ret = ib_post_send_mad(msg, NULL);
  1248. if (ret)
  1249. goto free;
  1250. return;
  1251. unlock: spin_unlock_irq(&cm_id_priv->lock);
  1252. free: cm_free_msg(msg);
  1253. }
  1254. static struct cm_id_private * cm_match_req(struct cm_work *work,
  1255. struct cm_id_private *cm_id_priv)
  1256. {
  1257. struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
  1258. struct cm_timewait_info *timewait_info;
  1259. struct cm_req_msg *req_msg;
  1260. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1261. /* Check for possible duplicate REQ. */
  1262. spin_lock_irq(&cm.lock);
  1263. timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
  1264. if (timewait_info) {
  1265. cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
  1266. timewait_info->work.remote_id);
  1267. spin_unlock_irq(&cm.lock);
  1268. if (cur_cm_id_priv) {
  1269. cm_dup_req_handler(work, cur_cm_id_priv);
  1270. cm_deref_id(cur_cm_id_priv);
  1271. }
  1272. return NULL;
  1273. }
  1274. /* Check for stale connections. */
  1275. timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
  1276. if (timewait_info) {
  1277. cm_cleanup_timewait(cm_id_priv->timewait_info);
  1278. spin_unlock_irq(&cm.lock);
  1279. cm_issue_rej(work->port, work->mad_recv_wc,
  1280. IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
  1281. NULL, 0);
  1282. return NULL;
  1283. }
  1284. /* Find matching listen request. */
  1285. listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
  1286. req_msg->service_id,
  1287. req_msg->private_data);
  1288. if (!listen_cm_id_priv) {
  1289. cm_cleanup_timewait(cm_id_priv->timewait_info);
  1290. spin_unlock_irq(&cm.lock);
  1291. cm_issue_rej(work->port, work->mad_recv_wc,
  1292. IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
  1293. NULL, 0);
  1294. goto out;
  1295. }
  1296. atomic_inc(&listen_cm_id_priv->refcount);
  1297. atomic_inc(&cm_id_priv->refcount);
  1298. cm_id_priv->id.state = IB_CM_REQ_RCVD;
  1299. atomic_inc(&cm_id_priv->work_count);
  1300. spin_unlock_irq(&cm.lock);
  1301. out:
  1302. return listen_cm_id_priv;
  1303. }
  1304. /*
  1305. * Work-around for inter-subnet connections. If the LIDs are permissive,
  1306. * we need to override the LID/SL data in the REQ with the LID information
  1307. * in the work completion.
  1308. */
  1309. static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
  1310. {
  1311. if (!cm_req_get_primary_subnet_local(req_msg)) {
  1312. if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
  1313. req_msg->primary_local_lid = cpu_to_be16(wc->slid);
  1314. cm_req_set_primary_sl(req_msg, wc->sl);
  1315. }
  1316. if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
  1317. req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
  1318. }
  1319. if (!cm_req_get_alt_subnet_local(req_msg)) {
  1320. if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
  1321. req_msg->alt_local_lid = cpu_to_be16(wc->slid);
  1322. cm_req_set_alt_sl(req_msg, wc->sl);
  1323. }
  1324. if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
  1325. req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
  1326. }
  1327. }
  1328. static int cm_req_handler(struct cm_work *work)
  1329. {
  1330. struct ib_cm_id *cm_id;
  1331. struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
  1332. struct cm_req_msg *req_msg;
  1333. int ret;
  1334. req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
  1335. cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
  1336. if (IS_ERR(cm_id))
  1337. return PTR_ERR(cm_id);
  1338. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1339. cm_id_priv->id.remote_id = req_msg->local_comm_id;
  1340. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  1341. work->mad_recv_wc->recv_buf.grh,
  1342. &cm_id_priv->av);
  1343. cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
  1344. id.local_id);
  1345. if (IS_ERR(cm_id_priv->timewait_info)) {
  1346. ret = PTR_ERR(cm_id_priv->timewait_info);
  1347. goto destroy;
  1348. }
  1349. cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
  1350. cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
  1351. cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
  1352. listen_cm_id_priv = cm_match_req(work, cm_id_priv);
  1353. if (!listen_cm_id_priv) {
  1354. ret = -EINVAL;
  1355. kfree(cm_id_priv->timewait_info);
  1356. goto destroy;
  1357. }
  1358. cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
  1359. cm_id_priv->id.context = listen_cm_id_priv->id.context;
  1360. cm_id_priv->id.service_id = req_msg->service_id;
  1361. cm_id_priv->id.service_mask = ~cpu_to_be64(0);
  1362. cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
  1363. cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
  1364. ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
  1365. if (ret) {
  1366. ib_get_cached_gid(work->port->cm_dev->ib_device,
  1367. work->port->port_num, 0, &work->path[0].sgid);
  1368. ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
  1369. &work->path[0].sgid, sizeof work->path[0].sgid,
  1370. NULL, 0);
  1371. goto rejected;
  1372. }
  1373. if (req_msg->alt_local_lid) {
  1374. ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
  1375. if (ret) {
  1376. ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
  1377. &work->path[0].sgid,
  1378. sizeof work->path[0].sgid, NULL, 0);
  1379. goto rejected;
  1380. }
  1381. }
  1382. cm_id_priv->tid = req_msg->hdr.tid;
  1383. cm_id_priv->timeout_ms = cm_convert_to_ms(
  1384. cm_req_get_local_resp_timeout(req_msg));
  1385. cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
  1386. cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
  1387. cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
  1388. cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
  1389. cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
  1390. cm_id_priv->pkey = req_msg->pkey;
  1391. cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
  1392. cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
  1393. cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
  1394. cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
  1395. cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
  1396. cm_process_work(cm_id_priv, work);
  1397. cm_deref_id(listen_cm_id_priv);
  1398. return 0;
  1399. rejected:
  1400. atomic_dec(&cm_id_priv->refcount);
  1401. cm_deref_id(listen_cm_id_priv);
  1402. destroy:
  1403. ib_destroy_cm_id(cm_id);
  1404. return ret;
  1405. }
  1406. static void cm_format_rep(struct cm_rep_msg *rep_msg,
  1407. struct cm_id_private *cm_id_priv,
  1408. struct ib_cm_rep_param *param)
  1409. {
  1410. cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
  1411. rep_msg->local_comm_id = cm_id_priv->id.local_id;
  1412. rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1413. cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
  1414. cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
  1415. rep_msg->resp_resources = param->responder_resources;
  1416. rep_msg->initiator_depth = param->initiator_depth;
  1417. cm_rep_set_target_ack_delay(rep_msg,
  1418. cm_id_priv->av.port->cm_dev->ack_delay);
  1419. cm_rep_set_failover(rep_msg, param->failover_accepted);
  1420. cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
  1421. cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
  1422. cm_rep_set_srq(rep_msg, param->srq);
  1423. rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
  1424. if (param->private_data && param->private_data_len)
  1425. memcpy(rep_msg->private_data, param->private_data,
  1426. param->private_data_len);
  1427. }
  1428. int ib_send_cm_rep(struct ib_cm_id *cm_id,
  1429. struct ib_cm_rep_param *param)
  1430. {
  1431. struct cm_id_private *cm_id_priv;
  1432. struct ib_mad_send_buf *msg;
  1433. struct cm_rep_msg *rep_msg;
  1434. unsigned long flags;
  1435. int ret;
  1436. if (param->private_data &&
  1437. param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
  1438. return -EINVAL;
  1439. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1440. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1441. if (cm_id->state != IB_CM_REQ_RCVD &&
  1442. cm_id->state != IB_CM_MRA_REQ_SENT) {
  1443. ret = -EINVAL;
  1444. goto out;
  1445. }
  1446. ret = cm_alloc_msg(cm_id_priv, &msg);
  1447. if (ret)
  1448. goto out;
  1449. rep_msg = (struct cm_rep_msg *) msg->mad;
  1450. cm_format_rep(rep_msg, cm_id_priv, param);
  1451. msg->timeout_ms = cm_id_priv->timeout_ms;
  1452. msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
  1453. ret = ib_post_send_mad(msg, NULL);
  1454. if (ret) {
  1455. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1456. cm_free_msg(msg);
  1457. return ret;
  1458. }
  1459. cm_id->state = IB_CM_REP_SENT;
  1460. cm_id_priv->msg = msg;
  1461. cm_id_priv->initiator_depth = param->initiator_depth;
  1462. cm_id_priv->responder_resources = param->responder_resources;
  1463. cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
  1464. cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
  1465. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1466. return ret;
  1467. }
  1468. EXPORT_SYMBOL(ib_send_cm_rep);
  1469. static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
  1470. struct cm_id_private *cm_id_priv,
  1471. const void *private_data,
  1472. u8 private_data_len)
  1473. {
  1474. cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
  1475. rtu_msg->local_comm_id = cm_id_priv->id.local_id;
  1476. rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1477. if (private_data && private_data_len)
  1478. memcpy(rtu_msg->private_data, private_data, private_data_len);
  1479. }
  1480. int ib_send_cm_rtu(struct ib_cm_id *cm_id,
  1481. const void *private_data,
  1482. u8 private_data_len)
  1483. {
  1484. struct cm_id_private *cm_id_priv;
  1485. struct ib_mad_send_buf *msg;
  1486. unsigned long flags;
  1487. void *data;
  1488. int ret;
  1489. if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
  1490. return -EINVAL;
  1491. data = cm_copy_private_data(private_data, private_data_len);
  1492. if (IS_ERR(data))
  1493. return PTR_ERR(data);
  1494. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1495. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1496. if (cm_id->state != IB_CM_REP_RCVD &&
  1497. cm_id->state != IB_CM_MRA_REP_SENT) {
  1498. ret = -EINVAL;
  1499. goto error;
  1500. }
  1501. ret = cm_alloc_msg(cm_id_priv, &msg);
  1502. if (ret)
  1503. goto error;
  1504. cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
  1505. private_data, private_data_len);
  1506. ret = ib_post_send_mad(msg, NULL);
  1507. if (ret) {
  1508. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1509. cm_free_msg(msg);
  1510. kfree(data);
  1511. return ret;
  1512. }
  1513. cm_id->state = IB_CM_ESTABLISHED;
  1514. cm_set_private_data(cm_id_priv, data, private_data_len);
  1515. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1516. return 0;
  1517. error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1518. kfree(data);
  1519. return ret;
  1520. }
  1521. EXPORT_SYMBOL(ib_send_cm_rtu);
  1522. static void cm_format_rep_event(struct cm_work *work)
  1523. {
  1524. struct cm_rep_msg *rep_msg;
  1525. struct ib_cm_rep_event_param *param;
  1526. rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
  1527. param = &work->cm_event.param.rep_rcvd;
  1528. param->remote_ca_guid = rep_msg->local_ca_guid;
  1529. param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
  1530. param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
  1531. param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
  1532. param->responder_resources = rep_msg->initiator_depth;
  1533. param->initiator_depth = rep_msg->resp_resources;
  1534. param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
  1535. param->failover_accepted = cm_rep_get_failover(rep_msg);
  1536. param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
  1537. param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
  1538. param->srq = cm_rep_get_srq(rep_msg);
  1539. work->cm_event.private_data = &rep_msg->private_data;
  1540. }
  1541. static void cm_dup_rep_handler(struct cm_work *work)
  1542. {
  1543. struct cm_id_private *cm_id_priv;
  1544. struct cm_rep_msg *rep_msg;
  1545. struct ib_mad_send_buf *msg = NULL;
  1546. int ret;
  1547. rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
  1548. cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
  1549. rep_msg->local_comm_id);
  1550. if (!cm_id_priv)
  1551. return;
  1552. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1553. counter[CM_REP_COUNTER]);
  1554. ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
  1555. if (ret)
  1556. goto deref;
  1557. spin_lock_irq(&cm_id_priv->lock);
  1558. if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
  1559. cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
  1560. cm_id_priv->private_data,
  1561. cm_id_priv->private_data_len);
  1562. else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
  1563. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  1564. CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
  1565. cm_id_priv->private_data,
  1566. cm_id_priv->private_data_len);
  1567. else
  1568. goto unlock;
  1569. spin_unlock_irq(&cm_id_priv->lock);
  1570. ret = ib_post_send_mad(msg, NULL);
  1571. if (ret)
  1572. goto free;
  1573. goto deref;
  1574. unlock: spin_unlock_irq(&cm_id_priv->lock);
  1575. free: cm_free_msg(msg);
  1576. deref: cm_deref_id(cm_id_priv);
  1577. }
  1578. static int cm_rep_handler(struct cm_work *work)
  1579. {
  1580. struct cm_id_private *cm_id_priv;
  1581. struct cm_rep_msg *rep_msg;
  1582. int ret;
  1583. rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
  1584. cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
  1585. if (!cm_id_priv) {
  1586. cm_dup_rep_handler(work);
  1587. return -EINVAL;
  1588. }
  1589. cm_format_rep_event(work);
  1590. spin_lock_irq(&cm_id_priv->lock);
  1591. switch (cm_id_priv->id.state) {
  1592. case IB_CM_REQ_SENT:
  1593. case IB_CM_MRA_REQ_RCVD:
  1594. break;
  1595. default:
  1596. spin_unlock_irq(&cm_id_priv->lock);
  1597. ret = -EINVAL;
  1598. goto error;
  1599. }
  1600. cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
  1601. cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
  1602. cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
  1603. spin_lock(&cm.lock);
  1604. /* Check for duplicate REP. */
  1605. if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
  1606. spin_unlock(&cm.lock);
  1607. spin_unlock_irq(&cm_id_priv->lock);
  1608. ret = -EINVAL;
  1609. goto error;
  1610. }
  1611. /* Check for a stale connection. */
  1612. if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
  1613. rb_erase(&cm_id_priv->timewait_info->remote_id_node,
  1614. &cm.remote_id_table);
  1615. cm_id_priv->timewait_info->inserted_remote_id = 0;
  1616. spin_unlock(&cm.lock);
  1617. spin_unlock_irq(&cm_id_priv->lock);
  1618. cm_issue_rej(work->port, work->mad_recv_wc,
  1619. IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
  1620. NULL, 0);
  1621. ret = -EINVAL;
  1622. goto error;
  1623. }
  1624. spin_unlock(&cm.lock);
  1625. cm_id_priv->id.state = IB_CM_REP_RCVD;
  1626. cm_id_priv->id.remote_id = rep_msg->local_comm_id;
  1627. cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
  1628. cm_id_priv->initiator_depth = rep_msg->resp_resources;
  1629. cm_id_priv->responder_resources = rep_msg->initiator_depth;
  1630. cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
  1631. cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
  1632. cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
  1633. cm_id_priv->av.timeout =
  1634. cm_ack_timeout(cm_id_priv->target_ack_delay,
  1635. cm_id_priv->av.timeout - 1);
  1636. cm_id_priv->alt_av.timeout =
  1637. cm_ack_timeout(cm_id_priv->target_ack_delay,
  1638. cm_id_priv->alt_av.timeout - 1);
  1639. /* todo: handle peer_to_peer */
  1640. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1641. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1642. if (!ret)
  1643. list_add_tail(&work->list, &cm_id_priv->work_list);
  1644. spin_unlock_irq(&cm_id_priv->lock);
  1645. if (ret)
  1646. cm_process_work(cm_id_priv, work);
  1647. else
  1648. cm_deref_id(cm_id_priv);
  1649. return 0;
  1650. error:
  1651. cm_deref_id(cm_id_priv);
  1652. return ret;
  1653. }
  1654. static int cm_establish_handler(struct cm_work *work)
  1655. {
  1656. struct cm_id_private *cm_id_priv;
  1657. int ret;
  1658. /* See comment in cm_establish about lookup. */
  1659. cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
  1660. if (!cm_id_priv)
  1661. return -EINVAL;
  1662. spin_lock_irq(&cm_id_priv->lock);
  1663. if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
  1664. spin_unlock_irq(&cm_id_priv->lock);
  1665. goto out;
  1666. }
  1667. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1668. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1669. if (!ret)
  1670. list_add_tail(&work->list, &cm_id_priv->work_list);
  1671. spin_unlock_irq(&cm_id_priv->lock);
  1672. if (ret)
  1673. cm_process_work(cm_id_priv, work);
  1674. else
  1675. cm_deref_id(cm_id_priv);
  1676. return 0;
  1677. out:
  1678. cm_deref_id(cm_id_priv);
  1679. return -EINVAL;
  1680. }
  1681. static int cm_rtu_handler(struct cm_work *work)
  1682. {
  1683. struct cm_id_private *cm_id_priv;
  1684. struct cm_rtu_msg *rtu_msg;
  1685. int ret;
  1686. rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
  1687. cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
  1688. rtu_msg->local_comm_id);
  1689. if (!cm_id_priv)
  1690. return -EINVAL;
  1691. work->cm_event.private_data = &rtu_msg->private_data;
  1692. spin_lock_irq(&cm_id_priv->lock);
  1693. if (cm_id_priv->id.state != IB_CM_REP_SENT &&
  1694. cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
  1695. spin_unlock_irq(&cm_id_priv->lock);
  1696. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1697. counter[CM_RTU_COUNTER]);
  1698. goto out;
  1699. }
  1700. cm_id_priv->id.state = IB_CM_ESTABLISHED;
  1701. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1702. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1703. if (!ret)
  1704. list_add_tail(&work->list, &cm_id_priv->work_list);
  1705. spin_unlock_irq(&cm_id_priv->lock);
  1706. if (ret)
  1707. cm_process_work(cm_id_priv, work);
  1708. else
  1709. cm_deref_id(cm_id_priv);
  1710. return 0;
  1711. out:
  1712. cm_deref_id(cm_id_priv);
  1713. return -EINVAL;
  1714. }
  1715. static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
  1716. struct cm_id_private *cm_id_priv,
  1717. const void *private_data,
  1718. u8 private_data_len)
  1719. {
  1720. cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
  1721. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
  1722. dreq_msg->local_comm_id = cm_id_priv->id.local_id;
  1723. dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1724. cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
  1725. if (private_data && private_data_len)
  1726. memcpy(dreq_msg->private_data, private_data, private_data_len);
  1727. }
  1728. int ib_send_cm_dreq(struct ib_cm_id *cm_id,
  1729. const void *private_data,
  1730. u8 private_data_len)
  1731. {
  1732. struct cm_id_private *cm_id_priv;
  1733. struct ib_mad_send_buf *msg;
  1734. unsigned long flags;
  1735. int ret;
  1736. if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
  1737. return -EINVAL;
  1738. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1739. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1740. if (cm_id->state != IB_CM_ESTABLISHED) {
  1741. ret = -EINVAL;
  1742. goto out;
  1743. }
  1744. if (cm_id->lap_state == IB_CM_LAP_SENT ||
  1745. cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
  1746. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1747. ret = cm_alloc_msg(cm_id_priv, &msg);
  1748. if (ret) {
  1749. cm_enter_timewait(cm_id_priv);
  1750. goto out;
  1751. }
  1752. cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
  1753. private_data, private_data_len);
  1754. msg->timeout_ms = cm_id_priv->timeout_ms;
  1755. msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
  1756. ret = ib_post_send_mad(msg, NULL);
  1757. if (ret) {
  1758. cm_enter_timewait(cm_id_priv);
  1759. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1760. cm_free_msg(msg);
  1761. return ret;
  1762. }
  1763. cm_id->state = IB_CM_DREQ_SENT;
  1764. cm_id_priv->msg = msg;
  1765. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1766. return ret;
  1767. }
  1768. EXPORT_SYMBOL(ib_send_cm_dreq);
  1769. static void cm_format_drep(struct cm_drep_msg *drep_msg,
  1770. struct cm_id_private *cm_id_priv,
  1771. const void *private_data,
  1772. u8 private_data_len)
  1773. {
  1774. cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
  1775. drep_msg->local_comm_id = cm_id_priv->id.local_id;
  1776. drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
  1777. if (private_data && private_data_len)
  1778. memcpy(drep_msg->private_data, private_data, private_data_len);
  1779. }
  1780. int ib_send_cm_drep(struct ib_cm_id *cm_id,
  1781. const void *private_data,
  1782. u8 private_data_len)
  1783. {
  1784. struct cm_id_private *cm_id_priv;
  1785. struct ib_mad_send_buf *msg;
  1786. unsigned long flags;
  1787. void *data;
  1788. int ret;
  1789. if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
  1790. return -EINVAL;
  1791. data = cm_copy_private_data(private_data, private_data_len);
  1792. if (IS_ERR(data))
  1793. return PTR_ERR(data);
  1794. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1795. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1796. if (cm_id->state != IB_CM_DREQ_RCVD) {
  1797. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1798. kfree(data);
  1799. return -EINVAL;
  1800. }
  1801. cm_set_private_data(cm_id_priv, data, private_data_len);
  1802. cm_enter_timewait(cm_id_priv);
  1803. ret = cm_alloc_msg(cm_id_priv, &msg);
  1804. if (ret)
  1805. goto out;
  1806. cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
  1807. private_data, private_data_len);
  1808. ret = ib_post_send_mad(msg, NULL);
  1809. if (ret) {
  1810. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1811. cm_free_msg(msg);
  1812. return ret;
  1813. }
  1814. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1815. return ret;
  1816. }
  1817. EXPORT_SYMBOL(ib_send_cm_drep);
  1818. static int cm_issue_drep(struct cm_port *port,
  1819. struct ib_mad_recv_wc *mad_recv_wc)
  1820. {
  1821. struct ib_mad_send_buf *msg = NULL;
  1822. struct cm_dreq_msg *dreq_msg;
  1823. struct cm_drep_msg *drep_msg;
  1824. int ret;
  1825. ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
  1826. if (ret)
  1827. return ret;
  1828. dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
  1829. drep_msg = (struct cm_drep_msg *) msg->mad;
  1830. cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
  1831. drep_msg->remote_comm_id = dreq_msg->local_comm_id;
  1832. drep_msg->local_comm_id = dreq_msg->remote_comm_id;
  1833. ret = ib_post_send_mad(msg, NULL);
  1834. if (ret)
  1835. cm_free_msg(msg);
  1836. return ret;
  1837. }
  1838. static int cm_dreq_handler(struct cm_work *work)
  1839. {
  1840. struct cm_id_private *cm_id_priv;
  1841. struct cm_dreq_msg *dreq_msg;
  1842. struct ib_mad_send_buf *msg = NULL;
  1843. int ret;
  1844. dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
  1845. cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
  1846. dreq_msg->local_comm_id);
  1847. if (!cm_id_priv) {
  1848. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1849. counter[CM_DREQ_COUNTER]);
  1850. cm_issue_drep(work->port, work->mad_recv_wc);
  1851. return -EINVAL;
  1852. }
  1853. work->cm_event.private_data = &dreq_msg->private_data;
  1854. spin_lock_irq(&cm_id_priv->lock);
  1855. if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
  1856. goto unlock;
  1857. switch (cm_id_priv->id.state) {
  1858. case IB_CM_REP_SENT:
  1859. case IB_CM_DREQ_SENT:
  1860. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1861. break;
  1862. case IB_CM_ESTABLISHED:
  1863. if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
  1864. cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
  1865. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1866. break;
  1867. case IB_CM_MRA_REP_RCVD:
  1868. break;
  1869. case IB_CM_TIMEWAIT:
  1870. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1871. counter[CM_DREQ_COUNTER]);
  1872. if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
  1873. goto unlock;
  1874. cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
  1875. cm_id_priv->private_data,
  1876. cm_id_priv->private_data_len);
  1877. spin_unlock_irq(&cm_id_priv->lock);
  1878. if (ib_post_send_mad(msg, NULL))
  1879. cm_free_msg(msg);
  1880. goto deref;
  1881. case IB_CM_DREQ_RCVD:
  1882. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  1883. counter[CM_DREQ_COUNTER]);
  1884. goto unlock;
  1885. default:
  1886. goto unlock;
  1887. }
  1888. cm_id_priv->id.state = IB_CM_DREQ_RCVD;
  1889. cm_id_priv->tid = dreq_msg->hdr.tid;
  1890. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1891. if (!ret)
  1892. list_add_tail(&work->list, &cm_id_priv->work_list);
  1893. spin_unlock_irq(&cm_id_priv->lock);
  1894. if (ret)
  1895. cm_process_work(cm_id_priv, work);
  1896. else
  1897. cm_deref_id(cm_id_priv);
  1898. return 0;
  1899. unlock: spin_unlock_irq(&cm_id_priv->lock);
  1900. deref: cm_deref_id(cm_id_priv);
  1901. return -EINVAL;
  1902. }
  1903. static int cm_drep_handler(struct cm_work *work)
  1904. {
  1905. struct cm_id_private *cm_id_priv;
  1906. struct cm_drep_msg *drep_msg;
  1907. int ret;
  1908. drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
  1909. cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
  1910. drep_msg->local_comm_id);
  1911. if (!cm_id_priv)
  1912. return -EINVAL;
  1913. work->cm_event.private_data = &drep_msg->private_data;
  1914. spin_lock_irq(&cm_id_priv->lock);
  1915. if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
  1916. cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
  1917. spin_unlock_irq(&cm_id_priv->lock);
  1918. goto out;
  1919. }
  1920. cm_enter_timewait(cm_id_priv);
  1921. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  1922. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  1923. if (!ret)
  1924. list_add_tail(&work->list, &cm_id_priv->work_list);
  1925. spin_unlock_irq(&cm_id_priv->lock);
  1926. if (ret)
  1927. cm_process_work(cm_id_priv, work);
  1928. else
  1929. cm_deref_id(cm_id_priv);
  1930. return 0;
  1931. out:
  1932. cm_deref_id(cm_id_priv);
  1933. return -EINVAL;
  1934. }
  1935. int ib_send_cm_rej(struct ib_cm_id *cm_id,
  1936. enum ib_cm_rej_reason reason,
  1937. void *ari,
  1938. u8 ari_length,
  1939. const void *private_data,
  1940. u8 private_data_len)
  1941. {
  1942. struct cm_id_private *cm_id_priv;
  1943. struct ib_mad_send_buf *msg;
  1944. unsigned long flags;
  1945. int ret;
  1946. if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
  1947. (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
  1948. return -EINVAL;
  1949. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  1950. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1951. switch (cm_id->state) {
  1952. case IB_CM_REQ_SENT:
  1953. case IB_CM_MRA_REQ_RCVD:
  1954. case IB_CM_REQ_RCVD:
  1955. case IB_CM_MRA_REQ_SENT:
  1956. case IB_CM_REP_RCVD:
  1957. case IB_CM_MRA_REP_SENT:
  1958. ret = cm_alloc_msg(cm_id_priv, &msg);
  1959. if (!ret)
  1960. cm_format_rej((struct cm_rej_msg *) msg->mad,
  1961. cm_id_priv, reason, ari, ari_length,
  1962. private_data, private_data_len);
  1963. cm_reset_to_idle(cm_id_priv);
  1964. break;
  1965. case IB_CM_REP_SENT:
  1966. case IB_CM_MRA_REP_RCVD:
  1967. ret = cm_alloc_msg(cm_id_priv, &msg);
  1968. if (!ret)
  1969. cm_format_rej((struct cm_rej_msg *) msg->mad,
  1970. cm_id_priv, reason, ari, ari_length,
  1971. private_data, private_data_len);
  1972. cm_enter_timewait(cm_id_priv);
  1973. break;
  1974. default:
  1975. ret = -EINVAL;
  1976. goto out;
  1977. }
  1978. if (ret)
  1979. goto out;
  1980. ret = ib_post_send_mad(msg, NULL);
  1981. if (ret)
  1982. cm_free_msg(msg);
  1983. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1984. return ret;
  1985. }
  1986. EXPORT_SYMBOL(ib_send_cm_rej);
  1987. static void cm_format_rej_event(struct cm_work *work)
  1988. {
  1989. struct cm_rej_msg *rej_msg;
  1990. struct ib_cm_rej_event_param *param;
  1991. rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
  1992. param = &work->cm_event.param.rej_rcvd;
  1993. param->ari = rej_msg->ari;
  1994. param->ari_length = cm_rej_get_reject_info_len(rej_msg);
  1995. param->reason = __be16_to_cpu(rej_msg->reason);
  1996. work->cm_event.private_data = &rej_msg->private_data;
  1997. }
  1998. static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
  1999. {
  2000. struct cm_timewait_info *timewait_info;
  2001. struct cm_id_private *cm_id_priv;
  2002. __be32 remote_id;
  2003. remote_id = rej_msg->local_comm_id;
  2004. if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
  2005. spin_lock_irq(&cm.lock);
  2006. timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
  2007. remote_id);
  2008. if (!timewait_info) {
  2009. spin_unlock_irq(&cm.lock);
  2010. return NULL;
  2011. }
  2012. cm_id_priv = idr_find(&cm.local_id_table, (__force int)
  2013. (timewait_info->work.local_id ^
  2014. cm.random_id_operand));
  2015. if (cm_id_priv) {
  2016. if (cm_id_priv->id.remote_id == remote_id)
  2017. atomic_inc(&cm_id_priv->refcount);
  2018. else
  2019. cm_id_priv = NULL;
  2020. }
  2021. spin_unlock_irq(&cm.lock);
  2022. } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
  2023. cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
  2024. else
  2025. cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
  2026. return cm_id_priv;
  2027. }
  2028. static int cm_rej_handler(struct cm_work *work)
  2029. {
  2030. struct cm_id_private *cm_id_priv;
  2031. struct cm_rej_msg *rej_msg;
  2032. int ret;
  2033. rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
  2034. cm_id_priv = cm_acquire_rejected_id(rej_msg);
  2035. if (!cm_id_priv)
  2036. return -EINVAL;
  2037. cm_format_rej_event(work);
  2038. spin_lock_irq(&cm_id_priv->lock);
  2039. switch (cm_id_priv->id.state) {
  2040. case IB_CM_REQ_SENT:
  2041. case IB_CM_MRA_REQ_RCVD:
  2042. case IB_CM_REP_SENT:
  2043. case IB_CM_MRA_REP_RCVD:
  2044. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2045. /* fall through */
  2046. case IB_CM_REQ_RCVD:
  2047. case IB_CM_MRA_REQ_SENT:
  2048. if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
  2049. cm_enter_timewait(cm_id_priv);
  2050. else
  2051. cm_reset_to_idle(cm_id_priv);
  2052. break;
  2053. case IB_CM_DREQ_SENT:
  2054. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2055. /* fall through */
  2056. case IB_CM_REP_RCVD:
  2057. case IB_CM_MRA_REP_SENT:
  2058. cm_enter_timewait(cm_id_priv);
  2059. break;
  2060. case IB_CM_ESTABLISHED:
  2061. if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
  2062. cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
  2063. if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
  2064. ib_cancel_mad(cm_id_priv->av.port->mad_agent,
  2065. cm_id_priv->msg);
  2066. cm_enter_timewait(cm_id_priv);
  2067. break;
  2068. }
  2069. /* fall through */
  2070. default:
  2071. spin_unlock_irq(&cm_id_priv->lock);
  2072. ret = -EINVAL;
  2073. goto out;
  2074. }
  2075. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2076. if (!ret)
  2077. list_add_tail(&work->list, &cm_id_priv->work_list);
  2078. spin_unlock_irq(&cm_id_priv->lock);
  2079. if (ret)
  2080. cm_process_work(cm_id_priv, work);
  2081. else
  2082. cm_deref_id(cm_id_priv);
  2083. return 0;
  2084. out:
  2085. cm_deref_id(cm_id_priv);
  2086. return -EINVAL;
  2087. }
  2088. int ib_send_cm_mra(struct ib_cm_id *cm_id,
  2089. u8 service_timeout,
  2090. const void *private_data,
  2091. u8 private_data_len)
  2092. {
  2093. struct cm_id_private *cm_id_priv;
  2094. struct ib_mad_send_buf *msg;
  2095. enum ib_cm_state cm_state;
  2096. enum ib_cm_lap_state lap_state;
  2097. enum cm_msg_response msg_response;
  2098. void *data;
  2099. unsigned long flags;
  2100. int ret;
  2101. if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
  2102. return -EINVAL;
  2103. data = cm_copy_private_data(private_data, private_data_len);
  2104. if (IS_ERR(data))
  2105. return PTR_ERR(data);
  2106. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2107. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2108. switch(cm_id_priv->id.state) {
  2109. case IB_CM_REQ_RCVD:
  2110. cm_state = IB_CM_MRA_REQ_SENT;
  2111. lap_state = cm_id->lap_state;
  2112. msg_response = CM_MSG_RESPONSE_REQ;
  2113. break;
  2114. case IB_CM_REP_RCVD:
  2115. cm_state = IB_CM_MRA_REP_SENT;
  2116. lap_state = cm_id->lap_state;
  2117. msg_response = CM_MSG_RESPONSE_REP;
  2118. break;
  2119. case IB_CM_ESTABLISHED:
  2120. if (cm_id->lap_state == IB_CM_LAP_RCVD) {
  2121. cm_state = cm_id->state;
  2122. lap_state = IB_CM_MRA_LAP_SENT;
  2123. msg_response = CM_MSG_RESPONSE_OTHER;
  2124. break;
  2125. }
  2126. default:
  2127. ret = -EINVAL;
  2128. goto error1;
  2129. }
  2130. if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
  2131. ret = cm_alloc_msg(cm_id_priv, &msg);
  2132. if (ret)
  2133. goto error1;
  2134. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  2135. msg_response, service_timeout,
  2136. private_data, private_data_len);
  2137. ret = ib_post_send_mad(msg, NULL);
  2138. if (ret)
  2139. goto error2;
  2140. }
  2141. cm_id->state = cm_state;
  2142. cm_id->lap_state = lap_state;
  2143. cm_id_priv->service_timeout = service_timeout;
  2144. cm_set_private_data(cm_id_priv, data, private_data_len);
  2145. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2146. return 0;
  2147. error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2148. kfree(data);
  2149. return ret;
  2150. error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2151. kfree(data);
  2152. cm_free_msg(msg);
  2153. return ret;
  2154. }
  2155. EXPORT_SYMBOL(ib_send_cm_mra);
  2156. static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
  2157. {
  2158. switch (cm_mra_get_msg_mraed(mra_msg)) {
  2159. case CM_MSG_RESPONSE_REQ:
  2160. return cm_acquire_id(mra_msg->remote_comm_id, 0);
  2161. case CM_MSG_RESPONSE_REP:
  2162. case CM_MSG_RESPONSE_OTHER:
  2163. return cm_acquire_id(mra_msg->remote_comm_id,
  2164. mra_msg->local_comm_id);
  2165. default:
  2166. return NULL;
  2167. }
  2168. }
  2169. static int cm_mra_handler(struct cm_work *work)
  2170. {
  2171. struct cm_id_private *cm_id_priv;
  2172. struct cm_mra_msg *mra_msg;
  2173. int timeout, ret;
  2174. mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
  2175. cm_id_priv = cm_acquire_mraed_id(mra_msg);
  2176. if (!cm_id_priv)
  2177. return -EINVAL;
  2178. work->cm_event.private_data = &mra_msg->private_data;
  2179. work->cm_event.param.mra_rcvd.service_timeout =
  2180. cm_mra_get_service_timeout(mra_msg);
  2181. timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
  2182. cm_convert_to_ms(cm_id_priv->av.timeout);
  2183. spin_lock_irq(&cm_id_priv->lock);
  2184. switch (cm_id_priv->id.state) {
  2185. case IB_CM_REQ_SENT:
  2186. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
  2187. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  2188. cm_id_priv->msg, timeout))
  2189. goto out;
  2190. cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
  2191. break;
  2192. case IB_CM_REP_SENT:
  2193. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
  2194. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  2195. cm_id_priv->msg, timeout))
  2196. goto out;
  2197. cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
  2198. break;
  2199. case IB_CM_ESTABLISHED:
  2200. if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
  2201. cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
  2202. ib_modify_mad(cm_id_priv->av.port->mad_agent,
  2203. cm_id_priv->msg, timeout)) {
  2204. if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
  2205. atomic_long_inc(&work->port->
  2206. counter_group[CM_RECV_DUPLICATES].
  2207. counter[CM_MRA_COUNTER]);
  2208. goto out;
  2209. }
  2210. cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
  2211. break;
  2212. case IB_CM_MRA_REQ_RCVD:
  2213. case IB_CM_MRA_REP_RCVD:
  2214. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2215. counter[CM_MRA_COUNTER]);
  2216. /* fall through */
  2217. default:
  2218. goto out;
  2219. }
  2220. cm_id_priv->msg->context[1] = (void *) (unsigned long)
  2221. cm_id_priv->id.state;
  2222. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2223. if (!ret)
  2224. list_add_tail(&work->list, &cm_id_priv->work_list);
  2225. spin_unlock_irq(&cm_id_priv->lock);
  2226. if (ret)
  2227. cm_process_work(cm_id_priv, work);
  2228. else
  2229. cm_deref_id(cm_id_priv);
  2230. return 0;
  2231. out:
  2232. spin_unlock_irq(&cm_id_priv->lock);
  2233. cm_deref_id(cm_id_priv);
  2234. return -EINVAL;
  2235. }
  2236. static void cm_format_lap(struct cm_lap_msg *lap_msg,
  2237. struct cm_id_private *cm_id_priv,
  2238. struct ib_sa_path_rec *alternate_path,
  2239. const void *private_data,
  2240. u8 private_data_len)
  2241. {
  2242. cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
  2243. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
  2244. lap_msg->local_comm_id = cm_id_priv->id.local_id;
  2245. lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
  2246. cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
  2247. /* todo: need remote CM response timeout */
  2248. cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
  2249. lap_msg->alt_local_lid = alternate_path->slid;
  2250. lap_msg->alt_remote_lid = alternate_path->dlid;
  2251. lap_msg->alt_local_gid = alternate_path->sgid;
  2252. lap_msg->alt_remote_gid = alternate_path->dgid;
  2253. cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
  2254. cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
  2255. lap_msg->alt_hop_limit = alternate_path->hop_limit;
  2256. cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
  2257. cm_lap_set_sl(lap_msg, alternate_path->sl);
  2258. cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
  2259. cm_lap_set_local_ack_timeout(lap_msg,
  2260. cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
  2261. alternate_path->packet_life_time));
  2262. if (private_data && private_data_len)
  2263. memcpy(lap_msg->private_data, private_data, private_data_len);
  2264. }
  2265. int ib_send_cm_lap(struct ib_cm_id *cm_id,
  2266. struct ib_sa_path_rec *alternate_path,
  2267. const void *private_data,
  2268. u8 private_data_len)
  2269. {
  2270. struct cm_id_private *cm_id_priv;
  2271. struct ib_mad_send_buf *msg;
  2272. unsigned long flags;
  2273. int ret;
  2274. if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
  2275. return -EINVAL;
  2276. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2277. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2278. if (cm_id->state != IB_CM_ESTABLISHED ||
  2279. (cm_id->lap_state != IB_CM_LAP_UNINIT &&
  2280. cm_id->lap_state != IB_CM_LAP_IDLE)) {
  2281. ret = -EINVAL;
  2282. goto out;
  2283. }
  2284. ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
  2285. if (ret)
  2286. goto out;
  2287. cm_id_priv->alt_av.timeout =
  2288. cm_ack_timeout(cm_id_priv->target_ack_delay,
  2289. cm_id_priv->alt_av.timeout - 1);
  2290. ret = cm_alloc_msg(cm_id_priv, &msg);
  2291. if (ret)
  2292. goto out;
  2293. cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
  2294. alternate_path, private_data, private_data_len);
  2295. msg->timeout_ms = cm_id_priv->timeout_ms;
  2296. msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
  2297. ret = ib_post_send_mad(msg, NULL);
  2298. if (ret) {
  2299. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2300. cm_free_msg(msg);
  2301. return ret;
  2302. }
  2303. cm_id->lap_state = IB_CM_LAP_SENT;
  2304. cm_id_priv->msg = msg;
  2305. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2306. return ret;
  2307. }
  2308. EXPORT_SYMBOL(ib_send_cm_lap);
  2309. static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
  2310. struct ib_sa_path_rec *path,
  2311. struct cm_lap_msg *lap_msg)
  2312. {
  2313. memset(path, 0, sizeof *path);
  2314. path->dgid = lap_msg->alt_local_gid;
  2315. path->sgid = lap_msg->alt_remote_gid;
  2316. path->dlid = lap_msg->alt_local_lid;
  2317. path->slid = lap_msg->alt_remote_lid;
  2318. path->flow_label = cm_lap_get_flow_label(lap_msg);
  2319. path->hop_limit = lap_msg->alt_hop_limit;
  2320. path->traffic_class = cm_lap_get_traffic_class(lap_msg);
  2321. path->reversible = 1;
  2322. path->pkey = cm_id_priv->pkey;
  2323. path->sl = cm_lap_get_sl(lap_msg);
  2324. path->mtu_selector = IB_SA_EQ;
  2325. path->mtu = cm_id_priv->path_mtu;
  2326. path->rate_selector = IB_SA_EQ;
  2327. path->rate = cm_lap_get_packet_rate(lap_msg);
  2328. path->packet_life_time_selector = IB_SA_EQ;
  2329. path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
  2330. path->packet_life_time -= (path->packet_life_time > 0);
  2331. }
  2332. static int cm_lap_handler(struct cm_work *work)
  2333. {
  2334. struct cm_id_private *cm_id_priv;
  2335. struct cm_lap_msg *lap_msg;
  2336. struct ib_cm_lap_event_param *param;
  2337. struct ib_mad_send_buf *msg = NULL;
  2338. int ret;
  2339. /* todo: verify LAP request and send reject APR if invalid. */
  2340. lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
  2341. cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
  2342. lap_msg->local_comm_id);
  2343. if (!cm_id_priv)
  2344. return -EINVAL;
  2345. param = &work->cm_event.param.lap_rcvd;
  2346. param->alternate_path = &work->path[0];
  2347. cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
  2348. work->cm_event.private_data = &lap_msg->private_data;
  2349. spin_lock_irq(&cm_id_priv->lock);
  2350. if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
  2351. goto unlock;
  2352. switch (cm_id_priv->id.lap_state) {
  2353. case IB_CM_LAP_UNINIT:
  2354. case IB_CM_LAP_IDLE:
  2355. break;
  2356. case IB_CM_MRA_LAP_SENT:
  2357. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2358. counter[CM_LAP_COUNTER]);
  2359. if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
  2360. goto unlock;
  2361. cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
  2362. CM_MSG_RESPONSE_OTHER,
  2363. cm_id_priv->service_timeout,
  2364. cm_id_priv->private_data,
  2365. cm_id_priv->private_data_len);
  2366. spin_unlock_irq(&cm_id_priv->lock);
  2367. if (ib_post_send_mad(msg, NULL))
  2368. cm_free_msg(msg);
  2369. goto deref;
  2370. case IB_CM_LAP_RCVD:
  2371. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2372. counter[CM_LAP_COUNTER]);
  2373. goto unlock;
  2374. default:
  2375. goto unlock;
  2376. }
  2377. cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
  2378. cm_id_priv->tid = lap_msg->hdr.tid;
  2379. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  2380. work->mad_recv_wc->recv_buf.grh,
  2381. &cm_id_priv->av);
  2382. cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
  2383. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2384. if (!ret)
  2385. list_add_tail(&work->list, &cm_id_priv->work_list);
  2386. spin_unlock_irq(&cm_id_priv->lock);
  2387. if (ret)
  2388. cm_process_work(cm_id_priv, work);
  2389. else
  2390. cm_deref_id(cm_id_priv);
  2391. return 0;
  2392. unlock: spin_unlock_irq(&cm_id_priv->lock);
  2393. deref: cm_deref_id(cm_id_priv);
  2394. return -EINVAL;
  2395. }
  2396. static void cm_format_apr(struct cm_apr_msg *apr_msg,
  2397. struct cm_id_private *cm_id_priv,
  2398. enum ib_cm_apr_status status,
  2399. void *info,
  2400. u8 info_length,
  2401. const void *private_data,
  2402. u8 private_data_len)
  2403. {
  2404. cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
  2405. apr_msg->local_comm_id = cm_id_priv->id.local_id;
  2406. apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
  2407. apr_msg->ap_status = (u8) status;
  2408. if (info && info_length) {
  2409. apr_msg->info_length = info_length;
  2410. memcpy(apr_msg->info, info, info_length);
  2411. }
  2412. if (private_data && private_data_len)
  2413. memcpy(apr_msg->private_data, private_data, private_data_len);
  2414. }
  2415. int ib_send_cm_apr(struct ib_cm_id *cm_id,
  2416. enum ib_cm_apr_status status,
  2417. void *info,
  2418. u8 info_length,
  2419. const void *private_data,
  2420. u8 private_data_len)
  2421. {
  2422. struct cm_id_private *cm_id_priv;
  2423. struct ib_mad_send_buf *msg;
  2424. unsigned long flags;
  2425. int ret;
  2426. if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
  2427. (info && info_length > IB_CM_APR_INFO_LENGTH))
  2428. return -EINVAL;
  2429. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2430. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2431. if (cm_id->state != IB_CM_ESTABLISHED ||
  2432. (cm_id->lap_state != IB_CM_LAP_RCVD &&
  2433. cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
  2434. ret = -EINVAL;
  2435. goto out;
  2436. }
  2437. ret = cm_alloc_msg(cm_id_priv, &msg);
  2438. if (ret)
  2439. goto out;
  2440. cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
  2441. info, info_length, private_data, private_data_len);
  2442. ret = ib_post_send_mad(msg, NULL);
  2443. if (ret) {
  2444. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2445. cm_free_msg(msg);
  2446. return ret;
  2447. }
  2448. cm_id->lap_state = IB_CM_LAP_IDLE;
  2449. out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2450. return ret;
  2451. }
  2452. EXPORT_SYMBOL(ib_send_cm_apr);
  2453. static int cm_apr_handler(struct cm_work *work)
  2454. {
  2455. struct cm_id_private *cm_id_priv;
  2456. struct cm_apr_msg *apr_msg;
  2457. int ret;
  2458. apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
  2459. cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
  2460. apr_msg->local_comm_id);
  2461. if (!cm_id_priv)
  2462. return -EINVAL; /* Unmatched reply. */
  2463. work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
  2464. work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
  2465. work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
  2466. work->cm_event.private_data = &apr_msg->private_data;
  2467. spin_lock_irq(&cm_id_priv->lock);
  2468. if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
  2469. (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
  2470. cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
  2471. spin_unlock_irq(&cm_id_priv->lock);
  2472. goto out;
  2473. }
  2474. cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
  2475. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2476. cm_id_priv->msg = NULL;
  2477. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2478. if (!ret)
  2479. list_add_tail(&work->list, &cm_id_priv->work_list);
  2480. spin_unlock_irq(&cm_id_priv->lock);
  2481. if (ret)
  2482. cm_process_work(cm_id_priv, work);
  2483. else
  2484. cm_deref_id(cm_id_priv);
  2485. return 0;
  2486. out:
  2487. cm_deref_id(cm_id_priv);
  2488. return -EINVAL;
  2489. }
  2490. static int cm_timewait_handler(struct cm_work *work)
  2491. {
  2492. struct cm_timewait_info *timewait_info;
  2493. struct cm_id_private *cm_id_priv;
  2494. int ret;
  2495. timewait_info = (struct cm_timewait_info *)work;
  2496. spin_lock_irq(&cm.lock);
  2497. list_del(&timewait_info->list);
  2498. spin_unlock_irq(&cm.lock);
  2499. cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
  2500. timewait_info->work.remote_id);
  2501. if (!cm_id_priv)
  2502. return -EINVAL;
  2503. spin_lock_irq(&cm_id_priv->lock);
  2504. if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
  2505. cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
  2506. spin_unlock_irq(&cm_id_priv->lock);
  2507. goto out;
  2508. }
  2509. cm_id_priv->id.state = IB_CM_IDLE;
  2510. ret = atomic_inc_and_test(&cm_id_priv->work_count);
  2511. if (!ret)
  2512. list_add_tail(&work->list, &cm_id_priv->work_list);
  2513. spin_unlock_irq(&cm_id_priv->lock);
  2514. if (ret)
  2515. cm_process_work(cm_id_priv, work);
  2516. else
  2517. cm_deref_id(cm_id_priv);
  2518. return 0;
  2519. out:
  2520. cm_deref_id(cm_id_priv);
  2521. return -EINVAL;
  2522. }
  2523. static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
  2524. struct cm_id_private *cm_id_priv,
  2525. struct ib_cm_sidr_req_param *param)
  2526. {
  2527. cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
  2528. cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
  2529. sidr_req_msg->request_id = cm_id_priv->id.local_id;
  2530. sidr_req_msg->pkey = param->path->pkey;
  2531. sidr_req_msg->service_id = param->service_id;
  2532. if (param->private_data && param->private_data_len)
  2533. memcpy(sidr_req_msg->private_data, param->private_data,
  2534. param->private_data_len);
  2535. }
  2536. int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
  2537. struct ib_cm_sidr_req_param *param)
  2538. {
  2539. struct cm_id_private *cm_id_priv;
  2540. struct ib_mad_send_buf *msg;
  2541. unsigned long flags;
  2542. int ret;
  2543. if (!param->path || (param->private_data &&
  2544. param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
  2545. return -EINVAL;
  2546. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2547. ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
  2548. if (ret)
  2549. goto out;
  2550. cm_id->service_id = param->service_id;
  2551. cm_id->service_mask = ~cpu_to_be64(0);
  2552. cm_id_priv->timeout_ms = param->timeout_ms;
  2553. cm_id_priv->max_cm_retries = param->max_cm_retries;
  2554. ret = cm_alloc_msg(cm_id_priv, &msg);
  2555. if (ret)
  2556. goto out;
  2557. cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
  2558. param);
  2559. msg->timeout_ms = cm_id_priv->timeout_ms;
  2560. msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
  2561. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2562. if (cm_id->state == IB_CM_IDLE)
  2563. ret = ib_post_send_mad(msg, NULL);
  2564. else
  2565. ret = -EINVAL;
  2566. if (ret) {
  2567. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2568. cm_free_msg(msg);
  2569. goto out;
  2570. }
  2571. cm_id->state = IB_CM_SIDR_REQ_SENT;
  2572. cm_id_priv->msg = msg;
  2573. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2574. out:
  2575. return ret;
  2576. }
  2577. EXPORT_SYMBOL(ib_send_cm_sidr_req);
  2578. static void cm_format_sidr_req_event(struct cm_work *work,
  2579. struct ib_cm_id *listen_id)
  2580. {
  2581. struct cm_sidr_req_msg *sidr_req_msg;
  2582. struct ib_cm_sidr_req_event_param *param;
  2583. sidr_req_msg = (struct cm_sidr_req_msg *)
  2584. work->mad_recv_wc->recv_buf.mad;
  2585. param = &work->cm_event.param.sidr_req_rcvd;
  2586. param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
  2587. param->listen_id = listen_id;
  2588. param->port = work->port->port_num;
  2589. work->cm_event.private_data = &sidr_req_msg->private_data;
  2590. }
  2591. static int cm_sidr_req_handler(struct cm_work *work)
  2592. {
  2593. struct ib_cm_id *cm_id;
  2594. struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
  2595. struct cm_sidr_req_msg *sidr_req_msg;
  2596. struct ib_wc *wc;
  2597. cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
  2598. if (IS_ERR(cm_id))
  2599. return PTR_ERR(cm_id);
  2600. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2601. /* Record SGID/SLID and request ID for lookup. */
  2602. sidr_req_msg = (struct cm_sidr_req_msg *)
  2603. work->mad_recv_wc->recv_buf.mad;
  2604. wc = work->mad_recv_wc->wc;
  2605. cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
  2606. cm_id_priv->av.dgid.global.interface_id = 0;
  2607. cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
  2608. work->mad_recv_wc->recv_buf.grh,
  2609. &cm_id_priv->av);
  2610. cm_id_priv->id.remote_id = sidr_req_msg->request_id;
  2611. cm_id_priv->tid = sidr_req_msg->hdr.tid;
  2612. atomic_inc(&cm_id_priv->work_count);
  2613. spin_lock_irq(&cm.lock);
  2614. cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
  2615. if (cur_cm_id_priv) {
  2616. spin_unlock_irq(&cm.lock);
  2617. atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
  2618. counter[CM_SIDR_REQ_COUNTER]);
  2619. goto out; /* Duplicate message. */
  2620. }
  2621. cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
  2622. cur_cm_id_priv = cm_find_listen(cm_id->device,
  2623. sidr_req_msg->service_id,
  2624. sidr_req_msg->private_data);
  2625. if (!cur_cm_id_priv) {
  2626. spin_unlock_irq(&cm.lock);
  2627. cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
  2628. goto out; /* No match. */
  2629. }
  2630. atomic_inc(&cur_cm_id_priv->refcount);
  2631. atomic_inc(&cm_id_priv->refcount);
  2632. spin_unlock_irq(&cm.lock);
  2633. cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
  2634. cm_id_priv->id.context = cur_cm_id_priv->id.context;
  2635. cm_id_priv->id.service_id = sidr_req_msg->service_id;
  2636. cm_id_priv->id.service_mask = ~cpu_to_be64(0);
  2637. cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
  2638. cm_process_work(cm_id_priv, work);
  2639. cm_deref_id(cur_cm_id_priv);
  2640. return 0;
  2641. out:
  2642. ib_destroy_cm_id(&cm_id_priv->id);
  2643. return -EINVAL;
  2644. }
  2645. static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
  2646. struct cm_id_private *cm_id_priv,
  2647. struct ib_cm_sidr_rep_param *param)
  2648. {
  2649. cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
  2650. cm_id_priv->tid);
  2651. sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
  2652. sidr_rep_msg->status = param->status;
  2653. cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
  2654. sidr_rep_msg->service_id = cm_id_priv->id.service_id;
  2655. sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
  2656. if (param->info && param->info_length)
  2657. memcpy(sidr_rep_msg->info, param->info, param->info_length);
  2658. if (param->private_data && param->private_data_len)
  2659. memcpy(sidr_rep_msg->private_data, param->private_data,
  2660. param->private_data_len);
  2661. }
  2662. int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
  2663. struct ib_cm_sidr_rep_param *param)
  2664. {
  2665. struct cm_id_private *cm_id_priv;
  2666. struct ib_mad_send_buf *msg;
  2667. unsigned long flags;
  2668. int ret;
  2669. if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
  2670. (param->private_data &&
  2671. param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
  2672. return -EINVAL;
  2673. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2674. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2675. if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
  2676. ret = -EINVAL;
  2677. goto error;
  2678. }
  2679. ret = cm_alloc_msg(cm_id_priv, &msg);
  2680. if (ret)
  2681. goto error;
  2682. cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
  2683. param);
  2684. ret = ib_post_send_mad(msg, NULL);
  2685. if (ret) {
  2686. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2687. cm_free_msg(msg);
  2688. return ret;
  2689. }
  2690. cm_id->state = IB_CM_IDLE;
  2691. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2692. spin_lock_irqsave(&cm.lock, flags);
  2693. rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
  2694. spin_unlock_irqrestore(&cm.lock, flags);
  2695. return 0;
  2696. error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2697. return ret;
  2698. }
  2699. EXPORT_SYMBOL(ib_send_cm_sidr_rep);
  2700. static void cm_format_sidr_rep_event(struct cm_work *work)
  2701. {
  2702. struct cm_sidr_rep_msg *sidr_rep_msg;
  2703. struct ib_cm_sidr_rep_event_param *param;
  2704. sidr_rep_msg = (struct cm_sidr_rep_msg *)
  2705. work->mad_recv_wc->recv_buf.mad;
  2706. param = &work->cm_event.param.sidr_rep_rcvd;
  2707. param->status = sidr_rep_msg->status;
  2708. param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
  2709. param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
  2710. param->info = &sidr_rep_msg->info;
  2711. param->info_len = sidr_rep_msg->info_length;
  2712. work->cm_event.private_data = &sidr_rep_msg->private_data;
  2713. }
  2714. static int cm_sidr_rep_handler(struct cm_work *work)
  2715. {
  2716. struct cm_sidr_rep_msg *sidr_rep_msg;
  2717. struct cm_id_private *cm_id_priv;
  2718. sidr_rep_msg = (struct cm_sidr_rep_msg *)
  2719. work->mad_recv_wc->recv_buf.mad;
  2720. cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
  2721. if (!cm_id_priv)
  2722. return -EINVAL; /* Unmatched reply. */
  2723. spin_lock_irq(&cm_id_priv->lock);
  2724. if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
  2725. spin_unlock_irq(&cm_id_priv->lock);
  2726. goto out;
  2727. }
  2728. cm_id_priv->id.state = IB_CM_IDLE;
  2729. ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
  2730. spin_unlock_irq(&cm_id_priv->lock);
  2731. cm_format_sidr_rep_event(work);
  2732. cm_process_work(cm_id_priv, work);
  2733. return 0;
  2734. out:
  2735. cm_deref_id(cm_id_priv);
  2736. return -EINVAL;
  2737. }
  2738. static void cm_process_send_error(struct ib_mad_send_buf *msg,
  2739. enum ib_wc_status wc_status)
  2740. {
  2741. struct cm_id_private *cm_id_priv;
  2742. struct ib_cm_event cm_event;
  2743. enum ib_cm_state state;
  2744. int ret;
  2745. memset(&cm_event, 0, sizeof cm_event);
  2746. cm_id_priv = msg->context[0];
  2747. /* Discard old sends or ones without a response. */
  2748. spin_lock_irq(&cm_id_priv->lock);
  2749. state = (enum ib_cm_state) (unsigned long) msg->context[1];
  2750. if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
  2751. goto discard;
  2752. switch (state) {
  2753. case IB_CM_REQ_SENT:
  2754. case IB_CM_MRA_REQ_RCVD:
  2755. cm_reset_to_idle(cm_id_priv);
  2756. cm_event.event = IB_CM_REQ_ERROR;
  2757. break;
  2758. case IB_CM_REP_SENT:
  2759. case IB_CM_MRA_REP_RCVD:
  2760. cm_reset_to_idle(cm_id_priv);
  2761. cm_event.event = IB_CM_REP_ERROR;
  2762. break;
  2763. case IB_CM_DREQ_SENT:
  2764. cm_enter_timewait(cm_id_priv);
  2765. cm_event.event = IB_CM_DREQ_ERROR;
  2766. break;
  2767. case IB_CM_SIDR_REQ_SENT:
  2768. cm_id_priv->id.state = IB_CM_IDLE;
  2769. cm_event.event = IB_CM_SIDR_REQ_ERROR;
  2770. break;
  2771. default:
  2772. goto discard;
  2773. }
  2774. spin_unlock_irq(&cm_id_priv->lock);
  2775. cm_event.param.send_status = wc_status;
  2776. /* No other events can occur on the cm_id at this point. */
  2777. ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
  2778. cm_free_msg(msg);
  2779. if (ret)
  2780. ib_destroy_cm_id(&cm_id_priv->id);
  2781. return;
  2782. discard:
  2783. spin_unlock_irq(&cm_id_priv->lock);
  2784. cm_free_msg(msg);
  2785. }
  2786. static void cm_send_handler(struct ib_mad_agent *mad_agent,
  2787. struct ib_mad_send_wc *mad_send_wc)
  2788. {
  2789. struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
  2790. struct cm_port *port;
  2791. u16 attr_index;
  2792. port = mad_agent->context;
  2793. attr_index = be16_to_cpu(((struct ib_mad_hdr *)
  2794. msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
  2795. /*
  2796. * If the send was in response to a received message (context[0] is not
  2797. * set to a cm_id), and is not a REJ, then it is a send that was
  2798. * manually retried.
  2799. */
  2800. if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
  2801. msg->retries = 1;
  2802. atomic_long_add(1 + msg->retries,
  2803. &port->counter_group[CM_XMIT].counter[attr_index]);
  2804. if (msg->retries)
  2805. atomic_long_add(msg->retries,
  2806. &port->counter_group[CM_XMIT_RETRIES].
  2807. counter[attr_index]);
  2808. switch (mad_send_wc->status) {
  2809. case IB_WC_SUCCESS:
  2810. case IB_WC_WR_FLUSH_ERR:
  2811. cm_free_msg(msg);
  2812. break;
  2813. default:
  2814. if (msg->context[0] && msg->context[1])
  2815. cm_process_send_error(msg, mad_send_wc->status);
  2816. else
  2817. cm_free_msg(msg);
  2818. break;
  2819. }
  2820. }
  2821. static void cm_work_handler(struct work_struct *_work)
  2822. {
  2823. struct cm_work *work = container_of(_work, struct cm_work, work.work);
  2824. int ret;
  2825. switch (work->cm_event.event) {
  2826. case IB_CM_REQ_RECEIVED:
  2827. ret = cm_req_handler(work);
  2828. break;
  2829. case IB_CM_MRA_RECEIVED:
  2830. ret = cm_mra_handler(work);
  2831. break;
  2832. case IB_CM_REJ_RECEIVED:
  2833. ret = cm_rej_handler(work);
  2834. break;
  2835. case IB_CM_REP_RECEIVED:
  2836. ret = cm_rep_handler(work);
  2837. break;
  2838. case IB_CM_RTU_RECEIVED:
  2839. ret = cm_rtu_handler(work);
  2840. break;
  2841. case IB_CM_USER_ESTABLISHED:
  2842. ret = cm_establish_handler(work);
  2843. break;
  2844. case IB_CM_DREQ_RECEIVED:
  2845. ret = cm_dreq_handler(work);
  2846. break;
  2847. case IB_CM_DREP_RECEIVED:
  2848. ret = cm_drep_handler(work);
  2849. break;
  2850. case IB_CM_SIDR_REQ_RECEIVED:
  2851. ret = cm_sidr_req_handler(work);
  2852. break;
  2853. case IB_CM_SIDR_REP_RECEIVED:
  2854. ret = cm_sidr_rep_handler(work);
  2855. break;
  2856. case IB_CM_LAP_RECEIVED:
  2857. ret = cm_lap_handler(work);
  2858. break;
  2859. case IB_CM_APR_RECEIVED:
  2860. ret = cm_apr_handler(work);
  2861. break;
  2862. case IB_CM_TIMEWAIT_EXIT:
  2863. ret = cm_timewait_handler(work);
  2864. break;
  2865. default:
  2866. ret = -EINVAL;
  2867. break;
  2868. }
  2869. if (ret)
  2870. cm_free_work(work);
  2871. }
  2872. static int cm_establish(struct ib_cm_id *cm_id)
  2873. {
  2874. struct cm_id_private *cm_id_priv;
  2875. struct cm_work *work;
  2876. unsigned long flags;
  2877. int ret = 0;
  2878. work = kmalloc(sizeof *work, GFP_ATOMIC);
  2879. if (!work)
  2880. return -ENOMEM;
  2881. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2882. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2883. switch (cm_id->state)
  2884. {
  2885. case IB_CM_REP_SENT:
  2886. case IB_CM_MRA_REP_RCVD:
  2887. cm_id->state = IB_CM_ESTABLISHED;
  2888. break;
  2889. case IB_CM_ESTABLISHED:
  2890. ret = -EISCONN;
  2891. break;
  2892. default:
  2893. ret = -EINVAL;
  2894. break;
  2895. }
  2896. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2897. if (ret) {
  2898. kfree(work);
  2899. goto out;
  2900. }
  2901. /*
  2902. * The CM worker thread may try to destroy the cm_id before it
  2903. * can execute this work item. To prevent potential deadlock,
  2904. * we need to find the cm_id once we're in the context of the
  2905. * worker thread, rather than holding a reference on it.
  2906. */
  2907. INIT_DELAYED_WORK(&work->work, cm_work_handler);
  2908. work->local_id = cm_id->local_id;
  2909. work->remote_id = cm_id->remote_id;
  2910. work->mad_recv_wc = NULL;
  2911. work->cm_event.event = IB_CM_USER_ESTABLISHED;
  2912. queue_delayed_work(cm.wq, &work->work, 0);
  2913. out:
  2914. return ret;
  2915. }
  2916. static int cm_migrate(struct ib_cm_id *cm_id)
  2917. {
  2918. struct cm_id_private *cm_id_priv;
  2919. unsigned long flags;
  2920. int ret = 0;
  2921. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  2922. spin_lock_irqsave(&cm_id_priv->lock, flags);
  2923. if (cm_id->state == IB_CM_ESTABLISHED &&
  2924. (cm_id->lap_state == IB_CM_LAP_UNINIT ||
  2925. cm_id->lap_state == IB_CM_LAP_IDLE)) {
  2926. cm_id->lap_state = IB_CM_LAP_IDLE;
  2927. cm_id_priv->av = cm_id_priv->alt_av;
  2928. } else
  2929. ret = -EINVAL;
  2930. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  2931. return ret;
  2932. }
  2933. int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
  2934. {
  2935. int ret;
  2936. switch (event) {
  2937. case IB_EVENT_COMM_EST:
  2938. ret = cm_establish(cm_id);
  2939. break;
  2940. case IB_EVENT_PATH_MIG:
  2941. ret = cm_migrate(cm_id);
  2942. break;
  2943. default:
  2944. ret = -EINVAL;
  2945. }
  2946. return ret;
  2947. }
  2948. EXPORT_SYMBOL(ib_cm_notify);
  2949. static void cm_recv_handler(struct ib_mad_agent *mad_agent,
  2950. struct ib_mad_recv_wc *mad_recv_wc)
  2951. {
  2952. struct cm_port *port = mad_agent->context;
  2953. struct cm_work *work;
  2954. enum ib_cm_event_type event;
  2955. u16 attr_id;
  2956. int paths = 0;
  2957. switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
  2958. case CM_REQ_ATTR_ID:
  2959. paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
  2960. alt_local_lid != 0);
  2961. event = IB_CM_REQ_RECEIVED;
  2962. break;
  2963. case CM_MRA_ATTR_ID:
  2964. event = IB_CM_MRA_RECEIVED;
  2965. break;
  2966. case CM_REJ_ATTR_ID:
  2967. event = IB_CM_REJ_RECEIVED;
  2968. break;
  2969. case CM_REP_ATTR_ID:
  2970. event = IB_CM_REP_RECEIVED;
  2971. break;
  2972. case CM_RTU_ATTR_ID:
  2973. event = IB_CM_RTU_RECEIVED;
  2974. break;
  2975. case CM_DREQ_ATTR_ID:
  2976. event = IB_CM_DREQ_RECEIVED;
  2977. break;
  2978. case CM_DREP_ATTR_ID:
  2979. event = IB_CM_DREP_RECEIVED;
  2980. break;
  2981. case CM_SIDR_REQ_ATTR_ID:
  2982. event = IB_CM_SIDR_REQ_RECEIVED;
  2983. break;
  2984. case CM_SIDR_REP_ATTR_ID:
  2985. event = IB_CM_SIDR_REP_RECEIVED;
  2986. break;
  2987. case CM_LAP_ATTR_ID:
  2988. paths = 1;
  2989. event = IB_CM_LAP_RECEIVED;
  2990. break;
  2991. case CM_APR_ATTR_ID:
  2992. event = IB_CM_APR_RECEIVED;
  2993. break;
  2994. default:
  2995. ib_free_recv_mad(mad_recv_wc);
  2996. return;
  2997. }
  2998. attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
  2999. atomic_long_inc(&port->counter_group[CM_RECV].
  3000. counter[attr_id - CM_ATTR_ID_OFFSET]);
  3001. work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
  3002. GFP_KERNEL);
  3003. if (!work) {
  3004. ib_free_recv_mad(mad_recv_wc);
  3005. return;
  3006. }
  3007. INIT_DELAYED_WORK(&work->work, cm_work_handler);
  3008. work->cm_event.event = event;
  3009. work->mad_recv_wc = mad_recv_wc;
  3010. work->port = port;
  3011. queue_delayed_work(cm.wq, &work->work, 0);
  3012. }
  3013. static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
  3014. struct ib_qp_attr *qp_attr,
  3015. int *qp_attr_mask)
  3016. {
  3017. unsigned long flags;
  3018. int ret;
  3019. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3020. switch (cm_id_priv->id.state) {
  3021. case IB_CM_REQ_SENT:
  3022. case IB_CM_MRA_REQ_RCVD:
  3023. case IB_CM_REQ_RCVD:
  3024. case IB_CM_MRA_REQ_SENT:
  3025. case IB_CM_REP_RCVD:
  3026. case IB_CM_MRA_REP_SENT:
  3027. case IB_CM_REP_SENT:
  3028. case IB_CM_MRA_REP_RCVD:
  3029. case IB_CM_ESTABLISHED:
  3030. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
  3031. IB_QP_PKEY_INDEX | IB_QP_PORT;
  3032. qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
  3033. if (cm_id_priv->responder_resources)
  3034. qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
  3035. IB_ACCESS_REMOTE_ATOMIC;
  3036. qp_attr->pkey_index = cm_id_priv->av.pkey_index;
  3037. qp_attr->port_num = cm_id_priv->av.port->port_num;
  3038. ret = 0;
  3039. break;
  3040. default:
  3041. ret = -EINVAL;
  3042. break;
  3043. }
  3044. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3045. return ret;
  3046. }
  3047. static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
  3048. struct ib_qp_attr *qp_attr,
  3049. int *qp_attr_mask)
  3050. {
  3051. unsigned long flags;
  3052. int ret;
  3053. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3054. switch (cm_id_priv->id.state) {
  3055. case IB_CM_REQ_RCVD:
  3056. case IB_CM_MRA_REQ_SENT:
  3057. case IB_CM_REP_RCVD:
  3058. case IB_CM_MRA_REP_SENT:
  3059. case IB_CM_REP_SENT:
  3060. case IB_CM_MRA_REP_RCVD:
  3061. case IB_CM_ESTABLISHED:
  3062. *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
  3063. IB_QP_DEST_QPN | IB_QP_RQ_PSN;
  3064. qp_attr->ah_attr = cm_id_priv->av.ah_attr;
  3065. qp_attr->path_mtu = cm_id_priv->path_mtu;
  3066. qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
  3067. qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
  3068. if (cm_id_priv->qp_type == IB_QPT_RC) {
  3069. *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
  3070. IB_QP_MIN_RNR_TIMER;
  3071. qp_attr->max_dest_rd_atomic =
  3072. cm_id_priv->responder_resources;
  3073. qp_attr->min_rnr_timer = 0;
  3074. }
  3075. if (cm_id_priv->alt_av.ah_attr.dlid) {
  3076. *qp_attr_mask |= IB_QP_ALT_PATH;
  3077. qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
  3078. qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
  3079. qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
  3080. qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
  3081. }
  3082. ret = 0;
  3083. break;
  3084. default:
  3085. ret = -EINVAL;
  3086. break;
  3087. }
  3088. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3089. return ret;
  3090. }
  3091. static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
  3092. struct ib_qp_attr *qp_attr,
  3093. int *qp_attr_mask)
  3094. {
  3095. unsigned long flags;
  3096. int ret;
  3097. spin_lock_irqsave(&cm_id_priv->lock, flags);
  3098. switch (cm_id_priv->id.state) {
  3099. /* Allow transition to RTS before sending REP */
  3100. case IB_CM_REQ_RCVD:
  3101. case IB_CM_MRA_REQ_SENT:
  3102. case IB_CM_REP_RCVD:
  3103. case IB_CM_MRA_REP_SENT:
  3104. case IB_CM_REP_SENT:
  3105. case IB_CM_MRA_REP_RCVD:
  3106. case IB_CM_ESTABLISHED:
  3107. if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
  3108. *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
  3109. qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
  3110. if (cm_id_priv->qp_type == IB_QPT_RC) {
  3111. *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
  3112. IB_QP_RNR_RETRY |
  3113. IB_QP_MAX_QP_RD_ATOMIC;
  3114. qp_attr->timeout = cm_id_priv->av.timeout;
  3115. qp_attr->retry_cnt = cm_id_priv->retry_count;
  3116. qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
  3117. qp_attr->max_rd_atomic =
  3118. cm_id_priv->initiator_depth;
  3119. }
  3120. if (cm_id_priv->alt_av.ah_attr.dlid) {
  3121. *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
  3122. qp_attr->path_mig_state = IB_MIG_REARM;
  3123. }
  3124. } else {
  3125. *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
  3126. qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
  3127. qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
  3128. qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
  3129. qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
  3130. qp_attr->path_mig_state = IB_MIG_REARM;
  3131. }
  3132. ret = 0;
  3133. break;
  3134. default:
  3135. ret = -EINVAL;
  3136. break;
  3137. }
  3138. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  3139. return ret;
  3140. }
  3141. int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
  3142. struct ib_qp_attr *qp_attr,
  3143. int *qp_attr_mask)
  3144. {
  3145. struct cm_id_private *cm_id_priv;
  3146. int ret;
  3147. cm_id_priv = container_of(cm_id, struct cm_id_private, id);
  3148. switch (qp_attr->qp_state) {
  3149. case IB_QPS_INIT:
  3150. ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
  3151. break;
  3152. case IB_QPS_RTR:
  3153. ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
  3154. break;
  3155. case IB_QPS_RTS:
  3156. ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
  3157. break;
  3158. default:
  3159. ret = -EINVAL;
  3160. break;
  3161. }
  3162. return ret;
  3163. }
  3164. EXPORT_SYMBOL(ib_cm_init_qp_attr);
  3165. static void cm_get_ack_delay(struct cm_device *cm_dev)
  3166. {
  3167. struct ib_device_attr attr;
  3168. if (ib_query_device(cm_dev->ib_device, &attr))
  3169. cm_dev->ack_delay = 0; /* acks will rely on packet life time */
  3170. else
  3171. cm_dev->ack_delay = attr.local_ca_ack_delay;
  3172. }
  3173. static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
  3174. char *buf)
  3175. {
  3176. struct cm_counter_group *group;
  3177. struct cm_counter_attribute *cm_attr;
  3178. group = container_of(obj, struct cm_counter_group, obj);
  3179. cm_attr = container_of(attr, struct cm_counter_attribute, attr);
  3180. return sprintf(buf, "%ld\n",
  3181. atomic_long_read(&group->counter[cm_attr->index]));
  3182. }
  3183. static const struct sysfs_ops cm_counter_ops = {
  3184. .show = cm_show_counter
  3185. };
  3186. static struct kobj_type cm_counter_obj_type = {
  3187. .sysfs_ops = &cm_counter_ops,
  3188. .default_attrs = cm_counter_default_attrs
  3189. };
  3190. static void cm_release_port_obj(struct kobject *obj)
  3191. {
  3192. struct cm_port *cm_port;
  3193. cm_port = container_of(obj, struct cm_port, port_obj);
  3194. kfree(cm_port);
  3195. }
  3196. static struct kobj_type cm_port_obj_type = {
  3197. .release = cm_release_port_obj
  3198. };
  3199. static char *cm_devnode(struct device *dev, mode_t *mode)
  3200. {
  3201. if (mode)
  3202. *mode = 0666;
  3203. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  3204. }
  3205. struct class cm_class = {
  3206. .owner = THIS_MODULE,
  3207. .name = "infiniband_cm",
  3208. .devnode = cm_devnode,
  3209. };
  3210. EXPORT_SYMBOL(cm_class);
  3211. static int cm_create_port_fs(struct cm_port *port)
  3212. {
  3213. int i, ret;
  3214. ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
  3215. &port->cm_dev->device->kobj,
  3216. "%d", port->port_num);
  3217. if (ret) {
  3218. kfree(port);
  3219. return ret;
  3220. }
  3221. for (i = 0; i < CM_COUNTER_GROUPS; i++) {
  3222. ret = kobject_init_and_add(&port->counter_group[i].obj,
  3223. &cm_counter_obj_type,
  3224. &port->port_obj,
  3225. "%s", counter_group_names[i]);
  3226. if (ret)
  3227. goto error;
  3228. }
  3229. return 0;
  3230. error:
  3231. while (i--)
  3232. kobject_put(&port->counter_group[i].obj);
  3233. kobject_put(&port->port_obj);
  3234. return ret;
  3235. }
  3236. static void cm_remove_port_fs(struct cm_port *port)
  3237. {
  3238. int i;
  3239. for (i = 0; i < CM_COUNTER_GROUPS; i++)
  3240. kobject_put(&port->counter_group[i].obj);
  3241. kobject_put(&port->port_obj);
  3242. }
  3243. static void cm_add_one(struct ib_device *ib_device)
  3244. {
  3245. struct cm_device *cm_dev;
  3246. struct cm_port *port;
  3247. struct ib_mad_reg_req reg_req = {
  3248. .mgmt_class = IB_MGMT_CLASS_CM,
  3249. .mgmt_class_version = IB_CM_CLASS_VERSION
  3250. };
  3251. struct ib_port_modify port_modify = {
  3252. .set_port_cap_mask = IB_PORT_CM_SUP
  3253. };
  3254. unsigned long flags;
  3255. int ret;
  3256. u8 i;
  3257. if (rdma_node_get_transport(ib_device->node_type) != RDMA_TRANSPORT_IB)
  3258. return;
  3259. cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
  3260. ib_device->phys_port_cnt, GFP_KERNEL);
  3261. if (!cm_dev)
  3262. return;
  3263. cm_dev->ib_device = ib_device;
  3264. cm_get_ack_delay(cm_dev);
  3265. cm_dev->device = device_create(&cm_class, &ib_device->dev,
  3266. MKDEV(0, 0), NULL,
  3267. "%s", ib_device->name);
  3268. if (IS_ERR(cm_dev->device)) {
  3269. kfree(cm_dev);
  3270. return;
  3271. }
  3272. set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
  3273. for (i = 1; i <= ib_device->phys_port_cnt; i++) {
  3274. port = kzalloc(sizeof *port, GFP_KERNEL);
  3275. if (!port)
  3276. goto error1;
  3277. cm_dev->port[i-1] = port;
  3278. port->cm_dev = cm_dev;
  3279. port->port_num = i;
  3280. ret = cm_create_port_fs(port);
  3281. if (ret)
  3282. goto error1;
  3283. port->mad_agent = ib_register_mad_agent(ib_device, i,
  3284. IB_QPT_GSI,
  3285. &reg_req,
  3286. 0,
  3287. cm_send_handler,
  3288. cm_recv_handler,
  3289. port);
  3290. if (IS_ERR(port->mad_agent))
  3291. goto error2;
  3292. ret = ib_modify_port(ib_device, i, 0, &port_modify);
  3293. if (ret)
  3294. goto error3;
  3295. }
  3296. ib_set_client_data(ib_device, &cm_client, cm_dev);
  3297. write_lock_irqsave(&cm.device_lock, flags);
  3298. list_add_tail(&cm_dev->list, &cm.device_list);
  3299. write_unlock_irqrestore(&cm.device_lock, flags);
  3300. return;
  3301. error3:
  3302. ib_unregister_mad_agent(port->mad_agent);
  3303. error2:
  3304. cm_remove_port_fs(port);
  3305. error1:
  3306. port_modify.set_port_cap_mask = 0;
  3307. port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
  3308. while (--i) {
  3309. port = cm_dev->port[i-1];
  3310. ib_modify_port(ib_device, port->port_num, 0, &port_modify);
  3311. ib_unregister_mad_agent(port->mad_agent);
  3312. cm_remove_port_fs(port);
  3313. }
  3314. device_unregister(cm_dev->device);
  3315. kfree(cm_dev);
  3316. }
  3317. static void cm_remove_one(struct ib_device *ib_device)
  3318. {
  3319. struct cm_device *cm_dev;
  3320. struct cm_port *port;
  3321. struct ib_port_modify port_modify = {
  3322. .clr_port_cap_mask = IB_PORT_CM_SUP
  3323. };
  3324. unsigned long flags;
  3325. int i;
  3326. cm_dev = ib_get_client_data(ib_device, &cm_client);
  3327. if (!cm_dev)
  3328. return;
  3329. write_lock_irqsave(&cm.device_lock, flags);
  3330. list_del(&cm_dev->list);
  3331. write_unlock_irqrestore(&cm.device_lock, flags);
  3332. for (i = 1; i <= ib_device->phys_port_cnt; i++) {
  3333. port = cm_dev->port[i-1];
  3334. ib_modify_port(ib_device, port->port_num, 0, &port_modify);
  3335. ib_unregister_mad_agent(port->mad_agent);
  3336. flush_workqueue(cm.wq);
  3337. cm_remove_port_fs(port);
  3338. }
  3339. device_unregister(cm_dev->device);
  3340. kfree(cm_dev);
  3341. }
  3342. static int __init ib_cm_init(void)
  3343. {
  3344. int ret;
  3345. memset(&cm, 0, sizeof cm);
  3346. INIT_LIST_HEAD(&cm.device_list);
  3347. rwlock_init(&cm.device_lock);
  3348. spin_lock_init(&cm.lock);
  3349. cm.listen_service_table = RB_ROOT;
  3350. cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
  3351. cm.remote_id_table = RB_ROOT;
  3352. cm.remote_qp_table = RB_ROOT;
  3353. cm.remote_sidr_table = RB_ROOT;
  3354. idr_init(&cm.local_id_table);
  3355. get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
  3356. idr_pre_get(&cm.local_id_table, GFP_KERNEL);
  3357. INIT_LIST_HEAD(&cm.timewait_list);
  3358. ret = class_register(&cm_class);
  3359. if (ret)
  3360. return -ENOMEM;
  3361. cm.wq = create_workqueue("ib_cm");
  3362. if (!cm.wq) {
  3363. ret = -ENOMEM;
  3364. goto error1;
  3365. }
  3366. ret = ib_register_client(&cm_client);
  3367. if (ret)
  3368. goto error2;
  3369. return 0;
  3370. error2:
  3371. destroy_workqueue(cm.wq);
  3372. error1:
  3373. class_unregister(&cm_class);
  3374. return ret;
  3375. }
  3376. static void __exit ib_cm_cleanup(void)
  3377. {
  3378. struct cm_timewait_info *timewait_info, *tmp;
  3379. spin_lock_irq(&cm.lock);
  3380. list_for_each_entry(timewait_info, &cm.timewait_list, list)
  3381. cancel_delayed_work(&timewait_info->work.work);
  3382. spin_unlock_irq(&cm.lock);
  3383. ib_unregister_client(&cm_client);
  3384. destroy_workqueue(cm.wq);
  3385. list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
  3386. list_del(&timewait_info->list);
  3387. kfree(timewait_info);
  3388. }
  3389. class_unregister(&cm_class);
  3390. idr_destroy(&cm.local_id_table);
  3391. }
  3392. module_init(ib_cm_init);
  3393. module_exit(ib_cm_cleanup);