device.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. ib_cache_release_one(dev);
  153. kfree(dev->port_immutable);
  154. kfree(dev);
  155. }
  156. static int ib_device_uevent(struct device *device,
  157. struct kobj_uevent_env *env)
  158. {
  159. struct ib_device *dev = container_of(device, struct ib_device, dev);
  160. if (add_uevent_var(env, "NAME=%s", dev->name))
  161. return -ENOMEM;
  162. /*
  163. * It would be nice to pass the node GUID with the event...
  164. */
  165. return 0;
  166. }
  167. static struct class ib_class = {
  168. .name = "infiniband",
  169. .dev_release = ib_device_release,
  170. .dev_uevent = ib_device_uevent,
  171. };
  172. /**
  173. * ib_alloc_device - allocate an IB device struct
  174. * @size:size of structure to allocate
  175. *
  176. * Low-level drivers should use ib_alloc_device() to allocate &struct
  177. * ib_device. @size is the size of the structure to be allocated,
  178. * including any private data used by the low-level driver.
  179. * ib_dealloc_device() must be used to free structures allocated with
  180. * ib_alloc_device().
  181. */
  182. struct ib_device *ib_alloc_device(size_t size)
  183. {
  184. struct ib_device *device;
  185. if (WARN_ON(size < sizeof(struct ib_device)))
  186. return NULL;
  187. device = kzalloc(size, GFP_KERNEL);
  188. if (!device)
  189. return NULL;
  190. device->dev.class = &ib_class;
  191. device_initialize(&device->dev);
  192. dev_set_drvdata(&device->dev, device);
  193. INIT_LIST_HEAD(&device->event_handler_list);
  194. spin_lock_init(&device->event_handler_lock);
  195. spin_lock_init(&device->client_data_lock);
  196. INIT_LIST_HEAD(&device->client_data_list);
  197. INIT_LIST_HEAD(&device->port_list);
  198. return device;
  199. }
  200. EXPORT_SYMBOL(ib_alloc_device);
  201. /**
  202. * ib_dealloc_device - free an IB device struct
  203. * @device:structure to free
  204. *
  205. * Free a structure allocated with ib_alloc_device().
  206. */
  207. void ib_dealloc_device(struct ib_device *device)
  208. {
  209. WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
  210. device->reg_state != IB_DEV_UNINITIALIZED);
  211. kobject_put(&device->dev.kobj);
  212. }
  213. EXPORT_SYMBOL(ib_dealloc_device);
  214. static int add_client_context(struct ib_device *device, struct ib_client *client)
  215. {
  216. struct ib_client_data *context;
  217. unsigned long flags;
  218. context = kmalloc(sizeof *context, GFP_KERNEL);
  219. if (!context) {
  220. pr_warn("Couldn't allocate client context for %s/%s\n",
  221. device->name, client->name);
  222. return -ENOMEM;
  223. }
  224. context->client = client;
  225. context->data = NULL;
  226. context->going_down = false;
  227. down_write(&lists_rwsem);
  228. spin_lock_irqsave(&device->client_data_lock, flags);
  229. list_add(&context->list, &device->client_data_list);
  230. spin_unlock_irqrestore(&device->client_data_lock, flags);
  231. up_write(&lists_rwsem);
  232. return 0;
  233. }
  234. static int verify_immutable(const struct ib_device *dev, u8 port)
  235. {
  236. return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
  237. rdma_max_mad_size(dev, port) != 0);
  238. }
  239. static int read_port_immutable(struct ib_device *device)
  240. {
  241. int ret;
  242. u8 start_port = rdma_start_port(device);
  243. u8 end_port = rdma_end_port(device);
  244. u8 port;
  245. /**
  246. * device->port_immutable is indexed directly by the port number to make
  247. * access to this data as efficient as possible.
  248. *
  249. * Therefore port_immutable is declared as a 1 based array with
  250. * potential empty slots at the beginning.
  251. */
  252. device->port_immutable = kzalloc(sizeof(*device->port_immutable)
  253. * (end_port + 1),
  254. GFP_KERNEL);
  255. if (!device->port_immutable)
  256. return -ENOMEM;
  257. for (port = start_port; port <= end_port; ++port) {
  258. ret = device->get_port_immutable(device, port,
  259. &device->port_immutable[port]);
  260. if (ret)
  261. return ret;
  262. if (verify_immutable(device, port))
  263. return -EINVAL;
  264. }
  265. return 0;
  266. }
  267. /**
  268. * ib_register_device - Register an IB device with IB core
  269. * @device:Device to register
  270. *
  271. * Low-level drivers use ib_register_device() to register their
  272. * devices with the IB core. All registered clients will receive a
  273. * callback for each device that is added. @device must be allocated
  274. * with ib_alloc_device().
  275. */
  276. int ib_register_device(struct ib_device *device,
  277. int (*port_callback)(struct ib_device *,
  278. u8, struct kobject *))
  279. {
  280. int ret;
  281. struct ib_client *client;
  282. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  283. mutex_lock(&device_mutex);
  284. if (strchr(device->name, '%')) {
  285. ret = alloc_name(device->name);
  286. if (ret)
  287. goto out;
  288. }
  289. if (ib_device_check_mandatory(device)) {
  290. ret = -EINVAL;
  291. goto out;
  292. }
  293. ret = read_port_immutable(device);
  294. if (ret) {
  295. pr_warn("Couldn't create per port immutable data %s\n",
  296. device->name);
  297. goto out;
  298. }
  299. ret = ib_cache_setup_one(device);
  300. if (ret) {
  301. pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
  302. goto out;
  303. }
  304. memset(&device->attrs, 0, sizeof(device->attrs));
  305. ret = device->query_device(device, &device->attrs, &uhw);
  306. if (ret) {
  307. pr_warn("Couldn't query the device attributes\n");
  308. ib_cache_cleanup_one(device);
  309. goto out;
  310. }
  311. ret = ib_device_register_sysfs(device, port_callback);
  312. if (ret) {
  313. pr_warn("Couldn't register device %s with driver model\n",
  314. device->name);
  315. ib_cache_cleanup_one(device);
  316. goto out;
  317. }
  318. device->reg_state = IB_DEV_REGISTERED;
  319. list_for_each_entry(client, &client_list, list)
  320. if (client->add && !add_client_context(device, client))
  321. client->add(device);
  322. down_write(&lists_rwsem);
  323. list_add_tail(&device->core_list, &device_list);
  324. up_write(&lists_rwsem);
  325. out:
  326. mutex_unlock(&device_mutex);
  327. return ret;
  328. }
  329. EXPORT_SYMBOL(ib_register_device);
  330. /**
  331. * ib_unregister_device - Unregister an IB device
  332. * @device:Device to unregister
  333. *
  334. * Unregister an IB device. All clients will receive a remove callback.
  335. */
  336. void ib_unregister_device(struct ib_device *device)
  337. {
  338. struct ib_client_data *context, *tmp;
  339. unsigned long flags;
  340. mutex_lock(&device_mutex);
  341. down_write(&lists_rwsem);
  342. list_del(&device->core_list);
  343. spin_lock_irqsave(&device->client_data_lock, flags);
  344. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  345. context->going_down = true;
  346. spin_unlock_irqrestore(&device->client_data_lock, flags);
  347. downgrade_write(&lists_rwsem);
  348. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  349. list) {
  350. if (context->client->remove)
  351. context->client->remove(device, context->data);
  352. }
  353. up_read(&lists_rwsem);
  354. mutex_unlock(&device_mutex);
  355. ib_device_unregister_sysfs(device);
  356. ib_cache_cleanup_one(device);
  357. down_write(&lists_rwsem);
  358. spin_lock_irqsave(&device->client_data_lock, flags);
  359. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  360. kfree(context);
  361. spin_unlock_irqrestore(&device->client_data_lock, flags);
  362. up_write(&lists_rwsem);
  363. device->reg_state = IB_DEV_UNREGISTERED;
  364. }
  365. EXPORT_SYMBOL(ib_unregister_device);
  366. /**
  367. * ib_register_client - Register an IB client
  368. * @client:Client to register
  369. *
  370. * Upper level users of the IB drivers can use ib_register_client() to
  371. * register callbacks for IB device addition and removal. When an IB
  372. * device is added, each registered client's add method will be called
  373. * (in the order the clients were registered), and when a device is
  374. * removed, each client's remove method will be called (in the reverse
  375. * order that clients were registered). In addition, when
  376. * ib_register_client() is called, the client will receive an add
  377. * callback for all devices already registered.
  378. */
  379. int ib_register_client(struct ib_client *client)
  380. {
  381. struct ib_device *device;
  382. mutex_lock(&device_mutex);
  383. list_for_each_entry(device, &device_list, core_list)
  384. if (client->add && !add_client_context(device, client))
  385. client->add(device);
  386. down_write(&lists_rwsem);
  387. list_add_tail(&client->list, &client_list);
  388. up_write(&lists_rwsem);
  389. mutex_unlock(&device_mutex);
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(ib_register_client);
  393. /**
  394. * ib_unregister_client - Unregister an IB client
  395. * @client:Client to unregister
  396. *
  397. * Upper level users use ib_unregister_client() to remove their client
  398. * registration. When ib_unregister_client() is called, the client
  399. * will receive a remove callback for each IB device still registered.
  400. */
  401. void ib_unregister_client(struct ib_client *client)
  402. {
  403. struct ib_client_data *context, *tmp;
  404. struct ib_device *device;
  405. unsigned long flags;
  406. mutex_lock(&device_mutex);
  407. down_write(&lists_rwsem);
  408. list_del(&client->list);
  409. up_write(&lists_rwsem);
  410. list_for_each_entry(device, &device_list, core_list) {
  411. struct ib_client_data *found_context = NULL;
  412. down_write(&lists_rwsem);
  413. spin_lock_irqsave(&device->client_data_lock, flags);
  414. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  415. if (context->client == client) {
  416. context->going_down = true;
  417. found_context = context;
  418. break;
  419. }
  420. spin_unlock_irqrestore(&device->client_data_lock, flags);
  421. up_write(&lists_rwsem);
  422. if (client->remove)
  423. client->remove(device, found_context ?
  424. found_context->data : NULL);
  425. if (!found_context) {
  426. pr_warn("No client context found for %s/%s\n",
  427. device->name, client->name);
  428. continue;
  429. }
  430. down_write(&lists_rwsem);
  431. spin_lock_irqsave(&device->client_data_lock, flags);
  432. list_del(&found_context->list);
  433. kfree(found_context);
  434. spin_unlock_irqrestore(&device->client_data_lock, flags);
  435. up_write(&lists_rwsem);
  436. }
  437. mutex_unlock(&device_mutex);
  438. }
  439. EXPORT_SYMBOL(ib_unregister_client);
  440. /**
  441. * ib_get_client_data - Get IB client context
  442. * @device:Device to get context for
  443. * @client:Client to get context for
  444. *
  445. * ib_get_client_data() returns client context set with
  446. * ib_set_client_data().
  447. */
  448. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  449. {
  450. struct ib_client_data *context;
  451. void *ret = NULL;
  452. unsigned long flags;
  453. spin_lock_irqsave(&device->client_data_lock, flags);
  454. list_for_each_entry(context, &device->client_data_list, list)
  455. if (context->client == client) {
  456. ret = context->data;
  457. break;
  458. }
  459. spin_unlock_irqrestore(&device->client_data_lock, flags);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL(ib_get_client_data);
  463. /**
  464. * ib_set_client_data - Set IB client context
  465. * @device:Device to set context for
  466. * @client:Client to set context for
  467. * @data:Context to set
  468. *
  469. * ib_set_client_data() sets client context that can be retrieved with
  470. * ib_get_client_data().
  471. */
  472. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  473. void *data)
  474. {
  475. struct ib_client_data *context;
  476. unsigned long flags;
  477. spin_lock_irqsave(&device->client_data_lock, flags);
  478. list_for_each_entry(context, &device->client_data_list, list)
  479. if (context->client == client) {
  480. context->data = data;
  481. goto out;
  482. }
  483. pr_warn("No client context found for %s/%s\n",
  484. device->name, client->name);
  485. out:
  486. spin_unlock_irqrestore(&device->client_data_lock, flags);
  487. }
  488. EXPORT_SYMBOL(ib_set_client_data);
  489. /**
  490. * ib_register_event_handler - Register an IB event handler
  491. * @event_handler:Handler to register
  492. *
  493. * ib_register_event_handler() registers an event handler that will be
  494. * called back when asynchronous IB events occur (as defined in
  495. * chapter 11 of the InfiniBand Architecture Specification). This
  496. * callback may occur in interrupt context.
  497. */
  498. int ib_register_event_handler (struct ib_event_handler *event_handler)
  499. {
  500. unsigned long flags;
  501. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  502. list_add_tail(&event_handler->list,
  503. &event_handler->device->event_handler_list);
  504. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  505. return 0;
  506. }
  507. EXPORT_SYMBOL(ib_register_event_handler);
  508. /**
  509. * ib_unregister_event_handler - Unregister an event handler
  510. * @event_handler:Handler to unregister
  511. *
  512. * Unregister an event handler registered with
  513. * ib_register_event_handler().
  514. */
  515. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  516. {
  517. unsigned long flags;
  518. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  519. list_del(&event_handler->list);
  520. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  521. return 0;
  522. }
  523. EXPORT_SYMBOL(ib_unregister_event_handler);
  524. /**
  525. * ib_dispatch_event - Dispatch an asynchronous event
  526. * @event:Event to dispatch
  527. *
  528. * Low-level drivers must call ib_dispatch_event() to dispatch the
  529. * event to all registered event handlers when an asynchronous event
  530. * occurs.
  531. */
  532. void ib_dispatch_event(struct ib_event *event)
  533. {
  534. unsigned long flags;
  535. struct ib_event_handler *handler;
  536. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  537. list_for_each_entry(handler, &event->device->event_handler_list, list)
  538. handler->handler(handler, event);
  539. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  540. }
  541. EXPORT_SYMBOL(ib_dispatch_event);
  542. /**
  543. * ib_query_port - Query IB port attributes
  544. * @device:Device to query
  545. * @port_num:Port number to query
  546. * @port_attr:Port attributes
  547. *
  548. * ib_query_port() returns the attributes of a port through the
  549. * @port_attr pointer.
  550. */
  551. int ib_query_port(struct ib_device *device,
  552. u8 port_num,
  553. struct ib_port_attr *port_attr)
  554. {
  555. union ib_gid gid;
  556. int err;
  557. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  558. return -EINVAL;
  559. memset(port_attr, 0, sizeof(*port_attr));
  560. err = device->query_port(device, port_num, port_attr);
  561. if (err || port_attr->subnet_prefix)
  562. return err;
  563. err = ib_query_gid(device, port_num, 0, &gid, NULL);
  564. if (err)
  565. return err;
  566. port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
  567. return 0;
  568. }
  569. EXPORT_SYMBOL(ib_query_port);
  570. /**
  571. * ib_query_gid - Get GID table entry
  572. * @device:Device to query
  573. * @port_num:Port number to query
  574. * @index:GID table index to query
  575. * @gid:Returned GID
  576. * @attr: Returned GID attributes related to this GID index (only in RoCE).
  577. * NULL means ignore.
  578. *
  579. * ib_query_gid() fetches the specified GID table entry.
  580. */
  581. int ib_query_gid(struct ib_device *device,
  582. u8 port_num, int index, union ib_gid *gid,
  583. struct ib_gid_attr *attr)
  584. {
  585. if (rdma_cap_roce_gid_table(device, port_num))
  586. return ib_get_cached_gid(device, port_num, index, gid, attr);
  587. if (attr)
  588. return -EINVAL;
  589. return device->query_gid(device, port_num, index, gid);
  590. }
  591. EXPORT_SYMBOL(ib_query_gid);
  592. /**
  593. * ib_enum_roce_netdev - enumerate all RoCE ports
  594. * @ib_dev : IB device we want to query
  595. * @filter: Should we call the callback?
  596. * @filter_cookie: Cookie passed to filter
  597. * @cb: Callback to call for each found RoCE ports
  598. * @cookie: Cookie passed back to the callback
  599. *
  600. * Enumerates all of the physical RoCE ports of ib_dev
  601. * which are related to netdevice and calls callback() on each
  602. * device for which filter() function returns non zero.
  603. */
  604. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  605. roce_netdev_filter filter,
  606. void *filter_cookie,
  607. roce_netdev_callback cb,
  608. void *cookie)
  609. {
  610. u8 port;
  611. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  612. port++)
  613. if (rdma_protocol_roce(ib_dev, port)) {
  614. struct net_device *idev = NULL;
  615. if (ib_dev->get_netdev)
  616. idev = ib_dev->get_netdev(ib_dev, port);
  617. if (idev &&
  618. idev->reg_state >= NETREG_UNREGISTERED) {
  619. dev_put(idev);
  620. idev = NULL;
  621. }
  622. if (filter(ib_dev, port, idev, filter_cookie))
  623. cb(ib_dev, port, idev, cookie);
  624. if (idev)
  625. dev_put(idev);
  626. }
  627. }
  628. /**
  629. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  630. * @filter: Should we call the callback?
  631. * @filter_cookie: Cookie passed to filter
  632. * @cb: Callback to call for each found RoCE ports
  633. * @cookie: Cookie passed back to the callback
  634. *
  635. * Enumerates all RoCE devices' physical ports which are related
  636. * to netdevices and calls callback() on each device for which
  637. * filter() function returns non zero.
  638. */
  639. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  640. void *filter_cookie,
  641. roce_netdev_callback cb,
  642. void *cookie)
  643. {
  644. struct ib_device *dev;
  645. down_read(&lists_rwsem);
  646. list_for_each_entry(dev, &device_list, core_list)
  647. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  648. up_read(&lists_rwsem);
  649. }
  650. /**
  651. * ib_query_pkey - Get P_Key table entry
  652. * @device:Device to query
  653. * @port_num:Port number to query
  654. * @index:P_Key table index to query
  655. * @pkey:Returned P_Key
  656. *
  657. * ib_query_pkey() fetches the specified P_Key table entry.
  658. */
  659. int ib_query_pkey(struct ib_device *device,
  660. u8 port_num, u16 index, u16 *pkey)
  661. {
  662. return device->query_pkey(device, port_num, index, pkey);
  663. }
  664. EXPORT_SYMBOL(ib_query_pkey);
  665. /**
  666. * ib_modify_device - Change IB device attributes
  667. * @device:Device to modify
  668. * @device_modify_mask:Mask of attributes to change
  669. * @device_modify:New attribute values
  670. *
  671. * ib_modify_device() changes a device's attributes as specified by
  672. * the @device_modify_mask and @device_modify structure.
  673. */
  674. int ib_modify_device(struct ib_device *device,
  675. int device_modify_mask,
  676. struct ib_device_modify *device_modify)
  677. {
  678. if (!device->modify_device)
  679. return -ENOSYS;
  680. return device->modify_device(device, device_modify_mask,
  681. device_modify);
  682. }
  683. EXPORT_SYMBOL(ib_modify_device);
  684. /**
  685. * ib_modify_port - Modifies the attributes for the specified port.
  686. * @device: The device to modify.
  687. * @port_num: The number of the port to modify.
  688. * @port_modify_mask: Mask used to specify which attributes of the port
  689. * to change.
  690. * @port_modify: New attribute values for the port.
  691. *
  692. * ib_modify_port() changes a port's attributes as specified by the
  693. * @port_modify_mask and @port_modify structure.
  694. */
  695. int ib_modify_port(struct ib_device *device,
  696. u8 port_num, int port_modify_mask,
  697. struct ib_port_modify *port_modify)
  698. {
  699. if (!device->modify_port)
  700. return -ENOSYS;
  701. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  702. return -EINVAL;
  703. return device->modify_port(device, port_num, port_modify_mask,
  704. port_modify);
  705. }
  706. EXPORT_SYMBOL(ib_modify_port);
  707. /**
  708. * ib_find_gid - Returns the port number and GID table index where
  709. * a specified GID value occurs.
  710. * @device: The device to query.
  711. * @gid: The GID value to search for.
  712. * @gid_type: Type of GID.
  713. * @ndev: The ndev related to the GID to search for.
  714. * @port_num: The port number of the device where the GID value was found.
  715. * @index: The index into the GID table where the GID was found. This
  716. * parameter may be NULL.
  717. */
  718. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  719. enum ib_gid_type gid_type, struct net_device *ndev,
  720. u8 *port_num, u16 *index)
  721. {
  722. union ib_gid tmp_gid;
  723. int ret, port, i;
  724. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  725. if (rdma_cap_roce_gid_table(device, port)) {
  726. if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
  727. ndev, index)) {
  728. *port_num = port;
  729. return 0;
  730. }
  731. }
  732. if (gid_type != IB_GID_TYPE_IB)
  733. continue;
  734. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  735. ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
  736. if (ret)
  737. return ret;
  738. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  739. *port_num = port;
  740. if (index)
  741. *index = i;
  742. return 0;
  743. }
  744. }
  745. }
  746. return -ENOENT;
  747. }
  748. EXPORT_SYMBOL(ib_find_gid);
  749. /**
  750. * ib_find_pkey - Returns the PKey table index where a specified
  751. * PKey value occurs.
  752. * @device: The device to query.
  753. * @port_num: The port number of the device to search for the PKey.
  754. * @pkey: The PKey value to search for.
  755. * @index: The index into the PKey table where the PKey was found.
  756. */
  757. int ib_find_pkey(struct ib_device *device,
  758. u8 port_num, u16 pkey, u16 *index)
  759. {
  760. int ret, i;
  761. u16 tmp_pkey;
  762. int partial_ix = -1;
  763. for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
  764. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  765. if (ret)
  766. return ret;
  767. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  768. /* if there is full-member pkey take it.*/
  769. if (tmp_pkey & 0x8000) {
  770. *index = i;
  771. return 0;
  772. }
  773. if (partial_ix < 0)
  774. partial_ix = i;
  775. }
  776. }
  777. /*no full-member, if exists take the limited*/
  778. if (partial_ix >= 0) {
  779. *index = partial_ix;
  780. return 0;
  781. }
  782. return -ENOENT;
  783. }
  784. EXPORT_SYMBOL(ib_find_pkey);
  785. /**
  786. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  787. * for a received CM request
  788. * @dev: An RDMA device on which the request has been received.
  789. * @port: Port number on the RDMA device.
  790. * @pkey: The Pkey the request came on.
  791. * @gid: A GID that the net_dev uses to communicate.
  792. * @addr: Contains the IP address that the request specified as its
  793. * destination.
  794. */
  795. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  796. u8 port,
  797. u16 pkey,
  798. const union ib_gid *gid,
  799. const struct sockaddr *addr)
  800. {
  801. struct net_device *net_dev = NULL;
  802. struct ib_client_data *context;
  803. if (!rdma_protocol_ib(dev, port))
  804. return NULL;
  805. down_read(&lists_rwsem);
  806. list_for_each_entry(context, &dev->client_data_list, list) {
  807. struct ib_client *client = context->client;
  808. if (context->going_down)
  809. continue;
  810. if (client->get_net_dev_by_params) {
  811. net_dev = client->get_net_dev_by_params(dev, port, pkey,
  812. gid, addr,
  813. context->data);
  814. if (net_dev)
  815. break;
  816. }
  817. }
  818. up_read(&lists_rwsem);
  819. return net_dev;
  820. }
  821. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  822. static int __init ib_core_init(void)
  823. {
  824. int ret;
  825. ib_wq = alloc_workqueue("infiniband", 0, 0);
  826. if (!ib_wq)
  827. return -ENOMEM;
  828. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  829. WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
  830. WQ_UNBOUND_MAX_ACTIVE);
  831. if (!ib_comp_wq) {
  832. ret = -ENOMEM;
  833. goto err;
  834. }
  835. ret = class_register(&ib_class);
  836. if (ret) {
  837. pr_warn("Couldn't create InfiniBand device class\n");
  838. goto err_comp;
  839. }
  840. ret = ibnl_init();
  841. if (ret) {
  842. pr_warn("Couldn't init IB netlink interface\n");
  843. goto err_sysfs;
  844. }
  845. ib_cache_setup();
  846. return 0;
  847. err_sysfs:
  848. class_unregister(&ib_class);
  849. err_comp:
  850. destroy_workqueue(ib_comp_wq);
  851. err:
  852. destroy_workqueue(ib_wq);
  853. return ret;
  854. }
  855. static void __exit ib_core_cleanup(void)
  856. {
  857. ib_cache_cleanup();
  858. ibnl_cleanup();
  859. class_unregister(&ib_class);
  860. destroy_workqueue(ib_comp_wq);
  861. /* Make sure that any pending umem accounting work is done. */
  862. destroy_workqueue(ib_wq);
  863. }
  864. module_init(ib_core_init);
  865. module_exit(ib_core_cleanup);