port.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. if (free < 0) {
  98. err = -ENOMEM;
  99. goto out;
  100. }
  101. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  102. if (table->total == table->max) {
  103. /* No free mac entries */
  104. err = -ENOSPC;
  105. goto out;
  106. }
  107. /* Register new MAC */
  108. table->refs[free] = 1;
  109. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  110. err = mlx4_set_port_mac_table(dev, port, table->entries);
  111. if (unlikely(err)) {
  112. mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
  113. table->refs[free] = 0;
  114. table->entries[free] = 0;
  115. goto out;
  116. }
  117. *index = free;
  118. ++table->total;
  119. out:
  120. mutex_unlock(&table->mutex);
  121. return err;
  122. }
  123. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  124. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index)
  125. {
  126. struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
  127. mutex_lock(&table->mutex);
  128. if (!table->refs[index]) {
  129. mlx4_warn(dev, "No MAC entry for index %d\n", index);
  130. goto out;
  131. }
  132. if (--table->refs[index]) {
  133. mlx4_warn(dev, "Have more references for index %d,"
  134. "no need to modify MAC table\n", index);
  135. goto out;
  136. }
  137. table->entries[index] = 0;
  138. mlx4_set_port_mac_table(dev, port, table->entries);
  139. --table->total;
  140. out:
  141. mutex_unlock(&table->mutex);
  142. }
  143. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  144. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  145. __be32 *entries)
  146. {
  147. struct mlx4_cmd_mailbox *mailbox;
  148. u32 in_mod;
  149. int err;
  150. mailbox = mlx4_alloc_cmd_mailbox(dev);
  151. if (IS_ERR(mailbox))
  152. return PTR_ERR(mailbox);
  153. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  154. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  155. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  156. MLX4_CMD_TIME_CLASS_B);
  157. mlx4_free_cmd_mailbox(dev, mailbox);
  158. return err;
  159. }
  160. int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  161. {
  162. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  163. int i;
  164. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  165. if (table->refs[i] &&
  166. (vid == (MLX4_VLAN_MASK &
  167. be32_to_cpu(table->entries[i])))) {
  168. /* VLAN already registered, increase reference count */
  169. *idx = i;
  170. return 0;
  171. }
  172. }
  173. return -ENOENT;
  174. }
  175. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  176. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  177. {
  178. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  179. int i, err = 0;
  180. int free = -1;
  181. mutex_lock(&table->mutex);
  182. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  183. if (free < 0 && (table->refs[i] == 0)) {
  184. free = i;
  185. continue;
  186. }
  187. if (table->refs[i] &&
  188. (vlan == (MLX4_VLAN_MASK &
  189. be32_to_cpu(table->entries[i])))) {
  190. /* Vlan already registered, increase refernce count */
  191. *index = i;
  192. ++table->refs[i];
  193. goto out;
  194. }
  195. }
  196. if (free < 0) {
  197. err = -ENOMEM;
  198. goto out;
  199. }
  200. if (table->total == table->max) {
  201. /* No free vlan entries */
  202. err = -ENOSPC;
  203. goto out;
  204. }
  205. /* Register new MAC */
  206. table->refs[free] = 1;
  207. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  208. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  209. if (unlikely(err)) {
  210. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  211. table->refs[free] = 0;
  212. table->entries[free] = 0;
  213. goto out;
  214. }
  215. *index = free;
  216. ++table->total;
  217. out:
  218. mutex_unlock(&table->mutex);
  219. return err;
  220. }
  221. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  222. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
  223. {
  224. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  225. if (index < MLX4_VLAN_REGULAR) {
  226. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  227. return;
  228. }
  229. mutex_lock(&table->mutex);
  230. if (!table->refs[index]) {
  231. mlx4_warn(dev, "No vlan entry for index %d\n", index);
  232. goto out;
  233. }
  234. if (--table->refs[index]) {
  235. mlx4_dbg(dev, "Have more references for index %d,"
  236. "no need to modify vlan table\n", index);
  237. goto out;
  238. }
  239. table->entries[index] = 0;
  240. mlx4_set_port_vlan_table(dev, port, table->entries);
  241. --table->total;
  242. out:
  243. mutex_unlock(&table->mutex);
  244. }
  245. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  246. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  247. {
  248. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  249. u8 *inbuf, *outbuf;
  250. int err;
  251. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  252. if (IS_ERR(inmailbox))
  253. return PTR_ERR(inmailbox);
  254. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  255. if (IS_ERR(outmailbox)) {
  256. mlx4_free_cmd_mailbox(dev, inmailbox);
  257. return PTR_ERR(outmailbox);
  258. }
  259. inbuf = inmailbox->buf;
  260. outbuf = outmailbox->buf;
  261. memset(inbuf, 0, 256);
  262. memset(outbuf, 0, 256);
  263. inbuf[0] = 1;
  264. inbuf[1] = 1;
  265. inbuf[2] = 1;
  266. inbuf[3] = 1;
  267. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  268. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  269. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  270. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
  271. if (!err)
  272. *caps = *(__be32 *) (outbuf + 84);
  273. mlx4_free_cmd_mailbox(dev, inmailbox);
  274. mlx4_free_cmd_mailbox(dev, outmailbox);
  275. return err;
  276. }
  277. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
  278. {
  279. struct mlx4_cmd_mailbox *mailbox;
  280. int err;
  281. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  282. return 0;
  283. mailbox = mlx4_alloc_cmd_mailbox(dev);
  284. if (IS_ERR(mailbox))
  285. return PTR_ERR(mailbox);
  286. memset(mailbox->buf, 0, 256);
  287. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  288. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  289. MLX4_CMD_TIME_CLASS_B);
  290. mlx4_free_cmd_mailbox(dev, mailbox);
  291. return err;
  292. }