port.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/mlx4/cmd.h>
  35. #include "mlx4.h"
  36. #define MLX4_MAC_VALID (1ull << 63)
  37. #define MLX4_MAC_MASK 0xffffffffffffULL
  38. #define MLX4_VLAN_VALID (1u << 31)
  39. #define MLX4_VLAN_MASK 0xfff
  40. void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
  41. {
  42. int i;
  43. mutex_init(&table->mutex);
  44. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  45. table->entries[i] = 0;
  46. table->refs[i] = 0;
  47. }
  48. table->max = 1 << dev->caps.log_num_macs;
  49. table->total = 0;
  50. }
  51. void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
  52. {
  53. int i;
  54. mutex_init(&table->mutex);
  55. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  56. table->entries[i] = 0;
  57. table->refs[i] = 0;
  58. }
  59. table->max = 1 << dev->caps.log_num_vlans;
  60. table->total = 0;
  61. }
  62. static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
  63. __be64 *entries)
  64. {
  65. struct mlx4_cmd_mailbox *mailbox;
  66. u32 in_mod;
  67. int err;
  68. mailbox = mlx4_alloc_cmd_mailbox(dev);
  69. if (IS_ERR(mailbox))
  70. return PTR_ERR(mailbox);
  71. memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
  72. in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
  73. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  74. MLX4_CMD_TIME_CLASS_B);
  75. mlx4_free_cmd_mailbox(dev, mailbox);
  76. return err;
  77. }
  78. int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *index)
  79. {
  80. struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
  81. int i, err = 0;
  82. int free = -1;
  83. mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
  84. mutex_lock(&table->mutex);
  85. for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
  86. if (free < 0 && !table->refs[i]) {
  87. free = i;
  88. continue;
  89. }
  90. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  91. /* MAC already registered, increase refernce count */
  92. *index = i;
  93. ++table->refs[i];
  94. goto out;
  95. }
  96. }
  97. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  98. if (table->total == table->max) {
  99. /* No free mac entries */
  100. err = -ENOSPC;
  101. goto out;
  102. }
  103. /* Register new MAC */
  104. table->refs[free] = 1;
  105. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  106. err = mlx4_set_port_mac_table(dev, port, table->entries);
  107. if (unlikely(err)) {
  108. mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
  109. table->refs[free] = 0;
  110. table->entries[free] = 0;
  111. goto out;
  112. }
  113. *index = free;
  114. ++table->total;
  115. out:
  116. mutex_unlock(&table->mutex);
  117. return err;
  118. }
  119. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  120. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index)
  121. {
  122. struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
  123. mutex_lock(&table->mutex);
  124. if (!table->refs[index]) {
  125. mlx4_warn(dev, "No MAC entry for index %d\n", index);
  126. goto out;
  127. }
  128. if (--table->refs[index]) {
  129. mlx4_warn(dev, "Have more references for index %d,"
  130. "no need to modify MAC table\n", index);
  131. goto out;
  132. }
  133. table->entries[index] = 0;
  134. mlx4_set_port_mac_table(dev, port, table->entries);
  135. --table->total;
  136. out:
  137. mutex_unlock(&table->mutex);
  138. }
  139. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  140. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  141. __be32 *entries)
  142. {
  143. struct mlx4_cmd_mailbox *mailbox;
  144. u32 in_mod;
  145. int err;
  146. mailbox = mlx4_alloc_cmd_mailbox(dev);
  147. if (IS_ERR(mailbox))
  148. return PTR_ERR(mailbox);
  149. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  150. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  151. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  152. MLX4_CMD_TIME_CLASS_B);
  153. mlx4_free_cmd_mailbox(dev, mailbox);
  154. return err;
  155. }
  156. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  157. {
  158. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  159. int i, err = 0;
  160. int free = -1;
  161. mutex_lock(&table->mutex);
  162. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  163. if (free < 0 && (table->refs[i] == 0)) {
  164. free = i;
  165. continue;
  166. }
  167. if (table->refs[i] &&
  168. (vlan == (MLX4_VLAN_MASK &
  169. be32_to_cpu(table->entries[i])))) {
  170. /* Vlan already registered, increase refernce count */
  171. *index = i;
  172. ++table->refs[i];
  173. goto out;
  174. }
  175. }
  176. if (table->total == table->max) {
  177. /* No free vlan entries */
  178. err = -ENOSPC;
  179. goto out;
  180. }
  181. /* Register new MAC */
  182. table->refs[free] = 1;
  183. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  184. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  185. if (unlikely(err)) {
  186. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  187. table->refs[free] = 0;
  188. table->entries[free] = 0;
  189. goto out;
  190. }
  191. *index = free;
  192. ++table->total;
  193. out:
  194. mutex_unlock(&table->mutex);
  195. return err;
  196. }
  197. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  198. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
  199. {
  200. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  201. if (index < MLX4_VLAN_REGULAR) {
  202. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  203. return;
  204. }
  205. mutex_lock(&table->mutex);
  206. if (!table->refs[index]) {
  207. mlx4_warn(dev, "No vlan entry for index %d\n", index);
  208. goto out;
  209. }
  210. if (--table->refs[index]) {
  211. mlx4_dbg(dev, "Have more references for index %d,"
  212. "no need to modify vlan table\n", index);
  213. goto out;
  214. }
  215. table->entries[index] = 0;
  216. mlx4_set_port_vlan_table(dev, port, table->entries);
  217. --table->total;
  218. out:
  219. mutex_unlock(&table->mutex);
  220. }
  221. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  222. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  223. {
  224. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  225. u8 *inbuf, *outbuf;
  226. int err;
  227. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  228. if (IS_ERR(inmailbox))
  229. return PTR_ERR(inmailbox);
  230. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  231. if (IS_ERR(outmailbox)) {
  232. mlx4_free_cmd_mailbox(dev, inmailbox);
  233. return PTR_ERR(outmailbox);
  234. }
  235. inbuf = inmailbox->buf;
  236. outbuf = outmailbox->buf;
  237. memset(inbuf, 0, 256);
  238. memset(outbuf, 0, 256);
  239. inbuf[0] = 1;
  240. inbuf[1] = 1;
  241. inbuf[2] = 1;
  242. inbuf[3] = 1;
  243. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  244. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  245. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  246. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
  247. if (!err)
  248. *caps = *(__be32 *) (outbuf + 84);
  249. mlx4_free_cmd_mailbox(dev, inmailbox);
  250. mlx4_free_cmd_mailbox(dev, outmailbox);
  251. return err;
  252. }
  253. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
  254. {
  255. struct mlx4_cmd_mailbox *mailbox;
  256. int err;
  257. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  258. return 0;
  259. mailbox = mlx4_alloc_cmd_mailbox(dev);
  260. if (IS_ERR(mailbox))
  261. return PTR_ERR(mailbox);
  262. memset(mailbox->buf, 0, 256);
  263. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  264. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  265. MLX4_CMD_TIME_CLASS_B);
  266. mlx4_free_cmd_mailbox(dev, mailbox);
  267. return err;
  268. }