port.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  157. {
  158. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  159. int i;
  160. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  161. if (table->refs[i] &&
  162. (vid == (MLX4_VLAN_MASK &
  163. be32_to_cpu(table->entries[i])))) {
  164. /* VLAN already registered, increase reference count */
  165. *idx = i;
  166. return 0;
  167. }
  168. }
  169. return -ENOENT;
  170. }
  171. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  172. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  173. {
  174. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  175. int i, err = 0;
  176. int free = -1;
  177. mutex_lock(&table->mutex);
  178. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  179. if (free < 0 && (table->refs[i] == 0)) {
  180. free = i;
  181. continue;
  182. }
  183. if (table->refs[i] &&
  184. (vlan == (MLX4_VLAN_MASK &
  185. be32_to_cpu(table->entries[i])))) {
  186. /* Vlan already registered, increase refernce count */
  187. *index = i;
  188. ++table->refs[i];
  189. goto out;
  190. }
  191. }
  192. if (table->total == table->max) {
  193. /* No free vlan entries */
  194. err = -ENOSPC;
  195. goto out;
  196. }
  197. /* Register new MAC */
  198. table->refs[free] = 1;
  199. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  200. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  201. if (unlikely(err)) {
  202. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  203. table->refs[free] = 0;
  204. table->entries[free] = 0;
  205. goto out;
  206. }
  207. *index = free;
  208. ++table->total;
  209. out:
  210. mutex_unlock(&table->mutex);
  211. return err;
  212. }
  213. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  214. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
  215. {
  216. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  217. if (index < MLX4_VLAN_REGULAR) {
  218. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  219. return;
  220. }
  221. mutex_lock(&table->mutex);
  222. if (!table->refs[index]) {
  223. mlx4_warn(dev, "No vlan entry for index %d\n", index);
  224. goto out;
  225. }
  226. if (--table->refs[index]) {
  227. mlx4_dbg(dev, "Have more references for index %d,"
  228. "no need to modify vlan table\n", index);
  229. goto out;
  230. }
  231. table->entries[index] = 0;
  232. mlx4_set_port_vlan_table(dev, port, table->entries);
  233. --table->total;
  234. out:
  235. mutex_unlock(&table->mutex);
  236. }
  237. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  238. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  239. {
  240. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  241. u8 *inbuf, *outbuf;
  242. int err;
  243. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  244. if (IS_ERR(inmailbox))
  245. return PTR_ERR(inmailbox);
  246. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  247. if (IS_ERR(outmailbox)) {
  248. mlx4_free_cmd_mailbox(dev, inmailbox);
  249. return PTR_ERR(outmailbox);
  250. }
  251. inbuf = inmailbox->buf;
  252. outbuf = outmailbox->buf;
  253. memset(inbuf, 0, 256);
  254. memset(outbuf, 0, 256);
  255. inbuf[0] = 1;
  256. inbuf[1] = 1;
  257. inbuf[2] = 1;
  258. inbuf[3] = 1;
  259. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  260. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  261. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  262. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
  263. if (!err)
  264. *caps = *(__be32 *) (outbuf + 84);
  265. mlx4_free_cmd_mailbox(dev, inmailbox);
  266. mlx4_free_cmd_mailbox(dev, outmailbox);
  267. return err;
  268. }
  269. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
  270. {
  271. struct mlx4_cmd_mailbox *mailbox;
  272. int err;
  273. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  274. return 0;
  275. mailbox = mlx4_alloc_cmd_mailbox(dev);
  276. if (IS_ERR(mailbox))
  277. return PTR_ERR(mailbox);
  278. memset(mailbox->buf, 0, 256);
  279. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  280. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  281. MLX4_CMD_TIME_CLASS_B);
  282. mlx4_free_cmd_mailbox(dev, mailbox);
  283. return err;
  284. }