mcg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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;
  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 refernce */
  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. kfree(pqp);
  417. /* set the default entry not to include the removed one */
  418. mailbox = mlx4_alloc_cmd_mailbox(dev);
  419. if (IS_ERR(mailbox)) {
  420. err = -ENOMEM;
  421. back_to_list = true;
  422. goto out_list;
  423. }
  424. mgm = mailbox->buf;
  425. members_count = 0;
  426. list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
  427. mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
  428. mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
  429. err = mlx4_WRITE_PROMISC(dev, vep_num, port, steer, mailbox);
  430. if (err)
  431. goto out_mailbox;
  432. /* remove the qp from all the steering entries*/
  433. list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
  434. found = false;
  435. list_for_each_entry(dqp, &entry->duplicates, list) {
  436. if (dqp->qpn == qpn) {
  437. found = true;
  438. break;
  439. }
  440. }
  441. if (found) {
  442. /* a duplicate, no need to change the mgm,
  443. * only update the duplicates list */
  444. list_del(&dqp->list);
  445. kfree(dqp);
  446. } else {
  447. err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
  448. if (err)
  449. goto out_mailbox;
  450. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  451. for (loc = -1, i = 0; i < members_count; ++i)
  452. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn)
  453. loc = i;
  454. mgm->members_count = cpu_to_be32(--members_count |
  455. (MLX4_PROT_ETH << 30));
  456. mgm->qp[loc] = mgm->qp[i - 1];
  457. mgm->qp[i - 1] = 0;
  458. err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
  459. if (err)
  460. goto out_mailbox;
  461. }
  462. }
  463. out_mailbox:
  464. mlx4_free_cmd_mailbox(dev, mailbox);
  465. out_list:
  466. if (back_to_list)
  467. list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
  468. out_mutex:
  469. mutex_unlock(&priv->mcg_table.mutex);
  470. return err;
  471. }
  472. /*
  473. * Caller must hold MCG table semaphore. gid and mgm parameters must
  474. * be properly aligned for command interface.
  475. *
  476. * Returns 0 unless a firmware command error occurs.
  477. *
  478. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  479. * and *mgm holds MGM entry.
  480. *
  481. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  482. * previous entry in hash chain and *mgm holds AMGM entry.
  483. *
  484. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  485. * entry in hash chain and *mgm holds end of hash chain.
  486. */
  487. static int find_entry(struct mlx4_dev *dev, u8 port,
  488. u8 *gid, enum mlx4_protocol prot,
  489. enum mlx4_steer_type steer,
  490. struct mlx4_cmd_mailbox *mgm_mailbox,
  491. u16 *hash, int *prev, int *index)
  492. {
  493. struct mlx4_cmd_mailbox *mailbox;
  494. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  495. u8 *mgid;
  496. int err;
  497. u8 op_mod = (prot == MLX4_PROT_ETH) ? !!(dev->caps.vep_mc_steering) : 0;
  498. mailbox = mlx4_alloc_cmd_mailbox(dev);
  499. if (IS_ERR(mailbox))
  500. return -ENOMEM;
  501. mgid = mailbox->buf;
  502. memcpy(mgid, gid, 16);
  503. err = mlx4_GID_HASH(dev, mailbox, hash, op_mod);
  504. mlx4_free_cmd_mailbox(dev, mailbox);
  505. if (err)
  506. return err;
  507. if (0)
  508. mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
  509. *index = *hash;
  510. *prev = -1;
  511. do {
  512. err = mlx4_READ_ENTRY(dev, *index, mgm_mailbox);
  513. if (err)
  514. return err;
  515. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  516. if (*index != *hash) {
  517. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  518. err = -EINVAL;
  519. }
  520. return err;
  521. }
  522. if (!memcmp(mgm->gid, gid, 16) &&
  523. be32_to_cpu(mgm->members_count) >> 30 == prot)
  524. return err;
  525. *prev = *index;
  526. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  527. } while (*index);
  528. *index = -1;
  529. return err;
  530. }
  531. int mlx4_qp_attach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  532. int block_mcast_loopback, enum mlx4_protocol prot,
  533. enum mlx4_steer_type steer)
  534. {
  535. struct mlx4_priv *priv = mlx4_priv(dev);
  536. struct mlx4_cmd_mailbox *mailbox;
  537. struct mlx4_mgm *mgm;
  538. u32 members_count;
  539. u16 hash;
  540. int index, prev;
  541. int link = 0;
  542. int i;
  543. int err;
  544. u8 port = gid[5];
  545. u8 new_entry = 0;
  546. mailbox = mlx4_alloc_cmd_mailbox(dev);
  547. if (IS_ERR(mailbox))
  548. return PTR_ERR(mailbox);
  549. mgm = mailbox->buf;
  550. mutex_lock(&priv->mcg_table.mutex);
  551. err = find_entry(dev, port, gid, prot, steer,
  552. mailbox, &hash, &prev, &index);
  553. if (err)
  554. goto out;
  555. if (index != -1) {
  556. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  557. new_entry = 1;
  558. memcpy(mgm->gid, gid, 16);
  559. }
  560. } else {
  561. link = 1;
  562. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  563. if (index == -1) {
  564. mlx4_err(dev, "No AMGM entries left\n");
  565. err = -ENOMEM;
  566. goto out;
  567. }
  568. index += dev->caps.num_mgms;
  569. memset(mgm, 0, sizeof *mgm);
  570. memcpy(mgm->gid, gid, 16);
  571. }
  572. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  573. if (members_count == MLX4_QP_PER_MGM) {
  574. mlx4_err(dev, "MGM at index %x is full.\n", index);
  575. err = -ENOMEM;
  576. goto out;
  577. }
  578. for (i = 0; i < members_count; ++i)
  579. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  580. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  581. err = 0;
  582. goto out;
  583. }
  584. if (block_mcast_loopback)
  585. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  586. (1U << MGM_BLCK_LB_BIT));
  587. else
  588. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  589. mgm->members_count = cpu_to_be32(members_count | (u32) prot << 30);
  590. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  591. if (err)
  592. goto out;
  593. if (!link)
  594. goto out;
  595. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  596. if (err)
  597. goto out;
  598. mgm->next_gid_index = cpu_to_be32(index << 6);
  599. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  600. if (err)
  601. goto out;
  602. out:
  603. if (prot == MLX4_PROT_ETH) {
  604. /* manage the steering entry for promisc mode */
  605. if (new_entry)
  606. new_steering_entry(dev, 0, port, steer, index, qp->qpn);
  607. else
  608. existing_steering_entry(dev, 0, port, steer,
  609. index, qp->qpn);
  610. }
  611. if (err && link && index != -1) {
  612. if (index < dev->caps.num_mgms)
  613. mlx4_warn(dev, "Got AMGM index %d < %d",
  614. index, dev->caps.num_mgms);
  615. else
  616. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  617. index - dev->caps.num_mgms);
  618. }
  619. mutex_unlock(&priv->mcg_table.mutex);
  620. mlx4_free_cmd_mailbox(dev, mailbox);
  621. return err;
  622. }
  623. int mlx4_qp_detach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  624. enum mlx4_protocol prot, enum mlx4_steer_type steer)
  625. {
  626. struct mlx4_priv *priv = mlx4_priv(dev);
  627. struct mlx4_cmd_mailbox *mailbox;
  628. struct mlx4_mgm *mgm;
  629. u32 members_count;
  630. u16 hash;
  631. int prev, index;
  632. int i, loc;
  633. int err;
  634. u8 port = gid[5];
  635. bool removed_entry = false;
  636. mailbox = mlx4_alloc_cmd_mailbox(dev);
  637. if (IS_ERR(mailbox))
  638. return PTR_ERR(mailbox);
  639. mgm = mailbox->buf;
  640. mutex_lock(&priv->mcg_table.mutex);
  641. err = find_entry(dev, port, gid, prot, steer,
  642. mailbox, &hash, &prev, &index);
  643. if (err)
  644. goto out;
  645. if (index == -1) {
  646. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  647. err = -EINVAL;
  648. goto out;
  649. }
  650. /* if this pq is also a promisc qp, it shouldn't be removed */
  651. if (prot == MLX4_PROT_ETH &&
  652. check_duplicate_entry(dev, 0, port, steer, index, qp->qpn))
  653. goto out;
  654. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  655. for (loc = -1, i = 0; i < members_count; ++i)
  656. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  657. loc = i;
  658. if (loc == -1) {
  659. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  660. err = -EINVAL;
  661. goto out;
  662. }
  663. mgm->members_count = cpu_to_be32(--members_count | (u32) prot << 30);
  664. mgm->qp[loc] = mgm->qp[i - 1];
  665. mgm->qp[i - 1] = 0;
  666. if (prot == MLX4_PROT_ETH)
  667. removed_entry = can_remove_steering_entry(dev, 0, port, steer, index, qp->qpn);
  668. if (i != 1 && (prot != MLX4_PROT_ETH || !removed_entry)) {
  669. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  670. goto out;
  671. }
  672. /* We are going to delete the entry, members count should be 0 */
  673. mgm->members_count = cpu_to_be32((u32) prot << 30);
  674. if (prev == -1) {
  675. /* Remove entry from MGM */
  676. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  677. if (amgm_index) {
  678. err = mlx4_READ_ENTRY(dev, amgm_index, mailbox);
  679. if (err)
  680. goto out;
  681. } else
  682. memset(mgm->gid, 0, 16);
  683. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  684. if (err)
  685. goto out;
  686. if (amgm_index) {
  687. if (amgm_index < dev->caps.num_mgms)
  688. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  689. index, amgm_index, dev->caps.num_mgms);
  690. else
  691. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  692. amgm_index - dev->caps.num_mgms);
  693. }
  694. } else {
  695. /* Remove entry from AMGM */
  696. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  697. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  698. if (err)
  699. goto out;
  700. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  701. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  702. if (err)
  703. goto out;
  704. if (index < dev->caps.num_mgms)
  705. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  706. prev, index, dev->caps.num_mgms);
  707. else
  708. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  709. index - dev->caps.num_mgms);
  710. }
  711. out:
  712. mutex_unlock(&priv->mcg_table.mutex);
  713. mlx4_free_cmd_mailbox(dev, mailbox);
  714. return err;
  715. }
  716. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  717. int block_mcast_loopback, enum mlx4_protocol prot)
  718. {
  719. enum mlx4_steer_type steer;
  720. steer = (is_valid_ether_addr(&gid[10])) ? MLX4_UC_STEER : MLX4_MC_STEER;
  721. if (prot == MLX4_PROT_ETH && !dev->caps.vep_mc_steering)
  722. return 0;
  723. if (prot == MLX4_PROT_ETH)
  724. gid[7] |= (steer << 1);
  725. return mlx4_qp_attach_common(dev, qp, gid,
  726. block_mcast_loopback, prot,
  727. steer);
  728. }
  729. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  730. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  731. enum mlx4_protocol prot)
  732. {
  733. enum mlx4_steer_type steer;
  734. steer = (is_valid_ether_addr(&gid[10])) ? MLX4_UC_STEER : MLX4_MC_STEER;
  735. if (prot == MLX4_PROT_ETH && !dev->caps.vep_mc_steering)
  736. return 0;
  737. if (prot == MLX4_PROT_ETH) {
  738. gid[7] |= (steer << 1);
  739. }
  740. return mlx4_qp_detach_common(dev, qp, gid, prot, steer);
  741. }
  742. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  743. int mlx4_multicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
  744. {
  745. if (!dev->caps.vep_mc_steering)
  746. return 0;
  747. return add_promisc_qp(dev, 0, port, MLX4_MC_STEER, qpn);
  748. }
  749. EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_add);
  750. int mlx4_multicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
  751. {
  752. if (!dev->caps.vep_mc_steering)
  753. return 0;
  754. return remove_promisc_qp(dev, 0, port, MLX4_MC_STEER, qpn);
  755. }
  756. EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_remove);
  757. int mlx4_unicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
  758. {
  759. if (!dev->caps.vep_mc_steering)
  760. return 0;
  761. return add_promisc_qp(dev, 0, port, MLX4_UC_STEER, qpn);
  762. }
  763. EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_add);
  764. int mlx4_unicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
  765. {
  766. if (!dev->caps.vep_mc_steering)
  767. return 0;
  768. return remove_promisc_qp(dev, 0, port, MLX4_UC_STEER, qpn);
  769. }
  770. EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_remove);
  771. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  772. {
  773. struct mlx4_priv *priv = mlx4_priv(dev);
  774. int err;
  775. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  776. dev->caps.num_amgms - 1, 0, 0);
  777. if (err)
  778. return err;
  779. mutex_init(&priv->mcg_table.mutex);
  780. return 0;
  781. }
  782. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  783. {
  784. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  785. }