cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  5. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/errno.h>
  37. #include <linux/slab.h>
  38. #include <linux/workqueue.h>
  39. #include <rdma/ib_cache.h>
  40. #include "core_priv.h"
  41. struct ib_pkey_cache {
  42. int table_len;
  43. u16 table[0];
  44. };
  45. struct ib_gid_cache {
  46. int table_len;
  47. union ib_gid table[0];
  48. };
  49. struct ib_update_work {
  50. struct work_struct work;
  51. struct ib_device *device;
  52. u8 port_num;
  53. };
  54. static inline int start_port(struct ib_device *device)
  55. {
  56. return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
  57. }
  58. static inline int end_port(struct ib_device *device)
  59. {
  60. return (device->node_type == RDMA_NODE_IB_SWITCH) ?
  61. 0 : device->phys_port_cnt;
  62. }
  63. int ib_get_cached_gid(struct ib_device *device,
  64. u8 port_num,
  65. int index,
  66. union ib_gid *gid)
  67. {
  68. struct ib_gid_cache *cache;
  69. unsigned long flags;
  70. int ret = 0;
  71. if (port_num < start_port(device) || port_num > end_port(device))
  72. return -EINVAL;
  73. read_lock_irqsave(&device->cache.lock, flags);
  74. cache = device->cache.gid_cache[port_num - start_port(device)];
  75. if (index < 0 || index >= cache->table_len)
  76. ret = -EINVAL;
  77. else
  78. *gid = cache->table[index];
  79. read_unlock_irqrestore(&device->cache.lock, flags);
  80. return ret;
  81. }
  82. EXPORT_SYMBOL(ib_get_cached_gid);
  83. int ib_find_cached_gid(struct ib_device *device,
  84. union ib_gid *gid,
  85. u8 *port_num,
  86. u16 *index)
  87. {
  88. struct ib_gid_cache *cache;
  89. unsigned long flags;
  90. int p, i;
  91. int ret = -ENOENT;
  92. *port_num = -1;
  93. if (index)
  94. *index = -1;
  95. read_lock_irqsave(&device->cache.lock, flags);
  96. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  97. cache = device->cache.gid_cache[p];
  98. for (i = 0; i < cache->table_len; ++i) {
  99. if (!memcmp(gid, &cache->table[i], sizeof *gid)) {
  100. *port_num = p + start_port(device);
  101. if (index)
  102. *index = i;
  103. ret = 0;
  104. goto found;
  105. }
  106. }
  107. }
  108. found:
  109. read_unlock_irqrestore(&device->cache.lock, flags);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(ib_find_cached_gid);
  113. int ib_get_cached_pkey(struct ib_device *device,
  114. u8 port_num,
  115. int index,
  116. u16 *pkey)
  117. {
  118. struct ib_pkey_cache *cache;
  119. unsigned long flags;
  120. int ret = 0;
  121. if (port_num < start_port(device) || port_num > end_port(device))
  122. return -EINVAL;
  123. read_lock_irqsave(&device->cache.lock, flags);
  124. cache = device->cache.pkey_cache[port_num - start_port(device)];
  125. if (index < 0 || index >= cache->table_len)
  126. ret = -EINVAL;
  127. else
  128. *pkey = cache->table[index];
  129. read_unlock_irqrestore(&device->cache.lock, flags);
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(ib_get_cached_pkey);
  133. int ib_find_cached_pkey(struct ib_device *device,
  134. u8 port_num,
  135. u16 pkey,
  136. u16 *index)
  137. {
  138. struct ib_pkey_cache *cache;
  139. unsigned long flags;
  140. int i;
  141. int ret = -ENOENT;
  142. int partial_ix = -1;
  143. if (port_num < start_port(device) || port_num > end_port(device))
  144. return -EINVAL;
  145. read_lock_irqsave(&device->cache.lock, flags);
  146. cache = device->cache.pkey_cache[port_num - start_port(device)];
  147. *index = -1;
  148. for (i = 0; i < cache->table_len; ++i)
  149. if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
  150. if (cache->table[i] & 0x8000) {
  151. *index = i;
  152. ret = 0;
  153. break;
  154. } else
  155. partial_ix = i;
  156. }
  157. if (ret && partial_ix >= 0) {
  158. *index = partial_ix;
  159. ret = 0;
  160. }
  161. read_unlock_irqrestore(&device->cache.lock, flags);
  162. return ret;
  163. }
  164. EXPORT_SYMBOL(ib_find_cached_pkey);
  165. int ib_find_exact_cached_pkey(struct ib_device *device,
  166. u8 port_num,
  167. u16 pkey,
  168. u16 *index)
  169. {
  170. struct ib_pkey_cache *cache;
  171. unsigned long flags;
  172. int i;
  173. int ret = -ENOENT;
  174. if (port_num < start_port(device) || port_num > end_port(device))
  175. return -EINVAL;
  176. read_lock_irqsave(&device->cache.lock, flags);
  177. cache = device->cache.pkey_cache[port_num - start_port(device)];
  178. *index = -1;
  179. for (i = 0; i < cache->table_len; ++i)
  180. if (cache->table[i] == pkey) {
  181. *index = i;
  182. ret = 0;
  183. break;
  184. }
  185. read_unlock_irqrestore(&device->cache.lock, flags);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(ib_find_exact_cached_pkey);
  189. int ib_get_cached_lmc(struct ib_device *device,
  190. u8 port_num,
  191. u8 *lmc)
  192. {
  193. unsigned long flags;
  194. int ret = 0;
  195. if (port_num < start_port(device) || port_num > end_port(device))
  196. return -EINVAL;
  197. read_lock_irqsave(&device->cache.lock, flags);
  198. *lmc = device->cache.lmc_cache[port_num - start_port(device)];
  199. read_unlock_irqrestore(&device->cache.lock, flags);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL(ib_get_cached_lmc);
  203. static void ib_cache_update(struct ib_device *device,
  204. u8 port)
  205. {
  206. struct ib_port_attr *tprops = NULL;
  207. struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
  208. struct ib_gid_cache *gid_cache = NULL, *old_gid_cache;
  209. int i;
  210. int ret;
  211. tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
  212. if (!tprops)
  213. return;
  214. ret = ib_query_port(device, port, tprops);
  215. if (ret) {
  216. printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
  217. ret, device->name);
  218. goto err;
  219. }
  220. pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
  221. sizeof *pkey_cache->table, GFP_KERNEL);
  222. if (!pkey_cache)
  223. goto err;
  224. pkey_cache->table_len = tprops->pkey_tbl_len;
  225. gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
  226. sizeof *gid_cache->table, GFP_KERNEL);
  227. if (!gid_cache)
  228. goto err;
  229. gid_cache->table_len = tprops->gid_tbl_len;
  230. for (i = 0; i < pkey_cache->table_len; ++i) {
  231. ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
  232. if (ret) {
  233. printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
  234. ret, device->name, i);
  235. goto err;
  236. }
  237. }
  238. for (i = 0; i < gid_cache->table_len; ++i) {
  239. ret = ib_query_gid(device, port, i, gid_cache->table + i);
  240. if (ret) {
  241. printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
  242. ret, device->name, i);
  243. goto err;
  244. }
  245. }
  246. write_lock_irq(&device->cache.lock);
  247. old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
  248. old_gid_cache = device->cache.gid_cache [port - start_port(device)];
  249. device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
  250. device->cache.gid_cache [port - start_port(device)] = gid_cache;
  251. device->cache.lmc_cache[port - start_port(device)] = tprops->lmc;
  252. write_unlock_irq(&device->cache.lock);
  253. kfree(old_pkey_cache);
  254. kfree(old_gid_cache);
  255. kfree(tprops);
  256. return;
  257. err:
  258. kfree(pkey_cache);
  259. kfree(gid_cache);
  260. kfree(tprops);
  261. }
  262. static void ib_cache_task(struct work_struct *_work)
  263. {
  264. struct ib_update_work *work =
  265. container_of(_work, struct ib_update_work, work);
  266. ib_cache_update(work->device, work->port_num);
  267. kfree(work);
  268. }
  269. static void ib_cache_event(struct ib_event_handler *handler,
  270. struct ib_event *event)
  271. {
  272. struct ib_update_work *work;
  273. if (event->event == IB_EVENT_PORT_ERR ||
  274. event->event == IB_EVENT_PORT_ACTIVE ||
  275. event->event == IB_EVENT_LID_CHANGE ||
  276. event->event == IB_EVENT_PKEY_CHANGE ||
  277. event->event == IB_EVENT_SM_CHANGE ||
  278. event->event == IB_EVENT_CLIENT_REREGISTER ||
  279. event->event == IB_EVENT_GID_CHANGE) {
  280. work = kmalloc(sizeof *work, GFP_ATOMIC);
  281. if (work) {
  282. INIT_WORK(&work->work, ib_cache_task);
  283. work->device = event->device;
  284. work->port_num = event->element.port_num;
  285. queue_work(ib_wq, &work->work);
  286. }
  287. }
  288. }
  289. static void ib_cache_setup_one(struct ib_device *device)
  290. {
  291. int p;
  292. rwlock_init(&device->cache.lock);
  293. device->cache.pkey_cache =
  294. kmalloc(sizeof *device->cache.pkey_cache *
  295. (end_port(device) - start_port(device) + 1), GFP_KERNEL);
  296. device->cache.gid_cache =
  297. kmalloc(sizeof *device->cache.gid_cache *
  298. (end_port(device) - start_port(device) + 1), GFP_KERNEL);
  299. device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
  300. (end_port(device) -
  301. start_port(device) + 1),
  302. GFP_KERNEL);
  303. if (!device->cache.pkey_cache || !device->cache.gid_cache ||
  304. !device->cache.lmc_cache) {
  305. printk(KERN_WARNING "Couldn't allocate cache "
  306. "for %s\n", device->name);
  307. goto err;
  308. }
  309. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  310. device->cache.pkey_cache[p] = NULL;
  311. device->cache.gid_cache [p] = NULL;
  312. ib_cache_update(device, p + start_port(device));
  313. }
  314. INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
  315. device, ib_cache_event);
  316. if (ib_register_event_handler(&device->cache.event_handler))
  317. goto err_cache;
  318. return;
  319. err_cache:
  320. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  321. kfree(device->cache.pkey_cache[p]);
  322. kfree(device->cache.gid_cache[p]);
  323. }
  324. err:
  325. kfree(device->cache.pkey_cache);
  326. kfree(device->cache.gid_cache);
  327. kfree(device->cache.lmc_cache);
  328. }
  329. static void ib_cache_cleanup_one(struct ib_device *device)
  330. {
  331. int p;
  332. ib_unregister_event_handler(&device->cache.event_handler);
  333. flush_workqueue(ib_wq);
  334. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  335. kfree(device->cache.pkey_cache[p]);
  336. kfree(device->cache.gid_cache[p]);
  337. }
  338. kfree(device->cache.pkey_cache);
  339. kfree(device->cache.gid_cache);
  340. kfree(device->cache.lmc_cache);
  341. }
  342. static struct ib_client cache_client = {
  343. .name = "cache",
  344. .add = ib_cache_setup_one,
  345. .remove = ib_cache_cleanup_one
  346. };
  347. int __init ib_cache_setup(void)
  348. {
  349. return ib_register_client(&cache_client);
  350. }
  351. void __exit ib_cache_cleanup(void)
  352. {
  353. ib_unregister_client(&cache_client);
  354. }