device.c 74 KB

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