cm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (c) 2012 Mellanox Technologies. 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. #include <rdma/ib_mad.h>
  33. #include <linux/mlx4/cmd.h>
  34. #include <linux/rbtree.h>
  35. #include <linux/idr.h>
  36. #include <rdma/ib_cm.h>
  37. #include "mlx4_ib.h"
  38. #define CM_CLEANUP_CACHE_TIMEOUT (5 * HZ)
  39. struct id_map_entry {
  40. struct rb_node node;
  41. u32 sl_cm_id;
  42. u32 pv_cm_id;
  43. int slave_id;
  44. int scheduled_delete;
  45. struct mlx4_ib_dev *dev;
  46. struct list_head list;
  47. struct delayed_work timeout;
  48. };
  49. struct cm_generic_msg {
  50. struct ib_mad_hdr hdr;
  51. __be32 local_comm_id;
  52. __be32 remote_comm_id;
  53. };
  54. struct cm_req_msg {
  55. unsigned char unused[0x60];
  56. union ib_gid primary_path_sgid;
  57. };
  58. static void set_local_comm_id(struct ib_mad *mad, u32 cm_id)
  59. {
  60. struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
  61. msg->local_comm_id = cpu_to_be32(cm_id);
  62. }
  63. static u32 get_local_comm_id(struct ib_mad *mad)
  64. {
  65. struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
  66. return be32_to_cpu(msg->local_comm_id);
  67. }
  68. static void set_remote_comm_id(struct ib_mad *mad, u32 cm_id)
  69. {
  70. struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
  71. msg->remote_comm_id = cpu_to_be32(cm_id);
  72. }
  73. static u32 get_remote_comm_id(struct ib_mad *mad)
  74. {
  75. struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
  76. return be32_to_cpu(msg->remote_comm_id);
  77. }
  78. static union ib_gid gid_from_req_msg(struct ib_device *ibdev, struct ib_mad *mad)
  79. {
  80. struct cm_req_msg *msg = (struct cm_req_msg *)mad;
  81. return msg->primary_path_sgid;
  82. }
  83. /* Lock should be taken before called */
  84. static struct id_map_entry *
  85. id_map_find_by_sl_id(struct ib_device *ibdev, u32 slave_id, u32 sl_cm_id)
  86. {
  87. struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map;
  88. struct rb_node *node = sl_id_map->rb_node;
  89. while (node) {
  90. struct id_map_entry *id_map_entry =
  91. rb_entry(node, struct id_map_entry, node);
  92. if (id_map_entry->sl_cm_id > sl_cm_id)
  93. node = node->rb_left;
  94. else if (id_map_entry->sl_cm_id < sl_cm_id)
  95. node = node->rb_right;
  96. else if (id_map_entry->slave_id > slave_id)
  97. node = node->rb_left;
  98. else if (id_map_entry->slave_id < slave_id)
  99. node = node->rb_right;
  100. else
  101. return id_map_entry;
  102. }
  103. return NULL;
  104. }
  105. static void id_map_ent_timeout(struct work_struct *work)
  106. {
  107. struct delayed_work *delay = to_delayed_work(work);
  108. struct id_map_entry *ent = container_of(delay, struct id_map_entry, timeout);
  109. struct id_map_entry *db_ent, *found_ent;
  110. struct mlx4_ib_dev *dev = ent->dev;
  111. struct mlx4_ib_sriov *sriov = &dev->sriov;
  112. struct rb_root *sl_id_map = &sriov->sl_id_map;
  113. int pv_id = (int) ent->pv_cm_id;
  114. spin_lock(&sriov->id_map_lock);
  115. db_ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, pv_id);
  116. if (!db_ent)
  117. goto out;
  118. found_ent = id_map_find_by_sl_id(&dev->ib_dev, ent->slave_id, ent->sl_cm_id);
  119. if (found_ent && found_ent == ent)
  120. rb_erase(&found_ent->node, sl_id_map);
  121. idr_remove(&sriov->pv_id_table, pv_id);
  122. out:
  123. list_del(&ent->list);
  124. spin_unlock(&sriov->id_map_lock);
  125. kfree(ent);
  126. }
  127. static void id_map_find_del(struct ib_device *ibdev, int pv_cm_id)
  128. {
  129. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  130. struct rb_root *sl_id_map = &sriov->sl_id_map;
  131. struct id_map_entry *ent, *found_ent;
  132. spin_lock(&sriov->id_map_lock);
  133. ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, pv_cm_id);
  134. if (!ent)
  135. goto out;
  136. found_ent = id_map_find_by_sl_id(ibdev, ent->slave_id, ent->sl_cm_id);
  137. if (found_ent && found_ent == ent)
  138. rb_erase(&found_ent->node, sl_id_map);
  139. idr_remove(&sriov->pv_id_table, pv_cm_id);
  140. out:
  141. spin_unlock(&sriov->id_map_lock);
  142. }
  143. static void sl_id_map_add(struct ib_device *ibdev, struct id_map_entry *new)
  144. {
  145. struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map;
  146. struct rb_node **link = &sl_id_map->rb_node, *parent = NULL;
  147. struct id_map_entry *ent;
  148. int slave_id = new->slave_id;
  149. int sl_cm_id = new->sl_cm_id;
  150. ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id);
  151. if (ent) {
  152. pr_debug("overriding existing sl_id_map entry (cm_id = %x)\n",
  153. sl_cm_id);
  154. rb_replace_node(&ent->node, &new->node, sl_id_map);
  155. return;
  156. }
  157. /* Go to the bottom of the tree */
  158. while (*link) {
  159. parent = *link;
  160. ent = rb_entry(parent, struct id_map_entry, node);
  161. if (ent->sl_cm_id > sl_cm_id || (ent->sl_cm_id == sl_cm_id && ent->slave_id > slave_id))
  162. link = &(*link)->rb_left;
  163. else
  164. link = &(*link)->rb_right;
  165. }
  166. rb_link_node(&new->node, parent, link);
  167. rb_insert_color(&new->node, sl_id_map);
  168. }
  169. static struct id_map_entry *
  170. id_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id)
  171. {
  172. int ret;
  173. static int next_id;
  174. struct id_map_entry *ent;
  175. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  176. ent = kmalloc(sizeof (struct id_map_entry), GFP_KERNEL);
  177. if (!ent) {
  178. mlx4_ib_warn(ibdev, "Couldn't allocate id cache entry - out of memory\n");
  179. return ERR_PTR(-ENOMEM);
  180. }
  181. ent->sl_cm_id = sl_cm_id;
  182. ent->slave_id = slave_id;
  183. ent->scheduled_delete = 0;
  184. ent->dev = to_mdev(ibdev);
  185. INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout);
  186. idr_preload(GFP_KERNEL);
  187. spin_lock(&to_mdev(ibdev)->sriov.id_map_lock);
  188. ret = idr_alloc(&sriov->pv_id_table, ent, next_id, 0, GFP_NOWAIT);
  189. if (ret >= 0) {
  190. next_id = max(ret + 1, 0);
  191. ent->pv_cm_id = (u32)ret;
  192. sl_id_map_add(ibdev, ent);
  193. list_add_tail(&ent->list, &sriov->cm_list);
  194. }
  195. spin_unlock(&sriov->id_map_lock);
  196. idr_preload_end();
  197. if (ret >= 0)
  198. return ent;
  199. /*error flow*/
  200. kfree(ent);
  201. mlx4_ib_warn(ibdev, "No more space in the idr (err:0x%x)\n", ret);
  202. return ERR_PTR(-ENOMEM);
  203. }
  204. static struct id_map_entry *
  205. id_map_get(struct ib_device *ibdev, int *pv_cm_id, int sl_cm_id, int slave_id)
  206. {
  207. struct id_map_entry *ent;
  208. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  209. spin_lock(&sriov->id_map_lock);
  210. if (*pv_cm_id == -1) {
  211. ent = id_map_find_by_sl_id(ibdev, sl_cm_id, slave_id);
  212. if (ent)
  213. *pv_cm_id = (int) ent->pv_cm_id;
  214. } else
  215. ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, *pv_cm_id);
  216. spin_unlock(&sriov->id_map_lock);
  217. return ent;
  218. }
  219. static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id)
  220. {
  221. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  222. unsigned long flags;
  223. spin_lock(&sriov->id_map_lock);
  224. spin_lock_irqsave(&sriov->going_down_lock, flags);
  225. /*make sure that there is no schedule inside the scheduled work.*/
  226. if (!sriov->is_going_down) {
  227. id->scheduled_delete = 1;
  228. schedule_delayed_work(&id->timeout, CM_CLEANUP_CACHE_TIMEOUT);
  229. }
  230. spin_unlock_irqrestore(&sriov->going_down_lock, flags);
  231. spin_unlock(&sriov->id_map_lock);
  232. }
  233. int mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id,
  234. struct ib_mad *mad)
  235. {
  236. struct id_map_entry *id;
  237. u32 sl_cm_id;
  238. int pv_cm_id = -1;
  239. sl_cm_id = get_local_comm_id(mad);
  240. if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
  241. mad->mad_hdr.attr_id == CM_REP_ATTR_ID) {
  242. id = id_map_alloc(ibdev, slave_id, sl_cm_id);
  243. if (IS_ERR(id)) {
  244. mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n",
  245. __func__, slave_id, sl_cm_id);
  246. return PTR_ERR(id);
  247. }
  248. } else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID) {
  249. return 0;
  250. } else {
  251. id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
  252. }
  253. if (!id) {
  254. pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL!\n",
  255. slave_id, sl_cm_id);
  256. return -EINVAL;
  257. }
  258. set_local_comm_id(mad, id->pv_cm_id);
  259. if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
  260. schedule_delayed(ibdev, id);
  261. else if (mad->mad_hdr.attr_id == CM_DREP_ATTR_ID)
  262. id_map_find_del(ibdev, pv_cm_id);
  263. return 0;
  264. }
  265. int mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave,
  266. struct ib_mad *mad)
  267. {
  268. u32 pv_cm_id;
  269. struct id_map_entry *id;
  270. if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID) {
  271. union ib_gid gid;
  272. gid = gid_from_req_msg(ibdev, mad);
  273. *slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id);
  274. if (*slave < 0) {
  275. mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n",
  276. gid.global.interface_id);
  277. return -ENOENT;
  278. }
  279. return 0;
  280. }
  281. pv_cm_id = get_remote_comm_id(mad);
  282. id = id_map_get(ibdev, (int *)&pv_cm_id, -1, -1);
  283. if (!id) {
  284. pr_debug("Couldn't find an entry for pv_cm_id 0x%x\n", pv_cm_id);
  285. return -ENOENT;
  286. }
  287. *slave = id->slave_id;
  288. set_remote_comm_id(mad, id->sl_cm_id);
  289. if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
  290. schedule_delayed(ibdev, id);
  291. else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID ||
  292. mad->mad_hdr.attr_id == CM_DREP_ATTR_ID) {
  293. id_map_find_del(ibdev, (int) pv_cm_id);
  294. }
  295. return 0;
  296. }
  297. void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev)
  298. {
  299. spin_lock_init(&dev->sriov.id_map_lock);
  300. INIT_LIST_HEAD(&dev->sriov.cm_list);
  301. dev->sriov.sl_id_map = RB_ROOT;
  302. idr_init(&dev->sriov.pv_id_table);
  303. }
  304. /* slave = -1 ==> all slaves */
  305. /* TBD -- call paravirt clean for single slave. Need for slave RESET event */
  306. void mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave)
  307. {
  308. struct mlx4_ib_sriov *sriov = &dev->sriov;
  309. struct rb_root *sl_id_map = &sriov->sl_id_map;
  310. struct list_head lh;
  311. struct rb_node *nd;
  312. int need_flush = 1;
  313. struct id_map_entry *map, *tmp_map;
  314. /* cancel all delayed work queue entries */
  315. INIT_LIST_HEAD(&lh);
  316. spin_lock(&sriov->id_map_lock);
  317. list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
  318. if (slave < 0 || slave == map->slave_id) {
  319. if (map->scheduled_delete)
  320. need_flush &= !!cancel_delayed_work(&map->timeout);
  321. }
  322. }
  323. spin_unlock(&sriov->id_map_lock);
  324. if (!need_flush)
  325. flush_scheduled_work(); /* make sure all timers were flushed */
  326. /* now, remove all leftover entries from databases*/
  327. spin_lock(&sriov->id_map_lock);
  328. if (slave < 0) {
  329. while (rb_first(sl_id_map)) {
  330. struct id_map_entry *ent =
  331. rb_entry(rb_first(sl_id_map),
  332. struct id_map_entry, node);
  333. rb_erase(&ent->node, sl_id_map);
  334. idr_remove(&sriov->pv_id_table, (int) ent->pv_cm_id);
  335. }
  336. list_splice_init(&dev->sriov.cm_list, &lh);
  337. } else {
  338. /* first, move nodes belonging to slave to db remove list */
  339. nd = rb_first(sl_id_map);
  340. while (nd) {
  341. struct id_map_entry *ent =
  342. rb_entry(nd, struct id_map_entry, node);
  343. nd = rb_next(nd);
  344. if (ent->slave_id == slave)
  345. list_move_tail(&ent->list, &lh);
  346. }
  347. /* remove those nodes from databases */
  348. list_for_each_entry_safe(map, tmp_map, &lh, list) {
  349. rb_erase(&map->node, sl_id_map);
  350. idr_remove(&sriov->pv_id_table, (int) map->pv_cm_id);
  351. }
  352. /* add remaining nodes from cm_list */
  353. list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
  354. if (slave == map->slave_id)
  355. list_move_tail(&map->list, &lh);
  356. }
  357. }
  358. spin_unlock(&sriov->id_map_lock);
  359. /* free any map entries left behind due to cancel_delayed_work above */
  360. list_for_each_entry_safe(map, tmp_map, &lh, list) {
  361. list_del(&map->list);
  362. kfree(map);
  363. }
  364. }