blob: cacac4e966eebf0851b1f4a21e881e62608578bc [file] [log] [blame]
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001/*
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
34#include <linux/cpumask.h>
35#include <linux/module.h>
36#include <linux/delay.h>
37#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070039
40#include <linux/mlx4/driver.h>
41#include <linux/mlx4/device.h>
42#include <linux/mlx4/cmd.h>
43
44#include "mlx4_en.h"
45
46MODULE_AUTHOR("Liran Liss, Yevgeny Petrilin");
47MODULE_DESCRIPTION("Mellanox ConnectX HCA Ethernet driver");
48MODULE_LICENSE("Dual BSD/GPL");
49MODULE_VERSION(DRV_VERSION " ("DRV_RELDATE")");
50
51static const char mlx4_en_version[] =
52 DRV_NAME ": Mellanox ConnectX HCA Ethernet driver v"
53 DRV_VERSION " (" DRV_RELDATE ")\n";
54
Yevgeny Petrilina2b28732009-06-01 23:20:18 +000055#define MLX4_EN_PARM_INT(X, def_val, desc) \
56 static unsigned int X = def_val;\
57 module_param(X , uint, 0444); \
58 MODULE_PARM_DESC(X, desc);
59
60
61/*
62 * Device scope module parameters
63 */
64
65
66/* Use a XOR rathern than Toeplitz hash function for RSS */
67MLX4_EN_PARM_INT(rss_xor, 0, "Use XOR hash function for RSS");
68
69/* RSS hash type mask - default to <saddr, daddr, sport, dport> */
70MLX4_EN_PARM_INT(rss_mask, 0xf, "RSS hash type bitmask");
71
Yevgeny Petrilina2b28732009-06-01 23:20:18 +000072/* Priority pausing */
73MLX4_EN_PARM_INT(pfctx, 0, "Priority based Flow Control policy on TX[7:0]."
74 " Per priority bit mask");
75MLX4_EN_PARM_INT(pfcrx, 0, "Priority based Flow Control policy on RX[7:0]."
76 " Per priority bit mask");
77
Joe Perches0a645e82010-07-10 07:22:46 +000078int en_print(const char *level, const struct mlx4_en_priv *priv,
79 const char *format, ...)
80{
81 va_list args;
82 struct va_format vaf;
83 int i;
84
85 va_start(args, format);
86
87 vaf.fmt = format;
88 vaf.va = &args;
89 if (priv->registered)
90 i = printk("%s%s: %s: %pV",
91 level, DRV_NAME, priv->dev->name, &vaf);
92 else
93 i = printk("%s%s: %s: Port %d: %pV",
94 level, DRV_NAME, dev_name(&priv->mdev->pdev->dev),
95 priv->port, &vaf);
96 va_end(args);
97
98 return i;
99}
100
Yevgeny Petrilina2b28732009-06-01 23:20:18 +0000101static int mlx4_en_get_profile(struct mlx4_en_dev *mdev)
102{
103 struct mlx4_en_profile *params = &mdev->profile;
104 int i;
105
106 params->rss_xor = (rss_xor != 0);
107 params->rss_mask = rss_mask & 0x1f;
Yevgeny Petrilina2b28732009-06-01 23:20:18 +0000108 for (i = 1; i <= MLX4_MAX_PORTS; i++) {
109 params->prof[i].rx_pause = 1;
110 params->prof[i].rx_ppp = pfcrx;
111 params->prof[i].tx_pause = 1;
112 params->prof[i].tx_ppp = pfctx;
113 params->prof[i].tx_ring_size = MLX4_EN_DEF_TX_RING_SIZE;
114 params->prof[i].rx_ring_size = MLX4_EN_DEF_RX_RING_SIZE;
Yevgeny Petrilinf813cad2009-06-01 23:24:07 +0000115 params->prof[i].tx_ring_num = MLX4_EN_NUM_TX_RINGS +
116 (!!pfcrx) * MLX4_EN_NUM_PPP_RINGS;
Yevgeny Petrilina2b28732009-06-01 23:20:18 +0000117 }
118
119 return 0;
120}
121
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700122static void mlx4_en_event(struct mlx4_dev *dev, void *endev_ptr,
123 enum mlx4_dev_event event, int port)
124{
125 struct mlx4_en_dev *mdev = (struct mlx4_en_dev *) endev_ptr;
126 struct mlx4_en_priv *priv;
127
128 if (!mdev->pndev[port])
129 return;
130
131 priv = netdev_priv(mdev->pndev[port]);
132 switch (event) {
133 case MLX4_DEV_EVENT_PORT_UP:
134 case MLX4_DEV_EVENT_PORT_DOWN:
135 /* To prevent races, we poll the link state in a separate
136 task rather than changing it here */
137 priv->link_state = event;
138 queue_work(mdev->workqueue, &priv->linkstate_task);
139 break;
140
141 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
142 mlx4_err(mdev, "Internal error detected, restarting device\n");
143 break;
144
145 default:
146 mlx4_warn(mdev, "Unhandled event: %d\n", event);
147 }
148}
149
150static void mlx4_en_remove(struct mlx4_dev *dev, void *endev_ptr)
151{
152 struct mlx4_en_dev *mdev = endev_ptr;
153 int i;
154
155 mutex_lock(&mdev->state_lock);
156 mdev->device_up = false;
157 mutex_unlock(&mdev->state_lock);
158
159 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
160 if (mdev->pndev[i])
161 mlx4_en_destroy_netdev(mdev->pndev[i]);
162
163 flush_workqueue(mdev->workqueue);
164 destroy_workqueue(mdev->workqueue);
165 mlx4_mr_free(dev, &mdev->mr);
166 mlx4_uar_free(dev, &mdev->priv_uar);
167 mlx4_pd_free(dev, mdev->priv_pdn);
168 kfree(mdev);
169}
170
171static void *mlx4_en_add(struct mlx4_dev *dev)
172{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700173 struct mlx4_en_dev *mdev;
174 int i;
175 int err;
176
Joe Perches0a645e82010-07-10 07:22:46 +0000177 printk_once(KERN_INFO "%s", mlx4_en_version);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700178
179 mdev = kzalloc(sizeof *mdev, GFP_KERNEL);
180 if (!mdev) {
181 dev_err(&dev->pdev->dev, "Device struct alloc failed, "
182 "aborting.\n");
183 err = -ENOMEM;
184 goto err_free_res;
185 }
186
187 if (mlx4_pd_alloc(dev, &mdev->priv_pdn))
188 goto err_free_dev;
189
190 if (mlx4_uar_alloc(dev, &mdev->priv_uar))
191 goto err_pd;
192
193 mdev->uar_map = ioremap(mdev->priv_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
194 if (!mdev->uar_map)
195 goto err_uar;
196 spin_lock_init(&mdev->uar_lock);
197
198 mdev->dev = dev;
199 mdev->dma_device = &(dev->pdev->dev);
200 mdev->pdev = dev->pdev;
201 mdev->device_up = false;
202
203 mdev->LSO_support = !!(dev->caps.flags & (1 << 15));
204 if (!mdev->LSO_support)
205 mlx4_warn(mdev, "LSO not supported, please upgrade to later "
206 "FW version to enable LSO\n");
207
208 if (mlx4_mr_alloc(mdev->dev, mdev->priv_pdn, 0, ~0ull,
209 MLX4_PERM_LOCAL_WRITE | MLX4_PERM_LOCAL_READ,
210 0, 0, &mdev->mr)) {
211 mlx4_err(mdev, "Failed allocating memory region\n");
212 goto err_uar;
213 }
214 if (mlx4_mr_enable(mdev->dev, &mdev->mr)) {
215 mlx4_err(mdev, "Failed enabling memory region\n");
216 goto err_mr;
217 }
218
219 /* Build device profile according to supplied module parameters */
220 err = mlx4_en_get_profile(mdev);
221 if (err) {
222 mlx4_err(mdev, "Bad module parameters, aborting.\n");
223 goto err_mr;
224 }
225
226 /* Configure wich ports to start according to module parameters */
227 mdev->port_cnt = 0;
228 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
229 mdev->port_cnt++;
230
231 /* If we did not receive an explicit number of Rx rings, default to
232 * the number of completion vectors populated by the mlx4_core */
233 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
234 mlx4_info(mdev, "Using %d tx rings for port:%d\n",
235 mdev->profile.prof[i].tx_ring_num, i);
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -0700236 mdev->profile.prof[i].rx_ring_num = min_t(int,
237 roundup_pow_of_two(dev->caps.num_comp_vectors),
238 MAX_RX_RINGS);
Yevgeny Petrilin2d6a7b72008-12-29 18:38:54 -0800239 mlx4_info(mdev, "Defaulting to %d rx rings for port:%d\n",
240 mdev->profile.prof[i].rx_ring_num, i);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700241 }
242
243 /* Create our own workqueue for reset/multicast tasks
244 * Note: we cannot use the shared workqueue because of deadlocks caused
245 * by the rtnl lock */
246 mdev->workqueue = create_singlethread_workqueue("mlx4_en");
247 if (!mdev->workqueue) {
248 err = -ENOMEM;
Roland Dreier1a44cc32009-04-20 18:32:08 +0000249 goto err_mr;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700250 }
251
252 /* At this stage all non-port specific tasks are complete:
253 * mark the card state as up */
254 mutex_init(&mdev->state_lock);
255 mdev->device_up = true;
256
257 /* Setup ports */
258
259 /* Create a netdev for each port */
260 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
261 mlx4_info(mdev, "Activating port:%d\n", i);
Yevgeny Petrilin3c2fa832009-06-01 23:22:18 +0000262 if (mlx4_en_init_netdev(mdev, i, &mdev->profile.prof[i]))
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700263 mdev->pndev[i] = NULL;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700264 }
265 return mdev;
266
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700267err_mr:
268 mlx4_mr_free(dev, &mdev->mr);
269err_uar:
270 mlx4_uar_free(dev, &mdev->priv_uar);
271err_pd:
272 mlx4_pd_free(dev, mdev->priv_pdn);
273err_free_dev:
274 kfree(mdev);
275err_free_res:
276 return NULL;
277}
278
279static struct mlx4_interface mlx4_en_interface = {
280 .add = mlx4_en_add,
281 .remove = mlx4_en_remove,
282 .event = mlx4_en_event,
283};
284
285static int __init mlx4_en_init(void)
286{
287 return mlx4_register_interface(&mlx4_en_interface);
288}
289
290static void __exit mlx4_en_cleanup(void)
291{
292 mlx4_unregister_interface(&mlx4_en_interface);
293}
294
295module_init(mlx4_en_init);
296module_exit(mlx4_en_cleanup);
297