port.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. void mlx4_init_roce_gid_table(struct mlx4_dev *dev,
  68. struct mlx4_roce_gid_table *table)
  69. {
  70. int i;
  71. mutex_init(&table->mutex);
  72. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++)
  73. memset(table->roce_gids[i].raw, 0, MLX4_ROCE_GID_ENTRY_SIZE);
  74. }
  75. static int validate_index(struct mlx4_dev *dev,
  76. struct mlx4_mac_table *table, int index)
  77. {
  78. int err = 0;
  79. if (index < 0 || index >= table->max || !table->entries[index]) {
  80. mlx4_warn(dev, "No valid Mac entry for the given index\n");
  81. err = -EINVAL;
  82. }
  83. return err;
  84. }
  85. static int find_index(struct mlx4_dev *dev,
  86. struct mlx4_mac_table *table, u64 mac)
  87. {
  88. int i;
  89. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  90. if (table->refs[i] &&
  91. (MLX4_MAC_MASK & mac) ==
  92. (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
  93. return i;
  94. }
  95. /* Mac not found */
  96. return -EINVAL;
  97. }
  98. static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
  99. __be64 *entries)
  100. {
  101. struct mlx4_cmd_mailbox *mailbox;
  102. u32 in_mod;
  103. int err;
  104. mailbox = mlx4_alloc_cmd_mailbox(dev);
  105. if (IS_ERR(mailbox))
  106. return PTR_ERR(mailbox);
  107. memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
  108. in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
  109. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  110. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  111. mlx4_free_cmd_mailbox(dev, mailbox);
  112. return err;
  113. }
  114. int mlx4_find_cached_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *idx)
  115. {
  116. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  117. struct mlx4_mac_table *table = &info->mac_table;
  118. int i;
  119. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  120. if (!table->refs[i])
  121. continue;
  122. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  123. *idx = i;
  124. return 0;
  125. }
  126. }
  127. return -ENOENT;
  128. }
  129. EXPORT_SYMBOL_GPL(mlx4_find_cached_mac);
  130. int __mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  131. {
  132. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  133. struct mlx4_mac_table *table = &info->mac_table;
  134. int i, err = 0;
  135. int free = -1;
  136. mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d\n",
  137. (unsigned long long) mac, port);
  138. mutex_lock(&table->mutex);
  139. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  140. if (!table->refs[i]) {
  141. if (free < 0)
  142. free = i;
  143. continue;
  144. }
  145. if ((MLX4_MAC_MASK & mac) ==
  146. (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  147. /* MAC already registered, increment ref count */
  148. err = i;
  149. ++table->refs[i];
  150. goto out;
  151. }
  152. }
  153. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  154. if (table->total == table->max) {
  155. /* No free mac entries */
  156. err = -ENOSPC;
  157. goto out;
  158. }
  159. /* Register new MAC */
  160. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  161. err = mlx4_set_port_mac_table(dev, port, table->entries);
  162. if (unlikely(err)) {
  163. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  164. (unsigned long long) mac);
  165. table->entries[free] = 0;
  166. goto out;
  167. }
  168. table->refs[free] = 1;
  169. err = free;
  170. ++table->total;
  171. out:
  172. mutex_unlock(&table->mutex);
  173. return err;
  174. }
  175. EXPORT_SYMBOL_GPL(__mlx4_register_mac);
  176. int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  177. {
  178. u64 out_param = 0;
  179. int err = -EINVAL;
  180. if (mlx4_is_mfunc(dev)) {
  181. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  182. err = mlx4_cmd_imm(dev, mac, &out_param,
  183. ((u32) port) << 8 | (u32) RES_MAC,
  184. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  185. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  186. }
  187. if (err && err == -EINVAL && mlx4_is_slave(dev)) {
  188. /* retry using old REG_MAC format */
  189. set_param_l(&out_param, port);
  190. err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  191. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  192. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  193. if (!err)
  194. dev->flags |= MLX4_FLAG_OLD_REG_MAC;
  195. }
  196. if (err)
  197. return err;
  198. return get_param_l(&out_param);
  199. }
  200. return __mlx4_register_mac(dev, port, mac);
  201. }
  202. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  203. int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
  204. {
  205. return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
  206. (port - 1) * (1 << dev->caps.log_num_macs);
  207. }
  208. EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
  209. void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  210. {
  211. struct mlx4_port_info *info;
  212. struct mlx4_mac_table *table;
  213. int index;
  214. if (port < 1 || port > dev->caps.num_ports) {
  215. mlx4_warn(dev, "invalid port number (%d), aborting...\n", port);
  216. return;
  217. }
  218. info = &mlx4_priv(dev)->port[port];
  219. table = &info->mac_table;
  220. mutex_lock(&table->mutex);
  221. index = find_index(dev, table, mac);
  222. if (validate_index(dev, table, index))
  223. goto out;
  224. if (--table->refs[index]) {
  225. mlx4_dbg(dev, "Have more references for index %d, no need to modify mac table\n",
  226. index);
  227. goto out;
  228. }
  229. table->entries[index] = 0;
  230. mlx4_set_port_mac_table(dev, port, table->entries);
  231. --table->total;
  232. out:
  233. mutex_unlock(&table->mutex);
  234. }
  235. EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
  236. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  237. {
  238. u64 out_param = 0;
  239. if (mlx4_is_mfunc(dev)) {
  240. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  241. (void) mlx4_cmd_imm(dev, mac, &out_param,
  242. ((u32) port) << 8 | (u32) RES_MAC,
  243. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  244. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  245. } else {
  246. /* use old unregister mac format */
  247. set_param_l(&out_param, port);
  248. (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  249. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  250. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  251. }
  252. return;
  253. }
  254. __mlx4_unregister_mac(dev, port, mac);
  255. return;
  256. }
  257. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  258. int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
  259. {
  260. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  261. struct mlx4_mac_table *table = &info->mac_table;
  262. int index = qpn - info->base_qpn;
  263. int err = 0;
  264. /* CX1 doesn't support multi-functions */
  265. mutex_lock(&table->mutex);
  266. err = validate_index(dev, table, index);
  267. if (err)
  268. goto out;
  269. table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
  270. err = mlx4_set_port_mac_table(dev, port, table->entries);
  271. if (unlikely(err)) {
  272. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  273. (unsigned long long) new_mac);
  274. table->entries[index] = 0;
  275. }
  276. out:
  277. mutex_unlock(&table->mutex);
  278. return err;
  279. }
  280. EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
  281. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  282. __be32 *entries)
  283. {
  284. struct mlx4_cmd_mailbox *mailbox;
  285. u32 in_mod;
  286. int err;
  287. mailbox = mlx4_alloc_cmd_mailbox(dev);
  288. if (IS_ERR(mailbox))
  289. return PTR_ERR(mailbox);
  290. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  291. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  292. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  293. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  294. mlx4_free_cmd_mailbox(dev, mailbox);
  295. return err;
  296. }
  297. int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  298. {
  299. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  300. int i;
  301. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  302. if (table->refs[i] &&
  303. (vid == (MLX4_VLAN_MASK &
  304. be32_to_cpu(table->entries[i])))) {
  305. /* VLAN already registered, increase reference count */
  306. *idx = i;
  307. return 0;
  308. }
  309. }
  310. return -ENOENT;
  311. }
  312. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  313. int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
  314. int *index)
  315. {
  316. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  317. int i, err = 0;
  318. int free = -1;
  319. mutex_lock(&table->mutex);
  320. if (table->total == table->max) {
  321. /* No free vlan entries */
  322. err = -ENOSPC;
  323. goto out;
  324. }
  325. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  326. if (free < 0 && (table->refs[i] == 0)) {
  327. free = i;
  328. continue;
  329. }
  330. if (table->refs[i] &&
  331. (vlan == (MLX4_VLAN_MASK &
  332. be32_to_cpu(table->entries[i])))) {
  333. /* Vlan already registered, increase references count */
  334. *index = i;
  335. ++table->refs[i];
  336. goto out;
  337. }
  338. }
  339. if (free < 0) {
  340. err = -ENOMEM;
  341. goto out;
  342. }
  343. /* Register new VLAN */
  344. table->refs[free] = 1;
  345. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  346. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  347. if (unlikely(err)) {
  348. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  349. table->refs[free] = 0;
  350. table->entries[free] = 0;
  351. goto out;
  352. }
  353. *index = free;
  354. ++table->total;
  355. out:
  356. mutex_unlock(&table->mutex);
  357. return err;
  358. }
  359. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  360. {
  361. u64 out_param = 0;
  362. int err;
  363. if (vlan > 4095)
  364. return -EINVAL;
  365. if (mlx4_is_mfunc(dev)) {
  366. err = mlx4_cmd_imm(dev, vlan, &out_param,
  367. ((u32) port) << 8 | (u32) RES_VLAN,
  368. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  369. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  370. if (!err)
  371. *index = get_param_l(&out_param);
  372. return err;
  373. }
  374. return __mlx4_register_vlan(dev, port, vlan, index);
  375. }
  376. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  377. void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  378. {
  379. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  380. int index;
  381. mutex_lock(&table->mutex);
  382. if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
  383. mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
  384. goto out;
  385. }
  386. if (index < MLX4_VLAN_REGULAR) {
  387. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  388. goto out;
  389. }
  390. if (--table->refs[index]) {
  391. mlx4_dbg(dev, "Have %d more references for index %d, no need to modify vlan table\n",
  392. table->refs[index], index);
  393. goto out;
  394. }
  395. table->entries[index] = 0;
  396. mlx4_set_port_vlan_table(dev, port, table->entries);
  397. --table->total;
  398. out:
  399. mutex_unlock(&table->mutex);
  400. }
  401. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  402. {
  403. u64 out_param = 0;
  404. if (mlx4_is_mfunc(dev)) {
  405. (void) mlx4_cmd_imm(dev, vlan, &out_param,
  406. ((u32) port) << 8 | (u32) RES_VLAN,
  407. RES_OP_RESERVE_AND_MAP,
  408. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  409. MLX4_CMD_WRAPPED);
  410. return;
  411. }
  412. __mlx4_unregister_vlan(dev, port, vlan);
  413. }
  414. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  415. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  416. {
  417. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  418. u8 *inbuf, *outbuf;
  419. int err;
  420. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  421. if (IS_ERR(inmailbox))
  422. return PTR_ERR(inmailbox);
  423. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  424. if (IS_ERR(outmailbox)) {
  425. mlx4_free_cmd_mailbox(dev, inmailbox);
  426. return PTR_ERR(outmailbox);
  427. }
  428. inbuf = inmailbox->buf;
  429. outbuf = outmailbox->buf;
  430. inbuf[0] = 1;
  431. inbuf[1] = 1;
  432. inbuf[2] = 1;
  433. inbuf[3] = 1;
  434. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  435. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  436. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  437. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  438. MLX4_CMD_NATIVE);
  439. if (!err)
  440. *caps = *(__be32 *) (outbuf + 84);
  441. mlx4_free_cmd_mailbox(dev, inmailbox);
  442. mlx4_free_cmd_mailbox(dev, outmailbox);
  443. return err;
  444. }
  445. static struct mlx4_roce_gid_entry zgid_entry;
  446. int mlx4_get_slave_num_gids(struct mlx4_dev *dev, int slave, int port)
  447. {
  448. int vfs;
  449. int slave_gid = slave;
  450. unsigned i;
  451. struct mlx4_slaves_pport slaves_pport;
  452. struct mlx4_active_ports actv_ports;
  453. unsigned max_port_p_one;
  454. if (slave == 0)
  455. return MLX4_ROCE_PF_GIDS;
  456. /* Slave is a VF */
  457. slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
  458. actv_ports = mlx4_get_active_ports(dev, slave);
  459. max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
  460. bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
  461. for (i = 1; i < max_port_p_one; i++) {
  462. struct mlx4_active_ports exclusive_ports;
  463. struct mlx4_slaves_pport slaves_pport_actv;
  464. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  465. set_bit(i - 1, exclusive_ports.ports);
  466. if (i == port)
  467. continue;
  468. slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
  469. dev, &exclusive_ports);
  470. slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
  471. dev->persist->num_vfs + 1);
  472. }
  473. vfs = bitmap_weight(slaves_pport.slaves, dev->persist->num_vfs + 1) - 1;
  474. if (slave_gid <= ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) % vfs))
  475. return ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs) + 1;
  476. return (MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs;
  477. }
  478. int mlx4_get_base_gid_ix(struct mlx4_dev *dev, int slave, int port)
  479. {
  480. int gids;
  481. unsigned i;
  482. int slave_gid = slave;
  483. int vfs;
  484. struct mlx4_slaves_pport slaves_pport;
  485. struct mlx4_active_ports actv_ports;
  486. unsigned max_port_p_one;
  487. if (slave == 0)
  488. return 0;
  489. slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
  490. actv_ports = mlx4_get_active_ports(dev, slave);
  491. max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
  492. bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
  493. for (i = 1; i < max_port_p_one; i++) {
  494. struct mlx4_active_ports exclusive_ports;
  495. struct mlx4_slaves_pport slaves_pport_actv;
  496. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  497. set_bit(i - 1, exclusive_ports.ports);
  498. if (i == port)
  499. continue;
  500. slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
  501. dev, &exclusive_ports);
  502. slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
  503. dev->persist->num_vfs + 1);
  504. }
  505. gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
  506. vfs = bitmap_weight(slaves_pport.slaves, dev->persist->num_vfs + 1) - 1;
  507. if (slave_gid <= gids % vfs)
  508. return MLX4_ROCE_PF_GIDS + ((gids / vfs) + 1) * (slave_gid - 1);
  509. return MLX4_ROCE_PF_GIDS + (gids % vfs) +
  510. ((gids / vfs) * (slave_gid - 1));
  511. }
  512. EXPORT_SYMBOL_GPL(mlx4_get_base_gid_ix);
  513. static int mlx4_reset_roce_port_gids(struct mlx4_dev *dev, int slave,
  514. int port, struct mlx4_cmd_mailbox *mailbox)
  515. {
  516. struct mlx4_roce_gid_entry *gid_entry_mbox;
  517. struct mlx4_priv *priv = mlx4_priv(dev);
  518. int num_gids, base, offset;
  519. int i, err;
  520. num_gids = mlx4_get_slave_num_gids(dev, slave, port);
  521. base = mlx4_get_base_gid_ix(dev, slave, port);
  522. memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
  523. mutex_lock(&(priv->port[port].gid_table.mutex));
  524. /* Zero-out gids belonging to that slave in the port GID table */
  525. for (i = 0, offset = base; i < num_gids; offset++, i++)
  526. memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
  527. zgid_entry.raw, MLX4_ROCE_GID_ENTRY_SIZE);
  528. /* Now, copy roce port gids table to mailbox for passing to FW */
  529. gid_entry_mbox = (struct mlx4_roce_gid_entry *)mailbox->buf;
  530. for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
  531. memcpy(gid_entry_mbox->raw,
  532. priv->port[port].gid_table.roce_gids[i].raw,
  533. MLX4_ROCE_GID_ENTRY_SIZE);
  534. err = mlx4_cmd(dev, mailbox->dma,
  535. ((u32)port) | (MLX4_SET_PORT_GID_TABLE << 8), 1,
  536. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  537. MLX4_CMD_NATIVE);
  538. mutex_unlock(&(priv->port[port].gid_table.mutex));
  539. return err;
  540. }
  541. void mlx4_reset_roce_gids(struct mlx4_dev *dev, int slave)
  542. {
  543. struct mlx4_active_ports actv_ports;
  544. struct mlx4_cmd_mailbox *mailbox;
  545. int num_eth_ports, err;
  546. int i;
  547. if (slave < 0 || slave > dev->persist->num_vfs)
  548. return;
  549. actv_ports = mlx4_get_active_ports(dev, slave);
  550. for (i = 0, num_eth_ports = 0; i < dev->caps.num_ports; i++) {
  551. if (test_bit(i, actv_ports.ports)) {
  552. if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
  553. continue;
  554. num_eth_ports++;
  555. }
  556. }
  557. if (!num_eth_ports)
  558. return;
  559. /* have ETH ports. Alloc mailbox for SET_PORT command */
  560. mailbox = mlx4_alloc_cmd_mailbox(dev);
  561. if (IS_ERR(mailbox))
  562. return;
  563. for (i = 0; i < dev->caps.num_ports; i++) {
  564. if (test_bit(i, actv_ports.ports)) {
  565. if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
  566. continue;
  567. err = mlx4_reset_roce_port_gids(dev, slave, i + 1, mailbox);
  568. if (err)
  569. mlx4_warn(dev, "Could not reset ETH port GID table for slave %d, port %d (%d)\n",
  570. slave, i + 1, err);
  571. }
  572. }
  573. mlx4_free_cmd_mailbox(dev, mailbox);
  574. return;
  575. }
  576. static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
  577. u8 op_mod, struct mlx4_cmd_mailbox *inbox)
  578. {
  579. struct mlx4_priv *priv = mlx4_priv(dev);
  580. struct mlx4_port_info *port_info;
  581. struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
  582. struct mlx4_slave_state *slave_st = &master->slave_state[slave];
  583. struct mlx4_set_port_rqp_calc_context *qpn_context;
  584. struct mlx4_set_port_general_context *gen_context;
  585. struct mlx4_roce_gid_entry *gid_entry_tbl, *gid_entry_mbox, *gid_entry_mb1;
  586. int reset_qkey_viols;
  587. int port;
  588. int is_eth;
  589. int num_gids;
  590. int base;
  591. u32 in_modifier;
  592. u32 promisc;
  593. u16 mtu, prev_mtu;
  594. int err;
  595. int i, j;
  596. int offset;
  597. __be32 agg_cap_mask;
  598. __be32 slave_cap_mask;
  599. __be32 new_cap_mask;
  600. port = in_mod & 0xff;
  601. in_modifier = in_mod >> 8;
  602. is_eth = op_mod;
  603. port_info = &priv->port[port];
  604. /* Slaves cannot perform SET_PORT operations except changing MTU */
  605. if (is_eth) {
  606. if (slave != dev->caps.function &&
  607. in_modifier != MLX4_SET_PORT_GENERAL &&
  608. in_modifier != MLX4_SET_PORT_GID_TABLE) {
  609. mlx4_warn(dev, "denying SET_PORT for slave:%d\n",
  610. slave);
  611. return -EINVAL;
  612. }
  613. switch (in_modifier) {
  614. case MLX4_SET_PORT_RQP_CALC:
  615. qpn_context = inbox->buf;
  616. qpn_context->base_qpn =
  617. cpu_to_be32(port_info->base_qpn);
  618. qpn_context->n_mac = 0x7;
  619. promisc = be32_to_cpu(qpn_context->promisc) >>
  620. SET_PORT_PROMISC_SHIFT;
  621. qpn_context->promisc = cpu_to_be32(
  622. promisc << SET_PORT_PROMISC_SHIFT |
  623. port_info->base_qpn);
  624. promisc = be32_to_cpu(qpn_context->mcast) >>
  625. SET_PORT_MC_PROMISC_SHIFT;
  626. qpn_context->mcast = cpu_to_be32(
  627. promisc << SET_PORT_MC_PROMISC_SHIFT |
  628. port_info->base_qpn);
  629. break;
  630. case MLX4_SET_PORT_GENERAL:
  631. gen_context = inbox->buf;
  632. /* Mtu is configured as the max MTU among all the
  633. * the functions on the port. */
  634. mtu = be16_to_cpu(gen_context->mtu);
  635. mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
  636. ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  637. prev_mtu = slave_st->mtu[port];
  638. slave_st->mtu[port] = mtu;
  639. if (mtu > master->max_mtu[port])
  640. master->max_mtu[port] = mtu;
  641. if (mtu < prev_mtu && prev_mtu ==
  642. master->max_mtu[port]) {
  643. slave_st->mtu[port] = mtu;
  644. master->max_mtu[port] = mtu;
  645. for (i = 0; i < dev->num_slaves; i++) {
  646. master->max_mtu[port] =
  647. max(master->max_mtu[port],
  648. master->slave_state[i].mtu[port]);
  649. }
  650. }
  651. gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
  652. break;
  653. case MLX4_SET_PORT_GID_TABLE:
  654. /* change to MULTIPLE entries: number of guest's gids
  655. * need a FOR-loop here over number of gids the guest has.
  656. * 1. Check no duplicates in gids passed by slave
  657. */
  658. num_gids = mlx4_get_slave_num_gids(dev, slave, port);
  659. base = mlx4_get_base_gid_ix(dev, slave, port);
  660. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  661. for (i = 0; i < num_gids; gid_entry_mbox++, i++) {
  662. if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
  663. sizeof(zgid_entry)))
  664. continue;
  665. gid_entry_mb1 = gid_entry_mbox + 1;
  666. for (j = i + 1; j < num_gids; gid_entry_mb1++, j++) {
  667. if (!memcmp(gid_entry_mb1->raw,
  668. zgid_entry.raw, sizeof(zgid_entry)))
  669. continue;
  670. if (!memcmp(gid_entry_mb1->raw, gid_entry_mbox->raw,
  671. sizeof(gid_entry_mbox->raw))) {
  672. /* found duplicate */
  673. return -EINVAL;
  674. }
  675. }
  676. }
  677. /* 2. Check that do not have duplicates in OTHER
  678. * entries in the port GID table
  679. */
  680. mutex_lock(&(priv->port[port].gid_table.mutex));
  681. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
  682. if (i >= base && i < base + num_gids)
  683. continue; /* don't compare to slave's current gids */
  684. gid_entry_tbl = &priv->port[port].gid_table.roce_gids[i];
  685. if (!memcmp(gid_entry_tbl->raw, zgid_entry.raw, sizeof(zgid_entry)))
  686. continue;
  687. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  688. for (j = 0; j < num_gids; gid_entry_mbox++, j++) {
  689. if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
  690. sizeof(zgid_entry)))
  691. continue;
  692. if (!memcmp(gid_entry_mbox->raw, gid_entry_tbl->raw,
  693. sizeof(gid_entry_tbl->raw))) {
  694. /* found duplicate */
  695. mlx4_warn(dev, "requested gid entry for slave:%d is a duplicate of gid at index %d\n",
  696. slave, i);
  697. mutex_unlock(&(priv->port[port].gid_table.mutex));
  698. return -EINVAL;
  699. }
  700. }
  701. }
  702. /* insert slave GIDs with memcpy, starting at slave's base index */
  703. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  704. for (i = 0, offset = base; i < num_gids; gid_entry_mbox++, offset++, i++)
  705. memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
  706. gid_entry_mbox->raw, MLX4_ROCE_GID_ENTRY_SIZE);
  707. /* Now, copy roce port gids table to current mailbox for passing to FW */
  708. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  709. for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
  710. memcpy(gid_entry_mbox->raw,
  711. priv->port[port].gid_table.roce_gids[i].raw,
  712. MLX4_ROCE_GID_ENTRY_SIZE);
  713. err = mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
  714. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  715. MLX4_CMD_NATIVE);
  716. mutex_unlock(&(priv->port[port].gid_table.mutex));
  717. return err;
  718. }
  719. return mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
  720. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  721. MLX4_CMD_NATIVE);
  722. }
  723. /* For IB, we only consider:
  724. * - The capability mask, which is set to the aggregate of all
  725. * slave function capabilities
  726. * - The QKey violatin counter - reset according to each request.
  727. */
  728. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  729. reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
  730. new_cap_mask = ((__be32 *) inbox->buf)[2];
  731. } else {
  732. reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
  733. new_cap_mask = ((__be32 *) inbox->buf)[1];
  734. }
  735. /* slave may not set the IS_SM capability for the port */
  736. if (slave != mlx4_master_func_num(dev) &&
  737. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
  738. return -EINVAL;
  739. /* No DEV_MGMT in multifunc mode */
  740. if (mlx4_is_mfunc(dev) &&
  741. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
  742. return -EINVAL;
  743. agg_cap_mask = 0;
  744. slave_cap_mask =
  745. priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  746. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
  747. for (i = 0; i < dev->num_slaves; i++)
  748. agg_cap_mask |=
  749. priv->mfunc.master.slave_state[i].ib_cap_mask[port];
  750. /* only clear mailbox for guests. Master may be setting
  751. * MTU or PKEY table size
  752. */
  753. if (slave != dev->caps.function)
  754. memset(inbox->buf, 0, 256);
  755. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  756. *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
  757. ((__be32 *) inbox->buf)[2] = agg_cap_mask;
  758. } else {
  759. ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
  760. ((__be32 *) inbox->buf)[1] = agg_cap_mask;
  761. }
  762. err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  763. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  764. if (err)
  765. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
  766. slave_cap_mask;
  767. return err;
  768. }
  769. int mlx4_SET_PORT_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. int port = mlx4_slave_convert_port(
  776. dev, slave, vhcr->in_modifier & 0xFF);
  777. if (port < 0)
  778. return -EINVAL;
  779. vhcr->in_modifier = (vhcr->in_modifier & ~0xFF) |
  780. (port & 0xFF);
  781. return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
  782. vhcr->op_modifier, inbox);
  783. }
  784. /* bit locations for set port command with zero op modifier */
  785. enum {
  786. MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
  787. MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
  788. MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
  789. MLX4_CHANGE_PORT_VL_CAP = 21,
  790. MLX4_CHANGE_PORT_MTU_CAP = 22,
  791. };
  792. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
  793. {
  794. struct mlx4_cmd_mailbox *mailbox;
  795. int err, vl_cap, pkey_tbl_flag = 0;
  796. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  797. return 0;
  798. mailbox = mlx4_alloc_cmd_mailbox(dev);
  799. if (IS_ERR(mailbox))
  800. return PTR_ERR(mailbox);
  801. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  802. if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
  803. pkey_tbl_flag = 1;
  804. ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
  805. }
  806. /* IB VL CAP enum isn't used by the firmware, just numerical values */
  807. for (vl_cap = 8; vl_cap >= 1; vl_cap >>= 1) {
  808. ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
  809. (1 << MLX4_CHANGE_PORT_MTU_CAP) |
  810. (1 << MLX4_CHANGE_PORT_VL_CAP) |
  811. (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
  812. (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
  813. (vl_cap << MLX4_SET_PORT_VL_CAP));
  814. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  815. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  816. if (err != -ENOMEM)
  817. break;
  818. }
  819. mlx4_free_cmd_mailbox(dev, mailbox);
  820. return err;
  821. }
  822. int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
  823. u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
  824. {
  825. struct mlx4_cmd_mailbox *mailbox;
  826. struct mlx4_set_port_general_context *context;
  827. int err;
  828. u32 in_mod;
  829. mailbox = mlx4_alloc_cmd_mailbox(dev);
  830. if (IS_ERR(mailbox))
  831. return PTR_ERR(mailbox);
  832. context = mailbox->buf;
  833. context->flags = SET_PORT_GEN_ALL_VALID;
  834. context->mtu = cpu_to_be16(mtu);
  835. context->pptx = (pptx * (!pfctx)) << 7;
  836. context->pfctx = pfctx;
  837. context->pprx = (pprx * (!pfcrx)) << 7;
  838. context->pfcrx = pfcrx;
  839. in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
  840. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  841. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  842. mlx4_free_cmd_mailbox(dev, mailbox);
  843. return err;
  844. }
  845. EXPORT_SYMBOL(mlx4_SET_PORT_general);
  846. int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
  847. u8 promisc)
  848. {
  849. struct mlx4_cmd_mailbox *mailbox;
  850. struct mlx4_set_port_rqp_calc_context *context;
  851. int err;
  852. u32 in_mod;
  853. u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
  854. MCAST_DIRECT : MCAST_DEFAULT;
  855. if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  856. return 0;
  857. mailbox = mlx4_alloc_cmd_mailbox(dev);
  858. if (IS_ERR(mailbox))
  859. return PTR_ERR(mailbox);
  860. context = mailbox->buf;
  861. context->base_qpn = cpu_to_be32(base_qpn);
  862. context->n_mac = dev->caps.log_num_macs;
  863. context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
  864. base_qpn);
  865. context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
  866. base_qpn);
  867. context->intra_no_vlan = 0;
  868. context->no_vlan = MLX4_NO_VLAN_IDX;
  869. context->intra_vlan_miss = 0;
  870. context->vlan_miss = MLX4_VLAN_MISS_IDX;
  871. in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
  872. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  873. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  874. mlx4_free_cmd_mailbox(dev, mailbox);
  875. return err;
  876. }
  877. EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
  878. int mlx4_SET_PORT_PRIO2TC(struct mlx4_dev *dev, u8 port, u8 *prio2tc)
  879. {
  880. struct mlx4_cmd_mailbox *mailbox;
  881. struct mlx4_set_port_prio2tc_context *context;
  882. int err;
  883. u32 in_mod;
  884. int i;
  885. mailbox = mlx4_alloc_cmd_mailbox(dev);
  886. if (IS_ERR(mailbox))
  887. return PTR_ERR(mailbox);
  888. context = mailbox->buf;
  889. for (i = 0; i < MLX4_NUM_UP; i += 2)
  890. context->prio2tc[i >> 1] = prio2tc[i] << 4 | prio2tc[i + 1];
  891. in_mod = MLX4_SET_PORT_PRIO2TC << 8 | port;
  892. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  893. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  894. mlx4_free_cmd_mailbox(dev, mailbox);
  895. return err;
  896. }
  897. EXPORT_SYMBOL(mlx4_SET_PORT_PRIO2TC);
  898. int mlx4_SET_PORT_SCHEDULER(struct mlx4_dev *dev, u8 port, u8 *tc_tx_bw,
  899. u8 *pg, u16 *ratelimit)
  900. {
  901. struct mlx4_cmd_mailbox *mailbox;
  902. struct mlx4_set_port_scheduler_context *context;
  903. int err;
  904. u32 in_mod;
  905. int i;
  906. mailbox = mlx4_alloc_cmd_mailbox(dev);
  907. if (IS_ERR(mailbox))
  908. return PTR_ERR(mailbox);
  909. context = mailbox->buf;
  910. for (i = 0; i < MLX4_NUM_TC; i++) {
  911. struct mlx4_port_scheduler_tc_cfg_be *tc = &context->tc[i];
  912. u16 r;
  913. if (ratelimit && ratelimit[i]) {
  914. if (ratelimit[i] <= MLX4_MAX_100M_UNITS_VAL) {
  915. r = ratelimit[i];
  916. tc->max_bw_units =
  917. htons(MLX4_RATELIMIT_100M_UNITS);
  918. } else {
  919. r = ratelimit[i]/10;
  920. tc->max_bw_units =
  921. htons(MLX4_RATELIMIT_1G_UNITS);
  922. }
  923. tc->max_bw_value = htons(r);
  924. } else {
  925. tc->max_bw_value = htons(MLX4_RATELIMIT_DEFAULT);
  926. tc->max_bw_units = htons(MLX4_RATELIMIT_1G_UNITS);
  927. }
  928. tc->pg = htons(pg[i]);
  929. tc->bw_precentage = htons(tc_tx_bw[i]);
  930. }
  931. in_mod = MLX4_SET_PORT_SCHEDULER << 8 | port;
  932. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  933. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  934. mlx4_free_cmd_mailbox(dev, mailbox);
  935. return err;
  936. }
  937. EXPORT_SYMBOL(mlx4_SET_PORT_SCHEDULER);
  938. enum {
  939. VXLAN_ENABLE_MODIFY = 1 << 7,
  940. VXLAN_STEERING_MODIFY = 1 << 6,
  941. VXLAN_ENABLE = 1 << 7,
  942. };
  943. struct mlx4_set_port_vxlan_context {
  944. u32 reserved1;
  945. u8 modify_flags;
  946. u8 reserved2;
  947. u8 enable_flags;
  948. u8 steering;
  949. };
  950. int mlx4_SET_PORT_VXLAN(struct mlx4_dev *dev, u8 port, u8 steering, int enable)
  951. {
  952. int err;
  953. u32 in_mod;
  954. struct mlx4_cmd_mailbox *mailbox;
  955. struct mlx4_set_port_vxlan_context *context;
  956. mailbox = mlx4_alloc_cmd_mailbox(dev);
  957. if (IS_ERR(mailbox))
  958. return PTR_ERR(mailbox);
  959. context = mailbox->buf;
  960. memset(context, 0, sizeof(*context));
  961. context->modify_flags = VXLAN_ENABLE_MODIFY | VXLAN_STEERING_MODIFY;
  962. if (enable)
  963. context->enable_flags = VXLAN_ENABLE;
  964. context->steering = steering;
  965. in_mod = MLX4_SET_PORT_VXLAN << 8 | port;
  966. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  967. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  968. mlx4_free_cmd_mailbox(dev, mailbox);
  969. return err;
  970. }
  971. EXPORT_SYMBOL(mlx4_SET_PORT_VXLAN);
  972. int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  973. struct mlx4_vhcr *vhcr,
  974. struct mlx4_cmd_mailbox *inbox,
  975. struct mlx4_cmd_mailbox *outbox,
  976. struct mlx4_cmd_info *cmd)
  977. {
  978. int err = 0;
  979. return err;
  980. }
  981. int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
  982. u64 mac, u64 clear, u8 mode)
  983. {
  984. return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
  985. MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
  986. MLX4_CMD_WRAPPED);
  987. }
  988. EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
  989. int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  990. struct mlx4_vhcr *vhcr,
  991. struct mlx4_cmd_mailbox *inbox,
  992. struct mlx4_cmd_mailbox *outbox,
  993. struct mlx4_cmd_info *cmd)
  994. {
  995. int err = 0;
  996. return err;
  997. }
  998. int mlx4_common_dump_eth_stats(struct mlx4_dev *dev, int slave,
  999. u32 in_mod, struct mlx4_cmd_mailbox *outbox)
  1000. {
  1001. return mlx4_cmd_box(dev, 0, outbox->dma, in_mod, 0,
  1002. MLX4_CMD_DUMP_ETH_STATS, MLX4_CMD_TIME_CLASS_B,
  1003. MLX4_CMD_NATIVE);
  1004. }
  1005. int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
  1006. struct mlx4_vhcr *vhcr,
  1007. struct mlx4_cmd_mailbox *inbox,
  1008. struct mlx4_cmd_mailbox *outbox,
  1009. struct mlx4_cmd_info *cmd)
  1010. {
  1011. if (slave != dev->caps.function)
  1012. return 0;
  1013. return mlx4_common_dump_eth_stats(dev, slave,
  1014. vhcr->in_modifier, outbox);
  1015. }
  1016. void mlx4_set_stats_bitmap(struct mlx4_dev *dev, u64 *stats_bitmap)
  1017. {
  1018. if (!mlx4_is_mfunc(dev)) {
  1019. *stats_bitmap = 0;
  1020. return;
  1021. }
  1022. *stats_bitmap = (MLX4_STATS_TRAFFIC_COUNTERS_MASK |
  1023. MLX4_STATS_TRAFFIC_DROPS_MASK |
  1024. MLX4_STATS_PORT_COUNTERS_MASK);
  1025. if (mlx4_is_master(dev))
  1026. *stats_bitmap |= MLX4_STATS_ERROR_COUNTERS_MASK;
  1027. }
  1028. EXPORT_SYMBOL(mlx4_set_stats_bitmap);
  1029. int mlx4_get_slave_from_roce_gid(struct mlx4_dev *dev, int port, u8 *gid,
  1030. int *slave_id)
  1031. {
  1032. struct mlx4_priv *priv = mlx4_priv(dev);
  1033. int i, found_ix = -1;
  1034. int vf_gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
  1035. struct mlx4_slaves_pport slaves_pport;
  1036. unsigned num_vfs;
  1037. int slave_gid;
  1038. if (!mlx4_is_mfunc(dev))
  1039. return -EINVAL;
  1040. slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
  1041. num_vfs = bitmap_weight(slaves_pport.slaves,
  1042. dev->persist->num_vfs + 1) - 1;
  1043. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
  1044. if (!memcmp(priv->port[port].gid_table.roce_gids[i].raw, gid,
  1045. MLX4_ROCE_GID_ENTRY_SIZE)) {
  1046. found_ix = i;
  1047. break;
  1048. }
  1049. }
  1050. if (found_ix >= 0) {
  1051. /* Calculate a slave_gid which is the slave number in the gid
  1052. * table and not a globally unique slave number.
  1053. */
  1054. if (found_ix < MLX4_ROCE_PF_GIDS)
  1055. slave_gid = 0;
  1056. else if (found_ix < MLX4_ROCE_PF_GIDS + (vf_gids % num_vfs) *
  1057. (vf_gids / num_vfs + 1))
  1058. slave_gid = ((found_ix - MLX4_ROCE_PF_GIDS) /
  1059. (vf_gids / num_vfs + 1)) + 1;
  1060. else
  1061. slave_gid =
  1062. ((found_ix - MLX4_ROCE_PF_GIDS -
  1063. ((vf_gids % num_vfs) * ((vf_gids / num_vfs + 1)))) /
  1064. (vf_gids / num_vfs)) + vf_gids % num_vfs + 1;
  1065. /* Calculate the globally unique slave id */
  1066. if (slave_gid) {
  1067. struct mlx4_active_ports exclusive_ports;
  1068. struct mlx4_active_ports actv_ports;
  1069. struct mlx4_slaves_pport slaves_pport_actv;
  1070. unsigned max_port_p_one;
  1071. int num_vfs_before = 0;
  1072. int candidate_slave_gid;
  1073. /* Calculate how many VFs are on the previous port, if exists */
  1074. for (i = 1; i < port; i++) {
  1075. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  1076. set_bit(i - 1, exclusive_ports.ports);
  1077. slaves_pport_actv =
  1078. mlx4_phys_to_slaves_pport_actv(
  1079. dev, &exclusive_ports);
  1080. num_vfs_before += bitmap_weight(
  1081. slaves_pport_actv.slaves,
  1082. dev->persist->num_vfs + 1);
  1083. }
  1084. /* candidate_slave_gid isn't necessarily the correct slave, but
  1085. * it has the same number of ports and is assigned to the same
  1086. * ports as the real slave we're looking for. On dual port VF,
  1087. * slave_gid = [single port VFs on port <port>] +
  1088. * [offset of the current slave from the first dual port VF] +
  1089. * 1 (for the PF).
  1090. */
  1091. candidate_slave_gid = slave_gid + num_vfs_before;
  1092. actv_ports = mlx4_get_active_ports(dev, candidate_slave_gid);
  1093. max_port_p_one = find_first_bit(
  1094. actv_ports.ports, dev->caps.num_ports) +
  1095. bitmap_weight(actv_ports.ports,
  1096. dev->caps.num_ports) + 1;
  1097. /* Calculate the real slave number */
  1098. for (i = 1; i < max_port_p_one; i++) {
  1099. if (i == port)
  1100. continue;
  1101. bitmap_zero(exclusive_ports.ports,
  1102. dev->caps.num_ports);
  1103. set_bit(i - 1, exclusive_ports.ports);
  1104. slaves_pport_actv =
  1105. mlx4_phys_to_slaves_pport_actv(
  1106. dev, &exclusive_ports);
  1107. slave_gid += bitmap_weight(
  1108. slaves_pport_actv.slaves,
  1109. dev->persist->num_vfs + 1);
  1110. }
  1111. }
  1112. *slave_id = slave_gid;
  1113. }
  1114. return (found_ix >= 0) ? 0 : -EINVAL;
  1115. }
  1116. EXPORT_SYMBOL(mlx4_get_slave_from_roce_gid);
  1117. int mlx4_get_roce_gid_from_slave(struct mlx4_dev *dev, int port, int slave_id,
  1118. u8 *gid)
  1119. {
  1120. struct mlx4_priv *priv = mlx4_priv(dev);
  1121. if (!mlx4_is_master(dev))
  1122. return -EINVAL;
  1123. memcpy(gid, priv->port[port].gid_table.roce_gids[slave_id].raw,
  1124. MLX4_ROCE_GID_ENTRY_SIZE);
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL(mlx4_get_roce_gid_from_slave);
  1128. /* Cable Module Info */
  1129. #define MODULE_INFO_MAX_READ 48
  1130. #define I2C_ADDR_LOW 0x50
  1131. #define I2C_ADDR_HIGH 0x51
  1132. #define I2C_PAGE_SIZE 256
  1133. /* Module Info Data */
  1134. struct mlx4_cable_info {
  1135. u8 i2c_addr;
  1136. u8 page_num;
  1137. __be16 dev_mem_address;
  1138. __be16 reserved1;
  1139. __be16 size;
  1140. __be32 reserved2[2];
  1141. u8 data[MODULE_INFO_MAX_READ];
  1142. };
  1143. enum cable_info_err {
  1144. CABLE_INF_INV_PORT = 0x1,
  1145. CABLE_INF_OP_NOSUP = 0x2,
  1146. CABLE_INF_NOT_CONN = 0x3,
  1147. CABLE_INF_NO_EEPRM = 0x4,
  1148. CABLE_INF_PAGE_ERR = 0x5,
  1149. CABLE_INF_INV_ADDR = 0x6,
  1150. CABLE_INF_I2C_ADDR = 0x7,
  1151. CABLE_INF_QSFP_VIO = 0x8,
  1152. CABLE_INF_I2C_BUSY = 0x9,
  1153. };
  1154. #define MAD_STATUS_2_CABLE_ERR(mad_status) ((mad_status >> 8) & 0xFF)
  1155. static inline const char *cable_info_mad_err_str(u16 mad_status)
  1156. {
  1157. u8 err = MAD_STATUS_2_CABLE_ERR(mad_status);
  1158. switch (err) {
  1159. case CABLE_INF_INV_PORT:
  1160. return "invalid port selected";
  1161. case CABLE_INF_OP_NOSUP:
  1162. return "operation not supported for this port (the port is of type CX4 or internal)";
  1163. case CABLE_INF_NOT_CONN:
  1164. return "cable is not connected";
  1165. case CABLE_INF_NO_EEPRM:
  1166. return "the connected cable has no EPROM (passive copper cable)";
  1167. case CABLE_INF_PAGE_ERR:
  1168. return "page number is greater than 15";
  1169. case CABLE_INF_INV_ADDR:
  1170. return "invalid device_address or size (that is, size equals 0 or address+size is greater than 256)";
  1171. case CABLE_INF_I2C_ADDR:
  1172. return "invalid I2C slave address";
  1173. case CABLE_INF_QSFP_VIO:
  1174. return "at least one cable violates the QSFP specification and ignores the modsel signal";
  1175. case CABLE_INF_I2C_BUSY:
  1176. return "I2C bus is constantly busy";
  1177. }
  1178. return "Unknown Error";
  1179. }
  1180. /**
  1181. * mlx4_get_module_info - Read cable module eeprom data
  1182. * @dev: mlx4_dev.
  1183. * @port: port number.
  1184. * @offset: byte offset in eeprom to start reading data from.
  1185. * @size: num of bytes to read.
  1186. * @data: output buffer to put the requested data into.
  1187. *
  1188. * Reads cable module eeprom data, puts the outcome data into
  1189. * data pointer paramer.
  1190. * Returns num of read bytes on success or a negative error
  1191. * code.
  1192. */
  1193. int mlx4_get_module_info(struct mlx4_dev *dev, u8 port,
  1194. u16 offset, u16 size, u8 *data)
  1195. {
  1196. struct mlx4_cmd_mailbox *inbox, *outbox;
  1197. struct mlx4_mad_ifc *inmad, *outmad;
  1198. struct mlx4_cable_info *cable_info;
  1199. u16 i2c_addr;
  1200. int ret;
  1201. if (size > MODULE_INFO_MAX_READ)
  1202. size = MODULE_INFO_MAX_READ;
  1203. inbox = mlx4_alloc_cmd_mailbox(dev);
  1204. if (IS_ERR(inbox))
  1205. return PTR_ERR(inbox);
  1206. outbox = mlx4_alloc_cmd_mailbox(dev);
  1207. if (IS_ERR(outbox)) {
  1208. mlx4_free_cmd_mailbox(dev, inbox);
  1209. return PTR_ERR(outbox);
  1210. }
  1211. inmad = (struct mlx4_mad_ifc *)(inbox->buf);
  1212. outmad = (struct mlx4_mad_ifc *)(outbox->buf);
  1213. inmad->method = 0x1; /* Get */
  1214. inmad->class_version = 0x1;
  1215. inmad->mgmt_class = 0x1;
  1216. inmad->base_version = 0x1;
  1217. inmad->attr_id = cpu_to_be16(0xFF60); /* Module Info */
  1218. if (offset < I2C_PAGE_SIZE && offset + size > I2C_PAGE_SIZE)
  1219. /* Cross pages reads are not allowed
  1220. * read until offset 256 in low page
  1221. */
  1222. size -= offset + size - I2C_PAGE_SIZE;
  1223. i2c_addr = I2C_ADDR_LOW;
  1224. if (offset >= I2C_PAGE_SIZE) {
  1225. /* Reset offset to high page */
  1226. i2c_addr = I2C_ADDR_HIGH;
  1227. offset -= I2C_PAGE_SIZE;
  1228. }
  1229. cable_info = (struct mlx4_cable_info *)inmad->data;
  1230. cable_info->dev_mem_address = cpu_to_be16(offset);
  1231. cable_info->page_num = 0;
  1232. cable_info->i2c_addr = i2c_addr;
  1233. cable_info->size = cpu_to_be16(size);
  1234. ret = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  1235. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  1236. MLX4_CMD_NATIVE);
  1237. if (ret)
  1238. goto out;
  1239. if (be16_to_cpu(outmad->status)) {
  1240. /* Mad returned with bad status */
  1241. ret = be16_to_cpu(outmad->status);
  1242. mlx4_warn(dev,
  1243. "MLX4_CMD_MAD_IFC Get Module info attr(%x) port(%d) i2c_addr(%x) offset(%d) size(%d): Response Mad Status(%x) - %s\n",
  1244. 0xFF60, port, i2c_addr, offset, size,
  1245. ret, cable_info_mad_err_str(ret));
  1246. if (i2c_addr == I2C_ADDR_HIGH &&
  1247. MAD_STATUS_2_CABLE_ERR(ret) == CABLE_INF_I2C_ADDR)
  1248. /* Some SFP cables do not support i2c slave
  1249. * address 0x51 (high page), abort silently.
  1250. */
  1251. ret = 0;
  1252. else
  1253. ret = -ret;
  1254. goto out;
  1255. }
  1256. cable_info = (struct mlx4_cable_info *)outmad->data;
  1257. memcpy(data, cable_info->data, size);
  1258. ret = size;
  1259. out:
  1260. mlx4_free_cmd_mailbox(dev, inbox);
  1261. mlx4_free_cmd_mailbox(dev, outbox);
  1262. return ret;
  1263. }
  1264. EXPORT_SYMBOL(mlx4_get_module_info);