port.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/errno.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/export.h>
  36. #include <linux/mlx4/cmd.h>
  37. #include "mlx4.h"
  38. #define MLX4_MAC_VALID (1ull << 63)
  39. #define MLX4_VLAN_VALID (1u << 31)
  40. #define MLX4_VLAN_MASK 0xfff
  41. #define MLX4_STATS_TRAFFIC_COUNTERS_MASK 0xfULL
  42. #define MLX4_STATS_TRAFFIC_DROPS_MASK 0xc0ULL
  43. #define MLX4_STATS_ERROR_COUNTERS_MASK 0x1ffc30ULL
  44. #define MLX4_STATS_PORT_COUNTERS_MASK 0x1fe00000ULL
  45. void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
  46. {
  47. int i;
  48. mutex_init(&table->mutex);
  49. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  50. table->entries[i] = 0;
  51. table->refs[i] = 0;
  52. }
  53. table->max = 1 << dev->caps.log_num_macs;
  54. table->total = 0;
  55. }
  56. void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
  57. {
  58. int i;
  59. mutex_init(&table->mutex);
  60. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  61. table->entries[i] = 0;
  62. table->refs[i] = 0;
  63. }
  64. table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
  65. table->total = 0;
  66. }
  67. static int validate_index(struct mlx4_dev *dev,
  68. struct mlx4_mac_table *table, int index)
  69. {
  70. int err = 0;
  71. if (index < 0 || index >= table->max || !table->entries[index]) {
  72. mlx4_warn(dev, "No valid Mac entry for the given index\n");
  73. err = -EINVAL;
  74. }
  75. return err;
  76. }
  77. static int find_index(struct mlx4_dev *dev,
  78. struct mlx4_mac_table *table, u64 mac)
  79. {
  80. int i;
  81. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  82. if ((mac & MLX4_MAC_MASK) ==
  83. (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
  84. return i;
  85. }
  86. /* Mac not found */
  87. return -EINVAL;
  88. }
  89. static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
  90. __be64 *entries)
  91. {
  92. struct mlx4_cmd_mailbox *mailbox;
  93. u32 in_mod;
  94. int err;
  95. mailbox = mlx4_alloc_cmd_mailbox(dev);
  96. if (IS_ERR(mailbox))
  97. return PTR_ERR(mailbox);
  98. memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
  99. in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
  100. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  101. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  102. mlx4_free_cmd_mailbox(dev, mailbox);
  103. return err;
  104. }
  105. int mlx4_find_cached_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *idx)
  106. {
  107. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  108. struct mlx4_mac_table *table = &info->mac_table;
  109. int i;
  110. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  111. if (!table->refs[i])
  112. continue;
  113. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  114. *idx = i;
  115. return 0;
  116. }
  117. }
  118. return -ENOENT;
  119. }
  120. EXPORT_SYMBOL_GPL(mlx4_find_cached_mac);
  121. int __mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  122. {
  123. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  124. struct mlx4_mac_table *table = &info->mac_table;
  125. int i, err = 0;
  126. int free = -1;
  127. mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d\n",
  128. (unsigned long long) mac, port);
  129. mutex_lock(&table->mutex);
  130. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  131. if (free < 0 && !table->entries[i]) {
  132. free = i;
  133. continue;
  134. }
  135. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  136. /* MAC already registered, increment ref count */
  137. err = i;
  138. ++table->refs[i];
  139. goto out;
  140. }
  141. }
  142. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  143. if (table->total == table->max) {
  144. /* No free mac entries */
  145. err = -ENOSPC;
  146. goto out;
  147. }
  148. /* Register new MAC */
  149. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  150. err = mlx4_set_port_mac_table(dev, port, table->entries);
  151. if (unlikely(err)) {
  152. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  153. (unsigned long long) mac);
  154. table->entries[free] = 0;
  155. goto out;
  156. }
  157. table->refs[free] = 1;
  158. err = free;
  159. ++table->total;
  160. out:
  161. mutex_unlock(&table->mutex);
  162. return err;
  163. }
  164. EXPORT_SYMBOL_GPL(__mlx4_register_mac);
  165. int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  166. {
  167. u64 out_param = 0;
  168. int err = -EINVAL;
  169. if (mlx4_is_mfunc(dev)) {
  170. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  171. err = mlx4_cmd_imm(dev, mac, &out_param,
  172. ((u32) port) << 8 | (u32) RES_MAC,
  173. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  174. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  175. }
  176. if (err && err == -EINVAL && mlx4_is_slave(dev)) {
  177. /* retry using old REG_MAC format */
  178. set_param_l(&out_param, port);
  179. err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  180. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  181. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  182. if (!err)
  183. dev->flags |= MLX4_FLAG_OLD_REG_MAC;
  184. }
  185. if (err)
  186. return err;
  187. return get_param_l(&out_param);
  188. }
  189. return __mlx4_register_mac(dev, port, mac);
  190. }
  191. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  192. int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
  193. {
  194. return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
  195. (port - 1) * (1 << dev->caps.log_num_macs);
  196. }
  197. EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
  198. void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  199. {
  200. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  201. struct mlx4_mac_table *table = &info->mac_table;
  202. int index;
  203. mutex_lock(&table->mutex);
  204. index = find_index(dev, table, mac);
  205. if (validate_index(dev, table, index))
  206. goto out;
  207. if (--table->refs[index]) {
  208. mlx4_dbg(dev, "Have more references for index %d,"
  209. "no need to modify mac table\n", index);
  210. goto out;
  211. }
  212. table->entries[index] = 0;
  213. mlx4_set_port_mac_table(dev, port, table->entries);
  214. --table->total;
  215. out:
  216. mutex_unlock(&table->mutex);
  217. }
  218. EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
  219. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  220. {
  221. u64 out_param = 0;
  222. if (mlx4_is_mfunc(dev)) {
  223. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  224. (void) mlx4_cmd_imm(dev, mac, &out_param,
  225. ((u32) port) << 8 | (u32) RES_MAC,
  226. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  227. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  228. } else {
  229. /* use old unregister mac format */
  230. set_param_l(&out_param, port);
  231. (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  232. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  233. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  234. }
  235. return;
  236. }
  237. __mlx4_unregister_mac(dev, port, mac);
  238. return;
  239. }
  240. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  241. int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
  242. {
  243. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  244. struct mlx4_mac_table *table = &info->mac_table;
  245. int index = qpn - info->base_qpn;
  246. int err = 0;
  247. /* CX1 doesn't support multi-functions */
  248. mutex_lock(&table->mutex);
  249. err = validate_index(dev, table, index);
  250. if (err)
  251. goto out;
  252. table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
  253. err = mlx4_set_port_mac_table(dev, port, table->entries);
  254. if (unlikely(err)) {
  255. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  256. (unsigned long long) new_mac);
  257. table->entries[index] = 0;
  258. }
  259. out:
  260. mutex_unlock(&table->mutex);
  261. return err;
  262. }
  263. EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
  264. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  265. __be32 *entries)
  266. {
  267. struct mlx4_cmd_mailbox *mailbox;
  268. u32 in_mod;
  269. int err;
  270. mailbox = mlx4_alloc_cmd_mailbox(dev);
  271. if (IS_ERR(mailbox))
  272. return PTR_ERR(mailbox);
  273. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  274. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  275. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  276. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  277. mlx4_free_cmd_mailbox(dev, mailbox);
  278. return err;
  279. }
  280. int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  281. {
  282. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  283. int i;
  284. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  285. if (table->refs[i] &&
  286. (vid == (MLX4_VLAN_MASK &
  287. be32_to_cpu(table->entries[i])))) {
  288. /* VLAN already registered, increase reference count */
  289. *idx = i;
  290. return 0;
  291. }
  292. }
  293. return -ENOENT;
  294. }
  295. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  296. int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
  297. int *index)
  298. {
  299. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  300. int i, err = 0;
  301. int free = -1;
  302. mutex_lock(&table->mutex);
  303. if (table->total == table->max) {
  304. /* No free vlan entries */
  305. err = -ENOSPC;
  306. goto out;
  307. }
  308. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  309. if (free < 0 && (table->refs[i] == 0)) {
  310. free = i;
  311. continue;
  312. }
  313. if (table->refs[i] &&
  314. (vlan == (MLX4_VLAN_MASK &
  315. be32_to_cpu(table->entries[i])))) {
  316. /* Vlan already registered, increase references count */
  317. *index = i;
  318. ++table->refs[i];
  319. goto out;
  320. }
  321. }
  322. if (free < 0) {
  323. err = -ENOMEM;
  324. goto out;
  325. }
  326. /* Register new VLAN */
  327. table->refs[free] = 1;
  328. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  329. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  330. if (unlikely(err)) {
  331. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  332. table->refs[free] = 0;
  333. table->entries[free] = 0;
  334. goto out;
  335. }
  336. *index = free;
  337. ++table->total;
  338. out:
  339. mutex_unlock(&table->mutex);
  340. return err;
  341. }
  342. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  343. {
  344. u64 out_param = 0;
  345. int err;
  346. if (vlan > 4095)
  347. return -EINVAL;
  348. if (mlx4_is_mfunc(dev)) {
  349. err = mlx4_cmd_imm(dev, vlan, &out_param,
  350. ((u32) port) << 8 | (u32) RES_VLAN,
  351. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  352. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  353. if (!err)
  354. *index = get_param_l(&out_param);
  355. return err;
  356. }
  357. return __mlx4_register_vlan(dev, port, vlan, index);
  358. }
  359. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  360. void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  361. {
  362. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  363. int index;
  364. mutex_lock(&table->mutex);
  365. if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
  366. mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
  367. goto out;
  368. }
  369. if (index < MLX4_VLAN_REGULAR) {
  370. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  371. goto out;
  372. }
  373. if (--table->refs[index]) {
  374. mlx4_dbg(dev, "Have %d more references for index %d,"
  375. "no need to modify vlan table\n", table->refs[index],
  376. index);
  377. goto out;
  378. }
  379. table->entries[index] = 0;
  380. mlx4_set_port_vlan_table(dev, port, table->entries);
  381. --table->total;
  382. out:
  383. mutex_unlock(&table->mutex);
  384. }
  385. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  386. {
  387. u64 out_param = 0;
  388. if (mlx4_is_mfunc(dev)) {
  389. (void) mlx4_cmd_imm(dev, vlan, &out_param,
  390. ((u32) port) << 8 | (u32) RES_VLAN,
  391. RES_OP_RESERVE_AND_MAP,
  392. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  393. MLX4_CMD_WRAPPED);
  394. return;
  395. }
  396. __mlx4_unregister_vlan(dev, port, vlan);
  397. }
  398. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  399. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  400. {
  401. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  402. u8 *inbuf, *outbuf;
  403. int err;
  404. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  405. if (IS_ERR(inmailbox))
  406. return PTR_ERR(inmailbox);
  407. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  408. if (IS_ERR(outmailbox)) {
  409. mlx4_free_cmd_mailbox(dev, inmailbox);
  410. return PTR_ERR(outmailbox);
  411. }
  412. inbuf = inmailbox->buf;
  413. outbuf = outmailbox->buf;
  414. inbuf[0] = 1;
  415. inbuf[1] = 1;
  416. inbuf[2] = 1;
  417. inbuf[3] = 1;
  418. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  419. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  420. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  421. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  422. MLX4_CMD_NATIVE);
  423. if (!err)
  424. *caps = *(__be32 *) (outbuf + 84);
  425. mlx4_free_cmd_mailbox(dev, inmailbox);
  426. mlx4_free_cmd_mailbox(dev, outmailbox);
  427. return err;
  428. }
  429. static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
  430. u8 op_mod, struct mlx4_cmd_mailbox *inbox)
  431. {
  432. struct mlx4_priv *priv = mlx4_priv(dev);
  433. struct mlx4_port_info *port_info;
  434. struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
  435. struct mlx4_slave_state *slave_st = &master->slave_state[slave];
  436. struct mlx4_set_port_rqp_calc_context *qpn_context;
  437. struct mlx4_set_port_general_context *gen_context;
  438. int reset_qkey_viols;
  439. int port;
  440. int is_eth;
  441. u32 in_modifier;
  442. u32 promisc;
  443. u16 mtu, prev_mtu;
  444. int err;
  445. int i;
  446. __be32 agg_cap_mask;
  447. __be32 slave_cap_mask;
  448. __be32 new_cap_mask;
  449. port = in_mod & 0xff;
  450. in_modifier = in_mod >> 8;
  451. is_eth = op_mod;
  452. port_info = &priv->port[port];
  453. /* Slaves cannot perform SET_PORT operations except changing MTU */
  454. if (is_eth) {
  455. if (slave != dev->caps.function &&
  456. in_modifier != MLX4_SET_PORT_GENERAL) {
  457. mlx4_warn(dev, "denying SET_PORT for slave:%d\n",
  458. slave);
  459. return -EINVAL;
  460. }
  461. switch (in_modifier) {
  462. case MLX4_SET_PORT_RQP_CALC:
  463. qpn_context = inbox->buf;
  464. qpn_context->base_qpn =
  465. cpu_to_be32(port_info->base_qpn);
  466. qpn_context->n_mac = 0x7;
  467. promisc = be32_to_cpu(qpn_context->promisc) >>
  468. SET_PORT_PROMISC_SHIFT;
  469. qpn_context->promisc = cpu_to_be32(
  470. promisc << SET_PORT_PROMISC_SHIFT |
  471. port_info->base_qpn);
  472. promisc = be32_to_cpu(qpn_context->mcast) >>
  473. SET_PORT_MC_PROMISC_SHIFT;
  474. qpn_context->mcast = cpu_to_be32(
  475. promisc << SET_PORT_MC_PROMISC_SHIFT |
  476. port_info->base_qpn);
  477. break;
  478. case MLX4_SET_PORT_GENERAL:
  479. gen_context = inbox->buf;
  480. /* Mtu is configured as the max MTU among all the
  481. * the functions on the port. */
  482. mtu = be16_to_cpu(gen_context->mtu);
  483. mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
  484. ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  485. prev_mtu = slave_st->mtu[port];
  486. slave_st->mtu[port] = mtu;
  487. if (mtu > master->max_mtu[port])
  488. master->max_mtu[port] = mtu;
  489. if (mtu < prev_mtu && prev_mtu ==
  490. master->max_mtu[port]) {
  491. slave_st->mtu[port] = mtu;
  492. master->max_mtu[port] = mtu;
  493. for (i = 0; i < dev->num_slaves; i++) {
  494. master->max_mtu[port] =
  495. max(master->max_mtu[port],
  496. master->slave_state[i].mtu[port]);
  497. }
  498. }
  499. gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
  500. break;
  501. }
  502. return mlx4_cmd(dev, inbox->dma, in_mod, op_mod,
  503. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  504. MLX4_CMD_NATIVE);
  505. }
  506. /* For IB, we only consider:
  507. * - The capability mask, which is set to the aggregate of all
  508. * slave function capabilities
  509. * - The QKey violatin counter - reset according to each request.
  510. */
  511. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  512. reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
  513. new_cap_mask = ((__be32 *) inbox->buf)[2];
  514. } else {
  515. reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
  516. new_cap_mask = ((__be32 *) inbox->buf)[1];
  517. }
  518. /* slave may not set the IS_SM capability for the port */
  519. if (slave != mlx4_master_func_num(dev) &&
  520. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
  521. return -EINVAL;
  522. /* No DEV_MGMT in multifunc mode */
  523. if (mlx4_is_mfunc(dev) &&
  524. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
  525. return -EINVAL;
  526. agg_cap_mask = 0;
  527. slave_cap_mask =
  528. priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  529. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
  530. for (i = 0; i < dev->num_slaves; i++)
  531. agg_cap_mask |=
  532. priv->mfunc.master.slave_state[i].ib_cap_mask[port];
  533. /* only clear mailbox for guests. Master may be setting
  534. * MTU or PKEY table size
  535. */
  536. if (slave != dev->caps.function)
  537. memset(inbox->buf, 0, 256);
  538. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  539. *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
  540. ((__be32 *) inbox->buf)[2] = agg_cap_mask;
  541. } else {
  542. ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
  543. ((__be32 *) inbox->buf)[1] = agg_cap_mask;
  544. }
  545. err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  546. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  547. if (err)
  548. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
  549. slave_cap_mask;
  550. return err;
  551. }
  552. int mlx4_SET_PORT_wrapper(struct mlx4_dev *dev, int slave,
  553. struct mlx4_vhcr *vhcr,
  554. struct mlx4_cmd_mailbox *inbox,
  555. struct mlx4_cmd_mailbox *outbox,
  556. struct mlx4_cmd_info *cmd)
  557. {
  558. return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
  559. vhcr->op_modifier, inbox);
  560. }
  561. /* bit locations for set port command with zero op modifier */
  562. enum {
  563. MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
  564. MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
  565. MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
  566. MLX4_CHANGE_PORT_VL_CAP = 21,
  567. MLX4_CHANGE_PORT_MTU_CAP = 22,
  568. };
  569. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
  570. {
  571. struct mlx4_cmd_mailbox *mailbox;
  572. int err, vl_cap, pkey_tbl_flag = 0;
  573. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  574. return 0;
  575. mailbox = mlx4_alloc_cmd_mailbox(dev);
  576. if (IS_ERR(mailbox))
  577. return PTR_ERR(mailbox);
  578. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  579. if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
  580. pkey_tbl_flag = 1;
  581. ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
  582. }
  583. /* IB VL CAP enum isn't used by the firmware, just numerical values */
  584. for (vl_cap = 8; vl_cap >= 1; vl_cap >>= 1) {
  585. ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
  586. (1 << MLX4_CHANGE_PORT_MTU_CAP) |
  587. (1 << MLX4_CHANGE_PORT_VL_CAP) |
  588. (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
  589. (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
  590. (vl_cap << MLX4_SET_PORT_VL_CAP));
  591. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  592. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  593. if (err != -ENOMEM)
  594. break;
  595. }
  596. mlx4_free_cmd_mailbox(dev, mailbox);
  597. return err;
  598. }
  599. int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
  600. u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
  601. {
  602. struct mlx4_cmd_mailbox *mailbox;
  603. struct mlx4_set_port_general_context *context;
  604. int err;
  605. u32 in_mod;
  606. mailbox = mlx4_alloc_cmd_mailbox(dev);
  607. if (IS_ERR(mailbox))
  608. return PTR_ERR(mailbox);
  609. context = mailbox->buf;
  610. context->flags = SET_PORT_GEN_ALL_VALID;
  611. context->mtu = cpu_to_be16(mtu);
  612. context->pptx = (pptx * (!pfctx)) << 7;
  613. context->pfctx = pfctx;
  614. context->pprx = (pprx * (!pfcrx)) << 7;
  615. context->pfcrx = pfcrx;
  616. in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
  617. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  618. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  619. mlx4_free_cmd_mailbox(dev, mailbox);
  620. return err;
  621. }
  622. EXPORT_SYMBOL(mlx4_SET_PORT_general);
  623. int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
  624. u8 promisc)
  625. {
  626. struct mlx4_cmd_mailbox *mailbox;
  627. struct mlx4_set_port_rqp_calc_context *context;
  628. int err;
  629. u32 in_mod;
  630. u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
  631. MCAST_DIRECT : MCAST_DEFAULT;
  632. if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  633. return 0;
  634. mailbox = mlx4_alloc_cmd_mailbox(dev);
  635. if (IS_ERR(mailbox))
  636. return PTR_ERR(mailbox);
  637. context = mailbox->buf;
  638. context->base_qpn = cpu_to_be32(base_qpn);
  639. context->n_mac = dev->caps.log_num_macs;
  640. context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
  641. base_qpn);
  642. context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
  643. base_qpn);
  644. context->intra_no_vlan = 0;
  645. context->no_vlan = MLX4_NO_VLAN_IDX;
  646. context->intra_vlan_miss = 0;
  647. context->vlan_miss = MLX4_VLAN_MISS_IDX;
  648. in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
  649. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  650. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  651. mlx4_free_cmd_mailbox(dev, mailbox);
  652. return err;
  653. }
  654. EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
  655. int mlx4_SET_PORT_PRIO2TC(struct mlx4_dev *dev, u8 port, u8 *prio2tc)
  656. {
  657. struct mlx4_cmd_mailbox *mailbox;
  658. struct mlx4_set_port_prio2tc_context *context;
  659. int err;
  660. u32 in_mod;
  661. int i;
  662. mailbox = mlx4_alloc_cmd_mailbox(dev);
  663. if (IS_ERR(mailbox))
  664. return PTR_ERR(mailbox);
  665. context = mailbox->buf;
  666. for (i = 0; i < MLX4_NUM_UP; i += 2)
  667. context->prio2tc[i >> 1] = prio2tc[i] << 4 | prio2tc[i + 1];
  668. in_mod = MLX4_SET_PORT_PRIO2TC << 8 | port;
  669. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  670. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  671. mlx4_free_cmd_mailbox(dev, mailbox);
  672. return err;
  673. }
  674. EXPORT_SYMBOL(mlx4_SET_PORT_PRIO2TC);
  675. int mlx4_SET_PORT_SCHEDULER(struct mlx4_dev *dev, u8 port, u8 *tc_tx_bw,
  676. u8 *pg, u16 *ratelimit)
  677. {
  678. struct mlx4_cmd_mailbox *mailbox;
  679. struct mlx4_set_port_scheduler_context *context;
  680. int err;
  681. u32 in_mod;
  682. int i;
  683. mailbox = mlx4_alloc_cmd_mailbox(dev);
  684. if (IS_ERR(mailbox))
  685. return PTR_ERR(mailbox);
  686. context = mailbox->buf;
  687. for (i = 0; i < MLX4_NUM_TC; i++) {
  688. struct mlx4_port_scheduler_tc_cfg_be *tc = &context->tc[i];
  689. u16 r = ratelimit && ratelimit[i] ? ratelimit[i] :
  690. MLX4_RATELIMIT_DEFAULT;
  691. tc->pg = htons(pg[i]);
  692. tc->bw_precentage = htons(tc_tx_bw[i]);
  693. tc->max_bw_units = htons(MLX4_RATELIMIT_UNITS);
  694. tc->max_bw_value = htons(r);
  695. }
  696. in_mod = MLX4_SET_PORT_SCHEDULER << 8 | port;
  697. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  698. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  699. mlx4_free_cmd_mailbox(dev, mailbox);
  700. return err;
  701. }
  702. EXPORT_SYMBOL(mlx4_SET_PORT_SCHEDULER);
  703. enum {
  704. VXLAN_ENABLE_MODIFY = 1 << 7,
  705. VXLAN_STEERING_MODIFY = 1 << 6,
  706. VXLAN_ENABLE = 1 << 7,
  707. };
  708. struct mlx4_set_port_vxlan_context {
  709. u32 reserved1;
  710. u8 modify_flags;
  711. u8 reserved2;
  712. u8 enable_flags;
  713. u8 steering;
  714. };
  715. int mlx4_SET_PORT_VXLAN(struct mlx4_dev *dev, u8 port, u8 steering)
  716. {
  717. int err;
  718. u32 in_mod;
  719. struct mlx4_cmd_mailbox *mailbox;
  720. struct mlx4_set_port_vxlan_context *context;
  721. mailbox = mlx4_alloc_cmd_mailbox(dev);
  722. if (IS_ERR(mailbox))
  723. return PTR_ERR(mailbox);
  724. context = mailbox->buf;
  725. memset(context, 0, sizeof(*context));
  726. context->modify_flags = VXLAN_ENABLE_MODIFY | VXLAN_STEERING_MODIFY;
  727. context->enable_flags = VXLAN_ENABLE;
  728. context->steering = steering;
  729. in_mod = MLX4_SET_PORT_VXLAN << 8 | port;
  730. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  731. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  732. mlx4_free_cmd_mailbox(dev, mailbox);
  733. return err;
  734. }
  735. EXPORT_SYMBOL(mlx4_SET_PORT_VXLAN);
  736. int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  737. struct mlx4_vhcr *vhcr,
  738. struct mlx4_cmd_mailbox *inbox,
  739. struct mlx4_cmd_mailbox *outbox,
  740. struct mlx4_cmd_info *cmd)
  741. {
  742. int err = 0;
  743. return err;
  744. }
  745. int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
  746. u64 mac, u64 clear, u8 mode)
  747. {
  748. return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
  749. MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
  750. MLX4_CMD_WRAPPED);
  751. }
  752. EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
  753. int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  754. struct mlx4_vhcr *vhcr,
  755. struct mlx4_cmd_mailbox *inbox,
  756. struct mlx4_cmd_mailbox *outbox,
  757. struct mlx4_cmd_info *cmd)
  758. {
  759. int err = 0;
  760. return err;
  761. }
  762. int mlx4_common_dump_eth_stats(struct mlx4_dev *dev, int slave,
  763. u32 in_mod, struct mlx4_cmd_mailbox *outbox)
  764. {
  765. return mlx4_cmd_box(dev, 0, outbox->dma, in_mod, 0,
  766. MLX4_CMD_DUMP_ETH_STATS, MLX4_CMD_TIME_CLASS_B,
  767. MLX4_CMD_NATIVE);
  768. }
  769. int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
  770. struct mlx4_vhcr *vhcr,
  771. struct mlx4_cmd_mailbox *inbox,
  772. struct mlx4_cmd_mailbox *outbox,
  773. struct mlx4_cmd_info *cmd)
  774. {
  775. if (slave != dev->caps.function)
  776. return 0;
  777. return mlx4_common_dump_eth_stats(dev, slave,
  778. vhcr->in_modifier, outbox);
  779. }
  780. void mlx4_set_stats_bitmap(struct mlx4_dev *dev, u64 *stats_bitmap)
  781. {
  782. if (!mlx4_is_mfunc(dev)) {
  783. *stats_bitmap = 0;
  784. return;
  785. }
  786. *stats_bitmap = (MLX4_STATS_TRAFFIC_COUNTERS_MASK |
  787. MLX4_STATS_TRAFFIC_DROPS_MASK |
  788. MLX4_STATS_PORT_COUNTERS_MASK);
  789. if (mlx4_is_master(dev))
  790. *stats_bitmap |= MLX4_STATS_ERROR_COUNTERS_MASK;
  791. }
  792. EXPORT_SYMBOL(mlx4_set_stats_bitmap);