device.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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/mutex.h>
  40. #include <linux/netdevice.h>
  41. #include <rdma/rdma_netlink.h>
  42. #include <rdma/ib_addr.h>
  43. #include <rdma/ib_cache.h>
  44. #include "core_priv.h"
  45. MODULE_AUTHOR("Roland Dreier");
  46. MODULE_DESCRIPTION("core kernel InfiniBand API");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. struct ib_client_data {
  49. struct list_head list;
  50. struct ib_client *client;
  51. void * data;
  52. /* The device or client is going down. Do not call client or device
  53. * callbacks other than remove(). */
  54. bool going_down;
  55. };
  56. struct workqueue_struct *ib_comp_wq;
  57. struct workqueue_struct *ib_wq;
  58. EXPORT_SYMBOL_GPL(ib_wq);
  59. /* The device_list and client_list contain devices and clients after their
  60. * registration has completed, and the devices and clients are removed
  61. * during unregistration. */
  62. static LIST_HEAD(device_list);
  63. static LIST_HEAD(client_list);
  64. /*
  65. * device_mutex and lists_rwsem protect access to both device_list and
  66. * client_list. device_mutex protects writer access by device and client
  67. * registration / de-registration. lists_rwsem protects reader access to
  68. * these lists. Iterators of these lists must lock it for read, while updates
  69. * to the lists must be done with a write lock. A special case is when the
  70. * device_mutex is locked. In this case locking the lists for read access is
  71. * not necessary as the device_mutex implies it.
  72. *
  73. * lists_rwsem also protects access to the client data list.
  74. */
  75. static DEFINE_MUTEX(device_mutex);
  76. static DECLARE_RWSEM(lists_rwsem);
  77. static int ib_device_check_mandatory(struct ib_device *device)
  78. {
  79. #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
  80. static const struct {
  81. size_t offset;
  82. char *name;
  83. } mandatory_table[] = {
  84. IB_MANDATORY_FUNC(query_device),
  85. IB_MANDATORY_FUNC(query_port),
  86. IB_MANDATORY_FUNC(query_pkey),
  87. IB_MANDATORY_FUNC(query_gid),
  88. IB_MANDATORY_FUNC(alloc_pd),
  89. IB_MANDATORY_FUNC(dealloc_pd),
  90. IB_MANDATORY_FUNC(create_ah),
  91. IB_MANDATORY_FUNC(destroy_ah),
  92. IB_MANDATORY_FUNC(create_qp),
  93. IB_MANDATORY_FUNC(modify_qp),
  94. IB_MANDATORY_FUNC(destroy_qp),
  95. IB_MANDATORY_FUNC(post_send),
  96. IB_MANDATORY_FUNC(post_recv),
  97. IB_MANDATORY_FUNC(create_cq),
  98. IB_MANDATORY_FUNC(destroy_cq),
  99. IB_MANDATORY_FUNC(poll_cq),
  100. IB_MANDATORY_FUNC(req_notify_cq),
  101. IB_MANDATORY_FUNC(get_dma_mr),
  102. IB_MANDATORY_FUNC(dereg_mr),
  103. IB_MANDATORY_FUNC(get_port_immutable)
  104. };
  105. int i;
  106. for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
  107. if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
  108. pr_warn("Device %s is missing mandatory function %s\n",
  109. device->name, mandatory_table[i].name);
  110. return -EINVAL;
  111. }
  112. }
  113. return 0;
  114. }
  115. static struct ib_device *__ib_device_get_by_name(const char *name)
  116. {
  117. struct ib_device *device;
  118. list_for_each_entry(device, &device_list, core_list)
  119. if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
  120. return device;
  121. return NULL;
  122. }
  123. static int alloc_name(char *name)
  124. {
  125. unsigned long *inuse;
  126. char buf[IB_DEVICE_NAME_MAX];
  127. struct ib_device *device;
  128. int i;
  129. inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  130. if (!inuse)
  131. return -ENOMEM;
  132. list_for_each_entry(device, &device_list, core_list) {
  133. if (!sscanf(device->name, name, &i))
  134. continue;
  135. if (i < 0 || i >= PAGE_SIZE * 8)
  136. continue;
  137. snprintf(buf, sizeof buf, name, i);
  138. if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
  139. set_bit(i, inuse);
  140. }
  141. i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
  142. free_page((unsigned long) inuse);
  143. snprintf(buf, sizeof buf, name, i);
  144. if (__ib_device_get_by_name(buf))
  145. return -ENFILE;
  146. strlcpy(name, buf, IB_DEVICE_NAME_MAX);
  147. return 0;
  148. }
  149. static void ib_device_release(struct device *device)
  150. {
  151. struct ib_device *dev = container_of(device, struct ib_device, dev);
  152. WARN_ON(dev->reg_state == IB_DEV_REGISTERED);
  153. if (dev->reg_state == IB_DEV_UNREGISTERED) {
  154. /*
  155. * In IB_DEV_UNINITIALIZED state, cache or port table
  156. * is not even created. Free cache and port table only when
  157. * device reaches UNREGISTERED state.
  158. */
  159. ib_cache_release_one(dev);
  160. kfree(dev->port_immutable);
  161. }
  162. kfree(dev);
  163. }
  164. static int ib_device_uevent(struct device *device,
  165. struct kobj_uevent_env *env)
  166. {
  167. struct ib_device *dev = container_of(device, struct ib_device, dev);
  168. if (add_uevent_var(env, "NAME=%s", dev->name))
  169. return -ENOMEM;
  170. /*
  171. * It would be nice to pass the node GUID with the event...
  172. */
  173. return 0;
  174. }
  175. static struct class ib_class = {
  176. .name = "infiniband",
  177. .dev_release = ib_device_release,
  178. .dev_uevent = ib_device_uevent,
  179. };
  180. /**
  181. * ib_alloc_device - allocate an IB device struct
  182. * @size:size of structure to allocate
  183. *
  184. * Low-level drivers should use ib_alloc_device() to allocate &struct
  185. * ib_device. @size is the size of the structure to be allocated,
  186. * including any private data used by the low-level driver.
  187. * ib_dealloc_device() must be used to free structures allocated with
  188. * ib_alloc_device().
  189. */
  190. struct ib_device *ib_alloc_device(size_t size)
  191. {
  192. struct ib_device *device;
  193. if (WARN_ON(size < sizeof(struct ib_device)))
  194. return NULL;
  195. device = kzalloc(size, GFP_KERNEL);
  196. if (!device)
  197. return NULL;
  198. device->dev.class = &ib_class;
  199. device_initialize(&device->dev);
  200. dev_set_drvdata(&device->dev, device);
  201. INIT_LIST_HEAD(&device->event_handler_list);
  202. spin_lock_init(&device->event_handler_lock);
  203. spin_lock_init(&device->client_data_lock);
  204. INIT_LIST_HEAD(&device->client_data_list);
  205. INIT_LIST_HEAD(&device->port_list);
  206. return device;
  207. }
  208. EXPORT_SYMBOL(ib_alloc_device);
  209. /**
  210. * ib_dealloc_device - free an IB device struct
  211. * @device:structure to free
  212. *
  213. * Free a structure allocated with ib_alloc_device().
  214. */
  215. void ib_dealloc_device(struct ib_device *device)
  216. {
  217. WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
  218. device->reg_state != IB_DEV_UNINITIALIZED);
  219. kobject_put(&device->dev.kobj);
  220. }
  221. EXPORT_SYMBOL(ib_dealloc_device);
  222. static int add_client_context(struct ib_device *device, struct ib_client *client)
  223. {
  224. struct ib_client_data *context;
  225. unsigned long flags;
  226. context = kmalloc(sizeof *context, GFP_KERNEL);
  227. if (!context)
  228. return -ENOMEM;
  229. context->client = client;
  230. context->data = NULL;
  231. context->going_down = false;
  232. down_write(&lists_rwsem);
  233. spin_lock_irqsave(&device->client_data_lock, flags);
  234. list_add(&context->list, &device->client_data_list);
  235. spin_unlock_irqrestore(&device->client_data_lock, flags);
  236. up_write(&lists_rwsem);
  237. return 0;
  238. }
  239. static int verify_immutable(const struct ib_device *dev, u8 port)
  240. {
  241. return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
  242. rdma_max_mad_size(dev, port) != 0);
  243. }
  244. static int read_port_immutable(struct ib_device *device)
  245. {
  246. int ret;
  247. u8 start_port = rdma_start_port(device);
  248. u8 end_port = rdma_end_port(device);
  249. u8 port;
  250. /**
  251. * device->port_immutable is indexed directly by the port number to make
  252. * access to this data as efficient as possible.
  253. *
  254. * Therefore port_immutable is declared as a 1 based array with
  255. * potential empty slots at the beginning.
  256. */
  257. device->port_immutable = kzalloc(sizeof(*device->port_immutable)
  258. * (end_port + 1),
  259. GFP_KERNEL);
  260. if (!device->port_immutable)
  261. return -ENOMEM;
  262. for (port = start_port; port <= end_port; ++port) {
  263. ret = device->get_port_immutable(device, port,
  264. &device->port_immutable[port]);
  265. if (ret)
  266. return ret;
  267. if (verify_immutable(device, port))
  268. return -EINVAL;
  269. }
  270. return 0;
  271. }
  272. void ib_get_device_fw_str(struct ib_device *dev, char *str, size_t str_len)
  273. {
  274. if (dev->get_dev_fw_str)
  275. dev->get_dev_fw_str(dev, str, str_len);
  276. else
  277. str[0] = '\0';
  278. }
  279. EXPORT_SYMBOL(ib_get_device_fw_str);
  280. /**
  281. * ib_register_device - Register an IB device with IB core
  282. * @device:Device to register
  283. *
  284. * Low-level drivers use ib_register_device() to register their
  285. * devices with the IB core. All registered clients will receive a
  286. * callback for each device that is added. @device must be allocated
  287. * with ib_alloc_device().
  288. */
  289. int ib_register_device(struct ib_device *device,
  290. int (*port_callback)(struct ib_device *,
  291. u8, struct kobject *))
  292. {
  293. int ret;
  294. struct ib_client *client;
  295. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  296. struct device *parent = device->dev.parent;
  297. WARN_ON_ONCE(!parent);
  298. WARN_ON_ONCE(device->dma_device);
  299. if (device->dev.dma_ops) {
  300. /*
  301. * The caller provided custom DMA operations. Copy the
  302. * DMA-related fields that are used by e.g. dma_alloc_coherent()
  303. * into device->dev.
  304. */
  305. device->dma_device = &device->dev;
  306. if (!device->dev.dma_mask)
  307. device->dev.dma_mask = parent->dma_mask;
  308. if (!device->dev.coherent_dma_mask)
  309. device->dev.coherent_dma_mask =
  310. parent->coherent_dma_mask;
  311. } else {
  312. /*
  313. * The caller did not provide custom DMA operations. Use the
  314. * DMA mapping operations of the parent device.
  315. */
  316. device->dma_device = parent;
  317. }
  318. mutex_lock(&device_mutex);
  319. if (strchr(device->name, '%')) {
  320. ret = alloc_name(device->name);
  321. if (ret)
  322. goto out;
  323. }
  324. if (ib_device_check_mandatory(device)) {
  325. ret = -EINVAL;
  326. goto out;
  327. }
  328. ret = read_port_immutable(device);
  329. if (ret) {
  330. pr_warn("Couldn't create per port immutable data %s\n",
  331. device->name);
  332. goto out;
  333. }
  334. ret = ib_cache_setup_one(device);
  335. if (ret) {
  336. pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
  337. goto port_cleanup;
  338. }
  339. ret = ib_device_register_rdmacg(device);
  340. if (ret) {
  341. pr_warn("Couldn't register device with rdma cgroup\n");
  342. goto cache_cleanup;
  343. }
  344. memset(&device->attrs, 0, sizeof(device->attrs));
  345. ret = device->query_device(device, &device->attrs, &uhw);
  346. if (ret) {
  347. pr_warn("Couldn't query the device attributes\n");
  348. goto cache_cleanup;
  349. }
  350. ret = ib_device_register_sysfs(device, port_callback);
  351. if (ret) {
  352. pr_warn("Couldn't register device %s with driver model\n",
  353. device->name);
  354. goto cache_cleanup;
  355. }
  356. device->reg_state = IB_DEV_REGISTERED;
  357. list_for_each_entry(client, &client_list, list)
  358. if (client->add && !add_client_context(device, client))
  359. client->add(device);
  360. down_write(&lists_rwsem);
  361. list_add_tail(&device->core_list, &device_list);
  362. up_write(&lists_rwsem);
  363. mutex_unlock(&device_mutex);
  364. return 0;
  365. cache_cleanup:
  366. ib_cache_cleanup_one(device);
  367. ib_cache_release_one(device);
  368. port_cleanup:
  369. kfree(device->port_immutable);
  370. out:
  371. mutex_unlock(&device_mutex);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL(ib_register_device);
  375. /**
  376. * ib_unregister_device - Unregister an IB device
  377. * @device:Device to unregister
  378. *
  379. * Unregister an IB device. All clients will receive a remove callback.
  380. */
  381. void ib_unregister_device(struct ib_device *device)
  382. {
  383. struct ib_client_data *context, *tmp;
  384. unsigned long flags;
  385. mutex_lock(&device_mutex);
  386. down_write(&lists_rwsem);
  387. list_del(&device->core_list);
  388. spin_lock_irqsave(&device->client_data_lock, flags);
  389. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  390. context->going_down = true;
  391. spin_unlock_irqrestore(&device->client_data_lock, flags);
  392. downgrade_write(&lists_rwsem);
  393. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  394. list) {
  395. if (context->client->remove)
  396. context->client->remove(device, context->data);
  397. }
  398. up_read(&lists_rwsem);
  399. mutex_unlock(&device_mutex);
  400. ib_device_unregister_rdmacg(device);
  401. ib_device_unregister_sysfs(device);
  402. ib_cache_cleanup_one(device);
  403. down_write(&lists_rwsem);
  404. spin_lock_irqsave(&device->client_data_lock, flags);
  405. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  406. kfree(context);
  407. spin_unlock_irqrestore(&device->client_data_lock, flags);
  408. up_write(&lists_rwsem);
  409. device->reg_state = IB_DEV_UNREGISTERED;
  410. }
  411. EXPORT_SYMBOL(ib_unregister_device);
  412. /**
  413. * ib_register_client - Register an IB client
  414. * @client:Client to register
  415. *
  416. * Upper level users of the IB drivers can use ib_register_client() to
  417. * register callbacks for IB device addition and removal. When an IB
  418. * device is added, each registered client's add method will be called
  419. * (in the order the clients were registered), and when a device is
  420. * removed, each client's remove method will be called (in the reverse
  421. * order that clients were registered). In addition, when
  422. * ib_register_client() is called, the client will receive an add
  423. * callback for all devices already registered.
  424. */
  425. int ib_register_client(struct ib_client *client)
  426. {
  427. struct ib_device *device;
  428. mutex_lock(&device_mutex);
  429. list_for_each_entry(device, &device_list, core_list)
  430. if (client->add && !add_client_context(device, client))
  431. client->add(device);
  432. down_write(&lists_rwsem);
  433. list_add_tail(&client->list, &client_list);
  434. up_write(&lists_rwsem);
  435. mutex_unlock(&device_mutex);
  436. return 0;
  437. }
  438. EXPORT_SYMBOL(ib_register_client);
  439. /**
  440. * ib_unregister_client - Unregister an IB client
  441. * @client:Client to unregister
  442. *
  443. * Upper level users use ib_unregister_client() to remove their client
  444. * registration. When ib_unregister_client() is called, the client
  445. * will receive a remove callback for each IB device still registered.
  446. */
  447. void ib_unregister_client(struct ib_client *client)
  448. {
  449. struct ib_client_data *context, *tmp;
  450. struct ib_device *device;
  451. unsigned long flags;
  452. mutex_lock(&device_mutex);
  453. down_write(&lists_rwsem);
  454. list_del(&client->list);
  455. up_write(&lists_rwsem);
  456. list_for_each_entry(device, &device_list, core_list) {
  457. struct ib_client_data *found_context = NULL;
  458. down_write(&lists_rwsem);
  459. spin_lock_irqsave(&device->client_data_lock, flags);
  460. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  461. if (context->client == client) {
  462. context->going_down = true;
  463. found_context = context;
  464. break;
  465. }
  466. spin_unlock_irqrestore(&device->client_data_lock, flags);
  467. up_write(&lists_rwsem);
  468. if (client->remove)
  469. client->remove(device, found_context ?
  470. found_context->data : NULL);
  471. if (!found_context) {
  472. pr_warn("No client context found for %s/%s\n",
  473. device->name, client->name);
  474. continue;
  475. }
  476. down_write(&lists_rwsem);
  477. spin_lock_irqsave(&device->client_data_lock, flags);
  478. list_del(&found_context->list);
  479. kfree(found_context);
  480. spin_unlock_irqrestore(&device->client_data_lock, flags);
  481. up_write(&lists_rwsem);
  482. }
  483. mutex_unlock(&device_mutex);
  484. }
  485. EXPORT_SYMBOL(ib_unregister_client);
  486. /**
  487. * ib_get_client_data - Get IB client context
  488. * @device:Device to get context for
  489. * @client:Client to get context for
  490. *
  491. * ib_get_client_data() returns client context set with
  492. * ib_set_client_data().
  493. */
  494. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  495. {
  496. struct ib_client_data *context;
  497. void *ret = NULL;
  498. unsigned long flags;
  499. spin_lock_irqsave(&device->client_data_lock, flags);
  500. list_for_each_entry(context, &device->client_data_list, list)
  501. if (context->client == client) {
  502. ret = context->data;
  503. break;
  504. }
  505. spin_unlock_irqrestore(&device->client_data_lock, flags);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL(ib_get_client_data);
  509. /**
  510. * ib_set_client_data - Set IB client context
  511. * @device:Device to set context for
  512. * @client:Client to set context for
  513. * @data:Context to set
  514. *
  515. * ib_set_client_data() sets client context that can be retrieved with
  516. * ib_get_client_data().
  517. */
  518. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  519. void *data)
  520. {
  521. struct ib_client_data *context;
  522. unsigned long flags;
  523. spin_lock_irqsave(&device->client_data_lock, flags);
  524. list_for_each_entry(context, &device->client_data_list, list)
  525. if (context->client == client) {
  526. context->data = data;
  527. goto out;
  528. }
  529. pr_warn("No client context found for %s/%s\n",
  530. device->name, client->name);
  531. out:
  532. spin_unlock_irqrestore(&device->client_data_lock, flags);
  533. }
  534. EXPORT_SYMBOL(ib_set_client_data);
  535. /**
  536. * ib_register_event_handler - Register an IB event handler
  537. * @event_handler:Handler to register
  538. *
  539. * ib_register_event_handler() registers an event handler that will be
  540. * called back when asynchronous IB events occur (as defined in
  541. * chapter 11 of the InfiniBand Architecture Specification). This
  542. * callback may occur in interrupt context.
  543. */
  544. int ib_register_event_handler (struct ib_event_handler *event_handler)
  545. {
  546. unsigned long flags;
  547. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  548. list_add_tail(&event_handler->list,
  549. &event_handler->device->event_handler_list);
  550. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  551. return 0;
  552. }
  553. EXPORT_SYMBOL(ib_register_event_handler);
  554. /**
  555. * ib_unregister_event_handler - Unregister an event handler
  556. * @event_handler:Handler to unregister
  557. *
  558. * Unregister an event handler registered with
  559. * ib_register_event_handler().
  560. */
  561. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  562. {
  563. unsigned long flags;
  564. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  565. list_del(&event_handler->list);
  566. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  567. return 0;
  568. }
  569. EXPORT_SYMBOL(ib_unregister_event_handler);
  570. /**
  571. * ib_dispatch_event - Dispatch an asynchronous event
  572. * @event:Event to dispatch
  573. *
  574. * Low-level drivers must call ib_dispatch_event() to dispatch the
  575. * event to all registered event handlers when an asynchronous event
  576. * occurs.
  577. */
  578. void ib_dispatch_event(struct ib_event *event)
  579. {
  580. unsigned long flags;
  581. struct ib_event_handler *handler;
  582. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  583. list_for_each_entry(handler, &event->device->event_handler_list, list)
  584. handler->handler(handler, event);
  585. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  586. }
  587. EXPORT_SYMBOL(ib_dispatch_event);
  588. /**
  589. * ib_query_port - Query IB port attributes
  590. * @device:Device to query
  591. * @port_num:Port number to query
  592. * @port_attr:Port attributes
  593. *
  594. * ib_query_port() returns the attributes of a port through the
  595. * @port_attr pointer.
  596. */
  597. int ib_query_port(struct ib_device *device,
  598. u8 port_num,
  599. struct ib_port_attr *port_attr)
  600. {
  601. union ib_gid gid;
  602. int err;
  603. if (!rdma_is_port_valid(device, port_num))
  604. return -EINVAL;
  605. memset(port_attr, 0, sizeof(*port_attr));
  606. err = device->query_port(device, port_num, port_attr);
  607. if (err || port_attr->subnet_prefix)
  608. return err;
  609. if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
  610. return 0;
  611. err = ib_query_gid(device, port_num, 0, &gid, NULL);
  612. if (err)
  613. return err;
  614. port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
  615. return 0;
  616. }
  617. EXPORT_SYMBOL(ib_query_port);
  618. /**
  619. * ib_query_gid - Get GID table entry
  620. * @device:Device to query
  621. * @port_num:Port number to query
  622. * @index:GID table index to query
  623. * @gid:Returned GID
  624. * @attr: Returned GID attributes related to this GID index (only in RoCE).
  625. * NULL means ignore.
  626. *
  627. * ib_query_gid() fetches the specified GID table entry.
  628. */
  629. int ib_query_gid(struct ib_device *device,
  630. u8 port_num, int index, union ib_gid *gid,
  631. struct ib_gid_attr *attr)
  632. {
  633. if (rdma_cap_roce_gid_table(device, port_num))
  634. return ib_get_cached_gid(device, port_num, index, gid, attr);
  635. if (attr)
  636. return -EINVAL;
  637. return device->query_gid(device, port_num, index, gid);
  638. }
  639. EXPORT_SYMBOL(ib_query_gid);
  640. /**
  641. * ib_enum_roce_netdev - enumerate all RoCE ports
  642. * @ib_dev : IB device we want to query
  643. * @filter: Should we call the callback?
  644. * @filter_cookie: Cookie passed to filter
  645. * @cb: Callback to call for each found RoCE ports
  646. * @cookie: Cookie passed back to the callback
  647. *
  648. * Enumerates all of the physical RoCE ports of ib_dev
  649. * which are related to netdevice and calls callback() on each
  650. * device for which filter() function returns non zero.
  651. */
  652. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  653. roce_netdev_filter filter,
  654. void *filter_cookie,
  655. roce_netdev_callback cb,
  656. void *cookie)
  657. {
  658. u8 port;
  659. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  660. port++)
  661. if (rdma_protocol_roce(ib_dev, port)) {
  662. struct net_device *idev = NULL;
  663. if (ib_dev->get_netdev)
  664. idev = ib_dev->get_netdev(ib_dev, port);
  665. if (idev &&
  666. idev->reg_state >= NETREG_UNREGISTERED) {
  667. dev_put(idev);
  668. idev = NULL;
  669. }
  670. if (filter(ib_dev, port, idev, filter_cookie))
  671. cb(ib_dev, port, idev, cookie);
  672. if (idev)
  673. dev_put(idev);
  674. }
  675. }
  676. /**
  677. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  678. * @filter: Should we call the callback?
  679. * @filter_cookie: Cookie passed to filter
  680. * @cb: Callback to call for each found RoCE ports
  681. * @cookie: Cookie passed back to the callback
  682. *
  683. * Enumerates all RoCE devices' physical ports which are related
  684. * to netdevices and calls callback() on each device for which
  685. * filter() function returns non zero.
  686. */
  687. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  688. void *filter_cookie,
  689. roce_netdev_callback cb,
  690. void *cookie)
  691. {
  692. struct ib_device *dev;
  693. down_read(&lists_rwsem);
  694. list_for_each_entry(dev, &device_list, core_list)
  695. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  696. up_read(&lists_rwsem);
  697. }
  698. /**
  699. * ib_query_pkey - Get P_Key table entry
  700. * @device:Device to query
  701. * @port_num:Port number to query
  702. * @index:P_Key table index to query
  703. * @pkey:Returned P_Key
  704. *
  705. * ib_query_pkey() fetches the specified P_Key table entry.
  706. */
  707. int ib_query_pkey(struct ib_device *device,
  708. u8 port_num, u16 index, u16 *pkey)
  709. {
  710. return device->query_pkey(device, port_num, index, pkey);
  711. }
  712. EXPORT_SYMBOL(ib_query_pkey);
  713. /**
  714. * ib_modify_device - Change IB device attributes
  715. * @device:Device to modify
  716. * @device_modify_mask:Mask of attributes to change
  717. * @device_modify:New attribute values
  718. *
  719. * ib_modify_device() changes a device's attributes as specified by
  720. * the @device_modify_mask and @device_modify structure.
  721. */
  722. int ib_modify_device(struct ib_device *device,
  723. int device_modify_mask,
  724. struct ib_device_modify *device_modify)
  725. {
  726. if (!device->modify_device)
  727. return -ENOSYS;
  728. return device->modify_device(device, device_modify_mask,
  729. device_modify);
  730. }
  731. EXPORT_SYMBOL(ib_modify_device);
  732. /**
  733. * ib_modify_port - Modifies the attributes for the specified port.
  734. * @device: The device to modify.
  735. * @port_num: The number of the port to modify.
  736. * @port_modify_mask: Mask used to specify which attributes of the port
  737. * to change.
  738. * @port_modify: New attribute values for the port.
  739. *
  740. * ib_modify_port() changes a port's attributes as specified by the
  741. * @port_modify_mask and @port_modify structure.
  742. */
  743. int ib_modify_port(struct ib_device *device,
  744. u8 port_num, int port_modify_mask,
  745. struct ib_port_modify *port_modify)
  746. {
  747. if (!device->modify_port)
  748. return -ENOSYS;
  749. if (!rdma_is_port_valid(device, port_num))
  750. return -EINVAL;
  751. return device->modify_port(device, port_num, port_modify_mask,
  752. port_modify);
  753. }
  754. EXPORT_SYMBOL(ib_modify_port);
  755. /**
  756. * ib_find_gid - Returns the port number and GID table index where
  757. * a specified GID value occurs.
  758. * @device: The device to query.
  759. * @gid: The GID value to search for.
  760. * @gid_type: Type of GID.
  761. * @ndev: The ndev related to the GID to search for.
  762. * @port_num: The port number of the device where the GID value was found.
  763. * @index: The index into the GID table where the GID was found. This
  764. * parameter may be NULL.
  765. */
  766. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  767. enum ib_gid_type gid_type, struct net_device *ndev,
  768. u8 *port_num, u16 *index)
  769. {
  770. union ib_gid tmp_gid;
  771. int ret, port, i;
  772. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  773. if (rdma_cap_roce_gid_table(device, port)) {
  774. if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
  775. ndev, index)) {
  776. *port_num = port;
  777. return 0;
  778. }
  779. }
  780. if (gid_type != IB_GID_TYPE_IB)
  781. continue;
  782. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  783. ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
  784. if (ret)
  785. return ret;
  786. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  787. *port_num = port;
  788. if (index)
  789. *index = i;
  790. return 0;
  791. }
  792. }
  793. }
  794. return -ENOENT;
  795. }
  796. EXPORT_SYMBOL(ib_find_gid);
  797. /**
  798. * ib_find_pkey - Returns the PKey table index where a specified
  799. * PKey value occurs.
  800. * @device: The device to query.
  801. * @port_num: The port number of the device to search for the PKey.
  802. * @pkey: The PKey value to search for.
  803. * @index: The index into the PKey table where the PKey was found.
  804. */
  805. int ib_find_pkey(struct ib_device *device,
  806. u8 port_num, u16 pkey, u16 *index)
  807. {
  808. int ret, i;
  809. u16 tmp_pkey;
  810. int partial_ix = -1;
  811. for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
  812. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  813. if (ret)
  814. return ret;
  815. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  816. /* if there is full-member pkey take it.*/
  817. if (tmp_pkey & 0x8000) {
  818. *index = i;
  819. return 0;
  820. }
  821. if (partial_ix < 0)
  822. partial_ix = i;
  823. }
  824. }
  825. /*no full-member, if exists take the limited*/
  826. if (partial_ix >= 0) {
  827. *index = partial_ix;
  828. return 0;
  829. }
  830. return -ENOENT;
  831. }
  832. EXPORT_SYMBOL(ib_find_pkey);
  833. /**
  834. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  835. * for a received CM request
  836. * @dev: An RDMA device on which the request has been received.
  837. * @port: Port number on the RDMA device.
  838. * @pkey: The Pkey the request came on.
  839. * @gid: A GID that the net_dev uses to communicate.
  840. * @addr: Contains the IP address that the request specified as its
  841. * destination.
  842. */
  843. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  844. u8 port,
  845. u16 pkey,
  846. const union ib_gid *gid,
  847. const struct sockaddr *addr)
  848. {
  849. struct net_device *net_dev = NULL;
  850. struct ib_client_data *context;
  851. if (!rdma_protocol_ib(dev, port))
  852. return NULL;
  853. down_read(&lists_rwsem);
  854. list_for_each_entry(context, &dev->client_data_list, list) {
  855. struct ib_client *client = context->client;
  856. if (context->going_down)
  857. continue;
  858. if (client->get_net_dev_by_params) {
  859. net_dev = client->get_net_dev_by_params(dev, port, pkey,
  860. gid, addr,
  861. context->data);
  862. if (net_dev)
  863. break;
  864. }
  865. }
  866. up_read(&lists_rwsem);
  867. return net_dev;
  868. }
  869. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  870. static struct ibnl_client_cbs ibnl_ls_cb_table[] = {
  871. [RDMA_NL_LS_OP_RESOLVE] = {
  872. .dump = ib_nl_handle_resolve_resp,
  873. .module = THIS_MODULE },
  874. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  875. .dump = ib_nl_handle_set_timeout,
  876. .module = THIS_MODULE },
  877. [RDMA_NL_LS_OP_IP_RESOLVE] = {
  878. .dump = ib_nl_handle_ip_res_resp,
  879. .module = THIS_MODULE },
  880. };
  881. static int ib_add_ibnl_clients(void)
  882. {
  883. return ibnl_add_client(RDMA_NL_LS, ARRAY_SIZE(ibnl_ls_cb_table),
  884. ibnl_ls_cb_table);
  885. }
  886. static void ib_remove_ibnl_clients(void)
  887. {
  888. ibnl_remove_client(RDMA_NL_LS);
  889. }
  890. static int __init ib_core_init(void)
  891. {
  892. int ret;
  893. ib_wq = alloc_workqueue("infiniband", 0, 0);
  894. if (!ib_wq)
  895. return -ENOMEM;
  896. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  897. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
  898. if (!ib_comp_wq) {
  899. ret = -ENOMEM;
  900. goto err;
  901. }
  902. ret = class_register(&ib_class);
  903. if (ret) {
  904. pr_warn("Couldn't create InfiniBand device class\n");
  905. goto err_comp;
  906. }
  907. ret = ibnl_init();
  908. if (ret) {
  909. pr_warn("Couldn't init IB netlink interface\n");
  910. goto err_sysfs;
  911. }
  912. ret = addr_init();
  913. if (ret) {
  914. pr_warn("Could't init IB address resolution\n");
  915. goto err_ibnl;
  916. }
  917. ret = ib_mad_init();
  918. if (ret) {
  919. pr_warn("Couldn't init IB MAD\n");
  920. goto err_addr;
  921. }
  922. ret = ib_sa_init();
  923. if (ret) {
  924. pr_warn("Couldn't init SA\n");
  925. goto err_mad;
  926. }
  927. ret = ib_add_ibnl_clients();
  928. if (ret) {
  929. pr_warn("Couldn't register ibnl clients\n");
  930. goto err_sa;
  931. }
  932. ib_cache_setup();
  933. return 0;
  934. err_sa:
  935. ib_sa_cleanup();
  936. err_mad:
  937. ib_mad_cleanup();
  938. err_addr:
  939. addr_cleanup();
  940. err_ibnl:
  941. ibnl_cleanup();
  942. err_sysfs:
  943. class_unregister(&ib_class);
  944. err_comp:
  945. destroy_workqueue(ib_comp_wq);
  946. err:
  947. destroy_workqueue(ib_wq);
  948. return ret;
  949. }
  950. static void __exit ib_core_cleanup(void)
  951. {
  952. ib_cache_cleanup();
  953. ib_remove_ibnl_clients();
  954. ib_sa_cleanup();
  955. ib_mad_cleanup();
  956. addr_cleanup();
  957. ibnl_cleanup();
  958. class_unregister(&ib_class);
  959. destroy_workqueue(ib_comp_wq);
  960. /* Make sure that any pending umem accounting work is done. */
  961. destroy_workqueue(ib_wq);
  962. }
  963. module_init(ib_core_init);
  964. module_exit(ib_core_cleanup);