port.c 23 KB

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