15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
35a2cc190SJeff Kirsher  *
45a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
55a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
65a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
75a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
85a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
95a2cc190SJeff Kirsher  *
105a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
115a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
125a2cc190SJeff Kirsher  *     conditions are met:
135a2cc190SJeff Kirsher  *
145a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
155a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
165a2cc190SJeff Kirsher  *        disclaimer.
175a2cc190SJeff Kirsher  *
185a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
195a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
205a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
215a2cc190SJeff Kirsher  *        provided with the distribution.
225a2cc190SJeff Kirsher  *
235a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
245a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
255a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
265a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
275a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
285a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
295a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
305a2cc190SJeff Kirsher  * SOFTWARE.
315a2cc190SJeff Kirsher  *
325a2cc190SJeff Kirsher  */
335a2cc190SJeff Kirsher 
345a2cc190SJeff Kirsher #include <linux/cpumask.h>
355a2cc190SJeff Kirsher #include <linux/module.h>
365a2cc190SJeff Kirsher #include <linux/delay.h>
375a2cc190SJeff Kirsher #include <linux/netdevice.h>
385a2cc190SJeff Kirsher #include <linux/slab.h>
395a2cc190SJeff Kirsher 
405a2cc190SJeff Kirsher #include <linux/mlx4/driver.h>
415a2cc190SJeff Kirsher #include <linux/mlx4/device.h>
425a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
435a2cc190SJeff Kirsher 
445a2cc190SJeff Kirsher #include "mlx4_en.h"
455a2cc190SJeff Kirsher 
465a2cc190SJeff Kirsher MODULE_AUTHOR("Liran Liss, Yevgeny Petrilin");
475a2cc190SJeff Kirsher MODULE_DESCRIPTION("Mellanox ConnectX HCA Ethernet driver");
485a2cc190SJeff Kirsher MODULE_LICENSE("Dual BSD/GPL");
495a2cc190SJeff Kirsher MODULE_VERSION(DRV_VERSION " ("DRV_RELDATE")");
505a2cc190SJeff Kirsher 
515a2cc190SJeff Kirsher static const char mlx4_en_version[] =
525a2cc190SJeff Kirsher 	DRV_NAME ": Mellanox ConnectX HCA Ethernet driver v"
535a2cc190SJeff Kirsher 	DRV_VERSION " (" DRV_RELDATE ")\n";
545a2cc190SJeff Kirsher 
555a2cc190SJeff Kirsher #define MLX4_EN_PARM_INT(X, def_val, desc) \
565a2cc190SJeff Kirsher 	static unsigned int X = def_val;\
575a2cc190SJeff Kirsher 	module_param(X , uint, 0444); \
585a2cc190SJeff Kirsher 	MODULE_PARM_DESC(X, desc);
595a2cc190SJeff Kirsher 
605a2cc190SJeff Kirsher 
615a2cc190SJeff Kirsher /*
625a2cc190SJeff Kirsher  * Device scope module parameters
635a2cc190SJeff Kirsher  */
645a2cc190SJeff Kirsher 
655a2cc190SJeff Kirsher /* Enable RSS UDP traffic */
665a2cc190SJeff Kirsher MLX4_EN_PARM_INT(udp_rss, 1,
675a2cc190SJeff Kirsher 		 "Enable RSS for incomming UDP traffic or disabled (0)");
685a2cc190SJeff Kirsher 
695a2cc190SJeff Kirsher /* Priority pausing */
705a2cc190SJeff Kirsher MLX4_EN_PARM_INT(pfctx, 0, "Priority based Flow Control policy on TX[7:0]."
715a2cc190SJeff Kirsher 			   " Per priority bit mask");
725a2cc190SJeff Kirsher MLX4_EN_PARM_INT(pfcrx, 0, "Priority based Flow Control policy on RX[7:0]."
735a2cc190SJeff Kirsher 			   " Per priority bit mask");
745a2cc190SJeff Kirsher 
755a2cc190SJeff Kirsher int en_print(const char *level, const struct mlx4_en_priv *priv,
765a2cc190SJeff Kirsher 	     const char *format, ...)
775a2cc190SJeff Kirsher {
785a2cc190SJeff Kirsher 	va_list args;
795a2cc190SJeff Kirsher 	struct va_format vaf;
805a2cc190SJeff Kirsher 	int i;
815a2cc190SJeff Kirsher 
825a2cc190SJeff Kirsher 	va_start(args, format);
835a2cc190SJeff Kirsher 
845a2cc190SJeff Kirsher 	vaf.fmt = format;
855a2cc190SJeff Kirsher 	vaf.va = &args;
865a2cc190SJeff Kirsher 	if (priv->registered)
875a2cc190SJeff Kirsher 		i = printk("%s%s: %s: %pV",
885a2cc190SJeff Kirsher 			   level, DRV_NAME, priv->dev->name, &vaf);
895a2cc190SJeff Kirsher 	else
905a2cc190SJeff Kirsher 		i = printk("%s%s: %s: Port %d: %pV",
915a2cc190SJeff Kirsher 			   level, DRV_NAME, dev_name(&priv->mdev->pdev->dev),
925a2cc190SJeff Kirsher 			   priv->port, &vaf);
935a2cc190SJeff Kirsher 	va_end(args);
945a2cc190SJeff Kirsher 
955a2cc190SJeff Kirsher 	return i;
965a2cc190SJeff Kirsher }
975a2cc190SJeff Kirsher 
985a2cc190SJeff Kirsher static int mlx4_en_get_profile(struct mlx4_en_dev *mdev)
995a2cc190SJeff Kirsher {
1005a2cc190SJeff Kirsher 	struct mlx4_en_profile *params = &mdev->profile;
1015a2cc190SJeff Kirsher 	int i;
1025a2cc190SJeff Kirsher 
1035a2cc190SJeff Kirsher 	params->udp_rss = udp_rss;
104bc6a4744SAmir Vadai 	params->num_tx_rings_p_up = min_t(int, num_online_cpus(),
105bc6a4744SAmir Vadai 			MLX4_EN_MAX_TX_RING_P_UP);
1065a2cc190SJeff Kirsher 	if (params->udp_rss && !(mdev->dev->caps.flags
1075a2cc190SJeff Kirsher 					& MLX4_DEV_CAP_FLAG_UDP_RSS)) {
1085a2cc190SJeff Kirsher 		mlx4_warn(mdev, "UDP RSS is not supported on this device.\n");
1095a2cc190SJeff Kirsher 		params->udp_rss = 0;
1105a2cc190SJeff Kirsher 	}
1115a2cc190SJeff Kirsher 	for (i = 1; i <= MLX4_MAX_PORTS; i++) {
1125a2cc190SJeff Kirsher 		params->prof[i].rx_pause = 1;
1135a2cc190SJeff Kirsher 		params->prof[i].rx_ppp = pfcrx;
1145a2cc190SJeff Kirsher 		params->prof[i].tx_pause = 1;
1155a2cc190SJeff Kirsher 		params->prof[i].tx_ppp = pfctx;
1165a2cc190SJeff Kirsher 		params->prof[i].tx_ring_size = MLX4_EN_DEF_TX_RING_SIZE;
1175a2cc190SJeff Kirsher 		params->prof[i].rx_ring_size = MLX4_EN_DEF_RX_RING_SIZE;
118bc6a4744SAmir Vadai 		params->prof[i].tx_ring_num = params->num_tx_rings_p_up *
119bc6a4744SAmir Vadai 			MLX4_EN_NUM_UP;
12093d3e367SYevgeny Petrilin 		params->prof[i].rss_rings = 0;
1215a2cc190SJeff Kirsher 	}
1225a2cc190SJeff Kirsher 
1235a2cc190SJeff Kirsher 	return 0;
1245a2cc190SJeff Kirsher }
1255a2cc190SJeff Kirsher 
1265a2cc190SJeff Kirsher static void *mlx4_en_get_netdev(struct mlx4_dev *dev, void *ctx, u8 port)
1275a2cc190SJeff Kirsher {
1285a2cc190SJeff Kirsher 	struct mlx4_en_dev *endev = ctx;
1295a2cc190SJeff Kirsher 
1305a2cc190SJeff Kirsher 	return endev->pndev[port];
1315a2cc190SJeff Kirsher }
1325a2cc190SJeff Kirsher 
1335a2cc190SJeff Kirsher static void mlx4_en_event(struct mlx4_dev *dev, void *endev_ptr,
1345a2cc190SJeff Kirsher 			  enum mlx4_dev_event event, int port)
1355a2cc190SJeff Kirsher {
1365a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = (struct mlx4_en_dev *) endev_ptr;
1375a2cc190SJeff Kirsher 	struct mlx4_en_priv *priv;
1385a2cc190SJeff Kirsher 
1395a2cc190SJeff Kirsher 	if (!mdev->pndev[port])
1405a2cc190SJeff Kirsher 		return;
1415a2cc190SJeff Kirsher 
1425a2cc190SJeff Kirsher 	priv = netdev_priv(mdev->pndev[port]);
1435a2cc190SJeff Kirsher 	switch (event) {
1445a2cc190SJeff Kirsher 	case MLX4_DEV_EVENT_PORT_UP:
1455a2cc190SJeff Kirsher 	case MLX4_DEV_EVENT_PORT_DOWN:
1465a2cc190SJeff Kirsher 		/* To prevent races, we poll the link state in a separate
1475a2cc190SJeff Kirsher 		  task rather than changing it here */
1485a2cc190SJeff Kirsher 		priv->link_state = event;
1495a2cc190SJeff Kirsher 		queue_work(mdev->workqueue, &priv->linkstate_task);
1505a2cc190SJeff Kirsher 		break;
1515a2cc190SJeff Kirsher 
1525a2cc190SJeff Kirsher 	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
1535a2cc190SJeff Kirsher 		mlx4_err(mdev, "Internal error detected, restarting device\n");
1545a2cc190SJeff Kirsher 		break;
1555a2cc190SJeff Kirsher 
1565a2cc190SJeff Kirsher 	default:
1575a2cc190SJeff Kirsher 		mlx4_warn(mdev, "Unhandled event: %d\n", event);
1585a2cc190SJeff Kirsher 	}
1595a2cc190SJeff Kirsher }
1605a2cc190SJeff Kirsher 
1615a2cc190SJeff Kirsher static void mlx4_en_remove(struct mlx4_dev *dev, void *endev_ptr)
1625a2cc190SJeff Kirsher {
1635a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = endev_ptr;
1645a2cc190SJeff Kirsher 	int i;
1655a2cc190SJeff Kirsher 
1665a2cc190SJeff Kirsher 	mutex_lock(&mdev->state_lock);
1675a2cc190SJeff Kirsher 	mdev->device_up = false;
1685a2cc190SJeff Kirsher 	mutex_unlock(&mdev->state_lock);
1695a2cc190SJeff Kirsher 
1705a2cc190SJeff Kirsher 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
1715a2cc190SJeff Kirsher 		if (mdev->pndev[i])
1725a2cc190SJeff Kirsher 			mlx4_en_destroy_netdev(mdev->pndev[i]);
1735a2cc190SJeff Kirsher 
1745a2cc190SJeff Kirsher 	flush_workqueue(mdev->workqueue);
1755a2cc190SJeff Kirsher 	destroy_workqueue(mdev->workqueue);
1765a2cc190SJeff Kirsher 	mlx4_mr_free(dev, &mdev->mr);
1777398af40SAlexander Guller 	iounmap(mdev->uar_map);
1785a2cc190SJeff Kirsher 	mlx4_uar_free(dev, &mdev->priv_uar);
1795a2cc190SJeff Kirsher 	mlx4_pd_free(dev, mdev->priv_pdn);
1805a2cc190SJeff Kirsher 	kfree(mdev);
1815a2cc190SJeff Kirsher }
1825a2cc190SJeff Kirsher 
1835a2cc190SJeff Kirsher static void *mlx4_en_add(struct mlx4_dev *dev)
1845a2cc190SJeff Kirsher {
1855a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev;
1865a2cc190SJeff Kirsher 	int i;
1875a2cc190SJeff Kirsher 	int err;
1885a2cc190SJeff Kirsher 
1895a2cc190SJeff Kirsher 	printk_once(KERN_INFO "%s", mlx4_en_version);
1905a2cc190SJeff Kirsher 
1915a2cc190SJeff Kirsher 	mdev = kzalloc(sizeof *mdev, GFP_KERNEL);
1925a2cc190SJeff Kirsher 	if (!mdev) {
1935a2cc190SJeff Kirsher 		dev_err(&dev->pdev->dev, "Device struct alloc failed, "
1945a2cc190SJeff Kirsher 			"aborting.\n");
1955a2cc190SJeff Kirsher 		err = -ENOMEM;
1965a2cc190SJeff Kirsher 		goto err_free_res;
1975a2cc190SJeff Kirsher 	}
1985a2cc190SJeff Kirsher 
1995a2cc190SJeff Kirsher 	if (mlx4_pd_alloc(dev, &mdev->priv_pdn))
2005a2cc190SJeff Kirsher 		goto err_free_dev;
2015a2cc190SJeff Kirsher 
2025a2cc190SJeff Kirsher 	if (mlx4_uar_alloc(dev, &mdev->priv_uar))
2035a2cc190SJeff Kirsher 		goto err_pd;
2045a2cc190SJeff Kirsher 
2055a2cc190SJeff Kirsher 	mdev->uar_map = ioremap((phys_addr_t) mdev->priv_uar.pfn << PAGE_SHIFT,
2065a2cc190SJeff Kirsher 				PAGE_SIZE);
2075a2cc190SJeff Kirsher 	if (!mdev->uar_map)
2085a2cc190SJeff Kirsher 		goto err_uar;
2095a2cc190SJeff Kirsher 	spin_lock_init(&mdev->uar_lock);
2105a2cc190SJeff Kirsher 
2115a2cc190SJeff Kirsher 	mdev->dev = dev;
2125a2cc190SJeff Kirsher 	mdev->dma_device = &(dev->pdev->dev);
2135a2cc190SJeff Kirsher 	mdev->pdev = dev->pdev;
2145a2cc190SJeff Kirsher 	mdev->device_up = false;
2155a2cc190SJeff Kirsher 
2165a2cc190SJeff Kirsher 	mdev->LSO_support = !!(dev->caps.flags & (1 << 15));
2175a2cc190SJeff Kirsher 	if (!mdev->LSO_support)
2185a2cc190SJeff Kirsher 		mlx4_warn(mdev, "LSO not supported, please upgrade to later "
2195a2cc190SJeff Kirsher 				"FW version to enable LSO\n");
2205a2cc190SJeff Kirsher 
2215a2cc190SJeff Kirsher 	if (mlx4_mr_alloc(mdev->dev, mdev->priv_pdn, 0, ~0ull,
2225a2cc190SJeff Kirsher 			 MLX4_PERM_LOCAL_WRITE |  MLX4_PERM_LOCAL_READ,
2235a2cc190SJeff Kirsher 			 0, 0, &mdev->mr)) {
2245a2cc190SJeff Kirsher 		mlx4_err(mdev, "Failed allocating memory region\n");
2257398af40SAlexander Guller 		goto err_map;
2265a2cc190SJeff Kirsher 	}
2275a2cc190SJeff Kirsher 	if (mlx4_mr_enable(mdev->dev, &mdev->mr)) {
2285a2cc190SJeff Kirsher 		mlx4_err(mdev, "Failed enabling memory region\n");
2295a2cc190SJeff Kirsher 		goto err_mr;
2305a2cc190SJeff Kirsher 	}
2315a2cc190SJeff Kirsher 
2325a2cc190SJeff Kirsher 	/* Build device profile according to supplied module parameters */
2335a2cc190SJeff Kirsher 	err = mlx4_en_get_profile(mdev);
2345a2cc190SJeff Kirsher 	if (err) {
2355a2cc190SJeff Kirsher 		mlx4_err(mdev, "Bad module parameters, aborting.\n");
2365a2cc190SJeff Kirsher 		goto err_mr;
2375a2cc190SJeff Kirsher 	}
2385a2cc190SJeff Kirsher 
2395a2cc190SJeff Kirsher 	/* Configure which ports to start according to module parameters */
2405a2cc190SJeff Kirsher 	mdev->port_cnt = 0;
2415a2cc190SJeff Kirsher 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
2425a2cc190SJeff Kirsher 		mdev->port_cnt++;
2435a2cc190SJeff Kirsher 
2445a2cc190SJeff Kirsher 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2455a2cc190SJeff Kirsher 		if (!dev->caps.comp_pool) {
2465a2cc190SJeff Kirsher 			mdev->profile.prof[i].rx_ring_num =
2475a2cc190SJeff Kirsher 				rounddown_pow_of_two(max_t(int, MIN_RX_RINGS,
2485a2cc190SJeff Kirsher 							   min_t(int,
2495a2cc190SJeff Kirsher 								 dev->caps.num_comp_vectors,
2505a2cc190SJeff Kirsher 								 MAX_RX_RINGS)));
2515a2cc190SJeff Kirsher 		} else {
2525a2cc190SJeff Kirsher 			mdev->profile.prof[i].rx_ring_num = rounddown_pow_of_two(
2535a2cc190SJeff Kirsher 				min_t(int, dev->caps.comp_pool/
2545a2cc190SJeff Kirsher 				      dev->caps.num_ports - 1 , MAX_MSIX_P_PORT - 1));
2555a2cc190SJeff Kirsher 		}
2565a2cc190SJeff Kirsher 	}
2575a2cc190SJeff Kirsher 
2585a2cc190SJeff Kirsher 	/* Create our own workqueue for reset/multicast tasks
2595a2cc190SJeff Kirsher 	 * Note: we cannot use the shared workqueue because of deadlocks caused
2605a2cc190SJeff Kirsher 	 *       by the rtnl lock */
2615a2cc190SJeff Kirsher 	mdev->workqueue = create_singlethread_workqueue("mlx4_en");
2625a2cc190SJeff Kirsher 	if (!mdev->workqueue) {
2635a2cc190SJeff Kirsher 		err = -ENOMEM;
2645a2cc190SJeff Kirsher 		goto err_mr;
2655a2cc190SJeff Kirsher 	}
2665a2cc190SJeff Kirsher 
2675a2cc190SJeff Kirsher 	/* At this stage all non-port specific tasks are complete:
2685a2cc190SJeff Kirsher 	 * mark the card state as up */
2695a2cc190SJeff Kirsher 	mutex_init(&mdev->state_lock);
2705a2cc190SJeff Kirsher 	mdev->device_up = true;
2715a2cc190SJeff Kirsher 
2725a2cc190SJeff Kirsher 	/* Setup ports */
2735a2cc190SJeff Kirsher 
2745a2cc190SJeff Kirsher 	/* Create a netdev for each port */
2755a2cc190SJeff Kirsher 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2765a2cc190SJeff Kirsher 		mlx4_info(mdev, "Activating port:%d\n", i);
2775a2cc190SJeff Kirsher 		if (mlx4_en_init_netdev(mdev, i, &mdev->profile.prof[i]))
2785a2cc190SJeff Kirsher 			mdev->pndev[i] = NULL;
2795a2cc190SJeff Kirsher 	}
2805a2cc190SJeff Kirsher 	return mdev;
2815a2cc190SJeff Kirsher 
2825a2cc190SJeff Kirsher err_mr:
2835a2cc190SJeff Kirsher 	mlx4_mr_free(dev, &mdev->mr);
2847398af40SAlexander Guller err_map:
2857398af40SAlexander Guller 	if (!mdev->uar_map)
2867398af40SAlexander Guller 		iounmap(mdev->uar_map);
2875a2cc190SJeff Kirsher err_uar:
2885a2cc190SJeff Kirsher 	mlx4_uar_free(dev, &mdev->priv_uar);
2895a2cc190SJeff Kirsher err_pd:
2905a2cc190SJeff Kirsher 	mlx4_pd_free(dev, mdev->priv_pdn);
2915a2cc190SJeff Kirsher err_free_dev:
2925a2cc190SJeff Kirsher 	kfree(mdev);
2935a2cc190SJeff Kirsher err_free_res:
2945a2cc190SJeff Kirsher 	return NULL;
2955a2cc190SJeff Kirsher }
2965a2cc190SJeff Kirsher 
2975a2cc190SJeff Kirsher static struct mlx4_interface mlx4_en_interface = {
2985a2cc190SJeff Kirsher 	.add		= mlx4_en_add,
2995a2cc190SJeff Kirsher 	.remove		= mlx4_en_remove,
3005a2cc190SJeff Kirsher 	.event		= mlx4_en_event,
3015a2cc190SJeff Kirsher 	.get_dev	= mlx4_en_get_netdev,
3025a2cc190SJeff Kirsher 	.protocol	= MLX4_PROT_ETH,
3035a2cc190SJeff Kirsher };
3045a2cc190SJeff Kirsher 
3055a2cc190SJeff Kirsher static int __init mlx4_en_init(void)
3065a2cc190SJeff Kirsher {
3075a2cc190SJeff Kirsher 	return mlx4_register_interface(&mlx4_en_interface);
3085a2cc190SJeff Kirsher }
3095a2cc190SJeff Kirsher 
3105a2cc190SJeff Kirsher static void __exit mlx4_en_cleanup(void)
3115a2cc190SJeff Kirsher {
3125a2cc190SJeff Kirsher 	mlx4_unregister_interface(&mlx4_en_interface);
3135a2cc190SJeff Kirsher }
3145a2cc190SJeff Kirsher 
3155a2cc190SJeff Kirsher module_init(mlx4_en_init);
3165a2cc190SJeff Kirsher module_exit(mlx4_en_cleanup);
3175a2cc190SJeff Kirsher 
318