mcg.c 36 KB

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