mcg.c 42 KB

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