mcg.c 37 KB

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