port.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  106. {
  107. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  108. struct mlx4_mac_table *table = &info->mac_table;
  109. int i, err = 0;
  110. int free = -1;
  111. mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d\n",
  112. (unsigned long long) mac, port);
  113. mutex_lock(&table->mutex);
  114. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  115. if (free < 0 && !table->entries[i]) {
  116. free = i;
  117. continue;
  118. }
  119. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  120. /* MAC already registered, increment ref count */
  121. err = i;
  122. ++table->refs[i];
  123. goto out;
  124. }
  125. }
  126. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  127. if (table->total == table->max) {
  128. /* No free mac entries */
  129. err = -ENOSPC;
  130. goto out;
  131. }
  132. /* Register new MAC */
  133. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  134. err = mlx4_set_port_mac_table(dev, port, table->entries);
  135. if (unlikely(err)) {
  136. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  137. (unsigned long long) mac);
  138. table->entries[free] = 0;
  139. goto out;
  140. }
  141. table->refs[free] = 1;
  142. err = free;
  143. ++table->total;
  144. out:
  145. mutex_unlock(&table->mutex);
  146. return err;
  147. }
  148. EXPORT_SYMBOL_GPL(__mlx4_register_mac);
  149. int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  150. {
  151. u64 out_param = 0;
  152. int err = -EINVAL;
  153. if (mlx4_is_mfunc(dev)) {
  154. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  155. err = mlx4_cmd_imm(dev, mac, &out_param,
  156. ((u32) port) << 8 | (u32) RES_MAC,
  157. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  158. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  159. }
  160. if (err && err == -EINVAL && mlx4_is_slave(dev)) {
  161. /* retry using old REG_MAC format */
  162. set_param_l(&out_param, port);
  163. err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  164. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  165. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  166. if (!err)
  167. dev->flags |= MLX4_FLAG_OLD_REG_MAC;
  168. }
  169. if (err)
  170. return err;
  171. return get_param_l(&out_param);
  172. }
  173. return __mlx4_register_mac(dev, port, mac);
  174. }
  175. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  176. int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
  177. {
  178. return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
  179. (port - 1) * (1 << dev->caps.log_num_macs);
  180. }
  181. EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
  182. void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  183. {
  184. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  185. struct mlx4_mac_table *table = &info->mac_table;
  186. int index;
  187. mutex_lock(&table->mutex);
  188. index = find_index(dev, table, mac);
  189. if (validate_index(dev, table, index))
  190. goto out;
  191. if (--table->refs[index]) {
  192. mlx4_dbg(dev, "Have more references for index %d,"
  193. "no need to modify mac table\n", index);
  194. goto out;
  195. }
  196. table->entries[index] = 0;
  197. mlx4_set_port_mac_table(dev, port, table->entries);
  198. --table->total;
  199. out:
  200. mutex_unlock(&table->mutex);
  201. }
  202. EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
  203. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  204. {
  205. u64 out_param = 0;
  206. if (mlx4_is_mfunc(dev)) {
  207. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  208. (void) mlx4_cmd_imm(dev, mac, &out_param,
  209. ((u32) port) << 8 | (u32) RES_MAC,
  210. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  211. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  212. } else {
  213. /* use old unregister mac format */
  214. set_param_l(&out_param, port);
  215. (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  216. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  217. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  218. }
  219. return;
  220. }
  221. __mlx4_unregister_mac(dev, port, mac);
  222. return;
  223. }
  224. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  225. int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
  226. {
  227. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  228. struct mlx4_mac_table *table = &info->mac_table;
  229. int index = qpn - info->base_qpn;
  230. int err = 0;
  231. /* CX1 doesn't support multi-functions */
  232. mutex_lock(&table->mutex);
  233. err = validate_index(dev, table, index);
  234. if (err)
  235. goto out;
  236. table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
  237. err = mlx4_set_port_mac_table(dev, port, table->entries);
  238. if (unlikely(err)) {
  239. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  240. (unsigned long long) new_mac);
  241. table->entries[index] = 0;
  242. }
  243. out:
  244. mutex_unlock(&table->mutex);
  245. return err;
  246. }
  247. EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
  248. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  249. __be32 *entries)
  250. {
  251. struct mlx4_cmd_mailbox *mailbox;
  252. u32 in_mod;
  253. int err;
  254. mailbox = mlx4_alloc_cmd_mailbox(dev);
  255. if (IS_ERR(mailbox))
  256. return PTR_ERR(mailbox);
  257. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  258. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  259. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  260. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  261. mlx4_free_cmd_mailbox(dev, mailbox);
  262. return err;
  263. }
  264. int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  265. {
  266. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  267. int i;
  268. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  269. if (table->refs[i] &&
  270. (vid == (MLX4_VLAN_MASK &
  271. be32_to_cpu(table->entries[i])))) {
  272. /* VLAN already registered, increase reference count */
  273. *idx = i;
  274. return 0;
  275. }
  276. }
  277. return -ENOENT;
  278. }
  279. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  280. int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
  281. int *index)
  282. {
  283. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  284. int i, err = 0;
  285. int free = -1;
  286. mutex_lock(&table->mutex);
  287. if (table->total == table->max) {
  288. /* No free vlan entries */
  289. err = -ENOSPC;
  290. goto out;
  291. }
  292. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  293. if (free < 0 && (table->refs[i] == 0)) {
  294. free = i;
  295. continue;
  296. }
  297. if (table->refs[i] &&
  298. (vlan == (MLX4_VLAN_MASK &
  299. be32_to_cpu(table->entries[i])))) {
  300. /* Vlan already registered, increase references count */
  301. *index = i;
  302. ++table->refs[i];
  303. goto out;
  304. }
  305. }
  306. if (free < 0) {
  307. err = -ENOMEM;
  308. goto out;
  309. }
  310. /* Register new VLAN */
  311. table->refs[free] = 1;
  312. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  313. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  314. if (unlikely(err)) {
  315. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  316. table->refs[free] = 0;
  317. table->entries[free] = 0;
  318. goto out;
  319. }
  320. *index = free;
  321. ++table->total;
  322. out:
  323. mutex_unlock(&table->mutex);
  324. return err;
  325. }
  326. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  327. {
  328. u64 out_param = 0;
  329. int err;
  330. if (vlan > 4095)
  331. return -EINVAL;
  332. if (mlx4_is_mfunc(dev)) {
  333. err = mlx4_cmd_imm(dev, vlan, &out_param,
  334. ((u32) port) << 8 | (u32) RES_VLAN,
  335. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  336. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  337. if (!err)
  338. *index = get_param_l(&out_param);
  339. return err;
  340. }
  341. return __mlx4_register_vlan(dev, port, vlan, index);
  342. }
  343. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  344. void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  345. {
  346. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  347. int index;
  348. mutex_lock(&table->mutex);
  349. if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
  350. mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
  351. goto out;
  352. }
  353. if (index < MLX4_VLAN_REGULAR) {
  354. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  355. goto out;
  356. }
  357. if (--table->refs[index]) {
  358. mlx4_dbg(dev, "Have %d more references for index %d,"
  359. "no need to modify vlan table\n", table->refs[index],
  360. index);
  361. goto out;
  362. }
  363. table->entries[index] = 0;
  364. mlx4_set_port_vlan_table(dev, port, table->entries);
  365. --table->total;
  366. out:
  367. mutex_unlock(&table->mutex);
  368. }
  369. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  370. {
  371. u64 out_param = 0;
  372. if (mlx4_is_mfunc(dev)) {
  373. (void) mlx4_cmd_imm(dev, vlan, &out_param,
  374. ((u32) port) << 8 | (u32) RES_VLAN,
  375. RES_OP_RESERVE_AND_MAP,
  376. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  377. MLX4_CMD_WRAPPED);
  378. return;
  379. }
  380. __mlx4_unregister_vlan(dev, port, vlan);
  381. }
  382. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  383. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  384. {
  385. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  386. u8 *inbuf, *outbuf;
  387. int err;
  388. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  389. if (IS_ERR(inmailbox))
  390. return PTR_ERR(inmailbox);
  391. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  392. if (IS_ERR(outmailbox)) {
  393. mlx4_free_cmd_mailbox(dev, inmailbox);
  394. return PTR_ERR(outmailbox);
  395. }
  396. inbuf = inmailbox->buf;
  397. outbuf = outmailbox->buf;
  398. inbuf[0] = 1;
  399. inbuf[1] = 1;
  400. inbuf[2] = 1;
  401. inbuf[3] = 1;
  402. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  403. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  404. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  405. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  406. MLX4_CMD_NATIVE);
  407. if (!err)
  408. *caps = *(__be32 *) (outbuf + 84);
  409. mlx4_free_cmd_mailbox(dev, inmailbox);
  410. mlx4_free_cmd_mailbox(dev, outmailbox);
  411. return err;
  412. }
  413. static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
  414. u8 op_mod, struct mlx4_cmd_mailbox *inbox)
  415. {
  416. struct mlx4_priv *priv = mlx4_priv(dev);
  417. struct mlx4_port_info *port_info;
  418. struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
  419. struct mlx4_slave_state *slave_st = &master->slave_state[slave];
  420. struct mlx4_set_port_rqp_calc_context *qpn_context;
  421. struct mlx4_set_port_general_context *gen_context;
  422. int reset_qkey_viols;
  423. int port;
  424. int is_eth;
  425. u32 in_modifier;
  426. u32 promisc;
  427. u16 mtu, prev_mtu;
  428. int err;
  429. int i;
  430. __be32 agg_cap_mask;
  431. __be32 slave_cap_mask;
  432. __be32 new_cap_mask;
  433. port = in_mod & 0xff;
  434. in_modifier = in_mod >> 8;
  435. is_eth = op_mod;
  436. port_info = &priv->port[port];
  437. /* Slaves cannot perform SET_PORT operations except changing MTU */
  438. if (is_eth) {
  439. if (slave != dev->caps.function &&
  440. in_modifier != MLX4_SET_PORT_GENERAL) {
  441. mlx4_warn(dev, "denying SET_PORT for slave:%d\n",
  442. slave);
  443. return -EINVAL;
  444. }
  445. switch (in_modifier) {
  446. case MLX4_SET_PORT_RQP_CALC:
  447. qpn_context = inbox->buf;
  448. qpn_context->base_qpn =
  449. cpu_to_be32(port_info->base_qpn);
  450. qpn_context->n_mac = 0x7;
  451. promisc = be32_to_cpu(qpn_context->promisc) >>
  452. SET_PORT_PROMISC_SHIFT;
  453. qpn_context->promisc = cpu_to_be32(
  454. promisc << SET_PORT_PROMISC_SHIFT |
  455. port_info->base_qpn);
  456. promisc = be32_to_cpu(qpn_context->mcast) >>
  457. SET_PORT_MC_PROMISC_SHIFT;
  458. qpn_context->mcast = cpu_to_be32(
  459. promisc << SET_PORT_MC_PROMISC_SHIFT |
  460. port_info->base_qpn);
  461. break;
  462. case MLX4_SET_PORT_GENERAL:
  463. gen_context = inbox->buf;
  464. /* Mtu is configured as the max MTU among all the
  465. * the functions on the port. */
  466. mtu = be16_to_cpu(gen_context->mtu);
  467. mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
  468. ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  469. prev_mtu = slave_st->mtu[port];
  470. slave_st->mtu[port] = mtu;
  471. if (mtu > master->max_mtu[port])
  472. master->max_mtu[port] = mtu;
  473. if (mtu < prev_mtu && prev_mtu ==
  474. master->max_mtu[port]) {
  475. slave_st->mtu[port] = mtu;
  476. master->max_mtu[port] = mtu;
  477. for (i = 0; i < dev->num_slaves; i++) {
  478. master->max_mtu[port] =
  479. max(master->max_mtu[port],
  480. master->slave_state[i].mtu[port]);
  481. }
  482. }
  483. gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
  484. break;
  485. }
  486. return mlx4_cmd(dev, inbox->dma, in_mod, op_mod,
  487. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  488. MLX4_CMD_NATIVE);
  489. }
  490. /* For IB, we only consider:
  491. * - The capability mask, which is set to the aggregate of all
  492. * slave function capabilities
  493. * - The QKey violatin counter - reset according to each request.
  494. */
  495. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  496. reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
  497. new_cap_mask = ((__be32 *) inbox->buf)[2];
  498. } else {
  499. reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
  500. new_cap_mask = ((__be32 *) inbox->buf)[1];
  501. }
  502. /* slave may not set the IS_SM capability for the port */
  503. if (slave != mlx4_master_func_num(dev) &&
  504. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
  505. return -EINVAL;
  506. /* No DEV_MGMT in multifunc mode */
  507. if (mlx4_is_mfunc(dev) &&
  508. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
  509. return -EINVAL;
  510. agg_cap_mask = 0;
  511. slave_cap_mask =
  512. priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  513. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
  514. for (i = 0; i < dev->num_slaves; i++)
  515. agg_cap_mask |=
  516. priv->mfunc.master.slave_state[i].ib_cap_mask[port];
  517. /* only clear mailbox for guests. Master may be setting
  518. * MTU or PKEY table size
  519. */
  520. if (slave != dev->caps.function)
  521. memset(inbox->buf, 0, 256);
  522. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  523. *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
  524. ((__be32 *) inbox->buf)[2] = agg_cap_mask;
  525. } else {
  526. ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
  527. ((__be32 *) inbox->buf)[1] = agg_cap_mask;
  528. }
  529. err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  530. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  531. if (err)
  532. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
  533. slave_cap_mask;
  534. return err;
  535. }
  536. int mlx4_SET_PORT_wrapper(struct mlx4_dev *dev, int slave,
  537. struct mlx4_vhcr *vhcr,
  538. struct mlx4_cmd_mailbox *inbox,
  539. struct mlx4_cmd_mailbox *outbox,
  540. struct mlx4_cmd_info *cmd)
  541. {
  542. return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
  543. vhcr->op_modifier, inbox);
  544. }
  545. /* bit locations for set port command with zero op modifier */
  546. enum {
  547. MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
  548. MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
  549. MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
  550. MLX4_CHANGE_PORT_VL_CAP = 21,
  551. MLX4_CHANGE_PORT_MTU_CAP = 22,
  552. };
  553. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
  554. {
  555. struct mlx4_cmd_mailbox *mailbox;
  556. int err, vl_cap, pkey_tbl_flag = 0;
  557. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  558. return 0;
  559. mailbox = mlx4_alloc_cmd_mailbox(dev);
  560. if (IS_ERR(mailbox))
  561. return PTR_ERR(mailbox);
  562. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  563. if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
  564. pkey_tbl_flag = 1;
  565. ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
  566. }
  567. /* IB VL CAP enum isn't used by the firmware, just numerical values */
  568. for (vl_cap = 8; vl_cap >= 1; vl_cap >>= 1) {
  569. ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
  570. (1 << MLX4_CHANGE_PORT_MTU_CAP) |
  571. (1 << MLX4_CHANGE_PORT_VL_CAP) |
  572. (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
  573. (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
  574. (vl_cap << MLX4_SET_PORT_VL_CAP));
  575. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  576. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  577. if (err != -ENOMEM)
  578. break;
  579. }
  580. mlx4_free_cmd_mailbox(dev, mailbox);
  581. return err;
  582. }
  583. int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
  584. u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
  585. {
  586. struct mlx4_cmd_mailbox *mailbox;
  587. struct mlx4_set_port_general_context *context;
  588. int err;
  589. u32 in_mod;
  590. mailbox = mlx4_alloc_cmd_mailbox(dev);
  591. if (IS_ERR(mailbox))
  592. return PTR_ERR(mailbox);
  593. context = mailbox->buf;
  594. context->flags = SET_PORT_GEN_ALL_VALID;
  595. context->mtu = cpu_to_be16(mtu);
  596. context->pptx = (pptx * (!pfctx)) << 7;
  597. context->pfctx = pfctx;
  598. context->pprx = (pprx * (!pfcrx)) << 7;
  599. context->pfcrx = pfcrx;
  600. in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
  601. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  602. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  603. mlx4_free_cmd_mailbox(dev, mailbox);
  604. return err;
  605. }
  606. EXPORT_SYMBOL(mlx4_SET_PORT_general);
  607. int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
  608. u8 promisc)
  609. {
  610. struct mlx4_cmd_mailbox *mailbox;
  611. struct mlx4_set_port_rqp_calc_context *context;
  612. int err;
  613. u32 in_mod;
  614. u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
  615. MCAST_DIRECT : MCAST_DEFAULT;
  616. if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  617. return 0;
  618. mailbox = mlx4_alloc_cmd_mailbox(dev);
  619. if (IS_ERR(mailbox))
  620. return PTR_ERR(mailbox);
  621. context = mailbox->buf;
  622. context->base_qpn = cpu_to_be32(base_qpn);
  623. context->n_mac = dev->caps.log_num_macs;
  624. context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
  625. base_qpn);
  626. context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
  627. base_qpn);
  628. context->intra_no_vlan = 0;
  629. context->no_vlan = MLX4_NO_VLAN_IDX;
  630. context->intra_vlan_miss = 0;
  631. context->vlan_miss = MLX4_VLAN_MISS_IDX;
  632. in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
  633. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  634. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  635. mlx4_free_cmd_mailbox(dev, mailbox);
  636. return err;
  637. }
  638. EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
  639. int mlx4_SET_PORT_PRIO2TC(struct mlx4_dev *dev, u8 port, u8 *prio2tc)
  640. {
  641. struct mlx4_cmd_mailbox *mailbox;
  642. struct mlx4_set_port_prio2tc_context *context;
  643. int err;
  644. u32 in_mod;
  645. int i;
  646. mailbox = mlx4_alloc_cmd_mailbox(dev);
  647. if (IS_ERR(mailbox))
  648. return PTR_ERR(mailbox);
  649. context = mailbox->buf;
  650. for (i = 0; i < MLX4_NUM_UP; i += 2)
  651. context->prio2tc[i >> 1] = prio2tc[i] << 4 | prio2tc[i + 1];
  652. in_mod = MLX4_SET_PORT_PRIO2TC << 8 | port;
  653. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  654. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  655. mlx4_free_cmd_mailbox(dev, mailbox);
  656. return err;
  657. }
  658. EXPORT_SYMBOL(mlx4_SET_PORT_PRIO2TC);
  659. int mlx4_SET_PORT_SCHEDULER(struct mlx4_dev *dev, u8 port, u8 *tc_tx_bw,
  660. u8 *pg, u16 *ratelimit)
  661. {
  662. struct mlx4_cmd_mailbox *mailbox;
  663. struct mlx4_set_port_scheduler_context *context;
  664. int err;
  665. u32 in_mod;
  666. int i;
  667. mailbox = mlx4_alloc_cmd_mailbox(dev);
  668. if (IS_ERR(mailbox))
  669. return PTR_ERR(mailbox);
  670. context = mailbox->buf;
  671. for (i = 0; i < MLX4_NUM_TC; i++) {
  672. struct mlx4_port_scheduler_tc_cfg_be *tc = &context->tc[i];
  673. u16 r = ratelimit && ratelimit[i] ? ratelimit[i] :
  674. MLX4_RATELIMIT_DEFAULT;
  675. tc->pg = htons(pg[i]);
  676. tc->bw_precentage = htons(tc_tx_bw[i]);
  677. tc->max_bw_units = htons(MLX4_RATELIMIT_UNITS);
  678. tc->max_bw_value = htons(r);
  679. }
  680. in_mod = MLX4_SET_PORT_SCHEDULER << 8 | port;
  681. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  682. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  683. mlx4_free_cmd_mailbox(dev, mailbox);
  684. return err;
  685. }
  686. EXPORT_SYMBOL(mlx4_SET_PORT_SCHEDULER);
  687. int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  688. struct mlx4_vhcr *vhcr,
  689. struct mlx4_cmd_mailbox *inbox,
  690. struct mlx4_cmd_mailbox *outbox,
  691. struct mlx4_cmd_info *cmd)
  692. {
  693. int err = 0;
  694. return err;
  695. }
  696. int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
  697. u64 mac, u64 clear, u8 mode)
  698. {
  699. return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
  700. MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
  701. MLX4_CMD_WRAPPED);
  702. }
  703. EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
  704. int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  705. struct mlx4_vhcr *vhcr,
  706. struct mlx4_cmd_mailbox *inbox,
  707. struct mlx4_cmd_mailbox *outbox,
  708. struct mlx4_cmd_info *cmd)
  709. {
  710. int err = 0;
  711. return err;
  712. }
  713. int mlx4_common_dump_eth_stats(struct mlx4_dev *dev, int slave,
  714. u32 in_mod, struct mlx4_cmd_mailbox *outbox)
  715. {
  716. return mlx4_cmd_box(dev, 0, outbox->dma, in_mod, 0,
  717. MLX4_CMD_DUMP_ETH_STATS, MLX4_CMD_TIME_CLASS_B,
  718. MLX4_CMD_NATIVE);
  719. }
  720. int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
  721. struct mlx4_vhcr *vhcr,
  722. struct mlx4_cmd_mailbox *inbox,
  723. struct mlx4_cmd_mailbox *outbox,
  724. struct mlx4_cmd_info *cmd)
  725. {
  726. if (slave != dev->caps.function)
  727. return 0;
  728. return mlx4_common_dump_eth_stats(dev, slave,
  729. vhcr->in_modifier, outbox);
  730. }
  731. void mlx4_set_stats_bitmap(struct mlx4_dev *dev, u64 *stats_bitmap)
  732. {
  733. if (!mlx4_is_mfunc(dev)) {
  734. *stats_bitmap = 0;
  735. return;
  736. }
  737. *stats_bitmap = (MLX4_STATS_TRAFFIC_COUNTERS_MASK |
  738. MLX4_STATS_TRAFFIC_DROPS_MASK |
  739. MLX4_STATS_PORT_COUNTERS_MASK);
  740. if (mlx4_is_master(dev))
  741. *stats_bitmap |= MLX4_STATS_ERROR_COUNTERS_MASK;
  742. }
  743. EXPORT_SYMBOL(mlx4_set_stats_bitmap);