cache.c 9.1 KB

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