1219820Sjeff/*
2272407Shselasky * Copyright (c) 2007, 2014 Mellanox Technologies. All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff *
32219820Sjeff */
33219820Sjeff
34219820Sjeff#include <linux/module.h>
35219820Sjeff#include <linux/delay.h>
36219820Sjeff#include <linux/netdevice.h>
37272407Shselasky#include <linux/slab.h>
38219820Sjeff
39219820Sjeff#include <linux/mlx4/driver.h>
40219820Sjeff#include <linux/mlx4/device.h>
41219820Sjeff#include <linux/mlx4/cmd.h>
42219820Sjeff
43219820Sjeff#include "mlx4_en.h"
44219820Sjeff
45292107Shselasky/* Mellanox ConnectX HCA Ethernet driver */
46219820Sjeff
47219820Sjeff#define MLX4_EN_PARM_INT(X, def_val, desc) \
48219820Sjeff	static unsigned int X = def_val;\
49219820Sjeff	module_param(X , uint, 0444); \
50219820Sjeff	MODULE_PARM_DESC(X, desc);
51219820Sjeff
52219820Sjeff
53219820Sjeff/*
54219820Sjeff * Device scope module parameters
55219820Sjeff */
56219820Sjeff
57219820Sjeff/* Enable RSS UDP traffic */
58219820SjeffMLX4_EN_PARM_INT(udp_rss, 1,
59272407Shselasky		 "Enable RSS for incoming UDP traffic");
60219820Sjeff
61219820Sjeff/* Priority pausing */
62219820SjeffMLX4_EN_PARM_INT(pfctx, 0, "Priority based Flow Control policy on TX[7:0]."
63219820Sjeff			   " Per priority bit mask");
64219820SjeffMLX4_EN_PARM_INT(pfcrx, 0, "Priority based Flow Control policy on RX[7:0]."
65219820Sjeff			   " Per priority bit mask");
66219820Sjeff
67272407Shselasky#define MAX_PFC_TX	0xff
68272407Shselasky#define MAX_PFC_RX	0xff
69272407Shselasky
70272407Shselasky
71219820Sjeffstatic int mlx4_en_get_profile(struct mlx4_en_dev *mdev)
72219820Sjeff{
73219820Sjeff	struct mlx4_en_profile *params = &mdev->profile;
74219820Sjeff	int i;
75219820Sjeff
76219820Sjeff	params->udp_rss = udp_rss;
77272407Shselasky	params->num_tx_rings_p_up = min_t(int, mp_ncpus,
78272407Shselasky			MLX4_EN_MAX_TX_RING_P_UP);
79272407Shselasky	if (params->udp_rss && !(mdev->dev->caps.flags
80272407Shselasky					& MLX4_DEV_CAP_FLAG_UDP_RSS)) {
81219820Sjeff		mlx4_warn(mdev, "UDP RSS is not supported on this device.\n");
82219820Sjeff		params->udp_rss = 0;
83219820Sjeff	}
84219820Sjeff	for (i = 1; i <= MLX4_MAX_PORTS; i++) {
85219820Sjeff		params->prof[i].rx_pause = 1;
86219820Sjeff		params->prof[i].rx_ppp = pfcrx;
87219820Sjeff		params->prof[i].tx_pause = 1;
88219820Sjeff		params->prof[i].tx_ppp = pfctx;
89219820Sjeff		params->prof[i].tx_ring_size = MLX4_EN_DEF_TX_RING_SIZE;
90219820Sjeff		params->prof[i].rx_ring_size = MLX4_EN_DEF_RX_RING_SIZE;
91272407Shselasky		params->prof[i].tx_ring_num = params->num_tx_rings_p_up *
92272407Shselasky			MLX4_EN_NUM_UP;
93272407Shselasky		params->prof[i].rss_rings = 0;
94219820Sjeff	}
95219820Sjeff
96219820Sjeff	return 0;
97219820Sjeff}
98219820Sjeff
99272407Shselaskystatic void *mlx4_en_get_netdev(struct mlx4_dev *dev, void *ctx, u8 port)
100219820Sjeff{
101219820Sjeff	struct mlx4_en_dev *endev = ctx;
102219820Sjeff
103219820Sjeff	return endev->pndev[port];
104219820Sjeff}
105219820Sjeff
106219820Sjeffstatic void mlx4_en_event(struct mlx4_dev *dev, void *endev_ptr,
107255932Salfred			  enum mlx4_dev_event event, unsigned long port)
108219820Sjeff{
109219820Sjeff	struct mlx4_en_dev *mdev = (struct mlx4_en_dev *) endev_ptr;
110219820Sjeff	struct mlx4_en_priv *priv;
111219820Sjeff
112219820Sjeff	switch (event) {
113219820Sjeff	case MLX4_DEV_EVENT_PORT_UP:
114219820Sjeff	case MLX4_DEV_EVENT_PORT_DOWN:
115255932Salfred		if (!mdev->pndev[port])
116255932Salfred			return;
117255932Salfred		priv = netdev_priv(mdev->pndev[port]);
118219820Sjeff		/* To prevent races, we poll the link state in a separate
119219820Sjeff		  task rather than changing it here */
120219820Sjeff		priv->link_state = event;
121219820Sjeff		queue_work(mdev->workqueue, &priv->linkstate_task);
122219820Sjeff		break;
123219820Sjeff
124219820Sjeff	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
125219820Sjeff		mlx4_err(mdev, "Internal error detected, restarting device\n");
126219820Sjeff		break;
127219820Sjeff
128272407Shselasky	case MLX4_DEV_EVENT_SLAVE_INIT:
129272407Shselasky	case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
130272407Shselasky		break;
131219820Sjeff	default:
132255932Salfred		if (port < 1 || port > dev->caps.num_ports ||
133255932Salfred		    !mdev->pndev[port])
134255932Salfred			return;
135255932Salfred		mlx4_warn(mdev, "Unhandled event %d for port %d\n", event,
136255932Salfred			  (int) port);
137219820Sjeff	}
138219820Sjeff}
139219820Sjeff
140219820Sjeffstatic void mlx4_en_remove(struct mlx4_dev *dev, void *endev_ptr)
141219820Sjeff{
142219820Sjeff	struct mlx4_en_dev *mdev = endev_ptr;
143272407Shselasky	int i, ret;
144219820Sjeff
145219820Sjeff	mutex_lock(&mdev->state_lock);
146219820Sjeff	mdev->device_up = false;
147219820Sjeff	mutex_unlock(&mdev->state_lock);
148219820Sjeff
149219820Sjeff	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
150219820Sjeff		if (mdev->pndev[i])
151219820Sjeff			mlx4_en_destroy_netdev(mdev->pndev[i]);
152219820Sjeff
153219820Sjeff	flush_workqueue(mdev->workqueue);
154219820Sjeff	destroy_workqueue(mdev->workqueue);
155272407Shselasky	ret = mlx4_mr_free(dev, &mdev->mr);
156272407Shselasky	if (ret)
157272407Shselasky		mlx4_err(mdev, "Error deregistering MR. The system may have become unstable.");
158272407Shselasky	iounmap(mdev->uar_map);
159219820Sjeff	mlx4_uar_free(dev, &mdev->priv_uar);
160219820Sjeff	mlx4_pd_free(dev, mdev->priv_pdn);
161219820Sjeff	kfree(mdev);
162219820Sjeff}
163219820Sjeff
164219820Sjeffstatic void *mlx4_en_add(struct mlx4_dev *dev)
165219820Sjeff{
166219820Sjeff	struct mlx4_en_dev *mdev;
167219820Sjeff	int i;
168219820Sjeff	int err;
169219820Sjeff
170219820Sjeff	mdev = kzalloc(sizeof *mdev, GFP_KERNEL);
171219820Sjeff	if (!mdev) {
172219820Sjeff		dev_err(&dev->pdev->dev, "Device struct alloc failed, "
173219820Sjeff			"aborting.\n");
174219820Sjeff		err = -ENOMEM;
175219820Sjeff		goto err_free_res;
176219820Sjeff	}
177219820Sjeff
178219820Sjeff	if (mlx4_pd_alloc(dev, &mdev->priv_pdn))
179219820Sjeff		goto err_free_dev;
180219820Sjeff
181219820Sjeff	if (mlx4_uar_alloc(dev, &mdev->priv_uar))
182219820Sjeff		goto err_pd;
183219820Sjeff
184272407Shselasky	mdev->uar_map = ioremap((phys_addr_t) mdev->priv_uar.pfn << PAGE_SHIFT,
185272407Shselasky				PAGE_SIZE);
186219820Sjeff	if (!mdev->uar_map)
187219820Sjeff		goto err_uar;
188272407Shselasky	spin_lock_init(&mdev->uar_lock);
189219820Sjeff
190219820Sjeff	mdev->dev = dev;
191219820Sjeff	mdev->dma_device = &(dev->pdev->dev);
192219820Sjeff	mdev->pdev = dev->pdev;
193219820Sjeff	mdev->device_up = false;
194219820Sjeff
195219820Sjeff	mdev->LSO_support = !!(dev->caps.flags & (1 << 15));
196219820Sjeff	if (!mdev->LSO_support)
197219820Sjeff		mlx4_warn(mdev, "LSO not supported, please upgrade to later "
198219820Sjeff				"FW version to enable LSO\n");
199219820Sjeff
200219820Sjeff	if (mlx4_mr_alloc(mdev->dev, mdev->priv_pdn, 0, ~0ull,
201219820Sjeff			 MLX4_PERM_LOCAL_WRITE |  MLX4_PERM_LOCAL_READ,
202219820Sjeff			 0, 0, &mdev->mr)) {
203219820Sjeff		mlx4_err(mdev, "Failed allocating memory region\n");
204272407Shselasky		goto err_map;
205219820Sjeff	}
206219820Sjeff	if (mlx4_mr_enable(mdev->dev, &mdev->mr)) {
207219820Sjeff		mlx4_err(mdev, "Failed enabling memory region\n");
208219820Sjeff		goto err_mr;
209219820Sjeff	}
210219820Sjeff
211219820Sjeff	/* Build device profile according to supplied module parameters */
212219820Sjeff	err = mlx4_en_get_profile(mdev);
213219820Sjeff	if (err) {
214219820Sjeff		mlx4_err(mdev, "Bad module parameters, aborting.\n");
215219820Sjeff		goto err_mr;
216219820Sjeff	}
217219820Sjeff
218272407Shselasky	/* Configure which ports to start according to module parameters */
219219820Sjeff	mdev->port_cnt = 0;
220219820Sjeff	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
221219820Sjeff		mdev->port_cnt++;
222219820Sjeff
223272407Shselasky
224219820Sjeff	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
225272407Shselasky		if (!dev->caps.comp_pool) {
226272407Shselasky			mdev->profile.prof[i].rx_ring_num =
227272407Shselasky				rounddown_pow_of_two(max_t(int, MIN_RX_RINGS,
228272407Shselasky							   min_t(int,
229272407Shselasky								 dev->caps.num_comp_vectors,
230272407Shselasky								 DEF_RX_RINGS)));
231272407Shselasky		} else {
232272407Shselasky			mdev->profile.prof[i].rx_ring_num = rounddown_pow_of_two(
233279731Shselasky				min_t(int, dev->caps.comp_pool /
234279731Shselasky				      dev->caps.num_ports, MAX_MSIX_P_PORT));
235272407Shselasky		}
236219820Sjeff	}
237219820Sjeff
238219820Sjeff	/* Create our own workqueue for reset/multicast tasks
239219820Sjeff	 * Note: we cannot use the shared workqueue because of deadlocks caused
240219820Sjeff	 *       by the rtnl lock */
241219820Sjeff	mdev->workqueue = create_singlethread_workqueue("mlx4_en");
242219820Sjeff	if (!mdev->workqueue) {
243219820Sjeff		err = -ENOMEM;
244219820Sjeff		goto err_mr;
245219820Sjeff	}
246219820Sjeff
247219820Sjeff	/* At this stage all non-port specific tasks are complete:
248219820Sjeff	 * mark the card state as up */
249272407Shselasky	mutex_init(&mdev->state_lock);
250219820Sjeff	mdev->device_up = true;
251219820Sjeff
252219820Sjeff	/* Setup ports */
253219820Sjeff
254219820Sjeff	/* Create a netdev for each port */
255219820Sjeff	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
256219820Sjeff		mlx4_info(mdev, "Activating port:%d\n", i);
257272407Shselasky		if (mlx4_en_init_netdev(mdev, i, &mdev->profile.prof[i]))
258219820Sjeff			mdev->pndev[i] = NULL;
259219820Sjeff	}
260272407Shselasky
261219820Sjeff	return mdev;
262219820Sjeff
263219820Sjefferr_mr:
264272407Shselasky	err = mlx4_mr_free(dev, &mdev->mr);
265272407Shselasky	if (err)
266272407Shselasky		mlx4_err(mdev, "Error deregistering MR. The system may have become unstable.");
267272407Shselaskyerr_map:
268272407Shselasky	if (mdev->uar_map)
269272407Shselasky		iounmap(mdev->uar_map);
270219820Sjefferr_uar:
271219820Sjeff	mlx4_uar_free(dev, &mdev->priv_uar);
272219820Sjefferr_pd:
273219820Sjeff	mlx4_pd_free(dev, mdev->priv_pdn);
274219820Sjefferr_free_dev:
275219820Sjeff	kfree(mdev);
276219820Sjefferr_free_res:
277219820Sjeff	return NULL;
278219820Sjeff}
279219820Sjeff
280272407Shselaskystatic struct mlx4_interface mlx4_en_interface = {
281272407Shselasky	.add		= mlx4_en_add,
282272407Shselasky	.remove		= mlx4_en_remove,
283272407Shselasky	.event		= mlx4_en_event,
284272407Shselasky	.get_dev	= mlx4_en_get_netdev,
285272407Shselasky	.protocol	= MLX4_PROT_ETH,
286272407Shselasky};
287272407Shselasky
288272407Shselaskystatic void mlx4_en_verify_params(void)
289219820Sjeff{
290272407Shselasky        if (pfctx > MAX_PFC_TX) {
291272407Shselasky                pr_warn("mlx4_en: WARNING: illegal module parameter pfctx 0x%x - "
292272407Shselasky                                "should be in range 0-0x%x, will be changed to default (0)\n",
293272407Shselasky                                pfctx, MAX_PFC_TX);
294272407Shselasky                pfctx = 0;
295272407Shselasky        }
296219820Sjeff
297272407Shselasky        if (pfcrx > MAX_PFC_RX) {
298272407Shselasky                pr_warn("mlx4_en: WARNING: illegal module parameter pfcrx 0x%x - "
299272407Shselasky                                "should be in range 0-0x%x, will be changed to default (0)\n",
300272407Shselasky                                pfcrx, MAX_PFC_RX);
301272407Shselasky                pfcrx = 0;
302272407Shselasky        }
303219820Sjeff}
304219820Sjeff
305219820Sjeff
306219820Sjeffstatic int __init mlx4_en_init(void)
307219820Sjeff{
308272407Shselasky        mlx4_en_verify_params();
309272407Shselasky
310272407Shselasky#ifdef CONFIG_DEBUG_FS
311272407Shselasky	int err = 0;
312272407Shselasky	err = mlx4_en_register_debugfs();
313272407Shselasky	if (err)
314272407Shselasky		pr_err(KERN_ERR "Failed to register debugfs\n");
315272407Shselasky#endif
316219820Sjeff	return mlx4_register_interface(&mlx4_en_interface);
317219820Sjeff}
318219820Sjeff
319219820Sjeffstatic void __exit mlx4_en_cleanup(void)
320219820Sjeff{
321219820Sjeff	mlx4_unregister_interface(&mlx4_en_interface);
322272407Shselasky#ifdef CONFIG_DEBUG_FS
323272407Shselasky	mlx4_en_unregister_debugfs();
324272407Shselasky#endif
325219820Sjeff}
326219820Sjeff
327219820Sjeffmodule_init(mlx4_en_init);
328219820Sjeffmodule_exit(mlx4_en_cleanup);
329219820Sjeff
330219820Sjeff#undef MODULE_VERSION
331219820Sjeff#include <sys/module.h>
332219820Sjeffstatic int
333219820Sjeffmlxen_evhand(module_t mod, int event, void *arg)
334219820Sjeff{
335219820Sjeff        return (0);
336219820Sjeff}
337219820Sjeffstatic moduledata_t mlxen_mod = {
338219820Sjeff        .name = "mlxen",
339219820Sjeff	.evhand = mlxen_evhand,
340219820Sjeff};
341269862ShselaskyDECLARE_MODULE(mlxen, mlxen_mod, SI_SUB_OFED_PREINIT, SI_ORDER_ANY);
342219820SjeffMODULE_DEPEND(mlxen, mlx4, 1, 1, 1);
343