xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlx4/reset.c (revision e5451c8f8330e03ad3cfa16048b4daf961af434f)
15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2006, 2007 Cisco Systems, Inc.  All rights reserved.
35a2cc190SJeff Kirsher  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
45a2cc190SJeff Kirsher  *
55a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
65a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
75a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
85a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
95a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
105a2cc190SJeff Kirsher  *
115a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
125a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
135a2cc190SJeff Kirsher  *     conditions are met:
145a2cc190SJeff Kirsher  *
155a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
165a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
175a2cc190SJeff Kirsher  *        disclaimer.
185a2cc190SJeff Kirsher  *
195a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
205a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
215a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
225a2cc190SJeff Kirsher  *        provided with the distribution.
235a2cc190SJeff Kirsher  *
245a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
255a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
265a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
275a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
285a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
295a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
305a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
315a2cc190SJeff Kirsher  * SOFTWARE.
325a2cc190SJeff Kirsher  */
335a2cc190SJeff Kirsher 
345a2cc190SJeff Kirsher #include <linux/errno.h>
355a2cc190SJeff Kirsher #include <linux/pci.h>
365a2cc190SJeff Kirsher #include <linux/delay.h>
375a2cc190SJeff Kirsher #include <linux/slab.h>
385a2cc190SJeff Kirsher #include <linux/jiffies.h>
395a2cc190SJeff Kirsher 
405a2cc190SJeff Kirsher #include "mlx4.h"
415a2cc190SJeff Kirsher 
mlx4_reset(struct mlx4_dev * dev)425a2cc190SJeff Kirsher int mlx4_reset(struct mlx4_dev *dev)
435a2cc190SJeff Kirsher {
445a2cc190SJeff Kirsher 	void __iomem *reset;
455a2cc190SJeff Kirsher 	u32 *hca_header = NULL;
465a2cc190SJeff Kirsher 	int pcie_cap;
475a2cc190SJeff Kirsher 	u16 devctl;
485a2cc190SJeff Kirsher 	u16 linkctl;
495a2cc190SJeff Kirsher 	u16 vendor;
505a2cc190SJeff Kirsher 	unsigned long end;
515a2cc190SJeff Kirsher 	u32 sem;
525a2cc190SJeff Kirsher 	int i;
535a2cc190SJeff Kirsher 	int err = 0;
545a2cc190SJeff Kirsher 
555a2cc190SJeff Kirsher #define MLX4_RESET_BASE		0xf0000
565a2cc190SJeff Kirsher #define MLX4_RESET_SIZE		  0x400
575a2cc190SJeff Kirsher #define MLX4_SEM_OFFSET		  0x3fc
585a2cc190SJeff Kirsher #define MLX4_RESET_OFFSET	   0x10
595a2cc190SJeff Kirsher #define MLX4_RESET_VALUE	swab32(1)
605a2cc190SJeff Kirsher 
615a2cc190SJeff Kirsher #define MLX4_SEM_TIMEOUT_JIFFIES	(10 * HZ)
625a2cc190SJeff Kirsher #define MLX4_RESET_TIMEOUT_JIFFIES	(2 * HZ)
635a2cc190SJeff Kirsher 
645a2cc190SJeff Kirsher 	/*
655a2cc190SJeff Kirsher 	 * Reset the chip.  This is somewhat ugly because we have to
665a2cc190SJeff Kirsher 	 * save off the PCI header before reset and then restore it
675a2cc190SJeff Kirsher 	 * after the chip reboots.  We skip config space offsets 22
685a2cc190SJeff Kirsher 	 * and 23 since those have a special meaning.
695a2cc190SJeff Kirsher 	 */
705a2cc190SJeff Kirsher 
715a2cc190SJeff Kirsher 	/* Do we need to save off the full 4K PCI Express header?? */
725a2cc190SJeff Kirsher 	hca_header = kmalloc(256, GFP_KERNEL);
735a2cc190SJeff Kirsher 	if (!hca_header) {
745a2cc190SJeff Kirsher 		err = -ENOMEM;
751a91de28SJoe Perches 		mlx4_err(dev, "Couldn't allocate memory to save HCA PCI header, aborting\n");
765a2cc190SJeff Kirsher 		goto out;
775a2cc190SJeff Kirsher 	}
785a2cc190SJeff Kirsher 
79*872bf2fbSYishai Hadas 	pcie_cap = pci_pcie_cap(dev->persist->pdev);
805a2cc190SJeff Kirsher 
815a2cc190SJeff Kirsher 	for (i = 0; i < 64; ++i) {
825a2cc190SJeff Kirsher 		if (i == 22 || i == 23)
835a2cc190SJeff Kirsher 			continue;
84*872bf2fbSYishai Hadas 		if (pci_read_config_dword(dev->persist->pdev, i * 4,
85*872bf2fbSYishai Hadas 					  hca_header + i)) {
865a2cc190SJeff Kirsher 			err = -ENODEV;
871a91de28SJoe Perches 			mlx4_err(dev, "Couldn't save HCA PCI header, aborting\n");
885a2cc190SJeff Kirsher 			goto out;
895a2cc190SJeff Kirsher 		}
905a2cc190SJeff Kirsher 	}
915a2cc190SJeff Kirsher 
92*872bf2fbSYishai Hadas 	reset = ioremap(pci_resource_start(dev->persist->pdev, 0) +
93*872bf2fbSYishai Hadas 			MLX4_RESET_BASE,
945a2cc190SJeff Kirsher 			MLX4_RESET_SIZE);
955a2cc190SJeff Kirsher 	if (!reset) {
965a2cc190SJeff Kirsher 		err = -ENOMEM;
971a91de28SJoe Perches 		mlx4_err(dev, "Couldn't map HCA reset register, aborting\n");
985a2cc190SJeff Kirsher 		goto out;
995a2cc190SJeff Kirsher 	}
1005a2cc190SJeff Kirsher 
1015a2cc190SJeff Kirsher 	/* grab HW semaphore to lock out flash updates */
1025a2cc190SJeff Kirsher 	end = jiffies + MLX4_SEM_TIMEOUT_JIFFIES;
1035a2cc190SJeff Kirsher 	do {
1045a2cc190SJeff Kirsher 		sem = readl(reset + MLX4_SEM_OFFSET);
1055a2cc190SJeff Kirsher 		if (!sem)
1065a2cc190SJeff Kirsher 			break;
1075a2cc190SJeff Kirsher 
1085a2cc190SJeff Kirsher 		msleep(1);
1095a2cc190SJeff Kirsher 	} while (time_before(jiffies, end));
1105a2cc190SJeff Kirsher 
1115a2cc190SJeff Kirsher 	if (sem) {
1125a2cc190SJeff Kirsher 		mlx4_err(dev, "Failed to obtain HW semaphore, aborting\n");
1135a2cc190SJeff Kirsher 		err = -EAGAIN;
1145a2cc190SJeff Kirsher 		iounmap(reset);
1155a2cc190SJeff Kirsher 		goto out;
1165a2cc190SJeff Kirsher 	}
1175a2cc190SJeff Kirsher 
1185a2cc190SJeff Kirsher 	/* actually hit reset */
1195a2cc190SJeff Kirsher 	writel(MLX4_RESET_VALUE, reset + MLX4_RESET_OFFSET);
1205a2cc190SJeff Kirsher 	iounmap(reset);
1215a2cc190SJeff Kirsher 
1225a2cc190SJeff Kirsher 	/* Docs say to wait one second before accessing device */
1235a2cc190SJeff Kirsher 	msleep(1000);
1245a2cc190SJeff Kirsher 
1255a2cc190SJeff Kirsher 	end = jiffies + MLX4_RESET_TIMEOUT_JIFFIES;
1265a2cc190SJeff Kirsher 	do {
127*872bf2fbSYishai Hadas 		if (!pci_read_config_word(dev->persist->pdev, PCI_VENDOR_ID,
128*872bf2fbSYishai Hadas 					  &vendor) && vendor != 0xffff)
1295a2cc190SJeff Kirsher 			break;
1305a2cc190SJeff Kirsher 
1315a2cc190SJeff Kirsher 		msleep(1);
1325a2cc190SJeff Kirsher 	} while (time_before(jiffies, end));
1335a2cc190SJeff Kirsher 
1345a2cc190SJeff Kirsher 	if (vendor == 0xffff) {
1355a2cc190SJeff Kirsher 		err = -ENODEV;
1361a91de28SJoe Perches 		mlx4_err(dev, "PCI device did not come back after reset, aborting\n");
1375a2cc190SJeff Kirsher 		goto out;
1385a2cc190SJeff Kirsher 	}
1395a2cc190SJeff Kirsher 
1405a2cc190SJeff Kirsher 	/* Now restore the PCI headers */
1415a2cc190SJeff Kirsher 	if (pcie_cap) {
1425a2cc190SJeff Kirsher 		devctl = hca_header[(pcie_cap + PCI_EXP_DEVCTL) / 4];
143*872bf2fbSYishai Hadas 		if (pcie_capability_write_word(dev->persist->pdev,
144*872bf2fbSYishai Hadas 					       PCI_EXP_DEVCTL,
1455a2cc190SJeff Kirsher 					       devctl)) {
1465a2cc190SJeff Kirsher 			err = -ENODEV;
1471a91de28SJoe Perches 			mlx4_err(dev, "Couldn't restore HCA PCI Express Device Control register, aborting\n");
1485a2cc190SJeff Kirsher 			goto out;
1495a2cc190SJeff Kirsher 		}
1505a2cc190SJeff Kirsher 		linkctl = hca_header[(pcie_cap + PCI_EXP_LNKCTL) / 4];
151*872bf2fbSYishai Hadas 		if (pcie_capability_write_word(dev->persist->pdev,
152*872bf2fbSYishai Hadas 					       PCI_EXP_LNKCTL,
1535a2cc190SJeff Kirsher 					       linkctl)) {
1545a2cc190SJeff Kirsher 			err = -ENODEV;
1551a91de28SJoe Perches 			mlx4_err(dev, "Couldn't restore HCA PCI Express Link control register, aborting\n");
1565a2cc190SJeff Kirsher 			goto out;
1575a2cc190SJeff Kirsher 		}
1585a2cc190SJeff Kirsher 	}
1595a2cc190SJeff Kirsher 
1605a2cc190SJeff Kirsher 	for (i = 0; i < 16; ++i) {
1615a2cc190SJeff Kirsher 		if (i * 4 == PCI_COMMAND)
1625a2cc190SJeff Kirsher 			continue;
1635a2cc190SJeff Kirsher 
164*872bf2fbSYishai Hadas 		if (pci_write_config_dword(dev->persist->pdev, i * 4,
165*872bf2fbSYishai Hadas 					   hca_header[i])) {
1665a2cc190SJeff Kirsher 			err = -ENODEV;
1671a91de28SJoe Perches 			mlx4_err(dev, "Couldn't restore HCA reg %x, aborting\n",
1681a91de28SJoe Perches 				 i);
1695a2cc190SJeff Kirsher 			goto out;
1705a2cc190SJeff Kirsher 		}
1715a2cc190SJeff Kirsher 	}
1725a2cc190SJeff Kirsher 
173*872bf2fbSYishai Hadas 	if (pci_write_config_dword(dev->persist->pdev, PCI_COMMAND,
1745a2cc190SJeff Kirsher 				   hca_header[PCI_COMMAND / 4])) {
1755a2cc190SJeff Kirsher 		err = -ENODEV;
1761a91de28SJoe Perches 		mlx4_err(dev, "Couldn't restore HCA COMMAND, aborting\n");
1775a2cc190SJeff Kirsher 		goto out;
1785a2cc190SJeff Kirsher 	}
1795a2cc190SJeff Kirsher 
1805a2cc190SJeff Kirsher out:
1815a2cc190SJeff Kirsher 	kfree(hca_header);
1825a2cc190SJeff Kirsher 
1835a2cc190SJeff Kirsher 	return err;
1845a2cc190SJeff Kirsher }
185