18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2006, 2007 Cisco Systems, Inc.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
68c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
98c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
128c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
138c2ecf20Sopenharmony_ci *     conditions are met:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
168c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
178c2ecf20Sopenharmony_ci *        disclaimer.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
208c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
218c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
228c2ecf20Sopenharmony_ci *        provided with the distribution.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
258c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
268c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
278c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
288c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
298c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
308c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
318c2ecf20Sopenharmony_ci * SOFTWARE.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <linux/errno.h>
358c2ecf20Sopenharmony_ci#include <linux/pci.h>
368c2ecf20Sopenharmony_ci#include <linux/delay.h>
378c2ecf20Sopenharmony_ci#include <linux/slab.h>
388c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include "mlx4.h"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciint mlx4_reset(struct mlx4_dev *dev)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	void __iomem *reset;
458c2ecf20Sopenharmony_ci	u32 *hca_header = NULL;
468c2ecf20Sopenharmony_ci	int pcie_cap;
478c2ecf20Sopenharmony_ci	u16 devctl;
488c2ecf20Sopenharmony_ci	u16 linkctl;
498c2ecf20Sopenharmony_ci	u16 vendor;
508c2ecf20Sopenharmony_ci	unsigned long end;
518c2ecf20Sopenharmony_ci	u32 sem;
528c2ecf20Sopenharmony_ci	int i;
538c2ecf20Sopenharmony_ci	int err = 0;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define MLX4_RESET_BASE		0xf0000
568c2ecf20Sopenharmony_ci#define MLX4_RESET_SIZE		  0x400
578c2ecf20Sopenharmony_ci#define MLX4_SEM_OFFSET		  0x3fc
588c2ecf20Sopenharmony_ci#define MLX4_RESET_OFFSET	   0x10
598c2ecf20Sopenharmony_ci#define MLX4_RESET_VALUE	swab32(1)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define MLX4_SEM_TIMEOUT_JIFFIES	(10 * HZ)
628c2ecf20Sopenharmony_ci#define MLX4_RESET_TIMEOUT_JIFFIES	(2 * HZ)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/*
658c2ecf20Sopenharmony_ci	 * Reset the chip.  This is somewhat ugly because we have to
668c2ecf20Sopenharmony_ci	 * save off the PCI header before reset and then restore it
678c2ecf20Sopenharmony_ci	 * after the chip reboots.  We skip config space offsets 22
688c2ecf20Sopenharmony_ci	 * and 23 since those have a special meaning.
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Do we need to save off the full 4K PCI Express header?? */
728c2ecf20Sopenharmony_ci	hca_header = kmalloc(256, GFP_KERNEL);
738c2ecf20Sopenharmony_ci	if (!hca_header) {
748c2ecf20Sopenharmony_ci		err = -ENOMEM;
758c2ecf20Sopenharmony_ci		mlx4_err(dev, "Couldn't allocate memory to save HCA PCI header, aborting\n");
768c2ecf20Sopenharmony_ci		goto out;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	pcie_cap = pci_pcie_cap(dev->persist->pdev);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	for (i = 0; i < 64; ++i) {
828c2ecf20Sopenharmony_ci		if (i == 22 || i == 23)
838c2ecf20Sopenharmony_ci			continue;
848c2ecf20Sopenharmony_ci		if (pci_read_config_dword(dev->persist->pdev, i * 4,
858c2ecf20Sopenharmony_ci					  hca_header + i)) {
868c2ecf20Sopenharmony_ci			err = -ENODEV;
878c2ecf20Sopenharmony_ci			mlx4_err(dev, "Couldn't save HCA PCI header, aborting\n");
888c2ecf20Sopenharmony_ci			goto out;
898c2ecf20Sopenharmony_ci		}
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	reset = ioremap(pci_resource_start(dev->persist->pdev, 0) +
938c2ecf20Sopenharmony_ci			MLX4_RESET_BASE,
948c2ecf20Sopenharmony_ci			MLX4_RESET_SIZE);
958c2ecf20Sopenharmony_ci	if (!reset) {
968c2ecf20Sopenharmony_ci		err = -ENOMEM;
978c2ecf20Sopenharmony_ci		mlx4_err(dev, "Couldn't map HCA reset register, aborting\n");
988c2ecf20Sopenharmony_ci		goto out;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* grab HW semaphore to lock out flash updates */
1028c2ecf20Sopenharmony_ci	end = jiffies + MLX4_SEM_TIMEOUT_JIFFIES;
1038c2ecf20Sopenharmony_ci	do {
1048c2ecf20Sopenharmony_ci		sem = readl(reset + MLX4_SEM_OFFSET);
1058c2ecf20Sopenharmony_ci		if (!sem)
1068c2ecf20Sopenharmony_ci			break;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		msleep(1);
1098c2ecf20Sopenharmony_ci	} while (time_before(jiffies, end));
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (sem) {
1128c2ecf20Sopenharmony_ci		mlx4_err(dev, "Failed to obtain HW semaphore, aborting\n");
1138c2ecf20Sopenharmony_ci		err = -EAGAIN;
1148c2ecf20Sopenharmony_ci		iounmap(reset);
1158c2ecf20Sopenharmony_ci		goto out;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* actually hit reset */
1198c2ecf20Sopenharmony_ci	writel(MLX4_RESET_VALUE, reset + MLX4_RESET_OFFSET);
1208c2ecf20Sopenharmony_ci	iounmap(reset);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Docs say to wait one second before accessing device */
1238c2ecf20Sopenharmony_ci	msleep(1000);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	end = jiffies + MLX4_RESET_TIMEOUT_JIFFIES;
1268c2ecf20Sopenharmony_ci	do {
1278c2ecf20Sopenharmony_ci		if (!pci_read_config_word(dev->persist->pdev, PCI_VENDOR_ID,
1288c2ecf20Sopenharmony_ci					  &vendor) && vendor != 0xffff)
1298c2ecf20Sopenharmony_ci			break;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		msleep(1);
1328c2ecf20Sopenharmony_ci	} while (time_before(jiffies, end));
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (vendor == 0xffff) {
1358c2ecf20Sopenharmony_ci		err = -ENODEV;
1368c2ecf20Sopenharmony_ci		mlx4_err(dev, "PCI device did not come back after reset, aborting\n");
1378c2ecf20Sopenharmony_ci		goto out;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* Now restore the PCI headers */
1418c2ecf20Sopenharmony_ci	if (pcie_cap) {
1428c2ecf20Sopenharmony_ci		devctl = hca_header[(pcie_cap + PCI_EXP_DEVCTL) / 4];
1438c2ecf20Sopenharmony_ci		if (pcie_capability_write_word(dev->persist->pdev,
1448c2ecf20Sopenharmony_ci					       PCI_EXP_DEVCTL,
1458c2ecf20Sopenharmony_ci					       devctl)) {
1468c2ecf20Sopenharmony_ci			err = -ENODEV;
1478c2ecf20Sopenharmony_ci			mlx4_err(dev, "Couldn't restore HCA PCI Express Device Control register, aborting\n");
1488c2ecf20Sopenharmony_ci			goto out;
1498c2ecf20Sopenharmony_ci		}
1508c2ecf20Sopenharmony_ci		linkctl = hca_header[(pcie_cap + PCI_EXP_LNKCTL) / 4];
1518c2ecf20Sopenharmony_ci		if (pcie_capability_write_word(dev->persist->pdev,
1528c2ecf20Sopenharmony_ci					       PCI_EXP_LNKCTL,
1538c2ecf20Sopenharmony_ci					       linkctl)) {
1548c2ecf20Sopenharmony_ci			err = -ENODEV;
1558c2ecf20Sopenharmony_ci			mlx4_err(dev, "Couldn't restore HCA PCI Express Link control register, aborting\n");
1568c2ecf20Sopenharmony_ci			goto out;
1578c2ecf20Sopenharmony_ci		}
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	for (i = 0; i < 16; ++i) {
1618c2ecf20Sopenharmony_ci		if (i * 4 == PCI_COMMAND)
1628c2ecf20Sopenharmony_ci			continue;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		if (pci_write_config_dword(dev->persist->pdev, i * 4,
1658c2ecf20Sopenharmony_ci					   hca_header[i])) {
1668c2ecf20Sopenharmony_ci			err = -ENODEV;
1678c2ecf20Sopenharmony_ci			mlx4_err(dev, "Couldn't restore HCA reg %x, aborting\n",
1688c2ecf20Sopenharmony_ci				 i);
1698c2ecf20Sopenharmony_ci			goto out;
1708c2ecf20Sopenharmony_ci		}
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (pci_write_config_dword(dev->persist->pdev, PCI_COMMAND,
1748c2ecf20Sopenharmony_ci				   hca_header[PCI_COMMAND / 4])) {
1758c2ecf20Sopenharmony_ci		err = -ENODEV;
1768c2ecf20Sopenharmony_ci		mlx4_err(dev, "Couldn't restore HCA COMMAND, aborting\n");
1778c2ecf20Sopenharmony_ci		goto out;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciout:
1818c2ecf20Sopenharmony_ci	kfree(hca_header);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return err;
1848c2ecf20Sopenharmony_ci}
185