mcg.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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 <linux/export.h>
  37. #include "mlx4.h"
  38. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  39. int mlx4_get_mgm_entry_size(struct mlx4_dev *dev)
  40. {
  41. return 1 << dev->oper_log_mgm_entry_size;
  42. }
  43. int mlx4_get_qp_per_mgm(struct mlx4_dev *dev)
  44. {
  45. return 4 * (mlx4_get_mgm_entry_size(dev) / 16 - 2);
  46. }
  47. static int mlx4_QP_FLOW_STEERING_ATTACH(struct mlx4_dev *dev,
  48. struct mlx4_cmd_mailbox *mailbox,
  49. u32 size,
  50. u64 *reg_id)
  51. {
  52. u64 imm;
  53. int err = 0;
  54. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, size, 0,
  55. MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
  56. MLX4_CMD_NATIVE);
  57. if (err)
  58. return err;
  59. *reg_id = imm;
  60. return err;
  61. }
  62. static int mlx4_QP_FLOW_STEERING_DETACH(struct mlx4_dev *dev, u64 regid)
  63. {
  64. int err = 0;
  65. err = mlx4_cmd(dev, regid, 0, 0,
  66. MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
  67. MLX4_CMD_NATIVE);
  68. return err;
  69. }
  70. static int mlx4_READ_ENTRY(struct mlx4_dev *dev, int index,
  71. struct mlx4_cmd_mailbox *mailbox)
  72. {
  73. return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
  74. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  75. }
  76. static int mlx4_WRITE_ENTRY(struct mlx4_dev *dev, int index,
  77. struct mlx4_cmd_mailbox *mailbox)
  78. {
  79. return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
  80. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  81. }
  82. static int mlx4_WRITE_PROMISC(struct mlx4_dev *dev, u8 port, u8 steer,
  83. struct mlx4_cmd_mailbox *mailbox)
  84. {
  85. u32 in_mod;
  86. in_mod = (u32) port << 16 | steer << 1;
  87. return mlx4_cmd(dev, mailbox->dma, in_mod, 0x1,
  88. MLX4_CMD_WRITE_MCG, MLX4_CMD_TIME_CLASS_A,
  89. MLX4_CMD_NATIVE);
  90. }
  91. static int mlx4_GID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  92. u16 *hash, u8 op_mod)
  93. {
  94. u64 imm;
  95. int err;
  96. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, op_mod,
  97. MLX4_CMD_MGID_HASH, MLX4_CMD_TIME_CLASS_A,
  98. MLX4_CMD_NATIVE);
  99. if (!err)
  100. *hash = imm;
  101. return err;
  102. }
  103. static struct mlx4_promisc_qp *get_promisc_qp(struct mlx4_dev *dev, u8 port,
  104. enum mlx4_steer_type steer,
  105. u32 qpn)
  106. {
  107. struct mlx4_steer *s_steer = &mlx4_priv(dev)->steer[port - 1];
  108. struct mlx4_promisc_qp *pqp;
  109. list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
  110. if (pqp->qpn == qpn)
  111. return pqp;
  112. }
  113. /* not found */
  114. return NULL;
  115. }
  116. /*
  117. * Add new entry to steering data structure.
  118. * All promisc QPs should be added as well
  119. */
  120. static int new_steering_entry(struct mlx4_dev *dev, u8 port,
  121. enum mlx4_steer_type steer,
  122. unsigned int index, u32 qpn)
  123. {
  124. struct mlx4_steer *s_steer;
  125. struct mlx4_cmd_mailbox *mailbox;
  126. struct mlx4_mgm *mgm;
  127. u32 members_count;
  128. struct mlx4_steer_index *new_entry;
  129. struct mlx4_promisc_qp *pqp;
  130. struct mlx4_promisc_qp *dqp = NULL;
  131. u32 prot;
  132. int err;
  133. s_steer = &mlx4_priv(dev)->steer[port - 1];
  134. new_entry = kzalloc(sizeof *new_entry, GFP_KERNEL);
  135. if (!new_entry)
  136. return -ENOMEM;
  137. INIT_LIST_HEAD(&new_entry->duplicates);
  138. new_entry->index = index;
  139. list_add_tail(&new_entry->list, &s_steer->steer_entries[steer]);
  140. /* If the given qpn is also a promisc qp,
  141. * it should be inserted to duplicates list
  142. */
  143. pqp = get_promisc_qp(dev, port, steer, qpn);
  144. if (pqp) {
  145. dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
  146. if (!dqp) {
  147. err = -ENOMEM;
  148. goto out_alloc;
  149. }
  150. dqp->qpn = qpn;
  151. list_add_tail(&dqp->list, &new_entry->duplicates);
  152. }
  153. /* if no promisc qps for this vep, we are done */
  154. if (list_empty(&s_steer->promisc_qps[steer]))
  155. return 0;
  156. /* now need to add all the promisc qps to the new
  157. * steering entry, as they should also receive the packets
  158. * destined to this address */
  159. mailbox = mlx4_alloc_cmd_mailbox(dev);
  160. if (IS_ERR(mailbox)) {
  161. err = -ENOMEM;
  162. goto out_alloc;
  163. }
  164. mgm = mailbox->buf;
  165. err = mlx4_READ_ENTRY(dev, index, mailbox);
  166. if (err)
  167. goto out_mailbox;
  168. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  169. prot = be32_to_cpu(mgm->members_count) >> 30;
  170. list_for_each_entry(pqp, &s_steer->promisc_qps[steer], list) {
  171. /* don't add already existing qpn */
  172. if (pqp->qpn == qpn)
  173. continue;
  174. if (members_count == dev->caps.num_qp_per_mgm) {
  175. /* out of space */
  176. err = -ENOMEM;
  177. goto out_mailbox;
  178. }
  179. /* add the qpn */
  180. mgm->qp[members_count++] = cpu_to_be32(pqp->qpn & MGM_QPN_MASK);
  181. }
  182. /* update the qps count and update the entry with all the promisc qps*/
  183. mgm->members_count = cpu_to_be32(members_count | (prot << 30));
  184. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  185. out_mailbox:
  186. mlx4_free_cmd_mailbox(dev, mailbox);
  187. if (!err)
  188. return 0;
  189. out_alloc:
  190. if (dqp) {
  191. list_del(&dqp->list);
  192. kfree(dqp);
  193. }
  194. list_del(&new_entry->list);
  195. kfree(new_entry);
  196. return err;
  197. }
  198. /* update the data structures with existing steering entry */
  199. static int existing_steering_entry(struct mlx4_dev *dev, u8 port,
  200. enum mlx4_steer_type steer,
  201. unsigned int index, u32 qpn)
  202. {
  203. struct mlx4_steer *s_steer;
  204. struct mlx4_steer_index *tmp_entry, *entry = NULL;
  205. struct mlx4_promisc_qp *pqp;
  206. struct mlx4_promisc_qp *dqp;
  207. s_steer = &mlx4_priv(dev)->steer[port - 1];
  208. pqp = get_promisc_qp(dev, port, steer, qpn);
  209. if (!pqp)
  210. return 0; /* nothing to do */
  211. list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
  212. if (tmp_entry->index == index) {
  213. entry = tmp_entry;
  214. break;
  215. }
  216. }
  217. if (unlikely(!entry)) {
  218. mlx4_warn(dev, "Steering entry at index %x is not registered\n", index);
  219. return -EINVAL;
  220. }
  221. /* the given qpn is listed as a promisc qpn
  222. * we need to add it as a duplicate to this entry
  223. * for future references */
  224. list_for_each_entry(dqp, &entry->duplicates, list) {
  225. if (qpn == pqp->qpn)
  226. return 0; /* qp is already duplicated */
  227. }
  228. /* add the qp as a duplicate on this index */
  229. dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
  230. if (!dqp)
  231. return -ENOMEM;
  232. dqp->qpn = qpn;
  233. list_add_tail(&dqp->list, &entry->duplicates);
  234. return 0;
  235. }
  236. /* Check whether a qpn is a duplicate on steering entry
  237. * If so, it should not be removed from mgm */
  238. static bool check_duplicate_entry(struct mlx4_dev *dev, u8 port,
  239. enum mlx4_steer_type steer,
  240. unsigned int index, u32 qpn)
  241. {
  242. struct mlx4_steer *s_steer;
  243. struct mlx4_steer_index *tmp_entry, *entry = NULL;
  244. struct mlx4_promisc_qp *dqp, *tmp_dqp;
  245. s_steer = &mlx4_priv(dev)->steer[port - 1];
  246. /* if qp is not promisc, it cannot be duplicated */
  247. if (!get_promisc_qp(dev, port, steer, qpn))
  248. return false;
  249. /* The qp is promisc qp so it is a duplicate on this index
  250. * Find the index entry, and remove the duplicate */
  251. list_for_each_entry(tmp_entry, &s_steer->steer_entries[steer], list) {
  252. if (tmp_entry->index == index) {
  253. entry = tmp_entry;
  254. break;
  255. }
  256. }
  257. if (unlikely(!entry)) {
  258. mlx4_warn(dev, "Steering entry for index %x is not registered\n", index);
  259. return false;
  260. }
  261. list_for_each_entry_safe(dqp, tmp_dqp, &entry->duplicates, list) {
  262. if (dqp->qpn == qpn) {
  263. list_del(&dqp->list);
  264. kfree(dqp);
  265. }
  266. }
  267. return true;
  268. }
  269. /* I a steering entry contains only promisc QPs, it can be removed. */
  270. static bool can_remove_steering_entry(struct mlx4_dev *dev, u8 port,
  271. enum mlx4_steer_type steer,
  272. unsigned int index, u32 tqpn)
  273. {
  274. struct mlx4_steer *s_steer;
  275. struct mlx4_cmd_mailbox *mailbox;
  276. struct mlx4_mgm *mgm;
  277. struct mlx4_steer_index *entry = NULL, *tmp_entry;
  278. u32 qpn;
  279. u32 members_count;
  280. bool ret = false;
  281. int i;
  282. s_steer = &mlx4_priv(dev)->steer[port - 1];
  283. mailbox = mlx4_alloc_cmd_mailbox(dev);
  284. if (IS_ERR(mailbox))
  285. return false;
  286. mgm = mailbox->buf;
  287. if (mlx4_READ_ENTRY(dev, index, mailbox))
  288. goto out;
  289. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  290. for (i = 0; i < members_count; i++) {
  291. qpn = be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK;
  292. if (!get_promisc_qp(dev, port, steer, qpn) && qpn != tqpn) {
  293. /* the qp is not promisc, the entry can't be removed */
  294. goto out;
  295. }
  296. }
  297. /* All the qps currently registered for this entry are promiscuous,
  298. * Checking for duplicates */
  299. ret = true;
  300. list_for_each_entry_safe(entry, tmp_entry, &s_steer->steer_entries[steer], list) {
  301. if (entry->index == index) {
  302. if (list_empty(&entry->duplicates)) {
  303. list_del(&entry->list);
  304. kfree(entry);
  305. } else {
  306. /* This entry contains duplicates so it shouldn't be removed */
  307. ret = false;
  308. goto out;
  309. }
  310. }
  311. }
  312. out:
  313. mlx4_free_cmd_mailbox(dev, mailbox);
  314. return ret;
  315. }
  316. static int add_promisc_qp(struct mlx4_dev *dev, u8 port,
  317. enum mlx4_steer_type steer, u32 qpn)
  318. {
  319. struct mlx4_steer *s_steer;
  320. struct mlx4_cmd_mailbox *mailbox;
  321. struct mlx4_mgm *mgm;
  322. struct mlx4_steer_index *entry;
  323. struct mlx4_promisc_qp *pqp;
  324. struct mlx4_promisc_qp *dqp;
  325. u32 members_count;
  326. u32 prot;
  327. int i;
  328. bool found;
  329. int err;
  330. struct mlx4_priv *priv = mlx4_priv(dev);
  331. s_steer = &mlx4_priv(dev)->steer[port - 1];
  332. mutex_lock(&priv->mcg_table.mutex);
  333. if (get_promisc_qp(dev, port, steer, qpn)) {
  334. err = 0; /* Noting to do, already exists */
  335. goto out_mutex;
  336. }
  337. pqp = kmalloc(sizeof *pqp, GFP_KERNEL);
  338. if (!pqp) {
  339. err = -ENOMEM;
  340. goto out_mutex;
  341. }
  342. pqp->qpn = qpn;
  343. mailbox = mlx4_alloc_cmd_mailbox(dev);
  344. if (IS_ERR(mailbox)) {
  345. err = -ENOMEM;
  346. goto out_alloc;
  347. }
  348. mgm = mailbox->buf;
  349. /* the promisc qp needs to be added for each one of the steering
  350. * entries, if it already exists, needs to be added as a duplicate
  351. * for this entry */
  352. list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
  353. err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
  354. if (err)
  355. goto out_mailbox;
  356. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  357. prot = be32_to_cpu(mgm->members_count) >> 30;
  358. found = false;
  359. for (i = 0; i < members_count; i++) {
  360. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn) {
  361. /* Entry already exists, add to duplicates */
  362. dqp = kmalloc(sizeof *dqp, GFP_KERNEL);
  363. if (!dqp) {
  364. err = -ENOMEM;
  365. goto out_mailbox;
  366. }
  367. dqp->qpn = qpn;
  368. list_add_tail(&dqp->list, &entry->duplicates);
  369. found = true;
  370. }
  371. }
  372. if (!found) {
  373. /* Need to add the qpn to mgm */
  374. if (members_count == dev->caps.num_qp_per_mgm) {
  375. /* entry is full */
  376. err = -ENOMEM;
  377. goto out_mailbox;
  378. }
  379. mgm->qp[members_count++] = cpu_to_be32(qpn & MGM_QPN_MASK);
  380. mgm->members_count = cpu_to_be32(members_count | (prot << 30));
  381. err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
  382. if (err)
  383. goto out_mailbox;
  384. }
  385. }
  386. /* add the new qpn to list of promisc qps */
  387. list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
  388. /* now need to add all the promisc qps to default entry */
  389. memset(mgm, 0, sizeof *mgm);
  390. members_count = 0;
  391. list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
  392. mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
  393. mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
  394. err = mlx4_WRITE_PROMISC(dev, port, steer, mailbox);
  395. if (err)
  396. goto out_list;
  397. mlx4_free_cmd_mailbox(dev, mailbox);
  398. mutex_unlock(&priv->mcg_table.mutex);
  399. return 0;
  400. out_list:
  401. list_del(&pqp->list);
  402. out_mailbox:
  403. mlx4_free_cmd_mailbox(dev, mailbox);
  404. out_alloc:
  405. kfree(pqp);
  406. out_mutex:
  407. mutex_unlock(&priv->mcg_table.mutex);
  408. return err;
  409. }
  410. static int remove_promisc_qp(struct mlx4_dev *dev, u8 port,
  411. enum mlx4_steer_type steer, u32 qpn)
  412. {
  413. struct mlx4_priv *priv = mlx4_priv(dev);
  414. struct mlx4_steer *s_steer;
  415. struct mlx4_cmd_mailbox *mailbox;
  416. struct mlx4_mgm *mgm;
  417. struct mlx4_steer_index *entry;
  418. struct mlx4_promisc_qp *pqp;
  419. struct mlx4_promisc_qp *dqp;
  420. u32 members_count;
  421. bool found;
  422. bool back_to_list = false;
  423. int loc, i;
  424. int err;
  425. s_steer = &mlx4_priv(dev)->steer[port - 1];
  426. mutex_lock(&priv->mcg_table.mutex);
  427. pqp = get_promisc_qp(dev, port, steer, qpn);
  428. if (unlikely(!pqp)) {
  429. mlx4_warn(dev, "QP %x is not promiscuous QP\n", qpn);
  430. /* nothing to do */
  431. err = 0;
  432. goto out_mutex;
  433. }
  434. /*remove from list of promisc qps */
  435. list_del(&pqp->list);
  436. /* set the default entry not to include the removed one */
  437. mailbox = mlx4_alloc_cmd_mailbox(dev);
  438. if (IS_ERR(mailbox)) {
  439. err = -ENOMEM;
  440. back_to_list = true;
  441. goto out_list;
  442. }
  443. mgm = mailbox->buf;
  444. memset(mgm, 0, sizeof *mgm);
  445. members_count = 0;
  446. list_for_each_entry(dqp, &s_steer->promisc_qps[steer], list)
  447. mgm->qp[members_count++] = cpu_to_be32(dqp->qpn & MGM_QPN_MASK);
  448. mgm->members_count = cpu_to_be32(members_count | MLX4_PROT_ETH << 30);
  449. err = mlx4_WRITE_PROMISC(dev, port, steer, mailbox);
  450. if (err)
  451. goto out_mailbox;
  452. /* remove the qp from all the steering entries*/
  453. list_for_each_entry(entry, &s_steer->steer_entries[steer], list) {
  454. found = false;
  455. list_for_each_entry(dqp, &entry->duplicates, list) {
  456. if (dqp->qpn == qpn) {
  457. found = true;
  458. break;
  459. }
  460. }
  461. if (found) {
  462. /* a duplicate, no need to change the mgm,
  463. * only update the duplicates list */
  464. list_del(&dqp->list);
  465. kfree(dqp);
  466. } else {
  467. err = mlx4_READ_ENTRY(dev, entry->index, mailbox);
  468. if (err)
  469. goto out_mailbox;
  470. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  471. for (loc = -1, i = 0; i < members_count; ++i)
  472. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qpn)
  473. loc = i;
  474. mgm->members_count = cpu_to_be32(--members_count |
  475. (MLX4_PROT_ETH << 30));
  476. mgm->qp[loc] = mgm->qp[i - 1];
  477. mgm->qp[i - 1] = 0;
  478. err = mlx4_WRITE_ENTRY(dev, entry->index, mailbox);
  479. if (err)
  480. goto out_mailbox;
  481. }
  482. }
  483. out_mailbox:
  484. mlx4_free_cmd_mailbox(dev, mailbox);
  485. out_list:
  486. if (back_to_list)
  487. list_add_tail(&pqp->list, &s_steer->promisc_qps[steer]);
  488. else
  489. kfree(pqp);
  490. out_mutex:
  491. mutex_unlock(&priv->mcg_table.mutex);
  492. return err;
  493. }
  494. /*
  495. * Caller must hold MCG table semaphore. gid and mgm parameters must
  496. * be properly aligned for command interface.
  497. *
  498. * Returns 0 unless a firmware command error occurs.
  499. *
  500. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  501. * and *mgm holds MGM entry.
  502. *
  503. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  504. * previous entry in hash chain and *mgm holds AMGM entry.
  505. *
  506. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  507. * entry in hash chain and *mgm holds end of hash chain.
  508. */
  509. static int find_entry(struct mlx4_dev *dev, u8 port,
  510. u8 *gid, enum mlx4_protocol prot,
  511. struct mlx4_cmd_mailbox *mgm_mailbox,
  512. int *prev, int *index)
  513. {
  514. struct mlx4_cmd_mailbox *mailbox;
  515. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  516. u8 *mgid;
  517. int err;
  518. u16 hash;
  519. u8 op_mod = (prot == MLX4_PROT_ETH) ?
  520. !!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) : 0;
  521. mailbox = mlx4_alloc_cmd_mailbox(dev);
  522. if (IS_ERR(mailbox))
  523. return -ENOMEM;
  524. mgid = mailbox->buf;
  525. memcpy(mgid, gid, 16);
  526. err = mlx4_GID_HASH(dev, mailbox, &hash, op_mod);
  527. mlx4_free_cmd_mailbox(dev, mailbox);
  528. if (err)
  529. return err;
  530. if (0)
  531. mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, hash);
  532. *index = hash;
  533. *prev = -1;
  534. do {
  535. err = mlx4_READ_ENTRY(dev, *index, mgm_mailbox);
  536. if (err)
  537. return err;
  538. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  539. if (*index != hash) {
  540. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  541. err = -EINVAL;
  542. }
  543. return err;
  544. }
  545. if (!memcmp(mgm->gid, gid, 16) &&
  546. be32_to_cpu(mgm->members_count) >> 30 == prot)
  547. return err;
  548. *prev = *index;
  549. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  550. } while (*index);
  551. *index = -1;
  552. return err;
  553. }
  554. static const u8 __promisc_mode[] = {
  555. [MLX4_FS_REGULAR] = 0x0,
  556. [MLX4_FS_ALL_DEFAULT] = 0x1,
  557. [MLX4_FS_MC_DEFAULT] = 0x3,
  558. [MLX4_FS_UC_SNIFFER] = 0x4,
  559. [MLX4_FS_MC_SNIFFER] = 0x5,
  560. };
  561. int mlx4_map_sw_to_hw_steering_mode(struct mlx4_dev *dev,
  562. enum mlx4_net_trans_promisc_mode flow_type)
  563. {
  564. if (flow_type >= MLX4_FS_MODE_NUM || flow_type < 0) {
  565. mlx4_err(dev, "Invalid flow type. type = %d\n", flow_type);
  566. return -EINVAL;
  567. }
  568. return __promisc_mode[flow_type];
  569. }
  570. EXPORT_SYMBOL_GPL(mlx4_map_sw_to_hw_steering_mode);
  571. static void trans_rule_ctrl_to_hw(struct mlx4_net_trans_rule *ctrl,
  572. struct mlx4_net_trans_rule_hw_ctrl *hw)
  573. {
  574. u8 flags = 0;
  575. flags = ctrl->queue_mode == MLX4_NET_TRANS_Q_LIFO ? 1 : 0;
  576. flags |= ctrl->exclusive ? (1 << 2) : 0;
  577. flags |= ctrl->allow_loopback ? (1 << 3) : 0;
  578. hw->flags = flags;
  579. hw->type = __promisc_mode[ctrl->promisc_mode];
  580. hw->prio = cpu_to_be16(ctrl->priority);
  581. hw->port = ctrl->port;
  582. hw->qpn = cpu_to_be32(ctrl->qpn);
  583. }
  584. const u16 __sw_id_hw[] = {
  585. [MLX4_NET_TRANS_RULE_ID_ETH] = 0xE001,
  586. [MLX4_NET_TRANS_RULE_ID_IB] = 0xE005,
  587. [MLX4_NET_TRANS_RULE_ID_IPV6] = 0xE003,
  588. [MLX4_NET_TRANS_RULE_ID_IPV4] = 0xE002,
  589. [MLX4_NET_TRANS_RULE_ID_TCP] = 0xE004,
  590. [MLX4_NET_TRANS_RULE_ID_UDP] = 0xE006
  591. };
  592. int mlx4_map_sw_to_hw_steering_id(struct mlx4_dev *dev,
  593. enum mlx4_net_trans_rule_id id)
  594. {
  595. if (id >= MLX4_NET_TRANS_RULE_NUM || id < 0) {
  596. mlx4_err(dev, "Invalid network rule id. id = %d\n", id);
  597. return -EINVAL;
  598. }
  599. return __sw_id_hw[id];
  600. }
  601. EXPORT_SYMBOL_GPL(mlx4_map_sw_to_hw_steering_id);
  602. static const int __rule_hw_sz[] = {
  603. [MLX4_NET_TRANS_RULE_ID_ETH] =
  604. sizeof(struct mlx4_net_trans_rule_hw_eth),
  605. [MLX4_NET_TRANS_RULE_ID_IB] =
  606. sizeof(struct mlx4_net_trans_rule_hw_ib),
  607. [MLX4_NET_TRANS_RULE_ID_IPV6] = 0,
  608. [MLX4_NET_TRANS_RULE_ID_IPV4] =
  609. sizeof(struct mlx4_net_trans_rule_hw_ipv4),
  610. [MLX4_NET_TRANS_RULE_ID_TCP] =
  611. sizeof(struct mlx4_net_trans_rule_hw_tcp_udp),
  612. [MLX4_NET_TRANS_RULE_ID_UDP] =
  613. sizeof(struct mlx4_net_trans_rule_hw_tcp_udp)
  614. };
  615. int mlx4_hw_rule_sz(struct mlx4_dev *dev,
  616. enum mlx4_net_trans_rule_id id)
  617. {
  618. if (id >= MLX4_NET_TRANS_RULE_NUM || id < 0) {
  619. mlx4_err(dev, "Invalid network rule id. id = %d\n", id);
  620. return -EINVAL;
  621. }
  622. return __rule_hw_sz[id];
  623. }
  624. EXPORT_SYMBOL_GPL(mlx4_hw_rule_sz);
  625. static int parse_trans_rule(struct mlx4_dev *dev, struct mlx4_spec_list *spec,
  626. struct _rule_hw *rule_hw)
  627. {
  628. if (mlx4_hw_rule_sz(dev, spec->id) < 0)
  629. return -EINVAL;
  630. memset(rule_hw, 0, mlx4_hw_rule_sz(dev, spec->id));
  631. rule_hw->id = cpu_to_be16(__sw_id_hw[spec->id]);
  632. rule_hw->size = mlx4_hw_rule_sz(dev, spec->id) >> 2;
  633. switch (spec->id) {
  634. case MLX4_NET_TRANS_RULE_ID_ETH:
  635. memcpy(rule_hw->eth.dst_mac, spec->eth.dst_mac, ETH_ALEN);
  636. memcpy(rule_hw->eth.dst_mac_msk, spec->eth.dst_mac_msk,
  637. ETH_ALEN);
  638. memcpy(rule_hw->eth.src_mac, spec->eth.src_mac, ETH_ALEN);
  639. memcpy(rule_hw->eth.src_mac_msk, spec->eth.src_mac_msk,
  640. ETH_ALEN);
  641. if (spec->eth.ether_type_enable) {
  642. rule_hw->eth.ether_type_enable = 1;
  643. rule_hw->eth.ether_type = spec->eth.ether_type;
  644. }
  645. rule_hw->eth.vlan_tag = spec->eth.vlan_id;
  646. rule_hw->eth.vlan_tag_msk = spec->eth.vlan_id_msk;
  647. break;
  648. case MLX4_NET_TRANS_RULE_ID_IB:
  649. rule_hw->ib.l3_qpn = spec->ib.l3_qpn;
  650. rule_hw->ib.qpn_mask = spec->ib.qpn_msk;
  651. memcpy(&rule_hw->ib.dst_gid, &spec->ib.dst_gid, 16);
  652. memcpy(&rule_hw->ib.dst_gid_msk, &spec->ib.dst_gid_msk, 16);
  653. break;
  654. case MLX4_NET_TRANS_RULE_ID_IPV6:
  655. return -EOPNOTSUPP;
  656. case MLX4_NET_TRANS_RULE_ID_IPV4:
  657. rule_hw->ipv4.src_ip = spec->ipv4.src_ip;
  658. rule_hw->ipv4.src_ip_msk = spec->ipv4.src_ip_msk;
  659. rule_hw->ipv4.dst_ip = spec->ipv4.dst_ip;
  660. rule_hw->ipv4.dst_ip_msk = spec->ipv4.dst_ip_msk;
  661. break;
  662. case MLX4_NET_TRANS_RULE_ID_TCP:
  663. case MLX4_NET_TRANS_RULE_ID_UDP:
  664. rule_hw->tcp_udp.dst_port = spec->tcp_udp.dst_port;
  665. rule_hw->tcp_udp.dst_port_msk = spec->tcp_udp.dst_port_msk;
  666. rule_hw->tcp_udp.src_port = spec->tcp_udp.src_port;
  667. rule_hw->tcp_udp.src_port_msk = spec->tcp_udp.src_port_msk;
  668. break;
  669. default:
  670. return -EINVAL;
  671. }
  672. return __rule_hw_sz[spec->id];
  673. }
  674. static void mlx4_err_rule(struct mlx4_dev *dev, char *str,
  675. struct mlx4_net_trans_rule *rule)
  676. {
  677. #define BUF_SIZE 256
  678. struct mlx4_spec_list *cur;
  679. char buf[BUF_SIZE];
  680. int len = 0;
  681. mlx4_err(dev, "%s", str);
  682. len += snprintf(buf + len, BUF_SIZE - len,
  683. "port = %d prio = 0x%x qp = 0x%x ",
  684. rule->port, rule->priority, rule->qpn);
  685. list_for_each_entry(cur, &rule->list, list) {
  686. switch (cur->id) {
  687. case MLX4_NET_TRANS_RULE_ID_ETH:
  688. len += snprintf(buf + len, BUF_SIZE - len,
  689. "dmac = %pM ", &cur->eth.dst_mac);
  690. if (cur->eth.ether_type)
  691. len += snprintf(buf + len, BUF_SIZE - len,
  692. "ethertype = 0x%x ",
  693. be16_to_cpu(cur->eth.ether_type));
  694. if (cur->eth.vlan_id)
  695. len += snprintf(buf + len, BUF_SIZE - len,
  696. "vlan-id = %d ",
  697. be16_to_cpu(cur->eth.vlan_id));
  698. break;
  699. case MLX4_NET_TRANS_RULE_ID_IPV4:
  700. if (cur->ipv4.src_ip)
  701. len += snprintf(buf + len, BUF_SIZE - len,
  702. "src-ip = %pI4 ",
  703. &cur->ipv4.src_ip);
  704. if (cur->ipv4.dst_ip)
  705. len += snprintf(buf + len, BUF_SIZE - len,
  706. "dst-ip = %pI4 ",
  707. &cur->ipv4.dst_ip);
  708. break;
  709. case MLX4_NET_TRANS_RULE_ID_TCP:
  710. case MLX4_NET_TRANS_RULE_ID_UDP:
  711. if (cur->tcp_udp.src_port)
  712. len += snprintf(buf + len, BUF_SIZE - len,
  713. "src-port = %d ",
  714. be16_to_cpu(cur->tcp_udp.src_port));
  715. if (cur->tcp_udp.dst_port)
  716. len += snprintf(buf + len, BUF_SIZE - len,
  717. "dst-port = %d ",
  718. be16_to_cpu(cur->tcp_udp.dst_port));
  719. break;
  720. case MLX4_NET_TRANS_RULE_ID_IB:
  721. len += snprintf(buf + len, BUF_SIZE - len,
  722. "dst-gid = %pI6\n", cur->ib.dst_gid);
  723. len += snprintf(buf + len, BUF_SIZE - len,
  724. "dst-gid-mask = %pI6\n",
  725. cur->ib.dst_gid_msk);
  726. break;
  727. case MLX4_NET_TRANS_RULE_ID_IPV6:
  728. break;
  729. default:
  730. break;
  731. }
  732. }
  733. len += snprintf(buf + len, BUF_SIZE - len, "\n");
  734. mlx4_err(dev, "%s", buf);
  735. if (len >= BUF_SIZE)
  736. mlx4_err(dev, "Network rule error message was truncated, print buffer is too small.\n");
  737. }
  738. int mlx4_flow_attach(struct mlx4_dev *dev,
  739. struct mlx4_net_trans_rule *rule, u64 *reg_id)
  740. {
  741. struct mlx4_cmd_mailbox *mailbox;
  742. struct mlx4_spec_list *cur;
  743. u32 size = 0;
  744. int ret;
  745. mailbox = mlx4_alloc_cmd_mailbox(dev);
  746. if (IS_ERR(mailbox))
  747. return PTR_ERR(mailbox);
  748. memset(mailbox->buf, 0, sizeof(struct mlx4_net_trans_rule_hw_ctrl));
  749. trans_rule_ctrl_to_hw(rule, mailbox->buf);
  750. size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
  751. list_for_each_entry(cur, &rule->list, list) {
  752. ret = parse_trans_rule(dev, cur, mailbox->buf + size);
  753. if (ret < 0) {
  754. mlx4_free_cmd_mailbox(dev, mailbox);
  755. return -EINVAL;
  756. }
  757. size += ret;
  758. }
  759. ret = mlx4_QP_FLOW_STEERING_ATTACH(dev, mailbox, size >> 2, reg_id);
  760. if (ret == -ENOMEM)
  761. mlx4_err_rule(dev,
  762. "mcg table is full. Fail to register network rule.\n",
  763. rule);
  764. else if (ret)
  765. mlx4_err_rule(dev, "Fail to register network rule.\n", rule);
  766. mlx4_free_cmd_mailbox(dev, mailbox);
  767. return ret;
  768. }
  769. EXPORT_SYMBOL_GPL(mlx4_flow_attach);
  770. int mlx4_flow_detach(struct mlx4_dev *dev, u64 reg_id)
  771. {
  772. int err;
  773. err = mlx4_QP_FLOW_STEERING_DETACH(dev, reg_id);
  774. if (err)
  775. mlx4_err(dev, "Fail to detach network rule. registration id = 0x%llx\n",
  776. reg_id);
  777. return err;
  778. }
  779. EXPORT_SYMBOL_GPL(mlx4_flow_detach);
  780. int mlx4_qp_attach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  781. int block_mcast_loopback, enum mlx4_protocol prot,
  782. enum mlx4_steer_type steer)
  783. {
  784. struct mlx4_priv *priv = mlx4_priv(dev);
  785. struct mlx4_cmd_mailbox *mailbox;
  786. struct mlx4_mgm *mgm;
  787. u32 members_count;
  788. int index, prev;
  789. int link = 0;
  790. int i;
  791. int err;
  792. u8 port = gid[5];
  793. u8 new_entry = 0;
  794. mailbox = mlx4_alloc_cmd_mailbox(dev);
  795. if (IS_ERR(mailbox))
  796. return PTR_ERR(mailbox);
  797. mgm = mailbox->buf;
  798. mutex_lock(&priv->mcg_table.mutex);
  799. err = find_entry(dev, port, gid, prot,
  800. mailbox, &prev, &index);
  801. if (err)
  802. goto out;
  803. if (index != -1) {
  804. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  805. new_entry = 1;
  806. memcpy(mgm->gid, gid, 16);
  807. }
  808. } else {
  809. link = 1;
  810. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  811. if (index == -1) {
  812. mlx4_err(dev, "No AMGM entries left\n");
  813. err = -ENOMEM;
  814. goto out;
  815. }
  816. index += dev->caps.num_mgms;
  817. new_entry = 1;
  818. memset(mgm, 0, sizeof *mgm);
  819. memcpy(mgm->gid, gid, 16);
  820. }
  821. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  822. if (members_count == dev->caps.num_qp_per_mgm) {
  823. mlx4_err(dev, "MGM at index %x is full.\n", index);
  824. err = -ENOMEM;
  825. goto out;
  826. }
  827. for (i = 0; i < members_count; ++i)
  828. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  829. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  830. err = 0;
  831. goto out;
  832. }
  833. if (block_mcast_loopback)
  834. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  835. (1U << MGM_BLCK_LB_BIT));
  836. else
  837. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  838. mgm->members_count = cpu_to_be32(members_count | (u32) prot << 30);
  839. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  840. if (err)
  841. goto out;
  842. if (!link)
  843. goto out;
  844. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  845. if (err)
  846. goto out;
  847. mgm->next_gid_index = cpu_to_be32(index << 6);
  848. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  849. if (err)
  850. goto out;
  851. out:
  852. if (prot == MLX4_PROT_ETH) {
  853. /* manage the steering entry for promisc mode */
  854. if (new_entry)
  855. new_steering_entry(dev, port, steer, index, qp->qpn);
  856. else
  857. existing_steering_entry(dev, port, steer,
  858. index, qp->qpn);
  859. }
  860. if (err && link && index != -1) {
  861. if (index < dev->caps.num_mgms)
  862. mlx4_warn(dev, "Got AMGM index %d < %d",
  863. index, dev->caps.num_mgms);
  864. else
  865. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  866. index - dev->caps.num_mgms);
  867. }
  868. mutex_unlock(&priv->mcg_table.mutex);
  869. mlx4_free_cmd_mailbox(dev, mailbox);
  870. return err;
  871. }
  872. int mlx4_qp_detach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  873. enum mlx4_protocol prot, enum mlx4_steer_type steer)
  874. {
  875. struct mlx4_priv *priv = mlx4_priv(dev);
  876. struct mlx4_cmd_mailbox *mailbox;
  877. struct mlx4_mgm *mgm;
  878. u32 members_count;
  879. int prev, index;
  880. int i, loc;
  881. int err;
  882. u8 port = gid[5];
  883. bool removed_entry = false;
  884. mailbox = mlx4_alloc_cmd_mailbox(dev);
  885. if (IS_ERR(mailbox))
  886. return PTR_ERR(mailbox);
  887. mgm = mailbox->buf;
  888. mutex_lock(&priv->mcg_table.mutex);
  889. err = find_entry(dev, port, gid, prot,
  890. mailbox, &prev, &index);
  891. if (err)
  892. goto out;
  893. if (index == -1) {
  894. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  895. err = -EINVAL;
  896. goto out;
  897. }
  898. /* if this pq is also a promisc qp, it shouldn't be removed */
  899. if (prot == MLX4_PROT_ETH &&
  900. check_duplicate_entry(dev, port, steer, index, qp->qpn))
  901. goto out;
  902. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  903. for (loc = -1, i = 0; i < members_count; ++i)
  904. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  905. loc = i;
  906. if (loc == -1) {
  907. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  908. err = -EINVAL;
  909. goto out;
  910. }
  911. mgm->members_count = cpu_to_be32(--members_count | (u32) prot << 30);
  912. mgm->qp[loc] = mgm->qp[i - 1];
  913. mgm->qp[i - 1] = 0;
  914. if (prot == MLX4_PROT_ETH)
  915. removed_entry = can_remove_steering_entry(dev, port, steer,
  916. index, qp->qpn);
  917. if (i != 1 && (prot != MLX4_PROT_ETH || !removed_entry)) {
  918. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  919. goto out;
  920. }
  921. /* We are going to delete the entry, members count should be 0 */
  922. mgm->members_count = cpu_to_be32((u32) prot << 30);
  923. if (prev == -1) {
  924. /* Remove entry from MGM */
  925. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  926. if (amgm_index) {
  927. err = mlx4_READ_ENTRY(dev, amgm_index, mailbox);
  928. if (err)
  929. goto out;
  930. } else
  931. memset(mgm->gid, 0, 16);
  932. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  933. if (err)
  934. goto out;
  935. if (amgm_index) {
  936. if (amgm_index < dev->caps.num_mgms)
  937. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  938. index, amgm_index, dev->caps.num_mgms);
  939. else
  940. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  941. amgm_index - dev->caps.num_mgms);
  942. }
  943. } else {
  944. /* Remove entry from AMGM */
  945. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  946. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  947. if (err)
  948. goto out;
  949. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  950. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  951. if (err)
  952. goto out;
  953. if (index < dev->caps.num_mgms)
  954. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  955. prev, index, dev->caps.num_mgms);
  956. else
  957. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  958. index - dev->caps.num_mgms);
  959. }
  960. out:
  961. mutex_unlock(&priv->mcg_table.mutex);
  962. mlx4_free_cmd_mailbox(dev, mailbox);
  963. return err;
  964. }
  965. static int mlx4_QP_ATTACH(struct mlx4_dev *dev, struct mlx4_qp *qp,
  966. u8 gid[16], u8 attach, u8 block_loopback,
  967. enum mlx4_protocol prot)
  968. {
  969. struct mlx4_cmd_mailbox *mailbox;
  970. int err = 0;
  971. int qpn;
  972. if (!mlx4_is_mfunc(dev))
  973. return -EBADF;
  974. mailbox = mlx4_alloc_cmd_mailbox(dev);
  975. if (IS_ERR(mailbox))
  976. return PTR_ERR(mailbox);
  977. memcpy(mailbox->buf, gid, 16);
  978. qpn = qp->qpn;
  979. qpn |= (prot << 28);
  980. if (attach && block_loopback)
  981. qpn |= (1 << 31);
  982. err = mlx4_cmd(dev, mailbox->dma, qpn, attach,
  983. MLX4_CMD_QP_ATTACH, MLX4_CMD_TIME_CLASS_A,
  984. MLX4_CMD_WRAPPED);
  985. mlx4_free_cmd_mailbox(dev, mailbox);
  986. return err;
  987. }
  988. int mlx4_trans_to_dmfs_attach(struct mlx4_dev *dev, struct mlx4_qp *qp,
  989. u8 gid[16], u8 port,
  990. int block_mcast_loopback,
  991. enum mlx4_protocol prot, u64 *reg_id)
  992. {
  993. struct mlx4_spec_list spec = { {NULL} };
  994. __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
  995. struct mlx4_net_trans_rule rule = {
  996. .queue_mode = MLX4_NET_TRANS_Q_FIFO,
  997. .exclusive = 0,
  998. .promisc_mode = MLX4_FS_REGULAR,
  999. .priority = MLX4_DOMAIN_NIC,
  1000. };
  1001. rule.allow_loopback = !block_mcast_loopback;
  1002. rule.port = port;
  1003. rule.qpn = qp->qpn;
  1004. INIT_LIST_HEAD(&rule.list);
  1005. switch (prot) {
  1006. case MLX4_PROT_ETH:
  1007. spec.id = MLX4_NET_TRANS_RULE_ID_ETH;
  1008. memcpy(spec.eth.dst_mac, &gid[10], ETH_ALEN);
  1009. memcpy(spec.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
  1010. break;
  1011. case MLX4_PROT_IB_IPV6:
  1012. spec.id = MLX4_NET_TRANS_RULE_ID_IB;
  1013. memcpy(spec.ib.dst_gid, gid, 16);
  1014. memset(&spec.ib.dst_gid_msk, 0xff, 16);
  1015. break;
  1016. default:
  1017. return -EINVAL;
  1018. }
  1019. list_add_tail(&spec.list, &rule.list);
  1020. return mlx4_flow_attach(dev, &rule, reg_id);
  1021. }
  1022. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  1023. u8 port, int block_mcast_loopback,
  1024. enum mlx4_protocol prot, u64 *reg_id)
  1025. {
  1026. switch (dev->caps.steering_mode) {
  1027. case MLX4_STEERING_MODE_A0:
  1028. if (prot == MLX4_PROT_ETH)
  1029. return 0;
  1030. case MLX4_STEERING_MODE_B0:
  1031. if (prot == MLX4_PROT_ETH)
  1032. gid[7] |= (MLX4_MC_STEER << 1);
  1033. if (mlx4_is_mfunc(dev))
  1034. return mlx4_QP_ATTACH(dev, qp, gid, 1,
  1035. block_mcast_loopback, prot);
  1036. return mlx4_qp_attach_common(dev, qp, gid,
  1037. block_mcast_loopback, prot,
  1038. MLX4_MC_STEER);
  1039. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  1040. return mlx4_trans_to_dmfs_attach(dev, qp, gid, port,
  1041. block_mcast_loopback,
  1042. prot, reg_id);
  1043. default:
  1044. return -EINVAL;
  1045. }
  1046. }
  1047. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  1048. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  1049. enum mlx4_protocol prot, u64 reg_id)
  1050. {
  1051. switch (dev->caps.steering_mode) {
  1052. case MLX4_STEERING_MODE_A0:
  1053. if (prot == MLX4_PROT_ETH)
  1054. return 0;
  1055. case MLX4_STEERING_MODE_B0:
  1056. if (prot == MLX4_PROT_ETH)
  1057. gid[7] |= (MLX4_MC_STEER << 1);
  1058. if (mlx4_is_mfunc(dev))
  1059. return mlx4_QP_ATTACH(dev, qp, gid, 0, 0, prot);
  1060. return mlx4_qp_detach_common(dev, qp, gid, prot,
  1061. MLX4_MC_STEER);
  1062. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  1063. return mlx4_flow_detach(dev, reg_id);
  1064. default:
  1065. return -EINVAL;
  1066. }
  1067. }
  1068. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  1069. int mlx4_flow_steer_promisc_add(struct mlx4_dev *dev, u8 port,
  1070. u32 qpn, enum mlx4_net_trans_promisc_mode mode)
  1071. {
  1072. struct mlx4_net_trans_rule rule;
  1073. u64 *regid_p;
  1074. switch (mode) {
  1075. case MLX4_FS_ALL_DEFAULT:
  1076. regid_p = &dev->regid_promisc_array[port];
  1077. break;
  1078. case MLX4_FS_MC_DEFAULT:
  1079. regid_p = &dev->regid_allmulti_array[port];
  1080. break;
  1081. default:
  1082. return -1;
  1083. }
  1084. if (*regid_p != 0)
  1085. return -1;
  1086. rule.promisc_mode = mode;
  1087. rule.port = port;
  1088. rule.qpn = qpn;
  1089. INIT_LIST_HEAD(&rule.list);
  1090. mlx4_err(dev, "going promisc on %x\n", port);
  1091. return mlx4_flow_attach(dev, &rule, regid_p);
  1092. }
  1093. EXPORT_SYMBOL_GPL(mlx4_flow_steer_promisc_add);
  1094. int mlx4_flow_steer_promisc_remove(struct mlx4_dev *dev, u8 port,
  1095. enum mlx4_net_trans_promisc_mode mode)
  1096. {
  1097. int ret;
  1098. u64 *regid_p;
  1099. switch (mode) {
  1100. case MLX4_FS_ALL_DEFAULT:
  1101. regid_p = &dev->regid_promisc_array[port];
  1102. break;
  1103. case MLX4_FS_MC_DEFAULT:
  1104. regid_p = &dev->regid_allmulti_array[port];
  1105. break;
  1106. default:
  1107. return -1;
  1108. }
  1109. if (*regid_p == 0)
  1110. return -1;
  1111. ret = mlx4_flow_detach(dev, *regid_p);
  1112. if (ret == 0)
  1113. *regid_p = 0;
  1114. return ret;
  1115. }
  1116. EXPORT_SYMBOL_GPL(mlx4_flow_steer_promisc_remove);
  1117. int mlx4_unicast_attach(struct mlx4_dev *dev,
  1118. struct mlx4_qp *qp, u8 gid[16],
  1119. int block_mcast_loopback, enum mlx4_protocol prot)
  1120. {
  1121. if (prot == MLX4_PROT_ETH)
  1122. gid[7] |= (MLX4_UC_STEER << 1);
  1123. if (mlx4_is_mfunc(dev))
  1124. return mlx4_QP_ATTACH(dev, qp, gid, 1,
  1125. block_mcast_loopback, prot);
  1126. return mlx4_qp_attach_common(dev, qp, gid, block_mcast_loopback,
  1127. prot, MLX4_UC_STEER);
  1128. }
  1129. EXPORT_SYMBOL_GPL(mlx4_unicast_attach);
  1130. int mlx4_unicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp,
  1131. u8 gid[16], enum mlx4_protocol prot)
  1132. {
  1133. if (prot == MLX4_PROT_ETH)
  1134. gid[7] |= (MLX4_UC_STEER << 1);
  1135. if (mlx4_is_mfunc(dev))
  1136. return mlx4_QP_ATTACH(dev, qp, gid, 0, 0, prot);
  1137. return mlx4_qp_detach_common(dev, qp, gid, prot, MLX4_UC_STEER);
  1138. }
  1139. EXPORT_SYMBOL_GPL(mlx4_unicast_detach);
  1140. int mlx4_PROMISC_wrapper(struct mlx4_dev *dev, int slave,
  1141. struct mlx4_vhcr *vhcr,
  1142. struct mlx4_cmd_mailbox *inbox,
  1143. struct mlx4_cmd_mailbox *outbox,
  1144. struct mlx4_cmd_info *cmd)
  1145. {
  1146. u32 qpn = (u32) vhcr->in_param & 0xffffffff;
  1147. u8 port = vhcr->in_param >> 62;
  1148. enum mlx4_steer_type steer = vhcr->in_modifier;
  1149. /* Promiscuous unicast is not allowed in mfunc */
  1150. if (mlx4_is_mfunc(dev) && steer == MLX4_UC_STEER)
  1151. return 0;
  1152. if (vhcr->op_modifier)
  1153. return add_promisc_qp(dev, port, steer, qpn);
  1154. else
  1155. return remove_promisc_qp(dev, port, steer, qpn);
  1156. }
  1157. static int mlx4_PROMISC(struct mlx4_dev *dev, u32 qpn,
  1158. enum mlx4_steer_type steer, u8 add, u8 port)
  1159. {
  1160. return mlx4_cmd(dev, (u64) qpn | (u64) port << 62, (u32) steer, add,
  1161. MLX4_CMD_PROMISC, MLX4_CMD_TIME_CLASS_A,
  1162. MLX4_CMD_WRAPPED);
  1163. }
  1164. int mlx4_multicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
  1165. {
  1166. if (mlx4_is_mfunc(dev))
  1167. return mlx4_PROMISC(dev, qpn, MLX4_MC_STEER, 1, port);
  1168. return add_promisc_qp(dev, port, MLX4_MC_STEER, qpn);
  1169. }
  1170. EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_add);
  1171. int mlx4_multicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
  1172. {
  1173. if (mlx4_is_mfunc(dev))
  1174. return mlx4_PROMISC(dev, qpn, MLX4_MC_STEER, 0, port);
  1175. return remove_promisc_qp(dev, port, MLX4_MC_STEER, qpn);
  1176. }
  1177. EXPORT_SYMBOL_GPL(mlx4_multicast_promisc_remove);
  1178. int mlx4_unicast_promisc_add(struct mlx4_dev *dev, u32 qpn, u8 port)
  1179. {
  1180. if (mlx4_is_mfunc(dev))
  1181. return mlx4_PROMISC(dev, qpn, MLX4_UC_STEER, 1, port);
  1182. return add_promisc_qp(dev, port, MLX4_UC_STEER, qpn);
  1183. }
  1184. EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_add);
  1185. int mlx4_unicast_promisc_remove(struct mlx4_dev *dev, u32 qpn, u8 port)
  1186. {
  1187. if (mlx4_is_mfunc(dev))
  1188. return mlx4_PROMISC(dev, qpn, MLX4_UC_STEER, 0, port);
  1189. return remove_promisc_qp(dev, port, MLX4_UC_STEER, qpn);
  1190. }
  1191. EXPORT_SYMBOL_GPL(mlx4_unicast_promisc_remove);
  1192. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  1193. {
  1194. struct mlx4_priv *priv = mlx4_priv(dev);
  1195. int err;
  1196. /* No need for mcg_table when fw managed the mcg table*/
  1197. if (dev->caps.steering_mode ==
  1198. MLX4_STEERING_MODE_DEVICE_MANAGED)
  1199. return 0;
  1200. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  1201. dev->caps.num_amgms - 1, 0, 0);
  1202. if (err)
  1203. return err;
  1204. mutex_init(&priv->mcg_table.mutex);
  1205. return 0;
  1206. }
  1207. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  1208. {
  1209. if (dev->caps.steering_mode !=
  1210. MLX4_STEERING_MODE_DEVICE_MANAGED)
  1211. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  1212. }