cma.c 87 KB

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