mcg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/string.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/mlx4/cmd.h>
  36. #include "mlx4.h"
  37. #define MGM_QPN_MASK 0x00FFFFFF
  38. #define MGM_BLCK_LB_BIT 30
  39. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  40. static int mlx4_READ_ENTRY(struct mlx4_dev *dev, int index,
  41. struct mlx4_cmd_mailbox *mailbox)
  42. {
  43. return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
  44. MLX4_CMD_TIME_CLASS_A);
  45. }
  46. static int mlx4_WRITE_ENTRY(struct mlx4_dev *dev, int index,
  47. struct mlx4_cmd_mailbox *mailbox)
  48. {
  49. return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
  50. MLX4_CMD_TIME_CLASS_A);
  51. }
  52. static int mlx4_WRITE_PROMISC(struct mlx4_dev *dev, u8 vep_num, u8 port, u8 steer,
  53. struct mlx4_cmd_mailbox *mailbox)
  54. {
  55. u32 in_mod;
  56. in_mod = (u32) vep_num << 24 | (u32) port << 16 | steer << 1;
  57. return mlx4_cmd(dev, mailbox->dma, in_mod, 0x1,
  58. MLX4_CMD_WRITE_MCG, MLX4_CMD_TIME_CLASS_A);
  59. }
  60. static int mlx4_GID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  61. u16 *hash, u8 op_mod)
  62. {
  63. u64 imm;
  64. int err;
  65. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, op_mod,
  66. MLX4_CMD_MGID_HASH, MLX4_CMD_TIME_CLASS_A);
  67. if (!err)
  68. *hash = imm;
  69. return err;
  70. }
  71. static struct mlx4_promisc_qp *get_promisc_qp(struct mlx4_dev *dev, u8 pf_num,
  72. enum mlx4_steer_type steer,
  73. u32 qpn)
  74. {
  75. struct mlx4_steer *s_steer = &mlx4_priv(dev)->steer[pf_num];
  76. struct mlx4_promisc_qp *pqp;
  77. list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
  78. if (pqp->qpn == qpn)
  79. return pqp;
  80. }
  81. /* not found */
  82. return NULL;
  83. }
  84. /*
  85. * Add new entry to steering data structure.
  86. * All promisc QPs should be added as well
  87. */
  88. static int new_steering_entry(struct mlx4_dev *dev, u8 vep_num, u8 port,
  89. enum mlx4_steer_type steer,
  90. unsigned int index, u32 qpn)
  91. {
  92. struct mlx4_steer *s_steer;
  93. struct mlx4_cmd_mailbox *mailbox;
  94. struct mlx4_mgm *mgm;
  95. u32 members_count;
  96. struct mlx4_steer_index *new_entry;
  97. struct mlx4_promisc_qp *pqp;
  98. struct mlx4_promisc_qp *dqp = NULL;
  99. u32 prot;
  100. int err;
  101. u8 pf_num;
  102. pf_num = (dev->caps.num_ports == 1) ? vep_num : (vep_num << 1) | (port - 1);
  103. s_steer = &mlx4_priv(dev)->steer[pf_num];
  104. new_entry = kzalloc(sizeof *new_entry, GFP_KERNEL);
  105. if (!new_entry)
  106. return -ENOMEM;
  107. INIT_LIST_HEAD(&new_entry->duplicates);
  108. new_entry->index = index;
  109. list_add_tail(&new_entry->list, &s_steer->steer_entries[steer]);
  110. /* If the given qpn is also a promisc qp,
  111. * it should be inserted to duplicates list
  112. */
  113. pqp = get_promisc_qp(dev, pf_num, steer, qpn);
  114. if (pqp) {
  115. dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
  116. if (!dqp) {
  117. err = -ENOMEM;
  118. goto out_alloc;
  119. }
  120. dqp->qpn = qpn;
  121. list_add_tail(&dqp->list, &new_entry->duplicates);
  122. }
  123. /* if no promisc qps for this vep, we are done */
  124. if (list_empty(&s_steer->promisc_qps[steer]))
  125. return 0;
  126. /* now need to add all the promisc qps to the new
  127. * steering entry, as they should also receive the packets
  128. * destined to this address */
  129. mailbox = mlx4_alloc_cmd_mailbox(dev);
  130. if (IS_ERR(mailbox)) {
  131. err = -ENOMEM;
  132. goto out_alloc;
  133. }
  134. mgm = mailbox->buf;
  135. err = mlx4_READ_ENTRY(dev, index, mailbox);
  136. if (err)
  137. goto out_mailbox;
  138. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  139. prot = be32_to_cpu(mgm->members_count) >> 30;
  140. list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
  141. /* don't add already existing qpn */
  142. if (pqp->qpn == qpn)
  143. continue;
  144. if (members_count == MLX4_QP_PER_MGM) {
  145. /* out of space */
  146. err = -ENOMEM;
  147. goto out_mailbox;
  148. }
  149. /* add the qpn */
  150. mgm->qp[members_count++] = cpu_to_be32(pqp->qpn & MGM_QPN_MASK);
  151. }
  152. /* update the qps count and update the entry with all the promisc qps*/
  153. mgm->members_count = cpu_to_be32(members_count | (prot << 30));
  154. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  155. out_mailbox:
  156. mlx4_free_cmd_mailbox(dev, mailbox);
  157. if (!err)
  158. return 0;
  159. out_alloc:
  160. if (dqp) {
  161. list_del(&dqp->list);
  162. kfree(dqp);
  163. }
  164. list_del(&new_entry->list);
  165. kfree(new_entry);
  166. return err;
  167. }
  168. /* update the data structures with existing steering entry */
  169. static int existing_steering_entry(struct mlx4_dev *dev, u8 vep_num, u8 port,
  170. enum mlx4_steer_type steer,
  171. unsigned int index, u32 qpn)
  172. {
  173. struct mlx4_steer *s_steer;
  174. struct mlx4_steer_index *tmp_entry, *entry = NULL;
  175. struct mlx4_promisc_qp *pqp;
  176. struct mlx4_promisc_qp *dqp;
  177. u8 pf_num;
  178. pf_num = (dev->caps.num_ports == 1) ? vep_num : (vep_num << 1) | (port - 1);
  179. s_steer = &mlx4_priv(dev)->steer[pf_num];
  180. pqp = get_promisc_qp(dev, pf_num, steer, qpn);
  181. if (!pqp)
  182. return 0; /* nothing to do */
  183. list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
  184. if (tmp_entry->index == index) {
  185. entry = tmp_entry;
  186. break;
  187. }
  188. }
  189. if (unlikely(!entry)) {
  190. mlx4_warn(dev, "Steering entry at index %x is not registered\n", index);
  191. return -EINVAL;
  192. }
  193. /* the given qpn is listed as a promisc qpn
  194. * we need to add it as a duplicate to this entry
  195. * for future references */
  196. list_for_each_entry(dqp, &entry->duplicates, list) {
  197. if (qpn == dqp->qpn)
  198. return 0; /* qp is already duplicated */
  199. }
  200. /* add the qp as a duplicate on this index */
  201. dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
  202. if (!dqp)
  203. return -ENOMEM;
  204. dqp->qpn = qpn;
  205. list_add_tail(&dqp->list, &entry->duplicates);
  206. return 0;
  207. }
  208. /* Check whether a qpn is a duplicate on steering entry
  209. * If so, it should not be removed from mgm */
  210. static bool check_duplicate_entry(struct mlx4_dev *dev, u8 vep_num, u8 port,
  211. enum mlx4_steer_type steer,
  212. unsigned int index, u32 qpn)
  213. {
  214. struct mlx4_steer *s_steer;
  215. struct mlx4_steer_index *tmp_entry, *entry = NULL;
  216. struct mlx4_promisc_qp *dqp, *tmp_dqp;
  217. u8 pf_num;
  218. pf_num = (dev->caps.num_ports == 1) ? vep_num : (vep_num << 1) | (port - 1);
  219. s_steer = &mlx4_priv(dev)->steer[pf_num];
  220. /* if qp is not promisc, it cannot be duplicated */
  221. if (!get_promisc_qp(dev, pf_num, steer, qpn))
  222. return false;
  223. /* The qp is promisc qp so it is a duplicate on this index
  224. * Find the index entry, and remove the duplicate */
  225. list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
  226. if (tmp_entry->index == index) {
  227. entry = tmp_entry;
  228. break;
  229. }
  230. }
  231. if (unlikely(!entry)) {
  232. mlx4_warn(dev, "Steering entry for index %x is not registered\n", index);
  233. return false;
  234. }
  235. list_for_each_entry_safe(dqp, tmp_dqp, &entry->duplicates, list) {
  236. if (dqp->qpn == qpn) {
  237. list_del(&dqp->list);
  238. kfree(dqp);
  239. }
  240. }
  241. return true;
  242. }
  243. /* I a steering entry contains only promisc QPs, it can be removed. */
  244. static bool can_remove_steering_entry(struct mlx4_dev *dev, u8 vep_num, u8 port,
  245. enum mlx4_steer_type steer,
  246. unsigned int index, u32 tqpn)
  247. {
  248. struct mlx4_steer *s_steer;
  249. struct mlx4_cmd_mailbox *mailbox;
  250. struct mlx4_mgm *mgm;
  251. struct mlx4_steer_index *entry = NULL, *tmp_entry;
  252. u32 qpn;
  253. u32 members_count;
  254. bool ret = false;
  255. int i;
  256. u8 pf_num;
  257. pf_num = (dev->caps.num_ports == 1) ? vep_num : (vep_num << 1) | (port - 1);
  258. s_steer = &mlx4_priv(dev)->steer[pf_num];
  259. mailbox = mlx4_alloc_cmd_mailbox(dev);
  260. if (IS_ERR(mailbox))
  261. return false;
  262. mgm = mailbox->buf;
  263. if (mlx4_READ_ENTRY(dev, index, mailbox))
  264. goto out;
  265. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  266. for (i = 0; i < members_count; i++) {
  267. qpn = be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK;
  268. if (!get_promisc_qp(dev, pf_num, steer, qpn) && qpn != tqpn) {
  269. /* the qp is not promisc, the entry can't be removed */
  270. goto out;
  271. }
  272. }
  273. /* All the qps currently registered for this entry are promiscuous,
  274. * Checking for duplicates */
  275. ret = true;
  276. list_for_each_entry_safe(entry, tmp_entry, &s_steer->steer_entries[steer], list) {
  277. if (entry->index == index) {
  278. if (list_empty(&entry->duplicates)) {
  279. list_del(&entry->list);
  280. kfree(entry);
  281. } else {
  282. /* This entry contains duplicates so it shouldn't be removed */
  283. ret = false;
  284. goto out;
  285. }
  286. }
  287. }
  288. out:
  289. mlx4_free_cmd_mailbox(dev, mailbox);
  290. return ret;
  291. }
  292. static int add_promisc_qp(struct mlx4_dev *dev, u8 vep_num, u8 port,
  293. enum mlx4_steer_type steer, u32 qpn)
  294. {
  295. struct mlx4_steer *s_steer;
  296. struct mlx4_cmd_mailbox *mailbox;
  297. struct mlx4_mgm *mgm;
  298. struct mlx4_steer_index *entry;
  299. struct mlx4_promisc_qp *pqp;
  300. struct mlx4_promisc_qp *dqp;
  301. u32 members_count;
  302. u32 prot;
  303. int i;
  304. bool found;
  305. int last_index;
  306. int err;
  307. u8 pf_num;
  308. struct mlx4_priv *priv = mlx4_priv(dev);
  309. pf_num = (dev->caps.num_ports == 1) ? vep_num : (vep_num << 1) | (port - 1);
  310. s_steer = &mlx4_priv(dev)->steer[pf_num];
  311. mutex_lock(&priv->mcg_table.mutex);
  312. if (get_promisc_qp(dev, pf_num, steer, qpn)) {
  313. err = 0; /* Noting to do, already exists */
  314. goto out_mutex;
  315. }
  316. pqp = kmalloc(sizeof *pqp, GFP_KERNEL);
  317. if (!pqp) {
  318. err = -ENOMEM;
  319. goto out_mutex;
  320. }
  321. pqp->qpn = qpn;
  322. mailbox = mlx4_alloc_cmd_mailbox(dev);
  323. if (IS_ERR(mailbox)) {
  324. err = -ENOMEM;
  325. goto out_alloc;
  326. }
  327. mgm = mailbox->buf;
  328. /* the promisc qp needs to be added for each one of the steering
  329. * entries, if it already exists, needs to be added as a duplicate
  330. * for this entry */
  331. list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
  332. err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
  333. if (err)
  334. goto out_mailbox;
  335. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  336. prot = be32_to_cpu(mgm->members_count) >> 30;
  337. found = false;
  338. for (i = 0; i < members_count; i++) {
  339. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn) {
  340. /* Entry already exists, add to duplicates */
  341. dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
  342. if (!dqp)
  343. goto out_mailbox;
  344. dqp->qpn = qpn;
  345. list_add_tail(&dqp->list, &entry->duplicates);
  346. found = true;
  347. }
  348. }
  349. if (!found) {
  350. /* Need to add the qpn to mgm */
  351. if (members_count == MLX4_QP_PER_MGM) {
  352. /* entry is full */
  353. err = -ENOMEM;
  354. goto out_mailbox;
  355. }
  356. mgm->qp[members_count++] = cpu_to_be32(qpn & MGM_QPN_MASK);
  357. mgm->members_count = cpu_to_be32(members_count | (prot << 30));
  358. err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
  359. if (err)
  360. goto out_mailbox;
  361. }
  362. last_index = entry->index;
  363. }
  364. /* add the new qpn to list of promisc qps */
  365. list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
  366. /* now need to add all the promisc qps to default entry */
  367. memset(mgm, 0, sizeof *mgm);
  368. members_count = 0;
  369. list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
  370. mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
  371. mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
  372. err = mlx4_WRITE_PROMISC(dev, vep_num, port, steer, mailbox);
  373. if (err)
  374. goto out_list;
  375. mlx4_free_cmd_mailbox(dev, mailbox);
  376. mutex_unlock(&priv->mcg_table.mutex);
  377. return 0;
  378. out_list:
  379. list_del(&pqp->list);
  380. out_mailbox:
  381. mlx4_free_cmd_mailbox(dev, mailbox);
  382. out_alloc:
  383. kfree(pqp);
  384. out_mutex:
  385. mutex_unlock(&priv->mcg_table.mutex);
  386. return err;
  387. }
  388. static int remove_promisc_qp(struct mlx4_dev *dev, u8 vep_num, u8 port,
  389. enum mlx4_steer_type steer, u32 qpn)
  390. {
  391. struct mlx4_priv *priv = mlx4_priv(dev);
  392. struct mlx4_steer *s_steer;
  393. struct mlx4_cmd_mailbox *mailbox;
  394. struct mlx4_mgm *mgm;
  395. struct mlx4_steer_index *entry;
  396. struct mlx4_promisc_qp *pqp;
  397. struct mlx4_promisc_qp *dqp;
  398. u32 members_count;
  399. bool found;
  400. bool back_to_list = false;
  401. int loc, i;
  402. int err;
  403. u8 pf_num;
  404. pf_num = (dev->caps.num_ports == 1) ? vep_num : (vep_num << 1) | (port - 1);
  405. s_steer = &mlx4_priv(dev)->steer[pf_num];
  406. mutex_lock(&priv->mcg_table.mutex);
  407. pqp = get_promisc_qp(dev, pf_num, steer, qpn);
  408. if (unlikely(!pqp)) {
  409. mlx4_warn(dev, "QP %x is not promiscuous QP\n", qpn);
  410. /* nothing to do */
  411. err = 0;
  412. goto out_mutex;
  413. }
  414. /*remove from list of promisc qps */
  415. list_del(&pqp->list);
  416. /* set the default entry not to include the removed one */
  417. mailbox = mlx4_alloc_cmd_mailbox(dev);
  418. if (IS_ERR(mailbox)) {
  419. err = -ENOMEM;
  420. back_to_list = true;
  421. goto out_list;
  422. }
  423. mgm = mailbox->buf;
  424. members_count = 0;
  425. list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
  426. mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
  427. mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
  428. err = mlx4_WRITE_PROMISC(dev, vep_num, port, steer, mailbox);
  429. if (err)
  430. goto out_mailbox;
  431. /* remove the qp from all the steering entries*/
  432. list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
  433. found = false;
  434. list_for_each_entry(dqp, &entry->duplicates, list) {
  435. if (dqp->qpn == qpn) {
  436. found = true;
  437. break;
  438. }
  439. }
  440. if (found) {
  441. /* a duplicate, no need to change the mgm,
  442. * only update the duplicates list */
  443. list_del(&dqp->list);
  444. kfree(dqp);
  445. } else {
  446. err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
  447. if (err)
  448. goto out_mailbox;
  449. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  450. for (loc = -1, i = 0; i < members_count; ++i)
  451. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn)
  452. loc = i;
  453. mgm->members_count = cpu_to_be32(--members_count |
  454. (MLX4_PROT_ETH << 30));
  455. mgm->qp[loc] = mgm->qp[i - 1];
  456. mgm->qp[i - 1] = 0;
  457. err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
  458. if (err)
  459. goto out_mailbox;
  460. }
  461. }
  462. out_mailbox:
  463. mlx4_free_cmd_mailbox(dev, mailbox);
  464. out_list:
  465. if (back_to_list)
  466. list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
  467. else
  468. kfree(pqp);
  469. out_mutex:
  470. mutex_unlock(&priv->mcg_table.mutex);
  471. return err;
  472. }
  473. /*
  474. * Caller must hold MCG table semaphore. gid and mgm parameters must
  475. * be properly aligned for command interface.
  476. *
  477. * Returns 0 unless a firmware command error occurs.
  478. *
  479. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  480. * and *mgm holds MGM entry.
  481. *
  482. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  483. * previous entry in hash chain and *mgm holds AMGM entry.
  484. *
  485. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  486. * entry in hash chain and *mgm holds end of hash chain.
  487. */
  488. static int find_entry(struct mlx4_dev *dev, u8 port,
  489. u8 *gid, enum mlx4_protocol prot,
  490. enum mlx4_steer_type steer,
  491. struct mlx4_cmd_mailbox *mgm_mailbox,
  492. u16 *hash, int *prev, int *index)
  493. {
  494. struct mlx4_cmd_mailbox *mailbox;
  495. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  496. u8 *mgid;
  497. int err;
  498. u8 op_mod = (prot == MLX4_PROT_ETH) ? !!(dev->caps.vep_mc_steering) : 0;
  499. mailbox = mlx4_alloc_cmd_mailbox(dev);
  500. if (IS_ERR(mailbox))
  501. return -ENOMEM;
  502. mgid = mailbox->buf;
  503. memcpy(mgid, gid, 16);
  504. err = mlx4_GID_HASH(dev, mailbox, hash, op_mod);
  505. mlx4_free_cmd_mailbox(dev, mailbox);
  506. if (err)
  507. return err;
  508. if (0)
  509. mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
  510. *index = *hash;
  511. *prev = -1;
  512. do {
  513. err = mlx4_READ_ENTRY(dev, *index, mgm_mailbox);
  514. if (err)
  515. return err;
  516. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  517. if (*index != *hash) {
  518. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  519. err = -EINVAL;
  520. }
  521. return err;
  522. }
  523. if (!memcmp(mgm->gid, gid, 16) &&
  524. be32_to_cpu(mgm->members_count) >> 30 == prot)
  525. return err;
  526. *prev = *index;
  527. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  528. } while (*index);
  529. *index = -1;
  530. return err;
  531. }
  532. int mlx4_qp_attach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  533. int block_mcast_loopback, enum mlx4_protocol prot,
  534. enum mlx4_steer_type steer)
  535. {
  536. struct mlx4_priv *priv = mlx4_priv(dev);
  537. struct mlx4_cmd_mailbox *mailbox;
  538. struct mlx4_mgm *mgm;
  539. u32 members_count;
  540. u16 hash;
  541. int index, prev;
  542. int link = 0;
  543. int i;
  544. int err;
  545. u8 port = gid[5];
  546. u8 new_entry = 0;
  547. mailbox = mlx4_alloc_cmd_mailbox(dev);
  548. if (IS_ERR(mailbox))
  549. return PTR_ERR(mailbox);
  550. mgm = mailbox->buf;
  551. mutex_lock(&priv->mcg_table.mutex);
  552. err = find_entry(dev, port, gid, prot, steer,
  553. mailbox, &hash, &prev, &index);
  554. if (err)
  555. goto out;
  556. if (index != -1) {
  557. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  558. new_entry = 1;
  559. memcpy(mgm->gid, gid, 16);
  560. }
  561. } else {
  562. link = 1;
  563. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  564. if (index == -1) {
  565. mlx4_err(dev, "No AMGM entries left\n");
  566. err = -ENOMEM;
  567. goto out;
  568. }
  569. index += dev->caps.num_mgms;
  570. memset(mgm, 0, sizeof *mgm);
  571. memcpy(mgm->gid, gid, 16);
  572. }
  573. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  574. if (members_count == MLX4_QP_PER_MGM) {
  575. mlx4_err(dev, "MGM at index %x is full.\n", index);
  576. err = -ENOMEM;
  577. goto out;
  578. }
  579. for (i = 0; i < members_count; ++i)
  580. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  581. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  582. err = 0;
  583. goto out;
  584. }
  585. if (block_mcast_loopback)
  586. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  587. (1U << MGM_BLCK_LB_BIT));
  588. else
  589. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  590. mgm->members_count = cpu_to_be32(members_count | (u32) prot << 30);
  591. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  592. if (err)
  593. goto out;
  594. if (!link)
  595. goto out;
  596. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  597. if (err)
  598. goto out;
  599. mgm->next_gid_index = cpu_to_be32(index << 6);
  600. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  601. if (err)
  602. goto out;
  603. out:
  604. if (prot == MLX4_PROT_ETH) {
  605. /* manage the steering entry for promisc mode */
  606. if (new_entry)
  607. new_steering_entry(dev, 0, port, steer, index, qp->qpn);
  608. else
  609. existing_steering_entry(dev, 0, port, steer,
  610. index, qp->qpn);
  611. }
  612. if (err && link && index != -1) {
  613. if (index < dev->caps.num_mgms)
  614. mlx4_warn(dev, "Got AMGM index %d < %d",
  615. index, dev->caps.num_mgms);
  616. else
  617. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  618. index - dev->caps.num_mgms);
  619. }
  620. mutex_unlock(&priv->mcg_table.mutex);
  621. mlx4_free_cmd_mailbox(dev, mailbox);
  622. return err;
  623. }
  624. int mlx4_qp_detach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  625. enum mlx4_protocol prot, enum mlx4_steer_type steer)
  626. {
  627. struct mlx4_priv *priv = mlx4_priv(dev);
  628. struct mlx4_cmd_mailbox *mailbox;
  629. struct mlx4_mgm *mgm;
  630. u32 members_count;
  631. u16 hash;
  632. int prev, index;
  633. int i, loc;
  634. int err;
  635. u8 port = gid[5];
  636. bool removed_entry = false;
  637. mailbox = mlx4_alloc_cmd_mailbox(dev);
  638. if (IS_ERR(mailbox))
  639. return PTR_ERR(mailbox);
  640. mgm = mailbox->buf;
  641. mutex_lock(&priv->mcg_table.mutex);
  642. err = find_entry(dev, port, gid, prot, steer,
  643. mailbox, &hash, &prev, &index);
  644. if (err)
  645. goto out;
  646. if (index == -1) {
  647. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  648. err = -EINVAL;
  649. goto out;
  650. }
  651. /* if this pq is also a promisc qp, it shouldn't be removed */
  652. if (prot == MLX4_PROT_ETH &&
  653. check_duplicate_entry(dev, 0, port, steer, index, qp->qpn))
  654. goto out;
  655. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  656. for (loc = -1, i = 0; i < members_count; ++i)
  657. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  658. loc = i;
  659. if (loc == -1) {
  660. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  661. err = -EINVAL;
  662. goto out;
  663. }
  664. mgm->members_count = cpu_to_be32(--members_count | (u32) prot << 30);
  665. mgm->qp[loc] = mgm->qp[i - 1];
  666. mgm->qp[i - 1] = 0;
  667. if (prot == MLX4_PROT_ETH)
  668. removed_entry = can_remove_steering_entry(dev, 0, port, steer, index, qp->qpn);
  669. if (i != 1 && (prot != MLX4_PROT_ETH || !removed_entry)) {
  670. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  671. goto out;
  672. }
  673. /* We are going to delete the entry, members count should be 0 */
  674. mgm->members_count = cpu_to_be32((u32) prot << 30);
  675. if (prev == -1) {
  676. /* Remove entry from MGM */
  677. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  678. if (amgm_index) {
  679. err = mlx4_READ_ENTRY(dev, amgm_index, mailbox);
  680. if (err)
  681. goto out;
  682. } else
  683. memset(mgm->gid, 0, 16);
  684. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  685. if (err)
  686. goto out;
  687. if (amgm_index) {
  688. if (amgm_index < dev->caps.num_mgms)
  689. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  690. index, amgm_index, dev->caps.num_mgms);
  691. else
  692. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  693. amgm_index - dev->caps.num_mgms);
  694. }
  695. } else {
  696. /* Remove entry from AMGM */
  697. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  698. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  699. if (err)
  700. goto out;
  701. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  702. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  703. if (err)
  704. goto out;
  705. if (index < dev->caps.num_mgms)
  706. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  707. prev, index, dev->caps.num_mgms);
  708. else
  709. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  710. index - dev->caps.num_mgms);
  711. }
  712. out:
  713. mutex_unlock(&priv->mcg_table.mutex);
  714. mlx4_free_cmd_mailbox(dev, mailbox);
  715. return err;
  716. }
  717. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  718. int block_mcast_loopback, enum mlx4_protocol prot)
  719. {
  720. enum mlx4_steer_type steer;
  721. steer = (is_valid_ether_addr(&gid[10])) ? MLX4_UC_STEER : MLX4_MC_STEER;
  722. if (prot == MLX4_PROT_ETH && !dev->caps.vep_mc_steering)
  723. return 0;
  724. if (prot == MLX4_PROT_ETH)
  725. gid[7] |= (steer << 1);
  726. return mlx4_qp_attach_common(dev, qp, gid,
  727. block_mcast_loopback, prot,
  728. steer);
  729. }
  730. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  731. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  732. enum mlx4_protocol prot)
  733. {
  734. enum mlx4_steer_type steer;
  735. steer = (is_valid_ether_addr(&gid[10])) ? MLX4_UC_STEER : MLX4_MC_STEER;
  736. if (prot == MLX4_PROT_ETH && !dev->caps.vep_mc_steering)
  737. return 0;
  738. if (prot == MLX4_PROT_ETH) {
  739. gid[7] |= (steer << 1);
  740. }
  741. return mlx4_qp_detach_common(dev, qp, gid, prot, steer);
  742. }
  743. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  744. int mlx4_multicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
  745. {
  746. if (!dev->caps.vep_mc_steering)
  747. return 0;
  748. return add_promisc_qp(dev, 0, port, MLX4_MC_STEER, qpn);
  749. }
  750. EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_add);
  751. int mlx4_multicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
  752. {
  753. if (!dev->caps.vep_mc_steering)
  754. return 0;
  755. return remove_promisc_qp(dev, 0, port, MLX4_MC_STEER, qpn);
  756. }
  757. EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_remove);
  758. int mlx4_unicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
  759. {
  760. if (!dev->caps.vep_mc_steering)
  761. return 0;
  762. return add_promisc_qp(dev, 0, port, MLX4_UC_STEER, qpn);
  763. }
  764. EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_add);
  765. int mlx4_unicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
  766. {
  767. if (!dev->caps.vep_mc_steering)
  768. return 0;
  769. return remove_promisc_qp(dev, 0, port, MLX4_UC_STEER, qpn);
  770. }
  771. EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_remove);
  772. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  773. {
  774. struct mlx4_priv *priv = mlx4_priv(dev);
  775. int err;
  776. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  777. dev->caps.num_amgms - 1, 0, 0);
  778. if (err)
  779. return err;
  780. mutex_init(&priv->mcg_table.mutex);
  781. return 0;
  782. }
  783. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  784. {
  785. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  786. }