port.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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->num_vfs + 1);
  472. }
  473. vfs = bitmap_weight(slaves_pport.slaves, dev->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->num_vfs + 1);
  504. }
  505. gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
  506. vfs = bitmap_weight(slaves_pport.slaves, dev->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->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, dev->num_vfs + 1) - 1;
  1042. for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
  1043. if (!memcmp(priv->port[port].gid_table.roce_gids[i].raw, gid,
  1044. MLX4_ROCE_GID_ENTRY_SIZE)) {
  1045. found_ix = i;
  1046. break;
  1047. }
  1048. }
  1049. if (found_ix >= 0) {
  1050. /* Calculate a slave_gid which is the slave number in the gid
  1051. * table and not a globally unique slave number.
  1052. */
  1053. if (found_ix < MLX4_ROCE_PF_GIDS)
  1054. slave_gid = 0;
  1055. else if (found_ix < MLX4_ROCE_PF_GIDS + (vf_gids % num_vfs) *
  1056. (vf_gids / num_vfs + 1))
  1057. slave_gid = ((found_ix - MLX4_ROCE_PF_GIDS) /
  1058. (vf_gids / num_vfs + 1)) + 1;
  1059. else
  1060. slave_gid =
  1061. ((found_ix - MLX4_ROCE_PF_GIDS -
  1062. ((vf_gids % num_vfs) * ((vf_gids / num_vfs + 1)))) /
  1063. (vf_gids / num_vfs)) + vf_gids % num_vfs + 1;
  1064. /* Calculate the globally unique slave id */
  1065. if (slave_gid) {
  1066. struct mlx4_active_ports exclusive_ports;
  1067. struct mlx4_active_ports actv_ports;
  1068. struct mlx4_slaves_pport slaves_pport_actv;
  1069. unsigned max_port_p_one;
  1070. int num_vfs_before = 0;
  1071. int candidate_slave_gid;
  1072. /* Calculate how many VFs are on the previous port, if exists */
  1073. for (i = 1; i < port; i++) {
  1074. bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
  1075. set_bit(i - 1, exclusive_ports.ports);
  1076. slaves_pport_actv =
  1077. mlx4_phys_to_slaves_pport_actv(
  1078. dev, &exclusive_ports);
  1079. num_vfs_before += bitmap_weight(
  1080. slaves_pport_actv.slaves,
  1081. dev->num_vfs + 1);
  1082. }
  1083. /* candidate_slave_gid isn't necessarily the correct slave, but
  1084. * it has the same number of ports and is assigned to the same
  1085. * ports as the real slave we're looking for. On dual port VF,
  1086. * slave_gid = [single port VFs on port <port>] +
  1087. * [offset of the current slave from the first dual port VF] +
  1088. * 1 (for the PF).
  1089. */
  1090. candidate_slave_gid = slave_gid + num_vfs_before;
  1091. actv_ports = mlx4_get_active_ports(dev, candidate_slave_gid);
  1092. max_port_p_one = find_first_bit(
  1093. actv_ports.ports, dev->caps.num_ports) +
  1094. bitmap_weight(actv_ports.ports,
  1095. dev->caps.num_ports) + 1;
  1096. /* Calculate the real slave number */
  1097. for (i = 1; i < max_port_p_one; i++) {
  1098. if (i == port)
  1099. continue;
  1100. bitmap_zero(exclusive_ports.ports,
  1101. dev->caps.num_ports);
  1102. set_bit(i - 1, exclusive_ports.ports);
  1103. slaves_pport_actv =
  1104. mlx4_phys_to_slaves_pport_actv(
  1105. dev, &exclusive_ports);
  1106. slave_gid += bitmap_weight(
  1107. slaves_pport_actv.slaves,
  1108. dev->num_vfs + 1);
  1109. }
  1110. }
  1111. *slave_id = slave_gid;
  1112. }
  1113. return (found_ix >= 0) ? 0 : -EINVAL;
  1114. }
  1115. EXPORT_SYMBOL(mlx4_get_slave_from_roce_gid);
  1116. int mlx4_get_roce_gid_from_slave(struct mlx4_dev *dev, int port, int slave_id,
  1117. u8 *gid)
  1118. {
  1119. struct mlx4_priv *priv = mlx4_priv(dev);
  1120. if (!mlx4_is_master(dev))
  1121. return -EINVAL;
  1122. memcpy(gid, priv->port[port].gid_table.roce_gids[slave_id].raw,
  1123. MLX4_ROCE_GID_ENTRY_SIZE);
  1124. return 0;
  1125. }
  1126. EXPORT_SYMBOL(mlx4_get_roce_gid_from_slave);
  1127. /* Cable Module Info */
  1128. #define MODULE_INFO_MAX_READ 48
  1129. #define I2C_ADDR_LOW 0x50
  1130. #define I2C_ADDR_HIGH 0x51
  1131. #define I2C_PAGE_SIZE 256
  1132. /* Module Info Data */
  1133. struct mlx4_cable_info {
  1134. u8 i2c_addr;
  1135. u8 page_num;
  1136. __be16 dev_mem_address;
  1137. __be16 reserved1;
  1138. __be16 size;
  1139. __be32 reserved2[2];
  1140. u8 data[MODULE_INFO_MAX_READ];
  1141. };
  1142. enum cable_info_err {
  1143. CABLE_INF_INV_PORT = 0x1,
  1144. CABLE_INF_OP_NOSUP = 0x2,
  1145. CABLE_INF_NOT_CONN = 0x3,
  1146. CABLE_INF_NO_EEPRM = 0x4,
  1147. CABLE_INF_PAGE_ERR = 0x5,
  1148. CABLE_INF_INV_ADDR = 0x6,
  1149. CABLE_INF_I2C_ADDR = 0x7,
  1150. CABLE_INF_QSFP_VIO = 0x8,
  1151. CABLE_INF_I2C_BUSY = 0x9,
  1152. };
  1153. #define MAD_STATUS_2_CABLE_ERR(mad_status) ((mad_status >> 8) & 0xFF)
  1154. static inline const char *cable_info_mad_err_str(u16 mad_status)
  1155. {
  1156. u8 err = MAD_STATUS_2_CABLE_ERR(mad_status);
  1157. switch (err) {
  1158. case CABLE_INF_INV_PORT:
  1159. return "invalid port selected";
  1160. case CABLE_INF_OP_NOSUP:
  1161. return "operation not supported for this port (the port is of type CX4 or internal)";
  1162. case CABLE_INF_NOT_CONN:
  1163. return "cable is not connected";
  1164. case CABLE_INF_NO_EEPRM:
  1165. return "the connected cable has no EPROM (passive copper cable)";
  1166. case CABLE_INF_PAGE_ERR:
  1167. return "page number is greater than 15";
  1168. case CABLE_INF_INV_ADDR:
  1169. return "invalid device_address or size (that is, size equals 0 or address+size is greater than 256)";
  1170. case CABLE_INF_I2C_ADDR:
  1171. return "invalid I2C slave address";
  1172. case CABLE_INF_QSFP_VIO:
  1173. return "at least one cable violates the QSFP specification and ignores the modsel signal";
  1174. case CABLE_INF_I2C_BUSY:
  1175. return "I2C bus is constantly busy";
  1176. }
  1177. return "Unknown Error";
  1178. }
  1179. /**
  1180. * mlx4_get_module_info - Read cable module eeprom data
  1181. * @dev: mlx4_dev.
  1182. * @port: port number.
  1183. * @offset: byte offset in eeprom to start reading data from.
  1184. * @size: num of bytes to read.
  1185. * @data: output buffer to put the requested data into.
  1186. *
  1187. * Reads cable module eeprom data, puts the outcome data into
  1188. * data pointer paramer.
  1189. * Returns num of read bytes on success or a negative error
  1190. * code.
  1191. */
  1192. int mlx4_get_module_info(struct mlx4_dev *dev, u8 port,
  1193. u16 offset, u16 size, u8 *data)
  1194. {
  1195. struct mlx4_cmd_mailbox *inbox, *outbox;
  1196. struct mlx4_mad_ifc *inmad, *outmad;
  1197. struct mlx4_cable_info *cable_info;
  1198. u16 i2c_addr;
  1199. int ret;
  1200. if (size > MODULE_INFO_MAX_READ)
  1201. size = MODULE_INFO_MAX_READ;
  1202. inbox = mlx4_alloc_cmd_mailbox(dev);
  1203. if (IS_ERR(inbox))
  1204. return PTR_ERR(inbox);
  1205. outbox = mlx4_alloc_cmd_mailbox(dev);
  1206. if (IS_ERR(outbox)) {
  1207. mlx4_free_cmd_mailbox(dev, inbox);
  1208. return PTR_ERR(outbox);
  1209. }
  1210. inmad = (struct mlx4_mad_ifc *)(inbox->buf);
  1211. outmad = (struct mlx4_mad_ifc *)(outbox->buf);
  1212. inmad->method = 0x1; /* Get */
  1213. inmad->class_version = 0x1;
  1214. inmad->mgmt_class = 0x1;
  1215. inmad->base_version = 0x1;
  1216. inmad->attr_id = cpu_to_be16(0xFF60); /* Module Info */
  1217. if (offset < I2C_PAGE_SIZE && offset + size > I2C_PAGE_SIZE)
  1218. /* Cross pages reads are not allowed
  1219. * read until offset 256 in low page
  1220. */
  1221. size -= offset + size - I2C_PAGE_SIZE;
  1222. i2c_addr = I2C_ADDR_LOW;
  1223. if (offset >= I2C_PAGE_SIZE) {
  1224. /* Reset offset to high page */
  1225. i2c_addr = I2C_ADDR_HIGH;
  1226. offset -= I2C_PAGE_SIZE;
  1227. }
  1228. cable_info = (struct mlx4_cable_info *)inmad->data;
  1229. cable_info->dev_mem_address = cpu_to_be16(offset);
  1230. cable_info->page_num = 0;
  1231. cable_info->i2c_addr = i2c_addr;
  1232. cable_info->size = cpu_to_be16(size);
  1233. ret = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  1234. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  1235. MLX4_CMD_NATIVE);
  1236. if (ret)
  1237. goto out;
  1238. if (be16_to_cpu(outmad->status)) {
  1239. /* Mad returned with bad status */
  1240. ret = be16_to_cpu(outmad->status);
  1241. mlx4_warn(dev,
  1242. "MLX4_CMD_MAD_IFC Get Module info attr(%x) port(%d) i2c_addr(%x) offset(%d) size(%d): Response Mad Status(%x) - %s\n",
  1243. 0xFF60, port, i2c_addr, offset, size,
  1244. ret, cable_info_mad_err_str(ret));
  1245. if (i2c_addr == I2C_ADDR_HIGH &&
  1246. MAD_STATUS_2_CABLE_ERR(ret) == CABLE_INF_I2C_ADDR)
  1247. /* Some SFP cables do not support i2c slave
  1248. * address 0x51 (high page), abort silently.
  1249. */
  1250. ret = 0;
  1251. else
  1252. ret = -ret;
  1253. goto out;
  1254. }
  1255. cable_info = (struct mlx4_cable_info *)outmad->data;
  1256. memcpy(data, cable_info->data, size);
  1257. ret = size;
  1258. out:
  1259. mlx4_free_cmd_mailbox(dev, inbox);
  1260. mlx4_free_cmd_mailbox(dev, outbox);
  1261. return ret;
  1262. }
  1263. EXPORT_SYMBOL(mlx4_get_module_info);