port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/export.h>
  35. #include <linux/mlx4/cmd.h>
  36. #include "mlx4.h"
  37. #define MLX4_MAC_VALID (1ull << 63)
  38. #define MLX4_MAC_MASK 0xffffffffffffULL
  39. #define MLX4_VLAN_VALID (1u << 31)
  40. #define MLX4_VLAN_MASK 0xfff
  41. void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
  42. {
  43. int i;
  44. mutex_init(&table->mutex);
  45. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  46. table->entries[i] = 0;
  47. table->refs[i] = 0;
  48. }
  49. table->max = 1 << dev->caps.log_num_macs;
  50. table->total = 0;
  51. }
  52. void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
  53. {
  54. int i;
  55. mutex_init(&table->mutex);
  56. for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
  57. table->entries[i] = 0;
  58. table->refs[i] = 0;
  59. }
  60. table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
  61. table->total = 0;
  62. }
  63. static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
  64. __be64 *entries)
  65. {
  66. struct mlx4_cmd_mailbox *mailbox;
  67. u32 in_mod;
  68. int err;
  69. mailbox = mlx4_alloc_cmd_mailbox(dev);
  70. if (IS_ERR(mailbox))
  71. return PTR_ERR(mailbox);
  72. memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
  73. in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
  74. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  75. MLX4_CMD_TIME_CLASS_B);
  76. mlx4_free_cmd_mailbox(dev, mailbox);
  77. return err;
  78. }
  79. static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port,
  80. u64 mac, int *qpn, u8 reserve)
  81. {
  82. struct mlx4_qp qp;
  83. u8 gid[16] = {0};
  84. int err;
  85. if (reserve) {
  86. err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
  87. if (err) {
  88. mlx4_err(dev, "Failed to reserve qp for mac registration\n");
  89. return err;
  90. }
  91. }
  92. qp.qpn = *qpn;
  93. mac &= 0xffffffffffffULL;
  94. mac = cpu_to_be64(mac << 16);
  95. memcpy(&gid[10], &mac, ETH_ALEN);
  96. gid[5] = port;
  97. gid[7] = MLX4_UC_STEER << 1;
  98. err = mlx4_qp_attach_common(dev, &qp, gid, 0,
  99. MLX4_PROT_ETH, MLX4_UC_STEER);
  100. if (err && reserve)
  101. mlx4_qp_release_range(dev, *qpn, 1);
  102. return err;
  103. }
  104. static void mlx4_uc_steer_release(struct mlx4_dev *dev, u8 port,
  105. u64 mac, int qpn, u8 free)
  106. {
  107. struct mlx4_qp qp;
  108. u8 gid[16] = {0};
  109. qp.qpn = qpn;
  110. mac &= 0xffffffffffffULL;
  111. mac = cpu_to_be64(mac << 16);
  112. memcpy(&gid[10], &mac, ETH_ALEN);
  113. gid[5] = port;
  114. gid[7] = MLX4_UC_STEER << 1;
  115. mlx4_qp_detach_common(dev, &qp, gid, MLX4_PROT_ETH, MLX4_UC_STEER);
  116. if (free)
  117. mlx4_qp_release_range(dev, qpn, 1);
  118. }
  119. int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *qpn, u8 wrap)
  120. {
  121. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  122. struct mlx4_mac_table *table = &info->mac_table;
  123. struct mlx4_mac_entry *entry;
  124. int i, err = 0;
  125. int free = -1;
  126. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
  127. err = mlx4_uc_steer_add(dev, port, mac, qpn, 1);
  128. if (!err) {
  129. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  130. if (!entry) {
  131. mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
  132. return -ENOMEM;
  133. }
  134. entry->mac = mac;
  135. err = radix_tree_insert(&info->mac_tree, *qpn, entry);
  136. if (err) {
  137. mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
  138. return err;
  139. }
  140. } else
  141. return err;
  142. }
  143. mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
  144. mutex_lock(&table->mutex);
  145. for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
  146. if (free < 0 && !table->refs[i]) {
  147. free = i;
  148. continue;
  149. }
  150. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
  151. /* MAC already registered, increase references count */
  152. ++table->refs[i];
  153. goto out;
  154. }
  155. }
  156. if (free < 0) {
  157. err = -ENOMEM;
  158. goto out;
  159. }
  160. mlx4_dbg(dev, "Free MAC index is %d\n", free);
  161. if (table->total == table->max) {
  162. /* No free mac entries */
  163. err = -ENOSPC;
  164. goto out;
  165. }
  166. /* Register new MAC */
  167. table->refs[free] = 1;
  168. table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
  169. err = mlx4_set_port_mac_table(dev, port, table->entries);
  170. if (unlikely(err)) {
  171. mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
  172. table->refs[free] = 0;
  173. table->entries[free] = 0;
  174. goto out;
  175. }
  176. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
  177. *qpn = info->base_qpn + free;
  178. ++table->total;
  179. out:
  180. mutex_unlock(&table->mutex);
  181. return err;
  182. }
  183. EXPORT_SYMBOL_GPL(mlx4_register_mac);
  184. static int validate_index(struct mlx4_dev *dev,
  185. struct mlx4_mac_table *table, int index)
  186. {
  187. int err = 0;
  188. if (index < 0 || index >= table->max || !table->entries[index]) {
  189. mlx4_warn(dev, "No valid Mac entry for the given index\n");
  190. err = -EINVAL;
  191. }
  192. return err;
  193. }
  194. static int find_index(struct mlx4_dev *dev,
  195. struct mlx4_mac_table *table, u64 mac)
  196. {
  197. int i;
  198. for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
  199. if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
  200. return i;
  201. }
  202. /* Mac not found */
  203. return -EINVAL;
  204. }
  205. void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
  206. {
  207. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  208. struct mlx4_mac_table *table = &info->mac_table;
  209. int index = qpn - info->base_qpn;
  210. struct mlx4_mac_entry *entry;
  211. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
  212. entry = radix_tree_lookup(&info->mac_tree, qpn);
  213. if (entry) {
  214. mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
  215. radix_tree_delete(&info->mac_tree, qpn);
  216. index = find_index(dev, table, entry->mac);
  217. kfree(entry);
  218. }
  219. }
  220. mutex_lock(&table->mutex);
  221. if (validate_index(dev, table, index))
  222. goto out;
  223. /* Check whether this address has reference count */
  224. if (!(--table->refs[index])) {
  225. table->entries[index] = 0;
  226. mlx4_set_port_mac_table(dev, port, table->entries);
  227. --table->total;
  228. }
  229. out:
  230. mutex_unlock(&table->mutex);
  231. }
  232. EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
  233. int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
  234. {
  235. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  236. struct mlx4_mac_table *table = &info->mac_table;
  237. int index = qpn - info->base_qpn;
  238. struct mlx4_mac_entry *entry;
  239. int err;
  240. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
  241. entry = radix_tree_lookup(&info->mac_tree, qpn);
  242. if (!entry)
  243. return -EINVAL;
  244. index = find_index(dev, table, entry->mac);
  245. mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
  246. entry->mac = new_mac;
  247. err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
  248. if (err || index < 0)
  249. return err;
  250. }
  251. mutex_lock(&table->mutex);
  252. err = validate_index(dev, table, index);
  253. if (err)
  254. goto out;
  255. table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
  256. err = mlx4_set_port_mac_table(dev, port, table->entries);
  257. if (unlikely(err)) {
  258. mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
  259. table->entries[index] = 0;
  260. }
  261. out:
  262. mutex_unlock(&table->mutex);
  263. return err;
  264. }
  265. EXPORT_SYMBOL_GPL(mlx4_replace_mac);
  266. static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
  267. __be32 *entries)
  268. {
  269. struct mlx4_cmd_mailbox *mailbox;
  270. u32 in_mod;
  271. int err;
  272. mailbox = mlx4_alloc_cmd_mailbox(dev);
  273. if (IS_ERR(mailbox))
  274. return PTR_ERR(mailbox);
  275. memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
  276. in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
  277. err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
  278. MLX4_CMD_TIME_CLASS_B);
  279. mlx4_free_cmd_mailbox(dev, mailbox);
  280. return err;
  281. }
  282. int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
  283. {
  284. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  285. int i;
  286. for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
  287. if (table->refs[i] &&
  288. (vid == (MLX4_VLAN_MASK &
  289. be32_to_cpu(table->entries[i])))) {
  290. /* VLAN already registered, increase reference count */
  291. *idx = i;
  292. return 0;
  293. }
  294. }
  295. return -ENOENT;
  296. }
  297. EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
  298. int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
  299. {
  300. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  301. int i, err = 0;
  302. int free = -1;
  303. mutex_lock(&table->mutex);
  304. if (table->total == table->max) {
  305. /* No free vlan entries */
  306. err = -ENOSPC;
  307. goto out;
  308. }
  309. for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
  310. if (free < 0 && (table->refs[i] == 0)) {
  311. free = i;
  312. continue;
  313. }
  314. if (table->refs[i] &&
  315. (vlan == (MLX4_VLAN_MASK &
  316. be32_to_cpu(table->entries[i])))) {
  317. /* Vlan already registered, increase references count */
  318. *index = i;
  319. ++table->refs[i];
  320. goto out;
  321. }
  322. }
  323. if (free < 0) {
  324. err = -ENOMEM;
  325. goto out;
  326. }
  327. /* Register new MAC */
  328. table->refs[free] = 1;
  329. table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
  330. err = mlx4_set_port_vlan_table(dev, port, table->entries);
  331. if (unlikely(err)) {
  332. mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
  333. table->refs[free] = 0;
  334. table->entries[free] = 0;
  335. goto out;
  336. }
  337. *index = free;
  338. ++table->total;
  339. out:
  340. mutex_unlock(&table->mutex);
  341. return err;
  342. }
  343. EXPORT_SYMBOL_GPL(mlx4_register_vlan);
  344. void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
  345. {
  346. struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
  347. if (index < MLX4_VLAN_REGULAR) {
  348. mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
  349. return;
  350. }
  351. mutex_lock(&table->mutex);
  352. if (!table->refs[index]) {
  353. mlx4_warn(dev, "No vlan entry for index %d\n", index);
  354. goto out;
  355. }
  356. if (--table->refs[index]) {
  357. mlx4_dbg(dev, "Have more references for index %d,"
  358. "no need to modify vlan table\n", index);
  359. goto out;
  360. }
  361. table->entries[index] = 0;
  362. mlx4_set_port_vlan_table(dev, port, table->entries);
  363. --table->total;
  364. out:
  365. mutex_unlock(&table->mutex);
  366. }
  367. EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
  368. int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
  369. {
  370. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  371. u8 *inbuf, *outbuf;
  372. int err;
  373. inmailbox = mlx4_alloc_cmd_mailbox(dev);
  374. if (IS_ERR(inmailbox))
  375. return PTR_ERR(inmailbox);
  376. outmailbox = mlx4_alloc_cmd_mailbox(dev);
  377. if (IS_ERR(outmailbox)) {
  378. mlx4_free_cmd_mailbox(dev, inmailbox);
  379. return PTR_ERR(outmailbox);
  380. }
  381. inbuf = inmailbox->buf;
  382. outbuf = outmailbox->buf;
  383. memset(inbuf, 0, 256);
  384. memset(outbuf, 0, 256);
  385. inbuf[0] = 1;
  386. inbuf[1] = 1;
  387. inbuf[2] = 1;
  388. inbuf[3] = 1;
  389. *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
  390. *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
  391. err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
  392. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
  393. if (!err)
  394. *caps = *(__be32 *) (outbuf + 84);
  395. mlx4_free_cmd_mailbox(dev, inmailbox);
  396. mlx4_free_cmd_mailbox(dev, outmailbox);
  397. return err;
  398. }
  399. int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
  400. {
  401. struct mlx4_cmd_mailbox *mailbox;
  402. int err;
  403. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  404. return 0;
  405. mailbox = mlx4_alloc_cmd_mailbox(dev);
  406. if (IS_ERR(mailbox))
  407. return PTR_ERR(mailbox);
  408. memset(mailbox->buf, 0, 256);
  409. ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
  410. err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  411. MLX4_CMD_TIME_CLASS_B);
  412. mlx4_free_cmd_mailbox(dev, mailbox);
  413. return err;
  414. }