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 33#include <linux/errno.h> 34#include <linux/if_ether.h> 35 36#include <linux/mlx4/cmd.h> 37 38#include "mlx4.h" 39 40int mlx4_ib_set_4k_mtu = 0; 41module_param_named(set_4k_mtu, mlx4_ib_set_4k_mtu, int, 0444); 42MODULE_PARM_DESC(set_4k_mtu, "attempt to set 4K MTU to all ConnectX ports"); 43 44#define MLX4_MAC_VALID (1ull << 63) 45#define MLX4_MAC_MASK 0xffffffffffffULL 46 47#define MLX4_VLAN_VALID (1u << 31) 48#define MLX4_VLAN_MASK 0xfff 49 50void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table) 51{ 52 int i; 53 54 mutex_init(&table->mutex); 55 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) { 56 table->entries[i] = 0; 57 table->refs[i] = 0; 58 } 59 table->max = 1 << dev->caps.log_num_macs; 60 table->total = 0; 61} 62 63void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table) 64{ 65 int i; 66 67 mutex_init(&table->mutex); 68 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) { 69 table->entries[i] = 0; 70 table->refs[i] = 0; 71 } 72 table->max = 1 << dev->caps.log_num_vlans; 73 table->total = 0; 74} 75 76static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port, 77 __be64 *entries) 78{ 79 struct mlx4_cmd_mailbox *mailbox; 80 u32 in_mod; 81 int err; 82 83 mailbox = mlx4_alloc_cmd_mailbox(dev); 84 if (IS_ERR(mailbox)) 85 return PTR_ERR(mailbox); 86 87 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE); 88 89 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port; 90 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT, 91 MLX4_CMD_TIME_CLASS_B); 92 93 mlx4_free_cmd_mailbox(dev, mailbox); 94 return err; 95} 96 97int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *index) 98{ 99 struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table; 100 int i, err = 0; 101 int free = -1; 102 103 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac); 104 mutex_lock(&table->mutex); 105 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) { 106 if (free < 0 && !table->refs[i]) { 107 free = i; 108 continue; 109 } 110 111 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) { 112 /* MAC already registered, increase refernce count */ 113 *index = i; 114 ++table->refs[i]; 115 goto out; 116 } 117 } 118 119 if (free < 0) { 120 err = -ENOMEM; 121 goto out; 122 } 123 124 mlx4_dbg(dev, "Free MAC index is %d\n", free); 125 126 if (table->total == table->max) { 127 /* No free mac entries */ 128 err = -ENOSPC; 129 goto out; 130 } 131 132 /* Register new MAC */ 133 table->refs[free] = 1; 134 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID); 135 136 err = mlx4_set_port_mac_table(dev, port, table->entries); 137 if (unlikely(err)) { 138 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac); 139 table->refs[free] = 0; 140 table->entries[free] = 0; 141 goto out; 142 } 143 144 *index = free; 145 ++table->total; 146out: 147 mutex_unlock(&table->mutex); 148 return err; 149} 150EXPORT_SYMBOL_GPL(mlx4_register_mac); 151 152void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index) 153{ 154 struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table; 155 156 mutex_lock(&table->mutex); 157 if (!table->refs[index]) { 158 mlx4_warn(dev, "No MAC entry for index %d\n", index); 159 goto out; 160 } 161 if (--table->refs[index]) { 162 mlx4_warn(dev, "Have more references for index %d," 163 "no need to modify MAC table\n", index); 164 goto out; 165 } 166 table->entries[index] = 0; 167 mlx4_set_port_mac_table(dev, port, table->entries); 168 --table->total; 169out: 170 mutex_unlock(&table->mutex); 171} 172EXPORT_SYMBOL_GPL(mlx4_unregister_mac); 173 174static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port, 175 __be32 *entries) 176{ 177 struct mlx4_cmd_mailbox *mailbox; 178 u32 in_mod; 179 int err; 180 181 mailbox = mlx4_alloc_cmd_mailbox(dev); 182 if (IS_ERR(mailbox)) 183 return PTR_ERR(mailbox); 184 185 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE); 186 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port; 187 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT, 188 MLX4_CMD_TIME_CLASS_B); 189 190 mlx4_free_cmd_mailbox(dev, mailbox); 191 192 return err; 193} 194 195int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx) 196{ 197 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table; 198 int i; 199 200 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) { 201 if (table->refs[i] && 202 (vid == (MLX4_VLAN_MASK & 203 be32_to_cpu(table->entries[i])))) { 204 /* Vlan already registered, increase refernce count */ 205 *idx = i; 206 return 0; 207 } 208 } 209 210 return -ENOENT; 211} 212EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan); 213 214int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index) 215{ 216 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table; 217 int i, err = 0; 218 int free = -1; 219 220 mutex_lock(&table->mutex); 221 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) { 222 if (free < 0 && (table->refs[i] == 0)) { 223 free = i; 224 continue; 225 } 226 227 if (table->refs[i] && 228 (vlan == (MLX4_VLAN_MASK & 229 be32_to_cpu(table->entries[i])))) { 230 /* Vlan already registered, increase refernce count */ 231 *index = i; 232 ++table->refs[i]; 233 goto out; 234 } 235 } 236 237 if (free < 0) { 238 err = -ENOMEM; 239 goto out; 240 } 241 242 if (table->total == table->max) { 243 /* No free vlan entries */ 244 err = -ENOSPC; 245 goto out; 246 } 247 248 /* Register new MAC */ 249 table->refs[free] = 1; 250 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID); 251 252 err = mlx4_set_port_vlan_table(dev, port, table->entries); 253 if (unlikely(err)) { 254 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan); 255 table->refs[free] = 0; 256 table->entries[free] = 0; 257 goto out; 258 } 259 260 *index = free; 261 ++table->total; 262out: 263 mutex_unlock(&table->mutex); 264 return err; 265} 266EXPORT_SYMBOL_GPL(mlx4_register_vlan); 267 268void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index) 269{ 270 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table; 271 272 if (index < MLX4_VLAN_REGULAR) { 273 mlx4_warn(dev, "Trying to free special vlan index %d\n", index); 274 return; 275 } 276 277 mutex_lock(&table->mutex); 278 if (!table->refs[index]) { 279 mlx4_warn(dev, "No vlan entry for index %d\n", index); 280 goto out; 281 } 282 if (--table->refs[index]) { 283 mlx4_dbg(dev, "Have more references for index %d," 284 "no need to modify vlan table\n", index); 285 goto out; 286 } 287 table->entries[index] = 0; 288 mlx4_set_port_vlan_table(dev, port, table->entries); 289 --table->total; 290out: 291 mutex_unlock(&table->mutex); 292} 293EXPORT_SYMBOL_GPL(mlx4_unregister_vlan); 294 295int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps) 296{ 297 struct mlx4_cmd_mailbox *inmailbox, *outmailbox; 298 u8 *inbuf, *outbuf; 299 int err; 300 301 inmailbox = mlx4_alloc_cmd_mailbox(dev); 302 if (IS_ERR(inmailbox)) 303 return PTR_ERR(inmailbox); 304 305 outmailbox = mlx4_alloc_cmd_mailbox(dev); 306 if (IS_ERR(outmailbox)) { 307 mlx4_free_cmd_mailbox(dev, inmailbox); 308 return PTR_ERR(outmailbox); 309 } 310 311 inbuf = inmailbox->buf; 312 outbuf = outmailbox->buf; 313 memset(inbuf, 0, 256); 314 memset(outbuf, 0, 256); 315 inbuf[0] = 1; 316 inbuf[1] = 1; 317 inbuf[2] = 1; 318 inbuf[3] = 1; 319 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015); 320 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port); 321 322 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3, 323 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C); 324 if (!err) 325 *caps = *(__be32 *) (outbuf + 84); 326 mlx4_free_cmd_mailbox(dev, inmailbox); 327 mlx4_free_cmd_mailbox(dev, outmailbox); 328 return err; 329} 330 331int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port) 332{ 333 struct mlx4_cmd_mailbox *mailbox; 334 int err; 335 336 if (dev->caps.port_type[port] != MLX4_PORT_TYPE_IB) 337 return 0; 338 339 mailbox = mlx4_alloc_cmd_mailbox(dev); 340 if (IS_ERR(mailbox)) 341 return PTR_ERR(mailbox); 342 343 memset(mailbox->buf, 0, 256); 344 345 if (mlx4_ib_set_4k_mtu) 346 ((__be32 *) mailbox->buf)[0] |= cpu_to_be32((1 << 22) | (1 << 21) | (5 << 12) | (2 << 4)); 347 348 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port]; 349 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT, 350 MLX4_CMD_TIME_CLASS_B); 351 352 mlx4_free_cmd_mailbox(dev, mailbox); 353 return err; 354} 355