port.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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. #include "mlx4_stats.h"
  39. #define MLX4_MAC_VALID (1ull << 63)
  40. #define MLX4_VLAN_VALID (1u << 31)
  41. #define MLX4_VLAN_MASK 0xfff
  42. #define MLX4_STATS_TRAFFIC_COUNTERS_MASK 0xfULL
  43. #define MLX4_STATS_TRAFFIC_DROPS_MASK 0xc0ULL
  44. #define MLX4_STATS_ERROR_COUNTERS_MASK 0x1ffc30ULL
  45. #define MLX4_STATS_PORT_COUNTERS_MASK 0x1fe00000ULL
  46. #define MLX4_FLAG_V_IGNORE_FCS_MASK 0x2
  47. #define MLX4_IGNORE_FCS_MASK 0x1
  48. void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
  49. {
  50. int i;
  51. mutex_init(&table->mutex);
  52. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  53. table->entries[i] = 0;
  54. table->refs[i] = 0;
  55. table->is_dup[i] = false;
  56. }
  57. table->max = 1 << dev->caps.log_num_macs;
  58. table->total = 0;
  59. }
  60. void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
  61. {
  62. int i;
  63. mutex_init(&table->mutex);
  64. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  65. table->entries[i] = 0;
  66. table->refs[i] = 0;
  67. table->is_dup[i] = false;
  68. }
  69. table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
  70. table->total = 0;
  71. }
  72. void mlx4_init_roce_gid_table(struct mlx4_dev *dev,
  73. struct mlx4_roce_gid_table *table)
  74. {
  75. int i;
  76. mutex_init(&table->mutex);
  77. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++)
  78. memset(table->roce_gids[i].raw, 0, MLX4_ROCE_GID_ENTRY_SIZE);
  79. }
  80. static int validate_index(struct mlx4_dev *dev,
  81. struct mlx4_mac_table *table, int index)
  82. {
  83. int err = 0;
  84. if (index < 0 || index >= table->max || !table->entries[index]) {
  85. mlx4_warn(dev, "No valid Mac entry for the given index\n");
  86. err = -EINVAL;
  87. }
  88. return err;
  89. }
  90. static int find_index(struct mlx4_dev *dev,
  91. struct mlx4_mac_table *table, u64 mac)
  92. {
  93. int i;
  94. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  95. if (table->refs[i] &&
  96. (MLX4_MAC_MASK & mac) ==
  97. (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
  98. return i;
  99. }
  100. /* Mac not found */
  101. return -EINVAL;
  102. }
  103. static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
  104. __be64 *entries)
  105. {
  106. struct mlx4_cmd_mailbox *mailbox;
  107. u32 in_mod;
  108. int err;
  109. mailbox = mlx4_alloc_cmd_mailbox(dev);
  110. if (IS_ERR(mailbox))
  111. return PTR_ERR(mailbox);
  112. memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
  113. in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
  114. err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
  115. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  116. MLX4_CMD_NATIVE);
  117. mlx4_free_cmd_mailbox(dev, mailbox);
  118. return err;
  119. }
  120. int mlx4_find_cached_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *idx)
  121. {
  122. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  123. struct mlx4_mac_table *table = &info->mac_table;
  124. int i;
  125. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  126. if (!table->refs[i])
  127. continue;
  128. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  129. *idx = i;
  130. return 0;
  131. }
  132. }
  133. return -ENOENT;
  134. }
  135. EXPORT_SYMBOL_GPL(mlx4_find_cached_mac);
  136. static bool mlx4_need_mf_bond(struct mlx4_dev *dev)
  137. {
  138. int i, num_eth_ports = 0;
  139. if (!mlx4_is_mfunc(dev))
  140. return false;
  141. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
  142. ++num_eth_ports;
  143. return (num_eth_ports == 2) ? true : false;
  144. }
  145. int __mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  146. {
  147. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  148. struct mlx4_mac_table *table = &info->mac_table;
  149. int i, err = 0;
  150. int free = -1;
  151. int free_for_dup = -1;
  152. bool dup = mlx4_is_mf_bonded(dev);
  153. u8 dup_port = (port == 1) ? 2 : 1;
  154. struct mlx4_mac_table *dup_table = &mlx4_priv(dev)->port[dup_port].mac_table;
  155. bool need_mf_bond = mlx4_need_mf_bond(dev);
  156. bool can_mf_bond = true;
  157. mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d %s duplicate\n",
  158. (unsigned long long)mac, port,
  159. dup ? "with" : "without");
  160. if (need_mf_bond) {
  161. if (port == 1) {
  162. mutex_lock(&table->mutex);
  163. mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
  164. } else {
  165. mutex_lock(&dup_table->mutex);
  166. mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
  167. }
  168. } else {
  169. mutex_lock(&table->mutex);
  170. }
  171. if (need_mf_bond) {
  172. int index_at_port = -1;
  173. int index_at_dup_port = -1;
  174. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  175. if (((MLX4_MAC_MASK & mac) == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))))
  176. index_at_port = i;
  177. if (((MLX4_MAC_MASK & mac) == (MLX4_MAC_MASK & be64_to_cpu(dup_table->entries[i]))))
  178. index_at_dup_port = i;
  179. }
  180. /* check that same mac is not in the tables at different indices */
  181. if ((index_at_port != index_at_dup_port) &&
  182. (index_at_port >= 0) &&
  183. (index_at_dup_port >= 0))
  184. can_mf_bond = false;
  185. /* If the mac is already in the primary table, the slot must be
  186. * available in the duplicate table as well.
  187. */
  188. if (index_at_port >= 0 && index_at_dup_port < 0 &&
  189. dup_table->refs[index_at_port]) {
  190. can_mf_bond = false;
  191. }
  192. /* If the mac is already in the duplicate table, check that the
  193. * corresponding index is not occupied in the primary table, or
  194. * the primary table already contains the mac at the same index.
  195. * Otherwise, you cannot bond (primary contains a different mac
  196. * at that index).
  197. */
  198. if (index_at_dup_port >= 0) {
  199. if (!table->refs[index_at_dup_port] ||
  200. ((MLX4_MAC_MASK & mac) == (MLX4_MAC_MASK & be64_to_cpu(table->entries[index_at_dup_port]))))
  201. free_for_dup = index_at_dup_port;
  202. else
  203. can_mf_bond = false;
  204. }
  205. }
  206. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  207. if (!table->refs[i]) {
  208. if (free < 0)
  209. free = i;
  210. if (free_for_dup < 0 && need_mf_bond && can_mf_bond) {
  211. if (!dup_table->refs[i])
  212. free_for_dup = i;
  213. }
  214. continue;
  215. }
  216. if ((MLX4_MAC_MASK & mac) ==
  217. (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  218. /* MAC already registered, increment ref count */
  219. err = i;
  220. ++table->refs[i];
  221. if (dup) {
  222. u64 dup_mac = MLX4_MAC_MASK & be64_to_cpu(dup_table->entries[i]);
  223. if (dup_mac != mac || !dup_table->is_dup[i]) {
  224. mlx4_warn(dev, "register mac: expect duplicate mac 0x%llx on port %d index %d\n",
  225. mac, dup_port, i);
  226. }
  227. }
  228. goto out;
  229. }
  230. }
  231. if (need_mf_bond && (free_for_dup < 0)) {
  232. if (dup) {
  233. mlx4_warn(dev, "Fail to allocate duplicate MAC table entry\n");
  234. mlx4_warn(dev, "High Availability for virtual functions may not work as expected\n");
  235. dup = false;
  236. }
  237. can_mf_bond = false;
  238. }
  239. if (need_mf_bond && can_mf_bond)
  240. free = free_for_dup;
  241. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  242. if (table->total == table->max) {
  243. /* No free mac entries */
  244. err = -ENOSPC;
  245. goto out;
  246. }
  247. /* Register new MAC */
  248. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  249. err = mlx4_set_port_mac_table(dev, port, table->entries);
  250. if (unlikely(err)) {
  251. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  252. (unsigned long long) mac);
  253. table->entries[free] = 0;
  254. goto out;
  255. }
  256. table->refs[free] = 1;
  257. table->is_dup[free] = false;
  258. ++table->total;
  259. if (dup) {
  260. dup_table->refs[free] = 0;
  261. dup_table->is_dup[free] = true;
  262. dup_table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  263. err = mlx4_set_port_mac_table(dev, dup_port, dup_table->entries);
  264. if (unlikely(err)) {
  265. mlx4_warn(dev, "Failed adding duplicate mac: 0x%llx\n", mac);
  266. dup_table->is_dup[free] = false;
  267. dup_table->entries[free] = 0;
  268. goto out;
  269. }
  270. ++dup_table->total;
  271. }
  272. err = free;
  273. out:
  274. if (need_mf_bond) {
  275. if (port == 2) {
  276. mutex_unlock(&table->mutex);
  277. mutex_unlock(&dup_table->mutex);
  278. } else {
  279. mutex_unlock(&dup_table->mutex);
  280. mutex_unlock(&table->mutex);
  281. }
  282. } else {
  283. mutex_unlock(&table->mutex);
  284. }
  285. return err;
  286. }
  287. EXPORT_SYMBOL_GPL(__mlx4_register_mac);
  288. int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  289. {
  290. u64 out_param = 0;
  291. int err = -EINVAL;
  292. if (mlx4_is_mfunc(dev)) {
  293. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  294. err = mlx4_cmd_imm(dev, mac, &out_param,
  295. ((u32) port) << 8 | (u32) RES_MAC,
  296. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  297. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  298. }
  299. if (err && err == -EINVAL && mlx4_is_slave(dev)) {
  300. /* retry using old REG_MAC format */
  301. set_param_l(&out_param, port);
  302. err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  303. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  304. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  305. if (!err)
  306. dev->flags |= MLX4_FLAG_OLD_REG_MAC;
  307. }
  308. if (err)
  309. return err;
  310. return get_param_l(&out_param);
  311. }
  312. return __mlx4_register_mac(dev, port, mac);
  313. }
  314. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  315. int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
  316. {
  317. return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
  318. (port - 1) * (1 << dev->caps.log_num_macs);
  319. }
  320. EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
  321. void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  322. {
  323. struct mlx4_port_info *info;
  324. struct mlx4_mac_table *table;
  325. int index;
  326. bool dup = mlx4_is_mf_bonded(dev);
  327. u8 dup_port = (port == 1) ? 2 : 1;
  328. struct mlx4_mac_table *dup_table = &mlx4_priv(dev)->port[dup_port].mac_table;
  329. if (port < 1 || port > dev->caps.num_ports) {
  330. mlx4_warn(dev, "invalid port number (%d), aborting...\n", port);
  331. return;
  332. }
  333. info = &mlx4_priv(dev)->port[port];
  334. table = &info->mac_table;
  335. if (dup) {
  336. if (port == 1) {
  337. mutex_lock(&table->mutex);
  338. mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
  339. } else {
  340. mutex_lock(&dup_table->mutex);
  341. mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
  342. }
  343. } else {
  344. mutex_lock(&table->mutex);
  345. }
  346. index = find_index(dev, table, mac);
  347. if (validate_index(dev, table, index))
  348. goto out;
  349. if (--table->refs[index] || table->is_dup[index]) {
  350. mlx4_dbg(dev, "Have more references for index %d, no need to modify mac table\n",
  351. index);
  352. if (!table->refs[index])
  353. dup_table->is_dup[index] = false;
  354. goto out;
  355. }
  356. table->entries[index] = 0;
  357. if (mlx4_set_port_mac_table(dev, port, table->entries))
  358. mlx4_warn(dev, "Fail to set mac in port %d during unregister\n", port);
  359. --table->total;
  360. if (dup) {
  361. dup_table->is_dup[index] = false;
  362. if (dup_table->refs[index])
  363. goto out;
  364. dup_table->entries[index] = 0;
  365. if (mlx4_set_port_mac_table(dev, dup_port, dup_table->entries))
  366. mlx4_warn(dev, "Fail to set mac in duplicate port %d during unregister\n", dup_port);
  367. --table->total;
  368. }
  369. out:
  370. if (dup) {
  371. if (port == 2) {
  372. mutex_unlock(&table->mutex);
  373. mutex_unlock(&dup_table->mutex);
  374. } else {
  375. mutex_unlock(&dup_table->mutex);
  376. mutex_unlock(&table->mutex);
  377. }
  378. } else {
  379. mutex_unlock(&table->mutex);
  380. }
  381. }
  382. EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
  383. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
  384. {
  385. u64 out_param = 0;
  386. if (mlx4_is_mfunc(dev)) {
  387. if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
  388. (void) mlx4_cmd_imm(dev, mac, &out_param,
  389. ((u32) port) << 8 | (u32) RES_MAC,
  390. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  391. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  392. } else {
  393. /* use old unregister mac format */
  394. set_param_l(&out_param, port);
  395. (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
  396. RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
  397. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  398. }
  399. return;
  400. }
  401. __mlx4_unregister_mac(dev, port, mac);
  402. return;
  403. }
  404. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  405. int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
  406. {
  407. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  408. struct mlx4_mac_table *table = &info->mac_table;
  409. int index = qpn - info->base_qpn;
  410. int err = 0;
  411. bool dup = mlx4_is_mf_bonded(dev);
  412. u8 dup_port = (port == 1) ? 2 : 1;
  413. struct mlx4_mac_table *dup_table = &mlx4_priv(dev)->port[dup_port].mac_table;
  414. /* CX1 doesn't support multi-functions */
  415. if (dup) {
  416. if (port == 1) {
  417. mutex_lock(&table->mutex);
  418. mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
  419. } else {
  420. mutex_lock(&dup_table->mutex);
  421. mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
  422. }
  423. } else {
  424. mutex_lock(&table->mutex);
  425. }
  426. err = validate_index(dev, table, index);
  427. if (err)
  428. goto out;
  429. table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
  430. err = mlx4_set_port_mac_table(dev, port, table->entries);
  431. if (unlikely(err)) {
  432. mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
  433. (unsigned long long) new_mac);
  434. table->entries[index] = 0;
  435. } else {
  436. if (dup) {
  437. dup_table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
  438. err = mlx4_set_port_mac_table(dev, dup_port, dup_table->entries);
  439. if (unlikely(err)) {
  440. mlx4_err(dev, "Failed adding duplicate MAC: 0x%llx\n",
  441. (unsigned long long)new_mac);
  442. dup_table->entries[index] = 0;
  443. }
  444. }
  445. }
  446. out:
  447. if (dup) {
  448. if (port == 2) {
  449. mutex_unlock(&table->mutex);
  450. mutex_unlock(&dup_table->mutex);
  451. } else {
  452. mutex_unlock(&dup_table->mutex);
  453. mutex_unlock(&table->mutex);
  454. }
  455. } else {
  456. mutex_unlock(&table->mutex);
  457. }
  458. return err;
  459. }
  460. EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
  461. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  462. __be32 *entries)
  463. {
  464. struct mlx4_cmd_mailbox *mailbox;
  465. u32 in_mod;
  466. int err;
  467. mailbox = mlx4_alloc_cmd_mailbox(dev);
  468. if (IS_ERR(mailbox))
  469. return PTR_ERR(mailbox);
  470. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  471. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  472. err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
  473. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  474. MLX4_CMD_NATIVE);
  475. mlx4_free_cmd_mailbox(dev, mailbox);
  476. return err;
  477. }
  478. int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  479. {
  480. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  481. int i;
  482. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  483. if (table->refs[i] &&
  484. (vid == (MLX4_VLAN_MASK &
  485. be32_to_cpu(table->entries[i])))) {
  486. /* VLAN already registered, increase reference count */
  487. *idx = i;
  488. return 0;
  489. }
  490. }
  491. return -ENOENT;
  492. }
  493. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  494. int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
  495. int *index)
  496. {
  497. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  498. int i, err = 0;
  499. int free = -1;
  500. int free_for_dup = -1;
  501. bool dup = mlx4_is_mf_bonded(dev);
  502. u8 dup_port = (port == 1) ? 2 : 1;
  503. struct mlx4_vlan_table *dup_table = &mlx4_priv(dev)->port[dup_port].vlan_table;
  504. bool need_mf_bond = mlx4_need_mf_bond(dev);
  505. bool can_mf_bond = true;
  506. mlx4_dbg(dev, "Registering VLAN: %d for port %d %s duplicate\n",
  507. vlan, port,
  508. dup ? "with" : "without");
  509. if (need_mf_bond) {
  510. if (port == 1) {
  511. mutex_lock(&table->mutex);
  512. mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
  513. } else {
  514. mutex_lock(&dup_table->mutex);
  515. mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
  516. }
  517. } else {
  518. mutex_lock(&table->mutex);
  519. }
  520. if (table->total == table->max) {
  521. /* No free vlan entries */
  522. err = -ENOSPC;
  523. goto out;
  524. }
  525. if (need_mf_bond) {
  526. int index_at_port = -1;
  527. int index_at_dup_port = -1;
  528. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  529. if ((vlan == (MLX4_VLAN_MASK & be32_to_cpu(table->entries[i]))))
  530. index_at_port = i;
  531. if ((vlan == (MLX4_VLAN_MASK & be32_to_cpu(dup_table->entries[i]))))
  532. index_at_dup_port = i;
  533. }
  534. /* check that same vlan is not in the tables at different indices */
  535. if ((index_at_port != index_at_dup_port) &&
  536. (index_at_port >= 0) &&
  537. (index_at_dup_port >= 0))
  538. can_mf_bond = false;
  539. /* If the vlan is already in the primary table, the slot must be
  540. * available in the duplicate table as well.
  541. */
  542. if (index_at_port >= 0 && index_at_dup_port < 0 &&
  543. dup_table->refs[index_at_port]) {
  544. can_mf_bond = false;
  545. }
  546. /* If the vlan is already in the duplicate table, check that the
  547. * corresponding index is not occupied in the primary table, or
  548. * the primary table already contains the vlan at the same index.
  549. * Otherwise, you cannot bond (primary contains a different vlan
  550. * at that index).
  551. */
  552. if (index_at_dup_port >= 0) {
  553. if (!table->refs[index_at_dup_port] ||
  554. (vlan == (MLX4_VLAN_MASK & be32_to_cpu(dup_table->entries[index_at_dup_port]))))
  555. free_for_dup = index_at_dup_port;
  556. else
  557. can_mf_bond = false;
  558. }
  559. }
  560. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  561. if (!table->refs[i]) {
  562. if (free < 0)
  563. free = i;
  564. if (free_for_dup < 0 && need_mf_bond && can_mf_bond) {
  565. if (!dup_table->refs[i])
  566. free_for_dup = i;
  567. }
  568. }
  569. if ((table->refs[i] || table->is_dup[i]) &&
  570. (vlan == (MLX4_VLAN_MASK &
  571. be32_to_cpu(table->entries[i])))) {
  572. /* Vlan already registered, increase references count */
  573. mlx4_dbg(dev, "vlan %u is already registered.\n", vlan);
  574. *index = i;
  575. ++table->refs[i];
  576. if (dup) {
  577. u16 dup_vlan = MLX4_VLAN_MASK & be32_to_cpu(dup_table->entries[i]);
  578. if (dup_vlan != vlan || !dup_table->is_dup[i]) {
  579. mlx4_warn(dev, "register vlan: expected duplicate vlan %u on port %d index %d\n",
  580. vlan, dup_port, i);
  581. }
  582. }
  583. goto out;
  584. }
  585. }
  586. if (need_mf_bond && (free_for_dup < 0)) {
  587. if (dup) {
  588. mlx4_warn(dev, "Fail to allocate duplicate VLAN table entry\n");
  589. mlx4_warn(dev, "High Availability for virtual functions may not work as expected\n");
  590. dup = false;
  591. }
  592. can_mf_bond = false;
  593. }
  594. if (need_mf_bond && can_mf_bond)
  595. free = free_for_dup;
  596. if (free < 0) {
  597. err = -ENOMEM;
  598. goto out;
  599. }
  600. /* Register new VLAN */
  601. table->refs[free] = 1;
  602. table->is_dup[free] = false;
  603. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  604. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  605. if (unlikely(err)) {
  606. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  607. table->refs[free] = 0;
  608. table->entries[free] = 0;
  609. goto out;
  610. }
  611. ++table->total;
  612. if (dup) {
  613. dup_table->refs[free] = 0;
  614. dup_table->is_dup[free] = true;
  615. dup_table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  616. err = mlx4_set_port_vlan_table(dev, dup_port, dup_table->entries);
  617. if (unlikely(err)) {
  618. mlx4_warn(dev, "Failed adding duplicate vlan: %u\n", vlan);
  619. dup_table->is_dup[free] = false;
  620. dup_table->entries[free] = 0;
  621. goto out;
  622. }
  623. ++dup_table->total;
  624. }
  625. *index = free;
  626. out:
  627. if (need_mf_bond) {
  628. if (port == 2) {
  629. mutex_unlock(&table->mutex);
  630. mutex_unlock(&dup_table->mutex);
  631. } else {
  632. mutex_unlock(&dup_table->mutex);
  633. mutex_unlock(&table->mutex);
  634. }
  635. } else {
  636. mutex_unlock(&table->mutex);
  637. }
  638. return err;
  639. }
  640. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  641. {
  642. u64 out_param = 0;
  643. int err;
  644. if (vlan > 4095)
  645. return -EINVAL;
  646. if (mlx4_is_mfunc(dev)) {
  647. err = mlx4_cmd_imm(dev, vlan, &out_param,
  648. ((u32) port) << 8 | (u32) RES_VLAN,
  649. RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
  650. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  651. if (!err)
  652. *index = get_param_l(&out_param);
  653. return err;
  654. }
  655. return __mlx4_register_vlan(dev, port, vlan, index);
  656. }
  657. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  658. void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  659. {
  660. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  661. int index;
  662. bool dup = mlx4_is_mf_bonded(dev);
  663. u8 dup_port = (port == 1) ? 2 : 1;
  664. struct mlx4_vlan_table *dup_table = &mlx4_priv(dev)->port[dup_port].vlan_table;
  665. if (dup) {
  666. if (port == 1) {
  667. mutex_lock(&table->mutex);
  668. mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
  669. } else {
  670. mutex_lock(&dup_table->mutex);
  671. mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
  672. }
  673. } else {
  674. mutex_lock(&table->mutex);
  675. }
  676. if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
  677. mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
  678. goto out;
  679. }
  680. if (index < MLX4_VLAN_REGULAR) {
  681. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  682. goto out;
  683. }
  684. if (--table->refs[index] || table->is_dup[index]) {
  685. mlx4_dbg(dev, "Have %d more references for index %d, no need to modify vlan table\n",
  686. table->refs[index], index);
  687. if (!table->refs[index])
  688. dup_table->is_dup[index] = false;
  689. goto out;
  690. }
  691. table->entries[index] = 0;
  692. if (mlx4_set_port_vlan_table(dev, port, table->entries))
  693. mlx4_warn(dev, "Fail to set vlan in port %d during unregister\n", port);
  694. --table->total;
  695. if (dup) {
  696. dup_table->is_dup[index] = false;
  697. if (dup_table->refs[index])
  698. goto out;
  699. dup_table->entries[index] = 0;
  700. if (mlx4_set_port_vlan_table(dev, dup_port, dup_table->entries))
  701. mlx4_warn(dev, "Fail to set vlan in duplicate port %d during unregister\n", dup_port);
  702. --dup_table->total;
  703. }
  704. out:
  705. if (dup) {
  706. if (port == 2) {
  707. mutex_unlock(&table->mutex);
  708. mutex_unlock(&dup_table->mutex);
  709. } else {
  710. mutex_unlock(&dup_table->mutex);
  711. mutex_unlock(&table->mutex);
  712. }
  713. } else {
  714. mutex_unlock(&table->mutex);
  715. }
  716. }
  717. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
  718. {
  719. u64 out_param = 0;
  720. if (mlx4_is_mfunc(dev)) {
  721. (void) mlx4_cmd_imm(dev, vlan, &out_param,
  722. ((u32) port) << 8 | (u32) RES_VLAN,
  723. RES_OP_RESERVE_AND_MAP,
  724. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  725. MLX4_CMD_WRAPPED);
  726. return;
  727. }
  728. __mlx4_unregister_vlan(dev, port, vlan);
  729. }
  730. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  731. int mlx4_bond_mac_table(struct mlx4_dev *dev)
  732. {
  733. struct mlx4_mac_table *t1 = &mlx4_priv(dev)->port[1].mac_table;
  734. struct mlx4_mac_table *t2 = &mlx4_priv(dev)->port[2].mac_table;
  735. int ret = 0;
  736. int i;
  737. bool update1 = false;
  738. bool update2 = false;
  739. mutex_lock(&t1->mutex);
  740. mutex_lock(&t2->mutex);
  741. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  742. if ((t1->entries[i] != t2->entries[i]) &&
  743. t1->entries[i] && t2->entries[i]) {
  744. mlx4_warn(dev, "can't duplicate entry %d in mac table\n", i);
  745. ret = -EINVAL;
  746. goto unlock;
  747. }
  748. }
  749. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  750. if (t1->entries[i] && !t2->entries[i]) {
  751. t2->entries[i] = t1->entries[i];
  752. t2->is_dup[i] = true;
  753. update2 = true;
  754. } else if (!t1->entries[i] && t2->entries[i]) {
  755. t1->entries[i] = t2->entries[i];
  756. t1->is_dup[i] = true;
  757. update1 = true;
  758. } else if (t1->entries[i] && t2->entries[i]) {
  759. t1->is_dup[i] = true;
  760. t2->is_dup[i] = true;
  761. }
  762. }
  763. if (update1) {
  764. ret = mlx4_set_port_mac_table(dev, 1, t1->entries);
  765. if (ret)
  766. mlx4_warn(dev, "failed to set MAC table for port 1 (%d)\n", ret);
  767. }
  768. if (!ret && update2) {
  769. ret = mlx4_set_port_mac_table(dev, 2, t2->entries);
  770. if (ret)
  771. mlx4_warn(dev, "failed to set MAC table for port 2 (%d)\n", ret);
  772. }
  773. if (ret)
  774. mlx4_warn(dev, "failed to create mirror MAC tables\n");
  775. unlock:
  776. mutex_unlock(&t2->mutex);
  777. mutex_unlock(&t1->mutex);
  778. return ret;
  779. }
  780. int mlx4_unbond_mac_table(struct mlx4_dev *dev)
  781. {
  782. struct mlx4_mac_table *t1 = &mlx4_priv(dev)->port[1].mac_table;
  783. struct mlx4_mac_table *t2 = &mlx4_priv(dev)->port[2].mac_table;
  784. int ret = 0;
  785. int ret1;
  786. int i;
  787. bool update1 = false;
  788. bool update2 = false;
  789. mutex_lock(&t1->mutex);
  790. mutex_lock(&t2->mutex);
  791. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  792. if (t1->entries[i] != t2->entries[i]) {
  793. mlx4_warn(dev, "mac table is in an unexpected state when trying to unbond\n");
  794. ret = -EINVAL;
  795. goto unlock;
  796. }
  797. }
  798. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  799. if (!t1->entries[i])
  800. continue;
  801. t1->is_dup[i] = false;
  802. if (!t1->refs[i]) {
  803. t1->entries[i] = 0;
  804. update1 = true;
  805. }
  806. t2->is_dup[i] = false;
  807. if (!t2->refs[i]) {
  808. t2->entries[i] = 0;
  809. update2 = true;
  810. }
  811. }
  812. if (update1) {
  813. ret = mlx4_set_port_mac_table(dev, 1, t1->entries);
  814. if (ret)
  815. mlx4_warn(dev, "failed to unmirror MAC tables for port 1(%d)\n", ret);
  816. }
  817. if (update2) {
  818. ret1 = mlx4_set_port_mac_table(dev, 2, t2->entries);
  819. if (ret1) {
  820. mlx4_warn(dev, "failed to unmirror MAC tables for port 2(%d)\n", ret1);
  821. ret = ret1;
  822. }
  823. }
  824. unlock:
  825. mutex_unlock(&t2->mutex);
  826. mutex_unlock(&t1->mutex);
  827. return ret;
  828. }
  829. int mlx4_bond_vlan_table(struct mlx4_dev *dev)
  830. {
  831. struct mlx4_vlan_table *t1 = &mlx4_priv(dev)->port[1].vlan_table;
  832. struct mlx4_vlan_table *t2 = &mlx4_priv(dev)->port[2].vlan_table;
  833. int ret = 0;
  834. int i;
  835. bool update1 = false;
  836. bool update2 = false;
  837. mutex_lock(&t1->mutex);
  838. mutex_lock(&t2->mutex);
  839. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  840. if ((t1->entries[i] != t2->entries[i]) &&
  841. t1->entries[i] && t2->entries[i]) {
  842. mlx4_warn(dev, "can't duplicate entry %d in vlan table\n", i);
  843. ret = -EINVAL;
  844. goto unlock;
  845. }
  846. }
  847. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  848. if (t1->entries[i] && !t2->entries[i]) {
  849. t2->entries[i] = t1->entries[i];
  850. t2->is_dup[i] = true;
  851. update2 = true;
  852. } else if (!t1->entries[i] && t2->entries[i]) {
  853. t1->entries[i] = t2->entries[i];
  854. t1->is_dup[i] = true;
  855. update1 = true;
  856. } else if (t1->entries[i] && t2->entries[i]) {
  857. t1->is_dup[i] = true;
  858. t2->is_dup[i] = true;
  859. }
  860. }
  861. if (update1) {
  862. ret = mlx4_set_port_vlan_table(dev, 1, t1->entries);
  863. if (ret)
  864. mlx4_warn(dev, "failed to set VLAN table for port 1 (%d)\n", ret);
  865. }
  866. if (!ret && update2) {
  867. ret = mlx4_set_port_vlan_table(dev, 2, t2->entries);
  868. if (ret)
  869. mlx4_warn(dev, "failed to set VLAN table for port 2 (%d)\n", ret);
  870. }
  871. if (ret)
  872. mlx4_warn(dev, "failed to create mirror VLAN tables\n");
  873. unlock:
  874. mutex_unlock(&t2->mutex);
  875. mutex_unlock(&t1->mutex);
  876. return ret;
  877. }
  878. int mlx4_unbond_vlan_table(struct mlx4_dev *dev)
  879. {
  880. struct mlx4_vlan_table *t1 = &mlx4_priv(dev)->port[1].vlan_table;
  881. struct mlx4_vlan_table *t2 = &mlx4_priv(dev)->port[2].vlan_table;
  882. int ret = 0;
  883. int ret1;
  884. int i;
  885. bool update1 = false;
  886. bool update2 = false;
  887. mutex_lock(&t1->mutex);
  888. mutex_lock(&t2->mutex);
  889. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  890. if (t1->entries[i] != t2->entries[i]) {
  891. mlx4_warn(dev, "vlan table is in an unexpected state when trying to unbond\n");
  892. ret = -EINVAL;
  893. goto unlock;
  894. }
  895. }
  896. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  897. if (!t1->entries[i])
  898. continue;
  899. t1->is_dup[i] = false;
  900. if (!t1->refs[i]) {
  901. t1->entries[i] = 0;
  902. update1 = true;
  903. }
  904. t2->is_dup[i] = false;
  905. if (!t2->refs[i]) {
  906. t2->entries[i] = 0;
  907. update2 = true;
  908. }
  909. }
  910. if (update1) {
  911. ret = mlx4_set_port_vlan_table(dev, 1, t1->entries);
  912. if (ret)
  913. mlx4_warn(dev, "failed to unmirror VLAN tables for port 1(%d)\n", ret);
  914. }
  915. if (update2) {
  916. ret1 = mlx4_set_port_vlan_table(dev, 2, t2->entries);
  917. if (ret1) {
  918. mlx4_warn(dev, "failed to unmirror VLAN tables for port 2(%d)\n", ret1);
  919. ret = ret1;
  920. }
  921. }
  922. unlock:
  923. mutex_unlock(&t2->mutex);
  924. mutex_unlock(&t1->mutex);
  925. return ret;
  926. }
  927. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  928. {
  929. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  930. u8 *inbuf, *outbuf;
  931. int err;
  932. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  933. if (IS_ERR(inmailbox))
  934. return PTR_ERR(inmailbox);
  935. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  936. if (IS_ERR(outmailbox)) {
  937. mlx4_free_cmd_mailbox(dev, inmailbox);
  938. return PTR_ERR(outmailbox);
  939. }
  940. inbuf = inmailbox->buf;
  941. outbuf = outmailbox->buf;
  942. inbuf[0] = 1;
  943. inbuf[1] = 1;
  944. inbuf[2] = 1;
  945. inbuf[3] = 1;
  946. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  947. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  948. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  949. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  950. MLX4_CMD_NATIVE);
  951. if (!err)
  952. *caps = *(__be32 *) (outbuf + 84);
  953. mlx4_free_cmd_mailbox(dev, inmailbox);
  954. mlx4_free_cmd_mailbox(dev, outmailbox);
  955. return err;
  956. }
  957. static struct mlx4_roce_gid_entry zgid_entry;
  958. int mlx4_get_slave_num_gids(struct mlx4_dev *dev, int slave, int port)
  959. {
  960. int vfs;
  961. int slave_gid = slave;
  962. unsigned i;
  963. struct mlx4_slaves_pport slaves_pport;
  964. struct mlx4_active_ports actv_ports;
  965. unsigned max_port_p_one;
  966. if (slave == 0)
  967. return MLX4_ROCE_PF_GIDS;
  968. /* Slave is a VF */
  969. slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
  970. actv_ports = mlx4_get_active_ports(dev, slave);
  971. max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
  972. bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
  973. for (i = 1; i < max_port_p_one; i++) {
  974. struct mlx4_active_ports exclusive_ports;
  975. struct mlx4_slaves_pport slaves_pport_actv;
  976. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  977. set_bit(i - 1, exclusive_ports.ports);
  978. if (i == port)
  979. continue;
  980. slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
  981. dev, &exclusive_ports);
  982. slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
  983. dev->persist->num_vfs + 1);
  984. }
  985. vfs = bitmap_weight(slaves_pport.slaves, dev->persist->num_vfs + 1) - 1;
  986. if (slave_gid <= ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) % vfs))
  987. return ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs) + 1;
  988. return (MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs;
  989. }
  990. int mlx4_get_base_gid_ix(struct mlx4_dev *dev, int slave, int port)
  991. {
  992. int gids;
  993. unsigned i;
  994. int slave_gid = slave;
  995. int vfs;
  996. struct mlx4_slaves_pport slaves_pport;
  997. struct mlx4_active_ports actv_ports;
  998. unsigned max_port_p_one;
  999. if (slave == 0)
  1000. return 0;
  1001. slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
  1002. actv_ports = mlx4_get_active_ports(dev, slave);
  1003. max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
  1004. bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
  1005. for (i = 1; i < max_port_p_one; i++) {
  1006. struct mlx4_active_ports exclusive_ports;
  1007. struct mlx4_slaves_pport slaves_pport_actv;
  1008. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  1009. set_bit(i - 1, exclusive_ports.ports);
  1010. if (i == port)
  1011. continue;
  1012. slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
  1013. dev, &exclusive_ports);
  1014. slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
  1015. dev->persist->num_vfs + 1);
  1016. }
  1017. gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
  1018. vfs = bitmap_weight(slaves_pport.slaves, dev->persist->num_vfs + 1) - 1;
  1019. if (slave_gid <= gids % vfs)
  1020. return MLX4_ROCE_PF_GIDS + ((gids / vfs) + 1) * (slave_gid - 1);
  1021. return MLX4_ROCE_PF_GIDS + (gids % vfs) +
  1022. ((gids / vfs) * (slave_gid - 1));
  1023. }
  1024. EXPORT_SYMBOL_GPL(mlx4_get_base_gid_ix);
  1025. static int mlx4_reset_roce_port_gids(struct mlx4_dev *dev, int slave,
  1026. int port, struct mlx4_cmd_mailbox *mailbox)
  1027. {
  1028. struct mlx4_roce_gid_entry *gid_entry_mbox;
  1029. struct mlx4_priv *priv = mlx4_priv(dev);
  1030. int num_gids, base, offset;
  1031. int i, err;
  1032. num_gids = mlx4_get_slave_num_gids(dev, slave, port);
  1033. base = mlx4_get_base_gid_ix(dev, slave, port);
  1034. memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
  1035. mutex_lock(&(priv->port[port].gid_table.mutex));
  1036. /* Zero-out gids belonging to that slave in the port GID table */
  1037. for (i = 0, offset = base; i < num_gids; offset++, i++)
  1038. memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
  1039. zgid_entry.raw, MLX4_ROCE_GID_ENTRY_SIZE);
  1040. /* Now, copy roce port gids table to mailbox for passing to FW */
  1041. gid_entry_mbox = (struct mlx4_roce_gid_entry *)mailbox->buf;
  1042. for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
  1043. memcpy(gid_entry_mbox->raw,
  1044. priv->port[port].gid_table.roce_gids[i].raw,
  1045. MLX4_ROCE_GID_ENTRY_SIZE);
  1046. err = mlx4_cmd(dev, mailbox->dma,
  1047. ((u32)port) | (MLX4_SET_PORT_GID_TABLE << 8),
  1048. MLX4_SET_PORT_ETH_OPCODE, MLX4_CMD_SET_PORT,
  1049. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  1050. mutex_unlock(&(priv->port[port].gid_table.mutex));
  1051. return err;
  1052. }
  1053. void mlx4_reset_roce_gids(struct mlx4_dev *dev, int slave)
  1054. {
  1055. struct mlx4_active_ports actv_ports;
  1056. struct mlx4_cmd_mailbox *mailbox;
  1057. int num_eth_ports, err;
  1058. int i;
  1059. if (slave < 0 || slave > dev->persist->num_vfs)
  1060. return;
  1061. actv_ports = mlx4_get_active_ports(dev, slave);
  1062. for (i = 0, num_eth_ports = 0; i < dev->caps.num_ports; i++) {
  1063. if (test_bit(i, actv_ports.ports)) {
  1064. if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
  1065. continue;
  1066. num_eth_ports++;
  1067. }
  1068. }
  1069. if (!num_eth_ports)
  1070. return;
  1071. /* have ETH ports. Alloc mailbox for SET_PORT command */
  1072. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1073. if (IS_ERR(mailbox))
  1074. return;
  1075. for (i = 0; i < dev->caps.num_ports; i++) {
  1076. if (test_bit(i, actv_ports.ports)) {
  1077. if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
  1078. continue;
  1079. err = mlx4_reset_roce_port_gids(dev, slave, i + 1, mailbox);
  1080. if (err)
  1081. mlx4_warn(dev, "Could not reset ETH port GID table for slave %d, port %d (%d)\n",
  1082. slave, i + 1, err);
  1083. }
  1084. }
  1085. mlx4_free_cmd_mailbox(dev, mailbox);
  1086. return;
  1087. }
  1088. static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
  1089. u8 op_mod, struct mlx4_cmd_mailbox *inbox)
  1090. {
  1091. struct mlx4_priv *priv = mlx4_priv(dev);
  1092. struct mlx4_port_info *port_info;
  1093. struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
  1094. struct mlx4_slave_state *slave_st = &master->slave_state[slave];
  1095. struct mlx4_set_port_rqp_calc_context *qpn_context;
  1096. struct mlx4_set_port_general_context *gen_context;
  1097. struct mlx4_roce_gid_entry *gid_entry_tbl, *gid_entry_mbox, *gid_entry_mb1;
  1098. int reset_qkey_viols;
  1099. int port;
  1100. int is_eth;
  1101. int num_gids;
  1102. int base;
  1103. u32 in_modifier;
  1104. u32 promisc;
  1105. u16 mtu, prev_mtu;
  1106. int err;
  1107. int i, j;
  1108. int offset;
  1109. __be32 agg_cap_mask;
  1110. __be32 slave_cap_mask;
  1111. __be32 new_cap_mask;
  1112. port = in_mod & 0xff;
  1113. in_modifier = in_mod >> 8;
  1114. is_eth = op_mod;
  1115. port_info = &priv->port[port];
  1116. /* Slaves cannot perform SET_PORT operations except changing MTU */
  1117. if (is_eth) {
  1118. if (slave != dev->caps.function &&
  1119. in_modifier != MLX4_SET_PORT_GENERAL &&
  1120. in_modifier != MLX4_SET_PORT_GID_TABLE) {
  1121. mlx4_warn(dev, "denying SET_PORT for slave:%d\n",
  1122. slave);
  1123. return -EINVAL;
  1124. }
  1125. switch (in_modifier) {
  1126. case MLX4_SET_PORT_RQP_CALC:
  1127. qpn_context = inbox->buf;
  1128. qpn_context->base_qpn =
  1129. cpu_to_be32(port_info->base_qpn);
  1130. qpn_context->n_mac = 0x7;
  1131. promisc = be32_to_cpu(qpn_context->promisc) >>
  1132. SET_PORT_PROMISC_SHIFT;
  1133. qpn_context->promisc = cpu_to_be32(
  1134. promisc << SET_PORT_PROMISC_SHIFT |
  1135. port_info->base_qpn);
  1136. promisc = be32_to_cpu(qpn_context->mcast) >>
  1137. SET_PORT_MC_PROMISC_SHIFT;
  1138. qpn_context->mcast = cpu_to_be32(
  1139. promisc << SET_PORT_MC_PROMISC_SHIFT |
  1140. port_info->base_qpn);
  1141. break;
  1142. case MLX4_SET_PORT_GENERAL:
  1143. gen_context = inbox->buf;
  1144. /* Mtu is configured as the max MTU among all the
  1145. * the functions on the port. */
  1146. mtu = be16_to_cpu(gen_context->mtu);
  1147. mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
  1148. ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  1149. prev_mtu = slave_st->mtu[port];
  1150. slave_st->mtu[port] = mtu;
  1151. if (mtu > master->max_mtu[port])
  1152. master->max_mtu[port] = mtu;
  1153. if (mtu < prev_mtu && prev_mtu ==
  1154. master->max_mtu[port]) {
  1155. slave_st->mtu[port] = mtu;
  1156. master->max_mtu[port] = mtu;
  1157. for (i = 0; i < dev->num_slaves; i++) {
  1158. master->max_mtu[port] =
  1159. max(master->max_mtu[port],
  1160. master->slave_state[i].mtu[port]);
  1161. }
  1162. }
  1163. gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
  1164. break;
  1165. case MLX4_SET_PORT_GID_TABLE:
  1166. /* change to MULTIPLE entries: number of guest's gids
  1167. * need a FOR-loop here over number of gids the guest has.
  1168. * 1. Check no duplicates in gids passed by slave
  1169. */
  1170. num_gids = mlx4_get_slave_num_gids(dev, slave, port);
  1171. base = mlx4_get_base_gid_ix(dev, slave, port);
  1172. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  1173. for (i = 0; i < num_gids; gid_entry_mbox++, i++) {
  1174. if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
  1175. sizeof(zgid_entry)))
  1176. continue;
  1177. gid_entry_mb1 = gid_entry_mbox + 1;
  1178. for (j = i + 1; j < num_gids; gid_entry_mb1++, j++) {
  1179. if (!memcmp(gid_entry_mb1->raw,
  1180. zgid_entry.raw, sizeof(zgid_entry)))
  1181. continue;
  1182. if (!memcmp(gid_entry_mb1->raw, gid_entry_mbox->raw,
  1183. sizeof(gid_entry_mbox->raw))) {
  1184. /* found duplicate */
  1185. return -EINVAL;
  1186. }
  1187. }
  1188. }
  1189. /* 2. Check that do not have duplicates in OTHER
  1190. * entries in the port GID table
  1191. */
  1192. mutex_lock(&(priv->port[port].gid_table.mutex));
  1193. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
  1194. if (i >= base && i < base + num_gids)
  1195. continue; /* don't compare to slave's current gids */
  1196. gid_entry_tbl = &priv->port[port].gid_table.roce_gids[i];
  1197. if (!memcmp(gid_entry_tbl->raw, zgid_entry.raw, sizeof(zgid_entry)))
  1198. continue;
  1199. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  1200. for (j = 0; j < num_gids; gid_entry_mbox++, j++) {
  1201. if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
  1202. sizeof(zgid_entry)))
  1203. continue;
  1204. if (!memcmp(gid_entry_mbox->raw, gid_entry_tbl->raw,
  1205. sizeof(gid_entry_tbl->raw))) {
  1206. /* found duplicate */
  1207. mlx4_warn(dev, "requested gid entry for slave:%d is a duplicate of gid at index %d\n",
  1208. slave, i);
  1209. mutex_unlock(&(priv->port[port].gid_table.mutex));
  1210. return -EINVAL;
  1211. }
  1212. }
  1213. }
  1214. /* insert slave GIDs with memcpy, starting at slave's base index */
  1215. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  1216. for (i = 0, offset = base; i < num_gids; gid_entry_mbox++, offset++, i++)
  1217. memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
  1218. gid_entry_mbox->raw, MLX4_ROCE_GID_ENTRY_SIZE);
  1219. /* Now, copy roce port gids table to current mailbox for passing to FW */
  1220. gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
  1221. for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
  1222. memcpy(gid_entry_mbox->raw,
  1223. priv->port[port].gid_table.roce_gids[i].raw,
  1224. MLX4_ROCE_GID_ENTRY_SIZE);
  1225. err = mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
  1226. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1227. MLX4_CMD_NATIVE);
  1228. mutex_unlock(&(priv->port[port].gid_table.mutex));
  1229. return err;
  1230. }
  1231. return mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
  1232. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1233. MLX4_CMD_NATIVE);
  1234. }
  1235. /* Slaves are not allowed to SET_PORT beacon (LED) blink */
  1236. if (op_mod == MLX4_SET_PORT_BEACON_OPCODE) {
  1237. mlx4_warn(dev, "denying SET_PORT Beacon slave:%d\n", slave);
  1238. return -EPERM;
  1239. }
  1240. /* For IB, we only consider:
  1241. * - The capability mask, which is set to the aggregate of all
  1242. * slave function capabilities
  1243. * - The QKey violatin counter - reset according to each request.
  1244. */
  1245. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  1246. reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
  1247. new_cap_mask = ((__be32 *) inbox->buf)[2];
  1248. } else {
  1249. reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
  1250. new_cap_mask = ((__be32 *) inbox->buf)[1];
  1251. }
  1252. /* slave may not set the IS_SM capability for the port */
  1253. if (slave != mlx4_master_func_num(dev) &&
  1254. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
  1255. return -EINVAL;
  1256. /* No DEV_MGMT in multifunc mode */
  1257. if (mlx4_is_mfunc(dev) &&
  1258. (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
  1259. return -EINVAL;
  1260. agg_cap_mask = 0;
  1261. slave_cap_mask =
  1262. priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  1263. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
  1264. for (i = 0; i < dev->num_slaves; i++)
  1265. agg_cap_mask |=
  1266. priv->mfunc.master.slave_state[i].ib_cap_mask[port];
  1267. /* only clear mailbox for guests. Master may be setting
  1268. * MTU or PKEY table size
  1269. */
  1270. if (slave != dev->caps.function)
  1271. memset(inbox->buf, 0, 256);
  1272. if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  1273. *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
  1274. ((__be32 *) inbox->buf)[2] = agg_cap_mask;
  1275. } else {
  1276. ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
  1277. ((__be32 *) inbox->buf)[1] = agg_cap_mask;
  1278. }
  1279. err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  1280. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  1281. if (err)
  1282. priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
  1283. slave_cap_mask;
  1284. return err;
  1285. }
  1286. int mlx4_SET_PORT_wrapper(struct mlx4_dev *dev, int slave,
  1287. struct mlx4_vhcr *vhcr,
  1288. struct mlx4_cmd_mailbox *inbox,
  1289. struct mlx4_cmd_mailbox *outbox,
  1290. struct mlx4_cmd_info *cmd)
  1291. {
  1292. int port = mlx4_slave_convert_port(
  1293. dev, slave, vhcr->in_modifier & 0xFF);
  1294. if (port < 0)
  1295. return -EINVAL;
  1296. vhcr->in_modifier = (vhcr->in_modifier & ~0xFF) |
  1297. (port & 0xFF);
  1298. return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
  1299. vhcr->op_modifier, inbox);
  1300. }
  1301. /* bit locations for set port command with zero op modifier */
  1302. enum {
  1303. MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
  1304. MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
  1305. MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
  1306. MLX4_CHANGE_PORT_VL_CAP = 21,
  1307. MLX4_CHANGE_PORT_MTU_CAP = 22,
  1308. };
  1309. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
  1310. {
  1311. struct mlx4_cmd_mailbox *mailbox;
  1312. int err, vl_cap, pkey_tbl_flag = 0;
  1313. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  1314. return 0;
  1315. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1316. if (IS_ERR(mailbox))
  1317. return PTR_ERR(mailbox);
  1318. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  1319. if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
  1320. pkey_tbl_flag = 1;
  1321. ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
  1322. }
  1323. /* IB VL CAP enum isn't used by the firmware, just numerical values */
  1324. for (vl_cap = 8; vl_cap >= 1; vl_cap >>= 1) {
  1325. ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
  1326. (1 << MLX4_CHANGE_PORT_MTU_CAP) |
  1327. (1 << MLX4_CHANGE_PORT_VL_CAP) |
  1328. (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
  1329. (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
  1330. (vl_cap << MLX4_SET_PORT_VL_CAP));
  1331. err = mlx4_cmd(dev, mailbox->dma, port,
  1332. MLX4_SET_PORT_IB_OPCODE, MLX4_CMD_SET_PORT,
  1333. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  1334. if (err != -ENOMEM)
  1335. break;
  1336. }
  1337. mlx4_free_cmd_mailbox(dev, mailbox);
  1338. return err;
  1339. }
  1340. #define SET_PORT_ROCE_2_FLAGS 0x10
  1341. #define MLX4_SET_PORT_ROCE_V1_V2 0x2
  1342. int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
  1343. u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
  1344. {
  1345. struct mlx4_cmd_mailbox *mailbox;
  1346. struct mlx4_set_port_general_context *context;
  1347. int err;
  1348. u32 in_mod;
  1349. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1350. if (IS_ERR(mailbox))
  1351. return PTR_ERR(mailbox);
  1352. context = mailbox->buf;
  1353. context->flags = SET_PORT_GEN_ALL_VALID;
  1354. context->mtu = cpu_to_be16(mtu);
  1355. context->pptx = (pptx * (!pfctx)) << 7;
  1356. context->pfctx = pfctx;
  1357. context->pprx = (pprx * (!pfcrx)) << 7;
  1358. context->pfcrx = pfcrx;
  1359. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  1360. context->flags |= SET_PORT_ROCE_2_FLAGS;
  1361. context->roce_mode |=
  1362. MLX4_SET_PORT_ROCE_V1_V2 << 4;
  1363. }
  1364. in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
  1365. err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
  1366. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1367. MLX4_CMD_WRAPPED);
  1368. mlx4_free_cmd_mailbox(dev, mailbox);
  1369. return err;
  1370. }
  1371. EXPORT_SYMBOL(mlx4_SET_PORT_general);
  1372. int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
  1373. u8 promisc)
  1374. {
  1375. struct mlx4_cmd_mailbox *mailbox;
  1376. struct mlx4_set_port_rqp_calc_context *context;
  1377. int err;
  1378. u32 in_mod;
  1379. u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
  1380. MCAST_DIRECT : MCAST_DEFAULT;
  1381. if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  1382. return 0;
  1383. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1384. if (IS_ERR(mailbox))
  1385. return PTR_ERR(mailbox);
  1386. context = mailbox->buf;
  1387. context->base_qpn = cpu_to_be32(base_qpn);
  1388. context->n_mac = dev->caps.log_num_macs;
  1389. context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
  1390. base_qpn);
  1391. context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
  1392. base_qpn);
  1393. context->intra_no_vlan = 0;
  1394. context->no_vlan = MLX4_NO_VLAN_IDX;
  1395. context->intra_vlan_miss = 0;
  1396. context->vlan_miss = MLX4_VLAN_MISS_IDX;
  1397. in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
  1398. err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
  1399. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1400. MLX4_CMD_WRAPPED);
  1401. mlx4_free_cmd_mailbox(dev, mailbox);
  1402. return err;
  1403. }
  1404. EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
  1405. int mlx4_SET_PORT_fcs_check(struct mlx4_dev *dev, u8 port, u8 ignore_fcs_value)
  1406. {
  1407. struct mlx4_cmd_mailbox *mailbox;
  1408. struct mlx4_set_port_general_context *context;
  1409. u32 in_mod;
  1410. int err;
  1411. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1412. if (IS_ERR(mailbox))
  1413. return PTR_ERR(mailbox);
  1414. context = mailbox->buf;
  1415. context->v_ignore_fcs |= MLX4_FLAG_V_IGNORE_FCS_MASK;
  1416. if (ignore_fcs_value)
  1417. context->ignore_fcs |= MLX4_IGNORE_FCS_MASK;
  1418. else
  1419. context->ignore_fcs &= ~MLX4_IGNORE_FCS_MASK;
  1420. in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
  1421. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  1422. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  1423. mlx4_free_cmd_mailbox(dev, mailbox);
  1424. return err;
  1425. }
  1426. EXPORT_SYMBOL(mlx4_SET_PORT_fcs_check);
  1427. enum {
  1428. VXLAN_ENABLE_MODIFY = 1 << 7,
  1429. VXLAN_STEERING_MODIFY = 1 << 6,
  1430. VXLAN_ENABLE = 1 << 7,
  1431. };
  1432. struct mlx4_set_port_vxlan_context {
  1433. u32 reserved1;
  1434. u8 modify_flags;
  1435. u8 reserved2;
  1436. u8 enable_flags;
  1437. u8 steering;
  1438. };
  1439. int mlx4_SET_PORT_VXLAN(struct mlx4_dev *dev, u8 port, u8 steering, int enable)
  1440. {
  1441. int err;
  1442. u32 in_mod;
  1443. struct mlx4_cmd_mailbox *mailbox;
  1444. struct mlx4_set_port_vxlan_context *context;
  1445. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1446. if (IS_ERR(mailbox))
  1447. return PTR_ERR(mailbox);
  1448. context = mailbox->buf;
  1449. memset(context, 0, sizeof(*context));
  1450. context->modify_flags = VXLAN_ENABLE_MODIFY | VXLAN_STEERING_MODIFY;
  1451. if (enable)
  1452. context->enable_flags = VXLAN_ENABLE;
  1453. context->steering = steering;
  1454. in_mod = MLX4_SET_PORT_VXLAN << 8 | port;
  1455. err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
  1456. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1457. MLX4_CMD_NATIVE);
  1458. mlx4_free_cmd_mailbox(dev, mailbox);
  1459. return err;
  1460. }
  1461. EXPORT_SYMBOL(mlx4_SET_PORT_VXLAN);
  1462. int mlx4_SET_PORT_BEACON(struct mlx4_dev *dev, u8 port, u16 time)
  1463. {
  1464. int err;
  1465. struct mlx4_cmd_mailbox *mailbox;
  1466. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1467. if (IS_ERR(mailbox))
  1468. return PTR_ERR(mailbox);
  1469. *((__be32 *)mailbox->buf) = cpu_to_be32(time);
  1470. err = mlx4_cmd(dev, mailbox->dma, port, MLX4_SET_PORT_BEACON_OPCODE,
  1471. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1472. MLX4_CMD_NATIVE);
  1473. mlx4_free_cmd_mailbox(dev, mailbox);
  1474. return err;
  1475. }
  1476. EXPORT_SYMBOL(mlx4_SET_PORT_BEACON);
  1477. int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  1478. struct mlx4_vhcr *vhcr,
  1479. struct mlx4_cmd_mailbox *inbox,
  1480. struct mlx4_cmd_mailbox *outbox,
  1481. struct mlx4_cmd_info *cmd)
  1482. {
  1483. int err = 0;
  1484. return err;
  1485. }
  1486. int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
  1487. u64 mac, u64 clear, u8 mode)
  1488. {
  1489. return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
  1490. MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
  1491. MLX4_CMD_WRAPPED);
  1492. }
  1493. EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
  1494. int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
  1495. struct mlx4_vhcr *vhcr,
  1496. struct mlx4_cmd_mailbox *inbox,
  1497. struct mlx4_cmd_mailbox *outbox,
  1498. struct mlx4_cmd_info *cmd)
  1499. {
  1500. int err = 0;
  1501. return err;
  1502. }
  1503. int mlx4_common_dump_eth_stats(struct mlx4_dev *dev, int slave,
  1504. u32 in_mod, struct mlx4_cmd_mailbox *outbox)
  1505. {
  1506. return mlx4_cmd_box(dev, 0, outbox->dma, in_mod, 0,
  1507. MLX4_CMD_DUMP_ETH_STATS, MLX4_CMD_TIME_CLASS_B,
  1508. MLX4_CMD_NATIVE);
  1509. }
  1510. int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
  1511. struct mlx4_vhcr *vhcr,
  1512. struct mlx4_cmd_mailbox *inbox,
  1513. struct mlx4_cmd_mailbox *outbox,
  1514. struct mlx4_cmd_info *cmd)
  1515. {
  1516. if (slave != dev->caps.function)
  1517. return 0;
  1518. return mlx4_common_dump_eth_stats(dev, slave,
  1519. vhcr->in_modifier, outbox);
  1520. }
  1521. int mlx4_get_slave_from_roce_gid(struct mlx4_dev *dev, int port, u8 *gid,
  1522. int *slave_id)
  1523. {
  1524. struct mlx4_priv *priv = mlx4_priv(dev);
  1525. int i, found_ix = -1;
  1526. int vf_gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
  1527. struct mlx4_slaves_pport slaves_pport;
  1528. unsigned num_vfs;
  1529. int slave_gid;
  1530. if (!mlx4_is_mfunc(dev))
  1531. return -EINVAL;
  1532. slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
  1533. num_vfs = bitmap_weight(slaves_pport.slaves,
  1534. dev->persist->num_vfs + 1) - 1;
  1535. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
  1536. if (!memcmp(priv->port[port].gid_table.roce_gids[i].raw, gid,
  1537. MLX4_ROCE_GID_ENTRY_SIZE)) {
  1538. found_ix = i;
  1539. break;
  1540. }
  1541. }
  1542. if (found_ix >= 0) {
  1543. /* Calculate a slave_gid which is the slave number in the gid
  1544. * table and not a globally unique slave number.
  1545. */
  1546. if (found_ix < MLX4_ROCE_PF_GIDS)
  1547. slave_gid = 0;
  1548. else if (found_ix < MLX4_ROCE_PF_GIDS + (vf_gids % num_vfs) *
  1549. (vf_gids / num_vfs + 1))
  1550. slave_gid = ((found_ix - MLX4_ROCE_PF_GIDS) /
  1551. (vf_gids / num_vfs + 1)) + 1;
  1552. else
  1553. slave_gid =
  1554. ((found_ix - MLX4_ROCE_PF_GIDS -
  1555. ((vf_gids % num_vfs) * ((vf_gids / num_vfs + 1)))) /
  1556. (vf_gids / num_vfs)) + vf_gids % num_vfs + 1;
  1557. /* Calculate the globally unique slave id */
  1558. if (slave_gid) {
  1559. struct mlx4_active_ports exclusive_ports;
  1560. struct mlx4_active_ports actv_ports;
  1561. struct mlx4_slaves_pport slaves_pport_actv;
  1562. unsigned max_port_p_one;
  1563. int num_vfs_before = 0;
  1564. int candidate_slave_gid;
  1565. /* Calculate how many VFs are on the previous port, if exists */
  1566. for (i = 1; i < port; i++) {
  1567. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  1568. set_bit(i - 1, exclusive_ports.ports);
  1569. slaves_pport_actv =
  1570. mlx4_phys_to_slaves_pport_actv(
  1571. dev, &exclusive_ports);
  1572. num_vfs_before += bitmap_weight(
  1573. slaves_pport_actv.slaves,
  1574. dev->persist->num_vfs + 1);
  1575. }
  1576. /* candidate_slave_gid isn't necessarily the correct slave, but
  1577. * it has the same number of ports and is assigned to the same
  1578. * ports as the real slave we're looking for. On dual port VF,
  1579. * slave_gid = [single port VFs on port <port>] +
  1580. * [offset of the current slave from the first dual port VF] +
  1581. * 1 (for the PF).
  1582. */
  1583. candidate_slave_gid = slave_gid + num_vfs_before;
  1584. actv_ports = mlx4_get_active_ports(dev, candidate_slave_gid);
  1585. max_port_p_one = find_first_bit(
  1586. actv_ports.ports, dev->caps.num_ports) +
  1587. bitmap_weight(actv_ports.ports,
  1588. dev->caps.num_ports) + 1;
  1589. /* Calculate the real slave number */
  1590. for (i = 1; i < max_port_p_one; i++) {
  1591. if (i == port)
  1592. continue;
  1593. bitmap_zero(exclusive_ports.ports,
  1594. dev->caps.num_ports);
  1595. set_bit(i - 1, exclusive_ports.ports);
  1596. slaves_pport_actv =
  1597. mlx4_phys_to_slaves_pport_actv(
  1598. dev, &exclusive_ports);
  1599. slave_gid += bitmap_weight(
  1600. slaves_pport_actv.slaves,
  1601. dev->persist->num_vfs + 1);
  1602. }
  1603. }
  1604. *slave_id = slave_gid;
  1605. }
  1606. return (found_ix >= 0) ? 0 : -EINVAL;
  1607. }
  1608. EXPORT_SYMBOL(mlx4_get_slave_from_roce_gid);
  1609. int mlx4_get_roce_gid_from_slave(struct mlx4_dev *dev, int port, int slave_id,
  1610. u8 *gid)
  1611. {
  1612. struct mlx4_priv *priv = mlx4_priv(dev);
  1613. if (!mlx4_is_master(dev))
  1614. return -EINVAL;
  1615. memcpy(gid, priv->port[port].gid_table.roce_gids[slave_id].raw,
  1616. MLX4_ROCE_GID_ENTRY_SIZE);
  1617. return 0;
  1618. }
  1619. EXPORT_SYMBOL(mlx4_get_roce_gid_from_slave);
  1620. /* Cable Module Info */
  1621. #define MODULE_INFO_MAX_READ 48
  1622. #define I2C_ADDR_LOW 0x50
  1623. #define I2C_ADDR_HIGH 0x51
  1624. #define I2C_PAGE_SIZE 256
  1625. /* Module Info Data */
  1626. struct mlx4_cable_info {
  1627. u8 i2c_addr;
  1628. u8 page_num;
  1629. __be16 dev_mem_address;
  1630. __be16 reserved1;
  1631. __be16 size;
  1632. __be32 reserved2[2];
  1633. u8 data[MODULE_INFO_MAX_READ];
  1634. };
  1635. enum cable_info_err {
  1636. CABLE_INF_INV_PORT = 0x1,
  1637. CABLE_INF_OP_NOSUP = 0x2,
  1638. CABLE_INF_NOT_CONN = 0x3,
  1639. CABLE_INF_NO_EEPRM = 0x4,
  1640. CABLE_INF_PAGE_ERR = 0x5,
  1641. CABLE_INF_INV_ADDR = 0x6,
  1642. CABLE_INF_I2C_ADDR = 0x7,
  1643. CABLE_INF_QSFP_VIO = 0x8,
  1644. CABLE_INF_I2C_BUSY = 0x9,
  1645. };
  1646. #define MAD_STATUS_2_CABLE_ERR(mad_status) ((mad_status >> 8) & 0xFF)
  1647. static inline const char *cable_info_mad_err_str(u16 mad_status)
  1648. {
  1649. u8 err = MAD_STATUS_2_CABLE_ERR(mad_status);
  1650. switch (err) {
  1651. case CABLE_INF_INV_PORT:
  1652. return "invalid port selected";
  1653. case CABLE_INF_OP_NOSUP:
  1654. return "operation not supported for this port (the port is of type CX4 or internal)";
  1655. case CABLE_INF_NOT_CONN:
  1656. return "cable is not connected";
  1657. case CABLE_INF_NO_EEPRM:
  1658. return "the connected cable has no EPROM (passive copper cable)";
  1659. case CABLE_INF_PAGE_ERR:
  1660. return "page number is greater than 15";
  1661. case CABLE_INF_INV_ADDR:
  1662. return "invalid device_address or size (that is, size equals 0 or address+size is greater than 256)";
  1663. case CABLE_INF_I2C_ADDR:
  1664. return "invalid I2C slave address";
  1665. case CABLE_INF_QSFP_VIO:
  1666. return "at least one cable violates the QSFP specification and ignores the modsel signal";
  1667. case CABLE_INF_I2C_BUSY:
  1668. return "I2C bus is constantly busy";
  1669. }
  1670. return "Unknown Error";
  1671. }
  1672. /**
  1673. * mlx4_get_module_info - Read cable module eeprom data
  1674. * @dev: mlx4_dev.
  1675. * @port: port number.
  1676. * @offset: byte offset in eeprom to start reading data from.
  1677. * @size: num of bytes to read.
  1678. * @data: output buffer to put the requested data into.
  1679. *
  1680. * Reads cable module eeprom data, puts the outcome data into
  1681. * data pointer paramer.
  1682. * Returns num of read bytes on success or a negative error
  1683. * code.
  1684. */
  1685. int mlx4_get_module_info(struct mlx4_dev *dev, u8 port,
  1686. u16 offset, u16 size, u8 *data)
  1687. {
  1688. struct mlx4_cmd_mailbox *inbox, *outbox;
  1689. struct mlx4_mad_ifc *inmad, *outmad;
  1690. struct mlx4_cable_info *cable_info;
  1691. u16 i2c_addr;
  1692. int ret;
  1693. if (size > MODULE_INFO_MAX_READ)
  1694. size = MODULE_INFO_MAX_READ;
  1695. inbox = mlx4_alloc_cmd_mailbox(dev);
  1696. if (IS_ERR(inbox))
  1697. return PTR_ERR(inbox);
  1698. outbox = mlx4_alloc_cmd_mailbox(dev);
  1699. if (IS_ERR(outbox)) {
  1700. mlx4_free_cmd_mailbox(dev, inbox);
  1701. return PTR_ERR(outbox);
  1702. }
  1703. inmad = (struct mlx4_mad_ifc *)(inbox->buf);
  1704. outmad = (struct mlx4_mad_ifc *)(outbox->buf);
  1705. inmad->method = 0x1; /* Get */
  1706. inmad->class_version = 0x1;
  1707. inmad->mgmt_class = 0x1;
  1708. inmad->base_version = 0x1;
  1709. inmad->attr_id = cpu_to_be16(0xFF60); /* Module Info */
  1710. if (offset < I2C_PAGE_SIZE && offset + size > I2C_PAGE_SIZE)
  1711. /* Cross pages reads are not allowed
  1712. * read until offset 256 in low page
  1713. */
  1714. size -= offset + size - I2C_PAGE_SIZE;
  1715. i2c_addr = I2C_ADDR_LOW;
  1716. if (offset >= I2C_PAGE_SIZE) {
  1717. /* Reset offset to high page */
  1718. i2c_addr = I2C_ADDR_HIGH;
  1719. offset -= I2C_PAGE_SIZE;
  1720. }
  1721. cable_info = (struct mlx4_cable_info *)inmad->data;
  1722. cable_info->dev_mem_address = cpu_to_be16(offset);
  1723. cable_info->page_num = 0;
  1724. cable_info->i2c_addr = i2c_addr;
  1725. cable_info->size = cpu_to_be16(size);
  1726. ret = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  1727. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  1728. MLX4_CMD_NATIVE);
  1729. if (ret)
  1730. goto out;
  1731. if (be16_to_cpu(outmad->status)) {
  1732. /* Mad returned with bad status */
  1733. ret = be16_to_cpu(outmad->status);
  1734. mlx4_warn(dev,
  1735. "MLX4_CMD_MAD_IFC Get Module info attr(%x) port(%d) i2c_addr(%x) offset(%d) size(%d): Response Mad Status(%x) - %s\n",
  1736. 0xFF60, port, i2c_addr, offset, size,
  1737. ret, cable_info_mad_err_str(ret));
  1738. if (i2c_addr == I2C_ADDR_HIGH &&
  1739. MAD_STATUS_2_CABLE_ERR(ret) == CABLE_INF_I2C_ADDR)
  1740. /* Some SFP cables do not support i2c slave
  1741. * address 0x51 (high page), abort silently.
  1742. */
  1743. ret = 0;
  1744. else
  1745. ret = -ret;
  1746. goto out;
  1747. }
  1748. cable_info = (struct mlx4_cable_info *)outmad->data;
  1749. memcpy(data, cable_info->data, size);
  1750. ret = size;
  1751. out:
  1752. mlx4_free_cmd_mailbox(dev, inbox);
  1753. mlx4_free_cmd_mailbox(dev, outbox);
  1754. return ret;
  1755. }
  1756. EXPORT_SYMBOL(mlx4_get_module_info);