cma.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552
  1. /*
  2. * Copyright (c) 2005 Voltaire Inc. All rights reserved.
  3. * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
  4. * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
  5. * Copyright (c) 2005-2006 Intel Corporation. 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/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/mutex.h>
  39. #include <linux/random.h>
  40. #include <linux/idr.h>
  41. #include <linux/inetdevice.h>
  42. #include <linux/slab.h>
  43. #include <linux/module.h>
  44. #include <net/tcp.h>
  45. #include <net/ipv6.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <rdma/rdma_cm_ib.h>
  48. #include <rdma/rdma_netlink.h>
  49. #include <rdma/ib_cache.h>
  50. #include <rdma/ib_cm.h>
  51. #include <rdma/ib_sa.h>
  52. #include <rdma/iw_cm.h>
  53. MODULE_AUTHOR("Sean Hefty");
  54. MODULE_DESCRIPTION("Generic RDMA CM Agent");
  55. MODULE_LICENSE("Dual BSD/GPL");
  56. #define CMA_CM_RESPONSE_TIMEOUT 20
  57. #define CMA_MAX_CM_RETRIES 15
  58. #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
  59. #define CMA_IBOE_PACKET_LIFETIME 18
  60. static void cma_add_one(struct ib_device *device);
  61. static void cma_remove_one(struct ib_device *device);
  62. static struct ib_client cma_client = {
  63. .name = "cma",
  64. .add = cma_add_one,
  65. .remove = cma_remove_one
  66. };
  67. static struct ib_sa_client sa_client;
  68. static struct rdma_addr_client addr_client;
  69. static LIST_HEAD(dev_list);
  70. static LIST_HEAD(listen_any_list);
  71. static DEFINE_MUTEX(lock);
  72. static struct workqueue_struct *cma_wq;
  73. static DEFINE_IDR(sdp_ps);
  74. static DEFINE_IDR(tcp_ps);
  75. static DEFINE_IDR(udp_ps);
  76. static DEFINE_IDR(ipoib_ps);
  77. static DEFINE_IDR(ib_ps);
  78. struct cma_device {
  79. struct list_head list;
  80. struct ib_device *device;
  81. struct completion comp;
  82. atomic_t refcount;
  83. struct list_head id_list;
  84. };
  85. struct rdma_bind_list {
  86. struct idr *ps;
  87. struct hlist_head owners;
  88. unsigned short port;
  89. };
  90. enum {
  91. CMA_OPTION_AFONLY,
  92. };
  93. /*
  94. * Device removal can occur at anytime, so we need extra handling to
  95. * serialize notifying the user of device removal with other callbacks.
  96. * We do this by disabling removal notification while a callback is in process,
  97. * and reporting it after the callback completes.
  98. */
  99. struct rdma_id_private {
  100. struct rdma_cm_id id;
  101. struct rdma_bind_list *bind_list;
  102. struct hlist_node node;
  103. struct list_head list; /* listen_any_list or cma_device.list */
  104. struct list_head listen_list; /* per device listens */
  105. struct cma_device *cma_dev;
  106. struct list_head mc_list;
  107. int internal_id;
  108. enum rdma_cm_state state;
  109. spinlock_t lock;
  110. struct mutex qp_mutex;
  111. struct completion comp;
  112. atomic_t refcount;
  113. struct mutex handler_mutex;
  114. int backlog;
  115. int timeout_ms;
  116. struct ib_sa_query *query;
  117. int query_id;
  118. union {
  119. struct ib_cm_id *ib;
  120. struct iw_cm_id *iw;
  121. } cm_id;
  122. u32 seq_num;
  123. u32 qkey;
  124. u32 qp_num;
  125. pid_t owner;
  126. u32 options;
  127. u8 srq;
  128. u8 tos;
  129. u8 reuseaddr;
  130. u8 afonly;
  131. };
  132. struct cma_multicast {
  133. struct rdma_id_private *id_priv;
  134. union {
  135. struct ib_sa_multicast *ib;
  136. } multicast;
  137. struct list_head list;
  138. void *context;
  139. struct sockaddr_storage addr;
  140. struct kref mcref;
  141. };
  142. struct cma_work {
  143. struct work_struct work;
  144. struct rdma_id_private *id;
  145. enum rdma_cm_state old_state;
  146. enum rdma_cm_state new_state;
  147. struct rdma_cm_event event;
  148. };
  149. struct cma_ndev_work {
  150. struct work_struct work;
  151. struct rdma_id_private *id;
  152. struct rdma_cm_event event;
  153. };
  154. struct iboe_mcast_work {
  155. struct work_struct work;
  156. struct rdma_id_private *id;
  157. struct cma_multicast *mc;
  158. };
  159. union cma_ip_addr {
  160. struct in6_addr ip6;
  161. struct {
  162. __be32 pad[3];
  163. __be32 addr;
  164. } ip4;
  165. };
  166. struct cma_hdr {
  167. u8 cma_version;
  168. u8 ip_version; /* IP version: 7:4 */
  169. __be16 port;
  170. union cma_ip_addr src_addr;
  171. union cma_ip_addr dst_addr;
  172. };
  173. struct sdp_hh {
  174. u8 bsdh[16];
  175. u8 sdp_version; /* Major version: 7:4 */
  176. u8 ip_version; /* IP version: 7:4 */
  177. u8 sdp_specific1[10];
  178. __be16 port;
  179. __be16 sdp_specific2;
  180. union cma_ip_addr src_addr;
  181. union cma_ip_addr dst_addr;
  182. };
  183. struct sdp_hah {
  184. u8 bsdh[16];
  185. u8 sdp_version;
  186. };
  187. #define CMA_VERSION 0x00
  188. #define SDP_MAJ_VERSION 0x2
  189. static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
  190. {
  191. unsigned long flags;
  192. int ret;
  193. spin_lock_irqsave(&id_priv->lock, flags);
  194. ret = (id_priv->state == comp);
  195. spin_unlock_irqrestore(&id_priv->lock, flags);
  196. return ret;
  197. }
  198. static int cma_comp_exch(struct rdma_id_private *id_priv,
  199. enum rdma_cm_state comp, enum rdma_cm_state exch)
  200. {
  201. unsigned long flags;
  202. int ret;
  203. spin_lock_irqsave(&id_priv->lock, flags);
  204. if ((ret = (id_priv->state == comp)))
  205. id_priv->state = exch;
  206. spin_unlock_irqrestore(&id_priv->lock, flags);
  207. return ret;
  208. }
  209. static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
  210. enum rdma_cm_state exch)
  211. {
  212. unsigned long flags;
  213. enum rdma_cm_state old;
  214. spin_lock_irqsave(&id_priv->lock, flags);
  215. old = id_priv->state;
  216. id_priv->state = exch;
  217. spin_unlock_irqrestore(&id_priv->lock, flags);
  218. return old;
  219. }
  220. static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
  221. {
  222. return hdr->ip_version >> 4;
  223. }
  224. static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
  225. {
  226. hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
  227. }
  228. static inline u8 sdp_get_majv(u8 sdp_version)
  229. {
  230. return sdp_version >> 4;
  231. }
  232. static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
  233. {
  234. return hh->ip_version >> 4;
  235. }
  236. static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
  237. {
  238. hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
  239. }
  240. static void cma_attach_to_dev(struct rdma_id_private *id_priv,
  241. struct cma_device *cma_dev)
  242. {
  243. atomic_inc(&cma_dev->refcount);
  244. id_priv->cma_dev = cma_dev;
  245. id_priv->id.device = cma_dev->device;
  246. id_priv->id.route.addr.dev_addr.transport =
  247. rdma_node_get_transport(cma_dev->device->node_type);
  248. list_add_tail(&id_priv->list, &cma_dev->id_list);
  249. }
  250. static inline void cma_deref_dev(struct cma_device *cma_dev)
  251. {
  252. if (atomic_dec_and_test(&cma_dev->refcount))
  253. complete(&cma_dev->comp);
  254. }
  255. static inline void release_mc(struct kref *kref)
  256. {
  257. struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
  258. kfree(mc->multicast.ib);
  259. kfree(mc);
  260. }
  261. static void cma_release_dev(struct rdma_id_private *id_priv)
  262. {
  263. mutex_lock(&lock);
  264. list_del(&id_priv->list);
  265. cma_deref_dev(id_priv->cma_dev);
  266. id_priv->cma_dev = NULL;
  267. mutex_unlock(&lock);
  268. }
  269. static int cma_set_qkey(struct rdma_id_private *id_priv)
  270. {
  271. struct ib_sa_mcmember_rec rec;
  272. int ret = 0;
  273. if (id_priv->qkey)
  274. return 0;
  275. switch (id_priv->id.ps) {
  276. case RDMA_PS_UDP:
  277. id_priv->qkey = RDMA_UDP_QKEY;
  278. break;
  279. case RDMA_PS_IPOIB:
  280. ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
  281. ret = ib_sa_get_mcmember_rec(id_priv->id.device,
  282. id_priv->id.port_num, &rec.mgid,
  283. &rec);
  284. if (!ret)
  285. id_priv->qkey = be32_to_cpu(rec.qkey);
  286. break;
  287. default:
  288. break;
  289. }
  290. return ret;
  291. }
  292. static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
  293. {
  294. int i;
  295. int err;
  296. struct ib_port_attr props;
  297. union ib_gid tmp;
  298. err = ib_query_port(device, port_num, &props);
  299. if (err)
  300. return 1;
  301. for (i = 0; i < props.gid_tbl_len; ++i) {
  302. err = ib_query_gid(device, port_num, i, &tmp);
  303. if (err)
  304. return 1;
  305. if (!memcmp(&tmp, gid, sizeof tmp))
  306. return 0;
  307. }
  308. return -EAGAIN;
  309. }
  310. static int cma_acquire_dev(struct rdma_id_private *id_priv)
  311. {
  312. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  313. struct cma_device *cma_dev;
  314. union ib_gid gid, iboe_gid;
  315. int ret = -ENODEV;
  316. u8 port;
  317. enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
  318. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  319. if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
  320. id_priv->id.ps == RDMA_PS_IPOIB)
  321. return -EINVAL;
  322. mutex_lock(&lock);
  323. iboe_addr_get_sgid(dev_addr, &iboe_gid);
  324. memcpy(&gid, dev_addr->src_dev_addr +
  325. rdma_addr_gid_offset(dev_addr), sizeof gid);
  326. list_for_each_entry(cma_dev, &dev_list, list) {
  327. for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
  328. if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
  329. if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
  330. rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
  331. ret = find_gid_port(cma_dev->device, &iboe_gid, port);
  332. else
  333. ret = find_gid_port(cma_dev->device, &gid, port);
  334. if (!ret) {
  335. id_priv->id.port_num = port;
  336. goto out;
  337. } else if (ret == 1)
  338. break;
  339. }
  340. }
  341. }
  342. out:
  343. if (!ret)
  344. cma_attach_to_dev(id_priv, cma_dev);
  345. mutex_unlock(&lock);
  346. return ret;
  347. }
  348. static void cma_deref_id(struct rdma_id_private *id_priv)
  349. {
  350. if (atomic_dec_and_test(&id_priv->refcount))
  351. complete(&id_priv->comp);
  352. }
  353. static int cma_disable_callback(struct rdma_id_private *id_priv,
  354. enum rdma_cm_state state)
  355. {
  356. mutex_lock(&id_priv->handler_mutex);
  357. if (id_priv->state != state) {
  358. mutex_unlock(&id_priv->handler_mutex);
  359. return -EINVAL;
  360. }
  361. return 0;
  362. }
  363. struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
  364. void *context, enum rdma_port_space ps,
  365. enum ib_qp_type qp_type)
  366. {
  367. struct rdma_id_private *id_priv;
  368. id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
  369. if (!id_priv)
  370. return ERR_PTR(-ENOMEM);
  371. id_priv->owner = task_pid_nr(current);
  372. id_priv->state = RDMA_CM_IDLE;
  373. id_priv->id.context = context;
  374. id_priv->id.event_handler = event_handler;
  375. id_priv->id.ps = ps;
  376. id_priv->id.qp_type = qp_type;
  377. spin_lock_init(&id_priv->lock);
  378. mutex_init(&id_priv->qp_mutex);
  379. init_completion(&id_priv->comp);
  380. atomic_set(&id_priv->refcount, 1);
  381. mutex_init(&id_priv->handler_mutex);
  382. INIT_LIST_HEAD(&id_priv->listen_list);
  383. INIT_LIST_HEAD(&id_priv->mc_list);
  384. get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
  385. return &id_priv->id;
  386. }
  387. EXPORT_SYMBOL(rdma_create_id);
  388. static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
  389. {
  390. struct ib_qp_attr qp_attr;
  391. int qp_attr_mask, ret;
  392. qp_attr.qp_state = IB_QPS_INIT;
  393. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  394. if (ret)
  395. return ret;
  396. ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
  397. if (ret)
  398. return ret;
  399. qp_attr.qp_state = IB_QPS_RTR;
  400. ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  401. if (ret)
  402. return ret;
  403. qp_attr.qp_state = IB_QPS_RTS;
  404. qp_attr.sq_psn = 0;
  405. ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
  406. return ret;
  407. }
  408. static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
  409. {
  410. struct ib_qp_attr qp_attr;
  411. int qp_attr_mask, ret;
  412. qp_attr.qp_state = IB_QPS_INIT;
  413. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  414. if (ret)
  415. return ret;
  416. return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
  417. }
  418. int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
  419. struct ib_qp_init_attr *qp_init_attr)
  420. {
  421. struct rdma_id_private *id_priv;
  422. struct ib_qp *qp;
  423. int ret;
  424. id_priv = container_of(id, struct rdma_id_private, id);
  425. if (id->device != pd->device)
  426. return -EINVAL;
  427. qp = ib_create_qp(pd, qp_init_attr);
  428. if (IS_ERR(qp))
  429. return PTR_ERR(qp);
  430. if (id->qp_type == IB_QPT_UD)
  431. ret = cma_init_ud_qp(id_priv, qp);
  432. else
  433. ret = cma_init_conn_qp(id_priv, qp);
  434. if (ret)
  435. goto err;
  436. id->qp = qp;
  437. id_priv->qp_num = qp->qp_num;
  438. id_priv->srq = (qp->srq != NULL);
  439. return 0;
  440. err:
  441. ib_destroy_qp(qp);
  442. return ret;
  443. }
  444. EXPORT_SYMBOL(rdma_create_qp);
  445. void rdma_destroy_qp(struct rdma_cm_id *id)
  446. {
  447. struct rdma_id_private *id_priv;
  448. id_priv = container_of(id, struct rdma_id_private, id);
  449. mutex_lock(&id_priv->qp_mutex);
  450. ib_destroy_qp(id_priv->id.qp);
  451. id_priv->id.qp = NULL;
  452. mutex_unlock(&id_priv->qp_mutex);
  453. }
  454. EXPORT_SYMBOL(rdma_destroy_qp);
  455. static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
  456. struct rdma_conn_param *conn_param)
  457. {
  458. struct ib_qp_attr qp_attr;
  459. int qp_attr_mask, ret;
  460. mutex_lock(&id_priv->qp_mutex);
  461. if (!id_priv->id.qp) {
  462. ret = 0;
  463. goto out;
  464. }
  465. /* Need to update QP attributes from default values. */
  466. qp_attr.qp_state = IB_QPS_INIT;
  467. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  468. if (ret)
  469. goto out;
  470. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
  471. if (ret)
  472. goto out;
  473. qp_attr.qp_state = IB_QPS_RTR;
  474. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  475. if (ret)
  476. goto out;
  477. if (conn_param)
  478. qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
  479. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
  480. out:
  481. mutex_unlock(&id_priv->qp_mutex);
  482. return ret;
  483. }
  484. static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
  485. struct rdma_conn_param *conn_param)
  486. {
  487. struct ib_qp_attr qp_attr;
  488. int qp_attr_mask, ret;
  489. mutex_lock(&id_priv->qp_mutex);
  490. if (!id_priv->id.qp) {
  491. ret = 0;
  492. goto out;
  493. }
  494. qp_attr.qp_state = IB_QPS_RTS;
  495. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  496. if (ret)
  497. goto out;
  498. if (conn_param)
  499. qp_attr.max_rd_atomic = conn_param->initiator_depth;
  500. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
  501. out:
  502. mutex_unlock(&id_priv->qp_mutex);
  503. return ret;
  504. }
  505. static int cma_modify_qp_err(struct rdma_id_private *id_priv)
  506. {
  507. struct ib_qp_attr qp_attr;
  508. int ret;
  509. mutex_lock(&id_priv->qp_mutex);
  510. if (!id_priv->id.qp) {
  511. ret = 0;
  512. goto out;
  513. }
  514. qp_attr.qp_state = IB_QPS_ERR;
  515. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
  516. out:
  517. mutex_unlock(&id_priv->qp_mutex);
  518. return ret;
  519. }
  520. static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
  521. struct ib_qp_attr *qp_attr, int *qp_attr_mask)
  522. {
  523. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  524. int ret;
  525. u16 pkey;
  526. if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
  527. IB_LINK_LAYER_INFINIBAND)
  528. pkey = ib_addr_get_pkey(dev_addr);
  529. else
  530. pkey = 0xffff;
  531. ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
  532. pkey, &qp_attr->pkey_index);
  533. if (ret)
  534. return ret;
  535. qp_attr->port_num = id_priv->id.port_num;
  536. *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
  537. if (id_priv->id.qp_type == IB_QPT_UD) {
  538. ret = cma_set_qkey(id_priv);
  539. if (ret)
  540. return ret;
  541. qp_attr->qkey = id_priv->qkey;
  542. *qp_attr_mask |= IB_QP_QKEY;
  543. } else {
  544. qp_attr->qp_access_flags = 0;
  545. *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
  546. }
  547. return 0;
  548. }
  549. int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
  550. int *qp_attr_mask)
  551. {
  552. struct rdma_id_private *id_priv;
  553. int ret = 0;
  554. id_priv = container_of(id, struct rdma_id_private, id);
  555. switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
  556. case RDMA_TRANSPORT_IB:
  557. if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
  558. ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
  559. else
  560. ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
  561. qp_attr_mask);
  562. if (qp_attr->qp_state == IB_QPS_RTR)
  563. qp_attr->rq_psn = id_priv->seq_num;
  564. break;
  565. case RDMA_TRANSPORT_IWARP:
  566. if (!id_priv->cm_id.iw) {
  567. qp_attr->qp_access_flags = 0;
  568. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
  569. } else
  570. ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
  571. qp_attr_mask);
  572. break;
  573. default:
  574. ret = -ENOSYS;
  575. break;
  576. }
  577. return ret;
  578. }
  579. EXPORT_SYMBOL(rdma_init_qp_attr);
  580. static inline int cma_zero_addr(struct sockaddr *addr)
  581. {
  582. struct in6_addr *ip6;
  583. if (addr->sa_family == AF_INET)
  584. return ipv4_is_zeronet(
  585. ((struct sockaddr_in *)addr)->sin_addr.s_addr);
  586. else {
  587. ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
  588. return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
  589. ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
  590. }
  591. }
  592. static inline int cma_loopback_addr(struct sockaddr *addr)
  593. {
  594. if (addr->sa_family == AF_INET)
  595. return ipv4_is_loopback(
  596. ((struct sockaddr_in *) addr)->sin_addr.s_addr);
  597. else
  598. return ipv6_addr_loopback(
  599. &((struct sockaddr_in6 *) addr)->sin6_addr);
  600. }
  601. static inline int cma_any_addr(struct sockaddr *addr)
  602. {
  603. return cma_zero_addr(addr) || cma_loopback_addr(addr);
  604. }
  605. static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
  606. {
  607. if (src->sa_family != dst->sa_family)
  608. return -1;
  609. switch (src->sa_family) {
  610. case AF_INET:
  611. return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
  612. ((struct sockaddr_in *) dst)->sin_addr.s_addr;
  613. default:
  614. return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
  615. &((struct sockaddr_in6 *) dst)->sin6_addr);
  616. }
  617. }
  618. static inline __be16 cma_port(struct sockaddr *addr)
  619. {
  620. if (addr->sa_family == AF_INET)
  621. return ((struct sockaddr_in *) addr)->sin_port;
  622. else
  623. return ((struct sockaddr_in6 *) addr)->sin6_port;
  624. }
  625. static inline int cma_any_port(struct sockaddr *addr)
  626. {
  627. return !cma_port(addr);
  628. }
  629. static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
  630. u8 *ip_ver, __be16 *port,
  631. union cma_ip_addr **src, union cma_ip_addr **dst)
  632. {
  633. switch (ps) {
  634. case RDMA_PS_SDP:
  635. if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
  636. SDP_MAJ_VERSION)
  637. return -EINVAL;
  638. *ip_ver = sdp_get_ip_ver(hdr);
  639. *port = ((struct sdp_hh *) hdr)->port;
  640. *src = &((struct sdp_hh *) hdr)->src_addr;
  641. *dst = &((struct sdp_hh *) hdr)->dst_addr;
  642. break;
  643. default:
  644. if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
  645. return -EINVAL;
  646. *ip_ver = cma_get_ip_ver(hdr);
  647. *port = ((struct cma_hdr *) hdr)->port;
  648. *src = &((struct cma_hdr *) hdr)->src_addr;
  649. *dst = &((struct cma_hdr *) hdr)->dst_addr;
  650. break;
  651. }
  652. if (*ip_ver != 4 && *ip_ver != 6)
  653. return -EINVAL;
  654. return 0;
  655. }
  656. static __be16 ss_get_port(const struct sockaddr_storage *ss)
  657. {
  658. if (ss->ss_family == AF_INET)
  659. return ((struct sockaddr_in *)ss)->sin_port;
  660. else if (ss->ss_family == AF_INET6)
  661. return ((struct sockaddr_in6 *)ss)->sin6_port;
  662. BUG();
  663. }
  664. static void cma_save_net_info(struct rdma_addr *addr,
  665. struct rdma_addr *listen_addr,
  666. u8 ip_ver, __be16 port,
  667. union cma_ip_addr *src, union cma_ip_addr *dst)
  668. {
  669. struct sockaddr_in *ip4;
  670. struct sockaddr_in6 *ip6;
  671. switch (ip_ver) {
  672. case 4:
  673. ip4 = (struct sockaddr_in *) &addr->src_addr;
  674. ip4->sin_family = AF_INET;;
  675. ip4->sin_addr.s_addr = dst->ip4.addr;
  676. ip4->sin_port = ss_get_port(&listen_addr->src_addr);
  677. ip4 = (struct sockaddr_in *) &addr->dst_addr;
  678. ip4->sin_family = AF_INET;
  679. ip4->sin_addr.s_addr = src->ip4.addr;
  680. ip4->sin_port = port;
  681. break;
  682. case 6:
  683. ip6 = (struct sockaddr_in6 *) &addr->src_addr;
  684. ip6->sin6_family = AF_INET6;
  685. ip6->sin6_addr = dst->ip6;
  686. ip6->sin6_port = ss_get_port(&listen_addr->src_addr);
  687. ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
  688. ip6->sin6_family = AF_INET6;
  689. ip6->sin6_addr = src->ip6;
  690. ip6->sin6_port = port;
  691. break;
  692. default:
  693. break;
  694. }
  695. }
  696. static inline int cma_user_data_offset(enum rdma_port_space ps)
  697. {
  698. switch (ps) {
  699. case RDMA_PS_SDP:
  700. return 0;
  701. default:
  702. return sizeof(struct cma_hdr);
  703. }
  704. }
  705. static void cma_cancel_route(struct rdma_id_private *id_priv)
  706. {
  707. switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
  708. case IB_LINK_LAYER_INFINIBAND:
  709. if (id_priv->query)
  710. ib_sa_cancel_query(id_priv->query_id, id_priv->query);
  711. break;
  712. default:
  713. break;
  714. }
  715. }
  716. static void cma_cancel_listens(struct rdma_id_private *id_priv)
  717. {
  718. struct rdma_id_private *dev_id_priv;
  719. /*
  720. * Remove from listen_any_list to prevent added devices from spawning
  721. * additional listen requests.
  722. */
  723. mutex_lock(&lock);
  724. list_del(&id_priv->list);
  725. while (!list_empty(&id_priv->listen_list)) {
  726. dev_id_priv = list_entry(id_priv->listen_list.next,
  727. struct rdma_id_private, listen_list);
  728. /* sync with device removal to avoid duplicate destruction */
  729. list_del_init(&dev_id_priv->list);
  730. list_del(&dev_id_priv->listen_list);
  731. mutex_unlock(&lock);
  732. rdma_destroy_id(&dev_id_priv->id);
  733. mutex_lock(&lock);
  734. }
  735. mutex_unlock(&lock);
  736. }
  737. static void cma_cancel_operation(struct rdma_id_private *id_priv,
  738. enum rdma_cm_state state)
  739. {
  740. switch (state) {
  741. case RDMA_CM_ADDR_QUERY:
  742. rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
  743. break;
  744. case RDMA_CM_ROUTE_QUERY:
  745. cma_cancel_route(id_priv);
  746. break;
  747. case RDMA_CM_LISTEN:
  748. if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
  749. && !id_priv->cma_dev)
  750. cma_cancel_listens(id_priv);
  751. break;
  752. default:
  753. break;
  754. }
  755. }
  756. static void cma_release_port(struct rdma_id_private *id_priv)
  757. {
  758. struct rdma_bind_list *bind_list = id_priv->bind_list;
  759. if (!bind_list)
  760. return;
  761. mutex_lock(&lock);
  762. hlist_del(&id_priv->node);
  763. if (hlist_empty(&bind_list->owners)) {
  764. idr_remove(bind_list->ps, bind_list->port);
  765. kfree(bind_list);
  766. }
  767. mutex_unlock(&lock);
  768. }
  769. static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
  770. {
  771. struct cma_multicast *mc;
  772. while (!list_empty(&id_priv->mc_list)) {
  773. mc = container_of(id_priv->mc_list.next,
  774. struct cma_multicast, list);
  775. list_del(&mc->list);
  776. switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
  777. case IB_LINK_LAYER_INFINIBAND:
  778. ib_sa_free_multicast(mc->multicast.ib);
  779. kfree(mc);
  780. break;
  781. case IB_LINK_LAYER_ETHERNET:
  782. kref_put(&mc->mcref, release_mc);
  783. break;
  784. default:
  785. break;
  786. }
  787. }
  788. }
  789. void rdma_destroy_id(struct rdma_cm_id *id)
  790. {
  791. struct rdma_id_private *id_priv;
  792. enum rdma_cm_state state;
  793. id_priv = container_of(id, struct rdma_id_private, id);
  794. state = cma_exch(id_priv, RDMA_CM_DESTROYING);
  795. cma_cancel_operation(id_priv, state);
  796. /*
  797. * Wait for any active callback to finish. New callbacks will find
  798. * the id_priv state set to destroying and abort.
  799. */
  800. mutex_lock(&id_priv->handler_mutex);
  801. mutex_unlock(&id_priv->handler_mutex);
  802. if (id_priv->cma_dev) {
  803. switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
  804. case RDMA_TRANSPORT_IB:
  805. if (id_priv->cm_id.ib)
  806. ib_destroy_cm_id(id_priv->cm_id.ib);
  807. break;
  808. case RDMA_TRANSPORT_IWARP:
  809. if (id_priv->cm_id.iw)
  810. iw_destroy_cm_id(id_priv->cm_id.iw);
  811. break;
  812. default:
  813. break;
  814. }
  815. cma_leave_mc_groups(id_priv);
  816. cma_release_dev(id_priv);
  817. }
  818. cma_release_port(id_priv);
  819. cma_deref_id(id_priv);
  820. wait_for_completion(&id_priv->comp);
  821. if (id_priv->internal_id)
  822. cma_deref_id(id_priv->id.context);
  823. kfree(id_priv->id.route.path_rec);
  824. kfree(id_priv);
  825. }
  826. EXPORT_SYMBOL(rdma_destroy_id);
  827. static int cma_rep_recv(struct rdma_id_private *id_priv)
  828. {
  829. int ret;
  830. ret = cma_modify_qp_rtr(id_priv, NULL);
  831. if (ret)
  832. goto reject;
  833. ret = cma_modify_qp_rts(id_priv, NULL);
  834. if (ret)
  835. goto reject;
  836. ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
  837. if (ret)
  838. goto reject;
  839. return 0;
  840. reject:
  841. cma_modify_qp_err(id_priv);
  842. ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
  843. NULL, 0, NULL, 0);
  844. return ret;
  845. }
  846. static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
  847. {
  848. if (id_priv->id.ps == RDMA_PS_SDP &&
  849. sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
  850. SDP_MAJ_VERSION)
  851. return -EINVAL;
  852. return 0;
  853. }
  854. static void cma_set_rep_event_data(struct rdma_cm_event *event,
  855. struct ib_cm_rep_event_param *rep_data,
  856. void *private_data)
  857. {
  858. event->param.conn.private_data = private_data;
  859. event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  860. event->param.conn.responder_resources = rep_data->responder_resources;
  861. event->param.conn.initiator_depth = rep_data->initiator_depth;
  862. event->param.conn.flow_control = rep_data->flow_control;
  863. event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
  864. event->param.conn.srq = rep_data->srq;
  865. event->param.conn.qp_num = rep_data->remote_qpn;
  866. }
  867. static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
  868. {
  869. struct rdma_id_private *id_priv = cm_id->context;
  870. struct rdma_cm_event event;
  871. int ret = 0;
  872. if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
  873. cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
  874. (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
  875. cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
  876. return 0;
  877. memset(&event, 0, sizeof event);
  878. switch (ib_event->event) {
  879. case IB_CM_REQ_ERROR:
  880. case IB_CM_REP_ERROR:
  881. event.event = RDMA_CM_EVENT_UNREACHABLE;
  882. event.status = -ETIMEDOUT;
  883. break;
  884. case IB_CM_REP_RECEIVED:
  885. event.status = cma_verify_rep(id_priv, ib_event->private_data);
  886. if (event.status)
  887. event.event = RDMA_CM_EVENT_CONNECT_ERROR;
  888. else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
  889. event.status = cma_rep_recv(id_priv);
  890. event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
  891. RDMA_CM_EVENT_ESTABLISHED;
  892. } else
  893. event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
  894. cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
  895. ib_event->private_data);
  896. break;
  897. case IB_CM_RTU_RECEIVED:
  898. case IB_CM_USER_ESTABLISHED:
  899. event.event = RDMA_CM_EVENT_ESTABLISHED;
  900. break;
  901. case IB_CM_DREQ_ERROR:
  902. event.status = -ETIMEDOUT; /* fall through */
  903. case IB_CM_DREQ_RECEIVED:
  904. case IB_CM_DREP_RECEIVED:
  905. if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
  906. RDMA_CM_DISCONNECT))
  907. goto out;
  908. event.event = RDMA_CM_EVENT_DISCONNECTED;
  909. break;
  910. case IB_CM_TIMEWAIT_EXIT:
  911. event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
  912. break;
  913. case IB_CM_MRA_RECEIVED:
  914. /* ignore event */
  915. goto out;
  916. case IB_CM_REJ_RECEIVED:
  917. cma_modify_qp_err(id_priv);
  918. event.status = ib_event->param.rej_rcvd.reason;
  919. event.event = RDMA_CM_EVENT_REJECTED;
  920. event.param.conn.private_data = ib_event->private_data;
  921. event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  922. break;
  923. default:
  924. printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
  925. ib_event->event);
  926. goto out;
  927. }
  928. ret = id_priv->id.event_handler(&id_priv->id, &event);
  929. if (ret) {
  930. /* Destroy the CM ID by returning a non-zero value. */
  931. id_priv->cm_id.ib = NULL;
  932. cma_exch(id_priv, RDMA_CM_DESTROYING);
  933. mutex_unlock(&id_priv->handler_mutex);
  934. rdma_destroy_id(&id_priv->id);
  935. return ret;
  936. }
  937. out:
  938. mutex_unlock(&id_priv->handler_mutex);
  939. return ret;
  940. }
  941. static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
  942. struct ib_cm_event *ib_event)
  943. {
  944. struct rdma_id_private *id_priv;
  945. struct rdma_cm_id *id;
  946. struct rdma_route *rt;
  947. union cma_ip_addr *src, *dst;
  948. __be16 port;
  949. u8 ip_ver;
  950. int ret;
  951. if (cma_get_net_info(ib_event->private_data, listen_id->ps,
  952. &ip_ver, &port, &src, &dst))
  953. return NULL;
  954. id = rdma_create_id(listen_id->event_handler, listen_id->context,
  955. listen_id->ps, ib_event->param.req_rcvd.qp_type);
  956. if (IS_ERR(id))
  957. return NULL;
  958. cma_save_net_info(&id->route.addr, &listen_id->route.addr,
  959. ip_ver, port, src, dst);
  960. rt = &id->route;
  961. rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
  962. rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
  963. GFP_KERNEL);
  964. if (!rt->path_rec)
  965. goto err;
  966. rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
  967. if (rt->num_paths == 2)
  968. rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
  969. if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
  970. rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
  971. rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
  972. ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
  973. } else {
  974. ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
  975. &rt->addr.dev_addr);
  976. if (ret)
  977. goto err;
  978. }
  979. rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
  980. id_priv = container_of(id, struct rdma_id_private, id);
  981. id_priv->state = RDMA_CM_CONNECT;
  982. return id_priv;
  983. err:
  984. rdma_destroy_id(id);
  985. return NULL;
  986. }
  987. static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
  988. struct ib_cm_event *ib_event)
  989. {
  990. struct rdma_id_private *id_priv;
  991. struct rdma_cm_id *id;
  992. union cma_ip_addr *src, *dst;
  993. __be16 port;
  994. u8 ip_ver;
  995. int ret;
  996. id = rdma_create_id(listen_id->event_handler, listen_id->context,
  997. listen_id->ps, IB_QPT_UD);
  998. if (IS_ERR(id))
  999. return NULL;
  1000. if (cma_get_net_info(ib_event->private_data, listen_id->ps,
  1001. &ip_ver, &port, &src, &dst))
  1002. goto err;
  1003. cma_save_net_info(&id->route.addr, &listen_id->route.addr,
  1004. ip_ver, port, src, dst);
  1005. if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
  1006. ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
  1007. &id->route.addr.dev_addr);
  1008. if (ret)
  1009. goto err;
  1010. }
  1011. id_priv = container_of(id, struct rdma_id_private, id);
  1012. id_priv->state = RDMA_CM_CONNECT;
  1013. return id_priv;
  1014. err:
  1015. rdma_destroy_id(id);
  1016. return NULL;
  1017. }
  1018. static void cma_set_req_event_data(struct rdma_cm_event *event,
  1019. struct ib_cm_req_event_param *req_data,
  1020. void *private_data, int offset)
  1021. {
  1022. event->param.conn.private_data = private_data + offset;
  1023. event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
  1024. event->param.conn.responder_resources = req_data->responder_resources;
  1025. event->param.conn.initiator_depth = req_data->initiator_depth;
  1026. event->param.conn.flow_control = req_data->flow_control;
  1027. event->param.conn.retry_count = req_data->retry_count;
  1028. event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
  1029. event->param.conn.srq = req_data->srq;
  1030. event->param.conn.qp_num = req_data->remote_qpn;
  1031. }
  1032. static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
  1033. {
  1034. return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
  1035. (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
  1036. ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
  1037. (id->qp_type == IB_QPT_UD)) ||
  1038. (!id->qp_type));
  1039. }
  1040. static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
  1041. {
  1042. struct rdma_id_private *listen_id, *conn_id;
  1043. struct rdma_cm_event event;
  1044. int offset, ret;
  1045. listen_id = cm_id->context;
  1046. if (!cma_check_req_qp_type(&listen_id->id, ib_event))
  1047. return -EINVAL;
  1048. if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
  1049. return -ECONNABORTED;
  1050. memset(&event, 0, sizeof event);
  1051. offset = cma_user_data_offset(listen_id->id.ps);
  1052. event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
  1053. if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
  1054. conn_id = cma_new_udp_id(&listen_id->id, ib_event);
  1055. event.param.ud.private_data = ib_event->private_data + offset;
  1056. event.param.ud.private_data_len =
  1057. IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
  1058. } else {
  1059. conn_id = cma_new_conn_id(&listen_id->id, ib_event);
  1060. cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
  1061. ib_event->private_data, offset);
  1062. }
  1063. if (!conn_id) {
  1064. ret = -ENOMEM;
  1065. goto out;
  1066. }
  1067. mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
  1068. ret = cma_acquire_dev(conn_id);
  1069. if (ret)
  1070. goto release_conn_id;
  1071. conn_id->cm_id.ib = cm_id;
  1072. cm_id->context = conn_id;
  1073. cm_id->cm_handler = cma_ib_handler;
  1074. /*
  1075. * Protect against the user destroying conn_id from another thread
  1076. * until we're done accessing it.
  1077. */
  1078. atomic_inc(&conn_id->refcount);
  1079. ret = conn_id->id.event_handler(&conn_id->id, &event);
  1080. if (!ret) {
  1081. /*
  1082. * Acquire mutex to prevent user executing rdma_destroy_id()
  1083. * while we're accessing the cm_id.
  1084. */
  1085. mutex_lock(&lock);
  1086. if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD))
  1087. ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
  1088. mutex_unlock(&lock);
  1089. mutex_unlock(&conn_id->handler_mutex);
  1090. cma_deref_id(conn_id);
  1091. goto out;
  1092. }
  1093. cma_deref_id(conn_id);
  1094. /* Destroy the CM ID by returning a non-zero value. */
  1095. conn_id->cm_id.ib = NULL;
  1096. release_conn_id:
  1097. cma_exch(conn_id, RDMA_CM_DESTROYING);
  1098. mutex_unlock(&conn_id->handler_mutex);
  1099. rdma_destroy_id(&conn_id->id);
  1100. out:
  1101. mutex_unlock(&listen_id->handler_mutex);
  1102. return ret;
  1103. }
  1104. static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
  1105. {
  1106. return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
  1107. }
  1108. static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
  1109. struct ib_cm_compare_data *compare)
  1110. {
  1111. struct cma_hdr *cma_data, *cma_mask;
  1112. struct sdp_hh *sdp_data, *sdp_mask;
  1113. __be32 ip4_addr;
  1114. struct in6_addr ip6_addr;
  1115. memset(compare, 0, sizeof *compare);
  1116. cma_data = (void *) compare->data;
  1117. cma_mask = (void *) compare->mask;
  1118. sdp_data = (void *) compare->data;
  1119. sdp_mask = (void *) compare->mask;
  1120. switch (addr->sa_family) {
  1121. case AF_INET:
  1122. ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  1123. if (ps == RDMA_PS_SDP) {
  1124. sdp_set_ip_ver(sdp_data, 4);
  1125. sdp_set_ip_ver(sdp_mask, 0xF);
  1126. sdp_data->dst_addr.ip4.addr = ip4_addr;
  1127. sdp_mask->dst_addr.ip4.addr = htonl(~0);
  1128. } else {
  1129. cma_set_ip_ver(cma_data, 4);
  1130. cma_set_ip_ver(cma_mask, 0xF);
  1131. if (!cma_any_addr(addr)) {
  1132. cma_data->dst_addr.ip4.addr = ip4_addr;
  1133. cma_mask->dst_addr.ip4.addr = htonl(~0);
  1134. }
  1135. }
  1136. break;
  1137. case AF_INET6:
  1138. ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
  1139. if (ps == RDMA_PS_SDP) {
  1140. sdp_set_ip_ver(sdp_data, 6);
  1141. sdp_set_ip_ver(sdp_mask, 0xF);
  1142. sdp_data->dst_addr.ip6 = ip6_addr;
  1143. memset(&sdp_mask->dst_addr.ip6, 0xFF,
  1144. sizeof sdp_mask->dst_addr.ip6);
  1145. } else {
  1146. cma_set_ip_ver(cma_data, 6);
  1147. cma_set_ip_ver(cma_mask, 0xF);
  1148. if (!cma_any_addr(addr)) {
  1149. cma_data->dst_addr.ip6 = ip6_addr;
  1150. memset(&cma_mask->dst_addr.ip6, 0xFF,
  1151. sizeof cma_mask->dst_addr.ip6);
  1152. }
  1153. }
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. }
  1159. static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
  1160. {
  1161. struct rdma_id_private *id_priv = iw_id->context;
  1162. struct rdma_cm_event event;
  1163. struct sockaddr_in *sin;
  1164. int ret = 0;
  1165. if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
  1166. return 0;
  1167. memset(&event, 0, sizeof event);
  1168. switch (iw_event->event) {
  1169. case IW_CM_EVENT_CLOSE:
  1170. event.event = RDMA_CM_EVENT_DISCONNECTED;
  1171. break;
  1172. case IW_CM_EVENT_CONNECT_REPLY:
  1173. sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
  1174. *sin = iw_event->local_addr;
  1175. sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
  1176. *sin = iw_event->remote_addr;
  1177. switch (iw_event->status) {
  1178. case 0:
  1179. event.event = RDMA_CM_EVENT_ESTABLISHED;
  1180. event.param.conn.initiator_depth = iw_event->ird;
  1181. event.param.conn.responder_resources = iw_event->ord;
  1182. break;
  1183. case -ECONNRESET:
  1184. case -ECONNREFUSED:
  1185. event.event = RDMA_CM_EVENT_REJECTED;
  1186. break;
  1187. case -ETIMEDOUT:
  1188. event.event = RDMA_CM_EVENT_UNREACHABLE;
  1189. break;
  1190. default:
  1191. event.event = RDMA_CM_EVENT_CONNECT_ERROR;
  1192. break;
  1193. }
  1194. break;
  1195. case IW_CM_EVENT_ESTABLISHED:
  1196. event.event = RDMA_CM_EVENT_ESTABLISHED;
  1197. event.param.conn.initiator_depth = iw_event->ird;
  1198. event.param.conn.responder_resources = iw_event->ord;
  1199. break;
  1200. default:
  1201. BUG_ON(1);
  1202. }
  1203. event.status = iw_event->status;
  1204. event.param.conn.private_data = iw_event->private_data;
  1205. event.param.conn.private_data_len = iw_event->private_data_len;
  1206. ret = id_priv->id.event_handler(&id_priv->id, &event);
  1207. if (ret) {
  1208. /* Destroy the CM ID by returning a non-zero value. */
  1209. id_priv->cm_id.iw = NULL;
  1210. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1211. mutex_unlock(&id_priv->handler_mutex);
  1212. rdma_destroy_id(&id_priv->id);
  1213. return ret;
  1214. }
  1215. mutex_unlock(&id_priv->handler_mutex);
  1216. return ret;
  1217. }
  1218. static int iw_conn_req_handler(struct iw_cm_id *cm_id,
  1219. struct iw_cm_event *iw_event)
  1220. {
  1221. struct rdma_cm_id *new_cm_id;
  1222. struct rdma_id_private *listen_id, *conn_id;
  1223. struct sockaddr_in *sin;
  1224. struct net_device *dev = NULL;
  1225. struct rdma_cm_event event;
  1226. int ret;
  1227. struct ib_device_attr attr;
  1228. listen_id = cm_id->context;
  1229. if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
  1230. return -ECONNABORTED;
  1231. /* Create a new RDMA id for the new IW CM ID */
  1232. new_cm_id = rdma_create_id(listen_id->id.event_handler,
  1233. listen_id->id.context,
  1234. RDMA_PS_TCP, IB_QPT_RC);
  1235. if (IS_ERR(new_cm_id)) {
  1236. ret = -ENOMEM;
  1237. goto out;
  1238. }
  1239. conn_id = container_of(new_cm_id, struct rdma_id_private, id);
  1240. mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
  1241. conn_id->state = RDMA_CM_CONNECT;
  1242. dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
  1243. if (!dev) {
  1244. ret = -EADDRNOTAVAIL;
  1245. mutex_unlock(&conn_id->handler_mutex);
  1246. rdma_destroy_id(new_cm_id);
  1247. goto out;
  1248. }
  1249. ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
  1250. if (ret) {
  1251. mutex_unlock(&conn_id->handler_mutex);
  1252. rdma_destroy_id(new_cm_id);
  1253. goto out;
  1254. }
  1255. ret = cma_acquire_dev(conn_id);
  1256. if (ret) {
  1257. mutex_unlock(&conn_id->handler_mutex);
  1258. rdma_destroy_id(new_cm_id);
  1259. goto out;
  1260. }
  1261. conn_id->cm_id.iw = cm_id;
  1262. cm_id->context = conn_id;
  1263. cm_id->cm_handler = cma_iw_handler;
  1264. sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
  1265. *sin = iw_event->local_addr;
  1266. sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
  1267. *sin = iw_event->remote_addr;
  1268. ret = ib_query_device(conn_id->id.device, &attr);
  1269. if (ret) {
  1270. mutex_unlock(&conn_id->handler_mutex);
  1271. rdma_destroy_id(new_cm_id);
  1272. goto out;
  1273. }
  1274. memset(&event, 0, sizeof event);
  1275. event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
  1276. event.param.conn.private_data = iw_event->private_data;
  1277. event.param.conn.private_data_len = iw_event->private_data_len;
  1278. event.param.conn.initiator_depth = iw_event->ird;
  1279. event.param.conn.responder_resources = iw_event->ord;
  1280. /*
  1281. * Protect against the user destroying conn_id from another thread
  1282. * until we're done accessing it.
  1283. */
  1284. atomic_inc(&conn_id->refcount);
  1285. ret = conn_id->id.event_handler(&conn_id->id, &event);
  1286. if (ret) {
  1287. /* User wants to destroy the CM ID */
  1288. conn_id->cm_id.iw = NULL;
  1289. cma_exch(conn_id, RDMA_CM_DESTROYING);
  1290. mutex_unlock(&conn_id->handler_mutex);
  1291. cma_deref_id(conn_id);
  1292. rdma_destroy_id(&conn_id->id);
  1293. goto out;
  1294. }
  1295. mutex_unlock(&conn_id->handler_mutex);
  1296. cma_deref_id(conn_id);
  1297. out:
  1298. if (dev)
  1299. dev_put(dev);
  1300. mutex_unlock(&listen_id->handler_mutex);
  1301. return ret;
  1302. }
  1303. static int cma_ib_listen(struct rdma_id_private *id_priv)
  1304. {
  1305. struct ib_cm_compare_data compare_data;
  1306. struct sockaddr *addr;
  1307. struct ib_cm_id *id;
  1308. __be64 svc_id;
  1309. int ret;
  1310. id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
  1311. if (IS_ERR(id))
  1312. return PTR_ERR(id);
  1313. id_priv->cm_id.ib = id;
  1314. addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
  1315. svc_id = cma_get_service_id(id_priv->id.ps, addr);
  1316. if (cma_any_addr(addr) && !id_priv->afonly)
  1317. ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
  1318. else {
  1319. cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
  1320. ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
  1321. }
  1322. if (ret) {
  1323. ib_destroy_cm_id(id_priv->cm_id.ib);
  1324. id_priv->cm_id.ib = NULL;
  1325. }
  1326. return ret;
  1327. }
  1328. static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
  1329. {
  1330. int ret;
  1331. struct sockaddr_in *sin;
  1332. struct iw_cm_id *id;
  1333. id = iw_create_cm_id(id_priv->id.device,
  1334. iw_conn_req_handler,
  1335. id_priv);
  1336. if (IS_ERR(id))
  1337. return PTR_ERR(id);
  1338. id_priv->cm_id.iw = id;
  1339. sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
  1340. id_priv->cm_id.iw->local_addr = *sin;
  1341. ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
  1342. if (ret) {
  1343. iw_destroy_cm_id(id_priv->cm_id.iw);
  1344. id_priv->cm_id.iw = NULL;
  1345. }
  1346. return ret;
  1347. }
  1348. static int cma_listen_handler(struct rdma_cm_id *id,
  1349. struct rdma_cm_event *event)
  1350. {
  1351. struct rdma_id_private *id_priv = id->context;
  1352. id->context = id_priv->id.context;
  1353. id->event_handler = id_priv->id.event_handler;
  1354. return id_priv->id.event_handler(id, event);
  1355. }
  1356. static void cma_listen_on_dev(struct rdma_id_private *id_priv,
  1357. struct cma_device *cma_dev)
  1358. {
  1359. struct rdma_id_private *dev_id_priv;
  1360. struct rdma_cm_id *id;
  1361. int ret;
  1362. id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
  1363. id_priv->id.qp_type);
  1364. if (IS_ERR(id))
  1365. return;
  1366. dev_id_priv = container_of(id, struct rdma_id_private, id);
  1367. dev_id_priv->state = RDMA_CM_ADDR_BOUND;
  1368. memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
  1369. ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
  1370. cma_attach_to_dev(dev_id_priv, cma_dev);
  1371. list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
  1372. atomic_inc(&id_priv->refcount);
  1373. dev_id_priv->internal_id = 1;
  1374. dev_id_priv->afonly = id_priv->afonly;
  1375. ret = rdma_listen(id, id_priv->backlog);
  1376. if (ret)
  1377. printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
  1378. "listening on device %s\n", ret, cma_dev->device->name);
  1379. }
  1380. static void cma_listen_on_all(struct rdma_id_private *id_priv)
  1381. {
  1382. struct cma_device *cma_dev;
  1383. mutex_lock(&lock);
  1384. list_add_tail(&id_priv->list, &listen_any_list);
  1385. list_for_each_entry(cma_dev, &dev_list, list)
  1386. cma_listen_on_dev(id_priv, cma_dev);
  1387. mutex_unlock(&lock);
  1388. }
  1389. void rdma_set_service_type(struct rdma_cm_id *id, int tos)
  1390. {
  1391. struct rdma_id_private *id_priv;
  1392. id_priv = container_of(id, struct rdma_id_private, id);
  1393. id_priv->tos = (u8) tos;
  1394. }
  1395. EXPORT_SYMBOL(rdma_set_service_type);
  1396. static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
  1397. void *context)
  1398. {
  1399. struct cma_work *work = context;
  1400. struct rdma_route *route;
  1401. route = &work->id->id.route;
  1402. if (!status) {
  1403. route->num_paths = 1;
  1404. *route->path_rec = *path_rec;
  1405. } else {
  1406. work->old_state = RDMA_CM_ROUTE_QUERY;
  1407. work->new_state = RDMA_CM_ADDR_RESOLVED;
  1408. work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
  1409. work->event.status = status;
  1410. }
  1411. queue_work(cma_wq, &work->work);
  1412. }
  1413. static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
  1414. struct cma_work *work)
  1415. {
  1416. struct rdma_addr *addr = &id_priv->id.route.addr;
  1417. struct ib_sa_path_rec path_rec;
  1418. ib_sa_comp_mask comp_mask;
  1419. struct sockaddr_in6 *sin6;
  1420. memset(&path_rec, 0, sizeof path_rec);
  1421. rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
  1422. rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
  1423. path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
  1424. path_rec.numb_path = 1;
  1425. path_rec.reversible = 1;
  1426. path_rec.service_id = cma_get_service_id(id_priv->id.ps,
  1427. (struct sockaddr *) &addr->dst_addr);
  1428. comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
  1429. IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
  1430. IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
  1431. if (addr->src_addr.ss_family == AF_INET) {
  1432. path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
  1433. comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
  1434. } else {
  1435. sin6 = (struct sockaddr_in6 *) &addr->src_addr;
  1436. path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
  1437. comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
  1438. }
  1439. id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
  1440. id_priv->id.port_num, &path_rec,
  1441. comp_mask, timeout_ms,
  1442. GFP_KERNEL, cma_query_handler,
  1443. work, &id_priv->query);
  1444. return (id_priv->query_id < 0) ? id_priv->query_id : 0;
  1445. }
  1446. static void cma_work_handler(struct work_struct *_work)
  1447. {
  1448. struct cma_work *work = container_of(_work, struct cma_work, work);
  1449. struct rdma_id_private *id_priv = work->id;
  1450. int destroy = 0;
  1451. mutex_lock(&id_priv->handler_mutex);
  1452. if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
  1453. goto out;
  1454. if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
  1455. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1456. destroy = 1;
  1457. }
  1458. out:
  1459. mutex_unlock(&id_priv->handler_mutex);
  1460. cma_deref_id(id_priv);
  1461. if (destroy)
  1462. rdma_destroy_id(&id_priv->id);
  1463. kfree(work);
  1464. }
  1465. static void cma_ndev_work_handler(struct work_struct *_work)
  1466. {
  1467. struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
  1468. struct rdma_id_private *id_priv = work->id;
  1469. int destroy = 0;
  1470. mutex_lock(&id_priv->handler_mutex);
  1471. if (id_priv->state == RDMA_CM_DESTROYING ||
  1472. id_priv->state == RDMA_CM_DEVICE_REMOVAL)
  1473. goto out;
  1474. if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
  1475. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1476. destroy = 1;
  1477. }
  1478. out:
  1479. mutex_unlock(&id_priv->handler_mutex);
  1480. cma_deref_id(id_priv);
  1481. if (destroy)
  1482. rdma_destroy_id(&id_priv->id);
  1483. kfree(work);
  1484. }
  1485. static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
  1486. {
  1487. struct rdma_route *route = &id_priv->id.route;
  1488. struct cma_work *work;
  1489. int ret;
  1490. work = kzalloc(sizeof *work, GFP_KERNEL);
  1491. if (!work)
  1492. return -ENOMEM;
  1493. work->id = id_priv;
  1494. INIT_WORK(&work->work, cma_work_handler);
  1495. work->old_state = RDMA_CM_ROUTE_QUERY;
  1496. work->new_state = RDMA_CM_ROUTE_RESOLVED;
  1497. work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  1498. route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
  1499. if (!route->path_rec) {
  1500. ret = -ENOMEM;
  1501. goto err1;
  1502. }
  1503. ret = cma_query_ib_route(id_priv, timeout_ms, work);
  1504. if (ret)
  1505. goto err2;
  1506. return 0;
  1507. err2:
  1508. kfree(route->path_rec);
  1509. route->path_rec = NULL;
  1510. err1:
  1511. kfree(work);
  1512. return ret;
  1513. }
  1514. int rdma_set_ib_paths(struct rdma_cm_id *id,
  1515. struct ib_sa_path_rec *path_rec, int num_paths)
  1516. {
  1517. struct rdma_id_private *id_priv;
  1518. int ret;
  1519. id_priv = container_of(id, struct rdma_id_private, id);
  1520. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
  1521. RDMA_CM_ROUTE_RESOLVED))
  1522. return -EINVAL;
  1523. id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
  1524. GFP_KERNEL);
  1525. if (!id->route.path_rec) {
  1526. ret = -ENOMEM;
  1527. goto err;
  1528. }
  1529. id->route.num_paths = num_paths;
  1530. return 0;
  1531. err:
  1532. cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
  1533. return ret;
  1534. }
  1535. EXPORT_SYMBOL(rdma_set_ib_paths);
  1536. static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
  1537. {
  1538. struct cma_work *work;
  1539. work = kzalloc(sizeof *work, GFP_KERNEL);
  1540. if (!work)
  1541. return -ENOMEM;
  1542. work->id = id_priv;
  1543. INIT_WORK(&work->work, cma_work_handler);
  1544. work->old_state = RDMA_CM_ROUTE_QUERY;
  1545. work->new_state = RDMA_CM_ROUTE_RESOLVED;
  1546. work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  1547. queue_work(cma_wq, &work->work);
  1548. return 0;
  1549. }
  1550. static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
  1551. {
  1552. struct rdma_route *route = &id_priv->id.route;
  1553. struct rdma_addr *addr = &route->addr;
  1554. struct cma_work *work;
  1555. int ret;
  1556. struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
  1557. struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
  1558. struct net_device *ndev = NULL;
  1559. u16 vid;
  1560. if (src_addr->sin_family != dst_addr->sin_family)
  1561. return -EINVAL;
  1562. work = kzalloc(sizeof *work, GFP_KERNEL);
  1563. if (!work)
  1564. return -ENOMEM;
  1565. work->id = id_priv;
  1566. INIT_WORK(&work->work, cma_work_handler);
  1567. route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
  1568. if (!route->path_rec) {
  1569. ret = -ENOMEM;
  1570. goto err1;
  1571. }
  1572. route->num_paths = 1;
  1573. if (addr->dev_addr.bound_dev_if)
  1574. ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
  1575. if (!ndev) {
  1576. ret = -ENODEV;
  1577. goto err2;
  1578. }
  1579. vid = rdma_vlan_dev_vlan_id(ndev);
  1580. iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
  1581. iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
  1582. route->path_rec->hop_limit = 1;
  1583. route->path_rec->reversible = 1;
  1584. route->path_rec->pkey = cpu_to_be16(0xffff);
  1585. route->path_rec->mtu_selector = IB_SA_EQ;
  1586. route->path_rec->sl = id_priv->tos >> 5;
  1587. route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
  1588. route->path_rec->rate_selector = IB_SA_EQ;
  1589. route->path_rec->rate = iboe_get_rate(ndev);
  1590. dev_put(ndev);
  1591. route->path_rec->packet_life_time_selector = IB_SA_EQ;
  1592. route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
  1593. if (!route->path_rec->mtu) {
  1594. ret = -EINVAL;
  1595. goto err2;
  1596. }
  1597. work->old_state = RDMA_CM_ROUTE_QUERY;
  1598. work->new_state = RDMA_CM_ROUTE_RESOLVED;
  1599. work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  1600. work->event.status = 0;
  1601. queue_work(cma_wq, &work->work);
  1602. return 0;
  1603. err2:
  1604. kfree(route->path_rec);
  1605. route->path_rec = NULL;
  1606. err1:
  1607. kfree(work);
  1608. return ret;
  1609. }
  1610. int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
  1611. {
  1612. struct rdma_id_private *id_priv;
  1613. int ret;
  1614. id_priv = container_of(id, struct rdma_id_private, id);
  1615. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
  1616. return -EINVAL;
  1617. atomic_inc(&id_priv->refcount);
  1618. switch (rdma_node_get_transport(id->device->node_type)) {
  1619. case RDMA_TRANSPORT_IB:
  1620. switch (rdma_port_get_link_layer(id->device, id->port_num)) {
  1621. case IB_LINK_LAYER_INFINIBAND:
  1622. ret = cma_resolve_ib_route(id_priv, timeout_ms);
  1623. break;
  1624. case IB_LINK_LAYER_ETHERNET:
  1625. ret = cma_resolve_iboe_route(id_priv);
  1626. break;
  1627. default:
  1628. ret = -ENOSYS;
  1629. }
  1630. break;
  1631. case RDMA_TRANSPORT_IWARP:
  1632. ret = cma_resolve_iw_route(id_priv, timeout_ms);
  1633. break;
  1634. default:
  1635. ret = -ENOSYS;
  1636. break;
  1637. }
  1638. if (ret)
  1639. goto err;
  1640. return 0;
  1641. err:
  1642. cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
  1643. cma_deref_id(id_priv);
  1644. return ret;
  1645. }
  1646. EXPORT_SYMBOL(rdma_resolve_route);
  1647. static int cma_bind_loopback(struct rdma_id_private *id_priv)
  1648. {
  1649. struct cma_device *cma_dev;
  1650. struct ib_port_attr port_attr;
  1651. union ib_gid gid;
  1652. u16 pkey;
  1653. int ret;
  1654. u8 p;
  1655. mutex_lock(&lock);
  1656. if (list_empty(&dev_list)) {
  1657. ret = -ENODEV;
  1658. goto out;
  1659. }
  1660. list_for_each_entry(cma_dev, &dev_list, list)
  1661. for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
  1662. if (!ib_query_port(cma_dev->device, p, &port_attr) &&
  1663. port_attr.state == IB_PORT_ACTIVE)
  1664. goto port_found;
  1665. p = 1;
  1666. cma_dev = list_entry(dev_list.next, struct cma_device, list);
  1667. port_found:
  1668. ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
  1669. if (ret)
  1670. goto out;
  1671. ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
  1672. if (ret)
  1673. goto out;
  1674. id_priv->id.route.addr.dev_addr.dev_type =
  1675. (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
  1676. ARPHRD_INFINIBAND : ARPHRD_ETHER;
  1677. rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
  1678. ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
  1679. id_priv->id.port_num = p;
  1680. cma_attach_to_dev(id_priv, cma_dev);
  1681. out:
  1682. mutex_unlock(&lock);
  1683. return ret;
  1684. }
  1685. static void addr_handler(int status, struct sockaddr *src_addr,
  1686. struct rdma_dev_addr *dev_addr, void *context)
  1687. {
  1688. struct rdma_id_private *id_priv = context;
  1689. struct rdma_cm_event event;
  1690. memset(&event, 0, sizeof event);
  1691. mutex_lock(&id_priv->handler_mutex);
  1692. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
  1693. RDMA_CM_ADDR_RESOLVED))
  1694. goto out;
  1695. if (!status && !id_priv->cma_dev)
  1696. status = cma_acquire_dev(id_priv);
  1697. if (status) {
  1698. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
  1699. RDMA_CM_ADDR_BOUND))
  1700. goto out;
  1701. event.event = RDMA_CM_EVENT_ADDR_ERROR;
  1702. event.status = status;
  1703. } else {
  1704. memcpy(&id_priv->id.route.addr.src_addr, src_addr,
  1705. ip_addr_size(src_addr));
  1706. event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
  1707. }
  1708. if (id_priv->id.event_handler(&id_priv->id, &event)) {
  1709. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1710. mutex_unlock(&id_priv->handler_mutex);
  1711. cma_deref_id(id_priv);
  1712. rdma_destroy_id(&id_priv->id);
  1713. return;
  1714. }
  1715. out:
  1716. mutex_unlock(&id_priv->handler_mutex);
  1717. cma_deref_id(id_priv);
  1718. }
  1719. static int cma_resolve_loopback(struct rdma_id_private *id_priv)
  1720. {
  1721. struct cma_work *work;
  1722. struct sockaddr *src, *dst;
  1723. union ib_gid gid;
  1724. int ret;
  1725. work = kzalloc(sizeof *work, GFP_KERNEL);
  1726. if (!work)
  1727. return -ENOMEM;
  1728. if (!id_priv->cma_dev) {
  1729. ret = cma_bind_loopback(id_priv);
  1730. if (ret)
  1731. goto err;
  1732. }
  1733. rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
  1734. rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
  1735. src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
  1736. if (cma_zero_addr(src)) {
  1737. dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
  1738. if ((src->sa_family = dst->sa_family) == AF_INET) {
  1739. ((struct sockaddr_in *)src)->sin_addr =
  1740. ((struct sockaddr_in *)dst)->sin_addr;
  1741. } else {
  1742. ((struct sockaddr_in6 *)src)->sin6_addr =
  1743. ((struct sockaddr_in6 *)dst)->sin6_addr;
  1744. }
  1745. }
  1746. work->id = id_priv;
  1747. INIT_WORK(&work->work, cma_work_handler);
  1748. work->old_state = RDMA_CM_ADDR_QUERY;
  1749. work->new_state = RDMA_CM_ADDR_RESOLVED;
  1750. work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
  1751. queue_work(cma_wq, &work->work);
  1752. return 0;
  1753. err:
  1754. kfree(work);
  1755. return ret;
  1756. }
  1757. static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
  1758. struct sockaddr *dst_addr)
  1759. {
  1760. if (!src_addr || !src_addr->sa_family) {
  1761. src_addr = (struct sockaddr *) &id->route.addr.src_addr;
  1762. if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
  1763. ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
  1764. ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
  1765. }
  1766. }
  1767. return rdma_bind_addr(id, src_addr);
  1768. }
  1769. int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
  1770. struct sockaddr *dst_addr, int timeout_ms)
  1771. {
  1772. struct rdma_id_private *id_priv;
  1773. int ret;
  1774. id_priv = container_of(id, struct rdma_id_private, id);
  1775. if (id_priv->state == RDMA_CM_IDLE) {
  1776. ret = cma_bind_addr(id, src_addr, dst_addr);
  1777. if (ret)
  1778. return ret;
  1779. }
  1780. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
  1781. return -EINVAL;
  1782. atomic_inc(&id_priv->refcount);
  1783. memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
  1784. if (cma_any_addr(dst_addr))
  1785. ret = cma_resolve_loopback(id_priv);
  1786. else
  1787. ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
  1788. dst_addr, &id->route.addr.dev_addr,
  1789. timeout_ms, addr_handler, id_priv);
  1790. if (ret)
  1791. goto err;
  1792. return 0;
  1793. err:
  1794. cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
  1795. cma_deref_id(id_priv);
  1796. return ret;
  1797. }
  1798. EXPORT_SYMBOL(rdma_resolve_addr);
  1799. int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
  1800. {
  1801. struct rdma_id_private *id_priv;
  1802. unsigned long flags;
  1803. int ret;
  1804. id_priv = container_of(id, struct rdma_id_private, id);
  1805. spin_lock_irqsave(&id_priv->lock, flags);
  1806. if (id_priv->state == RDMA_CM_IDLE) {
  1807. id_priv->reuseaddr = reuse;
  1808. ret = 0;
  1809. } else {
  1810. ret = -EINVAL;
  1811. }
  1812. spin_unlock_irqrestore(&id_priv->lock, flags);
  1813. return ret;
  1814. }
  1815. EXPORT_SYMBOL(rdma_set_reuseaddr);
  1816. int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
  1817. {
  1818. struct rdma_id_private *id_priv;
  1819. unsigned long flags;
  1820. int ret;
  1821. id_priv = container_of(id, struct rdma_id_private, id);
  1822. spin_lock_irqsave(&id_priv->lock, flags);
  1823. if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
  1824. id_priv->options |= (1 << CMA_OPTION_AFONLY);
  1825. id_priv->afonly = afonly;
  1826. ret = 0;
  1827. } else {
  1828. ret = -EINVAL;
  1829. }
  1830. spin_unlock_irqrestore(&id_priv->lock, flags);
  1831. return ret;
  1832. }
  1833. EXPORT_SYMBOL(rdma_set_afonly);
  1834. static void cma_bind_port(struct rdma_bind_list *bind_list,
  1835. struct rdma_id_private *id_priv)
  1836. {
  1837. struct sockaddr_in *sin;
  1838. sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
  1839. sin->sin_port = htons(bind_list->port);
  1840. id_priv->bind_list = bind_list;
  1841. hlist_add_head(&id_priv->node, &bind_list->owners);
  1842. }
  1843. static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
  1844. unsigned short snum)
  1845. {
  1846. struct rdma_bind_list *bind_list;
  1847. int port, ret;
  1848. bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
  1849. if (!bind_list)
  1850. return -ENOMEM;
  1851. do {
  1852. ret = idr_get_new_above(ps, bind_list, snum, &port);
  1853. } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
  1854. if (ret)
  1855. goto err1;
  1856. if (port != snum) {
  1857. ret = -EADDRNOTAVAIL;
  1858. goto err2;
  1859. }
  1860. bind_list->ps = ps;
  1861. bind_list->port = (unsigned short) port;
  1862. cma_bind_port(bind_list, id_priv);
  1863. return 0;
  1864. err2:
  1865. idr_remove(ps, port);
  1866. err1:
  1867. kfree(bind_list);
  1868. return ret;
  1869. }
  1870. static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
  1871. {
  1872. static unsigned int last_used_port;
  1873. int low, high, remaining;
  1874. unsigned int rover;
  1875. inet_get_local_port_range(&low, &high);
  1876. remaining = (high - low) + 1;
  1877. rover = net_random() % remaining + low;
  1878. retry:
  1879. if (last_used_port != rover &&
  1880. !idr_find(ps, (unsigned short) rover)) {
  1881. int ret = cma_alloc_port(ps, id_priv, rover);
  1882. /*
  1883. * Remember previously used port number in order to avoid
  1884. * re-using same port immediately after it is closed.
  1885. */
  1886. if (!ret)
  1887. last_used_port = rover;
  1888. if (ret != -EADDRNOTAVAIL)
  1889. return ret;
  1890. }
  1891. if (--remaining) {
  1892. rover++;
  1893. if ((rover < low) || (rover > high))
  1894. rover = low;
  1895. goto retry;
  1896. }
  1897. return -EADDRNOTAVAIL;
  1898. }
  1899. /*
  1900. * Check that the requested port is available. This is called when trying to
  1901. * bind to a specific port, or when trying to listen on a bound port. In
  1902. * the latter case, the provided id_priv may already be on the bind_list, but
  1903. * we still need to check that it's okay to start listening.
  1904. */
  1905. static int cma_check_port(struct rdma_bind_list *bind_list,
  1906. struct rdma_id_private *id_priv, uint8_t reuseaddr)
  1907. {
  1908. struct rdma_id_private *cur_id;
  1909. struct sockaddr *addr, *cur_addr;
  1910. struct hlist_node *node;
  1911. addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
  1912. hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
  1913. if (id_priv == cur_id)
  1914. continue;
  1915. if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
  1916. cur_id->reuseaddr)
  1917. continue;
  1918. cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
  1919. if (id_priv->afonly && cur_id->afonly &&
  1920. (addr->sa_family != cur_addr->sa_family))
  1921. continue;
  1922. if (cma_any_addr(addr) || cma_any_addr(cur_addr))
  1923. return -EADDRNOTAVAIL;
  1924. if (!cma_addr_cmp(addr, cur_addr))
  1925. return -EADDRINUSE;
  1926. }
  1927. return 0;
  1928. }
  1929. static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
  1930. {
  1931. struct rdma_bind_list *bind_list;
  1932. unsigned short snum;
  1933. int ret;
  1934. snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr));
  1935. if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  1936. return -EACCES;
  1937. bind_list = idr_find(ps, snum);
  1938. if (!bind_list) {
  1939. ret = cma_alloc_port(ps, id_priv, snum);
  1940. } else {
  1941. ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
  1942. if (!ret)
  1943. cma_bind_port(bind_list, id_priv);
  1944. }
  1945. return ret;
  1946. }
  1947. static int cma_bind_listen(struct rdma_id_private *id_priv)
  1948. {
  1949. struct rdma_bind_list *bind_list = id_priv->bind_list;
  1950. int ret = 0;
  1951. mutex_lock(&lock);
  1952. if (bind_list->owners.first->next)
  1953. ret = cma_check_port(bind_list, id_priv, 0);
  1954. mutex_unlock(&lock);
  1955. return ret;
  1956. }
  1957. static int cma_get_port(struct rdma_id_private *id_priv)
  1958. {
  1959. struct idr *ps;
  1960. int ret;
  1961. switch (id_priv->id.ps) {
  1962. case RDMA_PS_SDP:
  1963. ps = &sdp_ps;
  1964. break;
  1965. case RDMA_PS_TCP:
  1966. ps = &tcp_ps;
  1967. break;
  1968. case RDMA_PS_UDP:
  1969. ps = &udp_ps;
  1970. break;
  1971. case RDMA_PS_IPOIB:
  1972. ps = &ipoib_ps;
  1973. break;
  1974. case RDMA_PS_IB:
  1975. ps = &ib_ps;
  1976. break;
  1977. default:
  1978. return -EPROTONOSUPPORT;
  1979. }
  1980. mutex_lock(&lock);
  1981. if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
  1982. ret = cma_alloc_any_port(ps, id_priv);
  1983. else
  1984. ret = cma_use_port(ps, id_priv);
  1985. mutex_unlock(&lock);
  1986. return ret;
  1987. }
  1988. static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
  1989. struct sockaddr *addr)
  1990. {
  1991. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1992. struct sockaddr_in6 *sin6;
  1993. if (addr->sa_family != AF_INET6)
  1994. return 0;
  1995. sin6 = (struct sockaddr_in6 *) addr;
  1996. if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
  1997. !sin6->sin6_scope_id)
  1998. return -EINVAL;
  1999. dev_addr->bound_dev_if = sin6->sin6_scope_id;
  2000. #endif
  2001. return 0;
  2002. }
  2003. int rdma_listen(struct rdma_cm_id *id, int backlog)
  2004. {
  2005. struct rdma_id_private *id_priv;
  2006. int ret;
  2007. id_priv = container_of(id, struct rdma_id_private, id);
  2008. if (id_priv->state == RDMA_CM_IDLE) {
  2009. ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
  2010. ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
  2011. if (ret)
  2012. return ret;
  2013. }
  2014. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
  2015. return -EINVAL;
  2016. if (id_priv->reuseaddr) {
  2017. ret = cma_bind_listen(id_priv);
  2018. if (ret)
  2019. goto err;
  2020. }
  2021. id_priv->backlog = backlog;
  2022. if (id->device) {
  2023. switch (rdma_node_get_transport(id->device->node_type)) {
  2024. case RDMA_TRANSPORT_IB:
  2025. ret = cma_ib_listen(id_priv);
  2026. if (ret)
  2027. goto err;
  2028. break;
  2029. case RDMA_TRANSPORT_IWARP:
  2030. ret = cma_iw_listen(id_priv, backlog);
  2031. if (ret)
  2032. goto err;
  2033. break;
  2034. default:
  2035. ret = -ENOSYS;
  2036. goto err;
  2037. }
  2038. } else
  2039. cma_listen_on_all(id_priv);
  2040. return 0;
  2041. err:
  2042. id_priv->backlog = 0;
  2043. cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
  2044. return ret;
  2045. }
  2046. EXPORT_SYMBOL(rdma_listen);
  2047. int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
  2048. {
  2049. struct rdma_id_private *id_priv;
  2050. int ret;
  2051. if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
  2052. return -EAFNOSUPPORT;
  2053. id_priv = container_of(id, struct rdma_id_private, id);
  2054. if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
  2055. return -EINVAL;
  2056. ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
  2057. if (ret)
  2058. goto err1;
  2059. if (!cma_any_addr(addr)) {
  2060. ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
  2061. if (ret)
  2062. goto err1;
  2063. ret = cma_acquire_dev(id_priv);
  2064. if (ret)
  2065. goto err1;
  2066. }
  2067. memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
  2068. if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
  2069. if (addr->sa_family == AF_INET)
  2070. id_priv->afonly = 1;
  2071. #if IS_ENABLED(CONFIG_IPV6)
  2072. else if (addr->sa_family == AF_INET6)
  2073. id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
  2074. #endif
  2075. }
  2076. ret = cma_get_port(id_priv);
  2077. if (ret)
  2078. goto err2;
  2079. return 0;
  2080. err2:
  2081. if (id_priv->cma_dev)
  2082. cma_release_dev(id_priv);
  2083. err1:
  2084. cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
  2085. return ret;
  2086. }
  2087. EXPORT_SYMBOL(rdma_bind_addr);
  2088. static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
  2089. struct rdma_route *route)
  2090. {
  2091. struct cma_hdr *cma_hdr;
  2092. struct sdp_hh *sdp_hdr;
  2093. if (route->addr.src_addr.ss_family == AF_INET) {
  2094. struct sockaddr_in *src4, *dst4;
  2095. src4 = (struct sockaddr_in *) &route->addr.src_addr;
  2096. dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
  2097. switch (ps) {
  2098. case RDMA_PS_SDP:
  2099. sdp_hdr = hdr;
  2100. if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
  2101. return -EINVAL;
  2102. sdp_set_ip_ver(sdp_hdr, 4);
  2103. sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
  2104. sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
  2105. sdp_hdr->port = src4->sin_port;
  2106. break;
  2107. default:
  2108. cma_hdr = hdr;
  2109. cma_hdr->cma_version = CMA_VERSION;
  2110. cma_set_ip_ver(cma_hdr, 4);
  2111. cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
  2112. cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
  2113. cma_hdr->port = src4->sin_port;
  2114. break;
  2115. }
  2116. } else {
  2117. struct sockaddr_in6 *src6, *dst6;
  2118. src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
  2119. dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
  2120. switch (ps) {
  2121. case RDMA_PS_SDP:
  2122. sdp_hdr = hdr;
  2123. if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
  2124. return -EINVAL;
  2125. sdp_set_ip_ver(sdp_hdr, 6);
  2126. sdp_hdr->src_addr.ip6 = src6->sin6_addr;
  2127. sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
  2128. sdp_hdr->port = src6->sin6_port;
  2129. break;
  2130. default:
  2131. cma_hdr = hdr;
  2132. cma_hdr->cma_version = CMA_VERSION;
  2133. cma_set_ip_ver(cma_hdr, 6);
  2134. cma_hdr->src_addr.ip6 = src6->sin6_addr;
  2135. cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
  2136. cma_hdr->port = src6->sin6_port;
  2137. break;
  2138. }
  2139. }
  2140. return 0;
  2141. }
  2142. static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
  2143. struct ib_cm_event *ib_event)
  2144. {
  2145. struct rdma_id_private *id_priv = cm_id->context;
  2146. struct rdma_cm_event event;
  2147. struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
  2148. int ret = 0;
  2149. if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
  2150. return 0;
  2151. memset(&event, 0, sizeof event);
  2152. switch (ib_event->event) {
  2153. case IB_CM_SIDR_REQ_ERROR:
  2154. event.event = RDMA_CM_EVENT_UNREACHABLE;
  2155. event.status = -ETIMEDOUT;
  2156. break;
  2157. case IB_CM_SIDR_REP_RECEIVED:
  2158. event.param.ud.private_data = ib_event->private_data;
  2159. event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  2160. if (rep->status != IB_SIDR_SUCCESS) {
  2161. event.event = RDMA_CM_EVENT_UNREACHABLE;
  2162. event.status = ib_event->param.sidr_rep_rcvd.status;
  2163. break;
  2164. }
  2165. ret = cma_set_qkey(id_priv);
  2166. if (ret) {
  2167. event.event = RDMA_CM_EVENT_ADDR_ERROR;
  2168. event.status = -EINVAL;
  2169. break;
  2170. }
  2171. if (id_priv->qkey != rep->qkey) {
  2172. event.event = RDMA_CM_EVENT_UNREACHABLE;
  2173. event.status = -EINVAL;
  2174. break;
  2175. }
  2176. ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
  2177. id_priv->id.route.path_rec,
  2178. &event.param.ud.ah_attr);
  2179. event.param.ud.qp_num = rep->qpn;
  2180. event.param.ud.qkey = rep->qkey;
  2181. event.event = RDMA_CM_EVENT_ESTABLISHED;
  2182. event.status = 0;
  2183. break;
  2184. default:
  2185. printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
  2186. ib_event->event);
  2187. goto out;
  2188. }
  2189. ret = id_priv->id.event_handler(&id_priv->id, &event);
  2190. if (ret) {
  2191. /* Destroy the CM ID by returning a non-zero value. */
  2192. id_priv->cm_id.ib = NULL;
  2193. cma_exch(id_priv, RDMA_CM_DESTROYING);
  2194. mutex_unlock(&id_priv->handler_mutex);
  2195. rdma_destroy_id(&id_priv->id);
  2196. return ret;
  2197. }
  2198. out:
  2199. mutex_unlock(&id_priv->handler_mutex);
  2200. return ret;
  2201. }
  2202. static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
  2203. struct rdma_conn_param *conn_param)
  2204. {
  2205. struct ib_cm_sidr_req_param req;
  2206. struct rdma_route *route;
  2207. struct ib_cm_id *id;
  2208. int ret;
  2209. req.private_data_len = sizeof(struct cma_hdr) +
  2210. conn_param->private_data_len;
  2211. if (req.private_data_len < conn_param->private_data_len)
  2212. return -EINVAL;
  2213. req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
  2214. if (!req.private_data)
  2215. return -ENOMEM;
  2216. if (conn_param->private_data && conn_param->private_data_len)
  2217. memcpy((void *) req.private_data + sizeof(struct cma_hdr),
  2218. conn_param->private_data, conn_param->private_data_len);
  2219. route = &id_priv->id.route;
  2220. ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
  2221. if (ret)
  2222. goto out;
  2223. id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
  2224. id_priv);
  2225. if (IS_ERR(id)) {
  2226. ret = PTR_ERR(id);
  2227. goto out;
  2228. }
  2229. id_priv->cm_id.ib = id;
  2230. req.path = route->path_rec;
  2231. req.service_id = cma_get_service_id(id_priv->id.ps,
  2232. (struct sockaddr *) &route->addr.dst_addr);
  2233. req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
  2234. req.max_cm_retries = CMA_MAX_CM_RETRIES;
  2235. ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
  2236. if (ret) {
  2237. ib_destroy_cm_id(id_priv->cm_id.ib);
  2238. id_priv->cm_id.ib = NULL;
  2239. }
  2240. out:
  2241. kfree(req.private_data);
  2242. return ret;
  2243. }
  2244. static int cma_connect_ib(struct rdma_id_private *id_priv,
  2245. struct rdma_conn_param *conn_param)
  2246. {
  2247. struct ib_cm_req_param req;
  2248. struct rdma_route *route;
  2249. void *private_data;
  2250. struct ib_cm_id *id;
  2251. int offset, ret;
  2252. memset(&req, 0, sizeof req);
  2253. offset = cma_user_data_offset(id_priv->id.ps);
  2254. req.private_data_len = offset + conn_param->private_data_len;
  2255. if (req.private_data_len < conn_param->private_data_len)
  2256. return -EINVAL;
  2257. private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
  2258. if (!private_data)
  2259. return -ENOMEM;
  2260. if (conn_param->private_data && conn_param->private_data_len)
  2261. memcpy(private_data + offset, conn_param->private_data,
  2262. conn_param->private_data_len);
  2263. id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
  2264. if (IS_ERR(id)) {
  2265. ret = PTR_ERR(id);
  2266. goto out;
  2267. }
  2268. id_priv->cm_id.ib = id;
  2269. route = &id_priv->id.route;
  2270. ret = cma_format_hdr(private_data, id_priv->id.ps, route);
  2271. if (ret)
  2272. goto out;
  2273. req.private_data = private_data;
  2274. req.primary_path = &route->path_rec[0];
  2275. if (route->num_paths == 2)
  2276. req.alternate_path = &route->path_rec[1];
  2277. req.service_id = cma_get_service_id(id_priv->id.ps,
  2278. (struct sockaddr *) &route->addr.dst_addr);
  2279. req.qp_num = id_priv->qp_num;
  2280. req.qp_type = id_priv->id.qp_type;
  2281. req.starting_psn = id_priv->seq_num;
  2282. req.responder_resources = conn_param->responder_resources;
  2283. req.initiator_depth = conn_param->initiator_depth;
  2284. req.flow_control = conn_param->flow_control;
  2285. req.retry_count = conn_param->retry_count;
  2286. req.rnr_retry_count = conn_param->rnr_retry_count;
  2287. req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
  2288. req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
  2289. req.max_cm_retries = CMA_MAX_CM_RETRIES;
  2290. req.srq = id_priv->srq ? 1 : 0;
  2291. ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
  2292. out:
  2293. if (ret && !IS_ERR(id)) {
  2294. ib_destroy_cm_id(id);
  2295. id_priv->cm_id.ib = NULL;
  2296. }
  2297. kfree(private_data);
  2298. return ret;
  2299. }
  2300. static int cma_connect_iw(struct rdma_id_private *id_priv,
  2301. struct rdma_conn_param *conn_param)
  2302. {
  2303. struct iw_cm_id *cm_id;
  2304. struct sockaddr_in* sin;
  2305. int ret;
  2306. struct iw_cm_conn_param iw_param;
  2307. cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
  2308. if (IS_ERR(cm_id))
  2309. return PTR_ERR(cm_id);
  2310. id_priv->cm_id.iw = cm_id;
  2311. sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
  2312. cm_id->local_addr = *sin;
  2313. sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
  2314. cm_id->remote_addr = *sin;
  2315. ret = cma_modify_qp_rtr(id_priv, conn_param);
  2316. if (ret)
  2317. goto out;
  2318. if (conn_param) {
  2319. iw_param.ord = conn_param->initiator_depth;
  2320. iw_param.ird = conn_param->responder_resources;
  2321. iw_param.private_data = conn_param->private_data;
  2322. iw_param.private_data_len = conn_param->private_data_len;
  2323. iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
  2324. } else {
  2325. memset(&iw_param, 0, sizeof iw_param);
  2326. iw_param.qpn = id_priv->qp_num;
  2327. }
  2328. ret = iw_cm_connect(cm_id, &iw_param);
  2329. out:
  2330. if (ret) {
  2331. iw_destroy_cm_id(cm_id);
  2332. id_priv->cm_id.iw = NULL;
  2333. }
  2334. return ret;
  2335. }
  2336. int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
  2337. {
  2338. struct rdma_id_private *id_priv;
  2339. int ret;
  2340. id_priv = container_of(id, struct rdma_id_private, id);
  2341. if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
  2342. return -EINVAL;
  2343. if (!id->qp) {
  2344. id_priv->qp_num = conn_param->qp_num;
  2345. id_priv->srq = conn_param->srq;
  2346. }
  2347. switch (rdma_node_get_transport(id->device->node_type)) {
  2348. case RDMA_TRANSPORT_IB:
  2349. if (id->qp_type == IB_QPT_UD)
  2350. ret = cma_resolve_ib_udp(id_priv, conn_param);
  2351. else
  2352. ret = cma_connect_ib(id_priv, conn_param);
  2353. break;
  2354. case RDMA_TRANSPORT_IWARP:
  2355. ret = cma_connect_iw(id_priv, conn_param);
  2356. break;
  2357. default:
  2358. ret = -ENOSYS;
  2359. break;
  2360. }
  2361. if (ret)
  2362. goto err;
  2363. return 0;
  2364. err:
  2365. cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
  2366. return ret;
  2367. }
  2368. EXPORT_SYMBOL(rdma_connect);
  2369. static int cma_accept_ib(struct rdma_id_private *id_priv,
  2370. struct rdma_conn_param *conn_param)
  2371. {
  2372. struct ib_cm_rep_param rep;
  2373. int ret;
  2374. ret = cma_modify_qp_rtr(id_priv, conn_param);
  2375. if (ret)
  2376. goto out;
  2377. ret = cma_modify_qp_rts(id_priv, conn_param);
  2378. if (ret)
  2379. goto out;
  2380. memset(&rep, 0, sizeof rep);
  2381. rep.qp_num = id_priv->qp_num;
  2382. rep.starting_psn = id_priv->seq_num;
  2383. rep.private_data = conn_param->private_data;
  2384. rep.private_data_len = conn_param->private_data_len;
  2385. rep.responder_resources = conn_param->responder_resources;
  2386. rep.initiator_depth = conn_param->initiator_depth;
  2387. rep.failover_accepted = 0;
  2388. rep.flow_control = conn_param->flow_control;
  2389. rep.rnr_retry_count = conn_param->rnr_retry_count;
  2390. rep.srq = id_priv->srq ? 1 : 0;
  2391. ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
  2392. out:
  2393. return ret;
  2394. }
  2395. static int cma_accept_iw(struct rdma_id_private *id_priv,
  2396. struct rdma_conn_param *conn_param)
  2397. {
  2398. struct iw_cm_conn_param iw_param;
  2399. int ret;
  2400. ret = cma_modify_qp_rtr(id_priv, conn_param);
  2401. if (ret)
  2402. return ret;
  2403. iw_param.ord = conn_param->initiator_depth;
  2404. iw_param.ird = conn_param->responder_resources;
  2405. iw_param.private_data = conn_param->private_data;
  2406. iw_param.private_data_len = conn_param->private_data_len;
  2407. if (id_priv->id.qp) {
  2408. iw_param.qpn = id_priv->qp_num;
  2409. } else
  2410. iw_param.qpn = conn_param->qp_num;
  2411. return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
  2412. }
  2413. static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
  2414. enum ib_cm_sidr_status status,
  2415. const void *private_data, int private_data_len)
  2416. {
  2417. struct ib_cm_sidr_rep_param rep;
  2418. int ret;
  2419. memset(&rep, 0, sizeof rep);
  2420. rep.status = status;
  2421. if (status == IB_SIDR_SUCCESS) {
  2422. ret = cma_set_qkey(id_priv);
  2423. if (ret)
  2424. return ret;
  2425. rep.qp_num = id_priv->qp_num;
  2426. rep.qkey = id_priv->qkey;
  2427. }
  2428. rep.private_data = private_data;
  2429. rep.private_data_len = private_data_len;
  2430. return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
  2431. }
  2432. int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
  2433. {
  2434. struct rdma_id_private *id_priv;
  2435. int ret;
  2436. id_priv = container_of(id, struct rdma_id_private, id);
  2437. id_priv->owner = task_pid_nr(current);
  2438. if (!cma_comp(id_priv, RDMA_CM_CONNECT))
  2439. return -EINVAL;
  2440. if (!id->qp && conn_param) {
  2441. id_priv->qp_num = conn_param->qp_num;
  2442. id_priv->srq = conn_param->srq;
  2443. }
  2444. switch (rdma_node_get_transport(id->device->node_type)) {
  2445. case RDMA_TRANSPORT_IB:
  2446. if (id->qp_type == IB_QPT_UD) {
  2447. if (conn_param)
  2448. ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
  2449. conn_param->private_data,
  2450. conn_param->private_data_len);
  2451. else
  2452. ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
  2453. NULL, 0);
  2454. } else {
  2455. if (conn_param)
  2456. ret = cma_accept_ib(id_priv, conn_param);
  2457. else
  2458. ret = cma_rep_recv(id_priv);
  2459. }
  2460. break;
  2461. case RDMA_TRANSPORT_IWARP:
  2462. ret = cma_accept_iw(id_priv, conn_param);
  2463. break;
  2464. default:
  2465. ret = -ENOSYS;
  2466. break;
  2467. }
  2468. if (ret)
  2469. goto reject;
  2470. return 0;
  2471. reject:
  2472. cma_modify_qp_err(id_priv);
  2473. rdma_reject(id, NULL, 0);
  2474. return ret;
  2475. }
  2476. EXPORT_SYMBOL(rdma_accept);
  2477. int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
  2478. {
  2479. struct rdma_id_private *id_priv;
  2480. int ret;
  2481. id_priv = container_of(id, struct rdma_id_private, id);
  2482. if (!id_priv->cm_id.ib)
  2483. return -EINVAL;
  2484. switch (id->device->node_type) {
  2485. case RDMA_NODE_IB_CA:
  2486. ret = ib_cm_notify(id_priv->cm_id.ib, event);
  2487. break;
  2488. default:
  2489. ret = 0;
  2490. break;
  2491. }
  2492. return ret;
  2493. }
  2494. EXPORT_SYMBOL(rdma_notify);
  2495. int rdma_reject(struct rdma_cm_id *id, const void *private_data,
  2496. u8 private_data_len)
  2497. {
  2498. struct rdma_id_private *id_priv;
  2499. int ret;
  2500. id_priv = container_of(id, struct rdma_id_private, id);
  2501. if (!id_priv->cm_id.ib)
  2502. return -EINVAL;
  2503. switch (rdma_node_get_transport(id->device->node_type)) {
  2504. case RDMA_TRANSPORT_IB:
  2505. if (id->qp_type == IB_QPT_UD)
  2506. ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
  2507. private_data, private_data_len);
  2508. else
  2509. ret = ib_send_cm_rej(id_priv->cm_id.ib,
  2510. IB_CM_REJ_CONSUMER_DEFINED, NULL,
  2511. 0, private_data, private_data_len);
  2512. break;
  2513. case RDMA_TRANSPORT_IWARP:
  2514. ret = iw_cm_reject(id_priv->cm_id.iw,
  2515. private_data, private_data_len);
  2516. break;
  2517. default:
  2518. ret = -ENOSYS;
  2519. break;
  2520. }
  2521. return ret;
  2522. }
  2523. EXPORT_SYMBOL(rdma_reject);
  2524. int rdma_disconnect(struct rdma_cm_id *id)
  2525. {
  2526. struct rdma_id_private *id_priv;
  2527. int ret;
  2528. id_priv = container_of(id, struct rdma_id_private, id);
  2529. if (!id_priv->cm_id.ib)
  2530. return -EINVAL;
  2531. switch (rdma_node_get_transport(id->device->node_type)) {
  2532. case RDMA_TRANSPORT_IB:
  2533. ret = cma_modify_qp_err(id_priv);
  2534. if (ret)
  2535. goto out;
  2536. /* Initiate or respond to a disconnect. */
  2537. if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
  2538. ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
  2539. break;
  2540. case RDMA_TRANSPORT_IWARP:
  2541. ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
  2542. break;
  2543. default:
  2544. ret = -EINVAL;
  2545. break;
  2546. }
  2547. out:
  2548. return ret;
  2549. }
  2550. EXPORT_SYMBOL(rdma_disconnect);
  2551. static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
  2552. {
  2553. struct rdma_id_private *id_priv;
  2554. struct cma_multicast *mc = multicast->context;
  2555. struct rdma_cm_event event;
  2556. int ret;
  2557. id_priv = mc->id_priv;
  2558. if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
  2559. cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
  2560. return 0;
  2561. mutex_lock(&id_priv->qp_mutex);
  2562. if (!status && id_priv->id.qp)
  2563. status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
  2564. be16_to_cpu(multicast->rec.mlid));
  2565. mutex_unlock(&id_priv->qp_mutex);
  2566. memset(&event, 0, sizeof event);
  2567. event.status = status;
  2568. event.param.ud.private_data = mc->context;
  2569. if (!status) {
  2570. event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
  2571. ib_init_ah_from_mcmember(id_priv->id.device,
  2572. id_priv->id.port_num, &multicast->rec,
  2573. &event.param.ud.ah_attr);
  2574. event.param.ud.qp_num = 0xFFFFFF;
  2575. event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
  2576. } else
  2577. event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
  2578. ret = id_priv->id.event_handler(&id_priv->id, &event);
  2579. if (ret) {
  2580. cma_exch(id_priv, RDMA_CM_DESTROYING);
  2581. mutex_unlock(&id_priv->handler_mutex);
  2582. rdma_destroy_id(&id_priv->id);
  2583. return 0;
  2584. }
  2585. mutex_unlock(&id_priv->handler_mutex);
  2586. return 0;
  2587. }
  2588. static void cma_set_mgid(struct rdma_id_private *id_priv,
  2589. struct sockaddr *addr, union ib_gid *mgid)
  2590. {
  2591. unsigned char mc_map[MAX_ADDR_LEN];
  2592. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  2593. struct sockaddr_in *sin = (struct sockaddr_in *) addr;
  2594. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
  2595. if (cma_any_addr(addr)) {
  2596. memset(mgid, 0, sizeof *mgid);
  2597. } else if ((addr->sa_family == AF_INET6) &&
  2598. ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
  2599. 0xFF10A01B)) {
  2600. /* IPv6 address is an SA assigned MGID. */
  2601. memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
  2602. } else if ((addr->sa_family == AF_INET6)) {
  2603. ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
  2604. if (id_priv->id.ps == RDMA_PS_UDP)
  2605. mc_map[7] = 0x01; /* Use RDMA CM signature */
  2606. *mgid = *(union ib_gid *) (mc_map + 4);
  2607. } else {
  2608. ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
  2609. if (id_priv->id.ps == RDMA_PS_UDP)
  2610. mc_map[7] = 0x01; /* Use RDMA CM signature */
  2611. *mgid = *(union ib_gid *) (mc_map + 4);
  2612. }
  2613. }
  2614. static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
  2615. struct cma_multicast *mc)
  2616. {
  2617. struct ib_sa_mcmember_rec rec;
  2618. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  2619. ib_sa_comp_mask comp_mask;
  2620. int ret;
  2621. ib_addr_get_mgid(dev_addr, &rec.mgid);
  2622. ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
  2623. &rec.mgid, &rec);
  2624. if (ret)
  2625. return ret;
  2626. cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
  2627. if (id_priv->id.ps == RDMA_PS_UDP)
  2628. rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
  2629. rdma_addr_get_sgid(dev_addr, &rec.port_gid);
  2630. rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  2631. rec.join_state = 1;
  2632. comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
  2633. IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
  2634. IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
  2635. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  2636. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
  2637. if (id_priv->id.ps == RDMA_PS_IPOIB)
  2638. comp_mask |= IB_SA_MCMEMBER_REC_RATE |
  2639. IB_SA_MCMEMBER_REC_RATE_SELECTOR;
  2640. mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
  2641. id_priv->id.port_num, &rec,
  2642. comp_mask, GFP_KERNEL,
  2643. cma_ib_mc_handler, mc);
  2644. if (IS_ERR(mc->multicast.ib))
  2645. return PTR_ERR(mc->multicast.ib);
  2646. return 0;
  2647. }
  2648. static void iboe_mcast_work_handler(struct work_struct *work)
  2649. {
  2650. struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
  2651. struct cma_multicast *mc = mw->mc;
  2652. struct ib_sa_multicast *m = mc->multicast.ib;
  2653. mc->multicast.ib->context = mc;
  2654. cma_ib_mc_handler(0, m);
  2655. kref_put(&mc->mcref, release_mc);
  2656. kfree(mw);
  2657. }
  2658. static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
  2659. {
  2660. struct sockaddr_in *sin = (struct sockaddr_in *)addr;
  2661. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
  2662. if (cma_any_addr(addr)) {
  2663. memset(mgid, 0, sizeof *mgid);
  2664. } else if (addr->sa_family == AF_INET6) {
  2665. memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
  2666. } else {
  2667. mgid->raw[0] = 0xff;
  2668. mgid->raw[1] = 0x0e;
  2669. mgid->raw[2] = 0;
  2670. mgid->raw[3] = 0;
  2671. mgid->raw[4] = 0;
  2672. mgid->raw[5] = 0;
  2673. mgid->raw[6] = 0;
  2674. mgid->raw[7] = 0;
  2675. mgid->raw[8] = 0;
  2676. mgid->raw[9] = 0;
  2677. mgid->raw[10] = 0xff;
  2678. mgid->raw[11] = 0xff;
  2679. *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
  2680. }
  2681. }
  2682. static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
  2683. struct cma_multicast *mc)
  2684. {
  2685. struct iboe_mcast_work *work;
  2686. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  2687. int err;
  2688. struct sockaddr *addr = (struct sockaddr *)&mc->addr;
  2689. struct net_device *ndev = NULL;
  2690. if (cma_zero_addr((struct sockaddr *)&mc->addr))
  2691. return -EINVAL;
  2692. work = kzalloc(sizeof *work, GFP_KERNEL);
  2693. if (!work)
  2694. return -ENOMEM;
  2695. mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
  2696. if (!mc->multicast.ib) {
  2697. err = -ENOMEM;
  2698. goto out1;
  2699. }
  2700. cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
  2701. mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
  2702. if (id_priv->id.ps == RDMA_PS_UDP)
  2703. mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
  2704. if (dev_addr->bound_dev_if)
  2705. ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
  2706. if (!ndev) {
  2707. err = -ENODEV;
  2708. goto out2;
  2709. }
  2710. mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
  2711. mc->multicast.ib->rec.hop_limit = 1;
  2712. mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
  2713. dev_put(ndev);
  2714. if (!mc->multicast.ib->rec.mtu) {
  2715. err = -EINVAL;
  2716. goto out2;
  2717. }
  2718. iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
  2719. work->id = id_priv;
  2720. work->mc = mc;
  2721. INIT_WORK(&work->work, iboe_mcast_work_handler);
  2722. kref_get(&mc->mcref);
  2723. queue_work(cma_wq, &work->work);
  2724. return 0;
  2725. out2:
  2726. kfree(mc->multicast.ib);
  2727. out1:
  2728. kfree(work);
  2729. return err;
  2730. }
  2731. int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
  2732. void *context)
  2733. {
  2734. struct rdma_id_private *id_priv;
  2735. struct cma_multicast *mc;
  2736. int ret;
  2737. id_priv = container_of(id, struct rdma_id_private, id);
  2738. if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
  2739. !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
  2740. return -EINVAL;
  2741. mc = kmalloc(sizeof *mc, GFP_KERNEL);
  2742. if (!mc)
  2743. return -ENOMEM;
  2744. memcpy(&mc->addr, addr, ip_addr_size(addr));
  2745. mc->context = context;
  2746. mc->id_priv = id_priv;
  2747. spin_lock(&id_priv->lock);
  2748. list_add(&mc->list, &id_priv->mc_list);
  2749. spin_unlock(&id_priv->lock);
  2750. switch (rdma_node_get_transport(id->device->node_type)) {
  2751. case RDMA_TRANSPORT_IB:
  2752. switch (rdma_port_get_link_layer(id->device, id->port_num)) {
  2753. case IB_LINK_LAYER_INFINIBAND:
  2754. ret = cma_join_ib_multicast(id_priv, mc);
  2755. break;
  2756. case IB_LINK_LAYER_ETHERNET:
  2757. kref_init(&mc->mcref);
  2758. ret = cma_iboe_join_multicast(id_priv, mc);
  2759. break;
  2760. default:
  2761. ret = -EINVAL;
  2762. }
  2763. break;
  2764. default:
  2765. ret = -ENOSYS;
  2766. break;
  2767. }
  2768. if (ret) {
  2769. spin_lock_irq(&id_priv->lock);
  2770. list_del(&mc->list);
  2771. spin_unlock_irq(&id_priv->lock);
  2772. kfree(mc);
  2773. }
  2774. return ret;
  2775. }
  2776. EXPORT_SYMBOL(rdma_join_multicast);
  2777. void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
  2778. {
  2779. struct rdma_id_private *id_priv;
  2780. struct cma_multicast *mc;
  2781. id_priv = container_of(id, struct rdma_id_private, id);
  2782. spin_lock_irq(&id_priv->lock);
  2783. list_for_each_entry(mc, &id_priv->mc_list, list) {
  2784. if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
  2785. list_del(&mc->list);
  2786. spin_unlock_irq(&id_priv->lock);
  2787. if (id->qp)
  2788. ib_detach_mcast(id->qp,
  2789. &mc->multicast.ib->rec.mgid,
  2790. be16_to_cpu(mc->multicast.ib->rec.mlid));
  2791. if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
  2792. switch (rdma_port_get_link_layer(id->device, id->port_num)) {
  2793. case IB_LINK_LAYER_INFINIBAND:
  2794. ib_sa_free_multicast(mc->multicast.ib);
  2795. kfree(mc);
  2796. break;
  2797. case IB_LINK_LAYER_ETHERNET:
  2798. kref_put(&mc->mcref, release_mc);
  2799. break;
  2800. default:
  2801. break;
  2802. }
  2803. }
  2804. return;
  2805. }
  2806. }
  2807. spin_unlock_irq(&id_priv->lock);
  2808. }
  2809. EXPORT_SYMBOL(rdma_leave_multicast);
  2810. static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
  2811. {
  2812. struct rdma_dev_addr *dev_addr;
  2813. struct cma_ndev_work *work;
  2814. dev_addr = &id_priv->id.route.addr.dev_addr;
  2815. if ((dev_addr->bound_dev_if == ndev->ifindex) &&
  2816. memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
  2817. printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
  2818. ndev->name, &id_priv->id);
  2819. work = kzalloc(sizeof *work, GFP_KERNEL);
  2820. if (!work)
  2821. return -ENOMEM;
  2822. INIT_WORK(&work->work, cma_ndev_work_handler);
  2823. work->id = id_priv;
  2824. work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
  2825. atomic_inc(&id_priv->refcount);
  2826. queue_work(cma_wq, &work->work);
  2827. }
  2828. return 0;
  2829. }
  2830. static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
  2831. void *ctx)
  2832. {
  2833. struct net_device *ndev = (struct net_device *)ctx;
  2834. struct cma_device *cma_dev;
  2835. struct rdma_id_private *id_priv;
  2836. int ret = NOTIFY_DONE;
  2837. if (dev_net(ndev) != &init_net)
  2838. return NOTIFY_DONE;
  2839. if (event != NETDEV_BONDING_FAILOVER)
  2840. return NOTIFY_DONE;
  2841. if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
  2842. return NOTIFY_DONE;
  2843. mutex_lock(&lock);
  2844. list_for_each_entry(cma_dev, &dev_list, list)
  2845. list_for_each_entry(id_priv, &cma_dev->id_list, list) {
  2846. ret = cma_netdev_change(ndev, id_priv);
  2847. if (ret)
  2848. goto out;
  2849. }
  2850. out:
  2851. mutex_unlock(&lock);
  2852. return ret;
  2853. }
  2854. static struct notifier_block cma_nb = {
  2855. .notifier_call = cma_netdev_callback
  2856. };
  2857. static void cma_add_one(struct ib_device *device)
  2858. {
  2859. struct cma_device *cma_dev;
  2860. struct rdma_id_private *id_priv;
  2861. cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
  2862. if (!cma_dev)
  2863. return;
  2864. cma_dev->device = device;
  2865. init_completion(&cma_dev->comp);
  2866. atomic_set(&cma_dev->refcount, 1);
  2867. INIT_LIST_HEAD(&cma_dev->id_list);
  2868. ib_set_client_data(device, &cma_client, cma_dev);
  2869. mutex_lock(&lock);
  2870. list_add_tail(&cma_dev->list, &dev_list);
  2871. list_for_each_entry(id_priv, &listen_any_list, list)
  2872. cma_listen_on_dev(id_priv, cma_dev);
  2873. mutex_unlock(&lock);
  2874. }
  2875. static int cma_remove_id_dev(struct rdma_id_private *id_priv)
  2876. {
  2877. struct rdma_cm_event event;
  2878. enum rdma_cm_state state;
  2879. int ret = 0;
  2880. /* Record that we want to remove the device */
  2881. state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
  2882. if (state == RDMA_CM_DESTROYING)
  2883. return 0;
  2884. cma_cancel_operation(id_priv, state);
  2885. mutex_lock(&id_priv->handler_mutex);
  2886. /* Check for destruction from another callback. */
  2887. if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
  2888. goto out;
  2889. memset(&event, 0, sizeof event);
  2890. event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
  2891. ret = id_priv->id.event_handler(&id_priv->id, &event);
  2892. out:
  2893. mutex_unlock(&id_priv->handler_mutex);
  2894. return ret;
  2895. }
  2896. static void cma_process_remove(struct cma_device *cma_dev)
  2897. {
  2898. struct rdma_id_private *id_priv;
  2899. int ret;
  2900. mutex_lock(&lock);
  2901. while (!list_empty(&cma_dev->id_list)) {
  2902. id_priv = list_entry(cma_dev->id_list.next,
  2903. struct rdma_id_private, list);
  2904. list_del(&id_priv->listen_list);
  2905. list_del_init(&id_priv->list);
  2906. atomic_inc(&id_priv->refcount);
  2907. mutex_unlock(&lock);
  2908. ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
  2909. cma_deref_id(id_priv);
  2910. if (ret)
  2911. rdma_destroy_id(&id_priv->id);
  2912. mutex_lock(&lock);
  2913. }
  2914. mutex_unlock(&lock);
  2915. cma_deref_dev(cma_dev);
  2916. wait_for_completion(&cma_dev->comp);
  2917. }
  2918. static void cma_remove_one(struct ib_device *device)
  2919. {
  2920. struct cma_device *cma_dev;
  2921. cma_dev = ib_get_client_data(device, &cma_client);
  2922. if (!cma_dev)
  2923. return;
  2924. mutex_lock(&lock);
  2925. list_del(&cma_dev->list);
  2926. mutex_unlock(&lock);
  2927. cma_process_remove(cma_dev);
  2928. kfree(cma_dev);
  2929. }
  2930. static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
  2931. {
  2932. struct nlmsghdr *nlh;
  2933. struct rdma_cm_id_stats *id_stats;
  2934. struct rdma_id_private *id_priv;
  2935. struct rdma_cm_id *id = NULL;
  2936. struct cma_device *cma_dev;
  2937. int i_dev = 0, i_id = 0;
  2938. /*
  2939. * We export all of the IDs as a sequence of messages. Each
  2940. * ID gets its own netlink message.
  2941. */
  2942. mutex_lock(&lock);
  2943. list_for_each_entry(cma_dev, &dev_list, list) {
  2944. if (i_dev < cb->args[0]) {
  2945. i_dev++;
  2946. continue;
  2947. }
  2948. i_id = 0;
  2949. list_for_each_entry(id_priv, &cma_dev->id_list, list) {
  2950. if (i_id < cb->args[1]) {
  2951. i_id++;
  2952. continue;
  2953. }
  2954. id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
  2955. sizeof *id_stats, RDMA_NL_RDMA_CM,
  2956. RDMA_NL_RDMA_CM_ID_STATS);
  2957. if (!id_stats)
  2958. goto out;
  2959. memset(id_stats, 0, sizeof *id_stats);
  2960. id = &id_priv->id;
  2961. id_stats->node_type = id->route.addr.dev_addr.dev_type;
  2962. id_stats->port_num = id->port_num;
  2963. id_stats->bound_dev_if =
  2964. id->route.addr.dev_addr.bound_dev_if;
  2965. if (id->route.addr.src_addr.ss_family == AF_INET) {
  2966. if (ibnl_put_attr(skb, nlh,
  2967. sizeof(struct sockaddr_in),
  2968. &id->route.addr.src_addr,
  2969. RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
  2970. goto out;
  2971. }
  2972. if (ibnl_put_attr(skb, nlh,
  2973. sizeof(struct sockaddr_in),
  2974. &id->route.addr.dst_addr,
  2975. RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
  2976. goto out;
  2977. }
  2978. } else if (id->route.addr.src_addr.ss_family == AF_INET6) {
  2979. if (ibnl_put_attr(skb, nlh,
  2980. sizeof(struct sockaddr_in6),
  2981. &id->route.addr.src_addr,
  2982. RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
  2983. goto out;
  2984. }
  2985. if (ibnl_put_attr(skb, nlh,
  2986. sizeof(struct sockaddr_in6),
  2987. &id->route.addr.dst_addr,
  2988. RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
  2989. goto out;
  2990. }
  2991. }
  2992. id_stats->pid = id_priv->owner;
  2993. id_stats->port_space = id->ps;
  2994. id_stats->cm_state = id_priv->state;
  2995. id_stats->qp_num = id_priv->qp_num;
  2996. id_stats->qp_type = id->qp_type;
  2997. i_id++;
  2998. }
  2999. cb->args[1] = 0;
  3000. i_dev++;
  3001. }
  3002. out:
  3003. mutex_unlock(&lock);
  3004. cb->args[0] = i_dev;
  3005. cb->args[1] = i_id;
  3006. return skb->len;
  3007. }
  3008. static const struct ibnl_client_cbs cma_cb_table[] = {
  3009. [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
  3010. .module = THIS_MODULE },
  3011. };
  3012. static int __init cma_init(void)
  3013. {
  3014. int ret;
  3015. cma_wq = create_singlethread_workqueue("rdma_cm");
  3016. if (!cma_wq)
  3017. return -ENOMEM;
  3018. ib_sa_register_client(&sa_client);
  3019. rdma_addr_register_client(&addr_client);
  3020. register_netdevice_notifier(&cma_nb);
  3021. ret = ib_register_client(&cma_client);
  3022. if (ret)
  3023. goto err;
  3024. if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
  3025. printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
  3026. return 0;
  3027. err:
  3028. unregister_netdevice_notifier(&cma_nb);
  3029. rdma_addr_unregister_client(&addr_client);
  3030. ib_sa_unregister_client(&sa_client);
  3031. destroy_workqueue(cma_wq);
  3032. return ret;
  3033. }
  3034. static void __exit cma_cleanup(void)
  3035. {
  3036. ibnl_remove_client(RDMA_NL_RDMA_CM);
  3037. ib_unregister_client(&cma_client);
  3038. unregister_netdevice_notifier(&cma_nb);
  3039. rdma_addr_unregister_client(&addr_client);
  3040. ib_sa_unregister_client(&sa_client);
  3041. destroy_workqueue(cma_wq);
  3042. idr_destroy(&sdp_ps);
  3043. idr_destroy(&tcp_ps);
  3044. idr_destroy(&udp_ps);
  3045. idr_destroy(&ipoib_ps);
  3046. idr_destroy(&ib_ps);
  3047. }
  3048. module_init(cma_init);
  3049. module_exit(cma_cleanup);