device.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/string.h>
  35. #include <linux/errno.h>
  36. #include <linux/kernel.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <linux/netdevice.h>
  40. #include <net/net_namespace.h>
  41. #include <linux/security.h>
  42. #include <linux/notifier.h>
  43. #include <linux/hashtable.h>
  44. #include <rdma/rdma_netlink.h>
  45. #include <rdma/ib_addr.h>
  46. #include <rdma/ib_cache.h>
  47. #include <rdma/rdma_counter.h>
  48. #include "core_priv.h"
  49. #include "restrack.h"
  50. MODULE_AUTHOR("Roland Dreier");
  51. MODULE_DESCRIPTION("core kernel InfiniBand API");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. struct workqueue_struct *ib_comp_wq;
  54. struct workqueue_struct *ib_comp_unbound_wq;
  55. struct workqueue_struct *ib_wq;
  56. EXPORT_SYMBOL_GPL(ib_wq);
  57. static struct workqueue_struct *ib_unreg_wq;
  58. /*
  59. * Each of the three rwsem locks (devices, clients, client_data) protects the
  60. * xarray of the same name. Specifically it allows the caller to assert that
  61. * the MARK will/will not be changing under the lock, and for devices and
  62. * clients, that the value in the xarray is still a valid pointer. Change of
  63. * the MARK is linked to the object state, so holding the lock and testing the
  64. * MARK also asserts that the contained object is in a certain state.
  65. *
  66. * This is used to build a two stage register/unregister flow where objects
  67. * can continue to be in the xarray even though they are still in progress to
  68. * register/unregister.
  69. *
  70. * The xarray itself provides additional locking, and restartable iteration,
  71. * which is also relied on.
  72. *
  73. * Locks should not be nested, with the exception of client_data, which is
  74. * allowed to nest under the read side of the other two locks.
  75. *
  76. * The devices_rwsem also protects the device name list, any change or
  77. * assignment of device name must also hold the write side to guarantee unique
  78. * names.
  79. */
  80. /*
  81. * devices contains devices that have had their names assigned. The
  82. * devices may not be registered. Users that care about the registration
  83. * status need to call ib_device_try_get() on the device to ensure it is
  84. * registered, and keep it registered, for the required duration.
  85. *
  86. */
  87. static DEFINE_XARRAY_FLAGS(devices, XA_FLAGS_ALLOC);
  88. static DECLARE_RWSEM(devices_rwsem);
  89. #define DEVICE_REGISTERED XA_MARK_1
  90. static u32 highest_client_id;
  91. #define CLIENT_REGISTERED XA_MARK_1
  92. static DEFINE_XARRAY_FLAGS(clients, XA_FLAGS_ALLOC);
  93. static DECLARE_RWSEM(clients_rwsem);
  94. static void ib_client_put(struct ib_client *client)
  95. {
  96. if (refcount_dec_and_test(&client->uses))
  97. complete(&client->uses_zero);
  98. }
  99. /*
  100. * If client_data is registered then the corresponding client must also still
  101. * be registered.
  102. */
  103. #define CLIENT_DATA_REGISTERED XA_MARK_1
  104. unsigned int rdma_dev_net_id;
  105. /*
  106. * A list of net namespaces is maintained in an xarray. This is necessary
  107. * because we can't get the locking right using the existing net ns list. We
  108. * would require a init_net callback after the list is updated.
  109. */
  110. static DEFINE_XARRAY_FLAGS(rdma_nets, XA_FLAGS_ALLOC);
  111. /*
  112. * rwsem to protect accessing the rdma_nets xarray entries.
  113. */
  114. static DECLARE_RWSEM(rdma_nets_rwsem);
  115. bool ib_devices_shared_netns = true;
  116. module_param_named(netns_mode, ib_devices_shared_netns, bool, 0444);
  117. MODULE_PARM_DESC(netns_mode,
  118. "Share device among net namespaces; default=1 (shared)");
  119. /**
  120. * rdma_dev_access_netns() - Return whether an rdma device can be accessed
  121. * from a specified net namespace or not.
  122. * @dev: Pointer to rdma device which needs to be checked
  123. * @net: Pointer to net namesapce for which access to be checked
  124. *
  125. * When the rdma device is in shared mode, it ignores the net namespace.
  126. * When the rdma device is exclusive to a net namespace, rdma device net
  127. * namespace is checked against the specified one.
  128. */
  129. bool rdma_dev_access_netns(const struct ib_device *dev, const struct net *net)
  130. {
  131. return (ib_devices_shared_netns ||
  132. net_eq(read_pnet(&dev->coredev.rdma_net), net));
  133. }
  134. EXPORT_SYMBOL(rdma_dev_access_netns);
  135. /*
  136. * xarray has this behavior where it won't iterate over NULL values stored in
  137. * allocated arrays. So we need our own iterator to see all values stored in
  138. * the array. This does the same thing as xa_for_each except that it also
  139. * returns NULL valued entries if the array is allocating. Simplified to only
  140. * work on simple xarrays.
  141. */
  142. static void *xan_find_marked(struct xarray *xa, unsigned long *indexp,
  143. xa_mark_t filter)
  144. {
  145. XA_STATE(xas, xa, *indexp);
  146. void *entry;
  147. rcu_read_lock();
  148. do {
  149. entry = xas_find_marked(&xas, ULONG_MAX, filter);
  150. if (xa_is_zero(entry))
  151. break;
  152. } while (xas_retry(&xas, entry));
  153. rcu_read_unlock();
  154. if (entry) {
  155. *indexp = xas.xa_index;
  156. if (xa_is_zero(entry))
  157. return NULL;
  158. return entry;
  159. }
  160. return XA_ERROR(-ENOENT);
  161. }
  162. #define xan_for_each_marked(xa, index, entry, filter) \
  163. for (index = 0, entry = xan_find_marked(xa, &(index), filter); \
  164. !xa_is_err(entry); \
  165. (index)++, entry = xan_find_marked(xa, &(index), filter))
  166. /* RCU hash table mapping netdevice pointers to struct ib_port_data */
  167. static DEFINE_SPINLOCK(ndev_hash_lock);
  168. static DECLARE_HASHTABLE(ndev_hash, 5);
  169. static void free_netdevs(struct ib_device *ib_dev);
  170. static void ib_unregister_work(struct work_struct *work);
  171. static void __ib_unregister_device(struct ib_device *device);
  172. static int ib_security_change(struct notifier_block *nb, unsigned long event,
  173. void *lsm_data);
  174. static void ib_policy_change_task(struct work_struct *work);
  175. static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
  176. static void __ibdev_printk(const char *level, const struct ib_device *ibdev,
  177. struct va_format *vaf)
  178. {
  179. if (ibdev && ibdev->dev.parent)
  180. dev_printk_emit(level[1] - '0',
  181. ibdev->dev.parent,
  182. "%s %s %s: %pV",
  183. dev_driver_string(ibdev->dev.parent),
  184. dev_name(ibdev->dev.parent),
  185. dev_name(&ibdev->dev),
  186. vaf);
  187. else if (ibdev)
  188. printk("%s%s: %pV",
  189. level, dev_name(&ibdev->dev), vaf);
  190. else
  191. printk("%s(NULL ib_device): %pV", level, vaf);
  192. }
  193. void ibdev_printk(const char *level, const struct ib_device *ibdev,
  194. const char *format, ...)
  195. {
  196. struct va_format vaf;
  197. va_list args;
  198. va_start(args, format);
  199. vaf.fmt = format;
  200. vaf.va = &args;
  201. __ibdev_printk(level, ibdev, &vaf);
  202. va_end(args);
  203. }
  204. EXPORT_SYMBOL(ibdev_printk);
  205. #define define_ibdev_printk_level(func, level) \
  206. void func(const struct ib_device *ibdev, const char *fmt, ...) \
  207. { \
  208. struct va_format vaf; \
  209. va_list args; \
  210. \
  211. va_start(args, fmt); \
  212. \
  213. vaf.fmt = fmt; \
  214. vaf.va = &args; \
  215. \
  216. __ibdev_printk(level, ibdev, &vaf); \
  217. \
  218. va_end(args); \
  219. } \
  220. EXPORT_SYMBOL(func);
  221. define_ibdev_printk_level(ibdev_emerg, KERN_EMERG);
  222. define_ibdev_printk_level(ibdev_alert, KERN_ALERT);
  223. define_ibdev_printk_level(ibdev_crit, KERN_CRIT);
  224. define_ibdev_printk_level(ibdev_err, KERN_ERR);
  225. define_ibdev_printk_level(ibdev_warn, KERN_WARNING);
  226. define_ibdev_printk_level(ibdev_notice, KERN_NOTICE);
  227. define_ibdev_printk_level(ibdev_info, KERN_INFO);
  228. static struct notifier_block ibdev_lsm_nb = {
  229. .notifier_call = ib_security_change,
  230. };
  231. static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
  232. struct net *net);
  233. /* Pointer to the RCU head at the start of the ib_port_data array */
  234. struct ib_port_data_rcu {
  235. struct rcu_head rcu_head;
  236. struct ib_port_data pdata[];
  237. };
  238. static void ib_device_check_mandatory(struct ib_device *device)
  239. {
  240. #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x }
  241. static const struct {
  242. size_t offset;
  243. char *name;
  244. } mandatory_table[] = {
  245. IB_MANDATORY_FUNC(query_device),
  246. IB_MANDATORY_FUNC(query_port),
  247. IB_MANDATORY_FUNC(alloc_pd),
  248. IB_MANDATORY_FUNC(dealloc_pd),
  249. IB_MANDATORY_FUNC(create_qp),
  250. IB_MANDATORY_FUNC(modify_qp),
  251. IB_MANDATORY_FUNC(destroy_qp),
  252. IB_MANDATORY_FUNC(post_send),
  253. IB_MANDATORY_FUNC(post_recv),
  254. IB_MANDATORY_FUNC(create_cq),
  255. IB_MANDATORY_FUNC(destroy_cq),
  256. IB_MANDATORY_FUNC(poll_cq),
  257. IB_MANDATORY_FUNC(req_notify_cq),
  258. IB_MANDATORY_FUNC(get_dma_mr),
  259. IB_MANDATORY_FUNC(reg_user_mr),
  260. IB_MANDATORY_FUNC(dereg_mr),
  261. IB_MANDATORY_FUNC(get_port_immutable)
  262. };
  263. int i;
  264. device->kverbs_provider = true;
  265. for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
  266. if (!*(void **) ((void *) &device->ops +
  267. mandatory_table[i].offset)) {
  268. device->kverbs_provider = false;
  269. break;
  270. }
  271. }
  272. }
  273. /*
  274. * Caller must perform ib_device_put() to return the device reference count
  275. * when ib_device_get_by_index() returns valid device pointer.
  276. */
  277. struct ib_device *ib_device_get_by_index(const struct net *net, u32 index)
  278. {
  279. struct ib_device *device;
  280. down_read(&devices_rwsem);
  281. device = xa_load(&devices, index);
  282. if (device) {
  283. if (!rdma_dev_access_netns(device, net)) {
  284. device = NULL;
  285. goto out;
  286. }
  287. if (!ib_device_try_get(device))
  288. device = NULL;
  289. }
  290. out:
  291. up_read(&devices_rwsem);
  292. return device;
  293. }
  294. /**
  295. * ib_device_put - Release IB device reference
  296. * @device: device whose reference to be released
  297. *
  298. * ib_device_put() releases reference to the IB device to allow it to be
  299. * unregistered and eventually free.
  300. */
  301. void ib_device_put(struct ib_device *device)
  302. {
  303. if (refcount_dec_and_test(&device->refcount))
  304. complete(&device->unreg_completion);
  305. }
  306. EXPORT_SYMBOL(ib_device_put);
  307. static struct ib_device *__ib_device_get_by_name(const char *name)
  308. {
  309. struct ib_device *device;
  310. unsigned long index;
  311. xa_for_each (&devices, index, device)
  312. if (!strcmp(name, dev_name(&device->dev)))
  313. return device;
  314. return NULL;
  315. }
  316. /**
  317. * ib_device_get_by_name - Find an IB device by name
  318. * @name: The name to look for
  319. * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all)
  320. *
  321. * Find and hold an ib_device by its name. The caller must call
  322. * ib_device_put() on the returned pointer.
  323. */
  324. struct ib_device *ib_device_get_by_name(const char *name,
  325. enum rdma_driver_id driver_id)
  326. {
  327. struct ib_device *device;
  328. down_read(&devices_rwsem);
  329. device = __ib_device_get_by_name(name);
  330. if (device && driver_id != RDMA_DRIVER_UNKNOWN &&
  331. device->ops.driver_id != driver_id)
  332. device = NULL;
  333. if (device) {
  334. if (!ib_device_try_get(device))
  335. device = NULL;
  336. }
  337. up_read(&devices_rwsem);
  338. return device;
  339. }
  340. EXPORT_SYMBOL(ib_device_get_by_name);
  341. static int rename_compat_devs(struct ib_device *device)
  342. {
  343. struct ib_core_device *cdev;
  344. unsigned long index;
  345. int ret = 0;
  346. mutex_lock(&device->compat_devs_mutex);
  347. xa_for_each (&device->compat_devs, index, cdev) {
  348. ret = device_rename(&cdev->dev, dev_name(&device->dev));
  349. if (ret) {
  350. dev_warn(&cdev->dev,
  351. "Fail to rename compatdev to new name %s\n",
  352. dev_name(&device->dev));
  353. break;
  354. }
  355. }
  356. mutex_unlock(&device->compat_devs_mutex);
  357. return ret;
  358. }
  359. int ib_device_rename(struct ib_device *ibdev, const char *name)
  360. {
  361. unsigned long index;
  362. void *client_data;
  363. int ret;
  364. down_write(&devices_rwsem);
  365. if (!strcmp(name, dev_name(&ibdev->dev))) {
  366. up_write(&devices_rwsem);
  367. return 0;
  368. }
  369. if (__ib_device_get_by_name(name)) {
  370. up_write(&devices_rwsem);
  371. return -EEXIST;
  372. }
  373. ret = device_rename(&ibdev->dev, name);
  374. if (ret) {
  375. up_write(&devices_rwsem);
  376. return ret;
  377. }
  378. strscpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
  379. ret = rename_compat_devs(ibdev);
  380. downgrade_write(&devices_rwsem);
  381. down_read(&ibdev->client_data_rwsem);
  382. xan_for_each_marked(&ibdev->client_data, index, client_data,
  383. CLIENT_DATA_REGISTERED) {
  384. struct ib_client *client = xa_load(&clients, index);
  385. if (!client || !client->rename)
  386. continue;
  387. client->rename(ibdev, client_data);
  388. }
  389. up_read(&ibdev->client_data_rwsem);
  390. up_read(&devices_rwsem);
  391. return 0;
  392. }
  393. int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim)
  394. {
  395. if (use_dim > 1)
  396. return -EINVAL;
  397. ibdev->use_cq_dim = use_dim;
  398. return 0;
  399. }
  400. static int alloc_name(struct ib_device *ibdev, const char *name)
  401. {
  402. struct ib_device *device;
  403. unsigned long index;
  404. struct ida inuse;
  405. int rc;
  406. int i;
  407. lockdep_assert_held_write(&devices_rwsem);
  408. ida_init(&inuse);
  409. xa_for_each (&devices, index, device) {
  410. char buf[IB_DEVICE_NAME_MAX];
  411. if (sscanf(dev_name(&device->dev), name, &i) != 1)
  412. continue;
  413. if (i < 0 || i >= INT_MAX)
  414. continue;
  415. snprintf(buf, sizeof buf, name, i);
  416. if (strcmp(buf, dev_name(&device->dev)) != 0)
  417. continue;
  418. rc = ida_alloc_range(&inuse, i, i, GFP_KERNEL);
  419. if (rc < 0)
  420. goto out;
  421. }
  422. rc = ida_alloc(&inuse, GFP_KERNEL);
  423. if (rc < 0)
  424. goto out;
  425. rc = dev_set_name(&ibdev->dev, name, rc);
  426. out:
  427. ida_destroy(&inuse);
  428. return rc;
  429. }
  430. static void ib_device_release(struct device *device)
  431. {
  432. struct ib_device *dev = container_of(device, struct ib_device, dev);
  433. free_netdevs(dev);
  434. WARN_ON(refcount_read(&dev->refcount));
  435. if (dev->hw_stats_data)
  436. ib_device_release_hw_stats(dev->hw_stats_data);
  437. if (dev->port_data) {
  438. ib_cache_release_one(dev);
  439. ib_security_release_port_pkey_list(dev);
  440. rdma_counter_release(dev);
  441. kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu,
  442. pdata[0]),
  443. rcu_head);
  444. }
  445. mutex_destroy(&dev->subdev_lock);
  446. mutex_destroy(&dev->unregistration_lock);
  447. mutex_destroy(&dev->compat_devs_mutex);
  448. xa_destroy(&dev->compat_devs);
  449. xa_destroy(&dev->client_data);
  450. kfree_rcu(dev, rcu_head);
  451. }
  452. static int ib_device_uevent(const struct device *device,
  453. struct kobj_uevent_env *env)
  454. {
  455. if (add_uevent_var(env, "NAME=%s", dev_name(device)))
  456. return -ENOMEM;
  457. /*
  458. * It would be nice to pass the node GUID with the event...
  459. */
  460. return 0;
  461. }
  462. static const void *net_namespace(const struct device *d)
  463. {
  464. const struct ib_core_device *coredev =
  465. container_of(d, struct ib_core_device, dev);
  466. return read_pnet(&coredev->rdma_net);
  467. }
  468. static struct class ib_class = {
  469. .name = "infiniband",
  470. .dev_release = ib_device_release,
  471. .dev_uevent = ib_device_uevent,
  472. .ns_type = &net_ns_type_operations,
  473. .namespace = net_namespace,
  474. };
  475. static void rdma_init_coredev(struct ib_core_device *coredev,
  476. struct ib_device *dev, struct net *net)
  477. {
  478. bool is_full_dev = &dev->coredev == coredev;
  479. /* This BUILD_BUG_ON is intended to catch layout change
  480. * of union of ib_core_device and device.
  481. * dev must be the first element as ib_core and providers
  482. * driver uses it. Adding anything in ib_core_device before
  483. * device will break this assumption.
  484. */
  485. BUILD_BUG_ON(offsetof(struct ib_device, coredev.dev) !=
  486. offsetof(struct ib_device, dev));
  487. coredev->dev.class = &ib_class;
  488. coredev->dev.groups = dev->groups;
  489. /*
  490. * Don't expose hw counters outside of the init namespace.
  491. */
  492. if (!is_full_dev && dev->hw_stats_attr_index)
  493. coredev->dev.groups[dev->hw_stats_attr_index] = NULL;
  494. device_initialize(&coredev->dev);
  495. coredev->owner = dev;
  496. INIT_LIST_HEAD(&coredev->port_list);
  497. write_pnet(&coredev->rdma_net, net);
  498. }
  499. /**
  500. * _ib_alloc_device - allocate an IB device struct
  501. * @size:size of structure to allocate
  502. *
  503. * Low-level drivers should use ib_alloc_device() to allocate &struct
  504. * ib_device. @size is the size of the structure to be allocated,
  505. * including any private data used by the low-level driver.
  506. * ib_dealloc_device() must be used to free structures allocated with
  507. * ib_alloc_device().
  508. */
  509. struct ib_device *_ib_alloc_device(size_t size)
  510. {
  511. struct ib_device *device;
  512. unsigned int i;
  513. if (WARN_ON(size < sizeof(struct ib_device)))
  514. return NULL;
  515. device = kzalloc(size, GFP_KERNEL);
  516. if (!device)
  517. return NULL;
  518. if (rdma_restrack_init(device)) {
  519. kfree(device);
  520. return NULL;
  521. }
  522. rdma_init_coredev(&device->coredev, device, &init_net);
  523. INIT_LIST_HEAD(&device->event_handler_list);
  524. spin_lock_init(&device->qp_open_list_lock);
  525. init_rwsem(&device->event_handler_rwsem);
  526. mutex_init(&device->unregistration_lock);
  527. /*
  528. * client_data needs to be alloc because we don't want our mark to be
  529. * destroyed if the user stores NULL in the client data.
  530. */
  531. xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
  532. init_rwsem(&device->client_data_rwsem);
  533. xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC);
  534. mutex_init(&device->compat_devs_mutex);
  535. init_completion(&device->unreg_completion);
  536. INIT_WORK(&device->unregistration_work, ib_unregister_work);
  537. spin_lock_init(&device->cq_pools_lock);
  538. for (i = 0; i < ARRAY_SIZE(device->cq_pools); i++)
  539. INIT_LIST_HEAD(&device->cq_pools[i]);
  540. rwlock_init(&device->cache_lock);
  541. device->uverbs_cmd_mask =
  542. BIT_ULL(IB_USER_VERBS_CMD_ALLOC_MW) |
  543. BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD) |
  544. BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST) |
  545. BIT_ULL(IB_USER_VERBS_CMD_CLOSE_XRCD) |
  546. BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH) |
  547. BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  548. BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ) |
  549. BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP) |
  550. BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ) |
  551. BIT_ULL(IB_USER_VERBS_CMD_CREATE_XSRQ) |
  552. BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_MW) |
  553. BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD) |
  554. BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR) |
  555. BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH) |
  556. BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ) |
  557. BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP) |
  558. BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ) |
  559. BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST) |
  560. BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT) |
  561. BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP) |
  562. BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ) |
  563. BIT_ULL(IB_USER_VERBS_CMD_OPEN_QP) |
  564. BIT_ULL(IB_USER_VERBS_CMD_OPEN_XRCD) |
  565. BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE) |
  566. BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT) |
  567. BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP) |
  568. BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ) |
  569. BIT_ULL(IB_USER_VERBS_CMD_REG_MR) |
  570. BIT_ULL(IB_USER_VERBS_CMD_REREG_MR) |
  571. BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ);
  572. mutex_init(&device->subdev_lock);
  573. INIT_LIST_HEAD(&device->subdev_list_head);
  574. INIT_LIST_HEAD(&device->subdev_list);
  575. return device;
  576. }
  577. EXPORT_SYMBOL(_ib_alloc_device);
  578. /**
  579. * ib_dealloc_device - free an IB device struct
  580. * @device:structure to free
  581. *
  582. * Free a structure allocated with ib_alloc_device().
  583. */
  584. void ib_dealloc_device(struct ib_device *device)
  585. {
  586. if (device->ops.dealloc_driver)
  587. device->ops.dealloc_driver(device);
  588. /*
  589. * ib_unregister_driver() requires all devices to remain in the xarray
  590. * while their ops are callable. The last op we call is dealloc_driver
  591. * above. This is needed to create a fence on op callbacks prior to
  592. * allowing the driver module to unload.
  593. */
  594. down_write(&devices_rwsem);
  595. if (xa_load(&devices, device->index) == device)
  596. xa_erase(&devices, device->index);
  597. up_write(&devices_rwsem);
  598. /* Expedite releasing netdev references */
  599. free_netdevs(device);
  600. WARN_ON(!xa_empty(&device->compat_devs));
  601. WARN_ON(!xa_empty(&device->client_data));
  602. WARN_ON(refcount_read(&device->refcount));
  603. rdma_restrack_clean(device);
  604. /* Balances with device_initialize */
  605. put_device(&device->dev);
  606. }
  607. EXPORT_SYMBOL(ib_dealloc_device);
  608. /*
  609. * add_client_context() and remove_client_context() must be safe against
  610. * parallel calls on the same device - registration/unregistration of both the
  611. * device and client can be occurring in parallel.
  612. *
  613. * The routines need to be a fence, any caller must not return until the add
  614. * or remove is fully completed.
  615. */
  616. static int add_client_context(struct ib_device *device,
  617. struct ib_client *client)
  618. {
  619. int ret = 0;
  620. if (!device->kverbs_provider && !client->no_kverbs_req)
  621. return 0;
  622. down_write(&device->client_data_rwsem);
  623. /*
  624. * So long as the client is registered hold both the client and device
  625. * unregistration locks.
  626. */
  627. if (!refcount_inc_not_zero(&client->uses))
  628. goto out_unlock;
  629. refcount_inc(&device->refcount);
  630. /*
  631. * Another caller to add_client_context got here first and has already
  632. * completely initialized context.
  633. */
  634. if (xa_get_mark(&device->client_data, client->client_id,
  635. CLIENT_DATA_REGISTERED))
  636. goto out;
  637. ret = xa_err(xa_store(&device->client_data, client->client_id, NULL,
  638. GFP_KERNEL));
  639. if (ret)
  640. goto out;
  641. downgrade_write(&device->client_data_rwsem);
  642. if (client->add) {
  643. if (client->add(device)) {
  644. /*
  645. * If a client fails to add then the error code is
  646. * ignored, but we won't call any more ops on this
  647. * client.
  648. */
  649. xa_erase(&device->client_data, client->client_id);
  650. up_read(&device->client_data_rwsem);
  651. ib_device_put(device);
  652. ib_client_put(client);
  653. return 0;
  654. }
  655. }
  656. /* Readers shall not see a client until add has been completed */
  657. xa_set_mark(&device->client_data, client->client_id,
  658. CLIENT_DATA_REGISTERED);
  659. up_read(&device->client_data_rwsem);
  660. return 0;
  661. out:
  662. ib_device_put(device);
  663. ib_client_put(client);
  664. out_unlock:
  665. up_write(&device->client_data_rwsem);
  666. return ret;
  667. }
  668. static void remove_client_context(struct ib_device *device,
  669. unsigned int client_id)
  670. {
  671. struct ib_client *client;
  672. void *client_data;
  673. down_write(&device->client_data_rwsem);
  674. if (!xa_get_mark(&device->client_data, client_id,
  675. CLIENT_DATA_REGISTERED)) {
  676. up_write(&device->client_data_rwsem);
  677. return;
  678. }
  679. client_data = xa_load(&device->client_data, client_id);
  680. xa_clear_mark(&device->client_data, client_id, CLIENT_DATA_REGISTERED);
  681. client = xa_load(&clients, client_id);
  682. up_write(&device->client_data_rwsem);
  683. /*
  684. * Notice we cannot be holding any exclusive locks when calling the
  685. * remove callback as the remove callback can recurse back into any
  686. * public functions in this module and thus try for any locks those
  687. * functions take.
  688. *
  689. * For this reason clients and drivers should not call the
  690. * unregistration functions will holdling any locks.
  691. */
  692. if (client->remove)
  693. client->remove(device, client_data);
  694. xa_erase(&device->client_data, client_id);
  695. ib_device_put(device);
  696. ib_client_put(client);
  697. }
  698. static int alloc_port_data(struct ib_device *device)
  699. {
  700. struct ib_port_data_rcu *pdata_rcu;
  701. u32 port;
  702. if (device->port_data)
  703. return 0;
  704. /* This can only be called once the physical port range is defined */
  705. if (WARN_ON(!device->phys_port_cnt))
  706. return -EINVAL;
  707. /* Reserve U32_MAX so the logic to go over all the ports is sane */
  708. if (WARN_ON(device->phys_port_cnt == U32_MAX))
  709. return -EINVAL;
  710. /*
  711. * device->port_data is indexed directly by the port number to make
  712. * access to this data as efficient as possible.
  713. *
  714. * Therefore port_data is declared as a 1 based array with potential
  715. * empty slots at the beginning.
  716. */
  717. pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata,
  718. size_add(rdma_end_port(device), 1)),
  719. GFP_KERNEL);
  720. if (!pdata_rcu)
  721. return -ENOMEM;
  722. /*
  723. * The rcu_head is put in front of the port data array and the stored
  724. * pointer is adjusted since we never need to see that member until
  725. * kfree_rcu.
  726. */
  727. device->port_data = pdata_rcu->pdata;
  728. rdma_for_each_port (device, port) {
  729. struct ib_port_data *pdata = &device->port_data[port];
  730. pdata->ib_dev = device;
  731. spin_lock_init(&pdata->pkey_list_lock);
  732. INIT_LIST_HEAD(&pdata->pkey_list);
  733. spin_lock_init(&pdata->netdev_lock);
  734. INIT_HLIST_NODE(&pdata->ndev_hash_link);
  735. }
  736. return 0;
  737. }
  738. static int verify_immutable(const struct ib_device *dev, u32 port)
  739. {
  740. return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
  741. rdma_max_mad_size(dev, port) != 0);
  742. }
  743. static int setup_port_data(struct ib_device *device)
  744. {
  745. u32 port;
  746. int ret;
  747. ret = alloc_port_data(device);
  748. if (ret)
  749. return ret;
  750. rdma_for_each_port (device, port) {
  751. struct ib_port_data *pdata = &device->port_data[port];
  752. ret = device->ops.get_port_immutable(device, port,
  753. &pdata->immutable);
  754. if (ret)
  755. return ret;
  756. if (verify_immutable(device, port))
  757. return -EINVAL;
  758. }
  759. return 0;
  760. }
  761. /**
  762. * ib_port_immutable_read() - Read rdma port's immutable data
  763. * @dev: IB device
  764. * @port: port number whose immutable data to read. It starts with index 1 and
  765. * valid upto including rdma_end_port().
  766. */
  767. const struct ib_port_immutable*
  768. ib_port_immutable_read(struct ib_device *dev, unsigned int port)
  769. {
  770. WARN_ON(!rdma_is_port_valid(dev, port));
  771. return &dev->port_data[port].immutable;
  772. }
  773. EXPORT_SYMBOL(ib_port_immutable_read);
  774. void ib_get_device_fw_str(struct ib_device *dev, char *str)
  775. {
  776. if (dev->ops.get_dev_fw_str)
  777. dev->ops.get_dev_fw_str(dev, str);
  778. else
  779. str[0] = '\0';
  780. }
  781. EXPORT_SYMBOL(ib_get_device_fw_str);
  782. static void ib_policy_change_task(struct work_struct *work)
  783. {
  784. struct ib_device *dev;
  785. unsigned long index;
  786. down_read(&devices_rwsem);
  787. xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
  788. unsigned int i;
  789. rdma_for_each_port (dev, i) {
  790. u64 sp;
  791. ib_get_cached_subnet_prefix(dev, i, &sp);
  792. ib_security_cache_change(dev, i, sp);
  793. }
  794. }
  795. up_read(&devices_rwsem);
  796. }
  797. static int ib_security_change(struct notifier_block *nb, unsigned long event,
  798. void *lsm_data)
  799. {
  800. if (event != LSM_POLICY_CHANGE)
  801. return NOTIFY_DONE;
  802. schedule_work(&ib_policy_change_work);
  803. ib_mad_agent_security_change();
  804. return NOTIFY_OK;
  805. }
  806. static void compatdev_release(struct device *dev)
  807. {
  808. struct ib_core_device *cdev =
  809. container_of(dev, struct ib_core_device, dev);
  810. kfree(cdev);
  811. }
  812. static int add_one_compat_dev(struct ib_device *device,
  813. struct rdma_dev_net *rnet)
  814. {
  815. struct ib_core_device *cdev;
  816. int ret;
  817. lockdep_assert_held(&rdma_nets_rwsem);
  818. if (!ib_devices_shared_netns)
  819. return 0;
  820. /*
  821. * Create and add compat device in all namespaces other than where it
  822. * is currently bound to.
  823. */
  824. if (net_eq(read_pnet(&rnet->net),
  825. read_pnet(&device->coredev.rdma_net)))
  826. return 0;
  827. /*
  828. * The first of init_net() or ib_register_device() to take the
  829. * compat_devs_mutex wins and gets to add the device. Others will wait
  830. * for completion here.
  831. */
  832. mutex_lock(&device->compat_devs_mutex);
  833. cdev = xa_load(&device->compat_devs, rnet->id);
  834. if (cdev) {
  835. ret = 0;
  836. goto done;
  837. }
  838. ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
  839. if (ret)
  840. goto done;
  841. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  842. if (!cdev) {
  843. ret = -ENOMEM;
  844. goto cdev_err;
  845. }
  846. cdev->dev.parent = device->dev.parent;
  847. rdma_init_coredev(cdev, device, read_pnet(&rnet->net));
  848. cdev->dev.release = compatdev_release;
  849. ret = dev_set_name(&cdev->dev, "%s", dev_name(&device->dev));
  850. if (ret)
  851. goto add_err;
  852. ret = device_add(&cdev->dev);
  853. if (ret)
  854. goto add_err;
  855. ret = ib_setup_port_attrs(cdev);
  856. if (ret)
  857. goto port_err;
  858. ret = xa_err(xa_store(&device->compat_devs, rnet->id,
  859. cdev, GFP_KERNEL));
  860. if (ret)
  861. goto insert_err;
  862. mutex_unlock(&device->compat_devs_mutex);
  863. return 0;
  864. insert_err:
  865. ib_free_port_attrs(cdev);
  866. port_err:
  867. device_del(&cdev->dev);
  868. add_err:
  869. put_device(&cdev->dev);
  870. cdev_err:
  871. xa_release(&device->compat_devs, rnet->id);
  872. done:
  873. mutex_unlock(&device->compat_devs_mutex);
  874. return ret;
  875. }
  876. static void remove_one_compat_dev(struct ib_device *device, u32 id)
  877. {
  878. struct ib_core_device *cdev;
  879. mutex_lock(&device->compat_devs_mutex);
  880. cdev = xa_erase(&device->compat_devs, id);
  881. mutex_unlock(&device->compat_devs_mutex);
  882. if (cdev) {
  883. ib_free_port_attrs(cdev);
  884. device_del(&cdev->dev);
  885. put_device(&cdev->dev);
  886. }
  887. }
  888. static void remove_compat_devs(struct ib_device *device)
  889. {
  890. struct ib_core_device *cdev;
  891. unsigned long index;
  892. xa_for_each (&device->compat_devs, index, cdev)
  893. remove_one_compat_dev(device, index);
  894. }
  895. static int add_compat_devs(struct ib_device *device)
  896. {
  897. struct rdma_dev_net *rnet;
  898. unsigned long index;
  899. int ret = 0;
  900. lockdep_assert_held(&devices_rwsem);
  901. down_read(&rdma_nets_rwsem);
  902. xa_for_each (&rdma_nets, index, rnet) {
  903. ret = add_one_compat_dev(device, rnet);
  904. if (ret)
  905. break;
  906. }
  907. up_read(&rdma_nets_rwsem);
  908. return ret;
  909. }
  910. static void remove_all_compat_devs(void)
  911. {
  912. struct ib_compat_device *cdev;
  913. struct ib_device *dev;
  914. unsigned long index;
  915. down_read(&devices_rwsem);
  916. xa_for_each (&devices, index, dev) {
  917. unsigned long c_index = 0;
  918. /* Hold nets_rwsem so that any other thread modifying this
  919. * system param can sync with this thread.
  920. */
  921. down_read(&rdma_nets_rwsem);
  922. xa_for_each (&dev->compat_devs, c_index, cdev)
  923. remove_one_compat_dev(dev, c_index);
  924. up_read(&rdma_nets_rwsem);
  925. }
  926. up_read(&devices_rwsem);
  927. }
  928. static int add_all_compat_devs(void)
  929. {
  930. struct rdma_dev_net *rnet;
  931. struct ib_device *dev;
  932. unsigned long index;
  933. int ret = 0;
  934. down_read(&devices_rwsem);
  935. xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
  936. unsigned long net_index = 0;
  937. /* Hold nets_rwsem so that any other thread modifying this
  938. * system param can sync with this thread.
  939. */
  940. down_read(&rdma_nets_rwsem);
  941. xa_for_each (&rdma_nets, net_index, rnet) {
  942. ret = add_one_compat_dev(dev, rnet);
  943. if (ret)
  944. break;
  945. }
  946. up_read(&rdma_nets_rwsem);
  947. }
  948. up_read(&devices_rwsem);
  949. if (ret)
  950. remove_all_compat_devs();
  951. return ret;
  952. }
  953. int rdma_compatdev_set(u8 enable)
  954. {
  955. struct rdma_dev_net *rnet;
  956. unsigned long index;
  957. int ret = 0;
  958. down_write(&rdma_nets_rwsem);
  959. if (ib_devices_shared_netns == enable) {
  960. up_write(&rdma_nets_rwsem);
  961. return 0;
  962. }
  963. /* enable/disable of compat devices is not supported
  964. * when more than default init_net exists.
  965. */
  966. xa_for_each (&rdma_nets, index, rnet) {
  967. ret++;
  968. break;
  969. }
  970. if (!ret)
  971. ib_devices_shared_netns = enable;
  972. up_write(&rdma_nets_rwsem);
  973. if (ret)
  974. return -EBUSY;
  975. if (enable)
  976. ret = add_all_compat_devs();
  977. else
  978. remove_all_compat_devs();
  979. return ret;
  980. }
  981. static void rdma_dev_exit_net(struct net *net)
  982. {
  983. struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
  984. struct ib_device *dev;
  985. unsigned long index;
  986. int ret;
  987. down_write(&rdma_nets_rwsem);
  988. /*
  989. * Prevent the ID from being re-used and hide the id from xa_for_each.
  990. */
  991. ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL));
  992. WARN_ON(ret);
  993. up_write(&rdma_nets_rwsem);
  994. down_read(&devices_rwsem);
  995. xa_for_each (&devices, index, dev) {
  996. get_device(&dev->dev);
  997. /*
  998. * Release the devices_rwsem so that pontentially blocking
  999. * device_del, doesn't hold the devices_rwsem for too long.
  1000. */
  1001. up_read(&devices_rwsem);
  1002. remove_one_compat_dev(dev, rnet->id);
  1003. /*
  1004. * If the real device is in the NS then move it back to init.
  1005. */
  1006. rdma_dev_change_netns(dev, net, &init_net);
  1007. put_device(&dev->dev);
  1008. down_read(&devices_rwsem);
  1009. }
  1010. up_read(&devices_rwsem);
  1011. rdma_nl_net_exit(rnet);
  1012. xa_erase(&rdma_nets, rnet->id);
  1013. }
  1014. static __net_init int rdma_dev_init_net(struct net *net)
  1015. {
  1016. struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
  1017. unsigned long index;
  1018. struct ib_device *dev;
  1019. int ret;
  1020. write_pnet(&rnet->net, net);
  1021. ret = rdma_nl_net_init(rnet);
  1022. if (ret)
  1023. return ret;
  1024. /* No need to create any compat devices in default init_net. */
  1025. if (net_eq(net, &init_net))
  1026. return 0;
  1027. ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
  1028. if (ret) {
  1029. rdma_nl_net_exit(rnet);
  1030. return ret;
  1031. }
  1032. down_read(&devices_rwsem);
  1033. xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
  1034. /* Hold nets_rwsem so that netlink command cannot change
  1035. * system configuration for device sharing mode.
  1036. */
  1037. down_read(&rdma_nets_rwsem);
  1038. ret = add_one_compat_dev(dev, rnet);
  1039. up_read(&rdma_nets_rwsem);
  1040. if (ret)
  1041. break;
  1042. }
  1043. up_read(&devices_rwsem);
  1044. if (ret)
  1045. rdma_dev_exit_net(net);
  1046. return ret;
  1047. }
  1048. /*
  1049. * Assign the unique string device name and the unique device index. This is
  1050. * undone by ib_dealloc_device.
  1051. */
  1052. static int assign_name(struct ib_device *device, const char *name)
  1053. {
  1054. static u32 last_id;
  1055. int ret;
  1056. down_write(&devices_rwsem);
  1057. /* Assign a unique name to the device */
  1058. if (strchr(name, '%'))
  1059. ret = alloc_name(device, name);
  1060. else
  1061. ret = dev_set_name(&device->dev, name);
  1062. if (ret)
  1063. goto out;
  1064. if (__ib_device_get_by_name(dev_name(&device->dev))) {
  1065. ret = -ENFILE;
  1066. goto out;
  1067. }
  1068. strscpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
  1069. ret = xa_alloc_cyclic(&devices, &device->index, device, xa_limit_31b,
  1070. &last_id, GFP_KERNEL);
  1071. if (ret > 0)
  1072. ret = 0;
  1073. out:
  1074. up_write(&devices_rwsem);
  1075. return ret;
  1076. }
  1077. /*
  1078. * setup_device() allocates memory and sets up data that requires calling the
  1079. * device ops, this is the only reason these actions are not done during
  1080. * ib_alloc_device. It is undone by ib_dealloc_device().
  1081. */
  1082. static int setup_device(struct ib_device *device)
  1083. {
  1084. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  1085. int ret;
  1086. ib_device_check_mandatory(device);
  1087. ret = setup_port_data(device);
  1088. if (ret) {
  1089. dev_warn(&device->dev, "Couldn't create per-port data\n");
  1090. return ret;
  1091. }
  1092. memset(&device->attrs, 0, sizeof(device->attrs));
  1093. ret = device->ops.query_device(device, &device->attrs, &uhw);
  1094. if (ret) {
  1095. dev_warn(&device->dev,
  1096. "Couldn't query the device attributes\n");
  1097. return ret;
  1098. }
  1099. return 0;
  1100. }
  1101. static void disable_device(struct ib_device *device)
  1102. {
  1103. u32 cid;
  1104. WARN_ON(!refcount_read(&device->refcount));
  1105. down_write(&devices_rwsem);
  1106. xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
  1107. up_write(&devices_rwsem);
  1108. /*
  1109. * Remove clients in LIFO order, see assign_client_id. This could be
  1110. * more efficient if xarray learns to reverse iterate. Since no new
  1111. * clients can be added to this ib_device past this point we only need
  1112. * the maximum possible client_id value here.
  1113. */
  1114. down_read(&clients_rwsem);
  1115. cid = highest_client_id;
  1116. up_read(&clients_rwsem);
  1117. while (cid) {
  1118. cid--;
  1119. remove_client_context(device, cid);
  1120. }
  1121. ib_cq_pool_cleanup(device);
  1122. /* Pairs with refcount_set in enable_device */
  1123. ib_device_put(device);
  1124. wait_for_completion(&device->unreg_completion);
  1125. /*
  1126. * compat devices must be removed after device refcount drops to zero.
  1127. * Otherwise init_net() may add more compatdevs after removing compat
  1128. * devices and before device is disabled.
  1129. */
  1130. remove_compat_devs(device);
  1131. }
  1132. /*
  1133. * An enabled device is visible to all clients and to all the public facing
  1134. * APIs that return a device pointer. This always returns with a new get, even
  1135. * if it fails.
  1136. */
  1137. static int enable_device_and_get(struct ib_device *device)
  1138. {
  1139. struct ib_client *client;
  1140. unsigned long index;
  1141. int ret = 0;
  1142. /*
  1143. * One ref belongs to the xa and the other belongs to this
  1144. * thread. This is needed to guard against parallel unregistration.
  1145. */
  1146. refcount_set(&device->refcount, 2);
  1147. down_write(&devices_rwsem);
  1148. xa_set_mark(&devices, device->index, DEVICE_REGISTERED);
  1149. /*
  1150. * By using downgrade_write() we ensure that no other thread can clear
  1151. * DEVICE_REGISTERED while we are completing the client setup.
  1152. */
  1153. downgrade_write(&devices_rwsem);
  1154. if (device->ops.enable_driver) {
  1155. ret = device->ops.enable_driver(device);
  1156. if (ret)
  1157. goto out;
  1158. }
  1159. down_read(&clients_rwsem);
  1160. xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
  1161. ret = add_client_context(device, client);
  1162. if (ret)
  1163. break;
  1164. }
  1165. up_read(&clients_rwsem);
  1166. if (!ret)
  1167. ret = add_compat_devs(device);
  1168. out:
  1169. up_read(&devices_rwsem);
  1170. return ret;
  1171. }
  1172. static void prevent_dealloc_device(struct ib_device *ib_dev)
  1173. {
  1174. }
  1175. static void ib_device_notify_register(struct ib_device *device)
  1176. {
  1177. struct net_device *netdev;
  1178. u32 port;
  1179. int ret;
  1180. down_read(&devices_rwsem);
  1181. /* Mark for userspace that device is ready */
  1182. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1183. ret = rdma_nl_notify_event(device, 0, RDMA_REGISTER_EVENT);
  1184. if (ret)
  1185. goto out;
  1186. rdma_for_each_port(device, port) {
  1187. netdev = ib_device_get_netdev(device, port);
  1188. if (!netdev)
  1189. continue;
  1190. ret = rdma_nl_notify_event(device, port,
  1191. RDMA_NETDEV_ATTACH_EVENT);
  1192. dev_put(netdev);
  1193. if (ret)
  1194. goto out;
  1195. }
  1196. out:
  1197. up_read(&devices_rwsem);
  1198. }
  1199. /**
  1200. * ib_register_device - Register an IB device with IB core
  1201. * @device: Device to register
  1202. * @name: unique string device name. This may include a '%' which will
  1203. * cause a unique index to be added to the passed device name.
  1204. * @dma_device: pointer to a DMA-capable device. If %NULL, then the IB
  1205. * device will be used. In this case the caller should fully
  1206. * setup the ibdev for DMA. This usually means using dma_virt_ops.
  1207. *
  1208. * Low-level drivers use ib_register_device() to register their
  1209. * devices with the IB core. All registered clients will receive a
  1210. * callback for each device that is added. @device must be allocated
  1211. * with ib_alloc_device().
  1212. *
  1213. * If the driver uses ops.dealloc_driver and calls any ib_unregister_device()
  1214. * asynchronously then the device pointer may become freed as soon as this
  1215. * function returns.
  1216. */
  1217. int ib_register_device(struct ib_device *device, const char *name,
  1218. struct device *dma_device)
  1219. {
  1220. int ret;
  1221. ret = assign_name(device, name);
  1222. if (ret)
  1223. return ret;
  1224. /*
  1225. * If the caller does not provide a DMA capable device then the IB core
  1226. * will set up ib_sge and scatterlist structures that stash the kernel
  1227. * virtual address into the address field.
  1228. */
  1229. WARN_ON(dma_device && !dma_device->dma_parms);
  1230. device->dma_device = dma_device;
  1231. ret = setup_device(device);
  1232. if (ret)
  1233. return ret;
  1234. ret = ib_cache_setup_one(device);
  1235. if (ret) {
  1236. dev_warn(&device->dev,
  1237. "Couldn't set up InfiniBand P_Key/GID cache\n");
  1238. return ret;
  1239. }
  1240. device->groups[0] = &ib_dev_attr_group;
  1241. device->groups[1] = device->ops.device_group;
  1242. ret = ib_setup_device_attrs(device);
  1243. if (ret)
  1244. goto cache_cleanup;
  1245. ib_device_register_rdmacg(device);
  1246. rdma_counter_init(device);
  1247. /*
  1248. * Ensure that ADD uevent is not fired because it
  1249. * is too early amd device is not initialized yet.
  1250. */
  1251. dev_set_uevent_suppress(&device->dev, true);
  1252. ret = device_add(&device->dev);
  1253. if (ret)
  1254. goto cg_cleanup;
  1255. ret = ib_setup_port_attrs(&device->coredev);
  1256. if (ret) {
  1257. dev_warn(&device->dev,
  1258. "Couldn't register device with driver model\n");
  1259. goto dev_cleanup;
  1260. }
  1261. ret = enable_device_and_get(device);
  1262. if (ret) {
  1263. void (*dealloc_fn)(struct ib_device *);
  1264. /*
  1265. * If we hit this error flow then we don't want to
  1266. * automatically dealloc the device since the caller is
  1267. * expected to call ib_dealloc_device() after
  1268. * ib_register_device() fails. This is tricky due to the
  1269. * possibility for a parallel unregistration along with this
  1270. * error flow. Since we have a refcount here we know any
  1271. * parallel flow is stopped in disable_device and will see the
  1272. * special dealloc_driver pointer, causing the responsibility to
  1273. * ib_dealloc_device() to revert back to this thread.
  1274. */
  1275. dealloc_fn = device->ops.dealloc_driver;
  1276. device->ops.dealloc_driver = prevent_dealloc_device;
  1277. ib_device_put(device);
  1278. __ib_unregister_device(device);
  1279. device->ops.dealloc_driver = dealloc_fn;
  1280. dev_set_uevent_suppress(&device->dev, false);
  1281. return ret;
  1282. }
  1283. dev_set_uevent_suppress(&device->dev, false);
  1284. ib_device_notify_register(device);
  1285. ib_device_put(device);
  1286. return 0;
  1287. dev_cleanup:
  1288. device_del(&device->dev);
  1289. cg_cleanup:
  1290. dev_set_uevent_suppress(&device->dev, false);
  1291. ib_device_unregister_rdmacg(device);
  1292. cache_cleanup:
  1293. ib_cache_cleanup_one(device);
  1294. return ret;
  1295. }
  1296. EXPORT_SYMBOL(ib_register_device);
  1297. /* Callers must hold a get on the device. */
  1298. static void __ib_unregister_device(struct ib_device *ib_dev)
  1299. {
  1300. struct ib_device *sub, *tmp;
  1301. mutex_lock(&ib_dev->subdev_lock);
  1302. list_for_each_entry_safe_reverse(sub, tmp,
  1303. &ib_dev->subdev_list_head,
  1304. subdev_list) {
  1305. list_del(&sub->subdev_list);
  1306. ib_dev->ops.del_sub_dev(sub);
  1307. ib_device_put(ib_dev);
  1308. }
  1309. mutex_unlock(&ib_dev->subdev_lock);
  1310. /*
  1311. * We have a registration lock so that all the calls to unregister are
  1312. * fully fenced, once any unregister returns the device is truely
  1313. * unregistered even if multiple callers are unregistering it at the
  1314. * same time. This also interacts with the registration flow and
  1315. * provides sane semantics if register and unregister are racing.
  1316. */
  1317. mutex_lock(&ib_dev->unregistration_lock);
  1318. if (!refcount_read(&ib_dev->refcount))
  1319. goto out;
  1320. disable_device(ib_dev);
  1321. rdma_nl_notify_event(ib_dev, 0, RDMA_UNREGISTER_EVENT);
  1322. /* Expedite removing unregistered pointers from the hash table */
  1323. free_netdevs(ib_dev);
  1324. ib_free_port_attrs(&ib_dev->coredev);
  1325. device_del(&ib_dev->dev);
  1326. ib_device_unregister_rdmacg(ib_dev);
  1327. ib_cache_cleanup_one(ib_dev);
  1328. /*
  1329. * Drivers using the new flow may not call ib_dealloc_device except
  1330. * in error unwind prior to registration success.
  1331. */
  1332. if (ib_dev->ops.dealloc_driver &&
  1333. ib_dev->ops.dealloc_driver != prevent_dealloc_device) {
  1334. WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
  1335. ib_dealloc_device(ib_dev);
  1336. }
  1337. out:
  1338. mutex_unlock(&ib_dev->unregistration_lock);
  1339. }
  1340. /**
  1341. * ib_unregister_device - Unregister an IB device
  1342. * @ib_dev: The device to unregister
  1343. *
  1344. * Unregister an IB device. All clients will receive a remove callback.
  1345. *
  1346. * Callers should call this routine only once, and protect against races with
  1347. * registration. Typically it should only be called as part of a remove
  1348. * callback in an implementation of driver core's struct device_driver and
  1349. * related.
  1350. *
  1351. * If ops.dealloc_driver is used then ib_dev will be freed upon return from
  1352. * this function.
  1353. */
  1354. void ib_unregister_device(struct ib_device *ib_dev)
  1355. {
  1356. get_device(&ib_dev->dev);
  1357. __ib_unregister_device(ib_dev);
  1358. put_device(&ib_dev->dev);
  1359. }
  1360. EXPORT_SYMBOL(ib_unregister_device);
  1361. /**
  1362. * ib_unregister_device_and_put - Unregister a device while holding a 'get'
  1363. * @ib_dev: The device to unregister
  1364. *
  1365. * This is the same as ib_unregister_device(), except it includes an internal
  1366. * ib_device_put() that should match a 'get' obtained by the caller.
  1367. *
  1368. * It is safe to call this routine concurrently from multiple threads while
  1369. * holding the 'get'. When the function returns the device is fully
  1370. * unregistered.
  1371. *
  1372. * Drivers using this flow MUST use the driver_unregister callback to clean up
  1373. * their resources associated with the device and dealloc it.
  1374. */
  1375. void ib_unregister_device_and_put(struct ib_device *ib_dev)
  1376. {
  1377. WARN_ON(!ib_dev->ops.dealloc_driver);
  1378. get_device(&ib_dev->dev);
  1379. ib_device_put(ib_dev);
  1380. __ib_unregister_device(ib_dev);
  1381. put_device(&ib_dev->dev);
  1382. }
  1383. EXPORT_SYMBOL(ib_unregister_device_and_put);
  1384. /**
  1385. * ib_unregister_driver - Unregister all IB devices for a driver
  1386. * @driver_id: The driver to unregister
  1387. *
  1388. * This implements a fence for device unregistration. It only returns once all
  1389. * devices associated with the driver_id have fully completed their
  1390. * unregistration and returned from ib_unregister_device*().
  1391. *
  1392. * If device's are not yet unregistered it goes ahead and starts unregistering
  1393. * them.
  1394. *
  1395. * This does not block creation of new devices with the given driver_id, that
  1396. * is the responsibility of the caller.
  1397. */
  1398. void ib_unregister_driver(enum rdma_driver_id driver_id)
  1399. {
  1400. struct ib_device *ib_dev;
  1401. unsigned long index;
  1402. down_read(&devices_rwsem);
  1403. xa_for_each (&devices, index, ib_dev) {
  1404. if (ib_dev->ops.driver_id != driver_id)
  1405. continue;
  1406. get_device(&ib_dev->dev);
  1407. up_read(&devices_rwsem);
  1408. WARN_ON(!ib_dev->ops.dealloc_driver);
  1409. __ib_unregister_device(ib_dev);
  1410. put_device(&ib_dev->dev);
  1411. down_read(&devices_rwsem);
  1412. }
  1413. up_read(&devices_rwsem);
  1414. }
  1415. EXPORT_SYMBOL(ib_unregister_driver);
  1416. static void ib_unregister_work(struct work_struct *work)
  1417. {
  1418. struct ib_device *ib_dev =
  1419. container_of(work, struct ib_device, unregistration_work);
  1420. __ib_unregister_device(ib_dev);
  1421. put_device(&ib_dev->dev);
  1422. }
  1423. /**
  1424. * ib_unregister_device_queued - Unregister a device using a work queue
  1425. * @ib_dev: The device to unregister
  1426. *
  1427. * This schedules an asynchronous unregistration using a WQ for the device. A
  1428. * driver should use this to avoid holding locks while doing unregistration,
  1429. * such as holding the RTNL lock.
  1430. *
  1431. * Drivers using this API must use ib_unregister_driver before module unload
  1432. * to ensure that all scheduled unregistrations have completed.
  1433. */
  1434. void ib_unregister_device_queued(struct ib_device *ib_dev)
  1435. {
  1436. WARN_ON(!refcount_read(&ib_dev->refcount));
  1437. WARN_ON(!ib_dev->ops.dealloc_driver);
  1438. get_device(&ib_dev->dev);
  1439. if (!queue_work(ib_unreg_wq, &ib_dev->unregistration_work))
  1440. put_device(&ib_dev->dev);
  1441. }
  1442. EXPORT_SYMBOL(ib_unregister_device_queued);
  1443. /*
  1444. * The caller must pass in a device that has the kref held and the refcount
  1445. * released. If the device is in cur_net and still registered then it is moved
  1446. * into net.
  1447. */
  1448. static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
  1449. struct net *net)
  1450. {
  1451. int ret2 = -EINVAL;
  1452. int ret;
  1453. mutex_lock(&device->unregistration_lock);
  1454. /*
  1455. * If a device not under ib_device_get() or if the unregistration_lock
  1456. * is not held, the namespace can be changed, or it can be unregistered.
  1457. * Check again under the lock.
  1458. */
  1459. if (refcount_read(&device->refcount) == 0 ||
  1460. !net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) {
  1461. ret = -ENODEV;
  1462. goto out;
  1463. }
  1464. kobject_uevent(&device->dev.kobj, KOBJ_REMOVE);
  1465. disable_device(device);
  1466. /*
  1467. * At this point no one can be using the device, so it is safe to
  1468. * change the namespace.
  1469. */
  1470. write_pnet(&device->coredev.rdma_net, net);
  1471. down_read(&devices_rwsem);
  1472. /*
  1473. * Currently rdma devices are system wide unique. So the device name
  1474. * is guaranteed free in the new namespace. Publish the new namespace
  1475. * at the sysfs level.
  1476. */
  1477. ret = device_rename(&device->dev, dev_name(&device->dev));
  1478. up_read(&devices_rwsem);
  1479. if (ret) {
  1480. dev_warn(&device->dev,
  1481. "%s: Couldn't rename device after namespace change\n",
  1482. __func__);
  1483. /* Try and put things back and re-enable the device */
  1484. write_pnet(&device->coredev.rdma_net, cur_net);
  1485. }
  1486. ret2 = enable_device_and_get(device);
  1487. if (ret2) {
  1488. /*
  1489. * This shouldn't really happen, but if it does, let the user
  1490. * retry at later point. So don't disable the device.
  1491. */
  1492. dev_warn(&device->dev,
  1493. "%s: Couldn't re-enable device after namespace change\n",
  1494. __func__);
  1495. }
  1496. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1497. ib_device_put(device);
  1498. out:
  1499. mutex_unlock(&device->unregistration_lock);
  1500. if (ret)
  1501. return ret;
  1502. return ret2;
  1503. }
  1504. int ib_device_set_netns_put(struct sk_buff *skb,
  1505. struct ib_device *dev, u32 ns_fd)
  1506. {
  1507. struct net *net;
  1508. int ret;
  1509. net = get_net_ns_by_fd(ns_fd);
  1510. if (IS_ERR(net)) {
  1511. ret = PTR_ERR(net);
  1512. goto net_err;
  1513. }
  1514. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
  1515. ret = -EPERM;
  1516. goto ns_err;
  1517. }
  1518. /*
  1519. * All the ib_clients, including uverbs, are reset when the namespace is
  1520. * changed and this cannot be blocked waiting for userspace to do
  1521. * something, so disassociation is mandatory.
  1522. */
  1523. if (!dev->ops.disassociate_ucontext || ib_devices_shared_netns) {
  1524. ret = -EOPNOTSUPP;
  1525. goto ns_err;
  1526. }
  1527. get_device(&dev->dev);
  1528. ib_device_put(dev);
  1529. ret = rdma_dev_change_netns(dev, current->nsproxy->net_ns, net);
  1530. put_device(&dev->dev);
  1531. put_net(net);
  1532. return ret;
  1533. ns_err:
  1534. put_net(net);
  1535. net_err:
  1536. ib_device_put(dev);
  1537. return ret;
  1538. }
  1539. static struct pernet_operations rdma_dev_net_ops = {
  1540. .init = rdma_dev_init_net,
  1541. .exit = rdma_dev_exit_net,
  1542. .id = &rdma_dev_net_id,
  1543. .size = sizeof(struct rdma_dev_net),
  1544. };
  1545. static int assign_client_id(struct ib_client *client)
  1546. {
  1547. int ret;
  1548. lockdep_assert_held(&clients_rwsem);
  1549. /*
  1550. * The add/remove callbacks must be called in FIFO/LIFO order. To
  1551. * achieve this we assign client_ids so they are sorted in
  1552. * registration order.
  1553. */
  1554. client->client_id = highest_client_id;
  1555. ret = xa_insert(&clients, client->client_id, client, GFP_KERNEL);
  1556. if (ret)
  1557. return ret;
  1558. highest_client_id++;
  1559. xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED);
  1560. return 0;
  1561. }
  1562. static void remove_client_id(struct ib_client *client)
  1563. {
  1564. down_write(&clients_rwsem);
  1565. xa_erase(&clients, client->client_id);
  1566. for (; highest_client_id; highest_client_id--)
  1567. if (xa_load(&clients, highest_client_id - 1))
  1568. break;
  1569. up_write(&clients_rwsem);
  1570. }
  1571. /**
  1572. * ib_register_client - Register an IB client
  1573. * @client:Client to register
  1574. *
  1575. * Upper level users of the IB drivers can use ib_register_client() to
  1576. * register callbacks for IB device addition and removal. When an IB
  1577. * device is added, each registered client's add method will be called
  1578. * (in the order the clients were registered), and when a device is
  1579. * removed, each client's remove method will be called (in the reverse
  1580. * order that clients were registered). In addition, when
  1581. * ib_register_client() is called, the client will receive an add
  1582. * callback for all devices already registered.
  1583. */
  1584. int ib_register_client(struct ib_client *client)
  1585. {
  1586. struct ib_device *device;
  1587. unsigned long index;
  1588. bool need_unreg = false;
  1589. int ret;
  1590. refcount_set(&client->uses, 1);
  1591. init_completion(&client->uses_zero);
  1592. /*
  1593. * The devices_rwsem is held in write mode to ensure that a racing
  1594. * ib_register_device() sees a consisent view of clients and devices.
  1595. */
  1596. down_write(&devices_rwsem);
  1597. down_write(&clients_rwsem);
  1598. ret = assign_client_id(client);
  1599. if (ret)
  1600. goto out;
  1601. need_unreg = true;
  1602. xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) {
  1603. ret = add_client_context(device, client);
  1604. if (ret)
  1605. goto out;
  1606. }
  1607. ret = 0;
  1608. out:
  1609. up_write(&clients_rwsem);
  1610. up_write(&devices_rwsem);
  1611. if (need_unreg && ret)
  1612. ib_unregister_client(client);
  1613. return ret;
  1614. }
  1615. EXPORT_SYMBOL(ib_register_client);
  1616. /**
  1617. * ib_unregister_client - Unregister an IB client
  1618. * @client:Client to unregister
  1619. *
  1620. * Upper level users use ib_unregister_client() to remove their client
  1621. * registration. When ib_unregister_client() is called, the client
  1622. * will receive a remove callback for each IB device still registered.
  1623. *
  1624. * This is a full fence, once it returns no client callbacks will be called,
  1625. * or are running in another thread.
  1626. */
  1627. void ib_unregister_client(struct ib_client *client)
  1628. {
  1629. struct ib_device *device;
  1630. unsigned long index;
  1631. down_write(&clients_rwsem);
  1632. ib_client_put(client);
  1633. xa_clear_mark(&clients, client->client_id, CLIENT_REGISTERED);
  1634. up_write(&clients_rwsem);
  1635. /* We do not want to have locks while calling client->remove() */
  1636. rcu_read_lock();
  1637. xa_for_each (&devices, index, device) {
  1638. if (!ib_device_try_get(device))
  1639. continue;
  1640. rcu_read_unlock();
  1641. remove_client_context(device, client->client_id);
  1642. ib_device_put(device);
  1643. rcu_read_lock();
  1644. }
  1645. rcu_read_unlock();
  1646. /*
  1647. * remove_client_context() is not a fence, it can return even though a
  1648. * removal is ongoing. Wait until all removals are completed.
  1649. */
  1650. wait_for_completion(&client->uses_zero);
  1651. remove_client_id(client);
  1652. }
  1653. EXPORT_SYMBOL(ib_unregister_client);
  1654. static int __ib_get_global_client_nl_info(const char *client_name,
  1655. struct ib_client_nl_info *res)
  1656. {
  1657. struct ib_client *client;
  1658. unsigned long index;
  1659. int ret = -ENOENT;
  1660. down_read(&clients_rwsem);
  1661. xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
  1662. if (strcmp(client->name, client_name) != 0)
  1663. continue;
  1664. if (!client->get_global_nl_info) {
  1665. ret = -EOPNOTSUPP;
  1666. break;
  1667. }
  1668. ret = client->get_global_nl_info(res);
  1669. if (WARN_ON(ret == -ENOENT))
  1670. ret = -EINVAL;
  1671. if (!ret && res->cdev)
  1672. get_device(res->cdev);
  1673. break;
  1674. }
  1675. up_read(&clients_rwsem);
  1676. return ret;
  1677. }
  1678. static int __ib_get_client_nl_info(struct ib_device *ibdev,
  1679. const char *client_name,
  1680. struct ib_client_nl_info *res)
  1681. {
  1682. unsigned long index;
  1683. void *client_data;
  1684. int ret = -ENOENT;
  1685. down_read(&ibdev->client_data_rwsem);
  1686. xan_for_each_marked (&ibdev->client_data, index, client_data,
  1687. CLIENT_DATA_REGISTERED) {
  1688. struct ib_client *client = xa_load(&clients, index);
  1689. if (!client || strcmp(client->name, client_name) != 0)
  1690. continue;
  1691. if (!client->get_nl_info) {
  1692. ret = -EOPNOTSUPP;
  1693. break;
  1694. }
  1695. ret = client->get_nl_info(ibdev, client_data, res);
  1696. if (WARN_ON(ret == -ENOENT))
  1697. ret = -EINVAL;
  1698. /*
  1699. * The cdev is guaranteed valid as long as we are inside the
  1700. * client_data_rwsem as remove_one can't be called. Keep it
  1701. * valid for the caller.
  1702. */
  1703. if (!ret && res->cdev)
  1704. get_device(res->cdev);
  1705. break;
  1706. }
  1707. up_read(&ibdev->client_data_rwsem);
  1708. return ret;
  1709. }
  1710. /**
  1711. * ib_get_client_nl_info - Fetch the nl_info from a client
  1712. * @ibdev: IB device
  1713. * @client_name: Name of the client
  1714. * @res: Result of the query
  1715. */
  1716. int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
  1717. struct ib_client_nl_info *res)
  1718. {
  1719. int ret;
  1720. if (ibdev)
  1721. ret = __ib_get_client_nl_info(ibdev, client_name, res);
  1722. else
  1723. ret = __ib_get_global_client_nl_info(client_name, res);
  1724. #ifdef CONFIG_MODULES
  1725. if (ret == -ENOENT) {
  1726. request_module("rdma-client-%s", client_name);
  1727. if (ibdev)
  1728. ret = __ib_get_client_nl_info(ibdev, client_name, res);
  1729. else
  1730. ret = __ib_get_global_client_nl_info(client_name, res);
  1731. }
  1732. #endif
  1733. if (ret) {
  1734. if (ret == -ENOENT)
  1735. return -EOPNOTSUPP;
  1736. return ret;
  1737. }
  1738. if (WARN_ON(!res->cdev))
  1739. return -EINVAL;
  1740. return 0;
  1741. }
  1742. /**
  1743. * ib_set_client_data - Set IB client context
  1744. * @device:Device to set context for
  1745. * @client:Client to set context for
  1746. * @data:Context to set
  1747. *
  1748. * ib_set_client_data() sets client context data that can be retrieved with
  1749. * ib_get_client_data(). This can only be called while the client is
  1750. * registered to the device, once the ib_client remove() callback returns this
  1751. * cannot be called.
  1752. */
  1753. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  1754. void *data)
  1755. {
  1756. void *rc;
  1757. if (WARN_ON(IS_ERR(data)))
  1758. data = NULL;
  1759. rc = xa_store(&device->client_data, client->client_id, data,
  1760. GFP_KERNEL);
  1761. WARN_ON(xa_is_err(rc));
  1762. }
  1763. EXPORT_SYMBOL(ib_set_client_data);
  1764. /**
  1765. * ib_register_event_handler - Register an IB event handler
  1766. * @event_handler:Handler to register
  1767. *
  1768. * ib_register_event_handler() registers an event handler that will be
  1769. * called back when asynchronous IB events occur (as defined in
  1770. * chapter 11 of the InfiniBand Architecture Specification). This
  1771. * callback occurs in workqueue context.
  1772. */
  1773. void ib_register_event_handler(struct ib_event_handler *event_handler)
  1774. {
  1775. down_write(&event_handler->device->event_handler_rwsem);
  1776. list_add_tail(&event_handler->list,
  1777. &event_handler->device->event_handler_list);
  1778. up_write(&event_handler->device->event_handler_rwsem);
  1779. }
  1780. EXPORT_SYMBOL(ib_register_event_handler);
  1781. /**
  1782. * ib_unregister_event_handler - Unregister an event handler
  1783. * @event_handler:Handler to unregister
  1784. *
  1785. * Unregister an event handler registered with
  1786. * ib_register_event_handler().
  1787. */
  1788. void ib_unregister_event_handler(struct ib_event_handler *event_handler)
  1789. {
  1790. down_write(&event_handler->device->event_handler_rwsem);
  1791. list_del(&event_handler->list);
  1792. up_write(&event_handler->device->event_handler_rwsem);
  1793. }
  1794. EXPORT_SYMBOL(ib_unregister_event_handler);
  1795. void ib_dispatch_event_clients(struct ib_event *event)
  1796. {
  1797. struct ib_event_handler *handler;
  1798. down_read(&event->device->event_handler_rwsem);
  1799. list_for_each_entry(handler, &event->device->event_handler_list, list)
  1800. handler->handler(handler, event);
  1801. up_read(&event->device->event_handler_rwsem);
  1802. }
  1803. static int iw_query_port(struct ib_device *device,
  1804. u32 port_num,
  1805. struct ib_port_attr *port_attr)
  1806. {
  1807. struct in_device *inetdev;
  1808. struct net_device *netdev;
  1809. memset(port_attr, 0, sizeof(*port_attr));
  1810. netdev = ib_device_get_netdev(device, port_num);
  1811. if (!netdev)
  1812. return -ENODEV;
  1813. port_attr->max_mtu = IB_MTU_4096;
  1814. port_attr->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
  1815. if (!netif_carrier_ok(netdev)) {
  1816. port_attr->state = IB_PORT_DOWN;
  1817. port_attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
  1818. } else {
  1819. rcu_read_lock();
  1820. inetdev = __in_dev_get_rcu(netdev);
  1821. if (inetdev && inetdev->ifa_list) {
  1822. port_attr->state = IB_PORT_ACTIVE;
  1823. port_attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
  1824. } else {
  1825. port_attr->state = IB_PORT_INIT;
  1826. port_attr->phys_state =
  1827. IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING;
  1828. }
  1829. rcu_read_unlock();
  1830. }
  1831. dev_put(netdev);
  1832. return device->ops.query_port(device, port_num, port_attr);
  1833. }
  1834. static int __ib_query_port(struct ib_device *device,
  1835. u32 port_num,
  1836. struct ib_port_attr *port_attr)
  1837. {
  1838. int err;
  1839. memset(port_attr, 0, sizeof(*port_attr));
  1840. err = device->ops.query_port(device, port_num, port_attr);
  1841. if (err || port_attr->subnet_prefix)
  1842. return err;
  1843. if (rdma_port_get_link_layer(device, port_num) !=
  1844. IB_LINK_LAYER_INFINIBAND)
  1845. return 0;
  1846. ib_get_cached_subnet_prefix(device, port_num,
  1847. &port_attr->subnet_prefix);
  1848. return 0;
  1849. }
  1850. /**
  1851. * ib_query_port - Query IB port attributes
  1852. * @device:Device to query
  1853. * @port_num:Port number to query
  1854. * @port_attr:Port attributes
  1855. *
  1856. * ib_query_port() returns the attributes of a port through the
  1857. * @port_attr pointer.
  1858. */
  1859. int ib_query_port(struct ib_device *device,
  1860. u32 port_num,
  1861. struct ib_port_attr *port_attr)
  1862. {
  1863. if (!rdma_is_port_valid(device, port_num))
  1864. return -EINVAL;
  1865. if (rdma_protocol_iwarp(device, port_num))
  1866. return iw_query_port(device, port_num, port_attr);
  1867. else
  1868. return __ib_query_port(device, port_num, port_attr);
  1869. }
  1870. EXPORT_SYMBOL(ib_query_port);
  1871. static void add_ndev_hash(struct ib_port_data *pdata)
  1872. {
  1873. unsigned long flags;
  1874. might_sleep();
  1875. spin_lock_irqsave(&ndev_hash_lock, flags);
  1876. if (hash_hashed(&pdata->ndev_hash_link)) {
  1877. hash_del_rcu(&pdata->ndev_hash_link);
  1878. spin_unlock_irqrestore(&ndev_hash_lock, flags);
  1879. /*
  1880. * We cannot do hash_add_rcu after a hash_del_rcu until the
  1881. * grace period
  1882. */
  1883. synchronize_rcu();
  1884. spin_lock_irqsave(&ndev_hash_lock, flags);
  1885. }
  1886. if (pdata->netdev)
  1887. hash_add_rcu(ndev_hash, &pdata->ndev_hash_link,
  1888. (uintptr_t)pdata->netdev);
  1889. spin_unlock_irqrestore(&ndev_hash_lock, flags);
  1890. }
  1891. /**
  1892. * ib_device_set_netdev - Associate the ib_dev with an underlying net_device
  1893. * @ib_dev: Device to modify
  1894. * @ndev: net_device to affiliate, may be NULL
  1895. * @port: IB port the net_device is connected to
  1896. *
  1897. * Drivers should use this to link the ib_device to a netdev so the netdev
  1898. * shows up in interfaces like ib_enum_roce_netdev. Only one netdev may be
  1899. * affiliated with any port.
  1900. *
  1901. * The caller must ensure that the given ndev is not unregistered or
  1902. * unregistering, and that either the ib_device is unregistered or
  1903. * ib_device_set_netdev() is called with NULL when the ndev sends a
  1904. * NETDEV_UNREGISTER event.
  1905. */
  1906. int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
  1907. u32 port)
  1908. {
  1909. enum rdma_nl_notify_event_type etype;
  1910. struct net_device *old_ndev;
  1911. struct ib_port_data *pdata;
  1912. unsigned long flags;
  1913. int ret;
  1914. if (!rdma_is_port_valid(ib_dev, port))
  1915. return -EINVAL;
  1916. /*
  1917. * Drivers wish to call this before ib_register_driver, so we have to
  1918. * setup the port data early.
  1919. */
  1920. ret = alloc_port_data(ib_dev);
  1921. if (ret)
  1922. return ret;
  1923. pdata = &ib_dev->port_data[port];
  1924. spin_lock_irqsave(&pdata->netdev_lock, flags);
  1925. old_ndev = rcu_dereference_protected(
  1926. pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
  1927. if (old_ndev == ndev) {
  1928. spin_unlock_irqrestore(&pdata->netdev_lock, flags);
  1929. return 0;
  1930. }
  1931. rcu_assign_pointer(pdata->netdev, ndev);
  1932. netdev_put(old_ndev, &pdata->netdev_tracker);
  1933. netdev_hold(ndev, &pdata->netdev_tracker, GFP_ATOMIC);
  1934. spin_unlock_irqrestore(&pdata->netdev_lock, flags);
  1935. add_ndev_hash(pdata);
  1936. /* Make sure that the device is registered before we send events */
  1937. if (xa_load(&devices, ib_dev->index) != ib_dev)
  1938. return 0;
  1939. etype = ndev ? RDMA_NETDEV_ATTACH_EVENT : RDMA_NETDEV_DETACH_EVENT;
  1940. rdma_nl_notify_event(ib_dev, port, etype);
  1941. return 0;
  1942. }
  1943. EXPORT_SYMBOL(ib_device_set_netdev);
  1944. static void free_netdevs(struct ib_device *ib_dev)
  1945. {
  1946. unsigned long flags;
  1947. u32 port;
  1948. if (!ib_dev->port_data)
  1949. return;
  1950. rdma_for_each_port (ib_dev, port) {
  1951. struct ib_port_data *pdata = &ib_dev->port_data[port];
  1952. struct net_device *ndev;
  1953. spin_lock_irqsave(&pdata->netdev_lock, flags);
  1954. ndev = rcu_dereference_protected(
  1955. pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
  1956. if (ndev) {
  1957. spin_lock(&ndev_hash_lock);
  1958. hash_del_rcu(&pdata->ndev_hash_link);
  1959. spin_unlock(&ndev_hash_lock);
  1960. /*
  1961. * If this is the last dev_put there is still a
  1962. * synchronize_rcu before the netdev is kfreed, so we
  1963. * can continue to rely on unlocked pointer
  1964. * comparisons after the put
  1965. */
  1966. rcu_assign_pointer(pdata->netdev, NULL);
  1967. netdev_put(ndev, &pdata->netdev_tracker);
  1968. }
  1969. spin_unlock_irqrestore(&pdata->netdev_lock, flags);
  1970. }
  1971. }
  1972. struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
  1973. u32 port)
  1974. {
  1975. struct ib_port_data *pdata;
  1976. struct net_device *res;
  1977. if (!rdma_is_port_valid(ib_dev, port))
  1978. return NULL;
  1979. if (!ib_dev->port_data)
  1980. return NULL;
  1981. pdata = &ib_dev->port_data[port];
  1982. /*
  1983. * New drivers should use ib_device_set_netdev() not the legacy
  1984. * get_netdev().
  1985. */
  1986. if (ib_dev->ops.get_netdev)
  1987. res = ib_dev->ops.get_netdev(ib_dev, port);
  1988. else {
  1989. spin_lock(&pdata->netdev_lock);
  1990. res = rcu_dereference_protected(
  1991. pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
  1992. dev_hold(res);
  1993. spin_unlock(&pdata->netdev_lock);
  1994. }
  1995. return res;
  1996. }
  1997. EXPORT_SYMBOL(ib_device_get_netdev);
  1998. /**
  1999. * ib_device_get_by_netdev - Find an IB device associated with a netdev
  2000. * @ndev: netdev to locate
  2001. * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all)
  2002. *
  2003. * Find and hold an ib_device that is associated with a netdev via
  2004. * ib_device_set_netdev(). The caller must call ib_device_put() on the
  2005. * returned pointer.
  2006. */
  2007. struct ib_device *ib_device_get_by_netdev(struct net_device *ndev,
  2008. enum rdma_driver_id driver_id)
  2009. {
  2010. struct ib_device *res = NULL;
  2011. struct ib_port_data *cur;
  2012. rcu_read_lock();
  2013. hash_for_each_possible_rcu (ndev_hash, cur, ndev_hash_link,
  2014. (uintptr_t)ndev) {
  2015. if (rcu_access_pointer(cur->netdev) == ndev &&
  2016. (driver_id == RDMA_DRIVER_UNKNOWN ||
  2017. cur->ib_dev->ops.driver_id == driver_id) &&
  2018. ib_device_try_get(cur->ib_dev)) {
  2019. res = cur->ib_dev;
  2020. break;
  2021. }
  2022. }
  2023. rcu_read_unlock();
  2024. return res;
  2025. }
  2026. EXPORT_SYMBOL(ib_device_get_by_netdev);
  2027. /**
  2028. * ib_enum_roce_netdev - enumerate all RoCE ports
  2029. * @ib_dev : IB device we want to query
  2030. * @filter: Should we call the callback?
  2031. * @filter_cookie: Cookie passed to filter
  2032. * @cb: Callback to call for each found RoCE ports
  2033. * @cookie: Cookie passed back to the callback
  2034. *
  2035. * Enumerates all of the physical RoCE ports of ib_dev
  2036. * which are related to netdevice and calls callback() on each
  2037. * device for which filter() function returns non zero.
  2038. */
  2039. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  2040. roce_netdev_filter filter,
  2041. void *filter_cookie,
  2042. roce_netdev_callback cb,
  2043. void *cookie)
  2044. {
  2045. u32 port;
  2046. rdma_for_each_port (ib_dev, port)
  2047. if (rdma_protocol_roce(ib_dev, port)) {
  2048. struct net_device *idev =
  2049. ib_device_get_netdev(ib_dev, port);
  2050. if (filter(ib_dev, port, idev, filter_cookie))
  2051. cb(ib_dev, port, idev, cookie);
  2052. dev_put(idev);
  2053. }
  2054. }
  2055. /**
  2056. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  2057. * @filter: Should we call the callback?
  2058. * @filter_cookie: Cookie passed to filter
  2059. * @cb: Callback to call for each found RoCE ports
  2060. * @cookie: Cookie passed back to the callback
  2061. *
  2062. * Enumerates all RoCE devices' physical ports which are related
  2063. * to netdevices and calls callback() on each device for which
  2064. * filter() function returns non zero.
  2065. */
  2066. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  2067. void *filter_cookie,
  2068. roce_netdev_callback cb,
  2069. void *cookie)
  2070. {
  2071. struct ib_device *dev;
  2072. unsigned long index;
  2073. down_read(&devices_rwsem);
  2074. xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED)
  2075. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  2076. up_read(&devices_rwsem);
  2077. }
  2078. /*
  2079. * ib_enum_all_devs - enumerate all ib_devices
  2080. * @cb: Callback to call for each found ib_device
  2081. *
  2082. * Enumerates all ib_devices and calls callback() on each device.
  2083. */
  2084. int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
  2085. struct netlink_callback *cb)
  2086. {
  2087. unsigned long index;
  2088. struct ib_device *dev;
  2089. unsigned int idx = 0;
  2090. int ret = 0;
  2091. down_read(&devices_rwsem);
  2092. xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
  2093. if (!rdma_dev_access_netns(dev, sock_net(skb->sk)))
  2094. continue;
  2095. ret = nldev_cb(dev, skb, cb, idx);
  2096. if (ret)
  2097. break;
  2098. idx++;
  2099. }
  2100. up_read(&devices_rwsem);
  2101. return ret;
  2102. }
  2103. /**
  2104. * ib_query_pkey - Get P_Key table entry
  2105. * @device:Device to query
  2106. * @port_num:Port number to query
  2107. * @index:P_Key table index to query
  2108. * @pkey:Returned P_Key
  2109. *
  2110. * ib_query_pkey() fetches the specified P_Key table entry.
  2111. */
  2112. int ib_query_pkey(struct ib_device *device,
  2113. u32 port_num, u16 index, u16 *pkey)
  2114. {
  2115. if (!rdma_is_port_valid(device, port_num))
  2116. return -EINVAL;
  2117. if (!device->ops.query_pkey)
  2118. return -EOPNOTSUPP;
  2119. return device->ops.query_pkey(device, port_num, index, pkey);
  2120. }
  2121. EXPORT_SYMBOL(ib_query_pkey);
  2122. /**
  2123. * ib_modify_device - Change IB device attributes
  2124. * @device:Device to modify
  2125. * @device_modify_mask:Mask of attributes to change
  2126. * @device_modify:New attribute values
  2127. *
  2128. * ib_modify_device() changes a device's attributes as specified by
  2129. * the @device_modify_mask and @device_modify structure.
  2130. */
  2131. int ib_modify_device(struct ib_device *device,
  2132. int device_modify_mask,
  2133. struct ib_device_modify *device_modify)
  2134. {
  2135. if (!device->ops.modify_device)
  2136. return -EOPNOTSUPP;
  2137. return device->ops.modify_device(device, device_modify_mask,
  2138. device_modify);
  2139. }
  2140. EXPORT_SYMBOL(ib_modify_device);
  2141. /**
  2142. * ib_modify_port - Modifies the attributes for the specified port.
  2143. * @device: The device to modify.
  2144. * @port_num: The number of the port to modify.
  2145. * @port_modify_mask: Mask used to specify which attributes of the port
  2146. * to change.
  2147. * @port_modify: New attribute values for the port.
  2148. *
  2149. * ib_modify_port() changes a port's attributes as specified by the
  2150. * @port_modify_mask and @port_modify structure.
  2151. */
  2152. int ib_modify_port(struct ib_device *device,
  2153. u32 port_num, int port_modify_mask,
  2154. struct ib_port_modify *port_modify)
  2155. {
  2156. int rc;
  2157. if (!rdma_is_port_valid(device, port_num))
  2158. return -EINVAL;
  2159. if (device->ops.modify_port)
  2160. rc = device->ops.modify_port(device, port_num,
  2161. port_modify_mask,
  2162. port_modify);
  2163. else if (rdma_protocol_roce(device, port_num) &&
  2164. ((port_modify->set_port_cap_mask & ~IB_PORT_CM_SUP) == 0 ||
  2165. (port_modify->clr_port_cap_mask & ~IB_PORT_CM_SUP) == 0))
  2166. rc = 0;
  2167. else
  2168. rc = -EOPNOTSUPP;
  2169. return rc;
  2170. }
  2171. EXPORT_SYMBOL(ib_modify_port);
  2172. /**
  2173. * ib_find_gid - Returns the port number and GID table index where
  2174. * a specified GID value occurs. Its searches only for IB link layer.
  2175. * @device: The device to query.
  2176. * @gid: The GID value to search for.
  2177. * @port_num: The port number of the device where the GID value was found.
  2178. * @index: The index into the GID table where the GID was found. This
  2179. * parameter may be NULL.
  2180. */
  2181. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  2182. u32 *port_num, u16 *index)
  2183. {
  2184. union ib_gid tmp_gid;
  2185. u32 port;
  2186. int ret, i;
  2187. rdma_for_each_port (device, port) {
  2188. if (!rdma_protocol_ib(device, port))
  2189. continue;
  2190. for (i = 0; i < device->port_data[port].immutable.gid_tbl_len;
  2191. ++i) {
  2192. ret = rdma_query_gid(device, port, i, &tmp_gid);
  2193. if (ret)
  2194. continue;
  2195. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  2196. *port_num = port;
  2197. if (index)
  2198. *index = i;
  2199. return 0;
  2200. }
  2201. }
  2202. }
  2203. return -ENOENT;
  2204. }
  2205. EXPORT_SYMBOL(ib_find_gid);
  2206. /**
  2207. * ib_find_pkey - Returns the PKey table index where a specified
  2208. * PKey value occurs.
  2209. * @device: The device to query.
  2210. * @port_num: The port number of the device to search for the PKey.
  2211. * @pkey: The PKey value to search for.
  2212. * @index: The index into the PKey table where the PKey was found.
  2213. */
  2214. int ib_find_pkey(struct ib_device *device,
  2215. u32 port_num, u16 pkey, u16 *index)
  2216. {
  2217. int ret, i;
  2218. u16 tmp_pkey;
  2219. int partial_ix = -1;
  2220. for (i = 0; i < device->port_data[port_num].immutable.pkey_tbl_len;
  2221. ++i) {
  2222. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  2223. if (ret)
  2224. return ret;
  2225. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  2226. /* if there is full-member pkey take it.*/
  2227. if (tmp_pkey & 0x8000) {
  2228. *index = i;
  2229. return 0;
  2230. }
  2231. if (partial_ix < 0)
  2232. partial_ix = i;
  2233. }
  2234. }
  2235. /*no full-member, if exists take the limited*/
  2236. if (partial_ix >= 0) {
  2237. *index = partial_ix;
  2238. return 0;
  2239. }
  2240. return -ENOENT;
  2241. }
  2242. EXPORT_SYMBOL(ib_find_pkey);
  2243. /**
  2244. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  2245. * for a received CM request
  2246. * @dev: An RDMA device on which the request has been received.
  2247. * @port: Port number on the RDMA device.
  2248. * @pkey: The Pkey the request came on.
  2249. * @gid: A GID that the net_dev uses to communicate.
  2250. * @addr: Contains the IP address that the request specified as its
  2251. * destination.
  2252. *
  2253. */
  2254. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  2255. u32 port,
  2256. u16 pkey,
  2257. const union ib_gid *gid,
  2258. const struct sockaddr *addr)
  2259. {
  2260. struct net_device *net_dev = NULL;
  2261. unsigned long index;
  2262. void *client_data;
  2263. if (!rdma_protocol_ib(dev, port))
  2264. return NULL;
  2265. /*
  2266. * Holding the read side guarantees that the client will not become
  2267. * unregistered while we are calling get_net_dev_by_params()
  2268. */
  2269. down_read(&dev->client_data_rwsem);
  2270. xan_for_each_marked (&dev->client_data, index, client_data,
  2271. CLIENT_DATA_REGISTERED) {
  2272. struct ib_client *client = xa_load(&clients, index);
  2273. if (!client || !client->get_net_dev_by_params)
  2274. continue;
  2275. net_dev = client->get_net_dev_by_params(dev, port, pkey, gid,
  2276. addr, client_data);
  2277. if (net_dev)
  2278. break;
  2279. }
  2280. up_read(&dev->client_data_rwsem);
  2281. return net_dev;
  2282. }
  2283. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  2284. void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
  2285. {
  2286. struct ib_device_ops *dev_ops = &dev->ops;
  2287. #define SET_DEVICE_OP(ptr, name) \
  2288. do { \
  2289. if (ops->name) \
  2290. if (!((ptr)->name)) \
  2291. (ptr)->name = ops->name; \
  2292. } while (0)
  2293. #define SET_OBJ_SIZE(ptr, name) SET_DEVICE_OP(ptr, size_##name)
  2294. if (ops->driver_id != RDMA_DRIVER_UNKNOWN) {
  2295. WARN_ON(dev_ops->driver_id != RDMA_DRIVER_UNKNOWN &&
  2296. dev_ops->driver_id != ops->driver_id);
  2297. dev_ops->driver_id = ops->driver_id;
  2298. }
  2299. if (ops->owner) {
  2300. WARN_ON(dev_ops->owner && dev_ops->owner != ops->owner);
  2301. dev_ops->owner = ops->owner;
  2302. }
  2303. if (ops->uverbs_abi_ver)
  2304. dev_ops->uverbs_abi_ver = ops->uverbs_abi_ver;
  2305. dev_ops->uverbs_no_driver_id_binding |=
  2306. ops->uverbs_no_driver_id_binding;
  2307. SET_DEVICE_OP(dev_ops, add_gid);
  2308. SET_DEVICE_OP(dev_ops, add_sub_dev);
  2309. SET_DEVICE_OP(dev_ops, advise_mr);
  2310. SET_DEVICE_OP(dev_ops, alloc_dm);
  2311. SET_DEVICE_OP(dev_ops, alloc_hw_device_stats);
  2312. SET_DEVICE_OP(dev_ops, alloc_hw_port_stats);
  2313. SET_DEVICE_OP(dev_ops, alloc_mr);
  2314. SET_DEVICE_OP(dev_ops, alloc_mr_integrity);
  2315. SET_DEVICE_OP(dev_ops, alloc_mw);
  2316. SET_DEVICE_OP(dev_ops, alloc_pd);
  2317. SET_DEVICE_OP(dev_ops, alloc_rdma_netdev);
  2318. SET_DEVICE_OP(dev_ops, alloc_ucontext);
  2319. SET_DEVICE_OP(dev_ops, alloc_xrcd);
  2320. SET_DEVICE_OP(dev_ops, attach_mcast);
  2321. SET_DEVICE_OP(dev_ops, check_mr_status);
  2322. SET_DEVICE_OP(dev_ops, counter_alloc_stats);
  2323. SET_DEVICE_OP(dev_ops, counter_bind_qp);
  2324. SET_DEVICE_OP(dev_ops, counter_dealloc);
  2325. SET_DEVICE_OP(dev_ops, counter_unbind_qp);
  2326. SET_DEVICE_OP(dev_ops, counter_update_stats);
  2327. SET_DEVICE_OP(dev_ops, create_ah);
  2328. SET_DEVICE_OP(dev_ops, create_counters);
  2329. SET_DEVICE_OP(dev_ops, create_cq);
  2330. SET_DEVICE_OP(dev_ops, create_flow);
  2331. SET_DEVICE_OP(dev_ops, create_qp);
  2332. SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
  2333. SET_DEVICE_OP(dev_ops, create_srq);
  2334. SET_DEVICE_OP(dev_ops, create_user_ah);
  2335. SET_DEVICE_OP(dev_ops, create_wq);
  2336. SET_DEVICE_OP(dev_ops, dealloc_dm);
  2337. SET_DEVICE_OP(dev_ops, dealloc_driver);
  2338. SET_DEVICE_OP(dev_ops, dealloc_mw);
  2339. SET_DEVICE_OP(dev_ops, dealloc_pd);
  2340. SET_DEVICE_OP(dev_ops, dealloc_ucontext);
  2341. SET_DEVICE_OP(dev_ops, dealloc_xrcd);
  2342. SET_DEVICE_OP(dev_ops, del_gid);
  2343. SET_DEVICE_OP(dev_ops, del_sub_dev);
  2344. SET_DEVICE_OP(dev_ops, dereg_mr);
  2345. SET_DEVICE_OP(dev_ops, destroy_ah);
  2346. SET_DEVICE_OP(dev_ops, destroy_counters);
  2347. SET_DEVICE_OP(dev_ops, destroy_cq);
  2348. SET_DEVICE_OP(dev_ops, destroy_flow);
  2349. SET_DEVICE_OP(dev_ops, destroy_flow_action);
  2350. SET_DEVICE_OP(dev_ops, destroy_qp);
  2351. SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table);
  2352. SET_DEVICE_OP(dev_ops, destroy_srq);
  2353. SET_DEVICE_OP(dev_ops, destroy_wq);
  2354. SET_DEVICE_OP(dev_ops, device_group);
  2355. SET_DEVICE_OP(dev_ops, detach_mcast);
  2356. SET_DEVICE_OP(dev_ops, disassociate_ucontext);
  2357. SET_DEVICE_OP(dev_ops, drain_rq);
  2358. SET_DEVICE_OP(dev_ops, drain_sq);
  2359. SET_DEVICE_OP(dev_ops, enable_driver);
  2360. SET_DEVICE_OP(dev_ops, fill_res_cm_id_entry);
  2361. SET_DEVICE_OP(dev_ops, fill_res_cq_entry);
  2362. SET_DEVICE_OP(dev_ops, fill_res_cq_entry_raw);
  2363. SET_DEVICE_OP(dev_ops, fill_res_mr_entry);
  2364. SET_DEVICE_OP(dev_ops, fill_res_mr_entry_raw);
  2365. SET_DEVICE_OP(dev_ops, fill_res_qp_entry);
  2366. SET_DEVICE_OP(dev_ops, fill_res_qp_entry_raw);
  2367. SET_DEVICE_OP(dev_ops, fill_res_srq_entry);
  2368. SET_DEVICE_OP(dev_ops, fill_res_srq_entry_raw);
  2369. SET_DEVICE_OP(dev_ops, fill_stat_mr_entry);
  2370. SET_DEVICE_OP(dev_ops, get_dev_fw_str);
  2371. SET_DEVICE_OP(dev_ops, get_dma_mr);
  2372. SET_DEVICE_OP(dev_ops, get_hw_stats);
  2373. SET_DEVICE_OP(dev_ops, get_link_layer);
  2374. SET_DEVICE_OP(dev_ops, get_netdev);
  2375. SET_DEVICE_OP(dev_ops, get_numa_node);
  2376. SET_DEVICE_OP(dev_ops, get_port_immutable);
  2377. SET_DEVICE_OP(dev_ops, get_vector_affinity);
  2378. SET_DEVICE_OP(dev_ops, get_vf_config);
  2379. SET_DEVICE_OP(dev_ops, get_vf_guid);
  2380. SET_DEVICE_OP(dev_ops, get_vf_stats);
  2381. SET_DEVICE_OP(dev_ops, iw_accept);
  2382. SET_DEVICE_OP(dev_ops, iw_add_ref);
  2383. SET_DEVICE_OP(dev_ops, iw_connect);
  2384. SET_DEVICE_OP(dev_ops, iw_create_listen);
  2385. SET_DEVICE_OP(dev_ops, iw_destroy_listen);
  2386. SET_DEVICE_OP(dev_ops, iw_get_qp);
  2387. SET_DEVICE_OP(dev_ops, iw_reject);
  2388. SET_DEVICE_OP(dev_ops, iw_rem_ref);
  2389. SET_DEVICE_OP(dev_ops, map_mr_sg);
  2390. SET_DEVICE_OP(dev_ops, map_mr_sg_pi);
  2391. SET_DEVICE_OP(dev_ops, mmap);
  2392. SET_DEVICE_OP(dev_ops, mmap_free);
  2393. SET_DEVICE_OP(dev_ops, modify_ah);
  2394. SET_DEVICE_OP(dev_ops, modify_cq);
  2395. SET_DEVICE_OP(dev_ops, modify_device);
  2396. SET_DEVICE_OP(dev_ops, modify_hw_stat);
  2397. SET_DEVICE_OP(dev_ops, modify_port);
  2398. SET_DEVICE_OP(dev_ops, modify_qp);
  2399. SET_DEVICE_OP(dev_ops, modify_srq);
  2400. SET_DEVICE_OP(dev_ops, modify_wq);
  2401. SET_DEVICE_OP(dev_ops, peek_cq);
  2402. SET_DEVICE_OP(dev_ops, poll_cq);
  2403. SET_DEVICE_OP(dev_ops, port_groups);
  2404. SET_DEVICE_OP(dev_ops, post_recv);
  2405. SET_DEVICE_OP(dev_ops, post_send);
  2406. SET_DEVICE_OP(dev_ops, post_srq_recv);
  2407. SET_DEVICE_OP(dev_ops, process_mad);
  2408. SET_DEVICE_OP(dev_ops, query_ah);
  2409. SET_DEVICE_OP(dev_ops, query_device);
  2410. SET_DEVICE_OP(dev_ops, query_gid);
  2411. SET_DEVICE_OP(dev_ops, query_pkey);
  2412. SET_DEVICE_OP(dev_ops, query_port);
  2413. SET_DEVICE_OP(dev_ops, query_qp);
  2414. SET_DEVICE_OP(dev_ops, query_srq);
  2415. SET_DEVICE_OP(dev_ops, query_ucontext);
  2416. SET_DEVICE_OP(dev_ops, rdma_netdev_get_params);
  2417. SET_DEVICE_OP(dev_ops, read_counters);
  2418. SET_DEVICE_OP(dev_ops, reg_dm_mr);
  2419. SET_DEVICE_OP(dev_ops, reg_user_mr);
  2420. SET_DEVICE_OP(dev_ops, reg_user_mr_dmabuf);
  2421. SET_DEVICE_OP(dev_ops, req_notify_cq);
  2422. SET_DEVICE_OP(dev_ops, rereg_user_mr);
  2423. SET_DEVICE_OP(dev_ops, resize_cq);
  2424. SET_DEVICE_OP(dev_ops, set_vf_guid);
  2425. SET_DEVICE_OP(dev_ops, set_vf_link_state);
  2426. SET_OBJ_SIZE(dev_ops, ib_ah);
  2427. SET_OBJ_SIZE(dev_ops, ib_counters);
  2428. SET_OBJ_SIZE(dev_ops, ib_cq);
  2429. SET_OBJ_SIZE(dev_ops, ib_mw);
  2430. SET_OBJ_SIZE(dev_ops, ib_pd);
  2431. SET_OBJ_SIZE(dev_ops, ib_qp);
  2432. SET_OBJ_SIZE(dev_ops, ib_rwq_ind_table);
  2433. SET_OBJ_SIZE(dev_ops, ib_srq);
  2434. SET_OBJ_SIZE(dev_ops, ib_ucontext);
  2435. SET_OBJ_SIZE(dev_ops, ib_xrcd);
  2436. }
  2437. EXPORT_SYMBOL(ib_set_device_ops);
  2438. int ib_add_sub_device(struct ib_device *parent,
  2439. enum rdma_nl_dev_type type,
  2440. const char *name)
  2441. {
  2442. struct ib_device *sub;
  2443. int ret = 0;
  2444. if (!parent->ops.add_sub_dev || !parent->ops.del_sub_dev)
  2445. return -EOPNOTSUPP;
  2446. if (!ib_device_try_get(parent))
  2447. return -EINVAL;
  2448. sub = parent->ops.add_sub_dev(parent, type, name);
  2449. if (IS_ERR(sub)) {
  2450. ib_device_put(parent);
  2451. return PTR_ERR(sub);
  2452. }
  2453. sub->type = type;
  2454. sub->parent = parent;
  2455. mutex_lock(&parent->subdev_lock);
  2456. list_add_tail(&parent->subdev_list_head, &sub->subdev_list);
  2457. mutex_unlock(&parent->subdev_lock);
  2458. return ret;
  2459. }
  2460. EXPORT_SYMBOL(ib_add_sub_device);
  2461. int ib_del_sub_device_and_put(struct ib_device *sub)
  2462. {
  2463. struct ib_device *parent = sub->parent;
  2464. if (!parent)
  2465. return -EOPNOTSUPP;
  2466. mutex_lock(&parent->subdev_lock);
  2467. list_del(&sub->subdev_list);
  2468. mutex_unlock(&parent->subdev_lock);
  2469. ib_device_put(sub);
  2470. parent->ops.del_sub_dev(sub);
  2471. ib_device_put(parent);
  2472. return 0;
  2473. }
  2474. EXPORT_SYMBOL(ib_del_sub_device_and_put);
  2475. #ifdef CONFIG_INFINIBAND_VIRT_DMA
  2476. int ib_dma_virt_map_sg(struct ib_device *dev, struct scatterlist *sg, int nents)
  2477. {
  2478. struct scatterlist *s;
  2479. int i;
  2480. for_each_sg(sg, s, nents, i) {
  2481. sg_dma_address(s) = (uintptr_t)sg_virt(s);
  2482. sg_dma_len(s) = s->length;
  2483. }
  2484. return nents;
  2485. }
  2486. EXPORT_SYMBOL(ib_dma_virt_map_sg);
  2487. #endif /* CONFIG_INFINIBAND_VIRT_DMA */
  2488. static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
  2489. [RDMA_NL_LS_OP_RESOLVE] = {
  2490. .doit = ib_nl_handle_resolve_resp,
  2491. .flags = RDMA_NL_ADMIN_PERM,
  2492. },
  2493. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  2494. .doit = ib_nl_handle_set_timeout,
  2495. .flags = RDMA_NL_ADMIN_PERM,
  2496. },
  2497. [RDMA_NL_LS_OP_IP_RESOLVE] = {
  2498. .doit = ib_nl_handle_ip_res_resp,
  2499. .flags = RDMA_NL_ADMIN_PERM,
  2500. },
  2501. };
  2502. static int __init ib_core_init(void)
  2503. {
  2504. int ret = -ENOMEM;
  2505. ib_wq = alloc_workqueue("infiniband", 0, 0);
  2506. if (!ib_wq)
  2507. return -ENOMEM;
  2508. ib_unreg_wq = alloc_workqueue("ib-unreg-wq", WQ_UNBOUND,
  2509. WQ_UNBOUND_MAX_ACTIVE);
  2510. if (!ib_unreg_wq)
  2511. goto err;
  2512. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  2513. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
  2514. if (!ib_comp_wq)
  2515. goto err_unbound;
  2516. ib_comp_unbound_wq =
  2517. alloc_workqueue("ib-comp-unb-wq",
  2518. WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM |
  2519. WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE);
  2520. if (!ib_comp_unbound_wq)
  2521. goto err_comp;
  2522. ret = class_register(&ib_class);
  2523. if (ret) {
  2524. pr_warn("Couldn't create InfiniBand device class\n");
  2525. goto err_comp_unbound;
  2526. }
  2527. rdma_nl_init();
  2528. ret = addr_init();
  2529. if (ret) {
  2530. pr_warn("Couldn't init IB address resolution\n");
  2531. goto err_ibnl;
  2532. }
  2533. ret = ib_mad_init();
  2534. if (ret) {
  2535. pr_warn("Couldn't init IB MAD\n");
  2536. goto err_addr;
  2537. }
  2538. ret = ib_sa_init();
  2539. if (ret) {
  2540. pr_warn("Couldn't init SA\n");
  2541. goto err_mad;
  2542. }
  2543. ret = register_blocking_lsm_notifier(&ibdev_lsm_nb);
  2544. if (ret) {
  2545. pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
  2546. goto err_sa;
  2547. }
  2548. ret = register_pernet_device(&rdma_dev_net_ops);
  2549. if (ret) {
  2550. pr_warn("Couldn't init compat dev. ret %d\n", ret);
  2551. goto err_compat;
  2552. }
  2553. nldev_init();
  2554. rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
  2555. ret = roce_gid_mgmt_init();
  2556. if (ret) {
  2557. pr_warn("Couldn't init RoCE GID management\n");
  2558. goto err_parent;
  2559. }
  2560. return 0;
  2561. err_parent:
  2562. rdma_nl_unregister(RDMA_NL_LS);
  2563. nldev_exit();
  2564. unregister_pernet_device(&rdma_dev_net_ops);
  2565. err_compat:
  2566. unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
  2567. err_sa:
  2568. ib_sa_cleanup();
  2569. err_mad:
  2570. ib_mad_cleanup();
  2571. err_addr:
  2572. addr_cleanup();
  2573. err_ibnl:
  2574. class_unregister(&ib_class);
  2575. err_comp_unbound:
  2576. destroy_workqueue(ib_comp_unbound_wq);
  2577. err_comp:
  2578. destroy_workqueue(ib_comp_wq);
  2579. err_unbound:
  2580. destroy_workqueue(ib_unreg_wq);
  2581. err:
  2582. destroy_workqueue(ib_wq);
  2583. return ret;
  2584. }
  2585. static void __exit ib_core_cleanup(void)
  2586. {
  2587. roce_gid_mgmt_cleanup();
  2588. rdma_nl_unregister(RDMA_NL_LS);
  2589. nldev_exit();
  2590. unregister_pernet_device(&rdma_dev_net_ops);
  2591. unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
  2592. ib_sa_cleanup();
  2593. ib_mad_cleanup();
  2594. addr_cleanup();
  2595. rdma_nl_exit();
  2596. class_unregister(&ib_class);
  2597. destroy_workqueue(ib_comp_unbound_wq);
  2598. destroy_workqueue(ib_comp_wq);
  2599. /* Make sure that any pending umem accounting work is done. */
  2600. destroy_workqueue(ib_wq);
  2601. destroy_workqueue(ib_unreg_wq);
  2602. WARN_ON(!xa_empty(&clients));
  2603. WARN_ON(!xa_empty(&devices));
  2604. }
  2605. MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
  2606. /* ib core relies on netdev stack to first register net_ns_type_operations
  2607. * ns kobject type before ib_core initialization.
  2608. */
  2609. fs_initcall(ib_core_init);
  2610. module_exit(ib_core_cleanup);