cm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. mlx4_ib_warn(ibdev, "Couldn't allocate id cache entry - out of memory\n");
  218. return ERR_PTR(-ENOMEM);
  219. }
  220. ent->sl_cm_id = sl_cm_id;
  221. ent->slave_id = slave_id;
  222. ent->scheduled_delete = 0;
  223. ent->dev = to_mdev(ibdev);
  224. INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout);
  225. idr_preload(GFP_KERNEL);
  226. spin_lock(&to_mdev(ibdev)->sriov.id_map_lock);
  227. ret = idr_alloc_cyclic(&sriov->pv_id_table, ent, 0, 0, GFP_NOWAIT);
  228. if (ret >= 0) {
  229. ent->pv_cm_id = (u32)ret;
  230. sl_id_map_add(ibdev, ent);
  231. list_add_tail(&ent->list, &sriov->cm_list);
  232. }
  233. spin_unlock(&sriov->id_map_lock);
  234. idr_preload_end();
  235. if (ret >= 0)
  236. return ent;
  237. /*error flow*/
  238. kfree(ent);
  239. mlx4_ib_warn(ibdev, "No more space in the idr (err:0x%x)\n", ret);
  240. return ERR_PTR(-ENOMEM);
  241. }
  242. static struct id_map_entry *
  243. id_map_get(struct ib_device *ibdev, int *pv_cm_id, int sl_cm_id, int slave_id)
  244. {
  245. struct id_map_entry *ent;
  246. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  247. spin_lock(&sriov->id_map_lock);
  248. if (*pv_cm_id == -1) {
  249. ent = id_map_find_by_sl_id(ibdev, sl_cm_id, slave_id);
  250. if (ent)
  251. *pv_cm_id = (int) ent->pv_cm_id;
  252. } else
  253. ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, *pv_cm_id);
  254. spin_unlock(&sriov->id_map_lock);
  255. return ent;
  256. }
  257. static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id)
  258. {
  259. struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
  260. unsigned long flags;
  261. spin_lock(&sriov->id_map_lock);
  262. spin_lock_irqsave(&sriov->going_down_lock, flags);
  263. /*make sure that there is no schedule inside the scheduled work.*/
  264. if (!sriov->is_going_down) {
  265. id->scheduled_delete = 1;
  266. schedule_delayed_work(&id->timeout, CM_CLEANUP_CACHE_TIMEOUT);
  267. }
  268. spin_unlock_irqrestore(&sriov->going_down_lock, flags);
  269. spin_unlock(&sriov->id_map_lock);
  270. }
  271. int mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id,
  272. struct ib_mad *mad)
  273. {
  274. struct id_map_entry *id;
  275. u32 sl_cm_id;
  276. int pv_cm_id = -1;
  277. if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
  278. mad->mad_hdr.attr_id == CM_REP_ATTR_ID ||
  279. mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
  280. sl_cm_id = get_local_comm_id(mad);
  281. id = id_map_alloc(ibdev, slave_id, sl_cm_id);
  282. if (IS_ERR(id)) {
  283. mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n",
  284. __func__, slave_id, sl_cm_id);
  285. return PTR_ERR(id);
  286. }
  287. } else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID ||
  288. mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
  289. return 0;
  290. } else {
  291. sl_cm_id = get_local_comm_id(mad);
  292. id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
  293. }
  294. if (!id) {
  295. pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL!\n",
  296. slave_id, sl_cm_id);
  297. return -EINVAL;
  298. }
  299. set_local_comm_id(mad, id->pv_cm_id);
  300. if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
  301. schedule_delayed(ibdev, id);
  302. else if (mad->mad_hdr.attr_id == CM_DREP_ATTR_ID)
  303. id_map_find_del(ibdev, pv_cm_id);
  304. return 0;
  305. }
  306. int mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave,
  307. struct ib_mad *mad)
  308. {
  309. u32 pv_cm_id;
  310. struct id_map_entry *id;
  311. if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
  312. mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
  313. union ib_gid gid;
  314. if (!slave)
  315. return 0;
  316. gid = gid_from_req_msg(ibdev, mad);
  317. *slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id);
  318. if (*slave < 0) {
  319. mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n",
  320. be64_to_cpu(gid.global.interface_id));
  321. return -ENOENT;
  322. }
  323. return 0;
  324. }
  325. pv_cm_id = get_remote_comm_id(mad);
  326. id = id_map_get(ibdev, (int *)&pv_cm_id, -1, -1);
  327. if (!id) {
  328. pr_debug("Couldn't find an entry for pv_cm_id 0x%x\n", pv_cm_id);
  329. return -ENOENT;
  330. }
  331. if (slave)
  332. *slave = id->slave_id;
  333. set_remote_comm_id(mad, id->sl_cm_id);
  334. if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
  335. schedule_delayed(ibdev, id);
  336. else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID ||
  337. mad->mad_hdr.attr_id == CM_DREP_ATTR_ID) {
  338. id_map_find_del(ibdev, (int) pv_cm_id);
  339. }
  340. return 0;
  341. }
  342. void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev)
  343. {
  344. spin_lock_init(&dev->sriov.id_map_lock);
  345. INIT_LIST_HEAD(&dev->sriov.cm_list);
  346. dev->sriov.sl_id_map = RB_ROOT;
  347. idr_init(&dev->sriov.pv_id_table);
  348. }
  349. /* slave = -1 ==> all slaves */
  350. /* TBD -- call paravirt clean for single slave. Need for slave RESET event */
  351. void mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave)
  352. {
  353. struct mlx4_ib_sriov *sriov = &dev->sriov;
  354. struct rb_root *sl_id_map = &sriov->sl_id_map;
  355. struct list_head lh;
  356. struct rb_node *nd;
  357. int need_flush = 1;
  358. struct id_map_entry *map, *tmp_map;
  359. /* cancel all delayed work queue entries */
  360. INIT_LIST_HEAD(&lh);
  361. spin_lock(&sriov->id_map_lock);
  362. list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
  363. if (slave < 0 || slave == map->slave_id) {
  364. if (map->scheduled_delete)
  365. need_flush &= !!cancel_delayed_work(&map->timeout);
  366. }
  367. }
  368. spin_unlock(&sriov->id_map_lock);
  369. if (!need_flush)
  370. flush_scheduled_work(); /* make sure all timers were flushed */
  371. /* now, remove all leftover entries from databases*/
  372. spin_lock(&sriov->id_map_lock);
  373. if (slave < 0) {
  374. while (rb_first(sl_id_map)) {
  375. struct id_map_entry *ent =
  376. rb_entry(rb_first(sl_id_map),
  377. struct id_map_entry, node);
  378. rb_erase(&ent->node, sl_id_map);
  379. idr_remove(&sriov->pv_id_table, (int) ent->pv_cm_id);
  380. }
  381. list_splice_init(&dev->sriov.cm_list, &lh);
  382. } else {
  383. /* first, move nodes belonging to slave to db remove list */
  384. nd = rb_first(sl_id_map);
  385. while (nd) {
  386. struct id_map_entry *ent =
  387. rb_entry(nd, struct id_map_entry, node);
  388. nd = rb_next(nd);
  389. if (ent->slave_id == slave)
  390. list_move_tail(&ent->list, &lh);
  391. }
  392. /* remove those nodes from databases */
  393. list_for_each_entry_safe(map, tmp_map, &lh, list) {
  394. rb_erase(&map->node, sl_id_map);
  395. idr_remove(&sriov->pv_id_table, (int) map->pv_cm_id);
  396. }
  397. /* add remaining nodes from cm_list */
  398. list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
  399. if (slave == map->slave_id)
  400. list_move_tail(&map->list, &lh);
  401. }
  402. }
  403. spin_unlock(&sriov->id_map_lock);
  404. /* free any map entries left behind due to cancel_delayed_work above */
  405. list_for_each_entry_safe(map, tmp_map, &lh, list) {
  406. list_del(&map->list);
  407. kfree(map);
  408. }
  409. }