cache.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * $Id: cache.c 1349 2004-12-16 21:09:43Z roland $
  36. */
  37. #include <linux/module.h>
  38. #include <linux/errno.h>
  39. #include <linux/slab.h>
  40. #include <rdma/ib_cache.h>
  41. #include "core_priv.h"
  42. struct ib_pkey_cache {
  43. int table_len;
  44. u16 table[0];
  45. };
  46. struct ib_gid_cache {
  47. int table_len;
  48. union ib_gid table[0];
  49. };
  50. struct ib_update_work {
  51. struct work_struct work;
  52. struct ib_device *device;
  53. u8 port_num;
  54. };
  55. static inline int start_port(struct ib_device *device)
  56. {
  57. return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
  58. }
  59. static inline int end_port(struct ib_device *device)
  60. {
  61. return (device->node_type == RDMA_NODE_IB_SWITCH) ?
  62. 0 : device->phys_port_cnt;
  63. }
  64. int ib_get_cached_gid(struct ib_device *device,
  65. u8 port_num,
  66. int index,
  67. union ib_gid *gid)
  68. {
  69. struct ib_gid_cache *cache;
  70. unsigned long flags;
  71. int ret = 0;
  72. if (port_num < start_port(device) || port_num > end_port(device))
  73. return -EINVAL;
  74. read_lock_irqsave(&device->cache.lock, flags);
  75. cache = device->cache.gid_cache[port_num - start_port(device)];
  76. if (index < 0 || index >= cache->table_len)
  77. ret = -EINVAL;
  78. else
  79. *gid = cache->table[index];
  80. read_unlock_irqrestore(&device->cache.lock, flags);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL(ib_get_cached_gid);
  84. int ib_find_cached_gid(struct ib_device *device,
  85. union ib_gid *gid,
  86. u8 *port_num,
  87. u16 *index)
  88. {
  89. struct ib_gid_cache *cache;
  90. unsigned long flags;
  91. int p, i;
  92. int ret = -ENOENT;
  93. *port_num = -1;
  94. if (index)
  95. *index = -1;
  96. read_lock_irqsave(&device->cache.lock, flags);
  97. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  98. cache = device->cache.gid_cache[p];
  99. for (i = 0; i < cache->table_len; ++i) {
  100. if (!memcmp(gid, &cache->table[i], sizeof *gid)) {
  101. *port_num = p + start_port(device);
  102. if (index)
  103. *index = i;
  104. ret = 0;
  105. goto found;
  106. }
  107. }
  108. }
  109. found:
  110. read_unlock_irqrestore(&device->cache.lock, flags);
  111. return ret;
  112. }
  113. EXPORT_SYMBOL(ib_find_cached_gid);
  114. int ib_get_cached_pkey(struct ib_device *device,
  115. u8 port_num,
  116. int index,
  117. u16 *pkey)
  118. {
  119. struct ib_pkey_cache *cache;
  120. unsigned long flags;
  121. int ret = 0;
  122. if (port_num < start_port(device) || port_num > end_port(device))
  123. return -EINVAL;
  124. read_lock_irqsave(&device->cache.lock, flags);
  125. cache = device->cache.pkey_cache[port_num - start_port(device)];
  126. if (index < 0 || index >= cache->table_len)
  127. ret = -EINVAL;
  128. else
  129. *pkey = cache->table[index];
  130. read_unlock_irqrestore(&device->cache.lock, flags);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(ib_get_cached_pkey);
  134. int ib_find_cached_pkey(struct ib_device *device,
  135. u8 port_num,
  136. u16 pkey,
  137. u16 *index)
  138. {
  139. struct ib_pkey_cache *cache;
  140. unsigned long flags;
  141. int i;
  142. int ret = -ENOENT;
  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. *index = i;
  151. ret = 0;
  152. break;
  153. }
  154. read_unlock_irqrestore(&device->cache.lock, flags);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL(ib_find_cached_pkey);
  158. int ib_get_cached_lmc(struct ib_device *device,
  159. u8 port_num,
  160. u8 *lmc)
  161. {
  162. unsigned long flags;
  163. int ret = 0;
  164. if (port_num < start_port(device) || port_num > end_port(device))
  165. return -EINVAL;
  166. read_lock_irqsave(&device->cache.lock, flags);
  167. *lmc = device->cache.lmc_cache[port_num - start_port(device)];
  168. read_unlock_irqrestore(&device->cache.lock, flags);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(ib_get_cached_lmc);
  172. static void ib_cache_update(struct ib_device *device,
  173. u8 port)
  174. {
  175. struct ib_port_attr *tprops = NULL;
  176. struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
  177. struct ib_gid_cache *gid_cache = NULL, *old_gid_cache;
  178. int i;
  179. int ret;
  180. tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
  181. if (!tprops)
  182. return;
  183. ret = ib_query_port(device, port, tprops);
  184. if (ret) {
  185. printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
  186. ret, device->name);
  187. goto err;
  188. }
  189. pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
  190. sizeof *pkey_cache->table, GFP_KERNEL);
  191. if (!pkey_cache)
  192. goto err;
  193. pkey_cache->table_len = tprops->pkey_tbl_len;
  194. gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
  195. sizeof *gid_cache->table, GFP_KERNEL);
  196. if (!gid_cache)
  197. goto err;
  198. gid_cache->table_len = tprops->gid_tbl_len;
  199. for (i = 0; i < pkey_cache->table_len; ++i) {
  200. ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
  201. if (ret) {
  202. printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
  203. ret, device->name, i);
  204. goto err;
  205. }
  206. }
  207. for (i = 0; i < gid_cache->table_len; ++i) {
  208. ret = ib_query_gid(device, port, i, gid_cache->table + i);
  209. if (ret) {
  210. printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
  211. ret, device->name, i);
  212. goto err;
  213. }
  214. }
  215. write_lock_irq(&device->cache.lock);
  216. old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
  217. old_gid_cache = device->cache.gid_cache [port - start_port(device)];
  218. device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
  219. device->cache.gid_cache [port - start_port(device)] = gid_cache;
  220. device->cache.lmc_cache[port - start_port(device)] = tprops->lmc;
  221. write_unlock_irq(&device->cache.lock);
  222. kfree(old_pkey_cache);
  223. kfree(old_gid_cache);
  224. kfree(tprops);
  225. return;
  226. err:
  227. kfree(pkey_cache);
  228. kfree(gid_cache);
  229. kfree(tprops);
  230. }
  231. static void ib_cache_task(struct work_struct *_work)
  232. {
  233. struct ib_update_work *work =
  234. container_of(_work, struct ib_update_work, work);
  235. ib_cache_update(work->device, work->port_num);
  236. kfree(work);
  237. }
  238. static void ib_cache_event(struct ib_event_handler *handler,
  239. struct ib_event *event)
  240. {
  241. struct ib_update_work *work;
  242. if (event->event == IB_EVENT_PORT_ERR ||
  243. event->event == IB_EVENT_PORT_ACTIVE ||
  244. event->event == IB_EVENT_LID_CHANGE ||
  245. event->event == IB_EVENT_PKEY_CHANGE ||
  246. event->event == IB_EVENT_SM_CHANGE ||
  247. event->event == IB_EVENT_CLIENT_REREGISTER) {
  248. work = kmalloc(sizeof *work, GFP_ATOMIC);
  249. if (work) {
  250. INIT_WORK(&work->work, ib_cache_task);
  251. work->device = event->device;
  252. work->port_num = event->element.port_num;
  253. schedule_work(&work->work);
  254. }
  255. }
  256. }
  257. static void ib_cache_setup_one(struct ib_device *device)
  258. {
  259. int p;
  260. rwlock_init(&device->cache.lock);
  261. device->cache.pkey_cache =
  262. kmalloc(sizeof *device->cache.pkey_cache *
  263. (end_port(device) - start_port(device) + 1), GFP_KERNEL);
  264. device->cache.gid_cache =
  265. kmalloc(sizeof *device->cache.gid_cache *
  266. (end_port(device) - start_port(device) + 1), GFP_KERNEL);
  267. device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
  268. (end_port(device) -
  269. start_port(device) + 1),
  270. GFP_KERNEL);
  271. if (!device->cache.pkey_cache || !device->cache.gid_cache ||
  272. !device->cache.lmc_cache) {
  273. printk(KERN_WARNING "Couldn't allocate cache "
  274. "for %s\n", device->name);
  275. goto err;
  276. }
  277. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  278. device->cache.pkey_cache[p] = NULL;
  279. device->cache.gid_cache [p] = NULL;
  280. ib_cache_update(device, p + start_port(device));
  281. }
  282. INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
  283. device, ib_cache_event);
  284. if (ib_register_event_handler(&device->cache.event_handler))
  285. goto err_cache;
  286. return;
  287. err_cache:
  288. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  289. kfree(device->cache.pkey_cache[p]);
  290. kfree(device->cache.gid_cache[p]);
  291. }
  292. err:
  293. kfree(device->cache.pkey_cache);
  294. kfree(device->cache.gid_cache);
  295. kfree(device->cache.lmc_cache);
  296. }
  297. static void ib_cache_cleanup_one(struct ib_device *device)
  298. {
  299. int p;
  300. ib_unregister_event_handler(&device->cache.event_handler);
  301. flush_scheduled_work();
  302. for (p = 0; p <= end_port(device) - start_port(device); ++p) {
  303. kfree(device->cache.pkey_cache[p]);
  304. kfree(device->cache.gid_cache[p]);
  305. }
  306. kfree(device->cache.pkey_cache);
  307. kfree(device->cache.gid_cache);
  308. kfree(device->cache.lmc_cache);
  309. }
  310. static struct ib_client cache_client = {
  311. .name = "cache",
  312. .add = ib_cache_setup_one,
  313. .remove = ib_cache_cleanup_one
  314. };
  315. int __init ib_cache_setup(void)
  316. {
  317. return ib_register_client(&cache_client);
  318. }
  319. void __exit ib_cache_cleanup(void)
  320. {
  321. ib_unregister_client(&cache_client);
  322. }