cm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. struct id_map_entry *ent;
  174. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  175. ent = kmalloc(sizeof (struct id_map_entry), GFP_KERNEL);
  176. if (!ent) {
  177. mlx4_ib_warn(ibdev, "Couldn't allocate id cache entry - out of memory\n");
  178. return ERR_PTR(-ENOMEM);
  179. }
  180. ent->sl_cm_id = sl_cm_id;
  181. ent->slave_id = slave_id;
  182. ent->scheduled_delete = 0;
  183. ent->dev = to_mdev(ibdev);
  184. INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout);
  185. idr_preload(GFP_KERNEL);
  186. spin_lock(&to_mdev(ibdev)->sriov.id_map_lock);
  187. ret = idr_alloc_cyclic(&sriov->pv_id_table, ent, 0, 0, GFP_NOWAIT);
  188. if (ret >= 0) {
  189. ent->pv_cm_id = (u32)ret;
  190. sl_id_map_add(ibdev, ent);
  191. list_add_tail(&ent->list, &sriov->cm_list);
  192. }
  193. spin_unlock(&sriov->id_map_lock);
  194. idr_preload_end();
  195. if (ret >= 0)
  196. return ent;
  197. /*error flow*/
  198. kfree(ent);
  199. mlx4_ib_warn(ibdev, "No more space in the idr (err:0x%x)\n", ret);
  200. return ERR_PTR(-ENOMEM);
  201. }
  202. static struct id_map_entry *
  203. id_map_get(struct ib_device *ibdev, int *pv_cm_id, int sl_cm_id, int slave_id)
  204. {
  205. struct id_map_entry *ent;
  206. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  207. spin_lock(&sriov->id_map_lock);
  208. if (*pv_cm_id == -1) {
  209. ent = id_map_find_by_sl_id(ibdev, sl_cm_id, slave_id);
  210. if (ent)
  211. *pv_cm_id = (int) ent->pv_cm_id;
  212. } else
  213. ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, *pv_cm_id);
  214. spin_unlock(&sriov->id_map_lock);
  215. return ent;
  216. }
  217. static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id)
  218. {
  219. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  220. unsigned long flags;
  221. spin_lock(&sriov->id_map_lock);
  222. spin_lock_irqsave(&sriov->going_down_lock, flags);
  223. /*make sure that there is no schedule inside the scheduled work.*/
  224. if (!sriov->is_going_down) {
  225. id->scheduled_delete = 1;
  226. schedule_delayed_work(&id->timeout, CM_CLEANUP_CACHE_TIMEOUT);
  227. }
  228. spin_unlock_irqrestore(&sriov->going_down_lock, flags);
  229. spin_unlock(&sriov->id_map_lock);
  230. }
  231. int mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id,
  232. struct ib_mad *mad)
  233. {
  234. struct id_map_entry *id;
  235. u32 sl_cm_id;
  236. int pv_cm_id = -1;
  237. sl_cm_id = get_local_comm_id(mad);
  238. if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
  239. mad->mad_hdr.attr_id == CM_REP_ATTR_ID) {
  240. id = id_map_alloc(ibdev, slave_id, sl_cm_id);
  241. if (IS_ERR(id)) {
  242. mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n",
  243. __func__, slave_id, sl_cm_id);
  244. return PTR_ERR(id);
  245. }
  246. } else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID) {
  247. return 0;
  248. } else {
  249. id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
  250. }
  251. if (!id) {
  252. pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL!\n",
  253. slave_id, sl_cm_id);
  254. return -EINVAL;
  255. }
  256. set_local_comm_id(mad, id->pv_cm_id);
  257. if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
  258. schedule_delayed(ibdev, id);
  259. else if (mad->mad_hdr.attr_id == CM_DREP_ATTR_ID)
  260. id_map_find_del(ibdev, pv_cm_id);
  261. return 0;
  262. }
  263. int mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave,
  264. struct ib_mad *mad)
  265. {
  266. u32 pv_cm_id;
  267. struct id_map_entry *id;
  268. if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID) {
  269. union ib_gid gid;
  270. gid = gid_from_req_msg(ibdev, mad);
  271. *slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id);
  272. if (*slave < 0) {
  273. mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n",
  274. gid.global.interface_id);
  275. return -ENOENT;
  276. }
  277. return 0;
  278. }
  279. pv_cm_id = get_remote_comm_id(mad);
  280. id = id_map_get(ibdev, (int *)&pv_cm_id, -1, -1);
  281. if (!id) {
  282. pr_debug("Couldn't find an entry for pv_cm_id 0x%x\n", pv_cm_id);
  283. return -ENOENT;
  284. }
  285. *slave = id->slave_id;
  286. set_remote_comm_id(mad, id->sl_cm_id);
  287. if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
  288. schedule_delayed(ibdev, id);
  289. else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID ||
  290. mad->mad_hdr.attr_id == CM_DREP_ATTR_ID) {
  291. id_map_find_del(ibdev, (int) pv_cm_id);
  292. }
  293. return 0;
  294. }
  295. void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev)
  296. {
  297. spin_lock_init(&dev->sriov.id_map_lock);
  298. INIT_LIST_HEAD(&dev->sriov.cm_list);
  299. dev->sriov.sl_id_map = RB_ROOT;
  300. idr_init(&dev->sriov.pv_id_table);
  301. }
  302. /* slave = -1 ==> all slaves */
  303. /* TBD -- call paravirt clean for single slave. Need for slave RESET event */
  304. void mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave)
  305. {
  306. struct mlx4_ib_sriov *sriov = &dev->sriov;
  307. struct rb_root *sl_id_map = &sriov->sl_id_map;
  308. struct list_head lh;
  309. struct rb_node *nd;
  310. int need_flush = 1;
  311. struct id_map_entry *map, *tmp_map;
  312. /* cancel all delayed work queue entries */
  313. INIT_LIST_HEAD(&lh);
  314. spin_lock(&sriov->id_map_lock);
  315. list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
  316. if (slave < 0 || slave == map->slave_id) {
  317. if (map->scheduled_delete)
  318. need_flush &= !!cancel_delayed_work(&map->timeout);
  319. }
  320. }
  321. spin_unlock(&sriov->id_map_lock);
  322. if (!need_flush)
  323. flush_scheduled_work(); /* make sure all timers were flushed */
  324. /* now, remove all leftover entries from databases*/
  325. spin_lock(&sriov->id_map_lock);
  326. if (slave < 0) {
  327. while (rb_first(sl_id_map)) {
  328. struct id_map_entry *ent =
  329. rb_entry(rb_first(sl_id_map),
  330. struct id_map_entry, node);
  331. rb_erase(&ent->node, sl_id_map);
  332. idr_remove(&sriov->pv_id_table, (int) ent->pv_cm_id);
  333. }
  334. list_splice_init(&dev->sriov.cm_list, &lh);
  335. } else {
  336. /* first, move nodes belonging to slave to db remove list */
  337. nd = rb_first(sl_id_map);
  338. while (nd) {
  339. struct id_map_entry *ent =
  340. rb_entry(nd, struct id_map_entry, node);
  341. nd = rb_next(nd);
  342. if (ent->slave_id == slave)
  343. list_move_tail(&ent->list, &lh);
  344. }
  345. /* remove those nodes from databases */
  346. list_for_each_entry_safe(map, tmp_map, &lh, list) {
  347. rb_erase(&map->node, sl_id_map);
  348. idr_remove(&sriov->pv_id_table, (int) map->pv_cm_id);
  349. }
  350. /* add remaining nodes from cm_list */
  351. list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
  352. if (slave == map->slave_id)
  353. list_move_tail(&map->list, &lh);
  354. }
  355. }
  356. spin_unlock(&sriov->id_map_lock);
  357. /* free any map entries left behind due to cancel_delayed_work above */
  358. list_for_each_entry_safe(map, tmp_map, &lh, list) {
  359. list_del(&map->list);
  360. kfree(map);
  361. }
  362. }