cm.c 13 KB

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